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