]> git.sur5r.net Git - u-boot/blob - drivers/serial/usbtty.c
Merge branch 'master' of git://git.denx.de/u-boot-i2c
[u-boot] / drivers / serial / usbtty.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Gerry Hamel, geh@ti.com, Texas Instruments
5  *
6  * (C) Copyright 2006
7  * Bryan O'Donoghue, bodonoghue@codehermit.ie
8  */
9
10 #include <common.h>
11 #include <config.h>
12 #include <circbuf.h>
13 #include <stdio_dev.h>
14 #include <asm/unaligned.h>
15 #include "usbtty.h"
16 #include "usb_cdc_acm.h"
17 #include "usbdescriptors.h"
18
19 #ifdef DEBUG
20 #define TTYDBG(fmt,args...)\
21         serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
22 #else
23 #define TTYDBG(fmt,args...) do{}while(0)
24 #endif
25
26 #if 1
27 #define TTYERR(fmt,args...)\
28         serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
29         __LINE__,##args)
30 #else
31 #define TTYERR(fmt,args...) do{}while(0)
32 #endif
33
34 /*
35  * Defines
36  */
37 #define NUM_CONFIGS    1
38 #define MAX_INTERFACES 2
39 #define NUM_ENDPOINTS  3
40 #define ACM_TX_ENDPOINT 3
41 #define ACM_RX_ENDPOINT 2
42 #define GSERIAL_TX_ENDPOINT 2
43 #define GSERIAL_RX_ENDPOINT 1
44 #define NUM_ACM_INTERFACES 2
45 #define NUM_GSERIAL_INTERFACES 1
46 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
47 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
48
49 /*
50  * Buffers to hold input and output data
51  */
52 #define USBTTY_BUFFER_SIZE 2048
53 static circbuf_t usbtty_input;
54 static circbuf_t usbtty_output;
55
56
57 /*
58  * Instance variables
59  */
60 static struct stdio_dev usbttydev;
61 static struct usb_device_instance device_instance[1];
62 static struct usb_bus_instance bus_instance[1];
63 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
64 static struct usb_interface_instance interface_instance[MAX_INTERFACES];
65 static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
66 /* one extra for control endpoint */
67 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
68
69 /*
70  * Global flag
71  */
72 int usbtty_configured_flag = 0;
73
74 /*
75  * Serial number
76  */
77 static char serial_number[16];
78
79
80 /*
81  * Descriptors, Strings, Local variables.
82  */
83
84 /* defined and used by gadget/ep0.c */
85 extern struct usb_string_descriptor **usb_strings;
86
87 /* Indicies, References */
88 static unsigned short rx_endpoint = 0;
89 static unsigned short tx_endpoint = 0;
90 static unsigned short interface_count = 0;
91 static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
92
93 /* USB Descriptor Strings */
94 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
95 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
96 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
97 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
98 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
99 static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
100 static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
101
102 /* Standard USB Data Structures */
103 static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
104 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
105 static struct usb_configuration_descriptor      *configuration_descriptor = 0;
106 static struct usb_device_descriptor device_descriptor = {
107         .bLength = sizeof(struct usb_device_descriptor),
108         .bDescriptorType =      USB_DT_DEVICE,
109         .bcdUSB =               cpu_to_le16(USB_BCD_VERSION),
110         .bDeviceSubClass =      0x00,
111         .bDeviceProtocol =      0x00,
112         .bMaxPacketSize0 =      EP0_MAX_PACKET_SIZE,
113         .idVendor =             cpu_to_le16(CONFIG_USBD_VENDORID),
114         .bcdDevice =            cpu_to_le16(USBTTY_BCD_DEVICE),
115         .iManufacturer =        STR_MANUFACTURER,
116         .iProduct =             STR_PRODUCT,
117         .iSerialNumber =        STR_SERIAL,
118         .bNumConfigurations =   NUM_CONFIGS
119 };
120
121
122 #if defined(CONFIG_USBD_HS)
123 static struct usb_qualifier_descriptor qualifier_descriptor = {
124         .bLength = sizeof(struct usb_qualifier_descriptor),
125         .bDescriptorType =      USB_DT_QUAL,
126         .bcdUSB =               cpu_to_le16(USB_BCD_VERSION),
127         .bDeviceClass =         COMMUNICATIONS_DEVICE_CLASS,
128         .bDeviceSubClass =      0x00,
129         .bDeviceProtocol =      0x00,
130         .bMaxPacketSize0 =      EP0_MAX_PACKET_SIZE,
131         .bNumConfigurations =   NUM_CONFIGS
132 };
133 #endif
134
135 /*
136  * Static CDC ACM specific descriptors
137  */
138
139 struct acm_config_desc {
140         struct usb_configuration_descriptor configuration_desc;
141
142         /* Master Interface */
143         struct usb_interface_descriptor interface_desc;
144
145         struct usb_class_header_function_descriptor usb_class_header;
146         struct usb_class_call_management_descriptor usb_class_call_mgt;
147         struct usb_class_abstract_control_descriptor usb_class_acm;
148         struct usb_class_union_function_descriptor usb_class_union;
149         struct usb_endpoint_descriptor notification_endpoint;
150
151         /* Slave Interface */
152         struct usb_interface_descriptor data_class_interface;
153         struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
154 } __attribute__((packed));
155
156 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
157         {
158                 .configuration_desc ={
159                         .bLength =
160                                 sizeof(struct usb_configuration_descriptor),
161                         .bDescriptorType = USB_DT_CONFIG,
162                         .wTotalLength =
163                                 cpu_to_le16(sizeof(struct acm_config_desc)),
164                         .bNumInterfaces = NUM_ACM_INTERFACES,
165                         .bConfigurationValue = 1,
166                         .iConfiguration = STR_CONFIG,
167                         .bmAttributes =
168                                 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
169                         .bMaxPower = USBTTY_MAXPOWER
170                 },
171                 /* Interface 1 */
172                 .interface_desc = {
173                         .bLength  = sizeof(struct usb_interface_descriptor),
174                         .bDescriptorType = USB_DT_INTERFACE,
175                         .bInterfaceNumber = 0,
176                         .bAlternateSetting = 0,
177                         .bNumEndpoints = 0x01,
178                         .bInterfaceClass =
179                                 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
180                         .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
181                         .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
182                         .iInterface = STR_CTRL_INTERFACE,
183                 },
184                 .usb_class_header = {
185                         .bFunctionLength        =
186                                 sizeof(struct usb_class_header_function_descriptor),
187                         .bDescriptorType        = CS_INTERFACE,
188                         .bDescriptorSubtype     = USB_ST_HEADER,
189                         .bcdCDC = cpu_to_le16(110),
190                 },
191                 .usb_class_call_mgt = {
192                         .bFunctionLength        =
193                                 sizeof(struct usb_class_call_management_descriptor),
194                         .bDescriptorType        = CS_INTERFACE,
195                         .bDescriptorSubtype     = USB_ST_CMF,
196                         .bmCapabilities         = 0x00,
197                         .bDataInterface         = 0x01,
198                 },
199                 .usb_class_acm = {
200                         .bFunctionLength        =
201                                 sizeof(struct usb_class_abstract_control_descriptor),
202                         .bDescriptorType        = CS_INTERFACE,
203                         .bDescriptorSubtype     = USB_ST_ACMF,
204                         .bmCapabilities         = 0x00,
205                 },
206                 .usb_class_union = {
207                         .bFunctionLength        =
208                                 sizeof(struct usb_class_union_function_descriptor),
209                         .bDescriptorType        = CS_INTERFACE,
210                         .bDescriptorSubtype     = USB_ST_UF,
211                         .bMasterInterface       = 0x00,
212                         .bSlaveInterface0       = 0x01,
213                 },
214                 .notification_endpoint = {
215                         .bLength =
216                                 sizeof(struct usb_endpoint_descriptor),
217                         .bDescriptorType        = USB_DT_ENDPOINT,
218                         .bEndpointAddress       = UDC_INT_ENDPOINT | USB_DIR_IN,
219                         .bmAttributes           = USB_ENDPOINT_XFER_INT,
220                         .wMaxPacketSize
221                                 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
222                         .bInterval              = 0xFF,
223                 },
224
225                 /* Interface 2 */
226                 .data_class_interface = {
227                         .bLength                =
228                                 sizeof(struct usb_interface_descriptor),
229                         .bDescriptorType        = USB_DT_INTERFACE,
230                         .bInterfaceNumber       = 0x01,
231                         .bAlternateSetting      = 0x00,
232                         .bNumEndpoints          = 0x02,
233                         .bInterfaceClass        =
234                                 COMMUNICATIONS_INTERFACE_CLASS_DATA,
235                         .bInterfaceSubClass     = DATA_INTERFACE_SUBCLASS_NONE,
236                         .bInterfaceProtocol     = DATA_INTERFACE_PROTOCOL_NONE,
237                         .iInterface             = STR_DATA_INTERFACE,
238                 },
239                 .data_endpoints = {
240                         {
241                                 .bLength                =
242                                         sizeof(struct usb_endpoint_descriptor),
243                                 .bDescriptorType        = USB_DT_ENDPOINT,
244                                 .bEndpointAddress       = UDC_OUT_ENDPOINT | USB_DIR_OUT,
245                                 .bmAttributes           =
246                                         USB_ENDPOINT_XFER_BULK,
247                                 .wMaxPacketSize         =
248                                         cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
249                                 .bInterval              = 0xFF,
250                         },
251                         {
252                                 .bLength                =
253                                         sizeof(struct usb_endpoint_descriptor),
254                                 .bDescriptorType        = USB_DT_ENDPOINT,
255                                 .bEndpointAddress       = UDC_IN_ENDPOINT | USB_DIR_IN,
256                                 .bmAttributes           =
257                                         USB_ENDPOINT_XFER_BULK,
258                                 .wMaxPacketSize         =
259                                         cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
260                                 .bInterval              = 0xFF,
261                         },
262                 },
263         },
264 };
265
266 static struct rs232_emu rs232_desc={
267                 .dter           =       115200,
268                 .stop_bits      =       0x00,
269                 .parity         =       0x00,
270                 .data_bits      =       0x08
271 };
272
273
274 /*
275  * Static Generic Serial specific data
276  */
277
278
279 struct gserial_config_desc {
280
281         struct usb_configuration_descriptor configuration_desc;
282         struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
283         struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
284
285 } __attribute__((packed));
286
287 static struct gserial_config_desc
288 gserial_configuration_descriptors[NUM_CONFIGS] ={
289         {
290                 .configuration_desc ={
291                         .bLength = sizeof(struct usb_configuration_descriptor),
292                         .bDescriptorType = USB_DT_CONFIG,
293                         .wTotalLength =
294                                 cpu_to_le16(sizeof(struct gserial_config_desc)),
295                         .bNumInterfaces = NUM_GSERIAL_INTERFACES,
296                         .bConfigurationValue = 1,
297                         .iConfiguration = STR_CONFIG,
298                         .bmAttributes =
299                                 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
300                         .bMaxPower = USBTTY_MAXPOWER
301                 },
302                 .interface_desc = {
303                         {
304                                 .bLength  =
305                                         sizeof(struct usb_interface_descriptor),
306                                 .bDescriptorType = USB_DT_INTERFACE,
307                                 .bInterfaceNumber = 0,
308                                 .bAlternateSetting = 0,
309                                 .bNumEndpoints = NUM_ENDPOINTS,
310                                 .bInterfaceClass =
311                                         COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
312                                 .bInterfaceSubClass =
313                                         COMMUNICATIONS_NO_SUBCLASS,
314                                 .bInterfaceProtocol =
315                                         COMMUNICATIONS_NO_PROTOCOL,
316                                 .iInterface = STR_DATA_INTERFACE
317                         },
318                 },
319                 .data_endpoints  = {
320                         {
321                                 .bLength =
322                                         sizeof(struct usb_endpoint_descriptor),
323                                 .bDescriptorType =      USB_DT_ENDPOINT,
324                                 .bEndpointAddress =     UDC_OUT_ENDPOINT | USB_DIR_OUT,
325                                 .bmAttributes =         USB_ENDPOINT_XFER_BULK,
326                                 .wMaxPacketSize =
327                                         cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
328                                 .bInterval=             0xFF,
329                         },
330                         {
331                                 .bLength =
332                                         sizeof(struct usb_endpoint_descriptor),
333                                 .bDescriptorType =      USB_DT_ENDPOINT,
334                                 .bEndpointAddress =     UDC_IN_ENDPOINT | USB_DIR_IN,
335                                 .bmAttributes =         USB_ENDPOINT_XFER_BULK,
336                                 .wMaxPacketSize =
337                                         cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
338                                 .bInterval =            0xFF,
339                         },
340                         {
341                                 .bLength =
342                                         sizeof(struct usb_endpoint_descriptor),
343                                 .bDescriptorType =      USB_DT_ENDPOINT,
344                                 .bEndpointAddress =     UDC_INT_ENDPOINT | USB_DIR_IN,
345                                 .bmAttributes =         USB_ENDPOINT_XFER_INT,
346                                 .wMaxPacketSize =
347                                         cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
348                                 .bInterval =            0xFF,
349                         },
350                 },
351         },
352 };
353
354 /*
355  * Static Function Prototypes
356  */
357
358 static void usbtty_init_strings (void);
359 static void usbtty_init_instances (void);
360 static void usbtty_init_endpoints (void);
361 static void usbtty_init_terminal_type(short type);
362 static void usbtty_event_handler (struct usb_device_instance *device,
363                                 usb_device_event_t event, int data);
364 static int usbtty_cdc_setup(struct usb_device_request *request,
365                                 struct urb *urb);
366 static int usbtty_configured (void);
367 static int write_buffer (circbuf_t * buf);
368 static int fill_buffer (circbuf_t * buf);
369
370 void usbtty_poll (void);
371
372 /* utility function for converting char* to wide string used by USB */
373 static void str2wide (char *str, u16 * wide)
374 {
375         int i;
376         for (i = 0; i < strlen (str) && str[i]; i++){
377                 #if defined(__LITTLE_ENDIAN)
378                         wide[i] = (u16) str[i];
379                 #elif defined(__BIG_ENDIAN)
380                         wide[i] = ((u16)(str[i])<<8);
381                 #else
382                         #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
383                 #endif
384         }
385 }
386
387 /*
388  * Test whether a character is in the RX buffer
389  */
390
391 int usbtty_tstc(struct stdio_dev *dev)
392 {
393         struct usb_endpoint_instance *endpoint =
394                 &endpoint_instance[rx_endpoint];
395
396         /* If no input data exists, allow more RX to be accepted */
397         if(usbtty_input.size <= 0){
398                 udc_unset_nak(endpoint->endpoint_address&0x03);
399         }
400
401         usbtty_poll ();
402         return (usbtty_input.size > 0);
403 }
404
405 /*
406  * Read a single byte from the usb client port. Returns 1 on success, 0
407  * otherwise. When the function is succesfull, the character read is
408  * written into its argument c.
409  */
410
411 int usbtty_getc(struct stdio_dev *dev)
412 {
413         char c;
414         struct usb_endpoint_instance *endpoint =
415                 &endpoint_instance[rx_endpoint];
416
417         while (usbtty_input.size <= 0) {
418                 udc_unset_nak(endpoint->endpoint_address&0x03);
419                 usbtty_poll ();
420         }
421
422         buf_pop (&usbtty_input, &c, 1);
423         udc_set_nak(endpoint->endpoint_address&0x03);
424
425         return c;
426 }
427
428 /*
429  * Output a single byte to the usb client port.
430  */
431 void usbtty_putc(struct stdio_dev *dev, const char c)
432 {
433         if (!usbtty_configured ())
434                 return;
435
436         /* If \n, also do \r */
437         if (c == '\n')
438                 buf_push (&usbtty_output, "\r", 1);
439
440         buf_push(&usbtty_output, &c, 1);
441
442         /* Poll at end to handle new data... */
443         if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
444                 usbtty_poll ();
445         }
446 }
447
448 /* usbtty_puts() helper function for finding the next '\n' in a string */
449 static int next_nl_pos (const char *s)
450 {
451         int i;
452
453         for (i = 0; s[i] != '\0'; i++) {
454                 if (s[i] == '\n')
455                         return i;
456         }
457         return i;
458 }
459
460 /*
461  * Output a string to the usb client port - implementing flow control
462  */
463
464 static void __usbtty_puts (const char *str, int len)
465 {
466         int maxlen = usbtty_output.totalsize;
467         int space, n;
468
469         /* break str into chunks < buffer size, if needed */
470         while (len > 0) {
471                 usbtty_poll ();
472
473                 space = maxlen - usbtty_output.size;
474                 /* Empty buffer here, if needed, to ensure space... */
475                 if (space) {
476                         write_buffer (&usbtty_output);
477
478                         n = min(space, min(len, maxlen));
479                         buf_push (&usbtty_output, str, n);
480
481                         str += n;
482                         len -= n;
483                 }
484         }
485 }
486
487 void usbtty_puts(struct stdio_dev *dev, const char *str)
488 {
489         int n;
490         int len;
491
492         if (!usbtty_configured ())
493                 return;
494
495         len = strlen (str);
496         /* add '\r' for each '\n' */
497         while (len > 0) {
498                 n = next_nl_pos (str);
499
500                 if (str[n] == '\n') {
501                         __usbtty_puts("\r", 1);
502                         __usbtty_puts(str, n + 1);
503                         str += (n + 1);
504                         len -= (n + 1);
505                 } else {
506                         /* No \n found.  All done. */
507                         __usbtty_puts (str, n);
508                         break;
509                 }
510         }
511
512         /* Poll at end to handle new data... */
513         usbtty_poll ();
514 }
515
516 /*
517  * Initialize the usb client port.
518  *
519  */
520 int drv_usbtty_init (void)
521 {
522         int rc;
523         char * sn;
524         char * tt;
525         int snlen;
526
527         /* Get serial number */
528         sn = env_get("serial#");
529         if (!sn)
530                 sn = "000000000000";
531         snlen = strlen(sn);
532         if (snlen > sizeof(serial_number) - 1) {
533                 printf ("Warning: serial number %s is too long (%d > %lu)\n",
534                         sn, snlen, (ulong)(sizeof(serial_number) - 1));
535                 snlen = sizeof(serial_number) - 1;
536         }
537         memcpy (serial_number, sn, snlen);
538         serial_number[snlen] = '\0';
539
540         /* Decide on which type of UDC device to be.
541          */
542         tt = env_get("usbtty");
543         if (!tt)
544                 tt = "generic";
545         usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
546
547         /* prepare buffers... */
548         buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
549         buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
550
551         /* Now, set up USB controller and infrastructure */
552         udc_init ();            /* Basic USB initialization */
553
554         usbtty_init_strings ();
555         usbtty_init_instances ();
556
557         usbtty_init_endpoints ();
558
559         udc_startup_events (device_instance);/* Enable dev, init udc pointers */
560         udc_connect ();         /* Enable pullup for host detection */
561
562         /* Device initialization */
563         memset (&usbttydev, 0, sizeof (usbttydev));
564
565         strcpy (usbttydev.name, "usbtty");
566         usbttydev.ext = 0;      /* No extensions */
567         usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
568         usbttydev.tstc = usbtty_tstc;   /* 'tstc' function */
569         usbttydev.getc = usbtty_getc;   /* 'getc' function */
570         usbttydev.putc = usbtty_putc;   /* 'putc' function */
571         usbttydev.puts = usbtty_puts;   /* 'puts' function */
572
573         rc = stdio_register (&usbttydev);
574
575         return (rc == 0) ? 1 : rc;
576 }
577
578 static void usbtty_init_strings (void)
579 {
580         struct usb_string_descriptor *string;
581
582         usbtty_string_table[STR_LANG] =
583                 (struct usb_string_descriptor*)wstrLang;
584
585         string = (struct usb_string_descriptor *) wstrManufacturer;
586         string->bLength = sizeof(wstrManufacturer);
587         string->bDescriptorType = USB_DT_STRING;
588         str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
589         usbtty_string_table[STR_MANUFACTURER]=string;
590
591
592         string = (struct usb_string_descriptor *) wstrProduct;
593         string->bLength = sizeof(wstrProduct);
594         string->bDescriptorType = USB_DT_STRING;
595         str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
596         usbtty_string_table[STR_PRODUCT]=string;
597
598
599         string = (struct usb_string_descriptor *) wstrSerial;
600         string->bLength = sizeof(serial_number);
601         string->bDescriptorType = USB_DT_STRING;
602         str2wide (serial_number, string->wData);
603         usbtty_string_table[STR_SERIAL]=string;
604
605
606         string = (struct usb_string_descriptor *) wstrConfiguration;
607         string->bLength = sizeof(wstrConfiguration);
608         string->bDescriptorType = USB_DT_STRING;
609         str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
610         usbtty_string_table[STR_CONFIG]=string;
611
612
613         string = (struct usb_string_descriptor *) wstrDataInterface;
614         string->bLength = sizeof(wstrDataInterface);
615         string->bDescriptorType = USB_DT_STRING;
616         str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
617         usbtty_string_table[STR_DATA_INTERFACE]=string;
618
619         string = (struct usb_string_descriptor *) wstrCtrlInterface;
620         string->bLength = sizeof(wstrCtrlInterface);
621         string->bDescriptorType = USB_DT_STRING;
622         str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
623         usbtty_string_table[STR_CTRL_INTERFACE]=string;
624
625         /* Now, initialize the string table for ep0 handling */
626         usb_strings = usbtty_string_table;
627 }
628
629 #define init_wMaxPacketSize(x)  le16_to_cpu(get_unaligned(\
630                         &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
631
632 static void usbtty_init_instances (void)
633 {
634         int i;
635
636         /* initialize device instance */
637         memset (device_instance, 0, sizeof (struct usb_device_instance));
638         device_instance->device_state = STATE_INIT;
639         device_instance->device_descriptor = &device_descriptor;
640 #if defined(CONFIG_USBD_HS)
641         device_instance->qualifier_descriptor = &qualifier_descriptor;
642 #endif
643         device_instance->event = usbtty_event_handler;
644         device_instance->cdc_recv_setup = usbtty_cdc_setup;
645         device_instance->bus = bus_instance;
646         device_instance->configurations = NUM_CONFIGS;
647         device_instance->configuration_instance_array = config_instance;
648
649         /* initialize bus instance */
650         memset (bus_instance, 0, sizeof (struct usb_bus_instance));
651         bus_instance->device = device_instance;
652         bus_instance->endpoint_array = endpoint_instance;
653         bus_instance->max_endpoints = 1;
654         bus_instance->maxpacketsize = 64;
655         bus_instance->serial_number_str = serial_number;
656
657         /* configuration instance */
658         memset (config_instance, 0,
659                 sizeof (struct usb_configuration_instance));
660         config_instance->interfaces = interface_count;
661         config_instance->configuration_descriptor = configuration_descriptor;
662         config_instance->interface_instance_array = interface_instance;
663
664         /* interface instance */
665         memset (interface_instance, 0,
666                 sizeof (struct usb_interface_instance));
667         interface_instance->alternates = 1;
668         interface_instance->alternates_instance_array = alternate_instance;
669
670         /* alternates instance */
671         memset (alternate_instance, 0,
672                 sizeof (struct usb_alternate_instance));
673         alternate_instance->interface_descriptor = interface_descriptors;
674         alternate_instance->endpoints = NUM_ENDPOINTS;
675         alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
676
677         /* endpoint instances */
678         memset (&endpoint_instance[0], 0,
679                 sizeof (struct usb_endpoint_instance));
680         endpoint_instance[0].endpoint_address = 0;
681         endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
682         endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
683         endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
684         endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
685         udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
686
687         for (i = 1; i <= NUM_ENDPOINTS; i++) {
688                 memset (&endpoint_instance[i], 0,
689                         sizeof (struct usb_endpoint_instance));
690
691                 endpoint_instance[i].endpoint_address =
692                         ep_descriptor_ptrs[i - 1]->bEndpointAddress;
693
694                 endpoint_instance[i].rcv_attributes =
695                         ep_descriptor_ptrs[i - 1]->bmAttributes;
696
697                 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
698
699                 endpoint_instance[i].tx_attributes =
700                         ep_descriptor_ptrs[i - 1]->bmAttributes;
701
702                 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
703
704                 endpoint_instance[i].tx_attributes =
705                         ep_descriptor_ptrs[i - 1]->bmAttributes;
706
707                 urb_link_init (&endpoint_instance[i].rcv);
708                 urb_link_init (&endpoint_instance[i].rdy);
709                 urb_link_init (&endpoint_instance[i].tx);
710                 urb_link_init (&endpoint_instance[i].done);
711
712                 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
713                         endpoint_instance[i].tx_urb =
714                                 usbd_alloc_urb (device_instance,
715                                                 &endpoint_instance[i]);
716                 else
717                         endpoint_instance[i].rcv_urb =
718                                 usbd_alloc_urb (device_instance,
719                                                 &endpoint_instance[i]);
720         }
721 }
722
723 static void usbtty_init_endpoints (void)
724 {
725         int i;
726
727         bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
728         for (i = 1; i <= NUM_ENDPOINTS; i++) {
729                 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
730         }
731 }
732
733 /* usbtty_init_terminal_type
734  *
735  * Do some late binding for our device type.
736  */
737 static void usbtty_init_terminal_type(short type)
738 {
739         switch(type){
740                 /* CDC ACM */
741                 case 0:
742                         /* Assign endpoint descriptors */
743                         ep_descriptor_ptrs[0] =
744                                 &acm_configuration_descriptors[0].notification_endpoint;
745                         ep_descriptor_ptrs[1] =
746                                 &acm_configuration_descriptors[0].data_endpoints[0];
747                         ep_descriptor_ptrs[2] =
748                                 &acm_configuration_descriptors[0].data_endpoints[1];
749
750                         /* Enumerate Device Descriptor */
751                         device_descriptor.bDeviceClass =
752                                 COMMUNICATIONS_DEVICE_CLASS;
753                         device_descriptor.idProduct =
754                                 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
755
756 #if defined(CONFIG_USBD_HS)
757                         qualifier_descriptor.bDeviceClass =
758                                 COMMUNICATIONS_DEVICE_CLASS;
759 #endif
760                         /* Assign endpoint indices */
761                         tx_endpoint = ACM_TX_ENDPOINT;
762                         rx_endpoint = ACM_RX_ENDPOINT;
763
764                         /* Configuration Descriptor */
765                         configuration_descriptor =
766                                 (struct usb_configuration_descriptor*)
767                                 &acm_configuration_descriptors;
768
769                         /* Interface count */
770                         interface_count = NUM_ACM_INTERFACES;
771                 break;
772
773                 /* BULK IN/OUT & Default */
774                 case 1:
775                 default:
776                         /* Assign endpoint descriptors */
777                         ep_descriptor_ptrs[0] =
778                                 &gserial_configuration_descriptors[0].data_endpoints[0];
779                         ep_descriptor_ptrs[1] =
780                                 &gserial_configuration_descriptors[0].data_endpoints[1];
781                         ep_descriptor_ptrs[2] =
782                                 &gserial_configuration_descriptors[0].data_endpoints[2];
783
784                         /* Enumerate Device Descriptor */
785                         device_descriptor.bDeviceClass = 0xFF;
786                         device_descriptor.idProduct =
787                                 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
788 #if defined(CONFIG_USBD_HS)
789                         qualifier_descriptor.bDeviceClass = 0xFF;
790 #endif
791                         /* Assign endpoint indices */
792                         tx_endpoint = GSERIAL_TX_ENDPOINT;
793                         rx_endpoint = GSERIAL_RX_ENDPOINT;
794
795                         /* Configuration Descriptor */
796                         configuration_descriptor =
797                                 (struct usb_configuration_descriptor*)
798                                 &gserial_configuration_descriptors;
799
800                         /* Interface count */
801                         interface_count = NUM_GSERIAL_INTERFACES;
802                 break;
803         }
804 }
805
806 /******************************************************************************/
807
808 static struct urb *next_urb (struct usb_device_instance *device,
809                              struct usb_endpoint_instance *endpoint)
810 {
811         struct urb *current_urb = NULL;
812         int space;
813
814         /* If there's a queue, then we should add to the last urb */
815         if (!endpoint->tx_queue) {
816                 current_urb = endpoint->tx_urb;
817         } else {
818                 /* Last urb from tx chain */
819                 current_urb =
820                         p2surround (struct urb, link, endpoint->tx.prev);
821         }
822
823         /* Make sure this one has enough room */
824         space = current_urb->buffer_length - current_urb->actual_length;
825         if (space > 0) {
826                 return current_urb;
827         } else {                /* No space here */
828                 /* First look at done list */
829                 current_urb = first_urb_detached (&endpoint->done);
830                 if (!current_urb) {
831                         current_urb = usbd_alloc_urb (device, endpoint);
832                 }
833
834                 urb_append (&endpoint->tx, current_urb);
835                 endpoint->tx_queue++;
836         }
837         return current_urb;
838 }
839
840 static int write_buffer (circbuf_t * buf)
841 {
842         if (!usbtty_configured ()) {
843                 return 0;
844         }
845
846         struct usb_endpoint_instance *endpoint =
847                         &endpoint_instance[tx_endpoint];
848         struct urb *current_urb = NULL;
849
850         current_urb = next_urb (device_instance, endpoint);
851
852         if (!current_urb) {
853                 TTYERR ("current_urb is NULL, buf->size %d\n",
854                 buf->size);
855                 return 0;
856         }
857
858         /* TX data still exists - send it now
859          */
860         if(endpoint->sent < current_urb->actual_length){
861                 if(udc_endpoint_write (endpoint)){
862                         /* Write pre-empted by RX */
863                         return -1;
864                 }
865         }
866
867         if (buf->size) {
868                 char *dest;
869
870                 int space_avail;
871                 int popnum, popped;
872                 int total = 0;
873
874                 /* Break buffer into urb sized pieces,
875                  * and link each to the endpoint
876                  */
877                 while (buf->size > 0) {
878
879                         dest = (char*)current_urb->buffer +
880                                 current_urb->actual_length;
881
882                         space_avail =
883                                 current_urb->buffer_length -
884                                 current_urb->actual_length;
885                         popnum = min(space_avail, (int)buf->size);
886                         if (popnum == 0)
887                                 break;
888
889                         popped = buf_pop (buf, dest, popnum);
890                         if (popped == 0)
891                                 break;
892                         current_urb->actual_length += popped;
893                         total += popped;
894
895                         /* If endpoint->last == 0, then transfers have
896                          * not started on this endpoint
897                          */
898                         if (endpoint->last == 0) {
899                                 if(udc_endpoint_write (endpoint)){
900                                         /* Write pre-empted by RX */
901                                         return -1;
902                                 }
903                         }
904
905                 }/* end while */
906                 return total;
907         }
908
909         return 0;
910 }
911
912 static int fill_buffer (circbuf_t * buf)
913 {
914         struct usb_endpoint_instance *endpoint =
915                 &endpoint_instance[rx_endpoint];
916
917         if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
918                 unsigned int nb = 0;
919                 char *src = (char *) endpoint->rcv_urb->buffer;
920                 unsigned int rx_avail = buf->totalsize - buf->size;
921
922                 if(rx_avail >= endpoint->rcv_urb->actual_length){
923
924                         nb = endpoint->rcv_urb->actual_length;
925                         buf_push (buf, src, nb);
926                         endpoint->rcv_urb->actual_length = 0;
927
928                 }
929                 return nb;
930         }
931         return 0;
932 }
933
934 static int usbtty_configured (void)
935 {
936         return usbtty_configured_flag;
937 }
938
939 /******************************************************************************/
940
941 static void usbtty_event_handler (struct usb_device_instance *device,
942                                   usb_device_event_t event, int data)
943 {
944 #if defined(CONFIG_USBD_HS)
945         int i;
946 #endif
947         switch (event) {
948         case DEVICE_RESET:
949         case DEVICE_BUS_INACTIVE:
950                 usbtty_configured_flag = 0;
951                 break;
952         case DEVICE_CONFIGURED:
953                 usbtty_configured_flag = 1;
954                 break;
955
956         case DEVICE_ADDRESS_ASSIGNED:
957 #if defined(CONFIG_USBD_HS)
958                 /*
959                  * is_usbd_high_speed routine needs to be defined by
960                  * specific gadget driver
961                  * It returns true if device enumerates at High speed
962                  * Retuns false otherwise
963                  */
964                 for (i = 0; i < NUM_ENDPOINTS; i++) {
965                         if (((ep_descriptor_ptrs[i]->bmAttributes &
966                               USB_ENDPOINT_XFERTYPE_MASK) ==
967                               USB_ENDPOINT_XFER_BULK)
968                             && is_usbd_high_speed()) {
969
970                                 ep_descriptor_ptrs[i]->wMaxPacketSize =
971                                         CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
972                         }
973
974                         endpoint_instance[i + 1].tx_packetSize =
975                                 ep_descriptor_ptrs[i]->wMaxPacketSize;
976                         endpoint_instance[i + 1].rcv_packetSize =
977                                 ep_descriptor_ptrs[i]->wMaxPacketSize;
978                 }
979 #endif
980                 usbtty_init_endpoints ();
981
982         default:
983                 break;
984         }
985 }
986
987 /******************************************************************************/
988
989 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
990 {
991         switch (request->bRequest){
992
993                 case ACM_SET_CONTROL_LINE_STATE:        /* Implies DTE ready */
994                         break;
995                 case ACM_SEND_ENCAPSULATED_COMMAND :    /* Required */
996                         break;
997                 case ACM_SET_LINE_ENCODING :            /* DTE stop/parity bits
998                                                          * per character */
999                         break;
1000                 case ACM_GET_ENCAPSULATED_RESPONSE :    /* request response */
1001                         break;
1002                 case ACM_GET_LINE_ENCODING :            /* request DTE rate,
1003                                                          * stop/parity bits */
1004                         memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1005                         urb->actual_length = sizeof(rs232_desc);
1006
1007                         break;
1008                 default:
1009                         return 1;
1010         }
1011         return 0;
1012 }
1013
1014 /******************************************************************************/
1015
1016 /*
1017  * Since interrupt handling has not yet been implemented, we use this function
1018  * to handle polling.  This is called by the tstc,getc,putc,puts routines to
1019  * update the USB state.
1020  */
1021 void usbtty_poll (void)
1022 {
1023         /* New interrupts? */
1024         udc_irq();
1025
1026         /* Write any output data to host buffer
1027          * (do this before checking interrupts to avoid missing one)
1028          */
1029         if (usbtty_configured ()) {
1030                 write_buffer (&usbtty_output);
1031         }
1032
1033         /* New interrupts? */
1034         udc_irq();
1035
1036         /* Check for new data from host..
1037          * (do this after checking interrupts to get latest data)
1038          */
1039         if (usbtty_configured ()) {
1040                 fill_buffer (&usbtty_input);
1041         }
1042
1043         /* New interrupts? */
1044         udc_irq();
1045
1046 }