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 <asm/proc/system.h>
33 #include "usb_cdc_acm.h"
34 #include "usbdescriptors.h"
35 #include <usb_dfu_descriptors.h>
37 #include <config.h> /* If defined, override Linux identifiers with
38 * vendor specific ones */
41 #define TTYDBG(fmt,args...)\
42 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
44 #define TTYDBG(fmt,args...) do{}while(0)
48 #define TTYERR(fmt,args...)\
49 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
52 #define TTYERR(fmt,args...) do{}while(0)
59 #define MAX_INTERFACES 2
60 #define NUM_ENDPOINTS 3
61 #define ACM_TX_ENDPOINT 3
62 #define ACM_RX_ENDPOINT 2
63 #define GSERIAL_TX_ENDPOINT 2
64 #define GSERIAL_RX_ENDPOINT 1
65 #define NUM_ACM_INTERFACES 2
66 #define NUM_GSERIAL_INTERFACES 1
67 #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
68 #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
71 * Buffers to hold input and output data
73 #define USBTTY_BUFFER_SIZE 256
74 static circbuf_t usbtty_input
;
75 static circbuf_t usbtty_output
;
81 static device_t usbttydev
;
82 static struct usb_device_instance device_instance
[1];
83 static struct usb_bus_instance bus_instance
[1];
84 static struct usb_configuration_instance config_instance
[NUM_CONFIGS
];
85 static struct usb_interface_instance interface_instance
[MAX_INTERFACES
];
86 static struct usb_alternate_instance alternate_instance
[MAX_INTERFACES
];
87 /* one extra for control endpoint */
88 static struct usb_endpoint_instance endpoint_instance
[NUM_ENDPOINTS
+1];
93 int usbtty_configured_flag
= 0;
98 static char serial_number
[16];
102 * Descriptors, Strings, Local variables.
105 /* defined and used by usbdcore_ep0.c */
106 extern struct usb_string_descriptor
**usb_strings
;
108 /* Indicies, References */
109 static unsigned short rx_endpoint
= 0;
110 static unsigned short tx_endpoint
= 0;
111 static unsigned short interface_count
= 0;
112 static struct usb_string_descriptor
*usbtty_string_table
[NUM_STRINGS
];
114 /* USB Descriptor Strings */
115 static u8 wstrLang
[4] = {4,USB_DT_STRING
,0x9,0x4};
116 static u8 wstrManufacturer
[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER
)-1)];
117 static u8 wstrProduct
[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME
)-1)];
118 static u8 wstrSerial
[2 + 2*(sizeof(serial_number
) - 1)];
119 static u8 wstrConfiguration
[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR
)-1)];
120 static u8 wstrDataInterface
[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR
)-1)];
121 static u8 wstrCtrlInterface
[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR
)-1)];
123 /* Standard USB Data Structures */
124 static struct usb_interface_descriptor interface_descriptors
[MAX_INTERFACES
];
125 static struct usb_endpoint_descriptor
*ep_descriptor_ptrs
[NUM_ENDPOINTS
];
126 static struct usb_configuration_descriptor
*configuration_descriptor
= 0;
127 static struct usb_device_descriptor device_descriptor
= {
128 .bLength
= sizeof(struct usb_device_descriptor
),
129 .bDescriptorType
= USB_DT_DEVICE
,
130 .bcdUSB
= cpu_to_le16(USB_BCD_VERSION
),
131 .bDeviceSubClass
= 0x00,
132 .bDeviceProtocol
= 0x00,
133 .bMaxPacketSize0
= EP0_MAX_PACKET_SIZE
,
134 .idVendor
= cpu_to_le16(CONFIG_USBD_VENDORID
),
135 .bcdDevice
= cpu_to_le16(USBTTY_BCD_DEVICE
),
136 .iManufacturer
= STR_MANUFACTURER
,
137 .iProduct
= STR_PRODUCT
,
138 .iSerialNumber
= STR_SERIAL
,
139 .bNumConfigurations
= NUM_CONFIGS
144 * Static CDC ACM specific descriptors
147 struct acm_config_desc
{
148 struct usb_configuration_descriptor configuration_desc
;
150 /* Master Interface */
151 struct usb_interface_descriptor interface_desc
;
153 struct usb_class_header_function_descriptor usb_class_header
;
154 struct usb_class_call_management_descriptor usb_class_call_mgt
;
155 struct usb_class_abstract_control_descriptor usb_class_acm
;
156 struct usb_class_union_function_descriptor usb_class_union
;
157 struct usb_endpoint_descriptor notification_endpoint
;
159 /* Slave Interface */
160 struct usb_interface_descriptor data_class_interface
;
161 struct usb_endpoint_descriptor
162 data_endpoints
[NUM_ENDPOINTS
-1] __attribute__((packed
));
163 #ifdef CONFIG_USBD_DFU
164 struct usb_interface_descriptor uif_dfu
;
165 struct usb_dfu_func_descriptor func_dfu
;
167 } __attribute__((packed
));
169 static struct acm_config_desc acm_configuration_descriptors
[NUM_CONFIGS
] = {
171 .configuration_desc
={
173 sizeof(struct usb_configuration_descriptor
),
174 .bDescriptorType
= USB_DT_CONFIG
,
176 cpu_to_le16(sizeof(struct acm_config_desc
)),
177 #ifdef CONFIG_USBD_DFU
178 .bNumInterfaces
= NUM_ACM_INTERFACES
+1,
180 .bNumInterfaces
= NUM_ACM_INTERFACES
,
182 .bConfigurationValue
= 1,
183 .iConfiguration
= STR_CONFIG
,
184 .bmAttributes
= BMATTRIBUTE_RESERVED
,
185 .bMaxPower
= USBTTY_MAXPOWER
189 .bLength
= sizeof(struct usb_interface_descriptor
),
190 .bDescriptorType
= USB_DT_INTERFACE
,
191 .bInterfaceNumber
= 0,
192 .bAlternateSetting
= 0,
193 .bNumEndpoints
= 0x01,
195 COMMUNICATIONS_INTERFACE_CLASS_CONTROL
,
196 .bInterfaceSubClass
= COMMUNICATIONS_ACM_SUBCLASS
,
197 .bInterfaceProtocol
= COMMUNICATIONS_V25TER_PROTOCOL
,
198 .iInterface
= STR_CTRL_INTERFACE
,
200 .usb_class_header
= {
202 sizeof(struct usb_class_header_function_descriptor
),
203 .bDescriptorType
= CS_INTERFACE
,
204 .bDescriptorSubtype
= USB_ST_HEADER
,
205 .bcdCDC
= cpu_to_le16(110),
207 .usb_class_call_mgt
= {
209 sizeof(struct usb_class_call_management_descriptor
),
210 .bDescriptorType
= CS_INTERFACE
,
211 .bDescriptorSubtype
= USB_ST_CMF
,
212 .bmCapabilities
= 0x00,
213 .bDataInterface
= 0x01,
217 sizeof(struct usb_class_abstract_control_descriptor
),
218 .bDescriptorType
= CS_INTERFACE
,
219 .bDescriptorSubtype
= USB_ST_ACMF
,
220 .bmCapabilities
= 0x00,
224 sizeof(struct usb_class_union_function_descriptor
),
225 .bDescriptorType
= CS_INTERFACE
,
226 .bDescriptorSubtype
= USB_ST_UF
,
227 .bMasterInterface
= 0x00,
228 .bSlaveInterface0
= 0x01,
230 .notification_endpoint
= {
232 sizeof(struct usb_endpoint_descriptor
),
233 .bDescriptorType
= USB_DT_ENDPOINT
,
234 .bEndpointAddress
= 0x01 | USB_DIR_IN
,
235 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
237 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
242 .data_class_interface
= {
244 sizeof(struct usb_interface_descriptor
),
245 .bDescriptorType
= USB_DT_INTERFACE
,
246 .bInterfaceNumber
= 0x01,
247 .bAlternateSetting
= 0x00,
248 .bNumEndpoints
= 0x02,
250 COMMUNICATIONS_INTERFACE_CLASS_DATA
,
251 .bInterfaceSubClass
= DATA_INTERFACE_SUBCLASS_NONE
,
252 .bInterfaceProtocol
= DATA_INTERFACE_PROTOCOL_NONE
,
253 .iInterface
= STR_DATA_INTERFACE
,
258 sizeof(struct usb_endpoint_descriptor
),
259 .bDescriptorType
= USB_DT_ENDPOINT
,
260 .bEndpointAddress
= 0x02 | USB_DIR_OUT
,
262 USB_ENDPOINT_XFER_BULK
,
264 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
269 sizeof(struct usb_endpoint_descriptor
),
270 .bDescriptorType
= USB_DT_ENDPOINT
,
271 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
273 USB_ENDPOINT_XFER_BULK
,
275 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
279 #ifdef CONFIG_USBD_DFU
281 .uif_dfu
= DFU_RT_IF_DESC
,
282 .func_dfu
= DFU_FUNC_DESC
,
286 .configuration_desc
={
288 sizeof(struct usb_configuration_descriptor
),
289 .bDescriptorType
= USB_DT_CONFIG
,
291 cpu_to_le16(sizeof(struct acm_config_desc
)
292 #ifdef CONFIG_USBD_DFU
293 - sizeof(struct usb_interface_descriptor
)
294 - sizeof(struct usb_dfu_func_descriptor
)
297 .bNumInterfaces
= NUM_ACM_INTERFACES
,
298 .bConfigurationValue
= 2,
299 .iConfiguration
= STR_CONFIG
,
300 .bmAttributes
= BMATTRIBUTE_RESERVED
,
301 .bMaxPower
= 50, /* 100mA */
305 .bLength
= sizeof(struct usb_interface_descriptor
),
306 .bDescriptorType
= USB_DT_INTERFACE
,
307 .bInterfaceNumber
= 0,
308 .bAlternateSetting
= 0,
309 .bNumEndpoints
= 0x01,
311 COMMUNICATIONS_INTERFACE_CLASS_CONTROL
,
312 .bInterfaceSubClass
= COMMUNICATIONS_ACM_SUBCLASS
,
313 .bInterfaceProtocol
= COMMUNICATIONS_V25TER_PROTOCOL
,
314 .iInterface
= STR_CTRL_INTERFACE
,
316 .usb_class_header
= {
318 sizeof(struct usb_class_header_function_descriptor
),
319 .bDescriptorType
= CS_INTERFACE
,
320 .bDescriptorSubtype
= USB_ST_HEADER
,
321 .bcdCDC
= cpu_to_le16(110),
323 .usb_class_call_mgt
= {
325 sizeof(struct usb_class_call_management_descriptor
),
326 .bDescriptorType
= CS_INTERFACE
,
327 .bDescriptorSubtype
= USB_ST_CMF
,
328 .bmCapabilities
= 0x00,
329 .bDataInterface
= 0x01,
333 sizeof(struct usb_class_abstract_control_descriptor
),
334 .bDescriptorType
= CS_INTERFACE
,
335 .bDescriptorSubtype
= USB_ST_ACMF
,
336 .bmCapabilities
= 0x00,
340 sizeof(struct usb_class_union_function_descriptor
),
341 .bDescriptorType
= CS_INTERFACE
,
342 .bDescriptorSubtype
= USB_ST_UF
,
343 .bMasterInterface
= 0x00,
344 .bSlaveInterface0
= 0x01,
346 .notification_endpoint
= {
348 sizeof(struct usb_endpoint_descriptor
),
349 .bDescriptorType
= USB_DT_ENDPOINT
,
350 .bEndpointAddress
= 0x01 | USB_DIR_IN
,
351 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
353 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
358 .data_class_interface
= {
360 sizeof(struct usb_interface_descriptor
),
361 .bDescriptorType
= USB_DT_INTERFACE
,
362 .bInterfaceNumber
= 0x01,
363 .bAlternateSetting
= 0x00,
364 .bNumEndpoints
= 0x02,
366 COMMUNICATIONS_INTERFACE_CLASS_DATA
,
367 .bInterfaceSubClass
= DATA_INTERFACE_SUBCLASS_NONE
,
368 .bInterfaceProtocol
= DATA_INTERFACE_PROTOCOL_NONE
,
369 .iInterface
= STR_DATA_INTERFACE
,
374 sizeof(struct usb_endpoint_descriptor
),
375 .bDescriptorType
= USB_DT_ENDPOINT
,
376 .bEndpointAddress
= 0x02 | USB_DIR_OUT
,
378 USB_ENDPOINT_XFER_BULK
,
380 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
385 sizeof(struct usb_endpoint_descriptor
),
386 .bDescriptorType
= USB_DT_ENDPOINT
,
387 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
389 USB_ENDPOINT_XFER_BULK
,
391 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE
),
395 /* We don't add the DFU functional descriptor here since we only
396 * want to do DFU in the high-current charging mode for safety reasons */
401 static struct rs232_emu rs232_desc
={
410 * Static Generic Serial specific data
414 struct gserial_config_desc
{
416 struct usb_configuration_descriptor configuration_desc
;
417 struct usb_interface_descriptor
418 interface_desc
[NUM_GSERIAL_INTERFACES
] __attribute__((packed
));
419 struct usb_endpoint_descriptor
420 data_endpoints
[NUM_ENDPOINTS
] __attribute__((packed
));
422 } __attribute__((packed
));
424 static struct gserial_config_desc
425 gserial_configuration_descriptors
[NUM_CONFIGS
] ={
427 .configuration_desc
={
428 .bLength
= sizeof(struct usb_configuration_descriptor
),
429 .bDescriptorType
= USB_DT_CONFIG
,
431 cpu_to_le16(sizeof(struct gserial_config_desc
)),
432 .bNumInterfaces
= NUM_GSERIAL_INTERFACES
,
433 .bConfigurationValue
= 1,
434 .iConfiguration
= STR_CONFIG
,
435 .bmAttributes
= BMATTRIBUTE_RESERVED
,
436 .bMaxPower
= USBTTY_MAXPOWER
441 sizeof(struct usb_interface_descriptor
),
442 .bDescriptorType
= USB_DT_INTERFACE
,
443 .bInterfaceNumber
= 0,
444 .bAlternateSetting
= 0,
445 .bNumEndpoints
= NUM_ENDPOINTS
,
447 COMMUNICATIONS_INTERFACE_CLASS_VENDOR
,
448 .bInterfaceSubClass
=
449 COMMUNICATIONS_NO_SUBCLASS
,
450 .bInterfaceProtocol
=
451 COMMUNICATIONS_NO_PROTOCOL
,
452 .iInterface
= STR_DATA_INTERFACE
458 sizeof(struct usb_endpoint_descriptor
),
459 .bDescriptorType
= USB_DT_ENDPOINT
,
460 .bEndpointAddress
= 0x01 | USB_DIR_OUT
,
461 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
463 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE
),
468 sizeof(struct usb_endpoint_descriptor
),
469 .bDescriptorType
= USB_DT_ENDPOINT
,
470 .bEndpointAddress
= 0x02 | USB_DIR_IN
,
471 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
473 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE
),
478 sizeof(struct usb_endpoint_descriptor
),
479 .bDescriptorType
= USB_DT_ENDPOINT
,
480 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
481 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
483 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
489 .configuration_desc
={
490 .bLength
= sizeof(struct usb_configuration_descriptor
),
491 .bDescriptorType
= USB_DT_CONFIG
,
493 cpu_to_le16(sizeof(struct gserial_config_desc
)),
494 .bNumInterfaces
= NUM_GSERIAL_INTERFACES
,
495 .bConfigurationValue
= 1,
496 .iConfiguration
= STR_CONFIG
,
497 .bmAttributes
= BMATTRIBUTE_RESERVED
,
503 sizeof(struct usb_interface_descriptor
),
504 .bDescriptorType
= USB_DT_INTERFACE
,
505 .bInterfaceNumber
= 0,
506 .bAlternateSetting
= 0,
507 .bNumEndpoints
= NUM_ENDPOINTS
,
509 COMMUNICATIONS_INTERFACE_CLASS_VENDOR
,
510 .bInterfaceSubClass
=
511 COMMUNICATIONS_NO_SUBCLASS
,
512 .bInterfaceProtocol
=
513 COMMUNICATIONS_NO_PROTOCOL
,
514 .iInterface
= STR_DATA_INTERFACE
520 sizeof(struct usb_endpoint_descriptor
),
521 .bDescriptorType
= USB_DT_ENDPOINT
,
522 .bEndpointAddress
= 0x01 | USB_DIR_OUT
,
523 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
525 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE
),
530 sizeof(struct usb_endpoint_descriptor
),
531 .bDescriptorType
= USB_DT_ENDPOINT
,
532 .bEndpointAddress
= 0x02 | USB_DIR_IN
,
533 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
535 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE
),
540 sizeof(struct usb_endpoint_descriptor
),
541 .bDescriptorType
= USB_DT_ENDPOINT
,
542 .bEndpointAddress
= 0x03 | USB_DIR_IN
,
543 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
545 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE
),
553 * Static Function Prototypes
556 static void usbtty_init_strings (void);
557 static void usbtty_init_instances (void);
558 static void usbtty_init_endpoints (void);
559 static void usbtty_init_terminal_type(short type
);
560 static void usbtty_event_handler (struct usb_device_instance
*device
,
561 usb_device_event_t event
, int data
);
562 static int usbtty_cdc_setup(struct usb_device_request
*request
,
564 static int usbtty_configured (void);
565 static int write_buffer (circbuf_t
* buf
);
566 static int fill_buffer (circbuf_t
* buf
);
568 void usbtty_poll (void);
570 /* utility function for converting char* to wide string used by USB */
571 void str2wide (char *str
, u16
* wide
)
574 for (i
= 0; i
< strlen (str
) && str
[i
]; i
++){
575 #if defined(__LITTLE_ENDIAN)
576 wide
[i
] = (u16
) str
[i
];
577 #elif defined(__BIG_ENDIAN)
578 wide
[i
] = ((u16
)(str
[i
])<<8);
580 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
586 * Test whether a character is in the RX buffer
589 int usbtty_tstc (void)
591 struct usb_endpoint_instance
*endpoint
=
592 &endpoint_instance
[rx_endpoint
];
594 /* If no input data exists, allow more RX to be accepted */
595 if(usbtty_input
.size
<= 0){
596 udc_unset_nak(endpoint
->endpoint_address
&0x03);
600 return (usbtty_input
.size
> 0);
604 * Read a single byte from the usb client port. Returns 1 on success, 0
605 * otherwise. When the function is succesfull, the character read is
606 * written into its argument c.
609 int usbtty_getc (void)
612 struct usb_endpoint_instance
*endpoint
=
613 &endpoint_instance
[rx_endpoint
];
615 while (usbtty_input
.size
<= 0) {
616 udc_unset_nak(endpoint
->endpoint_address
&0x03);
620 buf_pop (&usbtty_input
, &c
, 1);
621 udc_set_nak(endpoint
->endpoint_address
&0x03);
627 * Output a single byte to the usb client port.
629 void usbtty_putc (const char c
)
631 buf_push (&usbtty_output
, &c
, 1);
632 /* If \n, also do \r */
634 buf_push (&usbtty_output
, "\r", 1);
636 /* Poll at end to handle new data... */
637 if ((usbtty_output
.size
+ 2) >= usbtty_output
.totalsize
) {
642 /* usbtty_puts() helper function for finding the next '\n' in a string */
643 static int next_nl_pos (const char *s
)
647 for (i
= 0; s
[i
] != '\0'; i
++) {
655 * Output a string to the usb client port - implementing flow control
658 static void __usbtty_puts (const char *str
, int len
)
660 int maxlen
= usbtty_output
.totalsize
;
663 /* break str into chunks < buffer size, if needed */
667 space
= maxlen
- usbtty_output
.size
;
669 /* If the USB is not configured, allow the circular buffer to
670 be overwritten. Otherwise this while() will loop forever. */
671 if (!usbtty_configured())
674 /* Empty buffer here, if needed, to ensure space... */
676 write_buffer (&usbtty_output
);
678 n
= MIN (space
, MIN (len
, maxlen
));
679 buf_push (&usbtty_output
, str
, n
);
687 void usbtty_puts (const char *str
)
690 int len
= strlen (str
);
692 /* add '\r' for each '\n' */
694 n
= next_nl_pos (str
);
696 if (str
[n
] == '\n') {
697 __usbtty_puts (str
, n
+ 1);
698 __usbtty_puts ("\r", 1);
702 /* No \n found. All done. */
703 __usbtty_puts (str
, n
);
708 /* Poll at end to handle new data... */
713 * Initialize the usb client port.
716 int drv_usbtty_init (void)
723 /* Ger seiral number */
724 if (!(sn
= getenv("serial#"))) {
728 if (snlen
> sizeof(serial_number
) - 1) {
729 printf ("Warning: serial number %s is too long (%d > %d)\n",
730 sn
, snlen
, sizeof(serial_number
) - 1);
731 snlen
= sizeof(serial_number
) - 1;
733 memcpy (serial_number
, sn
, snlen
);
734 serial_number
[snlen
] = '\0';
736 /* Decide on which type of UDC device to be.
739 if(!(tt
= getenv("usbtty"))) {
742 usbtty_init_terminal_type(strcmp(tt
,"cdc_acm"));
744 /* Decide on which type of UDC device to be.
747 if(!(tt
= getenv("usbtty"))) {
750 usbtty_init_terminal_type(strcmp(tt
,"cdc_acm"));
752 /* prepare buffers... */
753 buf_init (&usbtty_input
, USBTTY_BUFFER_SIZE
);
754 buf_init (&usbtty_output
, USBTTY_BUFFER_SIZE
);
756 /* Now, set up USB controller and infrastructure */
757 udc_init (); /* Basic USB initialization */
759 usbtty_init_strings ();
760 usbtty_init_instances ();
762 udc_startup_events (device_instance
);/* Enable dev, init udc pointers */
763 udc_connect (); /* Enable pullup for host detection */
765 usbtty_init_endpoints ();
767 /* Device initialization */
768 memset (&usbttydev
, 0, sizeof (usbttydev
));
770 strcpy (usbttydev
.name
, "usbtty");
771 usbttydev
.ext
= 0; /* No extensions */
772 usbttydev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_OUTPUT
;
773 usbttydev
.tstc
= usbtty_tstc
; /* 'tstc' function */
774 usbttydev
.getc
= usbtty_getc
; /* 'getc' function */
775 usbttydev
.putc
= usbtty_putc
; /* 'putc' function */
776 usbttydev
.puts
= usbtty_puts
; /* 'puts' function */
778 rc
= device_register (&usbttydev
);
780 return (rc
== 0) ? 1 : rc
;
783 static void usbtty_init_strings (void)
785 struct usb_string_descriptor
*string
;
787 usbtty_string_table
[STR_LANG
] =
788 (struct usb_string_descriptor
*)wstrLang
;
790 string
= (struct usb_string_descriptor
*) wstrManufacturer
;
791 string
->bLength
= sizeof(wstrManufacturer
);
792 string
->bDescriptorType
= USB_DT_STRING
;
793 str2wide (CONFIG_USBD_MANUFACTURER
, string
->wData
);
794 usbtty_string_table
[STR_MANUFACTURER
]=string
;
797 string
= (struct usb_string_descriptor
*) wstrProduct
;
798 string
->bLength
= sizeof(wstrProduct
);
799 string
->bDescriptorType
= USB_DT_STRING
;
800 str2wide (CONFIG_USBD_PRODUCT_NAME
, string
->wData
);
801 usbtty_string_table
[STR_PRODUCT
]=string
;
804 string
= (struct usb_string_descriptor
*) wstrSerial
;
805 string
->bLength
= sizeof(serial_number
);
806 string
->bDescriptorType
= USB_DT_STRING
;
807 str2wide (serial_number
, string
->wData
);
808 usbtty_string_table
[STR_SERIAL
]=string
;
811 string
= (struct usb_string_descriptor
*) wstrConfiguration
;
812 string
->bLength
= sizeof(wstrConfiguration
);
813 string
->bDescriptorType
= USB_DT_STRING
;
814 str2wide (CONFIG_USBD_CONFIGURATION_STR
, string
->wData
);
815 usbtty_string_table
[STR_CONFIG
]=string
;
818 string
= (struct usb_string_descriptor
*) wstrDataInterface
;
819 string
->bLength
= sizeof(wstrDataInterface
);
820 string
->bDescriptorType
= USB_DT_STRING
;
821 str2wide (CONFIG_USBD_DATA_INTERFACE_STR
, string
->wData
);
822 usbtty_string_table
[STR_DATA_INTERFACE
]=string
;
824 string
= (struct usb_string_descriptor
*) wstrCtrlInterface
;
825 string
->bLength
= sizeof(wstrCtrlInterface
);
826 string
->bDescriptorType
= USB_DT_STRING
;
827 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR
, string
->wData
);
828 usbtty_string_table
[STR_CTRL_INTERFACE
]=string
;
830 /* Now, initialize the string table for ep0 handling */
831 usb_strings
= usbtty_string_table
;
834 static void usbtty_init_instances (void)
838 /* initialize device instance */
839 memset (device_instance
, 0, sizeof (struct usb_device_instance
));
840 device_instance
->device_state
= STATE_INIT
;
841 device_instance
->device_descriptor
= &device_descriptor
;
842 device_instance
->event
= usbtty_event_handler
;
843 device_instance
->cdc_recv_setup
= usbtty_cdc_setup
;
844 device_instance
->bus
= bus_instance
;
845 device_instance
->configurations
= NUM_CONFIGS
;
846 device_instance
->configuration_instance_array
= config_instance
;
847 #ifdef CONFIG_USBD_DFU
848 dfu_init_instance(device_instance
);
851 /* initialize bus instance */
852 memset (bus_instance
, 0, sizeof (struct usb_bus_instance
));
853 bus_instance
->device
= device_instance
;
854 bus_instance
->endpoint_array
= endpoint_instance
;
855 bus_instance
->max_endpoints
= 1;
856 bus_instance
->maxpacketsize
= 64;
857 bus_instance
->serial_number_str
= serial_number
;
859 /* configuration instances */
860 for (i
= 0; i
< NUM_CONFIGS
; i
++) {
861 memset(&config_instance
[i
], 0, sizeof(config_instance
));
862 config_instance
[i
].interfaces
= interface_count
;
863 /* FIXME: this breaks for the non-ACM case */
864 config_instance
[i
].configuration_descriptor
= &acm_configuration_descriptors
[i
];
865 config_instance
[i
].interface_instance_array
= interface_instance
;
868 /* interface instance */
869 memset (interface_instance
, 0,
870 sizeof (struct usb_interface_instance
));
871 interface_instance
->alternates
= 1;
872 interface_instance
->alternates_instance_array
= alternate_instance
;
874 /* alternates instance */
875 memset (alternate_instance
, 0,
876 sizeof (struct usb_alternate_instance
));
877 alternate_instance
->interface_descriptor
= interface_descriptors
;
878 alternate_instance
->endpoints
= NUM_ENDPOINTS
;
879 alternate_instance
->endpoints_descriptor_array
= ep_descriptor_ptrs
;
881 /* endpoint instances */
882 memset (&endpoint_instance
[0], 0,
883 sizeof (struct usb_endpoint_instance
));
884 endpoint_instance
[0].endpoint_address
= 0;
885 endpoint_instance
[0].rcv_packetSize
= EP0_MAX_PACKET_SIZE
;
886 endpoint_instance
[0].rcv_attributes
= USB_ENDPOINT_XFER_CONTROL
;
887 endpoint_instance
[0].tx_packetSize
= EP0_MAX_PACKET_SIZE
;
888 endpoint_instance
[0].tx_attributes
= USB_ENDPOINT_XFER_CONTROL
;
889 udc_setup_ep (device_instance
, 0, &endpoint_instance
[0]);
891 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
892 memset (&endpoint_instance
[i
], 0,
893 sizeof (struct usb_endpoint_instance
));
895 endpoint_instance
[i
].endpoint_address
=
896 ep_descriptor_ptrs
[i
- 1]->bEndpointAddress
;
898 endpoint_instance
[i
].rcv_attributes
=
899 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
901 endpoint_instance
[i
].rcv_packetSize
=
902 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
904 endpoint_instance
[i
].tx_attributes
=
905 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
907 endpoint_instance
[i
].tx_packetSize
=
908 le16_to_cpu(ep_descriptor_ptrs
[i
- 1]->wMaxPacketSize
);
910 endpoint_instance
[i
].tx_attributes
=
911 ep_descriptor_ptrs
[i
- 1]->bmAttributes
;
913 urb_link_init (&endpoint_instance
[i
].rcv
);
914 urb_link_init (&endpoint_instance
[i
].rdy
);
915 urb_link_init (&endpoint_instance
[i
].tx
);
916 urb_link_init (&endpoint_instance
[i
].done
);
918 if (endpoint_instance
[i
].endpoint_address
& USB_DIR_IN
)
919 endpoint_instance
[i
].tx_urb
=
920 usbd_alloc_urb (device_instance
,
921 &endpoint_instance
[i
]);
923 endpoint_instance
[i
].rcv_urb
=
924 usbd_alloc_urb (device_instance
,
925 &endpoint_instance
[i
]);
929 static void usbtty_init_endpoints (void)
933 bus_instance
->max_endpoints
= NUM_ENDPOINTS
+ 1;
934 for (i
= 1; i
<= NUM_ENDPOINTS
; i
++) {
935 udc_setup_ep (device_instance
, i
, &endpoint_instance
[i
]);
939 /* usbtty_init_terminal_type
941 * Do some late binding for our device type.
943 static void usbtty_init_terminal_type(short type
)
948 /* Assign endpoint descriptors */
949 ep_descriptor_ptrs
[0] =
950 &acm_configuration_descriptors
[0].notification_endpoint
;
951 ep_descriptor_ptrs
[1] =
952 &acm_configuration_descriptors
[0].data_endpoints
[0];
953 ep_descriptor_ptrs
[2] =
954 &acm_configuration_descriptors
[0].data_endpoints
[1];
956 /* Enumerate Device Descriptor */
957 device_descriptor
.bDeviceClass
=
958 COMMUNICATIONS_DEVICE_CLASS
;
959 device_descriptor
.idProduct
=
960 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM
);
962 /* Assign endpoint indices */
963 tx_endpoint
= ACM_TX_ENDPOINT
;
964 rx_endpoint
= ACM_RX_ENDPOINT
;
966 /* Configuration Descriptor */
967 configuration_descriptor
=
968 (struct usb_configuration_descriptor
*)
969 &acm_configuration_descriptors
;
971 /* Interface count */
972 interface_count
= NUM_ACM_INTERFACES
;
975 /* BULK IN/OUT & Default */
978 /* Assign endpoint descriptors */
979 ep_descriptor_ptrs
[0] =
980 &gserial_configuration_descriptors
[0].data_endpoints
[0];
981 ep_descriptor_ptrs
[1] =
982 &gserial_configuration_descriptors
[0].data_endpoints
[1];
983 ep_descriptor_ptrs
[2] =
984 &gserial_configuration_descriptors
[0].data_endpoints
[2];
986 /* Enumerate Device Descriptor */
987 device_descriptor
.bDeviceClass
= 0xFF;
988 device_descriptor
.idProduct
=
989 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL
);
991 /* Assign endpoint indices */
992 tx_endpoint
= GSERIAL_TX_ENDPOINT
;
993 rx_endpoint
= GSERIAL_RX_ENDPOINT
;
995 /* Configuration Descriptor */
996 configuration_descriptor
=
997 (struct usb_configuration_descriptor
*)
998 &gserial_configuration_descriptors
;
1000 /* Interface count */
1001 interface_count
= NUM_GSERIAL_INTERFACES
;
1006 /******************************************************************************/
1008 static struct urb
*next_urb (struct usb_device_instance
*device
,
1009 struct usb_endpoint_instance
*endpoint
)
1011 struct urb
*current_urb
= NULL
;
1014 /* If there's a queue, then we should add to the last urb */
1015 if (!endpoint
->tx_queue
) {
1016 current_urb
= endpoint
->tx_urb
;
1018 /* Last urb from tx chain */
1020 p2surround (struct urb
, link
, endpoint
->tx
.prev
);
1023 /* Make sure this one has enough room */
1024 space
= current_urb
->buffer_length
- current_urb
->actual_length
;
1027 } else { /* No space here */
1028 /* First look at done list */
1029 current_urb
= first_urb_detached (&endpoint
->done
);
1031 current_urb
= usbd_alloc_urb (device
, endpoint
);
1034 urb_append (&endpoint
->tx
, current_urb
);
1035 endpoint
->tx_queue
++;
1040 static int write_buffer (circbuf_t
* buf
)
1042 if (!usbtty_configured ()) {
1046 struct usb_endpoint_instance
*endpoint
=
1047 &endpoint_instance
[tx_endpoint
];
1048 struct urb
*current_urb
= NULL
;
1049 unsigned long flags
;
1051 local_irq_save(flags
);
1052 current_urb
= next_urb (device_instance
, endpoint
);
1053 /* TX data still exists - send it now
1056 /* I don't really understand what this logic is supposed to do,
1057 * at least on the s3c24xx usb driver it causes all data to be
1058 * transmitted twice !?! - Harald Welte */
1059 #if !(defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440) || \
1060 defined(CONFIG_S3C2442) || defined(CONFIG_S3C2443))
1061 if(endpoint
->sent
< current_urb
->actual_length
){
1062 if(udc_endpoint_write (endpoint
)){
1063 /* Write pre-empted by RX */
1064 local_irq_restore(flags
);
1077 /* Break buffer into urb sized pieces,
1078 * and link each to the endpoint
1080 while (buf
->size
> 0) {
1083 TTYERR ("current_urb is NULL, buf->size %d\n",
1085 local_irq_restore(flags
);
1089 dest
= (char*)current_urb
->buffer
+
1090 current_urb
->actual_length
;
1093 current_urb
->buffer_length
-
1094 current_urb
->actual_length
;
1095 popnum
= MIN (space_avail
, buf
->size
);
1099 popped
= buf_pop (buf
, dest
, popnum
);
1102 current_urb
->actual_length
+= popped
;
1105 /* If endpoint->last == 0, then transfers have
1106 * not started on this endpoint
1108 if (endpoint
->last
== 0) {
1109 if(udc_endpoint_write (endpoint
)){
1110 /* Write pre-empted by RX */
1111 local_irq_restore(flags
);
1117 local_irq_restore(flags
);
1121 local_irq_restore(flags
);
1125 static int fill_buffer (circbuf_t
* buf
)
1127 struct usb_endpoint_instance
*endpoint
=
1128 &endpoint_instance
[rx_endpoint
];
1129 unsigned int nb
= 0;
1130 unsigned long flags
;
1132 local_irq_save(flags
);
1133 if (endpoint
->rcv_urb
&& endpoint
->rcv_urb
->actual_length
) {
1134 char *src
= (char *) endpoint
->rcv_urb
->buffer
;
1135 unsigned int rx_avail
= buf
->totalsize
- buf
->size
;
1137 if(rx_avail
>= endpoint
->rcv_urb
->actual_length
){
1139 nb
= endpoint
->rcv_urb
->actual_length
;
1140 buf_push (buf
, src
, nb
);
1141 endpoint
->rcv_urb
->actual_length
= 0;
1145 local_irq_restore(flags
);
1149 static int usbtty_configured (void)
1151 return usbtty_configured_flag
;
1154 /******************************************************************************/
1156 static void usbtty_event_handler (struct usb_device_instance
*device
,
1157 usb_device_event_t event
, int data
)
1161 case DEVICE_BUS_INACTIVE
:
1162 usbtty_configured_flag
= 0;
1164 case DEVICE_CONFIGURED
:
1166 /* we can't afford sitting here and printing stuff. This code runs
1167 * in a time-critical context and printing stuff is a synchronous
1168 * serial activity... bad things can happen to USB peripheral
1170 printf("DEVICE_CONFIGURED: %u\n", device
->configuration
);
1172 if (device
->configuration
== 1)
1173 udc_ctrl(UDC_CTRL_500mA_ENABLE
, 1);
1175 udc_ctrl(UDC_CTRL_500mA_ENABLE
, 0);
1176 usbtty_configured_flag
= 1;
1178 case DEVICE_DE_CONFIGURED
:
1180 /* we can't afford sitting here and printing stuff. This code runs
1181 * in a time-critical context and printing stuff is a synchronous
1182 * serial activity... bad things can happen to USB peripheral
1184 printf("DEVICE_DECONFIGURED: %u\n", device
->configuration
);
1186 udc_ctrl(UDC_CTRL_500mA_ENABLE
, 0);
1188 case DEVICE_ADDRESS_ASSIGNED
:
1189 usbtty_init_endpoints ();
1196 /******************************************************************************/
1198 int usbtty_cdc_setup(struct usb_device_request
*request
, struct urb
*urb
)
1200 switch (request
->bRequest
){
1202 case ACM_SET_CONTROL_LINE_STATE
: /* Implies DTE ready */
1204 case ACM_SEND_ENCAPSULATED_COMMAND
: /* Required */
1206 case ACM_SET_LINE_ENCODING
: /* DTE stop/parity bits
1209 case ACM_GET_ENCAPSULATED_RESPONSE
: /* request response */
1211 case ACM_GET_LINE_ENCODING
: /* request DTE rate,
1212 * stop/parity bits */
1213 memcpy (urb
->buffer
, &rs232_desc
, sizeof(rs232_desc
));
1214 urb
->actual_length
= sizeof(rs232_desc
);
1223 /******************************************************************************/
1226 * Since interrupt handling has not yet been implemented, we use this function
1227 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1228 * update the USB state.
1230 void usbtty_poll (void)
1232 /* New interrupts? */
1235 /* Write any output data to host buffer
1236 * (do this before checking interrupts to avoid missing one)
1238 if (usbtty_configured ()) {
1239 write_buffer (&usbtty_output
);
1242 /* New interrupts? */
1245 /* Check for new data from host..
1246 * (do this after checking interrupts to get latest data)
1248 if (usbtty_configured ()) {
1249 fill_buffer (&usbtty_input
);
1252 /* New interrupts? */