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
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 device_t 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 usbdcore_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
154 data_endpoints
[NUM_ENDPOINTS
-1] __attribute__((packed
));
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
= 0x01 | 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
= 0x02 | 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
= 0x03 | 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
284 interface_desc
[NUM_GSERIAL_INTERFACES
] __attribute__((packed
));
285 struct usb_endpoint_descriptor
286 data_endpoints
[NUM_ENDPOINTS
] __attribute__((packed
));
288 } __attribute__((packed
));
290 static struct gserial_config_desc
291 gserial_configuration_descriptors
[NUM_CONFIGS
] ={
293 .configuration_desc
={
294 .bLength
= sizeof(struct usb_configuration_descriptor
),
295 .bDescriptorType
= USB_DT_CONFIG
,
297 cpu_to_le16(sizeof(struct gserial_config_desc
)),
298 .bNumInterfaces
= NUM_GSERIAL_INTERFACES
,
299 .bConfigurationValue
= 1,
300 .iConfiguration
= STR_CONFIG
,
302 BMATTRIBUTE_SELF_POWERED
|BMATTRIBUTE_RESERVED
,
303 .bMaxPower
= USBTTY_MAXPOWER
308 sizeof(struct usb_interface_descriptor
),
309 .bDescriptorType
= USB_DT_INTERFACE
,
310 .bInterfaceNumber
= 0,
311 .bAlternateSetting
= 0,
312 .bNumEndpoints
= NUM_ENDPOINTS
,
314 COMMUNICATIONS_INTERFACE_CLASS_VENDOR
,
315 .bInterfaceSubClass
=
316 COMMUNICATIONS_NO_SUBCLASS
,
317 .bInterfaceProtocol
=
318 COMMUNICATIONS_NO_PROTOCOL
,
319 .iInterface
= STR_DATA_INTERFACE
325 sizeof(struct usb_endpoint_descriptor
),
326 .bDescriptorType
= USB_DT_ENDPOINT
,
327 .bEndpointAddress
= 0x01 | USB_DIR_OUT
,
328 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
330 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE
),
335 sizeof(struct usb_endpoint_descriptor
),
336 .bDescriptorType
= USB_DT_ENDPOINT
,
337 .bEndpointAddress
= 0x02 | USB_DIR_IN
,
338 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
340 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE
),
345 sizeof(struct usb_endpoint_descriptor
),
346 .bDescriptorType
= USB_DT_ENDPOINT
,
347 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
348 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
350 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
358 * Static Function Prototypes
361 static void usbtty_init_strings (void);
362 static void usbtty_init_instances (void);
363 static void usbtty_init_endpoints (void);
364 static void usbtty_init_terminal_type(short type
);
365 static void usbtty_event_handler (struct usb_device_instance
*device
,
366 usb_device_event_t event
, int data
);
367 static int usbtty_cdc_setup(struct usb_device_request
*request
,
369 static int usbtty_configured (void);
370 static int write_buffer (circbuf_t
* buf
);
371 static int fill_buffer (circbuf_t
* buf
);
373 void usbtty_poll (void);
375 /* utility function for converting char* to wide string used by USB */
376 static void str2wide (char *str
, u16
* wide
)
379 for (i
= 0; i
< strlen (str
) && str
[i
]; i
++){
380 #if defined(__LITTLE_ENDIAN)
381 wide
[i
] = (u16
) str
[i
];
382 #elif defined(__BIG_ENDIAN)
383 wide
[i
] = ((u16
)(str
[i
])<<8);
385 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
391 * Test whether a character is in the RX buffer
394 int usbtty_tstc (void)
396 struct usb_endpoint_instance
*endpoint
=
397 &endpoint_instance
[rx_endpoint
];
399 /* If no input data exists, allow more RX to be accepted */
400 if(usbtty_input
.size
<= 0){
401 udc_unset_nak(endpoint
->endpoint_address
&0x03);
405 return (usbtty_input
.size
> 0);
409 * Read a single byte from the usb client port. Returns 1 on success, 0
410 * otherwise. When the function is succesfull, the character read is
411 * written into its argument c.
414 int usbtty_getc (void)
417 struct usb_endpoint_instance
*endpoint
=
418 &endpoint_instance
[rx_endpoint
];
420 while (usbtty_input
.size
<= 0) {
421 udc_unset_nak(endpoint
->endpoint_address
&0x03);
425 buf_pop (&usbtty_input
, &c
, 1);
426 udc_set_nak(endpoint
->endpoint_address
&0x03);
432 * Output a single byte to the usb client port.
434 void usbtty_putc (const char c
)
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
)
489 int len
= strlen (str
);
491 /* add '\r' for each '\n' */
493 n
= next_nl_pos (str
);
495 if (str
[n
] == '\n') {
496 __usbtty_puts (str
, n
+ 1);
497 __usbtty_puts ("\r", 1);
501 /* No \n found. All done. */
502 __usbtty_puts (str
, n
);
507 /* Poll at end to handle new data... */
512 * Initialize the usb client port.
515 int drv_usbtty_init (void)
522 /* Ger seiral number */
523 if (!(sn
= getenv("serial#"))) {
527 if (snlen
> sizeof(serial_number
) - 1) {
528 printf ("Warning: serial number %s is too long (%d > %lu)\n",
529 sn
, snlen
, (ulong
)(sizeof(serial_number
) - 1));
530 snlen
= sizeof(serial_number
) - 1;
532 memcpy (serial_number
, sn
, snlen
);
533 serial_number
[snlen
] = '\0';
535 /* Decide on which type of UDC device to be.
538 if(!(tt
= getenv("usbtty"))) {
541 usbtty_init_terminal_type(strcmp(tt
,"cdc_acm"));
543 /* prepare buffers... */
544 buf_init (&usbtty_input
, USBTTY_BUFFER_SIZE
);
545 buf_init (&usbtty_output
, USBTTY_BUFFER_SIZE
);
547 /* Now, set up USB controller and infrastructure */
548 udc_init (); /* Basic USB initialization */
550 usbtty_init_strings ();
551 usbtty_init_instances ();
553 udc_startup_events (device_instance
);/* Enable dev, init udc pointers */
554 udc_connect (); /* Enable pullup for host detection */
556 usbtty_init_endpoints ();
558 /* Device initialization */
559 memset (&usbttydev
, 0, sizeof (usbttydev
));
561 strcpy (usbttydev
.name
, "usbtty");
562 usbttydev
.ext
= 0; /* No extensions */
563 usbttydev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_OUTPUT
;
564 usbttydev
.tstc
= usbtty_tstc
; /* 'tstc' function */
565 usbttydev
.getc
= usbtty_getc
; /* 'getc' function */
566 usbttydev
.putc
= usbtty_putc
; /* 'putc' function */
567 usbttydev
.puts
= usbtty_puts
; /* 'puts' function */
569 rc
= device_register (&usbttydev
);
571 return (rc
== 0) ? 1 : rc
;
574 static void usbtty_init_strings (void)
576 struct usb_string_descriptor
*string
;
578 usbtty_string_table
[STR_LANG
] =
579 (struct usb_string_descriptor
*)wstrLang
;
581 string
= (struct usb_string_descriptor
*) wstrManufacturer
;
582 string
->bLength
= sizeof(wstrManufacturer
);
583 string
->bDescriptorType
= USB_DT_STRING
;
584 str2wide (CONFIG_USBD_MANUFACTURER
, string
->wData
);
585 usbtty_string_table
[STR_MANUFACTURER
]=string
;
588 string
= (struct usb_string_descriptor
*) wstrProduct
;
589 string
->bLength
= sizeof(wstrProduct
);
590 string
->bDescriptorType
= USB_DT_STRING
;
591 str2wide (CONFIG_USBD_PRODUCT_NAME
, string
->wData
);
592 usbtty_string_table
[STR_PRODUCT
]=string
;
595 string
= (struct usb_string_descriptor
*) wstrSerial
;
596 string
->bLength
= sizeof(serial_number
);
597 string
->bDescriptorType
= USB_DT_STRING
;
598 str2wide (serial_number
, string
->wData
);
599 usbtty_string_table
[STR_SERIAL
]=string
;
602 string
= (struct usb_string_descriptor
*) wstrConfiguration
;
603 string
->bLength
= sizeof(wstrConfiguration
);
604 string
->bDescriptorType
= USB_DT_STRING
;
605 str2wide (CONFIG_USBD_CONFIGURATION_STR
, string
->wData
);
606 usbtty_string_table
[STR_CONFIG
]=string
;
609 string
= (struct usb_string_descriptor
*) wstrDataInterface
;
610 string
->bLength
= sizeof(wstrDataInterface
);
611 string
->bDescriptorType
= USB_DT_STRING
;
612 str2wide (CONFIG_USBD_DATA_INTERFACE_STR
, string
->wData
);
613 usbtty_string_table
[STR_DATA_INTERFACE
]=string
;
615 string
= (struct usb_string_descriptor
*) wstrCtrlInterface
;
616 string
->bLength
= sizeof(wstrCtrlInterface
);
617 string
->bDescriptorType
= USB_DT_STRING
;
618 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR
, string
->wData
);
619 usbtty_string_table
[STR_CTRL_INTERFACE
]=string
;
621 /* Now, initialize the string table for ep0 handling */
622 usb_strings
= usbtty_string_table
;
625 static void usbtty_init_instances (void)
629 /* initialize device instance */
630 memset (device_instance
, 0, sizeof (struct usb_device_instance
));
631 device_instance
->device_state
= STATE_INIT
;
632 device_instance
->device_descriptor
= &device_descriptor
;
633 device_instance
->event
= usbtty_event_handler
;
634 device_instance
->cdc_recv_setup
= usbtty_cdc_setup
;
635 device_instance
->bus
= bus_instance
;
636 device_instance
->configurations
= NUM_CONFIGS
;
637 device_instance
->configuration_instance_array
= config_instance
;
639 /* initialize bus instance */
640 memset (bus_instance
, 0, sizeof (struct usb_bus_instance
));
641 bus_instance
->device
= device_instance
;
642 bus_instance
->endpoint_array
= endpoint_instance
;
643 bus_instance
->max_endpoints
= 1;
644 bus_instance
->maxpacketsize
= 64;
645 bus_instance
->serial_number_str
= serial_number
;
647 /* configuration instance */
648 memset (config_instance
, 0,
649 sizeof (struct usb_configuration_instance
));
650 config_instance
->interfaces
= interface_count
;
651 config_instance
->configuration_descriptor
= configuration_descriptor
;
652 config_instance
->interface_instance_array
= interface_instance
;
654 /* interface instance */
655 memset (interface_instance
, 0,
656 sizeof (struct usb_interface_instance
));
657 interface_instance
->alternates
= 1;
658 interface_instance
->alternates_instance_array
= alternate_instance
;
660 /* alternates instance */
661 memset (alternate_instance
, 0,
662 sizeof (struct usb_alternate_instance
));
663 alternate_instance
->interface_descriptor
= interface_descriptors
;
664 alternate_instance
->endpoints
= NUM_ENDPOINTS
;
665 alternate_instance
->endpoints_descriptor_array
= ep_descriptor_ptrs
;
667 /* endpoint instances */
668 memset (&endpoint_instance
[0], 0,
669 sizeof (struct usb_endpoint_instance
));
670 endpoint_instance
[0].endpoint_address
= 0;
671 endpoint_instance
[0].rcv_packetSize
= EP0_MAX_PACKET_SIZE
;
672 endpoint_instance
[0].rcv_attributes
= USB_ENDPOINT_XFER_CONTROL
;
673 endpoint_instance
[0].tx_packetSize
= EP0_MAX_PACKET_SIZE
;
674 endpoint_instance
[0].tx_attributes
= USB_ENDPOINT_XFER_CONTROL
;
675 udc_setup_ep (device_instance
, 0, &endpoint_instance
[0]);
677 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
678 memset (&endpoint_instance
[i
], 0,
679 sizeof (struct usb_endpoint_instance
));
681 endpoint_instance
[i
].endpoint_address
=
682 ep_descriptor_ptrs
[i
- 1]->bEndpointAddress
;
684 endpoint_instance
[i
].rcv_attributes
=
685 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
687 endpoint_instance
[i
].rcv_packetSize
=
688 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
690 endpoint_instance
[i
].tx_attributes
=
691 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
693 endpoint_instance
[i
].tx_packetSize
=
694 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
696 endpoint_instance
[i
].tx_attributes
=
697 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
699 urb_link_init (&endpoint_instance
[i
].rcv
);
700 urb_link_init (&endpoint_instance
[i
].rdy
);
701 urb_link_init (&endpoint_instance
[i
].tx
);
702 urb_link_init (&endpoint_instance
[i
].done
);
704 if (endpoint_instance
[i
].endpoint_address
& USB_DIR_IN
)
705 endpoint_instance
[i
].tx_urb
=
706 usbd_alloc_urb (device_instance
,
707 &endpoint_instance
[i
]);
709 endpoint_instance
[i
].rcv_urb
=
710 usbd_alloc_urb (device_instance
,
711 &endpoint_instance
[i
]);
715 static void usbtty_init_endpoints (void)
719 bus_instance
->max_endpoints
= NUM_ENDPOINTS
+ 1;
720 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
721 udc_setup_ep (device_instance
, i
, &endpoint_instance
[i
]);
725 /* usbtty_init_terminal_type
727 * Do some late binding for our device type.
729 static void usbtty_init_terminal_type(short type
)
734 /* Assign endpoint descriptors */
735 ep_descriptor_ptrs
[0] =
736 &acm_configuration_descriptors
[0].notification_endpoint
;
737 ep_descriptor_ptrs
[1] =
738 &acm_configuration_descriptors
[0].data_endpoints
[0];
739 ep_descriptor_ptrs
[2] =
740 &acm_configuration_descriptors
[0].data_endpoints
[1];
742 /* Enumerate Device Descriptor */
743 device_descriptor
.bDeviceClass
=
744 COMMUNICATIONS_DEVICE_CLASS
;
745 device_descriptor
.idProduct
=
746 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM
);
748 /* Assign endpoint indices */
749 tx_endpoint
= ACM_TX_ENDPOINT
;
750 rx_endpoint
= ACM_RX_ENDPOINT
;
752 /* Configuration Descriptor */
753 configuration_descriptor
=
754 (struct usb_configuration_descriptor
*)
755 &acm_configuration_descriptors
;
757 /* Interface count */
758 interface_count
= NUM_ACM_INTERFACES
;
761 /* BULK IN/OUT & Default */
764 /* Assign endpoint descriptors */
765 ep_descriptor_ptrs
[0] =
766 &gserial_configuration_descriptors
[0].data_endpoints
[0];
767 ep_descriptor_ptrs
[1] =
768 &gserial_configuration_descriptors
[0].data_endpoints
[1];
769 ep_descriptor_ptrs
[2] =
770 &gserial_configuration_descriptors
[0].data_endpoints
[2];
772 /* Enumerate Device Descriptor */
773 device_descriptor
.bDeviceClass
= 0xFF;
774 device_descriptor
.idProduct
=
775 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL
);
777 /* Assign endpoint indices */
778 tx_endpoint
= GSERIAL_TX_ENDPOINT
;
779 rx_endpoint
= GSERIAL_RX_ENDPOINT
;
781 /* Configuration Descriptor */
782 configuration_descriptor
=
783 (struct usb_configuration_descriptor
*)
784 &gserial_configuration_descriptors
;
786 /* Interface count */
787 interface_count
= NUM_GSERIAL_INTERFACES
;
792 /******************************************************************************/
794 static struct urb
*next_urb (struct usb_device_instance
*device
,
795 struct usb_endpoint_instance
*endpoint
)
797 struct urb
*current_urb
= NULL
;
800 /* If there's a queue, then we should add to the last urb */
801 if (!endpoint
->tx_queue
) {
802 current_urb
= endpoint
->tx_urb
;
804 /* Last urb from tx chain */
806 p2surround (struct urb
, link
, endpoint
->tx
.prev
);
809 /* Make sure this one has enough room */
810 space
= current_urb
->buffer_length
- current_urb
->actual_length
;
813 } else { /* No space here */
814 /* First look at done list */
815 current_urb
= first_urb_detached (&endpoint
->done
);
817 current_urb
= usbd_alloc_urb (device
, endpoint
);
820 urb_append (&endpoint
->tx
, current_urb
);
821 endpoint
->tx_queue
++;
826 static int write_buffer (circbuf_t
* buf
)
828 if (!usbtty_configured ()) {
832 struct usb_endpoint_instance
*endpoint
=
833 &endpoint_instance
[tx_endpoint
];
834 struct urb
*current_urb
= NULL
;
836 current_urb
= next_urb (device_instance
, endpoint
);
837 /* TX data still exists - send it now
839 if(endpoint
->sent
< current_urb
->actual_length
){
840 if(udc_endpoint_write (endpoint
)){
841 /* Write pre-empted by RX */
853 /* Break buffer into urb sized pieces,
854 * and link each to the endpoint
856 while (buf
->size
> 0) {
859 TTYERR ("current_urb is NULL, buf->size %d\n",
864 dest
= (char*)current_urb
->buffer
+
865 current_urb
->actual_length
;
868 current_urb
->buffer_length
-
869 current_urb
->actual_length
;
870 popnum
= MIN (space_avail
, buf
->size
);
874 popped
= buf_pop (buf
, dest
, popnum
);
877 current_urb
->actual_length
+= popped
;
880 /* If endpoint->last == 0, then transfers have
881 * not started on this endpoint
883 if (endpoint
->last
== 0) {
884 if(udc_endpoint_write (endpoint
)){
885 /* Write pre-empted by RX */
897 static int fill_buffer (circbuf_t
* buf
)
899 struct usb_endpoint_instance
*endpoint
=
900 &endpoint_instance
[rx_endpoint
];
902 if (endpoint
->rcv_urb
&& endpoint
->rcv_urb
->actual_length
) {
904 char *src
= (char *) endpoint
->rcv_urb
->buffer
;
905 unsigned int rx_avail
= buf
->totalsize
- buf
->size
;
907 if(rx_avail
>= endpoint
->rcv_urb
->actual_length
){
909 nb
= endpoint
->rcv_urb
->actual_length
;
910 buf_push (buf
, src
, nb
);
911 endpoint
->rcv_urb
->actual_length
= 0;
919 static int usbtty_configured (void)
921 return usbtty_configured_flag
;
924 /******************************************************************************/
926 static void usbtty_event_handler (struct usb_device_instance
*device
,
927 usb_device_event_t event
, int data
)
931 case DEVICE_BUS_INACTIVE
:
932 usbtty_configured_flag
= 0;
934 case DEVICE_CONFIGURED
:
935 usbtty_configured_flag
= 1;
938 case DEVICE_ADDRESS_ASSIGNED
:
939 usbtty_init_endpoints ();
946 /******************************************************************************/
948 int usbtty_cdc_setup(struct usb_device_request
*request
, struct urb
*urb
)
950 switch (request
->bRequest
){
952 case ACM_SET_CONTROL_LINE_STATE
: /* Implies DTE ready */
954 case ACM_SEND_ENCAPSULATED_COMMAND
: /* Required */
956 case ACM_SET_LINE_ENCODING
: /* DTE stop/parity bits
959 case ACM_GET_ENCAPSULATED_RESPONSE
: /* request response */
961 case ACM_GET_LINE_ENCODING
: /* request DTE rate,
962 * stop/parity bits */
963 memcpy (urb
->buffer
, &rs232_desc
, sizeof(rs232_desc
));
964 urb
->actual_length
= sizeof(rs232_desc
);
973 /******************************************************************************/
976 * Since interrupt handling has not yet been implemented, we use this function
977 * to handle polling. This is called by the tstc,getc,putc,puts routines to
978 * update the USB state.
980 void usbtty_poll (void)
982 /* New interrupts? */
985 /* Write any output data to host buffer
986 * (do this before checking interrupts to avoid missing one)
988 if (usbtty_configured ()) {
989 write_buffer (&usbtty_output
);
992 /* New interrupts? */
995 /* Check for new data from host..
996 * (do this after checking interrupts to get latest data)
998 if (usbtty_configured ()) {
999 fill_buffer (&usbtty_input
);
1002 /* New interrupts? */