blob: 69a36af28a71dfbc9e1b92e4839661235312418e [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07008 * Copyright (c) 2011 Code Aurora Forum. All rights reserved.
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01009 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
David Brownell4d5a73d2008-06-19 18:18:40 -070010 *
11 * This software is distributed under the terms of the GNU General
12 * Public License ("GPL") as published by the Free Software Foundation,
13 * either version 2 of that License or (at your option) any later version.
14 */
15
16/* #define VERBOSE_DEBUG */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070019#include <linux/kernel.h>
20#include <linux/device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021#include <linux/usb/android_composite.h>
Anji jonnala92be1b42011-12-19 09:44:41 +053022#include <mach/usb_gadget_xport.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070023
24#include "u_serial.h"
25#include "gadget_chips.h"
26
27
28/*
29 * This CDC ACM function support just wraps control functions and
30 * notifications around the generic serial-over-usb code.
31 *
32 * Because CDC ACM is standardized by the USB-IF, many host operating
33 * systems have drivers for it. Accordingly, ACM is the preferred
34 * interop solution for serial-port type connections. The control
35 * models are often not necessary, and in any case don't do much in
36 * this bare-bones implementation.
37 *
38 * Note that even MS-Windows has some support for ACM. However, that
39 * support is somewhat broken because when you use ACM in a composite
40 * device, having multiple interfaces confuses the poor OS. It doesn't
41 * seem to understand CDC Union descriptors. The new "association"
42 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
43 */
44
45struct acm_ep_descs {
46 struct usb_endpoint_descriptor *in;
47 struct usb_endpoint_descriptor *out;
48 struct usb_endpoint_descriptor *notify;
49};
50
51struct f_acm {
52 struct gserial port;
53 u8 ctrl_id, data_id;
54 u8 port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055 enum transport_type transport;
David Brownell4d5a73d2008-06-19 18:18:40 -070056
David Brownell1f1ba112008-08-06 18:49:57 -070057 u8 pending;
58
59 /* lock is mostly for pending and notify_req ... they get accessed
60 * by callbacks both from tty (open/close/break) under its spinlock,
61 * and notify_req.complete() which can't use that lock.
62 */
63 spinlock_t lock;
64
David Brownell4d5a73d2008-06-19 18:18:40 -070065 struct acm_ep_descs fs;
David Brownell4d5a73d2008-06-19 18:18:40 -070066 struct acm_ep_descs hs;
67
68 struct usb_ep *notify;
69 struct usb_endpoint_descriptor *notify_desc;
David Brownell1f1ba112008-08-06 18:49:57 -070070 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070071
72 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070073
74 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070075 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070076#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
77#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
78
79 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
80 u16 serial_state;
81#define ACM_CTRL_OVERRUN (1 << 6)
82#define ACM_CTRL_PARITY (1 << 5)
83#define ACM_CTRL_FRAMING (1 << 4)
84#define ACM_CTRL_RI (1 << 3)
85#define ACM_CTRL_BRK (1 << 2)
86#define ACM_CTRL_DSR (1 << 1)
87#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070088};
89
Anji jonnala92be1b42011-12-19 09:44:41 +053090static unsigned int no_acm_tty_ports;
91static unsigned int no_acm_sdio_ports;
92static unsigned int no_acm_smd_ports;
93static unsigned int nr_acm_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094
Anji jonnala92be1b42011-12-19 09:44:41 +053095static struct acm_port_info {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 enum transport_type transport;
97 unsigned port_num;
98 unsigned client_port_num;
99} gacm_ports[GSERIAL_NO_PORTS];
100
David Brownell4d5a73d2008-06-19 18:18:40 -0700101static inline struct f_acm *func_to_acm(struct usb_function *f)
102{
103 return container_of(f, struct f_acm, port.func);
104}
105
David Brownell1f1ba112008-08-06 18:49:57 -0700106static inline struct f_acm *port_to_acm(struct gserial *p)
107{
108 return container_of(p, struct f_acm, port);
109}
110
Anji jonnala92be1b42011-12-19 09:44:41 +0530111static int acm_port_setup(struct usb_configuration *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112{
113 int ret = 0;
114
Anji jonnala92be1b42011-12-19 09:44:41 +0530115 pr_debug("%s: no_acm_tty_ports:%u no_acm_sdio_ports: %u nr_acm_ports:%u\n",
116 __func__, no_acm_tty_ports, no_acm_sdio_ports,
117 nr_acm_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118
Anji jonnala92be1b42011-12-19 09:44:41 +0530119 if (no_acm_tty_ports)
120 ret = gserial_setup(c->cdev->gadget, no_acm_tty_ports);
121 if (no_acm_sdio_ports)
122 ret = gsdio_setup(c->cdev->gadget, no_acm_sdio_ports);
123 if (no_acm_smd_ports)
124 ret = gsmd_setup(c->cdev->gadget, no_acm_smd_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125
126 return ret;
127}
128
Anji jonnala92be1b42011-12-19 09:44:41 +0530129static int acm_port_connect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130{
131 unsigned port_num;
132
133 port_num = gacm_ports[acm->port_num].client_port_num;
134
135
136 pr_debug("%s: transport:%s f_acm:%p gserial:%p port_num:%d cl_port_no:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700137 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 acm, &acm->port, acm->port_num, port_num);
139
140 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530141 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 gserial_connect(&acm->port, port_num);
143 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530144 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 gsdio_connect(&acm->port, port_num);
146 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530147 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 gsmd_connect(&acm->port, port_num);
149 break;
150 default:
151 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700152 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 return -ENODEV;
154 }
155
156 return 0;
157}
158
Anji jonnala92be1b42011-12-19 09:44:41 +0530159static int acm_port_disconnect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160{
161 unsigned port_num;
162
163 port_num = gacm_ports[acm->port_num].client_port_num;
164
165 pr_debug("%s: transport:%s f_acm:%p gserial:%p port_num:%d cl_pno:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700166 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 acm, &acm->port, acm->port_num, port_num);
168
169 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530170 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171 gserial_disconnect(&acm->port);
172 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530173 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 gsdio_disconnect(&acm->port, port_num);
175 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530176 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 gsmd_disconnect(&acm->port, port_num);
178 break;
179 default:
180 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700181 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 return -ENODEV;
183 }
184
185 return 0;
186}
David Brownell4d5a73d2008-06-19 18:18:40 -0700187/*-------------------------------------------------------------------------*/
188
189/* notification endpoint uses smallish and infrequent fixed-size messages */
190
191#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
David Brownell1f1ba112008-08-06 18:49:57 -0700192#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -0700193
194/* interface and class descriptors: */
195
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100196static struct usb_interface_assoc_descriptor
197acm_iad_descriptor = {
198 .bLength = sizeof acm_iad_descriptor,
199 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
200
201 /* .bFirstInterface = DYNAMIC, */
202 .bInterfaceCount = 2, // control + data
203 .bFunctionClass = USB_CLASS_COMM,
204 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530205 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100206 /* .iFunction = DYNAMIC */
207};
208
209
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200210static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700211 .bLength = USB_DT_INTERFACE_SIZE,
212 .bDescriptorType = USB_DT_INTERFACE,
213 /* .bInterfaceNumber = DYNAMIC */
214 .bNumEndpoints = 1,
215 .bInterfaceClass = USB_CLASS_COMM,
216 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
217 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
218 /* .iInterface = DYNAMIC */
219};
220
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200221static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700222 .bLength = USB_DT_INTERFACE_SIZE,
223 .bDescriptorType = USB_DT_INTERFACE,
224 /* .bInterfaceNumber = DYNAMIC */
225 .bNumEndpoints = 2,
226 .bInterfaceClass = USB_CLASS_CDC_DATA,
227 .bInterfaceSubClass = 0,
228 .bInterfaceProtocol = 0,
229 /* .iInterface = DYNAMIC */
230};
231
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200232static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700233 .bLength = sizeof(acm_header_desc),
234 .bDescriptorType = USB_DT_CS_INTERFACE,
235 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800236 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700237};
238
239static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200240acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700241 .bLength = sizeof(acm_call_mgmt_descriptor),
242 .bDescriptorType = USB_DT_CS_INTERFACE,
243 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
244 .bmCapabilities = 0,
245 /* .bDataInterface = DYNAMIC */
246};
247
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200248static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700249 .bLength = sizeof(acm_descriptor),
250 .bDescriptorType = USB_DT_CS_INTERFACE,
251 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700252 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700253};
254
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200255static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700256 .bLength = sizeof(acm_union_desc),
257 .bDescriptorType = USB_DT_CS_INTERFACE,
258 .bDescriptorSubType = USB_CDC_UNION_TYPE,
259 /* .bMasterInterface0 = DYNAMIC */
260 /* .bSlaveInterface0 = DYNAMIC */
261};
262
263/* full speed support: */
264
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200265static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
268 .bEndpointAddress = USB_DIR_IN,
269 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800270 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700271 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
272};
273
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200274static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700275 .bLength = USB_DT_ENDPOINT_SIZE,
276 .bDescriptorType = USB_DT_ENDPOINT,
277 .bEndpointAddress = USB_DIR_IN,
278 .bmAttributes = USB_ENDPOINT_XFER_BULK,
279};
280
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200281static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700282 .bLength = USB_DT_ENDPOINT_SIZE,
283 .bDescriptorType = USB_DT_ENDPOINT,
284 .bEndpointAddress = USB_DIR_OUT,
285 .bmAttributes = USB_ENDPOINT_XFER_BULK,
286};
287
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200288static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100289 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700290 (struct usb_descriptor_header *) &acm_control_interface_desc,
291 (struct usb_descriptor_header *) &acm_header_desc,
292 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
293 (struct usb_descriptor_header *) &acm_descriptor,
294 (struct usb_descriptor_header *) &acm_union_desc,
295 (struct usb_descriptor_header *) &acm_fs_notify_desc,
296 (struct usb_descriptor_header *) &acm_data_interface_desc,
297 (struct usb_descriptor_header *) &acm_fs_in_desc,
298 (struct usb_descriptor_header *) &acm_fs_out_desc,
299 NULL,
300};
301
302/* high speed support: */
303
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200304static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700305 .bLength = USB_DT_ENDPOINT_SIZE,
306 .bDescriptorType = USB_DT_ENDPOINT,
307 .bEndpointAddress = USB_DIR_IN,
308 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800309 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700310 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
311};
312
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200313static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800317 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700318};
319
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200320static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700321 .bLength = USB_DT_ENDPOINT_SIZE,
322 .bDescriptorType = USB_DT_ENDPOINT,
323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800324 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700325};
326
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200327static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100328 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700329 (struct usb_descriptor_header *) &acm_control_interface_desc,
330 (struct usb_descriptor_header *) &acm_header_desc,
331 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
332 (struct usb_descriptor_header *) &acm_descriptor,
333 (struct usb_descriptor_header *) &acm_union_desc,
334 (struct usb_descriptor_header *) &acm_hs_notify_desc,
335 (struct usb_descriptor_header *) &acm_data_interface_desc,
336 (struct usb_descriptor_header *) &acm_hs_in_desc,
337 (struct usb_descriptor_header *) &acm_hs_out_desc,
338 NULL,
339};
340
341/* string descriptors: */
342
343#define ACM_CTRL_IDX 0
344#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100345#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700346
347/* static strings, in UTF-8 */
348static struct usb_string acm_string_defs[] = {
349 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
350 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100351 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700352 { /* ZEROES END LIST */ },
353};
354
355static struct usb_gadget_strings acm_string_table = {
356 .language = 0x0409, /* en-us */
357 .strings = acm_string_defs,
358};
359
360static struct usb_gadget_strings *acm_strings[] = {
361 &acm_string_table,
362 NULL,
363};
364
365/*-------------------------------------------------------------------------*/
366
367/* ACM control ... data handling is delegated to tty library code.
368 * The main task of this function is to activate and deactivate
369 * that code based on device state; track parameters like line
370 * speed, handshake state, and so on; and issue notifications.
371 */
372
373static void acm_complete_set_line_coding(struct usb_ep *ep,
374 struct usb_request *req)
375{
376 struct f_acm *acm = ep->driver_data;
377 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
378
379 if (req->status != 0) {
380 DBG(cdev, "acm ttyGS%d completion, err %d\n",
381 acm->port_num, req->status);
382 return;
383 }
384
385 /* normal completion */
386 if (req->actual != sizeof(acm->port_line_coding)) {
387 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
388 acm->port_num, req->actual);
389 usb_ep_set_halt(ep);
390 } else {
391 struct usb_cdc_line_coding *value = req->buf;
392
393 /* REVISIT: we currently just remember this data.
394 * If we change that, (a) validate it first, then
395 * (b) update whatever hardware needs updating,
396 * (c) worry about locking. This is information on
397 * the order of 9600-8-N-1 ... most of which means
398 * nothing unless we control a real RS232 line.
399 */
400 acm->port_line_coding = *value;
401 }
402}
403
404static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
405{
406 struct f_acm *acm = func_to_acm(f);
407 struct usb_composite_dev *cdev = f->config->cdev;
408 struct usb_request *req = cdev->req;
409 int value = -EOPNOTSUPP;
410 u16 w_index = le16_to_cpu(ctrl->wIndex);
411 u16 w_value = le16_to_cpu(ctrl->wValue);
412 u16 w_length = le16_to_cpu(ctrl->wLength);
413
414 /* composite driver infrastructure handles everything except
415 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700416 *
417 * Note CDC spec table 4 lists the ACM request profile. It requires
418 * encapsulated command support ... we don't handle any, and respond
419 * to them by stalling. Options include get/set/clear comm features
420 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700421 */
422 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
423
424 /* SET_LINE_CODING ... just read and save what the host sends */
425 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
426 | USB_CDC_REQ_SET_LINE_CODING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 if (w_length != sizeof(struct usb_cdc_line_coding))
David Brownell4d5a73d2008-06-19 18:18:40 -0700428 goto invalid;
429
430 value = w_length;
431 cdev->gadget->ep0->driver_data = acm;
432 req->complete = acm_complete_set_line_coding;
433 break;
434
435 /* GET_LINE_CODING ... return what host sent, or initial value */
436 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
437 | USB_CDC_REQ_GET_LINE_CODING:
David Brownell4d5a73d2008-06-19 18:18:40 -0700438
439 value = min_t(unsigned, w_length,
440 sizeof(struct usb_cdc_line_coding));
441 memcpy(req->buf, &acm->port_line_coding, value);
442 break;
443
444 /* SET_CONTROL_LINE_STATE ... save what the host sent */
445 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
446 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
David Brownell4d5a73d2008-06-19 18:18:40 -0700447 value = 0;
448
449 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700450 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700451 * that bit, we should return to that no-flow state.
452 */
453 acm->port_handshake_bits = w_value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 if (acm->port.notify_modem) {
455 unsigned port_num =
456 gacm_ports[acm->port_num].client_port_num;
457
458 acm->port.notify_modem(&acm->port, port_num, w_value);
459 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700460 break;
461
462 default:
463invalid:
464 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
465 ctrl->bRequestType, ctrl->bRequest,
466 w_value, w_index, w_length);
467 }
468
469 /* respond with data transfer or status phase? */
470 if (value >= 0) {
471 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
472 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
473 w_value, w_index, w_length);
474 req->zero = 0;
475 req->length = value;
476 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
477 if (value < 0)
478 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
479 acm->port_num, value);
480 }
481
482 /* device either stalls (value < 0) or reports success */
483 return value;
484}
485
486static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
487{
488 struct f_acm *acm = func_to_acm(f);
489 struct usb_composite_dev *cdev = f->config->cdev;
490
491 /* we know alt == 0, so this is an activation or a reset */
492
493 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700494 if (acm->notify->driver_data) {
495 VDBG(cdev, "reset acm control interface %d\n", intf);
496 usb_ep_disable(acm->notify);
497 } else {
498 VDBG(cdev, "init acm ctrl interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700499 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800500 acm->notify_desc = ep_choose(cdev->gadget,
501 acm->hs.notify,
502 acm->fs.notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700503 usb_ep_enable(acm->notify, acm->notify_desc);
504 acm->notify->driver_data = acm;
505
506 } else if (intf == acm->data_id) {
507 if (acm->port.in->driver_data) {
508 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530509 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700510 } else {
511 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
David Brownell4d5a73d2008-06-19 18:18:40 -0700512 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800513 acm->port.in_desc = ep_choose(cdev->gadget,
514 acm->hs.in, acm->fs.in);
515 acm->port.out_desc = ep_choose(cdev->gadget,
516 acm->hs.out, acm->fs.out);
Anji jonnala92be1b42011-12-19 09:44:41 +0530517 acm_port_connect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700518
519 } else
520 return -EINVAL;
521
522 return 0;
523}
524
525static void acm_disable(struct usb_function *f)
526{
527 struct f_acm *acm = func_to_acm(f);
528 struct usb_composite_dev *cdev = f->config->cdev;
529
530 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530531 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700532 usb_ep_disable(acm->notify);
533 acm->notify->driver_data = NULL;
534}
535
536/*-------------------------------------------------------------------------*/
537
David Brownell1f1ba112008-08-06 18:49:57 -0700538/**
539 * acm_cdc_notify - issue CDC notification to host
540 * @acm: wraps host to be notified
541 * @type: notification type
542 * @value: Refer to cdc specs, wValue field.
543 * @data: data to be sent
544 * @length: size of data
545 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
546 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200547 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700548 *
549 * See section 6.3.5 of the CDC 1.1 specification for information
550 * about the only notification we issue: SerialState change.
551 */
552static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
553 void *data, unsigned length)
554{
555 struct usb_ep *ep = acm->notify;
556 struct usb_request *req;
557 struct usb_cdc_notification *notify;
558 const unsigned len = sizeof(*notify) + length;
559 void *buf;
560 int status;
561
562 req = acm->notify_req;
563 acm->notify_req = NULL;
564 acm->pending = false;
565
566 req->length = len;
567 notify = req->buf;
568 buf = notify + 1;
569
570 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
571 | USB_RECIP_INTERFACE;
572 notify->bNotificationType = type;
573 notify->wValue = cpu_to_le16(value);
574 notify->wIndex = cpu_to_le16(acm->ctrl_id);
575 notify->wLength = cpu_to_le16(length);
576 memcpy(buf, data, length);
577
David Brownelle50ae572008-11-12 11:35:13 -0800578 /* ep_queue() can complete immediately if it fills the fifo... */
579 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700580 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800581 spin_lock(&acm->lock);
582
David Brownell1f1ba112008-08-06 18:49:57 -0700583 if (status < 0) {
584 ERROR(acm->port.func.config->cdev,
585 "acm ttyGS%d can't notify serial state, %d\n",
586 acm->port_num, status);
587 acm->notify_req = req;
588 }
589
590 return status;
591}
592
593static int acm_notify_serial_state(struct f_acm *acm)
594{
595 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
596 int status;
597
598 spin_lock(&acm->lock);
599 if (acm->notify_req) {
600 DBG(cdev, "acm ttyGS%d serial state %04x\n",
601 acm->port_num, acm->serial_state);
602 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
603 0, &acm->serial_state, sizeof(acm->serial_state));
604 } else {
605 acm->pending = true;
606 status = 0;
607 }
608 spin_unlock(&acm->lock);
609 return status;
610}
611
612static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
613{
614 struct f_acm *acm = req->context;
615 u8 doit = false;
616
617 /* on this call path we do NOT hold the port spinlock,
618 * which is why ACM needs its own spinlock
619 */
620 spin_lock(&acm->lock);
621 if (req->status != -ESHUTDOWN)
622 doit = acm->pending;
623 acm->notify_req = req;
624 spin_unlock(&acm->lock);
625
626 if (doit)
627 acm_notify_serial_state(acm);
628}
629
630/* connect == the TTY link is open */
631
632static void acm_connect(struct gserial *port)
633{
634 struct f_acm *acm = port_to_acm(port);
635
636 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
637 acm_notify_serial_state(acm);
638}
639
640static void acm_disconnect(struct gserial *port)
641{
642 struct f_acm *acm = port_to_acm(port);
643
644 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
645 acm_notify_serial_state(acm);
646}
647
648static int acm_send_break(struct gserial *port, int duration)
649{
650 struct f_acm *acm = port_to_acm(port);
651 u16 state;
652
653 state = acm->serial_state;
654 state &= ~ACM_CTRL_BRK;
655 if (duration)
656 state |= ACM_CTRL_BRK;
657
658 acm->serial_state = state;
659 return acm_notify_serial_state(acm);
660}
661
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662static int acm_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
663{
664 struct f_acm *acm = port_to_acm(port);
665
666 acm->serial_state = ctrl_bits;
667
668 return acm_notify_serial_state(acm);
669}
670
David Brownell1f1ba112008-08-06 18:49:57 -0700671/*-------------------------------------------------------------------------*/
672
David Brownell4d5a73d2008-06-19 18:18:40 -0700673/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200674static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700675acm_bind(struct usb_configuration *c, struct usb_function *f)
676{
677 struct usb_composite_dev *cdev = c->cdev;
678 struct f_acm *acm = func_to_acm(f);
679 int status;
680 struct usb_ep *ep;
681
682 /* allocate instance-specific interface IDs, and patch descriptors */
683 status = usb_interface_id(c, f);
684 if (status < 0)
685 goto fail;
686 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100687 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700688
689 acm_control_interface_desc.bInterfaceNumber = status;
690 acm_union_desc .bMasterInterface0 = status;
691
692 status = usb_interface_id(c, f);
693 if (status < 0)
694 goto fail;
695 acm->data_id = status;
696
697 acm_data_interface_desc.bInterfaceNumber = status;
698 acm_union_desc.bSlaveInterface0 = status;
699 acm_call_mgmt_descriptor.bDataInterface = status;
700
701 status = -ENODEV;
702
703 /* allocate instance-specific endpoints */
704 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
705 if (!ep)
706 goto fail;
707 acm->port.in = ep;
708 ep->driver_data = cdev; /* claim */
709
710 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
711 if (!ep)
712 goto fail;
713 acm->port.out = ep;
714 ep->driver_data = cdev; /* claim */
715
716 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
717 if (!ep)
718 goto fail;
719 acm->notify = ep;
720 ep->driver_data = cdev; /* claim */
721
David Brownell1f1ba112008-08-06 18:49:57 -0700722 /* allocate notification */
723 acm->notify_req = gs_alloc_req(ep,
724 sizeof(struct usb_cdc_notification) + 2,
725 GFP_KERNEL);
726 if (!acm->notify_req)
727 goto fail;
728
729 acm->notify_req->complete = acm_cdc_notify_complete;
730 acm->notify_req->context = acm;
731
David Brownell4d5a73d2008-06-19 18:18:40 -0700732 /* copy descriptors, and track endpoint copies */
733 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700734 if (!f->descriptors)
735 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700736
737 acm->fs.in = usb_find_endpoint(acm_fs_function,
738 f->descriptors, &acm_fs_in_desc);
739 acm->fs.out = usb_find_endpoint(acm_fs_function,
740 f->descriptors, &acm_fs_out_desc);
741 acm->fs.notify = usb_find_endpoint(acm_fs_function,
742 f->descriptors, &acm_fs_notify_desc);
743
744 /* support all relevant hardware speeds... we expect that when
745 * hardware is dual speed, all bulk-capable endpoints work at
746 * both speeds
747 */
748 if (gadget_is_dualspeed(c->cdev->gadget)) {
749 acm_hs_in_desc.bEndpointAddress =
750 acm_fs_in_desc.bEndpointAddress;
751 acm_hs_out_desc.bEndpointAddress =
752 acm_fs_out_desc.bEndpointAddress;
753 acm_hs_notify_desc.bEndpointAddress =
754 acm_fs_notify_desc.bEndpointAddress;
755
756 /* copy descriptors, and track endpoint copies */
757 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
758
759 acm->hs.in = usb_find_endpoint(acm_hs_function,
760 f->hs_descriptors, &acm_hs_in_desc);
761 acm->hs.out = usb_find_endpoint(acm_hs_function,
762 f->hs_descriptors, &acm_hs_out_desc);
763 acm->hs.notify = usb_find_endpoint(acm_hs_function,
764 f->hs_descriptors, &acm_hs_notify_desc);
765 }
766
David Brownell4d5a73d2008-06-19 18:18:40 -0700767 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
768 acm->port_num,
769 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
770 acm->port.in->name, acm->port.out->name,
771 acm->notify->name);
772 return 0;
773
774fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700775 if (acm->notify_req)
776 gs_free_req(acm->notify, acm->notify_req);
777
David Brownell4d5a73d2008-06-19 18:18:40 -0700778 /* we might as well release our claims on endpoints */
779 if (acm->notify)
780 acm->notify->driver_data = NULL;
781 if (acm->port.out)
782 acm->port.out->driver_data = NULL;
783 if (acm->port.in)
784 acm->port.in->driver_data = NULL;
785
786 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
787
788 return status;
789}
790
791static void
792acm_unbind(struct usb_configuration *c, struct usb_function *f)
793{
David Brownell1f1ba112008-08-06 18:49:57 -0700794 struct f_acm *acm = func_to_acm(f);
795
David Brownell4d5a73d2008-06-19 18:18:40 -0700796 if (gadget_is_dualspeed(c->cdev->gadget))
797 usb_free_descriptors(f->hs_descriptors);
798 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700799 gs_free_req(acm->notify, acm->notify_req);
John Michelau677ba872010-11-08 18:05:37 -0600800 kfree(acm->port.func.name);
David Brownell1f1ba112008-08-06 18:49:57 -0700801 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700802}
803
804/* Some controllers can't support CDC ACM ... */
805static inline bool can_support_cdc(struct usb_configuration *c)
806{
David Brownell4d5a73d2008-06-19 18:18:40 -0700807 /* everything else is *probably* fine ... */
808 return true;
809}
810
811/**
812 * acm_bind_config - add a CDC ACM function to a configuration
813 * @c: the configuration to support the CDC ACM instance
814 * @port_num: /dev/ttyGS* port this interface will use
815 * Context: single threaded during gadget setup
816 *
817 * Returns zero on success, else negative errno.
818 *
819 * Caller must have called @gserial_setup() with enough ports to
820 * handle all the ones it binds. Caller is also responsible
821 * for calling @gserial_cleanup() before module unload.
822 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200823int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700824{
825 struct f_acm *acm;
826 int status;
827
828 if (!can_support_cdc(c))
829 return -EINVAL;
830
831 /* REVISIT might want instance-specific strings to help
832 * distinguish instances ...
833 */
834
835 /* maybe allocate device-global string IDs, and patch descriptors */
836 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
837 status = usb_string_id(c->cdev);
838 if (status < 0)
839 return status;
840 acm_string_defs[ACM_CTRL_IDX].id = status;
841
842 acm_control_interface_desc.iInterface = status;
843
844 status = usb_string_id(c->cdev);
845 if (status < 0)
846 return status;
847 acm_string_defs[ACM_DATA_IDX].id = status;
848
849 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100850
851 status = usb_string_id(c->cdev);
852 if (status < 0)
853 return status;
854 acm_string_defs[ACM_IAD_IDX].id = status;
855
856 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700857 }
858
859 /* allocate and initialize one new instance */
860 acm = kzalloc(sizeof *acm, GFP_KERNEL);
861 if (!acm)
862 return -ENOMEM;
863
David Brownell1f1ba112008-08-06 18:49:57 -0700864 spin_lock_init(&acm->lock);
865
David Brownell4d5a73d2008-06-19 18:18:40 -0700866 acm->port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 acm->transport = gacm_ports[port_num].transport;
David Brownell4d5a73d2008-06-19 18:18:40 -0700868
David Brownell1f1ba112008-08-06 18:49:57 -0700869 acm->port.connect = acm_connect;
870 acm->port.disconnect = acm_disconnect;
871 acm->port.send_break = acm_send_break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 acm->port.send_modem_ctrl_bits = acm_send_modem_ctrl_bits;
David Brownell1f1ba112008-08-06 18:49:57 -0700873
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num + 1);
John Michelau677ba872010-11-08 18:05:37 -0600875 if (!acm->port.func.name) {
876 kfree(acm);
877 return -ENOMEM;
878 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700879 acm->port.func.strings = acm_strings;
880 /* descriptors are per-instance copies */
881 acm->port.func.bind = acm_bind;
882 acm->port.func.unbind = acm_unbind;
883 acm->port.func.set_alt = acm_set_alt;
884 acm->port.func.setup = acm_setup;
885 acm->port.func.disable = acm_disable;
886
887 status = usb_add_function(c, &acm->port.func);
888 if (status)
889 kfree(acm);
890 return status;
891}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
Anji jonnala92be1b42011-12-19 09:44:41 +0530893/**
894 * acm_init_port - bind a acm_port to its transport
895 */
896static int acm_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897{
Anji jonnala92be1b42011-12-19 09:44:41 +0530898 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899
Anji jonnala92be1b42011-12-19 09:44:41 +0530900 if (port_num >= GSERIAL_NO_PORTS)
901 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902
Anji jonnala92be1b42011-12-19 09:44:41 +0530903 transport = str_to_xport(name);
904 pr_debug("%s, port:%d, transport:%s\n", __func__,
905 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906
Anji jonnala92be1b42011-12-19 09:44:41 +0530907 gacm_ports[port_num].transport = transport;
908 gacm_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909
Anji jonnala92be1b42011-12-19 09:44:41 +0530910 switch (transport) {
911 case USB_GADGET_XPORT_TTY:
912 gacm_ports[port_num].client_port_num = no_acm_tty_ports;
913 no_acm_tty_ports++;
914 break;
915 case USB_GADGET_XPORT_SDIO:
916 gacm_ports[port_num].client_port_num = no_acm_sdio_ports;
917 no_acm_sdio_ports++;
918 break;
919 case USB_GADGET_XPORT_SMD:
920 gacm_ports[port_num].client_port_num = no_acm_smd_ports;
921 no_acm_smd_ports++;
922 break;
923 default:
924 pr_err("%s: Un-supported transport transport: %u\n",
925 __func__, gacm_ports[port_num].transport);
926 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 }
928
Anji jonnala92be1b42011-12-19 09:44:41 +0530929 nr_acm_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930
931 return 0;
932}