blob: cc972ff3343cfa1f7838914d6b921161efafa8ba [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
David Brownac5d1542012-02-06 10:37:22 -080032struct gser_descs {
33 struct usb_endpoint_descriptor *in;
34 struct usb_endpoint_descriptor *out;
35#ifdef CONFIG_MODEM_SUPPORT
36 struct usb_endpoint_descriptor *notify;
37#endif
38};
David Brownell61d8bae2008-06-19 18:18:50 -070039
40struct f_gser {
41 struct gserial port;
42 u8 data_id;
43 u8 port_num;
44
David Brownac5d1542012-02-06 10:37:22 -080045 struct gser_descs fs;
46 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);
David Brownell61d8bae2008-06-19 18:18:50 -0700479 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300480 gser->notify->desc = ep_choose(cdev->gadget,
David Brownac5d1542012-02-06 10:37:22 -0800481 gser->hs.notify,
482 gser->fs.notify);
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300483 rc = usb_ep_enable(gser->notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 if (rc) {
485 ERROR(cdev, "can't enable %s, result %d\n",
486 gser->notify->name, rc);
487 return rc;
488 }
489 gser->notify->driver_data = gser;
490#endif
491
492 if (gser->port.in->driver_data) {
493 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
494 gport_disconnect(gser);
David Brownac5d1542012-02-06 10:37:22 -0800495 } else {
496 DBG(cdev, "activate generic data ttyGS%d\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300498 gser->port.in->desc = ep_choose(cdev->gadget,
David Brownac5d1542012-02-06 10:37:22 -0800499 gser->hs.in, gser->fs.in);
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300500 gser->port.out->desc = ep_choose(cdev->gadget,
David Brownac5d1542012-02-06 10:37:22 -0800501 gser->hs.out, gser->fs.out);
502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 gport_connect(gser);
504
505 gser->online = 1;
506 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700507}
508
509static void gser_disable(struct usb_function *f)
510{
511 struct f_gser *gser = func_to_gser(f);
512 struct usb_composite_dev *cdev = f->config->cdev;
513
514 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 gport_disconnect(gser);
517
518#ifdef CONFIG_MODEM_SUPPORT
519 usb_ep_fifo_flush(gser->notify);
520 usb_ep_disable(gser->notify);
521#endif
522 gser->online = 0;
523}
524#ifdef CONFIG_MODEM_SUPPORT
525static int gser_notify(struct f_gser *gser, u8 type, u16 value,
526 void *data, unsigned length)
527{
528 struct usb_ep *ep = gser->notify;
529 struct usb_request *req;
530 struct usb_cdc_notification *notify;
531 const unsigned len = sizeof(*notify) + length;
532 void *buf;
533 int status;
534 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
535
536 req = gser->notify_req;
537 gser->notify_req = NULL;
538 gser->pending = false;
539
540 req->length = len;
541 notify = req->buf;
542 buf = notify + 1;
543
544 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
545 | USB_RECIP_INTERFACE;
546 notify->bNotificationType = type;
547 notify->wValue = cpu_to_le16(value);
548 notify->wIndex = cpu_to_le16(gser->data_id);
549 notify->wLength = cpu_to_le16(length);
550 memcpy(buf, data, length);
551
552 status = usb_ep_queue(ep, req, GFP_ATOMIC);
553 if (status < 0) {
554 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
555 gser->port_num, status);
556 gser->notify_req = req;
557 }
558
559 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700560}
561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562static int gser_notify_serial_state(struct f_gser *gser)
563{
564 int status;
565 unsigned long flags;
566 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
567
568 spin_lock_irqsave(&gser->lock, flags);
569 if (gser->notify_req) {
570 DBG(cdev, "gser ttyGS%d serial state %04x\n",
571 gser->port_num, gser->serial_state);
572 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
573 0, &gser->serial_state,
574 sizeof(gser->serial_state));
575 } else {
576 gser->pending = true;
577 status = 0;
578 }
579 spin_unlock_irqrestore(&gser->lock, flags);
580 return status;
581}
582
583static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
584{
585 struct f_gser *gser = req->context;
586 u8 doit = false;
587 unsigned long flags;
588
589 /* on this call path we do NOT hold the port spinlock,
590 * which is why ACM needs its own spinlock
591 */
592 spin_lock_irqsave(&gser->lock, flags);
593 if (req->status != -ESHUTDOWN)
594 doit = gser->pending;
595 gser->notify_req = req;
596 spin_unlock_irqrestore(&gser->lock, flags);
597
598 if (doit && gser->online)
599 gser_notify_serial_state(gser);
600}
601static void gser_connect(struct gserial *port)
602{
603 struct f_gser *gser = port_to_gser(port);
604
605 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
606 gser_notify_serial_state(gser);
607}
608
609unsigned int gser_get_dtr(struct gserial *port)
610{
611 struct f_gser *gser = port_to_gser(port);
612
613 if (gser->port_handshake_bits & ACM_CTRL_DTR)
614 return 1;
615 else
616 return 0;
617}
618
619unsigned int gser_get_rts(struct gserial *port)
620{
621 struct f_gser *gser = port_to_gser(port);
622
623 if (gser->port_handshake_bits & ACM_CTRL_RTS)
624 return 1;
625 else
626 return 0;
627}
628
629unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
630{
631 struct f_gser *gser = port_to_gser(port);
632 u16 state;
633
634 state = gser->serial_state;
635 state &= ~ACM_CTRL_DCD;
636 if (yes)
637 state |= ACM_CTRL_DCD;
638
639 gser->serial_state = state;
640 return gser_notify_serial_state(gser);
641
642}
643
644unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
645{
646 struct f_gser *gser = port_to_gser(port);
647 u16 state;
648
649 state = gser->serial_state;
650 state &= ~ACM_CTRL_RI;
651 if (yes)
652 state |= ACM_CTRL_RI;
653
654 gser->serial_state = state;
655 return gser_notify_serial_state(gser);
656
657}
658static void gser_disconnect(struct gserial *port)
659{
660 struct f_gser *gser = port_to_gser(port);
661
662 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
663 gser_notify_serial_state(gser);
664}
665
666static int gser_send_break(struct gserial *port, int duration)
667{
668 struct f_gser *gser = port_to_gser(port);
669 u16 state;
670
671 state = gser->serial_state;
672 state &= ~ACM_CTRL_BRK;
673 if (duration)
674 state |= ACM_CTRL_BRK;
675
676 gser->serial_state = state;
677 return gser_notify_serial_state(gser);
678}
679
680static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
681{
682 struct f_gser *gser = port_to_gser(port);
683
684 gser->serial_state = ctrl_bits;
685
686 return gser_notify_serial_state(gser);
687}
688#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700689/*-------------------------------------------------------------------------*/
690
691/* serial function driver setup/binding */
692
Manu Gautama4d993f2011-08-30 18:25:55 +0530693static int
David Brownell61d8bae2008-06-19 18:18:50 -0700694gser_bind(struct usb_configuration *c, struct usb_function *f)
695{
696 struct usb_composite_dev *cdev = c->cdev;
697 struct f_gser *gser = func_to_gser(f);
698 int status;
699 struct usb_ep *ep;
700
701 /* allocate instance-specific interface IDs */
702 status = usb_interface_id(c, f);
703 if (status < 0)
704 goto fail;
705 gser->data_id = status;
706 gser_interface_desc.bInterfaceNumber = status;
707
708 status = -ENODEV;
709
710 /* allocate instance-specific endpoints */
711 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
712 if (!ep)
713 goto fail;
714 gser->port.in = ep;
715 ep->driver_data = cdev; /* claim */
716
717 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
718 if (!ep)
719 goto fail;
720 gser->port.out = ep;
721 ep->driver_data = cdev; /* claim */
722
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723#ifdef CONFIG_MODEM_SUPPORT
724 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
725 if (!ep)
726 goto fail;
727 gser->notify = ep;
728 ep->driver_data = cdev; /* claim */
729 /* allocate notification */
730 gser->notify_req = gs_alloc_req(ep,
731 sizeof(struct usb_cdc_notification) + 2,
732 GFP_KERNEL);
733 if (!gser->notify_req)
734 goto fail;
735
736 gser->notify_req->complete = gser_notify_complete;
737 gser->notify_req->context = gser;
738#endif
739
David Brownell61d8bae2008-06-19 18:18:50 -0700740 /* copy descriptors, and track endpoint copies */
741 f->descriptors = usb_copy_descriptors(gser_fs_function);
742
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530743 if (!f->descriptors)
744 goto fail;
745
David Brownac5d1542012-02-06 10:37:22 -0800746 gser->fs.in = usb_find_endpoint(gser_fs_function,
747 f->descriptors, &gser_fs_in_desc);
748 gser->fs.out = usb_find_endpoint(gser_fs_function,
749 f->descriptors, &gser_fs_out_desc);
750#ifdef CONFIG_MODEM_SUPPORT
751 gser->fs.notify = usb_find_endpoint(gser_fs_function,
752 f->descriptors, &gser_fs_notify_desc);
753#endif
754
755
David Brownell61d8bae2008-06-19 18:18:50 -0700756 /* support all relevant hardware speeds... we expect that when
757 * hardware is dual speed, all bulk-capable endpoints work at
758 * both speeds
759 */
760 if (gadget_is_dualspeed(c->cdev->gadget)) {
761 gser_hs_in_desc.bEndpointAddress =
762 gser_fs_in_desc.bEndpointAddress;
763 gser_hs_out_desc.bEndpointAddress =
764 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765#ifdef CONFIG_MODEM_SUPPORT
766 gser_hs_notify_desc.bEndpointAddress =
767 gser_fs_notify_desc.bEndpointAddress;
768#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700769
770 /* copy descriptors, and track endpoint copies */
771 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
772
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530773 if (!f->hs_descriptors)
774 goto fail;
775
David Brownac5d1542012-02-06 10:37:22 -0800776 gser->hs.in = usb_find_endpoint(gser_hs_function,
777 f->hs_descriptors, &gser_hs_in_desc);
778 gser->hs.out = usb_find_endpoint(gser_hs_function,
779 f->hs_descriptors, &gser_hs_out_desc);
780#ifdef CONFIG_MODEM_SUPPORT
781 gser->hs.notify = usb_find_endpoint(gser_hs_function,
782 f->hs_descriptors, &gser_hs_notify_desc);
783#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700784 }
785
786 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
787 gser->port_num,
788 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
789 gser->port.in->name, gser->port.out->name);
790 return 0;
791
792fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530793 if (f->descriptors)
794 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795#ifdef CONFIG_MODEM_SUPPORT
796 if (gser->notify_req)
797 gs_free_req(gser->notify, gser->notify_req);
798
799 /* we might as well release our claims on endpoints */
800 if (gser->notify)
801 gser->notify->driver_data = NULL;
802#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700803 /* we might as well release our claims on endpoints */
804 if (gser->port.out)
805 gser->port.out->driver_data = NULL;
806 if (gser->port.in)
807 gser->port.in->driver_data = NULL;
808
809 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
810
811 return status;
812}
813
814static void
815gser_unbind(struct usb_configuration *c, struct usb_function *f)
816{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817#ifdef CONFIG_MODEM_SUPPORT
818 struct f_gser *gser = func_to_gser(f);
819#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700820 if (gadget_is_dualspeed(c->cdev->gadget))
821 usb_free_descriptors(f->hs_descriptors);
822 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823#ifdef CONFIG_MODEM_SUPPORT
824 gs_free_req(gser->notify, gser->notify_req);
825#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700826 kfree(func_to_gser(f));
827}
828
829/**
830 * gser_bind_config - add a generic serial function to a configuration
831 * @c: the configuration to support the serial instance
832 * @port_num: /dev/ttyGS* port this interface will use
833 * Context: single threaded during gadget setup
834 *
835 * Returns zero on success, else negative errno.
836 *
837 * Caller must have called @gserial_setup() with enough ports to
838 * handle all the ones it binds. Caller is also responsible
839 * for calling @gserial_cleanup() before module unload.
840 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530841int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700842{
843 struct f_gser *gser;
844 int status;
845
846 /* REVISIT might want instance-specific strings to help
847 * distinguish instances ...
848 */
849
850 /* maybe allocate device-global string ID */
851 if (gser_string_defs[0].id == 0) {
852 status = usb_string_id(c->cdev);
853 if (status < 0)
854 return status;
855 gser_string_defs[0].id = status;
856 }
857
858 /* allocate and initialize one new instance */
859 gser = kzalloc(sizeof *gser, GFP_KERNEL);
860 if (!gser)
861 return -ENOMEM;
862
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863#ifdef CONFIG_MODEM_SUPPORT
864 spin_lock_init(&gser->lock);
865#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700866 gser->port_num = port_num;
867
868 gser->port.func.name = "gser";
869 gser->port.func.strings = gser_strings;
870 gser->port.func.bind = gser_bind;
871 gser->port.func.unbind = gser_unbind;
872 gser->port.func.set_alt = gser_set_alt;
873 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 gser->transport = gserial_ports[port_num].transport;
875#ifdef CONFIG_MODEM_SUPPORT
876 /* We support only two ports for now */
877 if (port_num == 0)
878 gser->port.func.name = "modem";
879 else
880 gser->port.func.name = "nmea";
881 gser->port.func.setup = gser_setup;
882 gser->port.connect = gser_connect;
883 gser->port.get_dtr = gser_get_dtr;
884 gser->port.get_rts = gser_get_rts;
885 gser->port.send_carrier_detect = gser_send_carrier_detect;
886 gser->port.send_ring_indicator = gser_send_ring_indicator;
887 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
888 gser->port.disconnect = gser_disconnect;
889 gser->port.send_break = gser_send_break;
890#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700891
892 status = usb_add_function(c, &gser->port.func);
893 if (status)
894 kfree(gser);
895 return status;
896}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
Manu Gautama4d993f2011-08-30 18:25:55 +0530898/**
899 * gserial_init_port - bind a gserial_port to its transport
900 */
901static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902{
Manu Gautama4d993f2011-08-30 18:25:55 +0530903 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904
Manu Gautama4d993f2011-08-30 18:25:55 +0530905 if (port_num >= GSERIAL_NO_PORTS)
906 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907
Hemant Kumar1b820d52011-11-03 15:08:28 -0700908 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530909 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800910 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911
Manu Gautama4d993f2011-08-30 18:25:55 +0530912 gserial_ports[port_num].transport = transport;
913 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914
Manu Gautama4d993f2011-08-30 18:25:55 +0530915 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700916 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530917 gserial_ports[port_num].client_port_num = no_tty_ports;
918 no_tty_ports++;
919 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700920 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530921 gserial_ports[port_num].client_port_num = no_sdio_ports;
922 no_sdio_ports++;
923 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700924 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530925 gserial_ports[port_num].client_port_num = no_smd_ports;
926 no_smd_ports++;
927 break;
Jack Pham427f6922011-11-23 19:42:00 -0800928 case USB_GADGET_XPORT_HSIC:
929 /*client port number will be updated in gport_setup*/
930 no_hsic_sports++;
931 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530932 default:
933 pr_err("%s: Un-supported transport transport: %u\n",
934 __func__, gserial_ports[port_num].transport);
935 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 }
937
Manu Gautama4d993f2011-08-30 18:25:55 +0530938 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939
940 return 0;
941}