File Index
Files and directories
Sign in
FILE

f_acm.c

File preview with syntax highlighting.

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