Linux Audio

Check our new training course

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