blob: 2f89e2b7411c307cf27a5876ed6fb49b786e3d86 [file] [log] [blame]
wdenk232c1502004-03-12 00:14:09 +00001/*
2 * (C) Copyright 2003
3 * Gerry Hamel, geh@ti.com, Texas Instruments
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <common.h>
22
23#ifdef CONFIG_USB_TTY
24
25#include <circbuf.h>
26#include <devices.h>
27#include "usbtty.h"
28
29#if 0
30#define TTYDBG(fmt,args...) serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
31#else
32#define TTYDBG(fmt,args...) do{}while(0)
33#endif
34
35#if 0
36#define TTYERR(fmt,args...) serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
37#else
38#define TTYERR(fmt,args...) do{}while(0)
39#endif
40
41/*
42 * Buffers to hold input and output data
43 */
44#define USBTTY_BUFFER_SIZE 256
45static circbuf_t usbtty_input;
46static circbuf_t usbtty_output;
47
48
49/*
50 * Instance variables
51 */
52static device_t usbttydev;
53static struct usb_device_instance device_instance[1];
54static struct usb_bus_instance bus_instance[1];
55static struct usb_configuration_instance config_instance[NUM_CONFIGS];
56static struct usb_interface_instance interface_instance[NUM_INTERFACES];
57static struct usb_alternate_instance alternate_instance[NUM_INTERFACES];
58static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1]; /* one extra for control endpoint */
59
60/*
61 * Static allocation of urbs
62 */
63#define RECV_ENDPOINT 1
64#define TX_ENDPOINT 2
65
66/*
67 * Global flag
68 */
69int usbtty_configured_flag = 0;
70
71
72/*
73 * Descriptors
74 */
75static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
76static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
77static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
78static u8 wstrSerial[2 + 2*(sizeof(CONFIG_USBD_SERIAL_NUMBER)-1)];
79static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
80static u8 wstrInterface[2 + 2*(sizeof(CONFIG_USBD_INTERFACE_STR)-1)];
81
82static struct usb_string_descriptor *usbtty_string_table[] = {
83 (struct usb_string_descriptor*)wstrLang,
84 (struct usb_string_descriptor*)wstrManufacturer,
85 (struct usb_string_descriptor*)wstrProduct,
86 (struct usb_string_descriptor*)wstrSerial,
87 (struct usb_string_descriptor*)wstrConfiguration,
88 (struct usb_string_descriptor*)wstrInterface
89};
90extern struct usb_string_descriptor **usb_strings; /* defined and used by omap1510_ep0.c */
91
92static struct usb_device_descriptor device_descriptor = {
93 bLength: sizeof(struct usb_device_descriptor),
94 bDescriptorType: USB_DT_DEVICE,
95 bcdUSB: USB_BCD_VERSION,
96 bDeviceClass: USBTTY_DEVICE_CLASS,
97 bDeviceSubClass: USBTTY_DEVICE_SUBCLASS,
98 bDeviceProtocol: USBTTY_DEVICE_PROTOCOL,
99 bMaxPacketSize0: EP0_MAX_PACKET_SIZE,
100 idVendor: CONFIG_USBD_VENDORID,
101 idProduct: CONFIG_USBD_PRODUCTID,
102 bcdDevice: USBTTY_BCD_DEVICE,
103 iManufacturer: STR_MANUFACTURER,
104 iProduct: STR_PRODUCT,
105 iSerialNumber: STR_SERIAL,
106 bNumConfigurations: NUM_CONFIGS
107 };
108static struct usb_configuration_descriptor config_descriptors[NUM_CONFIGS] = {
109 {
110 bLength: sizeof(struct usb_configuration_descriptor),
111 bDescriptorType: USB_DT_CONFIG,
112 wTotalLength: (sizeof(struct usb_configuration_descriptor)*NUM_CONFIGS) +
113 (sizeof(struct usb_interface_descriptor)*NUM_INTERFACES) +
114 (sizeof(struct usb_endpoint_descriptor)*NUM_ENDPOINTS),
115 bNumInterfaces: NUM_INTERFACES,
116 bConfigurationValue: 1,
117 iConfiguration: STR_CONFIG,
118 bmAttributes: BMATTRIBUTE_SELF_POWERED | BMATTRIBUTE_RESERVED,
119 bMaxPower: USBTTY_MAXPOWER
120 },
121};
122static struct usb_interface_descriptor interface_descriptors[NUM_INTERFACES] = {
123 {
124 bLength: sizeof(struct usb_interface_descriptor),
125 bDescriptorType: USB_DT_INTERFACE,
126 bInterfaceNumber: 0,
127 bAlternateSetting: 0,
128 bNumEndpoints: NUM_ENDPOINTS,
129 bInterfaceClass: USBTTY_INTERFACE_CLASS,
130 bInterfaceSubClass: USBTTY_INTERFACE_SUBCLASS,
131 bInterfaceProtocol: USBTTY_INTERFACE_PROTOCOL,
132 iInterface: STR_INTERFACE
133 },
134};
135static struct usb_endpoint_descriptor ep_descriptors[NUM_ENDPOINTS] = {
136 {
137 bLength: sizeof(struct usb_endpoint_descriptor),
138 bDescriptorType: USB_DT_ENDPOINT,
139 bEndpointAddress: CONFIG_USBD_SERIAL_OUT_ENDPOINT | USB_DIR_OUT,
140 bmAttributes: USB_ENDPOINT_XFER_BULK,
141 wMaxPacketSize: CONFIG_USBD_SERIAL_OUT_PKTSIZE,
142 bInterval: 0
143 },
144 {
145 bLength: sizeof(struct usb_endpoint_descriptor),
146 bDescriptorType: USB_DT_ENDPOINT,
147 bEndpointAddress: CONFIG_USBD_SERIAL_IN_ENDPOINT | USB_DIR_IN,
148 bmAttributes: USB_ENDPOINT_XFER_BULK,
149 wMaxPacketSize: CONFIG_USBD_SERIAL_IN_PKTSIZE,
150 bInterval: 0
151 },
152 {
153 bLength: sizeof(struct usb_endpoint_descriptor),
154 bDescriptorType: USB_DT_ENDPOINT,
155 bEndpointAddress: CONFIG_USBD_SERIAL_INT_ENDPOINT | USB_DIR_IN,
156 bmAttributes: USB_ENDPOINT_XFER_INT,
157 wMaxPacketSize: CONFIG_USBD_SERIAL_INT_PKTSIZE,
158 bInterval: 0
159 },
160};
161static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS] = {
162 &(ep_descriptors[0]),
163 &(ep_descriptors[1]),
164 &(ep_descriptors[2]),
165};
166
167/* utility function for converting char* to wide string used by USB */
168static void str2wide (char *str, u16 * wide)
169{
170 int i;
171
172 for (i = 0; i < strlen (str) && str[i]; i++)
173 wide[i] = (u16) str[i];
174}
175
176/*
177 * Prototypes
178 */
179static void usbtty_init_strings (void);
180static void usbtty_init_instances (void);
181static void usbtty_init_endpoints (void);
182
183static void usbtty_event_handler (struct usb_device_instance *device,
184 usb_device_event_t event, int data);
185static int usbtty_configured (void);
186
187static int write_buffer (circbuf_t * buf);
188static int fill_buffer (circbuf_t * buf);
189
190void usbtty_poll (void);
191static void pretend_interrupts (void);
192
193
194/*
195 * Test whether a character is in the RX buffer
196 */
197int usbtty_tstc (void)
198{
199 usbtty_poll ();
200 return (usbtty_input.size > 0);
201}
202
203/*
204 * Read a single byte from the usb client port. Returns 1 on success, 0
205 * otherwise. When the function is succesfull, the character read is
206 * written into its argument c.
207 */
208int usbtty_getc (void)
209{
210 char c;
211
212 while (usbtty_input.size <= 0) {
213 usbtty_poll ();
214 }
215
216 buf_pop (&usbtty_input, &c, 1);
217 return c;
218}
219
220/*
221 * Output a single byte to the usb client port.
222 */
223void usbtty_putc (const char c)
224{
225 buf_push (&usbtty_output, &c, 1);
226 /* If \n, also do \r */
227 if (c == '\n')
228 buf_push (&usbtty_output, "\r", 1);
229
230 /* Poll at end to handle new data... */
231 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
232 usbtty_poll ();
233 }
234}
235
236
237/* usbtty_puts() helper function for finding the next '\n' in a string */
238static int next_nl_pos (const char *s)
239{
240 int i;
241
242 for (i = 0; s[i] != '\0'; i++) {
243 if (s[i] == '\n')
244 return i;
245 }
246 return i;
247}
248
249/*
250 * Output a string to the usb client port.
251 */
252static void __usbtty_puts (const char *str, int len)
253{
254 int maxlen = usbtty_output.totalsize;
255 int space, n;
256
257 /* break str into chunks < buffer size, if needed */
258 while (len > 0) {
259 space = maxlen - usbtty_output.size;
260
261 /* Empty buffer here, if needed, to ensure space... */
262 if (space <= 0) {
263 write_buffer (&usbtty_output);
264 space = maxlen - usbtty_output.size;
265 if (space <= 0) {
266 space = len; /* allow old data to be overwritten. */
267 }
268 }
269
270 n = MIN (space, MIN (len, maxlen));
271 buf_push (&usbtty_output, str, n);
272
273 str += n;
274 len -= n;
275 }
276}
277
278void usbtty_puts (const char *str)
279{
280 int n;
281 int len = strlen (str);
282
283 /* add '\r' for each '\n' */
284 while (len > 0) {
285 n = next_nl_pos (str);
286
287 if (str[n] == '\n') {
288 __usbtty_puts (str, n + 1);
289 __usbtty_puts ("\r", 1);
290 str += (n + 1);
291 len -= (n + 1);
292 } else {
293 /* No \n found. All done. */
294 __usbtty_puts (str, n);
295 break;
296 }
297 }
298
299 /* Poll at end to handle new data... */
300 usbtty_poll ();
301}
302
303/*
304 * Initialize the usb client port.
305 *
306 */
307int drv_usbtty_init (void)
308{
309 int rc;
310
311
312 /* prepare buffers... */
313 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
314 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
315
316 /* Now, set up USB controller and infrastructure */
317 udc_init (); /* Basic USB initialization */
318
319 usbtty_init_strings ();
320 usbtty_init_instances ();
321
322 udc_startup_events (device_instance); /* Enable our device, initialize udc pointers */
323 udc_connect (); /* Enable pullup for host detection */
324
325 usbtty_init_endpoints ();
326
327 /* Device initialization */
328 memset (&usbttydev, 0, sizeof (usbttydev));
329
330 strcpy (usbttydev.name, "usbtty");
331 usbttydev.ext = 0; /* No extensions */
332 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
333 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
334 usbttydev.getc = usbtty_getc; /* 'getc' function */
335 usbttydev.putc = usbtty_putc; /* 'putc' function */
336 usbttydev.puts = usbtty_puts; /* 'puts' function */
337
338 rc = device_register (&usbttydev);
339
340 return (rc == 0) ? 1 : rc;
341}
342
343static void usbtty_init_strings (void)
344{
345 struct usb_string_descriptor *string;
346
347 string = (struct usb_string_descriptor *) wstrManufacturer;
348 string->bLength = sizeof (wstrManufacturer);
349 string->bDescriptorType = USB_DT_STRING;
350 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
351
352 string = (struct usb_string_descriptor *) wstrProduct;
353 string->bLength = sizeof (wstrProduct);
354 string->bDescriptorType = USB_DT_STRING;
355 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
356
357 string = (struct usb_string_descriptor *) wstrSerial;
358 string->bLength = sizeof (wstrSerial);
359 string->bDescriptorType = USB_DT_STRING;
360 str2wide (CONFIG_USBD_SERIAL_NUMBER, string->wData);
361
362 string = (struct usb_string_descriptor *) wstrConfiguration;
363 string->bLength = sizeof (wstrConfiguration);
364 string->bDescriptorType = USB_DT_STRING;
365 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
366
367 string = (struct usb_string_descriptor *) wstrInterface;
368 string->bLength = sizeof (wstrInterface);
369 string->bDescriptorType = USB_DT_STRING;
370 str2wide (CONFIG_USBD_INTERFACE_STR, string->wData);
371
372 /* Now, initialize the string table for ep0 handling */
373 usb_strings = usbtty_string_table;
374}
375
376static void usbtty_init_instances (void)
377{
378 int i;
379
380 /* initialize device instance */
381 memset (device_instance, 0, sizeof (struct usb_device_instance));
382 device_instance->device_state = STATE_INIT;
383 device_instance->device_descriptor = &device_descriptor;
384 device_instance->event = usbtty_event_handler;
385 device_instance->bus = bus_instance;
386 device_instance->configurations = NUM_CONFIGS;
387 device_instance->configuration_instance_array = config_instance;
388
389 /* initialize bus instance */
390 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
391 bus_instance->device = device_instance;
392 bus_instance->endpoint_array = endpoint_instance;
393 bus_instance->max_endpoints = 1;
394 bus_instance->maxpacketsize = 64;
395 bus_instance->serial_number_str = CONFIG_USBD_SERIAL_NUMBER;
396
397 /* configuration instance */
398 memset (config_instance, 0,
399 sizeof (struct usb_configuration_instance));
400 config_instance->interfaces = NUM_INTERFACES;
401 config_instance->configuration_descriptor = config_descriptors;
402 config_instance->interface_instance_array = interface_instance;
403
404 /* interface instance */
405 memset (interface_instance, 0,
406 sizeof (struct usb_interface_instance));
407 interface_instance->alternates = 1;
408 interface_instance->alternates_instance_array = alternate_instance;
409
410 /* alternates instance */
411 memset (alternate_instance, 0,
412 sizeof (struct usb_alternate_instance));
413 alternate_instance->interface_descriptor = interface_descriptors;
414 alternate_instance->endpoints = NUM_ENDPOINTS;
415 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
416
417 /* endpoint instances */
418 memset (&endpoint_instance[0], 0,
419 sizeof (struct usb_endpoint_instance));
420 endpoint_instance[0].endpoint_address = 0;
421 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
422 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
423 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
424 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
425 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
426
427 for (i = 1; i <= NUM_ENDPOINTS; i++) {
428 memset (&endpoint_instance[i], 0,
429 sizeof (struct usb_endpoint_instance));
430
431 endpoint_instance[i].endpoint_address =
432 ep_descriptors[i - 1].bEndpointAddress;
433
434 endpoint_instance[i].rcv_packetSize =
435 ep_descriptors[i - 1].wMaxPacketSize;
436 endpoint_instance[i].rcv_attributes =
437 ep_descriptors[i - 1].bmAttributes;
438
439 endpoint_instance[i].tx_packetSize =
440 ep_descriptors[i - 1].wMaxPacketSize;
441 endpoint_instance[i].tx_attributes =
442 ep_descriptors[i - 1].bmAttributes;
443
444 urb_link_init (&endpoint_instance[i].rcv);
445 urb_link_init (&endpoint_instance[i].rdy);
446 urb_link_init (&endpoint_instance[i].tx);
447 urb_link_init (&endpoint_instance[i].done);
448
449 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
450 endpoint_instance[i].tx_urb =
451 usbd_alloc_urb (device_instance,
452 &endpoint_instance[i]);
453 else
454 endpoint_instance[i].rcv_urb =
455 usbd_alloc_urb (device_instance,
456 &endpoint_instance[i]);
457 }
458}
459
460static void usbtty_init_endpoints (void)
461{
462 int i;
463
464 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
465 for (i = 0; i <= NUM_ENDPOINTS; i++) {
466 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
467 }
468}
469
470
471/*********************************************************************************/
472
473static struct urb *next_urb (struct usb_device_instance *device,
474 struct usb_endpoint_instance *endpoint)
475{
476 struct urb *current_urb = NULL;
477 int space;
478
479 /* If there's a queue, then we should add to the last urb */
480 if (!endpoint->tx_queue) {
481 current_urb = endpoint->tx_urb;
482 } else {
483 /* Last urb from tx chain */
484 current_urb =
485 p2surround (struct urb, link, endpoint->tx.prev);
486 }
487
488 /* Make sure this one has enough room */
489 space = current_urb->buffer_length - current_urb->actual_length;
490 if (space > 0) {
491 return current_urb;
492 } else { /* No space here */
493 /* First look at done list */
494 current_urb = first_urb_detached (&endpoint->done);
495 if (!current_urb) {
496 current_urb = usbd_alloc_urb (device, endpoint);
497 }
498
499 urb_append (&endpoint->tx, current_urb);
500 endpoint->tx_queue++;
501 }
502 return current_urb;
503}
504
505static int write_buffer (circbuf_t * buf)
506{
507 if (!usbtty_configured ()) {
508 return 0;
509 }
510
511 if (buf->size) {
512
513 struct usb_endpoint_instance *endpoint =
514 &endpoint_instance[TX_ENDPOINT];
515 struct urb *current_urb = NULL;
516 char *dest;
517
518 int space_avail;
519 int popnum, popped;
520 int total = 0;
521
522 /* Break buffer into urb sized pieces, and link each to the endpoint */
523 while (buf->size > 0) {
524 current_urb = next_urb (device_instance, endpoint);
525 if (!current_urb) {
526 TTYERR ("current_urb is NULL, buf->size %d\n",
527 buf->size);
528 return total;
529 }
530
531 dest = current_urb->buffer +
532 current_urb->actual_length;
533
534 space_avail =
535 current_urb->buffer_length -
536 current_urb->actual_length;
537 popnum = MIN (space_avail, buf->size);
538 if (popnum == 0)
539 break;
540
541 popped = buf_pop (buf, dest, popnum);
542 if (popped == 0)
543 break;
544 current_urb->actual_length += popped;
545 total += popped;
546
547 /* If endpoint->last == 0, then transfers have not started on this endpoint */
548 if (endpoint->last == 0) {
549 udc_endpoint_write (endpoint);
550 }
551
552 } /* end while */
553 return total;
554 } /* end if tx_urb */
555
556 return 0;
557}
558
559static int fill_buffer (circbuf_t * buf)
560{
561 struct usb_endpoint_instance *endpoint =
562 &endpoint_instance[RECV_ENDPOINT];
563
564 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
565 unsigned int nb = endpoint->rcv_urb->actual_length;
566 char *src = (char *) endpoint->rcv_urb->buffer;
567
568 buf_push (buf, src, nb);
569 endpoint->rcv_urb->actual_length = 0;
570
571 return nb;
572 }
573
574 return 0;
575}
576
577static int usbtty_configured (void)
578{
579 return usbtty_configured_flag;
580}
581
582/*********************************************************************************/
583
584static void usbtty_event_handler (struct usb_device_instance *device,
585 usb_device_event_t event, int data)
586{
587 switch (event) {
588 case DEVICE_RESET:
589 case DEVICE_BUS_INACTIVE:
590 usbtty_configured_flag = 0;
591 break;
592 case DEVICE_CONFIGURED:
593 usbtty_configured_flag = 1;
594 break;
595
596 case DEVICE_ADDRESS_ASSIGNED:
597 usbtty_init_endpoints ();
598
599 default:
600 break;
601 }
602}
603
604/*********************************************************************************/
605
606
607/*
608 * Since interrupt handling has not yet been implemented, we use this function
609 * to handle polling. This is called by the tstc,getc,putc,puts routines to
610 * update the USB state.
611 */
612void usbtty_poll (void)
613{
614 /* New interrupts? */
615 pretend_interrupts ();
616
617 /* Write any output data to host buffer (do this before checking interrupts to avoid missing one) */
618 if (usbtty_configured ()) {
619 write_buffer (&usbtty_output);
620 }
621
622 /* New interrupts? */
623 pretend_interrupts ();
624
625 /* Check for new data from host.. (do this after checking interrupts to get latest data) */
626 if (usbtty_configured ()) {
627 fill_buffer (&usbtty_input);
628 }
629
630 /* New interrupts? */
631 pretend_interrupts ();
632}
633
634static void pretend_interrupts (void)
635{
636 /* Loop while we have interrupts.
637 * If we don't do this, the input chain
638 * polling delay is likely to miss
639 * host requests.
640 */
641 while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
642 /* Handle any new IRQs */
643 omap1510_udc_irq ();
644 omap1510_udc_noniso_irq ();
645 }
646}
647#endif