1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerry Hamel, geh@ti.com, Texas Instruments
7 * Bryan O'Donoghue, bodonoghue@codehermit.ie
15 #include <stdio_dev.h>
16 #include <asm/unaligned.h>
18 #include "usb_cdc_acm.h"
19 #include "usbdescriptors.h"
22 #define TTYDBG(fmt,args...)\
23 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
25 #define TTYDBG(fmt,args...) do{}while(0)
29 #define TTYERR(fmt,args...)\
30 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
33 #define TTYERR(fmt,args...) do{}while(0)
40 #define MAX_INTERFACES 2
41 #define NUM_ENDPOINTS 3
42 #define ACM_TX_ENDPOINT 3
43 #define ACM_RX_ENDPOINT 2
44 #define GSERIAL_TX_ENDPOINT 2
45 #define GSERIAL_RX_ENDPOINT 1
46 #define NUM_ACM_INTERFACES 2
47 #define NUM_GSERIAL_INTERFACES 1
48 #define CFG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
49 #define CFG_USBD_CTRL_INTERFACE_STR "Control Interface"
52 * Buffers to hold input and output data
54 #define USBTTY_BUFFER_SIZE 2048
55 static circbuf_t usbtty_input;
56 static circbuf_t usbtty_output;
62 static struct stdio_dev usbttydev;
63 static struct usb_device_instance device_instance[1];
64 static struct usb_bus_instance bus_instance[1];
65 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
66 static struct usb_interface_instance interface_instance[MAX_INTERFACES];
67 static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
68 /* one extra for control endpoint */
69 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
74 int usbtty_configured_flag = 0;
79 static char serial_number[16];
83 * Descriptors, Strings, Local variables.
86 /* defined and used by gadget/ep0.c */
87 extern struct usb_string_descriptor **usb_strings;
89 /* Indicies, References */
90 static unsigned short rx_endpoint = 0;
91 static unsigned short tx_endpoint = 0;
92 static unsigned short interface_count = 0;
93 static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
95 /* USB Descriptor Strings */
96 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
97 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
98 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
99 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
100 static u8 wstrConfiguration[2 + 2*(sizeof(CFG_USBD_CONFIGURATION_STR)-1)];
101 static u8 wstrDataInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
102 static u8 wstrCtrlInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
104 /* Standard USB Data Structures */
105 static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
106 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
107 static struct usb_configuration_descriptor *configuration_descriptor = 0;
108 static struct usb_device_descriptor device_descriptor = {
109 .bLength = sizeof(struct usb_device_descriptor),
110 .bDescriptorType = USB_DT_DEVICE,
111 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
112 .bDeviceSubClass = 0x00,
113 .bDeviceProtocol = 0x00,
114 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
115 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
116 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
117 .iManufacturer = STR_MANUFACTURER,
118 .iProduct = STR_PRODUCT,
119 .iSerialNumber = STR_SERIAL,
120 .bNumConfigurations = NUM_CONFIGS
124 * Static CDC ACM specific descriptors
127 struct acm_config_desc {
128 struct usb_configuration_descriptor configuration_desc;
130 /* Master Interface */
131 struct usb_interface_descriptor interface_desc;
133 struct usb_class_header_function_descriptor usb_class_header;
134 struct usb_class_call_management_descriptor usb_class_call_mgt;
135 struct usb_class_abstract_control_descriptor usb_class_acm;
136 struct usb_class_union_function_descriptor usb_class_union;
137 struct usb_endpoint_descriptor notification_endpoint;
139 /* Slave Interface */
140 struct usb_interface_descriptor data_class_interface;
141 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
142 } __attribute__((packed));
144 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
146 .configuration_desc ={
148 sizeof(struct usb_configuration_descriptor),
149 .bDescriptorType = USB_DT_CONFIG,
151 cpu_to_le16(sizeof(struct acm_config_desc)),
152 .bNumInterfaces = NUM_ACM_INTERFACES,
153 .bConfigurationValue = 1,
154 .iConfiguration = STR_CONFIG,
156 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
157 .bMaxPower = USBTTY_MAXPOWER
161 .bLength = sizeof(struct usb_interface_descriptor),
162 .bDescriptorType = USB_DT_INTERFACE,
163 .bInterfaceNumber = 0,
164 .bAlternateSetting = 0,
165 .bNumEndpoints = 0x01,
167 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
168 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
169 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
170 .iInterface = STR_CTRL_INTERFACE,
172 .usb_class_header = {
174 sizeof(struct usb_class_header_function_descriptor),
175 .bDescriptorType = CS_INTERFACE,
176 .bDescriptorSubtype = USB_ST_HEADER,
177 .bcdCDC = cpu_to_le16(110),
179 .usb_class_call_mgt = {
181 sizeof(struct usb_class_call_management_descriptor),
182 .bDescriptorType = CS_INTERFACE,
183 .bDescriptorSubtype = USB_ST_CMF,
184 .bmCapabilities = 0x00,
185 .bDataInterface = 0x01,
189 sizeof(struct usb_class_abstract_control_descriptor),
190 .bDescriptorType = CS_INTERFACE,
191 .bDescriptorSubtype = USB_ST_ACMF,
192 .bmCapabilities = 0x00,
196 sizeof(struct usb_class_union_function_descriptor),
197 .bDescriptorType = CS_INTERFACE,
198 .bDescriptorSubtype = USB_ST_UF,
199 .bMasterInterface = 0x00,
200 .bSlaveInterface0 = 0x01,
202 .notification_endpoint = {
204 sizeof(struct usb_endpoint_descriptor),
205 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
209 = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
214 .data_class_interface = {
216 sizeof(struct usb_interface_descriptor),
217 .bDescriptorType = USB_DT_INTERFACE,
218 .bInterfaceNumber = 0x01,
219 .bAlternateSetting = 0x00,
220 .bNumEndpoints = 0x02,
222 COMMUNICATIONS_INTERFACE_CLASS_DATA,
223 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
224 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
225 .iInterface = STR_DATA_INTERFACE,
230 sizeof(struct usb_endpoint_descriptor),
231 .bDescriptorType = USB_DT_ENDPOINT,
232 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
234 USB_ENDPOINT_XFER_BULK,
236 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
241 sizeof(struct usb_endpoint_descriptor),
242 .bDescriptorType = USB_DT_ENDPOINT,
243 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
245 USB_ENDPOINT_XFER_BULK,
247 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
254 static struct rs232_emu rs232_desc={
263 * Static Generic Serial specific data
267 struct gserial_config_desc {
269 struct usb_configuration_descriptor configuration_desc;
270 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
271 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
273 } __attribute__((packed));
275 static struct gserial_config_desc
276 gserial_configuration_descriptors[NUM_CONFIGS] ={
278 .configuration_desc ={
279 .bLength = sizeof(struct usb_configuration_descriptor),
280 .bDescriptorType = USB_DT_CONFIG,
282 cpu_to_le16(sizeof(struct gserial_config_desc)),
283 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
284 .bConfigurationValue = 1,
285 .iConfiguration = STR_CONFIG,
287 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
288 .bMaxPower = USBTTY_MAXPOWER
293 sizeof(struct usb_interface_descriptor),
294 .bDescriptorType = USB_DT_INTERFACE,
295 .bInterfaceNumber = 0,
296 .bAlternateSetting = 0,
297 .bNumEndpoints = NUM_ENDPOINTS,
299 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
300 .bInterfaceSubClass =
301 COMMUNICATIONS_NO_SUBCLASS,
302 .bInterfaceProtocol =
303 COMMUNICATIONS_NO_PROTOCOL,
304 .iInterface = STR_DATA_INTERFACE
310 sizeof(struct usb_endpoint_descriptor),
311 .bDescriptorType = USB_DT_ENDPOINT,
312 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE),
320 sizeof(struct usb_endpoint_descriptor),
321 .bDescriptorType = USB_DT_ENDPOINT,
322 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
325 cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE),
330 sizeof(struct usb_endpoint_descriptor),
331 .bDescriptorType = USB_DT_ENDPOINT,
332 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
333 .bmAttributes = USB_ENDPOINT_XFER_INT,
335 cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
343 * Static Function Prototypes
346 static void usbtty_init_strings (void);
347 static void usbtty_init_instances (void);
348 static void usbtty_init_endpoints (void);
349 static void usbtty_init_terminal_type(short type);
350 static void usbtty_event_handler (struct usb_device_instance *device,
351 usb_device_event_t event, int data);
352 static int usbtty_cdc_setup(struct usb_device_request *request,
354 static int usbtty_configured (void);
355 static int write_buffer (circbuf_t * buf);
356 static int fill_buffer (circbuf_t * buf);
358 void usbtty_poll (void);
360 /* utility function for converting char* to wide string used by USB */
361 static void str2wide (char *str, u16 * wide)
364 for (i = 0; i < strlen (str) && str[i]; i++){
365 #if defined(__LITTLE_ENDIAN)
366 wide[i] = (u16) str[i];
367 #elif defined(__BIG_ENDIAN)
368 wide[i] = ((u16)(str[i])<<8);
370 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
376 * Test whether a character is in the RX buffer
379 int usbtty_tstc(struct stdio_dev *dev)
381 struct usb_endpoint_instance *endpoint =
382 &endpoint_instance[rx_endpoint];
384 /* If no input data exists, allow more RX to be accepted */
385 if(usbtty_input.size <= 0){
386 udc_unset_nak(endpoint->endpoint_address&0x03);
390 return (usbtty_input.size > 0);
394 * Read a single byte from the usb client port. Returns 1 on success, 0
395 * otherwise. When the function is succesfull, the character read is
396 * written into its argument c.
399 int usbtty_getc(struct stdio_dev *dev)
402 struct usb_endpoint_instance *endpoint =
403 &endpoint_instance[rx_endpoint];
405 while (usbtty_input.size <= 0) {
406 udc_unset_nak(endpoint->endpoint_address&0x03);
410 buf_pop (&usbtty_input, &c, 1);
411 udc_set_nak(endpoint->endpoint_address&0x03);
417 * Output a single byte to the usb client port.
419 void usbtty_putc(struct stdio_dev *dev, const char c)
421 if (!usbtty_configured ())
424 /* If \n, also do \r */
426 buf_push (&usbtty_output, "\r", 1);
428 buf_push(&usbtty_output, &c, 1);
430 /* Poll at end to handle new data... */
431 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
436 /* usbtty_puts() helper function for finding the next '\n' in a string */
437 static int next_nl_pos (const char *s)
441 for (i = 0; s[i] != '\0'; i++) {
449 * Output a string to the usb client port - implementing flow control
452 static void __usbtty_puts (const char *str, int len)
454 int maxlen = usbtty_output.totalsize;
457 /* break str into chunks < buffer size, if needed */
461 space = maxlen - usbtty_output.size;
462 /* Empty buffer here, if needed, to ensure space... */
464 write_buffer (&usbtty_output);
466 n = min(space, min(len, maxlen));
467 buf_push (&usbtty_output, str, n);
475 void usbtty_puts(struct stdio_dev *dev, const char *str)
480 if (!usbtty_configured ())
484 /* add '\r' for each '\n' */
486 n = next_nl_pos (str);
488 if (str[n] == '\n') {
489 __usbtty_puts(str, n);
490 __usbtty_puts("\r\n", 2);
494 /* No \n found. All done. */
495 __usbtty_puts (str, n);
500 /* Poll at end to handle new data... */
505 * Initialize the usb client port.
508 int drv_usbtty_init (void)
515 /* Get serial number */
516 sn = env_get("serial#");
520 if (snlen > sizeof(serial_number) - 1) {
521 printf ("Warning: serial number %s is too long (%d > %lu)\n",
522 sn, snlen, (ulong)(sizeof(serial_number) - 1));
523 snlen = sizeof(serial_number) - 1;
525 memcpy (serial_number, sn, snlen);
526 serial_number[snlen] = '\0';
528 /* Decide on which type of UDC device to be.
530 tt = env_get("usbtty");
533 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
535 /* prepare buffers... */
536 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
537 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
539 /* Now, set up USB controller and infrastructure */
540 udc_init (); /* Basic USB initialization */
542 usbtty_init_strings ();
543 usbtty_init_instances ();
545 usbtty_init_endpoints ();
547 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
548 udc_connect (); /* Enable pullup for host detection */
550 /* Device initialization */
551 memset (&usbttydev, 0, sizeof (usbttydev));
553 strcpy (usbttydev.name, "usbtty");
554 usbttydev.ext = 0; /* No extensions */
555 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
556 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
557 usbttydev.getc = usbtty_getc; /* 'getc' function */
558 usbttydev.putc = usbtty_putc; /* 'putc' function */
559 usbttydev.puts = usbtty_puts; /* 'puts' function */
561 rc = stdio_register (&usbttydev);
563 return (rc == 0) ? 1 : rc;
566 static void usbtty_init_strings (void)
568 struct usb_string_descriptor *string;
570 usbtty_string_table[STR_LANG] =
571 (struct usb_string_descriptor*)wstrLang;
573 string = (struct usb_string_descriptor *) wstrManufacturer;
574 string->bLength = sizeof(wstrManufacturer);
575 string->bDescriptorType = USB_DT_STRING;
576 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
577 usbtty_string_table[STR_MANUFACTURER]=string;
580 string = (struct usb_string_descriptor *) wstrProduct;
581 string->bLength = sizeof(wstrProduct);
582 string->bDescriptorType = USB_DT_STRING;
583 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
584 usbtty_string_table[STR_PRODUCT]=string;
587 string = (struct usb_string_descriptor *) wstrSerial;
588 string->bLength = sizeof(serial_number);
589 string->bDescriptorType = USB_DT_STRING;
590 str2wide (serial_number, string->wData);
591 usbtty_string_table[STR_SERIAL]=string;
594 string = (struct usb_string_descriptor *) wstrConfiguration;
595 string->bLength = sizeof(wstrConfiguration);
596 string->bDescriptorType = USB_DT_STRING;
597 str2wide (CFG_USBD_CONFIGURATION_STR, string->wData);
598 usbtty_string_table[STR_CONFIG]=string;
601 string = (struct usb_string_descriptor *) wstrDataInterface;
602 string->bLength = sizeof(wstrDataInterface);
603 string->bDescriptorType = USB_DT_STRING;
604 str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData);
605 usbtty_string_table[STR_DATA_INTERFACE]=string;
607 string = (struct usb_string_descriptor *) wstrCtrlInterface;
608 string->bLength = sizeof(wstrCtrlInterface);
609 string->bDescriptorType = USB_DT_STRING;
610 str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData);
611 usbtty_string_table[STR_CTRL_INTERFACE]=string;
613 /* Now, initialize the string table for ep0 handling */
614 usb_strings = usbtty_string_table;
617 #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
618 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
620 static void usbtty_init_instances (void)
624 /* initialize device instance */
625 memset (device_instance, 0, sizeof (struct usb_device_instance));
626 device_instance->device_state = STATE_INIT;
627 device_instance->device_descriptor = &device_descriptor;
628 device_instance->event = usbtty_event_handler;
629 device_instance->cdc_recv_setup = usbtty_cdc_setup;
630 device_instance->bus = bus_instance;
631 device_instance->configurations = NUM_CONFIGS;
632 device_instance->configuration_instance_array = config_instance;
634 /* initialize bus instance */
635 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
636 bus_instance->device = device_instance;
637 bus_instance->endpoint_array = endpoint_instance;
638 bus_instance->max_endpoints = 1;
639 bus_instance->maxpacketsize = 64;
640 bus_instance->serial_number_str = serial_number;
642 /* configuration instance */
643 memset (config_instance, 0,
644 sizeof (struct usb_configuration_instance));
645 config_instance->interfaces = interface_count;
646 config_instance->configuration_descriptor = configuration_descriptor;
647 config_instance->interface_instance_array = interface_instance;
649 /* interface instance */
650 memset (interface_instance, 0,
651 sizeof (struct usb_interface_instance));
652 interface_instance->alternates = 1;
653 interface_instance->alternates_instance_array = alternate_instance;
655 /* alternates instance */
656 memset (alternate_instance, 0,
657 sizeof (struct usb_alternate_instance));
658 alternate_instance->interface_descriptor = interface_descriptors;
659 alternate_instance->endpoints = NUM_ENDPOINTS;
660 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
662 /* endpoint instances */
663 memset (&endpoint_instance[0], 0,
664 sizeof (struct usb_endpoint_instance));
665 endpoint_instance[0].endpoint_address = 0;
666 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
667 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
668 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
669 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
670 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
672 for (i = 1; i <= NUM_ENDPOINTS; i++) {
673 memset (&endpoint_instance[i], 0,
674 sizeof (struct usb_endpoint_instance));
676 endpoint_instance[i].endpoint_address =
677 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
679 endpoint_instance[i].rcv_attributes =
680 ep_descriptor_ptrs[i - 1]->bmAttributes;
682 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
684 endpoint_instance[i].tx_attributes =
685 ep_descriptor_ptrs[i - 1]->bmAttributes;
687 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
689 endpoint_instance[i].tx_attributes =
690 ep_descriptor_ptrs[i - 1]->bmAttributes;
692 urb_link_init (&endpoint_instance[i].rcv);
693 urb_link_init (&endpoint_instance[i].rdy);
694 urb_link_init (&endpoint_instance[i].tx);
695 urb_link_init (&endpoint_instance[i].done);
697 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
698 endpoint_instance[i].tx_urb =
699 usbd_alloc_urb (device_instance,
700 &endpoint_instance[i]);
702 endpoint_instance[i].rcv_urb =
703 usbd_alloc_urb (device_instance,
704 &endpoint_instance[i]);
708 static void usbtty_init_endpoints (void)
712 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
713 for (i = 1; i <= NUM_ENDPOINTS; i++) {
714 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
718 /* usbtty_init_terminal_type
720 * Do some late binding for our device type.
722 static void usbtty_init_terminal_type(short type)
727 /* Assign endpoint descriptors */
728 ep_descriptor_ptrs[0] =
729 &acm_configuration_descriptors[0].notification_endpoint;
730 ep_descriptor_ptrs[1] =
731 &acm_configuration_descriptors[0].data_endpoints[0];
732 ep_descriptor_ptrs[2] =
733 &acm_configuration_descriptors[0].data_endpoints[1];
735 /* Enumerate Device Descriptor */
736 device_descriptor.bDeviceClass =
737 COMMUNICATIONS_DEVICE_CLASS;
738 device_descriptor.idProduct =
739 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
741 /* Assign endpoint indices */
742 tx_endpoint = ACM_TX_ENDPOINT;
743 rx_endpoint = ACM_RX_ENDPOINT;
745 /* Configuration Descriptor */
746 configuration_descriptor =
747 (struct usb_configuration_descriptor*)
748 &acm_configuration_descriptors;
750 /* Interface count */
751 interface_count = NUM_ACM_INTERFACES;
754 /* BULK IN/OUT & Default */
757 /* Assign endpoint descriptors */
758 ep_descriptor_ptrs[0] =
759 &gserial_configuration_descriptors[0].data_endpoints[0];
760 ep_descriptor_ptrs[1] =
761 &gserial_configuration_descriptors[0].data_endpoints[1];
762 ep_descriptor_ptrs[2] =
763 &gserial_configuration_descriptors[0].data_endpoints[2];
765 /* Enumerate Device Descriptor */
766 device_descriptor.bDeviceClass = 0xFF;
767 device_descriptor.idProduct =
768 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
769 /* Assign endpoint indices */
770 tx_endpoint = GSERIAL_TX_ENDPOINT;
771 rx_endpoint = GSERIAL_RX_ENDPOINT;
773 /* Configuration Descriptor */
774 configuration_descriptor =
775 (struct usb_configuration_descriptor*)
776 &gserial_configuration_descriptors;
778 /* Interface count */
779 interface_count = NUM_GSERIAL_INTERFACES;
784 /******************************************************************************/
786 static struct urb *next_urb (struct usb_device_instance *device,
787 struct usb_endpoint_instance *endpoint)
789 struct urb *current_urb = NULL;
792 /* If there's a queue, then we should add to the last urb */
793 if (!endpoint->tx_queue) {
794 current_urb = endpoint->tx_urb;
796 /* Last urb from tx chain */
798 p2surround (struct urb, link, endpoint->tx.prev);
801 /* Make sure this one has enough room */
802 space = current_urb->buffer_length - current_urb->actual_length;
805 } else { /* No space here */
806 /* First look at done list */
807 current_urb = first_urb_detached (&endpoint->done);
809 current_urb = usbd_alloc_urb (device, endpoint);
812 urb_append (&endpoint->tx, current_urb);
813 endpoint->tx_queue++;
818 static int write_buffer (circbuf_t * buf)
820 if (!usbtty_configured ()) {
824 struct usb_endpoint_instance *endpoint =
825 &endpoint_instance[tx_endpoint];
826 struct urb *current_urb = NULL;
828 /* TX data still exists - send it now
830 if(endpoint->sent < endpoint->tx_urb->actual_length){
831 if(udc_endpoint_write (endpoint)){
832 /* Write pre-empted by RX */
844 /* Break buffer into urb sized pieces,
845 * and link each to the endpoint
847 while (buf->size > 0) {
849 current_urb = next_urb (device_instance, endpoint);
851 dest = (char*)current_urb->buffer +
852 current_urb->actual_length;
855 current_urb->buffer_length -
856 current_urb->actual_length;
857 popnum = min(space_avail, (int)buf->size);
861 popped = buf_pop (buf, dest, popnum);
864 current_urb->actual_length += popped;
867 /* If endpoint->last == 0, then transfers have
868 * not started on this endpoint
870 if (endpoint->last == 0) {
871 if(udc_endpoint_write (endpoint)){
872 /* Write pre-empted by RX */
884 static int fill_buffer (circbuf_t * buf)
886 struct usb_endpoint_instance *endpoint =
887 &endpoint_instance[rx_endpoint];
889 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
891 char *src = (char *) endpoint->rcv_urb->buffer;
892 unsigned int rx_avail = buf->totalsize - buf->size;
894 if(rx_avail >= endpoint->rcv_urb->actual_length){
896 nb = endpoint->rcv_urb->actual_length;
897 buf_push (buf, src, nb);
898 endpoint->rcv_urb->actual_length = 0;
906 static int usbtty_configured (void)
908 return usbtty_configured_flag;
911 /******************************************************************************/
913 static void usbtty_event_handler (struct usb_device_instance *device,
914 usb_device_event_t event, int data)
918 case DEVICE_BUS_INACTIVE:
919 usbtty_configured_flag = 0;
921 case DEVICE_CONFIGURED:
922 usbtty_configured_flag = 1;
925 case DEVICE_ADDRESS_ASSIGNED:
926 usbtty_init_endpoints ();
933 /******************************************************************************/
935 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
937 switch (request->bRequest){
939 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
941 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
943 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
946 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
948 case ACM_GET_LINE_ENCODING : /* request DTE rate,
949 * stop/parity bits */
950 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
951 urb->actual_length = sizeof(rs232_desc);
960 /******************************************************************************/
963 * Since interrupt handling has not yet been implemented, we use this function
964 * to handle polling. This is called by the tstc,getc,putc,puts routines to
965 * update the USB state.
967 void usbtty_poll (void)
969 /* New interrupts? */
972 /* Write any output data to host buffer
973 * (do this before checking interrupts to avoid missing one)
975 if (usbtty_configured ()) {
976 write_buffer (&usbtty_output);
979 /* New interrupts? */
982 /* Check for new data from host..
983 * (do this after checking interrupts to get latest data)
985 if (usbtty_configured ()) {
986 fill_buffer (&usbtty_input);
989 /* New interrupts? */