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