blob: de8c8ed51b30a714d207b374a1612e9c7befb1d7 [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;
David Brownac5d1542012-02-06 10:37:22 -080054 struct usb_endpoint_descriptor *notify_desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055 struct usb_request *notify_req;
56
57 struct usb_cdc_line_coding port_line_coding;
58
59 /* SetControlLineState request */
60 u16 port_handshake_bits;
61#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
62#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
63
64 /* SerialState notification */
65 u16 serial_state;
66#define ACM_CTRL_OVERRUN (1 << 6)
67#define ACM_CTRL_PARITY (1 << 5)
68#define ACM_CTRL_FRAMING (1 << 4)
69#define ACM_CTRL_RI (1 << 3)
70#define ACM_CTRL_BRK (1 << 2)
71#define ACM_CTRL_DSR (1 << 1)
72#define ACM_CTRL_DCD (1 << 0)
73#endif
David Brownell61d8bae2008-06-19 18:18:50 -070074};
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static unsigned int no_tty_ports;
77static unsigned int no_sdio_ports;
78static unsigned int no_smd_ports;
Jack Pham427f6922011-11-23 19:42:00 -080079static unsigned int no_hsic_sports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static unsigned int nr_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081
82static struct port_info {
83 enum transport_type transport;
84 unsigned port_num;
85 unsigned client_port_num;
86} gserial_ports[GSERIAL_NO_PORTS];
87
88static inline bool is_transport_sdio(enum transport_type t)
89{
Hemant Kumarbffd4142011-11-03 13:20:40 -070090 if (t == USB_GADGET_XPORT_SDIO)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 return 1;
92 return 0;
93}
94
David Brownell61d8bae2008-06-19 18:18:50 -070095static inline struct f_gser *func_to_gser(struct usb_function *f)
96{
97 return container_of(f, struct f_gser, port.func);
98}
99
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100#ifdef CONFIG_MODEM_SUPPORT
101static inline struct f_gser *port_to_gser(struct gserial *p)
102{
103 return container_of(p, struct f_gser, port);
104}
105#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
106#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
107#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700108/*-------------------------------------------------------------------------*/
109
110/* interface descriptor: */
111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700113 .bLength = USB_DT_INTERFACE_SIZE,
114 .bDescriptorType = USB_DT_INTERFACE,
115 /* .bInterfaceNumber = DYNAMIC */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#ifdef CONFIG_MODEM_SUPPORT
117 .bNumEndpoints = 3,
118#else
David Brownell61d8bae2008-06-19 18:18:50 -0700119 .bNumEndpoints = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700121 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
122 .bInterfaceSubClass = 0,
123 .bInterfaceProtocol = 0,
124 /* .iInterface = DYNAMIC */
125};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126#ifdef CONFIG_MODEM_SUPPORT
127static struct usb_cdc_header_desc gser_header_desc = {
128 .bLength = sizeof(gser_header_desc),
129 .bDescriptorType = USB_DT_CS_INTERFACE,
130 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
131 .bcdCDC = __constant_cpu_to_le16(0x0110),
132};
David Brownell61d8bae2008-06-19 18:18:50 -0700133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134static struct usb_cdc_call_mgmt_descriptor
135gser_call_mgmt_descriptor = {
136 .bLength = sizeof(gser_call_mgmt_descriptor),
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
139 .bmCapabilities = 0,
140 /* .bDataInterface = DYNAMIC */
141};
142
143static struct usb_cdc_acm_descriptor gser_descriptor = {
144 .bLength = sizeof(gser_descriptor),
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_ACM_TYPE,
147 .bmCapabilities = USB_CDC_CAP_LINE,
148};
149
150static struct usb_cdc_union_desc gser_union_desc = {
151 .bLength = sizeof(gser_union_desc),
152 .bDescriptorType = USB_DT_CS_INTERFACE,
153 .bDescriptorSubType = USB_CDC_UNION_TYPE,
154 /* .bMasterInterface0 = DYNAMIC */
155 /* .bSlaveInterface0 = DYNAMIC */
156};
157#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700158/* full speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159#ifdef CONFIG_MODEM_SUPPORT
160static struct usb_endpoint_descriptor gser_fs_notify_desc = {
161 .bLength = USB_DT_ENDPOINT_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_INT,
165 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
166 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
167};
168#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700169
Manu Gautama4d993f2011-08-30 18:25:55 +0530170static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173 .bEndpointAddress = USB_DIR_IN,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175};
176
Manu Gautama4d993f2011-08-30 18:25:55 +0530177static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700178 .bLength = USB_DT_ENDPOINT_SIZE,
179 .bDescriptorType = USB_DT_ENDPOINT,
180 .bEndpointAddress = USB_DIR_OUT,
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
182};
183
Manu Gautama4d993f2011-08-30 18:25:55 +0530184static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700185 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186#ifdef CONFIG_MODEM_SUPPORT
187 (struct usb_descriptor_header *) &gser_header_desc,
188 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
189 (struct usb_descriptor_header *) &gser_descriptor,
190 (struct usb_descriptor_header *) &gser_union_desc,
191 (struct usb_descriptor_header *) &gser_fs_notify_desc,
192#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700193 (struct usb_descriptor_header *) &gser_fs_in_desc,
194 (struct usb_descriptor_header *) &gser_fs_out_desc,
195 NULL,
196};
197
198/* high speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199#ifdef CONFIG_MODEM_SUPPORT
200static struct usb_endpoint_descriptor gser_hs_notify_desc = {
201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203 .bEndpointAddress = USB_DIR_IN,
204 .bmAttributes = USB_ENDPOINT_XFER_INT,
205 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
206 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
207};
208#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700209
Manu Gautama4d993f2011-08-30 18:25:55 +0530210static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700211 .bLength = USB_DT_ENDPOINT_SIZE,
212 .bDescriptorType = USB_DT_ENDPOINT,
213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700215};
216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700218 .bLength = USB_DT_ENDPOINT_SIZE,
219 .bDescriptorType = USB_DT_ENDPOINT,
220 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700222};
223
Manu Gautama4d993f2011-08-30 18:25:55 +0530224static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700225 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226#ifdef CONFIG_MODEM_SUPPORT
227 (struct usb_descriptor_header *) &gser_header_desc,
228 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
229 (struct usb_descriptor_header *) &gser_descriptor,
230 (struct usb_descriptor_header *) &gser_union_desc,
231 (struct usb_descriptor_header *) &gser_hs_notify_desc,
232#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700233 (struct usb_descriptor_header *) &gser_hs_in_desc,
234 (struct usb_descriptor_header *) &gser_hs_out_desc,
235 NULL,
236};
237
238/* string descriptors: */
239
240static struct usb_string gser_string_defs[] = {
241 [0].s = "Generic Serial",
242 { } /* end of list */
243};
244
245static struct usb_gadget_strings gser_string_table = {
246 .language = 0x0409, /* en-us */
247 .strings = gser_string_defs,
248};
249
250static struct usb_gadget_strings *gser_strings[] = {
251 &gser_string_table,
252 NULL,
253};
254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255static int gport_setup(struct usb_configuration *c)
256{
257 int ret = 0;
Jack Pham427f6922011-11-23 19:42:00 -0800258 int port_idx;
259 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260
Jack Pham427f6922011-11-23 19:42:00 -0800261 pr_debug("%s: no_tty_ports: %u no_sdio_ports: %u"
262 " no_smd_ports: %u no_hsic_sports: %u nr_ports: %u\n",
263 __func__, no_tty_ports, no_sdio_ports, no_smd_ports,
264 no_hsic_sports, nr_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265
266 if (no_tty_ports)
267 ret = gserial_setup(c->cdev->gadget, no_tty_ports);
268 if (no_sdio_ports)
269 ret = gsdio_setup(c->cdev->gadget, no_sdio_ports);
270 if (no_smd_ports)
271 ret = gsmd_setup(c->cdev->gadget, no_smd_ports);
Jack Pham427f6922011-11-23 19:42:00 -0800272 if (no_hsic_sports) {
273 port_idx = ghsic_data_setup(no_hsic_sports, USB_GADGET_SERIAL);
274 if (port_idx < 0)
275 return port_idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276
Jack Pham427f6922011-11-23 19:42:00 -0800277 for (i = 0; i < nr_ports; i++) {
278 if (gserial_ports[i].transport ==
279 USB_GADGET_XPORT_HSIC) {
280 gserial_ports[i].client_port_num = port_idx;
281 port_idx++;
282 }
283 }
284
285 /*clinet port num is same for data setup and ctrl setup*/
286 ret = ghsic_ctrl_setup(no_hsic_sports, USB_GADGET_SERIAL);
287 if (ret < 0)
288 return ret;
289 return 0;
290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 return ret;
292}
Manu Gautama4d993f2011-08-30 18:25:55 +0530293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294static int gport_connect(struct f_gser *gser)
295{
Jack Pham427f6922011-11-23 19:42:00 -0800296 unsigned port_num;
297 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
Jack Pham427f6922011-11-23 19:42:00 -0800299 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700300 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 gser, &gser->port, gser->port_num);
302
303 port_num = gserial_ports[gser->port_num].client_port_num;
304
305 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700306 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 gserial_connect(&gser->port, port_num);
308 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700309 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 gsdio_connect(&gser->port, port_num);
311 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700312 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 gsmd_connect(&gser->port, port_num);
314 break;
Jack Pham427f6922011-11-23 19:42:00 -0800315 case USB_GADGET_XPORT_HSIC:
316 ret = ghsic_ctrl_connect(&gser->port, port_num);
317 if (ret) {
318 pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
319 __func__, ret);
320 return ret;
321 }
322 ret = ghsic_data_connect(&gser->port, port_num);
323 if (ret) {
324 pr_err("%s: ghsic_data_connect failed: err:%d\n",
325 __func__, ret);
326 ghsic_ctrl_disconnect(&gser->port, port_num);
327 return ret;
328 }
329 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 default:
331 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700332 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 return -ENODEV;
334 }
335
336 return 0;
337}
338
339static int gport_disconnect(struct f_gser *gser)
340{
341 unsigned port_num;
342
Jack Pham427f6922011-11-23 19:42:00 -0800343 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700344 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 gser, &gser->port, gser->port_num);
346
347 port_num = gserial_ports[gser->port_num].client_port_num;
348
349 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700350 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 gserial_disconnect(&gser->port);
352 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700353 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 gsdio_disconnect(&gser->port, port_num);
355 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700356 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 gsmd_disconnect(&gser->port, port_num);
358 break;
Jack Pham427f6922011-11-23 19:42:00 -0800359 case USB_GADGET_XPORT_HSIC:
360 ghsic_ctrl_disconnect(&gser->port, port_num);
361 ghsic_data_disconnect(&gser->port, port_num);
362 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 default:
364 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700365 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366 return -ENODEV;
367 }
368
369 return 0;
370}
371
372#ifdef CONFIG_MODEM_SUPPORT
373static void gser_complete_set_line_coding(struct usb_ep *ep,
374 struct usb_request *req)
375{
376 struct f_gser *gser = ep->driver_data;
377 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
378
379 if (req->status != 0) {
380 DBG(cdev, "gser ttyGS%d completion, err %d\n",
381 gser->port_num, req->status);
382 return;
383 }
384
385 /* normal completion */
386 if (req->actual != sizeof(gser->port_line_coding)) {
387 DBG(cdev, "gser ttyGS%d short resp, len %d\n",
388 gser->port_num, req->actual);
389 usb_ep_set_halt(ep);
390 } else {
391 struct usb_cdc_line_coding *value = req->buf;
392 gser->port_line_coding = *value;
393 }
394}
David Brownell61d8bae2008-06-19 18:18:50 -0700395/*-------------------------------------------------------------------------*/
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397static int
398gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
399{
400 struct f_gser *gser = func_to_gser(f);
401 struct usb_composite_dev *cdev = f->config->cdev;
402 struct usb_request *req = cdev->req;
403 int value = -EOPNOTSUPP;
404 u16 w_index = le16_to_cpu(ctrl->wIndex);
405 u16 w_value = le16_to_cpu(ctrl->wValue);
406 u16 w_length = le16_to_cpu(ctrl->wLength);
407
408 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
409
410 /* SET_LINE_CODING ... just read and save what the host sends */
411 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
412 | USB_CDC_REQ_SET_LINE_CODING:
413 if (w_length != sizeof(struct usb_cdc_line_coding))
414 goto invalid;
415
416 value = w_length;
417 cdev->gadget->ep0->driver_data = gser;
418 req->complete = gser_complete_set_line_coding;
419 break;
420
421 /* GET_LINE_CODING ... return what host sent, or initial value */
422 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
423 | USB_CDC_REQ_GET_LINE_CODING:
424 value = min_t(unsigned, w_length,
425 sizeof(struct usb_cdc_line_coding));
426 memcpy(req->buf, &gser->port_line_coding, value);
427 break;
428
429 /* SET_CONTROL_LINE_STATE ... save what the host sent */
430 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
431 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
432
433 value = 0;
434 gser->port_handshake_bits = w_value;
435 if (gser->port.notify_modem) {
436 unsigned port_num =
437 gserial_ports[gser->port_num].client_port_num;
438
439 gser->port.notify_modem(&gser->port,
440 port_num, w_value);
441 }
442 break;
443
444 default:
445invalid:
446 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
447 ctrl->bRequestType, ctrl->bRequest,
448 w_value, w_index, w_length);
449 }
450
451 /* respond with data transfer or status phase? */
452 if (value >= 0) {
453 DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
454 gser->port_num, ctrl->bRequestType, ctrl->bRequest,
455 w_value, w_index, w_length);
456 req->zero = 0;
457 req->length = value;
458 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
459 if (value < 0)
460 ERROR(cdev, "gser response on ttyGS%d, err %d\n",
461 gser->port_num, value);
462 }
463
464 /* device either stalls (value < 0) or reports success */
465 return value;
466}
467#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700468static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
469{
470 struct f_gser *gser = func_to_gser(f);
471 struct usb_composite_dev *cdev = f->config->cdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 int rc = 0;
David Brownell61d8bae2008-06-19 18:18:50 -0700473
474 /* we know alt == 0, so this is an activation or a reset */
475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476#ifdef CONFIG_MODEM_SUPPORT
477 if (gser->notify->driver_data) {
478 DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num);
479 usb_ep_disable(gser->notify);
David Brownell61d8bae2008-06-19 18:18:50 -0700480 }
David Brownac5d1542012-02-06 10:37:22 -0800481 gser->notify_desc = ep_choose(cdev->gadget,
482 gser->hs.notify,
483 gser->fs.notify);
484 rc = usb_ep_enable(gser->notify, gser->notify_desc);
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);
David Brownac5d1542012-02-06 10:37:22 -0800496 } else {
497 DBG(cdev, "activate generic data ttyGS%d\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 }
David Brownac5d1542012-02-06 10:37:22 -0800499 gser->port.in_desc = ep_choose(cdev->gadget,
500 gser->hs.in, gser->fs.in);
501 gser->port.out_desc = ep_choose(cdev->gadget,
502 gser->hs.out, gser->fs.out);
503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 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);
522#endif
523 gser->online = 0;
524}
525#ifdef CONFIG_MODEM_SUPPORT
526static int gser_notify(struct f_gser *gser, u8 type, u16 value,
527 void *data, unsigned length)
528{
529 struct usb_ep *ep = gser->notify;
530 struct usb_request *req;
531 struct usb_cdc_notification *notify;
532 const unsigned len = sizeof(*notify) + length;
533 void *buf;
534 int status;
535 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
536
537 req = gser->notify_req;
538 gser->notify_req = NULL;
539 gser->pending = false;
540
541 req->length = len;
542 notify = req->buf;
543 buf = notify + 1;
544
545 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
546 | USB_RECIP_INTERFACE;
547 notify->bNotificationType = type;
548 notify->wValue = cpu_to_le16(value);
549 notify->wIndex = cpu_to_le16(gser->data_id);
550 notify->wLength = cpu_to_le16(length);
551 memcpy(buf, data, length);
552
553 status = usb_ep_queue(ep, req, GFP_ATOMIC);
554 if (status < 0) {
555 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
556 gser->port_num, status);
557 gser->notify_req = req;
558 }
559
560 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700561}
562
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563static int gser_notify_serial_state(struct f_gser *gser)
564{
565 int status;
566 unsigned long flags;
567 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
568
569 spin_lock_irqsave(&gser->lock, flags);
570 if (gser->notify_req) {
571 DBG(cdev, "gser ttyGS%d serial state %04x\n",
572 gser->port_num, gser->serial_state);
573 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
574 0, &gser->serial_state,
575 sizeof(gser->serial_state));
576 } else {
577 gser->pending = true;
578 status = 0;
579 }
580 spin_unlock_irqrestore(&gser->lock, flags);
581 return status;
582}
583
584static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
585{
586 struct f_gser *gser = req->context;
587 u8 doit = false;
588 unsigned long flags;
589
590 /* on this call path we do NOT hold the port spinlock,
591 * which is why ACM needs its own spinlock
592 */
593 spin_lock_irqsave(&gser->lock, flags);
594 if (req->status != -ESHUTDOWN)
595 doit = gser->pending;
596 gser->notify_req = req;
597 spin_unlock_irqrestore(&gser->lock, flags);
598
599 if (doit && gser->online)
600 gser_notify_serial_state(gser);
601}
602static void gser_connect(struct gserial *port)
603{
604 struct f_gser *gser = port_to_gser(port);
605
606 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
607 gser_notify_serial_state(gser);
608}
609
610unsigned int gser_get_dtr(struct gserial *port)
611{
612 struct f_gser *gser = port_to_gser(port);
613
614 if (gser->port_handshake_bits & ACM_CTRL_DTR)
615 return 1;
616 else
617 return 0;
618}
619
620unsigned int gser_get_rts(struct gserial *port)
621{
622 struct f_gser *gser = port_to_gser(port);
623
624 if (gser->port_handshake_bits & ACM_CTRL_RTS)
625 return 1;
626 else
627 return 0;
628}
629
630unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
631{
632 struct f_gser *gser = port_to_gser(port);
633 u16 state;
634
635 state = gser->serial_state;
636 state &= ~ACM_CTRL_DCD;
637 if (yes)
638 state |= ACM_CTRL_DCD;
639
640 gser->serial_state = state;
641 return gser_notify_serial_state(gser);
642
643}
644
645unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
646{
647 struct f_gser *gser = port_to_gser(port);
648 u16 state;
649
650 state = gser->serial_state;
651 state &= ~ACM_CTRL_RI;
652 if (yes)
653 state |= ACM_CTRL_RI;
654
655 gser->serial_state = state;
656 return gser_notify_serial_state(gser);
657
658}
659static void gser_disconnect(struct gserial *port)
660{
661 struct f_gser *gser = port_to_gser(port);
662
663 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
664 gser_notify_serial_state(gser);
665}
666
667static int gser_send_break(struct gserial *port, int duration)
668{
669 struct f_gser *gser = port_to_gser(port);
670 u16 state;
671
672 state = gser->serial_state;
673 state &= ~ACM_CTRL_BRK;
674 if (duration)
675 state |= ACM_CTRL_BRK;
676
677 gser->serial_state = state;
678 return gser_notify_serial_state(gser);
679}
680
681static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
682{
683 struct f_gser *gser = port_to_gser(port);
684
685 gser->serial_state = ctrl_bits;
686
687 return gser_notify_serial_state(gser);
688}
689#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700690/*-------------------------------------------------------------------------*/
691
692/* serial function driver setup/binding */
693
Manu Gautama4d993f2011-08-30 18:25:55 +0530694static int
David Brownell61d8bae2008-06-19 18:18:50 -0700695gser_bind(struct usb_configuration *c, struct usb_function *f)
696{
697 struct usb_composite_dev *cdev = c->cdev;
698 struct f_gser *gser = func_to_gser(f);
699 int status;
700 struct usb_ep *ep;
701
702 /* allocate instance-specific interface IDs */
703 status = usb_interface_id(c, f);
704 if (status < 0)
705 goto fail;
706 gser->data_id = status;
707 gser_interface_desc.bInterfaceNumber = status;
708
709 status = -ENODEV;
710
711 /* allocate instance-specific endpoints */
712 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
713 if (!ep)
714 goto fail;
715 gser->port.in = ep;
716 ep->driver_data = cdev; /* claim */
717
718 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
719 if (!ep)
720 goto fail;
721 gser->port.out = ep;
722 ep->driver_data = cdev; /* claim */
723
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724#ifdef CONFIG_MODEM_SUPPORT
725 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
726 if (!ep)
727 goto fail;
728 gser->notify = ep;
729 ep->driver_data = cdev; /* claim */
730 /* allocate notification */
731 gser->notify_req = gs_alloc_req(ep,
732 sizeof(struct usb_cdc_notification) + 2,
733 GFP_KERNEL);
734 if (!gser->notify_req)
735 goto fail;
736
737 gser->notify_req->complete = gser_notify_complete;
738 gser->notify_req->context = gser;
739#endif
740
David Brownell61d8bae2008-06-19 18:18:50 -0700741 /* copy descriptors, and track endpoint copies */
742 f->descriptors = usb_copy_descriptors(gser_fs_function);
743
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530744 if (!f->descriptors)
745 goto fail;
746
David Brownac5d1542012-02-06 10:37:22 -0800747 gser->fs.in = usb_find_endpoint(gser_fs_function,
748 f->descriptors, &gser_fs_in_desc);
749 gser->fs.out = usb_find_endpoint(gser_fs_function,
750 f->descriptors, &gser_fs_out_desc);
751#ifdef CONFIG_MODEM_SUPPORT
752 gser->fs.notify = usb_find_endpoint(gser_fs_function,
753 f->descriptors, &gser_fs_notify_desc);
754#endif
755
756
David Brownell61d8bae2008-06-19 18:18:50 -0700757 /* support all relevant hardware speeds... we expect that when
758 * hardware is dual speed, all bulk-capable endpoints work at
759 * both speeds
760 */
761 if (gadget_is_dualspeed(c->cdev->gadget)) {
762 gser_hs_in_desc.bEndpointAddress =
763 gser_fs_in_desc.bEndpointAddress;
764 gser_hs_out_desc.bEndpointAddress =
765 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766#ifdef CONFIG_MODEM_SUPPORT
767 gser_hs_notify_desc.bEndpointAddress =
768 gser_fs_notify_desc.bEndpointAddress;
769#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700770
771 /* copy descriptors, and track endpoint copies */
772 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
773
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530774 if (!f->hs_descriptors)
775 goto fail;
776
David Brownac5d1542012-02-06 10:37:22 -0800777 gser->hs.in = usb_find_endpoint(gser_hs_function,
778 f->hs_descriptors, &gser_hs_in_desc);
779 gser->hs.out = usb_find_endpoint(gser_hs_function,
780 f->hs_descriptors, &gser_hs_out_desc);
781#ifdef CONFIG_MODEM_SUPPORT
782 gser->hs.notify = usb_find_endpoint(gser_hs_function,
783 f->hs_descriptors, &gser_hs_notify_desc);
784#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700785 }
786
787 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
788 gser->port_num,
789 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
790 gser->port.in->name, gser->port.out->name);
791 return 0;
792
793fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530794 if (f->descriptors)
795 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796#ifdef CONFIG_MODEM_SUPPORT
797 if (gser->notify_req)
798 gs_free_req(gser->notify, gser->notify_req);
799
800 /* we might as well release our claims on endpoints */
801 if (gser->notify)
802 gser->notify->driver_data = NULL;
803#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700804 /* we might as well release our claims on endpoints */
805 if (gser->port.out)
806 gser->port.out->driver_data = NULL;
807 if (gser->port.in)
808 gser->port.in->driver_data = NULL;
809
810 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
811
812 return status;
813}
814
815static void
816gser_unbind(struct usb_configuration *c, struct usb_function *f)
817{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818#ifdef CONFIG_MODEM_SUPPORT
819 struct f_gser *gser = func_to_gser(f);
820#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700821 if (gadget_is_dualspeed(c->cdev->gadget))
822 usb_free_descriptors(f->hs_descriptors);
823 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824#ifdef CONFIG_MODEM_SUPPORT
825 gs_free_req(gser->notify, gser->notify_req);
826#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700827 kfree(func_to_gser(f));
828}
829
830/**
831 * gser_bind_config - add a generic serial function to a configuration
832 * @c: the configuration to support the serial instance
833 * @port_num: /dev/ttyGS* port this interface will use
834 * Context: single threaded during gadget setup
835 *
836 * Returns zero on success, else negative errno.
837 *
838 * Caller must have called @gserial_setup() with enough ports to
839 * handle all the ones it binds. Caller is also responsible
840 * for calling @gserial_cleanup() before module unload.
841 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530842int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700843{
844 struct f_gser *gser;
845 int status;
846
847 /* REVISIT might want instance-specific strings to help
848 * distinguish instances ...
849 */
850
851 /* maybe allocate device-global string ID */
852 if (gser_string_defs[0].id == 0) {
853 status = usb_string_id(c->cdev);
854 if (status < 0)
855 return status;
856 gser_string_defs[0].id = status;
857 }
858
859 /* allocate and initialize one new instance */
860 gser = kzalloc(sizeof *gser, GFP_KERNEL);
861 if (!gser)
862 return -ENOMEM;
863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864#ifdef CONFIG_MODEM_SUPPORT
865 spin_lock_init(&gser->lock);
866#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700867 gser->port_num = port_num;
868
869 gser->port.func.name = "gser";
870 gser->port.func.strings = gser_strings;
871 gser->port.func.bind = gser_bind;
872 gser->port.func.unbind = gser_unbind;
873 gser->port.func.set_alt = gser_set_alt;
874 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 gser->transport = gserial_ports[port_num].transport;
876#ifdef CONFIG_MODEM_SUPPORT
877 /* We support only two ports for now */
878 if (port_num == 0)
879 gser->port.func.name = "modem";
880 else
881 gser->port.func.name = "nmea";
882 gser->port.func.setup = gser_setup;
883 gser->port.connect = gser_connect;
884 gser->port.get_dtr = gser_get_dtr;
885 gser->port.get_rts = gser_get_rts;
886 gser->port.send_carrier_detect = gser_send_carrier_detect;
887 gser->port.send_ring_indicator = gser_send_ring_indicator;
888 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
889 gser->port.disconnect = gser_disconnect;
890 gser->port.send_break = gser_send_break;
891#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700892
893 status = usb_add_function(c, &gser->port.func);
894 if (status)
895 kfree(gser);
896 return status;
897}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898
Manu Gautama4d993f2011-08-30 18:25:55 +0530899/**
900 * gserial_init_port - bind a gserial_port to its transport
901 */
902static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903{
Manu Gautama4d993f2011-08-30 18:25:55 +0530904 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905
Manu Gautama4d993f2011-08-30 18:25:55 +0530906 if (port_num >= GSERIAL_NO_PORTS)
907 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908
Hemant Kumar1b820d52011-11-03 15:08:28 -0700909 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530910 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800911 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912
Manu Gautama4d993f2011-08-30 18:25:55 +0530913 gserial_ports[port_num].transport = transport;
914 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915
Manu Gautama4d993f2011-08-30 18:25:55 +0530916 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700917 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530918 gserial_ports[port_num].client_port_num = no_tty_ports;
919 no_tty_ports++;
920 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700921 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530922 gserial_ports[port_num].client_port_num = no_sdio_ports;
923 no_sdio_ports++;
924 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700925 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530926 gserial_ports[port_num].client_port_num = no_smd_ports;
927 no_smd_ports++;
928 break;
Jack Pham427f6922011-11-23 19:42:00 -0800929 case USB_GADGET_XPORT_HSIC:
930 /*client port number will be updated in gport_setup*/
931 no_hsic_sports++;
932 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530933 default:
934 pr_err("%s: Un-supported transport transport: %u\n",
935 __func__, gserial_ports[port_num].transport);
936 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 }
938
Manu Gautama4d993f2011-08-30 18:25:55 +0530939 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940
941 return 0;
942}