3 * Gerry Hamel, geh@ti.com, Texas Instruments
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #define TTYDBG(fmt,args...) serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
32 #define TTYDBG(fmt,args...) do{}while(0)
36 #define TTYERR(fmt,args...) serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
38 #define TTYERR(fmt,args...) do{}while(0)
42 * Buffers to hold input and output data
44 #define USBTTY_BUFFER_SIZE 256
45 static circbuf_t usbtty_input;
46 static circbuf_t usbtty_output;
52 static device_t usbttydev;
53 static struct usb_device_instance device_instance[1];
54 static struct usb_bus_instance bus_instance[1];
55 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
56 static struct usb_interface_instance interface_instance[NUM_INTERFACES];
57 static struct usb_alternate_instance alternate_instance[NUM_INTERFACES];
58 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1]; /* one extra for control endpoint */
61 * Static allocation of urbs
63 #define RECV_ENDPOINT 1
69 int usbtty_configured_flag = 0;
75 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
76 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
77 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
78 static u8 wstrSerial[2 + 2*(sizeof(CONFIG_USBD_SERIAL_NUMBER)-1)];
79 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
80 static u8 wstrInterface[2 + 2*(sizeof(CONFIG_USBD_INTERFACE_STR)-1)];
82 static struct usb_string_descriptor *usbtty_string_table[] = {
83 (struct usb_string_descriptor*)wstrLang,
84 (struct usb_string_descriptor*)wstrManufacturer,
85 (struct usb_string_descriptor*)wstrProduct,
86 (struct usb_string_descriptor*)wstrSerial,
87 (struct usb_string_descriptor*)wstrConfiguration,
88 (struct usb_string_descriptor*)wstrInterface
90 extern struct usb_string_descriptor **usb_strings; /* defined and used by omap1510_ep0.c */
92 static struct usb_device_descriptor device_descriptor = {
93 bLength: sizeof(struct usb_device_descriptor),
94 bDescriptorType: USB_DT_DEVICE,
95 bcdUSB: USB_BCD_VERSION,
96 bDeviceClass: USBTTY_DEVICE_CLASS,
97 bDeviceSubClass: USBTTY_DEVICE_SUBCLASS,
98 bDeviceProtocol: USBTTY_DEVICE_PROTOCOL,
99 bMaxPacketSize0: EP0_MAX_PACKET_SIZE,
100 idVendor: CONFIG_USBD_VENDORID,
101 idProduct: CONFIG_USBD_PRODUCTID,
102 bcdDevice: USBTTY_BCD_DEVICE,
103 iManufacturer: STR_MANUFACTURER,
104 iProduct: STR_PRODUCT,
105 iSerialNumber: STR_SERIAL,
106 bNumConfigurations: NUM_CONFIGS
108 static struct usb_configuration_descriptor config_descriptors[NUM_CONFIGS] = {
110 bLength: sizeof(struct usb_configuration_descriptor),
111 bDescriptorType: USB_DT_CONFIG,
112 wTotalLength: (sizeof(struct usb_configuration_descriptor)*NUM_CONFIGS) +
113 (sizeof(struct usb_interface_descriptor)*NUM_INTERFACES) +
114 (sizeof(struct usb_endpoint_descriptor)*NUM_ENDPOINTS),
115 bNumInterfaces: NUM_INTERFACES,
116 bConfigurationValue: 1,
117 iConfiguration: STR_CONFIG,
118 bmAttributes: BMATTRIBUTE_SELF_POWERED | BMATTRIBUTE_RESERVED,
119 bMaxPower: USBTTY_MAXPOWER
122 static struct usb_interface_descriptor interface_descriptors[NUM_INTERFACES] = {
124 bLength: sizeof(struct usb_interface_descriptor),
125 bDescriptorType: USB_DT_INTERFACE,
127 bAlternateSetting: 0,
128 bNumEndpoints: NUM_ENDPOINTS,
129 bInterfaceClass: USBTTY_INTERFACE_CLASS,
130 bInterfaceSubClass: USBTTY_INTERFACE_SUBCLASS,
131 bInterfaceProtocol: USBTTY_INTERFACE_PROTOCOL,
132 iInterface: STR_INTERFACE
135 static struct usb_endpoint_descriptor ep_descriptors[NUM_ENDPOINTS] = {
137 bLength: sizeof(struct usb_endpoint_descriptor),
138 bDescriptorType: USB_DT_ENDPOINT,
139 bEndpointAddress: CONFIG_USBD_SERIAL_OUT_ENDPOINT | USB_DIR_OUT,
140 bmAttributes: USB_ENDPOINT_XFER_BULK,
141 wMaxPacketSize: CONFIG_USBD_SERIAL_OUT_PKTSIZE,
145 bLength: sizeof(struct usb_endpoint_descriptor),
146 bDescriptorType: USB_DT_ENDPOINT,
147 bEndpointAddress: CONFIG_USBD_SERIAL_IN_ENDPOINT | USB_DIR_IN,
148 bmAttributes: USB_ENDPOINT_XFER_BULK,
149 wMaxPacketSize: CONFIG_USBD_SERIAL_IN_PKTSIZE,
153 bLength: sizeof(struct usb_endpoint_descriptor),
154 bDescriptorType: USB_DT_ENDPOINT,
155 bEndpointAddress: CONFIG_USBD_SERIAL_INT_ENDPOINT | USB_DIR_IN,
156 bmAttributes: USB_ENDPOINT_XFER_INT,
157 wMaxPacketSize: CONFIG_USBD_SERIAL_INT_PKTSIZE,
161 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS] = {
162 &(ep_descriptors[0]),
163 &(ep_descriptors[1]),
164 &(ep_descriptors[2]),
167 /* utility function for converting char* to wide string used by USB */
168 static void str2wide (char *str, u16 * wide)
172 for (i = 0; i < strlen (str) && str[i]; i++)
173 wide[i] = (u16) str[i];
179 static void usbtty_init_strings (void);
180 static void usbtty_init_instances (void);
181 static void usbtty_init_endpoints (void);
183 static void usbtty_event_handler (struct usb_device_instance *device,
184 usb_device_event_t event, int data);
185 static int usbtty_configured (void);
187 static int write_buffer (circbuf_t * buf);
188 static int fill_buffer (circbuf_t * buf);
190 void usbtty_poll (void);
191 static void pretend_interrupts (void);
195 * Test whether a character is in the RX buffer
197 int usbtty_tstc (void)
200 return (usbtty_input.size > 0);
204 * Read a single byte from the usb client port. Returns 1 on success, 0
205 * otherwise. When the function is succesfull, the character read is
206 * written into its argument c.
208 int usbtty_getc (void)
212 while (usbtty_input.size <= 0) {
216 buf_pop (&usbtty_input, &c, 1);
221 * Output a single byte to the usb client port.
223 void usbtty_putc (const char c)
225 buf_push (&usbtty_output, &c, 1);
226 /* If \n, also do \r */
228 buf_push (&usbtty_output, "\r", 1);
230 /* Poll at end to handle new data... */
231 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
237 /* usbtty_puts() helper function for finding the next '\n' in a string */
238 static int next_nl_pos (const char *s)
242 for (i = 0; s[i] != '\0'; i++) {
250 * Output a string to the usb client port.
252 static void __usbtty_puts (const char *str, int len)
254 int maxlen = usbtty_output.totalsize;
257 /* break str into chunks < buffer size, if needed */
259 space = maxlen - usbtty_output.size;
261 /* Empty buffer here, if needed, to ensure space... */
263 write_buffer (&usbtty_output);
264 space = maxlen - usbtty_output.size;
266 space = len; /* allow old data to be overwritten. */
270 n = MIN (space, MIN (len, maxlen));
271 buf_push (&usbtty_output, str, n);
278 void usbtty_puts (const char *str)
281 int len = strlen (str);
283 /* add '\r' for each '\n' */
285 n = next_nl_pos (str);
287 if (str[n] == '\n') {
288 __usbtty_puts (str, n + 1);
289 __usbtty_puts ("\r", 1);
293 /* No \n found. All done. */
294 __usbtty_puts (str, n);
299 /* Poll at end to handle new data... */
304 * Initialize the usb client port.
307 int drv_usbtty_init (void)
312 /* prepare buffers... */
313 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
314 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
316 /* Now, set up USB controller and infrastructure */
317 udc_init (); /* Basic USB initialization */
319 usbtty_init_strings ();
320 usbtty_init_instances ();
322 udc_startup_events (device_instance); /* Enable our device, initialize udc pointers */
323 udc_connect (); /* Enable pullup for host detection */
325 usbtty_init_endpoints ();
327 /* Device initialization */
328 memset (&usbttydev, 0, sizeof (usbttydev));
330 strcpy (usbttydev.name, "usbtty");
331 usbttydev.ext = 0; /* No extensions */
332 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
333 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
334 usbttydev.getc = usbtty_getc; /* 'getc' function */
335 usbttydev.putc = usbtty_putc; /* 'putc' function */
336 usbttydev.puts = usbtty_puts; /* 'puts' function */
338 rc = device_register (&usbttydev);
340 return (rc == 0) ? 1 : rc;
343 static void usbtty_init_strings (void)
345 struct usb_string_descriptor *string;
347 string = (struct usb_string_descriptor *) wstrManufacturer;
348 string->bLength = sizeof (wstrManufacturer);
349 string->bDescriptorType = USB_DT_STRING;
350 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
352 string = (struct usb_string_descriptor *) wstrProduct;
353 string->bLength = sizeof (wstrProduct);
354 string->bDescriptorType = USB_DT_STRING;
355 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
357 string = (struct usb_string_descriptor *) wstrSerial;
358 string->bLength = sizeof (wstrSerial);
359 string->bDescriptorType = USB_DT_STRING;
360 str2wide (CONFIG_USBD_SERIAL_NUMBER, string->wData);
362 string = (struct usb_string_descriptor *) wstrConfiguration;
363 string->bLength = sizeof (wstrConfiguration);
364 string->bDescriptorType = USB_DT_STRING;
365 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
367 string = (struct usb_string_descriptor *) wstrInterface;
368 string->bLength = sizeof (wstrInterface);
369 string->bDescriptorType = USB_DT_STRING;
370 str2wide (CONFIG_USBD_INTERFACE_STR, string->wData);
372 /* Now, initialize the string table for ep0 handling */
373 usb_strings = usbtty_string_table;
376 static void usbtty_init_instances (void)
380 /* initialize device instance */
381 memset (device_instance, 0, sizeof (struct usb_device_instance));
382 device_instance->device_state = STATE_INIT;
383 device_instance->device_descriptor = &device_descriptor;
384 device_instance->event = usbtty_event_handler;
385 device_instance->bus = bus_instance;
386 device_instance->configurations = NUM_CONFIGS;
387 device_instance->configuration_instance_array = config_instance;
389 /* initialize bus instance */
390 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
391 bus_instance->device = device_instance;
392 bus_instance->endpoint_array = endpoint_instance;
393 bus_instance->max_endpoints = 1;
394 bus_instance->maxpacketsize = 64;
395 bus_instance->serial_number_str = CONFIG_USBD_SERIAL_NUMBER;
397 /* configuration instance */
398 memset (config_instance, 0,
399 sizeof (struct usb_configuration_instance));
400 config_instance->interfaces = NUM_INTERFACES;
401 config_instance->configuration_descriptor = config_descriptors;
402 config_instance->interface_instance_array = interface_instance;
404 /* interface instance */
405 memset (interface_instance, 0,
406 sizeof (struct usb_interface_instance));
407 interface_instance->alternates = 1;
408 interface_instance->alternates_instance_array = alternate_instance;
410 /* alternates instance */
411 memset (alternate_instance, 0,
412 sizeof (struct usb_alternate_instance));
413 alternate_instance->interface_descriptor = interface_descriptors;
414 alternate_instance->endpoints = NUM_ENDPOINTS;
415 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
417 /* endpoint instances */
418 memset (&endpoint_instance[0], 0,
419 sizeof (struct usb_endpoint_instance));
420 endpoint_instance[0].endpoint_address = 0;
421 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
422 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
423 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
424 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
425 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
427 for (i = 1; i <= NUM_ENDPOINTS; i++) {
428 memset (&endpoint_instance[i], 0,
429 sizeof (struct usb_endpoint_instance));
431 endpoint_instance[i].endpoint_address =
432 ep_descriptors[i - 1].bEndpointAddress;
434 endpoint_instance[i].rcv_packetSize =
435 ep_descriptors[i - 1].wMaxPacketSize;
436 endpoint_instance[i].rcv_attributes =
437 ep_descriptors[i - 1].bmAttributes;
439 endpoint_instance[i].tx_packetSize =
440 ep_descriptors[i - 1].wMaxPacketSize;
441 endpoint_instance[i].tx_attributes =
442 ep_descriptors[i - 1].bmAttributes;
444 urb_link_init (&endpoint_instance[i].rcv);
445 urb_link_init (&endpoint_instance[i].rdy);
446 urb_link_init (&endpoint_instance[i].tx);
447 urb_link_init (&endpoint_instance[i].done);
449 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
450 endpoint_instance[i].tx_urb =
451 usbd_alloc_urb (device_instance,
452 &endpoint_instance[i]);
454 endpoint_instance[i].rcv_urb =
455 usbd_alloc_urb (device_instance,
456 &endpoint_instance[i]);
460 static void usbtty_init_endpoints (void)
464 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
465 for (i = 0; i <= NUM_ENDPOINTS; i++) {
466 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
471 /*********************************************************************************/
473 static struct urb *next_urb (struct usb_device_instance *device,
474 struct usb_endpoint_instance *endpoint)
476 struct urb *current_urb = NULL;
479 /* If there's a queue, then we should add to the last urb */
480 if (!endpoint->tx_queue) {
481 current_urb = endpoint->tx_urb;
483 /* Last urb from tx chain */
485 p2surround (struct urb, link, endpoint->tx.prev);
488 /* Make sure this one has enough room */
489 space = current_urb->buffer_length - current_urb->actual_length;
492 } else { /* No space here */
493 /* First look at done list */
494 current_urb = first_urb_detached (&endpoint->done);
496 current_urb = usbd_alloc_urb (device, endpoint);
499 urb_append (&endpoint->tx, current_urb);
500 endpoint->tx_queue++;
505 static int write_buffer (circbuf_t * buf)
507 if (!usbtty_configured ()) {
513 struct usb_endpoint_instance *endpoint =
514 &endpoint_instance[TX_ENDPOINT];
515 struct urb *current_urb = NULL;
522 /* Break buffer into urb sized pieces, and link each to the endpoint */
523 while (buf->size > 0) {
524 current_urb = next_urb (device_instance, endpoint);
526 TTYERR ("current_urb is NULL, buf->size %d\n",
531 dest = current_urb->buffer +
532 current_urb->actual_length;
535 current_urb->buffer_length -
536 current_urb->actual_length;
537 popnum = MIN (space_avail, buf->size);
541 popped = buf_pop (buf, dest, popnum);
544 current_urb->actual_length += popped;
547 /* If endpoint->last == 0, then transfers have not started on this endpoint */
548 if (endpoint->last == 0) {
549 udc_endpoint_write (endpoint);
554 } /* end if tx_urb */
559 static int fill_buffer (circbuf_t * buf)
561 struct usb_endpoint_instance *endpoint =
562 &endpoint_instance[RECV_ENDPOINT];
564 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
565 unsigned int nb = endpoint->rcv_urb->actual_length;
566 char *src = (char *) endpoint->rcv_urb->buffer;
568 buf_push (buf, src, nb);
569 endpoint->rcv_urb->actual_length = 0;
577 static int usbtty_configured (void)
579 return usbtty_configured_flag;
582 /*********************************************************************************/
584 static void usbtty_event_handler (struct usb_device_instance *device,
585 usb_device_event_t event, int data)
589 case DEVICE_BUS_INACTIVE:
590 usbtty_configured_flag = 0;
592 case DEVICE_CONFIGURED:
593 usbtty_configured_flag = 1;
596 case DEVICE_ADDRESS_ASSIGNED:
597 usbtty_init_endpoints ();
604 /*********************************************************************************/
608 * Since interrupt handling has not yet been implemented, we use this function
609 * to handle polling. This is called by the tstc,getc,putc,puts routines to
610 * update the USB state.
612 void usbtty_poll (void)
614 /* New interrupts? */
615 pretend_interrupts ();
617 /* Write any output data to host buffer (do this before checking interrupts to avoid missing one) */
618 if (usbtty_configured ()) {
619 write_buffer (&usbtty_output);
622 /* New interrupts? */
623 pretend_interrupts ();
625 /* Check for new data from host.. (do this after checking interrupts to get latest data) */
626 if (usbtty_configured ()) {
627 fill_buffer (&usbtty_input);
630 /* New interrupts? */
631 pretend_interrupts ();
634 static void pretend_interrupts (void)
636 /* Loop while we have interrupts.
637 * If we don't do this, the input chain
638 * polling delay is likely to miss
641 while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
642 /* Handle any new IRQs */
644 omap1510_udc_noniso_irq ();