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 acm_ep_descs fs;
51 struct acm_ep_descs hs;
53 struct usb_ep *notify;
54 struct usb_endpoint_descriptor *notify_desc;
56 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
57 u16 port_handshake_bits;
58 #define RS232_RTS (1 << 1) /* unused with full duplex */
59 #define RS232_DTR (1 << 0) /* host is ready for data r/w */
62 static inline struct f_acm *func_to_acm(struct usb_function *f)
64 return container_of(f, struct f_acm, port.func);
67 /*-------------------------------------------------------------------------*/
69 /* notification endpoint uses smallish and infrequent fixed-size messages */
71 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
72 #define GS_NOTIFY_MAXPACKET 8
74 /* interface and class descriptors: */
76 static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
77 .bLength = USB_DT_INTERFACE_SIZE,
78 .bDescriptorType = USB_DT_INTERFACE,
79 /* .bInterfaceNumber = DYNAMIC */
81 .bInterfaceClass = USB_CLASS_COMM,
82 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
83 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
84 /* .iInterface = DYNAMIC */
87 static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
88 .bLength = USB_DT_INTERFACE_SIZE,
89 .bDescriptorType = USB_DT_INTERFACE,
90 /* .bInterfaceNumber = DYNAMIC */
92 .bInterfaceClass = USB_CLASS_CDC_DATA,
93 .bInterfaceSubClass = 0,
94 .bInterfaceProtocol = 0,
95 /* .iInterface = DYNAMIC */
98 static struct usb_cdc_header_desc acm_header_desc __initdata = {
99 .bLength = sizeof(acm_header_desc),
100 .bDescriptorType = USB_DT_CS_INTERFACE,
101 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
102 .bcdCDC = __constant_cpu_to_le16(0x0110),
105 static struct usb_cdc_call_mgmt_descriptor
106 acm_call_mgmt_descriptor __initdata = {
107 .bLength = sizeof(acm_call_mgmt_descriptor),
108 .bDescriptorType = USB_DT_CS_INTERFACE,
109 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
111 /* .bDataInterface = DYNAMIC */
114 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
115 .bLength = sizeof(acm_descriptor),
116 .bDescriptorType = USB_DT_CS_INTERFACE,
117 .bDescriptorSubType = USB_CDC_ACM_TYPE,
118 .bmCapabilities = (1 << 1),
121 static struct usb_cdc_union_desc acm_union_desc __initdata = {
122 .bLength = sizeof(acm_union_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_UNION_TYPE,
125 /* .bMasterInterface0 = DYNAMIC */
126 /* .bSlaveInterface0 = DYNAMIC */
129 /* full speed support: */
131 static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
132 .bLength = USB_DT_ENDPOINT_SIZE,
133 .bDescriptorType = USB_DT_ENDPOINT,
134 .bEndpointAddress = USB_DIR_IN,
135 .bmAttributes = USB_ENDPOINT_XFER_INT,
136 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
137 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
140 static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = {
141 .bLength = USB_DT_ENDPOINT_SIZE,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_IN,
144 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147 static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
150 .bEndpointAddress = USB_DIR_OUT,
151 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 static struct usb_descriptor_header *acm_fs_function[] __initdata = {
155 (struct usb_descriptor_header *) &acm_control_interface_desc,
156 (struct usb_descriptor_header *) &acm_header_desc,
157 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
158 (struct usb_descriptor_header *) &acm_descriptor,
159 (struct usb_descriptor_header *) &acm_union_desc,
160 (struct usb_descriptor_header *) &acm_fs_notify_desc,
161 (struct usb_descriptor_header *) &acm_data_interface_desc,
162 (struct usb_descriptor_header *) &acm_fs_in_desc,
163 (struct usb_descriptor_header *) &acm_fs_out_desc,
167 /* high speed support: */
169 static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = USB_DIR_IN,
173 .bmAttributes = USB_ENDPOINT_XFER_INT,
174 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
175 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
178 static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182 .wMaxPacketSize = __constant_cpu_to_le16(512),
185 static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188 .bmAttributes = USB_ENDPOINT_XFER_BULK,
189 .wMaxPacketSize = __constant_cpu_to_le16(512),
192 static struct usb_descriptor_header *acm_hs_function[] __initdata = {
193 (struct usb_descriptor_header *) &acm_control_interface_desc,
194 (struct usb_descriptor_header *) &acm_header_desc,
195 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
196 (struct usb_descriptor_header *) &acm_descriptor,
197 (struct usb_descriptor_header *) &acm_union_desc,
198 (struct usb_descriptor_header *) &acm_hs_notify_desc,
199 (struct usb_descriptor_header *) &acm_data_interface_desc,
200 (struct usb_descriptor_header *) &acm_hs_in_desc,
201 (struct usb_descriptor_header *) &acm_hs_out_desc,
205 /* string descriptors: */
207 #define ACM_CTRL_IDX 0
208 #define ACM_DATA_IDX 1
210 /* static strings, in UTF-8 */
211 static struct usb_string acm_string_defs[] = {
212 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
213 [ACM_DATA_IDX].s = "CDC ACM Data",
214 { /* ZEROES END LIST */ },
217 static struct usb_gadget_strings acm_string_table = {
218 .language = 0x0409, /* en-us */
219 .strings = acm_string_defs,
222 static struct usb_gadget_strings *acm_strings[] = {
227 /*-------------------------------------------------------------------------*/
229 /* ACM control ... data handling is delegated to tty library code.
230 * The main task of this function is to activate and deactivate
231 * that code based on device state; track parameters like line
232 * speed, handshake state, and so on; and issue notifications.
235 static void acm_complete_set_line_coding(struct usb_ep *ep,
236 struct usb_request *req)
238 struct f_acm *acm = ep->driver_data;
239 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
241 if (req->status != 0) {
242 DBG(cdev, "acm ttyGS%d completion, err %d\n",
243 acm->port_num, req->status);
247 /* normal completion */
248 if (req->actual != sizeof(acm->port_line_coding)) {
249 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
250 acm->port_num, req->actual);
253 struct usb_cdc_line_coding *value = req->buf;
255 /* REVISIT: we currently just remember this data.
256 * If we change that, (a) validate it first, then
257 * (b) update whatever hardware needs updating,
258 * (c) worry about locking. This is information on
259 * the order of 9600-8-N-1 ... most of which means
260 * nothing unless we control a real RS232 line.
262 acm->port_line_coding = *value;
266 static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
268 struct f_acm *acm = func_to_acm(f);
269 struct usb_composite_dev *cdev = f->config->cdev;
270 struct usb_request *req = cdev->req;
271 int value = -EOPNOTSUPP;
272 u16 w_index = le16_to_cpu(ctrl->wIndex);
273 u16 w_value = le16_to_cpu(ctrl->wValue);
274 u16 w_length = le16_to_cpu(ctrl->wLength);
276 /* composite driver infrastructure handles everything except
277 * CDC class messages; interface activation uses set_alt().
279 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
281 /* SET_LINE_CODING ... just read and save what the host sends */
282 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
283 | USB_CDC_REQ_SET_LINE_CODING:
284 if (w_length != sizeof(struct usb_cdc_line_coding)
285 || w_index != acm->ctrl_id)
289 cdev->gadget->ep0->driver_data = acm;
290 req->complete = acm_complete_set_line_coding;
293 /* GET_LINE_CODING ... return what host sent, or initial value */
294 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
295 | USB_CDC_REQ_GET_LINE_CODING:
296 if (w_index != acm->ctrl_id)
299 value = min_t(unsigned, w_length,
300 sizeof(struct usb_cdc_line_coding));
301 memcpy(req->buf, &acm->port_line_coding, value);
304 /* SET_CONTROL_LINE_STATE ... save what the host sent */
305 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
306 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
307 if (w_index != acm->ctrl_id)
312 /* FIXME we should not allow data to flow until the
313 * host sets the RS232_DTR bit; and when it clears
314 * that bit, we should return to that no-flow state.
316 acm->port_handshake_bits = w_value;
321 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
322 ctrl->bRequestType, ctrl->bRequest,
323 w_value, w_index, w_length);
326 /* respond with data transfer or status phase? */
328 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
329 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
330 w_value, w_index, w_length);
333 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
335 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
336 acm->port_num, value);
339 /* device either stalls (value < 0) or reports success */
343 static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
345 struct f_acm *acm = func_to_acm(f);
346 struct usb_composite_dev *cdev = f->config->cdev;
348 /* we know alt == 0, so this is an activation or a reset */
350 if (intf == acm->ctrl_id) {
351 /* REVISIT this may need more work when we start to
352 * send notifications ...
354 if (acm->notify->driver_data) {
355 VDBG(cdev, "reset acm control interface %d\n", intf);
356 usb_ep_disable(acm->notify);
358 VDBG(cdev, "init acm ctrl interface %d\n", intf);
359 acm->notify_desc = ep_choose(cdev->gadget,
363 usb_ep_enable(acm->notify, acm->notify_desc);
364 acm->notify->driver_data = acm;
366 } else if (intf == acm->data_id) {
367 if (acm->port.in->driver_data) {
368 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
369 gserial_disconnect(&acm->port);
371 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
372 acm->port.in_desc = ep_choose(cdev->gadget,
373 acm->hs.in, acm->fs.in);
374 acm->port.out_desc = ep_choose(cdev->gadget,
375 acm->hs.out, acm->fs.out);
377 gserial_connect(&acm->port, acm->port_num);
385 static void acm_disable(struct usb_function *f)
387 struct f_acm *acm = func_to_acm(f);
388 struct usb_composite_dev *cdev = f->config->cdev;
390 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
391 gserial_disconnect(&acm->port);
392 usb_ep_disable(acm->notify);
393 acm->notify->driver_data = NULL;
396 /*-------------------------------------------------------------------------*/
398 /* ACM function driver setup/binding */
400 acm_bind(struct usb_configuration *c, struct usb_function *f)
402 struct usb_composite_dev *cdev = c->cdev;
403 struct f_acm *acm = func_to_acm(f);
407 /* allocate instance-specific interface IDs, and patch descriptors */
408 status = usb_interface_id(c, f);
411 acm->ctrl_id = status;
413 acm_control_interface_desc.bInterfaceNumber = status;
414 acm_union_desc .bMasterInterface0 = status;
416 status = usb_interface_id(c, f);
419 acm->data_id = status;
421 acm_data_interface_desc.bInterfaceNumber = status;
422 acm_union_desc.bSlaveInterface0 = status;
423 acm_call_mgmt_descriptor.bDataInterface = status;
427 /* allocate instance-specific endpoints */
428 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
432 ep->driver_data = cdev; /* claim */
434 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
438 ep->driver_data = cdev; /* claim */
440 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
444 ep->driver_data = cdev; /* claim */
446 /* copy descriptors, and track endpoint copies */
447 f->descriptors = usb_copy_descriptors(acm_fs_function);
449 acm->fs.in = usb_find_endpoint(acm_fs_function,
450 f->descriptors, &acm_fs_in_desc);
451 acm->fs.out = usb_find_endpoint(acm_fs_function,
452 f->descriptors, &acm_fs_out_desc);
453 acm->fs.notify = usb_find_endpoint(acm_fs_function,
454 f->descriptors, &acm_fs_notify_desc);
456 /* support all relevant hardware speeds... we expect that when
457 * hardware is dual speed, all bulk-capable endpoints work at
460 if (gadget_is_dualspeed(c->cdev->gadget)) {
461 acm_hs_in_desc.bEndpointAddress =
462 acm_fs_in_desc.bEndpointAddress;
463 acm_hs_out_desc.bEndpointAddress =
464 acm_fs_out_desc.bEndpointAddress;
465 acm_hs_notify_desc.bEndpointAddress =
466 acm_fs_notify_desc.bEndpointAddress;
468 /* copy descriptors, and track endpoint copies */
469 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
471 acm->hs.in = usb_find_endpoint(acm_hs_function,
472 f->hs_descriptors, &acm_hs_in_desc);
473 acm->hs.out = usb_find_endpoint(acm_hs_function,
474 f->hs_descriptors, &acm_hs_out_desc);
475 acm->hs.notify = usb_find_endpoint(acm_hs_function,
476 f->hs_descriptors, &acm_hs_notify_desc);
479 /* FIXME provide a callback for triggering notifications */
481 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
483 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
484 acm->port.in->name, acm->port.out->name,
489 /* we might as well release our claims on endpoints */
491 acm->notify->driver_data = NULL;
493 acm->port.out->driver_data = NULL;
495 acm->port.in->driver_data = NULL;
497 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
503 acm_unbind(struct usb_configuration *c, struct usb_function *f)
505 if (gadget_is_dualspeed(c->cdev->gadget))
506 usb_free_descriptors(f->hs_descriptors);
507 usb_free_descriptors(f->descriptors);
508 kfree(func_to_acm(f));
511 /* Some controllers can't support CDC ACM ... */
512 static inline bool can_support_cdc(struct usb_configuration *c)
514 /* SH3 doesn't support multiple interfaces */
515 if (gadget_is_sh(c->cdev->gadget))
518 /* sa1100 doesn't have a third interrupt endpoint */
519 if (gadget_is_sa1100(c->cdev->gadget))
522 /* everything else is *probably* fine ... */
527 * acm_bind_config - add a CDC ACM function to a configuration
528 * @c: the configuration to support the CDC ACM instance
529 * @port_num: /dev/ttyGS* port this interface will use
530 * Context: single threaded during gadget setup
532 * Returns zero on success, else negative errno.
534 * Caller must have called @gserial_setup() with enough ports to
535 * handle all the ones it binds. Caller is also responsible
536 * for calling @gserial_cleanup() before module unload.
538 int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
543 if (!can_support_cdc(c))
546 /* REVISIT might want instance-specific strings to help
547 * distinguish instances ...
550 /* maybe allocate device-global string IDs, and patch descriptors */
551 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
552 status = usb_string_id(c->cdev);
555 acm_string_defs[ACM_CTRL_IDX].id = status;
557 acm_control_interface_desc.iInterface = status;
559 status = usb_string_id(c->cdev);
562 acm_string_defs[ACM_DATA_IDX].id = status;
564 acm_data_interface_desc.iInterface = status;
567 /* allocate and initialize one new instance */
568 acm = kzalloc(sizeof *acm, GFP_KERNEL);
572 acm->port_num = port_num;
574 acm->port.func.name = "acm";
575 acm->port.func.strings = acm_strings;
576 /* descriptors are per-instance copies */
577 acm->port.func.bind = acm_bind;
578 acm->port.func.unbind = acm_unbind;
579 acm->port.func.set_alt = acm_set_alt;
580 acm->port.func.setup = acm_setup;
581 acm->port.func.disable = acm_disable;
583 status = usb_add_function(c, &acm->port.func);