blob: e4e81fa85b91e75466a04d3e2c6a11274a33fe71 [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
Duy Truonge833aca2013-02-12 13:35:08 -08008 * Copyright (c) 2011 The Linux Foundation. All rights reserved.
Michal Nazarewicz54b83602012-01-13 15:05:16 +01009 * Author: Michal Nazarewicz (mina86@mina86.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;
Devin Kim192986b2012-06-20 08:37:37 -070084static unsigned int no_acm_hsic_sports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085
Anji jonnala92be1b42011-12-19 09:44:41 +053086static struct acm_port_info {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 enum transport_type transport;
88 unsigned port_num;
89 unsigned client_port_num;
90} gacm_ports[GSERIAL_NO_PORTS];
91
David Brownell4d5a73d2008-06-19 18:18:40 -070092static inline struct f_acm *func_to_acm(struct usb_function *f)
93{
94 return container_of(f, struct f_acm, port.func);
95}
96
David Brownell1f1ba112008-08-06 18:49:57 -070097static inline struct f_acm *port_to_acm(struct gserial *p)
98{
99 return container_of(p, struct f_acm, port);
100}
101
Anji jonnala92be1b42011-12-19 09:44:41 +0530102static int acm_port_setup(struct usb_configuration *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103{
104 int ret = 0;
Devin Kim192986b2012-06-20 08:37:37 -0700105 int port_idx=0;
106 int i=0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107
Anji jonnala92be1b42011-12-19 09:44:41 +0530108 pr_debug("%s: no_acm_tty_ports:%u no_acm_sdio_ports: %u nr_acm_ports:%u\n",
109 __func__, no_acm_tty_ports, no_acm_sdio_ports,
110 nr_acm_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111
Anji jonnala92be1b42011-12-19 09:44:41 +0530112 if (no_acm_tty_ports)
113 ret = gserial_setup(c->cdev->gadget, no_acm_tty_ports);
114 if (no_acm_sdio_ports)
115 ret = gsdio_setup(c->cdev->gadget, no_acm_sdio_ports);
116 if (no_acm_smd_ports)
117 ret = gsmd_setup(c->cdev->gadget, no_acm_smd_ports);
Devin Kim192986b2012-06-20 08:37:37 -0700118 if (no_acm_hsic_sports) {
119 port_idx = ghsic_data_setup(no_acm_hsic_sports,
120 USB_GADGET_SERIAL);
121 if (port_idx < 0)
122 return port_idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123
Devin Kim192986b2012-06-20 08:37:37 -0700124 for (i = 0; i < nr_acm_ports; i++) {
125 if (gacm_ports[i].transport ==
126 USB_GADGET_XPORT_HSIC) {
127 gacm_ports[i].client_port_num = port_idx;
128 port_idx++;
129 }
130 }
131
132 /*clinet port num is same for data setup and ctrl setup*/
133 ret = ghsic_ctrl_setup(no_acm_hsic_sports, USB_GADGET_SERIAL);
134 if (ret < 0)
135 return ret;
136 return 0;
137 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 return ret;
139}
140
Anji jonnala92be1b42011-12-19 09:44:41 +0530141static int acm_port_connect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142{
143 unsigned port_num;
Devin Kim192986b2012-06-20 08:37:37 -0700144 int ret=0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145
146 port_num = gacm_ports[acm->port_num].client_port_num;
147
148
149 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 -0700150 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 acm, &acm->port, acm->port_num, port_num);
152
153 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530154 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 gserial_connect(&acm->port, port_num);
156 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530157 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 gsdio_connect(&acm->port, port_num);
159 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530160 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 gsmd_connect(&acm->port, port_num);
162 break;
Devin Kim192986b2012-06-20 08:37:37 -0700163 case USB_GADGET_XPORT_HSIC:
164 ret = ghsic_ctrl_connect(&acm->port, port_num);
165 if (ret) {
166 pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
167 __func__, ret);
168 return ret;
169 }
170 ret = ghsic_data_connect(&acm->port, port_num);
171 if (ret) {
172 pr_err("%s: ghsic_data_connect failed: err:%d\n",
173 __func__, ret);
174 ghsic_ctrl_disconnect(&acm->port, port_num);
175 return ret;
176 }
177 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 default:
179 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700180 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 return -ENODEV;
182 }
183
184 return 0;
185}
186
Anji jonnala92be1b42011-12-19 09:44:41 +0530187static int acm_port_disconnect(struct f_acm *acm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188{
189 unsigned port_num;
190
191 port_num = gacm_ports[acm->port_num].client_port_num;
192
193 pr_debug("%s: transport:%s f_acm:%p gserial:%p port_num:%d cl_pno:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700194 __func__, xport_to_str(acm->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 acm, &acm->port, acm->port_num, port_num);
196
197 switch (acm->transport) {
Anji jonnala92be1b42011-12-19 09:44:41 +0530198 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 gserial_disconnect(&acm->port);
200 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530201 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 gsdio_disconnect(&acm->port, port_num);
203 break;
Anji jonnala92be1b42011-12-19 09:44:41 +0530204 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 gsmd_disconnect(&acm->port, port_num);
206 break;
Devin Kim192986b2012-06-20 08:37:37 -0700207 case USB_GADGET_XPORT_HSIC:
208 ghsic_ctrl_disconnect(&acm->port, port_num);
209 ghsic_data_disconnect(&acm->port, port_num);
210 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 default:
212 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700213 xport_to_str(acm->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 return -ENODEV;
215 }
216
217 return 0;
218}
David Brownell4d5a73d2008-06-19 18:18:40 -0700219/*-------------------------------------------------------------------------*/
220
221/* notification endpoint uses smallish and infrequent fixed-size messages */
222
223#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
Devin Kim2bdc71b2012-06-20 08:33:44 -0700224#define GS_NOTIFY_MAXPACKET 16
David Brownell4d5a73d2008-06-19 18:18:40 -0700225
226/* interface and class descriptors: */
227
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100228static struct usb_interface_assoc_descriptor
229acm_iad_descriptor = {
230 .bLength = sizeof acm_iad_descriptor,
231 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
232
233 /* .bFirstInterface = DYNAMIC, */
234 .bInterfaceCount = 2, // control + data
235 .bFunctionClass = USB_CLASS_COMM,
236 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530237 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100238 /* .iFunction = DYNAMIC */
239};
240
241
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200242static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700243 .bLength = USB_DT_INTERFACE_SIZE,
244 .bDescriptorType = USB_DT_INTERFACE,
245 /* .bInterfaceNumber = DYNAMIC */
246 .bNumEndpoints = 1,
247 .bInterfaceClass = USB_CLASS_COMM,
248 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
249 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
250 /* .iInterface = DYNAMIC */
251};
252
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200253static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700254 .bLength = USB_DT_INTERFACE_SIZE,
255 .bDescriptorType = USB_DT_INTERFACE,
256 /* .bInterfaceNumber = DYNAMIC */
257 .bNumEndpoints = 2,
258 .bInterfaceClass = USB_CLASS_CDC_DATA,
259 .bInterfaceSubClass = 0,
260 .bInterfaceProtocol = 0,
261 /* .iInterface = DYNAMIC */
262};
263
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200264static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700265 .bLength = sizeof(acm_header_desc),
266 .bDescriptorType = USB_DT_CS_INTERFACE,
267 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800268 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700269};
270
271static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200272acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700273 .bLength = sizeof(acm_call_mgmt_descriptor),
274 .bDescriptorType = USB_DT_CS_INTERFACE,
275 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
276 .bmCapabilities = 0,
277 /* .bDataInterface = DYNAMIC */
278};
279
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200280static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700281 .bLength = sizeof(acm_descriptor),
282 .bDescriptorType = USB_DT_CS_INTERFACE,
283 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700284 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700285};
286
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200287static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700288 .bLength = sizeof(acm_union_desc),
289 .bDescriptorType = USB_DT_CS_INTERFACE,
290 .bDescriptorSubType = USB_CDC_UNION_TYPE,
291 /* .bMasterInterface0 = DYNAMIC */
292 /* .bSlaveInterface0 = DYNAMIC */
293};
294
295/* full speed support: */
296
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200297static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700298 .bLength = USB_DT_ENDPOINT_SIZE,
299 .bDescriptorType = USB_DT_ENDPOINT,
300 .bEndpointAddress = USB_DIR_IN,
301 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800302 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700303 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
304};
305
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200306static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
309 .bEndpointAddress = USB_DIR_IN,
310 .bmAttributes = USB_ENDPOINT_XFER_BULK,
311};
312
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200313static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316 .bEndpointAddress = USB_DIR_OUT,
317 .bmAttributes = USB_ENDPOINT_XFER_BULK,
318};
319
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200320static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100321 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700322 (struct usb_descriptor_header *) &acm_control_interface_desc,
323 (struct usb_descriptor_header *) &acm_header_desc,
324 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
325 (struct usb_descriptor_header *) &acm_descriptor,
326 (struct usb_descriptor_header *) &acm_union_desc,
327 (struct usb_descriptor_header *) &acm_fs_notify_desc,
328 (struct usb_descriptor_header *) &acm_data_interface_desc,
329 (struct usb_descriptor_header *) &acm_fs_in_desc,
330 (struct usb_descriptor_header *) &acm_fs_out_desc,
331 NULL,
332};
333
334/* high speed support: */
335
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200336static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700337 .bLength = USB_DT_ENDPOINT_SIZE,
338 .bDescriptorType = USB_DT_ENDPOINT,
339 .bEndpointAddress = USB_DIR_IN,
340 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800341 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
David Brownell4d5a73d2008-06-19 18:18:40 -0700342 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
343};
344
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200345static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700346 .bLength = USB_DT_ENDPOINT_SIZE,
347 .bDescriptorType = USB_DT_ENDPOINT,
348 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800349 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700350};
351
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200352static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700353 .bLength = USB_DT_ENDPOINT_SIZE,
354 .bDescriptorType = USB_DT_ENDPOINT,
355 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800356 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700357};
358
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200359static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100360 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700361 (struct usb_descriptor_header *) &acm_control_interface_desc,
362 (struct usb_descriptor_header *) &acm_header_desc,
363 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
364 (struct usb_descriptor_header *) &acm_descriptor,
365 (struct usb_descriptor_header *) &acm_union_desc,
366 (struct usb_descriptor_header *) &acm_hs_notify_desc,
367 (struct usb_descriptor_header *) &acm_data_interface_desc,
368 (struct usb_descriptor_header *) &acm_hs_in_desc,
369 (struct usb_descriptor_header *) &acm_hs_out_desc,
370 NULL,
371};
372
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100373static struct usb_endpoint_descriptor acm_ss_in_desc = {
374 .bLength = USB_DT_ENDPOINT_SIZE,
375 .bDescriptorType = USB_DT_ENDPOINT,
376 .bmAttributes = USB_ENDPOINT_XFER_BULK,
377 .wMaxPacketSize = cpu_to_le16(1024),
378};
379
380static struct usb_endpoint_descriptor acm_ss_out_desc = {
381 .bLength = USB_DT_ENDPOINT_SIZE,
382 .bDescriptorType = USB_DT_ENDPOINT,
383 .bmAttributes = USB_ENDPOINT_XFER_BULK,
384 .wMaxPacketSize = cpu_to_le16(1024),
385};
386
387static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
388 .bLength = sizeof acm_ss_bulk_comp_desc,
389 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
390};
391
392static struct usb_descriptor_header *acm_ss_function[] = {
393 (struct usb_descriptor_header *) &acm_iad_descriptor,
394 (struct usb_descriptor_header *) &acm_control_interface_desc,
395 (struct usb_descriptor_header *) &acm_header_desc,
396 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
397 (struct usb_descriptor_header *) &acm_descriptor,
398 (struct usb_descriptor_header *) &acm_union_desc,
399 (struct usb_descriptor_header *) &acm_hs_notify_desc,
400 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
401 (struct usb_descriptor_header *) &acm_data_interface_desc,
402 (struct usb_descriptor_header *) &acm_ss_in_desc,
403 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
404 (struct usb_descriptor_header *) &acm_ss_out_desc,
405 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
406 NULL,
407};
408
David Brownell4d5a73d2008-06-19 18:18:40 -0700409/* string descriptors: */
410
411#define ACM_CTRL_IDX 0
412#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100413#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700414
415/* static strings, in UTF-8 */
416static struct usb_string acm_string_defs[] = {
417 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
418 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100419 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700420 { /* ZEROES END LIST */ },
421};
422
423static struct usb_gadget_strings acm_string_table = {
424 .language = 0x0409, /* en-us */
425 .strings = acm_string_defs,
426};
427
428static struct usb_gadget_strings *acm_strings[] = {
429 &acm_string_table,
430 NULL,
431};
432
433/*-------------------------------------------------------------------------*/
434
435/* ACM control ... data handling is delegated to tty library code.
436 * The main task of this function is to activate and deactivate
437 * that code based on device state; track parameters like line
438 * speed, handshake state, and so on; and issue notifications.
439 */
440
441static void acm_complete_set_line_coding(struct usb_ep *ep,
442 struct usb_request *req)
443{
444 struct f_acm *acm = ep->driver_data;
445 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
446
447 if (req->status != 0) {
448 DBG(cdev, "acm ttyGS%d completion, err %d\n",
449 acm->port_num, req->status);
450 return;
451 }
452
453 /* normal completion */
454 if (req->actual != sizeof(acm->port_line_coding)) {
455 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
456 acm->port_num, req->actual);
457 usb_ep_set_halt(ep);
458 } else {
459 struct usb_cdc_line_coding *value = req->buf;
460
461 /* REVISIT: we currently just remember this data.
462 * If we change that, (a) validate it first, then
463 * (b) update whatever hardware needs updating,
464 * (c) worry about locking. This is information on
465 * the order of 9600-8-N-1 ... most of which means
466 * nothing unless we control a real RS232 line.
467 */
468 acm->port_line_coding = *value;
469 }
470}
471
472static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
473{
474 struct f_acm *acm = func_to_acm(f);
475 struct usb_composite_dev *cdev = f->config->cdev;
476 struct usb_request *req = cdev->req;
477 int value = -EOPNOTSUPP;
478 u16 w_index = le16_to_cpu(ctrl->wIndex);
479 u16 w_value = le16_to_cpu(ctrl->wValue);
480 u16 w_length = le16_to_cpu(ctrl->wLength);
481
482 /* composite driver infrastructure handles everything except
483 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700484 *
485 * Note CDC spec table 4 lists the ACM request profile. It requires
486 * encapsulated command support ... we don't handle any, and respond
487 * to them by stalling. Options include get/set/clear comm features
488 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700489 */
490 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
491
492 /* SET_LINE_CODING ... just read and save what the host sends */
493 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
494 | USB_CDC_REQ_SET_LINE_CODING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 if (w_length != sizeof(struct usb_cdc_line_coding))
David Brownell4d5a73d2008-06-19 18:18:40 -0700496 goto invalid;
497
498 value = w_length;
499 cdev->gadget->ep0->driver_data = acm;
500 req->complete = acm_complete_set_line_coding;
501 break;
502
503 /* GET_LINE_CODING ... return what host sent, or initial value */
504 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
505 | USB_CDC_REQ_GET_LINE_CODING:
David Brownell4d5a73d2008-06-19 18:18:40 -0700506
507 value = min_t(unsigned, w_length,
508 sizeof(struct usb_cdc_line_coding));
509 memcpy(req->buf, &acm->port_line_coding, value);
510 break;
511
512 /* SET_CONTROL_LINE_STATE ... save what the host sent */
513 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
514 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
David Brownell4d5a73d2008-06-19 18:18:40 -0700515 value = 0;
516
517 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700518 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700519 * that bit, we should return to that no-flow state.
520 */
521 acm->port_handshake_bits = w_value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 if (acm->port.notify_modem) {
523 unsigned port_num =
524 gacm_ports[acm->port_num].client_port_num;
525
526 acm->port.notify_modem(&acm->port, port_num, w_value);
527 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700528 break;
529
530 default:
531invalid:
532 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
533 ctrl->bRequestType, ctrl->bRequest,
534 w_value, w_index, w_length);
535 }
536
537 /* respond with data transfer or status phase? */
538 if (value >= 0) {
539 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
540 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
541 w_value, w_index, w_length);
542 req->zero = 0;
543 req->length = value;
544 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
545 if (value < 0)
546 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
547 acm->port_num, value);
548 }
549
550 /* device either stalls (value < 0) or reports success */
551 return value;
552}
553
554static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
555{
556 struct f_acm *acm = func_to_acm(f);
557 struct usb_composite_dev *cdev = f->config->cdev;
558
559 /* we know alt == 0, so this is an activation or a reset */
560
561 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700562 if (acm->notify->driver_data) {
563 VDBG(cdev, "reset acm control interface %d\n", intf);
564 usb_ep_disable(acm->notify);
565 } else {
566 VDBG(cdev, "init acm ctrl interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700567 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200568 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
569 return -EINVAL;
570
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300571 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700572 acm->notify->driver_data = acm;
573
574 } else if (intf == acm->data_id) {
575 if (acm->port.in->driver_data) {
576 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530577 acm_port_disconnect(acm);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300578 }
579 if (!acm->port.in->desc || !acm->port.out->desc) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700580 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300581 if (config_ep_by_speed(cdev->gadget, f,
582 acm->port.in) ||
583 config_ep_by_speed(cdev->gadget, f,
584 acm->port.out)) {
585 acm->port.in->desc = NULL;
586 acm->port.out->desc = NULL;
587 return -EINVAL;
588 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700589 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200590 if (config_ep_by_speed(cdev->gadget, f,
591 acm->port.in) ||
592 config_ep_by_speed(cdev->gadget, f,
593 acm->port.out)) {
594 acm->port.in->desc = NULL;
595 acm->port.out->desc = NULL;
596 return -EINVAL;
597 }
598
Anji jonnala92be1b42011-12-19 09:44:41 +0530599 acm_port_connect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700600
601 } else
602 return -EINVAL;
603
604 return 0;
605}
606
607static void acm_disable(struct usb_function *f)
608{
609 struct f_acm *acm = func_to_acm(f);
610 struct usb_composite_dev *cdev = f->config->cdev;
611
612 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530613 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700614 usb_ep_disable(acm->notify);
615 acm->notify->driver_data = NULL;
616}
617
618/*-------------------------------------------------------------------------*/
619
David Brownell1f1ba112008-08-06 18:49:57 -0700620/**
621 * acm_cdc_notify - issue CDC notification to host
622 * @acm: wraps host to be notified
623 * @type: notification type
624 * @value: Refer to cdc specs, wValue field.
625 * @data: data to be sent
626 * @length: size of data
627 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
628 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200629 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700630 *
631 * See section 6.3.5 of the CDC 1.1 specification for information
632 * about the only notification we issue: SerialState change.
633 */
634static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
635 void *data, unsigned length)
636{
637 struct usb_ep *ep = acm->notify;
638 struct usb_request *req;
639 struct usb_cdc_notification *notify;
David Brownell1f1ba112008-08-06 18:49:57 -0700640 void *buf;
641 int status;
Devin Kim192986b2012-06-20 08:37:37 -0700642 unsigned char noti_buf[GS_NOTIFY_MAXPACKET];
643
644 memset(noti_buf, 0, GS_NOTIFY_MAXPACKET);
David Brownell1f1ba112008-08-06 18:49:57 -0700645
646 req = acm->notify_req;
647 acm->notify_req = NULL;
648 acm->pending = false;
649
Devin Kim192986b2012-06-20 08:37:37 -0700650 req->length = GS_NOTIFY_MAXPACKET;
David Brownell1f1ba112008-08-06 18:49:57 -0700651 notify = req->buf;
652 buf = notify + 1;
653
654 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
655 | USB_RECIP_INTERFACE;
656 notify->bNotificationType = type;
657 notify->wValue = cpu_to_le16(value);
658 notify->wIndex = cpu_to_le16(acm->ctrl_id);
659 notify->wLength = cpu_to_le16(length);
Devin Kim192986b2012-06-20 08:37:37 -0700660 memcpy(noti_buf, data, length);
661 memcpy(buf, noti_buf, GS_NOTIFY_MAXPACKET);
David Brownell1f1ba112008-08-06 18:49:57 -0700662 memcpy(buf, data, length);
663
David Brownelle50ae572008-11-12 11:35:13 -0800664 /* ep_queue() can complete immediately if it fills the fifo... */
665 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700666 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800667 spin_lock(&acm->lock);
668
David Brownell1f1ba112008-08-06 18:49:57 -0700669 if (status < 0) {
670 ERROR(acm->port.func.config->cdev,
671 "acm ttyGS%d can't notify serial state, %d\n",
672 acm->port_num, status);
673 acm->notify_req = req;
674 }
675
676 return status;
677}
678
679static int acm_notify_serial_state(struct f_acm *acm)
680{
681 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
682 int status;
683
684 spin_lock(&acm->lock);
685 if (acm->notify_req) {
686 DBG(cdev, "acm ttyGS%d serial state %04x\n",
687 acm->port_num, acm->serial_state);
688 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
689 0, &acm->serial_state, sizeof(acm->serial_state));
690 } else {
691 acm->pending = true;
692 status = 0;
693 }
694 spin_unlock(&acm->lock);
695 return status;
696}
697
698static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
699{
700 struct f_acm *acm = req->context;
701 u8 doit = false;
702
703 /* on this call path we do NOT hold the port spinlock,
704 * which is why ACM needs its own spinlock
705 */
706 spin_lock(&acm->lock);
707 if (req->status != -ESHUTDOWN)
708 doit = acm->pending;
709 acm->notify_req = req;
710 spin_unlock(&acm->lock);
711
712 if (doit)
713 acm_notify_serial_state(acm);
714}
715
716/* connect == the TTY link is open */
717
718static void acm_connect(struct gserial *port)
719{
720 struct f_acm *acm = port_to_acm(port);
721
722 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
723 acm_notify_serial_state(acm);
724}
725
726static void acm_disconnect(struct gserial *port)
727{
728 struct f_acm *acm = port_to_acm(port);
729
730 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
731 acm_notify_serial_state(acm);
732}
733
734static int acm_send_break(struct gserial *port, int duration)
735{
736 struct f_acm *acm = port_to_acm(port);
737 u16 state;
738
739 state = acm->serial_state;
740 state &= ~ACM_CTRL_BRK;
741 if (duration)
742 state |= ACM_CTRL_BRK;
743
744 acm->serial_state = state;
745 return acm_notify_serial_state(acm);
746}
747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748static int acm_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
749{
750 struct f_acm *acm = port_to_acm(port);
751
752 acm->serial_state = ctrl_bits;
753
754 return acm_notify_serial_state(acm);
755}
756
David Brownell1f1ba112008-08-06 18:49:57 -0700757/*-------------------------------------------------------------------------*/
758
David Brownell4d5a73d2008-06-19 18:18:40 -0700759/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200760static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700761acm_bind(struct usb_configuration *c, struct usb_function *f)
762{
763 struct usb_composite_dev *cdev = c->cdev;
764 struct f_acm *acm = func_to_acm(f);
765 int status;
766 struct usb_ep *ep;
767
768 /* allocate instance-specific interface IDs, and patch descriptors */
769 status = usb_interface_id(c, f);
770 if (status < 0)
771 goto fail;
772 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100773 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700774
775 acm_control_interface_desc.bInterfaceNumber = status;
776 acm_union_desc .bMasterInterface0 = status;
777
778 status = usb_interface_id(c, f);
779 if (status < 0)
780 goto fail;
781 acm->data_id = status;
782
783 acm_data_interface_desc.bInterfaceNumber = status;
784 acm_union_desc.bSlaveInterface0 = status;
785 acm_call_mgmt_descriptor.bDataInterface = status;
786
787 status = -ENODEV;
788
789 /* allocate instance-specific endpoints */
790 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
791 if (!ep)
792 goto fail;
793 acm->port.in = ep;
794 ep->driver_data = cdev; /* claim */
795
796 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
797 if (!ep)
798 goto fail;
799 acm->port.out = ep;
800 ep->driver_data = cdev; /* claim */
801
802 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
803 if (!ep)
804 goto fail;
805 acm->notify = ep;
806 ep->driver_data = cdev; /* claim */
807
David Brownell1f1ba112008-08-06 18:49:57 -0700808 /* allocate notification */
809 acm->notify_req = gs_alloc_req(ep,
810 sizeof(struct usb_cdc_notification) + 2,
811 GFP_KERNEL);
812 if (!acm->notify_req)
813 goto fail;
814
815 acm->notify_req->complete = acm_cdc_notify_complete;
816 acm->notify_req->context = acm;
817
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300818 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700819 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700820 if (!f->descriptors)
821 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700822
David Brownell4d5a73d2008-06-19 18:18:40 -0700823 /* support all relevant hardware speeds... we expect that when
824 * hardware is dual speed, all bulk-capable endpoints work at
825 * both speeds
826 */
827 if (gadget_is_dualspeed(c->cdev->gadget)) {
828 acm_hs_in_desc.bEndpointAddress =
829 acm_fs_in_desc.bEndpointAddress;
830 acm_hs_out_desc.bEndpointAddress =
831 acm_fs_out_desc.bEndpointAddress;
832 acm_hs_notify_desc.bEndpointAddress =
833 acm_fs_notify_desc.bEndpointAddress;
834
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300835 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700836 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530837 if (!f->hs_descriptors)
838 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700839 }
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100840 if (gadget_is_superspeed(c->cdev->gadget)) {
841 acm_ss_in_desc.bEndpointAddress =
842 acm_fs_in_desc.bEndpointAddress;
843 acm_ss_out_desc.bEndpointAddress =
844 acm_fs_out_desc.bEndpointAddress;
845
846 /* copy descriptors, and track endpoint copies */
847 f->ss_descriptors = usb_copy_descriptors(acm_ss_function);
848 if (!f->ss_descriptors)
849 goto fail;
850 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700851
David Brownell4d5a73d2008-06-19 18:18:40 -0700852 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
853 acm->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100854 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell4d5a73d2008-06-19 18:18:40 -0700855 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
856 acm->port.in->name, acm->port.out->name,
857 acm->notify->name);
858 return 0;
859
860fail:
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530861 if (f->hs_descriptors)
862 usb_free_descriptors(f->hs_descriptors);
863 if (f->descriptors)
864 usb_free_descriptors(f->descriptors);
865
David Brownell1f1ba112008-08-06 18:49:57 -0700866 if (acm->notify_req)
867 gs_free_req(acm->notify, acm->notify_req);
868
David Brownell4d5a73d2008-06-19 18:18:40 -0700869 /* we might as well release our claims on endpoints */
870 if (acm->notify)
871 acm->notify->driver_data = NULL;
872 if (acm->port.out)
873 acm->port.out->driver_data = NULL;
874 if (acm->port.in)
875 acm->port.in->driver_data = NULL;
876
877 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
878
879 return status;
880}
881
882static void
883acm_unbind(struct usb_configuration *c, struct usb_function *f)
884{
David Brownell1f1ba112008-08-06 18:49:57 -0700885 struct f_acm *acm = func_to_acm(f);
886
David Brownell4d5a73d2008-06-19 18:18:40 -0700887 if (gadget_is_dualspeed(c->cdev->gadget))
888 usb_free_descriptors(f->hs_descriptors);
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100889 if (gadget_is_superspeed(c->cdev->gadget))
890 usb_free_descriptors(f->ss_descriptors);
David Brownell4d5a73d2008-06-19 18:18:40 -0700891 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700892 gs_free_req(acm->notify, acm->notify_req);
John Michelau677ba872010-11-08 18:05:37 -0600893 kfree(acm->port.func.name);
David Brownell1f1ba112008-08-06 18:49:57 -0700894 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700895}
896
897/* Some controllers can't support CDC ACM ... */
898static inline bool can_support_cdc(struct usb_configuration *c)
899{
David Brownell4d5a73d2008-06-19 18:18:40 -0700900 /* everything else is *probably* fine ... */
901 return true;
902}
903
904/**
905 * acm_bind_config - add a CDC ACM function to a configuration
906 * @c: the configuration to support the CDC ACM instance
907 * @port_num: /dev/ttyGS* port this interface will use
908 * Context: single threaded during gadget setup
909 *
910 * Returns zero on success, else negative errno.
911 *
912 * Caller must have called @gserial_setup() with enough ports to
913 * handle all the ones it binds. Caller is also responsible
914 * for calling @gserial_cleanup() before module unload.
915 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200916int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700917{
918 struct f_acm *acm;
919 int status;
920
921 if (!can_support_cdc(c))
922 return -EINVAL;
923
924 /* REVISIT might want instance-specific strings to help
925 * distinguish instances ...
926 */
927
928 /* maybe allocate device-global string IDs, and patch descriptors */
929 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
930 status = usb_string_id(c->cdev);
931 if (status < 0)
932 return status;
933 acm_string_defs[ACM_CTRL_IDX].id = status;
934
935 acm_control_interface_desc.iInterface = status;
936
937 status = usb_string_id(c->cdev);
938 if (status < 0)
939 return status;
940 acm_string_defs[ACM_DATA_IDX].id = status;
941
942 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100943
944 status = usb_string_id(c->cdev);
945 if (status < 0)
946 return status;
947 acm_string_defs[ACM_IAD_IDX].id = status;
948
949 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700950 }
951
952 /* allocate and initialize one new instance */
953 acm = kzalloc(sizeof *acm, GFP_KERNEL);
954 if (!acm)
955 return -ENOMEM;
956
David Brownell1f1ba112008-08-06 18:49:57 -0700957 spin_lock_init(&acm->lock);
958
David Brownell4d5a73d2008-06-19 18:18:40 -0700959 acm->port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960 acm->transport = gacm_ports[port_num].transport;
David Brownell4d5a73d2008-06-19 18:18:40 -0700961
David Brownell1f1ba112008-08-06 18:49:57 -0700962 acm->port.connect = acm_connect;
963 acm->port.disconnect = acm_disconnect;
964 acm->port.send_break = acm_send_break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 acm->port.send_modem_ctrl_bits = acm_send_modem_ctrl_bits;
David Brownell1f1ba112008-08-06 18:49:57 -0700966
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num + 1);
John Michelau677ba872010-11-08 18:05:37 -0600968 if (!acm->port.func.name) {
969 kfree(acm);
970 return -ENOMEM;
971 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700972 acm->port.func.strings = acm_strings;
973 /* descriptors are per-instance copies */
974 acm->port.func.bind = acm_bind;
975 acm->port.func.unbind = acm_unbind;
976 acm->port.func.set_alt = acm_set_alt;
977 acm->port.func.setup = acm_setup;
978 acm->port.func.disable = acm_disable;
979
980 status = usb_add_function(c, &acm->port.func);
981 if (status)
982 kfree(acm);
983 return status;
984}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985
Anji jonnala92be1b42011-12-19 09:44:41 +0530986/**
987 * acm_init_port - bind a acm_port to its transport
988 */
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530989static int acm_init_port(int port_num, const char *name, const char *port_name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990{
Anji jonnala92be1b42011-12-19 09:44:41 +0530991 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992
Anji jonnala92be1b42011-12-19 09:44:41 +0530993 if (port_num >= GSERIAL_NO_PORTS)
994 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995
Anji jonnala92be1b42011-12-19 09:44:41 +0530996 transport = str_to_xport(name);
997 pr_debug("%s, port:%d, transport:%s\n", __func__,
998 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999
Anji jonnala92be1b42011-12-19 09:44:41 +05301000 gacm_ports[port_num].transport = transport;
1001 gacm_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002
Anji jonnala92be1b42011-12-19 09:44:41 +05301003 switch (transport) {
1004 case USB_GADGET_XPORT_TTY:
1005 gacm_ports[port_num].client_port_num = no_acm_tty_ports;
1006 no_acm_tty_ports++;
1007 break;
1008 case USB_GADGET_XPORT_SDIO:
1009 gacm_ports[port_num].client_port_num = no_acm_sdio_ports;
1010 no_acm_sdio_ports++;
1011 break;
1012 case USB_GADGET_XPORT_SMD:
1013 gacm_ports[port_num].client_port_num = no_acm_smd_ports;
1014 no_acm_smd_ports++;
1015 break;
Devin Kim192986b2012-06-20 08:37:37 -07001016 case USB_GADGET_XPORT_HSIC:
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +05301017 ghsic_ctrl_set_port_name(port_name, name);
1018 ghsic_data_set_port_name(port_name, name);
1019
Devin Kim192986b2012-06-20 08:37:37 -07001020 /*client port number will be updated in acm_port_setup*/
1021 no_acm_hsic_sports++;
1022 break;
Anji jonnala92be1b42011-12-19 09:44:41 +05301023 default:
1024 pr_err("%s: Un-supported transport transport: %u\n",
1025 __func__, gacm_ports[port_num].transport);
1026 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 }
1028
Anji jonnala92be1b42011-12-19 09:44:41 +05301029 nr_acm_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030
1031 return 0;
1032}