blob: fefbbe6d7bf48bf15e4046b4b35def597b268a33 [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 */
Hemant Kumarbffd4142011-11-03 13:20:40 -070030#define GSERIAL_NO_PORTS 2
David Brownell61d8bae2008-06-19 18:18:50 -070031
32struct gser_descs {
33 struct usb_endpoint_descriptor *in;
34 struct usb_endpoint_descriptor *out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#ifdef CONFIG_MODEM_SUPPORT
36 struct usb_endpoint_descriptor *notify;
37#endif
David Brownell61d8bae2008-06-19 18:18:50 -070038};
39
40struct f_gser {
41 struct gserial port;
42 u8 data_id;
43 u8 port_num;
44
David Brownell61d8bae2008-06-19 18:18:50 -070045 struct gser_descs fs;
David Brownell61d8bae2008-06-19 18:18:50 -070046 struct gser_descs hs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047 u8 online;
48 enum transport_type transport;
49
50#ifdef CONFIG_MODEM_SUPPORT
51 u8 pending;
52 spinlock_t lock;
53 struct usb_ep *notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054 struct usb_request *notify_req;
55
56 struct usb_cdc_line_coding port_line_coding;
57
58 /* SetControlLineState request */
59 u16 port_handshake_bits;
60#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
61#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
62
63 /* SerialState notification */
64 u16 serial_state;
65#define ACM_CTRL_OVERRUN (1 << 6)
66#define ACM_CTRL_PARITY (1 << 5)
67#define ACM_CTRL_FRAMING (1 << 4)
68#define ACM_CTRL_RI (1 << 3)
69#define ACM_CTRL_BRK (1 << 2)
70#define ACM_CTRL_DSR (1 << 1)
71#define ACM_CTRL_DCD (1 << 0)
72#endif
David Brownell61d8bae2008-06-19 18:18:50 -070073};
74
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075static unsigned int no_tty_ports;
76static unsigned int no_sdio_ports;
77static unsigned int no_smd_ports;
Jack Pham427f6922011-11-23 19:42:00 -080078static unsigned int no_hsic_sports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079static unsigned int nr_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
81static struct port_info {
82 enum transport_type transport;
83 unsigned port_num;
84 unsigned client_port_num;
85} gserial_ports[GSERIAL_NO_PORTS];
86
87static inline bool is_transport_sdio(enum transport_type t)
88{
Hemant Kumarbffd4142011-11-03 13:20:40 -070089 if (t == USB_GADGET_XPORT_SDIO)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 return 1;
91 return 0;
92}
93
David Brownell61d8bae2008-06-19 18:18:50 -070094static inline struct f_gser *func_to_gser(struct usb_function *f)
95{
96 return container_of(f, struct f_gser, port.func);
97}
98
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099#ifdef CONFIG_MODEM_SUPPORT
100static inline struct f_gser *port_to_gser(struct gserial *p)
101{
102 return container_of(p, struct f_gser, port);
103}
104#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
105#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
106#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700107/*-------------------------------------------------------------------------*/
108
109/* interface descriptor: */
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700112 .bLength = USB_DT_INTERFACE_SIZE,
113 .bDescriptorType = USB_DT_INTERFACE,
114 /* .bInterfaceNumber = DYNAMIC */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115#ifdef CONFIG_MODEM_SUPPORT
116 .bNumEndpoints = 3,
117#else
David Brownell61d8bae2008-06-19 18:18:50 -0700118 .bNumEndpoints = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700120 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
121 .bInterfaceSubClass = 0,
122 .bInterfaceProtocol = 0,
123 /* .iInterface = DYNAMIC */
124};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125#ifdef CONFIG_MODEM_SUPPORT
126static struct usb_cdc_header_desc gser_header_desc = {
127 .bLength = sizeof(gser_header_desc),
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
130 .bcdCDC = __constant_cpu_to_le16(0x0110),
131};
David Brownell61d8bae2008-06-19 18:18:50 -0700132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133static struct usb_cdc_call_mgmt_descriptor
134gser_call_mgmt_descriptor = {
135 .bLength = sizeof(gser_call_mgmt_descriptor),
136 .bDescriptorType = USB_DT_CS_INTERFACE,
137 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
138 .bmCapabilities = 0,
139 /* .bDataInterface = DYNAMIC */
140};
141
142static struct usb_cdc_acm_descriptor gser_descriptor = {
143 .bLength = sizeof(gser_descriptor),
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubType = USB_CDC_ACM_TYPE,
146 .bmCapabilities = USB_CDC_CAP_LINE,
147};
148
149static struct usb_cdc_union_desc gser_union_desc = {
150 .bLength = sizeof(gser_union_desc),
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_UNION_TYPE,
153 /* .bMasterInterface0 = DYNAMIC */
154 /* .bSlaveInterface0 = DYNAMIC */
155};
156#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700157/* full speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158#ifdef CONFIG_MODEM_SUPPORT
159static struct usb_endpoint_descriptor gser_fs_notify_desc = {
160 .bLength = USB_DT_ENDPOINT_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
162 .bEndpointAddress = USB_DIR_IN,
163 .bmAttributes = USB_ENDPOINT_XFER_INT,
164 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
165 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
166};
167#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700168
Manu Gautama4d993f2011-08-30 18:25:55 +0530169static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = USB_DIR_IN,
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
174};
175
Manu Gautama4d993f2011-08-30 18:25:55 +0530176static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700177 .bLength = USB_DT_ENDPOINT_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
179 .bEndpointAddress = USB_DIR_OUT,
180 .bmAttributes = USB_ENDPOINT_XFER_BULK,
181};
182
Manu Gautama4d993f2011-08-30 18:25:55 +0530183static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700184 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185#ifdef CONFIG_MODEM_SUPPORT
186 (struct usb_descriptor_header *) &gser_header_desc,
187 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
188 (struct usb_descriptor_header *) &gser_descriptor,
189 (struct usb_descriptor_header *) &gser_union_desc,
190 (struct usb_descriptor_header *) &gser_fs_notify_desc,
191#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700192 (struct usb_descriptor_header *) &gser_fs_in_desc,
193 (struct usb_descriptor_header *) &gser_fs_out_desc,
194 NULL,
195};
196
197/* high speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198#ifdef CONFIG_MODEM_SUPPORT
199static struct usb_endpoint_descriptor gser_hs_notify_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202 .bEndpointAddress = USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_INT,
204 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
205 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
206};
207#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700208
Manu Gautama4d993f2011-08-30 18:25:55 +0530209static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700214};
215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700217 .bLength = USB_DT_ENDPOINT_SIZE,
218 .bDescriptorType = USB_DT_ENDPOINT,
219 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700221};
222
Manu Gautama4d993f2011-08-30 18:25:55 +0530223static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700224 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225#ifdef CONFIG_MODEM_SUPPORT
226 (struct usb_descriptor_header *) &gser_header_desc,
227 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
228 (struct usb_descriptor_header *) &gser_descriptor,
229 (struct usb_descriptor_header *) &gser_union_desc,
230 (struct usb_descriptor_header *) &gser_hs_notify_desc,
231#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700232 (struct usb_descriptor_header *) &gser_hs_in_desc,
233 (struct usb_descriptor_header *) &gser_hs_out_desc,
234 NULL,
235};
236
237/* string descriptors: */
238
239static struct usb_string gser_string_defs[] = {
240 [0].s = "Generic Serial",
241 { } /* end of list */
242};
243
244static struct usb_gadget_strings gser_string_table = {
245 .language = 0x0409, /* en-us */
246 .strings = gser_string_defs,
247};
248
249static struct usb_gadget_strings *gser_strings[] = {
250 &gser_string_table,
251 NULL,
252};
253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254static int gport_setup(struct usb_configuration *c)
255{
256 int ret = 0;
Jack Pham427f6922011-11-23 19:42:00 -0800257 int port_idx;
258 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259
Jack Pham427f6922011-11-23 19:42:00 -0800260 pr_debug("%s: no_tty_ports: %u no_sdio_ports: %u"
261 " no_smd_ports: %u no_hsic_sports: %u nr_ports: %u\n",
262 __func__, no_tty_ports, no_sdio_ports, no_smd_ports,
263 no_hsic_sports, nr_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
265 if (no_tty_ports)
266 ret = gserial_setup(c->cdev->gadget, no_tty_ports);
267 if (no_sdio_ports)
268 ret = gsdio_setup(c->cdev->gadget, no_sdio_ports);
269 if (no_smd_ports)
270 ret = gsmd_setup(c->cdev->gadget, no_smd_ports);
Jack Pham427f6922011-11-23 19:42:00 -0800271 if (no_hsic_sports) {
272 port_idx = ghsic_data_setup(no_hsic_sports, USB_GADGET_SERIAL);
273 if (port_idx < 0)
274 return port_idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
Jack Pham427f6922011-11-23 19:42:00 -0800276 for (i = 0; i < nr_ports; i++) {
277 if (gserial_ports[i].transport ==
278 USB_GADGET_XPORT_HSIC) {
279 gserial_ports[i].client_port_num = port_idx;
280 port_idx++;
281 }
282 }
283
284 /*clinet port num is same for data setup and ctrl setup*/
285 ret = ghsic_ctrl_setup(no_hsic_sports, USB_GADGET_SERIAL);
286 if (ret < 0)
287 return ret;
288 return 0;
289 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 return ret;
291}
Manu Gautama4d993f2011-08-30 18:25:55 +0530292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293static int gport_connect(struct f_gser *gser)
294{
Jack Pham427f6922011-11-23 19:42:00 -0800295 unsigned port_num;
296 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297
Jack Pham427f6922011-11-23 19:42:00 -0800298 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700299 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 gser, &gser->port, gser->port_num);
301
302 port_num = gserial_ports[gser->port_num].client_port_num;
303
304 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700305 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 gserial_connect(&gser->port, port_num);
307 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700308 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 gsdio_connect(&gser->port, port_num);
310 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700311 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 gsmd_connect(&gser->port, port_num);
313 break;
Jack Pham427f6922011-11-23 19:42:00 -0800314 case USB_GADGET_XPORT_HSIC:
315 ret = ghsic_ctrl_connect(&gser->port, port_num);
316 if (ret) {
317 pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
318 __func__, ret);
319 return ret;
320 }
321 ret = ghsic_data_connect(&gser->port, port_num);
322 if (ret) {
323 pr_err("%s: ghsic_data_connect failed: err:%d\n",
324 __func__, ret);
325 ghsic_ctrl_disconnect(&gser->port, port_num);
326 return ret;
327 }
328 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 default:
330 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700331 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 return -ENODEV;
333 }
334
335 return 0;
336}
337
338static int gport_disconnect(struct f_gser *gser)
339{
340 unsigned port_num;
341
Jack Pham427f6922011-11-23 19:42:00 -0800342 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700343 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 gser, &gser->port, gser->port_num);
345
346 port_num = gserial_ports[gser->port_num].client_port_num;
347
348 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700349 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 gserial_disconnect(&gser->port);
351 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700352 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 gsdio_disconnect(&gser->port, port_num);
354 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700355 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 gsmd_disconnect(&gser->port, port_num);
357 break;
Jack Pham427f6922011-11-23 19:42:00 -0800358 case USB_GADGET_XPORT_HSIC:
359 ghsic_ctrl_disconnect(&gser->port, port_num);
360 ghsic_data_disconnect(&gser->port, port_num);
361 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 default:
363 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700364 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 return -ENODEV;
366 }
367
368 return 0;
369}
370
371#ifdef CONFIG_MODEM_SUPPORT
372static void gser_complete_set_line_coding(struct usb_ep *ep,
373 struct usb_request *req)
374{
375 struct f_gser *gser = ep->driver_data;
376 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
377
378 if (req->status != 0) {
379 DBG(cdev, "gser ttyGS%d completion, err %d\n",
380 gser->port_num, req->status);
381 return;
382 }
383
384 /* normal completion */
385 if (req->actual != sizeof(gser->port_line_coding)) {
386 DBG(cdev, "gser ttyGS%d short resp, len %d\n",
387 gser->port_num, req->actual);
388 usb_ep_set_halt(ep);
389 } else {
390 struct usb_cdc_line_coding *value = req->buf;
391 gser->port_line_coding = *value;
392 }
393}
David Brownell61d8bae2008-06-19 18:18:50 -0700394/*-------------------------------------------------------------------------*/
395
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396static int
397gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
398{
399 struct f_gser *gser = func_to_gser(f);
400 struct usb_composite_dev *cdev = f->config->cdev;
401 struct usb_request *req = cdev->req;
402 int value = -EOPNOTSUPP;
403 u16 w_index = le16_to_cpu(ctrl->wIndex);
404 u16 w_value = le16_to_cpu(ctrl->wValue);
405 u16 w_length = le16_to_cpu(ctrl->wLength);
406
407 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
408
409 /* SET_LINE_CODING ... just read and save what the host sends */
410 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
411 | USB_CDC_REQ_SET_LINE_CODING:
412 if (w_length != sizeof(struct usb_cdc_line_coding))
413 goto invalid;
414
415 value = w_length;
416 cdev->gadget->ep0->driver_data = gser;
417 req->complete = gser_complete_set_line_coding;
418 break;
419
420 /* GET_LINE_CODING ... return what host sent, or initial value */
421 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
422 | USB_CDC_REQ_GET_LINE_CODING:
423 value = min_t(unsigned, w_length,
424 sizeof(struct usb_cdc_line_coding));
425 memcpy(req->buf, &gser->port_line_coding, value);
426 break;
427
428 /* SET_CONTROL_LINE_STATE ... save what the host sent */
429 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
430 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
431
432 value = 0;
433 gser->port_handshake_bits = w_value;
434 if (gser->port.notify_modem) {
435 unsigned port_num =
436 gserial_ports[gser->port_num].client_port_num;
437
438 gser->port.notify_modem(&gser->port,
439 port_num, w_value);
440 }
441 break;
442
443 default:
444invalid:
445 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
446 ctrl->bRequestType, ctrl->bRequest,
447 w_value, w_index, w_length);
448 }
449
450 /* respond with data transfer or status phase? */
451 if (value >= 0) {
452 DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
453 gser->port_num, ctrl->bRequestType, ctrl->bRequest,
454 w_value, w_index, w_length);
455 req->zero = 0;
456 req->length = value;
457 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
458 if (value < 0)
459 ERROR(cdev, "gser response on ttyGS%d, err %d\n",
460 gser->port_num, value);
461 }
462
463 /* device either stalls (value < 0) or reports success */
464 return value;
465}
466#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700467static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
468{
469 struct f_gser *gser = func_to_gser(f);
470 struct usb_composite_dev *cdev = f->config->cdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 int rc = 0;
David Brownell61d8bae2008-06-19 18:18:50 -0700472
473 /* we know alt == 0, so this is an activation or a reset */
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475#ifdef CONFIG_MODEM_SUPPORT
476 if (gser->notify->driver_data) {
477 DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num);
478 usb_ep_disable(gser->notify);
Anna Perel97b8c222012-01-18 10:08:14 +0200479 gser->notify->driver_data = NULL;
David Brownell61d8bae2008-06-19 18:18:50 -0700480 }
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300481 gser->notify->desc = ep_choose(cdev->gadget,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 gser->hs.notify,
483 gser->fs.notify);
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300484 rc = usb_ep_enable(gser->notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 if (rc) {
486 ERROR(cdev, "can't enable %s, result %d\n",
487 gser->notify->name, rc);
488 return rc;
489 }
490 gser->notify->driver_data = gser;
491#endif
492
493 if (gser->port.in->driver_data) {
494 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
495 gport_disconnect(gser);
496 } else {
497 DBG(cdev, "activate generic data ttyGS%d\n", gser->port_num);
498 }
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300499 gser->port.in->desc = ep_choose(cdev->gadget,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 gser->hs.in, gser->fs.in);
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300501 gser->port.out->desc = ep_choose(cdev->gadget,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 gser->hs.out, gser->fs.out);
503
504 gport_connect(gser);
505
506 gser->online = 1;
507 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700508}
509
510static void gser_disable(struct usb_function *f)
511{
512 struct f_gser *gser = func_to_gser(f);
513 struct usb_composite_dev *cdev = f->config->cdev;
514
515 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516
517 gport_disconnect(gser);
518
519#ifdef CONFIG_MODEM_SUPPORT
520 usb_ep_fifo_flush(gser->notify);
521 usb_ep_disable(gser->notify);
Anna Perel97b8c222012-01-18 10:08:14 +0200522 gser->notify->driver_data = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523#endif
524 gser->online = 0;
525}
526#ifdef CONFIG_MODEM_SUPPORT
527static int gser_notify(struct f_gser *gser, u8 type, u16 value,
528 void *data, unsigned length)
529{
530 struct usb_ep *ep = gser->notify;
531 struct usb_request *req;
532 struct usb_cdc_notification *notify;
533 const unsigned len = sizeof(*notify) + length;
534 void *buf;
535 int status;
536 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
537
538 req = gser->notify_req;
539 gser->notify_req = NULL;
540 gser->pending = false;
541
542 req->length = len;
543 notify = req->buf;
544 buf = notify + 1;
545
546 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
547 | USB_RECIP_INTERFACE;
548 notify->bNotificationType = type;
549 notify->wValue = cpu_to_le16(value);
550 notify->wIndex = cpu_to_le16(gser->data_id);
551 notify->wLength = cpu_to_le16(length);
552 memcpy(buf, data, length);
553
554 status = usb_ep_queue(ep, req, GFP_ATOMIC);
555 if (status < 0) {
556 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
557 gser->port_num, status);
558 gser->notify_req = req;
559 }
560
561 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700562}
563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564static int gser_notify_serial_state(struct f_gser *gser)
565{
566 int status;
567 unsigned long flags;
568 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
569
570 spin_lock_irqsave(&gser->lock, flags);
571 if (gser->notify_req) {
572 DBG(cdev, "gser ttyGS%d serial state %04x\n",
573 gser->port_num, gser->serial_state);
574 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
575 0, &gser->serial_state,
576 sizeof(gser->serial_state));
577 } else {
578 gser->pending = true;
579 status = 0;
580 }
581 spin_unlock_irqrestore(&gser->lock, flags);
582 return status;
583}
584
585static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
586{
587 struct f_gser *gser = req->context;
588 u8 doit = false;
589 unsigned long flags;
590
591 /* on this call path we do NOT hold the port spinlock,
592 * which is why ACM needs its own spinlock
593 */
594 spin_lock_irqsave(&gser->lock, flags);
595 if (req->status != -ESHUTDOWN)
596 doit = gser->pending;
597 gser->notify_req = req;
598 spin_unlock_irqrestore(&gser->lock, flags);
599
600 if (doit && gser->online)
601 gser_notify_serial_state(gser);
602}
603static void gser_connect(struct gserial *port)
604{
605 struct f_gser *gser = port_to_gser(port);
606
607 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
608 gser_notify_serial_state(gser);
609}
610
611unsigned int gser_get_dtr(struct gserial *port)
612{
613 struct f_gser *gser = port_to_gser(port);
614
615 if (gser->port_handshake_bits & ACM_CTRL_DTR)
616 return 1;
617 else
618 return 0;
619}
620
621unsigned int gser_get_rts(struct gserial *port)
622{
623 struct f_gser *gser = port_to_gser(port);
624
625 if (gser->port_handshake_bits & ACM_CTRL_RTS)
626 return 1;
627 else
628 return 0;
629}
630
631unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
632{
633 struct f_gser *gser = port_to_gser(port);
634 u16 state;
635
636 state = gser->serial_state;
637 state &= ~ACM_CTRL_DCD;
638 if (yes)
639 state |= ACM_CTRL_DCD;
640
641 gser->serial_state = state;
642 return gser_notify_serial_state(gser);
643
644}
645
646unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
647{
648 struct f_gser *gser = port_to_gser(port);
649 u16 state;
650
651 state = gser->serial_state;
652 state &= ~ACM_CTRL_RI;
653 if (yes)
654 state |= ACM_CTRL_RI;
655
656 gser->serial_state = state;
657 return gser_notify_serial_state(gser);
658
659}
660static void gser_disconnect(struct gserial *port)
661{
662 struct f_gser *gser = port_to_gser(port);
663
664 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
665 gser_notify_serial_state(gser);
666}
667
668static int gser_send_break(struct gserial *port, int duration)
669{
670 struct f_gser *gser = port_to_gser(port);
671 u16 state;
672
673 state = gser->serial_state;
674 state &= ~ACM_CTRL_BRK;
675 if (duration)
676 state |= ACM_CTRL_BRK;
677
678 gser->serial_state = state;
679 return gser_notify_serial_state(gser);
680}
681
682static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
683{
684 struct f_gser *gser = port_to_gser(port);
685
686 gser->serial_state = ctrl_bits;
687
688 return gser_notify_serial_state(gser);
689}
690#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700691/*-------------------------------------------------------------------------*/
692
693/* serial function driver setup/binding */
694
Manu Gautama4d993f2011-08-30 18:25:55 +0530695static int
David Brownell61d8bae2008-06-19 18:18:50 -0700696gser_bind(struct usb_configuration *c, struct usb_function *f)
697{
698 struct usb_composite_dev *cdev = c->cdev;
699 struct f_gser *gser = func_to_gser(f);
700 int status;
701 struct usb_ep *ep;
702
703 /* allocate instance-specific interface IDs */
704 status = usb_interface_id(c, f);
705 if (status < 0)
706 goto fail;
707 gser->data_id = status;
708 gser_interface_desc.bInterfaceNumber = status;
709
710 status = -ENODEV;
711
712 /* allocate instance-specific endpoints */
713 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
714 if (!ep)
715 goto fail;
716 gser->port.in = ep;
717 ep->driver_data = cdev; /* claim */
718
719 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
720 if (!ep)
721 goto fail;
722 gser->port.out = ep;
723 ep->driver_data = cdev; /* claim */
724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725#ifdef CONFIG_MODEM_SUPPORT
726 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
727 if (!ep)
728 goto fail;
729 gser->notify = ep;
730 ep->driver_data = cdev; /* claim */
731 /* allocate notification */
732 gser->notify_req = gs_alloc_req(ep,
733 sizeof(struct usb_cdc_notification) + 2,
734 GFP_KERNEL);
735 if (!gser->notify_req)
736 goto fail;
737
738 gser->notify_req->complete = gser_notify_complete;
739 gser->notify_req->context = gser;
740#endif
741
David Brownell61d8bae2008-06-19 18:18:50 -0700742 /* copy descriptors, and track endpoint copies */
743 f->descriptors = usb_copy_descriptors(gser_fs_function);
744
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530745 if (!f->descriptors)
746 goto fail;
747
David Brownell61d8bae2008-06-19 18:18:50 -0700748 gser->fs.in = usb_find_endpoint(gser_fs_function,
749 f->descriptors, &gser_fs_in_desc);
750 gser->fs.out = usb_find_endpoint(gser_fs_function,
751 f->descriptors, &gser_fs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752#ifdef CONFIG_MODEM_SUPPORT
753 gser->fs.notify = usb_find_endpoint(gser_fs_function,
754 f->descriptors, &gser_fs_notify_desc);
755#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700756
757
758 /* support all relevant hardware speeds... we expect that when
759 * hardware is dual speed, all bulk-capable endpoints work at
760 * both speeds
761 */
762 if (gadget_is_dualspeed(c->cdev->gadget)) {
763 gser_hs_in_desc.bEndpointAddress =
764 gser_fs_in_desc.bEndpointAddress;
765 gser_hs_out_desc.bEndpointAddress =
766 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767#ifdef CONFIG_MODEM_SUPPORT
768 gser_hs_notify_desc.bEndpointAddress =
769 gser_fs_notify_desc.bEndpointAddress;
770#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700771
772 /* copy descriptors, and track endpoint copies */
773 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
774
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530775 if (!f->hs_descriptors)
776 goto fail;
777
David Brownell61d8bae2008-06-19 18:18:50 -0700778 gser->hs.in = usb_find_endpoint(gser_hs_function,
779 f->hs_descriptors, &gser_hs_in_desc);
780 gser->hs.out = usb_find_endpoint(gser_hs_function,
781 f->hs_descriptors, &gser_hs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782#ifdef CONFIG_MODEM_SUPPORT
783 gser->hs.notify = usb_find_endpoint(gser_hs_function,
784 f->hs_descriptors, &gser_hs_notify_desc);
785#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700786 }
787
788 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
789 gser->port_num,
790 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
791 gser->port.in->name, gser->port.out->name);
792 return 0;
793
794fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530795 if (f->descriptors)
796 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797#ifdef CONFIG_MODEM_SUPPORT
798 if (gser->notify_req)
799 gs_free_req(gser->notify, gser->notify_req);
800
801 /* we might as well release our claims on endpoints */
802 if (gser->notify)
803 gser->notify->driver_data = NULL;
804#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700805 /* we might as well release our claims on endpoints */
806 if (gser->port.out)
807 gser->port.out->driver_data = NULL;
808 if (gser->port.in)
809 gser->port.in->driver_data = NULL;
810
811 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
812
813 return status;
814}
815
816static void
817gser_unbind(struct usb_configuration *c, struct usb_function *f)
818{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819#ifdef CONFIG_MODEM_SUPPORT
820 struct f_gser *gser = func_to_gser(f);
821#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700822 if (gadget_is_dualspeed(c->cdev->gadget))
823 usb_free_descriptors(f->hs_descriptors);
824 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825#ifdef CONFIG_MODEM_SUPPORT
826 gs_free_req(gser->notify, gser->notify_req);
827#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700828 kfree(func_to_gser(f));
829}
830
831/**
832 * gser_bind_config - add a generic serial function to a configuration
833 * @c: the configuration to support the serial instance
834 * @port_num: /dev/ttyGS* port this interface will use
835 * Context: single threaded during gadget setup
836 *
837 * Returns zero on success, else negative errno.
838 *
839 * Caller must have called @gserial_setup() with enough ports to
840 * handle all the ones it binds. Caller is also responsible
841 * for calling @gserial_cleanup() before module unload.
842 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530843int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700844{
845 struct f_gser *gser;
846 int status;
847
848 /* REVISIT might want instance-specific strings to help
849 * distinguish instances ...
850 */
851
852 /* maybe allocate device-global string ID */
853 if (gser_string_defs[0].id == 0) {
854 status = usb_string_id(c->cdev);
855 if (status < 0)
856 return status;
857 gser_string_defs[0].id = status;
858 }
859
860 /* allocate and initialize one new instance */
861 gser = kzalloc(sizeof *gser, GFP_KERNEL);
862 if (!gser)
863 return -ENOMEM;
864
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865#ifdef CONFIG_MODEM_SUPPORT
866 spin_lock_init(&gser->lock);
867#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700868 gser->port_num = port_num;
869
870 gser->port.func.name = "gser";
871 gser->port.func.strings = gser_strings;
872 gser->port.func.bind = gser_bind;
873 gser->port.func.unbind = gser_unbind;
874 gser->port.func.set_alt = gser_set_alt;
875 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 gser->transport = gserial_ports[port_num].transport;
877#ifdef CONFIG_MODEM_SUPPORT
878 /* We support only two ports for now */
879 if (port_num == 0)
880 gser->port.func.name = "modem";
881 else
882 gser->port.func.name = "nmea";
883 gser->port.func.setup = gser_setup;
884 gser->port.connect = gser_connect;
885 gser->port.get_dtr = gser_get_dtr;
886 gser->port.get_rts = gser_get_rts;
887 gser->port.send_carrier_detect = gser_send_carrier_detect;
888 gser->port.send_ring_indicator = gser_send_ring_indicator;
889 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
890 gser->port.disconnect = gser_disconnect;
891 gser->port.send_break = gser_send_break;
892#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700893
894 status = usb_add_function(c, &gser->port.func);
895 if (status)
896 kfree(gser);
897 return status;
898}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899
Manu Gautama4d993f2011-08-30 18:25:55 +0530900/**
901 * gserial_init_port - bind a gserial_port to its transport
902 */
903static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904{
Manu Gautama4d993f2011-08-30 18:25:55 +0530905 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906
Manu Gautama4d993f2011-08-30 18:25:55 +0530907 if (port_num >= GSERIAL_NO_PORTS)
908 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909
Hemant Kumar1b820d52011-11-03 15:08:28 -0700910 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530911 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800912 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913
Manu Gautama4d993f2011-08-30 18:25:55 +0530914 gserial_ports[port_num].transport = transport;
915 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916
Manu Gautama4d993f2011-08-30 18:25:55 +0530917 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700918 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530919 gserial_ports[port_num].client_port_num = no_tty_ports;
920 no_tty_ports++;
921 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700922 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530923 gserial_ports[port_num].client_port_num = no_sdio_ports;
924 no_sdio_ports++;
925 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700926 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530927 gserial_ports[port_num].client_port_num = no_smd_ports;
928 no_smd_ports++;
929 break;
Jack Pham427f6922011-11-23 19:42:00 -0800930 case USB_GADGET_XPORT_HSIC:
931 /*client port number will be updated in gport_setup*/
932 no_hsic_sports++;
933 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530934 default:
935 pr_err("%s: Un-supported transport transport: %u\n",
936 __func__, gserial_ports[port_num].transport);
937 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 }
939
Manu Gautama4d993f2011-08-30 18:25:55 +0530940 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
942 return 0;
943}