serial.c 58.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * g_serial.c -- USB gadget serial driver
 *
 * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
 *
 * This code is based in part on the Gadget Zero driver, which
 * is Copyright (C) 2003 by David Brownell, all rights reserved.
 *
 * This code also borrows from usbserial.c, which is
 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
 *
 * This software is distributed under the terms of the GNU General
 * Public License ("GPL") as published by the Free Software Foundation,
 * either version 2 of that License or (at your option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>

26
#include <linux/usb/ch9.h>
27
#include <linux/usb/cdc.h>
28
#include <linux/usb/gadget.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34

#include "gadget_chips.h"


/* Defines */

35 36
#define GS_VERSION_STR			"v2.2"
#define GS_VERSION_NUM			0x0202
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#define GS_LONG_NAME			"Gadget Serial"
#define GS_SHORT_NAME			"g_serial"

#define GS_MAJOR			127
#define GS_MINOR_START			0

#define GS_NUM_PORTS			16

#define GS_NUM_CONFIGS			1
#define GS_NO_CONFIG_ID			0
#define GS_BULK_CONFIG_ID		1
#define GS_ACM_CONFIG_ID		2

#define GS_MAX_NUM_INTERFACES		2
#define GS_BULK_INTERFACE_ID		0
#define GS_CONTROL_INTERFACE_ID		0
#define GS_DATA_INTERFACE_ID		1

#define GS_MAX_DESC_LEN			256

#define GS_DEFAULT_READ_Q_SIZE		32
#define GS_DEFAULT_WRITE_Q_SIZE		32

#define GS_DEFAULT_WRITE_BUF_SIZE	8192
#define GS_TMP_BUF_SIZE			8192

#define GS_CLOSE_TIMEOUT		15

#define GS_DEFAULT_USE_ACM		0

#define GS_DEFAULT_DTE_RATE		9600
#define GS_DEFAULT_DATA_BITS		8
#define GS_DEFAULT_PARITY		USB_CDC_NO_PARITY
#define GS_DEFAULT_CHAR_FORMAT		USB_CDC_1_STOP_BITS

73 74 75 76 77 78 79 80 81 82
/* maxpacket and other transfer characteristics vary by speed. */
static inline struct usb_endpoint_descriptor *
choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
		struct usb_endpoint_descriptor *fs)
{
	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
		return hs;
	return fs;
}

L
Linus Torvalds 已提交
83 84

/* debug settings */
85
#ifdef DEBUG
L
Linus Torvalds 已提交
86
static int debug = 1;
87 88 89
#else
#define	debug 0
#endif
L
Linus Torvalds 已提交
90 91

#define gs_debug(format, arg...) \
92
	do { if (debug) pr_debug(format, ## arg); } while (0)
L
Linus Torvalds 已提交
93
#define gs_debug_level(level, format, arg...) \
94
	do { if (debug >= level) pr_debug(format, ## arg); } while (0)
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


/* Thanks to NetChip Technologies for donating this product ID.
 *
 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 * Instead:  allocate your own, using normal USB-IF procedures.
 */
#define GS_VENDOR_ID			0x0525	/* NetChip */
#define GS_PRODUCT_ID			0xa4a6	/* Linux-USB Serial Gadget */
#define GS_CDC_PRODUCT_ID		0xa4a7	/* ... as CDC-ACM */

#define GS_LOG2_NOTIFY_INTERVAL		5	/* 1 << 5 == 32 msec */
#define GS_NOTIFY_MAXPACKET		8


/* Structures */

struct gs_dev;

/* circular buffer */
struct gs_buf {
	unsigned int		buf_size;
	char			*buf_buf;
	char			*buf_get;
	char			*buf_put;
};

/* list of requests */
struct gs_req_entry {
	struct list_head	re_entry;
	struct usb_request	*re_req;
};

/* the port structure holds info for each port, one for each minor number */
struct gs_port {
130
	struct gs_dev		*port_dev;	/* pointer to device struct */
L
Linus Torvalds 已提交
131 132
	struct tty_struct	*port_tty;	/* pointer to tty struct */
	spinlock_t		port_lock;
133
	int			port_num;
L
Linus Torvalds 已提交
134 135 136 137
	int			port_open_count;
	int			port_in_use;	/* open/close in progress */
	wait_queue_head_t	port_write_wait;/* waiting to write */
	struct gs_buf		*port_write_buf;
138 139 140 141
	struct usb_cdc_line_coding port_line_coding;	/* 8-N-1 etc */
	u16			port_handshake_bits;
#define RS232_RTS	(1 << 1)
#define RS232_DTE	(1 << 0)
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151
};

/* the device structure holds info for the USB device */
struct gs_dev {
	struct usb_gadget	*dev_gadget;	/* gadget device pointer */
	spinlock_t		dev_lock;	/* lock for set/reset config */
	int			dev_config;	/* configuration number */
	struct usb_ep		*dev_notify_ep;	/* address of notify endpoint */
	struct usb_ep		*dev_in_ep;	/* address of in endpoint */
	struct usb_ep		*dev_out_ep;	/* address of out endpoint */
152
	struct usb_endpoint_descriptor		/* descriptor of notify ep */
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
				*dev_notify_ep_desc;
	struct usb_endpoint_descriptor		/* descriptor of in endpoint */
				*dev_in_ep_desc;
	struct usb_endpoint_descriptor		/* descriptor of out endpoint */
				*dev_out_ep_desc;
	struct usb_request	*dev_ctrl_req;	/* control request */
	struct list_head	dev_req_list;	/* list of write requests */
	int			dev_sched_port;	/* round robin port scheduled */
	struct gs_port		*dev_port[GS_NUM_PORTS]; /* the ports */
};


/* Functions */

/* module */
static int __init gs_module_init(void);
static void __exit gs_module_exit(void);

/* tty driver */
static int gs_open(struct tty_struct *tty, struct file *file);
static void gs_close(struct tty_struct *tty, struct file *file);
174
static int gs_write(struct tty_struct *tty,
L
Linus Torvalds 已提交
175
	const unsigned char *buf, int count);
176
static int gs_put_char(struct tty_struct *tty, unsigned char ch);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184
static void gs_flush_chars(struct tty_struct *tty);
static int gs_write_room(struct tty_struct *tty);
static int gs_chars_in_buffer(struct tty_struct *tty);
static void gs_throttle(struct tty_struct * tty);
static void gs_unthrottle(struct tty_struct * tty);
static void gs_break(struct tty_struct *tty, int break_state);
static int  gs_ioctl(struct tty_struct *tty, struct file *file,
	unsigned int cmd, unsigned long arg);
A
Alan Cox 已提交
185
static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

static int gs_send(struct gs_dev *dev);
static int gs_send_packet(struct gs_dev *dev, char *packet,
	unsigned int size);
static int gs_recv_packet(struct gs_dev *dev, char *packet,
	unsigned int size);
static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);

/* gadget driver */
static int gs_bind(struct usb_gadget *gadget);
static void gs_unbind(struct usb_gadget *gadget);
static int gs_setup(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl);
static int gs_setup_standard(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl);
static int gs_setup_class(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl);
static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
205 206
static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
	struct usb_request *req);
L
Linus Torvalds 已提交
207 208 209
static void gs_disconnect(struct usb_gadget *gadget);
static int gs_set_config(struct gs_dev *dev, unsigned config);
static void gs_reset_config(struct gs_dev *dev);
210
static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
L
Linus Torvalds 已提交
211 212 213
		u8 type, unsigned int index, int is_otg);

static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
A
Al Viro 已提交
214
	gfp_t kmalloc_flags);
L
Linus Torvalds 已提交
215 216 217
static void gs_free_req(struct usb_ep *ep, struct usb_request *req);

static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
A
Al Viro 已提交
218
	gfp_t kmalloc_flags);
L
Linus Torvalds 已提交
219 220
static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);

A
Al Viro 已提交
221
static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
L
Linus Torvalds 已提交
222 223 224
static void gs_free_ports(struct gs_dev *dev);

/* circular buffer */
A
Al Viro 已提交
225
static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
static void gs_buf_free(struct gs_buf *gb);
static void gs_buf_clear(struct gs_buf *gb);
static unsigned int gs_buf_data_avail(struct gs_buf *gb);
static unsigned int gs_buf_space_avail(struct gs_buf *gb);
static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
	unsigned int count);
static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
	unsigned int count);

/* external functions */
extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);


/* Globals */

static struct gs_dev *gs_device;

static const char *EP_IN_NAME;
static const char *EP_OUT_NAME;
static const char *EP_NOTIFY_NAME;

247
static struct mutex gs_open_close_lock[GS_NUM_PORTS];
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257

static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;

static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;

static unsigned int use_acm = GS_DEFAULT_USE_ACM;


/* tty driver struct */
J
Jeff Dike 已提交
258
static const struct tty_operations gs_tty_ops = {
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	.open =			gs_open,
	.close =		gs_close,
	.write =		gs_write,
	.put_char =		gs_put_char,
	.flush_chars =		gs_flush_chars,
	.write_room =		gs_write_room,
	.ioctl =		gs_ioctl,
	.set_termios =		gs_set_termios,
	.throttle =		gs_throttle,
	.unthrottle =		gs_unthrottle,
	.break_ctl =		gs_break,
	.chars_in_buffer =	gs_chars_in_buffer,
};
static struct tty_driver *gs_tty_driver;

/* gadget driver struct */
static struct usb_gadget_driver gs_gadget_driver = {
#ifdef CONFIG_USB_GADGET_DUALSPEED
	.speed =		USB_SPEED_HIGH,
#else
	.speed =		USB_SPEED_FULL,
#endif /* CONFIG_USB_GADGET_DUALSPEED */
	.function =		GS_LONG_NAME,
	.bind =			gs_bind,
283
	.unbind =		gs_unbind,
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	.setup =		gs_setup,
	.disconnect =		gs_disconnect,
	.driver = {
		.name =		GS_SHORT_NAME,
	},
};


/* USB descriptors */

#define GS_MANUFACTURER_STR_ID	1
#define GS_PRODUCT_STR_ID	2
#define GS_SERIAL_STR_ID	3
#define GS_BULK_CONFIG_STR_ID	4
#define GS_ACM_CONFIG_STR_ID	5
#define GS_CONTROL_STR_ID	6
#define GS_DATA_STR_ID		7

/* static strings, in UTF-8 */
static char manufacturer[50];
static struct usb_string gs_strings[] = {
	{ GS_MANUFACTURER_STR_ID, manufacturer },
	{ GS_PRODUCT_STR_ID, GS_LONG_NAME },
	{ GS_SERIAL_STR_ID, "0" },
	{ GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
	{ GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
	{ GS_CONTROL_STR_ID, "Gadget Serial Control" },
	{ GS_DATA_STR_ID, "Gadget Serial Data" },
	{  } /* end of list */
};

static struct usb_gadget_strings gs_string_table = {
	.language =		0x0409,	/* en-us */
	.strings =		gs_strings,
};

static struct usb_device_descriptor gs_device_desc = {
	.bLength =		USB_DT_DEVICE_SIZE,
	.bDescriptorType =	USB_DT_DEVICE,
	.bcdUSB =		__constant_cpu_to_le16(0x0200),
	.bDeviceSubClass =	0,
	.bDeviceProtocol =	0,
	.idVendor =		__constant_cpu_to_le16(GS_VENDOR_ID),
	.idProduct =		__constant_cpu_to_le16(GS_PRODUCT_ID),
	.iManufacturer =	GS_MANUFACTURER_STR_ID,
	.iProduct =		GS_PRODUCT_STR_ID,
	.iSerialNumber =	GS_SERIAL_STR_ID,
	.bNumConfigurations =	GS_NUM_CONFIGS,
};

static struct usb_otg_descriptor gs_otg_descriptor = {
	.bLength =		sizeof(gs_otg_descriptor),
	.bDescriptorType =	USB_DT_OTG,
	.bmAttributes =		USB_OTG_SRP,
};

static struct usb_config_descriptor gs_bulk_config_desc = {
	.bLength =		USB_DT_CONFIG_SIZE,
	.bDescriptorType =	USB_DT_CONFIG,
	/* .wTotalLength computed dynamically */
	.bNumInterfaces =	1,
	.bConfigurationValue =	GS_BULK_CONFIG_ID,
	.iConfiguration =	GS_BULK_CONFIG_STR_ID,
	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
	.bMaxPower =		1,
};

static struct usb_config_descriptor gs_acm_config_desc = {
	.bLength =		USB_DT_CONFIG_SIZE,
	.bDescriptorType =	USB_DT_CONFIG,
	/* .wTotalLength computed dynamically */
	.bNumInterfaces =	2,
	.bConfigurationValue =	GS_ACM_CONFIG_ID,
	.iConfiguration =	GS_ACM_CONFIG_STR_ID,
	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
	.bMaxPower =		1,
};

static const struct usb_interface_descriptor gs_bulk_interface_desc = {
	.bLength =		USB_DT_INTERFACE_SIZE,
	.bDescriptorType =	USB_DT_INTERFACE,
	.bInterfaceNumber =	GS_BULK_INTERFACE_ID,
	.bNumEndpoints =	2,
	.bInterfaceClass =	USB_CLASS_CDC_DATA,
	.bInterfaceSubClass =	0,
	.bInterfaceProtocol =	0,
	.iInterface =		GS_DATA_STR_ID,
};

static const struct usb_interface_descriptor gs_control_interface_desc = {
	.bLength =		USB_DT_INTERFACE_SIZE,
	.bDescriptorType =	USB_DT_INTERFACE,
	.bInterfaceNumber =	GS_CONTROL_INTERFACE_ID,
	.bNumEndpoints =	1,
	.bInterfaceClass =	USB_CLASS_COMM,
	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ACM,
	.bInterfaceProtocol =	USB_CDC_ACM_PROTO_AT_V25TER,
	.iInterface =		GS_CONTROL_STR_ID,
};

static const struct usb_interface_descriptor gs_data_interface_desc = {
	.bLength =		USB_DT_INTERFACE_SIZE,
	.bDescriptorType =	USB_DT_INTERFACE,
	.bInterfaceNumber =	GS_DATA_INTERFACE_ID,
	.bNumEndpoints =	2,
	.bInterfaceClass =	USB_CLASS_CDC_DATA,
	.bInterfaceSubClass =	0,
	.bInterfaceProtocol =	0,
	.iInterface =		GS_DATA_STR_ID,
};

static const struct usb_cdc_header_desc gs_header_desc = {
	.bLength =		sizeof(gs_header_desc),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
	.bcdCDC =		__constant_cpu_to_le16(0x0110),
};

static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
403 404 405 406 407
	.bLength =		sizeof(gs_call_mgmt_descriptor),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
	.bmCapabilities =	0,
	.bDataInterface =	1,	/* index of data interface */
L
Linus Torvalds 已提交
408 409 410
};

static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
411 412 413
	.bLength =		sizeof(gs_acm_descriptor),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
414
	.bmCapabilities =	(1 << 1),
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423
};

static const struct usb_cdc_union_desc gs_union_desc = {
	.bLength =		sizeof(gs_union_desc),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
	.bMasterInterface0 =	0,	/* index of control interface */
	.bSlaveInterface0 =	1,	/* index of data interface */
};
424

L
Linus Torvalds 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_INT,
	.wMaxPacketSize =	__constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
	.bInterval =		1 << GS_LOG2_NOTIFY_INTERVAL,
};

static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bEndpointAddress =	USB_DIR_OUT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
	(struct usb_descriptor_header *) &gs_otg_descriptor,
	(struct usb_descriptor_header *) &gs_bulk_interface_desc,
	(struct usb_descriptor_header *) &gs_fullspeed_in_desc,
	(struct usb_descriptor_header *) &gs_fullspeed_out_desc,
	NULL,
};

static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
	(struct usb_descriptor_header *) &gs_otg_descriptor,
	(struct usb_descriptor_header *) &gs_control_interface_desc,
	(struct usb_descriptor_header *) &gs_header_desc,
	(struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
	(struct usb_descriptor_header *) &gs_acm_descriptor,
	(struct usb_descriptor_header *) &gs_union_desc,
	(struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
	(struct usb_descriptor_header *) &gs_data_interface_desc,
	(struct usb_descriptor_header *) &gs_fullspeed_in_desc,
	(struct usb_descriptor_header *) &gs_fullspeed_out_desc,
	NULL,
};

static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_INT,
	.wMaxPacketSize =	__constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
	.bInterval =		GS_LOG2_NOTIFY_INTERVAL+4,
};

static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
	.wMaxPacketSize =	__constant_cpu_to_le16(512),
};

static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
	.wMaxPacketSize =	__constant_cpu_to_le16(512),
};

static struct usb_qualifier_descriptor gs_qualifier_desc = {
	.bLength =		sizeof(struct usb_qualifier_descriptor),
	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
	.bcdUSB =		__constant_cpu_to_le16 (0x0200),
	/* assumes ep0 uses the same value for both speeds ... */
	.bNumConfigurations =	GS_NUM_CONFIGS,
};

static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
	(struct usb_descriptor_header *) &gs_otg_descriptor,
	(struct usb_descriptor_header *) &gs_bulk_interface_desc,
	(struct usb_descriptor_header *) &gs_highspeed_in_desc,
	(struct usb_descriptor_header *) &gs_highspeed_out_desc,
	NULL,
};

static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
	(struct usb_descriptor_header *) &gs_otg_descriptor,
	(struct usb_descriptor_header *) &gs_control_interface_desc,
	(struct usb_descriptor_header *) &gs_header_desc,
	(struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
	(struct usb_descriptor_header *) &gs_acm_descriptor,
	(struct usb_descriptor_header *) &gs_union_desc,
	(struct usb_descriptor_header *) &gs_highspeed_notify_desc,
	(struct usb_descriptor_header *) &gs_data_interface_desc,
	(struct usb_descriptor_header *) &gs_highspeed_in_desc,
	(struct usb_descriptor_header *) &gs_highspeed_out_desc,
	NULL,
};


/* Module */
MODULE_DESCRIPTION(GS_LONG_NAME);
MODULE_AUTHOR("Al Borchers");
MODULE_LICENSE("GPL");

529
#ifdef DEBUG
L
Linus Torvalds 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
module_param(debug, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
#endif

module_param(read_q_size, uint, S_IRUGO);
MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");

module_param(write_q_size, uint, S_IRUGO);
MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");

module_param(write_buf_size, uint, S_IRUGO);
MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");

module_param(use_acm, uint, S_IRUGO);
MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");

module_init(gs_module_init);
module_exit(gs_module_exit);

/*
*  gs_module_init
*
*  Register as a USB gadget driver and a tty driver.
*/
static int __init gs_module_init(void)
{
	int i;
	int retval;

	retval = usb_gadget_register_driver(&gs_gadget_driver);
	if (retval) {
561 562
		pr_err("gs_module_init: cannot register gadget driver, "
			"ret=%d\n", retval);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575
		return retval;
	}

	gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
	if (!gs_tty_driver)
		return -ENOMEM;
	gs_tty_driver->owner = THIS_MODULE;
	gs_tty_driver->driver_name = GS_SHORT_NAME;
	gs_tty_driver->name = "ttygs";
	gs_tty_driver->major = GS_MAJOR;
	gs_tty_driver->minor_start = GS_MINOR_START;
	gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
	gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
576
	gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
L
Linus Torvalds 已提交
577 578 579 580 581
	gs_tty_driver->init_termios = tty_std_termios;
	gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	tty_set_operations(gs_tty_driver, &gs_tty_ops);

	for (i=0; i < GS_NUM_PORTS; i++)
582
		mutex_init(&gs_open_close_lock[i]);
L
Linus Torvalds 已提交
583 584 585 586 587

	retval = tty_register_driver(gs_tty_driver);
	if (retval) {
		usb_gadget_unregister_driver(&gs_gadget_driver);
		put_tty_driver(gs_tty_driver);
588 589
		pr_err("gs_module_init: cannot register tty driver, "
				"ret=%d\n", retval);
L
Linus Torvalds 已提交
590 591 592
		return retval;
	}

593 594
	pr_info("gs_module_init: %s %s loaded\n",
			GS_LONG_NAME, GS_VERSION_STR);
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608
	return 0;
}

/*
* gs_module_exit
*
* Unregister as a tty driver and a USB gadget driver.
*/
static void __exit gs_module_exit(void)
{
	tty_unregister_driver(gs_tty_driver);
	put_tty_driver(gs_tty_driver);
	usb_gadget_unregister_driver(&gs_gadget_driver);

609 610
	pr_info("gs_module_exit: %s %s unloaded\n",
			GS_LONG_NAME, GS_VERSION_STR);
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624
}

/* TTY Driver */

/*
 * gs_open
 */
static int gs_open(struct tty_struct *tty, struct file *file)
{
	int port_num;
	unsigned long flags;
	struct gs_port *port;
	struct gs_dev *dev;
	struct gs_buf *buf;
625
	struct mutex *mtx;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
	int ret;

	port_num = tty->index;

	gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);

	if (port_num < 0 || port_num >= GS_NUM_PORTS) {
633
		pr_err("gs_open: (%d,%p,%p) invalid port number\n",
L
Linus Torvalds 已提交
634 635 636 637 638 639 640
			port_num, tty, file);
		return -ENODEV;
	}

	dev = gs_device;

	if (dev == NULL) {
641
		pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
L
Linus Torvalds 已提交
642 643 644 645
			port_num, tty, file);
		return -ENODEV;
	}

646 647
	mtx = &gs_open_close_lock[port_num];
	if (mutex_lock_interruptible(mtx)) {
648
		pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
L
Linus Torvalds 已提交
649 650 651 652 653 654 655
			port_num, tty, file);
		return -ERESTARTSYS;
	}

	spin_lock_irqsave(&dev->dev_lock, flags);

	if (dev->dev_config == GS_NO_CONFIG_ID) {
656
		pr_err("gs_open: (%d,%p,%p) device is not connected\n",
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664
			port_num, tty, file);
		ret = -ENODEV;
		goto exit_unlock_dev;
	}

	port = dev->dev_port[port_num];

	if (port == NULL) {
665
		pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674
			port_num, tty, file);
		ret = -ENODEV;
		goto exit_unlock_dev;
	}

	spin_lock(&port->port_lock);
	spin_unlock(&dev->dev_lock);

	if (port->port_dev == NULL) {
675
		pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
			port_num, tty, file);
		ret = -EIO;
		goto exit_unlock_port;
	}

	if (port->port_open_count > 0) {
		++port->port_open_count;
		gs_debug("gs_open: (%d,%p,%p) already open\n",
			port_num, tty, file);
		ret = 0;
		goto exit_unlock_port;
	}

	tty->driver_data = NULL;

	/* mark port as in use, we can drop port lock and sleep if necessary */
	port->port_in_use = 1;

	/* allocate write buffer on first open */
	if (port->port_write_buf == NULL) {
		spin_unlock_irqrestore(&port->port_lock, flags);
		buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
		spin_lock_irqsave(&port->port_lock, flags);

		/* might have been disconnected while asleep, check */
		if (port->port_dev == NULL) {
702
			pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
L
Linus Torvalds 已提交
703 704 705 706 707 708 709
				port_num, tty, file);
			port->port_in_use = 0;
			ret = -EIO;
			goto exit_unlock_port;
		}

		if ((port->port_write_buf=buf) == NULL) {
710 711
			pr_err("gs_open: (%d,%p,%p) cannot allocate "
				"port write buffer\n",
L
Linus Torvalds 已提交
712 713 714 715 716 717 718 719 720 721 722 723
				port_num, tty, file);
			port->port_in_use = 0;
			ret = -ENOMEM;
			goto exit_unlock_port;
		}

	}

	/* wait for carrier detect (not implemented) */

	/* might have been disconnected while asleep, check */
	if (port->port_dev == NULL) {
724
		pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
			port_num, tty, file);
		port->port_in_use = 0;
		ret = -EIO;
		goto exit_unlock_port;
	}

	tty->driver_data = port;
	port->port_tty = tty;
	port->port_open_count = 1;
	port->port_in_use = 0;

	gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);

	ret = 0;

exit_unlock_port:
	spin_unlock_irqrestore(&port->port_lock, flags);
742
	mutex_unlock(mtx);
L
Linus Torvalds 已提交
743 744 745 746
	return ret;

exit_unlock_dev:
	spin_unlock_irqrestore(&dev->dev_lock, flags);
747
	mutex_unlock(mtx);
L
Linus Torvalds 已提交
748 749 750 751 752 753 754
	return ret;

}

/*
 * gs_close
 */
755 756 757 758 759

#define GS_WRITE_FINISHED_EVENT_SAFELY(p)			\
({								\
	int cond;						\
								\
760
	spin_lock_irq(&(p)->port_lock);				\
761
	cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
762
	spin_unlock_irq(&(p)->port_lock);			\
763 764 765
	cond;							\
})

L
Linus Torvalds 已提交
766 767 768
static void gs_close(struct tty_struct *tty, struct file *file)
{
	struct gs_port *port = tty->driver_data;
769
	struct mutex *mtx;
L
Linus Torvalds 已提交
770 771

	if (port == NULL) {
772
		pr_err("gs_close: NULL port pointer\n");
L
Linus Torvalds 已提交
773 774 775 776 777
		return;
	}

	gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);

778 779
	mtx = &gs_open_close_lock[port->port_num];
	mutex_lock(mtx);
L
Linus Torvalds 已提交
780

781
	spin_lock_irq(&port->port_lock);
L
Linus Torvalds 已提交
782 783

	if (port->port_open_count == 0) {
784
		pr_err("gs_close: (%d,%p,%p) port is already closed\n",
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
			port->port_num, tty, file);
		goto exit;
	}

	if (port->port_open_count > 1) {
		--port->port_open_count;
		goto exit;
	}

	/* free disconnected port on final close */
	if (port->port_dev == NULL) {
		kfree(port);
		goto exit;
	}

	/* mark port as closed but in use, we can drop port lock */
	/* and sleep if necessary */
	port->port_in_use = 1;
	port->port_open_count = 0;

	/* wait for write buffer to drain, or */
	/* at most GS_CLOSE_TIMEOUT seconds */
	if (gs_buf_data_avail(port->port_write_buf) > 0) {
808
		spin_unlock_irq(&port->port_lock);
809 810 811
		wait_event_interruptible_timeout(port->port_write_wait,
					GS_WRITE_FINISHED_EVENT_SAFELY(port),
					GS_CLOSE_TIMEOUT * HZ);
812
		spin_lock_irq(&port->port_lock);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	}

	/* free disconnected port on final close */
	/* (might have happened during the above sleep) */
	if (port->port_dev == NULL) {
		kfree(port);
		goto exit;
	}

	gs_buf_clear(port->port_write_buf);

	tty->driver_data = NULL;
	port->port_tty = NULL;
	port->port_in_use = 0;

	gs_debug("gs_close: (%d,%p,%p) completed\n",
		port->port_num, tty, file);

exit:
832
	spin_unlock_irq(&port->port_lock);
833
	mutex_unlock(mtx);
L
Linus Torvalds 已提交
834 835 836 837 838 839 840 841 842 843 844 845
}

/*
 * gs_write
 */
static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
	unsigned long flags;
	struct gs_port *port = tty->driver_data;
	int ret;

	if (port == NULL) {
846
		pr_err("gs_write: NULL port pointer\n");
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858
		return -EIO;
	}

	gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
		count);

	if (count == 0)
		return 0;

	spin_lock_irqsave(&port->port_lock, flags);

	if (port->port_dev == NULL) {
859
		pr_err("gs_write: (%d,%p) port is not connected\n",
L
Linus Torvalds 已提交
860 861 862 863 864 865
			port->port_num, tty);
		ret = -EIO;
		goto exit;
	}

	if (port->port_open_count == 0) {
866
		pr_err("gs_write: (%d,%p) port is closed\n",
L
Linus Torvalds 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
			port->port_num, tty);
		ret = -EBADF;
		goto exit;
	}

	count = gs_buf_put(port->port_write_buf, buf, count);

	spin_unlock_irqrestore(&port->port_lock, flags);

	gs_send(gs_device);

	gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
		count);

	return count;

exit:
	spin_unlock_irqrestore(&port->port_lock, flags);
	return ret;
}

/*
 * gs_put_char
 */
891
static int gs_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
892 893 894
{
	unsigned long flags;
	struct gs_port *port = tty->driver_data;
895
	int ret = 0;
L
Linus Torvalds 已提交
896 897

	if (port == NULL) {
898
		pr_err("gs_put_char: NULL port pointer\n");
899
		return 0;
L
Linus Torvalds 已提交
900 901
	}

902 903
	gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
		port->port_num, tty, ch, __builtin_return_address(0));
L
Linus Torvalds 已提交
904 905 906 907

	spin_lock_irqsave(&port->port_lock, flags);

	if (port->port_dev == NULL) {
908
		pr_err("gs_put_char: (%d,%p) port is not connected\n",
L
Linus Torvalds 已提交
909 910 911 912 913
			port->port_num, tty);
		goto exit;
	}

	if (port->port_open_count == 0) {
914
		pr_err("gs_put_char: (%d,%p) port is closed\n",
L
Linus Torvalds 已提交
915 916 917 918
			port->port_num, tty);
		goto exit;
	}

919
	ret = gs_buf_put(port->port_write_buf, &ch, 1);
L
Linus Torvalds 已提交
920 921 922

exit:
	spin_unlock_irqrestore(&port->port_lock, flags);
923
	return ret;
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934
}

/*
 * gs_flush_chars
 */
static void gs_flush_chars(struct tty_struct *tty)
{
	unsigned long flags;
	struct gs_port *port = tty->driver_data;

	if (port == NULL) {
935
		pr_err("gs_flush_chars: NULL port pointer\n");
L
Linus Torvalds 已提交
936 937 938 939 940 941 942 943
		return;
	}

	gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);

	spin_lock_irqsave(&port->port_lock, flags);

	if (port->port_dev == NULL) {
944
		pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
L
Linus Torvalds 已提交
945 946 947 948 949
			port->port_num, tty);
		goto exit;
	}

	if (port->port_open_count == 0) {
950
		pr_err("gs_flush_chars: (%d,%p) port is closed\n",
L
Linus Torvalds 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
			port->port_num, tty);
		goto exit;
	}

	spin_unlock_irqrestore(&port->port_lock, flags);

	gs_send(gs_device);

	return;

exit:
	spin_unlock_irqrestore(&port->port_lock, flags);
}

/*
 * gs_write_room
 */
static int gs_write_room(struct tty_struct *tty)
{

	int room = 0;
	unsigned long flags;
	struct gs_port *port = tty->driver_data;


	if (port == NULL)
		return 0;

	spin_lock_irqsave(&port->port_lock, flags);

	if (port->port_dev != NULL && port->port_open_count > 0
	&& port->port_write_buf != NULL)
		room = gs_buf_space_avail(port->port_write_buf);

	spin_unlock_irqrestore(&port->port_lock, flags);

	gs_debug("gs_write_room: (%d,%p) room=%d\n",
		port->port_num, tty, room);

	return room;
}

/*
 * gs_chars_in_buffer
 */
static int gs_chars_in_buffer(struct tty_struct *tty)
{
	int chars = 0;
	unsigned long flags;
	struct gs_port *port = tty->driver_data;

	if (port == NULL)
		return 0;

	spin_lock_irqsave(&port->port_lock, flags);

	if (port->port_dev != NULL && port->port_open_count > 0
	&& port->port_write_buf != NULL)
		chars = gs_buf_data_avail(port->port_write_buf);

	spin_unlock_irqrestore(&port->port_lock, flags);

	gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
		port->port_num, tty, chars);

	return chars;
}

/*
 * gs_throttle
 */
static void gs_throttle(struct tty_struct *tty)
{
}

/*
 * gs_unthrottle
 */
static void gs_unthrottle(struct tty_struct *tty)
{
}

/*
 * gs_break
 */
static void gs_break(struct tty_struct *tty, int break_state)
{
}

/*
 * gs_ioctl
 */
static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct gs_port *port = tty->driver_data;

	if (port == NULL) {
1048
		pr_err("gs_ioctl: NULL port pointer\n");
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
		return -EIO;
	}

	gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
		port->port_num, tty, file, cmd, arg);

	/* handle ioctls */

	/* could not handle ioctl */
	return -ENOIOCTLCMD;
}

/*
 * gs_set_termios
 */
A
Alan Cox 已提交
1064
static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
{
}

/*
* gs_send
*
* This function finds available write requests, calls
* gs_send_packet to fill these packets with data, and
* continues until either there are no more write requests
* available or no more data to send.  This function is
* run whenever data arrives or write requests are available.
*/
static int gs_send(struct gs_dev *dev)
{
	int ret,len;
	unsigned long flags;
	struct usb_ep *ep;
	struct usb_request *req;
	struct gs_req_entry *req_entry;

	if (dev == NULL) {
1086
		pr_err("gs_send: NULL device pointer\n");
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		return -ENODEV;
	}

	spin_lock_irqsave(&dev->dev_lock, flags);

	ep = dev->dev_in_ep;

	while(!list_empty(&dev->dev_req_list)) {

		req_entry = list_entry(dev->dev_req_list.next,
			struct gs_req_entry, re_entry);

		req = req_entry->re_req;

		len = gs_send_packet(dev, req->buf, ep->maxpacket);

		if (len > 0) {
1104 1105 1106 1107 1108
			gs_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));
L
Linus Torvalds 已提交
1109 1110
			list_del(&req_entry->re_entry);
			req->length = len;
1111
			spin_unlock_irqrestore(&dev->dev_lock, flags);
L
Linus Torvalds 已提交
1112
			if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1113
				pr_err(
L
Linus Torvalds 已提交
1114 1115
				"gs_send: cannot queue read request, ret=%d\n",
					ret);
1116
				spin_lock_irqsave(&dev->dev_lock, flags);
L
Linus Torvalds 已提交
1117 1118
				break;
			}
1119
			spin_lock_irqsave(&dev->dev_lock, flags);
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
		} else {
			break;
		}

	}

	spin_unlock_irqrestore(&dev->dev_lock, flags);

	return 0;
}

/*
 * gs_send_packet
 *
 * If there is data to send, a packet is built in the given
 * buffer and the size is returned.  If there is no data to
 * send, 0 is returned.  If there is any error a negative
 * error number is returned.
 *
 * Called during USB completion routine, on interrupt time.
 *
 * We assume that disconnect will not happen until all completion
 * routines have completed, so we can assume that the dev_port
 * array does not change during the lifetime of this function.
 */
static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
{
	unsigned int len;
	struct gs_port *port;

	/* TEMPORARY -- only port 0 is supported right now */
	port = dev->dev_port[0];

	if (port == NULL) {
1154
		pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
		return -EIO;
	}

	spin_lock(&port->port_lock);

	len = gs_buf_data_avail(port->port_write_buf);
	if (len < size)
		size = len;

	if (size == 0)
		goto exit;

	size = gs_buf_get(port->port_write_buf, packet, size);

	if (port->port_tty)
		wake_up_interruptible(&port->port_tty->write_wait);

exit:
	spin_unlock(&port->port_lock);
	return size;
}

/*
 * gs_recv_packet
 *
 * Called for each USB packet received.  Reads the packet
 * header and stuffs the data in the appropriate tty buffer.
 * Returns 0 if successful, or a negative error number.
 *
 * Called during USB completion routine, on interrupt time.
 *
 * We assume that disconnect will not happen until all completion
 * routines have completed, so we can assume that the dev_port
 * array does not change during the lifetime of this function.
 */
static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
{
	unsigned int len;
	struct gs_port *port;
	int ret;
A
Alan Cox 已提交
1195
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1196 1197 1198 1199 1200

	/* TEMPORARY -- only port 0 is supported right now */
	port = dev->dev_port[0];

	if (port == NULL) {
1201
		pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206 1207 1208
			port->port_num);
		return -EIO;
	}

	spin_lock(&port->port_lock);

	if (port->port_open_count == 0) {
1209
		pr_err("gs_recv_packet: port=%d, port is closed\n",
L
Linus Torvalds 已提交
1210 1211 1212 1213 1214
			port->port_num);
		ret = -EIO;
		goto exit;
	}

A
Alan Cox 已提交
1215 1216 1217 1218

	tty = port->port_tty;

	if (tty == NULL) {
1219
		pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
L
Linus Torvalds 已提交
1220 1221 1222 1223 1224 1225
			port->port_num);
		ret = -EIO;
		goto exit;
	}

	if (port->port_tty->magic != TTY_MAGIC) {
1226
		pr_err("gs_recv_packet: port=%d, bad tty magic\n",
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231
			port->port_num);
		ret = -EIO;
		goto exit;
	}

A
Alan Cox 已提交
1232 1233 1234
	len = tty_buffer_request_room(tty, size);
	if (len > 0) {
		tty_insert_flip_string(tty, packet, len);
L
Linus Torvalds 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		tty_flip_buffer_push(port->port_tty);
		wake_up_interruptible(&port->port_tty->read_wait);
	}
	ret = 0;
exit:
	spin_unlock(&port->port_lock);
	return ret;
}

/*
* gs_read_complete
*/
static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
{
	int ret;
	struct gs_dev *dev = ep->driver_data;

	if (dev == NULL) {
1253
		pr_err("gs_read_complete: NULL device pointer\n");
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258
		return;
	}

	switch(req->status) {
	case 0:
1259
		/* normal completion */
L
Linus Torvalds 已提交
1260 1261 1262 1263
		gs_recv_packet(dev, req->buf, req->actual);
requeue:
		req->length = ep->maxpacket;
		if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1264
			pr_err(
L
Linus Torvalds 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
			"gs_read_complete: cannot queue read request, ret=%d\n",
				ret);
		}
		break;

	case -ESHUTDOWN:
		/* disconnect */
		gs_debug("gs_read_complete: shutdown\n");
		gs_free_req(ep, req);
		break;

	default:
		/* unexpected */
1278
		pr_err(
L
Linus Torvalds 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
		"gs_read_complete: unexpected status error, status=%d\n",
			req->status);
		goto requeue;
		break;
	}
}

/*
* gs_write_complete
*/
static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
{
	struct gs_dev *dev = ep->driver_data;
	struct gs_req_entry *gs_req = req->context;

	if (dev == NULL) {
1295
		pr_err("gs_write_complete: NULL device pointer\n");
L
Linus Torvalds 已提交
1296 1297 1298 1299 1300 1301 1302 1303
		return;
	}

	switch(req->status) {
	case 0:
		/* normal completion */
requeue:
		if (gs_req == NULL) {
1304
			pr_err("gs_write_complete: NULL request pointer\n");
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
			return;
		}

		spin_lock(&dev->dev_lock);
		list_add(&gs_req->re_entry, &dev->dev_req_list);
		spin_unlock(&dev->dev_lock);

		gs_send(dev);

		break;

	case -ESHUTDOWN:
		/* disconnect */
		gs_debug("gs_write_complete: shutdown\n");
		gs_free_req(ep, req);
		break;

	default:
1323
		pr_err(
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
		"gs_write_complete: unexpected status error, status=%d\n",
			req->status);
		goto requeue;
		break;
	}
}

/* Gadget Driver */

/*
 * gs_bind
 *
 * Called on module load.  Allocates and initializes the device
 * structure and a control request.
 */
1339
static int __init gs_bind(struct usb_gadget *gadget)
L
Linus Torvalds 已提交
1340 1341 1342 1343
{
	int ret;
	struct usb_ep *ep;
	struct gs_dev *dev;
1344
	int gcnum;
L
Linus Torvalds 已提交
1345

1346 1347 1348 1349 1350
	/* Some controllers can't support CDC ACM:
	 * - sh doesn't support multiple interfaces or configs;
	 * - sa1100 doesn't have a third interrupt endpoint
	 */
	if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
L
Linus Torvalds 已提交
1351
		use_acm = 0;
1352 1353 1354

	gcnum = usb_gadget_controller_number(gadget);
	if (gcnum >= 0)
L
Linus Torvalds 已提交
1355
		gs_device_desc.bcdDevice =
1356 1357
				cpu_to_le16(GS_VERSION_NUM | gcnum);
	else {
1358
		pr_warning("gs_bind: controller '%s' not recognized\n",
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
			gadget->name);
		/* unrecognized, but safe unless bulk is REALLY quirky */
		gs_device_desc.bcdDevice =
			__constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
	}

	usb_ep_autoconfig_reset(gadget);

	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
	if (!ep)
		goto autoconf_fail;
	EP_IN_NAME = ep->name;
	ep->driver_data = ep;	/* claim the endpoint */

	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
	if (!ep)
		goto autoconf_fail;
	EP_OUT_NAME = ep->name;
	ep->driver_data = ep;	/* claim the endpoint */

	if (use_acm) {
		ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
		if (!ep) {
1382
			pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
			goto autoconf_fail;
		}
		gs_device_desc.idProduct = __constant_cpu_to_le16(
						GS_CDC_PRODUCT_ID),
		EP_NOTIFY_NAME = ep->name;
		ep->driver_data = ep;	/* claim the endpoint */
	}

	gs_device_desc.bDeviceClass = use_acm
		? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
	gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	if (gadget_is_dualspeed(gadget)) {
		gs_qualifier_desc.bDeviceClass = use_acm
			? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
		/* assume ep0 uses the same packet size for both speeds */
		gs_qualifier_desc.bMaxPacketSize0 =
			gs_device_desc.bMaxPacketSize0;
		/* assume endpoints are dual-speed */
		gs_highspeed_notify_desc.bEndpointAddress =
			gs_fullspeed_notify_desc.bEndpointAddress;
		gs_highspeed_in_desc.bEndpointAddress =
			gs_fullspeed_in_desc.bEndpointAddress;
		gs_highspeed_out_desc.bEndpointAddress =
			gs_fullspeed_out_desc.bEndpointAddress;
	}
L
Linus Torvalds 已提交
1409 1410 1411

	usb_gadget_set_selfpowered(gadget);

1412
	if (gadget_is_otg(gadget)) {
L
Linus Torvalds 已提交
1413 1414 1415 1416 1417
		gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
		gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
		gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
	}

1418
	gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
1419 1420 1421 1422
	if (dev == NULL)
		return -ENOMEM;

	snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1423
		init_utsname()->sysname, init_utsname()->release,
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429 1430 1431
		gadget->name);

	dev->dev_gadget = gadget;
	spin_lock_init(&dev->dev_lock);
	INIT_LIST_HEAD(&dev->dev_req_list);
	set_gadget_data(gadget, dev);

	if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1432
		pr_err("gs_bind: cannot allocate ports\n");
L
Linus Torvalds 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
		gs_unbind(gadget);
		return ret;
	}

	/* preallocate control response and buffer */
	dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
		GFP_KERNEL);
	if (dev->dev_ctrl_req == NULL) {
		gs_unbind(gadget);
		return -ENOMEM;
	}
	dev->dev_ctrl_req->complete = gs_setup_complete;

	gadget->ep0->driver_data = dev;

1448
	pr_info("gs_bind: %s %s bound\n",
L
Linus Torvalds 已提交
1449 1450 1451 1452 1453
		GS_LONG_NAME, GS_VERSION_STR);

	return 0;

autoconf_fail:
1454
	pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463
	return -ENODEV;
}

/*
 * gs_unbind
 *
 * Called on module unload.  Frees the control request and device
 * structure.
 */
1464
static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
{
	struct gs_dev *dev = get_gadget_data(gadget);

	gs_device = NULL;

	/* read/write requests already freed, only control request remains */
	if (dev != NULL) {
		if (dev->dev_ctrl_req != NULL) {
			gs_free_req(gadget->ep0, dev->dev_ctrl_req);
			dev->dev_ctrl_req = NULL;
		}
		gs_free_ports(dev);
1477 1478 1479 1480 1481 1482
		if (dev->dev_notify_ep)
			usb_ep_disable(dev->dev_notify_ep);
		if (dev->dev_in_ep)
			usb_ep_disable(dev->dev_in_ep);
		if (dev->dev_out_ep)
			usb_ep_disable(dev->dev_out_ep);
L
Linus Torvalds 已提交
1483 1484 1485 1486
		kfree(dev);
		set_gadget_data(gadget, NULL);
	}

1487
	pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
		GS_VERSION_STR);
}

/*
 * gs_setup
 *
 * Implements all the control endpoint functionality that's not
 * handled in hardware or the hardware driver.
 *
 * Returns the size of the data sent to the host, or a negative
 * error number.
 */
static int gs_setup(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl)
{
	int ret = -EOPNOTSUPP;
	struct gs_dev *dev = get_gadget_data(gadget);
	struct usb_request *req = dev->dev_ctrl_req;
1506 1507 1508
	u16 wIndex = le16_to_cpu(ctrl->wIndex);
	u16 wValue = le16_to_cpu(ctrl->wValue);
	u16 wLength = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
1509

1510 1511
	req->complete = gs_setup_complete;

L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	switch (ctrl->bRequestType & USB_TYPE_MASK) {
	case USB_TYPE_STANDARD:
		ret = gs_setup_standard(gadget,ctrl);
		break;

	case USB_TYPE_CLASS:
		ret = gs_setup_class(gadget,ctrl);
		break;

	default:
1522 1523
		pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
			"value=%04x, index=%04x, length=%d\n",
L
Linus Torvalds 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
		break;
	}

	/* respond with data transfer before status phase? */
	if (ret >= 0) {
		req->length = ret;
		req->zero = ret < wLength
				&& (ret % gadget->ep0->maxpacket) == 0;
		ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
		if (ret < 0) {
1536
			pr_err("gs_setup: cannot queue response, ret=%d\n",
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
				ret);
			req->status = 0;
			gs_setup_complete(gadget->ep0, req);
		}
	}

	/* device either stalls (ret < 0) or reports success */
	return ret;
}

static int gs_setup_standard(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl)
{
	int ret = -EOPNOTSUPP;
	struct gs_dev *dev = get_gadget_data(gadget);
	struct usb_request *req = dev->dev_ctrl_req;
1553 1554 1555
	u16 wIndex = le16_to_cpu(ctrl->wIndex);
	u16 wValue = le16_to_cpu(ctrl->wValue);
	u16 wLength = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569

	switch (ctrl->bRequest) {
	case USB_REQ_GET_DESCRIPTOR:
		if (ctrl->bRequestType != USB_DIR_IN)
			break;

		switch (wValue >> 8) {
		case USB_DT_DEVICE:
			ret = min(wLength,
				(u16)sizeof(struct usb_device_descriptor));
			memcpy(req->buf, &gs_device_desc, ret);
			break;

		case USB_DT_DEVICE_QUALIFIER:
1570
			if (!gadget_is_dualspeed(gadget))
L
Linus Torvalds 已提交
1571 1572 1573 1574 1575 1576 1577
				break;
			ret = min(wLength,
				(u16)sizeof(struct usb_qualifier_descriptor));
			memcpy(req->buf, &gs_qualifier_desc, ret);
			break;

		case USB_DT_OTHER_SPEED_CONFIG:
1578
			if (!gadget_is_dualspeed(gadget))
L
Linus Torvalds 已提交
1579 1580 1581
				break;
			/* fall through */
		case USB_DT_CONFIG:
1582
			ret = gs_build_config_buf(req->buf, gadget,
L
Linus Torvalds 已提交
1583
				wValue >> 8, wValue & 0xff,
1584
				gadget_is_otg(gadget));
L
Linus Torvalds 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
			if (ret >= 0)
				ret = min(wLength, (u16)ret);
			break;

		case USB_DT_STRING:
			/* wIndex == language code. */
			ret = usb_gadget_get_string(&gs_string_table,
				wValue & 0xff, req->buf);
			if (ret >= 0)
				ret = min(wLength, (u16)ret);
			break;
		}
		break;

	case USB_REQ_SET_CONFIGURATION:
		if (ctrl->bRequestType != 0)
			break;
		spin_lock(&dev->dev_lock);
		ret = gs_set_config(dev, wValue);
		spin_unlock(&dev->dev_lock);
		break;

	case USB_REQ_GET_CONFIGURATION:
		if (ctrl->bRequestType != USB_DIR_IN)
			break;
		*(u8 *)req->buf = dev->dev_config;
		ret = min(wLength, (u16)1);
		break;

	case USB_REQ_SET_INTERFACE:
		if (ctrl->bRequestType != USB_RECIP_INTERFACE
				|| !dev->dev_config
				|| wIndex >= GS_MAX_NUM_INTERFACES)
			break;
		if (dev->dev_config == GS_BULK_CONFIG_ID
				&& wIndex != GS_BULK_INTERFACE_ID)
			break;
		/* no alternate interface settings */
		if (wValue != 0)
			break;
		spin_lock(&dev->dev_lock);
		/* PXA hardware partially handles SET_INTERFACE;
		 * we need to kluge around that interference.  */
		if (gadget_is_pxa(gadget)) {
			ret = gs_set_config(dev, use_acm ?
				GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
			goto set_interface_done;
		}
		if (dev->dev_config != GS_BULK_CONFIG_ID
				&& wIndex == GS_CONTROL_INTERFACE_ID) {
			if (dev->dev_notify_ep) {
				usb_ep_disable(dev->dev_notify_ep);
				usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
			}
		} else {
			usb_ep_disable(dev->dev_in_ep);
			usb_ep_disable(dev->dev_out_ep);
			usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
			usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
		}
		ret = 0;
set_interface_done:
		spin_unlock(&dev->dev_lock);
		break;

	case USB_REQ_GET_INTERFACE:
		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
		|| dev->dev_config == GS_NO_CONFIG_ID)
			break;
		if (wIndex >= GS_MAX_NUM_INTERFACES
				|| (dev->dev_config == GS_BULK_CONFIG_ID
				&& wIndex != GS_BULK_INTERFACE_ID)) {
			ret = -EDOM;
			break;
		}
		/* no alternate interface settings */
		*(u8 *)req->buf = 0;
		ret = min(wLength, (u16)1);
		break;

	default:
1666 1667
		pr_err("gs_setup: unknown standard request, type=%02x, "
			"request=%02x, value=%04x, index=%04x, length=%d\n",
L
Linus Torvalds 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
		break;
	}

	return ret;
}

static int gs_setup_class(struct usb_gadget *gadget,
	const struct usb_ctrlrequest *ctrl)
{
	int ret = -EOPNOTSUPP;
	struct gs_dev *dev = get_gadget_data(gadget);
	struct gs_port *port = dev->dev_port[0];	/* ACM only has one port */
	struct usb_request *req = dev->dev_ctrl_req;
1683 1684 1685
	u16 wIndex = le16_to_cpu(ctrl->wIndex);
	u16 wValue = le16_to_cpu(ctrl->wValue);
	u16 wLength = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
1686 1687 1688

	switch (ctrl->bRequest) {
	case USB_CDC_REQ_SET_LINE_CODING:
1689 1690 1691 1692
		if (wLength != sizeof(struct usb_cdc_line_coding))
			break;
		ret = wLength;
		req->complete = gs_setup_complete_set_line_coding;
L
Linus Torvalds 已提交
1693 1694 1695
		break;

	case USB_CDC_REQ_GET_LINE_CODING:
1696
		ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701 1702 1703 1704
		if (port) {
			spin_lock(&port->port_lock);
			memcpy(req->buf, &port->port_line_coding, ret);
			spin_unlock(&port->port_lock);
		}
		break;

	case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
		if (wLength != 0)
			break;
		ret = 0;
		if (port) {
			/* REVISIT:  we currently just remember this data.
			 * If we change that, update whatever hardware needs
			 * updating.
			 */
			spin_lock(&port->port_lock);
			port->port_handshake_bits = wValue;
			spin_unlock(&port->port_lock);
		}
L
Linus Torvalds 已提交
1717 1718 1719
		break;

	default:
1720 1721 1722 1723 1724 1725
		/* NOTE:  strictly speaking, we should accept AT-commands
		 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
		 * But our call management descriptor says we don't handle
		 * call management, so we should be able to get by without
		 * handling those "required" commands (except by stalling).
		 */
1726
		pr_err("gs_setup: unknown class request, "
1727 1728
				"type=%02x, request=%02x, value=%04x, "
				"index=%04x, length=%d\n",
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735 1736
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
		break;
	}

	return ret;
}

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
		struct usb_request *req)
{
	struct gs_dev *dev = ep->driver_data;
	struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */

	switch (req->status) {
	case 0:
		/* normal completion */
		if (req->actual != sizeof(port->port_line_coding))
			usb_ep_set_halt(ep);
		else if (port) {
			struct usb_cdc_line_coding	*value = req->buf;

			/* REVISIT:  we currently just remember this data.
			 * If we change that, (a) validate it first, then
			 * (b) update whatever hardware needs updating.
			 */
			spin_lock(&port->port_lock);
			port->port_line_coding = *value;
			spin_unlock(&port->port_lock);
		}
		break;

	case -ESHUTDOWN:
		/* disconnect */
		gs_free_req(ep, req);
		break;

	default:
		/* unexpected */
		break;
	}
	return;
}

L
Linus Torvalds 已提交
1773 1774 1775 1776 1777 1778
/*
 * gs_setup_complete
 */
static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
{
	if (req->status || req->actual != req->length) {
1779 1780
		pr_err("gs_setup_complete: status error, status=%d, "
			"actual=%d, length=%d\n",
L
Linus Torvalds 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
			req->status, req->actual, req->length);
	}
}

/*
 * gs_disconnect
 *
 * Called when the device is disconnected.  Frees the closed
 * ports and disconnects open ports.  Open ports will be freed
 * on close.  Then reallocates the ports for the next connection.
 */
static void gs_disconnect(struct usb_gadget *gadget)
{
	unsigned long flags;
	struct gs_dev *dev = get_gadget_data(gadget);

	spin_lock_irqsave(&dev->dev_lock, flags);

	gs_reset_config(dev);

	/* free closed ports and disconnect open ports */
	/* (open ports will be freed when closed) */
	gs_free_ports(dev);

	/* re-allocate ports for the next connection */
	if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1807
		pr_err("gs_disconnect: cannot re-allocate ports\n");
L
Linus Torvalds 已提交
1808 1809 1810

	spin_unlock_irqrestore(&dev->dev_lock, flags);

1811
	pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
}

/*
 * gs_set_config
 *
 * Configures the device by enabling device specific
 * optimizations, setting up the endpoints, allocating
 * read and write requests and queuing read requests.
 *
 * The device lock must be held when calling this function.
 */
static int gs_set_config(struct gs_dev *dev, unsigned config)
{
	int i;
	int ret = 0;
	struct usb_gadget *gadget = dev->dev_gadget;
	struct usb_ep *ep;
	struct usb_endpoint_descriptor *ep_desc;
	struct usb_request *req;
	struct gs_req_entry *req_entry;

	if (dev == NULL) {
1834
		pr_err("gs_set_config: NULL device pointer\n");
L
Linus Torvalds 已提交
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
		return 0;
	}

	if (config == dev->dev_config)
		return 0;

	gs_reset_config(dev);

	switch (config) {
	case GS_NO_CONFIG_ID:
		return 0;
	case GS_BULK_CONFIG_ID:
		if (use_acm)
			return -EINVAL;
		/* device specific optimizations */
		if (gadget_is_net2280(gadget))
			net2280_set_fifo_mode(gadget, 1);
		break;
	case GS_ACM_CONFIG_ID:
		if (!use_acm)
			return -EINVAL;
		/* device specific optimizations */
		if (gadget_is_net2280(gadget))
			net2280_set_fifo_mode(gadget, 1);
		break;
	default:
		return -EINVAL;
	}

	dev->dev_config = config;

	gadget_for_each_ep(ep, gadget) {

		if (EP_NOTIFY_NAME
		&& strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1870
			ep_desc = choose_ep_desc(gadget,
L
Linus Torvalds 已提交
1871 1872 1873 1874 1875 1876 1877 1878
				&gs_highspeed_notify_desc,
				&gs_fullspeed_notify_desc);
			ret = usb_ep_enable(ep,ep_desc);
			if (ret == 0) {
				ep->driver_data = dev;
				dev->dev_notify_ep = ep;
				dev->dev_notify_ep_desc = ep_desc;
			} else {
1879 1880
				pr_err("gs_set_config: cannot enable NOTIFY "
					"endpoint %s, ret=%d\n",
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885 1886
					ep->name, ret);
				goto exit_reset_config;
			}
		}

		else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1887 1888
			ep_desc = choose_ep_desc(gadget,
				&gs_highspeed_in_desc,
L
Linus Torvalds 已提交
1889 1890 1891 1892 1893 1894 1895
				&gs_fullspeed_in_desc);
			ret = usb_ep_enable(ep,ep_desc);
			if (ret == 0) {
				ep->driver_data = dev;
				dev->dev_in_ep = ep;
				dev->dev_in_ep_desc = ep_desc;
			} else {
1896 1897
				pr_err("gs_set_config: cannot enable IN "
					"endpoint %s, ret=%d\n",
L
Linus Torvalds 已提交
1898 1899 1900 1901 1902 1903
					ep->name, ret);
				goto exit_reset_config;
			}
		}

		else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
1904
			ep_desc = choose_ep_desc(gadget,
L
Linus Torvalds 已提交
1905 1906 1907 1908 1909 1910 1911 1912
				&gs_highspeed_out_desc,
				&gs_fullspeed_out_desc);
			ret = usb_ep_enable(ep,ep_desc);
			if (ret == 0) {
				ep->driver_data = dev;
				dev->dev_out_ep = ep;
				dev->dev_out_ep_desc = ep_desc;
			} else {
1913 1914
				pr_err("gs_set_config: cannot enable OUT "
					"endpoint %s, ret=%d\n",
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923
					ep->name, ret);
				goto exit_reset_config;
			}
		}

	}

	if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
	|| (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
1924
		pr_err("gs_set_config: cannot find endpoints\n");
L
Linus Torvalds 已提交
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
		ret = -ENODEV;
		goto exit_reset_config;
	}

	/* allocate and queue read requests */
	ep = dev->dev_out_ep;
	for (i=0; i<read_q_size && ret == 0; i++) {
		if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
			req->complete = gs_read_complete;
			if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1935 1936
				pr_err("gs_set_config: cannot queue read "
					"request, ret=%d\n", ret);
L
Linus Torvalds 已提交
1937 1938
			}
		} else {
1939 1940
			pr_err("gs_set_config: cannot allocate "
					"read requests\n");
L
Linus Torvalds 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
			ret = -ENOMEM;
			goto exit_reset_config;
		}
	}

	/* allocate write requests, and put on free list */
	ep = dev->dev_in_ep;
	for (i=0; i<write_q_size; i++) {
		if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
			req_entry->re_req->complete = gs_write_complete;
			list_add(&req_entry->re_entry, &dev->dev_req_list);
		} else {
1953 1954
			pr_err("gs_set_config: cannot allocate "
					"write requests\n");
L
Linus Torvalds 已提交
1955 1956 1957 1958 1959
			ret = -ENOMEM;
			goto exit_reset_config;
		}
	}

1960 1961 1962 1963 1964
	/* REVISIT the ACM mode should be able to actually *issue* some
	 * notifications, for at least serial state change events if
	 * not also for network connection; say so in bmCapabilities.
	 */

1965
	pr_info("gs_set_config: %s configured, %s speed %s config\n",
L
Linus Torvalds 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
		GS_LONG_NAME,
		gadget->speed == USB_SPEED_HIGH ? "high" : "full",
		config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");

	return 0;

exit_reset_config:
	gs_reset_config(dev);
	return ret;
}

/*
 * gs_reset_config
 *
 * Mark the device as not configured, disable all endpoints,
 * which forces completion of pending I/O and frees queued
 * requests, and free the remaining write requests on the
 * free list.
 *
 * The device lock must be held when calling this function.
 */
static void gs_reset_config(struct gs_dev *dev)
{
	struct gs_req_entry *req_entry;

	if (dev == NULL) {
1992
		pr_err("gs_reset_config: NULL device pointer\n");
L
Linus Torvalds 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
		return;
	}

	if (dev->dev_config == GS_NO_CONFIG_ID)
		return;

	dev->dev_config = GS_NO_CONFIG_ID;

	/* free write requests on the free list */
	while(!list_empty(&dev->dev_req_list)) {
		req_entry = list_entry(dev->dev_req_list.next,
			struct gs_req_entry, re_entry);
		list_del(&req_entry->re_entry);
		gs_free_req_entry(dev->dev_in_ep, req_entry);
	}

	/* disable endpoints, forcing completion of pending i/o; */
	/* completion handlers free their requests in this case */
	if (dev->dev_notify_ep) {
		usb_ep_disable(dev->dev_notify_ep);
		dev->dev_notify_ep = NULL;
	}
	if (dev->dev_in_ep) {
		usb_ep_disable(dev->dev_in_ep);
		dev->dev_in_ep = NULL;
	}
	if (dev->dev_out_ep) {
		usb_ep_disable(dev->dev_out_ep);
		dev->dev_out_ep = NULL;
	}
}

/*
 * gs_build_config_buf
 *
 * Builds the config descriptors in the given buffer and returns the
 * length, or a negative error number.
 */
2031
static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
L
Linus Torvalds 已提交
2032 2033 2034
	u8 type, unsigned int index, int is_otg)
{
	int len;
2035
	int high_speed = 0;
L
Linus Torvalds 已提交
2036 2037 2038 2039 2040 2041 2042
	const struct usb_config_descriptor *config_desc;
	const struct usb_descriptor_header **function;

	if (index >= gs_device_desc.bNumConfigurations)
		return -EINVAL;

	/* other speed switches high and full speed */
2043 2044 2045 2046 2047
	if (gadget_is_dualspeed(g)) {
		high_speed = (g->speed == USB_SPEED_HIGH);
		if (type == USB_DT_OTHER_SPEED_CONFIG)
			high_speed = !high_speed;
	}
L
Linus Torvalds 已提交
2048 2049 2050

	if (use_acm) {
		config_desc = &gs_acm_config_desc;
2051 2052 2053
		function = high_speed
			? gs_acm_highspeed_function
			: gs_acm_fullspeed_function;
L
Linus Torvalds 已提交
2054 2055
	} else {
		config_desc = &gs_bulk_config_desc;
2056 2057 2058
		function = high_speed
			? gs_bulk_highspeed_function
			: gs_bulk_fullspeed_function;
L
Linus Torvalds 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
	}

	/* for now, don't advertise srp-only devices */
	if (!is_otg)
		function++;

	len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
	if (len < 0)
		return len;

	((struct usb_config_descriptor *)buf)->bDescriptorType = type;

	return len;
}

/*
 * gs_alloc_req
 *
 * Allocate a usb_request and its buffer.  Returns a pointer to the
 * usb_request or NULL if there is an error.
 */
2080
static struct usb_request *
A
Al Viro 已提交
2081
gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
L
Linus Torvalds 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
{
	struct usb_request *req;

	if (ep == NULL)
		return NULL;

	req = usb_ep_alloc_request(ep, kmalloc_flags);

	if (req != NULL) {
		req->length = len;
		req->buf = kmalloc(len, kmalloc_flags);
		if (req->buf == NULL) {
			usb_ep_free_request(ep, req);
			return NULL;
		}
	}

	return req;
}

/*
 * gs_free_req
 *
 * Free a usb_request and its buffer.
 */
static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
{
	if (ep != NULL && req != NULL) {
		kfree(req->buf);
		usb_ep_free_request(ep, req);
	}
}

/*
 * gs_alloc_req_entry
 *
 * Allocates a request and its buffer, using the given
 * endpoint, buffer len, and kmalloc flags.
 */
2121
static struct gs_req_entry *
A
Al Viro 已提交
2122
gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
{
	struct gs_req_entry	*req;

	req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
	if (req == NULL)
		return NULL;

	req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
	if (req->re_req == NULL) {
		kfree(req);
		return NULL;
	}

	req->re_req->context = req;

	return req;
}

/*
 * gs_free_req_entry
 *
 * Frees a request and its buffer.
 */
static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
{
	if (ep != NULL && req != NULL) {
		if (req->re_req != NULL)
			gs_free_req(ep, req->re_req);
		kfree(req);
	}
}

/*
 * gs_alloc_ports
 *
 * Allocate all ports and set the gs_dev struct to point to them.
 * Return 0 if successful, or a negative error number.
 *
 * The device lock is normally held when calling this function.
 */
A
Al Viro 已提交
2163
static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
L
Linus Torvalds 已提交
2164 2165 2166 2167 2168 2169 2170 2171
{
	int i;
	struct gs_port *port;

	if (dev == NULL)
		return -EIO;

	for (i=0; i<GS_NUM_PORTS; i++) {
2172
		if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
L
Linus Torvalds 已提交
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
			return -ENOMEM;

		port->port_dev = dev;
		port->port_num = i;
		port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
		port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
		port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
		port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
		spin_lock_init(&port->port_lock);
		init_waitqueue_head(&port->port_write_wait);

		dev->dev_port[i] = port;
	}

	return 0;
}

/*
 * gs_free_ports
 *
 * Free all closed ports.  Open ports are disconnected by
 * freeing their write buffers, setting their device pointers
 * and the pointers to them in the device to NULL.  These
 * ports will be freed when closed.
 *
 * The device lock is normally held when calling this function.
 */
static void gs_free_ports(struct gs_dev *dev)
{
	int i;
	unsigned long flags;
	struct gs_port *port;

	if (dev == NULL)
		return;

	for (i=0; i<GS_NUM_PORTS; i++) {
		if ((port=dev->dev_port[i]) != NULL) {
			dev->dev_port[i] = NULL;

			spin_lock_irqsave(&port->port_lock, flags);

			if (port->port_write_buf != NULL) {
				gs_buf_free(port->port_write_buf);
				port->port_write_buf = NULL;
			}

			if (port->port_open_count > 0 || port->port_in_use) {
				port->port_dev = NULL;
				wake_up_interruptible(&port->port_write_wait);
				if (port->port_tty) {
2224
					tty_hangup(port->port_tty);
L
Linus Torvalds 已提交
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
				}
				spin_unlock_irqrestore(&port->port_lock, flags);
			} else {
				spin_unlock_irqrestore(&port->port_lock, flags);
				kfree(port);
			}

		}
	}
}

/* Circular Buffer */

/*
 * gs_buf_alloc
 *
 * Allocate a circular buffer and all associated memory.
 */
A
Al Viro 已提交
2243
static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
L
Linus Torvalds 已提交
2244 2245 2246 2247 2248 2249
{
	struct gs_buf *gb;

	if (size == 0)
		return NULL;

2250
	gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
L
Linus Torvalds 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
	if (gb == NULL)
		return NULL;

	gb->buf_buf = kmalloc(size, kmalloc_flags);
	if (gb->buf_buf == NULL) {
		kfree(gb);
		return NULL;
	}

	gb->buf_size = size;
	gb->buf_get = gb->buf_put = gb->buf_buf;

	return gb;
}

/*
 * gs_buf_free
 *
 * Free the buffer and all associated memory.
 */
2271
static void gs_buf_free(struct gs_buf *gb)
L
Linus Torvalds 已提交
2272
{
2273 2274
	if (gb) {
		kfree(gb->buf_buf);
L
Linus Torvalds 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283
		kfree(gb);
	}
}

/*
 * gs_buf_clear
 *
 * Clear out all data in the circular buffer.
 */
2284
static void gs_buf_clear(struct gs_buf *gb)
L
Linus Torvalds 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
{
	if (gb != NULL)
		gb->buf_get = gb->buf_put;
		/* equivalent to a get of all data available */
}

/*
 * gs_buf_data_avail
 *
 * Return the number of bytes of data available in the circular
 * buffer.
 */
2297
static unsigned int gs_buf_data_avail(struct gs_buf *gb)
L
Linus Torvalds 已提交
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
{
	if (gb != NULL)
		return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
	else
		return 0;
}

/*
 * gs_buf_space_avail
 *
 * Return the number of bytes of space available in the circular
 * buffer.
 */
2311
static unsigned int gs_buf_space_avail(struct gs_buf *gb)
L
Linus Torvalds 已提交
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
{
	if (gb != NULL)
		return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
	else
		return 0;
}

/*
 * gs_buf_put
 *
 * Copy data data from a user buffer and put it into the circular buffer.
 * Restrict to the amount of space available.
 *
 * Return the number of bytes copied.
 */
2327 2328
static unsigned int
gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
L
Linus Torvalds 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
{
	unsigned int len;

	if (gb == NULL)
		return 0;

	len  = gs_buf_space_avail(gb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = gb->buf_buf + gb->buf_size - gb->buf_put;
	if (count > len) {
		memcpy(gb->buf_put, buf, len);
		memcpy(gb->buf_buf, buf+len, count - len);
		gb->buf_put = gb->buf_buf + count - len;
	} else {
		memcpy(gb->buf_put, buf, count);
		if (count < len)
			gb->buf_put += count;
		else /* count == len */
			gb->buf_put = gb->buf_buf;
	}

	return count;
}

/*
 * gs_buf_get
 *
 * Get data from the circular buffer and copy to the given buffer.
 * Restrict to the amount of data available.
 *
 * Return the number of bytes copied.
 */
2366 2367
static unsigned int
gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
L
Linus Torvalds 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
{
	unsigned int len;

	if (gb == NULL)
		return 0;

	len = gs_buf_data_avail(gb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = gb->buf_buf + gb->buf_size - gb->buf_get;
	if (count > len) {
		memcpy(buf, gb->buf_get, len);
		memcpy(buf+len, gb->buf_buf, count - len);
		gb->buf_get = gb->buf_buf + count - len;
	} else {
		memcpy(buf, gb->buf_get, count);
		if (count < len)
			gb->buf_get += count;
		else /* count == len */
			gb->buf_get = gb->buf_buf;
	}

	return count;
}