blob: f3bf5612af2dd072f72adf468c8026bbc30782a9 [file] [log] [blame]
David Brownell4d5a73d2008-06-19 18:18:40 -07001/*
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 *
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.
11 */
12
13/* #define VERBOSE_DEBUG */
14
15#include <linux/kernel.h>
16#include <linux/device.h>
17
18#include "u_serial.h"
19#include "gadget_chips.h"
20
21
22/*
23 * This CDC ACM function support just wraps control functions and
24 * notifications around the generic serial-over-usb code.
25 *
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.
31 *
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.
37 */
38
39struct acm_ep_descs {
40 struct usb_endpoint_descriptor *in;
41 struct usb_endpoint_descriptor *out;
42 struct usb_endpoint_descriptor *notify;
43};
44
45struct f_acm {
46 struct gserial port;
47 u8 ctrl_id, data_id;
48 u8 port_num;
49
David Brownell4d5a73d2008-06-19 18:18:40 -070050 struct acm_ep_descs fs;
David Brownell4d5a73d2008-06-19 18:18:40 -070051 struct acm_ep_descs hs;
52
53 struct usb_ep *notify;
54 struct usb_endpoint_descriptor *notify_desc;
55
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 */
60};
61
62static inline struct f_acm *func_to_acm(struct usb_function *f)
63{
64 return container_of(f, struct f_acm, port.func);
65}
66
67/*-------------------------------------------------------------------------*/
68
69/* notification endpoint uses smallish and infrequent fixed-size messages */
70
71#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
72#define GS_NOTIFY_MAXPACKET 8
73
74/* interface and class descriptors: */
75
76static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
77 .bLength = USB_DT_INTERFACE_SIZE,
78 .bDescriptorType = USB_DT_INTERFACE,
79 /* .bInterfaceNumber = DYNAMIC */
80 .bNumEndpoints = 1,
81 .bInterfaceClass = USB_CLASS_COMM,
82 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
83 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
84 /* .iInterface = DYNAMIC */
85};
86
87static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
88 .bLength = USB_DT_INTERFACE_SIZE,
89 .bDescriptorType = USB_DT_INTERFACE,
90 /* .bInterfaceNumber = DYNAMIC */
91 .bNumEndpoints = 2,
92 .bInterfaceClass = USB_CLASS_CDC_DATA,
93 .bInterfaceSubClass = 0,
94 .bInterfaceProtocol = 0,
95 /* .iInterface = DYNAMIC */
96};
97
98static 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),
103};
104
105static struct usb_cdc_call_mgmt_descriptor
106acm_call_mgmt_descriptor __initdata = {
107 .bLength = sizeof(acm_call_mgmt_descriptor),
108 .bDescriptorType = USB_DT_CS_INTERFACE,
109 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
110 .bmCapabilities = 0,
111 /* .bDataInterface = DYNAMIC */
112};
113
114static 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),
119};
120
121static 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 */
127};
128
129/* full speed support: */
130
131static 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,
138};
139
140static 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,
145};
146
147static 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,
152};
153
154static 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,
164 NULL,
165};
166
167/* high speed support: */
168
169static 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,
176};
177
178static 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),
183};
184
185static 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),
190};
191
192static 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,
202 NULL,
203};
204
205/* string descriptors: */
206
207#define ACM_CTRL_IDX 0
208#define ACM_DATA_IDX 1
209
210/* static strings, in UTF-8 */
211static 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 */ },
215};
216
217static struct usb_gadget_strings acm_string_table = {
218 .language = 0x0409, /* en-us */
219 .strings = acm_string_defs,
220};
221
222static struct usb_gadget_strings *acm_strings[] = {
223 &acm_string_table,
224 NULL,
225};
226
227/*-------------------------------------------------------------------------*/
228
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.
233 */
234
235static void acm_complete_set_line_coding(struct usb_ep *ep,
236 struct usb_request *req)
237{
238 struct f_acm *acm = ep->driver_data;
239 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
240
241 if (req->status != 0) {
242 DBG(cdev, "acm ttyGS%d completion, err %d\n",
243 acm->port_num, req->status);
244 return;
245 }
246
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);
251 usb_ep_set_halt(ep);
252 } else {
253 struct usb_cdc_line_coding *value = req->buf;
254
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.
261 */
262 acm->port_line_coding = *value;
263 }
264}
265
266static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
267{
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);
275
276 /* composite driver infrastructure handles everything except
277 * CDC class messages; interface activation uses set_alt().
278 */
279 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
280
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)
286 goto invalid;
287
288 value = w_length;
289 cdev->gadget->ep0->driver_data = acm;
290 req->complete = acm_complete_set_line_coding;
291 break;
292
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)
297 goto invalid;
298
299 value = min_t(unsigned, w_length,
300 sizeof(struct usb_cdc_line_coding));
301 memcpy(req->buf, &acm->port_line_coding, value);
302 break;
303
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)
308 goto invalid;
309
310 value = 0;
311
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.
315 */
316 acm->port_handshake_bits = w_value;
317 break;
318
319 default:
320invalid:
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);
324 }
325
326 /* respond with data transfer or status phase? */
327 if (value >= 0) {
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);
331 req->zero = 0;
332 req->length = value;
333 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
334 if (value < 0)
335 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
336 acm->port_num, value);
337 }
338
339 /* device either stalls (value < 0) or reports success */
340 return value;
341}
342
343static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
344{
345 struct f_acm *acm = func_to_acm(f);
346 struct usb_composite_dev *cdev = f->config->cdev;
347
348 /* we know alt == 0, so this is an activation or a reset */
349
350 if (intf == acm->ctrl_id) {
351 /* REVISIT this may need more work when we start to
352 * send notifications ...
353 */
354 if (acm->notify->driver_data) {
355 VDBG(cdev, "reset acm control interface %d\n", intf);
356 usb_ep_disable(acm->notify);
357 } else {
358 VDBG(cdev, "init acm ctrl interface %d\n", intf);
359 acm->notify_desc = ep_choose(cdev->gadget,
360 acm->hs.notify,
361 acm->fs.notify);
362 }
363 usb_ep_enable(acm->notify, acm->notify_desc);
364 acm->notify->driver_data = acm;
365
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);
370 } else {
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);
376 }
377 gserial_connect(&acm->port, acm->port_num);
378
379 } else
380 return -EINVAL;
381
382 return 0;
383}
384
385static void acm_disable(struct usb_function *f)
386{
387 struct f_acm *acm = func_to_acm(f);
388 struct usb_composite_dev *cdev = f->config->cdev;
389
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;
394}
395
396/*-------------------------------------------------------------------------*/
397
398/* ACM function driver setup/binding */
399static int __init
400acm_bind(struct usb_configuration *c, struct usb_function *f)
401{
402 struct usb_composite_dev *cdev = c->cdev;
403 struct f_acm *acm = func_to_acm(f);
404 int status;
405 struct usb_ep *ep;
406
407 /* allocate instance-specific interface IDs, and patch descriptors */
408 status = usb_interface_id(c, f);
409 if (status < 0)
410 goto fail;
411 acm->ctrl_id = status;
412
413 acm_control_interface_desc.bInterfaceNumber = status;
414 acm_union_desc .bMasterInterface0 = status;
415
416 status = usb_interface_id(c, f);
417 if (status < 0)
418 goto fail;
419 acm->data_id = status;
420
421 acm_data_interface_desc.bInterfaceNumber = status;
422 acm_union_desc.bSlaveInterface0 = status;
423 acm_call_mgmt_descriptor.bDataInterface = status;
424
425 status = -ENODEV;
426
427 /* allocate instance-specific endpoints */
428 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
429 if (!ep)
430 goto fail;
431 acm->port.in = ep;
432 ep->driver_data = cdev; /* claim */
433
434 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
435 if (!ep)
436 goto fail;
437 acm->port.out = ep;
438 ep->driver_data = cdev; /* claim */
439
440 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
441 if (!ep)
442 goto fail;
443 acm->notify = ep;
444 ep->driver_data = cdev; /* claim */
445
446 /* copy descriptors, and track endpoint copies */
447 f->descriptors = usb_copy_descriptors(acm_fs_function);
448
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);
455
456 /* support all relevant hardware speeds... we expect that when
457 * hardware is dual speed, all bulk-capable endpoints work at
458 * both speeds
459 */
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;
467
468 /* copy descriptors, and track endpoint copies */
469 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
470
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);
477 }
478
479 /* FIXME provide a callback for triggering notifications */
480
481 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
482 acm->port_num,
483 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
484 acm->port.in->name, acm->port.out->name,
485 acm->notify->name);
486 return 0;
487
488fail:
489 /* we might as well release our claims on endpoints */
490 if (acm->notify)
491 acm->notify->driver_data = NULL;
492 if (acm->port.out)
493 acm->port.out->driver_data = NULL;
494 if (acm->port.in)
495 acm->port.in->driver_data = NULL;
496
497 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
498
499 return status;
500}
501
502static void
503acm_unbind(struct usb_configuration *c, struct usb_function *f)
504{
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));
509}
510
511/* Some controllers can't support CDC ACM ... */
512static inline bool can_support_cdc(struct usb_configuration *c)
513{
514 /* SH3 doesn't support multiple interfaces */
515 if (gadget_is_sh(c->cdev->gadget))
516 return false;
517
518 /* sa1100 doesn't have a third interrupt endpoint */
519 if (gadget_is_sa1100(c->cdev->gadget))
520 return false;
521
522 /* everything else is *probably* fine ... */
523 return true;
524}
525
526/**
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
531 *
532 * Returns zero on success, else negative errno.
533 *
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.
537 */
538int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
539{
540 struct f_acm *acm;
541 int status;
542
543 if (!can_support_cdc(c))
544 return -EINVAL;
545
546 /* REVISIT might want instance-specific strings to help
547 * distinguish instances ...
548 */
549
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);
553 if (status < 0)
554 return status;
555 acm_string_defs[ACM_CTRL_IDX].id = status;
556
557 acm_control_interface_desc.iInterface = status;
558
559 status = usb_string_id(c->cdev);
560 if (status < 0)
561 return status;
562 acm_string_defs[ACM_DATA_IDX].id = status;
563
564 acm_data_interface_desc.iInterface = status;
565 }
566
567 /* allocate and initialize one new instance */
568 acm = kzalloc(sizeof *acm, GFP_KERNEL);
569 if (!acm)
570 return -ENOMEM;
571
572 acm->port_num = port_num;
573
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;
582
583 status = usb_add_function(c, &acm->port.func);
584 if (status)
585 kfree(acm);
586 return status;
587}