blob: d04b4a68220da1d2fd1010773d30e6530ff3545f [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>
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
42struct acm_ep_descs {
43 struct usb_endpoint_descriptor *in;
44 struct usb_endpoint_descriptor *out;
45 struct usb_endpoint_descriptor *notify;
46};
47
48struct f_acm {
49 struct gserial port;
50 u8 ctrl_id, data_id;
51 u8 port_num;
52
David Brownell1f1ba112008-08-06 18:49:57 -070053 u8 pending;
54
55 /* lock is mostly for pending and notify_req ... they get accessed
56 * by callbacks both from tty (open/close/break) under its spinlock,
57 * and notify_req.complete() which can't use that lock.
58 */
59 spinlock_t lock;
60
David Brownell4d5a73d2008-06-19 18:18:40 -070061 struct acm_ep_descs fs;
David Brownell4d5a73d2008-06-19 18:18:40 -070062 struct acm_ep_descs hs;
63
64 struct usb_ep *notify;
David Brownell1f1ba112008-08-06 18:49:57 -070065 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070066
67 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070068
69 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070070 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070071#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
72#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
73
74 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
75 u16 serial_state;
76#define ACM_CTRL_OVERRUN (1 << 6)
77#define ACM_CTRL_PARITY (1 << 5)
78#define ACM_CTRL_FRAMING (1 << 4)
79#define ACM_CTRL_RI (1 << 3)
80#define ACM_CTRL_BRK (1 << 2)
81#define ACM_CTRL_DSR (1 << 1)
82#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070083};
84
85static inline struct f_acm *func_to_acm(struct usb_function *f)
86{
87 return container_of(f, struct f_acm, port.func);
88}
89
David Brownell1f1ba112008-08-06 18:49:57 -070090static inline struct f_acm *port_to_acm(struct gserial *p)
91{
92 return container_of(p, struct f_acm, port);
93}
94
David Brownell4d5a73d2008-06-19 18:18:40 -070095/*-------------------------------------------------------------------------*/
96
97/* notification endpoint uses smallish and infrequent fixed-size messages */
98
99#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
David Brownell1f1ba112008-08-06 18:49:57 -0700100#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -0700101
102/* interface and class descriptors: */
103
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100104static struct usb_interface_assoc_descriptor
105acm_iad_descriptor = {
106 .bLength = sizeof acm_iad_descriptor,
107 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
108
109 /* .bFirstInterface = DYNAMIC, */
110 .bInterfaceCount = 2, // control + data
111 .bFunctionClass = USB_CLASS_COMM,
112 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530113 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100114 /* .iFunction = DYNAMIC */
115};
116
117
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200118static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700119 .bLength = USB_DT_INTERFACE_SIZE,
120 .bDescriptorType = USB_DT_INTERFACE,
121 /* .bInterfaceNumber = DYNAMIC */
122 .bNumEndpoints = 1,
123 .bInterfaceClass = USB_CLASS_COMM,
124 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
125 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
126 /* .iInterface = DYNAMIC */
127};
128
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200129static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700130 .bLength = USB_DT_INTERFACE_SIZE,
131 .bDescriptorType = USB_DT_INTERFACE,
132 /* .bInterfaceNumber = DYNAMIC */
133 .bNumEndpoints = 2,
134 .bInterfaceClass = USB_CLASS_CDC_DATA,
135 .bInterfaceSubClass = 0,
136 .bInterfaceProtocol = 0,
137 /* .iInterface = DYNAMIC */
138};
139
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200140static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700141 .bLength = sizeof(acm_header_desc),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800144 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700145};
146
147static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200148acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700149 .bLength = sizeof(acm_call_mgmt_descriptor),
150 .bDescriptorType = USB_DT_CS_INTERFACE,
151 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
152 .bmCapabilities = 0,
153 /* .bDataInterface = DYNAMIC */
154};
155
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700157 .bLength = sizeof(acm_descriptor),
158 .bDescriptorType = USB_DT_CS_INTERFACE,
159 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700160 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700161};
162
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200163static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700164 .bLength = sizeof(acm_union_desc),
165 .bDescriptorType = USB_DT_CS_INTERFACE,
166 .bDescriptorSubType = USB_CDC_UNION_TYPE,
167 /* .bMasterInterface0 = DYNAMIC */
168 /* .bSlaveInterface0 = DYNAMIC */
169};
170
171/* full speed support: */
172
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200173static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700174 .bLength = USB_DT_ENDPOINT_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_IN,
177 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800178 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700179 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
180};
181
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200182static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_IN,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187};
188
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200189static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bEndpointAddress = USB_DIR_OUT,
193 .bmAttributes = USB_ENDPOINT_XFER_BULK,
194};
195
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200196static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100197 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700198 (struct usb_descriptor_header *) &acm_control_interface_desc,
199 (struct usb_descriptor_header *) &acm_header_desc,
200 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
201 (struct usb_descriptor_header *) &acm_descriptor,
202 (struct usb_descriptor_header *) &acm_union_desc,
203 (struct usb_descriptor_header *) &acm_fs_notify_desc,
204 (struct usb_descriptor_header *) &acm_data_interface_desc,
205 (struct usb_descriptor_header *) &acm_fs_in_desc,
206 (struct usb_descriptor_header *) &acm_fs_out_desc,
207 NULL,
208};
209
210/* high speed support: */
211
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200212static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700213 .bLength = USB_DT_ENDPOINT_SIZE,
214 .bDescriptorType = USB_DT_ENDPOINT,
215 .bEndpointAddress = USB_DIR_IN,
216 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800217 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700218 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
219};
220
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200221static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700222 .bLength = USB_DT_ENDPOINT_SIZE,
223 .bDescriptorType = USB_DT_ENDPOINT,
224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800225 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700226};
227
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200228static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700229 .bLength = USB_DT_ENDPOINT_SIZE,
230 .bDescriptorType = USB_DT_ENDPOINT,
231 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800232 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700233};
234
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200235static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100236 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700237 (struct usb_descriptor_header *) &acm_control_interface_desc,
238 (struct usb_descriptor_header *) &acm_header_desc,
239 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
240 (struct usb_descriptor_header *) &acm_descriptor,
241 (struct usb_descriptor_header *) &acm_union_desc,
242 (struct usb_descriptor_header *) &acm_hs_notify_desc,
243 (struct usb_descriptor_header *) &acm_data_interface_desc,
244 (struct usb_descriptor_header *) &acm_hs_in_desc,
245 (struct usb_descriptor_header *) &acm_hs_out_desc,
246 NULL,
247};
248
249/* string descriptors: */
250
251#define ACM_CTRL_IDX 0
252#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100253#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700254
255/* static strings, in UTF-8 */
256static struct usb_string acm_string_defs[] = {
257 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
258 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100259 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700260 { /* ZEROES END LIST */ },
261};
262
263static struct usb_gadget_strings acm_string_table = {
264 .language = 0x0409, /* en-us */
265 .strings = acm_string_defs,
266};
267
268static struct usb_gadget_strings *acm_strings[] = {
269 &acm_string_table,
270 NULL,
271};
272
273/*-------------------------------------------------------------------------*/
274
275/* ACM control ... data handling is delegated to tty library code.
276 * The main task of this function is to activate and deactivate
277 * that code based on device state; track parameters like line
278 * speed, handshake state, and so on; and issue notifications.
279 */
280
281static void acm_complete_set_line_coding(struct usb_ep *ep,
282 struct usb_request *req)
283{
284 struct f_acm *acm = ep->driver_data;
285 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
286
287 if (req->status != 0) {
288 DBG(cdev, "acm ttyGS%d completion, err %d\n",
289 acm->port_num, req->status);
290 return;
291 }
292
293 /* normal completion */
294 if (req->actual != sizeof(acm->port_line_coding)) {
295 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
296 acm->port_num, req->actual);
297 usb_ep_set_halt(ep);
298 } else {
299 struct usb_cdc_line_coding *value = req->buf;
300
301 /* REVISIT: we currently just remember this data.
302 * If we change that, (a) validate it first, then
303 * (b) update whatever hardware needs updating,
304 * (c) worry about locking. This is information on
305 * the order of 9600-8-N-1 ... most of which means
306 * nothing unless we control a real RS232 line.
307 */
308 acm->port_line_coding = *value;
309 }
310}
311
312static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
313{
314 struct f_acm *acm = func_to_acm(f);
315 struct usb_composite_dev *cdev = f->config->cdev;
316 struct usb_request *req = cdev->req;
317 int value = -EOPNOTSUPP;
318 u16 w_index = le16_to_cpu(ctrl->wIndex);
319 u16 w_value = le16_to_cpu(ctrl->wValue);
320 u16 w_length = le16_to_cpu(ctrl->wLength);
321
322 /* composite driver infrastructure handles everything except
323 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700324 *
325 * Note CDC spec table 4 lists the ACM request profile. It requires
326 * encapsulated command support ... we don't handle any, and respond
327 * to them by stalling. Options include get/set/clear comm features
328 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700329 */
330 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
331
332 /* SET_LINE_CODING ... just read and save what the host sends */
333 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
334 | USB_CDC_REQ_SET_LINE_CODING:
335 if (w_length != sizeof(struct usb_cdc_line_coding)
336 || w_index != acm->ctrl_id)
337 goto invalid;
338
339 value = w_length;
340 cdev->gadget->ep0->driver_data = acm;
341 req->complete = acm_complete_set_line_coding;
342 break;
343
344 /* GET_LINE_CODING ... return what host sent, or initial value */
345 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
346 | USB_CDC_REQ_GET_LINE_CODING:
347 if (w_index != acm->ctrl_id)
348 goto invalid;
349
350 value = min_t(unsigned, w_length,
351 sizeof(struct usb_cdc_line_coding));
352 memcpy(req->buf, &acm->port_line_coding, value);
353 break;
354
355 /* SET_CONTROL_LINE_STATE ... save what the host sent */
356 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
357 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
358 if (w_index != acm->ctrl_id)
359 goto invalid;
360
361 value = 0;
362
363 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700364 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700365 * that bit, we should return to that no-flow state.
366 */
367 acm->port_handshake_bits = w_value;
368 break;
369
370 default:
371invalid:
372 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
373 ctrl->bRequestType, ctrl->bRequest,
374 w_value, w_index, w_length);
375 }
376
377 /* respond with data transfer or status phase? */
378 if (value >= 0) {
379 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
380 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
381 w_value, w_index, w_length);
382 req->zero = 0;
383 req->length = value;
384 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
385 if (value < 0)
386 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
387 acm->port_num, value);
388 }
389
390 /* device either stalls (value < 0) or reports success */
391 return value;
392}
393
394static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
395{
396 struct f_acm *acm = func_to_acm(f);
397 struct usb_composite_dev *cdev = f->config->cdev;
398
399 /* we know alt == 0, so this is an activation or a reset */
400
401 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700402 if (acm->notify->driver_data) {
403 VDBG(cdev, "reset acm control interface %d\n", intf);
404 usb_ep_disable(acm->notify);
405 } else {
406 VDBG(cdev, "init acm ctrl interface %d\n", intf);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300407 acm->notify->desc = ep_choose(cdev->gadget,
David Brownell4d5a73d2008-06-19 18:18:40 -0700408 acm->hs.notify,
409 acm->fs.notify);
410 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300411 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700412 acm->notify->driver_data = acm;
413
414 } else if (intf == acm->data_id) {
415 if (acm->port.in->driver_data) {
416 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
417 gserial_disconnect(&acm->port);
418 } else {
419 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300420 acm->port.in->desc = ep_choose(cdev->gadget,
David Brownell4d5a73d2008-06-19 18:18:40 -0700421 acm->hs.in, acm->fs.in);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300422 acm->port.out->desc = ep_choose(cdev->gadget,
David Brownell4d5a73d2008-06-19 18:18:40 -0700423 acm->hs.out, acm->fs.out);
424 }
425 gserial_connect(&acm->port, acm->port_num);
426
427 } else
428 return -EINVAL;
429
430 return 0;
431}
432
433static void acm_disable(struct usb_function *f)
434{
435 struct f_acm *acm = func_to_acm(f);
436 struct usb_composite_dev *cdev = f->config->cdev;
437
438 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
439 gserial_disconnect(&acm->port);
440 usb_ep_disable(acm->notify);
441 acm->notify->driver_data = NULL;
442}
443
444/*-------------------------------------------------------------------------*/
445
David Brownell1f1ba112008-08-06 18:49:57 -0700446/**
447 * acm_cdc_notify - issue CDC notification to host
448 * @acm: wraps host to be notified
449 * @type: notification type
450 * @value: Refer to cdc specs, wValue field.
451 * @data: data to be sent
452 * @length: size of data
453 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
454 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200455 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700456 *
457 * See section 6.3.5 of the CDC 1.1 specification for information
458 * about the only notification we issue: SerialState change.
459 */
460static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
461 void *data, unsigned length)
462{
463 struct usb_ep *ep = acm->notify;
464 struct usb_request *req;
465 struct usb_cdc_notification *notify;
466 const unsigned len = sizeof(*notify) + length;
467 void *buf;
468 int status;
469
470 req = acm->notify_req;
471 acm->notify_req = NULL;
472 acm->pending = false;
473
474 req->length = len;
475 notify = req->buf;
476 buf = notify + 1;
477
478 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
479 | USB_RECIP_INTERFACE;
480 notify->bNotificationType = type;
481 notify->wValue = cpu_to_le16(value);
482 notify->wIndex = cpu_to_le16(acm->ctrl_id);
483 notify->wLength = cpu_to_le16(length);
484 memcpy(buf, data, length);
485
David Brownelle50ae572008-11-12 11:35:13 -0800486 /* ep_queue() can complete immediately if it fills the fifo... */
487 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700488 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800489 spin_lock(&acm->lock);
490
David Brownell1f1ba112008-08-06 18:49:57 -0700491 if (status < 0) {
492 ERROR(acm->port.func.config->cdev,
493 "acm ttyGS%d can't notify serial state, %d\n",
494 acm->port_num, status);
495 acm->notify_req = req;
496 }
497
498 return status;
499}
500
501static int acm_notify_serial_state(struct f_acm *acm)
502{
503 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
504 int status;
505
506 spin_lock(&acm->lock);
507 if (acm->notify_req) {
508 DBG(cdev, "acm ttyGS%d serial state %04x\n",
509 acm->port_num, acm->serial_state);
510 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
511 0, &acm->serial_state, sizeof(acm->serial_state));
512 } else {
513 acm->pending = true;
514 status = 0;
515 }
516 spin_unlock(&acm->lock);
517 return status;
518}
519
520static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
521{
522 struct f_acm *acm = req->context;
523 u8 doit = false;
524
525 /* on this call path we do NOT hold the port spinlock,
526 * which is why ACM needs its own spinlock
527 */
528 spin_lock(&acm->lock);
529 if (req->status != -ESHUTDOWN)
530 doit = acm->pending;
531 acm->notify_req = req;
532 spin_unlock(&acm->lock);
533
534 if (doit)
535 acm_notify_serial_state(acm);
536}
537
538/* connect == the TTY link is open */
539
540static void acm_connect(struct gserial *port)
541{
542 struct f_acm *acm = port_to_acm(port);
543
544 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
545 acm_notify_serial_state(acm);
546}
547
548static void acm_disconnect(struct gserial *port)
549{
550 struct f_acm *acm = port_to_acm(port);
551
552 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
553 acm_notify_serial_state(acm);
554}
555
556static int acm_send_break(struct gserial *port, int duration)
557{
558 struct f_acm *acm = port_to_acm(port);
559 u16 state;
560
561 state = acm->serial_state;
562 state &= ~ACM_CTRL_BRK;
563 if (duration)
564 state |= ACM_CTRL_BRK;
565
566 acm->serial_state = state;
567 return acm_notify_serial_state(acm);
568}
569
570/*-------------------------------------------------------------------------*/
571
David Brownell4d5a73d2008-06-19 18:18:40 -0700572/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200573static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700574acm_bind(struct usb_configuration *c, struct usb_function *f)
575{
576 struct usb_composite_dev *cdev = c->cdev;
577 struct f_acm *acm = func_to_acm(f);
578 int status;
579 struct usb_ep *ep;
580
581 /* allocate instance-specific interface IDs, and patch descriptors */
582 status = usb_interface_id(c, f);
583 if (status < 0)
584 goto fail;
585 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100586 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700587
588 acm_control_interface_desc.bInterfaceNumber = status;
589 acm_union_desc .bMasterInterface0 = status;
590
591 status = usb_interface_id(c, f);
592 if (status < 0)
593 goto fail;
594 acm->data_id = status;
595
596 acm_data_interface_desc.bInterfaceNumber = status;
597 acm_union_desc.bSlaveInterface0 = status;
598 acm_call_mgmt_descriptor.bDataInterface = status;
599
600 status = -ENODEV;
601
602 /* allocate instance-specific endpoints */
603 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
604 if (!ep)
605 goto fail;
606 acm->port.in = ep;
607 ep->driver_data = cdev; /* claim */
608
609 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
610 if (!ep)
611 goto fail;
612 acm->port.out = ep;
613 ep->driver_data = cdev; /* claim */
614
615 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
616 if (!ep)
617 goto fail;
618 acm->notify = ep;
619 ep->driver_data = cdev; /* claim */
620
David Brownell1f1ba112008-08-06 18:49:57 -0700621 /* allocate notification */
622 acm->notify_req = gs_alloc_req(ep,
623 sizeof(struct usb_cdc_notification) + 2,
624 GFP_KERNEL);
625 if (!acm->notify_req)
626 goto fail;
627
628 acm->notify_req->complete = acm_cdc_notify_complete;
629 acm->notify_req->context = acm;
630
David Brownell4d5a73d2008-06-19 18:18:40 -0700631 /* copy descriptors, and track endpoint copies */
632 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700633 if (!f->descriptors)
634 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700635
636 acm->fs.in = usb_find_endpoint(acm_fs_function,
637 f->descriptors, &acm_fs_in_desc);
638 acm->fs.out = usb_find_endpoint(acm_fs_function,
639 f->descriptors, &acm_fs_out_desc);
640 acm->fs.notify = usb_find_endpoint(acm_fs_function,
641 f->descriptors, &acm_fs_notify_desc);
642
643 /* support all relevant hardware speeds... we expect that when
644 * hardware is dual speed, all bulk-capable endpoints work at
645 * both speeds
646 */
647 if (gadget_is_dualspeed(c->cdev->gadget)) {
648 acm_hs_in_desc.bEndpointAddress =
649 acm_fs_in_desc.bEndpointAddress;
650 acm_hs_out_desc.bEndpointAddress =
651 acm_fs_out_desc.bEndpointAddress;
652 acm_hs_notify_desc.bEndpointAddress =
653 acm_fs_notify_desc.bEndpointAddress;
654
655 /* copy descriptors, and track endpoint copies */
656 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
657
658 acm->hs.in = usb_find_endpoint(acm_hs_function,
659 f->hs_descriptors, &acm_hs_in_desc);
660 acm->hs.out = usb_find_endpoint(acm_hs_function,
661 f->hs_descriptors, &acm_hs_out_desc);
662 acm->hs.notify = usb_find_endpoint(acm_hs_function,
663 f->hs_descriptors, &acm_hs_notify_desc);
664 }
665
David Brownell4d5a73d2008-06-19 18:18:40 -0700666 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
667 acm->port_num,
668 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
669 acm->port.in->name, acm->port.out->name,
670 acm->notify->name);
671 return 0;
672
673fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700674 if (acm->notify_req)
675 gs_free_req(acm->notify, acm->notify_req);
676
David Brownell4d5a73d2008-06-19 18:18:40 -0700677 /* we might as well release our claims on endpoints */
678 if (acm->notify)
679 acm->notify->driver_data = NULL;
680 if (acm->port.out)
681 acm->port.out->driver_data = NULL;
682 if (acm->port.in)
683 acm->port.in->driver_data = NULL;
684
685 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
686
687 return status;
688}
689
690static void
691acm_unbind(struct usb_configuration *c, struct usb_function *f)
692{
David Brownell1f1ba112008-08-06 18:49:57 -0700693 struct f_acm *acm = func_to_acm(f);
694
David Brownell4d5a73d2008-06-19 18:18:40 -0700695 if (gadget_is_dualspeed(c->cdev->gadget))
696 usb_free_descriptors(f->hs_descriptors);
697 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700698 gs_free_req(acm->notify, acm->notify_req);
699 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700700}
701
702/* Some controllers can't support CDC ACM ... */
703static inline bool can_support_cdc(struct usb_configuration *c)
704{
David Brownell4d5a73d2008-06-19 18:18:40 -0700705 /* everything else is *probably* fine ... */
706 return true;
707}
708
709/**
710 * acm_bind_config - add a CDC ACM function to a configuration
711 * @c: the configuration to support the CDC ACM instance
712 * @port_num: /dev/ttyGS* port this interface will use
713 * Context: single threaded during gadget setup
714 *
715 * Returns zero on success, else negative errno.
716 *
717 * Caller must have called @gserial_setup() with enough ports to
718 * handle all the ones it binds. Caller is also responsible
719 * for calling @gserial_cleanup() before module unload.
720 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200721int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700722{
723 struct f_acm *acm;
724 int status;
725
726 if (!can_support_cdc(c))
727 return -EINVAL;
728
729 /* REVISIT might want instance-specific strings to help
730 * distinguish instances ...
731 */
732
733 /* maybe allocate device-global string IDs, and patch descriptors */
734 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
735 status = usb_string_id(c->cdev);
736 if (status < 0)
737 return status;
738 acm_string_defs[ACM_CTRL_IDX].id = status;
739
740 acm_control_interface_desc.iInterface = status;
741
742 status = usb_string_id(c->cdev);
743 if (status < 0)
744 return status;
745 acm_string_defs[ACM_DATA_IDX].id = status;
746
747 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100748
749 status = usb_string_id(c->cdev);
750 if (status < 0)
751 return status;
752 acm_string_defs[ACM_IAD_IDX].id = status;
753
754 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700755 }
756
757 /* allocate and initialize one new instance */
758 acm = kzalloc(sizeof *acm, GFP_KERNEL);
759 if (!acm)
760 return -ENOMEM;
761
David Brownell1f1ba112008-08-06 18:49:57 -0700762 spin_lock_init(&acm->lock);
763
David Brownell4d5a73d2008-06-19 18:18:40 -0700764 acm->port_num = port_num;
765
David Brownell1f1ba112008-08-06 18:49:57 -0700766 acm->port.connect = acm_connect;
767 acm->port.disconnect = acm_disconnect;
768 acm->port.send_break = acm_send_break;
769
David Brownell4d5a73d2008-06-19 18:18:40 -0700770 acm->port.func.name = "acm";
771 acm->port.func.strings = acm_strings;
772 /* descriptors are per-instance copies */
773 acm->port.func.bind = acm_bind;
774 acm->port.func.unbind = acm_unbind;
775 acm->port.func.set_alt = acm_set_alt;
776 acm->port.func.setup = acm_setup;
777 acm->port.func.disable = acm_disable;
778
779 status = usb_add_function(c, &acm->port.func);
780 if (status)
781 kfree(acm);
782 return status;
783}