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
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
13 /* #define VERBOSE_DEBUG */
15 #include <linux/kernel.h>
16 #include <linux/device.h>
19 #include "gadget_chips.h"
23 * This CDC ACM function support just wraps control functions and
24 * notifications around the generic serial-over-usb code.
26 * Because CDC ACM is standardized by the USB-IF, many host operating
27 * systems have drivers for it. Accordingly, ACM is the preferred
28 * interop solution for serial-port type connections. The control
29 * models are often not necessary, and in any case don't do much in
30 * this bare-bones implementation.
32 * Note that even MS-Windows has some support for ACM. However, that
33 * support is somewhat broken because when you use ACM in a composite
34 * device, having multiple interfaces confuses the poor OS. It doesn't
35 * seem to understand CDC Union descriptors. The new "association"
36 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
40 struct usb_endpoint_descriptor *in;
41 struct usb_endpoint_descriptor *out;
42 struct usb_endpoint_descriptor *notify;
50 struct usb_descriptor_header **fs_function;
51 struct acm_ep_descs fs;
52 struct usb_descriptor_header **hs_function;
53 struct acm_ep_descs hs;
55 struct usb_ep *notify;
56 struct usb_endpoint_descriptor *notify_desc;
58 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
59 u16 port_handshake_bits;
60 #define RS232_RTS (1 << 1) /* unused with full duplex */
61 #define RS232_DTR (1 << 0) /* host is ready for data r/w */
64 static inline struct f_acm *func_to_acm(struct usb_function *f)
66 return container_of(f, struct f_acm, port.func);
69 /*-------------------------------------------------------------------------*/
71 /* notification endpoint uses smallish and infrequent fixed-size messages */
73 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
74 #define GS_NOTIFY_MAXPACKET 8
76 /* interface and class descriptors: */
78 static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
79 .bLength = USB_DT_INTERFACE_SIZE,
80 .bDescriptorType = USB_DT_INTERFACE,
81 /* .bInterfaceNumber = DYNAMIC */
83 .bInterfaceClass = USB_CLASS_COMM,
84 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
85 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
86 /* .iInterface = DYNAMIC */
89 static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
90 .bLength = USB_DT_INTERFACE_SIZE,
91 .bDescriptorType = USB_DT_INTERFACE,
92 /* .bInterfaceNumber = DYNAMIC */
94 .bInterfaceClass = USB_CLASS_CDC_DATA,
95 .bInterfaceSubClass = 0,
96 .bInterfaceProtocol = 0,
97 /* .iInterface = DYNAMIC */
100 static struct usb_cdc_header_desc acm_header_desc __initdata = {
101 .bLength = sizeof(acm_header_desc),
102 .bDescriptorType = USB_DT_CS_INTERFACE,
103 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
104 .bcdCDC = __constant_cpu_to_le16(0x0110),
107 static struct usb_cdc_call_mgmt_descriptor
108 acm_call_mgmt_descriptor __initdata = {
109 .bLength = sizeof(acm_call_mgmt_descriptor),
110 .bDescriptorType = USB_DT_CS_INTERFACE,
111 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
113 /* .bDataInterface = DYNAMIC */
116 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
117 .bLength = sizeof(acm_descriptor),
118 .bDescriptorType = USB_DT_CS_INTERFACE,
119 .bDescriptorSubType = USB_CDC_ACM_TYPE,
120 .bmCapabilities = (1 << 1),
123 static struct usb_cdc_union_desc acm_union_desc __initdata = {
124 .bLength = sizeof(acm_union_desc),
125 .bDescriptorType = USB_DT_CS_INTERFACE,
126 .bDescriptorSubType = USB_CDC_UNION_TYPE,
127 /* .bMasterInterface0 = DYNAMIC */
128 /* .bSlaveInterface0 = DYNAMIC */
131 /* full speed support: */
133 static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_INT,
138 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
139 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
142 static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = {
143 .bLength = USB_DT_ENDPOINT_SIZE,
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_OUT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
156 static struct usb_descriptor_header *acm_fs_function[] __initdata = {
157 (struct usb_descriptor_header *) &acm_control_interface_desc,
158 (struct usb_descriptor_header *) &acm_header_desc,
159 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
160 (struct usb_descriptor_header *) &acm_descriptor,
161 (struct usb_descriptor_header *) &acm_union_desc,
162 (struct usb_descriptor_header *) &acm_fs_notify_desc,
163 (struct usb_descriptor_header *) &acm_data_interface_desc,
164 (struct usb_descriptor_header *) &acm_fs_in_desc,
165 (struct usb_descriptor_header *) &acm_fs_out_desc,
169 /* high speed support: */
171 static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT,
174 .bEndpointAddress = USB_DIR_IN,
175 .bmAttributes = USB_ENDPOINT_XFER_INT,
176 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
177 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
180 static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = {
181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bmAttributes = USB_ENDPOINT_XFER_BULK,
184 .wMaxPacketSize = __constant_cpu_to_le16(512),
187 static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
190 .bmAttributes = USB_ENDPOINT_XFER_BULK,
191 .wMaxPacketSize = __constant_cpu_to_le16(512),
194 static struct usb_descriptor_header *acm_hs_function[] __initdata = {
195 (struct usb_descriptor_header *) &acm_control_interface_desc,
196 (struct usb_descriptor_header *) &acm_header_desc,
197 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
198 (struct usb_descriptor_header *) &acm_descriptor,
199 (struct usb_descriptor_header *) &acm_union_desc,
200 (struct usb_descriptor_header *) &acm_hs_notify_desc,
201 (struct usb_descriptor_header *) &acm_data_interface_desc,
202 (struct usb_descriptor_header *) &acm_hs_in_desc,
203 (struct usb_descriptor_header *) &acm_hs_out_desc,
207 /* string descriptors: */
209 #define ACM_CTRL_IDX 0
210 #define ACM_DATA_IDX 1
212 /* static strings, in UTF-8 */
213 static struct usb_string acm_string_defs[] = {
214 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
215 [ACM_DATA_IDX].s = "CDC ACM Data",
216 { /* ZEROES END LIST */ },
219 static struct usb_gadget_strings acm_string_table = {
220 .language = 0x0409, /* en-us */
221 .strings = acm_string_defs,
224 static struct usb_gadget_strings *acm_strings[] = {
229 /*-------------------------------------------------------------------------*/
231 /* ACM control ... data handling is delegated to tty library code.
232 * The main task of this function is to activate and deactivate
233 * that code based on device state; track parameters like line
234 * speed, handshake state, and so on; and issue notifications.
237 static void acm_complete_set_line_coding(struct usb_ep *ep,
238 struct usb_request *req)
240 struct f_acm *acm = ep->driver_data;
241 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
243 if (req->status != 0) {
244 DBG(cdev, "acm ttyGS%d completion, err %d\n",
245 acm->port_num, req->status);
249 /* normal completion */
250 if (req->actual != sizeof(acm->port_line_coding)) {
251 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
252 acm->port_num, req->actual);
255 struct usb_cdc_line_coding *value = req->buf;
257 /* REVISIT: we currently just remember this data.
258 * If we change that, (a) validate it first, then
259 * (b) update whatever hardware needs updating,
260 * (c) worry about locking. This is information on
261 * the order of 9600-8-N-1 ... most of which means
262 * nothing unless we control a real RS232 line.
264 acm->port_line_coding = *value;
268 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
270 struct f_acm *acm = func_to_acm(f);
271 struct usb_composite_dev *cdev = f->config->cdev;
272 struct usb_request *req = cdev->req;
273 int value = -EOPNOTSUPP;
274 u16 w_index = le16_to_cpu(ctrl->wIndex);
275 u16 w_value = le16_to_cpu(ctrl->wValue);
276 u16 w_length = le16_to_cpu(ctrl->wLength);
278 /* composite driver infrastructure handles everything except
279 * CDC class messages; interface activation uses set_alt().
281 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
283 /* SET_LINE_CODING ... just read and save what the host sends */
284 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
285 | USB_CDC_REQ_SET_LINE_CODING:
286 if (w_length != sizeof(struct usb_cdc_line_coding)
287 || w_index != acm->ctrl_id)
291 cdev->gadget->ep0->driver_data = acm;
292 req->complete = acm_complete_set_line_coding;
295 /* GET_LINE_CODING ... return what host sent, or initial value */
296 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
297 | USB_CDC_REQ_GET_LINE_CODING:
298 if (w_index != acm->ctrl_id)
301 value = min_t(unsigned, w_length,
302 sizeof(struct usb_cdc_line_coding));
303 memcpy(req->buf, &acm->port_line_coding, value);
306 /* SET_CONTROL_LINE_STATE ... save what the host sent */
307 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
308 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
309 if (w_index != acm->ctrl_id)
314 /* FIXME we should not allow data to flow until the
315 * host sets the RS232_DTR bit; and when it clears
316 * that bit, we should return to that no-flow state.
318 acm->port_handshake_bits = w_value;
323 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
324 ctrl->bRequestType, ctrl->bRequest,
325 w_value, w_index, w_length);
328 /* respond with data transfer or status phase? */
330 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
331 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
332 w_value, w_index, w_length);
335 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
337 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
338 acm->port_num, value);
341 /* device either stalls (value < 0) or reports success */
345 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
347 struct f_acm *acm = func_to_acm(f);
348 struct usb_composite_dev *cdev = f->config->cdev;
350 /* we know alt == 0, so this is an activation or a reset */
352 if (intf == acm->ctrl_id) {
353 /* REVISIT this may need more work when we start to
354 * send notifications ...
356 if (acm->notify->driver_data) {
357 VDBG(cdev, "reset acm control interface %d\n", intf);
358 usb_ep_disable(acm->notify);
360 VDBG(cdev, "init acm ctrl interface %d\n", intf);
361 acm->notify_desc = ep_choose(cdev->gadget,
365 usb_ep_enable(acm->notify, acm->notify_desc);
366 acm->notify->driver_data = acm;
368 } else if (intf == acm->data_id) {
369 if (acm->port.in->driver_data) {
370 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
371 gserial_disconnect(&acm->port);
373 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
374 acm->port.in_desc = ep_choose(cdev->gadget,
375 acm->hs.in, acm->fs.in);
376 acm->port.out_desc = ep_choose(cdev->gadget,
377 acm->hs.out, acm->fs.out);
379 gserial_connect(&acm->port, acm->port_num);
387 static void acm_disable(struct usb_function *f)
389 struct f_acm *acm = func_to_acm(f);
390 struct usb_composite_dev *cdev = f->config->cdev;
392 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
393 gserial_disconnect(&acm->port);
394 usb_ep_disable(acm->notify);
395 acm->notify->driver_data = NULL;
398 /*-------------------------------------------------------------------------*/
400 /* ACM function driver setup/binding */
402 acm_bind(struct usb_configuration *c, struct usb_function *f)
404 struct usb_composite_dev *cdev = c->cdev;
405 struct f_acm *acm = func_to_acm(f);
409 /* allocate instance-specific interface IDs, and patch descriptors */
410 status = usb_interface_id(c, f);
413 acm->ctrl_id = status;
415 acm_control_interface_desc.bInterfaceNumber = status;
416 acm_union_desc .bMasterInterface0 = status;
418 status = usb_interface_id(c, f);
421 acm->data_id = status;
423 acm_data_interface_desc.bInterfaceNumber = status;
424 acm_union_desc.bSlaveInterface0 = status;
425 acm_call_mgmt_descriptor.bDataInterface = status;
429 /* allocate instance-specific endpoints */
430 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
434 ep->driver_data = cdev; /* claim */
436 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
440 ep->driver_data = cdev; /* claim */
442 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
446 ep->driver_data = cdev; /* claim */
448 /* copy descriptors, and track endpoint copies */
449 f->descriptors = usb_copy_descriptors(acm_fs_function);
451 acm->fs.in = usb_find_endpoint(acm_fs_function,
452 f->descriptors, &acm_fs_in_desc);
453 acm->fs.out = usb_find_endpoint(acm_fs_function,
454 f->descriptors, &acm_fs_out_desc);
455 acm->fs.notify = usb_find_endpoint(acm_fs_function,
456 f->descriptors, &acm_fs_notify_desc);
458 /* support all relevant hardware speeds... we expect that when
459 * hardware is dual speed, all bulk-capable endpoints work at
462 if (gadget_is_dualspeed(c->cdev->gadget)) {
463 acm_hs_in_desc.bEndpointAddress =
464 acm_fs_in_desc.bEndpointAddress;
465 acm_hs_out_desc.bEndpointAddress =
466 acm_fs_out_desc.bEndpointAddress;
467 acm_hs_notify_desc.bEndpointAddress =
468 acm_fs_notify_desc.bEndpointAddress;
470 /* copy descriptors, and track endpoint copies */
471 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
473 acm->hs.in = usb_find_endpoint(acm_hs_function,
474 f->hs_descriptors, &acm_hs_in_desc);
475 acm->hs.out = usb_find_endpoint(acm_hs_function,
476 f->hs_descriptors, &acm_hs_out_desc);
477 acm->hs.notify = usb_find_endpoint(acm_hs_function,
478 f->hs_descriptors, &acm_hs_notify_desc);
481 /* FIXME provide a callback for triggering notifications */
483 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
485 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
486 acm->port.in->name, acm->port.out->name,
491 /* we might as well release our claims on endpoints */
493 acm->notify->driver_data = NULL;
495 acm->port.out->driver_data = NULL;
497 acm->port.in->driver_data = NULL;
499 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
505 acm_unbind(struct usb_configuration *c, struct usb_function *f)
507 if (gadget_is_dualspeed(c->cdev->gadget))
508 usb_free_descriptors(f->hs_descriptors);
509 usb_free_descriptors(f->descriptors);
510 kfree(func_to_acm(f));
513 /* Some controllers can't support CDC ACM ... */
514 static inline bool can_support_cdc(struct usb_configuration *c)
516 /* SH3 doesn't support multiple interfaces */
517 if (gadget_is_sh(c->cdev->gadget))
520 /* sa1100 doesn't have a third interrupt endpoint */
521 if (gadget_is_sa1100(c->cdev->gadget))
524 /* everything else is *probably* fine ... */
529 * acm_bind_config - add a CDC ACM function to a configuration
530 * @c: the configuration to support the CDC ACM instance
531 * @port_num: /dev/ttyGS* port this interface will use
532 * Context: single threaded during gadget setup
534 * Returns zero on success, else negative errno.
536 * Caller must have called @gserial_setup() with enough ports to
537 * handle all the ones it binds. Caller is also responsible
538 * for calling @gserial_cleanup() before module unload.
540 int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
545 if (!can_support_cdc(c))
548 /* REVISIT might want instance-specific strings to help
549 * distinguish instances ...
552 /* maybe allocate device-global string IDs, and patch descriptors */
553 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
554 status = usb_string_id(c->cdev);
557 acm_string_defs[ACM_CTRL_IDX].id = status;
559 acm_control_interface_desc.iInterface = status;
561 status = usb_string_id(c->cdev);
564 acm_string_defs[ACM_DATA_IDX].id = status;
566 acm_data_interface_desc.iInterface = status;
569 /* allocate and initialize one new instance */
570 acm = kzalloc(sizeof *acm, GFP_KERNEL);
574 acm->port_num = port_num;
576 acm->port.func.name = "acm";
577 acm->port.func.strings = acm_strings;
578 /* descriptors are per-instance copies */
579 acm->port.func.bind = acm_bind;
580 acm->port.func.unbind = acm_unbind;
581 acm->port.func.set_alt = acm_set_alt;
582 acm->port.func.setup = acm_setup;
583 acm->port.func.disable = acm_disable;
585 status = usb_add_function(c, &acm->port.func);