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>
29 #include "usb_cdc_acm.h"
30 #include "usbdescriptors.h"
33 #define TTYDBG(fmt,args...)\
34 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
36 #define TTYDBG(fmt,args...) do{}while(0)
40 #define TTYERR(fmt,args...)\
41 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
44 #define TTYERR(fmt,args...) do{}while(0)
51 #define MAX_INTERFACES 2
52 #define NUM_ENDPOINTS 3
53 #define ACM_TX_ENDPOINT 3
54 #define ACM_RX_ENDPOINT 2
55 #define GSERIAL_TX_ENDPOINT 2
56 #define GSERIAL_RX_ENDPOINT 1
57 #define NUM_ACM_INTERFACES 2
58 #define NUM_GSERIAL_INTERFACES 1
59 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
60 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
63 * Buffers to hold input and output data
65 #define USBTTY_BUFFER_SIZE 256
66 static circbuf_t usbtty_input;
67 static circbuf_t usbtty_output;
73 static struct stdio_dev usbttydev;
74 static struct usb_device_instance device_instance[1];
75 static struct usb_bus_instance bus_instance[1];
76 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
77 static struct usb_interface_instance interface_instance[MAX_INTERFACES];
78 static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
79 /* one extra for control endpoint */
80 static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
85 int usbtty_configured_flag = 0;
90 static char serial_number[16];
94 * Descriptors, Strings, Local variables.
97 /* defined and used by gadget/ep0.c */
98 extern struct usb_string_descriptor **usb_strings;
100 /* Indicies, References */
101 static unsigned short rx_endpoint = 0;
102 static unsigned short tx_endpoint = 0;
103 static unsigned short interface_count = 0;
104 static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
106 /* USB Descriptor Strings */
107 static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
108 static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
109 static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
110 static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
111 static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
112 static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
113 static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
115 /* Standard USB Data Structures */
116 static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
117 static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
118 static struct usb_configuration_descriptor *configuration_descriptor = 0;
119 static struct usb_device_descriptor device_descriptor = {
120 .bLength = sizeof(struct usb_device_descriptor),
121 .bDescriptorType = USB_DT_DEVICE,
122 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
123 .bDeviceSubClass = 0x00,
124 .bDeviceProtocol = 0x00,
125 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
126 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
127 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
128 .iManufacturer = STR_MANUFACTURER,
129 .iProduct = STR_PRODUCT,
130 .iSerialNumber = STR_SERIAL,
131 .bNumConfigurations = NUM_CONFIGS
136 * Static CDC ACM specific descriptors
139 struct acm_config_desc {
140 struct usb_configuration_descriptor configuration_desc;
142 /* Master Interface */
143 struct usb_interface_descriptor interface_desc;
145 struct usb_class_header_function_descriptor usb_class_header;
146 struct usb_class_call_management_descriptor usb_class_call_mgt;
147 struct usb_class_abstract_control_descriptor usb_class_acm;
148 struct usb_class_union_function_descriptor usb_class_union;
149 struct usb_endpoint_descriptor notification_endpoint;
151 /* Slave Interface */
152 struct usb_interface_descriptor data_class_interface;
153 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
154 } __attribute__((packed));
156 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
158 .configuration_desc ={
160 sizeof(struct usb_configuration_descriptor),
161 .bDescriptorType = USB_DT_CONFIG,
163 cpu_to_le16(sizeof(struct acm_config_desc)),
164 .bNumInterfaces = NUM_ACM_INTERFACES,
165 .bConfigurationValue = 1,
166 .iConfiguration = STR_CONFIG,
168 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
169 .bMaxPower = USBTTY_MAXPOWER
173 .bLength = sizeof(struct usb_interface_descriptor),
174 .bDescriptorType = USB_DT_INTERFACE,
175 .bInterfaceNumber = 0,
176 .bAlternateSetting = 0,
177 .bNumEndpoints = 0x01,
179 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
180 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
181 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
182 .iInterface = STR_CTRL_INTERFACE,
184 .usb_class_header = {
186 sizeof(struct usb_class_header_function_descriptor),
187 .bDescriptorType = CS_INTERFACE,
188 .bDescriptorSubtype = USB_ST_HEADER,
189 .bcdCDC = cpu_to_le16(110),
191 .usb_class_call_mgt = {
193 sizeof(struct usb_class_call_management_descriptor),
194 .bDescriptorType = CS_INTERFACE,
195 .bDescriptorSubtype = USB_ST_CMF,
196 .bmCapabilities = 0x00,
197 .bDataInterface = 0x01,
201 sizeof(struct usb_class_abstract_control_descriptor),
202 .bDescriptorType = CS_INTERFACE,
203 .bDescriptorSubtype = USB_ST_ACMF,
204 .bmCapabilities = 0x00,
208 sizeof(struct usb_class_union_function_descriptor),
209 .bDescriptorType = CS_INTERFACE,
210 .bDescriptorSubtype = USB_ST_UF,
211 .bMasterInterface = 0x00,
212 .bSlaveInterface0 = 0x01,
214 .notification_endpoint = {
216 sizeof(struct usb_endpoint_descriptor),
217 .bDescriptorType = USB_DT_ENDPOINT,
218 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
219 .bmAttributes = USB_ENDPOINT_XFER_INT,
221 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
226 .data_class_interface = {
228 sizeof(struct usb_interface_descriptor),
229 .bDescriptorType = USB_DT_INTERFACE,
230 .bInterfaceNumber = 0x01,
231 .bAlternateSetting = 0x00,
232 .bNumEndpoints = 0x02,
234 COMMUNICATIONS_INTERFACE_CLASS_DATA,
235 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
236 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
237 .iInterface = STR_DATA_INTERFACE,
242 sizeof(struct usb_endpoint_descriptor),
243 .bDescriptorType = USB_DT_ENDPOINT,
244 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
246 USB_ENDPOINT_XFER_BULK,
248 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
253 sizeof(struct usb_endpoint_descriptor),
254 .bDescriptorType = USB_DT_ENDPOINT,
255 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
257 USB_ENDPOINT_XFER_BULK,
259 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
266 static struct rs232_emu rs232_desc={
275 * Static Generic Serial specific data
279 struct gserial_config_desc {
281 struct usb_configuration_descriptor configuration_desc;
282 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
283 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
285 } __attribute__((packed));
287 static struct gserial_config_desc
288 gserial_configuration_descriptors[NUM_CONFIGS] ={
290 .configuration_desc ={
291 .bLength = sizeof(struct usb_configuration_descriptor),
292 .bDescriptorType = USB_DT_CONFIG,
294 cpu_to_le16(sizeof(struct gserial_config_desc)),
295 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
296 .bConfigurationValue = 1,
297 .iConfiguration = STR_CONFIG,
299 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
300 .bMaxPower = USBTTY_MAXPOWER
305 sizeof(struct usb_interface_descriptor),
306 .bDescriptorType = USB_DT_INTERFACE,
307 .bInterfaceNumber = 0,
308 .bAlternateSetting = 0,
309 .bNumEndpoints = NUM_ENDPOINTS,
311 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
312 .bInterfaceSubClass =
313 COMMUNICATIONS_NO_SUBCLASS,
314 .bInterfaceProtocol =
315 COMMUNICATIONS_NO_PROTOCOL,
316 .iInterface = STR_DATA_INTERFACE
322 sizeof(struct usb_endpoint_descriptor),
323 .bDescriptorType = USB_DT_ENDPOINT,
324 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
325 .bmAttributes = USB_ENDPOINT_XFER_BULK,
327 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
332 sizeof(struct usb_endpoint_descriptor),
333 .bDescriptorType = USB_DT_ENDPOINT,
334 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
335 .bmAttributes = USB_ENDPOINT_XFER_BULK,
337 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
342 sizeof(struct usb_endpoint_descriptor),
343 .bDescriptorType = USB_DT_ENDPOINT,
344 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
345 .bmAttributes = USB_ENDPOINT_XFER_INT,
347 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
355 * Static Function Prototypes
358 static void usbtty_init_strings (void);
359 static void usbtty_init_instances (void);
360 static void usbtty_init_endpoints (void);
361 static void usbtty_init_terminal_type(short type);
362 static void usbtty_event_handler (struct usb_device_instance *device,
363 usb_device_event_t event, int data);
364 static int usbtty_cdc_setup(struct usb_device_request *request,
366 static int usbtty_configured (void);
367 static int write_buffer (circbuf_t * buf);
368 static int fill_buffer (circbuf_t * buf);
370 void usbtty_poll (void);
372 /* utility function for converting char* to wide string used by USB */
373 static void str2wide (char *str, u16 * wide)
376 for (i = 0; i < strlen (str) && str[i]; i++){
377 #if defined(__LITTLE_ENDIAN)
378 wide[i] = (u16) str[i];
379 #elif defined(__BIG_ENDIAN)
380 wide[i] = ((u16)(str[i])<<8);
382 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
388 * Test whether a character is in the RX buffer
391 int usbtty_tstc (void)
393 struct usb_endpoint_instance *endpoint =
394 &endpoint_instance[rx_endpoint];
396 /* If no input data exists, allow more RX to be accepted */
397 if(usbtty_input.size <= 0){
398 udc_unset_nak(endpoint->endpoint_address&0x03);
402 return (usbtty_input.size > 0);
406 * Read a single byte from the usb client port. Returns 1 on success, 0
407 * otherwise. When the function is succesfull, the character read is
408 * written into its argument c.
411 int usbtty_getc (void)
414 struct usb_endpoint_instance *endpoint =
415 &endpoint_instance[rx_endpoint];
417 while (usbtty_input.size <= 0) {
418 udc_unset_nak(endpoint->endpoint_address&0x03);
422 buf_pop (&usbtty_input, &c, 1);
423 udc_set_nak(endpoint->endpoint_address&0x03);
429 * Output a single byte to the usb client port.
431 void usbtty_putc (const char c)
433 if (!usbtty_configured ())
436 buf_push (&usbtty_output, &c, 1);
437 /* If \n, also do \r */
439 buf_push (&usbtty_output, "\r", 1);
441 /* Poll at end to handle new data... */
442 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
447 /* usbtty_puts() helper function for finding the next '\n' in a string */
448 static int next_nl_pos (const char *s)
452 for (i = 0; s[i] != '\0'; i++) {
460 * Output a string to the usb client port - implementing flow control
463 static void __usbtty_puts (const char *str, int len)
465 int maxlen = usbtty_output.totalsize;
468 /* break str into chunks < buffer size, if needed */
472 space = maxlen - usbtty_output.size;
473 /* Empty buffer here, if needed, to ensure space... */
475 write_buffer (&usbtty_output);
477 n = MIN (space, MIN (len, maxlen));
478 buf_push (&usbtty_output, str, n);
486 void usbtty_puts (const char *str)
491 if (!usbtty_configured ())
495 /* add '\r' for each '\n' */
497 n = next_nl_pos (str);
499 if (str[n] == '\n') {
500 __usbtty_puts (str, n + 1);
501 __usbtty_puts ("\r", 1);
505 /* No \n found. All done. */
506 __usbtty_puts (str, n);
511 /* Poll at end to handle new data... */
516 * Initialize the usb client port.
519 int drv_usbtty_init (void)
526 /* Ger seiral number */
527 if (!(sn = getenv("serial#"))) {
531 if (snlen > sizeof(serial_number) - 1) {
532 printf ("Warning: serial number %s is too long (%d > %lu)\n",
533 sn, snlen, (ulong)(sizeof(serial_number) - 1));
534 snlen = sizeof(serial_number) - 1;
536 memcpy (serial_number, sn, snlen);
537 serial_number[snlen] = '\0';
539 /* Decide on which type of UDC device to be.
542 if(!(tt = getenv("usbtty"))) {
545 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
547 /* prepare buffers... */
548 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
549 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
551 /* Now, set up USB controller and infrastructure */
552 udc_init (); /* Basic USB initialization */
554 usbtty_init_strings ();
555 usbtty_init_instances ();
557 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
558 udc_connect (); /* Enable pullup for host detection */
560 usbtty_init_endpoints ();
562 /* Device initialization */
563 memset (&usbttydev, 0, sizeof (usbttydev));
565 strcpy (usbttydev.name, "usbtty");
566 usbttydev.ext = 0; /* No extensions */
567 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
568 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
569 usbttydev.getc = usbtty_getc; /* 'getc' function */
570 usbttydev.putc = usbtty_putc; /* 'putc' function */
571 usbttydev.puts = usbtty_puts; /* 'puts' function */
573 rc = stdio_register (&usbttydev);
575 return (rc == 0) ? 1 : rc;
578 static void usbtty_init_strings (void)
580 struct usb_string_descriptor *string;
582 usbtty_string_table[STR_LANG] =
583 (struct usb_string_descriptor*)wstrLang;
585 string = (struct usb_string_descriptor *) wstrManufacturer;
586 string->bLength = sizeof(wstrManufacturer);
587 string->bDescriptorType = USB_DT_STRING;
588 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
589 usbtty_string_table[STR_MANUFACTURER]=string;
592 string = (struct usb_string_descriptor *) wstrProduct;
593 string->bLength = sizeof(wstrProduct);
594 string->bDescriptorType = USB_DT_STRING;
595 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
596 usbtty_string_table[STR_PRODUCT]=string;
599 string = (struct usb_string_descriptor *) wstrSerial;
600 string->bLength = sizeof(serial_number);
601 string->bDescriptorType = USB_DT_STRING;
602 str2wide (serial_number, string->wData);
603 usbtty_string_table[STR_SERIAL]=string;
606 string = (struct usb_string_descriptor *) wstrConfiguration;
607 string->bLength = sizeof(wstrConfiguration);
608 string->bDescriptorType = USB_DT_STRING;
609 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
610 usbtty_string_table[STR_CONFIG]=string;
613 string = (struct usb_string_descriptor *) wstrDataInterface;
614 string->bLength = sizeof(wstrDataInterface);
615 string->bDescriptorType = USB_DT_STRING;
616 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
617 usbtty_string_table[STR_DATA_INTERFACE]=string;
619 string = (struct usb_string_descriptor *) wstrCtrlInterface;
620 string->bLength = sizeof(wstrCtrlInterface);
621 string->bDescriptorType = USB_DT_STRING;
622 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
623 usbtty_string_table[STR_CTRL_INTERFACE]=string;
625 /* Now, initialize the string table for ep0 handling */
626 usb_strings = usbtty_string_table;
629 static void usbtty_init_instances (void)
633 /* initialize device instance */
634 memset (device_instance, 0, sizeof (struct usb_device_instance));
635 device_instance->device_state = STATE_INIT;
636 device_instance->device_descriptor = &device_descriptor;
637 device_instance->event = usbtty_event_handler;
638 device_instance->cdc_recv_setup = usbtty_cdc_setup;
639 device_instance->bus = bus_instance;
640 device_instance->configurations = NUM_CONFIGS;
641 device_instance->configuration_instance_array = config_instance;
643 /* initialize bus instance */
644 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
645 bus_instance->device = device_instance;
646 bus_instance->endpoint_array = endpoint_instance;
647 bus_instance->max_endpoints = 1;
648 bus_instance->maxpacketsize = 64;
649 bus_instance->serial_number_str = serial_number;
651 /* configuration instance */
652 memset (config_instance, 0,
653 sizeof (struct usb_configuration_instance));
654 config_instance->interfaces = interface_count;
655 config_instance->configuration_descriptor = configuration_descriptor;
656 config_instance->interface_instance_array = interface_instance;
658 /* interface instance */
659 memset (interface_instance, 0,
660 sizeof (struct usb_interface_instance));
661 interface_instance->alternates = 1;
662 interface_instance->alternates_instance_array = alternate_instance;
664 /* alternates instance */
665 memset (alternate_instance, 0,
666 sizeof (struct usb_alternate_instance));
667 alternate_instance->interface_descriptor = interface_descriptors;
668 alternate_instance->endpoints = NUM_ENDPOINTS;
669 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
671 /* endpoint instances */
672 memset (&endpoint_instance[0], 0,
673 sizeof (struct usb_endpoint_instance));
674 endpoint_instance[0].endpoint_address = 0;
675 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
676 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
677 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
678 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
679 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
681 for (i = 1; i <= NUM_ENDPOINTS; i++) {
682 memset (&endpoint_instance[i], 0,
683 sizeof (struct usb_endpoint_instance));
685 endpoint_instance[i].endpoint_address =
686 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
688 endpoint_instance[i].rcv_attributes =
689 ep_descriptor_ptrs[i - 1]->bmAttributes;
691 endpoint_instance[i].rcv_packetSize =
692 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
694 endpoint_instance[i].tx_attributes =
695 ep_descriptor_ptrs[i - 1]->bmAttributes;
697 endpoint_instance[i].tx_packetSize =
698 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
700 endpoint_instance[i].tx_attributes =
701 ep_descriptor_ptrs[i - 1]->bmAttributes;
703 urb_link_init (&endpoint_instance[i].rcv);
704 urb_link_init (&endpoint_instance[i].rdy);
705 urb_link_init (&endpoint_instance[i].tx);
706 urb_link_init (&endpoint_instance[i].done);
708 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
709 endpoint_instance[i].tx_urb =
710 usbd_alloc_urb (device_instance,
711 &endpoint_instance[i]);
713 endpoint_instance[i].rcv_urb =
714 usbd_alloc_urb (device_instance,
715 &endpoint_instance[i]);
719 static void usbtty_init_endpoints (void)
723 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
724 for (i = 1; i <= NUM_ENDPOINTS; i++) {
725 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
729 /* usbtty_init_terminal_type
731 * Do some late binding for our device type.
733 static void usbtty_init_terminal_type(short type)
738 /* Assign endpoint descriptors */
739 ep_descriptor_ptrs[0] =
740 &acm_configuration_descriptors[0].notification_endpoint;
741 ep_descriptor_ptrs[1] =
742 &acm_configuration_descriptors[0].data_endpoints[0];
743 ep_descriptor_ptrs[2] =
744 &acm_configuration_descriptors[0].data_endpoints[1];
746 /* Enumerate Device Descriptor */
747 device_descriptor.bDeviceClass =
748 COMMUNICATIONS_DEVICE_CLASS;
749 device_descriptor.idProduct =
750 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
752 /* Assign endpoint indices */
753 tx_endpoint = ACM_TX_ENDPOINT;
754 rx_endpoint = ACM_RX_ENDPOINT;
756 /* Configuration Descriptor */
757 configuration_descriptor =
758 (struct usb_configuration_descriptor*)
759 &acm_configuration_descriptors;
761 /* Interface count */
762 interface_count = NUM_ACM_INTERFACES;
765 /* BULK IN/OUT & Default */
768 /* Assign endpoint descriptors */
769 ep_descriptor_ptrs[0] =
770 &gserial_configuration_descriptors[0].data_endpoints[0];
771 ep_descriptor_ptrs[1] =
772 &gserial_configuration_descriptors[0].data_endpoints[1];
773 ep_descriptor_ptrs[2] =
774 &gserial_configuration_descriptors[0].data_endpoints[2];
776 /* Enumerate Device Descriptor */
777 device_descriptor.bDeviceClass = 0xFF;
778 device_descriptor.idProduct =
779 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
781 /* Assign endpoint indices */
782 tx_endpoint = GSERIAL_TX_ENDPOINT;
783 rx_endpoint = GSERIAL_RX_ENDPOINT;
785 /* Configuration Descriptor */
786 configuration_descriptor =
787 (struct usb_configuration_descriptor*)
788 &gserial_configuration_descriptors;
790 /* Interface count */
791 interface_count = NUM_GSERIAL_INTERFACES;
796 /******************************************************************************/
798 static struct urb *next_urb (struct usb_device_instance *device,
799 struct usb_endpoint_instance *endpoint)
801 struct urb *current_urb = NULL;
804 /* If there's a queue, then we should add to the last urb */
805 if (!endpoint->tx_queue) {
806 current_urb = endpoint->tx_urb;
808 /* Last urb from tx chain */
810 p2surround (struct urb, link, endpoint->tx.prev);
813 /* Make sure this one has enough room */
814 space = current_urb->buffer_length - current_urb->actual_length;
817 } else { /* No space here */
818 /* First look at done list */
819 current_urb = first_urb_detached (&endpoint->done);
821 current_urb = usbd_alloc_urb (device, endpoint);
824 urb_append (&endpoint->tx, current_urb);
825 endpoint->tx_queue++;
830 static int write_buffer (circbuf_t * buf)
832 if (!usbtty_configured ()) {
836 struct usb_endpoint_instance *endpoint =
837 &endpoint_instance[tx_endpoint];
838 struct urb *current_urb = NULL;
840 current_urb = next_urb (device_instance, endpoint);
841 /* TX data still exists - send it now
843 if(endpoint->sent < current_urb->actual_length){
844 if(udc_endpoint_write (endpoint)){
845 /* Write pre-empted by RX */
857 /* Break buffer into urb sized pieces,
858 * and link each to the endpoint
860 while (buf->size > 0) {
863 TTYERR ("current_urb is NULL, buf->size %d\n",
868 dest = (char*)current_urb->buffer +
869 current_urb->actual_length;
872 current_urb->buffer_length -
873 current_urb->actual_length;
874 popnum = MIN (space_avail, buf->size);
878 popped = buf_pop (buf, dest, popnum);
881 current_urb->actual_length += popped;
884 /* If endpoint->last == 0, then transfers have
885 * not started on this endpoint
887 if (endpoint->last == 0) {
888 if(udc_endpoint_write (endpoint)){
889 /* Write pre-empted by RX */
901 static int fill_buffer (circbuf_t * buf)
903 struct usb_endpoint_instance *endpoint =
904 &endpoint_instance[rx_endpoint];
906 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
908 char *src = (char *) endpoint->rcv_urb->buffer;
909 unsigned int rx_avail = buf->totalsize - buf->size;
911 if(rx_avail >= endpoint->rcv_urb->actual_length){
913 nb = endpoint->rcv_urb->actual_length;
914 buf_push (buf, src, nb);
915 endpoint->rcv_urb->actual_length = 0;
923 static int usbtty_configured (void)
925 return usbtty_configured_flag;
928 /******************************************************************************/
930 static void usbtty_event_handler (struct usb_device_instance *device,
931 usb_device_event_t event, int data)
935 case DEVICE_BUS_INACTIVE:
936 usbtty_configured_flag = 0;
938 case DEVICE_CONFIGURED:
939 usbtty_configured_flag = 1;
942 case DEVICE_ADDRESS_ASSIGNED:
943 usbtty_init_endpoints ();
950 /******************************************************************************/
952 int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
954 switch (request->bRequest){
956 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
958 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
960 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
963 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
965 case ACM_GET_LINE_ENCODING : /* request DTE rate,
966 * stop/parity bits */
967 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
968 urb->actual_length = sizeof(rs232_desc);
977 /******************************************************************************/
980 * Since interrupt handling has not yet been implemented, we use this function
981 * to handle polling. This is called by the tstc,getc,putc,puts routines to
982 * update the USB state.
984 void usbtty_poll (void)
986 /* New interrupts? */
989 /* Write any output data to host buffer
990 * (do this before checking interrupts to avoid missing one)
992 if (usbtty_configured ()) {
993 write_buffer (&usbtty_output);
996 /* New interrupts? */
999 /* Check for new data from host..
1000 * (do this after checking interrupts to get latest data)
1002 if (usbtty_configured ()) {
1003 fill_buffer (&usbtty_input);
1006 /* New interrupts? */