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