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