xref: /linux/drivers/usb/gadget/function/f_acm.c (revision 19b3b13c932fc8d613e50e3e92c1944f9fcc02c7)
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 
40 struct 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 
74 static inline struct f_acm *func_to_acm(struct usb_function *f)
75 {
76 	return container_of(f, struct f_acm, port.func);
77 }
78 
79 static 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 
93 static struct usb_interface_assoc_descriptor
94 acm_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 
107 static 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 
118 static 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 
129 static 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 
136 static struct usb_cdc_call_mgmt_descriptor
137 acm_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 
145 static 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 
152 static 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 
162 static 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 
171 static 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 
178 static 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 
185 static 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: */
200 static 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 
209 static 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 
216 static 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 
223 static 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 
237 static 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 
244 static 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 
251 static 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 
256 static 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 */
280 static 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 
287 static struct usb_gadget_strings acm_string_table = {
288 	.language =		0x0409,	/* en-us */
289 	.strings =		acm_string_defs,
290 };
291 
292 static 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 
305 static 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 
336 static int acm_send_break(struct gserial *port, int duration);
337 
338 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
339 {
340 	struct f_acm		*acm = func_to_acm(f);
341 	struct usb_composite_dev *cdev = f->config->cdev;
342 	struct usb_request	*req = cdev->req;
343 	int			value = -EOPNOTSUPP;
344 	u16			w_index = le16_to_cpu(ctrl->wIndex);
345 	u16			w_value = le16_to_cpu(ctrl->wValue);
346 	u16			w_length = le16_to_cpu(ctrl->wLength);
347 
348 	/* composite driver infrastructure handles everything except
349 	 * CDC class messages; interface activation uses set_alt().
350 	 *
351 	 * Note CDC spec table 4 lists the ACM request profile.  It requires
352 	 * encapsulated command support ... we don't handle any, and respond
353 	 * to them by stalling.  Options include get/set/clear comm features
354 	 * (not that useful) and SEND_BREAK.
355 	 */
356 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
357 
358 	/* SET_LINE_CODING ... just read and save what the host sends */
359 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
360 			| USB_CDC_REQ_SET_LINE_CODING:
361 		if (w_length != sizeof(struct usb_cdc_line_coding)
362 				|| w_index != acm->ctrl_id)
363 			goto invalid;
364 
365 		value = w_length;
366 		cdev->gadget->ep0->driver_data = acm;
367 		req->complete = acm_complete_set_line_coding;
368 		break;
369 
370 	/* GET_LINE_CODING ... return what host sent, or initial value */
371 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
372 			| USB_CDC_REQ_GET_LINE_CODING:
373 		if (w_index != acm->ctrl_id)
374 			goto invalid;
375 
376 		value = min_t(unsigned, w_length,
377 				sizeof(struct usb_cdc_line_coding));
378 		memcpy(req->buf, &acm->port_line_coding, value);
379 		break;
380 
381 	/* SET_CONTROL_LINE_STATE ... save what the host sent */
382 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
383 			| USB_CDC_REQ_SET_CONTROL_LINE_STATE:
384 		if (w_index != acm->ctrl_id)
385 			goto invalid;
386 
387 		value = 0;
388 
389 		/* FIXME we should not allow data to flow until the
390 		 * host sets the ACM_CTRL_DTR bit; and when it clears
391 		 * that bit, we should return to that no-flow state.
392 		 */
393 		acm->port_handshake_bits = w_value;
394 		break;
395 
396 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
397 			| USB_CDC_REQ_SEND_BREAK:
398 		if (w_index != acm->ctrl_id)
399 			goto invalid;
400 
401 		acm_send_break(&acm->port, w_value);
402 		break;
403 
404 	default:
405 invalid:
406 		dev_vdbg(&cdev->gadget->dev,
407 			 "invalid control req%02x.%02x v%04x i%04x l%d\n",
408 			 ctrl->bRequestType, ctrl->bRequest,
409 			 w_value, w_index, w_length);
410 	}
411 
412 	/* respond with data transfer or status phase? */
413 	if (value >= 0) {
414 		dev_dbg(&cdev->gadget->dev,
415 			"acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
416 			acm->port_num, ctrl->bRequestType, ctrl->bRequest,
417 			w_value, w_index, w_length);
418 		req->zero = 0;
419 		req->length = value;
420 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
421 		if (value < 0)
422 			ERROR(cdev, "acm response on ttyGS%d, err %d\n",
423 					acm->port_num, value);
424 	}
425 
426 	/* device either stalls (value < 0) or reports success */
427 	return value;
428 }
429 
430 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
431 {
432 	struct f_acm		*acm = func_to_acm(f);
433 	struct usb_composite_dev *cdev = f->config->cdev;
434 
435 	/* we know alt == 0, so this is an activation or a reset */
436 
437 	if (intf == acm->ctrl_id) {
438 		if (acm->notify->enabled) {
439 			dev_vdbg(&cdev->gadget->dev,
440 					"reset acm control interface %d\n", intf);
441 			usb_ep_disable(acm->notify);
442 		}
443 
444 		if (!acm->notify->desc)
445 			if (config_ep_by_speed(cdev->gadget, f, acm->notify))
446 				return -EINVAL;
447 
448 		usb_ep_enable(acm->notify);
449 
450 	} else if (intf == acm->data_id) {
451 		if (acm->notify->enabled) {
452 			dev_dbg(&cdev->gadget->dev,
453 				"reset acm ttyGS%d\n", acm->port_num);
454 			gserial_disconnect(&acm->port);
455 		}
456 		if (!acm->port.in->desc || !acm->port.out->desc) {
457 			dev_dbg(&cdev->gadget->dev,
458 				"activate acm ttyGS%d\n", acm->port_num);
459 			if (config_ep_by_speed(cdev->gadget, f,
460 					       acm->port.in) ||
461 			    config_ep_by_speed(cdev->gadget, f,
462 					       acm->port.out)) {
463 				acm->port.in->desc = NULL;
464 				acm->port.out->desc = NULL;
465 				return -EINVAL;
466 			}
467 		}
468 		gserial_connect(&acm->port, acm->port_num);
469 
470 	} else
471 		return -EINVAL;
472 
473 	return 0;
474 }
475 
476 static void acm_disable(struct usb_function *f)
477 {
478 	struct f_acm	*acm = func_to_acm(f);
479 	struct usb_composite_dev *cdev = f->config->cdev;
480 
481 	dev_dbg(&cdev->gadget->dev, "acm ttyGS%d deactivated\n", acm->port_num);
482 	gserial_disconnect(&acm->port);
483 	usb_ep_disable(acm->notify);
484 }
485 
486 /*-------------------------------------------------------------------------*/
487 
488 /**
489  * acm_cdc_notify - issue CDC notification to host
490  * @acm: wraps host to be notified
491  * @type: notification type
492  * @value: Refer to cdc specs, wValue field.
493  * @data: data to be sent
494  * @length: size of data
495  * Context: irqs blocked, acm->lock held, acm_notify_req non-null
496  *
497  * Returns zero on success or a negative errno.
498  *
499  * See section 6.3.5 of the CDC 1.1 specification for information
500  * about the only notification we issue:  SerialState change.
501  */
502 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
503 		void *data, unsigned length)
504 {
505 	struct usb_ep			*ep = acm->notify;
506 	struct usb_request		*req;
507 	struct usb_cdc_notification	*notify;
508 	const unsigned			len = sizeof(*notify) + length;
509 	void				*buf;
510 	int				status;
511 
512 	req = acm->notify_req;
513 	acm->notify_req = NULL;
514 	acm->pending = false;
515 
516 	req->length = len;
517 	notify = req->buf;
518 	buf = notify + 1;
519 
520 	notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
521 			| USB_RECIP_INTERFACE;
522 	notify->bNotificationType = type;
523 	notify->wValue = cpu_to_le16(value);
524 	notify->wIndex = cpu_to_le16(acm->ctrl_id);
525 	notify->wLength = cpu_to_le16(length);
526 	memcpy(buf, data, length);
527 
528 	/* ep_queue() can complete immediately if it fills the fifo... */
529 	spin_unlock(&acm->lock);
530 	status = usb_ep_queue(ep, req, GFP_ATOMIC);
531 	spin_lock(&acm->lock);
532 
533 	if (status < 0) {
534 		ERROR(acm->port.func.config->cdev,
535 				"acm ttyGS%d can't notify serial state, %d\n",
536 				acm->port_num, status);
537 		acm->notify_req = req;
538 	}
539 
540 	return status;
541 }
542 
543 static int acm_notify_serial_state(struct f_acm *acm)
544 {
545 	struct usb_composite_dev *cdev = acm->port.func.config->cdev;
546 	int			status;
547 	__le16			serial_state;
548 
549 	spin_lock(&acm->lock);
550 	if (acm->notify_req) {
551 		dev_dbg(&cdev->gadget->dev, "acm ttyGS%d serial state %04x\n",
552 			acm->port_num, acm->serial_state);
553 		serial_state = cpu_to_le16(acm->serial_state);
554 		status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
555 				0, &serial_state, sizeof(acm->serial_state));
556 	} else {
557 		acm->pending = true;
558 		status = 0;
559 	}
560 	spin_unlock(&acm->lock);
561 	return status;
562 }
563 
564 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
565 {
566 	struct f_acm		*acm = req->context;
567 	u8			doit = false;
568 
569 	/* on this call path we do NOT hold the port spinlock,
570 	 * which is why ACM needs its own spinlock
571 	 */
572 	spin_lock(&acm->lock);
573 	if (req->status != -ESHUTDOWN)
574 		doit = acm->pending;
575 	acm->notify_req = req;
576 	spin_unlock(&acm->lock);
577 
578 	if (doit)
579 		acm_notify_serial_state(acm);
580 }
581 
582 /* connect == the TTY link is open */
583 
584 static void acm_connect(struct gserial *port)
585 {
586 	struct f_acm		*acm = port_to_acm(port);
587 
588 	acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
589 	acm_notify_serial_state(acm);
590 }
591 
592 static void acm_disconnect(struct gserial *port)
593 {
594 	struct f_acm		*acm = port_to_acm(port);
595 
596 	acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
597 	acm_notify_serial_state(acm);
598 }
599 
600 static int acm_send_break(struct gserial *port, int duration)
601 {
602 	struct f_acm		*acm = port_to_acm(port);
603 	u16			state;
604 
605 	state = acm->serial_state;
606 	state &= ~ACM_CTRL_BRK;
607 	if (duration)
608 		state |= ACM_CTRL_BRK;
609 
610 	acm->serial_state = state;
611 	return acm_notify_serial_state(acm);
612 }
613 
614 /*-------------------------------------------------------------------------*/
615 
616 /* ACM function driver setup/binding */
617 static int
618 acm_bind(struct usb_configuration *c, struct usb_function *f)
619 {
620 	struct usb_composite_dev *cdev = c->cdev;
621 	struct f_acm		*acm = func_to_acm(f);
622 	struct usb_string	*us;
623 	int			status;
624 	struct usb_ep		*ep;
625 
626 	/* REVISIT might want instance-specific strings to help
627 	 * distinguish instances ...
628 	 */
629 
630 	/* maybe allocate device-global string IDs, and patch descriptors */
631 	us = usb_gstrings_attach(cdev, acm_strings,
632 			ARRAY_SIZE(acm_string_defs));
633 	if (IS_ERR(us))
634 		return PTR_ERR(us);
635 	acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id;
636 	acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;
637 	acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;
638 
639 	/* allocate instance-specific interface IDs, and patch descriptors */
640 	status = usb_interface_id(c, f);
641 	if (status < 0)
642 		goto fail;
643 	acm->ctrl_id = status;
644 	acm_iad_descriptor.bFirstInterface = status;
645 
646 	acm_control_interface_desc.bInterfaceNumber = status;
647 	acm_union_desc .bMasterInterface0 = status;
648 
649 	status = usb_interface_id(c, f);
650 	if (status < 0)
651 		goto fail;
652 	acm->data_id = status;
653 
654 	acm_data_interface_desc.bInterfaceNumber = status;
655 	acm_union_desc.bSlaveInterface0 = status;
656 	acm_call_mgmt_descriptor.bDataInterface = status;
657 
658 	status = -ENODEV;
659 
660 	/* allocate instance-specific endpoints */
661 	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
662 	if (!ep)
663 		goto fail;
664 	acm->port.in = ep;
665 
666 	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
667 	if (!ep)
668 		goto fail;
669 	acm->port.out = ep;
670 
671 	ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
672 	if (!ep)
673 		goto fail;
674 	acm->notify = ep;
675 
676 	/* allocate notification */
677 	acm->notify_req = gs_alloc_req(ep,
678 			sizeof(struct usb_cdc_notification) + 2,
679 			GFP_KERNEL);
680 	if (!acm->notify_req)
681 		goto fail;
682 
683 	acm->notify_req->complete = acm_cdc_notify_complete;
684 	acm->notify_req->context = acm;
685 
686 	/* support all relevant hardware speeds... we expect that when
687 	 * hardware is dual speed, all bulk-capable endpoints work at
688 	 * both speeds
689 	 */
690 	acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
691 	acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
692 	acm_hs_notify_desc.bEndpointAddress =
693 		acm_fs_notify_desc.bEndpointAddress;
694 
695 	acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
696 	acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
697 
698 	status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
699 			acm_ss_function, acm_ss_function);
700 	if (status)
701 		goto fail;
702 
703 	dev_dbg(&cdev->gadget->dev,
704 		"acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
705 		acm->port_num,
706 		gadget_is_superspeed(c->cdev->gadget) ? "super" :
707 		gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
708 		acm->port.in->name, acm->port.out->name,
709 		acm->notify->name);
710 	return 0;
711 
712 fail:
713 	if (acm->notify_req)
714 		gs_free_req(acm->notify, acm->notify_req);
715 
716 	ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
717 
718 	return status;
719 }
720 
721 static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
722 {
723 	struct f_acm		*acm = func_to_acm(f);
724 
725 	acm_string_defs[0].id = 0;
726 	usb_free_all_descriptors(f);
727 	if (acm->notify_req)
728 		gs_free_req(acm->notify, acm->notify_req);
729 }
730 
731 static void acm_free_func(struct usb_function *f)
732 {
733 	struct f_acm		*acm = func_to_acm(f);
734 
735 	kfree(acm);
736 }
737 
738 static void acm_resume(struct usb_function *f)
739 {
740 	struct f_acm *acm = func_to_acm(f);
741 
742 	gserial_resume(&acm->port);
743 }
744 
745 static void acm_suspend(struct usb_function *f)
746 {
747 	struct f_acm *acm = func_to_acm(f);
748 
749 	gserial_suspend(&acm->port);
750 }
751 
752 static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
753 {
754 	struct f_serial_opts *opts;
755 	struct f_acm *acm;
756 
757 	acm = kzalloc(sizeof(*acm), GFP_KERNEL);
758 	if (!acm)
759 		return ERR_PTR(-ENOMEM);
760 
761 	spin_lock_init(&acm->lock);
762 
763 	acm->port.connect = acm_connect;
764 	acm->port.disconnect = acm_disconnect;
765 	acm->port.send_break = acm_send_break;
766 
767 	acm->port.func.name = "acm";
768 	acm->port.func.strings = acm_strings;
769 	/* descriptors are per-instance copies */
770 	acm->port.func.bind = acm_bind;
771 	acm->port.func.set_alt = acm_set_alt;
772 	acm->port.func.setup = acm_setup;
773 	acm->port.func.disable = acm_disable;
774 
775 	opts = container_of(fi, struct f_serial_opts, func_inst);
776 	acm->port_num = opts->port_num;
777 	acm->port.func.unbind = acm_unbind;
778 	acm->port.func.free_func = acm_free_func;
779 	acm->port.func.resume = acm_resume;
780 	acm->port.func.suspend = acm_suspend;
781 
782 	return &acm->port.func;
783 }
784 
785 static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
786 {
787 	return container_of(to_config_group(item), struct f_serial_opts,
788 			func_inst.group);
789 }
790 
791 static void acm_attr_release(struct config_item *item)
792 {
793 	struct f_serial_opts *opts = to_f_serial_opts(item);
794 
795 	usb_put_function_instance(&opts->func_inst);
796 }
797 
798 static struct configfs_item_operations acm_item_ops = {
799 	.release                = acm_attr_release,
800 };
801 
802 #ifdef CONFIG_U_SERIAL_CONSOLE
803 
804 static ssize_t f_acm_console_store(struct config_item *item,
805 		const char *page, size_t count)
806 {
807 	return gserial_set_console(to_f_serial_opts(item)->port_num,
808 				   page, count);
809 }
810 
811 static ssize_t f_acm_console_show(struct config_item *item, char *page)
812 {
813 	return gserial_get_console(to_f_serial_opts(item)->port_num, page);
814 }
815 
816 CONFIGFS_ATTR(f_acm_, console);
817 
818 #endif /* CONFIG_U_SERIAL_CONSOLE */
819 
820 static ssize_t f_acm_port_num_show(struct config_item *item, char *page)
821 {
822 	return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
823 }
824 
825 CONFIGFS_ATTR_RO(f_acm_, port_num);
826 
827 static struct configfs_attribute *acm_attrs[] = {
828 #ifdef CONFIG_U_SERIAL_CONSOLE
829 	&f_acm_attr_console,
830 #endif
831 	&f_acm_attr_port_num,
832 	NULL,
833 };
834 
835 static const struct config_item_type acm_func_type = {
836 	.ct_item_ops    = &acm_item_ops,
837 	.ct_attrs	= acm_attrs,
838 	.ct_owner       = THIS_MODULE,
839 };
840 
841 static void acm_free_instance(struct usb_function_instance *fi)
842 {
843 	struct f_serial_opts *opts;
844 
845 	opts = container_of(fi, struct f_serial_opts, func_inst);
846 	gserial_free_line(opts->port_num);
847 	kfree(opts);
848 }
849 
850 static struct usb_function_instance *acm_alloc_instance(void)
851 {
852 	struct f_serial_opts *opts;
853 	int ret;
854 
855 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
856 	if (!opts)
857 		return ERR_PTR(-ENOMEM);
858 	opts->func_inst.free_func_inst = acm_free_instance;
859 	ret = gserial_alloc_line(&opts->port_num);
860 	if (ret) {
861 		kfree(opts);
862 		return ERR_PTR(ret);
863 	}
864 	config_group_init_type_name(&opts->func_inst.group, "",
865 			&acm_func_type);
866 	return &opts->func_inst;
867 }
868 DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
869 MODULE_LICENSE("GPL");
870