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