blob: b992546c394d5c1e5aac6dd823d6875d506b9846 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * g_serial.c -- USB gadget serial driver
3 *
4 * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
5 *
6 * This code is based in part on the Gadget Zero driver, which
7 * is Copyright (C) 2003 by David Brownell, all rights reserved.
8 *
9 * This code also borrows from usbserial.c, which is
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
12 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
13 *
14 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation,
16 * either version 2 of that License or (at your option) any later version.
17 *
18 */
19
20#include <linux/config.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/delay.h>
24#include <linux/ioport.h>
25#include <linux/sched.h>
26#include <linux/slab.h>
27#include <linux/smp_lock.h>
28#include <linux/errno.h>
29#include <linux/init.h>
30#include <linux/timer.h>
31#include <linux/list.h>
32#include <linux/interrupt.h>
33#include <linux/utsname.h>
34#include <linux/wait.h>
35#include <linux/proc_fs.h>
36#include <linux/device.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40#include <asm/byteorder.h>
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/system.h>
44#include <asm/unaligned.h>
45#include <asm/uaccess.h>
46
47#include <linux/usb_ch9.h>
48#include <linux/usb_cdc.h>
49#include <linux/usb_gadget.h>
50
51#include "gadget_chips.h"
52
53
54/* Wait Cond */
55
56#define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
57do { \
58 wait_queue_t __wait; \
59 init_waitqueue_entry(&__wait, current); \
60 \
61 add_wait_queue(&wq, &__wait); \
62 for (;;) { \
63 set_current_state(TASK_INTERRUPTIBLE); \
64 if (condition) \
65 break; \
66 if (!signal_pending(current)) { \
67 spin_unlock_irqrestore(lock, flags); \
68 schedule(); \
69 spin_lock_irqsave(lock, flags); \
70 continue; \
71 } \
72 ret = -ERESTARTSYS; \
73 break; \
74 } \
75 current->state = TASK_RUNNING; \
76 remove_wait_queue(&wq, &__wait); \
77} while (0)
78
79#define wait_cond_interruptible(wq, condition, lock, flags) \
80({ \
81 int __ret = 0; \
82 if (!(condition)) \
83 __wait_cond_interruptible(wq, condition, lock, flags, \
84 __ret); \
85 __ret; \
86})
87
88#define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
89 timeout, ret) \
90do { \
91 signed long __timeout = timeout; \
92 wait_queue_t __wait; \
93 init_waitqueue_entry(&__wait, current); \
94 \
95 add_wait_queue(&wq, &__wait); \
96 for (;;) { \
97 set_current_state(TASK_INTERRUPTIBLE); \
98 if (__timeout == 0) \
99 break; \
100 if (condition) \
101 break; \
102 if (!signal_pending(current)) { \
103 spin_unlock_irqrestore(lock, flags); \
104 __timeout = schedule_timeout(__timeout); \
105 spin_lock_irqsave(lock, flags); \
106 continue; \
107 } \
108 ret = -ERESTARTSYS; \
109 break; \
110 } \
111 current->state = TASK_RUNNING; \
112 remove_wait_queue(&wq, &__wait); \
113} while (0)
114
115#define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
116 timeout) \
117({ \
118 int __ret = 0; \
119 if (!(condition)) \
120 __wait_cond_interruptible_timeout(wq, condition, lock, \
121 flags, timeout, __ret); \
122 __ret; \
123})
124
125
126/* Defines */
127
128#define GS_VERSION_STR "v2.0"
129#define GS_VERSION_NUM 0x0200
130
131#define GS_LONG_NAME "Gadget Serial"
132#define GS_SHORT_NAME "g_serial"
133
134#define GS_MAJOR 127
135#define GS_MINOR_START 0
136
137#define GS_NUM_PORTS 16
138
139#define GS_NUM_CONFIGS 1
140#define GS_NO_CONFIG_ID 0
141#define GS_BULK_CONFIG_ID 1
142#define GS_ACM_CONFIG_ID 2
143
144#define GS_MAX_NUM_INTERFACES 2
145#define GS_BULK_INTERFACE_ID 0
146#define GS_CONTROL_INTERFACE_ID 0
147#define GS_DATA_INTERFACE_ID 1
148
149#define GS_MAX_DESC_LEN 256
150
151#define GS_DEFAULT_READ_Q_SIZE 32
152#define GS_DEFAULT_WRITE_Q_SIZE 32
153
154#define GS_DEFAULT_WRITE_BUF_SIZE 8192
155#define GS_TMP_BUF_SIZE 8192
156
157#define GS_CLOSE_TIMEOUT 15
158
159#define GS_DEFAULT_USE_ACM 0
160
161#define GS_DEFAULT_DTE_RATE 9600
162#define GS_DEFAULT_DATA_BITS 8
163#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
164#define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
165
166/* select highspeed/fullspeed, hiding highspeed if not configured */
167#ifdef CONFIG_USB_GADGET_DUALSPEED
168#define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
169#else
170#define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
171#endif /* CONFIG_USB_GADGET_DUALSPEED */
172
173/* debug settings */
174#ifdef GS_DEBUG
175static int debug = 1;
176
177#define gs_debug(format, arg...) \
178 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
179#define gs_debug_level(level, format, arg...) \
180 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
181
182#else
183
184#define gs_debug(format, arg...) \
185 do { } while(0)
186#define gs_debug_level(level, format, arg...) \
187 do { } while(0)
188
189#endif /* GS_DEBUG */
190
191/* Thanks to NetChip Technologies for donating this product ID.
192 *
193 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
194 * Instead: allocate your own, using normal USB-IF procedures.
195 */
196#define GS_VENDOR_ID 0x0525 /* NetChip */
197#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
198#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
199
200#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
201#define GS_NOTIFY_MAXPACKET 8
202
203
204/* Structures */
205
206struct gs_dev;
207
208/* circular buffer */
209struct gs_buf {
210 unsigned int buf_size;
211 char *buf_buf;
212 char *buf_get;
213 char *buf_put;
214};
215
216/* list of requests */
217struct gs_req_entry {
218 struct list_head re_entry;
219 struct usb_request *re_req;
220};
221
222/* the port structure holds info for each port, one for each minor number */
223struct gs_port {
224 struct gs_dev *port_dev; /* pointer to device struct */
225 struct tty_struct *port_tty; /* pointer to tty struct */
226 spinlock_t port_lock;
227 int port_num;
228 int port_open_count;
229 int port_in_use; /* open/close in progress */
230 wait_queue_head_t port_write_wait;/* waiting to write */
231 struct gs_buf *port_write_buf;
232 struct usb_cdc_line_coding port_line_coding;
233};
234
235/* the device structure holds info for the USB device */
236struct gs_dev {
237 struct usb_gadget *dev_gadget; /* gadget device pointer */
238 spinlock_t dev_lock; /* lock for set/reset config */
239 int dev_config; /* configuration number */
240 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
241 struct usb_ep *dev_in_ep; /* address of in endpoint */
242 struct usb_ep *dev_out_ep; /* address of out endpoint */
Steven Cole093cf722005-05-03 19:07:24 -0600243 struct usb_endpoint_descriptor /* descriptor of notify ep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *dev_notify_ep_desc;
245 struct usb_endpoint_descriptor /* descriptor of in endpoint */
246 *dev_in_ep_desc;
247 struct usb_endpoint_descriptor /* descriptor of out endpoint */
248 *dev_out_ep_desc;
249 struct usb_request *dev_ctrl_req; /* control request */
250 struct list_head dev_req_list; /* list of write requests */
251 int dev_sched_port; /* round robin port scheduled */
252 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
253};
254
255
256/* Functions */
257
258/* module */
259static int __init gs_module_init(void);
260static void __exit gs_module_exit(void);
261
262/* tty driver */
263static int gs_open(struct tty_struct *tty, struct file *file);
264static void gs_close(struct tty_struct *tty, struct file *file);
265static int gs_write(struct tty_struct *tty,
266 const unsigned char *buf, int count);
267static void gs_put_char(struct tty_struct *tty, unsigned char ch);
268static void gs_flush_chars(struct tty_struct *tty);
269static int gs_write_room(struct tty_struct *tty);
270static int gs_chars_in_buffer(struct tty_struct *tty);
271static void gs_throttle(struct tty_struct * tty);
272static void gs_unthrottle(struct tty_struct * tty);
273static void gs_break(struct tty_struct *tty, int break_state);
274static int gs_ioctl(struct tty_struct *tty, struct file *file,
275 unsigned int cmd, unsigned long arg);
276static void gs_set_termios(struct tty_struct *tty, struct termios *old);
277
278static int gs_send(struct gs_dev *dev);
279static int gs_send_packet(struct gs_dev *dev, char *packet,
280 unsigned int size);
281static int gs_recv_packet(struct gs_dev *dev, char *packet,
282 unsigned int size);
283static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
284static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
285
286/* gadget driver */
287static int gs_bind(struct usb_gadget *gadget);
288static void gs_unbind(struct usb_gadget *gadget);
289static int gs_setup(struct usb_gadget *gadget,
290 const struct usb_ctrlrequest *ctrl);
291static int gs_setup_standard(struct usb_gadget *gadget,
292 const struct usb_ctrlrequest *ctrl);
293static int gs_setup_class(struct usb_gadget *gadget,
294 const struct usb_ctrlrequest *ctrl);
295static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
296static void gs_disconnect(struct usb_gadget *gadget);
297static int gs_set_config(struct gs_dev *dev, unsigned config);
298static void gs_reset_config(struct gs_dev *dev);
299static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
300 u8 type, unsigned int index, int is_otg);
301
302static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
Al Viro55016f12005-10-21 03:21:58 -0400303 gfp_t kmalloc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
305
306static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
Al Viro55016f12005-10-21 03:21:58 -0400307 gfp_t kmalloc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
309
Al Viro55016f12005-10-21 03:21:58 -0400310static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static void gs_free_ports(struct gs_dev *dev);
312
313/* circular buffer */
Al Viro55016f12005-10-21 03:21:58 -0400314static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void gs_buf_free(struct gs_buf *gb);
316static void gs_buf_clear(struct gs_buf *gb);
317static unsigned int gs_buf_data_avail(struct gs_buf *gb);
318static unsigned int gs_buf_space_avail(struct gs_buf *gb);
319static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
320 unsigned int count);
321static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
322 unsigned int count);
323
324/* external functions */
325extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
326
327
328/* Globals */
329
330static struct gs_dev *gs_device;
331
332static const char *EP_IN_NAME;
333static const char *EP_OUT_NAME;
334static const char *EP_NOTIFY_NAME;
335
336static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
337
338static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
339static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
340
341static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
342
343static unsigned int use_acm = GS_DEFAULT_USE_ACM;
344
345
346/* tty driver struct */
347static struct tty_operations gs_tty_ops = {
348 .open = gs_open,
349 .close = gs_close,
350 .write = gs_write,
351 .put_char = gs_put_char,
352 .flush_chars = gs_flush_chars,
353 .write_room = gs_write_room,
354 .ioctl = gs_ioctl,
355 .set_termios = gs_set_termios,
356 .throttle = gs_throttle,
357 .unthrottle = gs_unthrottle,
358 .break_ctl = gs_break,
359 .chars_in_buffer = gs_chars_in_buffer,
360};
361static struct tty_driver *gs_tty_driver;
362
363/* gadget driver struct */
364static struct usb_gadget_driver gs_gadget_driver = {
365#ifdef CONFIG_USB_GADGET_DUALSPEED
366 .speed = USB_SPEED_HIGH,
367#else
368 .speed = USB_SPEED_FULL,
369#endif /* CONFIG_USB_GADGET_DUALSPEED */
370 .function = GS_LONG_NAME,
371 .bind = gs_bind,
David Brownell329af282006-02-18 12:31:05 -0800372 .unbind = __exit_p(gs_unbind),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .setup = gs_setup,
374 .disconnect = gs_disconnect,
375 .driver = {
376 .name = GS_SHORT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 },
378};
379
380
381/* USB descriptors */
382
383#define GS_MANUFACTURER_STR_ID 1
384#define GS_PRODUCT_STR_ID 2
385#define GS_SERIAL_STR_ID 3
386#define GS_BULK_CONFIG_STR_ID 4
387#define GS_ACM_CONFIG_STR_ID 5
388#define GS_CONTROL_STR_ID 6
389#define GS_DATA_STR_ID 7
390
391/* static strings, in UTF-8 */
392static char manufacturer[50];
393static struct usb_string gs_strings[] = {
394 { GS_MANUFACTURER_STR_ID, manufacturer },
395 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
396 { GS_SERIAL_STR_ID, "0" },
397 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
398 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
399 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
400 { GS_DATA_STR_ID, "Gadget Serial Data" },
401 { } /* end of list */
402};
403
404static struct usb_gadget_strings gs_string_table = {
405 .language = 0x0409, /* en-us */
406 .strings = gs_strings,
407};
408
409static struct usb_device_descriptor gs_device_desc = {
410 .bLength = USB_DT_DEVICE_SIZE,
411 .bDescriptorType = USB_DT_DEVICE,
412 .bcdUSB = __constant_cpu_to_le16(0x0200),
413 .bDeviceSubClass = 0,
414 .bDeviceProtocol = 0,
415 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
416 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
417 .iManufacturer = GS_MANUFACTURER_STR_ID,
418 .iProduct = GS_PRODUCT_STR_ID,
419 .iSerialNumber = GS_SERIAL_STR_ID,
420 .bNumConfigurations = GS_NUM_CONFIGS,
421};
422
423static struct usb_otg_descriptor gs_otg_descriptor = {
424 .bLength = sizeof(gs_otg_descriptor),
425 .bDescriptorType = USB_DT_OTG,
426 .bmAttributes = USB_OTG_SRP,
427};
428
429static struct usb_config_descriptor gs_bulk_config_desc = {
430 .bLength = USB_DT_CONFIG_SIZE,
431 .bDescriptorType = USB_DT_CONFIG,
432 /* .wTotalLength computed dynamically */
433 .bNumInterfaces = 1,
434 .bConfigurationValue = GS_BULK_CONFIG_ID,
435 .iConfiguration = GS_BULK_CONFIG_STR_ID,
436 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
437 .bMaxPower = 1,
438};
439
440static struct usb_config_descriptor gs_acm_config_desc = {
441 .bLength = USB_DT_CONFIG_SIZE,
442 .bDescriptorType = USB_DT_CONFIG,
443 /* .wTotalLength computed dynamically */
444 .bNumInterfaces = 2,
445 .bConfigurationValue = GS_ACM_CONFIG_ID,
446 .iConfiguration = GS_ACM_CONFIG_STR_ID,
447 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
448 .bMaxPower = 1,
449};
450
451static const struct usb_interface_descriptor gs_bulk_interface_desc = {
452 .bLength = USB_DT_INTERFACE_SIZE,
453 .bDescriptorType = USB_DT_INTERFACE,
454 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
455 .bNumEndpoints = 2,
456 .bInterfaceClass = USB_CLASS_CDC_DATA,
457 .bInterfaceSubClass = 0,
458 .bInterfaceProtocol = 0,
459 .iInterface = GS_DATA_STR_ID,
460};
461
462static const struct usb_interface_descriptor gs_control_interface_desc = {
463 .bLength = USB_DT_INTERFACE_SIZE,
464 .bDescriptorType = USB_DT_INTERFACE,
465 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
466 .bNumEndpoints = 1,
467 .bInterfaceClass = USB_CLASS_COMM,
468 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
469 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
470 .iInterface = GS_CONTROL_STR_ID,
471};
472
473static const struct usb_interface_descriptor gs_data_interface_desc = {
474 .bLength = USB_DT_INTERFACE_SIZE,
475 .bDescriptorType = USB_DT_INTERFACE,
476 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
477 .bNumEndpoints = 2,
478 .bInterfaceClass = USB_CLASS_CDC_DATA,
479 .bInterfaceSubClass = 0,
480 .bInterfaceProtocol = 0,
481 .iInterface = GS_DATA_STR_ID,
482};
483
484static const struct usb_cdc_header_desc gs_header_desc = {
485 .bLength = sizeof(gs_header_desc),
486 .bDescriptorType = USB_DT_CS_INTERFACE,
487 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
488 .bcdCDC = __constant_cpu_to_le16(0x0110),
489};
490
491static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
492 .bLength = sizeof(gs_call_mgmt_descriptor),
493 .bDescriptorType = USB_DT_CS_INTERFACE,
494 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
495 .bmCapabilities = 0,
496 .bDataInterface = 1, /* index of data interface */
497};
498
499static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
500 .bLength = sizeof(gs_acm_descriptor),
501 .bDescriptorType = USB_DT_CS_INTERFACE,
502 .bDescriptorSubType = USB_CDC_ACM_TYPE,
503 .bmCapabilities = 0,
504};
505
506static const struct usb_cdc_union_desc gs_union_desc = {
507 .bLength = sizeof(gs_union_desc),
508 .bDescriptorType = USB_DT_CS_INTERFACE,
509 .bDescriptorSubType = USB_CDC_UNION_TYPE,
510 .bMasterInterface0 = 0, /* index of control interface */
511 .bSlaveInterface0 = 1, /* index of data interface */
512};
513
514static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
515 .bLength = USB_DT_ENDPOINT_SIZE,
516 .bDescriptorType = USB_DT_ENDPOINT,
517 .bEndpointAddress = USB_DIR_IN,
518 .bmAttributes = USB_ENDPOINT_XFER_INT,
519 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
520 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
521};
522
523static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
524 .bLength = USB_DT_ENDPOINT_SIZE,
525 .bDescriptorType = USB_DT_ENDPOINT,
526 .bEndpointAddress = USB_DIR_IN,
527 .bmAttributes = USB_ENDPOINT_XFER_BULK,
528};
529
530static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
531 .bLength = USB_DT_ENDPOINT_SIZE,
532 .bDescriptorType = USB_DT_ENDPOINT,
533 .bEndpointAddress = USB_DIR_OUT,
534 .bmAttributes = USB_ENDPOINT_XFER_BULK,
535};
536
537static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
538 (struct usb_descriptor_header *) &gs_otg_descriptor,
539 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
540 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
541 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
542 NULL,
543};
544
545static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
546 (struct usb_descriptor_header *) &gs_otg_descriptor,
547 (struct usb_descriptor_header *) &gs_control_interface_desc,
548 (struct usb_descriptor_header *) &gs_header_desc,
549 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
550 (struct usb_descriptor_header *) &gs_acm_descriptor,
551 (struct usb_descriptor_header *) &gs_union_desc,
552 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
553 (struct usb_descriptor_header *) &gs_data_interface_desc,
554 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
555 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
556 NULL,
557};
558
559#ifdef CONFIG_USB_GADGET_DUALSPEED
560static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
561 .bLength = USB_DT_ENDPOINT_SIZE,
562 .bDescriptorType = USB_DT_ENDPOINT,
563 .bEndpointAddress = USB_DIR_IN,
564 .bmAttributes = USB_ENDPOINT_XFER_INT,
565 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
566 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
567};
568
569static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
570 .bLength = USB_DT_ENDPOINT_SIZE,
571 .bDescriptorType = USB_DT_ENDPOINT,
572 .bmAttributes = USB_ENDPOINT_XFER_BULK,
573 .wMaxPacketSize = __constant_cpu_to_le16(512),
574};
575
576static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
577 .bLength = USB_DT_ENDPOINT_SIZE,
578 .bDescriptorType = USB_DT_ENDPOINT,
579 .bmAttributes = USB_ENDPOINT_XFER_BULK,
580 .wMaxPacketSize = __constant_cpu_to_le16(512),
581};
582
583static struct usb_qualifier_descriptor gs_qualifier_desc = {
584 .bLength = sizeof(struct usb_qualifier_descriptor),
585 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
586 .bcdUSB = __constant_cpu_to_le16 (0x0200),
587 /* assumes ep0 uses the same value for both speeds ... */
588 .bNumConfigurations = GS_NUM_CONFIGS,
589};
590
591static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
592 (struct usb_descriptor_header *) &gs_otg_descriptor,
593 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
594 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
595 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
596 NULL,
597};
598
599static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
600 (struct usb_descriptor_header *) &gs_otg_descriptor,
601 (struct usb_descriptor_header *) &gs_control_interface_desc,
602 (struct usb_descriptor_header *) &gs_header_desc,
603 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
604 (struct usb_descriptor_header *) &gs_acm_descriptor,
605 (struct usb_descriptor_header *) &gs_union_desc,
606 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
607 (struct usb_descriptor_header *) &gs_data_interface_desc,
608 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
609 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
610 NULL,
611};
612
613#endif /* CONFIG_USB_GADGET_DUALSPEED */
614
615
616/* Module */
617MODULE_DESCRIPTION(GS_LONG_NAME);
618MODULE_AUTHOR("Al Borchers");
619MODULE_LICENSE("GPL");
620
621#ifdef GS_DEBUG
622module_param(debug, int, S_IRUGO|S_IWUSR);
623MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
624#endif
625
626module_param(read_q_size, uint, S_IRUGO);
627MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
628
629module_param(write_q_size, uint, S_IRUGO);
630MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
631
632module_param(write_buf_size, uint, S_IRUGO);
633MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
634
635module_param(use_acm, uint, S_IRUGO);
636MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
637
638module_init(gs_module_init);
639module_exit(gs_module_exit);
640
641/*
642* gs_module_init
643*
644* Register as a USB gadget driver and a tty driver.
645*/
646static int __init gs_module_init(void)
647{
648 int i;
649 int retval;
650
651 retval = usb_gadget_register_driver(&gs_gadget_driver);
652 if (retval) {
653 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
654 return retval;
655 }
656
657 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
658 if (!gs_tty_driver)
659 return -ENOMEM;
660 gs_tty_driver->owner = THIS_MODULE;
661 gs_tty_driver->driver_name = GS_SHORT_NAME;
662 gs_tty_driver->name = "ttygs";
663 gs_tty_driver->devfs_name = "usb/ttygs/";
664 gs_tty_driver->major = GS_MAJOR;
665 gs_tty_driver->minor_start = GS_MINOR_START;
666 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
667 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
668 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
669 gs_tty_driver->init_termios = tty_std_termios;
670 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
671 tty_set_operations(gs_tty_driver, &gs_tty_ops);
672
673 for (i=0; i < GS_NUM_PORTS; i++)
674 sema_init(&gs_open_close_sem[i], 1);
675
676 retval = tty_register_driver(gs_tty_driver);
677 if (retval) {
678 usb_gadget_unregister_driver(&gs_gadget_driver);
679 put_tty_driver(gs_tty_driver);
680 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
681 return retval;
682 }
683
684 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
685 return 0;
686}
687
688/*
689* gs_module_exit
690*
691* Unregister as a tty driver and a USB gadget driver.
692*/
693static void __exit gs_module_exit(void)
694{
695 tty_unregister_driver(gs_tty_driver);
696 put_tty_driver(gs_tty_driver);
697 usb_gadget_unregister_driver(&gs_gadget_driver);
698
699 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
700}
701
702/* TTY Driver */
703
704/*
705 * gs_open
706 */
707static int gs_open(struct tty_struct *tty, struct file *file)
708{
709 int port_num;
710 unsigned long flags;
711 struct gs_port *port;
712 struct gs_dev *dev;
713 struct gs_buf *buf;
714 struct semaphore *sem;
715 int ret;
716
717 port_num = tty->index;
718
719 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
720
721 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
722 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
723 port_num, tty, file);
724 return -ENODEV;
725 }
726
727 dev = gs_device;
728
729 if (dev == NULL) {
730 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
731 port_num, tty, file);
732 return -ENODEV;
733 }
734
735 sem = &gs_open_close_sem[port_num];
736 if (down_interruptible(sem)) {
737 printk(KERN_ERR
738 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
739 port_num, tty, file);
740 return -ERESTARTSYS;
741 }
742
743 spin_lock_irqsave(&dev->dev_lock, flags);
744
745 if (dev->dev_config == GS_NO_CONFIG_ID) {
746 printk(KERN_ERR
747 "gs_open: (%d,%p,%p) device is not connected\n",
748 port_num, tty, file);
749 ret = -ENODEV;
750 goto exit_unlock_dev;
751 }
752
753 port = dev->dev_port[port_num];
754
755 if (port == NULL) {
756 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
757 port_num, tty, file);
758 ret = -ENODEV;
759 goto exit_unlock_dev;
760 }
761
762 spin_lock(&port->port_lock);
763 spin_unlock(&dev->dev_lock);
764
765 if (port->port_dev == NULL) {
766 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
767 port_num, tty, file);
768 ret = -EIO;
769 goto exit_unlock_port;
770 }
771
772 if (port->port_open_count > 0) {
773 ++port->port_open_count;
774 gs_debug("gs_open: (%d,%p,%p) already open\n",
775 port_num, tty, file);
776 ret = 0;
777 goto exit_unlock_port;
778 }
779
780 tty->driver_data = NULL;
781
782 /* mark port as in use, we can drop port lock and sleep if necessary */
783 port->port_in_use = 1;
784
785 /* allocate write buffer on first open */
786 if (port->port_write_buf == NULL) {
787 spin_unlock_irqrestore(&port->port_lock, flags);
788 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
789 spin_lock_irqsave(&port->port_lock, flags);
790
791 /* might have been disconnected while asleep, check */
792 if (port->port_dev == NULL) {
793 printk(KERN_ERR
794 "gs_open: (%d,%p,%p) port disconnected (2)\n",
795 port_num, tty, file);
796 port->port_in_use = 0;
797 ret = -EIO;
798 goto exit_unlock_port;
799 }
800
801 if ((port->port_write_buf=buf) == NULL) {
802 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
803 port_num, tty, file);
804 port->port_in_use = 0;
805 ret = -ENOMEM;
806 goto exit_unlock_port;
807 }
808
809 }
810
811 /* wait for carrier detect (not implemented) */
812
813 /* might have been disconnected while asleep, check */
814 if (port->port_dev == NULL) {
815 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
816 port_num, tty, file);
817 port->port_in_use = 0;
818 ret = -EIO;
819 goto exit_unlock_port;
820 }
821
822 tty->driver_data = port;
823 port->port_tty = tty;
824 port->port_open_count = 1;
825 port->port_in_use = 0;
826
827 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
828
829 ret = 0;
830
831exit_unlock_port:
832 spin_unlock_irqrestore(&port->port_lock, flags);
833 up(sem);
834 return ret;
835
836exit_unlock_dev:
837 spin_unlock_irqrestore(&dev->dev_lock, flags);
838 up(sem);
839 return ret;
840
841}
842
843/*
844 * gs_close
845 */
846static void gs_close(struct tty_struct *tty, struct file *file)
847{
848 unsigned long flags;
849 struct gs_port *port = tty->driver_data;
850 struct semaphore *sem;
851
852 if (port == NULL) {
853 printk(KERN_ERR "gs_close: NULL port pointer\n");
854 return;
855 }
856
857 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
858
859 sem = &gs_open_close_sem[port->port_num];
860 down(sem);
861
862 spin_lock_irqsave(&port->port_lock, flags);
863
864 if (port->port_open_count == 0) {
865 printk(KERN_ERR
866 "gs_close: (%d,%p,%p) port is already closed\n",
867 port->port_num, tty, file);
868 goto exit;
869 }
870
871 if (port->port_open_count > 1) {
872 --port->port_open_count;
873 goto exit;
874 }
875
876 /* free disconnected port on final close */
877 if (port->port_dev == NULL) {
878 kfree(port);
879 goto exit;
880 }
881
882 /* mark port as closed but in use, we can drop port lock */
883 /* and sleep if necessary */
884 port->port_in_use = 1;
885 port->port_open_count = 0;
886
887 /* wait for write buffer to drain, or */
888 /* at most GS_CLOSE_TIMEOUT seconds */
889 if (gs_buf_data_avail(port->port_write_buf) > 0) {
Fengwei Yin949bf642005-11-18 10:35:36 +0800890 spin_unlock_irqrestore(&port->port_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 wait_cond_interruptible_timeout(port->port_write_wait,
892 port->port_dev == NULL
893 || gs_buf_data_avail(port->port_write_buf) == 0,
894 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
Fengwei Yin949bf642005-11-18 10:35:36 +0800895 spin_lock_irqsave(&port->port_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 /* free disconnected port on final close */
899 /* (might have happened during the above sleep) */
900 if (port->port_dev == NULL) {
901 kfree(port);
902 goto exit;
903 }
904
905 gs_buf_clear(port->port_write_buf);
906
907 tty->driver_data = NULL;
908 port->port_tty = NULL;
909 port->port_in_use = 0;
910
911 gs_debug("gs_close: (%d,%p,%p) completed\n",
912 port->port_num, tty, file);
913
914exit:
915 spin_unlock_irqrestore(&port->port_lock, flags);
916 up(sem);
917}
918
919/*
920 * gs_write
921 */
922static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
923{
924 unsigned long flags;
925 struct gs_port *port = tty->driver_data;
926 int ret;
927
928 if (port == NULL) {
929 printk(KERN_ERR "gs_write: NULL port pointer\n");
930 return -EIO;
931 }
932
933 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
934 count);
935
936 if (count == 0)
937 return 0;
938
939 spin_lock_irqsave(&port->port_lock, flags);
940
941 if (port->port_dev == NULL) {
942 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
943 port->port_num, tty);
944 ret = -EIO;
945 goto exit;
946 }
947
948 if (port->port_open_count == 0) {
949 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
950 port->port_num, tty);
951 ret = -EBADF;
952 goto exit;
953 }
954
955 count = gs_buf_put(port->port_write_buf, buf, count);
956
957 spin_unlock_irqrestore(&port->port_lock, flags);
958
959 gs_send(gs_device);
960
961 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
962 count);
963
964 return count;
965
966exit:
967 spin_unlock_irqrestore(&port->port_lock, flags);
968 return ret;
969}
970
971/*
972 * gs_put_char
973 */
974static void gs_put_char(struct tty_struct *tty, unsigned char ch)
975{
976 unsigned long flags;
977 struct gs_port *port = tty->driver_data;
978
979 if (port == NULL) {
980 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
981 return;
982 }
983
984 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p, %p, %p\n", port->port_num, tty, ch, __builtin_return_address(0), __builtin_return_address(1), __builtin_return_address(2));
985
986 spin_lock_irqsave(&port->port_lock, flags);
987
988 if (port->port_dev == NULL) {
989 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
990 port->port_num, tty);
991 goto exit;
992 }
993
994 if (port->port_open_count == 0) {
995 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
996 port->port_num, tty);
997 goto exit;
998 }
999
1000 gs_buf_put(port->port_write_buf, &ch, 1);
1001
1002exit:
1003 spin_unlock_irqrestore(&port->port_lock, flags);
1004}
1005
1006/*
1007 * gs_flush_chars
1008 */
1009static void gs_flush_chars(struct tty_struct *tty)
1010{
1011 unsigned long flags;
1012 struct gs_port *port = tty->driver_data;
1013
1014 if (port == NULL) {
1015 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1016 return;
1017 }
1018
1019 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1020
1021 spin_lock_irqsave(&port->port_lock, flags);
1022
1023 if (port->port_dev == NULL) {
1024 printk(KERN_ERR
1025 "gs_flush_chars: (%d,%p) port is not connected\n",
1026 port->port_num, tty);
1027 goto exit;
1028 }
1029
1030 if (port->port_open_count == 0) {
1031 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1032 port->port_num, tty);
1033 goto exit;
1034 }
1035
1036 spin_unlock_irqrestore(&port->port_lock, flags);
1037
1038 gs_send(gs_device);
1039
1040 return;
1041
1042exit:
1043 spin_unlock_irqrestore(&port->port_lock, flags);
1044}
1045
1046/*
1047 * gs_write_room
1048 */
1049static int gs_write_room(struct tty_struct *tty)
1050{
1051
1052 int room = 0;
1053 unsigned long flags;
1054 struct gs_port *port = tty->driver_data;
1055
1056
1057 if (port == NULL)
1058 return 0;
1059
1060 spin_lock_irqsave(&port->port_lock, flags);
1061
1062 if (port->port_dev != NULL && port->port_open_count > 0
1063 && port->port_write_buf != NULL)
1064 room = gs_buf_space_avail(port->port_write_buf);
1065
1066 spin_unlock_irqrestore(&port->port_lock, flags);
1067
1068 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1069 port->port_num, tty, room);
1070
1071 return room;
1072}
1073
1074/*
1075 * gs_chars_in_buffer
1076 */
1077static int gs_chars_in_buffer(struct tty_struct *tty)
1078{
1079 int chars = 0;
1080 unsigned long flags;
1081 struct gs_port *port = tty->driver_data;
1082
1083 if (port == NULL)
1084 return 0;
1085
1086 spin_lock_irqsave(&port->port_lock, flags);
1087
1088 if (port->port_dev != NULL && port->port_open_count > 0
1089 && port->port_write_buf != NULL)
1090 chars = gs_buf_data_avail(port->port_write_buf);
1091
1092 spin_unlock_irqrestore(&port->port_lock, flags);
1093
1094 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1095 port->port_num, tty, chars);
1096
1097 return chars;
1098}
1099
1100/*
1101 * gs_throttle
1102 */
1103static void gs_throttle(struct tty_struct *tty)
1104{
1105}
1106
1107/*
1108 * gs_unthrottle
1109 */
1110static void gs_unthrottle(struct tty_struct *tty)
1111{
1112}
1113
1114/*
1115 * gs_break
1116 */
1117static void gs_break(struct tty_struct *tty, int break_state)
1118{
1119}
1120
1121/*
1122 * gs_ioctl
1123 */
1124static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1125{
1126 struct gs_port *port = tty->driver_data;
1127
1128 if (port == NULL) {
1129 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1130 return -EIO;
1131 }
1132
1133 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1134 port->port_num, tty, file, cmd, arg);
1135
1136 /* handle ioctls */
1137
1138 /* could not handle ioctl */
1139 return -ENOIOCTLCMD;
1140}
1141
1142/*
1143 * gs_set_termios
1144 */
1145static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1146{
1147}
1148
1149/*
1150* gs_send
1151*
1152* This function finds available write requests, calls
1153* gs_send_packet to fill these packets with data, and
1154* continues until either there are no more write requests
1155* available or no more data to send. This function is
1156* run whenever data arrives or write requests are available.
1157*/
1158static int gs_send(struct gs_dev *dev)
1159{
1160 int ret,len;
1161 unsigned long flags;
1162 struct usb_ep *ep;
1163 struct usb_request *req;
1164 struct gs_req_entry *req_entry;
1165
1166 if (dev == NULL) {
1167 printk(KERN_ERR "gs_send: NULL device pointer\n");
1168 return -ENODEV;
1169 }
1170
1171 spin_lock_irqsave(&dev->dev_lock, flags);
1172
1173 ep = dev->dev_in_ep;
1174
1175 while(!list_empty(&dev->dev_req_list)) {
1176
1177 req_entry = list_entry(dev->dev_req_list.next,
1178 struct gs_req_entry, re_entry);
1179
1180 req = req_entry->re_req;
1181
1182 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1183
1184 if (len > 0) {
1185gs_debug_level(3, "gs_send: len=%d, 0x%2.2x 0x%2.2x 0x%2.2x ...\n", len, *((unsigned char *)req->buf), *((unsigned char *)req->buf+1), *((unsigned char *)req->buf+2));
1186 list_del(&req_entry->re_entry);
1187 req->length = len;
1188 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1189 printk(KERN_ERR
1190 "gs_send: cannot queue read request, ret=%d\n",
1191 ret);
1192 break;
1193 }
1194 } else {
1195 break;
1196 }
1197
1198 }
1199
1200 spin_unlock_irqrestore(&dev->dev_lock, flags);
1201
1202 return 0;
1203}
1204
1205/*
1206 * gs_send_packet
1207 *
1208 * If there is data to send, a packet is built in the given
1209 * buffer and the size is returned. If there is no data to
1210 * send, 0 is returned. If there is any error a negative
1211 * error number is returned.
1212 *
1213 * Called during USB completion routine, on interrupt time.
1214 *
1215 * We assume that disconnect will not happen until all completion
1216 * routines have completed, so we can assume that the dev_port
1217 * array does not change during the lifetime of this function.
1218 */
1219static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1220{
1221 unsigned int len;
1222 struct gs_port *port;
1223
1224 /* TEMPORARY -- only port 0 is supported right now */
1225 port = dev->dev_port[0];
1226
1227 if (port == NULL) {
1228 printk(KERN_ERR
1229 "gs_send_packet: port=%d, NULL port pointer\n",
1230 0);
1231 return -EIO;
1232 }
1233
1234 spin_lock(&port->port_lock);
1235
1236 len = gs_buf_data_avail(port->port_write_buf);
1237 if (len < size)
1238 size = len;
1239
1240 if (size == 0)
1241 goto exit;
1242
1243 size = gs_buf_get(port->port_write_buf, packet, size);
1244
1245 if (port->port_tty)
1246 wake_up_interruptible(&port->port_tty->write_wait);
1247
1248exit:
1249 spin_unlock(&port->port_lock);
1250 return size;
1251}
1252
1253/*
1254 * gs_recv_packet
1255 *
1256 * Called for each USB packet received. Reads the packet
1257 * header and stuffs the data in the appropriate tty buffer.
1258 * Returns 0 if successful, or a negative error number.
1259 *
1260 * Called during USB completion routine, on interrupt time.
1261 *
1262 * We assume that disconnect will not happen until all completion
1263 * routines have completed, so we can assume that the dev_port
1264 * array does not change during the lifetime of this function.
1265 */
1266static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1267{
1268 unsigned int len;
1269 struct gs_port *port;
1270 int ret;
Alan Cox33f0f882006-01-09 20:54:13 -08001271 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* TEMPORARY -- only port 0 is supported right now */
1274 port = dev->dev_port[0];
1275
1276 if (port == NULL) {
1277 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1278 port->port_num);
1279 return -EIO;
1280 }
1281
1282 spin_lock(&port->port_lock);
1283
1284 if (port->port_open_count == 0) {
1285 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1286 port->port_num);
1287 ret = -EIO;
1288 goto exit;
1289 }
1290
Alan Cox33f0f882006-01-09 20:54:13 -08001291
1292 tty = port->port_tty;
1293
1294 if (tty == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1296 port->port_num);
1297 ret = -EIO;
1298 goto exit;
1299 }
1300
1301 if (port->port_tty->magic != TTY_MAGIC) {
1302 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1303 port->port_num);
1304 ret = -EIO;
1305 goto exit;
1306 }
1307
Alan Cox33f0f882006-01-09 20:54:13 -08001308 len = tty_buffer_request_room(tty, size);
1309 if (len > 0) {
1310 tty_insert_flip_string(tty, packet, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 tty_flip_buffer_push(port->port_tty);
1312 wake_up_interruptible(&port->port_tty->read_wait);
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315exit:
1316 spin_unlock(&port->port_lock);
1317 return ret;
1318}
1319
1320/*
1321* gs_read_complete
1322*/
1323static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1324{
1325 int ret;
1326 struct gs_dev *dev = ep->driver_data;
1327
1328 if (dev == NULL) {
1329 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1330 return;
1331 }
1332
1333 switch(req->status) {
1334 case 0:
1335 /* normal completion */
1336 gs_recv_packet(dev, req->buf, req->actual);
1337requeue:
1338 req->length = ep->maxpacket;
1339 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1340 printk(KERN_ERR
1341 "gs_read_complete: cannot queue read request, ret=%d\n",
1342 ret);
1343 }
1344 break;
1345
1346 case -ESHUTDOWN:
1347 /* disconnect */
1348 gs_debug("gs_read_complete: shutdown\n");
1349 gs_free_req(ep, req);
1350 break;
1351
1352 default:
1353 /* unexpected */
1354 printk(KERN_ERR
1355 "gs_read_complete: unexpected status error, status=%d\n",
1356 req->status);
1357 goto requeue;
1358 break;
1359 }
1360}
1361
1362/*
1363* gs_write_complete
1364*/
1365static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1366{
1367 struct gs_dev *dev = ep->driver_data;
1368 struct gs_req_entry *gs_req = req->context;
1369
1370 if (dev == NULL) {
1371 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1372 return;
1373 }
1374
1375 switch(req->status) {
1376 case 0:
1377 /* normal completion */
1378requeue:
1379 if (gs_req == NULL) {
1380 printk(KERN_ERR
1381 "gs_write_complete: NULL request pointer\n");
1382 return;
1383 }
1384
1385 spin_lock(&dev->dev_lock);
1386 list_add(&gs_req->re_entry, &dev->dev_req_list);
1387 spin_unlock(&dev->dev_lock);
1388
1389 gs_send(dev);
1390
1391 break;
1392
1393 case -ESHUTDOWN:
1394 /* disconnect */
1395 gs_debug("gs_write_complete: shutdown\n");
1396 gs_free_req(ep, req);
1397 break;
1398
1399 default:
1400 printk(KERN_ERR
1401 "gs_write_complete: unexpected status error, status=%d\n",
1402 req->status);
1403 goto requeue;
1404 break;
1405 }
1406}
1407
1408/* Gadget Driver */
1409
1410/*
1411 * gs_bind
1412 *
1413 * Called on module load. Allocates and initializes the device
1414 * structure and a control request.
1415 */
David Brownell329af282006-02-18 12:31:05 -08001416static int __init gs_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 int ret;
1419 struct usb_ep *ep;
1420 struct gs_dev *dev;
David Brownell91e79c92005-07-13 15:18:30 -07001421 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
David Brownell91e79c92005-07-13 15:18:30 -07001423 /* Some controllers can't support CDC ACM:
1424 * - sh doesn't support multiple interfaces or configs;
1425 * - sa1100 doesn't have a third interrupt endpoint
1426 */
1427 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 use_acm = 0;
David Brownell91e79c92005-07-13 15:18:30 -07001429
1430 gcnum = usb_gadget_controller_number(gadget);
1431 if (gcnum >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 gs_device_desc.bcdDevice =
David Brownell91e79c92005-07-13 15:18:30 -07001433 cpu_to_le16(GS_VERSION_NUM | gcnum);
1434 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1436 gadget->name);
1437 /* unrecognized, but safe unless bulk is REALLY quirky */
1438 gs_device_desc.bcdDevice =
1439 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1440 }
1441
1442 usb_ep_autoconfig_reset(gadget);
1443
1444 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1445 if (!ep)
1446 goto autoconf_fail;
1447 EP_IN_NAME = ep->name;
1448 ep->driver_data = ep; /* claim the endpoint */
1449
1450 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1451 if (!ep)
1452 goto autoconf_fail;
1453 EP_OUT_NAME = ep->name;
1454 ep->driver_data = ep; /* claim the endpoint */
1455
1456 if (use_acm) {
1457 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1458 if (!ep) {
1459 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1460 goto autoconf_fail;
1461 }
1462 gs_device_desc.idProduct = __constant_cpu_to_le16(
1463 GS_CDC_PRODUCT_ID),
1464 EP_NOTIFY_NAME = ep->name;
1465 ep->driver_data = ep; /* claim the endpoint */
1466 }
1467
1468 gs_device_desc.bDeviceClass = use_acm
1469 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1470 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1471
1472#ifdef CONFIG_USB_GADGET_DUALSPEED
1473 gs_qualifier_desc.bDeviceClass = use_acm
1474 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1475 /* assume ep0 uses the same packet size for both speeds */
1476 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1477 /* assume endpoints are dual-speed */
1478 gs_highspeed_notify_desc.bEndpointAddress =
1479 gs_fullspeed_notify_desc.bEndpointAddress;
1480 gs_highspeed_in_desc.bEndpointAddress =
1481 gs_fullspeed_in_desc.bEndpointAddress;
1482 gs_highspeed_out_desc.bEndpointAddress =
1483 gs_fullspeed_out_desc.bEndpointAddress;
1484#endif /* CONFIG_USB_GADGET_DUALSPEED */
1485
1486 usb_gadget_set_selfpowered(gadget);
1487
1488 if (gadget->is_otg) {
1489 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1490 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1491 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1492 }
1493
1494 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1495 if (dev == NULL)
1496 return -ENOMEM;
1497
1498 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1499 system_utsname.sysname, system_utsname.release,
1500 gadget->name);
1501
1502 memset(dev, 0, sizeof(struct gs_dev));
1503 dev->dev_gadget = gadget;
1504 spin_lock_init(&dev->dev_lock);
1505 INIT_LIST_HEAD(&dev->dev_req_list);
1506 set_gadget_data(gadget, dev);
1507
1508 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1509 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1510 gs_unbind(gadget);
1511 return ret;
1512 }
1513
1514 /* preallocate control response and buffer */
1515 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1516 GFP_KERNEL);
1517 if (dev->dev_ctrl_req == NULL) {
1518 gs_unbind(gadget);
1519 return -ENOMEM;
1520 }
1521 dev->dev_ctrl_req->complete = gs_setup_complete;
1522
1523 gadget->ep0->driver_data = dev;
1524
1525 printk(KERN_INFO "gs_bind: %s %s bound\n",
1526 GS_LONG_NAME, GS_VERSION_STR);
1527
1528 return 0;
1529
1530autoconf_fail:
1531 printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1532 return -ENODEV;
1533}
1534
1535/*
1536 * gs_unbind
1537 *
1538 * Called on module unload. Frees the control request and device
1539 * structure.
1540 */
David Brownell329af282006-02-18 12:31:05 -08001541static void __exit gs_unbind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct gs_dev *dev = get_gadget_data(gadget);
1544
1545 gs_device = NULL;
1546
1547 /* read/write requests already freed, only control request remains */
1548 if (dev != NULL) {
1549 if (dev->dev_ctrl_req != NULL) {
1550 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1551 dev->dev_ctrl_req = NULL;
1552 }
1553 gs_free_ports(dev);
1554 kfree(dev);
1555 set_gadget_data(gadget, NULL);
1556 }
1557
1558 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1559 GS_VERSION_STR);
1560}
1561
1562/*
1563 * gs_setup
1564 *
1565 * Implements all the control endpoint functionality that's not
1566 * handled in hardware or the hardware driver.
1567 *
1568 * Returns the size of the data sent to the host, or a negative
1569 * error number.
1570 */
1571static int gs_setup(struct usb_gadget *gadget,
1572 const struct usb_ctrlrequest *ctrl)
1573{
1574 int ret = -EOPNOTSUPP;
1575 struct gs_dev *dev = get_gadget_data(gadget);
1576 struct usb_request *req = dev->dev_ctrl_req;
David Brownell1bbc1692005-05-07 13:05:13 -07001577 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1578 u16 wValue = le16_to_cpu(ctrl->wValue);
1579 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1582 case USB_TYPE_STANDARD:
1583 ret = gs_setup_standard(gadget,ctrl);
1584 break;
1585
1586 case USB_TYPE_CLASS:
1587 ret = gs_setup_class(gadget,ctrl);
1588 break;
1589
1590 default:
1591 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1592 ctrl->bRequestType, ctrl->bRequest,
1593 wValue, wIndex, wLength);
1594 break;
1595 }
1596
1597 /* respond with data transfer before status phase? */
1598 if (ret >= 0) {
1599 req->length = ret;
1600 req->zero = ret < wLength
1601 && (ret % gadget->ep0->maxpacket) == 0;
1602 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1603 if (ret < 0) {
1604 printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1605 ret);
1606 req->status = 0;
1607 gs_setup_complete(gadget->ep0, req);
1608 }
1609 }
1610
1611 /* device either stalls (ret < 0) or reports success */
1612 return ret;
1613}
1614
1615static int gs_setup_standard(struct usb_gadget *gadget,
1616 const struct usb_ctrlrequest *ctrl)
1617{
1618 int ret = -EOPNOTSUPP;
1619 struct gs_dev *dev = get_gadget_data(gadget);
1620 struct usb_request *req = dev->dev_ctrl_req;
David Brownell1bbc1692005-05-07 13:05:13 -07001621 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1622 u16 wValue = le16_to_cpu(ctrl->wValue);
1623 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 switch (ctrl->bRequest) {
1626 case USB_REQ_GET_DESCRIPTOR:
1627 if (ctrl->bRequestType != USB_DIR_IN)
1628 break;
1629
1630 switch (wValue >> 8) {
1631 case USB_DT_DEVICE:
1632 ret = min(wLength,
1633 (u16)sizeof(struct usb_device_descriptor));
1634 memcpy(req->buf, &gs_device_desc, ret);
1635 break;
1636
1637#ifdef CONFIG_USB_GADGET_DUALSPEED
1638 case USB_DT_DEVICE_QUALIFIER:
1639 if (!gadget->is_dualspeed)
1640 break;
1641 ret = min(wLength,
1642 (u16)sizeof(struct usb_qualifier_descriptor));
1643 memcpy(req->buf, &gs_qualifier_desc, ret);
1644 break;
1645
1646 case USB_DT_OTHER_SPEED_CONFIG:
1647 if (!gadget->is_dualspeed)
1648 break;
1649 /* fall through */
1650#endif /* CONFIG_USB_GADGET_DUALSPEED */
1651 case USB_DT_CONFIG:
1652 ret = gs_build_config_buf(req->buf, gadget->speed,
1653 wValue >> 8, wValue & 0xff,
1654 gadget->is_otg);
1655 if (ret >= 0)
1656 ret = min(wLength, (u16)ret);
1657 break;
1658
1659 case USB_DT_STRING:
1660 /* wIndex == language code. */
1661 ret = usb_gadget_get_string(&gs_string_table,
1662 wValue & 0xff, req->buf);
1663 if (ret >= 0)
1664 ret = min(wLength, (u16)ret);
1665 break;
1666 }
1667 break;
1668
1669 case USB_REQ_SET_CONFIGURATION:
1670 if (ctrl->bRequestType != 0)
1671 break;
1672 spin_lock(&dev->dev_lock);
1673 ret = gs_set_config(dev, wValue);
1674 spin_unlock(&dev->dev_lock);
1675 break;
1676
1677 case USB_REQ_GET_CONFIGURATION:
1678 if (ctrl->bRequestType != USB_DIR_IN)
1679 break;
1680 *(u8 *)req->buf = dev->dev_config;
1681 ret = min(wLength, (u16)1);
1682 break;
1683
1684 case USB_REQ_SET_INTERFACE:
1685 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1686 || !dev->dev_config
1687 || wIndex >= GS_MAX_NUM_INTERFACES)
1688 break;
1689 if (dev->dev_config == GS_BULK_CONFIG_ID
1690 && wIndex != GS_BULK_INTERFACE_ID)
1691 break;
1692 /* no alternate interface settings */
1693 if (wValue != 0)
1694 break;
1695 spin_lock(&dev->dev_lock);
1696 /* PXA hardware partially handles SET_INTERFACE;
1697 * we need to kluge around that interference. */
1698 if (gadget_is_pxa(gadget)) {
1699 ret = gs_set_config(dev, use_acm ?
1700 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1701 goto set_interface_done;
1702 }
1703 if (dev->dev_config != GS_BULK_CONFIG_ID
1704 && wIndex == GS_CONTROL_INTERFACE_ID) {
1705 if (dev->dev_notify_ep) {
1706 usb_ep_disable(dev->dev_notify_ep);
1707 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1708 }
1709 } else {
1710 usb_ep_disable(dev->dev_in_ep);
1711 usb_ep_disable(dev->dev_out_ep);
1712 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1713 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1714 }
1715 ret = 0;
1716set_interface_done:
1717 spin_unlock(&dev->dev_lock);
1718 break;
1719
1720 case USB_REQ_GET_INTERFACE:
1721 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1722 || dev->dev_config == GS_NO_CONFIG_ID)
1723 break;
1724 if (wIndex >= GS_MAX_NUM_INTERFACES
1725 || (dev->dev_config == GS_BULK_CONFIG_ID
1726 && wIndex != GS_BULK_INTERFACE_ID)) {
1727 ret = -EDOM;
1728 break;
1729 }
1730 /* no alternate interface settings */
1731 *(u8 *)req->buf = 0;
1732 ret = min(wLength, (u16)1);
1733 break;
1734
1735 default:
1736 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1737 ctrl->bRequestType, ctrl->bRequest,
1738 wValue, wIndex, wLength);
1739 break;
1740 }
1741
1742 return ret;
1743}
1744
1745static int gs_setup_class(struct usb_gadget *gadget,
1746 const struct usb_ctrlrequest *ctrl)
1747{
1748 int ret = -EOPNOTSUPP;
1749 struct gs_dev *dev = get_gadget_data(gadget);
1750 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1751 struct usb_request *req = dev->dev_ctrl_req;
David Brownell1bbc1692005-05-07 13:05:13 -07001752 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1753 u16 wValue = le16_to_cpu(ctrl->wValue);
1754 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 switch (ctrl->bRequest) {
1757 case USB_CDC_REQ_SET_LINE_CODING:
1758 ret = min(wLength,
1759 (u16)sizeof(struct usb_cdc_line_coding));
1760 if (port) {
1761 spin_lock(&port->port_lock);
1762 memcpy(&port->port_line_coding, req->buf, ret);
1763 spin_unlock(&port->port_lock);
1764 }
1765 break;
1766
1767 case USB_CDC_REQ_GET_LINE_CODING:
1768 port = dev->dev_port[0]; /* ACM only has one port */
1769 ret = min(wLength,
1770 (u16)sizeof(struct usb_cdc_line_coding));
1771 if (port) {
1772 spin_lock(&port->port_lock);
1773 memcpy(req->buf, &port->port_line_coding, ret);
1774 spin_unlock(&port->port_lock);
1775 }
1776 break;
1777
1778 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1779 ret = 0;
1780 break;
1781
1782 default:
1783 printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1784 ctrl->bRequestType, ctrl->bRequest,
1785 wValue, wIndex, wLength);
1786 break;
1787 }
1788
1789 return ret;
1790}
1791
1792/*
1793 * gs_setup_complete
1794 */
1795static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1796{
1797 if (req->status || req->actual != req->length) {
1798 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1799 req->status, req->actual, req->length);
1800 }
1801}
1802
1803/*
1804 * gs_disconnect
1805 *
1806 * Called when the device is disconnected. Frees the closed
1807 * ports and disconnects open ports. Open ports will be freed
1808 * on close. Then reallocates the ports for the next connection.
1809 */
1810static void gs_disconnect(struct usb_gadget *gadget)
1811{
1812 unsigned long flags;
1813 struct gs_dev *dev = get_gadget_data(gadget);
1814
1815 spin_lock_irqsave(&dev->dev_lock, flags);
1816
1817 gs_reset_config(dev);
1818
1819 /* free closed ports and disconnect open ports */
1820 /* (open ports will be freed when closed) */
1821 gs_free_ports(dev);
1822
1823 /* re-allocate ports for the next connection */
1824 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1825 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1826
1827 spin_unlock_irqrestore(&dev->dev_lock, flags);
1828
1829 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1830}
1831
1832/*
1833 * gs_set_config
1834 *
1835 * Configures the device by enabling device specific
1836 * optimizations, setting up the endpoints, allocating
1837 * read and write requests and queuing read requests.
1838 *
1839 * The device lock must be held when calling this function.
1840 */
1841static int gs_set_config(struct gs_dev *dev, unsigned config)
1842{
1843 int i;
1844 int ret = 0;
1845 struct usb_gadget *gadget = dev->dev_gadget;
1846 struct usb_ep *ep;
1847 struct usb_endpoint_descriptor *ep_desc;
1848 struct usb_request *req;
1849 struct gs_req_entry *req_entry;
1850
1851 if (dev == NULL) {
1852 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1853 return 0;
1854 }
1855
1856 if (config == dev->dev_config)
1857 return 0;
1858
1859 gs_reset_config(dev);
1860
1861 switch (config) {
1862 case GS_NO_CONFIG_ID:
1863 return 0;
1864 case GS_BULK_CONFIG_ID:
1865 if (use_acm)
1866 return -EINVAL;
1867 /* device specific optimizations */
1868 if (gadget_is_net2280(gadget))
1869 net2280_set_fifo_mode(gadget, 1);
1870 break;
1871 case GS_ACM_CONFIG_ID:
1872 if (!use_acm)
1873 return -EINVAL;
1874 /* device specific optimizations */
1875 if (gadget_is_net2280(gadget))
1876 net2280_set_fifo_mode(gadget, 1);
1877 break;
1878 default:
1879 return -EINVAL;
1880 }
1881
1882 dev->dev_config = config;
1883
1884 gadget_for_each_ep(ep, gadget) {
1885
1886 if (EP_NOTIFY_NAME
1887 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1888 ep_desc = GS_SPEED_SELECT(
1889 gadget->speed == USB_SPEED_HIGH,
1890 &gs_highspeed_notify_desc,
1891 &gs_fullspeed_notify_desc);
1892 ret = usb_ep_enable(ep,ep_desc);
1893 if (ret == 0) {
1894 ep->driver_data = dev;
1895 dev->dev_notify_ep = ep;
1896 dev->dev_notify_ep_desc = ep_desc;
1897 } else {
1898 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1899 ep->name, ret);
1900 goto exit_reset_config;
1901 }
1902 }
1903
1904 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1905 ep_desc = GS_SPEED_SELECT(
1906 gadget->speed == USB_SPEED_HIGH,
1907 &gs_highspeed_in_desc,
1908 &gs_fullspeed_in_desc);
1909 ret = usb_ep_enable(ep,ep_desc);
1910 if (ret == 0) {
1911 ep->driver_data = dev;
1912 dev->dev_in_ep = ep;
1913 dev->dev_in_ep_desc = ep_desc;
1914 } else {
1915 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
1916 ep->name, ret);
1917 goto exit_reset_config;
1918 }
1919 }
1920
1921 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1922 ep_desc = GS_SPEED_SELECT(
1923 gadget->speed == USB_SPEED_HIGH,
1924 &gs_highspeed_out_desc,
1925 &gs_fullspeed_out_desc);
1926 ret = usb_ep_enable(ep,ep_desc);
1927 if (ret == 0) {
1928 ep->driver_data = dev;
1929 dev->dev_out_ep = ep;
1930 dev->dev_out_ep_desc = ep_desc;
1931 } else {
1932 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
1933 ep->name, ret);
1934 goto exit_reset_config;
1935 }
1936 }
1937
1938 }
1939
1940 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
1941 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1942 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
1943 ret = -ENODEV;
1944 goto exit_reset_config;
1945 }
1946
1947 /* allocate and queue read requests */
1948 ep = dev->dev_out_ep;
1949 for (i=0; i<read_q_size && ret == 0; i++) {
1950 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
1951 req->complete = gs_read_complete;
1952 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1953 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
1954 ret);
1955 }
1956 } else {
1957 printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
1958 ret = -ENOMEM;
1959 goto exit_reset_config;
1960 }
1961 }
1962
1963 /* allocate write requests, and put on free list */
1964 ep = dev->dev_in_ep;
1965 for (i=0; i<write_q_size; i++) {
1966 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
1967 req_entry->re_req->complete = gs_write_complete;
1968 list_add(&req_entry->re_entry, &dev->dev_req_list);
1969 } else {
1970 printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
1971 ret = -ENOMEM;
1972 goto exit_reset_config;
1973 }
1974 }
1975
1976 printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
1977 GS_LONG_NAME,
1978 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
1979 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
1980
1981 return 0;
1982
1983exit_reset_config:
1984 gs_reset_config(dev);
1985 return ret;
1986}
1987
1988/*
1989 * gs_reset_config
1990 *
1991 * Mark the device as not configured, disable all endpoints,
1992 * which forces completion of pending I/O and frees queued
1993 * requests, and free the remaining write requests on the
1994 * free list.
1995 *
1996 * The device lock must be held when calling this function.
1997 */
1998static void gs_reset_config(struct gs_dev *dev)
1999{
2000 struct gs_req_entry *req_entry;
2001
2002 if (dev == NULL) {
2003 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
2004 return;
2005 }
2006
2007 if (dev->dev_config == GS_NO_CONFIG_ID)
2008 return;
2009
2010 dev->dev_config = GS_NO_CONFIG_ID;
2011
2012 /* free write requests on the free list */
2013 while(!list_empty(&dev->dev_req_list)) {
2014 req_entry = list_entry(dev->dev_req_list.next,
2015 struct gs_req_entry, re_entry);
2016 list_del(&req_entry->re_entry);
2017 gs_free_req_entry(dev->dev_in_ep, req_entry);
2018 }
2019
2020 /* disable endpoints, forcing completion of pending i/o; */
2021 /* completion handlers free their requests in this case */
2022 if (dev->dev_notify_ep) {
2023 usb_ep_disable(dev->dev_notify_ep);
2024 dev->dev_notify_ep = NULL;
2025 }
2026 if (dev->dev_in_ep) {
2027 usb_ep_disable(dev->dev_in_ep);
2028 dev->dev_in_ep = NULL;
2029 }
2030 if (dev->dev_out_ep) {
2031 usb_ep_disable(dev->dev_out_ep);
2032 dev->dev_out_ep = NULL;
2033 }
2034}
2035
2036/*
2037 * gs_build_config_buf
2038 *
2039 * Builds the config descriptors in the given buffer and returns the
2040 * length, or a negative error number.
2041 */
2042static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
2043 u8 type, unsigned int index, int is_otg)
2044{
2045 int len;
2046 int high_speed;
2047 const struct usb_config_descriptor *config_desc;
2048 const struct usb_descriptor_header **function;
2049
2050 if (index >= gs_device_desc.bNumConfigurations)
2051 return -EINVAL;
2052
2053 /* other speed switches high and full speed */
2054 high_speed = (speed == USB_SPEED_HIGH);
2055 if (type == USB_DT_OTHER_SPEED_CONFIG)
2056 high_speed = !high_speed;
2057
2058 if (use_acm) {
2059 config_desc = &gs_acm_config_desc;
2060 function = GS_SPEED_SELECT(high_speed,
2061 gs_acm_highspeed_function,
2062 gs_acm_fullspeed_function);
2063 } else {
2064 config_desc = &gs_bulk_config_desc;
2065 function = GS_SPEED_SELECT(high_speed,
2066 gs_bulk_highspeed_function,
2067 gs_bulk_fullspeed_function);
2068 }
2069
2070 /* for now, don't advertise srp-only devices */
2071 if (!is_otg)
2072 function++;
2073
2074 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2075 if (len < 0)
2076 return len;
2077
2078 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2079
2080 return len;
2081}
2082
2083/*
2084 * gs_alloc_req
2085 *
2086 * Allocate a usb_request and its buffer. Returns a pointer to the
2087 * usb_request or NULL if there is an error.
2088 */
David Brownell1bbc1692005-05-07 13:05:13 -07002089static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -04002090gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct usb_request *req;
2093
2094 if (ep == NULL)
2095 return NULL;
2096
2097 req = usb_ep_alloc_request(ep, kmalloc_flags);
2098
2099 if (req != NULL) {
2100 req->length = len;
2101 req->buf = kmalloc(len, kmalloc_flags);
2102 if (req->buf == NULL) {
2103 usb_ep_free_request(ep, req);
2104 return NULL;
2105 }
2106 }
2107
2108 return req;
2109}
2110
2111/*
2112 * gs_free_req
2113 *
2114 * Free a usb_request and its buffer.
2115 */
2116static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2117{
2118 if (ep != NULL && req != NULL) {
2119 kfree(req->buf);
2120 usb_ep_free_request(ep, req);
2121 }
2122}
2123
2124/*
2125 * gs_alloc_req_entry
2126 *
2127 * Allocates a request and its buffer, using the given
2128 * endpoint, buffer len, and kmalloc flags.
2129 */
David Brownell1bbc1692005-05-07 13:05:13 -07002130static struct gs_req_entry *
Al Viro55016f12005-10-21 03:21:58 -04002131gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 struct gs_req_entry *req;
2134
2135 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2136 if (req == NULL)
2137 return NULL;
2138
2139 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2140 if (req->re_req == NULL) {
2141 kfree(req);
2142 return NULL;
2143 }
2144
2145 req->re_req->context = req;
2146
2147 return req;
2148}
2149
2150/*
2151 * gs_free_req_entry
2152 *
2153 * Frees a request and its buffer.
2154 */
2155static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2156{
2157 if (ep != NULL && req != NULL) {
2158 if (req->re_req != NULL)
2159 gs_free_req(ep, req->re_req);
2160 kfree(req);
2161 }
2162}
2163
2164/*
2165 * gs_alloc_ports
2166 *
2167 * Allocate all ports and set the gs_dev struct to point to them.
2168 * Return 0 if successful, or a negative error number.
2169 *
2170 * The device lock is normally held when calling this function.
2171 */
Al Viro55016f12005-10-21 03:21:58 -04002172static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 int i;
2175 struct gs_port *port;
2176
2177 if (dev == NULL)
2178 return -EIO;
2179
2180 for (i=0; i<GS_NUM_PORTS; i++) {
Eric Sesterhenn7039f422006-02-27 13:34:10 -08002181 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -ENOMEM;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 port->port_dev = dev;
2185 port->port_num = i;
2186 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
2187 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2188 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2189 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2190 spin_lock_init(&port->port_lock);
2191 init_waitqueue_head(&port->port_write_wait);
2192
2193 dev->dev_port[i] = port;
2194 }
2195
2196 return 0;
2197}
2198
2199/*
2200 * gs_free_ports
2201 *
2202 * Free all closed ports. Open ports are disconnected by
2203 * freeing their write buffers, setting their device pointers
2204 * and the pointers to them in the device to NULL. These
2205 * ports will be freed when closed.
2206 *
2207 * The device lock is normally held when calling this function.
2208 */
2209static void gs_free_ports(struct gs_dev *dev)
2210{
2211 int i;
2212 unsigned long flags;
2213 struct gs_port *port;
2214
2215 if (dev == NULL)
2216 return;
2217
2218 for (i=0; i<GS_NUM_PORTS; i++) {
2219 if ((port=dev->dev_port[i]) != NULL) {
2220 dev->dev_port[i] = NULL;
2221
2222 spin_lock_irqsave(&port->port_lock, flags);
2223
2224 if (port->port_write_buf != NULL) {
2225 gs_buf_free(port->port_write_buf);
2226 port->port_write_buf = NULL;
2227 }
2228
2229 if (port->port_open_count > 0 || port->port_in_use) {
2230 port->port_dev = NULL;
2231 wake_up_interruptible(&port->port_write_wait);
2232 if (port->port_tty) {
2233 wake_up_interruptible(&port->port_tty->read_wait);
2234 wake_up_interruptible(&port->port_tty->write_wait);
2235 }
2236 spin_unlock_irqrestore(&port->port_lock, flags);
2237 } else {
2238 spin_unlock_irqrestore(&port->port_lock, flags);
2239 kfree(port);
2240 }
2241
2242 }
2243 }
2244}
2245
2246/* Circular Buffer */
2247
2248/*
2249 * gs_buf_alloc
2250 *
2251 * Allocate a circular buffer and all associated memory.
2252 */
Al Viro55016f12005-10-21 03:21:58 -04002253static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 struct gs_buf *gb;
2256
2257 if (size == 0)
2258 return NULL;
2259
2260 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2261 if (gb == NULL)
2262 return NULL;
2263
2264 gb->buf_buf = kmalloc(size, kmalloc_flags);
2265 if (gb->buf_buf == NULL) {
2266 kfree(gb);
2267 return NULL;
2268 }
2269
2270 gb->buf_size = size;
2271 gb->buf_get = gb->buf_put = gb->buf_buf;
2272
2273 return gb;
2274}
2275
2276/*
2277 * gs_buf_free
2278 *
2279 * Free the buffer and all associated memory.
2280 */
2281void gs_buf_free(struct gs_buf *gb)
2282{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07002283 if (gb) {
2284 kfree(gb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 kfree(gb);
2286 }
2287}
2288
2289/*
2290 * gs_buf_clear
2291 *
2292 * Clear out all data in the circular buffer.
2293 */
2294void gs_buf_clear(struct gs_buf *gb)
2295{
2296 if (gb != NULL)
2297 gb->buf_get = gb->buf_put;
2298 /* equivalent to a get of all data available */
2299}
2300
2301/*
2302 * gs_buf_data_avail
2303 *
2304 * Return the number of bytes of data available in the circular
2305 * buffer.
2306 */
2307unsigned int gs_buf_data_avail(struct gs_buf *gb)
2308{
2309 if (gb != NULL)
2310 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2311 else
2312 return 0;
2313}
2314
2315/*
2316 * gs_buf_space_avail
2317 *
2318 * Return the number of bytes of space available in the circular
2319 * buffer.
2320 */
2321unsigned int gs_buf_space_avail(struct gs_buf *gb)
2322{
2323 if (gb != NULL)
2324 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2325 else
2326 return 0;
2327}
2328
2329/*
2330 * gs_buf_put
2331 *
2332 * Copy data data from a user buffer and put it into the circular buffer.
2333 * Restrict to the amount of space available.
2334 *
2335 * Return the number of bytes copied.
2336 */
2337unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2338{
2339 unsigned int len;
2340
2341 if (gb == NULL)
2342 return 0;
2343
2344 len = gs_buf_space_avail(gb);
2345 if (count > len)
2346 count = len;
2347
2348 if (count == 0)
2349 return 0;
2350
2351 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2352 if (count > len) {
2353 memcpy(gb->buf_put, buf, len);
2354 memcpy(gb->buf_buf, buf+len, count - len);
2355 gb->buf_put = gb->buf_buf + count - len;
2356 } else {
2357 memcpy(gb->buf_put, buf, count);
2358 if (count < len)
2359 gb->buf_put += count;
2360 else /* count == len */
2361 gb->buf_put = gb->buf_buf;
2362 }
2363
2364 return count;
2365}
2366
2367/*
2368 * gs_buf_get
2369 *
2370 * Get data from the circular buffer and copy to the given buffer.
2371 * Restrict to the amount of data available.
2372 *
2373 * Return the number of bytes copied.
2374 */
2375unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2376{
2377 unsigned int len;
2378
2379 if (gb == NULL)
2380 return 0;
2381
2382 len = gs_buf_data_avail(gb);
2383 if (count > len)
2384 count = len;
2385
2386 if (count == 0)
2387 return 0;
2388
2389 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2390 if (count > len) {
2391 memcpy(buf, gb->buf_get, len);
2392 memcpy(buf+len, gb->buf_buf, count - len);
2393 gb->buf_get = gb->buf_buf + count - len;
2394 } else {
2395 memcpy(buf, gb->buf_get, count);
2396 if (count < len)
2397 gb->buf_get += count;
2398 else /* count == len */
2399 gb->buf_get = gb->buf_buf;
2400 }
2401
2402 return count;
2403}