Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.17.
  1/*
  2 * f_acm.c -- USB CDC serial (ACM) function driver
  3 *
  4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  5 * Copyright (C) 2008 by David Brownell
  6 * Copyright (C) 2008 by Nokia Corporation
  7 * Copyright (C) 2009 by Samsung Electronics
  8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  9 *
 10 * This software is distributed under the terms of the GNU General
 11 * Public License ("GPL") as published by the Free Software Foundation,
 12 * either version 2 of that License or (at your option) any later version.
 13 */
 14
 15/* #define VERBOSE_DEBUG */
 16
 17#include <linux/slab.h>
 18#include <linux/kernel.h>
 19#include <linux/device.h>
 20
 21#include "u_serial.h"
 22#include "gadget_chips.h"
 23
 24
 25/*
 26 * This CDC ACM function support just wraps control functions and
 27 * notifications around the generic serial-over-usb code.
 28 *
 29 * Because CDC ACM is standardized by the USB-IF, many host operating
 30 * systems have drivers for it.  Accordingly, ACM is the preferred
 31 * interop solution for serial-port type connections.  The control
 32 * models are often not necessary, and in any case don't do much in
 33 * this bare-bones implementation.
 34 *
 35 * Note that even MS-Windows has some support for ACM.  However, that
 36 * support is somewhat broken because when you use ACM in a composite
 37 * device, having multiple interfaces confuses the poor OS.  It doesn't
 38 * seem to understand CDC Union descriptors.  The new "association"
 39 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
 40 */
 41
 42struct f_acm {
 43	struct gserial			port;
 44	u8				ctrl_id, data_id;
 45	u8				port_num;
 46
 47	u8				pending;
 48
 49	/* lock is mostly for pending and notify_req ... they get accessed
 50	 * by callbacks both from tty (open/close/break) under its spinlock,
 51	 * and notify_req.complete() which can't use that lock.
 52	 */
 53	spinlock_t			lock;
 54
 55	struct usb_ep			*notify;
 56	struct usb_request		*notify_req;
 57
 58	struct usb_cdc_line_coding	port_line_coding;	/* 8-N-1 etc */
 59
 60	/* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
 61	u16				port_handshake_bits;
 62#define ACM_CTRL_RTS	(1 << 1)	/* unused with full duplex */
 63#define ACM_CTRL_DTR	(1 << 0)	/* host is ready for data r/w */
 64
 65	/* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
 66	u16				serial_state;
 67#define ACM_CTRL_OVERRUN	(1 << 6)
 68#define ACM_CTRL_PARITY		(1 << 5)
 69#define ACM_CTRL_FRAMING	(1 << 4)
 70#define ACM_CTRL_RI		(1 << 3)
 71#define ACM_CTRL_BRK		(1 << 2)
 72#define ACM_CTRL_DSR		(1 << 1)
 73#define ACM_CTRL_DCD		(1 << 0)
 74};
 75
 76static inline struct f_acm *func_to_acm(struct usb_function *f)
 77{
 78	return container_of(f, struct f_acm, port.func);
 79}
 80
 81static inline struct f_acm *port_to_acm(struct gserial *p)
 82{
 83	return container_of(p, struct f_acm, port);
 84}
 85
 86/*-------------------------------------------------------------------------*/
 87
 88/* notification endpoint uses smallish and infrequent fixed-size messages */
 89
 90#define GS_LOG2_NOTIFY_INTERVAL		5	/* 1 << 5 == 32 msec */
 91#define GS_NOTIFY_MAXPACKET		10	/* notification + 2 bytes */
 92
 93/* interface and class descriptors: */
 94
 95static struct usb_interface_assoc_descriptor
 96acm_iad_descriptor = {
 97	.bLength =		sizeof acm_iad_descriptor,
 98	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION,
 99
100	/* .bFirstInterface =	DYNAMIC, */
101	.bInterfaceCount = 	2,	// control + data
102	.bFunctionClass =	USB_CLASS_COMM,
103	.bFunctionSubClass =	USB_CDC_SUBCLASS_ACM,
104	.bFunctionProtocol =	USB_CDC_ACM_PROTO_AT_V25TER,
105	/* .iFunction =		DYNAMIC */
106};
107
108
109static struct usb_interface_descriptor acm_control_interface_desc = {
110	.bLength =		USB_DT_INTERFACE_SIZE,
111	.bDescriptorType =	USB_DT_INTERFACE,
112	/* .bInterfaceNumber = DYNAMIC */
113	.bNumEndpoints =	1,
114	.bInterfaceClass =	USB_CLASS_COMM,
115	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ACM,
116	.bInterfaceProtocol =	USB_CDC_ACM_PROTO_AT_V25TER,
117	/* .iInterface = DYNAMIC */
118};
119
120static struct usb_interface_descriptor acm_data_interface_desc = {
121	.bLength =		USB_DT_INTERFACE_SIZE,
122	.bDescriptorType =	USB_DT_INTERFACE,
123	/* .bInterfaceNumber = DYNAMIC */
124	.bNumEndpoints =	2,
125	.bInterfaceClass =	USB_CLASS_CDC_DATA,
126	.bInterfaceSubClass =	0,
127	.bInterfaceProtocol =	0,
128	/* .iInterface = DYNAMIC */
129};
130
131static struct usb_cdc_header_desc acm_header_desc = {
132	.bLength =		sizeof(acm_header_desc),
133	.bDescriptorType =	USB_DT_CS_INTERFACE,
134	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
135	.bcdCDC =		cpu_to_le16(0x0110),
136};
137
138static struct usb_cdc_call_mgmt_descriptor
139acm_call_mgmt_descriptor = {
140	.bLength =		sizeof(acm_call_mgmt_descriptor),
141	.bDescriptorType =	USB_DT_CS_INTERFACE,
142	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
143	.bmCapabilities =	0,
144	/* .bDataInterface = DYNAMIC */
145};
146
147static struct usb_cdc_acm_descriptor acm_descriptor = {
148	.bLength =		sizeof(acm_descriptor),
149	.bDescriptorType =	USB_DT_CS_INTERFACE,
150	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
151	.bmCapabilities =	USB_CDC_CAP_LINE,
152};
153
154static struct usb_cdc_union_desc acm_union_desc = {
155	.bLength =		sizeof(acm_union_desc),
156	.bDescriptorType =	USB_DT_CS_INTERFACE,
157	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
158	/* .bMasterInterface0 =	DYNAMIC */
159	/* .bSlaveInterface0 =	DYNAMIC */
160};
161
162/* full speed support: */
163
164static struct usb_endpoint_descriptor acm_fs_notify_desc = {
165	.bLength =		USB_DT_ENDPOINT_SIZE,
166	.bDescriptorType =	USB_DT_ENDPOINT,
167	.bEndpointAddress =	USB_DIR_IN,
168	.bmAttributes =		USB_ENDPOINT_XFER_INT,
169	.wMaxPacketSize =	cpu_to_le16(GS_NOTIFY_MAXPACKET),
170	.bInterval =		1 << GS_LOG2_NOTIFY_INTERVAL,
171};
172
173static struct usb_endpoint_descriptor acm_fs_in_desc = {
174	.bLength =		USB_DT_ENDPOINT_SIZE,
175	.bDescriptorType =	USB_DT_ENDPOINT,
176	.bEndpointAddress =	USB_DIR_IN,
177	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
178};
179
180static struct usb_endpoint_descriptor acm_fs_out_desc = {
181	.bLength =		USB_DT_ENDPOINT_SIZE,
182	.bDescriptorType =	USB_DT_ENDPOINT,
183	.bEndpointAddress =	USB_DIR_OUT,
184	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
185};
186
187static struct usb_descriptor_header *acm_fs_function[] = {
188	(struct usb_descriptor_header *) &acm_iad_descriptor,
189	(struct usb_descriptor_header *) &acm_control_interface_desc,
190	(struct usb_descriptor_header *) &acm_header_desc,
191	(struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
192	(struct usb_descriptor_header *) &acm_descriptor,
193	(struct usb_descriptor_header *) &acm_union_desc,
194	(struct usb_descriptor_header *) &acm_fs_notify_desc,
195	(struct usb_descriptor_header *) &acm_data_interface_desc,
196	(struct usb_descriptor_header *) &acm_fs_in_desc,
197	(struct usb_descriptor_header *) &acm_fs_out_desc,
198	NULL,
199};
200
201/* high speed support: */
202
203static struct usb_endpoint_descriptor acm_hs_notify_desc = {
204	.bLength =		USB_DT_ENDPOINT_SIZE,
205	.bDescriptorType =	USB_DT_ENDPOINT,
206	.bEndpointAddress =	USB_DIR_IN,
207	.bmAttributes =		USB_ENDPOINT_XFER_INT,
208	.wMaxPacketSize =	cpu_to_le16(GS_NOTIFY_MAXPACKET),
209	.bInterval =		GS_LOG2_NOTIFY_INTERVAL+4,
210};
211
212static struct usb_endpoint_descriptor acm_hs_in_desc = {
213	.bLength =		USB_DT_ENDPOINT_SIZE,
214	.bDescriptorType =	USB_DT_ENDPOINT,
215	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
216	.wMaxPacketSize =	cpu_to_le16(512),
217};
218
219static struct usb_endpoint_descriptor acm_hs_out_desc = {
220	.bLength =		USB_DT_ENDPOINT_SIZE,
221	.bDescriptorType =	USB_DT_ENDPOINT,
222	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
223	.wMaxPacketSize =	cpu_to_le16(512),
224};
225
226static struct usb_descriptor_header *acm_hs_function[] = {
227	(struct usb_descriptor_header *) &acm_iad_descriptor,
228	(struct usb_descriptor_header *) &acm_control_interface_desc,
229	(struct usb_descriptor_header *) &acm_header_desc,
230	(struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
231	(struct usb_descriptor_header *) &acm_descriptor,
232	(struct usb_descriptor_header *) &acm_union_desc,
233	(struct usb_descriptor_header *) &acm_hs_notify_desc,
234	(struct usb_descriptor_header *) &acm_data_interface_desc,
235	(struct usb_descriptor_header *) &acm_hs_in_desc,
236	(struct usb_descriptor_header *) &acm_hs_out_desc,
237	NULL,
238};
239
240/* string descriptors: */
241
242#define ACM_CTRL_IDX	0
243#define ACM_DATA_IDX	1
244#define ACM_IAD_IDX	2
245
246/* static strings, in UTF-8 */
247static struct usb_string acm_string_defs[] = {
248	[ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
249	[ACM_DATA_IDX].s = "CDC ACM Data",
250	[ACM_IAD_IDX ].s = "CDC Serial",
251	{  /* ZEROES END LIST */ },
252};
253
254static struct usb_gadget_strings acm_string_table = {
255	.language =		0x0409,	/* en-us */
256	.strings =		acm_string_defs,
257};
258
259static struct usb_gadget_strings *acm_strings[] = {
260	&acm_string_table,
261	NULL,
262};
263
264/*-------------------------------------------------------------------------*/
265
266/* ACM control ... data handling is delegated to tty library code.
267 * The main task of this function is to activate and deactivate
268 * that code based on device state; track parameters like line
269 * speed, handshake state, and so on; and issue notifications.
270 */
271
272static void acm_complete_set_line_coding(struct usb_ep *ep,
273		struct usb_request *req)
274{
275	struct f_acm	*acm = ep->driver_data;
276	struct usb_composite_dev *cdev = acm->port.func.config->cdev;
277
278	if (req->status != 0) {
279		DBG(cdev, "acm ttyGS%d completion, err %d\n",
280				acm->port_num, req->status);
281		return;
282	}
283
284	/* normal completion */
285	if (req->actual != sizeof(acm->port_line_coding)) {
286		DBG(cdev, "acm ttyGS%d short resp, len %d\n",
287				acm->port_num, req->actual);
288		usb_ep_set_halt(ep);
289	} else {
290		struct usb_cdc_line_coding	*value = req->buf;
291
292		/* REVISIT:  we currently just remember this data.
293		 * If we change that, (a) validate it first, then
294		 * (b) update whatever hardware needs updating,
295		 * (c) worry about locking.  This is information on
296		 * the order of 9600-8-N-1 ... most of which means
297		 * nothing unless we control a real RS232 line.
298		 */
299		acm->port_line_coding = *value;
300	}
301}
302
303static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
304{
305	struct f_acm		*acm = func_to_acm(f);
306	struct usb_composite_dev *cdev = f->config->cdev;
307	struct usb_request	*req = cdev->req;
308	int			value = -EOPNOTSUPP;
309	u16			w_index = le16_to_cpu(ctrl->wIndex);
310	u16			w_value = le16_to_cpu(ctrl->wValue);
311	u16			w_length = le16_to_cpu(ctrl->wLength);
312
313	/* composite driver infrastructure handles everything except
314	 * CDC class messages; interface activation uses set_alt().
315	 *
316	 * Note CDC spec table 4 lists the ACM request profile.  It requires
317	 * encapsulated command support ... we don't handle any, and respond
318	 * to them by stalling.  Options include get/set/clear comm features
319	 * (not that useful) and SEND_BREAK.
320	 */
321	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
322
323	/* SET_LINE_CODING ... just read and save what the host sends */
324	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
325			| USB_CDC_REQ_SET_LINE_CODING:
326		if (w_length != sizeof(struct usb_cdc_line_coding)
327				|| w_index != acm->ctrl_id)
328			goto invalid;
329
330		value = w_length;
331		cdev->gadget->ep0->driver_data = acm;
332		req->complete = acm_complete_set_line_coding;
333		break;
334
335	/* GET_LINE_CODING ... return what host sent, or initial value */
336	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
337			| USB_CDC_REQ_GET_LINE_CODING:
338		if (w_index != acm->ctrl_id)
339			goto invalid;
340
341		value = min_t(unsigned, w_length,
342				sizeof(struct usb_cdc_line_coding));
343		memcpy(req->buf, &acm->port_line_coding, value);
344		break;
345
346	/* SET_CONTROL_LINE_STATE ... save what the host sent */
347	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
348			| USB_CDC_REQ_SET_CONTROL_LINE_STATE:
349		if (w_index != acm->ctrl_id)
350			goto invalid;
351
352		value = 0;
353
354		/* FIXME we should not allow data to flow until the
355		 * host sets the ACM_CTRL_DTR bit; and when it clears
356		 * that bit, we should return to that no-flow state.
357		 */
358		acm->port_handshake_bits = w_value;
359		break;
360
361	default:
362invalid:
363		VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
364			ctrl->bRequestType, ctrl->bRequest,
365			w_value, w_index, w_length);
366	}
367
368	/* respond with data transfer or status phase? */
369	if (value >= 0) {
370		DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
371			acm->port_num, ctrl->bRequestType, ctrl->bRequest,
372			w_value, w_index, w_length);
373		req->zero = 0;
374		req->length = value;
375		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
376		if (value < 0)
377			ERROR(cdev, "acm response on ttyGS%d, err %d\n",
378					acm->port_num, value);
379	}
380
381	/* device either stalls (value < 0) or reports success */
382	return value;
383}
384
385static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
386{
387	struct f_acm		*acm = func_to_acm(f);
388	struct usb_composite_dev *cdev = f->config->cdev;
389
390	/* we know alt == 0, so this is an activation or a reset */
391
392	if (intf == acm->ctrl_id) {
393		if (acm->notify->driver_data) {
394			VDBG(cdev, "reset acm control interface %d\n", intf);
395			usb_ep_disable(acm->notify);
396		} else {
397			VDBG(cdev, "init acm ctrl interface %d\n", intf);
398			if (config_ep_by_speed(cdev->gadget, f, acm->notify))
399				return -EINVAL;
400		}
401		usb_ep_enable(acm->notify);
402		acm->notify->driver_data = acm;
403
404	} else if (intf == acm->data_id) {
405		if (acm->port.in->driver_data) {
406			DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
407			gserial_disconnect(&acm->port);
408		}
409		if (!acm->port.in->desc || !acm->port.out->desc) {
410			DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
411			if (config_ep_by_speed(cdev->gadget, f,
412					       acm->port.in) ||
413			    config_ep_by_speed(cdev->gadget, f,
414					       acm->port.out)) {
415				acm->port.in->desc = NULL;
416				acm->port.out->desc = NULL;
417				return -EINVAL;
418			}
419		}
420		gserial_connect(&acm->port, acm->port_num);
421
422	} else
423		return -EINVAL;
424
425	return 0;
426}
427
428static void acm_disable(struct usb_function *f)
429{
430	struct f_acm	*acm = func_to_acm(f);
431	struct usb_composite_dev *cdev = f->config->cdev;
432
433	DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
434	gserial_disconnect(&acm->port);
435	usb_ep_disable(acm->notify);
436	acm->notify->driver_data = NULL;
437}
438
439/*-------------------------------------------------------------------------*/
440
441/**
442 * acm_cdc_notify - issue CDC notification to host
443 * @acm: wraps host to be notified
444 * @type: notification type
445 * @value: Refer to cdc specs, wValue field.
446 * @data: data to be sent
447 * @length: size of data
448 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
449 *
450 * Returns zero on success or a negative errno.
451 *
452 * See section 6.3.5 of the CDC 1.1 specification for information
453 * about the only notification we issue:  SerialState change.
454 */
455static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
456		void *data, unsigned length)
457{
458	struct usb_ep			*ep = acm->notify;
459	struct usb_request		*req;
460	struct usb_cdc_notification	*notify;
461	const unsigned			len = sizeof(*notify) + length;
462	void				*buf;
463	int				status;
464
465	req = acm->notify_req;
466	acm->notify_req = NULL;
467	acm->pending = false;
468
469	req->length = len;
470	notify = req->buf;
471	buf = notify + 1;
472
473	notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
474			| USB_RECIP_INTERFACE;
475	notify->bNotificationType = type;
476	notify->wValue = cpu_to_le16(value);
477	notify->wIndex = cpu_to_le16(acm->ctrl_id);
478	notify->wLength = cpu_to_le16(length);
479	memcpy(buf, data, length);
480
481	/* ep_queue() can complete immediately if it fills the fifo... */
482	spin_unlock(&acm->lock);
483	status = usb_ep_queue(ep, req, GFP_ATOMIC);
484	spin_lock(&acm->lock);
485
486	if (status < 0) {
487		ERROR(acm->port.func.config->cdev,
488				"acm ttyGS%d can't notify serial state, %d\n",
489				acm->port_num, status);
490		acm->notify_req = req;
491	}
492
493	return status;
494}
495
496static int acm_notify_serial_state(struct f_acm *acm)
497{
498	struct usb_composite_dev *cdev = acm->port.func.config->cdev;
499	int			status;
500
501	spin_lock(&acm->lock);
502	if (acm->notify_req) {
503		DBG(cdev, "acm ttyGS%d serial state %04x\n",
504				acm->port_num, acm->serial_state);
505		status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
506				0, &acm->serial_state, sizeof(acm->serial_state));
507	} else {
508		acm->pending = true;
509		status = 0;
510	}
511	spin_unlock(&acm->lock);
512	return status;
513}
514
515static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
516{
517	struct f_acm		*acm = req->context;
518	u8			doit = false;
519
520	/* on this call path we do NOT hold the port spinlock,
521	 * which is why ACM needs its own spinlock
522	 */
523	spin_lock(&acm->lock);
524	if (req->status != -ESHUTDOWN)
525		doit = acm->pending;
526	acm->notify_req = req;
527	spin_unlock(&acm->lock);
528
529	if (doit)
530		acm_notify_serial_state(acm);
531}
532
533/* connect == the TTY link is open */
534
535static void acm_connect(struct gserial *port)
536{
537	struct f_acm		*acm = port_to_acm(port);
538
539	acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
540	acm_notify_serial_state(acm);
541}
542
543static void acm_disconnect(struct gserial *port)
544{
545	struct f_acm		*acm = port_to_acm(port);
546
547	acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
548	acm_notify_serial_state(acm);
549}
550
551static int acm_send_break(struct gserial *port, int duration)
552{
553	struct f_acm		*acm = port_to_acm(port);
554	u16			state;
555
556	state = acm->serial_state;
557	state &= ~ACM_CTRL_BRK;
558	if (duration)
559		state |= ACM_CTRL_BRK;
560
561	acm->serial_state = state;
562	return acm_notify_serial_state(acm);
563}
564
565/*-------------------------------------------------------------------------*/
566
567/* ACM function driver setup/binding */
568static int
569acm_bind(struct usb_configuration *c, struct usb_function *f)
570{
571	struct usb_composite_dev *cdev = c->cdev;
572	struct f_acm		*acm = func_to_acm(f);
573	int			status;
574	struct usb_ep		*ep;
575
576	/* allocate instance-specific interface IDs, and patch descriptors */
577	status = usb_interface_id(c, f);
578	if (status < 0)
579		goto fail;
580	acm->ctrl_id = status;
581	acm_iad_descriptor.bFirstInterface = status;
582
583	acm_control_interface_desc.bInterfaceNumber = status;
584	acm_union_desc .bMasterInterface0 = status;
585
586	status = usb_interface_id(c, f);
587	if (status < 0)
588		goto fail;
589	acm->data_id = status;
590
591	acm_data_interface_desc.bInterfaceNumber = status;
592	acm_union_desc.bSlaveInterface0 = status;
593	acm_call_mgmt_descriptor.bDataInterface = status;
594
595	status = -ENODEV;
596
597	/* allocate instance-specific endpoints */
598	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
599	if (!ep)
600		goto fail;
601	acm->port.in = ep;
602	ep->driver_data = cdev;	/* claim */
603
604	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
605	if (!ep)
606		goto fail;
607	acm->port.out = ep;
608	ep->driver_data = cdev;	/* claim */
609
610	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
611	if (!ep)
612		goto fail;
613	acm->notify = ep;
614	ep->driver_data = cdev;	/* claim */
615
616	/* allocate notification */
617	acm->notify_req = gs_alloc_req(ep,
618			sizeof(struct usb_cdc_notification) + 2,
619			GFP_KERNEL);
620	if (!acm->notify_req)
621		goto fail;
622
623	acm->notify_req->complete = acm_cdc_notify_complete;
624	acm->notify_req->context = acm;
625
626	/* copy descriptors */
627	f->descriptors = usb_copy_descriptors(acm_fs_function);
628	if (!f->descriptors)
629		goto fail;
630
631	/* support all relevant hardware speeds... we expect that when
632	 * hardware is dual speed, all bulk-capable endpoints work at
633	 * both speeds
634	 */
635	if (gadget_is_dualspeed(c->cdev->gadget)) {
636		acm_hs_in_desc.bEndpointAddress =
637				acm_fs_in_desc.bEndpointAddress;
638		acm_hs_out_desc.bEndpointAddress =
639				acm_fs_out_desc.bEndpointAddress;
640		acm_hs_notify_desc.bEndpointAddress =
641				acm_fs_notify_desc.bEndpointAddress;
642
643		/* copy descriptors */
644		f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
645	}
646
647	DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
648			acm->port_num,
649			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
650			acm->port.in->name, acm->port.out->name,
651			acm->notify->name);
652	return 0;
653
654fail:
655	if (acm->notify_req)
656		gs_free_req(acm->notify, acm->notify_req);
657
658	/* we might as well release our claims on endpoints */
659	if (acm->notify)
660		acm->notify->driver_data = NULL;
661	if (acm->port.out)
662		acm->port.out->driver_data = NULL;
663	if (acm->port.in)
664		acm->port.in->driver_data = NULL;
665
666	ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
667
668	return status;
669}
670
671static void
672acm_unbind(struct usb_configuration *c, struct usb_function *f)
673{
674	struct f_acm		*acm = func_to_acm(f);
675
676	if (gadget_is_dualspeed(c->cdev->gadget))
677		usb_free_descriptors(f->hs_descriptors);
678	usb_free_descriptors(f->descriptors);
679	gs_free_req(acm->notify, acm->notify_req);
680	kfree(acm);
681}
682
683/* Some controllers can't support CDC ACM ... */
684static inline bool can_support_cdc(struct usb_configuration *c)
685{
686	/* everything else is *probably* fine ... */
687	return true;
688}
689
690/**
691 * acm_bind_config - add a CDC ACM function to a configuration
692 * @c: the configuration to support the CDC ACM instance
693 * @port_num: /dev/ttyGS* port this interface will use
694 * Context: single threaded during gadget setup
695 *
696 * Returns zero on success, else negative errno.
697 *
698 * Caller must have called @gserial_setup() with enough ports to
699 * handle all the ones it binds.  Caller is also responsible
700 * for calling @gserial_cleanup() before module unload.
701 */
702int acm_bind_config(struct usb_configuration *c, u8 port_num)
703{
704	struct f_acm	*acm;
705	int		status;
706
707	if (!can_support_cdc(c))
708		return -EINVAL;
709
710	/* REVISIT might want instance-specific strings to help
711	 * distinguish instances ...
712	 */
713
714	/* maybe allocate device-global string IDs, and patch descriptors */
715	if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
716		status = usb_string_id(c->cdev);
717		if (status < 0)
718			return status;
719		acm_string_defs[ACM_CTRL_IDX].id = status;
720
721		acm_control_interface_desc.iInterface = status;
722
723		status = usb_string_id(c->cdev);
724		if (status < 0)
725			return status;
726		acm_string_defs[ACM_DATA_IDX].id = status;
727
728		acm_data_interface_desc.iInterface = status;
729
730		status = usb_string_id(c->cdev);
731		if (status < 0)
732			return status;
733		acm_string_defs[ACM_IAD_IDX].id = status;
734
735		acm_iad_descriptor.iFunction = status;
736	}
737
738	/* allocate and initialize one new instance */
739	acm = kzalloc(sizeof *acm, GFP_KERNEL);
740	if (!acm)
741		return -ENOMEM;
742
743	spin_lock_init(&acm->lock);
744
745	acm->port_num = port_num;
746
747	acm->port.connect = acm_connect;
748	acm->port.disconnect = acm_disconnect;
749	acm->port.send_break = acm_send_break;
750
751	acm->port.func.name = "acm";
752	acm->port.func.strings = acm_strings;
753	/* descriptors are per-instance copies */
754	acm->port.func.bind = acm_bind;
755	acm->port.func.unbind = acm_unbind;
756	acm->port.func.set_alt = acm_set_alt;
757	acm->port.func.setup = acm_setup;
758	acm->port.func.disable = acm_disable;
759
760	status = usb_add_function(c, &acm->port.func);
761	if (status)
762		kfree(acm);
763	return status;
764}