blob: 3ea7dc89b43de1e624c24840a1eb16ccd07550fe [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
Michal Nazarewicz54b83602012-01-13 15:05:16 +01008 * Author: Michal Nazarewicz (mina86@mina86.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>
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +010019#include <linux/module.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070020#include <linux/device.h>
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +010021#include <linux/err.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070022
23#include "u_serial.h"
24#include "gadget_chips.h"
25
26
27/*
28 * This CDC ACM function support just wraps control functions and
29 * notifications around the generic serial-over-usb code.
30 *
31 * Because CDC ACM is standardized by the USB-IF, many host operating
32 * systems have drivers for it. Accordingly, ACM is the preferred
33 * interop solution for serial-port type connections. The control
34 * models are often not necessary, and in any case don't do much in
35 * this bare-bones implementation.
36 *
37 * Note that even MS-Windows has some support for ACM. However, that
38 * support is somewhat broken because when you use ACM in a composite
39 * device, having multiple interfaces confuses the poor OS. It doesn't
40 * seem to understand CDC Union descriptors. The new "association"
41 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
42 */
43
David Brownell4d5a73d2008-06-19 18:18:40 -070044struct f_acm {
45 struct gserial port;
46 u8 ctrl_id, data_id;
47 u8 port_num;
48
David Brownell1f1ba112008-08-06 18:49:57 -070049 u8 pending;
50
51 /* lock is mostly for pending and notify_req ... they get accessed
52 * by callbacks both from tty (open/close/break) under its spinlock,
53 * and notify_req.complete() which can't use that lock.
54 */
55 spinlock_t lock;
56
David Brownell4d5a73d2008-06-19 18:18:40 -070057 struct usb_ep *notify;
David Brownell1f1ba112008-08-06 18:49:57 -070058 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070059
60 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070061
62 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070063 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070064#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
65#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
66
67 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
68 u16 serial_state;
69#define ACM_CTRL_OVERRUN (1 << 6)
70#define ACM_CTRL_PARITY (1 << 5)
71#define ACM_CTRL_FRAMING (1 << 4)
72#define ACM_CTRL_RI (1 << 3)
73#define ACM_CTRL_BRK (1 << 2)
74#define ACM_CTRL_DSR (1 << 1)
75#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070076};
77
78static inline struct f_acm *func_to_acm(struct usb_function *f)
79{
80 return container_of(f, struct f_acm, port.func);
81}
82
David Brownell1f1ba112008-08-06 18:49:57 -070083static inline struct f_acm *port_to_acm(struct gserial *p)
84{
85 return container_of(p, struct f_acm, port);
86}
87
David Brownell4d5a73d2008-06-19 18:18:40 -070088/*-------------------------------------------------------------------------*/
89
90/* notification endpoint uses smallish and infrequent fixed-size messages */
91
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +020092#define GS_NOTIFY_INTERVAL_MS 32
David Brownell1f1ba112008-08-06 18:49:57 -070093#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -070094
95/* interface and class descriptors: */
96
Michal Nazarewiczb97503f2009-10-28 16:57:30 +010097static struct usb_interface_assoc_descriptor
98acm_iad_descriptor = {
99 .bLength = sizeof acm_iad_descriptor,
100 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
101
102 /* .bFirstInterface = DYNAMIC, */
103 .bInterfaceCount = 2, // control + data
104 .bFunctionClass = USB_CLASS_COMM,
105 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530106 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100107 /* .iFunction = DYNAMIC */
108};
109
110
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200111static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700112 .bLength = USB_DT_INTERFACE_SIZE,
113 .bDescriptorType = USB_DT_INTERFACE,
114 /* .bInterfaceNumber = DYNAMIC */
115 .bNumEndpoints = 1,
116 .bInterfaceClass = USB_CLASS_COMM,
117 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
118 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
119 /* .iInterface = DYNAMIC */
120};
121
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200122static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700123 .bLength = USB_DT_INTERFACE_SIZE,
124 .bDescriptorType = USB_DT_INTERFACE,
125 /* .bInterfaceNumber = DYNAMIC */
126 .bNumEndpoints = 2,
127 .bInterfaceClass = USB_CLASS_CDC_DATA,
128 .bInterfaceSubClass = 0,
129 .bInterfaceProtocol = 0,
130 /* .iInterface = DYNAMIC */
131};
132
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200133static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700134 .bLength = sizeof(acm_header_desc),
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800137 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700138};
139
140static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200141acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700142 .bLength = sizeof(acm_call_mgmt_descriptor),
143 .bDescriptorType = USB_DT_CS_INTERFACE,
144 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
145 .bmCapabilities = 0,
146 /* .bDataInterface = DYNAMIC */
147};
148
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200149static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700150 .bLength = sizeof(acm_descriptor),
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700153 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700154};
155
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700157 .bLength = sizeof(acm_union_desc),
158 .bDescriptorType = USB_DT_CS_INTERFACE,
159 .bDescriptorSubType = USB_CDC_UNION_TYPE,
160 /* .bMasterInterface0 = DYNAMIC */
161 /* .bSlaveInterface0 = DYNAMIC */
162};
163
164/* full speed support: */
165
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200166static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800171 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200172 .bInterval = GS_NOTIFY_INTERVAL_MS,
David Brownell4d5a73d2008-06-19 18:18:40 -0700173};
174
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200175static struct usb_endpoint_descriptor acm_fs_in_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_BULK,
180};
181
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200182static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_OUT,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187};
188
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200189static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100190 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700191 (struct usb_descriptor_header *) &acm_control_interface_desc,
192 (struct usb_descriptor_header *) &acm_header_desc,
193 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
194 (struct usb_descriptor_header *) &acm_descriptor,
195 (struct usb_descriptor_header *) &acm_union_desc,
196 (struct usb_descriptor_header *) &acm_fs_notify_desc,
197 (struct usb_descriptor_header *) &acm_data_interface_desc,
198 (struct usb_descriptor_header *) &acm_fs_in_desc,
199 (struct usb_descriptor_header *) &acm_fs_out_desc,
200 NULL,
201};
202
203/* high speed support: */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200204static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700205 .bLength = USB_DT_ENDPOINT_SIZE,
206 .bDescriptorType = USB_DT_ENDPOINT,
207 .bEndpointAddress = USB_DIR_IN,
208 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800209 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200210 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
David Brownell4d5a73d2008-06-19 18:18:40 -0700211};
212
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200213static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800217 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700218};
219
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200220static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
223 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800224 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700225};
226
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200227static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100228 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700229 (struct usb_descriptor_header *) &acm_control_interface_desc,
230 (struct usb_descriptor_header *) &acm_header_desc,
231 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
232 (struct usb_descriptor_header *) &acm_descriptor,
233 (struct usb_descriptor_header *) &acm_union_desc,
234 (struct usb_descriptor_header *) &acm_hs_notify_desc,
235 (struct usb_descriptor_header *) &acm_data_interface_desc,
236 (struct usb_descriptor_header *) &acm_hs_in_desc,
237 (struct usb_descriptor_header *) &acm_hs_out_desc,
238 NULL,
239};
240
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100241static struct usb_endpoint_descriptor acm_ss_in_desc = {
242 .bLength = USB_DT_ENDPOINT_SIZE,
243 .bDescriptorType = USB_DT_ENDPOINT,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = cpu_to_le16(1024),
246};
247
248static struct usb_endpoint_descriptor acm_ss_out_desc = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
252 .wMaxPacketSize = cpu_to_le16(1024),
253};
254
255static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
256 .bLength = sizeof acm_ss_bulk_comp_desc,
257 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
258};
259
260static struct usb_descriptor_header *acm_ss_function[] = {
261 (struct usb_descriptor_header *) &acm_iad_descriptor,
262 (struct usb_descriptor_header *) &acm_control_interface_desc,
263 (struct usb_descriptor_header *) &acm_header_desc,
264 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
265 (struct usb_descriptor_header *) &acm_descriptor,
266 (struct usb_descriptor_header *) &acm_union_desc,
267 (struct usb_descriptor_header *) &acm_hs_notify_desc,
268 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
269 (struct usb_descriptor_header *) &acm_data_interface_desc,
270 (struct usb_descriptor_header *) &acm_ss_in_desc,
271 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
272 (struct usb_descriptor_header *) &acm_ss_out_desc,
273 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
274 NULL,
275};
276
David Brownell4d5a73d2008-06-19 18:18:40 -0700277/* string descriptors: */
278
279#define ACM_CTRL_IDX 0
280#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100281#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700282
283/* static strings, in UTF-8 */
284static struct usb_string acm_string_defs[] = {
285 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
286 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100287 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700288 { /* ZEROES END LIST */ },
289};
290
291static struct usb_gadget_strings acm_string_table = {
292 .language = 0x0409, /* en-us */
293 .strings = acm_string_defs,
294};
295
296static struct usb_gadget_strings *acm_strings[] = {
297 &acm_string_table,
298 NULL,
299};
300
301/*-------------------------------------------------------------------------*/
302
303/* ACM control ... data handling is delegated to tty library code.
304 * The main task of this function is to activate and deactivate
305 * that code based on device state; track parameters like line
306 * speed, handshake state, and so on; and issue notifications.
307 */
308
309static void acm_complete_set_line_coding(struct usb_ep *ep,
310 struct usb_request *req)
311{
312 struct f_acm *acm = ep->driver_data;
313 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
314
315 if (req->status != 0) {
316 DBG(cdev, "acm ttyGS%d completion, err %d\n",
317 acm->port_num, req->status);
318 return;
319 }
320
321 /* normal completion */
322 if (req->actual != sizeof(acm->port_line_coding)) {
323 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
324 acm->port_num, req->actual);
325 usb_ep_set_halt(ep);
326 } else {
327 struct usb_cdc_line_coding *value = req->buf;
328
329 /* REVISIT: we currently just remember this data.
330 * If we change that, (a) validate it first, then
331 * (b) update whatever hardware needs updating,
332 * (c) worry about locking. This is information on
333 * the order of 9600-8-N-1 ... most of which means
334 * nothing unless we control a real RS232 line.
335 */
336 acm->port_line_coding = *value;
337 }
338}
339
340static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
341{
342 struct f_acm *acm = func_to_acm(f);
343 struct usb_composite_dev *cdev = f->config->cdev;
344 struct usb_request *req = cdev->req;
345 int value = -EOPNOTSUPP;
346 u16 w_index = le16_to_cpu(ctrl->wIndex);
347 u16 w_value = le16_to_cpu(ctrl->wValue);
348 u16 w_length = le16_to_cpu(ctrl->wLength);
349
350 /* composite driver infrastructure handles everything except
351 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700352 *
353 * Note CDC spec table 4 lists the ACM request profile. It requires
354 * encapsulated command support ... we don't handle any, and respond
355 * to them by stalling. Options include get/set/clear comm features
356 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700357 */
358 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
359
360 /* SET_LINE_CODING ... just read and save what the host sends */
361 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
362 | USB_CDC_REQ_SET_LINE_CODING:
363 if (w_length != sizeof(struct usb_cdc_line_coding)
364 || w_index != acm->ctrl_id)
365 goto invalid;
366
367 value = w_length;
368 cdev->gadget->ep0->driver_data = acm;
369 req->complete = acm_complete_set_line_coding;
370 break;
371
372 /* GET_LINE_CODING ... return what host sent, or initial value */
373 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
374 | USB_CDC_REQ_GET_LINE_CODING:
375 if (w_index != acm->ctrl_id)
376 goto invalid;
377
378 value = min_t(unsigned, w_length,
379 sizeof(struct usb_cdc_line_coding));
380 memcpy(req->buf, &acm->port_line_coding, value);
381 break;
382
383 /* SET_CONTROL_LINE_STATE ... save what the host sent */
384 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
385 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
386 if (w_index != acm->ctrl_id)
387 goto invalid;
388
389 value = 0;
390
391 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700392 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700393 * that bit, we should return to that no-flow state.
394 */
395 acm->port_handshake_bits = w_value;
396 break;
397
398 default:
399invalid:
400 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
401 ctrl->bRequestType, ctrl->bRequest,
402 w_value, w_index, w_length);
403 }
404
405 /* respond with data transfer or status phase? */
406 if (value >= 0) {
407 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
408 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
409 w_value, w_index, w_length);
410 req->zero = 0;
411 req->length = value;
412 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
413 if (value < 0)
414 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
415 acm->port_num, value);
416 }
417
418 /* device either stalls (value < 0) or reports success */
419 return value;
420}
421
422static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
423{
424 struct f_acm *acm = func_to_acm(f);
425 struct usb_composite_dev *cdev = f->config->cdev;
426
427 /* we know alt == 0, so this is an activation or a reset */
428
429 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700430 if (acm->notify->driver_data) {
431 VDBG(cdev, "reset acm control interface %d\n", intf);
432 usb_ep_disable(acm->notify);
433 } else {
434 VDBG(cdev, "init acm ctrl interface %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300435 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
436 return -EINVAL;
David Brownell4d5a73d2008-06-19 18:18:40 -0700437 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300438 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700439 acm->notify->driver_data = acm;
440
441 } else if (intf == acm->data_id) {
442 if (acm->port.in->driver_data) {
443 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
444 gserial_disconnect(&acm->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300445 }
446 if (!acm->port.in->desc || !acm->port.out->desc) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700447 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300448 if (config_ep_by_speed(cdev->gadget, f,
449 acm->port.in) ||
450 config_ep_by_speed(cdev->gadget, f,
451 acm->port.out)) {
452 acm->port.in->desc = NULL;
453 acm->port.out->desc = NULL;
454 return -EINVAL;
455 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700456 }
457 gserial_connect(&acm->port, acm->port_num);
458
459 } else
460 return -EINVAL;
461
462 return 0;
463}
464
465static void acm_disable(struct usb_function *f)
466{
467 struct f_acm *acm = func_to_acm(f);
468 struct usb_composite_dev *cdev = f->config->cdev;
469
470 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
471 gserial_disconnect(&acm->port);
472 usb_ep_disable(acm->notify);
473 acm->notify->driver_data = NULL;
474}
475
476/*-------------------------------------------------------------------------*/
477
David Brownell1f1ba112008-08-06 18:49:57 -0700478/**
479 * acm_cdc_notify - issue CDC notification to host
480 * @acm: wraps host to be notified
481 * @type: notification type
482 * @value: Refer to cdc specs, wValue field.
483 * @data: data to be sent
484 * @length: size of data
485 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
486 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200487 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700488 *
489 * See section 6.3.5 of the CDC 1.1 specification for information
490 * about the only notification we issue: SerialState change.
491 */
492static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
493 void *data, unsigned length)
494{
495 struct usb_ep *ep = acm->notify;
496 struct usb_request *req;
497 struct usb_cdc_notification *notify;
498 const unsigned len = sizeof(*notify) + length;
499 void *buf;
500 int status;
501
502 req = acm->notify_req;
503 acm->notify_req = NULL;
504 acm->pending = false;
505
506 req->length = len;
507 notify = req->buf;
508 buf = notify + 1;
509
510 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
511 | USB_RECIP_INTERFACE;
512 notify->bNotificationType = type;
513 notify->wValue = cpu_to_le16(value);
514 notify->wIndex = cpu_to_le16(acm->ctrl_id);
515 notify->wLength = cpu_to_le16(length);
516 memcpy(buf, data, length);
517
David Brownelle50ae572008-11-12 11:35:13 -0800518 /* ep_queue() can complete immediately if it fills the fifo... */
519 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700520 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800521 spin_lock(&acm->lock);
522
David Brownell1f1ba112008-08-06 18:49:57 -0700523 if (status < 0) {
524 ERROR(acm->port.func.config->cdev,
525 "acm ttyGS%d can't notify serial state, %d\n",
526 acm->port_num, status);
527 acm->notify_req = req;
528 }
529
530 return status;
531}
532
533static int acm_notify_serial_state(struct f_acm *acm)
534{
535 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
536 int status;
537
538 spin_lock(&acm->lock);
539 if (acm->notify_req) {
540 DBG(cdev, "acm ttyGS%d serial state %04x\n",
541 acm->port_num, acm->serial_state);
542 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
543 0, &acm->serial_state, sizeof(acm->serial_state));
544 } else {
545 acm->pending = true;
546 status = 0;
547 }
548 spin_unlock(&acm->lock);
549 return status;
550}
551
552static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
553{
554 struct f_acm *acm = req->context;
555 u8 doit = false;
556
557 /* on this call path we do NOT hold the port spinlock,
558 * which is why ACM needs its own spinlock
559 */
560 spin_lock(&acm->lock);
561 if (req->status != -ESHUTDOWN)
562 doit = acm->pending;
563 acm->notify_req = req;
564 spin_unlock(&acm->lock);
565
566 if (doit)
567 acm_notify_serial_state(acm);
568}
569
570/* connect == the TTY link is open */
571
572static void acm_connect(struct gserial *port)
573{
574 struct f_acm *acm = port_to_acm(port);
575
576 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
577 acm_notify_serial_state(acm);
578}
579
580static void acm_disconnect(struct gserial *port)
581{
582 struct f_acm *acm = port_to_acm(port);
583
584 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
585 acm_notify_serial_state(acm);
586}
587
588static int acm_send_break(struct gserial *port, int duration)
589{
590 struct f_acm *acm = port_to_acm(port);
591 u16 state;
592
593 state = acm->serial_state;
594 state &= ~ACM_CTRL_BRK;
595 if (duration)
596 state |= ACM_CTRL_BRK;
597
598 acm->serial_state = state;
599 return acm_notify_serial_state(acm);
600}
601
602/*-------------------------------------------------------------------------*/
603
David Brownell4d5a73d2008-06-19 18:18:40 -0700604/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200605static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700606acm_bind(struct usb_configuration *c, struct usb_function *f)
607{
608 struct usb_composite_dev *cdev = c->cdev;
609 struct f_acm *acm = func_to_acm(f);
610 int status;
611 struct usb_ep *ep;
612
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100613 /* REVISIT might want instance-specific strings to help
614 * distinguish instances ...
615 */
616
617 /* maybe allocate device-global string IDs, and patch descriptors */
618 if (acm_string_defs[0].id == 0) {
619 status = usb_string_ids_tab(c->cdev, acm_string_defs);
620 if (status < 0)
621 return status;
622 acm_control_interface_desc.iInterface =
623 acm_string_defs[ACM_CTRL_IDX].id;
624 acm_data_interface_desc.iInterface =
625 acm_string_defs[ACM_DATA_IDX].id;
626 acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
627 }
628
David Brownell4d5a73d2008-06-19 18:18:40 -0700629 /* allocate instance-specific interface IDs, and patch descriptors */
630 status = usb_interface_id(c, f);
631 if (status < 0)
632 goto fail;
633 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100634 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700635
636 acm_control_interface_desc.bInterfaceNumber = status;
637 acm_union_desc .bMasterInterface0 = status;
638
639 status = usb_interface_id(c, f);
640 if (status < 0)
641 goto fail;
642 acm->data_id = status;
643
644 acm_data_interface_desc.bInterfaceNumber = status;
645 acm_union_desc.bSlaveInterface0 = status;
646 acm_call_mgmt_descriptor.bDataInterface = status;
647
648 status = -ENODEV;
649
650 /* allocate instance-specific endpoints */
651 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
652 if (!ep)
653 goto fail;
654 acm->port.in = ep;
655 ep->driver_data = cdev; /* claim */
656
657 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
658 if (!ep)
659 goto fail;
660 acm->port.out = ep;
661 ep->driver_data = cdev; /* claim */
662
663 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
664 if (!ep)
665 goto fail;
666 acm->notify = ep;
667 ep->driver_data = cdev; /* claim */
668
David Brownell1f1ba112008-08-06 18:49:57 -0700669 /* allocate notification */
670 acm->notify_req = gs_alloc_req(ep,
671 sizeof(struct usb_cdc_notification) + 2,
672 GFP_KERNEL);
673 if (!acm->notify_req)
674 goto fail;
675
676 acm->notify_req->complete = acm_cdc_notify_complete;
677 acm->notify_req->context = acm;
678
David Brownell4d5a73d2008-06-19 18:18:40 -0700679 /* support all relevant hardware speeds... we expect that when
680 * hardware is dual speed, all bulk-capable endpoints work at
681 * both speeds
682 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200683 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
684 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
685 acm_hs_notify_desc.bEndpointAddress =
686 acm_fs_notify_desc.bEndpointAddress;
David Brownell4d5a73d2008-06-19 18:18:40 -0700687
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200688 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
689 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100690
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200691 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
692 acm_ss_function);
693 if (status)
694 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700695
David Brownell4d5a73d2008-06-19 18:18:40 -0700696 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
697 acm->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100698 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell4d5a73d2008-06-19 18:18:40 -0700699 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
700 acm->port.in->name, acm->port.out->name,
701 acm->notify->name);
702 return 0;
703
704fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700705 if (acm->notify_req)
706 gs_free_req(acm->notify, acm->notify_req);
707
David Brownell4d5a73d2008-06-19 18:18:40 -0700708 /* we might as well release our claims on endpoints */
709 if (acm->notify)
710 acm->notify->driver_data = NULL;
711 if (acm->port.out)
712 acm->port.out->driver_data = NULL;
713 if (acm->port.in)
714 acm->port.in->driver_data = NULL;
715
716 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
717
718 return status;
719}
720
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100721static struct f_acm *acm_alloc_basic_func(void)
722{
723 struct f_acm *acm;
724
725 acm = kzalloc(sizeof(*acm), GFP_KERNEL);
726 if (!acm)
727 return NULL;
728
729 spin_lock_init(&acm->lock);
730
731 acm->port.connect = acm_connect;
732 acm->port.disconnect = acm_disconnect;
733 acm->port.send_break = acm_send_break;
734
735 acm->port.func.name = "acm";
736 acm->port.func.strings = acm_strings;
737 /* descriptors are per-instance copies */
738 acm->port.func.bind = acm_bind;
739 acm->port.func.set_alt = acm_set_alt;
740 acm->port.func.setup = acm_setup;
741 acm->port.func.disable = acm_disable;
742
743 return acm;
744}
745
746#ifdef USB_FACM_INCLUDED
David Brownell4d5a73d2008-06-19 18:18:40 -0700747static void
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100748acm_old_unbind(struct usb_configuration *c, struct usb_function *f)
David Brownell4d5a73d2008-06-19 18:18:40 -0700749{
David Brownell1f1ba112008-08-06 18:49:57 -0700750 struct f_acm *acm = func_to_acm(f);
751
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200752 acm_string_defs[0].id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200753 usb_free_all_descriptors(f);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100754 if (acm->notify_req)
755 gs_free_req(acm->notify, acm->notify_req);
David Brownell1f1ba112008-08-06 18:49:57 -0700756 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700757}
758
David Brownell4d5a73d2008-06-19 18:18:40 -0700759/**
760 * acm_bind_config - add a CDC ACM function to a configuration
761 * @c: the configuration to support the CDC ACM instance
762 * @port_num: /dev/ttyGS* port this interface will use
763 * Context: single threaded during gadget setup
764 *
765 * Returns zero on success, else negative errno.
766 *
David Brownell4d5a73d2008-06-19 18:18:40 -0700767 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200768int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700769{
770 struct f_acm *acm;
771 int status;
772
David Brownell4d5a73d2008-06-19 18:18:40 -0700773 /* allocate and initialize one new instance */
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100774 acm = acm_alloc_basic_func();
David Brownell4d5a73d2008-06-19 18:18:40 -0700775 if (!acm)
776 return -ENOMEM;
777
778 acm->port_num = port_num;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100779 acm->port.func.unbind = acm_old_unbind;
David Brownell4d5a73d2008-06-19 18:18:40 -0700780
781 status = usb_add_function(c, &acm->port.func);
782 if (status)
783 kfree(acm);
784 return status;
785}
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100786
787#else
788
789static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
790{
791 struct f_acm *acm = func_to_acm(f);
792
793 acm_string_defs[0].id = 0;
794 usb_free_all_descriptors(f);
795 if (acm->notify_req)
796 gs_free_req(acm->notify, acm->notify_req);
797}
798
799static void acm_free_func(struct usb_function *f)
800{
801 struct f_acm *acm = func_to_acm(f);
802
803 kfree(acm);
804}
805
806static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
807{
808 struct f_serial_opts *opts;
809 struct f_acm *acm;
810
811 acm = acm_alloc_basic_func();
812 if (!acm)
813 return ERR_PTR(-ENOMEM);
814
815 opts = container_of(fi, struct f_serial_opts, func_inst);
816 acm->port_num = opts->port_num;
817 acm->port.func.unbind = acm_unbind;
818 acm->port.func.free_func = acm_free_func;
819
820 return &acm->port.func;
821}
822
823static void acm_free_instance(struct usb_function_instance *fi)
824{
825 struct f_serial_opts *opts;
826
827 opts = container_of(fi, struct f_serial_opts, func_inst);
828 kfree(opts);
829}
830
831static struct usb_function_instance *acm_alloc_instance(void)
832{
833 struct f_serial_opts *opts;
834
835 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
836 if (!opts)
837 return ERR_PTR(-ENOMEM);
838 opts->func_inst.free_func_inst = acm_free_instance;
839 return &opts->func_inst;
840}
841DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
842MODULE_LICENSE("GPL");
843#endif