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 (void)
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 (void)
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 (const char c)
434 if (!usbtty_configured ())
437 buf_push (&usbtty_output, &c, 1);
438 /* If \n, also do \r */
440 buf_push (&usbtty_output, "\r", 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 (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 (str, n + 1);
502 __usbtty_puts ("\r", 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 /* Ger seiral number */
528 if (!(sn = getenv("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.
543 if(!(tt = getenv("usbtty"))) {
546 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
548 /* prepare buffers... */
549 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
550 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
552 /* Now, set up USB controller and infrastructure */
553 udc_init (); /* Basic USB initialization */
555 usbtty_init_strings ();
556 usbtty_init_instances ();
558 usbtty_init_endpoints ();
560 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
561 udc_connect (); /* Enable pullup for host detection */
563 /* Device initialization */
564 memset (&usbttydev, 0, sizeof (usbttydev));
566 strcpy (usbttydev.name, "usbtty");
567 usbttydev.ext = 0; /* No extensions */
568 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
569 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
570 usbttydev.getc = usbtty_getc; /* 'getc' function */
571 usbttydev.putc = usbtty_putc; /* 'putc' function */
572 usbttydev.puts = usbtty_puts; /* 'puts' function */
574 rc = stdio_register (&usbttydev);
576 return (rc == 0) ? 1 : rc;
579 static void usbtty_init_strings (void)
581 struct usb_string_descriptor *string;
583 usbtty_string_table[STR_LANG] =
584 (struct usb_string_descriptor*)wstrLang;
586 string = (struct usb_string_descriptor *) wstrManufacturer;
587 string->bLength = sizeof(wstrManufacturer);
588 string->bDescriptorType = USB_DT_STRING;
589 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
590 usbtty_string_table[STR_MANUFACTURER]=string;
593 string = (struct usb_string_descriptor *) wstrProduct;
594 string->bLength = sizeof(wstrProduct);
595 string->bDescriptorType = USB_DT_STRING;
596 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
597 usbtty_string_table[STR_PRODUCT]=string;
600 string = (struct usb_string_descriptor *) wstrSerial;
601 string->bLength = sizeof(serial_number);
602 string->bDescriptorType = USB_DT_STRING;
603 str2wide (serial_number, string->wData);
604 usbtty_string_table[STR_SERIAL]=string;
607 string = (struct usb_string_descriptor *) wstrConfiguration;
608 string->bLength = sizeof(wstrConfiguration);
609 string->bDescriptorType = USB_DT_STRING;
610 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
611 usbtty_string_table[STR_CONFIG]=string;
614 string = (struct usb_string_descriptor *) wstrDataInterface;
615 string->bLength = sizeof(wstrDataInterface);
616 string->bDescriptorType = USB_DT_STRING;
617 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
618 usbtty_string_table[STR_DATA_INTERFACE]=string;
620 string = (struct usb_string_descriptor *) wstrCtrlInterface;
621 string->bLength = sizeof(wstrCtrlInterface);
622 string->bDescriptorType = USB_DT_STRING;
623 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
624 usbtty_string_table[STR_CTRL_INTERFACE]=string;
626 /* Now, initialize the string table for ep0 handling */
627 usb_strings = usbtty_string_table;
630 #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
631 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
633 static void usbtty_init_instances (void)
637 /* initialize device instance */
638 memset (device_instance, 0, sizeof (struct usb_device_instance));
639 device_instance->device_state = STATE_INIT;
640 device_instance->device_descriptor = &device_descriptor;
641 #if defined(CONFIG_USBD_HS)
642 device_instance->qualifier_descriptor = &qualifier_descriptor;
644 device_instance->event = usbtty_event_handler;
645 device_instance->cdc_recv_setup = usbtty_cdc_setup;
646 device_instance->bus = bus_instance;
647 device_instance->configurations = NUM_CONFIGS;
648 device_instance->configuration_instance_array = config_instance;
650 /* initialize bus instance */
651 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
652 bus_instance->device = device_instance;
653 bus_instance->endpoint_array = endpoint_instance;
654 bus_instance->max_endpoints = 1;
655 bus_instance->maxpacketsize = 64;
656 bus_instance->serial_number_str = serial_number;
658 /* configuration instance */
659 memset (config_instance, 0,
660 sizeof (struct usb_configuration_instance));
661 config_instance->interfaces = interface_count;
662 config_instance->configuration_descriptor = configuration_descriptor;
663 config_instance->interface_instance_array = interface_instance;
665 /* interface instance */
666 memset (interface_instance, 0,
667 sizeof (struct usb_interface_instance));
668 interface_instance->alternates = 1;
669 interface_instance->alternates_instance_array = alternate_instance;
671 /* alternates instance */
672 memset (alternate_instance, 0,
673 sizeof (struct usb_alternate_instance));
674 alternate_instance->interface_descriptor = interface_descriptors;
675 alternate_instance->endpoints = NUM_ENDPOINTS;
676 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
678 /* endpoint instances */
679 memset (&endpoint_instance[0], 0,
680 sizeof (struct usb_endpoint_instance));
681 endpoint_instance[0].endpoint_address = 0;
682 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
683 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
684 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
685 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
686 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
688 for (i = 1; i <= NUM_ENDPOINTS; i++) {
689 memset (&endpoint_instance[i], 0,
690 sizeof (struct usb_endpoint_instance));
692 endpoint_instance[i].endpoint_address =
693 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
695 endpoint_instance[i].rcv_attributes =
696 ep_descriptor_ptrs[i - 1]->bmAttributes;
698 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
700 endpoint_instance[i].tx_attributes =
701 ep_descriptor_ptrs[i - 1]->bmAttributes;
703 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
705 endpoint_instance[i].tx_attributes =
706 ep_descriptor_ptrs[i - 1]->bmAttributes;
708 urb_link_init (&endpoint_instance[i].rcv);
709 urb_link_init (&endpoint_instance[i].rdy);
710 urb_link_init (&endpoint_instance[i].tx);
711 urb_link_init (&endpoint_instance[i].done);
713 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
714 endpoint_instance[i].tx_urb =
715 usbd_alloc_urb (device_instance,
716 &endpoint_instance[i]);
718 endpoint_instance[i].rcv_urb =
719 usbd_alloc_urb (device_instance,
720 &endpoint_instance[i]);
724 static void usbtty_init_endpoints (void)
728 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
729 for (i = 1; i <= NUM_ENDPOINTS; i++) {
730 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
734 /* usbtty_init_terminal_type
736 * Do some late binding for our device type.
738 static void usbtty_init_terminal_type(short type)
743 /* Assign endpoint descriptors */
744 ep_descriptor_ptrs[0] =
745 &acm_configuration_descriptors[0].notification_endpoint;
746 ep_descriptor_ptrs[1] =
747 &acm_configuration_descriptors[0].data_endpoints[0];
748 ep_descriptor_ptrs[2] =
749 &acm_configuration_descriptors[0].data_endpoints[1];
751 /* Enumerate Device Descriptor */
752 device_descriptor.bDeviceClass =
753 COMMUNICATIONS_DEVICE_CLASS;
754 device_descriptor.idProduct =
755 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
757 #if defined(CONFIG_USBD_HS)
758 qualifier_descriptor.bDeviceClass =
759 COMMUNICATIONS_DEVICE_CLASS;
761 /* Assign endpoint indices */
762 tx_endpoint = ACM_TX_ENDPOINT;
763 rx_endpoint = ACM_RX_ENDPOINT;
765 /* Configuration Descriptor */
766 configuration_descriptor =
767 (struct usb_configuration_descriptor*)
768 &acm_configuration_descriptors;
770 /* Interface count */
771 interface_count = NUM_ACM_INTERFACES;
774 /* BULK IN/OUT & Default */
777 /* Assign endpoint descriptors */
778 ep_descriptor_ptrs[0] =
779 &gserial_configuration_descriptors[0].data_endpoints[0];
780 ep_descriptor_ptrs[1] =
781 &gserial_configuration_descriptors[0].data_endpoints[1];
782 ep_descriptor_ptrs[2] =
783 &gserial_configuration_descriptors[0].data_endpoints[2];
785 /* Enumerate Device Descriptor */
786 device_descriptor.bDeviceClass = 0xFF;
787 device_descriptor.idProduct =
788 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
789 #if defined(CONFIG_USBD_HS)
790 qualifier_descriptor.bDeviceClass = 0xFF;
792 /* Assign endpoint indices */
793 tx_endpoint = GSERIAL_TX_ENDPOINT;
794 rx_endpoint = GSERIAL_RX_ENDPOINT;
796 /* Configuration Descriptor */
797 configuration_descriptor =
798 (struct usb_configuration_descriptor*)
799 &gserial_configuration_descriptors;
801 /* Interface count */
802 interface_count = NUM_GSERIAL_INTERFACES;
807 /******************************************************************************/
809 static struct urb *next_urb (struct usb_device_instance *device,
810 struct usb_endpoint_instance *endpoint)
812 struct urb *current_urb = NULL;
815 /* If there's a queue, then we should add to the last urb */
816 if (!endpoint->tx_queue) {
817 current_urb = endpoint->tx_urb;
819 /* Last urb from tx chain */
821 p2surround (struct urb, link, endpoint->tx.prev);
824 /* Make sure this one has enough room */
825 space = current_urb->buffer_length - current_urb->actual_length;
828 } else { /* No space here */
829 /* First look at done list */
830 current_urb = first_urb_detached (&endpoint->done);
832 current_urb = usbd_alloc_urb (device, endpoint);
835 urb_append (&endpoint->tx, current_urb);
836 endpoint->tx_queue++;
841 static int write_buffer (circbuf_t * buf)
843 if (!usbtty_configured ()) {
847 struct usb_endpoint_instance *endpoint =
848 &endpoint_instance[tx_endpoint];
849 struct urb *current_urb = NULL;
851 current_urb = next_urb (device_instance, endpoint);
852 /* TX data still exists - send it now
854 if(endpoint->sent < current_urb->actual_length){
855 if(udc_endpoint_write (endpoint)){
856 /* Write pre-empted by RX */
868 /* Break buffer into urb sized pieces,
869 * and link each to the endpoint
871 while (buf->size > 0) {
874 TTYERR ("current_urb is NULL, buf->size %d\n",
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, 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? */