serial.c 27.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
D
David Brownell 已提交
2
 * serial.c -- USB gadget serial driver
L
Linus Torvalds 已提交
3
 *
D
David Brownell 已提交
4 5
 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
 * Copyright (C) 2008 by David Brownell
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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>

D
David Brownell 已提交
18
#include "u_serial.h"
L
Linus Torvalds 已提交
19 20 21 22 23
#include "gadget_chips.h"


/* Defines */

D
David Brownell 已提交
24 25
#define GS_VERSION_STR			"v2.3"
#define GS_VERSION_NUM			0x2300
L
Linus Torvalds 已提交
26 27 28 29

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

D
David Brownell 已提交
30 31
#define GS_VERSION_NAME			GS_LONG_NAME " " GS_VERSION_STR

L
Linus Torvalds 已提交
32

33 34 35 36 37
/* REVISIT only one port is supported for now;
 * see gs_{send,recv}_packet() ... no multiplexing,
 * and no support for multiple ACM devices.
 */
#define GS_NUM_PORTS			1
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#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_USE_ACM		0


54 55 56 57 58 59 60 61 62 63
/* 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 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

/* 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

/* 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_request	*dev_ctrl_req;	/* control request */
D
David Brownell 已提交
83 84

	struct gserial		gser;		/* serial/tty port */
L
Linus Torvalds 已提交
85 86 87 88 89
};


/* Functions */

90
/* gadget driver internals */
L
Linus Torvalds 已提交
91 92
static int gs_set_config(struct gs_dev *dev, unsigned config);
static void gs_reset_config(struct gs_dev *dev);
93
static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
L
Linus Torvalds 已提交
94 95 96
		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 已提交
97
	gfp_t kmalloc_flags);
L
Linus Torvalds 已提交
98 99
static void gs_free_req(struct usb_ep *ep, struct usb_request *req);

100
/*-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

/* 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 },
D
David Brownell 已提交
116
	{ GS_PRODUCT_STR_ID, GS_VERSION_NAME },
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	{ 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,
	.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,
175
	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	.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 = {
211 212 213 214 215
	.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 已提交
216 217 218
};

static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
219 220 221
	.bLength =		sizeof(gs_acm_descriptor),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
222
	.bmCapabilities =	(1 << 1),
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231
};

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 */
};
232

L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
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,
D
David Brownell 已提交
276
};
L
Linus Torvalds 已提交
277

D
David Brownell 已提交
278 279 280 281 282 283 284 285
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,
};
L
Linus Torvalds 已提交
286

D
David Brownell 已提交
287 288 289 290 291 292
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),
};
L
Linus Torvalds 已提交
293

D
David Brownell 已提交
294 295 296 297 298 299
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),
};
L
Linus Torvalds 已提交
300

D
David Brownell 已提交
301 302 303 304 305 306 307
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,
};
L
Linus Torvalds 已提交
308

D
David Brownell 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
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,
};
L
Linus Torvalds 已提交
330 331


D
David Brownell 已提交
332
/*-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
333

D
David Brownell 已提交
334 335 336 337 338
/* Module */
MODULE_DESCRIPTION(GS_VERSION_NAME);
MODULE_AUTHOR("Al Borchers");
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
339

D
David Brownell 已提交
340 341 342
static unsigned int use_acm = GS_DEFAULT_USE_ACM;
module_param(use_acm, uint, S_IRUGO);
MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
L
Linus Torvalds 已提交
343

344 345
/*-------------------------------------------------------------------------*/

L
Linus Torvalds 已提交
346 347
/* Gadget Driver */

348 349 350 351 352 353
/*
 * gs_unbind
 *
 * Called on module unload.  Frees the control request and device
 * structure.
 */
D
David Brownell 已提交
354
static void __exit gs_unbind(struct usb_gadget *gadget)
355 356 357 358 359 360 361 362 363
{
	struct gs_dev *dev = get_gadget_data(gadget);

	/* 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;
		}
364
		gs_reset_config(dev);
365 366 367 368
		kfree(dev);
		set_gadget_data(gadget, NULL);
	}

D
David Brownell 已提交
369 370 371
	pr_info("gs_unbind: %s unbound\n", GS_VERSION_NAME);

	gserial_cleanup();
372 373
}

L
Linus Torvalds 已提交
374 375 376 377 378 379
/*
 * gs_bind
 *
 * Called on module load.  Allocates and initializes the device
 * structure and a control request.
 */
380
static int __init gs_bind(struct usb_gadget *gadget)
L
Linus Torvalds 已提交
381 382 383 384
{
	int ret;
	struct usb_ep *ep;
	struct gs_dev *dev;
385
	int gcnum;
L
Linus Torvalds 已提交
386

D
David Brownell 已提交
387 388 389 390
	ret = gserial_setup(gadget, GS_NUM_PORTS);
	if (ret < 0)
		return ret;

391 392 393 394 395
	/* 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 已提交
396
		use_acm = 0;
397 398 399

	gcnum = usb_gadget_controller_number(gadget);
	if (gcnum >= 0)
L
Linus Torvalds 已提交
400
		gs_device_desc.bcdDevice =
401 402
				cpu_to_le16(GS_VERSION_NUM | gcnum);
	else {
403
		pr_warning("gs_bind: controller '%s' not recognized\n",
L
Linus Torvalds 已提交
404 405 406 407 408 409
			gadget->name);
		/* unrecognized, but safe unless bulk is REALLY quirky */
		gs_device_desc.bcdDevice =
			__constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
	}

410
	dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
D
David Brownell 已提交
411 412 413 414
	if (dev == NULL) {
		ret = -ENOMEM;
		goto autoconf_fail;
	}
415

L
Linus Torvalds 已提交
416
	usb_ep_autoconfig_reset(gadget);
D
David Brownell 已提交
417
	ret = -ENXIO;
L
Linus Torvalds 已提交
418 419 420 421

	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
	if (!ep)
		goto autoconf_fail;
D
David Brownell 已提交
422
	dev->gser.in = ep;
423
	ep->driver_data = dev;	/* claim the endpoint */
L
Linus Torvalds 已提交
424 425 426 427

	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
	if (!ep)
		goto autoconf_fail;
D
David Brownell 已提交
428
	dev->gser.out = ep;
429
	ep->driver_data = dev;	/* claim the endpoint */
L
Linus Torvalds 已提交
430 431 432 433

	if (use_acm) {
		ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
		if (!ep) {
434
			pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
L
Linus Torvalds 已提交
435 436 437 438
			goto autoconf_fail;
		}
		gs_device_desc.idProduct = __constant_cpu_to_le16(
						GS_CDC_PRODUCT_ID),
D
David Brownell 已提交
439
		dev->gser.notify = ep;
440
		ep->driver_data = dev;	/* claim the endpoint */
L
Linus Torvalds 已提交
441 442 443 444 445 446
	}

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

447 448 449 450 451 452 453 454 455 456 457 458 459 460
	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 已提交
461 462 463

	usb_gadget_set_selfpowered(gadget);

464
	if (gadget_is_otg(gadget)) {
L
Linus Torvalds 已提交
465 466 467 468 469 470
		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;
	}

	snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
471
		init_utsname()->sysname, init_utsname()->release,
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481
		gadget->name);

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

	/* 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) {
D
David Brownell 已提交
482 483
		ret = -ENOMEM;
		goto autoconf_fail;
L
Linus Torvalds 已提交
484 485 486
	}
	gadget->ep0->driver_data = dev;

D
David Brownell 已提交
487
	pr_info("gs_bind: %s bound\n", GS_VERSION_NAME);
L
Linus Torvalds 已提交
488 489 490 491

	return 0;

autoconf_fail:
492
	kfree(dev);
D
David Brownell 已提交
493 494 495
	gserial_cleanup();
	pr_err("gs_bind: to %s, err %d\n", gadget->name, ret);
	return ret;
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503
}

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;
504 505 506
	u16 wIndex = le16_to_cpu(ctrl->wIndex);
	u16 wValue = le16_to_cpu(ctrl->wValue);
	u16 wLength = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520

	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:
521
			if (!gadget_is_dualspeed(gadget))
L
Linus Torvalds 已提交
522 523 524 525 526 527 528
				break;
			ret = min(wLength,
				(u16)sizeof(struct usb_qualifier_descriptor));
			memcpy(req->buf, &gs_qualifier_desc, ret);
			break;

		case USB_DT_OTHER_SPEED_CONFIG:
529
			if (!gadget_is_dualspeed(gadget))
L
Linus Torvalds 已提交
530 531 532
				break;
			/* fall through */
		case USB_DT_CONFIG:
533
			ret = gs_build_config_buf(req->buf, gadget,
L
Linus Torvalds 已提交
534
				wValue >> 8, wValue & 0xff,
535
				gadget_is_otg(gadget));
L
Linus Torvalds 已提交
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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			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) {
D
David Brownell 已提交
586 587 588 589
			if (dev->gser.notify) {
				usb_ep_disable(dev->gser.notify);
				usb_ep_enable(dev->gser.notify,
						dev->gser.notify_desc);
L
Linus Torvalds 已提交
590 591
			}
		} else {
D
David Brownell 已提交
592 593
			gserial_connect(&dev->gser, 0);
			gserial_disconnect(&dev->gser);
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		}
		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:
616 617
		pr_err("gs_setup: unknown standard request, type=%02x, "
			"request=%02x, value=%04x, index=%04x, length=%d\n",
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
		break;
	}

	return ret;
}

626 627 628 629 630 631 632 633
static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
		struct usb_request *req)
{
	struct gs_dev *dev = ep->driver_data;

	switch (req->status) {
	case 0:
		/* normal completion */
D
David Brownell 已提交
634
		if (req->actual != sizeof(dev->gser.port_line_coding))
635
			usb_ep_set_halt(ep);
D
David Brownell 已提交
636
		else {
637 638 639 640 641 642
			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.
			 */
D
David Brownell 已提交
643 644 645
			spin_lock(&dev->dev_lock);
			dev->gser.port_line_coding = *value;
			spin_unlock(&dev->dev_lock);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		}
		break;

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

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

L
Linus Torvalds 已提交
661 662 663 664 665 666
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 usb_request *req = dev->dev_ctrl_req;
667 668 669
	u16 wIndex = le16_to_cpu(ctrl->wIndex);
	u16 wValue = le16_to_cpu(ctrl->wValue);
	u16 wLength = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
670 671 672

	switch (ctrl->bRequest) {
	case USB_CDC_REQ_SET_LINE_CODING:
673 674 675 676
		if (wLength != sizeof(struct usb_cdc_line_coding))
			break;
		ret = wLength;
		req->complete = gs_setup_complete_set_line_coding;
L
Linus Torvalds 已提交
677 678 679
		break;

	case USB_CDC_REQ_GET_LINE_CODING:
680
		ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
D
David Brownell 已提交
681 682 683
		spin_lock(&dev->dev_lock);
		memcpy(req->buf, &dev->gser.port_line_coding, ret);
		spin_unlock(&dev->dev_lock);
L
Linus Torvalds 已提交
684 685 686
		break;

	case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
687 688 689
		if (wLength != 0)
			break;
		ret = 0;
D
David Brownell 已提交
690 691 692 693 694 695 696
		/* REVISIT:  we currently just remember this data.
		 * If we change that, update whatever hardware needs
		 * updating.
		 */
		spin_lock(&dev->dev_lock);
		dev->gser.port_handshake_bits = wValue;
		spin_unlock(&dev->dev_lock);
L
Linus Torvalds 已提交
697 698 699
		break;

	default:
700 701 702 703 704 705
		/* 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).
		 */
706
		pr_err("gs_setup: unknown class request, "
707 708
				"type=%02x, request=%02x, value=%04x, "
				"index=%04x, length=%d\n",
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
		break;
	}

	return ret;
}

717 718 719 720
/*
 * gs_setup_complete
 */
static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
721
{
722 723 724 725 726 727
	if (req->status || req->actual != req->length) {
		pr_err("gs_setup_complete: status error, status=%d, "
			"actual=%d, length=%d\n",
			req->status, req->actual, req->length);
	}
}
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/*
 * 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;
	u16		wIndex = le16_to_cpu(ctrl->wIndex);
	u16		wValue = le16_to_cpu(ctrl->wValue);
	u16		wLength = le16_to_cpu(ctrl->wLength);
747

748 749 750 751 752
	req->complete = gs_setup_complete;

	switch (ctrl->bRequestType & USB_TYPE_MASK) {
	case USB_TYPE_STANDARD:
		ret = gs_setup_standard(gadget, ctrl);
753 754
		break;

755 756
	case USB_TYPE_CLASS:
		ret = gs_setup_class(gadget, ctrl);
757 758 759
		break;

	default:
760 761 762 763
		pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
			"value=%04x, index=%04x, length=%d\n",
			ctrl->bRequestType, ctrl->bRequest,
			wValue, wIndex, wLength);
764 765 766
		break;
	}

767 768 769 770 771 772 773 774 775 776 777 778
	/* 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) {
			pr_err("gs_setup: cannot queue response, ret=%d\n",
				ret);
			req->status = 0;
			gs_setup_complete(gadget->ep0, req);
		}
L
Linus Torvalds 已提交
779
	}
780 781 782

	/* device either stalls (ret < 0) or reports success */
	return ret;
L
Linus Torvalds 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
}

/*
 * 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);
	spin_unlock_irqrestore(&dev->dev_lock, flags);

801
	pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
L
Linus Torvalds 已提交
802 803
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
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,
	.unbind =		gs_unbind,
	.setup =		gs_setup,
	.disconnect =		gs_disconnect,
	.driver = {
		.name =		GS_SHORT_NAME,
		.owner =	THIS_MODULE,
	},
};

L
Linus Torvalds 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
/*
 * 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 ret = 0;
	struct usb_gadget *gadget = dev->dev_gadget;

	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;
		break;
	case GS_ACM_CONFIG_ID:
		if (!use_acm)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

D
David Brownell 已提交
855
	dev->gser.in_desc = choose_ep_desc(gadget,
856 857
			&gs_highspeed_in_desc,
			&gs_fullspeed_in_desc);
D
David Brownell 已提交
858
	dev->gser.out_desc = choose_ep_desc(gadget,
859 860
			&gs_highspeed_out_desc,
			&gs_fullspeed_out_desc);
D
David Brownell 已提交
861
	dev->gser.notify_desc = dev->gser.notify
862
		? choose_ep_desc(gadget,
L
Linus Torvalds 已提交
863
				&gs_highspeed_notify_desc,
864 865
				&gs_fullspeed_notify_desc)
		: NULL;
L
Linus Torvalds 已提交
866

D
David Brownell 已提交
867 868 869 870 871 872
	/* only support one "serial" port for now */
	if (dev->gser.notify) {
		ret = usb_ep_enable(dev->gser.notify, dev->gser.notify_desc);
		if (ret < 0)
			return ret;
		dev->gser.notify->driver_data = dev;
L
Linus Torvalds 已提交
873 874
	}

D
David Brownell 已提交
875 876 877 878 879
	ret = gserial_connect(&dev->gser, 0);
	if (ret < 0) {
		if (dev->gser.notify) {
			usb_ep_disable(dev->gser.notify);
			dev->gser.notify->driver_data = NULL;
880
		}
D
David Brownell 已提交
881
		return ret;
L
Linus Torvalds 已提交
882 883
	}

884 885
	dev->dev_config = config;

886 887 888 889 890
	/* 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.
	 */

891
	pr_info("gs_set_config: %s configured, %s speed %s config\n",
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
		GS_LONG_NAME,
		gadget->speed == USB_SPEED_HIGH ? "high" : "full",
		config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");

	return 0;
}

/*
 * 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)
{
	if (dev->dev_config == GS_NO_CONFIG_ID)
		return;

	dev->dev_config = GS_NO_CONFIG_ID;

D
David Brownell 已提交
916 917 918 919
	gserial_disconnect(&dev->gser);
	if (dev->gser.notify) {
		usb_ep_disable(dev->gser.notify);
		dev->gser.notify->driver_data = NULL;
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928
	}
}

/*
 * gs_build_config_buf
 *
 * Builds the config descriptors in the given buffer and returns the
 * length, or a negative error number.
 */
929
static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
L
Linus Torvalds 已提交
930 931 932
	u8 type, unsigned int index, int is_otg)
{
	int len;
933
	int high_speed = 0;
L
Linus Torvalds 已提交
934 935 936 937 938 939 940
	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 */
941 942 943 944 945
	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 已提交
946 947 948

	if (use_acm) {
		config_desc = &gs_acm_config_desc;
949 950 951
		function = high_speed
			? gs_acm_highspeed_function
			: gs_acm_fullspeed_function;
L
Linus Torvalds 已提交
952 953
	} else {
		config_desc = &gs_bulk_config_desc;
954 955 956
		function = high_speed
			? gs_bulk_highspeed_function
			: gs_bulk_fullspeed_function;
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	}

	/* 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.
 */
978
static struct usb_request *
A
Al Viro 已提交
979
gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
L
Linus Torvalds 已提交
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
{
	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);
	}
}

1013 1014 1015 1016 1017 1018 1019 1020 1021
/*-------------------------------------------------------------------------*/

/*
 *  gs_module_init
 *
 *  Register as a USB gadget driver and a tty driver.
 */
static int __init gs_module_init(void)
{
D
David Brownell 已提交
1022
	return usb_gadget_register_driver(&gs_gadget_driver);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
}
module_init(gs_module_init);

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