1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerry Hamel, geh@ti.com, Texas Instruments
7 * Bryan O'Donoghue, bodonoghue@codehermit.ie
13 #include <stdio_dev.h>
14 #include <asm/unaligned.h>
16 #include "usb_cdc_acm.h"
17 #include "usbdescriptors.h"
20 #define TTYDBG(fmt,args...)\
21 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
23 #define TTYDBG(fmt,args...) do{}while(0)
27 #define TTYERR(fmt,args...)\
28 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
31 #define TTYERR(fmt,args...) do{}while(0)
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"
50 * Buffers to hold input and output data
52 #define USBTTY_BUFFER_SIZE 2048
53 static circbuf_t usbtty_input;
54 static circbuf_t usbtty_output;
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];
72 int usbtty_configured_flag = 0;
77 static char serial_number[16];
81 * Descriptors, Strings, Local variables.
84 /* defined and used by gadget/ep0.c */
85 extern struct usb_string_descriptor **usb_strings;
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];
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)];
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
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
136 * Static CDC ACM specific descriptors
139 struct acm_config_desc {
140 struct usb_configuration_descriptor configuration_desc;
142 /* Master Interface */
143 struct usb_interface_descriptor interface_desc;
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;
151 /* Slave Interface */
152 struct usb_interface_descriptor data_class_interface;
153 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
154 } __attribute__((packed));
156 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
158 .configuration_desc ={
160 sizeof(struct usb_configuration_descriptor),
161 .bDescriptorType = USB_DT_CONFIG,
163 cpu_to_le16(sizeof(struct acm_config_desc)),
164 .bNumInterfaces = NUM_ACM_INTERFACES,
165 .bConfigurationValue = 1,
166 .iConfiguration = STR_CONFIG,
168 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
169 .bMaxPower = USBTTY_MAXPOWER
173 .bLength = sizeof(struct usb_interface_descriptor),
174 .bDescriptorType = USB_DT_INTERFACE,
175 .bInterfaceNumber = 0,
176 .bAlternateSetting = 0,
177 .bNumEndpoints = 0x01,
179 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
180 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
181 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
182 .iInterface = STR_CTRL_INTERFACE,
184 .usb_class_header = {
186 sizeof(struct usb_class_header_function_descriptor),
187 .bDescriptorType = CS_INTERFACE,
188 .bDescriptorSubtype = USB_ST_HEADER,
189 .bcdCDC = cpu_to_le16(110),
191 .usb_class_call_mgt = {
193 sizeof(struct usb_class_call_management_descriptor),
194 .bDescriptorType = CS_INTERFACE,
195 .bDescriptorSubtype = USB_ST_CMF,
196 .bmCapabilities = 0x00,
197 .bDataInterface = 0x01,
201 sizeof(struct usb_class_abstract_control_descriptor),
202 .bDescriptorType = CS_INTERFACE,
203 .bDescriptorSubtype = USB_ST_ACMF,
204 .bmCapabilities = 0x00,
208 sizeof(struct usb_class_union_function_descriptor),
209 .bDescriptorType = CS_INTERFACE,
210 .bDescriptorSubtype = USB_ST_UF,
211 .bMasterInterface = 0x00,
212 .bSlaveInterface0 = 0x01,
214 .notification_endpoint = {
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,
221 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
226 .data_class_interface = {
228 sizeof(struct usb_interface_descriptor),
229 .bDescriptorType = USB_DT_INTERFACE,
230 .bInterfaceNumber = 0x01,
231 .bAlternateSetting = 0x00,
232 .bNumEndpoints = 0x02,
234 COMMUNICATIONS_INTERFACE_CLASS_DATA,
235 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
236 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
237 .iInterface = STR_DATA_INTERFACE,
242 sizeof(struct usb_endpoint_descriptor),
243 .bDescriptorType = USB_DT_ENDPOINT,
244 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
246 USB_ENDPOINT_XFER_BULK,
248 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
253 sizeof(struct usb_endpoint_descriptor),
254 .bDescriptorType = USB_DT_ENDPOINT,
255 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
257 USB_ENDPOINT_XFER_BULK,
259 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
266 static struct rs232_emu rs232_desc={
275 * Static Generic Serial specific data
279 struct gserial_config_desc {
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];
285 } __attribute__((packed));
287 static struct gserial_config_desc
288 gserial_configuration_descriptors[NUM_CONFIGS] ={
290 .configuration_desc ={
291 .bLength = sizeof(struct usb_configuration_descriptor),
292 .bDescriptorType = USB_DT_CONFIG,
294 cpu_to_le16(sizeof(struct gserial_config_desc)),
295 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
296 .bConfigurationValue = 1,
297 .iConfiguration = STR_CONFIG,
299 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
300 .bMaxPower = USBTTY_MAXPOWER
305 sizeof(struct usb_interface_descriptor),
306 .bDescriptorType = USB_DT_INTERFACE,
307 .bInterfaceNumber = 0,
308 .bAlternateSetting = 0,
309 .bNumEndpoints = NUM_ENDPOINTS,
311 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
312 .bInterfaceSubClass =
313 COMMUNICATIONS_NO_SUBCLASS,
314 .bInterfaceProtocol =
315 COMMUNICATIONS_NO_PROTOCOL,
316 .iInterface = STR_DATA_INTERFACE
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,
327 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
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,
337 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
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,
347 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
355 * Static Function Prototypes
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,
366 static int usbtty_configured (void);
367 static int write_buffer (circbuf_t * buf);
368 static int fill_buffer (circbuf_t * buf);
370 void usbtty_poll (void);
372 /* utility function for converting char* to wide string used by USB */
373 static void str2wide (char *str, u16 * wide)
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);
382 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
388 * Test whether a character is in the RX buffer
391 int usbtty_tstc(struct stdio_dev *dev)
393 struct usb_endpoint_instance *endpoint =
394 &endpoint_instance[rx_endpoint];
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);
402 return (usbtty_input.size > 0);
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.
411 int usbtty_getc(struct stdio_dev *dev)
414 struct usb_endpoint_instance *endpoint =
415 &endpoint_instance[rx_endpoint];
417 while (usbtty_input.size <= 0) {
418 udc_unset_nak(endpoint->endpoint_address&0x03);
422 buf_pop (&usbtty_input, &c, 1);
423 udc_set_nak(endpoint->endpoint_address&0x03);
429 * Output a single byte to the usb client port.
431 void usbtty_putc(struct stdio_dev *dev, const char c)
433 if (!usbtty_configured ())
436 /* If \n, also do \r */
438 buf_push (&usbtty_output, "\r", 1);
440 buf_push(&usbtty_output, &c, 1);
442 /* Poll at end to handle new data... */
443 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
448 /* usbtty_puts() helper function for finding the next '\n' in a string */
449 static int next_nl_pos (const char *s)
453 for (i = 0; s[i] != '\0'; i++) {
461 * Output a string to the usb client port - implementing flow control
464 static void __usbtty_puts (const char *str, int len)
466 int maxlen = usbtty_output.totalsize;
469 /* break str into chunks < buffer size, if needed */
473 space = maxlen - usbtty_output.size;
474 /* Empty buffer here, if needed, to ensure space... */
476 write_buffer (&usbtty_output);
478 n = min(space, min(len, maxlen));
479 buf_push (&usbtty_output, str, n);
487 void usbtty_puts(struct stdio_dev *dev, const char *str)
492 if (!usbtty_configured ())
496 /* add '\r' for each '\n' */
498 n = next_nl_pos (str);
500 if (str[n] == '\n') {
501 __usbtty_puts("\r", 1);
502 __usbtty_puts(str, n + 1);
506 /* No \n found. All done. */
507 __usbtty_puts (str, n);
512 /* Poll at end to handle new data... */
517 * Initialize the usb client port.
520 int drv_usbtty_init (void)
527 /* Get serial number */
528 sn = env_get("serial#");
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;
537 memcpy (serial_number, sn, snlen);
538 serial_number[snlen] = '\0';
540 /* Decide on which type of UDC device to be.
542 tt = env_get("usbtty");
545 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
547 /* prepare buffers... */
548 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
549 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
551 /* Now, set up USB controller and infrastructure */
552 udc_init (); /* Basic USB initialization */
554 usbtty_init_strings ();
555 usbtty_init_instances ();
557 usbtty_init_endpoints ();
559 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
560 udc_connect (); /* Enable pullup for host detection */
562 /* Device initialization */
563 memset (&usbttydev, 0, sizeof (usbttydev));
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 */
573 rc = stdio_register (&usbttydev);
575 return (rc == 0) ? 1 : rc;
578 static void usbtty_init_strings (void)
580 struct usb_string_descriptor *string;
582 usbtty_string_table[STR_LANG] =
583 (struct usb_string_descriptor*)wstrLang;
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;
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;
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;
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;
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;
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;
625 /* Now, initialize the string table for ep0 handling */
626 usb_strings = usbtty_string_table;
629 #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
630 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
632 static void usbtty_init_instances (void)
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;
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;
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;
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;
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;
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;
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]);
687 for (i = 1; i <= NUM_ENDPOINTS; i++) {
688 memset (&endpoint_instance[i], 0,
689 sizeof (struct usb_endpoint_instance));
691 endpoint_instance[i].endpoint_address =
692 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
694 endpoint_instance[i].rcv_attributes =
695 ep_descriptor_ptrs[i - 1]->bmAttributes;
697 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
699 endpoint_instance[i].tx_attributes =
700 ep_descriptor_ptrs[i - 1]->bmAttributes;
702 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
704 endpoint_instance[i].tx_attributes =
705 ep_descriptor_ptrs[i - 1]->bmAttributes;
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);
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]);
717 endpoint_instance[i].rcv_urb =
718 usbd_alloc_urb (device_instance,
719 &endpoint_instance[i]);
723 static void usbtty_init_endpoints (void)
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]);
733 /* usbtty_init_terminal_type
735 * Do some late binding for our device type.
737 static void usbtty_init_terminal_type(short type)
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];
750 /* Enumerate Device Descriptor */
751 device_descriptor.bDeviceClass =
752 COMMUNICATIONS_DEVICE_CLASS;
753 device_descriptor.idProduct =
754 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
756 #if defined(CONFIG_USBD_HS)
757 qualifier_descriptor.bDeviceClass =
758 COMMUNICATIONS_DEVICE_CLASS;
760 /* Assign endpoint indices */
761 tx_endpoint = ACM_TX_ENDPOINT;
762 rx_endpoint = ACM_RX_ENDPOINT;
764 /* Configuration Descriptor */
765 configuration_descriptor =
766 (struct usb_configuration_descriptor*)
767 &acm_configuration_descriptors;
769 /* Interface count */
770 interface_count = NUM_ACM_INTERFACES;
773 /* BULK IN/OUT & 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];
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;
791 /* Assign endpoint indices */
792 tx_endpoint = GSERIAL_TX_ENDPOINT;
793 rx_endpoint = GSERIAL_RX_ENDPOINT;
795 /* Configuration Descriptor */
796 configuration_descriptor =
797 (struct usb_configuration_descriptor*)
798 &gserial_configuration_descriptors;
800 /* Interface count */
801 interface_count = NUM_GSERIAL_INTERFACES;
806 /******************************************************************************/
808 static struct urb *next_urb (struct usb_device_instance *device,
809 struct usb_endpoint_instance *endpoint)
811 struct urb *current_urb = NULL;
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;
818 /* Last urb from tx chain */
820 p2surround (struct urb, link, endpoint->tx.prev);
823 /* Make sure this one has enough room */
824 space = current_urb->buffer_length - current_urb->actual_length;
827 } else { /* No space here */
828 /* First look at done list */
829 current_urb = first_urb_detached (&endpoint->done);
831 current_urb = usbd_alloc_urb (device, endpoint);
834 urb_append (&endpoint->tx, current_urb);
835 endpoint->tx_queue++;
840 static int write_buffer (circbuf_t * buf)
842 if (!usbtty_configured ()) {
846 struct usb_endpoint_instance *endpoint =
847 &endpoint_instance[tx_endpoint];
848 struct urb *current_urb = NULL;
850 current_urb = next_urb (device_instance, endpoint);
853 TTYERR ("current_urb is NULL, buf->size %d\n",
858 /* TX data still exists - send it now
860 if(endpoint->sent < current_urb->actual_length){
861 if(udc_endpoint_write (endpoint)){
862 /* Write pre-empted by RX */
874 /* Break buffer into urb sized pieces,
875 * and link each to the endpoint
877 while (buf->size > 0) {
879 dest = (char*)current_urb->buffer +
880 current_urb->actual_length;
883 current_urb->buffer_length -
884 current_urb->actual_length;
885 popnum = min(space_avail, (int)buf->size);
889 popped = buf_pop (buf, dest, popnum);
892 current_urb->actual_length += popped;
895 /* If endpoint->last == 0, then transfers have
896 * not started on this endpoint
898 if (endpoint->last == 0) {
899 if(udc_endpoint_write (endpoint)){
900 /* Write pre-empted by RX */
912 static int fill_buffer (circbuf_t * buf)
914 struct usb_endpoint_instance *endpoint =
915 &endpoint_instance[rx_endpoint];
917 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
919 char *src = (char *) endpoint->rcv_urb->buffer;
920 unsigned int rx_avail = buf->totalsize - buf->size;
922 if(rx_avail >= endpoint->rcv_urb->actual_length){
924 nb = endpoint->rcv_urb->actual_length;
925 buf_push (buf, src, nb);
926 endpoint->rcv_urb->actual_length = 0;
934 static int usbtty_configured (void)
936 return usbtty_configured_flag;
939 /******************************************************************************/
941 static void usbtty_event_handler (struct usb_device_instance *device,
942 usb_device_event_t event, int data)
944 #if defined(CONFIG_USBD_HS)
949 case DEVICE_BUS_INACTIVE:
950 usbtty_configured_flag = 0;
952 case DEVICE_CONFIGURED:
953 usbtty_configured_flag = 1;
956 case DEVICE_ADDRESS_ASSIGNED:
957 #if defined(CONFIG_USBD_HS)
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
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()) {
970 ep_descriptor_ptrs[i]->wMaxPacketSize =
971 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
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;
980 usbtty_init_endpoints ();
987 /******************************************************************************/
989 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
991 switch (request->bRequest){
993 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
995 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
997 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
1000 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
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);
1014 /******************************************************************************/
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.
1021 void usbtty_poll (void)
1023 /* New interrupts? */
1026 /* Write any output data to host buffer
1027 * (do this before checking interrupts to avoid missing one)
1029 if (usbtty_configured ()) {
1030 write_buffer (&usbtty_output);
1033 /* New interrupts? */
1036 /* Check for new data from host..
1037 * (do this after checking interrupts to get latest data)
1039 if (usbtty_configured ()) {
1040 fill_buffer (&usbtty_input);
1043 /* New interrupts? */