]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/serial/usbtty.c
Merge git://git.denx.de/u-boot-tegra
[people/ms/u-boot.git] / drivers / serial / usbtty.c
CommitLineData
232c150a
WD
1/*
2 * (C) Copyright 2003
3 * Gerry Hamel, geh@ti.com, Texas Instruments
386eda02 4 *
16c8d5e7
WD
5 * (C) Copyright 2006
6 * Bryan O'Donoghue, bodonoghue@codehermit.ie
232c150a 7 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
232c150a
WD
9 */
10
11#include <common.h>
dedacc18 12#include <config.h>
232c150a 13#include <circbuf.h>
52cb4d4f 14#include <stdio_dev.h>
b2fb47f1 15#include <asm/unaligned.h>
232c150a 16#include "usbtty.h"
16c8d5e7
WD
17#include "usb_cdc_acm.h"
18#include "usbdescriptors.h"
232c150a 19
dedacc18 20#ifdef DEBUG
16c8d5e7
WD
21#define TTYDBG(fmt,args...)\
22 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
232c150a
WD
23#else
24#define TTYDBG(fmt,args...) do{}while(0)
25#endif
26
16c8d5e7
WD
27#if 1
28#define TTYERR(fmt,args...)\
29 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
30 __LINE__,##args)
232c150a
WD
31#else
32#define TTYERR(fmt,args...) do{}while(0)
33#endif
34
16c8d5e7
WD
35/*
36 * Defines
37 */
38#define NUM_CONFIGS 1
39#define MAX_INTERFACES 2
40#define NUM_ENDPOINTS 3
41#define ACM_TX_ENDPOINT 3
42#define ACM_RX_ENDPOINT 2
43#define GSERIAL_TX_ENDPOINT 2
44#define GSERIAL_RX_ENDPOINT 1
45#define NUM_ACM_INTERFACES 2
46#define NUM_GSERIAL_INTERFACES 1
47#define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
48#define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
49
232c150a
WD
50/*
51 * Buffers to hold input and output data
52 */
b2caefbb 53#define USBTTY_BUFFER_SIZE 2048
232c150a
WD
54static circbuf_t usbtty_input;
55static circbuf_t usbtty_output;
56
57
58/*
59 * Instance variables
60 */
52cb4d4f 61static struct stdio_dev usbttydev;
16c8d5e7
WD
62static struct usb_device_instance device_instance[1];
63static struct usb_bus_instance bus_instance[1];
232c150a 64static struct usb_configuration_instance config_instance[NUM_CONFIGS];
16c8d5e7
WD
65static struct usb_interface_instance interface_instance[MAX_INTERFACES];
66static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
67/* one extra for control endpoint */
68static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
232c150a
WD
69
70/*
71 * Global flag
72 */
73int usbtty_configured_flag = 0;
74
6629d2f2
WD
75/*
76 * Serial number
77 */
78static char serial_number[16];
79
16c8d5e7 80
232c150a 81/*
16c8d5e7 82 * Descriptors, Strings, Local variables.
232c150a 83 */
16c8d5e7 84
2731b9a8 85/* defined and used by gadget/ep0.c */
16c8d5e7
WD
86extern struct usb_string_descriptor **usb_strings;
87
88/* Indicies, References */
89static unsigned short rx_endpoint = 0;
90static unsigned short tx_endpoint = 0;
91static unsigned short interface_count = 0;
92static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
93
94/* USB Descriptor Strings */
232c150a
WD
95static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
96static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
97static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
6629d2f2 98static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
232c150a 99static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
16c8d5e7
WD
100static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
101static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
232c150a 102
16c8d5e7
WD
103/* Standard USB Data Structures */
104static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
105static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
106static struct usb_configuration_descriptor *configuration_descriptor = 0;
232c150a 107static struct usb_device_descriptor device_descriptor = {
16c8d5e7
WD
108 .bLength = sizeof(struct usb_device_descriptor),
109 .bDescriptorType = USB_DT_DEVICE,
53677ef1 110 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
16c8d5e7
WD
111 .bDeviceSubClass = 0x00,
112 .bDeviceProtocol = 0x00,
113 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
114 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
115 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
116 .iManufacturer = STR_MANUFACTURER,
117 .iProduct = STR_PRODUCT,
118 .iSerialNumber = STR_SERIAL,
119 .bNumConfigurations = NUM_CONFIGS
232c150a 120};
16c8d5e7
WD
121
122
f9da0f89
VK
123#if defined(CONFIG_USBD_HS)
124static struct usb_qualifier_descriptor qualifier_descriptor = {
125 .bLength = sizeof(struct usb_qualifier_descriptor),
126 .bDescriptorType = USB_DT_QUAL,
127 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
128 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
129 .bDeviceSubClass = 0x00,
130 .bDeviceProtocol = 0x00,
131 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
132 .bNumConfigurations = NUM_CONFIGS
133};
134#endif
135
16c8d5e7
WD
136/*
137 * Static CDC ACM specific descriptors
138 */
139
140struct acm_config_desc {
141 struct usb_configuration_descriptor configuration_desc;
386eda02 142
16c8d5e7
WD
143 /* Master Interface */
144 struct usb_interface_descriptor interface_desc;
386eda02 145
16c8d5e7
WD
146 struct usb_class_header_function_descriptor usb_class_header;
147 struct usb_class_call_management_descriptor usb_class_call_mgt;
148 struct usb_class_abstract_control_descriptor usb_class_acm;
149 struct usb_class_union_function_descriptor usb_class_union;
150 struct usb_endpoint_descriptor notification_endpoint;
151
152 /* Slave Interface */
153 struct usb_interface_descriptor data_class_interface;
f3c0de63 154 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
16c8d5e7
WD
155} __attribute__((packed));
156
157static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
158 {
159 .configuration_desc ={
386eda02 160 .bLength =
16c8d5e7 161 sizeof(struct usb_configuration_descriptor),
53677ef1 162 .bDescriptorType = USB_DT_CONFIG,
386eda02 163 .wTotalLength =
16c8d5e7 164 cpu_to_le16(sizeof(struct acm_config_desc)),
53677ef1
WD
165 .bNumInterfaces = NUM_ACM_INTERFACES,
166 .bConfigurationValue = 1,
16c8d5e7 167 .iConfiguration = STR_CONFIG,
386eda02 168 .bmAttributes =
16c8d5e7
WD
169 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
170 .bMaxPower = USBTTY_MAXPOWER
171 },
172 /* Interface 1 */
173 .interface_desc = {
174 .bLength = sizeof(struct usb_interface_descriptor),
175 .bDescriptorType = USB_DT_INTERFACE,
176 .bInterfaceNumber = 0,
177 .bAlternateSetting = 0,
178 .bNumEndpoints = 0x01,
386eda02 179 .bInterfaceClass =
16c8d5e7
WD
180 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
181 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
182 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
183 .iInterface = STR_CTRL_INTERFACE,
184 },
185 .usb_class_header = {
386eda02 186 .bFunctionLength =
16c8d5e7 187 sizeof(struct usb_class_header_function_descriptor),
386eda02 188 .bDescriptorType = CS_INTERFACE,
16c8d5e7
WD
189 .bDescriptorSubtype = USB_ST_HEADER,
190 .bcdCDC = cpu_to_le16(110),
191 },
192 .usb_class_call_mgt = {
386eda02 193 .bFunctionLength =
16c8d5e7
WD
194 sizeof(struct usb_class_call_management_descriptor),
195 .bDescriptorType = CS_INTERFACE,
196 .bDescriptorSubtype = USB_ST_CMF,
386eda02
WD
197 .bmCapabilities = 0x00,
198 .bDataInterface = 0x01,
16c8d5e7
WD
199 },
200 .usb_class_acm = {
386eda02 201 .bFunctionLength =
16c8d5e7
WD
202 sizeof(struct usb_class_abstract_control_descriptor),
203 .bDescriptorType = CS_INTERFACE,
386eda02
WD
204 .bDescriptorSubtype = USB_ST_ACMF,
205 .bmCapabilities = 0x00,
16c8d5e7
WD
206 },
207 .usb_class_union = {
386eda02 208 .bFunctionLength =
16c8d5e7
WD
209 sizeof(struct usb_class_union_function_descriptor),
210 .bDescriptorType = CS_INTERFACE,
211 .bDescriptorSubtype = USB_ST_UF,
386eda02
WD
212 .bMasterInterface = 0x00,
213 .bSlaveInterface0 = 0x01,
16c8d5e7
WD
214 },
215 .notification_endpoint = {
386eda02 216 .bLength =
16c8d5e7
WD
217 sizeof(struct usb_endpoint_descriptor),
218 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 219 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
16c8d5e7 220 .bmAttributes = USB_ENDPOINT_XFER_INT,
386eda02 221 .wMaxPacketSize
16c8d5e7
WD
222 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
223 .bInterval = 0xFF,
224 },
225
226 /* Interface 2 */
227 .data_class_interface = {
386eda02 228 .bLength =
16c8d5e7
WD
229 sizeof(struct usb_interface_descriptor),
230 .bDescriptorType = USB_DT_INTERFACE,
231 .bInterfaceNumber = 0x01,
232 .bAlternateSetting = 0x00,
233 .bNumEndpoints = 0x02,
386eda02 234 .bInterfaceClass =
16c8d5e7
WD
235 COMMUNICATIONS_INTERFACE_CLASS_DATA,
236 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
237 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
238 .iInterface = STR_DATA_INTERFACE,
239 },
240 .data_endpoints = {
241 {
386eda02 242 .bLength =
16c8d5e7
WD
243 sizeof(struct usb_endpoint_descriptor),
244 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 245 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
386eda02 246 .bmAttributes =
16c8d5e7 247 USB_ENDPOINT_XFER_BULK,
386eda02 248 .wMaxPacketSize =
16c8d5e7
WD
249 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
250 .bInterval = 0xFF,
251 },
252 {
386eda02 253 .bLength =
16c8d5e7
WD
254 sizeof(struct usb_endpoint_descriptor),
255 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 256 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
386eda02 257 .bmAttributes =
16c8d5e7 258 USB_ENDPOINT_XFER_BULK,
386eda02 259 .wMaxPacketSize =
16c8d5e7
WD
260 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
261 .bInterval = 0xFF,
262 },
263 },
264 },
386eda02 265};
16c8d5e7
WD
266
267static struct rs232_emu rs232_desc={
53677ef1
WD
268 .dter = 115200,
269 .stop_bits = 0x00,
270 .parity = 0x00,
16c8d5e7 271 .data_bits = 0x08
232c150a
WD
272};
273
232c150a 274
16c8d5e7
WD
275/*
276 * Static Generic Serial specific data
277 */
278
279
280struct gserial_config_desc {
386eda02 281
16c8d5e7 282 struct usb_configuration_descriptor configuration_desc;
f3c0de63
AM
283 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
284 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
16c8d5e7
WD
285
286} __attribute__((packed));
287
386eda02 288static struct gserial_config_desc
16c8d5e7
WD
289gserial_configuration_descriptors[NUM_CONFIGS] ={
290 {
291 .configuration_desc ={
292 .bLength = sizeof(struct usb_configuration_descriptor),
293 .bDescriptorType = USB_DT_CONFIG,
386eda02 294 .wTotalLength =
16c8d5e7
WD
295 cpu_to_le16(sizeof(struct gserial_config_desc)),
296 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
297 .bConfigurationValue = 1,
298 .iConfiguration = STR_CONFIG,
386eda02 299 .bmAttributes =
16c8d5e7
WD
300 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
301 .bMaxPower = USBTTY_MAXPOWER
302 },
303 .interface_desc = {
304 {
386eda02 305 .bLength =
16c8d5e7
WD
306 sizeof(struct usb_interface_descriptor),
307 .bDescriptorType = USB_DT_INTERFACE,
308 .bInterfaceNumber = 0,
309 .bAlternateSetting = 0,
310 .bNumEndpoints = NUM_ENDPOINTS,
386eda02 311 .bInterfaceClass =
16c8d5e7 312 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
386eda02 313 .bInterfaceSubClass =
16c8d5e7 314 COMMUNICATIONS_NO_SUBCLASS,
386eda02 315 .bInterfaceProtocol =
16c8d5e7
WD
316 COMMUNICATIONS_NO_PROTOCOL,
317 .iInterface = STR_DATA_INTERFACE
318 },
53677ef1 319 },
16c8d5e7
WD
320 .data_endpoints = {
321 {
386eda02 322 .bLength =
16c8d5e7
WD
323 sizeof(struct usb_endpoint_descriptor),
324 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 325 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
16c8d5e7 326 .bmAttributes = USB_ENDPOINT_XFER_BULK,
386eda02 327 .wMaxPacketSize =
16c8d5e7
WD
328 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
329 .bInterval= 0xFF,
330 },
331 {
386eda02 332 .bLength =
16c8d5e7
WD
333 sizeof(struct usb_endpoint_descriptor),
334 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 335 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
16c8d5e7 336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
386eda02 337 .wMaxPacketSize =
16c8d5e7 338 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
53677ef1 339 .bInterval = 0xFF,
16c8d5e7
WD
340 },
341 {
386eda02 342 .bLength =
16c8d5e7
WD
343 sizeof(struct usb_endpoint_descriptor),
344 .bDescriptorType = USB_DT_ENDPOINT,
9e78dae2 345 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
16c8d5e7 346 .bmAttributes = USB_ENDPOINT_XFER_INT,
53677ef1 347 .wMaxPacketSize =
16c8d5e7
WD
348 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
349 .bInterval = 0xFF,
350 },
351 },
352 },
353};
232c150a
WD
354
355/*
16c8d5e7 356 * Static Function Prototypes
232c150a 357 */
16c8d5e7 358
232c150a
WD
359static void usbtty_init_strings (void);
360static void usbtty_init_instances (void);
361static void usbtty_init_endpoints (void);
16c8d5e7 362static void usbtty_init_terminal_type(short type);
232c150a 363static void usbtty_event_handler (struct usb_device_instance *device,
16c8d5e7 364 usb_device_event_t event, int data);
386eda02 365static int usbtty_cdc_setup(struct usb_device_request *request,
16c8d5e7 366 struct urb *urb);
232c150a 367static int usbtty_configured (void);
232c150a
WD
368static int write_buffer (circbuf_t * buf);
369static int fill_buffer (circbuf_t * buf);
370
371void usbtty_poll (void);
232c150a 372
16c8d5e7
WD
373/* utility function for converting char* to wide string used by USB */
374static void str2wide (char *str, u16 * wide)
375{
376 int i;
377 for (i = 0; i < strlen (str) && str[i]; i++){
18135125 378 #if defined(__LITTLE_ENDIAN)
16c8d5e7 379 wide[i] = (u16) str[i];
18135125 380 #elif defined(__BIG_ENDIAN)
16c8d5e7
WD
381 wide[i] = ((u16)(str[i])<<8);
382 #else
18135125 383 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
16c8d5e7
WD
384 #endif
385 }
386}
232c150a
WD
387
388/*
389 * Test whether a character is in the RX buffer
390 */
16c8d5e7 391
709ea543 392int usbtty_tstc(struct stdio_dev *dev)
232c150a 393{
16c8d5e7
WD
394 struct usb_endpoint_instance *endpoint =
395 &endpoint_instance[rx_endpoint];
396
397 /* If no input data exists, allow more RX to be accepted */
398 if(usbtty_input.size <= 0){
399 udc_unset_nak(endpoint->endpoint_address&0x03);
400 }
401
232c150a
WD
402 usbtty_poll ();
403 return (usbtty_input.size > 0);
404}
405
406/*
407 * Read a single byte from the usb client port. Returns 1 on success, 0
408 * otherwise. When the function is succesfull, the character read is
409 * written into its argument c.
410 */
16c8d5e7 411
709ea543 412int usbtty_getc(struct stdio_dev *dev)
232c150a
WD
413{
414 char c;
16c8d5e7
WD
415 struct usb_endpoint_instance *endpoint =
416 &endpoint_instance[rx_endpoint];
232c150a
WD
417
418 while (usbtty_input.size <= 0) {
16c8d5e7 419 udc_unset_nak(endpoint->endpoint_address&0x03);
232c150a
WD
420 usbtty_poll ();
421 }
422
423 buf_pop (&usbtty_input, &c, 1);
16c8d5e7
WD
424 udc_set_nak(endpoint->endpoint_address&0x03);
425
232c150a
WD
426 return c;
427}
428
429/*
430 * Output a single byte to the usb client port.
431 */
709ea543 432void usbtty_putc(struct stdio_dev *dev, const char c)
232c150a 433{
f3c0de63
AM
434 if (!usbtty_configured ())
435 return;
436
232c150a
WD
437 /* If \n, also do \r */
438 if (c == '\n')
439 buf_push (&usbtty_output, "\r", 1);
440
055457ef
AW
441 buf_push(&usbtty_output, &c, 1);
442
232c150a
WD
443 /* Poll at end to handle new data... */
444 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
445 usbtty_poll ();
446 }
447}
448
232c150a
WD
449/* usbtty_puts() helper function for finding the next '\n' in a string */
450static int next_nl_pos (const char *s)
451{
452 int i;
453
454 for (i = 0; s[i] != '\0'; i++) {
455 if (s[i] == '\n')
456 return i;
457 }
458 return i;
459}
460
461/*
16c8d5e7 462 * Output a string to the usb client port - implementing flow control
232c150a 463 */
16c8d5e7 464
232c150a
WD
465static void __usbtty_puts (const char *str, int len)
466{
467 int maxlen = usbtty_output.totalsize;
468 int space, n;
469
470 /* break str into chunks < buffer size, if needed */
471 while (len > 0) {
16c8d5e7 472 usbtty_poll ();
232c150a 473
16c8d5e7 474 space = maxlen - usbtty_output.size;
232c150a 475 /* Empty buffer here, if needed, to ensure space... */
16c8d5e7 476 if (space) {
232c150a 477 write_buffer (&usbtty_output);
386eda02 478
c79cba37 479 n = min(space, min(len, maxlen));
16c8d5e7 480 buf_push (&usbtty_output, str, n);
232c150a 481
16c8d5e7 482 str += n;
386eda02 483 len -= n;
16c8d5e7 484 }
232c150a
WD
485 }
486}
487
709ea543 488void usbtty_puts(struct stdio_dev *dev, const char *str)
232c150a
WD
489{
490 int n;
f3c0de63
AM
491 int len;
492
493 if (!usbtty_configured ())
494 return;
232c150a 495
f3c0de63 496 len = strlen (str);
232c150a
WD
497 /* add '\r' for each '\n' */
498 while (len > 0) {
499 n = next_nl_pos (str);
500
501 if (str[n] == '\n') {
055457ef
AW
502 __usbtty_puts("\r", 1);
503 __usbtty_puts(str, n + 1);
232c150a
WD
504 str += (n + 1);
505 len -= (n + 1);
506 } else {
507 /* No \n found. All done. */
508 __usbtty_puts (str, n);
509 break;
510 }
511 }
512
513 /* Poll at end to handle new data... */
514 usbtty_poll ();
515}
516
517/*
518 * Initialize the usb client port.
519 *
520 */
521int drv_usbtty_init (void)
522{
523 int rc;
6629d2f2 524 char * sn;
16c8d5e7 525 char * tt;
6629d2f2 526 int snlen;
42dfe7a1 527
16c8d5e7 528 /* Ger seiral number */
00caae6d
SG
529 sn = env_get("serial#");
530 if (!sn)
6629d2f2 531 sn = "000000000000";
6629d2f2
WD
532 snlen = strlen(sn);
533 if (snlen > sizeof(serial_number) - 1) {
b64f190b
WD
534 printf ("Warning: serial number %s is too long (%d > %lu)\n",
535 sn, snlen, (ulong)(sizeof(serial_number) - 1));
6629d2f2
WD
536 snlen = sizeof(serial_number) - 1;
537 }
538 memcpy (serial_number, sn, snlen);
539 serial_number[snlen] = '\0';
232c150a 540
16c8d5e7
WD
541 /* Decide on which type of UDC device to be.
542 */
00caae6d
SG
543 tt = env_get("usbtty");
544 if (!tt)
16c8d5e7 545 tt = "generic";
16c8d5e7 546 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
386eda02 547
232c150a
WD
548 /* prepare buffers... */
549 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
550 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
551
552 /* Now, set up USB controller and infrastructure */
553 udc_init (); /* Basic USB initialization */
554
555 usbtty_init_strings ();
556 usbtty_init_instances ();
557
241d9a61
SH
558 usbtty_init_endpoints ();
559
16c8d5e7 560 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
232c150a
WD
561 udc_connect (); /* Enable pullup for host detection */
562
232c150a
WD
563 /* Device initialization */
564 memset (&usbttydev, 0, sizeof (usbttydev));
565
566 strcpy (usbttydev.name, "usbtty");
567 usbttydev.ext = 0; /* No extensions */
568 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
569 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
570 usbttydev.getc = usbtty_getc; /* 'getc' function */
571 usbttydev.putc = usbtty_putc; /* 'putc' function */
572 usbttydev.puts = usbtty_puts; /* 'puts' function */
573
52cb4d4f 574 rc = stdio_register (&usbttydev);
232c150a
WD
575
576 return (rc == 0) ? 1 : rc;
577}
578
579static void usbtty_init_strings (void)
580{
581 struct usb_string_descriptor *string;
582
386eda02 583 usbtty_string_table[STR_LANG] =
16c8d5e7
WD
584 (struct usb_string_descriptor*)wstrLang;
585
232c150a 586 string = (struct usb_string_descriptor *) wstrManufacturer;
16c8d5e7 587 string->bLength = sizeof(wstrManufacturer);
232c150a
WD
588 string->bDescriptorType = USB_DT_STRING;
589 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
16c8d5e7
WD
590 usbtty_string_table[STR_MANUFACTURER]=string;
591
232c150a
WD
592
593 string = (struct usb_string_descriptor *) wstrProduct;
16c8d5e7 594 string->bLength = sizeof(wstrProduct);
232c150a
WD
595 string->bDescriptorType = USB_DT_STRING;
596 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
16c8d5e7
WD
597 usbtty_string_table[STR_PRODUCT]=string;
598
232c150a
WD
599
600 string = (struct usb_string_descriptor *) wstrSerial;
16c8d5e7 601 string->bLength = sizeof(serial_number);
232c150a 602 string->bDescriptorType = USB_DT_STRING;
6629d2f2 603 str2wide (serial_number, string->wData);
16c8d5e7
WD
604 usbtty_string_table[STR_SERIAL]=string;
605
232c150a
WD
606
607 string = (struct usb_string_descriptor *) wstrConfiguration;
16c8d5e7 608 string->bLength = sizeof(wstrConfiguration);
232c150a
WD
609 string->bDescriptorType = USB_DT_STRING;
610 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
16c8d5e7
WD
611 usbtty_string_table[STR_CONFIG]=string;
612
613
614 string = (struct usb_string_descriptor *) wstrDataInterface;
615 string->bLength = sizeof(wstrDataInterface);
616 string->bDescriptorType = USB_DT_STRING;
617 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
618 usbtty_string_table[STR_DATA_INTERFACE]=string;
232c150a 619
16c8d5e7
WD
620 string = (struct usb_string_descriptor *) wstrCtrlInterface;
621 string->bLength = sizeof(wstrCtrlInterface);
232c150a 622 string->bDescriptorType = USB_DT_STRING;
16c8d5e7
WD
623 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
624 usbtty_string_table[STR_CTRL_INTERFACE]=string;
232c150a
WD
625
626 /* Now, initialize the string table for ep0 handling */
627 usb_strings = usbtty_string_table;
386eda02 628}
232c150a 629
b2fb47f1
TR
630#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
631 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
632
232c150a
WD
633static void usbtty_init_instances (void)
634{
635 int i;
636
637 /* initialize device instance */
638 memset (device_instance, 0, sizeof (struct usb_device_instance));
639 device_instance->device_state = STATE_INIT;
640 device_instance->device_descriptor = &device_descriptor;
f9da0f89
VK
641#if defined(CONFIG_USBD_HS)
642 device_instance->qualifier_descriptor = &qualifier_descriptor;
643#endif
232c150a 644 device_instance->event = usbtty_event_handler;
16c8d5e7 645 device_instance->cdc_recv_setup = usbtty_cdc_setup;
232c150a
WD
646 device_instance->bus = bus_instance;
647 device_instance->configurations = NUM_CONFIGS;
648 device_instance->configuration_instance_array = config_instance;
649
650 /* initialize bus instance */
651 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
652 bus_instance->device = device_instance;
653 bus_instance->endpoint_array = endpoint_instance;
654 bus_instance->max_endpoints = 1;
655 bus_instance->maxpacketsize = 64;
6629d2f2 656 bus_instance->serial_number_str = serial_number;
232c150a
WD
657
658 /* configuration instance */
659 memset (config_instance, 0,
660 sizeof (struct usb_configuration_instance));
16c8d5e7
WD
661 config_instance->interfaces = interface_count;
662 config_instance->configuration_descriptor = configuration_descriptor;
232c150a
WD
663 config_instance->interface_instance_array = interface_instance;
664
665 /* interface instance */
666 memset (interface_instance, 0,
667 sizeof (struct usb_interface_instance));
668 interface_instance->alternates = 1;
669 interface_instance->alternates_instance_array = alternate_instance;
670
671 /* alternates instance */
672 memset (alternate_instance, 0,
673 sizeof (struct usb_alternate_instance));
674 alternate_instance->interface_descriptor = interface_descriptors;
675 alternate_instance->endpoints = NUM_ENDPOINTS;
676 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
677
678 /* endpoint instances */
679 memset (&endpoint_instance[0], 0,
680 sizeof (struct usb_endpoint_instance));
681 endpoint_instance[0].endpoint_address = 0;
682 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
683 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
684 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
685 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
686 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
687
688 for (i = 1; i <= NUM_ENDPOINTS; i++) {
689 memset (&endpoint_instance[i], 0,
690 sizeof (struct usb_endpoint_instance));
691
692 endpoint_instance[i].endpoint_address =
16c8d5e7 693 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
232c150a 694
232c150a 695 endpoint_instance[i].rcv_attributes =
16c8d5e7
WD
696 ep_descriptor_ptrs[i - 1]->bmAttributes;
697
b2fb47f1 698 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
386eda02 699
16c8d5e7
WD
700 endpoint_instance[i].tx_attributes =
701 ep_descriptor_ptrs[i - 1]->bmAttributes;
232c150a 702
b2fb47f1 703 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
16c8d5e7 704
232c150a 705 endpoint_instance[i].tx_attributes =
16c8d5e7 706 ep_descriptor_ptrs[i - 1]->bmAttributes;
232c150a
WD
707
708 urb_link_init (&endpoint_instance[i].rcv);
709 urb_link_init (&endpoint_instance[i].rdy);
710 urb_link_init (&endpoint_instance[i].tx);
711 urb_link_init (&endpoint_instance[i].done);
712
713 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
714 endpoint_instance[i].tx_urb =
715 usbd_alloc_urb (device_instance,
716 &endpoint_instance[i]);
717 else
718 endpoint_instance[i].rcv_urb =
719 usbd_alloc_urb (device_instance,
720 &endpoint_instance[i]);
721 }
722}
723
724static void usbtty_init_endpoints (void)
725{
726 int i;
727
728 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
386eda02 729 for (i = 1; i <= NUM_ENDPOINTS; i++) {
232c150a
WD
730 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
731 }
732}
733
16c8d5e7 734/* usbtty_init_terminal_type
386eda02 735 *
16c8d5e7
WD
736 * Do some late binding for our device type.
737 */
738static void usbtty_init_terminal_type(short type)
739{
740 switch(type){
386eda02 741 /* CDC ACM */
16c8d5e7
WD
742 case 0:
743 /* Assign endpoint descriptors */
386eda02 744 ep_descriptor_ptrs[0] =
16c8d5e7 745 &acm_configuration_descriptors[0].notification_endpoint;
386eda02 746 ep_descriptor_ptrs[1] =
16c8d5e7 747 &acm_configuration_descriptors[0].data_endpoints[0];
386eda02 748 ep_descriptor_ptrs[2] =
16c8d5e7
WD
749 &acm_configuration_descriptors[0].data_endpoints[1];
750
751 /* Enumerate Device Descriptor */
386eda02 752 device_descriptor.bDeviceClass =
16c8d5e7
WD
753 COMMUNICATIONS_DEVICE_CLASS;
754 device_descriptor.idProduct =
755 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
756
f9da0f89
VK
757#if defined(CONFIG_USBD_HS)
758 qualifier_descriptor.bDeviceClass =
759 COMMUNICATIONS_DEVICE_CLASS;
760#endif
16c8d5e7
WD
761 /* Assign endpoint indices */
762 tx_endpoint = ACM_TX_ENDPOINT;
763 rx_endpoint = ACM_RX_ENDPOINT;
386eda02 764
16c8d5e7
WD
765 /* Configuration Descriptor */
766 configuration_descriptor =
767 (struct usb_configuration_descriptor*)
768 &acm_configuration_descriptors;
769
770 /* Interface count */
771 interface_count = NUM_ACM_INTERFACES;
772 break;
232c150a 773
16c8d5e7
WD
774 /* BULK IN/OUT & Default */
775 case 1:
776 default:
777 /* Assign endpoint descriptors */
386eda02 778 ep_descriptor_ptrs[0] =
16c8d5e7 779 &gserial_configuration_descriptors[0].data_endpoints[0];
386eda02 780 ep_descriptor_ptrs[1] =
16c8d5e7 781 &gserial_configuration_descriptors[0].data_endpoints[1];
386eda02 782 ep_descriptor_ptrs[2] =
16c8d5e7
WD
783 &gserial_configuration_descriptors[0].data_endpoints[2];
784
785 /* Enumerate Device Descriptor */
786 device_descriptor.bDeviceClass = 0xFF;
787 device_descriptor.idProduct =
788 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
f9da0f89
VK
789#if defined(CONFIG_USBD_HS)
790 qualifier_descriptor.bDeviceClass = 0xFF;
791#endif
16c8d5e7
WD
792 /* Assign endpoint indices */
793 tx_endpoint = GSERIAL_TX_ENDPOINT;
794 rx_endpoint = GSERIAL_RX_ENDPOINT;
795
796 /* Configuration Descriptor */
386eda02 797 configuration_descriptor =
16c8d5e7
WD
798 (struct usb_configuration_descriptor*)
799 &gserial_configuration_descriptors;
800
801 /* Interface count */
802 interface_count = NUM_GSERIAL_INTERFACES;
803 break;
804 }
805}
806
807/******************************************************************************/
232c150a
WD
808
809static struct urb *next_urb (struct usb_device_instance *device,
810 struct usb_endpoint_instance *endpoint)
811{
812 struct urb *current_urb = NULL;
813 int space;
814
815 /* If there's a queue, then we should add to the last urb */
816 if (!endpoint->tx_queue) {
817 current_urb = endpoint->tx_urb;
818 } else {
819 /* Last urb from tx chain */
820 current_urb =
821 p2surround (struct urb, link, endpoint->tx.prev);
822 }
823
824 /* Make sure this one has enough room */
825 space = current_urb->buffer_length - current_urb->actual_length;
826 if (space > 0) {
827 return current_urb;
828 } else { /* No space here */
829 /* First look at done list */
830 current_urb = first_urb_detached (&endpoint->done);
831 if (!current_urb) {
832 current_urb = usbd_alloc_urb (device, endpoint);
833 }
834
835 urb_append (&endpoint->tx, current_urb);
836 endpoint->tx_queue++;
837 }
838 return current_urb;
839}
840
841static int write_buffer (circbuf_t * buf)
842{
843 if (!usbtty_configured ()) {
844 return 0;
845 }
386eda02 846
16c8d5e7
WD
847 struct usb_endpoint_instance *endpoint =
848 &endpoint_instance[tx_endpoint];
849 struct urb *current_urb = NULL;
232c150a 850
16c8d5e7 851 current_urb = next_urb (device_instance, endpoint);
73be5b3f 852
853 if (!current_urb) {
854 TTYERR ("current_urb is NULL, buf->size %d\n",
855 buf->size);
856 return 0;
857 }
858
386eda02
WD
859 /* TX data still exists - send it now
860 */
16c8d5e7
WD
861 if(endpoint->sent < current_urb->actual_length){
862 if(udc_endpoint_write (endpoint)){
863 /* Write pre-empted by RX */
864 return -1;
865 }
866 }
232c150a 867
16c8d5e7 868 if (buf->size) {
232c150a
WD
869 char *dest;
870
871 int space_avail;
872 int popnum, popped;
873 int total = 0;
874
386eda02
WD
875 /* Break buffer into urb sized pieces,
876 * and link each to the endpoint
16c8d5e7 877 */
232c150a 878 while (buf->size > 0) {
386eda02 879
16c8d5e7 880 dest = (char*)current_urb->buffer +
232c150a
WD
881 current_urb->actual_length;
882
883 space_avail =
884 current_urb->buffer_length -
885 current_urb->actual_length;
b4141195 886 popnum = min(space_avail, (int)buf->size);
232c150a
WD
887 if (popnum == 0)
888 break;
889
890 popped = buf_pop (buf, dest, popnum);
891 if (popped == 0)
892 break;
893 current_urb->actual_length += popped;
894 total += popped;
895
386eda02
WD
896 /* If endpoint->last == 0, then transfers have
897 * not started on this endpoint
16c8d5e7 898 */
232c150a 899 if (endpoint->last == 0) {
16c8d5e7
WD
900 if(udc_endpoint_write (endpoint)){
901 /* Write pre-empted by RX */
902 return -1;
903 }
232c150a
WD
904 }
905
16c8d5e7 906 }/* end while */
232c150a 907 return total;
16c8d5e7 908 }
232c150a
WD
909
910 return 0;
911}
912
913static int fill_buffer (circbuf_t * buf)
914{
915 struct usb_endpoint_instance *endpoint =
16c8d5e7 916 &endpoint_instance[rx_endpoint];
232c150a
WD
917
918 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
386eda02 919 unsigned int nb = 0;
232c150a 920 char *src = (char *) endpoint->rcv_urb->buffer;
16c8d5e7 921 unsigned int rx_avail = buf->totalsize - buf->size;
232c150a 922
16c8d5e7 923 if(rx_avail >= endpoint->rcv_urb->actual_length){
232c150a 924
16c8d5e7
WD
925 nb = endpoint->rcv_urb->actual_length;
926 buf_push (buf, src, nb);
927 endpoint->rcv_urb->actual_length = 0;
386eda02 928
16c8d5e7 929 }
232c150a
WD
930 return nb;
931 }
232c150a
WD
932 return 0;
933}
934
935static int usbtty_configured (void)
936{
937 return usbtty_configured_flag;
938}
939
16c8d5e7 940/******************************************************************************/
232c150a
WD
941
942static void usbtty_event_handler (struct usb_device_instance *device,
943 usb_device_event_t event, int data)
944{
f9da0f89
VK
945#if defined(CONFIG_USBD_HS)
946 int i;
947#endif
232c150a
WD
948 switch (event) {
949 case DEVICE_RESET:
950 case DEVICE_BUS_INACTIVE:
951 usbtty_configured_flag = 0;
952 break;
953 case DEVICE_CONFIGURED:
954 usbtty_configured_flag = 1;
955 break;
956
957 case DEVICE_ADDRESS_ASSIGNED:
f9da0f89
VK
958#if defined(CONFIG_USBD_HS)
959 /*
960 * is_usbd_high_speed routine needs to be defined by
961 * specific gadget driver
472d5460
YS
962 * It returns true if device enumerates at High speed
963 * Retuns false otherwise
f9da0f89
VK
964 */
965 for (i = 0; i < NUM_ENDPOINTS; i++) {
966 if (((ep_descriptor_ptrs[i]->bmAttributes &
967 USB_ENDPOINT_XFERTYPE_MASK) ==
968 USB_ENDPOINT_XFER_BULK)
969 && is_usbd_high_speed()) {
970
971 ep_descriptor_ptrs[i]->wMaxPacketSize =
972 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
973 }
974
975 endpoint_instance[i + 1].tx_packetSize =
976 ep_descriptor_ptrs[i]->wMaxPacketSize;
977 endpoint_instance[i + 1].rcv_packetSize =
978 ep_descriptor_ptrs[i]->wMaxPacketSize;
979 }
980#endif
232c150a
WD
981 usbtty_init_endpoints ();
982
983 default:
984 break;
985 }
986}
987
16c8d5e7
WD
988/******************************************************************************/
989
990int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
991{
992 switch (request->bRequest){
232c150a 993
53677ef1 994 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
16c8d5e7 995 break;
53677ef1 996 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
16c8d5e7
WD
997 break;
998 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
386eda02 999 * per character */
16c8d5e7 1000 break;
53677ef1 1001 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
16c8d5e7
WD
1002 break;
1003 case ACM_GET_LINE_ENCODING : /* request DTE rate,
1004 * stop/parity bits */
1005 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1006 urb->actual_length = sizeof(rs232_desc);
1007
1008 break;
53677ef1 1009 default:
16c8d5e7
WD
1010 return 1;
1011 }
1012 return 0;
1013}
1014
1015/******************************************************************************/
232c150a
WD
1016
1017/*
1018 * Since interrupt handling has not yet been implemented, we use this function
1019 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1020 * update the USB state.
1021 */
1022void usbtty_poll (void)
1023{
1024 /* New interrupts? */
16c8d5e7 1025 udc_irq();
232c150a 1026
386eda02
WD
1027 /* Write any output data to host buffer
1028 * (do this before checking interrupts to avoid missing one)
16c8d5e7 1029 */
232c150a
WD
1030 if (usbtty_configured ()) {
1031 write_buffer (&usbtty_output);
1032 }
1033
1034 /* New interrupts? */
16c8d5e7 1035 udc_irq();
386eda02
WD
1036
1037 /* Check for new data from host..
1038 * (do this after checking interrupts to get latest data)
16c8d5e7 1039 */
232c150a
WD
1040 if (usbtty_configured ()) {
1041 fill_buffer (&usbtty_input);
1042 }
1043
1044 /* New interrupts? */
16c8d5e7 1045 udc_irq();
232c150a 1046
232c150a 1047}