]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/usb/gadget/function/f_acm.c
ACM gadget: fix endianness in notifications
[mirror_ubuntu-zesty-kernel.git] / drivers / usb / gadget / function / 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 */
16
5a0e3ad6 17#include <linux/slab.h>
4d5a73dc 18#include <linux/kernel.h>
ff47f594 19#include <linux/module.h>
4d5a73dc 20#include <linux/device.h>
ff47f594 21#include <linux/err.h>
4d5a73dc
DB
22
23#include "u_serial.h"
4d5a73dc
DB
24
25
26/*
27 * This CDC ACM function support just wraps control functions and
28 * notifications around the generic serial-over-usb code.
29 *
30 * Because CDC ACM is standardized by the USB-IF, many host operating
31 * systems have drivers for it. Accordingly, ACM is the preferred
32 * interop solution for serial-port type connections. The control
33 * models are often not necessary, and in any case don't do much in
34 * this bare-bones implementation.
35 *
36 * Note that even MS-Windows has some support for ACM. However, that
37 * support is somewhat broken because when you use ACM in a composite
38 * device, having multiple interfaces confuses the poor OS. It doesn't
39 * seem to understand CDC Union descriptors. The new "association"
40 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
41 */
42
4d5a73dc
DB
43struct f_acm {
44 struct gserial port;
45 u8 ctrl_id, data_id;
46 u8 port_num;
47
1f1ba11b
DB
48 u8 pending;
49
50 /* lock is mostly for pending and notify_req ... they get accessed
51 * by callbacks both from tty (open/close/break) under its spinlock,
52 * and notify_req.complete() which can't use that lock.
53 */
54 spinlock_t lock;
55
4d5a73dc 56 struct usb_ep *notify;
1f1ba11b 57 struct usb_request *notify_req;
4d5a73dc
DB
58
59 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
1f1ba11b
DB
60
61 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
4d5a73dc 62 u16 port_handshake_bits;
1f1ba11b
DB
63#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
64#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
65
66 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
67 u16 serial_state;
68#define ACM_CTRL_OVERRUN (1 << 6)
69#define ACM_CTRL_PARITY (1 << 5)
70#define ACM_CTRL_FRAMING (1 << 4)
71#define ACM_CTRL_RI (1 << 3)
72#define ACM_CTRL_BRK (1 << 2)
73#define ACM_CTRL_DSR (1 << 1)
74#define ACM_CTRL_DCD (1 << 0)
4d5a73dc
DB
75};
76
77static inline struct f_acm *func_to_acm(struct usb_function *f)
78{
79 return container_of(f, struct f_acm, port.func);
80}
81
1f1ba11b
DB
82static inline struct f_acm *port_to_acm(struct gserial *p)
83{
84 return container_of(p, struct f_acm, port);
85}
86
4d5a73dc
DB
87/*-------------------------------------------------------------------------*/
88
89/* notification endpoint uses smallish and infrequent fixed-size messages */
90
bcb2f99c 91#define GS_NOTIFY_INTERVAL_MS 32
1f1ba11b 92#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
4d5a73dc
DB
93
94/* interface and class descriptors: */
95
b97503ff
MN
96static struct usb_interface_assoc_descriptor
97acm_iad_descriptor = {
98 .bLength = sizeof acm_iad_descriptor,
99 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
100
101 /* .bFirstInterface = DYNAMIC, */
102 .bInterfaceCount = 2, // control + data
103 .bFunctionClass = USB_CLASS_COMM,
104 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
5c8db070 105 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
b97503ff
MN
106 /* .iFunction = DYNAMIC */
107};
108
109
28824b18 110static struct usb_interface_descriptor acm_control_interface_desc = {
4d5a73dc
DB
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 /* .bInterfaceNumber = DYNAMIC */
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_COMM,
116 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
118 /* .iInterface = DYNAMIC */
119};
120
28824b18 121static struct usb_interface_descriptor acm_data_interface_desc = {
4d5a73dc
DB
122 .bLength = USB_DT_INTERFACE_SIZE,
123 .bDescriptorType = USB_DT_INTERFACE,
124 /* .bInterfaceNumber = DYNAMIC */
125 .bNumEndpoints = 2,
126 .bInterfaceClass = USB_CLASS_CDC_DATA,
127 .bInterfaceSubClass = 0,
128 .bInterfaceProtocol = 0,
129 /* .iInterface = DYNAMIC */
130};
131
28824b18 132static struct usb_cdc_header_desc acm_header_desc = {
4d5a73dc
DB
133 .bLength = sizeof(acm_header_desc),
134 .bDescriptorType = USB_DT_CS_INTERFACE,
135 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
551509d2 136 .bcdCDC = cpu_to_le16(0x0110),
4d5a73dc
DB
137};
138
139static struct usb_cdc_call_mgmt_descriptor
28824b18 140acm_call_mgmt_descriptor = {
4d5a73dc
DB
141 .bLength = sizeof(acm_call_mgmt_descriptor),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
144 .bmCapabilities = 0,
145 /* .bDataInterface = DYNAMIC */
146};
147
28824b18 148static struct usb_cdc_acm_descriptor acm_descriptor = {
4d5a73dc
DB
149 .bLength = sizeof(acm_descriptor),
150 .bDescriptorType = USB_DT_CS_INTERFACE,
151 .bDescriptorSubType = USB_CDC_ACM_TYPE,
1f1ba11b 152 .bmCapabilities = USB_CDC_CAP_LINE,
4d5a73dc
DB
153};
154
28824b18 155static struct usb_cdc_union_desc acm_union_desc = {
4d5a73dc
DB
156 .bLength = sizeof(acm_union_desc),
157 .bDescriptorType = USB_DT_CS_INTERFACE,
158 .bDescriptorSubType = USB_CDC_UNION_TYPE,
159 /* .bMasterInterface0 = DYNAMIC */
160 /* .bSlaveInterface0 = DYNAMIC */
161};
162
163/* full speed support: */
164
28824b18 165static struct usb_endpoint_descriptor acm_fs_notify_desc = {
4d5a73dc
DB
166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
168 .bEndpointAddress = USB_DIR_IN,
169 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 170 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
bcb2f99c 171 .bInterval = GS_NOTIFY_INTERVAL_MS,
4d5a73dc
DB
172};
173
28824b18 174static struct usb_endpoint_descriptor acm_fs_in_desc = {
4d5a73dc
DB
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179};
180
28824b18 181static struct usb_endpoint_descriptor acm_fs_out_desc = {
4d5a73dc
DB
182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_OUT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
186};
187
28824b18 188static struct usb_descriptor_header *acm_fs_function[] = {
b97503ff 189 (struct usb_descriptor_header *) &acm_iad_descriptor,
4d5a73dc
DB
190 (struct usb_descriptor_header *) &acm_control_interface_desc,
191 (struct usb_descriptor_header *) &acm_header_desc,
192 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
193 (struct usb_descriptor_header *) &acm_descriptor,
194 (struct usb_descriptor_header *) &acm_union_desc,
195 (struct usb_descriptor_header *) &acm_fs_notify_desc,
196 (struct usb_descriptor_header *) &acm_data_interface_desc,
197 (struct usb_descriptor_header *) &acm_fs_in_desc,
198 (struct usb_descriptor_header *) &acm_fs_out_desc,
199 NULL,
200};
201
202/* high speed support: */
28824b18 203static struct usb_endpoint_descriptor acm_hs_notify_desc = {
4d5a73dc
DB
204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 208 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
bcb2f99c 209 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
4d5a73dc
DB
210};
211
28824b18 212static struct usb_endpoint_descriptor acm_hs_in_desc = {
4d5a73dc
DB
213 .bLength = USB_DT_ENDPOINT_SIZE,
214 .bDescriptorType = USB_DT_ENDPOINT,
215 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 216 .wMaxPacketSize = cpu_to_le16(512),
4d5a73dc
DB
217};
218
28824b18 219static struct usb_endpoint_descriptor acm_hs_out_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_descriptor_header *acm_hs_function[] = {
b97503ff 227 (struct usb_descriptor_header *) &acm_iad_descriptor,
4d5a73dc
DB
228 (struct usb_descriptor_header *) &acm_control_interface_desc,
229 (struct usb_descriptor_header *) &acm_header_desc,
230 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
231 (struct usb_descriptor_header *) &acm_descriptor,
232 (struct usb_descriptor_header *) &acm_union_desc,
233 (struct usb_descriptor_header *) &acm_hs_notify_desc,
234 (struct usb_descriptor_header *) &acm_data_interface_desc,
235 (struct usb_descriptor_header *) &acm_hs_in_desc,
236 (struct usb_descriptor_header *) &acm_hs_out_desc,
237 NULL,
238};
239
6fecfb05
SAS
240static struct usb_endpoint_descriptor acm_ss_in_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
243 .bmAttributes = USB_ENDPOINT_XFER_BULK,
244 .wMaxPacketSize = cpu_to_le16(1024),
245};
246
247static struct usb_endpoint_descriptor acm_ss_out_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_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
255 .bLength = sizeof acm_ss_bulk_comp_desc,
256 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
257};
258
259static struct usb_descriptor_header *acm_ss_function[] = {
260 (struct usb_descriptor_header *) &acm_iad_descriptor,
261 (struct usb_descriptor_header *) &acm_control_interface_desc,
262 (struct usb_descriptor_header *) &acm_header_desc,
263 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
264 (struct usb_descriptor_header *) &acm_descriptor,
265 (struct usb_descriptor_header *) &acm_union_desc,
266 (struct usb_descriptor_header *) &acm_hs_notify_desc,
267 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
268 (struct usb_descriptor_header *) &acm_data_interface_desc,
269 (struct usb_descriptor_header *) &acm_ss_in_desc,
270 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
271 (struct usb_descriptor_header *) &acm_ss_out_desc,
272 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
273 NULL,
274};
275
4d5a73dc
DB
276/* string descriptors: */
277
278#define ACM_CTRL_IDX 0
279#define ACM_DATA_IDX 1
b97503ff 280#define ACM_IAD_IDX 2
4d5a73dc
DB
281
282/* static strings, in UTF-8 */
283static struct usb_string acm_string_defs[] = {
284 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
285 [ACM_DATA_IDX].s = "CDC ACM Data",
b97503ff 286 [ACM_IAD_IDX ].s = "CDC Serial",
d2572218 287 { } /* end of list */
4d5a73dc
DB
288};
289
290static struct usb_gadget_strings acm_string_table = {
291 .language = 0x0409, /* en-us */
292 .strings = acm_string_defs,
293};
294
295static struct usb_gadget_strings *acm_strings[] = {
296 &acm_string_table,
297 NULL,
298};
299
300/*-------------------------------------------------------------------------*/
301
302/* ACM control ... data handling is delegated to tty library code.
303 * The main task of this function is to activate and deactivate
304 * that code based on device state; track parameters like line
305 * speed, handshake state, and so on; and issue notifications.
306 */
307
308static void acm_complete_set_line_coding(struct usb_ep *ep,
309 struct usb_request *req)
310{
311 struct f_acm *acm = ep->driver_data;
312 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
313
314 if (req->status != 0) {
b8b0ea51
RL
315 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d completion, err %d\n",
316 acm->port_num, req->status);
4d5a73dc
DB
317 return;
318 }
319
320 /* normal completion */
321 if (req->actual != sizeof(acm->port_line_coding)) {
b8b0ea51
RL
322 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d short resp, len %d\n",
323 acm->port_num, req->actual);
4d5a73dc
DB
324 usb_ep_set_halt(ep);
325 } else {
326 struct usb_cdc_line_coding *value = req->buf;
327
328 /* REVISIT: we currently just remember this data.
329 * If we change that, (a) validate it first, then
330 * (b) update whatever hardware needs updating,
331 * (c) worry about locking. This is information on
332 * the order of 9600-8-N-1 ... most of which means
333 * nothing unless we control a real RS232 line.
334 */
335 acm->port_line_coding = *value;
336 }
337}
338
339static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
340{
341 struct f_acm *acm = func_to_acm(f);
342 struct usb_composite_dev *cdev = f->config->cdev;
343 struct usb_request *req = cdev->req;
344 int value = -EOPNOTSUPP;
345 u16 w_index = le16_to_cpu(ctrl->wIndex);
346 u16 w_value = le16_to_cpu(ctrl->wValue);
347 u16 w_length = le16_to_cpu(ctrl->wLength);
348
349 /* composite driver infrastructure handles everything except
350 * CDC class messages; interface activation uses set_alt().
1f1ba11b
DB
351 *
352 * Note CDC spec table 4 lists the ACM request profile. It requires
353 * encapsulated command support ... we don't handle any, and respond
354 * to them by stalling. Options include get/set/clear comm features
355 * (not that useful) and SEND_BREAK.
4d5a73dc
DB
356 */
357 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
358
359 /* SET_LINE_CODING ... just read and save what the host sends */
360 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
361 | USB_CDC_REQ_SET_LINE_CODING:
362 if (w_length != sizeof(struct usb_cdc_line_coding)
363 || w_index != acm->ctrl_id)
364 goto invalid;
365
366 value = w_length;
367 cdev->gadget->ep0->driver_data = acm;
368 req->complete = acm_complete_set_line_coding;
369 break;
370
371 /* GET_LINE_CODING ... return what host sent, or initial value */
372 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
373 | USB_CDC_REQ_GET_LINE_CODING:
374 if (w_index != acm->ctrl_id)
375 goto invalid;
376
377 value = min_t(unsigned, w_length,
378 sizeof(struct usb_cdc_line_coding));
379 memcpy(req->buf, &acm->port_line_coding, value);
380 break;
381
382 /* SET_CONTROL_LINE_STATE ... save what the host sent */
383 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
384 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
385 if (w_index != acm->ctrl_id)
386 goto invalid;
387
388 value = 0;
389
390 /* FIXME we should not allow data to flow until the
1f1ba11b 391 * host sets the ACM_CTRL_DTR bit; and when it clears
4d5a73dc
DB
392 * that bit, we should return to that no-flow state.
393 */
394 acm->port_handshake_bits = w_value;
395 break;
396
397 default:
398invalid:
b8b0ea51
RL
399 dev_vdbg(&cdev->gadget->dev,
400 "invalid control req%02x.%02x v%04x i%04x l%d\n",
401 ctrl->bRequestType, ctrl->bRequest,
402 w_value, w_index, w_length);
4d5a73dc
DB
403 }
404
405 /* respond with data transfer or status phase? */
406 if (value >= 0) {
b8b0ea51
RL
407 dev_dbg(&cdev->gadget->dev,
408 "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
4d5a73dc
DB
409 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
410 w_value, w_index, w_length);
411 req->zero = 0;
412 req->length = value;
413 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
414 if (value < 0)
415 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
416 acm->port_num, value);
417 }
418
419 /* device either stalls (value < 0) or reports success */
420 return value;
421}
422
423static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
424{
425 struct f_acm *acm = func_to_acm(f);
426 struct usb_composite_dev *cdev = f->config->cdev;
427
428 /* we know alt == 0, so this is an activation or a reset */
429
430 if (intf == acm->ctrl_id) {
4aab757c
RB
431 dev_vdbg(&cdev->gadget->dev,
432 "reset acm control interface %d\n", intf);
433 usb_ep_disable(acm->notify);
52ec49a5
FB
434
435 if (!acm->notify->desc)
ea2a1df7
TB
436 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
437 return -EINVAL;
52ec49a5 438
72c973dd 439 usb_ep_enable(acm->notify);
4d5a73dc
DB
440
441 } else if (intf == acm->data_id) {
4aab757c 442 if (acm->notify->enabled) {
b8b0ea51
RL
443 dev_dbg(&cdev->gadget->dev,
444 "reset acm ttyGS%d\n", acm->port_num);
4d5a73dc 445 gserial_disconnect(&acm->port);
ea2a1df7
TB
446 }
447 if (!acm->port.in->desc || !acm->port.out->desc) {
b8b0ea51
RL
448 dev_dbg(&cdev->gadget->dev,
449 "activate acm ttyGS%d\n", acm->port_num);
ea2a1df7
TB
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 }
4d5a73dc
DB
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
b8b0ea51 472 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d deactivated\n", acm->port_num);
4d5a73dc
DB
473 gserial_disconnect(&acm->port);
474 usb_ep_disable(acm->notify);
4d5a73dc
DB
475}
476
477/*-------------------------------------------------------------------------*/
478
1f1ba11b
DB
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 *
af901ca1 488 * Returns zero on success or a negative errno.
1f1ba11b
DB
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
e50ae572
DB
519 /* ep_queue() can complete immediately if it fills the fifo... */
520 spin_unlock(&acm->lock);
1f1ba11b 521 status = usb_ep_queue(ep, req, GFP_ATOMIC);
e50ae572
DB
522 spin_lock(&acm->lock);
523
1f1ba11b
DB
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;
aceff2ce 538 __le16 serial_state;
1f1ba11b
DB
539
540 spin_lock(&acm->lock);
541 if (acm->notify_req) {
b8b0ea51
RL
542 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d serial state %04x\n",
543 acm->port_num, acm->serial_state);
aceff2ce 544 serial_state = cpu_to_le16(acm->serial_state);
1f1ba11b 545 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
aceff2ce 546 0, &serial_state, sizeof(acm->serial_state));
1f1ba11b
DB
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 |= ACM_CTRL_DSR | ACM_CTRL_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 &= ~(ACM_CTRL_DSR | ACM_CTRL_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 &= ~ACM_CTRL_BRK;
598 if (duration)
599 state |= ACM_CTRL_BRK;
600
601 acm->serial_state = state;
602 return acm_notify_serial_state(acm);
603}
604
605/*-------------------------------------------------------------------------*/
606
4d5a73dc 607/* ACM function driver setup/binding */
28824b18 608static int
4d5a73dc
DB
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);
27a46633 613 struct usb_string *us;
4d5a73dc
DB
614 int status;
615 struct usb_ep *ep;
616
ff47f594
SAS
617 /* REVISIT might want instance-specific strings to help
618 * distinguish instances ...
619 */
620
621 /* maybe allocate device-global string IDs, and patch descriptors */
27a46633
SAS
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;
ff47f594 629
4d5a73dc
DB
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;
b97503ff 635 acm_iad_descriptor.bFirstInterface = status;
4d5a73dc
DB
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;
4d5a73dc
DB
656
657 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
658 if (!ep)
659 goto fail;
660 acm->port.out = ep;
4d5a73dc
DB
661
662 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
663 if (!ep)
664 goto fail;
665 acm->notify = ep;
4d5a73dc 666
1f1ba11b
DB
667 /* allocate notification */
668 acm->notify_req = gs_alloc_req(ep,
669 sizeof(struct usb_cdc_notification) + 2,
670 GFP_KERNEL);
671 if (!acm->notify_req)
672 goto fail;
673
674 acm->notify_req->complete = acm_cdc_notify_complete;
675 acm->notify_req->context = acm;
676
4d5a73dc
DB
677 /* support all relevant hardware speeds... we expect that when
678 * hardware is dual speed, all bulk-capable endpoints work at
679 * both speeds
680 */
10287bae
SAS
681 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
682 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
683 acm_hs_notify_desc.bEndpointAddress =
684 acm_fs_notify_desc.bEndpointAddress;
685
686 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
687 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
688
689 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
eaef50c7 690 acm_ss_function, NULL);
10287bae
SAS
691 if (status)
692 goto fail;
4d5a73dc 693
b8b0ea51
RL
694 dev_dbg(&cdev->gadget->dev,
695 "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
696 acm->port_num,
697 gadget_is_superspeed(c->cdev->gadget) ? "super" :
698 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
699 acm->port.in->name, acm->port.out->name,
700 acm->notify->name);
4d5a73dc
DB
701 return 0;
702
703fail:
1f1ba11b
DB
704 if (acm->notify_req)
705 gs_free_req(acm->notify, acm->notify_req);
706
4d5a73dc
DB
707 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
708
709 return status;
710}
711
ff47f594
SAS
712static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
713{
714 struct f_acm *acm = func_to_acm(f);
715
716 acm_string_defs[0].id = 0;
717 usb_free_all_descriptors(f);
718 if (acm->notify_req)
719 gs_free_req(acm->notify, acm->notify_req);
720}
721
722static void acm_free_func(struct usb_function *f)
723{
724 struct f_acm *acm = func_to_acm(f);
725
726 kfree(acm);
727}
728
729static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
730{
731 struct f_serial_opts *opts;
732 struct f_acm *acm;
733
15761826 734 acm = kzalloc(sizeof(*acm), GFP_KERNEL);
ff47f594
SAS
735 if (!acm)
736 return ERR_PTR(-ENOMEM);
737
15761826
SAS
738 spin_lock_init(&acm->lock);
739
740 acm->port.connect = acm_connect;
741 acm->port.disconnect = acm_disconnect;
742 acm->port.send_break = acm_send_break;
743
744 acm->port.func.name = "acm";
745 acm->port.func.strings = acm_strings;
746 /* descriptors are per-instance copies */
747 acm->port.func.bind = acm_bind;
748 acm->port.func.set_alt = acm_set_alt;
749 acm->port.func.setup = acm_setup;
750 acm->port.func.disable = acm_disable;
751
ff47f594
SAS
752 opts = container_of(fi, struct f_serial_opts, func_inst);
753 acm->port_num = opts->port_num;
754 acm->port.func.unbind = acm_unbind;
755 acm->port.func.free_func = acm_free_func;
756
757 return &acm->port.func;
758}
759
88af8bbe
SAS
760static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
761{
762 return container_of(to_config_group(item), struct f_serial_opts,
763 func_inst.group);
764}
765
88af8bbe
SAS
766static void acm_attr_release(struct config_item *item)
767{
768 struct f_serial_opts *opts = to_f_serial_opts(item);
769
770 usb_put_function_instance(&opts->func_inst);
771}
772
773static struct configfs_item_operations acm_item_ops = {
774 .release = acm_attr_release,
88af8bbe
SAS
775};
776
ea6bd6b1 777static ssize_t f_acm_port_num_show(struct config_item *item, char *page)
88af8bbe 778{
ea6bd6b1 779 return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
88af8bbe
SAS
780}
781
0561f77e 782CONFIGFS_ATTR_RO(f_acm_, port_num);
88af8bbe
SAS
783
784static struct configfs_attribute *acm_attrs[] = {
0561f77e 785 &f_acm_attr_port_num,
88af8bbe
SAS
786 NULL,
787};
788
789static struct config_item_type acm_func_type = {
790 .ct_item_ops = &acm_item_ops,
791 .ct_attrs = acm_attrs,
792 .ct_owner = THIS_MODULE,
793};
794
ff47f594
SAS
795static void acm_free_instance(struct usb_function_instance *fi)
796{
797 struct f_serial_opts *opts;
798
799 opts = container_of(fi, struct f_serial_opts, func_inst);
c4ed4ac1 800 gserial_free_line(opts->port_num);
ff47f594
SAS
801 kfree(opts);
802}
803
804static struct usb_function_instance *acm_alloc_instance(void)
805{
806 struct f_serial_opts *opts;
c4ed4ac1 807 int ret;
ff47f594
SAS
808
809 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
810 if (!opts)
811 return ERR_PTR(-ENOMEM);
812 opts->func_inst.free_func_inst = acm_free_instance;
c4ed4ac1
SAS
813 ret = gserial_alloc_line(&opts->port_num);
814 if (ret) {
815 kfree(opts);
816 return ERR_PTR(ret);
817 }
88af8bbe
SAS
818 config_group_init_type_name(&opts->func_inst.group, "",
819 &acm_func_type);
ff47f594
SAS
820 return &opts->func_inst;
821}
822DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
823MODULE_LICENSE("GPL");