blob: 57cbc033bc409f892ca198dd44d2eb6f1e0a8d26 [file] [log] [blame]
David Brownell61d8bae2008-06-19 18:18:50 -07001/*
2 * f_serial.c - generic USB serial 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
7 *
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
11 */
12
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownell61d8bae2008-06-19 18:18:50 -070014#include <linux/kernel.h>
15#include <linux/device.h>
Hemant Kumarbffd4142011-11-03 13:20:40 -070016#include <mach/usb_gadget_xport.h>
David Brownell61d8bae2008-06-19 18:18:50 -070017
18#include "u_serial.h"
19#include "gadget_chips.h"
20
21
22/*
23 * This function packages a simple "generic serial" port with no real
24 * control mechanisms, just raw data transfer over two bulk endpoints.
25 *
26 * Because it's not standardized, this isn't as interoperable as the
27 * CDC ACM driver. However, for many purposes it's just as functional
28 * if you can arrange appropriate host side drivers.
29 */
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053030#define GSERIAL_NO_PORTS 3
David Brownell61d8bae2008-06-19 18:18:50 -070031
David Brownell61d8bae2008-06-19 18:18:50 -070032struct f_gser {
33 struct gserial port;
34 u8 data_id;
35 u8 port_num;
36
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037 u8 online;
38 enum transport_type transport;
39
40#ifdef CONFIG_MODEM_SUPPORT
41 u8 pending;
42 spinlock_t lock;
43 struct usb_ep *notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044 struct usb_request *notify_req;
45
46 struct usb_cdc_line_coding port_line_coding;
47
48 /* SetControlLineState request */
49 u16 port_handshake_bits;
50#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
51#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
52
53 /* SerialState notification */
54 u16 serial_state;
55#define ACM_CTRL_OVERRUN (1 << 6)
56#define ACM_CTRL_PARITY (1 << 5)
57#define ACM_CTRL_FRAMING (1 << 4)
58#define ACM_CTRL_RI (1 << 3)
59#define ACM_CTRL_BRK (1 << 2)
60#define ACM_CTRL_DSR (1 << 1)
61#define ACM_CTRL_DCD (1 << 0)
62#endif
David Brownell61d8bae2008-06-19 18:18:50 -070063};
64
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065static unsigned int no_tty_ports;
66static unsigned int no_sdio_ports;
67static unsigned int no_smd_ports;
Jack Pham427f6922011-11-23 19:42:00 -080068static unsigned int no_hsic_sports;
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053069static unsigned int no_hsuart_sports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static unsigned int nr_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071
72static struct port_info {
73 enum transport_type transport;
74 unsigned port_num;
75 unsigned client_port_num;
76} gserial_ports[GSERIAL_NO_PORTS];
77
78static inline bool is_transport_sdio(enum transport_type t)
79{
Hemant Kumarbffd4142011-11-03 13:20:40 -070080 if (t == USB_GADGET_XPORT_SDIO)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 return 1;
82 return 0;
83}
84
David Brownell61d8bae2008-06-19 18:18:50 -070085static inline struct f_gser *func_to_gser(struct usb_function *f)
86{
87 return container_of(f, struct f_gser, port.func);
88}
89
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090#ifdef CONFIG_MODEM_SUPPORT
91static inline struct f_gser *port_to_gser(struct gserial *p)
92{
93 return container_of(p, struct f_gser, port);
94}
95#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
96#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
97#endif
David Brownell61d8bae2008-06-19 18:18:50 -070098/*-------------------------------------------------------------------------*/
99
100/* interface descriptor: */
101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700103 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE,
105 /* .bInterfaceNumber = DYNAMIC */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106#ifdef CONFIG_MODEM_SUPPORT
107 .bNumEndpoints = 3,
108#else
David Brownell61d8bae2008-06-19 18:18:50 -0700109 .bNumEndpoints = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700111 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
112 .bInterfaceSubClass = 0,
113 .bInterfaceProtocol = 0,
114 /* .iInterface = DYNAMIC */
115};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#ifdef CONFIG_MODEM_SUPPORT
117static struct usb_cdc_header_desc gser_header_desc = {
118 .bLength = sizeof(gser_header_desc),
119 .bDescriptorType = USB_DT_CS_INTERFACE,
120 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
121 .bcdCDC = __constant_cpu_to_le16(0x0110),
122};
David Brownell61d8bae2008-06-19 18:18:50 -0700123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124static struct usb_cdc_call_mgmt_descriptor
125gser_call_mgmt_descriptor = {
126 .bLength = sizeof(gser_call_mgmt_descriptor),
127 .bDescriptorType = USB_DT_CS_INTERFACE,
128 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
129 .bmCapabilities = 0,
130 /* .bDataInterface = DYNAMIC */
131};
132
133static struct usb_cdc_acm_descriptor gser_descriptor = {
134 .bLength = sizeof(gser_descriptor),
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136 .bDescriptorSubType = USB_CDC_ACM_TYPE,
137 .bmCapabilities = USB_CDC_CAP_LINE,
138};
139
140static struct usb_cdc_union_desc gser_union_desc = {
141 .bLength = sizeof(gser_union_desc),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_UNION_TYPE,
144 /* .bMasterInterface0 = DYNAMIC */
145 /* .bSlaveInterface0 = DYNAMIC */
146};
147#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700148/* full speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149#ifdef CONFIG_MODEM_SUPPORT
150static struct usb_endpoint_descriptor gser_fs_notify_desc = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_IN,
154 .bmAttributes = USB_ENDPOINT_XFER_INT,
155 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
156 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
157};
158#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700159
Manu Gautama4d993f2011-08-30 18:25:55 +0530160static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700161 .bLength = USB_DT_ENDPOINT_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165};
166
Manu Gautama4d993f2011-08-30 18:25:55 +0530167static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700168 .bLength = USB_DT_ENDPOINT_SIZE,
169 .bDescriptorType = USB_DT_ENDPOINT,
170 .bEndpointAddress = USB_DIR_OUT,
171 .bmAttributes = USB_ENDPOINT_XFER_BULK,
172};
173
Manu Gautama4d993f2011-08-30 18:25:55 +0530174static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700175 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176#ifdef CONFIG_MODEM_SUPPORT
177 (struct usb_descriptor_header *) &gser_header_desc,
178 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
179 (struct usb_descriptor_header *) &gser_descriptor,
180 (struct usb_descriptor_header *) &gser_union_desc,
181 (struct usb_descriptor_header *) &gser_fs_notify_desc,
182#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700183 (struct usb_descriptor_header *) &gser_fs_in_desc,
184 (struct usb_descriptor_header *) &gser_fs_out_desc,
185 NULL,
186};
187
188/* high speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189#ifdef CONFIG_MODEM_SUPPORT
190static struct usb_endpoint_descriptor gser_hs_notify_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT,
193 .bEndpointAddress = USB_DIR_IN,
194 .bmAttributes = USB_ENDPOINT_XFER_INT,
195 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
196 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
197};
198#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700199
Manu Gautama4d993f2011-08-30 18:25:55 +0530200static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700205};
206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700212};
213
Manu Gautama4d993f2011-08-30 18:25:55 +0530214static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700215 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216#ifdef CONFIG_MODEM_SUPPORT
217 (struct usb_descriptor_header *) &gser_header_desc,
218 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
219 (struct usb_descriptor_header *) &gser_descriptor,
220 (struct usb_descriptor_header *) &gser_union_desc,
221 (struct usb_descriptor_header *) &gser_hs_notify_desc,
222#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700223 (struct usb_descriptor_header *) &gser_hs_in_desc,
224 (struct usb_descriptor_header *) &gser_hs_out_desc,
225 NULL,
226};
227
Pavankumar Kondetib92f7362012-10-12 15:44:31 +0530228static struct usb_endpoint_descriptor gser_ss_in_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100229 .bLength = USB_DT_ENDPOINT_SIZE,
230 .bDescriptorType = USB_DT_ENDPOINT,
231 .bmAttributes = USB_ENDPOINT_XFER_BULK,
232 .wMaxPacketSize = cpu_to_le16(1024),
233};
234
Pavankumar Kondetib92f7362012-10-12 15:44:31 +0530235static struct usb_endpoint_descriptor gser_ss_out_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238 .bmAttributes = USB_ENDPOINT_XFER_BULK,
239 .wMaxPacketSize = cpu_to_le16(1024),
240};
241
Pavankumar Kondetib92f7362012-10-12 15:44:31 +0530242static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100243 .bLength = sizeof gser_ss_bulk_comp_desc,
244 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
245};
246
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530247#ifdef CONFIG_MODEM_SUPPORT
248static struct usb_endpoint_descriptor gser_ss_notify_desc = {
Vijayavardhan Vennapusa724ae312012-11-20 17:36:21 +0530249 .bLength = USB_DT_ENDPOINT_SIZE,
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530250 .bDescriptorType = USB_DT_ENDPOINT,
251 .bEndpointAddress = USB_DIR_IN,
252 .bmAttributes = USB_ENDPOINT_XFER_INT,
253 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
254 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
255};
256
257static struct usb_ss_ep_comp_descriptor gser_ss_notify_comp_desc = {
258 .bLength = sizeof gser_ss_notify_comp_desc,
259 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
260
261 /* the following 2 values can be tweaked if necessary */
262 /* .bMaxBurst = 0, */
263 /* .bmAttributes = 0, */
264 .wBytesPerInterval = cpu_to_le16(GS_NOTIFY_MAXPACKET),
265};
266#endif
267
Pavankumar Kondetib92f7362012-10-12 15:44:31 +0530268static struct usb_descriptor_header *gser_ss_function[] = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100269 (struct usb_descriptor_header *) &gser_interface_desc,
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530270#ifdef CONFIG_MODEM_SUPPORT
271 (struct usb_descriptor_header *) &gser_header_desc,
272 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
273 (struct usb_descriptor_header *) &gser_descriptor,
274 (struct usb_descriptor_header *) &gser_union_desc,
275 (struct usb_descriptor_header *) &gser_ss_notify_desc,
276 (struct usb_descriptor_header *) &gser_ss_notify_comp_desc,
277#endif
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100278 (struct usb_descriptor_header *) &gser_ss_in_desc,
279 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
280 (struct usb_descriptor_header *) &gser_ss_out_desc,
281 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
282 NULL,
283};
284
David Brownell61d8bae2008-06-19 18:18:50 -0700285/* string descriptors: */
286
287static struct usb_string gser_string_defs[] = {
288 [0].s = "Generic Serial",
289 { } /* end of list */
290};
291
292static struct usb_gadget_strings gser_string_table = {
293 .language = 0x0409, /* en-us */
294 .strings = gser_string_defs,
295};
296
297static struct usb_gadget_strings *gser_strings[] = {
298 &gser_string_table,
299 NULL,
300};
301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302static int gport_setup(struct usb_configuration *c)
303{
304 int ret = 0;
Jack Pham427f6922011-11-23 19:42:00 -0800305 int port_idx;
306 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307
Jack Pham427f6922011-11-23 19:42:00 -0800308 pr_debug("%s: no_tty_ports: %u no_sdio_ports: %u"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530309 " no_smd_ports: %u no_hsic_sports: %u no_hsuart_ports: %u nr_ports: %u\n",
Jack Pham427f6922011-11-23 19:42:00 -0800310 __func__, no_tty_ports, no_sdio_ports, no_smd_ports,
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530311 no_hsic_sports, no_hsuart_sports, nr_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312
313 if (no_tty_ports)
314 ret = gserial_setup(c->cdev->gadget, no_tty_ports);
315 if (no_sdio_ports)
316 ret = gsdio_setup(c->cdev->gadget, no_sdio_ports);
317 if (no_smd_ports)
318 ret = gsmd_setup(c->cdev->gadget, no_smd_ports);
Jack Pham427f6922011-11-23 19:42:00 -0800319 if (no_hsic_sports) {
320 port_idx = ghsic_data_setup(no_hsic_sports, USB_GADGET_SERIAL);
321 if (port_idx < 0)
322 return port_idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
Jack Pham427f6922011-11-23 19:42:00 -0800324 for (i = 0; i < nr_ports; i++) {
325 if (gserial_ports[i].transport ==
326 USB_GADGET_XPORT_HSIC) {
327 gserial_ports[i].client_port_num = port_idx;
328 port_idx++;
329 }
330 }
331
332 /*clinet port num is same for data setup and ctrl setup*/
333 ret = ghsic_ctrl_setup(no_hsic_sports, USB_GADGET_SERIAL);
334 if (ret < 0)
335 return ret;
Jack Pham427f6922011-11-23 19:42:00 -0800336 }
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530337 if (no_hsuart_sports) {
338 port_idx = ghsuart_data_setup(no_hsuart_sports,
339 USB_GADGET_SERIAL);
340 if (port_idx < 0)
341 return port_idx;
342
343 for (i = 0; i < nr_ports; i++) {
344 if (gserial_ports[i].transport ==
345 USB_GADGET_XPORT_HSUART) {
346 gserial_ports[i].client_port_num = port_idx;
347 port_idx++;
348 }
349 }
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530350 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 return ret;
352}
Manu Gautama4d993f2011-08-30 18:25:55 +0530353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354static int gport_connect(struct f_gser *gser)
355{
Jack Pham427f6922011-11-23 19:42:00 -0800356 unsigned port_num;
357 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358
Jack Pham427f6922011-11-23 19:42:00 -0800359 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700360 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 gser, &gser->port, gser->port_num);
362
363 port_num = gserial_ports[gser->port_num].client_port_num;
364
365 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700366 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 gserial_connect(&gser->port, port_num);
368 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700369 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 gsdio_connect(&gser->port, port_num);
371 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700372 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 gsmd_connect(&gser->port, port_num);
374 break;
Jack Pham427f6922011-11-23 19:42:00 -0800375 case USB_GADGET_XPORT_HSIC:
376 ret = ghsic_ctrl_connect(&gser->port, port_num);
377 if (ret) {
378 pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
379 __func__, ret);
380 return ret;
381 }
382 ret = ghsic_data_connect(&gser->port, port_num);
383 if (ret) {
384 pr_err("%s: ghsic_data_connect failed: err:%d\n",
385 __func__, ret);
386 ghsic_ctrl_disconnect(&gser->port, port_num);
387 return ret;
388 }
389 break;
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530390 case USB_GADGET_XPORT_HSUART:
391 ret = ghsuart_data_connect(&gser->port, port_num);
392 if (ret) {
393 pr_err("%s: ghsuart_data_connect failed: err:%d\n",
394 __func__, ret);
395 return ret;
396 }
397 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398 default:
399 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700400 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 return -ENODEV;
402 }
403
404 return 0;
405}
406
407static int gport_disconnect(struct f_gser *gser)
408{
409 unsigned port_num;
410
Jack Pham427f6922011-11-23 19:42:00 -0800411 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700412 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 gser, &gser->port, gser->port_num);
414
415 port_num = gserial_ports[gser->port_num].client_port_num;
416
417 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700418 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 gserial_disconnect(&gser->port);
420 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700421 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 gsdio_disconnect(&gser->port, port_num);
423 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700424 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 gsmd_disconnect(&gser->port, port_num);
426 break;
Jack Pham427f6922011-11-23 19:42:00 -0800427 case USB_GADGET_XPORT_HSIC:
428 ghsic_ctrl_disconnect(&gser->port, port_num);
429 ghsic_data_disconnect(&gser->port, port_num);
430 break;
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530431 case USB_GADGET_XPORT_HSUART:
432 ghsuart_data_disconnect(&gser->port, port_num);
433 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 default:
435 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700436 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437 return -ENODEV;
438 }
439
440 return 0;
441}
442
443#ifdef CONFIG_MODEM_SUPPORT
444static void gser_complete_set_line_coding(struct usb_ep *ep,
445 struct usb_request *req)
446{
447 struct f_gser *gser = ep->driver_data;
448 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
449
450 if (req->status != 0) {
451 DBG(cdev, "gser ttyGS%d completion, err %d\n",
452 gser->port_num, req->status);
453 return;
454 }
455
456 /* normal completion */
457 if (req->actual != sizeof(gser->port_line_coding)) {
458 DBG(cdev, "gser ttyGS%d short resp, len %d\n",
459 gser->port_num, req->actual);
460 usb_ep_set_halt(ep);
461 } else {
462 struct usb_cdc_line_coding *value = req->buf;
463 gser->port_line_coding = *value;
464 }
465}
David Brownell61d8bae2008-06-19 18:18:50 -0700466/*-------------------------------------------------------------------------*/
467
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468static int
469gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
470{
471 struct f_gser *gser = func_to_gser(f);
472 struct usb_composite_dev *cdev = f->config->cdev;
473 struct usb_request *req = cdev->req;
474 int value = -EOPNOTSUPP;
475 u16 w_index = le16_to_cpu(ctrl->wIndex);
476 u16 w_value = le16_to_cpu(ctrl->wValue);
477 u16 w_length = le16_to_cpu(ctrl->wLength);
478
479 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
480
481 /* SET_LINE_CODING ... just read and save what the host sends */
482 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
483 | USB_CDC_REQ_SET_LINE_CODING:
484 if (w_length != sizeof(struct usb_cdc_line_coding))
485 goto invalid;
486
487 value = w_length;
488 cdev->gadget->ep0->driver_data = gser;
489 req->complete = gser_complete_set_line_coding;
490 break;
491
492 /* GET_LINE_CODING ... return what host sent, or initial value */
493 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
494 | USB_CDC_REQ_GET_LINE_CODING:
495 value = min_t(unsigned, w_length,
496 sizeof(struct usb_cdc_line_coding));
497 memcpy(req->buf, &gser->port_line_coding, value);
498 break;
499
500 /* SET_CONTROL_LINE_STATE ... save what the host sent */
501 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
502 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
503
504 value = 0;
505 gser->port_handshake_bits = w_value;
506 if (gser->port.notify_modem) {
507 unsigned port_num =
508 gserial_ports[gser->port_num].client_port_num;
509
510 gser->port.notify_modem(&gser->port,
511 port_num, w_value);
512 }
513 break;
514
515 default:
516invalid:
517 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
518 ctrl->bRequestType, ctrl->bRequest,
519 w_value, w_index, w_length);
520 }
521
522 /* respond with data transfer or status phase? */
523 if (value >= 0) {
524 DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
525 gser->port_num, ctrl->bRequestType, ctrl->bRequest,
526 w_value, w_index, w_length);
527 req->zero = 0;
528 req->length = value;
529 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
530 if (value < 0)
531 ERROR(cdev, "gser response on ttyGS%d, err %d\n",
532 gser->port_num, value);
533 }
534
535 /* device either stalls (value < 0) or reports success */
536 return value;
537}
538#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700539static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
540{
541 struct f_gser *gser = func_to_gser(f);
542 struct usb_composite_dev *cdev = f->config->cdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 int rc = 0;
David Brownell61d8bae2008-06-19 18:18:50 -0700544
545 /* we know alt == 0, so this is an activation or a reset */
546
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547#ifdef CONFIG_MODEM_SUPPORT
548 if (gser->notify->driver_data) {
549 DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num);
550 usb_ep_disable(gser->notify);
David Brownell61d8bae2008-06-19 18:18:50 -0700551 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200552
553 if (!gser->notify->desc) {
554 if (config_ep_by_speed(cdev->gadget, f, gser->notify)) {
555 gser->notify->desc = NULL;
556 return -EINVAL;
557 }
558 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300559 rc = usb_ep_enable(gser->notify);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200560
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 if (rc) {
562 ERROR(cdev, "can't enable %s, result %d\n",
563 gser->notify->name, rc);
564 return rc;
565 }
566 gser->notify->driver_data = gser;
567#endif
568
David Brownell61d8bae2008-06-19 18:18:50 -0700569 if (gser->port.in->driver_data) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
571 gport_disconnect(gser);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300572 }
573 if (!gser->port.in->desc || !gser->port.out->desc) {
David Brownell61d8bae2008-06-19 18:18:50 -0700574 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
Robert Jarzmikfef69642011-11-18 20:16:27 +0100575 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
576 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300577 gser->port.in->desc = NULL;
578 gser->port.out->desc = NULL;
579 return -EINVAL;
580 }
David Brownell61d8bae2008-06-19 18:18:50 -0700581 }
David Brownac5d1542012-02-06 10:37:22 -0800582
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 gport_connect(gser);
584
585 gser->online = 1;
586 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700587}
588
589static void gser_disable(struct usb_function *f)
590{
591 struct f_gser *gser = func_to_gser(f);
592 struct usb_composite_dev *cdev = f->config->cdev;
593
594 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 gport_disconnect(gser);
597
598#ifdef CONFIG_MODEM_SUPPORT
599 usb_ep_fifo_flush(gser->notify);
600 usb_ep_disable(gser->notify);
Chiranjeevi Velempati4fd1c162012-08-01 09:41:22 +0530601 gser->notify->driver_data = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602#endif
603 gser->online = 0;
604}
605#ifdef CONFIG_MODEM_SUPPORT
606static int gser_notify(struct f_gser *gser, u8 type, u16 value,
607 void *data, unsigned length)
608{
609 struct usb_ep *ep = gser->notify;
610 struct usb_request *req;
611 struct usb_cdc_notification *notify;
612 const unsigned len = sizeof(*notify) + length;
613 void *buf;
614 int status;
615 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
616
617 req = gser->notify_req;
618 gser->notify_req = NULL;
619 gser->pending = false;
620
621 req->length = len;
622 notify = req->buf;
623 buf = notify + 1;
624
625 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
626 | USB_RECIP_INTERFACE;
627 notify->bNotificationType = type;
628 notify->wValue = cpu_to_le16(value);
629 notify->wIndex = cpu_to_le16(gser->data_id);
630 notify->wLength = cpu_to_le16(length);
631 memcpy(buf, data, length);
632
633 status = usb_ep_queue(ep, req, GFP_ATOMIC);
634 if (status < 0) {
635 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
636 gser->port_num, status);
637 gser->notify_req = req;
638 }
639
640 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700641}
642
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643static int gser_notify_serial_state(struct f_gser *gser)
644{
645 int status;
646 unsigned long flags;
647 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
648
649 spin_lock_irqsave(&gser->lock, flags);
650 if (gser->notify_req) {
651 DBG(cdev, "gser ttyGS%d serial state %04x\n",
652 gser->port_num, gser->serial_state);
653 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
654 0, &gser->serial_state,
655 sizeof(gser->serial_state));
656 } else {
657 gser->pending = true;
658 status = 0;
659 }
660 spin_unlock_irqrestore(&gser->lock, flags);
661 return status;
662}
663
664static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
665{
666 struct f_gser *gser = req->context;
667 u8 doit = false;
668 unsigned long flags;
669
670 /* on this call path we do NOT hold the port spinlock,
671 * which is why ACM needs its own spinlock
672 */
673 spin_lock_irqsave(&gser->lock, flags);
674 if (req->status != -ESHUTDOWN)
675 doit = gser->pending;
676 gser->notify_req = req;
677 spin_unlock_irqrestore(&gser->lock, flags);
678
679 if (doit && gser->online)
680 gser_notify_serial_state(gser);
681}
682static void gser_connect(struct gserial *port)
683{
684 struct f_gser *gser = port_to_gser(port);
685
686 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
687 gser_notify_serial_state(gser);
688}
689
690unsigned int gser_get_dtr(struct gserial *port)
691{
692 struct f_gser *gser = port_to_gser(port);
693
694 if (gser->port_handshake_bits & ACM_CTRL_DTR)
695 return 1;
696 else
697 return 0;
698}
699
700unsigned int gser_get_rts(struct gserial *port)
701{
702 struct f_gser *gser = port_to_gser(port);
703
704 if (gser->port_handshake_bits & ACM_CTRL_RTS)
705 return 1;
706 else
707 return 0;
708}
709
710unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
711{
712 struct f_gser *gser = port_to_gser(port);
713 u16 state;
714
715 state = gser->serial_state;
716 state &= ~ACM_CTRL_DCD;
717 if (yes)
718 state |= ACM_CTRL_DCD;
719
720 gser->serial_state = state;
721 return gser_notify_serial_state(gser);
722
723}
724
725unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
726{
727 struct f_gser *gser = port_to_gser(port);
728 u16 state;
729
730 state = gser->serial_state;
731 state &= ~ACM_CTRL_RI;
732 if (yes)
733 state |= ACM_CTRL_RI;
734
735 gser->serial_state = state;
736 return gser_notify_serial_state(gser);
737
738}
739static void gser_disconnect(struct gserial *port)
740{
741 struct f_gser *gser = port_to_gser(port);
742
743 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
744 gser_notify_serial_state(gser);
745}
746
747static int gser_send_break(struct gserial *port, int duration)
748{
749 struct f_gser *gser = port_to_gser(port);
750 u16 state;
751
752 state = gser->serial_state;
753 state &= ~ACM_CTRL_BRK;
754 if (duration)
755 state |= ACM_CTRL_BRK;
756
757 gser->serial_state = state;
758 return gser_notify_serial_state(gser);
759}
760
761static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
762{
763 struct f_gser *gser = port_to_gser(port);
764
765 gser->serial_state = ctrl_bits;
766
767 return gser_notify_serial_state(gser);
768}
769#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700770/*-------------------------------------------------------------------------*/
771
772/* serial function driver setup/binding */
773
Manu Gautama4d993f2011-08-30 18:25:55 +0530774static int
David Brownell61d8bae2008-06-19 18:18:50 -0700775gser_bind(struct usb_configuration *c, struct usb_function *f)
776{
777 struct usb_composite_dev *cdev = c->cdev;
778 struct f_gser *gser = func_to_gser(f);
779 int status;
780 struct usb_ep *ep;
781
782 /* allocate instance-specific interface IDs */
783 status = usb_interface_id(c, f);
784 if (status < 0)
785 goto fail;
786 gser->data_id = status;
787 gser_interface_desc.bInterfaceNumber = status;
788
789 status = -ENODEV;
790
791 /* allocate instance-specific endpoints */
792 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
793 if (!ep)
794 goto fail;
795 gser->port.in = ep;
796 ep->driver_data = cdev; /* claim */
797
798 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
799 if (!ep)
800 goto fail;
801 gser->port.out = ep;
802 ep->driver_data = cdev; /* claim */
803
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804#ifdef CONFIG_MODEM_SUPPORT
805 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
806 if (!ep)
807 goto fail;
808 gser->notify = ep;
809 ep->driver_data = cdev; /* claim */
810 /* allocate notification */
811 gser->notify_req = gs_alloc_req(ep,
812 sizeof(struct usb_cdc_notification) + 2,
813 GFP_KERNEL);
814 if (!gser->notify_req)
815 goto fail;
816
817 gser->notify_req->complete = gser_notify_complete;
818 gser->notify_req->context = gser;
819#endif
820
David Brownell61d8bae2008-06-19 18:18:50 -0700821 /* copy descriptors, and track endpoint copies */
822 f->descriptors = usb_copy_descriptors(gser_fs_function);
823
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530824 if (!f->descriptors)
825 goto fail;
826
David Brownell61d8bae2008-06-19 18:18:50 -0700827 /* support all relevant hardware speeds... we expect that when
828 * hardware is dual speed, all bulk-capable endpoints work at
829 * both speeds
830 */
831 if (gadget_is_dualspeed(c->cdev->gadget)) {
832 gser_hs_in_desc.bEndpointAddress =
833 gser_fs_in_desc.bEndpointAddress;
834 gser_hs_out_desc.bEndpointAddress =
835 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836#ifdef CONFIG_MODEM_SUPPORT
837 gser_hs_notify_desc.bEndpointAddress =
838 gser_fs_notify_desc.bEndpointAddress;
839#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700840
841 /* copy descriptors, and track endpoint copies */
842 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
843
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530844 if (!f->hs_descriptors)
845 goto fail;
846
David Brownell61d8bae2008-06-19 18:18:50 -0700847 }
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100848 if (gadget_is_superspeed(c->cdev->gadget)) {
849 gser_ss_in_desc.bEndpointAddress =
850 gser_fs_in_desc.bEndpointAddress;
851 gser_ss_out_desc.bEndpointAddress =
852 gser_fs_out_desc.bEndpointAddress;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530853#ifdef CONFIG_MODEM_SUPPORT
854 gser_ss_notify_desc.bEndpointAddress =
855 gser_fs_notify_desc.bEndpointAddress;
856#endif
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100857
858 /* copy descriptors, and track endpoint copies */
859 f->ss_descriptors = usb_copy_descriptors(gser_ss_function);
860 if (!f->ss_descriptors)
861 goto fail;
862 }
David Brownell61d8bae2008-06-19 18:18:50 -0700863
864 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
865 gser->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100866 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell61d8bae2008-06-19 18:18:50 -0700867 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
868 gser->port.in->name, gser->port.out->name);
869 return 0;
870
871fail:
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530872 if (f->ss_descriptors)
873 usb_free_descriptors(f->ss_descriptors);
874 if (f->hs_descriptors)
875 usb_free_descriptors(f->hs_descriptors);
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530876 if (f->descriptors)
877 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878#ifdef CONFIG_MODEM_SUPPORT
879 if (gser->notify_req)
880 gs_free_req(gser->notify, gser->notify_req);
881
882 /* we might as well release our claims on endpoints */
883 if (gser->notify)
884 gser->notify->driver_data = NULL;
885#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700886 /* we might as well release our claims on endpoints */
887 if (gser->port.out)
888 gser->port.out->driver_data = NULL;
889 if (gser->port.in)
890 gser->port.in->driver_data = NULL;
891
892 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
893
894 return status;
895}
896
897static void
898gser_unbind(struct usb_configuration *c, struct usb_function *f)
899{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900#ifdef CONFIG_MODEM_SUPPORT
901 struct f_gser *gser = func_to_gser(f);
902#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700903 if (gadget_is_dualspeed(c->cdev->gadget))
904 usb_free_descriptors(f->hs_descriptors);
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100905 if (gadget_is_superspeed(c->cdev->gadget))
906 usb_free_descriptors(f->ss_descriptors);
David Brownell61d8bae2008-06-19 18:18:50 -0700907 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908#ifdef CONFIG_MODEM_SUPPORT
909 gs_free_req(gser->notify, gser->notify_req);
910#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700911 kfree(func_to_gser(f));
912}
913
914/**
915 * gser_bind_config - add a generic serial function to a configuration
916 * @c: the configuration to support the serial instance
917 * @port_num: /dev/ttyGS* port this interface will use
918 * Context: single threaded during gadget setup
919 *
920 * Returns zero on success, else negative errno.
921 *
922 * Caller must have called @gserial_setup() with enough ports to
923 * handle all the ones it binds. Caller is also responsible
924 * for calling @gserial_cleanup() before module unload.
925 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530926int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700927{
928 struct f_gser *gser;
929 int status;
930
931 /* REVISIT might want instance-specific strings to help
932 * distinguish instances ...
933 */
934
935 /* maybe allocate device-global string ID */
936 if (gser_string_defs[0].id == 0) {
937 status = usb_string_id(c->cdev);
938 if (status < 0)
939 return status;
940 gser_string_defs[0].id = status;
941 }
942
943 /* allocate and initialize one new instance */
944 gser = kzalloc(sizeof *gser, GFP_KERNEL);
945 if (!gser)
946 return -ENOMEM;
947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948#ifdef CONFIG_MODEM_SUPPORT
949 spin_lock_init(&gser->lock);
950#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700951 gser->port_num = port_num;
952
953 gser->port.func.name = "gser";
954 gser->port.func.strings = gser_strings;
955 gser->port.func.bind = gser_bind;
956 gser->port.func.unbind = gser_unbind;
957 gser->port.func.set_alt = gser_set_alt;
958 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 gser->transport = gserial_ports[port_num].transport;
960#ifdef CONFIG_MODEM_SUPPORT
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530961 /* We support only three ports for now */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 if (port_num == 0)
963 gser->port.func.name = "modem";
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530964 else if (port_num == 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 gser->port.func.name = "nmea";
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +0530966 else
967 gser->port.func.name = "modem2";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 gser->port.func.setup = gser_setup;
969 gser->port.connect = gser_connect;
970 gser->port.get_dtr = gser_get_dtr;
971 gser->port.get_rts = gser_get_rts;
972 gser->port.send_carrier_detect = gser_send_carrier_detect;
973 gser->port.send_ring_indicator = gser_send_ring_indicator;
974 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
975 gser->port.disconnect = gser_disconnect;
976 gser->port.send_break = gser_send_break;
977#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700978
979 status = usb_add_function(c, &gser->port.func);
980 if (status)
981 kfree(gser);
982 return status;
983}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984
Manu Gautama4d993f2011-08-30 18:25:55 +0530985/**
986 * gserial_init_port - bind a gserial_port to its transport
987 */
Hemant Kumarc2b17782013-02-03 15:56:29 -0800988static int gserial_init_port(int port_num, const char *name,
989 const char *port_name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990{
Manu Gautama4d993f2011-08-30 18:25:55 +0530991 enum transport_type transport;
Hemant Kumarc2b17782013-02-03 15:56:29 -0800992 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993
Manu Gautama4d993f2011-08-30 18:25:55 +0530994 if (port_num >= GSERIAL_NO_PORTS)
995 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996
Hemant Kumar1b820d52011-11-03 15:08:28 -0700997 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530998 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800999 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
Manu Gautama4d993f2011-08-30 18:25:55 +05301001 gserial_ports[port_num].transport = transport;
1002 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
Manu Gautama4d993f2011-08-30 18:25:55 +05301004 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -07001005 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +05301006 gserial_ports[port_num].client_port_num = no_tty_ports;
1007 no_tty_ports++;
1008 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -07001009 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +05301010 gserial_ports[port_num].client_port_num = no_sdio_ports;
1011 no_sdio_ports++;
1012 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -07001013 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +05301014 gserial_ports[port_num].client_port_num = no_smd_ports;
1015 no_smd_ports++;
1016 break;
Jack Pham427f6922011-11-23 19:42:00 -08001017 case USB_GADGET_XPORT_HSIC:
Hemant Kumarc2b17782013-02-03 15:56:29 -08001018 ghsic_ctrl_set_port_name(port_name, name);
1019 ghsic_data_set_port_name(port_name, name);
1020
Jack Pham427f6922011-11-23 19:42:00 -08001021 /*client port number will be updated in gport_setup*/
1022 no_hsic_sports++;
1023 break;
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +05301024 case USB_GADGET_XPORT_HSUART:
1025 /*client port number will be updated in gport_setup*/
1026 no_hsuart_sports++;
1027 break;
Manu Gautama4d993f2011-08-30 18:25:55 +05301028 default:
1029 pr_err("%s: Un-supported transport transport: %u\n",
1030 __func__, gserial_ports[port_num].transport);
1031 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 }
1033
Manu Gautama4d993f2011-08-30 18:25:55 +05301034 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035
Hemant Kumarc2b17782013-02-03 15:56:29 -08001036 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037}