3 * Gerry Hamel, geh@ti.com, Texas Instruments
6 * Bryan O'Donoghue, bodonoghue@codehermit.ie
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <stdio_dev.h>
28 #include <asm/unaligned.h>
30 #include "usb_cdc_acm.h"
31 #include "usbdescriptors.h"
34 #define TTYDBG(fmt,args...)\
35 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
37 #define TTYDBG(fmt,args...) do{}while(0)
41 #define TTYERR(fmt,args...)\
42 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
45 #define TTYERR(fmt,args...) do{}while(0)
52 #define MAX_INTERFACES 2
53 #define NUM_ENDPOINTS 3
54 #define ACM_TX_ENDPOINT 3
55 #define ACM_RX_ENDPOINT 2
56 #define GSERIAL_TX_ENDPOINT 2
57 #define GSERIAL_RX_ENDPOINT 1
58 #define NUM_ACM_INTERFACES 2
59 #define NUM_GSERIAL_INTERFACES 1
60 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
61 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
64 * Buffers to hold input and output data
66 #define USBTTY_BUFFER_SIZE 2048
67 static circbuf_t usbtty_input;
68 static circbuf_t usbtty_output;
74 static struct stdio_dev usbttydev;
75 static struct usb_device_instance device_instance[1];
76 static struct usb_bus_instance bus_instance[1];
77 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
78 static struct usb_interface_instance interface_instance[MAX_INTERFACES];
79 static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
80 /* one extra for control endpoint */
81 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
86 int usbtty_configured_flag = 0;
91 static char serial_number[16];
95 * Descriptors, Strings, Local variables.
98 /* defined and used by gadget/ep0.c */
99 extern struct usb_string_descriptor **usb_strings;
101 /* Indicies, References */
102 static unsigned short rx_endpoint = 0;
103 static unsigned short tx_endpoint = 0;
104 static unsigned short interface_count = 0;
105 static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
107 /* USB Descriptor Strings */
108 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
109 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
110 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
111 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
112 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
113 static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
114 static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
116 /* Standard USB Data Structures */
117 static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
118 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
119 static struct usb_configuration_descriptor *configuration_descriptor = 0;
120 static struct usb_device_descriptor device_descriptor = {
121 .bLength = sizeof(struct usb_device_descriptor),
122 .bDescriptorType = USB_DT_DEVICE,
123 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
124 .bDeviceSubClass = 0x00,
125 .bDeviceProtocol = 0x00,
126 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
127 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
128 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
129 .iManufacturer = STR_MANUFACTURER,
130 .iProduct = STR_PRODUCT,
131 .iSerialNumber = STR_SERIAL,
132 .bNumConfigurations = NUM_CONFIGS
136 #if defined(CONFIG_USBD_HS)
137 static struct usb_qualifier_descriptor qualifier_descriptor = {
138 .bLength = sizeof(struct usb_qualifier_descriptor),
139 .bDescriptorType = USB_DT_QUAL,
140 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
141 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
142 .bDeviceSubClass = 0x00,
143 .bDeviceProtocol = 0x00,
144 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
145 .bNumConfigurations = NUM_CONFIGS
150 * Static CDC ACM specific descriptors
153 struct acm_config_desc {
154 struct usb_configuration_descriptor configuration_desc;
156 /* Master Interface */
157 struct usb_interface_descriptor interface_desc;
159 struct usb_class_header_function_descriptor usb_class_header;
160 struct usb_class_call_management_descriptor usb_class_call_mgt;
161 struct usb_class_abstract_control_descriptor usb_class_acm;
162 struct usb_class_union_function_descriptor usb_class_union;
163 struct usb_endpoint_descriptor notification_endpoint;
165 /* Slave Interface */
166 struct usb_interface_descriptor data_class_interface;
167 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
168 } __attribute__((packed));
170 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
172 .configuration_desc ={
174 sizeof(struct usb_configuration_descriptor),
175 .bDescriptorType = USB_DT_CONFIG,
177 cpu_to_le16(sizeof(struct acm_config_desc)),
178 .bNumInterfaces = NUM_ACM_INTERFACES,
179 .bConfigurationValue = 1,
180 .iConfiguration = STR_CONFIG,
182 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
183 .bMaxPower = USBTTY_MAXPOWER
187 .bLength = sizeof(struct usb_interface_descriptor),
188 .bDescriptorType = USB_DT_INTERFACE,
189 .bInterfaceNumber = 0,
190 .bAlternateSetting = 0,
191 .bNumEndpoints = 0x01,
193 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
194 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
195 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
196 .iInterface = STR_CTRL_INTERFACE,
198 .usb_class_header = {
200 sizeof(struct usb_class_header_function_descriptor),
201 .bDescriptorType = CS_INTERFACE,
202 .bDescriptorSubtype = USB_ST_HEADER,
203 .bcdCDC = cpu_to_le16(110),
205 .usb_class_call_mgt = {
207 sizeof(struct usb_class_call_management_descriptor),
208 .bDescriptorType = CS_INTERFACE,
209 .bDescriptorSubtype = USB_ST_CMF,
210 .bmCapabilities = 0x00,
211 .bDataInterface = 0x01,
215 sizeof(struct usb_class_abstract_control_descriptor),
216 .bDescriptorType = CS_INTERFACE,
217 .bDescriptorSubtype = USB_ST_ACMF,
218 .bmCapabilities = 0x00,
222 sizeof(struct usb_class_union_function_descriptor),
223 .bDescriptorType = CS_INTERFACE,
224 .bDescriptorSubtype = USB_ST_UF,
225 .bMasterInterface = 0x00,
226 .bSlaveInterface0 = 0x01,
228 .notification_endpoint = {
230 sizeof(struct usb_endpoint_descriptor),
231 .bDescriptorType = USB_DT_ENDPOINT,
232 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
233 .bmAttributes = USB_ENDPOINT_XFER_INT,
235 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
240 .data_class_interface = {
242 sizeof(struct usb_interface_descriptor),
243 .bDescriptorType = USB_DT_INTERFACE,
244 .bInterfaceNumber = 0x01,
245 .bAlternateSetting = 0x00,
246 .bNumEndpoints = 0x02,
248 COMMUNICATIONS_INTERFACE_CLASS_DATA,
249 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
250 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
251 .iInterface = STR_DATA_INTERFACE,
256 sizeof(struct usb_endpoint_descriptor),
257 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
260 USB_ENDPOINT_XFER_BULK,
262 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
267 sizeof(struct usb_endpoint_descriptor),
268 .bDescriptorType = USB_DT_ENDPOINT,
269 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
271 USB_ENDPOINT_XFER_BULK,
273 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
280 static struct rs232_emu rs232_desc={
289 * Static Generic Serial specific data
293 struct gserial_config_desc {
295 struct usb_configuration_descriptor configuration_desc;
296 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
297 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
299 } __attribute__((packed));
301 static struct gserial_config_desc
302 gserial_configuration_descriptors[NUM_CONFIGS] ={
304 .configuration_desc ={
305 .bLength = sizeof(struct usb_configuration_descriptor),
306 .bDescriptorType = USB_DT_CONFIG,
308 cpu_to_le16(sizeof(struct gserial_config_desc)),
309 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
310 .bConfigurationValue = 1,
311 .iConfiguration = STR_CONFIG,
313 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
314 .bMaxPower = USBTTY_MAXPOWER
319 sizeof(struct usb_interface_descriptor),
320 .bDescriptorType = USB_DT_INTERFACE,
321 .bInterfaceNumber = 0,
322 .bAlternateSetting = 0,
323 .bNumEndpoints = NUM_ENDPOINTS,
325 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
326 .bInterfaceSubClass =
327 COMMUNICATIONS_NO_SUBCLASS,
328 .bInterfaceProtocol =
329 COMMUNICATIONS_NO_PROTOCOL,
330 .iInterface = STR_DATA_INTERFACE
336 sizeof(struct usb_endpoint_descriptor),
337 .bDescriptorType = USB_DT_ENDPOINT,
338 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
339 .bmAttributes = USB_ENDPOINT_XFER_BULK,
341 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
346 sizeof(struct usb_endpoint_descriptor),
347 .bDescriptorType = USB_DT_ENDPOINT,
348 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
349 .bmAttributes = USB_ENDPOINT_XFER_BULK,
351 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
356 sizeof(struct usb_endpoint_descriptor),
357 .bDescriptorType = USB_DT_ENDPOINT,
358 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
359 .bmAttributes = USB_ENDPOINT_XFER_INT,
361 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
369 * Static Function Prototypes
372 static void usbtty_init_strings (void);
373 static void usbtty_init_instances (void);
374 static void usbtty_init_endpoints (void);
375 static void usbtty_init_terminal_type(short type);
376 static void usbtty_event_handler (struct usb_device_instance *device,
377 usb_device_event_t event, int data);
378 static int usbtty_cdc_setup(struct usb_device_request *request,
380 static int usbtty_configured (void);
381 static int write_buffer (circbuf_t * buf);
382 static int fill_buffer (circbuf_t * buf);
384 void usbtty_poll (void);
386 /* utility function for converting char* to wide string used by USB */
387 static void str2wide (char *str, u16 * wide)
390 for (i = 0; i < strlen (str) && str[i]; i++){
391 #if defined(__LITTLE_ENDIAN)
392 wide[i] = (u16) str[i];
393 #elif defined(__BIG_ENDIAN)
394 wide[i] = ((u16)(str[i])<<8);
396 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
402 * Test whether a character is in the RX buffer
405 int usbtty_tstc (void)
407 struct usb_endpoint_instance *endpoint =
408 &endpoint_instance[rx_endpoint];
410 /* If no input data exists, allow more RX to be accepted */
411 if(usbtty_input.size <= 0){
412 udc_unset_nak(endpoint->endpoint_address&0x03);
416 return (usbtty_input.size > 0);
420 * Read a single byte from the usb client port. Returns 1 on success, 0
421 * otherwise. When the function is succesfull, the character read is
422 * written into its argument c.
425 int usbtty_getc (void)
428 struct usb_endpoint_instance *endpoint =
429 &endpoint_instance[rx_endpoint];
431 while (usbtty_input.size <= 0) {
432 udc_unset_nak(endpoint->endpoint_address&0x03);
436 buf_pop (&usbtty_input, &c, 1);
437 udc_set_nak(endpoint->endpoint_address&0x03);
443 * Output a single byte to the usb client port.
445 void usbtty_putc (const char c)
447 if (!usbtty_configured ())
450 buf_push (&usbtty_output, &c, 1);
451 /* If \n, also do \r */
453 buf_push (&usbtty_output, "\r", 1);
455 /* Poll at end to handle new data... */
456 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
461 /* usbtty_puts() helper function for finding the next '\n' in a string */
462 static int next_nl_pos (const char *s)
466 for (i = 0; s[i] != '\0'; i++) {
474 * Output a string to the usb client port - implementing flow control
477 static void __usbtty_puts (const char *str, int len)
479 int maxlen = usbtty_output.totalsize;
482 /* break str into chunks < buffer size, if needed */
486 space = maxlen - usbtty_output.size;
487 /* Empty buffer here, if needed, to ensure space... */
489 write_buffer (&usbtty_output);
491 n = MIN (space, MIN (len, maxlen));
492 buf_push (&usbtty_output, str, n);
500 void usbtty_puts (const char *str)
505 if (!usbtty_configured ())
509 /* add '\r' for each '\n' */
511 n = next_nl_pos (str);
513 if (str[n] == '\n') {
514 __usbtty_puts (str, n + 1);
515 __usbtty_puts ("\r", 1);
519 /* No \n found. All done. */
520 __usbtty_puts (str, n);
525 /* Poll at end to handle new data... */
530 * Initialize the usb client port.
533 int drv_usbtty_init (void)
540 /* Ger seiral number */
541 if (!(sn = getenv("serial#"))) {
545 if (snlen > sizeof(serial_number) - 1) {
546 printf ("Warning: serial number %s is too long (%d > %lu)\n",
547 sn, snlen, (ulong)(sizeof(serial_number) - 1));
548 snlen = sizeof(serial_number) - 1;
550 memcpy (serial_number, sn, snlen);
551 serial_number[snlen] = '\0';
553 /* Decide on which type of UDC device to be.
556 if(!(tt = getenv("usbtty"))) {
559 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
561 /* prepare buffers... */
562 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
563 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
565 /* Now, set up USB controller and infrastructure */
566 udc_init (); /* Basic USB initialization */
568 usbtty_init_strings ();
569 usbtty_init_instances ();
571 usbtty_init_endpoints ();
573 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
574 udc_connect (); /* Enable pullup for host detection */
576 /* Device initialization */
577 memset (&usbttydev, 0, sizeof (usbttydev));
579 strcpy (usbttydev.name, "usbtty");
580 usbttydev.ext = 0; /* No extensions */
581 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
582 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
583 usbttydev.getc = usbtty_getc; /* 'getc' function */
584 usbttydev.putc = usbtty_putc; /* 'putc' function */
585 usbttydev.puts = usbtty_puts; /* 'puts' function */
587 rc = stdio_register (&usbttydev);
589 return (rc == 0) ? 1 : rc;
592 static void usbtty_init_strings (void)
594 struct usb_string_descriptor *string;
596 usbtty_string_table[STR_LANG] =
597 (struct usb_string_descriptor*)wstrLang;
599 string = (struct usb_string_descriptor *) wstrManufacturer;
600 string->bLength = sizeof(wstrManufacturer);
601 string->bDescriptorType = USB_DT_STRING;
602 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
603 usbtty_string_table[STR_MANUFACTURER]=string;
606 string = (struct usb_string_descriptor *) wstrProduct;
607 string->bLength = sizeof(wstrProduct);
608 string->bDescriptorType = USB_DT_STRING;
609 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
610 usbtty_string_table[STR_PRODUCT]=string;
613 string = (struct usb_string_descriptor *) wstrSerial;
614 string->bLength = sizeof(serial_number);
615 string->bDescriptorType = USB_DT_STRING;
616 str2wide (serial_number, string->wData);
617 usbtty_string_table[STR_SERIAL]=string;
620 string = (struct usb_string_descriptor *) wstrConfiguration;
621 string->bLength = sizeof(wstrConfiguration);
622 string->bDescriptorType = USB_DT_STRING;
623 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
624 usbtty_string_table[STR_CONFIG]=string;
627 string = (struct usb_string_descriptor *) wstrDataInterface;
628 string->bLength = sizeof(wstrDataInterface);
629 string->bDescriptorType = USB_DT_STRING;
630 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
631 usbtty_string_table[STR_DATA_INTERFACE]=string;
633 string = (struct usb_string_descriptor *) wstrCtrlInterface;
634 string->bLength = sizeof(wstrCtrlInterface);
635 string->bDescriptorType = USB_DT_STRING;
636 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
637 usbtty_string_table[STR_CTRL_INTERFACE]=string;
639 /* Now, initialize the string table for ep0 handling */
640 usb_strings = usbtty_string_table;
643 #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
644 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
646 static void usbtty_init_instances (void)
650 /* initialize device instance */
651 memset (device_instance, 0, sizeof (struct usb_device_instance));
652 device_instance->device_state = STATE_INIT;
653 device_instance->device_descriptor = &device_descriptor;
654 #if defined(CONFIG_USBD_HS)
655 device_instance->qualifier_descriptor = &qualifier_descriptor;
657 device_instance->event = usbtty_event_handler;
658 device_instance->cdc_recv_setup = usbtty_cdc_setup;
659 device_instance->bus = bus_instance;
660 device_instance->configurations = NUM_CONFIGS;
661 device_instance->configuration_instance_array = config_instance;
663 /* initialize bus instance */
664 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
665 bus_instance->device = device_instance;
666 bus_instance->endpoint_array = endpoint_instance;
667 bus_instance->max_endpoints = 1;
668 bus_instance->maxpacketsize = 64;
669 bus_instance->serial_number_str = serial_number;
671 /* configuration instance */
672 memset (config_instance, 0,
673 sizeof (struct usb_configuration_instance));
674 config_instance->interfaces = interface_count;
675 config_instance->configuration_descriptor = configuration_descriptor;
676 config_instance->interface_instance_array = interface_instance;
678 /* interface instance */
679 memset (interface_instance, 0,
680 sizeof (struct usb_interface_instance));
681 interface_instance->alternates = 1;
682 interface_instance->alternates_instance_array = alternate_instance;
684 /* alternates instance */
685 memset (alternate_instance, 0,
686 sizeof (struct usb_alternate_instance));
687 alternate_instance->interface_descriptor = interface_descriptors;
688 alternate_instance->endpoints = NUM_ENDPOINTS;
689 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
691 /* endpoint instances */
692 memset (&endpoint_instance[0], 0,
693 sizeof (struct usb_endpoint_instance));
694 endpoint_instance[0].endpoint_address = 0;
695 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
696 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
697 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
698 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
699 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
701 for (i = 1; i <= NUM_ENDPOINTS; i++) {
702 memset (&endpoint_instance[i], 0,
703 sizeof (struct usb_endpoint_instance));
705 endpoint_instance[i].endpoint_address =
706 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
708 endpoint_instance[i].rcv_attributes =
709 ep_descriptor_ptrs[i - 1]->bmAttributes;
711 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
713 endpoint_instance[i].tx_attributes =
714 ep_descriptor_ptrs[i - 1]->bmAttributes;
716 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
718 endpoint_instance[i].tx_attributes =
719 ep_descriptor_ptrs[i - 1]->bmAttributes;
721 urb_link_init (&endpoint_instance[i].rcv);
722 urb_link_init (&endpoint_instance[i].rdy);
723 urb_link_init (&endpoint_instance[i].tx);
724 urb_link_init (&endpoint_instance[i].done);
726 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
727 endpoint_instance[i].tx_urb =
728 usbd_alloc_urb (device_instance,
729 &endpoint_instance[i]);
731 endpoint_instance[i].rcv_urb =
732 usbd_alloc_urb (device_instance,
733 &endpoint_instance[i]);
737 static void usbtty_init_endpoints (void)
741 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
742 for (i = 1; i <= NUM_ENDPOINTS; i++) {
743 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
747 /* usbtty_init_terminal_type
749 * Do some late binding for our device type.
751 static void usbtty_init_terminal_type(short type)
756 /* Assign endpoint descriptors */
757 ep_descriptor_ptrs[0] =
758 &acm_configuration_descriptors[0].notification_endpoint;
759 ep_descriptor_ptrs[1] =
760 &acm_configuration_descriptors[0].data_endpoints[0];
761 ep_descriptor_ptrs[2] =
762 &acm_configuration_descriptors[0].data_endpoints[1];
764 /* Enumerate Device Descriptor */
765 device_descriptor.bDeviceClass =
766 COMMUNICATIONS_DEVICE_CLASS;
767 device_descriptor.idProduct =
768 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
770 #if defined(CONFIG_USBD_HS)
771 qualifier_descriptor.bDeviceClass =
772 COMMUNICATIONS_DEVICE_CLASS;
774 /* Assign endpoint indices */
775 tx_endpoint = ACM_TX_ENDPOINT;
776 rx_endpoint = ACM_RX_ENDPOINT;
778 /* Configuration Descriptor */
779 configuration_descriptor =
780 (struct usb_configuration_descriptor*)
781 &acm_configuration_descriptors;
783 /* Interface count */
784 interface_count = NUM_ACM_INTERFACES;
787 /* BULK IN/OUT & Default */
790 /* Assign endpoint descriptors */
791 ep_descriptor_ptrs[0] =
792 &gserial_configuration_descriptors[0].data_endpoints[0];
793 ep_descriptor_ptrs[1] =
794 &gserial_configuration_descriptors[0].data_endpoints[1];
795 ep_descriptor_ptrs[2] =
796 &gserial_configuration_descriptors[0].data_endpoints[2];
798 /* Enumerate Device Descriptor */
799 device_descriptor.bDeviceClass = 0xFF;
800 device_descriptor.idProduct =
801 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
802 #if defined(CONFIG_USBD_HS)
803 qualifier_descriptor.bDeviceClass = 0xFF;
805 /* Assign endpoint indices */
806 tx_endpoint = GSERIAL_TX_ENDPOINT;
807 rx_endpoint = GSERIAL_RX_ENDPOINT;
809 /* Configuration Descriptor */
810 configuration_descriptor =
811 (struct usb_configuration_descriptor*)
812 &gserial_configuration_descriptors;
814 /* Interface count */
815 interface_count = NUM_GSERIAL_INTERFACES;
820 /******************************************************************************/
822 static struct urb *next_urb (struct usb_device_instance *device,
823 struct usb_endpoint_instance *endpoint)
825 struct urb *current_urb = NULL;
828 /* If there's a queue, then we should add to the last urb */
829 if (!endpoint->tx_queue) {
830 current_urb = endpoint->tx_urb;
832 /* Last urb from tx chain */
834 p2surround (struct urb, link, endpoint->tx.prev);
837 /* Make sure this one has enough room */
838 space = current_urb->buffer_length - current_urb->actual_length;
841 } else { /* No space here */
842 /* First look at done list */
843 current_urb = first_urb_detached (&endpoint->done);
845 current_urb = usbd_alloc_urb (device, endpoint);
848 urb_append (&endpoint->tx, current_urb);
849 endpoint->tx_queue++;
854 static int write_buffer (circbuf_t * buf)
856 if (!usbtty_configured ()) {
860 struct usb_endpoint_instance *endpoint =
861 &endpoint_instance[tx_endpoint];
862 struct urb *current_urb = NULL;
864 current_urb = next_urb (device_instance, endpoint);
865 /* TX data still exists - send it now
867 if(endpoint->sent < current_urb->actual_length){
868 if(udc_endpoint_write (endpoint)){
869 /* Write pre-empted by RX */
881 /* Break buffer into urb sized pieces,
882 * and link each to the endpoint
884 while (buf->size > 0) {
887 TTYERR ("current_urb is NULL, buf->size %d\n",
892 dest = (char*)current_urb->buffer +
893 current_urb->actual_length;
896 current_urb->buffer_length -
897 current_urb->actual_length;
898 popnum = MIN (space_avail, buf->size);
902 popped = buf_pop (buf, dest, popnum);
905 current_urb->actual_length += popped;
908 /* If endpoint->last == 0, then transfers have
909 * not started on this endpoint
911 if (endpoint->last == 0) {
912 if(udc_endpoint_write (endpoint)){
913 /* Write pre-empted by RX */
925 static int fill_buffer (circbuf_t * buf)
927 struct usb_endpoint_instance *endpoint =
928 &endpoint_instance[rx_endpoint];
930 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
932 char *src = (char *) endpoint->rcv_urb->buffer;
933 unsigned int rx_avail = buf->totalsize - buf->size;
935 if(rx_avail >= endpoint->rcv_urb->actual_length){
937 nb = endpoint->rcv_urb->actual_length;
938 buf_push (buf, src, nb);
939 endpoint->rcv_urb->actual_length = 0;
947 static int usbtty_configured (void)
949 return usbtty_configured_flag;
952 /******************************************************************************/
954 static void usbtty_event_handler (struct usb_device_instance *device,
955 usb_device_event_t event, int data)
957 #if defined(CONFIG_USBD_HS)
962 case DEVICE_BUS_INACTIVE:
963 usbtty_configured_flag = 0;
965 case DEVICE_CONFIGURED:
966 usbtty_configured_flag = 1;
969 case DEVICE_ADDRESS_ASSIGNED:
970 #if defined(CONFIG_USBD_HS)
972 * is_usbd_high_speed routine needs to be defined by
973 * specific gadget driver
974 * It returns true if device enumerates at High speed
975 * Retuns false otherwise
977 for (i = 0; i < NUM_ENDPOINTS; i++) {
978 if (((ep_descriptor_ptrs[i]->bmAttributes &
979 USB_ENDPOINT_XFERTYPE_MASK) ==
980 USB_ENDPOINT_XFER_BULK)
981 && is_usbd_high_speed()) {
983 ep_descriptor_ptrs[i]->wMaxPacketSize =
984 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
987 endpoint_instance[i + 1].tx_packetSize =
988 ep_descriptor_ptrs[i]->wMaxPacketSize;
989 endpoint_instance[i + 1].rcv_packetSize =
990 ep_descriptor_ptrs[i]->wMaxPacketSize;
993 usbtty_init_endpoints ();
1000 /******************************************************************************/
1002 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
1004 switch (request->bRequest){
1006 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
1008 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
1010 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
1013 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
1015 case ACM_GET_LINE_ENCODING : /* request DTE rate,
1016 * stop/parity bits */
1017 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1018 urb->actual_length = sizeof(rs232_desc);
1027 /******************************************************************************/
1030 * Since interrupt handling has not yet been implemented, we use this function
1031 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1032 * update the USB state.
1034 void usbtty_poll (void)
1036 /* New interrupts? */
1039 /* Write any output data to host buffer
1040 * (do this before checking interrupts to avoid missing one)
1042 if (usbtty_configured ()) {
1043 write_buffer (&usbtty_output);
1046 /* New interrupts? */
1049 /* Check for new data from host..
1050 * (do this after checking interrupts to get latest data)
1052 if (usbtty_configured ()) {
1053 fill_buffer (&usbtty_input);
1056 /* New interrupts? */