3 * Gerry Hamel, geh@ti.com, Texas Instruments
6 * Bryan O'Donoghue, bodonoghue@codehermit.ie
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <stdio_dev.h>
15 #include <asm/unaligned.h>
17 #include "usb_cdc_acm.h"
18 #include "usbdescriptors.h"
21 #define TTYDBG(fmt,args...)\
22 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
24 #define TTYDBG(fmt,args...) do{}while(0)
28 #define TTYERR(fmt,args...)\
29 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
32 #define TTYERR(fmt,args...) do{}while(0)
39 #define MAX_INTERFACES 2
40 #define NUM_ENDPOINTS 3
41 #define ACM_TX_ENDPOINT 3
42 #define ACM_RX_ENDPOINT 2
43 #define GSERIAL_TX_ENDPOINT 2
44 #define GSERIAL_RX_ENDPOINT 1
45 #define NUM_ACM_INTERFACES 2
46 #define NUM_GSERIAL_INTERFACES 1
47 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
48 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
51 * Buffers to hold input and output data
53 #define USBTTY_BUFFER_SIZE 2048
54 static circbuf_t usbtty_input;
55 static circbuf_t usbtty_output;
61 static struct stdio_dev usbttydev;
62 static struct usb_device_instance device_instance[1];
63 static struct usb_bus_instance bus_instance[1];
64 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
65 static struct usb_interface_instance interface_instance[MAX_INTERFACES];
66 static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
67 /* one extra for control endpoint */
68 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
73 int usbtty_configured_flag = 0;
78 static char serial_number[16];
82 * Descriptors, Strings, Local variables.
85 /* defined and used by gadget/ep0.c */
86 extern struct usb_string_descriptor **usb_strings;
88 /* Indicies, References */
89 static unsigned short rx_endpoint = 0;
90 static unsigned short tx_endpoint = 0;
91 static unsigned short interface_count = 0;
92 static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
94 /* USB Descriptor Strings */
95 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
96 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
97 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
98 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
99 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
100 static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
101 static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
103 /* Standard USB Data Structures */
104 static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
105 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
106 static struct usb_configuration_descriptor *configuration_descriptor = 0;
107 static struct usb_device_descriptor device_descriptor = {
108 .bLength = sizeof(struct usb_device_descriptor),
109 .bDescriptorType = USB_DT_DEVICE,
110 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
111 .bDeviceSubClass = 0x00,
112 .bDeviceProtocol = 0x00,
113 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
114 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
115 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
116 .iManufacturer = STR_MANUFACTURER,
117 .iProduct = STR_PRODUCT,
118 .iSerialNumber = STR_SERIAL,
119 .bNumConfigurations = NUM_CONFIGS
123 #if defined(CONFIG_USBD_HS)
124 static struct usb_qualifier_descriptor qualifier_descriptor = {
125 .bLength = sizeof(struct usb_qualifier_descriptor),
126 .bDescriptorType = USB_DT_QUAL,
127 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
128 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
129 .bDeviceSubClass = 0x00,
130 .bDeviceProtocol = 0x00,
131 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
132 .bNumConfigurations = NUM_CONFIGS
137 * Static CDC ACM specific descriptors
140 struct acm_config_desc {
141 struct usb_configuration_descriptor configuration_desc;
143 /* Master Interface */
144 struct usb_interface_descriptor interface_desc;
146 struct usb_class_header_function_descriptor usb_class_header;
147 struct usb_class_call_management_descriptor usb_class_call_mgt;
148 struct usb_class_abstract_control_descriptor usb_class_acm;
149 struct usb_class_union_function_descriptor usb_class_union;
150 struct usb_endpoint_descriptor notification_endpoint;
152 /* Slave Interface */
153 struct usb_interface_descriptor data_class_interface;
154 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
155 } __attribute__((packed));
157 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
159 .configuration_desc ={
161 sizeof(struct usb_configuration_descriptor),
162 .bDescriptorType = USB_DT_CONFIG,
164 cpu_to_le16(sizeof(struct acm_config_desc)),
165 .bNumInterfaces = NUM_ACM_INTERFACES,
166 .bConfigurationValue = 1,
167 .iConfiguration = STR_CONFIG,
169 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
170 .bMaxPower = USBTTY_MAXPOWER
174 .bLength = sizeof(struct usb_interface_descriptor),
175 .bDescriptorType = USB_DT_INTERFACE,
176 .bInterfaceNumber = 0,
177 .bAlternateSetting = 0,
178 .bNumEndpoints = 0x01,
180 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
181 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
182 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
183 .iInterface = STR_CTRL_INTERFACE,
185 .usb_class_header = {
187 sizeof(struct usb_class_header_function_descriptor),
188 .bDescriptorType = CS_INTERFACE,
189 .bDescriptorSubtype = USB_ST_HEADER,
190 .bcdCDC = cpu_to_le16(110),
192 .usb_class_call_mgt = {
194 sizeof(struct usb_class_call_management_descriptor),
195 .bDescriptorType = CS_INTERFACE,
196 .bDescriptorSubtype = USB_ST_CMF,
197 .bmCapabilities = 0x00,
198 .bDataInterface = 0x01,
202 sizeof(struct usb_class_abstract_control_descriptor),
203 .bDescriptorType = CS_INTERFACE,
204 .bDescriptorSubtype = USB_ST_ACMF,
205 .bmCapabilities = 0x00,
209 sizeof(struct usb_class_union_function_descriptor),
210 .bDescriptorType = CS_INTERFACE,
211 .bDescriptorSubtype = USB_ST_UF,
212 .bMasterInterface = 0x00,
213 .bSlaveInterface0 = 0x01,
215 .notification_endpoint = {
217 sizeof(struct usb_endpoint_descriptor),
218 .bDescriptorType = USB_DT_ENDPOINT,
219 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
220 .bmAttributes = USB_ENDPOINT_XFER_INT,
222 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
227 .data_class_interface = {
229 sizeof(struct usb_interface_descriptor),
230 .bDescriptorType = USB_DT_INTERFACE,
231 .bInterfaceNumber = 0x01,
232 .bAlternateSetting = 0x00,
233 .bNumEndpoints = 0x02,
235 COMMUNICATIONS_INTERFACE_CLASS_DATA,
236 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
237 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
238 .iInterface = STR_DATA_INTERFACE,
243 sizeof(struct usb_endpoint_descriptor),
244 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
247 USB_ENDPOINT_XFER_BULK,
249 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
254 sizeof(struct usb_endpoint_descriptor),
255 .bDescriptorType = USB_DT_ENDPOINT,
256 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
258 USB_ENDPOINT_XFER_BULK,
260 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
267 static struct rs232_emu rs232_desc={
276 * Static Generic Serial specific data
280 struct gserial_config_desc {
282 struct usb_configuration_descriptor configuration_desc;
283 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
284 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
286 } __attribute__((packed));
288 static struct gserial_config_desc
289 gserial_configuration_descriptors[NUM_CONFIGS] ={
291 .configuration_desc ={
292 .bLength = sizeof(struct usb_configuration_descriptor),
293 .bDescriptorType = USB_DT_CONFIG,
295 cpu_to_le16(sizeof(struct gserial_config_desc)),
296 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
297 .bConfigurationValue = 1,
298 .iConfiguration = STR_CONFIG,
300 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
301 .bMaxPower = USBTTY_MAXPOWER
306 sizeof(struct usb_interface_descriptor),
307 .bDescriptorType = USB_DT_INTERFACE,
308 .bInterfaceNumber = 0,
309 .bAlternateSetting = 0,
310 .bNumEndpoints = NUM_ENDPOINTS,
312 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
313 .bInterfaceSubClass =
314 COMMUNICATIONS_NO_SUBCLASS,
315 .bInterfaceProtocol =
316 COMMUNICATIONS_NO_PROTOCOL,
317 .iInterface = STR_DATA_INTERFACE
323 sizeof(struct usb_endpoint_descriptor),
324 .bDescriptorType = USB_DT_ENDPOINT,
325 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
326 .bmAttributes = USB_ENDPOINT_XFER_BULK,
328 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
333 sizeof(struct usb_endpoint_descriptor),
334 .bDescriptorType = USB_DT_ENDPOINT,
335 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
338 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
343 sizeof(struct usb_endpoint_descriptor),
344 .bDescriptorType = USB_DT_ENDPOINT,
345 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
346 .bmAttributes = USB_ENDPOINT_XFER_INT,
348 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
356 * Static Function Prototypes
359 static void usbtty_init_strings (void);
360 static void usbtty_init_instances (void);
361 static void usbtty_init_endpoints (void);
362 static void usbtty_init_terminal_type(short type);
363 static void usbtty_event_handler (struct usb_device_instance *device,
364 usb_device_event_t event, int data);
365 static int usbtty_cdc_setup(struct usb_device_request *request,
367 static int usbtty_configured (void);
368 static int write_buffer (circbuf_t * buf);
369 static int fill_buffer (circbuf_t * buf);
371 void usbtty_poll (void);
373 /* utility function for converting char* to wide string used by USB */
374 static void str2wide (char *str, u16 * wide)
377 for (i = 0; i < strlen (str) && str[i]; i++){
378 #if defined(__LITTLE_ENDIAN)
379 wide[i] = (u16) str[i];
380 #elif defined(__BIG_ENDIAN)
381 wide[i] = ((u16)(str[i])<<8);
383 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
389 * Test whether a character is in the RX buffer
392 int usbtty_tstc(struct stdio_dev *dev)
394 struct usb_endpoint_instance *endpoint =
395 &endpoint_instance[rx_endpoint];
397 /* If no input data exists, allow more RX to be accepted */
398 if(usbtty_input.size <= 0){
399 udc_unset_nak(endpoint->endpoint_address&0x03);
403 return (usbtty_input.size > 0);
407 * Read a single byte from the usb client port. Returns 1 on success, 0
408 * otherwise. When the function is succesfull, the character read is
409 * written into its argument c.
412 int usbtty_getc(struct stdio_dev *dev)
415 struct usb_endpoint_instance *endpoint =
416 &endpoint_instance[rx_endpoint];
418 while (usbtty_input.size <= 0) {
419 udc_unset_nak(endpoint->endpoint_address&0x03);
423 buf_pop (&usbtty_input, &c, 1);
424 udc_set_nak(endpoint->endpoint_address&0x03);
430 * Output a single byte to the usb client port.
432 void usbtty_putc(struct stdio_dev *dev, const char c)
434 if (!usbtty_configured ())
437 /* If \n, also do \r */
439 buf_push (&usbtty_output, "\r", 1);
441 buf_push(&usbtty_output, &c, 1);
443 /* Poll at end to handle new data... */
444 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
449 /* usbtty_puts() helper function for finding the next '\n' in a string */
450 static int next_nl_pos (const char *s)
454 for (i = 0; s[i] != '\0'; i++) {
462 * Output a string to the usb client port - implementing flow control
465 static void __usbtty_puts (const char *str, int len)
467 int maxlen = usbtty_output.totalsize;
470 /* break str into chunks < buffer size, if needed */
474 space = maxlen - usbtty_output.size;
475 /* Empty buffer here, if needed, to ensure space... */
477 write_buffer (&usbtty_output);
479 n = min(space, min(len, maxlen));
480 buf_push (&usbtty_output, str, n);
488 void usbtty_puts(struct stdio_dev *dev, const char *str)
493 if (!usbtty_configured ())
497 /* add '\r' for each '\n' */
499 n = next_nl_pos (str);
501 if (str[n] == '\n') {
502 __usbtty_puts("\r", 1);
503 __usbtty_puts(str, n + 1);
507 /* No \n found. All done. */
508 __usbtty_puts (str, n);
513 /* Poll at end to handle new data... */
518 * Initialize the usb client port.
521 int drv_usbtty_init (void)
528 /* Ger seiral number */
529 if (!(sn = getenv("serial#"))) {
533 if (snlen > sizeof(serial_number) - 1) {
534 printf ("Warning: serial number %s is too long (%d > %lu)\n",
535 sn, snlen, (ulong)(sizeof(serial_number) - 1));
536 snlen = sizeof(serial_number) - 1;
538 memcpy (serial_number, sn, snlen);
539 serial_number[snlen] = '\0';
541 /* Decide on which type of UDC device to be.
544 if(!(tt = getenv("usbtty"))) {
547 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
549 /* prepare buffers... */
550 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
551 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
553 /* Now, set up USB controller and infrastructure */
554 udc_init (); /* Basic USB initialization */
556 usbtty_init_strings ();
557 usbtty_init_instances ();
559 usbtty_init_endpoints ();
561 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
562 udc_connect (); /* Enable pullup for host detection */
564 /* Device initialization */
565 memset (&usbttydev, 0, sizeof (usbttydev));
567 strcpy (usbttydev.name, "usbtty");
568 usbttydev.ext = 0; /* No extensions */
569 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
570 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
571 usbttydev.getc = usbtty_getc; /* 'getc' function */
572 usbttydev.putc = usbtty_putc; /* 'putc' function */
573 usbttydev.puts = usbtty_puts; /* 'puts' function */
575 rc = stdio_register (&usbttydev);
577 return (rc == 0) ? 1 : rc;
580 static void usbtty_init_strings (void)
582 struct usb_string_descriptor *string;
584 usbtty_string_table[STR_LANG] =
585 (struct usb_string_descriptor*)wstrLang;
587 string = (struct usb_string_descriptor *) wstrManufacturer;
588 string->bLength = sizeof(wstrManufacturer);
589 string->bDescriptorType = USB_DT_STRING;
590 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
591 usbtty_string_table[STR_MANUFACTURER]=string;
594 string = (struct usb_string_descriptor *) wstrProduct;
595 string->bLength = sizeof(wstrProduct);
596 string->bDescriptorType = USB_DT_STRING;
597 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
598 usbtty_string_table[STR_PRODUCT]=string;
601 string = (struct usb_string_descriptor *) wstrSerial;
602 string->bLength = sizeof(serial_number);
603 string->bDescriptorType = USB_DT_STRING;
604 str2wide (serial_number, string->wData);
605 usbtty_string_table[STR_SERIAL]=string;
608 string = (struct usb_string_descriptor *) wstrConfiguration;
609 string->bLength = sizeof(wstrConfiguration);
610 string->bDescriptorType = USB_DT_STRING;
611 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
612 usbtty_string_table[STR_CONFIG]=string;
615 string = (struct usb_string_descriptor *) wstrDataInterface;
616 string->bLength = sizeof(wstrDataInterface);
617 string->bDescriptorType = USB_DT_STRING;
618 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
619 usbtty_string_table[STR_DATA_INTERFACE]=string;
621 string = (struct usb_string_descriptor *) wstrCtrlInterface;
622 string->bLength = sizeof(wstrCtrlInterface);
623 string->bDescriptorType = USB_DT_STRING;
624 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
625 usbtty_string_table[STR_CTRL_INTERFACE]=string;
627 /* Now, initialize the string table for ep0 handling */
628 usb_strings = usbtty_string_table;
631 #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
632 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
634 static void usbtty_init_instances (void)
638 /* initialize device instance */
639 memset (device_instance, 0, sizeof (struct usb_device_instance));
640 device_instance->device_state = STATE_INIT;
641 device_instance->device_descriptor = &device_descriptor;
642 #if defined(CONFIG_USBD_HS)
643 device_instance->qualifier_descriptor = &qualifier_descriptor;
645 device_instance->event = usbtty_event_handler;
646 device_instance->cdc_recv_setup = usbtty_cdc_setup;
647 device_instance->bus = bus_instance;
648 device_instance->configurations = NUM_CONFIGS;
649 device_instance->configuration_instance_array = config_instance;
651 /* initialize bus instance */
652 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
653 bus_instance->device = device_instance;
654 bus_instance->endpoint_array = endpoint_instance;
655 bus_instance->max_endpoints = 1;
656 bus_instance->maxpacketsize = 64;
657 bus_instance->serial_number_str = serial_number;
659 /* configuration instance */
660 memset (config_instance, 0,
661 sizeof (struct usb_configuration_instance));
662 config_instance->interfaces = interface_count;
663 config_instance->configuration_descriptor = configuration_descriptor;
664 config_instance->interface_instance_array = interface_instance;
666 /* interface instance */
667 memset (interface_instance, 0,
668 sizeof (struct usb_interface_instance));
669 interface_instance->alternates = 1;
670 interface_instance->alternates_instance_array = alternate_instance;
672 /* alternates instance */
673 memset (alternate_instance, 0,
674 sizeof (struct usb_alternate_instance));
675 alternate_instance->interface_descriptor = interface_descriptors;
676 alternate_instance->endpoints = NUM_ENDPOINTS;
677 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
679 /* endpoint instances */
680 memset (&endpoint_instance[0], 0,
681 sizeof (struct usb_endpoint_instance));
682 endpoint_instance[0].endpoint_address = 0;
683 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
684 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
685 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
686 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
687 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
689 for (i = 1; i <= NUM_ENDPOINTS; i++) {
690 memset (&endpoint_instance[i], 0,
691 sizeof (struct usb_endpoint_instance));
693 endpoint_instance[i].endpoint_address =
694 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
696 endpoint_instance[i].rcv_attributes =
697 ep_descriptor_ptrs[i - 1]->bmAttributes;
699 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
701 endpoint_instance[i].tx_attributes =
702 ep_descriptor_ptrs[i - 1]->bmAttributes;
704 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
706 endpoint_instance[i].tx_attributes =
707 ep_descriptor_ptrs[i - 1]->bmAttributes;
709 urb_link_init (&endpoint_instance[i].rcv);
710 urb_link_init (&endpoint_instance[i].rdy);
711 urb_link_init (&endpoint_instance[i].tx);
712 urb_link_init (&endpoint_instance[i].done);
714 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
715 endpoint_instance[i].tx_urb =
716 usbd_alloc_urb (device_instance,
717 &endpoint_instance[i]);
719 endpoint_instance[i].rcv_urb =
720 usbd_alloc_urb (device_instance,
721 &endpoint_instance[i]);
725 static void usbtty_init_endpoints (void)
729 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
730 for (i = 1; i <= NUM_ENDPOINTS; i++) {
731 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
735 /* usbtty_init_terminal_type
737 * Do some late binding for our device type.
739 static void usbtty_init_terminal_type(short type)
744 /* Assign endpoint descriptors */
745 ep_descriptor_ptrs[0] =
746 &acm_configuration_descriptors[0].notification_endpoint;
747 ep_descriptor_ptrs[1] =
748 &acm_configuration_descriptors[0].data_endpoints[0];
749 ep_descriptor_ptrs[2] =
750 &acm_configuration_descriptors[0].data_endpoints[1];
752 /* Enumerate Device Descriptor */
753 device_descriptor.bDeviceClass =
754 COMMUNICATIONS_DEVICE_CLASS;
755 device_descriptor.idProduct =
756 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
758 #if defined(CONFIG_USBD_HS)
759 qualifier_descriptor.bDeviceClass =
760 COMMUNICATIONS_DEVICE_CLASS;
762 /* Assign endpoint indices */
763 tx_endpoint = ACM_TX_ENDPOINT;
764 rx_endpoint = ACM_RX_ENDPOINT;
766 /* Configuration Descriptor */
767 configuration_descriptor =
768 (struct usb_configuration_descriptor*)
769 &acm_configuration_descriptors;
771 /* Interface count */
772 interface_count = NUM_ACM_INTERFACES;
775 /* BULK IN/OUT & Default */
778 /* Assign endpoint descriptors */
779 ep_descriptor_ptrs[0] =
780 &gserial_configuration_descriptors[0].data_endpoints[0];
781 ep_descriptor_ptrs[1] =
782 &gserial_configuration_descriptors[0].data_endpoints[1];
783 ep_descriptor_ptrs[2] =
784 &gserial_configuration_descriptors[0].data_endpoints[2];
786 /* Enumerate Device Descriptor */
787 device_descriptor.bDeviceClass = 0xFF;
788 device_descriptor.idProduct =
789 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
790 #if defined(CONFIG_USBD_HS)
791 qualifier_descriptor.bDeviceClass = 0xFF;
793 /* Assign endpoint indices */
794 tx_endpoint = GSERIAL_TX_ENDPOINT;
795 rx_endpoint = GSERIAL_RX_ENDPOINT;
797 /* Configuration Descriptor */
798 configuration_descriptor =
799 (struct usb_configuration_descriptor*)
800 &gserial_configuration_descriptors;
802 /* Interface count */
803 interface_count = NUM_GSERIAL_INTERFACES;
808 /******************************************************************************/
810 static struct urb *next_urb (struct usb_device_instance *device,
811 struct usb_endpoint_instance *endpoint)
813 struct urb *current_urb = NULL;
816 /* If there's a queue, then we should add to the last urb */
817 if (!endpoint->tx_queue) {
818 current_urb = endpoint->tx_urb;
820 /* Last urb from tx chain */
822 p2surround (struct urb, link, endpoint->tx.prev);
825 /* Make sure this one has enough room */
826 space = current_urb->buffer_length - current_urb->actual_length;
829 } else { /* No space here */
830 /* First look at done list */
831 current_urb = first_urb_detached (&endpoint->done);
833 current_urb = usbd_alloc_urb (device, endpoint);
836 urb_append (&endpoint->tx, current_urb);
837 endpoint->tx_queue++;
842 static int write_buffer (circbuf_t * buf)
844 if (!usbtty_configured ()) {
848 struct usb_endpoint_instance *endpoint =
849 &endpoint_instance[tx_endpoint];
850 struct urb *current_urb = NULL;
852 current_urb = next_urb (device_instance, endpoint);
853 /* TX data still exists - send it now
855 if(endpoint->sent < current_urb->actual_length){
856 if(udc_endpoint_write (endpoint)){
857 /* Write pre-empted by RX */
869 /* Break buffer into urb sized pieces,
870 * and link each to the endpoint
872 while (buf->size > 0) {
875 TTYERR ("current_urb is NULL, buf->size %d\n",
880 dest = (char*)current_urb->buffer +
881 current_urb->actual_length;
884 current_urb->buffer_length -
885 current_urb->actual_length;
886 popnum = min(space_avail, (int)buf->size);
890 popped = buf_pop (buf, dest, popnum);
893 current_urb->actual_length += popped;
896 /* If endpoint->last == 0, then transfers have
897 * not started on this endpoint
899 if (endpoint->last == 0) {
900 if(udc_endpoint_write (endpoint)){
901 /* Write pre-empted by RX */
913 static int fill_buffer (circbuf_t * buf)
915 struct usb_endpoint_instance *endpoint =
916 &endpoint_instance[rx_endpoint];
918 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
920 char *src = (char *) endpoint->rcv_urb->buffer;
921 unsigned int rx_avail = buf->totalsize - buf->size;
923 if(rx_avail >= endpoint->rcv_urb->actual_length){
925 nb = endpoint->rcv_urb->actual_length;
926 buf_push (buf, src, nb);
927 endpoint->rcv_urb->actual_length = 0;
935 static int usbtty_configured (void)
937 return usbtty_configured_flag;
940 /******************************************************************************/
942 static void usbtty_event_handler (struct usb_device_instance *device,
943 usb_device_event_t event, int data)
945 #if defined(CONFIG_USBD_HS)
950 case DEVICE_BUS_INACTIVE:
951 usbtty_configured_flag = 0;
953 case DEVICE_CONFIGURED:
954 usbtty_configured_flag = 1;
957 case DEVICE_ADDRESS_ASSIGNED:
958 #if defined(CONFIG_USBD_HS)
960 * is_usbd_high_speed routine needs to be defined by
961 * specific gadget driver
962 * It returns true if device enumerates at High speed
963 * Retuns false otherwise
965 for (i = 0; i < NUM_ENDPOINTS; i++) {
966 if (((ep_descriptor_ptrs[i]->bmAttributes &
967 USB_ENDPOINT_XFERTYPE_MASK) ==
968 USB_ENDPOINT_XFER_BULK)
969 && is_usbd_high_speed()) {
971 ep_descriptor_ptrs[i]->wMaxPacketSize =
972 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
975 endpoint_instance[i + 1].tx_packetSize =
976 ep_descriptor_ptrs[i]->wMaxPacketSize;
977 endpoint_instance[i + 1].rcv_packetSize =
978 ep_descriptor_ptrs[i]->wMaxPacketSize;
981 usbtty_init_endpoints ();
988 /******************************************************************************/
990 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
992 switch (request->bRequest){
994 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
996 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
998 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
1001 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
1003 case ACM_GET_LINE_ENCODING : /* request DTE rate,
1004 * stop/parity bits */
1005 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1006 urb->actual_length = sizeof(rs232_desc);
1015 /******************************************************************************/
1018 * Since interrupt handling has not yet been implemented, we use this function
1019 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1020 * update the USB state.
1022 void usbtty_poll (void)
1024 /* New interrupts? */
1027 /* Write any output data to host buffer
1028 * (do this before checking interrupts to avoid missing one)
1030 if (usbtty_configured ()) {
1031 write_buffer (&usbtty_output);
1034 /* New interrupts? */
1037 /* Check for new data from host..
1038 * (do this after checking interrupts to get latest data)
1040 if (usbtty_configured ()) {
1041 fill_buffer (&usbtty_input);
1044 /* New interrupts? */