Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
f_acm.c
Go to the documentation of this file.
1 /*
2  * f_acm.c -- USB CDC serial (ACM) function driver
3  *
4  * Copyright (C) 2003 Al Borchers ([email protected])
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 ([email protected])
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;
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  */
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) */
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) */
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_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
91 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
92 
93 /* interface and class descriptors: */
94 
96 acm_iad_descriptor = {
97  .bLength = sizeof acm_iad_descriptor,
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),
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),
143  .bmCapabilities = 0,
144  /* .bDataInterface = DYNAMIC */
145 };
146 
147 static struct usb_cdc_acm_descriptor acm_descriptor = {
148  .bLength = sizeof(acm_descriptor),
152 };
153 
154 static struct usb_cdc_union_desc acm_union_desc = {
155  .bLength = sizeof(acm_union_desc),
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 = 1 << GS_LOG2_NOTIFY_INTERVAL,
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 
203 static struct usb_endpoint_descriptor acm_hs_notify_desc = {
204  .bLength = USB_DT_ENDPOINT_SIZE,
205  .bDescriptorType = USB_DT_ENDPOINT,
206  .bEndpointAddress = USB_DIR_IN,
207  .bmAttributes = USB_ENDPOINT_XFER_INT,
208  .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
209  .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
210 };
211 
212 static struct usb_endpoint_descriptor acm_hs_in_desc = {
213  .bLength = USB_DT_ENDPOINT_SIZE,
214  .bDescriptorType = USB_DT_ENDPOINT,
215  .bmAttributes = USB_ENDPOINT_XFER_BULK,
216  .wMaxPacketSize = cpu_to_le16(512),
217 };
218 
219 static struct usb_endpoint_descriptor acm_hs_out_desc = {
220  .bLength = USB_DT_ENDPOINT_SIZE,
221  .bDescriptorType = USB_DT_ENDPOINT,
222  .bmAttributes = USB_ENDPOINT_XFER_BULK,
223  .wMaxPacketSize = cpu_to_le16(512),
224 };
225 
226 static struct usb_descriptor_header *acm_hs_function[] = {
227  (struct usb_descriptor_header *) &acm_iad_descriptor,
228  (struct usb_descriptor_header *) &acm_control_interface_desc,
229  (struct usb_descriptor_header *) &acm_header_desc,
230  (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
231  (struct usb_descriptor_header *) &acm_descriptor,
232  (struct usb_descriptor_header *) &acm_union_desc,
233  (struct usb_descriptor_header *) &acm_hs_notify_desc,
234  (struct usb_descriptor_header *) &acm_data_interface_desc,
235  (struct usb_descriptor_header *) &acm_hs_in_desc,
236  (struct usb_descriptor_header *) &acm_hs_out_desc,
237  NULL,
238 };
239 
240 static struct usb_endpoint_descriptor acm_ss_in_desc = {
241  .bLength = USB_DT_ENDPOINT_SIZE,
242  .bDescriptorType = USB_DT_ENDPOINT,
243  .bmAttributes = USB_ENDPOINT_XFER_BULK,
244  .wMaxPacketSize = cpu_to_le16(1024),
245 };
246 
247 static struct usb_endpoint_descriptor acm_ss_out_desc = {
248  .bLength = USB_DT_ENDPOINT_SIZE,
249  .bDescriptorType = USB_DT_ENDPOINT,
250  .bmAttributes = USB_ENDPOINT_XFER_BULK,
251  .wMaxPacketSize = cpu_to_le16(1024),
252 };
253 
254 static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
255  .bLength = sizeof acm_ss_bulk_comp_desc,
257 };
258 
259 static struct usb_descriptor_header *acm_ss_function[] = {
260  (struct usb_descriptor_header *) &acm_iad_descriptor,
261  (struct usb_descriptor_header *) &acm_control_interface_desc,
262  (struct usb_descriptor_header *) &acm_header_desc,
263  (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
264  (struct usb_descriptor_header *) &acm_descriptor,
265  (struct usb_descriptor_header *) &acm_union_desc,
266  (struct usb_descriptor_header *) &acm_hs_notify_desc,
267  (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
268  (struct usb_descriptor_header *) &acm_data_interface_desc,
269  (struct usb_descriptor_header *) &acm_ss_in_desc,
270  (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
271  (struct usb_descriptor_header *) &acm_ss_out_desc,
272  (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
273  NULL,
274 };
275 
276 /* string descriptors: */
277 
278 #define ACM_CTRL_IDX 0
279 #define ACM_DATA_IDX 1
280 #define ACM_IAD_IDX 2
281 
282 /* static strings, in UTF-8 */
283 static struct usb_string acm_string_defs[] = {
284  [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
285  [ACM_DATA_IDX].s = "CDC ACM Data",
286  [ACM_IAD_IDX ].s = "CDC Serial",
287  { /* ZEROES END LIST */ },
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 */
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 */
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 */
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 
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;
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 
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 
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 
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  int status;
610  struct usb_ep *ep;
611 
612  /* allocate instance-specific interface IDs, and patch descriptors */
613  status = usb_interface_id(c, f);
614  if (status < 0)
615  goto fail;
616  acm->ctrl_id = status;
617  acm_iad_descriptor.bFirstInterface = status;
618 
619  acm_control_interface_desc.bInterfaceNumber = status;
620  acm_union_desc .bMasterInterface0 = status;
621 
622  status = usb_interface_id(c, f);
623  if (status < 0)
624  goto fail;
625  acm->data_id = status;
626 
627  acm_data_interface_desc.bInterfaceNumber = status;
628  acm_union_desc.bSlaveInterface0 = status;
629  acm_call_mgmt_descriptor.bDataInterface = status;
630 
631  status = -ENODEV;
632 
633  /* allocate instance-specific endpoints */
634  ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
635  if (!ep)
636  goto fail;
637  acm->port.in = ep;
638  ep->driver_data = cdev; /* claim */
639 
640  ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
641  if (!ep)
642  goto fail;
643  acm->port.out = ep;
644  ep->driver_data = cdev; /* claim */
645 
646  ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
647  if (!ep)
648  goto fail;
649  acm->notify = ep;
650  ep->driver_data = cdev; /* claim */
651 
652  /* allocate notification */
653  acm->notify_req = gs_alloc_req(ep,
654  sizeof(struct usb_cdc_notification) + 2,
655  GFP_KERNEL);
656  if (!acm->notify_req)
657  goto fail;
658 
659  acm->notify_req->complete = acm_cdc_notify_complete;
660  acm->notify_req->context = acm;
661 
662  /* copy descriptors */
663  f->descriptors = usb_copy_descriptors(acm_fs_function);
664  if (!f->descriptors)
665  goto fail;
666 
667  /* support all relevant hardware speeds... we expect that when
668  * hardware is dual speed, all bulk-capable endpoints work at
669  * both speeds
670  */
671  if (gadget_is_dualspeed(c->cdev->gadget)) {
672  acm_hs_in_desc.bEndpointAddress =
673  acm_fs_in_desc.bEndpointAddress;
674  acm_hs_out_desc.bEndpointAddress =
675  acm_fs_out_desc.bEndpointAddress;
676  acm_hs_notify_desc.bEndpointAddress =
677  acm_fs_notify_desc.bEndpointAddress;
678 
679  /* copy descriptors */
680  f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
681  }
682  if (gadget_is_superspeed(c->cdev->gadget)) {
683  acm_ss_in_desc.bEndpointAddress =
684  acm_fs_in_desc.bEndpointAddress;
685  acm_ss_out_desc.bEndpointAddress =
686  acm_fs_out_desc.bEndpointAddress;
687 
688  /* copy descriptors, and track endpoint copies */
689  f->ss_descriptors = usb_copy_descriptors(acm_ss_function);
690  if (!f->ss_descriptors)
691  goto fail;
692  }
693 
694  DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
695  acm->port_num,
696  gadget_is_superspeed(c->cdev->gadget) ? "super" :
697  gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
698  acm->port.in->name, acm->port.out->name,
699  acm->notify->name);
700  return 0;
701 
702 fail:
703  if (acm->notify_req)
704  gs_free_req(acm->notify, acm->notify_req);
705 
706  /* we might as well release our claims on endpoints */
707  if (acm->notify)
708  acm->notify->driver_data = NULL;
709  if (acm->port.out)
710  acm->port.out->driver_data = NULL;
711  if (acm->port.in)
712  acm->port.in->driver_data = NULL;
713 
714  ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
715 
716  return status;
717 }
718 
719 static void
720 acm_unbind(struct usb_configuration *c, struct usb_function *f)
721 {
722  struct f_acm *acm = func_to_acm(f);
723 
724  if (gadget_is_dualspeed(c->cdev->gadget))
725  usb_free_descriptors(f->hs_descriptors);
726  if (gadget_is_superspeed(c->cdev->gadget))
727  usb_free_descriptors(f->ss_descriptors);
728  usb_free_descriptors(f->descriptors);
729  gs_free_req(acm->notify, acm->notify_req);
730  kfree(acm);
731 }
732 
733 /* Some controllers can't support CDC ACM ... */
734 static inline bool can_support_cdc(struct usb_configuration *c)
735 {
736  /* everything else is *probably* fine ... */
737  return true;
738 }
739 
753 {
754  struct f_acm *acm;
755  int status;
756 
757  if (!can_support_cdc(c))
758  return -EINVAL;
759 
760  /* REVISIT might want instance-specific strings to help
761  * distinguish instances ...
762  */
763 
764  /* maybe allocate device-global string IDs, and patch descriptors */
765  if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
766  status = usb_string_id(c->cdev);
767  if (status < 0)
768  return status;
769  acm_string_defs[ACM_CTRL_IDX].id = status;
770 
771  acm_control_interface_desc.iInterface = status;
772 
773  status = usb_string_id(c->cdev);
774  if (status < 0)
775  return status;
776  acm_string_defs[ACM_DATA_IDX].id = status;
777 
778  acm_data_interface_desc.iInterface = status;
779 
780  status = usb_string_id(c->cdev);
781  if (status < 0)
782  return status;
783  acm_string_defs[ACM_IAD_IDX].id = status;
784 
785  acm_iad_descriptor.iFunction = status;
786  }
787 
788  /* allocate and initialize one new instance */
789  acm = kzalloc(sizeof *acm, GFP_KERNEL);
790  if (!acm)
791  return -ENOMEM;
792 
793  spin_lock_init(&acm->lock);
794 
795  acm->port_num = port_num;
796 
797  acm->port.connect = acm_connect;
798  acm->port.disconnect = acm_disconnect;
799  acm->port.send_break = acm_send_break;
800 
801  acm->port.func.name = "acm";
802  acm->port.func.strings = acm_strings;
803  /* descriptors are per-instance copies */
804  acm->port.func.bind = acm_bind;
805  acm->port.func.unbind = acm_unbind;
806  acm->port.func.set_alt = acm_set_alt;
807  acm->port.func.setup = acm_setup;
808  acm->port.func.disable = acm_disable;
809 
810  status = usb_add_function(c, &acm->port.func);
811  if (status)
812  kfree(acm);
813  return status;
814 }