blob: cc151cbb763632814f6c56fbe403dbbf979c9717 [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 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;
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
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100331static struct usb_endpoint_descriptor acm_ss_in_desc = {
332 .bLength = USB_DT_ENDPOINT_SIZE,
333 .bDescriptorType = USB_DT_ENDPOINT,
334 .bmAttributes = USB_ENDPOINT_XFER_BULK,
335 .wMaxPacketSize = cpu_to_le16(1024),
336};
337
338static struct usb_endpoint_descriptor acm_ss_out_desc = {
339 .bLength = USB_DT_ENDPOINT_SIZE,
340 .bDescriptorType = USB_DT_ENDPOINT,
341 .bmAttributes = USB_ENDPOINT_XFER_BULK,
342 .wMaxPacketSize = cpu_to_le16(1024),
343};
344
345static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
346 .bLength = sizeof acm_ss_bulk_comp_desc,
347 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
348};
349
350static struct usb_descriptor_header *acm_ss_function[] = {
351 (struct usb_descriptor_header *) &acm_iad_descriptor,
352 (struct usb_descriptor_header *) &acm_control_interface_desc,
353 (struct usb_descriptor_header *) &acm_header_desc,
354 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
355 (struct usb_descriptor_header *) &acm_descriptor,
356 (struct usb_descriptor_header *) &acm_union_desc,
357 (struct usb_descriptor_header *) &acm_hs_notify_desc,
358 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
359 (struct usb_descriptor_header *) &acm_data_interface_desc,
360 (struct usb_descriptor_header *) &acm_ss_in_desc,
361 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
362 (struct usb_descriptor_header *) &acm_ss_out_desc,
363 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
364 NULL,
365};
366
David Brownell4d5a73d2008-06-19 18:18:40 -0700367/* string descriptors: */
368
369#define ACM_CTRL_IDX 0
370#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100371#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700372
373/* static strings, in UTF-8 */
374static struct usb_string acm_string_defs[] = {
375 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
376 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100377 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700378 { /* ZEROES END LIST */ },
379};
380
381static struct usb_gadget_strings acm_string_table = {
382 .language = 0x0409, /* en-us */
383 .strings = acm_string_defs,
384};
385
386static struct usb_gadget_strings *acm_strings[] = {
387 &acm_string_table,
388 NULL,
389};
390
391/*-------------------------------------------------------------------------*/
392
393/* ACM control ... data handling is delegated to tty library code.
394 * The main task of this function is to activate and deactivate
395 * that code based on device state; track parameters like line
396 * speed, handshake state, and so on; and issue notifications.
397 */
398
399static void acm_complete_set_line_coding(struct usb_ep *ep,
400 struct usb_request *req)
401{
402 struct f_acm *acm = ep->driver_data;
403 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
404
405 if (req->status != 0) {
406 DBG(cdev, "acm ttyGS%d completion, err %d\n",
407 acm->port_num, req->status);
408 return;
409 }
410
411 /* normal completion */
412 if (req->actual != sizeof(acm->port_line_coding)) {
413 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
414 acm->port_num, req->actual);
415 usb_ep_set_halt(ep);
416 } else {
417 struct usb_cdc_line_coding *value = req->buf;
418
419 /* REVISIT: we currently just remember this data.
420 * If we change that, (a) validate it first, then
421 * (b) update whatever hardware needs updating,
422 * (c) worry about locking. This is information on
423 * the order of 9600-8-N-1 ... most of which means
424 * nothing unless we control a real RS232 line.
425 */
426 acm->port_line_coding = *value;
427 }
428}
429
430static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
431{
432 struct f_acm *acm = func_to_acm(f);
433 struct usb_composite_dev *cdev = f->config->cdev;
434 struct usb_request *req = cdev->req;
435 int value = -EOPNOTSUPP;
436 u16 w_index = le16_to_cpu(ctrl->wIndex);
437 u16 w_value = le16_to_cpu(ctrl->wValue);
438 u16 w_length = le16_to_cpu(ctrl->wLength);
439
440 /* composite driver infrastructure handles everything except
441 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700442 *
443 * Note CDC spec table 4 lists the ACM request profile. It requires
444 * encapsulated command support ... we don't handle any, and respond
445 * to them by stalling. Options include get/set/clear comm features
446 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700447 */
448 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
449
450 /* SET_LINE_CODING ... just read and save what the host sends */
451 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
452 | USB_CDC_REQ_SET_LINE_CODING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 if (w_length != sizeof(struct usb_cdc_line_coding))
David Brownell4d5a73d2008-06-19 18:18:40 -0700454 goto invalid;
455
456 value = w_length;
457 cdev->gadget->ep0->driver_data = acm;
458 req->complete = acm_complete_set_line_coding;
459 break;
460
461 /* GET_LINE_CODING ... return what host sent, or initial value */
462 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
463 | USB_CDC_REQ_GET_LINE_CODING:
David Brownell4d5a73d2008-06-19 18:18:40 -0700464
465 value = min_t(unsigned, w_length,
466 sizeof(struct usb_cdc_line_coding));
467 memcpy(req->buf, &acm->port_line_coding, value);
468 break;
469
470 /* SET_CONTROL_LINE_STATE ... save what the host sent */
471 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
472 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
David Brownell4d5a73d2008-06-19 18:18:40 -0700473 value = 0;
474
475 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700476 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700477 * that bit, we should return to that no-flow state.
478 */
479 acm->port_handshake_bits = w_value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 if (acm->port.notify_modem) {
481 unsigned port_num =
482 gacm_ports[acm->port_num].client_port_num;
483
484 acm->port.notify_modem(&acm->port, port_num, w_value);
485 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700486 break;
487
488 default:
489invalid:
490 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
491 ctrl->bRequestType, ctrl->bRequest,
492 w_value, w_index, w_length);
493 }
494
495 /* respond with data transfer or status phase? */
496 if (value >= 0) {
497 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
498 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
499 w_value, w_index, w_length);
500 req->zero = 0;
501 req->length = value;
502 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
503 if (value < 0)
504 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
505 acm->port_num, value);
506 }
507
508 /* device either stalls (value < 0) or reports success */
509 return value;
510}
511
512static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
513{
514 struct f_acm *acm = func_to_acm(f);
515 struct usb_composite_dev *cdev = f->config->cdev;
516
517 /* we know alt == 0, so this is an activation or a reset */
518
519 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700520 if (acm->notify->driver_data) {
521 VDBG(cdev, "reset acm control interface %d\n", intf);
522 usb_ep_disable(acm->notify);
523 } else {
524 VDBG(cdev, "init acm ctrl interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700525 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200526 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
527 return -EINVAL;
528
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300529 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700530 acm->notify->driver_data = acm;
531
532 } else if (intf == acm->data_id) {
533 if (acm->port.in->driver_data) {
534 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530535 acm_port_disconnect(acm);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300536 }
537 if (!acm->port.in->desc || !acm->port.out->desc) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700538 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300539 if (config_ep_by_speed(cdev->gadget, f,
540 acm->port.in) ||
541 config_ep_by_speed(cdev->gadget, f,
542 acm->port.out)) {
543 acm->port.in->desc = NULL;
544 acm->port.out->desc = NULL;
545 return -EINVAL;
546 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700547 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200548 if (config_ep_by_speed(cdev->gadget, f,
549 acm->port.in) ||
550 config_ep_by_speed(cdev->gadget, f,
551 acm->port.out)) {
552 acm->port.in->desc = NULL;
553 acm->port.out->desc = NULL;
554 return -EINVAL;
555 }
556
Anji jonnala92be1b42011-12-19 09:44:41 +0530557 acm_port_connect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700558
559 } else
560 return -EINVAL;
561
562 return 0;
563}
564
565static void acm_disable(struct usb_function *f)
566{
567 struct f_acm *acm = func_to_acm(f);
568 struct usb_composite_dev *cdev = f->config->cdev;
569
570 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
Anji jonnala92be1b42011-12-19 09:44:41 +0530571 acm_port_disconnect(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700572 usb_ep_disable(acm->notify);
573 acm->notify->driver_data = NULL;
574}
575
576/*-------------------------------------------------------------------------*/
577
David Brownell1f1ba112008-08-06 18:49:57 -0700578/**
579 * acm_cdc_notify - issue CDC notification to host
580 * @acm: wraps host to be notified
581 * @type: notification type
582 * @value: Refer to cdc specs, wValue field.
583 * @data: data to be sent
584 * @length: size of data
585 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
586 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200587 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700588 *
589 * See section 6.3.5 of the CDC 1.1 specification for information
590 * about the only notification we issue: SerialState change.
591 */
592static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
593 void *data, unsigned length)
594{
595 struct usb_ep *ep = acm->notify;
596 struct usb_request *req;
597 struct usb_cdc_notification *notify;
598 const unsigned len = sizeof(*notify) + length;
599 void *buf;
600 int status;
601
602 req = acm->notify_req;
603 acm->notify_req = NULL;
604 acm->pending = false;
605
606 req->length = len;
607 notify = req->buf;
608 buf = notify + 1;
609
610 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
611 | USB_RECIP_INTERFACE;
612 notify->bNotificationType = type;
613 notify->wValue = cpu_to_le16(value);
614 notify->wIndex = cpu_to_le16(acm->ctrl_id);
615 notify->wLength = cpu_to_le16(length);
616 memcpy(buf, data, length);
617
David Brownelle50ae572008-11-12 11:35:13 -0800618 /* ep_queue() can complete immediately if it fills the fifo... */
619 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700620 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800621 spin_lock(&acm->lock);
622
David Brownell1f1ba112008-08-06 18:49:57 -0700623 if (status < 0) {
624 ERROR(acm->port.func.config->cdev,
625 "acm ttyGS%d can't notify serial state, %d\n",
626 acm->port_num, status);
627 acm->notify_req = req;
628 }
629
630 return status;
631}
632
633static int acm_notify_serial_state(struct f_acm *acm)
634{
635 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
636 int status;
637
638 spin_lock(&acm->lock);
639 if (acm->notify_req) {
640 DBG(cdev, "acm ttyGS%d serial state %04x\n",
641 acm->port_num, acm->serial_state);
642 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
643 0, &acm->serial_state, sizeof(acm->serial_state));
644 } else {
645 acm->pending = true;
646 status = 0;
647 }
648 spin_unlock(&acm->lock);
649 return status;
650}
651
652static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
653{
654 struct f_acm *acm = req->context;
655 u8 doit = false;
656
657 /* on this call path we do NOT hold the port spinlock,
658 * which is why ACM needs its own spinlock
659 */
660 spin_lock(&acm->lock);
661 if (req->status != -ESHUTDOWN)
662 doit = acm->pending;
663 acm->notify_req = req;
664 spin_unlock(&acm->lock);
665
666 if (doit)
667 acm_notify_serial_state(acm);
668}
669
670/* connect == the TTY link is open */
671
672static void acm_connect(struct gserial *port)
673{
674 struct f_acm *acm = port_to_acm(port);
675
676 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
677 acm_notify_serial_state(acm);
678}
679
680static void acm_disconnect(struct gserial *port)
681{
682 struct f_acm *acm = port_to_acm(port);
683
684 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
685 acm_notify_serial_state(acm);
686}
687
688static int acm_send_break(struct gserial *port, int duration)
689{
690 struct f_acm *acm = port_to_acm(port);
691 u16 state;
692
693 state = acm->serial_state;
694 state &= ~ACM_CTRL_BRK;
695 if (duration)
696 state |= ACM_CTRL_BRK;
697
698 acm->serial_state = state;
699 return acm_notify_serial_state(acm);
700}
701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702static int acm_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
703{
704 struct f_acm *acm = port_to_acm(port);
705
706 acm->serial_state = ctrl_bits;
707
708 return acm_notify_serial_state(acm);
709}
710
David Brownell1f1ba112008-08-06 18:49:57 -0700711/*-------------------------------------------------------------------------*/
712
David Brownell4d5a73d2008-06-19 18:18:40 -0700713/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200714static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700715acm_bind(struct usb_configuration *c, struct usb_function *f)
716{
717 struct usb_composite_dev *cdev = c->cdev;
718 struct f_acm *acm = func_to_acm(f);
719 int status;
720 struct usb_ep *ep;
721
722 /* allocate instance-specific interface IDs, and patch descriptors */
723 status = usb_interface_id(c, f);
724 if (status < 0)
725 goto fail;
726 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100727 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700728
729 acm_control_interface_desc.bInterfaceNumber = status;
730 acm_union_desc .bMasterInterface0 = status;
731
732 status = usb_interface_id(c, f);
733 if (status < 0)
734 goto fail;
735 acm->data_id = status;
736
737 acm_data_interface_desc.bInterfaceNumber = status;
738 acm_union_desc.bSlaveInterface0 = status;
739 acm_call_mgmt_descriptor.bDataInterface = status;
740
741 status = -ENODEV;
742
743 /* allocate instance-specific endpoints */
744 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
745 if (!ep)
746 goto fail;
747 acm->port.in = ep;
748 ep->driver_data = cdev; /* claim */
749
750 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
751 if (!ep)
752 goto fail;
753 acm->port.out = ep;
754 ep->driver_data = cdev; /* claim */
755
756 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
757 if (!ep)
758 goto fail;
759 acm->notify = ep;
760 ep->driver_data = cdev; /* claim */
761
David Brownell1f1ba112008-08-06 18:49:57 -0700762 /* allocate notification */
763 acm->notify_req = gs_alloc_req(ep,
764 sizeof(struct usb_cdc_notification) + 2,
765 GFP_KERNEL);
766 if (!acm->notify_req)
767 goto fail;
768
769 acm->notify_req->complete = acm_cdc_notify_complete;
770 acm->notify_req->context = acm;
771
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300772 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700773 f->descriptors = usb_copy_descriptors(acm_fs_function);
David Brownell1f1ba112008-08-06 18:49:57 -0700774 if (!f->descriptors)
775 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700776
David Brownell4d5a73d2008-06-19 18:18:40 -0700777 /* support all relevant hardware speeds... we expect that when
778 * hardware is dual speed, all bulk-capable endpoints work at
779 * both speeds
780 */
781 if (gadget_is_dualspeed(c->cdev->gadget)) {
782 acm_hs_in_desc.bEndpointAddress =
783 acm_fs_in_desc.bEndpointAddress;
784 acm_hs_out_desc.bEndpointAddress =
785 acm_fs_out_desc.bEndpointAddress;
786 acm_hs_notify_desc.bEndpointAddress =
787 acm_fs_notify_desc.bEndpointAddress;
788
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300789 /* copy descriptors */
David Brownell4d5a73d2008-06-19 18:18:40 -0700790 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530791 if (!f->hs_descriptors)
792 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700793 }
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100794 if (gadget_is_superspeed(c->cdev->gadget)) {
795 acm_ss_in_desc.bEndpointAddress =
796 acm_fs_in_desc.bEndpointAddress;
797 acm_ss_out_desc.bEndpointAddress =
798 acm_fs_out_desc.bEndpointAddress;
799
800 /* copy descriptors, and track endpoint copies */
801 f->ss_descriptors = usb_copy_descriptors(acm_ss_function);
802 if (!f->ss_descriptors)
803 goto fail;
804 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700805
David Brownell4d5a73d2008-06-19 18:18:40 -0700806 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
807 acm->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100808 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell4d5a73d2008-06-19 18:18:40 -0700809 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
810 acm->port.in->name, acm->port.out->name,
811 acm->notify->name);
812 return 0;
813
814fail:
Rajkumar Raghupathyc636cb42012-01-25 16:15:04 +0530815 if (f->hs_descriptors)
816 usb_free_descriptors(f->hs_descriptors);
817 if (f->descriptors)
818 usb_free_descriptors(f->descriptors);
819
David Brownell1f1ba112008-08-06 18:49:57 -0700820 if (acm->notify_req)
821 gs_free_req(acm->notify, acm->notify_req);
822
David Brownell4d5a73d2008-06-19 18:18:40 -0700823 /* we might as well release our claims on endpoints */
824 if (acm->notify)
825 acm->notify->driver_data = NULL;
826 if (acm->port.out)
827 acm->port.out->driver_data = NULL;
828 if (acm->port.in)
829 acm->port.in->driver_data = NULL;
830
831 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
832
833 return status;
834}
835
836static void
837acm_unbind(struct usb_configuration *c, struct usb_function *f)
838{
David Brownell1f1ba112008-08-06 18:49:57 -0700839 struct f_acm *acm = func_to_acm(f);
840
David Brownell4d5a73d2008-06-19 18:18:40 -0700841 if (gadget_is_dualspeed(c->cdev->gadget))
842 usb_free_descriptors(f->hs_descriptors);
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100843 if (gadget_is_superspeed(c->cdev->gadget))
844 usb_free_descriptors(f->ss_descriptors);
David Brownell4d5a73d2008-06-19 18:18:40 -0700845 usb_free_descriptors(f->descriptors);
David Brownell1f1ba112008-08-06 18:49:57 -0700846 gs_free_req(acm->notify, acm->notify_req);
John Michelau677ba872010-11-08 18:05:37 -0600847 kfree(acm->port.func.name);
David Brownell1f1ba112008-08-06 18:49:57 -0700848 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700849}
850
851/* Some controllers can't support CDC ACM ... */
852static inline bool can_support_cdc(struct usb_configuration *c)
853{
David Brownell4d5a73d2008-06-19 18:18:40 -0700854 /* everything else is *probably* fine ... */
855 return true;
856}
857
858/**
859 * acm_bind_config - add a CDC ACM function to a configuration
860 * @c: the configuration to support the CDC ACM instance
861 * @port_num: /dev/ttyGS* port this interface will use
862 * Context: single threaded during gadget setup
863 *
864 * Returns zero on success, else negative errno.
865 *
866 * Caller must have called @gserial_setup() with enough ports to
867 * handle all the ones it binds. Caller is also responsible
868 * for calling @gserial_cleanup() before module unload.
869 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200870int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700871{
872 struct f_acm *acm;
873 int status;
874
875 if (!can_support_cdc(c))
876 return -EINVAL;
877
878 /* REVISIT might want instance-specific strings to help
879 * distinguish instances ...
880 */
881
882 /* maybe allocate device-global string IDs, and patch descriptors */
883 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
884 status = usb_string_id(c->cdev);
885 if (status < 0)
886 return status;
887 acm_string_defs[ACM_CTRL_IDX].id = status;
888
889 acm_control_interface_desc.iInterface = status;
890
891 status = usb_string_id(c->cdev);
892 if (status < 0)
893 return status;
894 acm_string_defs[ACM_DATA_IDX].id = status;
895
896 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100897
898 status = usb_string_id(c->cdev);
899 if (status < 0)
900 return status;
901 acm_string_defs[ACM_IAD_IDX].id = status;
902
903 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700904 }
905
906 /* allocate and initialize one new instance */
907 acm = kzalloc(sizeof *acm, GFP_KERNEL);
908 if (!acm)
909 return -ENOMEM;
910
David Brownell1f1ba112008-08-06 18:49:57 -0700911 spin_lock_init(&acm->lock);
912
David Brownell4d5a73d2008-06-19 18:18:40 -0700913 acm->port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 acm->transport = gacm_ports[port_num].transport;
David Brownell4d5a73d2008-06-19 18:18:40 -0700915
David Brownell1f1ba112008-08-06 18:49:57 -0700916 acm->port.connect = acm_connect;
917 acm->port.disconnect = acm_disconnect;
918 acm->port.send_break = acm_send_break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 acm->port.send_modem_ctrl_bits = acm_send_modem_ctrl_bits;
David Brownell1f1ba112008-08-06 18:49:57 -0700920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921 acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num + 1);
John Michelau677ba872010-11-08 18:05:37 -0600922 if (!acm->port.func.name) {
923 kfree(acm);
924 return -ENOMEM;
925 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700926 acm->port.func.strings = acm_strings;
927 /* descriptors are per-instance copies */
928 acm->port.func.bind = acm_bind;
929 acm->port.func.unbind = acm_unbind;
930 acm->port.func.set_alt = acm_set_alt;
931 acm->port.func.setup = acm_setup;
932 acm->port.func.disable = acm_disable;
933
934 status = usb_add_function(c, &acm->port.func);
935 if (status)
936 kfree(acm);
937 return status;
938}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939
Anji jonnala92be1b42011-12-19 09:44:41 +0530940/**
941 * acm_init_port - bind a acm_port to its transport
942 */
943static int acm_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944{
Anji jonnala92be1b42011-12-19 09:44:41 +0530945 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946
Anji jonnala92be1b42011-12-19 09:44:41 +0530947 if (port_num >= GSERIAL_NO_PORTS)
948 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949
Anji jonnala92be1b42011-12-19 09:44:41 +0530950 transport = str_to_xport(name);
951 pr_debug("%s, port:%d, transport:%s\n", __func__,
952 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953
Anji jonnala92be1b42011-12-19 09:44:41 +0530954 gacm_ports[port_num].transport = transport;
955 gacm_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
Anji jonnala92be1b42011-12-19 09:44:41 +0530957 switch (transport) {
958 case USB_GADGET_XPORT_TTY:
959 gacm_ports[port_num].client_port_num = no_acm_tty_ports;
960 no_acm_tty_ports++;
961 break;
962 case USB_GADGET_XPORT_SDIO:
963 gacm_ports[port_num].client_port_num = no_acm_sdio_ports;
964 no_acm_sdio_ports++;
965 break;
966 case USB_GADGET_XPORT_SMD:
967 gacm_ports[port_num].client_port_num = no_acm_smd_ports;
968 no_acm_smd_ports++;
969 break;
970 default:
971 pr_err("%s: Un-supported transport transport: %u\n",
972 __func__, gacm_ports[port_num].transport);
973 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 }
975
Anji jonnala92be1b42011-12-19 09:44:41 +0530976 nr_acm_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977
978 return 0;
979}