blob: cf2e7fc7659f4218c8c6852bfe888e25980a76e1 [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
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01007 * Copyright (C) 2009 by Samsung Electronics
8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
David Brownell4d5a73d2008-06-19 18:18:40 -07009 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070018#include <linux/kernel.h>
19#include <linux/device.h>
Mike Lockwood789ef232010-01-12 10:33:59 -080020#include <linux/usb/android_composite.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070021
22#include "u_serial.h"
23#include "gadget_chips.h"
24
25
26/*
27 * This CDC ACM function support just wraps control functions and
28 * notifications around the generic serial-over-usb code.
29 *
30 * Because CDC ACM is standardized by the USB-IF, many host operating
31 * systems have drivers for it. Accordingly, ACM is the preferred
32 * interop solution for serial-port type connections. The control
33 * models are often not necessary, and in any case don't do much in
34 * this bare-bones implementation.
35 *
36 * Note that even MS-Windows has some support for ACM. However, that
37 * support is somewhat broken because when you use ACM in a composite
38 * device, having multiple interfaces confuses the poor OS. It doesn't
39 * seem to understand CDC Union descriptors. The new "association"
40 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
41 */
42
43struct acm_ep_descs {
44 struct usb_endpoint_descriptor *in;
45 struct usb_endpoint_descriptor *out;
46 struct usb_endpoint_descriptor *notify;
47};
48
49struct f_acm {
50 struct gserial port;
51 u8 ctrl_id, data_id;
52 u8 port_num;
53
David Brownell1f1ba112008-08-06 18:49:57 -070054 u8 pending;
55
56 /* lock is mostly for pending and notify_req ... they get accessed
57 * by callbacks both from tty (open/close/break) under its spinlock,
58 * and notify_req.complete() which can't use that lock.
59 */
60 spinlock_t lock;
61
David Brownell4d5a73d2008-06-19 18:18:40 -070062 struct acm_ep_descs fs;
David Brownell4d5a73d2008-06-19 18:18:40 -070063 struct acm_ep_descs hs;
64
65 struct usb_ep *notify;
66 struct usb_endpoint_descriptor *notify_desc;
David Brownell1f1ba112008-08-06 18:49:57 -070067 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070068
69 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070070
71 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070072 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070073#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
74#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
75
76 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
77 u16 serial_state;
78#define ACM_CTRL_OVERRUN (1 << 6)
79#define ACM_CTRL_PARITY (1 << 5)
80#define ACM_CTRL_FRAMING (1 << 4)
81#define ACM_CTRL_RI (1 << 3)
82#define ACM_CTRL_BRK (1 << 2)
83#define ACM_CTRL_DSR (1 << 1)
84#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070085};
86
87static inline struct f_acm *func_to_acm(struct usb_function *f)
88{
89 return container_of(f, struct f_acm, port.func);
90}
91
David Brownell1f1ba112008-08-06 18:49:57 -070092static inline struct f_acm *port_to_acm(struct gserial *p)
93{
94 return container_of(p, struct f_acm, port);
95}
96
David Brownell4d5a73d2008-06-19 18:18:40 -070097/*-------------------------------------------------------------------------*/
98
99/* notification endpoint uses smallish and infrequent fixed-size messages */
100
101#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
David Brownell1f1ba112008-08-06 18:49:57 -0700102#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -0700103
104/* interface and class descriptors: */
105
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100106static struct usb_interface_assoc_descriptor
107acm_iad_descriptor = {
108 .bLength = sizeof acm_iad_descriptor,
109 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
110
111 /* .bFirstInterface = DYNAMIC, */
112 .bInterfaceCount = 2, // control + data
113 .bFunctionClass = USB_CLASS_COMM,
114 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530115 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100116 /* .iFunction = DYNAMIC */
117};
118
119
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200120static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700121 .bLength = USB_DT_INTERFACE_SIZE,
122 .bDescriptorType = USB_DT_INTERFACE,
123 /* .bInterfaceNumber = DYNAMIC */
124 .bNumEndpoints = 1,
125 .bInterfaceClass = USB_CLASS_COMM,
126 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
127 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
128 /* .iInterface = DYNAMIC */
129};
130
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200131static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700132 .bLength = USB_DT_INTERFACE_SIZE,
133 .bDescriptorType = USB_DT_INTERFACE,
134 /* .bInterfaceNumber = DYNAMIC */
135 .bNumEndpoints = 2,
136 .bInterfaceClass = USB_CLASS_CDC_DATA,
137 .bInterfaceSubClass = 0,
138 .bInterfaceProtocol = 0,
139 /* .iInterface = DYNAMIC */
140};
141
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200142static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700143 .bLength = sizeof(acm_header_desc),
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800146 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700147};
148
149static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200150acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700151 .bLength = sizeof(acm_call_mgmt_descriptor),
152 .bDescriptorType = USB_DT_CS_INTERFACE,
153 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
154 .bmCapabilities = 0,
155 /* .bDataInterface = DYNAMIC */
156};
157
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200158static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700159 .bLength = sizeof(acm_descriptor),
160 .bDescriptorType = USB_DT_CS_INTERFACE,
161 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700162 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700163};
164
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200165static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700166 .bLength = sizeof(acm_union_desc),
167 .bDescriptorType = USB_DT_CS_INTERFACE,
168 .bDescriptorSubType = USB_CDC_UNION_TYPE,
169 /* .bMasterInterface0 = DYNAMIC */
170 /* .bSlaveInterface0 = DYNAMIC */
171};
172
173/* full speed support: */
174
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200175static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700176 .bLength = USB_DT_ENDPOINT_SIZE,
177 .bDescriptorType = USB_DT_ENDPOINT,
178 .bEndpointAddress = USB_DIR_IN,
179 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800180 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700181 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
182};
183
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200184static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187 .bEndpointAddress = USB_DIR_IN,
188 .bmAttributes = USB_ENDPOINT_XFER_BULK,
189};
190
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200191static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194 .bEndpointAddress = USB_DIR_OUT,
195 .bmAttributes = USB_ENDPOINT_XFER_BULK,
196};
197
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200198static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100199 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700200 (struct usb_descriptor_header *) &acm_control_interface_desc,
201 (struct usb_descriptor_header *) &acm_header_desc,
202 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
203 (struct usb_descriptor_header *) &acm_descriptor,
204 (struct usb_descriptor_header *) &acm_union_desc,
205 (struct usb_descriptor_header *) &acm_fs_notify_desc,
206 (struct usb_descriptor_header *) &acm_data_interface_desc,
207 (struct usb_descriptor_header *) &acm_fs_in_desc,
208 (struct usb_descriptor_header *) &acm_fs_out_desc,
209 NULL,
210};
211
212/* high speed support: */
213
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200214static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700215 .bLength = USB_DT_ENDPOINT_SIZE,
216 .bDescriptorType = USB_DT_ENDPOINT,
217 .bEndpointAddress = USB_DIR_IN,
218 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800219 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700220 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
221};
222
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200223static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700224 .bLength = USB_DT_ENDPOINT_SIZE,
225 .bDescriptorType = USB_DT_ENDPOINT,
226 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800227 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700228};
229
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200230static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800234 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700235};
236
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200237static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100238 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700239 (struct usb_descriptor_header *) &acm_control_interface_desc,
240 (struct usb_descriptor_header *) &acm_header_desc,
241 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
242 (struct usb_descriptor_header *) &acm_descriptor,
243 (struct usb_descriptor_header *) &acm_union_desc,
244 (struct usb_descriptor_header *) &acm_hs_notify_desc,
245 (struct usb_descriptor_header *) &acm_data_interface_desc,
246 (struct usb_descriptor_header *) &acm_hs_in_desc,
247 (struct usb_descriptor_header *) &acm_hs_out_desc,
248 NULL,
249};
250
251/* string descriptors: */
252
253#define ACM_CTRL_IDX 0
254#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100255#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700256
257/* static strings, in UTF-8 */
258static struct usb_string acm_string_defs[] = {
259 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
260 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100261 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700262 { /* ZEROES END LIST */ },
263};
264
265static struct usb_gadget_strings acm_string_table = {
266 .language = 0x0409, /* en-us */
267 .strings = acm_string_defs,
268};
269
270static struct usb_gadget_strings *acm_strings[] = {
271 &acm_string_table,
272 NULL,
273};
274
275/*-------------------------------------------------------------------------*/
276
277/* ACM control ... data handling is delegated to tty library code.
278 * The main task of this function is to activate and deactivate
279 * that code based on device state; track parameters like line
280 * speed, handshake state, and so on; and issue notifications.
281 */
282
283static void acm_complete_set_line_coding(struct usb_ep *ep,
284 struct usb_request *req)
285{
286 struct f_acm *acm = ep->driver_data;
287 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
288
289 if (req->status != 0) {
290 DBG(cdev, "acm ttyGS%d completion, err %d\n",
291 acm->port_num, req->status);
292 return;
293 }
294
295 /* normal completion */
296 if (req->actual != sizeof(acm->port_line_coding)) {
297 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
298 acm->port_num, req->actual);
299 usb_ep_set_halt(ep);
300 } else {
301 struct usb_cdc_line_coding *value = req->buf;
302
303 /* REVISIT: we currently just remember this data.
304 * If we change that, (a) validate it first, then
305 * (b) update whatever hardware needs updating,
306 * (c) worry about locking. This is information on
307 * the order of 9600-8-N-1 ... most of which means
308 * nothing unless we control a real RS232 line.
309 */
310 acm->port_line_coding = *value;
311 }
312}
313
314static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
315{
316 struct f_acm *acm = func_to_acm(f);
317 struct usb_composite_dev *cdev = f->config->cdev;
318 struct usb_request *req = cdev->req;
319 int value = -EOPNOTSUPP;
320 u16 w_index = le16_to_cpu(ctrl->wIndex);
321 u16 w_value = le16_to_cpu(ctrl->wValue);
322 u16 w_length = le16_to_cpu(ctrl->wLength);
323
324 /* composite driver infrastructure handles everything except
325 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700326 *
327 * Note CDC spec table 4 lists the ACM request profile. It requires
328 * encapsulated command support ... we don't handle any, and respond
329 * to them by stalling. Options include get/set/clear comm features
330 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700331 */
332 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
333
334 /* SET_LINE_CODING ... just read and save what the host sends */
335 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
336 | USB_CDC_REQ_SET_LINE_CODING:
337 if (w_length != sizeof(struct usb_cdc_line_coding)
338 || w_index != acm->ctrl_id)
339 goto invalid;
340
341 value = w_length;
342 cdev->gadget->ep0->driver_data = acm;
343 req->complete = acm_complete_set_line_coding;
344 break;
345
346 /* GET_LINE_CODING ... return what host sent, or initial value */
347 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
348 | USB_CDC_REQ_GET_LINE_CODING:
349 if (w_index != acm->ctrl_id)
350 goto invalid;
351
352 value = min_t(unsigned, w_length,
353 sizeof(struct usb_cdc_line_coding));
354 memcpy(req->buf, &acm->port_line_coding, value);
355 break;
356
357 /* SET_CONTROL_LINE_STATE ... save what the host sent */
358 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
359 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
360 if (w_index != acm->ctrl_id)
361 goto invalid;
362
363 value = 0;
364
365 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700366 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700367 * that bit, we should return to that no-flow state.
368 */
369 acm->port_handshake_bits = w_value;
370 break;
371
372 default:
373invalid:
374 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
375 ctrl->bRequestType, ctrl->bRequest,
376 w_value, w_index, w_length);
377 }
378
379 /* respond with data transfer or status phase? */
380 if (value >= 0) {
381 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
382 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
383 w_value, w_index, w_length);
384 req->zero = 0;
385 req->length = value;
386 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
387 if (value < 0)
388 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
389 acm->port_num, value);
390 }
391
392 /* device either stalls (value < 0) or reports success */
393 return value;
394}
395
396static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
397{
398 struct f_acm *acm = func_to_acm(f);
399 struct usb_composite_dev *cdev = f->config->cdev;
400
401 /* we know alt == 0, so this is an activation or a reset */
402
403 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700404 if (acm->notify->driver_data) {
405 VDBG(cdev, "reset acm control interface %d\n", intf);
406 usb_ep_disable(acm->notify);
407 } else {
408 VDBG(cdev, "init acm ctrl interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700409 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800410 acm->notify_desc = ep_choose(cdev->gadget,
411 acm->hs.notify,
412 acm->fs.notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700413 usb_ep_enable(acm->notify, acm->notify_desc);
414 acm->notify->driver_data = acm;
415
416 } else if (intf == acm->data_id) {
417 if (acm->port.in->driver_data) {
418 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
419 gserial_disconnect(&acm->port);
420 } else {
421 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
David Brownell4d5a73d2008-06-19 18:18:40 -0700422 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800423 acm->port.in_desc = ep_choose(cdev->gadget,
424 acm->hs.in, acm->fs.in);
425 acm->port.out_desc = ep_choose(cdev->gadget,
426 acm->hs.out, acm->fs.out);
David Brownell4d5a73d2008-06-19 18:18:40 -0700427 gserial_connect(&acm->port, acm->port_num);
428
429 } else
430 return -EINVAL;
431
432 return 0;
433}
434
435static void acm_disable(struct usb_function *f)
436{
437 struct f_acm *acm = func_to_acm(f);
438 struct usb_composite_dev *cdev = f->config->cdev;
439
440 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
441 gserial_disconnect(&acm->port);
442 usb_ep_disable(acm->notify);
443 acm->notify->driver_data = NULL;
444}
445
446/*-------------------------------------------------------------------------*/
447
David Brownell1f1ba112008-08-06 18:49:57 -0700448/**
449 * acm_cdc_notify - issue CDC notification to host
450 * @acm: wraps host to be notified
451 * @type: notification type
452 * @value: Refer to cdc specs, wValue field.
453 * @data: data to be sent
454 * @length: size of data
455 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
456 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200457 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700458 *
459 * See section 6.3.5 of the CDC 1.1 specification for information
460 * about the only notification we issue: SerialState change.
461 */
462static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
463 void *data, unsigned length)
464{
465 struct usb_ep *ep = acm->notify;
466 struct usb_request *req;
467 struct usb_cdc_notification *notify;
468 const unsigned len = sizeof(*notify) + length;
469 void *buf;
470 int status;
471
472 req = acm->notify_req;
473 acm->notify_req = NULL;
474 acm->pending = false;
475
476 req->length = len;
477 notify = req->buf;
478 buf = notify + 1;
479
480 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
481 | USB_RECIP_INTERFACE;
482 notify->bNotificationType = type;
483 notify->wValue = cpu_to_le16(value);
484 notify->wIndex = cpu_to_le16(acm->ctrl_id);
485 notify->wLength = cpu_to_le16(length);
486 memcpy(buf, data, length);
487
David Brownelle50ae572008-11-12 11:35:13 -0800488 /* ep_queue() can complete immediately if it fills the fifo... */
489 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700490 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800491 spin_lock(&acm->lock);
492
David Brownell1f1ba112008-08-06 18:49:57 -0700493 if (status < 0) {
494 ERROR(acm->port.func.config->cdev,
495 "acm ttyGS%d can't notify serial state, %d\n",
496 acm->port_num, status);
497 acm->notify_req = req;
498 }
499
500 return status;
501}
502
503static int acm_notify_serial_state(struct f_acm *acm)
504{
505 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
506 int status;
507
508 spin_lock(&acm->lock);
509 if (acm->notify_req) {
510 DBG(cdev, "acm ttyGS%d serial state %04x\n",
511 acm->port_num, acm->serial_state);
512 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
513 0, &acm->serial_state, sizeof(acm->serial_state));
514 } else {
515 acm->pending = true;
516 status = 0;
517 }
518 spin_unlock(&acm->lock);
519 return status;
520}
521
522static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
523{
524 struct f_acm *acm = req->context;
525 u8 doit = false;
526
527 /* on this call path we do NOT hold the port spinlock,
528 * which is why ACM needs its own spinlock
529 */
530 spin_lock(&acm->lock);
531 if (req->status != -ESHUTDOWN)
532 doit = acm->pending;
533 acm->notify_req = req;
534 spin_unlock(&acm->lock);
535
536 if (doit)
537 acm_notify_serial_state(acm);
538}
539
540/* connect == the TTY link is open */
541
542static void acm_connect(struct gserial *port)
543{
544 struct f_acm *acm = port_to_acm(port);
545
546 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
547 acm_notify_serial_state(acm);
548}
549
550static void acm_disconnect(struct gserial *port)
551{
552 struct f_acm *acm = port_to_acm(port);
553
554 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
555 acm_notify_serial_state(acm);
556}
557
558static int acm_send_break(struct gserial *port, int duration)
559{
560 struct f_acm *acm = port_to_acm(port);
561 u16 state;
562
563 state = acm->serial_state;
564 state &= ~ACM_CTRL_BRK;
565 if (duration)
566 state |= ACM_CTRL_BRK;
567
568 acm->serial_state = state;
569 return acm_notify_serial_state(acm);
570}
571
572/*-------------------------------------------------------------------------*/
573
David Brownell4d5a73d2008-06-19 18:18:40 -0700574/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200575static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700576acm_bind(struct usb_configuration *c, struct usb_function *f)
577{
578 struct usb_composite_dev *cdev = c->cdev;
579 struct f_acm *acm = func_to_acm(f);
580 int status;
581 struct usb_ep *ep;
582
583 /* allocate instance-specific interface IDs, and patch descriptors */
584 status = usb_interface_id(c, f);
585 if (status < 0)
586 goto fail;
587 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100588 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700589
590 acm_control_interface_desc.bInterfaceNumber = status;
591 acm_union_desc .bMasterInterface0 = status;
592
593 status = usb_interface_id(c, f);
594 if (status < 0)
595 goto fail;
596 acm->data_id = status;
597
598 acm_data_interface_desc.bInterfaceNumber = status;
599 acm_union_desc.bSlaveInterface0 = status;
600 acm_call_mgmt_descriptor.bDataInterface = status;
601
602 status = -ENODEV;
603
604 /* allocate instance-specific endpoints */
605 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
606 if (!ep)
607 goto fail;
608 acm->port.in = ep;
609 ep->driver_data = cdev; /* claim */
610
611 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
612 if (!ep)
613 goto fail;
614 acm->port.out = ep;
615 ep->driver_data = cdev; /* claim */
616
617 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
618 if (!ep)
619 goto fail;
620 acm->notify = ep;
621 ep->driver_data = cdev; /* claim */
622
David Brownell1f1ba112008-08-06 18:49:57 -0700623 /* allocate notification */
624 acm->notify_req = gs_alloc_req(ep,
625 sizeof(struct usb_cdc_notification) + 2,
626 GFP_KERNEL);
627 if (!acm->notify_req)
628 goto fail;
629
630 acm->notify_req->complete = acm_cdc_notify_complete;
631 acm->notify_req->context = acm;
632
David Brownell4d5a73d2008-06-19 18:18:40 -0700633 /* copy descriptors, and track endpoint copies */
634 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700635 if (!f->descriptors)
636 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700637
638 acm->fs.in = usb_find_endpoint(acm_fs_function,
639 f->descriptors, &acm_fs_in_desc);
640 acm->fs.out = usb_find_endpoint(acm_fs_function,
641 f->descriptors, &acm_fs_out_desc);
642 acm->fs.notify = usb_find_endpoint(acm_fs_function,
643 f->descriptors, &acm_fs_notify_desc);
644
645 /* support all relevant hardware speeds... we expect that when
646 * hardware is dual speed, all bulk-capable endpoints work at
647 * both speeds
648 */
649 if (gadget_is_dualspeed(c->cdev->gadget)) {
650 acm_hs_in_desc.bEndpointAddress =
651 acm_fs_in_desc.bEndpointAddress;
652 acm_hs_out_desc.bEndpointAddress =
653 acm_fs_out_desc.bEndpointAddress;
654 acm_hs_notify_desc.bEndpointAddress =
655 acm_fs_notify_desc.bEndpointAddress;
656
657 /* copy descriptors, and track endpoint copies */
658 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
659
660 acm->hs.in = usb_find_endpoint(acm_hs_function,
661 f->hs_descriptors, &acm_hs_in_desc);
662 acm->hs.out = usb_find_endpoint(acm_hs_function,
663 f->hs_descriptors, &acm_hs_out_desc);
664 acm->hs.notify = usb_find_endpoint(acm_hs_function,
665 f->hs_descriptors, &acm_hs_notify_desc);
666 }
667
David Brownell4d5a73d2008-06-19 18:18:40 -0700668 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
669 acm->port_num,
670 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
671 acm->port.in->name, acm->port.out->name,
672 acm->notify->name);
673 return 0;
674
675fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700676 if (acm->notify_req)
677 gs_free_req(acm->notify, acm->notify_req);
678
David Brownell4d5a73d2008-06-19 18:18:40 -0700679 /* we might as well release our claims on endpoints */
680 if (acm->notify)
681 acm->notify->driver_data = NULL;
682 if (acm->port.out)
683 acm->port.out->driver_data = NULL;
684 if (acm->port.in)
685 acm->port.in->driver_data = NULL;
686
687 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
688
689 return status;
690}
691
692static void
693acm_unbind(struct usb_configuration *c, struct usb_function *f)
694{
David Brownell1f1ba112008-08-06 18:49:57 -0700695 struct f_acm *acm = func_to_acm(f);
696
David Brownell4d5a73d2008-06-19 18:18:40 -0700697 if (gadget_is_dualspeed(c->cdev->gadget))
698 usb_free_descriptors(f->hs_descriptors);
699 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700700 gs_free_req(acm->notify, acm->notify_req);
John Michelau677ba872010-11-08 18:05:37 -0600701 kfree(acm->port.func.name);
David Brownell1f1ba112008-08-06 18:49:57 -0700702 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700703}
704
705/* Some controllers can't support CDC ACM ... */
706static inline bool can_support_cdc(struct usb_configuration *c)
707{
David Brownell4d5a73d2008-06-19 18:18:40 -0700708 /* everything else is *probably* fine ... */
709 return true;
710}
711
712/**
713 * acm_bind_config - add a CDC ACM function to a configuration
714 * @c: the configuration to support the CDC ACM instance
715 * @port_num: /dev/ttyGS* port this interface will use
716 * Context: single threaded during gadget setup
717 *
718 * Returns zero on success, else negative errno.
719 *
720 * Caller must have called @gserial_setup() with enough ports to
721 * handle all the ones it binds. Caller is also responsible
722 * for calling @gserial_cleanup() before module unload.
723 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200724int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700725{
726 struct f_acm *acm;
727 int status;
728
729 if (!can_support_cdc(c))
730 return -EINVAL;
731
732 /* REVISIT might want instance-specific strings to help
733 * distinguish instances ...
734 */
735
736 /* maybe allocate device-global string IDs, and patch descriptors */
737 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
738 status = usb_string_id(c->cdev);
739 if (status < 0)
740 return status;
741 acm_string_defs[ACM_CTRL_IDX].id = status;
742
743 acm_control_interface_desc.iInterface = status;
744
745 status = usb_string_id(c->cdev);
746 if (status < 0)
747 return status;
748 acm_string_defs[ACM_DATA_IDX].id = status;
749
750 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100751
752 status = usb_string_id(c->cdev);
753 if (status < 0)
754 return status;
755 acm_string_defs[ACM_IAD_IDX].id = status;
756
757 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700758 }
759
760 /* allocate and initialize one new instance */
761 acm = kzalloc(sizeof *acm, GFP_KERNEL);
762 if (!acm)
763 return -ENOMEM;
764
David Brownell1f1ba112008-08-06 18:49:57 -0700765 spin_lock_init(&acm->lock);
766
David Brownell4d5a73d2008-06-19 18:18:40 -0700767 acm->port_num = port_num;
768
David Brownell1f1ba112008-08-06 18:49:57 -0700769 acm->port.connect = acm_connect;
770 acm->port.disconnect = acm_disconnect;
771 acm->port.send_break = acm_send_break;
772
John Michelau677ba872010-11-08 18:05:37 -0600773 acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num);
774 if (!acm->port.func.name) {
775 kfree(acm);
776 return -ENOMEM;
777 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700778 acm->port.func.strings = acm_strings;
779 /* descriptors are per-instance copies */
780 acm->port.func.bind = acm_bind;
781 acm->port.func.unbind = acm_unbind;
782 acm->port.func.set_alt = acm_set_alt;
783 acm->port.func.setup = acm_setup;
784 acm->port.func.disable = acm_disable;
785
786 status = usb_add_function(c, &acm->port.func);
787 if (status)
788 kfree(acm);
789 return status;
790}
Joe Swantekb21ab232009-07-22 20:16:06 -0500791
Mike Lockwood789ef232010-01-12 10:33:59 -0800792#ifdef CONFIG_USB_ANDROID_ACM
John Michelau677ba872010-11-08 18:05:37 -0600793#include <linux/platform_device.h>
794
795static struct acm_platform_data *acm_pdata;
796
797static int acm_probe(struct platform_device *pdev)
798{
799 acm_pdata = pdev->dev.platform_data;
800 return 0;
801}
802
803static struct platform_driver acm_platform_driver = {
804 .driver = { .name = "acm", },
805 .probe = acm_probe,
806};
Mike Lockwood789ef232010-01-12 10:33:59 -0800807
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530808int acm_function_bind_config(struct usb_configuration *c)
Joe Swantekb21ab232009-07-22 20:16:06 -0500809{
John Michelau677ba872010-11-08 18:05:37 -0600810 int i;
811 u8 num_inst = acm_pdata ? acm_pdata->num_inst : 1;
812 int ret = gserial_setup(c->cdev->gadget, num_inst);
813
814 if (ret)
815 return ret;
816
817 for (i = 0; i < num_inst; i++) {
818 ret = acm_bind_config(c, i);
819 if (ret) {
820 pr_err("Could not bind acm%u config\n", i);
821 break;
822 }
823 }
824
Joe Swantekb21ab232009-07-22 20:16:06 -0500825 return ret;
826}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530827
828static struct android_usb_function acm_function = {
829 .name = "acm",
830 .bind_config = acm_function_bind_config,
831};
832
833static int __init init(void)
834{
Mike Lockwood789ef232010-01-12 10:33:59 -0800835 printk(KERN_INFO "f_acm init\n");
John Michelau677ba872010-11-08 18:05:37 -0600836 platform_driver_register(&acm_platform_driver);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530837 android_register_function(&acm_function);
838 return 0;
839}
840module_init(init);
Mike Lockwood789ef232010-01-12 10:33:59 -0800841
842#endif /* CONFIG_USB_ANDROID_ACM */