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
32 #include "usb_cdc_acm.h"
33 #include "usbdescriptors.h"
34 #include <config.h> /* If defined, override Linux identifiers with
35 * vendor specific ones */
38 #define TTYDBG(fmt,args...)\
39 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
41 #define TTYDBG(fmt,args...) do{}while(0)
45 #define TTYERR(fmt,args...)\
46 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
49 #define TTYERR(fmt,args...) do{}while(0)
56 #define MAX_INTERFACES 2
57 #define NUM_ENDPOINTS 3
58 #define ACM_TX_ENDPOINT 3
59 #define ACM_RX_ENDPOINT 2
60 #define GSERIAL_TX_ENDPOINT 2
61 #define GSERIAL_RX_ENDPOINT 1
62 #define NUM_ACM_INTERFACES 2
63 #define NUM_GSERIAL_INTERFACES 1
64 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
65 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
68 * Buffers to hold input and output data
70 #define USBTTY_BUFFER_SIZE 256
71 static circbuf_t usbtty_input
;
72 static circbuf_t usbtty_output
;
78 static device_t usbttydev
;
79 static struct usb_device_instance device_instance
[1];
80 static struct usb_bus_instance bus_instance
[1];
81 static struct usb_configuration_instance config_instance
[NUM_CONFIGS
];
82 static struct usb_interface_instance interface_instance
[MAX_INTERFACES
];
83 static struct usb_alternate_instance alternate_instance
[MAX_INTERFACES
];
84 /* one extra for control endpoint */
85 static struct usb_endpoint_instance endpoint_instance
[NUM_ENDPOINTS
+1];
90 int usbtty_configured_flag
= 0;
95 static char serial_number
[16];
99 * Descriptors, Strings, Local variables.
102 /* defined and used by usbdcore_ep0.c */
103 extern struct usb_string_descriptor
**usb_strings
;
105 /* Indicies, References */
106 static unsigned short rx_endpoint
= 0;
107 static unsigned short tx_endpoint
= 0;
108 static unsigned short interface_count
= 0;
109 static struct usb_string_descriptor
*usbtty_string_table
[STR_COUNT
];
111 /* USB Descriptor Strings */
112 static u8 wstrLang
[4] = {4,USB_DT_STRING
,0x9,0x4};
113 static u8 wstrManufacturer
[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER
)-1)];
114 static u8 wstrProduct
[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME
)-1)];
115 static u8 wstrSerial
[2 + 2*(sizeof(serial_number
) - 1)];
116 static u8 wstrConfiguration
[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR
)-1)];
117 static u8 wstrDataInterface
[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR
)-1)];
118 static u8 wstrCtrlInterface
[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR
)-1)];
120 /* Standard USB Data Structures */
121 static struct usb_interface_descriptor interface_descriptors
[MAX_INTERFACES
];
122 static struct usb_endpoint_descriptor
*ep_descriptor_ptrs
[NUM_ENDPOINTS
];
123 static struct usb_configuration_descriptor
*configuration_descriptor
= 0;
124 static struct usb_device_descriptor device_descriptor
= {
125 .bLength
= sizeof(struct usb_device_descriptor
),
126 .bDescriptorType
= USB_DT_DEVICE
,
127 .bcdUSB
= cpu_to_le16(USB_BCD_VERSION
),
128 .bDeviceSubClass
= 0x00,
129 .bDeviceProtocol
= 0x00,
130 .bMaxPacketSize0
= EP0_MAX_PACKET_SIZE
,
131 .idVendor
= cpu_to_le16(CONFIG_USBD_VENDORID
),
132 .bcdDevice
= cpu_to_le16(USBTTY_BCD_DEVICE
),
133 .iManufacturer
= STR_MANUFACTURER
,
134 .iProduct
= STR_PRODUCT
,
135 .iSerialNumber
= STR_SERIAL
,
136 .bNumConfigurations
= NUM_CONFIGS
141 * Static CDC ACM specific descriptors
144 struct acm_config_desc
{
145 struct usb_configuration_descriptor configuration_desc
;
147 /* Master Interface */
148 struct usb_interface_descriptor interface_desc
;
150 struct usb_class_header_function_descriptor usb_class_header
;
151 struct usb_class_call_management_descriptor usb_class_call_mgt
;
152 struct usb_class_abstract_control_descriptor usb_class_acm
;
153 struct usb_class_union_function_descriptor usb_class_union
;
154 struct usb_endpoint_descriptor notification_endpoint
;
156 /* Slave Interface */
157 struct usb_interface_descriptor data_class_interface
;
158 struct usb_endpoint_descriptor
159 data_endpoints
[NUM_ENDPOINTS
-1] __attribute__((packed
));
160 } __attribute__((packed
));
162 static struct acm_config_desc acm_configuration_descriptors
[NUM_CONFIGS
] = {
164 .configuration_desc
={
166 sizeof(struct usb_configuration_descriptor
),
167 .bDescriptorType
= USB_DT_CONFIG
,
169 cpu_to_le16(sizeof(struct acm_config_desc
)),
170 .bNumInterfaces
= NUM_ACM_INTERFACES
,
171 .bConfigurationValue
= 1,
172 .iConfiguration
= STR_CONFIG
,
174 BMATTRIBUTE_SELF_POWERED
|BMATTRIBUTE_RESERVED
,
175 .bMaxPower
= USBTTY_MAXPOWER
179 .bLength
= sizeof(struct usb_interface_descriptor
),
180 .bDescriptorType
= USB_DT_INTERFACE
,
181 .bInterfaceNumber
= 0,
182 .bAlternateSetting
= 0,
183 .bNumEndpoints
= 0x01,
185 COMMUNICATIONS_INTERFACE_CLASS_CONTROL
,
186 .bInterfaceSubClass
= COMMUNICATIONS_ACM_SUBCLASS
,
187 .bInterfaceProtocol
= COMMUNICATIONS_V25TER_PROTOCOL
,
188 .iInterface
= STR_CTRL_INTERFACE
,
190 .usb_class_header
= {
192 sizeof(struct usb_class_header_function_descriptor
),
193 .bDescriptorType
= CS_INTERFACE
,
194 .bDescriptorSubtype
= USB_ST_HEADER
,
195 .bcdCDC
= cpu_to_le16(110),
197 .usb_class_call_mgt
= {
199 sizeof(struct usb_class_call_management_descriptor
),
200 .bDescriptorType
= CS_INTERFACE
,
201 .bDescriptorSubtype
= USB_ST_CMF
,
202 .bmCapabilities
= 0x00,
203 .bDataInterface
= 0x01,
207 sizeof(struct usb_class_abstract_control_descriptor
),
208 .bDescriptorType
= CS_INTERFACE
,
209 .bDescriptorSubtype
= USB_ST_ACMF
,
210 .bmCapabilities
= 0x00,
214 sizeof(struct usb_class_union_function_descriptor
),
215 .bDescriptorType
= CS_INTERFACE
,
216 .bDescriptorSubtype
= USB_ST_UF
,
217 .bMasterInterface
= 0x00,
218 .bSlaveInterface0
= 0x01,
220 .notification_endpoint
= {
222 sizeof(struct usb_endpoint_descriptor
),
223 .bDescriptorType
= USB_DT_ENDPOINT
,
224 .bEndpointAddress
= 0x01 | USB_DIR_IN
,
225 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
227 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
232 .data_class_interface
= {
234 sizeof(struct usb_interface_descriptor
),
235 .bDescriptorType
= USB_DT_INTERFACE
,
236 .bInterfaceNumber
= 0x01,
237 .bAlternateSetting
= 0x00,
238 .bNumEndpoints
= 0x02,
240 COMMUNICATIONS_INTERFACE_CLASS_DATA
,
241 .bInterfaceSubClass
= DATA_INTERFACE_SUBCLASS_NONE
,
242 .bInterfaceProtocol
= DATA_INTERFACE_PROTOCOL_NONE
,
243 .iInterface
= STR_DATA_INTERFACE
,
248 sizeof(struct usb_endpoint_descriptor
),
249 .bDescriptorType
= USB_DT_ENDPOINT
,
250 .bEndpointAddress
= 0x02 | USB_DIR_OUT
,
252 USB_ENDPOINT_XFER_BULK
,
254 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
259 sizeof(struct usb_endpoint_descriptor
),
260 .bDescriptorType
= USB_DT_ENDPOINT
,
261 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
263 USB_ENDPOINT_XFER_BULK
,
265 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
272 static struct rs232_emu rs232_desc
={
281 * Static Generic Serial specific data
285 struct gserial_config_desc
{
287 struct usb_configuration_descriptor configuration_desc
;
288 struct usb_interface_descriptor
289 interface_desc
[NUM_GSERIAL_INTERFACES
] __attribute__((packed
));
290 struct usb_endpoint_descriptor
291 data_endpoints
[NUM_ENDPOINTS
] __attribute__((packed
));
293 } __attribute__((packed
));
295 static struct gserial_config_desc
296 gserial_configuration_descriptors
[NUM_CONFIGS
] ={
298 .configuration_desc
={
299 .bLength
= sizeof(struct usb_configuration_descriptor
),
300 .bDescriptorType
= USB_DT_CONFIG
,
302 cpu_to_le16(sizeof(struct gserial_config_desc
)),
303 .bNumInterfaces
= NUM_GSERIAL_INTERFACES
,
304 .bConfigurationValue
= 1,
305 .iConfiguration
= STR_CONFIG
,
307 BMATTRIBUTE_SELF_POWERED
|BMATTRIBUTE_RESERVED
,
308 .bMaxPower
= USBTTY_MAXPOWER
313 sizeof(struct usb_interface_descriptor
),
314 .bDescriptorType
= USB_DT_INTERFACE
,
315 .bInterfaceNumber
= 0,
316 .bAlternateSetting
= 0,
317 .bNumEndpoints
= NUM_ENDPOINTS
,
319 COMMUNICATIONS_INTERFACE_CLASS_VENDOR
,
320 .bInterfaceSubClass
=
321 COMMUNICATIONS_NO_SUBCLASS
,
322 .bInterfaceProtocol
=
323 COMMUNICATIONS_NO_PROTOCOL
,
324 .iInterface
= STR_DATA_INTERFACE
330 sizeof(struct usb_endpoint_descriptor
),
331 .bDescriptorType
= USB_DT_ENDPOINT
,
332 .bEndpointAddress
= 0x01 | USB_DIR_OUT
,
333 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
335 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE
),
340 sizeof(struct usb_endpoint_descriptor
),
341 .bDescriptorType
= USB_DT_ENDPOINT
,
342 .bEndpointAddress
= 0x02 | USB_DIR_IN
,
343 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
345 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE
),
350 sizeof(struct usb_endpoint_descriptor
),
351 .bDescriptorType
= USB_DT_ENDPOINT
,
352 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
353 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
355 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
363 * Static Function Prototypes
366 static void usbtty_init_strings (void);
367 static void usbtty_init_instances (void);
368 static void usbtty_init_endpoints (void);
369 static void usbtty_init_terminal_type(short type
);
370 static void usbtty_event_handler (struct usb_device_instance
*device
,
371 usb_device_event_t event
, int data
);
372 static int usbtty_cdc_setup(struct usb_device_request
*request
,
374 static int usbtty_configured (void);
375 static int write_buffer (circbuf_t
* buf
);
376 static int fill_buffer (circbuf_t
* buf
);
378 void usbtty_poll (void);
380 /* utility function for converting char* to wide string used by USB */
381 static void str2wide (char *str
, u16
* wide
)
384 for (i
= 0; i
< strlen (str
) && str
[i
]; i
++){
385 #if defined(__LITTLE_ENDIAN)
386 wide
[i
] = (u16
) str
[i
];
387 #elif defined(__BIG_ENDIAN)
388 wide
[i
] = ((u16
)(str
[i
])<<8);
390 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
396 * Test whether a character is in the RX buffer
399 int usbtty_tstc (void)
401 struct usb_endpoint_instance
*endpoint
=
402 &endpoint_instance
[rx_endpoint
];
404 /* If no input data exists, allow more RX to be accepted */
405 if(usbtty_input
.size
<= 0){
406 udc_unset_nak(endpoint
->endpoint_address
&0x03);
410 return (usbtty_input
.size
> 0);
414 * Read a single byte from the usb client port. Returns 1 on success, 0
415 * otherwise. When the function is succesfull, the character read is
416 * written into its argument c.
419 int usbtty_getc (void)
422 struct usb_endpoint_instance
*endpoint
=
423 &endpoint_instance
[rx_endpoint
];
425 while (usbtty_input
.size
<= 0) {
426 udc_unset_nak(endpoint
->endpoint_address
&0x03);
430 buf_pop (&usbtty_input
, &c
, 1);
431 udc_set_nak(endpoint
->endpoint_address
&0x03);
437 * Output a single byte to the usb client port.
439 void usbtty_putc (const char c
)
441 buf_push (&usbtty_output
, &c
, 1);
442 /* If \n, also do \r */
444 buf_push (&usbtty_output
, "\r", 1);
446 /* Poll at end to handle new data... */
447 if ((usbtty_output
.size
+ 2) >= usbtty_output
.totalsize
) {
452 /* usbtty_puts() helper function for finding the next '\n' in a string */
453 static int next_nl_pos (const char *s
)
457 for (i
= 0; s
[i
] != '\0'; i
++) {
465 * Output a string to the usb client port - implementing flow control
468 static void __usbtty_puts (const char *str
, int len
)
470 int maxlen
= usbtty_output
.totalsize
;
473 /* break str into chunks < buffer size, if needed */
477 space
= maxlen
- usbtty_output
.size
;
479 /* If the USB is not configured, allow the circular buffer to
480 be overwritten. Otherwise this while() will loop forever. */
481 if (!usbtty_configured())
484 /* Empty buffer here, if needed, to ensure space... */
486 write_buffer (&usbtty_output
);
488 n
= MIN (space
, MIN (len
, maxlen
));
489 buf_push (&usbtty_output
, str
, n
);
497 void usbtty_puts (const char *str
)
500 int len
= strlen (str
);
502 /* add '\r' for each '\n' */
504 n
= next_nl_pos (str
);
506 if (str
[n
] == '\n') {
507 __usbtty_puts (str
, n
+ 1);
508 __usbtty_puts ("\r", 1);
512 /* No \n found. All done. */
513 __usbtty_puts (str
, n
);
518 /* Poll at end to handle new data... */
523 * Initialize the usb client port.
526 int drv_usbtty_init (void)
533 /* Ger seiral number */
534 if (!(sn
= getenv("serial#"))) {
538 if (snlen
> sizeof(serial_number
) - 1) {
539 printf ("Warning: serial number %s is too long (%d > %d)\n",
540 sn
, snlen
, sizeof(serial_number
) - 1);
541 snlen
= sizeof(serial_number
) - 1;
543 memcpy (serial_number
, sn
, snlen
);
544 serial_number
[snlen
] = '\0';
546 /* Decide on which type of UDC device to be.
549 if(!(tt
= getenv("usbtty"))) {
552 usbtty_init_terminal_type(strcmp(tt
,"cdc_acm"));
554 /* Decide on which type of UDC device to be.
557 if(!(tt
= getenv("usbtty"))) {
560 usbtty_init_terminal_type(strcmp(tt
,"cdc_acm"));
562 /* prepare buffers... */
563 buf_init (&usbtty_input
, USBTTY_BUFFER_SIZE
);
564 buf_init (&usbtty_output
, USBTTY_BUFFER_SIZE
);
566 /* Now, set up USB controller and infrastructure */
567 udc_init (); /* Basic USB initialization */
569 usbtty_init_strings ();
570 usbtty_init_instances ();
572 udc_startup_events (device_instance
);/* Enable dev, init udc pointers */
573 udc_connect (); /* Enable pullup for host detection */
575 usbtty_init_endpoints ();
577 /* Device initialization */
578 memset (&usbttydev
, 0, sizeof (usbttydev
));
580 strcpy (usbttydev
.name
, "usbtty");
581 usbttydev
.ext
= 0; /* No extensions */
582 usbttydev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_OUTPUT
;
583 usbttydev
.tstc
= usbtty_tstc
; /* 'tstc' function */
584 usbttydev
.getc
= usbtty_getc
; /* 'getc' function */
585 usbttydev
.putc
= usbtty_putc
; /* 'putc' function */
586 usbttydev
.puts
= usbtty_puts
; /* 'puts' function */
588 rc
= device_register (&usbttydev
);
590 return (rc
== 0) ? 1 : rc
;
593 static void usbtty_init_strings (void)
595 struct usb_string_descriptor
*string
;
597 usbtty_string_table
[STR_LANG
] =
598 (struct usb_string_descriptor
*)wstrLang
;
600 string
= (struct usb_string_descriptor
*) wstrManufacturer
;
601 string
->bLength
= sizeof(wstrManufacturer
);
602 string
->bDescriptorType
= USB_DT_STRING
;
603 str2wide (CONFIG_USBD_MANUFACTURER
, string
->wData
);
604 usbtty_string_table
[STR_MANUFACTURER
]=string
;
607 string
= (struct usb_string_descriptor
*) wstrProduct
;
608 string
->bLength
= sizeof(wstrProduct
);
609 string
->bDescriptorType
= USB_DT_STRING
;
610 str2wide (CONFIG_USBD_PRODUCT_NAME
, string
->wData
);
611 usbtty_string_table
[STR_PRODUCT
]=string
;
614 string
= (struct usb_string_descriptor
*) wstrSerial
;
615 string
->bLength
= sizeof(serial_number
);
616 string
->bDescriptorType
= USB_DT_STRING
;
617 str2wide (serial_number
, string
->wData
);
618 usbtty_string_table
[STR_SERIAL
]=string
;
621 string
= (struct usb_string_descriptor
*) wstrConfiguration
;
622 string
->bLength
= sizeof(wstrConfiguration
);
623 string
->bDescriptorType
= USB_DT_STRING
;
624 str2wide (CONFIG_USBD_CONFIGURATION_STR
, string
->wData
);
625 usbtty_string_table
[STR_CONFIG
]=string
;
628 string
= (struct usb_string_descriptor
*) wstrDataInterface
;
629 string
->bLength
= sizeof(wstrDataInterface
);
630 string
->bDescriptorType
= USB_DT_STRING
;
631 str2wide (CONFIG_USBD_DATA_INTERFACE_STR
, string
->wData
);
632 usbtty_string_table
[STR_DATA_INTERFACE
]=string
;
634 string
= (struct usb_string_descriptor
*) wstrCtrlInterface
;
635 string
->bLength
= sizeof(wstrCtrlInterface
);
636 string
->bDescriptorType
= USB_DT_STRING
;
637 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR
, string
->wData
);
638 usbtty_string_table
[STR_CTRL_INTERFACE
]=string
;
640 /* Now, initialize the string table for ep0 handling */
641 usb_strings
= usbtty_string_table
;
644 static void usbtty_init_instances (void)
648 /* initialize device instance */
649 memset (device_instance
, 0, sizeof (struct usb_device_instance
));
650 device_instance
->device_state
= STATE_INIT
;
651 device_instance
->device_descriptor
= &device_descriptor
;
652 device_instance
->event
= usbtty_event_handler
;
653 device_instance
->cdc_recv_setup
= usbtty_cdc_setup
;
654 device_instance
->bus
= bus_instance
;
655 device_instance
->configurations
= NUM_CONFIGS
;
656 device_instance
->configuration_instance_array
= config_instance
;
658 /* initialize bus instance */
659 memset (bus_instance
, 0, sizeof (struct usb_bus_instance
));
660 bus_instance
->device
= device_instance
;
661 bus_instance
->endpoint_array
= endpoint_instance
;
662 bus_instance
->max_endpoints
= 1;
663 bus_instance
->maxpacketsize
= 64;
664 bus_instance
->serial_number_str
= serial_number
;
666 /* configuration instance */
667 memset (config_instance
, 0,
668 sizeof (struct usb_configuration_instance
));
669 config_instance
->interfaces
= interface_count
;
670 config_instance
->configuration_descriptor
= configuration_descriptor
;
671 config_instance
->interface_instance_array
= interface_instance
;
673 /* interface instance */
674 memset (interface_instance
, 0,
675 sizeof (struct usb_interface_instance
));
676 interface_instance
->alternates
= 1;
677 interface_instance
->alternates_instance_array
= alternate_instance
;
679 /* alternates instance */
680 memset (alternate_instance
, 0,
681 sizeof (struct usb_alternate_instance
));
682 alternate_instance
->interface_descriptor
= interface_descriptors
;
683 alternate_instance
->endpoints
= NUM_ENDPOINTS
;
684 alternate_instance
->endpoints_descriptor_array
= ep_descriptor_ptrs
;
686 /* endpoint instances */
687 memset (&endpoint_instance
[0], 0,
688 sizeof (struct usb_endpoint_instance
));
689 endpoint_instance
[0].endpoint_address
= 0;
690 endpoint_instance
[0].rcv_packetSize
= EP0_MAX_PACKET_SIZE
;
691 endpoint_instance
[0].rcv_attributes
= USB_ENDPOINT_XFER_CONTROL
;
692 endpoint_instance
[0].tx_packetSize
= EP0_MAX_PACKET_SIZE
;
693 endpoint_instance
[0].tx_attributes
= USB_ENDPOINT_XFER_CONTROL
;
694 udc_setup_ep (device_instance
, 0, &endpoint_instance
[0]);
696 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
697 memset (&endpoint_instance
[i
], 0,
698 sizeof (struct usb_endpoint_instance
));
700 endpoint_instance
[i
].endpoint_address
=
701 ep_descriptor_ptrs
[i
- 1]->bEndpointAddress
;
703 endpoint_instance
[i
].rcv_attributes
=
704 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
706 endpoint_instance
[i
].rcv_packetSize
=
707 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
709 endpoint_instance
[i
].tx_attributes
=
710 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
712 endpoint_instance
[i
].tx_packetSize
=
713 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
715 endpoint_instance
[i
].tx_attributes
=
716 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
718 urb_link_init (&endpoint_instance
[i
].rcv
);
719 urb_link_init (&endpoint_instance
[i
].rdy
);
720 urb_link_init (&endpoint_instance
[i
].tx
);
721 urb_link_init (&endpoint_instance
[i
].done
);
723 if (endpoint_instance
[i
].endpoint_address
& USB_DIR_IN
)
724 endpoint_instance
[i
].tx_urb
=
725 usbd_alloc_urb (device_instance
,
726 &endpoint_instance
[i
]);
728 endpoint_instance
[i
].rcv_urb
=
729 usbd_alloc_urb (device_instance
,
730 &endpoint_instance
[i
]);
734 static void usbtty_init_endpoints (void)
738 bus_instance
->max_endpoints
= NUM_ENDPOINTS
+ 1;
739 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
740 udc_setup_ep (device_instance
, i
, &endpoint_instance
[i
]);
744 /* usbtty_init_terminal_type
746 * Do some late binding for our device type.
748 static void usbtty_init_terminal_type(short type
)
753 /* Assign endpoint descriptors */
754 ep_descriptor_ptrs
[0] =
755 &acm_configuration_descriptors
[0].notification_endpoint
;
756 ep_descriptor_ptrs
[1] =
757 &acm_configuration_descriptors
[0].data_endpoints
[0];
758 ep_descriptor_ptrs
[2] =
759 &acm_configuration_descriptors
[0].data_endpoints
[1];
761 /* Enumerate Device Descriptor */
762 device_descriptor
.bDeviceClass
=
763 COMMUNICATIONS_DEVICE_CLASS
;
764 device_descriptor
.idProduct
=
765 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM
);
767 /* Assign endpoint indices */
768 tx_endpoint
= ACM_TX_ENDPOINT
;
769 rx_endpoint
= ACM_RX_ENDPOINT
;
771 /* Configuration Descriptor */
772 configuration_descriptor
=
773 (struct usb_configuration_descriptor
*)
774 &acm_configuration_descriptors
;
776 /* Interface count */
777 interface_count
= NUM_ACM_INTERFACES
;
780 /* BULK IN/OUT & Default */
783 /* Assign endpoint descriptors */
784 ep_descriptor_ptrs
[0] =
785 &gserial_configuration_descriptors
[0].data_endpoints
[0];
786 ep_descriptor_ptrs
[1] =
787 &gserial_configuration_descriptors
[0].data_endpoints
[1];
788 ep_descriptor_ptrs
[2] =
789 &gserial_configuration_descriptors
[0].data_endpoints
[2];
791 /* Enumerate Device Descriptor */
792 device_descriptor
.bDeviceClass
= 0xFF;
793 device_descriptor
.idProduct
=
794 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL
);
796 /* Assign endpoint indices */
797 tx_endpoint
= GSERIAL_TX_ENDPOINT
;
798 rx_endpoint
= GSERIAL_RX_ENDPOINT
;
800 /* Configuration Descriptor */
801 configuration_descriptor
=
802 (struct usb_configuration_descriptor
*)
803 &gserial_configuration_descriptors
;
805 /* Interface count */
806 interface_count
= NUM_GSERIAL_INTERFACES
;
811 /******************************************************************************/
813 static struct urb
*next_urb (struct usb_device_instance
*device
,
814 struct usb_endpoint_instance
*endpoint
)
816 struct urb
*current_urb
= NULL
;
819 /* If there's a queue, then we should add to the last urb */
820 if (!endpoint
->tx_queue
) {
821 current_urb
= endpoint
->tx_urb
;
823 /* Last urb from tx chain */
825 p2surround (struct urb
, link
, endpoint
->tx
.prev
);
828 /* Make sure this one has enough room */
829 space
= current_urb
->buffer_length
- current_urb
->actual_length
;
832 } else { /* No space here */
833 /* First look at done list */
834 current_urb
= first_urb_detached (&endpoint
->done
);
836 current_urb
= usbd_alloc_urb (device
, endpoint
);
839 urb_append (&endpoint
->tx
, current_urb
);
840 endpoint
->tx_queue
++;
845 static int write_buffer (circbuf_t
* buf
)
847 if (!usbtty_configured ()) {
851 struct usb_endpoint_instance
*endpoint
=
852 &endpoint_instance
[tx_endpoint
];
853 struct urb
*current_urb
= NULL
;
855 current_urb
= next_urb (device_instance
, endpoint
);
856 /* TX data still exists - send it now
858 if(endpoint
->sent
< current_urb
->actual_length
){
859 if(udc_endpoint_write (endpoint
)){
860 /* Write pre-empted by RX */
872 /* Break buffer into urb sized pieces,
873 * and link each to the endpoint
875 while (buf
->size
> 0) {
878 TTYERR ("current_urb is NULL, buf->size %d\n",
883 dest
= (char*)current_urb
->buffer
+
884 current_urb
->actual_length
;
887 current_urb
->buffer_length
-
888 current_urb
->actual_length
;
889 popnum
= MIN (space_avail
, buf
->size
);
893 popped
= buf_pop (buf
, dest
, popnum
);
896 current_urb
->actual_length
+= popped
;
899 /* If endpoint->last == 0, then transfers have
900 * not started on this endpoint
902 if (endpoint
->last
== 0) {
903 if(udc_endpoint_write (endpoint
)){
904 /* Write pre-empted by RX */
916 static int fill_buffer (circbuf_t
* buf
)
918 struct usb_endpoint_instance
*endpoint
=
919 &endpoint_instance
[rx_endpoint
];
921 if (endpoint
->rcv_urb
&& endpoint
->rcv_urb
->actual_length
) {
923 char *src
= (char *) endpoint
->rcv_urb
->buffer
;
924 unsigned int rx_avail
= buf
->totalsize
- buf
->size
;
926 if(rx_avail
>= endpoint
->rcv_urb
->actual_length
){
928 nb
= endpoint
->rcv_urb
->actual_length
;
929 buf_push (buf
, src
, nb
);
930 endpoint
->rcv_urb
->actual_length
= 0;
938 static int usbtty_configured (void)
940 return usbtty_configured_flag
;
943 /******************************************************************************/
945 static void usbtty_event_handler (struct usb_device_instance
*device
,
946 usb_device_event_t event
, int data
)
950 case DEVICE_BUS_INACTIVE
:
951 usbtty_configured_flag
= 0;
953 case DEVICE_CONFIGURED
:
954 usbtty_configured_flag
= 1;
957 case DEVICE_ADDRESS_ASSIGNED
:
958 usbtty_init_endpoints ();
965 /******************************************************************************/
967 int usbtty_cdc_setup(struct usb_device_request
*request
, struct urb
*urb
)
969 switch (request
->bRequest
){
971 case ACM_SET_CONTROL_LINE_STATE
: /* Implies DTE ready */
973 case ACM_SEND_ENCAPSULATED_COMMAND
: /* Required */
975 case ACM_SET_LINE_ENCODING
: /* DTE stop/parity bits
978 case ACM_GET_ENCAPSULATED_RESPONSE
: /* request response */
980 case ACM_GET_LINE_ENCODING
: /* request DTE rate,
981 * stop/parity bits */
982 memcpy (urb
->buffer
, &rs232_desc
, sizeof(rs232_desc
));
983 urb
->actual_length
= sizeof(rs232_desc
);
992 /******************************************************************************/
995 * Since interrupt handling has not yet been implemented, we use this function
996 * to handle polling. This is called by the tstc,getc,putc,puts routines to
997 * update the USB state.
999 void usbtty_poll (void)
1001 /* New interrupts? */
1004 /* Write any output data to host buffer
1005 * (do this before checking interrupts to avoid missing one)
1007 if (usbtty_configured ()) {
1008 write_buffer (&usbtty_output
);
1011 /* New interrupts? */
1014 /* Check for new data from host..
1015 * (do this after checking interrupts to get latest data)
1017 if (usbtty_configured ()) {
1018 fill_buffer (&usbtty_input
);
1021 /* New interrupts? */