Merge tag 'iommu-updates-v3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[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/module.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22
23 #include "u_serial.h"
24 #include "gadget_chips.h"
25
26
27 /*
28 * This CDC ACM function support just wraps control functions and
29 * notifications around the generic serial-over-usb code.
30 *
31 * Because CDC ACM is standardized by the USB-IF, many host operating
32 * systems have drivers for it. Accordingly, ACM is the preferred
33 * interop solution for serial-port type connections. The control
34 * models are often not necessary, and in any case don't do much in
35 * this bare-bones implementation.
36 *
37 * Note that even MS-Windows has some support for ACM. However, that
38 * support is somewhat broken because when you use ACM in a composite
39 * device, having multiple interfaces confuses the poor OS. It doesn't
40 * seem to understand CDC Union descriptors. The new "association"
41 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
42 */
43
44 struct f_acm {
45 struct gserial port;
46 u8 ctrl_id, data_id;
47 u8 port_num;
48
49 u8 pending;
50
51 /* lock is mostly for pending and notify_req ... they get accessed
52 * by callbacks both from tty (open/close/break) under its spinlock,
53 * and notify_req.complete() which can't use that lock.
54 */
55 spinlock_t lock;
56
57 struct usb_ep *notify;
58 struct usb_request *notify_req;
59
60 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
61
62 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
63 u16 port_handshake_bits;
64 #define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
65 #define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
66
67 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
68 u16 serial_state;
69 #define ACM_CTRL_OVERRUN (1 << 6)
70 #define ACM_CTRL_PARITY (1 << 5)
71 #define ACM_CTRL_FRAMING (1 << 4)
72 #define ACM_CTRL_RI (1 << 3)
73 #define ACM_CTRL_BRK (1 << 2)
74 #define ACM_CTRL_DSR (1 << 1)
75 #define ACM_CTRL_DCD (1 << 0)
76 };
77
78 static inline struct f_acm *func_to_acm(struct usb_function *f)
79 {
80 return container_of(f, struct f_acm, port.func);
81 }
82
83 static inline struct f_acm *port_to_acm(struct gserial *p)
84 {
85 return container_of(p, struct f_acm, port);
86 }
87
88 /*-------------------------------------------------------------------------*/
89
90 /* notification endpoint uses smallish and infrequent fixed-size messages */
91
92 #define GS_NOTIFY_INTERVAL_MS 32
93 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
94
95 /* interface and class descriptors: */
96
97 static struct usb_interface_assoc_descriptor
98 acm_iad_descriptor = {
99 .bLength = sizeof acm_iad_descriptor,
100 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
101
102 /* .bFirstInterface = DYNAMIC, */
103 .bInterfaceCount = 2, // control + data
104 .bFunctionClass = USB_CLASS_COMM,
105 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
106 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
107 /* .iFunction = DYNAMIC */
108 };
109
110
111 static struct usb_interface_descriptor acm_control_interface_desc = {
112 .bLength = USB_DT_INTERFACE_SIZE,
113 .bDescriptorType = USB_DT_INTERFACE,
114 /* .bInterfaceNumber = DYNAMIC */
115 .bNumEndpoints = 1,
116 .bInterfaceClass = USB_CLASS_COMM,
117 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
118 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
119 /* .iInterface = DYNAMIC */
120 };
121
122 static struct usb_interface_descriptor acm_data_interface_desc = {
123 .bLength = USB_DT_INTERFACE_SIZE,
124 .bDescriptorType = USB_DT_INTERFACE,
125 /* .bInterfaceNumber = DYNAMIC */
126 .bNumEndpoints = 2,
127 .bInterfaceClass = USB_CLASS_CDC_DATA,
128 .bInterfaceSubClass = 0,
129 .bInterfaceProtocol = 0,
130 /* .iInterface = DYNAMIC */
131 };
132
133 static struct usb_cdc_header_desc acm_header_desc = {
134 .bLength = sizeof(acm_header_desc),
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
137 .bcdCDC = cpu_to_le16(0x0110),
138 };
139
140 static struct usb_cdc_call_mgmt_descriptor
141 acm_call_mgmt_descriptor = {
142 .bLength = sizeof(acm_call_mgmt_descriptor),
143 .bDescriptorType = USB_DT_CS_INTERFACE,
144 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
145 .bmCapabilities = 0,
146 /* .bDataInterface = DYNAMIC */
147 };
148
149 static struct usb_cdc_acm_descriptor acm_descriptor = {
150 .bLength = sizeof(acm_descriptor),
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_ACM_TYPE,
153 .bmCapabilities = USB_CDC_CAP_LINE,
154 };
155
156 static struct usb_cdc_union_desc acm_union_desc = {
157 .bLength = sizeof(acm_union_desc),
158 .bDescriptorType = USB_DT_CS_INTERFACE,
159 .bDescriptorSubType = USB_CDC_UNION_TYPE,
160 /* .bMasterInterface0 = DYNAMIC */
161 /* .bSlaveInterface0 = DYNAMIC */
162 };
163
164 /* full speed support: */
165
166 static struct usb_endpoint_descriptor acm_fs_notify_desc = {
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_INT,
171 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
172 .bInterval = GS_NOTIFY_INTERVAL_MS,
173 };
174
175 static struct usb_endpoint_descriptor acm_fs_in_desc = {
176 .bLength = USB_DT_ENDPOINT_SIZE,
177 .bDescriptorType = USB_DT_ENDPOINT,
178 .bEndpointAddress = USB_DIR_IN,
179 .bmAttributes = USB_ENDPOINT_XFER_BULK,
180 };
181
182 static struct usb_endpoint_descriptor acm_fs_out_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_OUT,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 };
188
189 static struct usb_descriptor_header *acm_fs_function[] = {
190 (struct usb_descriptor_header *) &acm_iad_descriptor,
191 (struct usb_descriptor_header *) &acm_control_interface_desc,
192 (struct usb_descriptor_header *) &acm_header_desc,
193 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
194 (struct usb_descriptor_header *) &acm_descriptor,
195 (struct usb_descriptor_header *) &acm_union_desc,
196 (struct usb_descriptor_header *) &acm_fs_notify_desc,
197 (struct usb_descriptor_header *) &acm_data_interface_desc,
198 (struct usb_descriptor_header *) &acm_fs_in_desc,
199 (struct usb_descriptor_header *) &acm_fs_out_desc,
200 NULL,
201 };
202
203 /* high speed support: */
204 static struct usb_endpoint_descriptor acm_hs_notify_desc = {
205 .bLength = USB_DT_ENDPOINT_SIZE,
206 .bDescriptorType = USB_DT_ENDPOINT,
207 .bEndpointAddress = USB_DIR_IN,
208 .bmAttributes = USB_ENDPOINT_XFER_INT,
209 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
210 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
211 };
212
213 static struct usb_endpoint_descriptor acm_hs_in_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216 .bmAttributes = USB_ENDPOINT_XFER_BULK,
217 .wMaxPacketSize = cpu_to_le16(512),
218 };
219
220 static struct usb_endpoint_descriptor acm_hs_out_desc = {
221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
223 .bmAttributes = USB_ENDPOINT_XFER_BULK,
224 .wMaxPacketSize = cpu_to_le16(512),
225 };
226
227 static struct usb_descriptor_header *acm_hs_function[] = {
228 (struct usb_descriptor_header *) &acm_iad_descriptor,
229 (struct usb_descriptor_header *) &acm_control_interface_desc,
230 (struct usb_descriptor_header *) &acm_header_desc,
231 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
232 (struct usb_descriptor_header *) &acm_descriptor,
233 (struct usb_descriptor_header *) &acm_union_desc,
234 (struct usb_descriptor_header *) &acm_hs_notify_desc,
235 (struct usb_descriptor_header *) &acm_data_interface_desc,
236 (struct usb_descriptor_header *) &acm_hs_in_desc,
237 (struct usb_descriptor_header *) &acm_hs_out_desc,
238 NULL,
239 };
240
241 static struct usb_endpoint_descriptor acm_ss_in_desc = {
242 .bLength = USB_DT_ENDPOINT_SIZE,
243 .bDescriptorType = USB_DT_ENDPOINT,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = cpu_to_le16(1024),
246 };
247
248 static struct usb_endpoint_descriptor acm_ss_out_desc = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
252 .wMaxPacketSize = cpu_to_le16(1024),
253 };
254
255 static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
256 .bLength = sizeof acm_ss_bulk_comp_desc,
257 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
258 };
259
260 static struct usb_descriptor_header *acm_ss_function[] = {
261 (struct usb_descriptor_header *) &acm_iad_descriptor,
262 (struct usb_descriptor_header *) &acm_control_interface_desc,
263 (struct usb_descriptor_header *) &acm_header_desc,
264 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
265 (struct usb_descriptor_header *) &acm_descriptor,
266 (struct usb_descriptor_header *) &acm_union_desc,
267 (struct usb_descriptor_header *) &acm_hs_notify_desc,
268 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
269 (struct usb_descriptor_header *) &acm_data_interface_desc,
270 (struct usb_descriptor_header *) &acm_ss_in_desc,
271 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
272 (struct usb_descriptor_header *) &acm_ss_out_desc,
273 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
274 NULL,
275 };
276
277 /* string descriptors: */
278
279 #define ACM_CTRL_IDX 0
280 #define ACM_DATA_IDX 1
281 #define ACM_IAD_IDX 2
282
283 /* static strings, in UTF-8 */
284 static struct usb_string acm_string_defs[] = {
285 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
286 [ACM_DATA_IDX].s = "CDC ACM Data",
287 [ACM_IAD_IDX ].s = "CDC Serial",
288 };
289
290 static struct usb_gadget_strings acm_string_table = {
291 .language = 0x0409, /* en-us */
292 .strings = acm_string_defs,
293 };
294
295 static 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
308 static 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) {
315 DBG(cdev, "acm ttyGS%d completion, err %d\n",
316 acm->port_num, req->status);
317 return;
318 }
319
320 /* normal completion */
321 if (req->actual != sizeof(acm->port_line_coding)) {
322 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
323 acm->port_num, req->actual);
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
339 static 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().
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.
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
391 * host sets the ACM_CTRL_DTR bit; and when it clears
392 * that bit, we should return to that no-flow state.
393 */
394 acm->port_handshake_bits = w_value;
395 break;
396
397 default:
398 invalid:
399 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
400 ctrl->bRequestType, ctrl->bRequest,
401 w_value, w_index, w_length);
402 }
403
404 /* respond with data transfer or status phase? */
405 if (value >= 0) {
406 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
407 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
408 w_value, w_index, w_length);
409 req->zero = 0;
410 req->length = value;
411 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
412 if (value < 0)
413 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
414 acm->port_num, value);
415 }
416
417 /* device either stalls (value < 0) or reports success */
418 return value;
419 }
420
421 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
422 {
423 struct f_acm *acm = func_to_acm(f);
424 struct usb_composite_dev *cdev = f->config->cdev;
425
426 /* we know alt == 0, so this is an activation or a reset */
427
428 if (intf == acm->ctrl_id) {
429 if (acm->notify->driver_data) {
430 VDBG(cdev, "reset acm control interface %d\n", intf);
431 usb_ep_disable(acm->notify);
432 } else {
433 VDBG(cdev, "init acm ctrl interface %d\n", intf);
434 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
435 return -EINVAL;
436 }
437 usb_ep_enable(acm->notify);
438 acm->notify->driver_data = acm;
439
440 } else if (intf == acm->data_id) {
441 if (acm->port.in->driver_data) {
442 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
443 gserial_disconnect(&acm->port);
444 }
445 if (!acm->port.in->desc || !acm->port.out->desc) {
446 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
447 if (config_ep_by_speed(cdev->gadget, f,
448 acm->port.in) ||
449 config_ep_by_speed(cdev->gadget, f,
450 acm->port.out)) {
451 acm->port.in->desc = NULL;
452 acm->port.out->desc = NULL;
453 return -EINVAL;
454 }
455 }
456 gserial_connect(&acm->port, acm->port_num);
457
458 } else
459 return -EINVAL;
460
461 return 0;
462 }
463
464 static void acm_disable(struct usb_function *f)
465 {
466 struct f_acm *acm = func_to_acm(f);
467 struct usb_composite_dev *cdev = f->config->cdev;
468
469 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
470 gserial_disconnect(&acm->port);
471 usb_ep_disable(acm->notify);
472 acm->notify->driver_data = NULL;
473 }
474
475 /*-------------------------------------------------------------------------*/
476
477 /**
478 * acm_cdc_notify - issue CDC notification to host
479 * @acm: wraps host to be notified
480 * @type: notification type
481 * @value: Refer to cdc specs, wValue field.
482 * @data: data to be sent
483 * @length: size of data
484 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
485 *
486 * Returns zero on success or a negative errno.
487 *
488 * See section 6.3.5 of the CDC 1.1 specification for information
489 * about the only notification we issue: SerialState change.
490 */
491 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
492 void *data, unsigned length)
493 {
494 struct usb_ep *ep = acm->notify;
495 struct usb_request *req;
496 struct usb_cdc_notification *notify;
497 const unsigned len = sizeof(*notify) + length;
498 void *buf;
499 int status;
500
501 req = acm->notify_req;
502 acm->notify_req = NULL;
503 acm->pending = false;
504
505 req->length = len;
506 notify = req->buf;
507 buf = notify + 1;
508
509 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
510 | USB_RECIP_INTERFACE;
511 notify->bNotificationType = type;
512 notify->wValue = cpu_to_le16(value);
513 notify->wIndex = cpu_to_le16(acm->ctrl_id);
514 notify->wLength = cpu_to_le16(length);
515 memcpy(buf, data, length);
516
517 /* ep_queue() can complete immediately if it fills the fifo... */
518 spin_unlock(&acm->lock);
519 status = usb_ep_queue(ep, req, GFP_ATOMIC);
520 spin_lock(&acm->lock);
521
522 if (status < 0) {
523 ERROR(acm->port.func.config->cdev,
524 "acm ttyGS%d can't notify serial state, %d\n",
525 acm->port_num, status);
526 acm->notify_req = req;
527 }
528
529 return status;
530 }
531
532 static int acm_notify_serial_state(struct f_acm *acm)
533 {
534 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
535 int status;
536
537 spin_lock(&acm->lock);
538 if (acm->notify_req) {
539 DBG(cdev, "acm ttyGS%d serial state %04x\n",
540 acm->port_num, acm->serial_state);
541 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
542 0, &acm->serial_state, sizeof(acm->serial_state));
543 } else {
544 acm->pending = true;
545 status = 0;
546 }
547 spin_unlock(&acm->lock);
548 return status;
549 }
550
551 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
552 {
553 struct f_acm *acm = req->context;
554 u8 doit = false;
555
556 /* on this call path we do NOT hold the port spinlock,
557 * which is why ACM needs its own spinlock
558 */
559 spin_lock(&acm->lock);
560 if (req->status != -ESHUTDOWN)
561 doit = acm->pending;
562 acm->notify_req = req;
563 spin_unlock(&acm->lock);
564
565 if (doit)
566 acm_notify_serial_state(acm);
567 }
568
569 /* connect == the TTY link is open */
570
571 static void acm_connect(struct gserial *port)
572 {
573 struct f_acm *acm = port_to_acm(port);
574
575 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
576 acm_notify_serial_state(acm);
577 }
578
579 static void acm_disconnect(struct gserial *port)
580 {
581 struct f_acm *acm = port_to_acm(port);
582
583 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
584 acm_notify_serial_state(acm);
585 }
586
587 static int acm_send_break(struct gserial *port, int duration)
588 {
589 struct f_acm *acm = port_to_acm(port);
590 u16 state;
591
592 state = acm->serial_state;
593 state &= ~ACM_CTRL_BRK;
594 if (duration)
595 state |= ACM_CTRL_BRK;
596
597 acm->serial_state = state;
598 return acm_notify_serial_state(acm);
599 }
600
601 /*-------------------------------------------------------------------------*/
602
603 /* ACM function driver setup/binding */
604 static int
605 acm_bind(struct usb_configuration *c, struct usb_function *f)
606 {
607 struct usb_composite_dev *cdev = c->cdev;
608 struct f_acm *acm = func_to_acm(f);
609 struct usb_string *us;
610 int status;
611 struct usb_ep *ep;
612
613 /* REVISIT might want instance-specific strings to help
614 * distinguish instances ...
615 */
616
617 /* maybe allocate device-global string IDs, and patch descriptors */
618 us = usb_gstrings_attach(cdev, acm_strings,
619 ARRAY_SIZE(acm_string_defs));
620 if (IS_ERR(us))
621 return PTR_ERR(us);
622 acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id;
623 acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;
624 acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;
625
626 /* allocate instance-specific interface IDs, and patch descriptors */
627 status = usb_interface_id(c, f);
628 if (status < 0)
629 goto fail;
630 acm->ctrl_id = status;
631 acm_iad_descriptor.bFirstInterface = status;
632
633 acm_control_interface_desc.bInterfaceNumber = status;
634 acm_union_desc .bMasterInterface0 = status;
635
636 status = usb_interface_id(c, f);
637 if (status < 0)
638 goto fail;
639 acm->data_id = status;
640
641 acm_data_interface_desc.bInterfaceNumber = status;
642 acm_union_desc.bSlaveInterface0 = status;
643 acm_call_mgmt_descriptor.bDataInterface = status;
644
645 status = -ENODEV;
646
647 /* allocate instance-specific endpoints */
648 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
649 if (!ep)
650 goto fail;
651 acm->port.in = ep;
652 ep->driver_data = cdev; /* claim */
653
654 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
655 if (!ep)
656 goto fail;
657 acm->port.out = ep;
658 ep->driver_data = cdev; /* claim */
659
660 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
661 if (!ep)
662 goto fail;
663 acm->notify = ep;
664 ep->driver_data = cdev; /* claim */
665
666 /* allocate notification */
667 acm->notify_req = gs_alloc_req(ep,
668 sizeof(struct usb_cdc_notification) + 2,
669 GFP_KERNEL);
670 if (!acm->notify_req)
671 goto fail;
672
673 acm->notify_req->complete = acm_cdc_notify_complete;
674 acm->notify_req->context = acm;
675
676 /* support all relevant hardware speeds... we expect that when
677 * hardware is dual speed, all bulk-capable endpoints work at
678 * both speeds
679 */
680 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
681 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
682 acm_hs_notify_desc.bEndpointAddress =
683 acm_fs_notify_desc.bEndpointAddress;
684
685 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
686 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
687
688 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
689 acm_ss_function);
690 if (status)
691 goto fail;
692
693 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
694 acm->port_num,
695 gadget_is_superspeed(c->cdev->gadget) ? "super" :
696 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
697 acm->port.in->name, acm->port.out->name,
698 acm->notify->name);
699 return 0;
700
701 fail:
702 if (acm->notify_req)
703 gs_free_req(acm->notify, acm->notify_req);
704
705 /* we might as well release our claims on endpoints */
706 if (acm->notify)
707 acm->notify->driver_data = NULL;
708 if (acm->port.out)
709 acm->port.out->driver_data = NULL;
710 if (acm->port.in)
711 acm->port.in->driver_data = NULL;
712
713 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
714
715 return status;
716 }
717
718 static struct f_acm *acm_alloc_basic_func(void)
719 {
720 struct f_acm *acm;
721
722 acm = kzalloc(sizeof(*acm), GFP_KERNEL);
723 if (!acm)
724 return NULL;
725
726 spin_lock_init(&acm->lock);
727
728 acm->port.connect = acm_connect;
729 acm->port.disconnect = acm_disconnect;
730 acm->port.send_break = acm_send_break;
731
732 acm->port.func.name = "acm";
733 /* descriptors are per-instance copies */
734 acm->port.func.bind = acm_bind;
735 acm->port.func.set_alt = acm_set_alt;
736 acm->port.func.setup = acm_setup;
737 acm->port.func.disable = acm_disable;
738
739 return acm;
740 }
741
742 #ifdef USB_FACM_INCLUDED
743 static void
744 acm_old_unbind(struct usb_configuration *c, struct usb_function *f)
745 {
746 struct f_acm *acm = func_to_acm(f);
747
748 usb_free_all_descriptors(f);
749 if (acm->notify_req)
750 gs_free_req(acm->notify, acm->notify_req);
751 kfree(acm);
752 }
753
754 /**
755 * acm_bind_config - add a CDC ACM function to a configuration
756 * @c: the configuration to support the CDC ACM instance
757 * @port_num: /dev/ttyGS* port this interface will use
758 * Context: single threaded during gadget setup
759 *
760 * Returns zero on success, else negative errno.
761 *
762 */
763 int acm_bind_config(struct usb_configuration *c, u8 port_num)
764 {
765 struct f_acm *acm;
766 int status;
767
768 /* allocate and initialize one new instance */
769 acm = acm_alloc_basic_func();
770 if (!acm)
771 return -ENOMEM;
772
773 acm->port_num = port_num;
774 acm->port.func.unbind = acm_old_unbind;
775
776 status = usb_add_function(c, &acm->port.func);
777 if (status)
778 kfree(acm);
779 return status;
780 }
781
782 #else
783
784 static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
785 {
786 struct f_acm *acm = func_to_acm(f);
787
788 acm_string_defs[0].id = 0;
789 usb_free_all_descriptors(f);
790 if (acm->notify_req)
791 gs_free_req(acm->notify, acm->notify_req);
792 }
793
794 static void acm_free_func(struct usb_function *f)
795 {
796 struct f_acm *acm = func_to_acm(f);
797
798 kfree(acm);
799 }
800
801 static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
802 {
803 struct f_serial_opts *opts;
804 struct f_acm *acm;
805
806 acm = acm_alloc_basic_func();
807 if (!acm)
808 return ERR_PTR(-ENOMEM);
809
810 opts = container_of(fi, struct f_serial_opts, func_inst);
811 acm->port_num = opts->port_num;
812 acm->port.func.unbind = acm_unbind;
813 acm->port.func.free_func = acm_free_func;
814
815 return &acm->port.func;
816 }
817
818 static void acm_free_instance(struct usb_function_instance *fi)
819 {
820 struct f_serial_opts *opts;
821
822 opts = container_of(fi, struct f_serial_opts, func_inst);
823 kfree(opts);
824 }
825
826 static struct usb_function_instance *acm_alloc_instance(void)
827 {
828 struct f_serial_opts *opts;
829
830 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
831 if (!opts)
832 return ERR_PTR(-ENOMEM);
833 opts->func_inst.free_func_inst = acm_free_instance;
834 return &opts->func_inst;
835 }
836 DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
837 MODULE_LICENSE("GPL");
838 #endif
This page took 0.048906 seconds and 5 git commands to generate.