blob: 8e542a482262b0376d3d171d9762a90d1d85dd2b [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
David Brownell4d5a73d2008-06-19 18:18:40 -070045struct f_acm {
46 struct gserial port;
47 u8 ctrl_id, data_id;
48 u8 port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049 enum transport_type transport;
David Brownell4d5a73d2008-06-19 18:18:40 -070050
David Brownell1f1ba112008-08-06 18:49:57 -070051 u8 pending;
52
53 /* lock is mostly for pending and notify_req ... they get accessed
54 * by callbacks both from tty (open/close/break) under its spinlock,
55 * and notify_req.complete() which can't use that lock.
56 */
57 spinlock_t lock;
58
David Brownell4d5a73d2008-06-19 18:18:40 -070059 struct usb_ep *notify;
David Brownell1f1ba112008-08-06 18:49:57 -070060 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070061
62 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070063
64 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070065 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070066#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
67#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
68
69 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
70 u16 serial_state;
71#define ACM_CTRL_OVERRUN (1 << 6)
72#define ACM_CTRL_PARITY (1 << 5)
73#define ACM_CTRL_FRAMING (1 << 4)
74#define ACM_CTRL_RI (1 << 3)
75#define ACM_CTRL_BRK (1 << 2)
76#define ACM_CTRL_DSR (1 << 1)
77#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070078};
79
Anji jonnala92be1b42011-12-19 09:44:41 +053080static unsigned int no_acm_tty_ports;
81static unsigned int no_acm_sdio_ports;
82static unsigned int no_acm_smd_ports;
83static unsigned int nr_acm_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084
Anji jonnala92be1b42011-12-19 09:44:41 +053085static struct acm_port_info {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086 enum transport_type transport;
87 unsigned port_num;
88 unsigned client_port_num;
89} gacm_ports[GSERIAL_NO_PORTS];
90
David Brownell4d5a73d2008-06-19 18:18:40 -070091static inline struct f_acm *func_to_acm(struct usb_function *f)
92{
93 return container_of(f, struct f_acm, port.func);
94}
95
David Brownell1f1ba112008-08-06 18:49:57 -070096static inline struct f_acm *port_to_acm(struct gserial *p)
97{
98 return container_of(p, struct f_acm, port);
99}
100
Anji jonnala92be1b42011-12-19 09:44:41 +0530101static int acm_port_setup(struct usb_configuration *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102{
103 int ret = 0;
104
Anji jonnala92be1b42011-12-19 09:44:41 +0530105 pr_debug("%s: no_acm_tty_ports:%u no_acm_sdio_ports: %u nr_acm_ports:%u\n",
106 __func__, no_acm_tty_ports, no_acm_sdio_ports,
107 nr_acm_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Anji jonnala92be1b42011-12-19 09:44:41 +0530109 if (no_acm_tty_ports)
110 ret = gserial_setup(c->cdev->gadget, no_acm_tty_ports);
111 if (no_acm_sdio_ports)
112 ret = gsdio_setup(c->cdev->gadget, no_acm_sdio_ports);
113 if (no_acm_smd_ports)
114 ret = gsmd_setup(c->cdev->gadget, no_acm_smd_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115
116 return ret;
117}
118
Anji jonnala92be1b42011-12-19 09:44:41 +0530119static int acm_port_connect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120{
121 unsigned port_num;
122
123 port_num = gacm_ports[acm->port_num].client_port_num;
124
125
126 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 -0700127 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 acm, &acm->port, acm->port_num, port_num);
129
130 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530131 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 gserial_connect(&acm->port, port_num);
133 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530134 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 gsdio_connect(&acm->port, port_num);
136 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530137 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 gsmd_connect(&acm->port, port_num);
139 break;
140 default:
141 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700142 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 return -ENODEV;
144 }
145
146 return 0;
147}
148
Anji jonnala92be1b42011-12-19 09:44:41 +0530149static int acm_port_disconnect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150{
151 unsigned port_num;
152
153 port_num = gacm_ports[acm->port_num].client_port_num;
154
155 pr_debug("%s: transport:%s f_acm:%p gserial:%p port_num:%d cl_pno:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700156 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 acm, &acm->port, acm->port_num, port_num);
158
159 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530160 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 gserial_disconnect(&acm->port);
162 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530163 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 gsdio_disconnect(&acm->port, port_num);
165 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530166 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 gsmd_disconnect(&acm->port, port_num);
168 break;
169 default:
170 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700171 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172 return -ENODEV;
173 }
174
175 return 0;
176}
David Brownell4d5a73d2008-06-19 18:18:40 -0700177/*-------------------------------------------------------------------------*/
178
179/* notification endpoint uses smallish and infrequent fixed-size messages */
180
181#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
David Brownell1f1ba112008-08-06 18:49:57 -0700182#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -0700183
184/* interface and class descriptors: */
185
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100186static struct usb_interface_assoc_descriptor
187acm_iad_descriptor = {
188 .bLength = sizeof acm_iad_descriptor,
189 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
190
191 /* .bFirstInterface = DYNAMIC, */
192 .bInterfaceCount = 2, // control + data
193 .bFunctionClass = USB_CLASS_COMM,
194 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530195 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100196 /* .iFunction = DYNAMIC */
197};
198
199
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200200static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700201 .bLength = USB_DT_INTERFACE_SIZE,
202 .bDescriptorType = USB_DT_INTERFACE,
203 /* .bInterfaceNumber = DYNAMIC */
204 .bNumEndpoints = 1,
205 .bInterfaceClass = USB_CLASS_COMM,
206 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
207 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
208 /* .iInterface = DYNAMIC */
209};
210
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200211static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700212 .bLength = USB_DT_INTERFACE_SIZE,
213 .bDescriptorType = USB_DT_INTERFACE,
214 /* .bInterfaceNumber = DYNAMIC */
215 .bNumEndpoints = 2,
216 .bInterfaceClass = USB_CLASS_CDC_DATA,
217 .bInterfaceSubClass = 0,
218 .bInterfaceProtocol = 0,
219 /* .iInterface = DYNAMIC */
220};
221
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200222static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700223 .bLength = sizeof(acm_header_desc),
224 .bDescriptorType = USB_DT_CS_INTERFACE,
225 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800226 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700227};
228
229static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200230acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700231 .bLength = sizeof(acm_call_mgmt_descriptor),
232 .bDescriptorType = USB_DT_CS_INTERFACE,
233 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
234 .bmCapabilities = 0,
235 /* .bDataInterface = DYNAMIC */
236};
237
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200238static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700239 .bLength = sizeof(acm_descriptor),
240 .bDescriptorType = USB_DT_CS_INTERFACE,
241 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700242 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700243};
244
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200245static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700246 .bLength = sizeof(acm_union_desc),
247 .bDescriptorType = USB_DT_CS_INTERFACE,
248 .bDescriptorSubType = USB_CDC_UNION_TYPE,
249 /* .bMasterInterface0 = DYNAMIC */
250 /* .bSlaveInterface0 = DYNAMIC */
251};
252
253/* full speed support: */
254
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200255static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800260 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700261 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
262};
263
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200264static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700265 .bLength = USB_DT_ENDPOINT_SIZE,
266 .bDescriptorType = USB_DT_ENDPOINT,
267 .bEndpointAddress = USB_DIR_IN,
268 .bmAttributes = USB_ENDPOINT_XFER_BULK,
269};
270
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200271static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700272 .bLength = USB_DT_ENDPOINT_SIZE,
273 .bDescriptorType = USB_DT_ENDPOINT,
274 .bEndpointAddress = USB_DIR_OUT,
275 .bmAttributes = USB_ENDPOINT_XFER_BULK,
276};
277
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200278static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100279 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700280 (struct usb_descriptor_header *) &acm_control_interface_desc,
281 (struct usb_descriptor_header *) &acm_header_desc,
282 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
283 (struct usb_descriptor_header *) &acm_descriptor,
284 (struct usb_descriptor_header *) &acm_union_desc,
285 (struct usb_descriptor_header *) &acm_fs_notify_desc,
286 (struct usb_descriptor_header *) &acm_data_interface_desc,
287 (struct usb_descriptor_header *) &acm_fs_in_desc,
288 (struct usb_descriptor_header *) &acm_fs_out_desc,
289 NULL,
290};
291
292/* high speed support: */
293
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200294static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
297 .bEndpointAddress = USB_DIR_IN,
298 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800299 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700300 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
301};
302
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200303static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700304 .bLength = USB_DT_ENDPOINT_SIZE,
305 .bDescriptorType = USB_DT_ENDPOINT,
306 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800307 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700308};
309
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200310static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700311 .bLength = USB_DT_ENDPOINT_SIZE,
312 .bDescriptorType = USB_DT_ENDPOINT,
313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800314 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700315};
316
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200317static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100318 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700319 (struct usb_descriptor_header *) &acm_control_interface_desc,
320 (struct usb_descriptor_header *) &acm_header_desc,
321 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
322 (struct usb_descriptor_header *) &acm_descriptor,
323 (struct usb_descriptor_header *) &acm_union_desc,
324 (struct usb_descriptor_header *) &acm_hs_notify_desc,
325 (struct usb_descriptor_header *) &acm_data_interface_desc,
326 (struct usb_descriptor_header *) &acm_hs_in_desc,
327 (struct usb_descriptor_header *) &acm_hs_out_desc,
328 NULL,
329};
330
331/* string descriptors: */
332
333#define ACM_CTRL_IDX 0
334#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100335#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700336
337/* static strings, in UTF-8 */
338static struct usb_string acm_string_defs[] = {
339 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
340 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100341 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700342 { /* ZEROES END LIST */ },
343};
344
345static struct usb_gadget_strings acm_string_table = {
346 .language = 0x0409, /* en-us */
347 .strings = acm_string_defs,
348};
349
350static struct usb_gadget_strings *acm_strings[] = {
351 &acm_string_table,
352 NULL,
353};
354
355/*-------------------------------------------------------------------------*/
356
357/* ACM control ... data handling is delegated to tty library code.
358 * The main task of this function is to activate and deactivate
359 * that code based on device state; track parameters like line
360 * speed, handshake state, and so on; and issue notifications.
361 */
362
363static void acm_complete_set_line_coding(struct usb_ep *ep,
364 struct usb_request *req)
365{
366 struct f_acm *acm = ep->driver_data;
367 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
368
369 if (req->status != 0) {
370 DBG(cdev, "acm ttyGS%d completion, err %d\n",
371 acm->port_num, req->status);
372 return;
373 }
374
375 /* normal completion */
376 if (req->actual != sizeof(acm->port_line_coding)) {
377 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
378 acm->port_num, req->actual);
379 usb_ep_set_halt(ep);
380 } else {
381 struct usb_cdc_line_coding *value = req->buf;
382
383 /* REVISIT: we currently just remember this data.
384 * If we change that, (a) validate it first, then
385 * (b) update whatever hardware needs updating,
386 * (c) worry about locking. This is information on
387 * the order of 9600-8-N-1 ... most of which means
388 * nothing unless we control a real RS232 line.
389 */
390 acm->port_line_coding = *value;
391 }
392}
393
394static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
395{
396 struct f_acm *acm = func_to_acm(f);
397 struct usb_composite_dev *cdev = f->config->cdev;
398 struct usb_request *req = cdev->req;
399 int value = -EOPNOTSUPP;
400 u16 w_index = le16_to_cpu(ctrl->wIndex);
401 u16 w_value = le16_to_cpu(ctrl->wValue);
402 u16 w_length = le16_to_cpu(ctrl->wLength);
403
404 /* composite driver infrastructure handles everything except
405 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700406 *
407 * Note CDC spec table 4 lists the ACM request profile. It requires
408 * encapsulated command support ... we don't handle any, and respond
409 * to them by stalling. Options include get/set/clear comm features
410 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700411 */
412 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
413
414 /* SET_LINE_CODING ... just read and save what the host sends */
415 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
416 | USB_CDC_REQ_SET_LINE_CODING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 if (w_length != sizeof(struct usb_cdc_line_coding))
David Brownell4d5a73d2008-06-19 18:18:40 -0700418 goto invalid;
419
420 value = w_length;
421 cdev->gadget->ep0->driver_data = acm;
422 req->complete = acm_complete_set_line_coding;
423 break;
424
425 /* GET_LINE_CODING ... return what host sent, or initial value */
426 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
427 | USB_CDC_REQ_GET_LINE_CODING:
David Brownell4d5a73d2008-06-19 18:18:40 -0700428
429 value = min_t(unsigned, w_length,
430 sizeof(struct usb_cdc_line_coding));
431 memcpy(req->buf, &acm->port_line_coding, value);
432 break;
433
434 /* SET_CONTROL_LINE_STATE ... save what the host sent */
435 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
436 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
David Brownell4d5a73d2008-06-19 18:18:40 -0700437 value = 0;
438
439 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700440 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700441 * that bit, we should return to that no-flow state.
442 */
443 acm->port_handshake_bits = w_value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 if (acm->port.notify_modem) {
445 unsigned port_num =
446 gacm_ports[acm->port_num].client_port_num;
447
448 acm->port.notify_modem(&acm->port, port_num, w_value);
449 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700450 break;
451
452 default:
453invalid:
454 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
455 ctrl->bRequestType, ctrl->bRequest,
456 w_value, w_index, w_length);
457 }
458
459 /* respond with data transfer or status phase? */
460 if (value >= 0) {
461 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
462 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
463 w_value, w_index, w_length);
464 req->zero = 0;
465 req->length = value;
466 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
467 if (value < 0)
468 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
469 acm->port_num, value);
470 }
471
472 /* device either stalls (value < 0) or reports success */
473 return value;
474}
475
476static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
477{
478 struct f_acm *acm = func_to_acm(f);
479 struct usb_composite_dev *cdev = f->config->cdev;
480
481 /* we know alt == 0, so this is an activation or a reset */
482
483 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700484 if (acm->notify->driver_data) {
485 VDBG(cdev, "reset acm control interface %d\n", intf);
486 usb_ep_disable(acm->notify);
487 } else {
488 VDBG(cdev, "init acm ctrl interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700489 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200490 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
491 return -EINVAL;
492
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300493 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700494 acm->notify->driver_data = acm;
495
496 } else if (intf == acm->data_id) {
497 if (acm->port.in->driver_data) {
498 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530499 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700500 } else {
501 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
David Brownell4d5a73d2008-06-19 18:18:40 -0700502 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200503 if (config_ep_by_speed(cdev->gadget, f,
504 acm->port.in) ||
505 config_ep_by_speed(cdev->gadget, f,
506 acm->port.out)) {
507 acm->port.in->desc = NULL;
508 acm->port.out->desc = NULL;
509 return -EINVAL;
510 }
511
Anji jonnala92be1b42011-12-19 09:44:41 +0530512 acm_port_connect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700513
514 } else
515 return -EINVAL;
516
517 return 0;
518}
519
520static void acm_disable(struct usb_function *f)
521{
522 struct f_acm *acm = func_to_acm(f);
523 struct usb_composite_dev *cdev = f->config->cdev;
524
525 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530526 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700527 usb_ep_disable(acm->notify);
528 acm->notify->driver_data = NULL;
529}
530
531/*-------------------------------------------------------------------------*/
532
David Brownell1f1ba112008-08-06 18:49:57 -0700533/**
534 * acm_cdc_notify - issue CDC notification to host
535 * @acm: wraps host to be notified
536 * @type: notification type
537 * @value: Refer to cdc specs, wValue field.
538 * @data: data to be sent
539 * @length: size of data
540 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
541 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200542 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700543 *
544 * See section 6.3.5 of the CDC 1.1 specification for information
545 * about the only notification we issue: SerialState change.
546 */
547static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
548 void *data, unsigned length)
549{
550 struct usb_ep *ep = acm->notify;
551 struct usb_request *req;
552 struct usb_cdc_notification *notify;
553 const unsigned len = sizeof(*notify) + length;
554 void *buf;
555 int status;
556
557 req = acm->notify_req;
558 acm->notify_req = NULL;
559 acm->pending = false;
560
561 req->length = len;
562 notify = req->buf;
563 buf = notify + 1;
564
565 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
566 | USB_RECIP_INTERFACE;
567 notify->bNotificationType = type;
568 notify->wValue = cpu_to_le16(value);
569 notify->wIndex = cpu_to_le16(acm->ctrl_id);
570 notify->wLength = cpu_to_le16(length);
571 memcpy(buf, data, length);
572
David Brownelle50ae572008-11-12 11:35:13 -0800573 /* ep_queue() can complete immediately if it fills the fifo... */
574 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700575 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800576 spin_lock(&acm->lock);
577
David Brownell1f1ba112008-08-06 18:49:57 -0700578 if (status < 0) {
579 ERROR(acm->port.func.config->cdev,
580 "acm ttyGS%d can't notify serial state, %d\n",
581 acm->port_num, status);
582 acm->notify_req = req;
583 }
584
585 return status;
586}
587
588static int acm_notify_serial_state(struct f_acm *acm)
589{
590 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
591 int status;
592
593 spin_lock(&acm->lock);
594 if (acm->notify_req) {
595 DBG(cdev, "acm ttyGS%d serial state %04x\n",
596 acm->port_num, acm->serial_state);
597 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
598 0, &acm->serial_state, sizeof(acm->serial_state));
599 } else {
600 acm->pending = true;
601 status = 0;
602 }
603 spin_unlock(&acm->lock);
604 return status;
605}
606
607static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
608{
609 struct f_acm *acm = req->context;
610 u8 doit = false;
611
612 /* on this call path we do NOT hold the port spinlock,
613 * which is why ACM needs its own spinlock
614 */
615 spin_lock(&acm->lock);
616 if (req->status != -ESHUTDOWN)
617 doit = acm->pending;
618 acm->notify_req = req;
619 spin_unlock(&acm->lock);
620
621 if (doit)
622 acm_notify_serial_state(acm);
623}
624
625/* connect == the TTY link is open */
626
627static void acm_connect(struct gserial *port)
628{
629 struct f_acm *acm = port_to_acm(port);
630
631 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
632 acm_notify_serial_state(acm);
633}
634
635static void acm_disconnect(struct gserial *port)
636{
637 struct f_acm *acm = port_to_acm(port);
638
639 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
640 acm_notify_serial_state(acm);
641}
642
643static int acm_send_break(struct gserial *port, int duration)
644{
645 struct f_acm *acm = port_to_acm(port);
646 u16 state;
647
648 state = acm->serial_state;
649 state &= ~ACM_CTRL_BRK;
650 if (duration)
651 state |= ACM_CTRL_BRK;
652
653 acm->serial_state = state;
654 return acm_notify_serial_state(acm);
655}
656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657static int acm_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
658{
659 struct f_acm *acm = port_to_acm(port);
660
661 acm->serial_state = ctrl_bits;
662
663 return acm_notify_serial_state(acm);
664}
665
David Brownell1f1ba112008-08-06 18:49:57 -0700666/*-------------------------------------------------------------------------*/
667
David Brownell4d5a73d2008-06-19 18:18:40 -0700668/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200669static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700670acm_bind(struct usb_configuration *c, struct usb_function *f)
671{
672 struct usb_composite_dev *cdev = c->cdev;
673 struct f_acm *acm = func_to_acm(f);
674 int status;
675 struct usb_ep *ep;
676
677 /* allocate instance-specific interface IDs, and patch descriptors */
678 status = usb_interface_id(c, f);
679 if (status < 0)
680 goto fail;
681 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100682 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700683
684 acm_control_interface_desc.bInterfaceNumber = status;
685 acm_union_desc .bMasterInterface0 = status;
686
687 status = usb_interface_id(c, f);
688 if (status < 0)
689 goto fail;
690 acm->data_id = status;
691
692 acm_data_interface_desc.bInterfaceNumber = status;
693 acm_union_desc.bSlaveInterface0 = status;
694 acm_call_mgmt_descriptor.bDataInterface = status;
695
696 status = -ENODEV;
697
698 /* allocate instance-specific endpoints */
699 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
700 if (!ep)
701 goto fail;
702 acm->port.in = ep;
703 ep->driver_data = cdev; /* claim */
704
705 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
706 if (!ep)
707 goto fail;
708 acm->port.out = ep;
709 ep->driver_data = cdev; /* claim */
710
711 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
712 if (!ep)
713 goto fail;
714 acm->notify = ep;
715 ep->driver_data = cdev; /* claim */
716
David Brownell1f1ba112008-08-06 18:49:57 -0700717 /* allocate notification */
718 acm->notify_req = gs_alloc_req(ep,
719 sizeof(struct usb_cdc_notification) + 2,
720 GFP_KERNEL);
721 if (!acm->notify_req)
722 goto fail;
723
724 acm->notify_req->complete = acm_cdc_notify_complete;
725 acm->notify_req->context = acm;
726
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200727 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700728 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700729 if (!f->descriptors)
730 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700731
David Brownell4d5a73d2008-06-19 18:18:40 -0700732 /* support all relevant hardware speeds... we expect that when
733 * hardware is dual speed, all bulk-capable endpoints work at
734 * both speeds
735 */
736 if (gadget_is_dualspeed(c->cdev->gadget)) {
737 acm_hs_in_desc.bEndpointAddress =
738 acm_fs_in_desc.bEndpointAddress;
739 acm_hs_out_desc.bEndpointAddress =
740 acm_fs_out_desc.bEndpointAddress;
741 acm_hs_notify_desc.bEndpointAddress =
742 acm_fs_notify_desc.bEndpointAddress;
743
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200744 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700745 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530746 if (!f->hs_descriptors)
747 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700748 }
749
David Brownell4d5a73d2008-06-19 18:18:40 -0700750 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
751 acm->port_num,
752 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
753 acm->port.in->name, acm->port.out->name,
754 acm->notify->name);
755 return 0;
756
757fail:
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530758 if (f->hs_descriptors)
759 usb_free_descriptors(f->hs_descriptors);
760 if (f->descriptors)
761 usb_free_descriptors(f->descriptors);
762
David Brownell1f1ba112008-08-06 18:49:57 -0700763 if (acm->notify_req)
764 gs_free_req(acm->notify, acm->notify_req);
765
David Brownell4d5a73d2008-06-19 18:18:40 -0700766 /* we might as well release our claims on endpoints */
767 if (acm->notify)
768 acm->notify->driver_data = NULL;
769 if (acm->port.out)
770 acm->port.out->driver_data = NULL;
771 if (acm->port.in)
772 acm->port.in->driver_data = NULL;
773
774 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
775
776 return status;
777}
778
779static void
780acm_unbind(struct usb_configuration *c, struct usb_function *f)
781{
David Brownell1f1ba112008-08-06 18:49:57 -0700782 struct f_acm *acm = func_to_acm(f);
783
David Brownell4d5a73d2008-06-19 18:18:40 -0700784 if (gadget_is_dualspeed(c->cdev->gadget))
785 usb_free_descriptors(f->hs_descriptors);
786 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700787 gs_free_req(acm->notify, acm->notify_req);
John Michelau677ba872010-11-08 18:05:37 -0600788 kfree(acm->port.func.name);
David Brownell1f1ba112008-08-06 18:49:57 -0700789 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700790}
791
792/* Some controllers can't support CDC ACM ... */
793static inline bool can_support_cdc(struct usb_configuration *c)
794{
David Brownell4d5a73d2008-06-19 18:18:40 -0700795 /* everything else is *probably* fine ... */
796 return true;
797}
798
799/**
800 * acm_bind_config - add a CDC ACM function to a configuration
801 * @c: the configuration to support the CDC ACM instance
802 * @port_num: /dev/ttyGS* port this interface will use
803 * Context: single threaded during gadget setup
804 *
805 * Returns zero on success, else negative errno.
806 *
807 * Caller must have called @gserial_setup() with enough ports to
808 * handle all the ones it binds. Caller is also responsible
809 * for calling @gserial_cleanup() before module unload.
810 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200811int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700812{
813 struct f_acm *acm;
814 int status;
815
816 if (!can_support_cdc(c))
817 return -EINVAL;
818
819 /* REVISIT might want instance-specific strings to help
820 * distinguish instances ...
821 */
822
823 /* maybe allocate device-global string IDs, and patch descriptors */
824 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
825 status = usb_string_id(c->cdev);
826 if (status < 0)
827 return status;
828 acm_string_defs[ACM_CTRL_IDX].id = status;
829
830 acm_control_interface_desc.iInterface = status;
831
832 status = usb_string_id(c->cdev);
833 if (status < 0)
834 return status;
835 acm_string_defs[ACM_DATA_IDX].id = status;
836
837 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100838
839 status = usb_string_id(c->cdev);
840 if (status < 0)
841 return status;
842 acm_string_defs[ACM_IAD_IDX].id = status;
843
844 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700845 }
846
847 /* allocate and initialize one new instance */
848 acm = kzalloc(sizeof *acm, GFP_KERNEL);
849 if (!acm)
850 return -ENOMEM;
851
David Brownell1f1ba112008-08-06 18:49:57 -0700852 spin_lock_init(&acm->lock);
853
David Brownell4d5a73d2008-06-19 18:18:40 -0700854 acm->port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 acm->transport = gacm_ports[port_num].transport;
David Brownell4d5a73d2008-06-19 18:18:40 -0700856
David Brownell1f1ba112008-08-06 18:49:57 -0700857 acm->port.connect = acm_connect;
858 acm->port.disconnect = acm_disconnect;
859 acm->port.send_break = acm_send_break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 acm->port.send_modem_ctrl_bits = acm_send_modem_ctrl_bits;
David Brownell1f1ba112008-08-06 18:49:57 -0700861
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num + 1);
John Michelau677ba872010-11-08 18:05:37 -0600863 if (!acm->port.func.name) {
864 kfree(acm);
865 return -ENOMEM;
866 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700867 acm->port.func.strings = acm_strings;
868 /* descriptors are per-instance copies */
869 acm->port.func.bind = acm_bind;
870 acm->port.func.unbind = acm_unbind;
871 acm->port.func.set_alt = acm_set_alt;
872 acm->port.func.setup = acm_setup;
873 acm->port.func.disable = acm_disable;
874
875 status = usb_add_function(c, &acm->port.func);
876 if (status)
877 kfree(acm);
878 return status;
879}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880
Anji jonnala92be1b42011-12-19 09:44:41 +0530881/**
882 * acm_init_port - bind a acm_port to its transport
883 */
884static int acm_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885{
Anji jonnala92be1b42011-12-19 09:44:41 +0530886 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
Anji jonnala92be1b42011-12-19 09:44:41 +0530888 if (port_num >= GSERIAL_NO_PORTS)
889 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
Anji jonnala92be1b42011-12-19 09:44:41 +0530891 transport = str_to_xport(name);
892 pr_debug("%s, port:%d, transport:%s\n", __func__,
893 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894
Anji jonnala92be1b42011-12-19 09:44:41 +0530895 gacm_ports[port_num].transport = transport;
896 gacm_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
Anji jonnala92be1b42011-12-19 09:44:41 +0530898 switch (transport) {
899 case USB_GADGET_XPORT_TTY:
900 gacm_ports[port_num].client_port_num = no_acm_tty_ports;
901 no_acm_tty_ports++;
902 break;
903 case USB_GADGET_XPORT_SDIO:
904 gacm_ports[port_num].client_port_num = no_acm_sdio_ports;
905 no_acm_sdio_ports++;
906 break;
907 case USB_GADGET_XPORT_SMD:
908 gacm_ports[port_num].client_port_num = no_acm_smd_ports;
909 no_acm_smd_ports++;
910 break;
911 default:
912 pr_err("%s: Un-supported transport transport: %u\n",
913 __func__, gacm_ports[port_num].transport);
914 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 }
916
Anji jonnala92be1b42011-12-19 09:44:41 +0530917 nr_acm_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918
919 return 0;
920}