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 char serial_number[16];
80 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
81 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
82 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
83 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
84 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
85 static u8 wstrInterface[2 + 2*(sizeof(CONFIG_USBD_INTERFACE_STR)-1)];
87 static struct usb_string_descriptor *usbtty_string_table[] = {
88 (struct usb_string_descriptor*)wstrLang,
89 (struct usb_string_descriptor*)wstrManufacturer,
90 (struct usb_string_descriptor*)wstrProduct,
91 (struct usb_string_descriptor*)wstrSerial,
92 (struct usb_string_descriptor*)wstrConfiguration,
93 (struct usb_string_descriptor*)wstrInterface
95 extern struct usb_string_descriptor **usb_strings; /* defined and used by omap1510_ep0.c */
97 static struct usb_device_descriptor device_descriptor = {
98 bLength: sizeof(struct usb_device_descriptor),
99 bDescriptorType: USB_DT_DEVICE,
100 bcdUSB: USB_BCD_VERSION,
101 bDeviceClass: USBTTY_DEVICE_CLASS,
102 bDeviceSubClass: USBTTY_DEVICE_SUBCLASS,
103 bDeviceProtocol: USBTTY_DEVICE_PROTOCOL,
104 bMaxPacketSize0: EP0_MAX_PACKET_SIZE,
105 idVendor: CONFIG_USBD_VENDORID,
106 idProduct: CONFIG_USBD_PRODUCTID,
107 bcdDevice: USBTTY_BCD_DEVICE,
108 iManufacturer: STR_MANUFACTURER,
109 iProduct: STR_PRODUCT,
110 iSerialNumber: STR_SERIAL,
111 bNumConfigurations: NUM_CONFIGS
113 static struct usb_configuration_descriptor config_descriptors[NUM_CONFIGS] = {
115 bLength: sizeof(struct usb_configuration_descriptor),
116 bDescriptorType: USB_DT_CONFIG,
117 wTotalLength: (sizeof(struct usb_configuration_descriptor)*NUM_CONFIGS) +
118 (sizeof(struct usb_interface_descriptor)*NUM_INTERFACES) +
119 (sizeof(struct usb_endpoint_descriptor)*NUM_ENDPOINTS),
120 bNumInterfaces: NUM_INTERFACES,
121 bConfigurationValue: 1,
122 iConfiguration: STR_CONFIG,
123 bmAttributes: BMATTRIBUTE_SELF_POWERED | BMATTRIBUTE_RESERVED,
124 bMaxPower: USBTTY_MAXPOWER
127 static struct usb_interface_descriptor interface_descriptors[NUM_INTERFACES] = {
129 bLength: sizeof(struct usb_interface_descriptor),
130 bDescriptorType: USB_DT_INTERFACE,
132 bAlternateSetting: 0,
133 bNumEndpoints: NUM_ENDPOINTS,
134 bInterfaceClass: USBTTY_INTERFACE_CLASS,
135 bInterfaceSubClass: USBTTY_INTERFACE_SUBCLASS,
136 bInterfaceProtocol: USBTTY_INTERFACE_PROTOCOL,
137 iInterface: STR_INTERFACE
140 static struct usb_endpoint_descriptor ep_descriptors[NUM_ENDPOINTS] = {
142 bLength: sizeof(struct usb_endpoint_descriptor),
143 bDescriptorType: USB_DT_ENDPOINT,
144 bEndpointAddress: CONFIG_USBD_SERIAL_OUT_ENDPOINT | USB_DIR_OUT,
145 bmAttributes: USB_ENDPOINT_XFER_BULK,
146 wMaxPacketSize: CONFIG_USBD_SERIAL_OUT_PKTSIZE,
150 bLength: sizeof(struct usb_endpoint_descriptor),
151 bDescriptorType: USB_DT_ENDPOINT,
152 bEndpointAddress: CONFIG_USBD_SERIAL_IN_ENDPOINT | USB_DIR_IN,
153 bmAttributes: USB_ENDPOINT_XFER_BULK,
154 wMaxPacketSize: CONFIG_USBD_SERIAL_IN_PKTSIZE,
158 bLength: sizeof(struct usb_endpoint_descriptor),
159 bDescriptorType: USB_DT_ENDPOINT,
160 bEndpointAddress: CONFIG_USBD_SERIAL_INT_ENDPOINT | USB_DIR_IN,
161 bmAttributes: USB_ENDPOINT_XFER_INT,
162 wMaxPacketSize: CONFIG_USBD_SERIAL_INT_PKTSIZE,
166 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS] = {
167 &(ep_descriptors[0]),
168 &(ep_descriptors[1]),
169 &(ep_descriptors[2]),
172 /* utility function for converting char* to wide string used by USB */
173 static void str2wide (char *str, u16 * wide)
177 for (i = 0; i < strlen (str) && str[i]; i++)
178 wide[i] = (u16) str[i];
184 static void usbtty_init_strings (void);
185 static void usbtty_init_instances (void);
186 static void usbtty_init_endpoints (void);
188 static void usbtty_event_handler (struct usb_device_instance *device,
189 usb_device_event_t event, int data);
190 static int usbtty_configured (void);
192 static int write_buffer (circbuf_t * buf);
193 static int fill_buffer (circbuf_t * buf);
195 void usbtty_poll (void);
196 static void pretend_interrupts (void);
200 * Test whether a character is in the RX buffer
202 int usbtty_tstc (void)
205 return (usbtty_input.size > 0);
209 * Read a single byte from the usb client port. Returns 1 on success, 0
210 * otherwise. When the function is succesfull, the character read is
211 * written into its argument c.
213 int usbtty_getc (void)
217 while (usbtty_input.size <= 0) {
221 buf_pop (&usbtty_input, &c, 1);
226 * Output a single byte to the usb client port.
228 void usbtty_putc (const char c)
230 buf_push (&usbtty_output, &c, 1);
231 /* If \n, also do \r */
233 buf_push (&usbtty_output, "\r", 1);
235 /* Poll at end to handle new data... */
236 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
242 /* usbtty_puts() helper function for finding the next '\n' in a string */
243 static int next_nl_pos (const char *s)
247 for (i = 0; s[i] != '\0'; i++) {
255 * Output a string to the usb client port.
257 static void __usbtty_puts (const char *str, int len)
259 int maxlen = usbtty_output.totalsize;
262 /* break str into chunks < buffer size, if needed */
264 space = maxlen - usbtty_output.size;
266 /* Empty buffer here, if needed, to ensure space... */
268 write_buffer (&usbtty_output);
269 space = maxlen - usbtty_output.size;
271 space = len; /* allow old data to be overwritten. */
275 n = MIN (space, MIN (len, maxlen));
276 buf_push (&usbtty_output, str, n);
283 void usbtty_puts (const char *str)
286 int len = strlen (str);
288 /* add '\r' for each '\n' */
290 n = next_nl_pos (str);
292 if (str[n] == '\n') {
293 __usbtty_puts (str, n + 1);
294 __usbtty_puts ("\r", 1);
298 /* No \n found. All done. */
299 __usbtty_puts (str, n);
304 /* Poll at end to handle new data... */
309 * Initialize the usb client port.
312 int drv_usbtty_init (void)
318 if (!(sn = getenv("serial#"))) {
322 if (snlen > sizeof(serial_number) - 1) {
323 printf ("Warning: serial number %s is too long (%d > %d)\n",
324 sn, snlen, sizeof(serial_number) - 1);
325 snlen = sizeof(serial_number) - 1;
327 memcpy (serial_number, sn, snlen);
328 serial_number[snlen] = '\0';
330 /* prepare buffers... */
331 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
332 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
334 /* Now, set up USB controller and infrastructure */
335 udc_init (); /* Basic USB initialization */
337 usbtty_init_strings ();
338 usbtty_init_instances ();
340 udc_startup_events (device_instance); /* Enable our device, initialize udc pointers */
341 udc_connect (); /* Enable pullup for host detection */
343 usbtty_init_endpoints ();
345 /* Device initialization */
346 memset (&usbttydev, 0, sizeof (usbttydev));
348 strcpy (usbttydev.name, "usbtty");
349 usbttydev.ext = 0; /* No extensions */
350 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
351 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
352 usbttydev.getc = usbtty_getc; /* 'getc' function */
353 usbttydev.putc = usbtty_putc; /* 'putc' function */
354 usbttydev.puts = usbtty_puts; /* 'puts' function */
356 rc = device_register (&usbttydev);
358 return (rc == 0) ? 1 : rc;
361 static void usbtty_init_strings (void)
363 struct usb_string_descriptor *string;
365 string = (struct usb_string_descriptor *) wstrManufacturer;
366 string->bLength = sizeof (wstrManufacturer);
367 string->bDescriptorType = USB_DT_STRING;
368 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
370 string = (struct usb_string_descriptor *) wstrProduct;
371 string->bLength = sizeof (wstrProduct);
372 string->bDescriptorType = USB_DT_STRING;
373 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
375 string = (struct usb_string_descriptor *) wstrSerial;
376 string->bLength = 2 + 2*strlen(serial_number);
377 string->bDescriptorType = USB_DT_STRING;
378 str2wide (serial_number, string->wData);
380 string = (struct usb_string_descriptor *) wstrConfiguration;
381 string->bLength = sizeof (wstrConfiguration);
382 string->bDescriptorType = USB_DT_STRING;
383 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
385 string = (struct usb_string_descriptor *) wstrInterface;
386 string->bLength = sizeof (wstrInterface);
387 string->bDescriptorType = USB_DT_STRING;
388 str2wide (CONFIG_USBD_INTERFACE_STR, string->wData);
390 /* Now, initialize the string table for ep0 handling */
391 usb_strings = usbtty_string_table;
394 static void usbtty_init_instances (void)
398 /* initialize device instance */
399 memset (device_instance, 0, sizeof (struct usb_device_instance));
400 device_instance->device_state = STATE_INIT;
401 device_instance->device_descriptor = &device_descriptor;
402 device_instance->event = usbtty_event_handler;
403 device_instance->bus = bus_instance;
404 device_instance->configurations = NUM_CONFIGS;
405 device_instance->configuration_instance_array = config_instance;
407 /* initialize bus instance */
408 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
409 bus_instance->device = device_instance;
410 bus_instance->endpoint_array = endpoint_instance;
411 bus_instance->max_endpoints = 1;
412 bus_instance->maxpacketsize = 64;
413 bus_instance->serial_number_str = serial_number;
415 /* configuration instance */
416 memset (config_instance, 0,
417 sizeof (struct usb_configuration_instance));
418 config_instance->interfaces = NUM_INTERFACES;
419 config_instance->configuration_descriptor = config_descriptors;
420 config_instance->interface_instance_array = interface_instance;
422 /* interface instance */
423 memset (interface_instance, 0,
424 sizeof (struct usb_interface_instance));
425 interface_instance->alternates = 1;
426 interface_instance->alternates_instance_array = alternate_instance;
428 /* alternates instance */
429 memset (alternate_instance, 0,
430 sizeof (struct usb_alternate_instance));
431 alternate_instance->interface_descriptor = interface_descriptors;
432 alternate_instance->endpoints = NUM_ENDPOINTS;
433 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
435 /* endpoint instances */
436 memset (&endpoint_instance[0], 0,
437 sizeof (struct usb_endpoint_instance));
438 endpoint_instance[0].endpoint_address = 0;
439 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
440 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
441 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
442 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
443 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
445 for (i = 1; i <= NUM_ENDPOINTS; i++) {
446 memset (&endpoint_instance[i], 0,
447 sizeof (struct usb_endpoint_instance));
449 endpoint_instance[i].endpoint_address =
450 ep_descriptors[i - 1].bEndpointAddress;
452 endpoint_instance[i].rcv_packetSize =
453 ep_descriptors[i - 1].wMaxPacketSize;
454 endpoint_instance[i].rcv_attributes =
455 ep_descriptors[i - 1].bmAttributes;
457 endpoint_instance[i].tx_packetSize =
458 ep_descriptors[i - 1].wMaxPacketSize;
459 endpoint_instance[i].tx_attributes =
460 ep_descriptors[i - 1].bmAttributes;
462 urb_link_init (&endpoint_instance[i].rcv);
463 urb_link_init (&endpoint_instance[i].rdy);
464 urb_link_init (&endpoint_instance[i].tx);
465 urb_link_init (&endpoint_instance[i].done);
467 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
468 endpoint_instance[i].tx_urb =
469 usbd_alloc_urb (device_instance,
470 &endpoint_instance[i]);
472 endpoint_instance[i].rcv_urb =
473 usbd_alloc_urb (device_instance,
474 &endpoint_instance[i]);
478 static void usbtty_init_endpoints (void)
482 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
483 for (i = 0; i <= NUM_ENDPOINTS; i++) {
484 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
489 /*********************************************************************************/
491 static struct urb *next_urb (struct usb_device_instance *device,
492 struct usb_endpoint_instance *endpoint)
494 struct urb *current_urb = NULL;
497 /* If there's a queue, then we should add to the last urb */
498 if (!endpoint->tx_queue) {
499 current_urb = endpoint->tx_urb;
501 /* Last urb from tx chain */
503 p2surround (struct urb, link, endpoint->tx.prev);
506 /* Make sure this one has enough room */
507 space = current_urb->buffer_length - current_urb->actual_length;
510 } else { /* No space here */
511 /* First look at done list */
512 current_urb = first_urb_detached (&endpoint->done);
514 current_urb = usbd_alloc_urb (device, endpoint);
517 urb_append (&endpoint->tx, current_urb);
518 endpoint->tx_queue++;
523 static int write_buffer (circbuf_t * buf)
525 if (!usbtty_configured ()) {
531 struct usb_endpoint_instance *endpoint =
532 &endpoint_instance[TX_ENDPOINT];
533 struct urb *current_urb = NULL;
540 /* Break buffer into urb sized pieces, and link each to the endpoint */
541 while (buf->size > 0) {
542 current_urb = next_urb (device_instance, endpoint);
544 TTYERR ("current_urb is NULL, buf->size %d\n",
549 dest = current_urb->buffer +
550 current_urb->actual_length;
553 current_urb->buffer_length -
554 current_urb->actual_length;
555 popnum = MIN (space_avail, buf->size);
559 popped = buf_pop (buf, dest, popnum);
562 current_urb->actual_length += popped;
565 /* If endpoint->last == 0, then transfers have not started on this endpoint */
566 if (endpoint->last == 0) {
567 udc_endpoint_write (endpoint);
572 } /* end if tx_urb */
577 static int fill_buffer (circbuf_t * buf)
579 struct usb_endpoint_instance *endpoint =
580 &endpoint_instance[RECV_ENDPOINT];
582 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
583 unsigned int nb = endpoint->rcv_urb->actual_length;
584 char *src = (char *) endpoint->rcv_urb->buffer;
586 buf_push (buf, src, nb);
587 endpoint->rcv_urb->actual_length = 0;
595 static int usbtty_configured (void)
597 return usbtty_configured_flag;
600 /*********************************************************************************/
602 static void usbtty_event_handler (struct usb_device_instance *device,
603 usb_device_event_t event, int data)
607 case DEVICE_BUS_INACTIVE:
608 usbtty_configured_flag = 0;
610 case DEVICE_CONFIGURED:
611 usbtty_configured_flag = 1;
614 case DEVICE_ADDRESS_ASSIGNED:
615 usbtty_init_endpoints ();
622 /*********************************************************************************/
626 * Since interrupt handling has not yet been implemented, we use this function
627 * to handle polling. This is called by the tstc,getc,putc,puts routines to
628 * update the USB state.
630 void usbtty_poll (void)
632 /* New interrupts? */
633 pretend_interrupts ();
635 /* Write any output data to host buffer (do this before checking interrupts to avoid missing one) */
636 if (usbtty_configured ()) {
637 write_buffer (&usbtty_output);
640 /* New interrupts? */
641 pretend_interrupts ();
643 /* Check for new data from host.. (do this after checking interrupts to get latest data) */
644 if (usbtty_configured ()) {
645 fill_buffer (&usbtty_input);
648 /* New interrupts? */
649 pretend_interrupts ();
652 static void pretend_interrupts (void)
654 /* Loop while we have interrupts.
655 * If we don't do this, the input chain
656 * polling delay is likely to miss
659 while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
660 /* Handle any new IRQs */
662 omap1510_udc_noniso_irq ();