[SCSI] libsas: fix definition of wideport, include local sas address
[linux-2.6/linux-mips.git] / drivers / usb / gadget / f_acm.c
blobbd6226cbae86fa4b1b201ef8ac88df3942d9858f
1 /*
2 * f_acm.c -- USB CDC serial (ACM) function driver
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 (m.nazarewicz@samsung.com)
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.
15 /* #define VERBOSE_DEBUG */
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
21 #include "u_serial.h"
22 #include "gadget_chips.h"
26 * This CDC ACM function support just wraps control functions and
27 * notifications around the generic serial-over-usb code.
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.
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.
42 struct acm_ep_descs {
43 struct usb_endpoint_descriptor *in;
44 struct usb_endpoint_descriptor *out;
45 struct usb_endpoint_descriptor *notify;
48 struct f_acm {
49 struct gserial port;
50 u8 ctrl_id, data_id;
51 u8 port_num;
53 u8 pending;
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.
59 spinlock_t lock;
61 struct acm_ep_descs fs;
62 struct acm_ep_descs hs;
64 struct usb_ep *notify;
65 struct usb_endpoint_descriptor *notify_desc;
66 struct usb_request *notify_req;
68 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
70 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
71 u16 port_handshake_bits;
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 */
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)
86 static inline struct f_acm *func_to_acm(struct usb_function *f)
88 return container_of(f, struct f_acm, port.func);
91 static inline struct f_acm *port_to_acm(struct gserial *p)
93 return container_of(p, struct f_acm, port);
96 /*-------------------------------------------------------------------------*/
98 /* notification endpoint uses smallish and infrequent fixed-size messages */
100 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
101 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
103 /* interface and class descriptors: */
105 static struct usb_interface_assoc_descriptor
106 acm_iad_descriptor = {
107 .bLength = sizeof acm_iad_descriptor,
108 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
110 /* .bFirstInterface = DYNAMIC, */
111 .bInterfaceCount = 2, // control + data
112 .bFunctionClass = USB_CLASS_COMM,
113 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
114 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
115 /* .iFunction = DYNAMIC */
119 static struct usb_interface_descriptor acm_control_interface_desc = {
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 */
130 static struct usb_interface_descriptor acm_data_interface_desc = {
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 */
141 static struct usb_cdc_header_desc acm_header_desc = {
142 .bLength = sizeof(acm_header_desc),
143 .bDescriptorType = USB_DT_CS_INTERFACE,
144 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
145 .bcdCDC = cpu_to_le16(0x0110),
148 static struct usb_cdc_call_mgmt_descriptor
149 acm_call_mgmt_descriptor = {
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 */
157 static struct usb_cdc_acm_descriptor acm_descriptor = {
158 .bLength = sizeof(acm_descriptor),
159 .bDescriptorType = USB_DT_CS_INTERFACE,
160 .bDescriptorSubType = USB_CDC_ACM_TYPE,
161 .bmCapabilities = USB_CDC_CAP_LINE,
164 static struct usb_cdc_union_desc acm_union_desc = {
165 .bLength = sizeof(acm_union_desc),
166 .bDescriptorType = USB_DT_CS_INTERFACE,
167 .bDescriptorSubType = USB_CDC_UNION_TYPE,
168 /* .bMasterInterface0 = DYNAMIC */
169 /* .bSlaveInterface0 = DYNAMIC */
172 /* full speed support: */
174 static struct usb_endpoint_descriptor acm_fs_notify_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_INT,
179 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
180 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
183 static struct usb_endpoint_descriptor acm_fs_in_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bEndpointAddress = USB_DIR_IN,
187 .bmAttributes = USB_ENDPOINT_XFER_BULK,
190 static struct usb_endpoint_descriptor acm_fs_out_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT,
193 .bEndpointAddress = USB_DIR_OUT,
194 .bmAttributes = USB_ENDPOINT_XFER_BULK,
197 static struct usb_descriptor_header *acm_fs_function[] = {
198 (struct usb_descriptor_header *) &acm_iad_descriptor,
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,
211 /* high speed support: */
213 static struct usb_endpoint_descriptor acm_hs_notify_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216 .bEndpointAddress = USB_DIR_IN,
217 .bmAttributes = USB_ENDPOINT_XFER_INT,
218 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
219 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
222 static struct usb_endpoint_descriptor acm_hs_in_desc = {
223 .bLength = USB_DT_ENDPOINT_SIZE,
224 .bDescriptorType = USB_DT_ENDPOINT,
225 .bmAttributes = USB_ENDPOINT_XFER_BULK,
226 .wMaxPacketSize = cpu_to_le16(512),
229 static struct usb_endpoint_descriptor acm_hs_out_desc = {
230 .bLength = USB_DT_ENDPOINT_SIZE,
231 .bDescriptorType = USB_DT_ENDPOINT,
232 .bmAttributes = USB_ENDPOINT_XFER_BULK,
233 .wMaxPacketSize = cpu_to_le16(512),
236 static struct usb_descriptor_header *acm_hs_function[] = {
237 (struct usb_descriptor_header *) &acm_iad_descriptor,
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,
250 /* string descriptors: */
252 #define ACM_CTRL_IDX 0
253 #define ACM_DATA_IDX 1
254 #define ACM_IAD_IDX 2
256 /* static strings, in UTF-8 */
257 static struct usb_string acm_string_defs[] = {
258 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
259 [ACM_DATA_IDX].s = "CDC ACM Data",
260 [ACM_IAD_IDX ].s = "CDC Serial",
261 { /* ZEROES END LIST */ },
264 static struct usb_gadget_strings acm_string_table = {
265 .language = 0x0409, /* en-us */
266 .strings = acm_string_defs,
269 static struct usb_gadget_strings *acm_strings[] = {
270 &acm_string_table,
271 NULL,
274 /*-------------------------------------------------------------------------*/
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.
282 static void acm_complete_set_line_coding(struct usb_ep *ep,
283 struct usb_request *req)
285 struct f_acm *acm = ep->driver_data;
286 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
288 if (req->status != 0) {
289 DBG(cdev, "acm ttyGS%d completion, err %d\n",
290 acm->port_num, req->status);
291 return;
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;
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.
309 acm->port_line_coding = *value;
313 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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);
323 /* composite driver infrastructure handles everything except
324 * CDC class messages; interface activation uses set_alt().
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.
331 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
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;
340 value = w_length;
341 cdev->gadget->ep0->driver_data = acm;
342 req->complete = acm_complete_set_line_coding;
343 break;
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;
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;
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;
362 value = 0;
364 /* FIXME we should not allow data to flow until the
365 * host sets the ACM_CTRL_DTR bit; and when it clears
366 * that bit, we should return to that no-flow state.
368 acm->port_handshake_bits = w_value;
369 break;
371 default:
372 invalid:
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);
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);
391 /* device either stalls (value < 0) or reports success */
392 return value;
395 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
397 struct f_acm *acm = func_to_acm(f);
398 struct usb_composite_dev *cdev = f->config->cdev;
400 /* we know alt == 0, so this is an activation or a reset */
402 if (intf == acm->ctrl_id) {
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);
412 usb_ep_enable(acm->notify, acm->notify_desc);
413 acm->notify->driver_data = acm;
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);
426 gserial_connect(&acm->port, acm->port_num);
428 } else
429 return -EINVAL;
431 return 0;
434 static void acm_disable(struct usb_function *f)
436 struct f_acm *acm = func_to_acm(f);
437 struct usb_composite_dev *cdev = f->config->cdev;
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;
445 /*-------------------------------------------------------------------------*/
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
456 * Returns zero on success or a negative errno.
458 * See section 6.3.5 of the CDC 1.1 specification for information
459 * about the only notification we issue: SerialState change.
461 static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
462 void *data, unsigned length)
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;
471 req = acm->notify_req;
472 acm->notify_req = NULL;
473 acm->pending = false;
475 req->length = len;
476 notify = req->buf;
477 buf = notify + 1;
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);
487 /* ep_queue() can complete immediately if it fills the fifo... */
488 spin_unlock(&acm->lock);
489 status = usb_ep_queue(ep, req, GFP_ATOMIC);
490 spin_lock(&acm->lock);
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;
499 return status;
502 static int acm_notify_serial_state(struct f_acm *acm)
504 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
505 int status;
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;
517 spin_unlock(&acm->lock);
518 return status;
521 static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
523 struct f_acm *acm = req->context;
524 u8 doit = false;
526 /* on this call path we do NOT hold the port spinlock,
527 * which is why ACM needs its own spinlock
529 spin_lock(&acm->lock);
530 if (req->status != -ESHUTDOWN)
531 doit = acm->pending;
532 acm->notify_req = req;
533 spin_unlock(&acm->lock);
535 if (doit)
536 acm_notify_serial_state(acm);
539 /* connect == the TTY link is open */
541 static void acm_connect(struct gserial *port)
543 struct f_acm *acm = port_to_acm(port);
545 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
546 acm_notify_serial_state(acm);
549 static void acm_disconnect(struct gserial *port)
551 struct f_acm *acm = port_to_acm(port);
553 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
554 acm_notify_serial_state(acm);
557 static int acm_send_break(struct gserial *port, int duration)
559 struct f_acm *acm = port_to_acm(port);
560 u16 state;
562 state = acm->serial_state;
563 state &= ~ACM_CTRL_BRK;
564 if (duration)
565 state |= ACM_CTRL_BRK;
567 acm->serial_state = state;
568 return acm_notify_serial_state(acm);
571 /*-------------------------------------------------------------------------*/
573 /* ACM function driver setup/binding */
574 static int
575 acm_bind(struct usb_configuration *c, struct usb_function *f)
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;
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;
587 acm_iad_descriptor.bFirstInterface = status;
589 acm_control_interface_desc.bInterfaceNumber = status;
590 acm_union_desc .bMasterInterface0 = status;
592 status = usb_interface_id(c, f);
593 if (status < 0)
594 goto fail;
595 acm->data_id = status;
597 acm_data_interface_desc.bInterfaceNumber = status;
598 acm_union_desc.bSlaveInterface0 = status;
599 acm_call_mgmt_descriptor.bDataInterface = status;
601 status = -ENODEV;
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 */
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 */
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 */
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;
629 acm->notify_req->complete = acm_cdc_notify_complete;
630 acm->notify_req->context = acm;
632 /* copy descriptors, and track endpoint copies */
633 f->descriptors = usb_copy_descriptors(acm_fs_function);
634 if (!f->descriptors)
635 goto fail;
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);
644 /* support all relevant hardware speeds... we expect that when
645 * hardware is dual speed, all bulk-capable endpoints work at
646 * both speeds
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;
656 /* copy descriptors, and track endpoint copies */
657 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
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);
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;
674 fail:
675 if (acm->notify_req)
676 gs_free_req(acm->notify, acm->notify_req);
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;
686 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
688 return status;
691 static void
692 acm_unbind(struct usb_configuration *c, struct usb_function *f)
694 struct f_acm *acm = func_to_acm(f);
696 if (gadget_is_dualspeed(c->cdev->gadget))
697 usb_free_descriptors(f->hs_descriptors);
698 usb_free_descriptors(f->descriptors);
699 gs_free_req(acm->notify, acm->notify_req);
700 kfree(acm);
703 /* Some controllers can't support CDC ACM ... */
704 static inline bool can_support_cdc(struct usb_configuration *c)
706 /* everything else is *probably* fine ... */
707 return true;
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
716 * Returns zero on success, else negative errno.
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.
722 int acm_bind_config(struct usb_configuration *c, u8 port_num)
724 struct f_acm *acm;
725 int status;
727 if (!can_support_cdc(c))
728 return -EINVAL;
730 /* REVISIT might want instance-specific strings to help
731 * distinguish instances ...
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;
741 acm_control_interface_desc.iInterface = status;
743 status = usb_string_id(c->cdev);
744 if (status < 0)
745 return status;
746 acm_string_defs[ACM_DATA_IDX].id = status;
748 acm_data_interface_desc.iInterface = status;
750 status = usb_string_id(c->cdev);
751 if (status < 0)
752 return status;
753 acm_string_defs[ACM_IAD_IDX].id = status;
755 acm_iad_descriptor.iFunction = status;
758 /* allocate and initialize one new instance */
759 acm = kzalloc(sizeof *acm, GFP_KERNEL);
760 if (!acm)
761 return -ENOMEM;
763 spin_lock_init(&acm->lock);
765 acm->port_num = port_num;
767 acm->port.connect = acm_connect;
768 acm->port.disconnect = acm_disconnect;
769 acm->port.send_break = acm_send_break;
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;
780 status = usb_add_function(c, &acm->port.func);
781 if (status)
782 kfree(acm);
783 return status;