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