blob: d9a901bfd7e1ec0a112f0f7505254491716ed303 [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;
54 struct usb_endpoint_descriptor *notify_desc;
55 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);
Anna Perel97b8c222012-01-18 10:08:14 +0200480 gser->notify->driver_data = NULL;
David Brownell61d8bae2008-06-19 18:18:50 -0700481 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 gser->notify_desc = ep_choose(cdev->gadget,
483 gser->hs.notify,
484 gser->fs.notify);
485 rc = usb_ep_enable(gser->notify, gser->notify_desc);
486 if (rc) {
487 ERROR(cdev, "can't enable %s, result %d\n",
488 gser->notify->name, rc);
489 return rc;
490 }
491 gser->notify->driver_data = gser;
492#endif
493
494 if (gser->port.in->driver_data) {
495 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
496 gport_disconnect(gser);
497 } else {
498 DBG(cdev, "activate generic data ttyGS%d\n", gser->port_num);
499 }
500 gser->port.in_desc = ep_choose(cdev->gadget,
501 gser->hs.in, gser->fs.in);
502 gser->port.out_desc = ep_choose(cdev->gadget,
503 gser->hs.out, gser->fs.out);
504
505 gport_connect(gser);
506
507 gser->online = 1;
508 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700509}
510
511static void gser_disable(struct usb_function *f)
512{
513 struct f_gser *gser = func_to_gser(f);
514 struct usb_composite_dev *cdev = f->config->cdev;
515
516 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 gport_disconnect(gser);
519
520#ifdef CONFIG_MODEM_SUPPORT
521 usb_ep_fifo_flush(gser->notify);
522 usb_ep_disable(gser->notify);
Anna Perel97b8c222012-01-18 10:08:14 +0200523 gser->notify->driver_data = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524#endif
525 gser->online = 0;
526}
527#ifdef CONFIG_MODEM_SUPPORT
528static int gser_notify(struct f_gser *gser, u8 type, u16 value,
529 void *data, unsigned length)
530{
531 struct usb_ep *ep = gser->notify;
532 struct usb_request *req;
533 struct usb_cdc_notification *notify;
534 const unsigned len = sizeof(*notify) + length;
535 void *buf;
536 int status;
537 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
538
539 req = gser->notify_req;
540 gser->notify_req = NULL;
541 gser->pending = false;
542
543 req->length = len;
544 notify = req->buf;
545 buf = notify + 1;
546
547 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
548 | USB_RECIP_INTERFACE;
549 notify->bNotificationType = type;
550 notify->wValue = cpu_to_le16(value);
551 notify->wIndex = cpu_to_le16(gser->data_id);
552 notify->wLength = cpu_to_le16(length);
553 memcpy(buf, data, length);
554
555 status = usb_ep_queue(ep, req, GFP_ATOMIC);
556 if (status < 0) {
557 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
558 gser->port_num, status);
559 gser->notify_req = req;
560 }
561
562 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700563}
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565static int gser_notify_serial_state(struct f_gser *gser)
566{
567 int status;
568 unsigned long flags;
569 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
570
571 spin_lock_irqsave(&gser->lock, flags);
572 if (gser->notify_req) {
573 DBG(cdev, "gser ttyGS%d serial state %04x\n",
574 gser->port_num, gser->serial_state);
575 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
576 0, &gser->serial_state,
577 sizeof(gser->serial_state));
578 } else {
579 gser->pending = true;
580 status = 0;
581 }
582 spin_unlock_irqrestore(&gser->lock, flags);
583 return status;
584}
585
586static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
587{
588 struct f_gser *gser = req->context;
589 u8 doit = false;
590 unsigned long flags;
591
592 /* on this call path we do NOT hold the port spinlock,
593 * which is why ACM needs its own spinlock
594 */
595 spin_lock_irqsave(&gser->lock, flags);
596 if (req->status != -ESHUTDOWN)
597 doit = gser->pending;
598 gser->notify_req = req;
599 spin_unlock_irqrestore(&gser->lock, flags);
600
601 if (doit && gser->online)
602 gser_notify_serial_state(gser);
603}
604static void gser_connect(struct gserial *port)
605{
606 struct f_gser *gser = port_to_gser(port);
607
608 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
609 gser_notify_serial_state(gser);
610}
611
612unsigned int gser_get_dtr(struct gserial *port)
613{
614 struct f_gser *gser = port_to_gser(port);
615
616 if (gser->port_handshake_bits & ACM_CTRL_DTR)
617 return 1;
618 else
619 return 0;
620}
621
622unsigned int gser_get_rts(struct gserial *port)
623{
624 struct f_gser *gser = port_to_gser(port);
625
626 if (gser->port_handshake_bits & ACM_CTRL_RTS)
627 return 1;
628 else
629 return 0;
630}
631
632unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
633{
634 struct f_gser *gser = port_to_gser(port);
635 u16 state;
636
637 state = gser->serial_state;
638 state &= ~ACM_CTRL_DCD;
639 if (yes)
640 state |= ACM_CTRL_DCD;
641
642 gser->serial_state = state;
643 return gser_notify_serial_state(gser);
644
645}
646
647unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
648{
649 struct f_gser *gser = port_to_gser(port);
650 u16 state;
651
652 state = gser->serial_state;
653 state &= ~ACM_CTRL_RI;
654 if (yes)
655 state |= ACM_CTRL_RI;
656
657 gser->serial_state = state;
658 return gser_notify_serial_state(gser);
659
660}
661static void gser_disconnect(struct gserial *port)
662{
663 struct f_gser *gser = port_to_gser(port);
664
665 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
666 gser_notify_serial_state(gser);
667}
668
669static int gser_send_break(struct gserial *port, int duration)
670{
671 struct f_gser *gser = port_to_gser(port);
672 u16 state;
673
674 state = gser->serial_state;
675 state &= ~ACM_CTRL_BRK;
676 if (duration)
677 state |= ACM_CTRL_BRK;
678
679 gser->serial_state = state;
680 return gser_notify_serial_state(gser);
681}
682
683static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
684{
685 struct f_gser *gser = port_to_gser(port);
686
687 gser->serial_state = ctrl_bits;
688
689 return gser_notify_serial_state(gser);
690}
691#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700692/*-------------------------------------------------------------------------*/
693
694/* serial function driver setup/binding */
695
Manu Gautama4d993f2011-08-30 18:25:55 +0530696static int
David Brownell61d8bae2008-06-19 18:18:50 -0700697gser_bind(struct usb_configuration *c, struct usb_function *f)
698{
699 struct usb_composite_dev *cdev = c->cdev;
700 struct f_gser *gser = func_to_gser(f);
701 int status;
702 struct usb_ep *ep;
703
704 /* allocate instance-specific interface IDs */
705 status = usb_interface_id(c, f);
706 if (status < 0)
707 goto fail;
708 gser->data_id = status;
709 gser_interface_desc.bInterfaceNumber = status;
710
711 status = -ENODEV;
712
713 /* allocate instance-specific endpoints */
714 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
715 if (!ep)
716 goto fail;
717 gser->port.in = ep;
718 ep->driver_data = cdev; /* claim */
719
720 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
721 if (!ep)
722 goto fail;
723 gser->port.out = ep;
724 ep->driver_data = cdev; /* claim */
725
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726#ifdef CONFIG_MODEM_SUPPORT
727 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
728 if (!ep)
729 goto fail;
730 gser->notify = ep;
731 ep->driver_data = cdev; /* claim */
732 /* allocate notification */
733 gser->notify_req = gs_alloc_req(ep,
734 sizeof(struct usb_cdc_notification) + 2,
735 GFP_KERNEL);
736 if (!gser->notify_req)
737 goto fail;
738
739 gser->notify_req->complete = gser_notify_complete;
740 gser->notify_req->context = gser;
741#endif
742
David Brownell61d8bae2008-06-19 18:18:50 -0700743 /* copy descriptors, and track endpoint copies */
744 f->descriptors = usb_copy_descriptors(gser_fs_function);
745
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530746 if (!f->descriptors)
747 goto fail;
748
David Brownell61d8bae2008-06-19 18:18:50 -0700749 gser->fs.in = usb_find_endpoint(gser_fs_function,
750 f->descriptors, &gser_fs_in_desc);
751 gser->fs.out = usb_find_endpoint(gser_fs_function,
752 f->descriptors, &gser_fs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753#ifdef CONFIG_MODEM_SUPPORT
754 gser->fs.notify = usb_find_endpoint(gser_fs_function,
755 f->descriptors, &gser_fs_notify_desc);
756#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700757
758
759 /* support all relevant hardware speeds... we expect that when
760 * hardware is dual speed, all bulk-capable endpoints work at
761 * both speeds
762 */
763 if (gadget_is_dualspeed(c->cdev->gadget)) {
764 gser_hs_in_desc.bEndpointAddress =
765 gser_fs_in_desc.bEndpointAddress;
766 gser_hs_out_desc.bEndpointAddress =
767 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768#ifdef CONFIG_MODEM_SUPPORT
769 gser_hs_notify_desc.bEndpointAddress =
770 gser_fs_notify_desc.bEndpointAddress;
771#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700772
773 /* copy descriptors, and track endpoint copies */
774 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
775
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530776 if (!f->hs_descriptors)
777 goto fail;
778
David Brownell61d8bae2008-06-19 18:18:50 -0700779 gser->hs.in = usb_find_endpoint(gser_hs_function,
780 f->hs_descriptors, &gser_hs_in_desc);
781 gser->hs.out = usb_find_endpoint(gser_hs_function,
782 f->hs_descriptors, &gser_hs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783#ifdef CONFIG_MODEM_SUPPORT
784 gser->hs.notify = usb_find_endpoint(gser_hs_function,
785 f->hs_descriptors, &gser_hs_notify_desc);
786#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700787 }
788
789 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
790 gser->port_num,
791 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
792 gser->port.in->name, gser->port.out->name);
793 return 0;
794
795fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530796 if (f->descriptors)
797 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798#ifdef CONFIG_MODEM_SUPPORT
799 if (gser->notify_req)
800 gs_free_req(gser->notify, gser->notify_req);
801
802 /* we might as well release our claims on endpoints */
803 if (gser->notify)
804 gser->notify->driver_data = NULL;
805#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700806 /* we might as well release our claims on endpoints */
807 if (gser->port.out)
808 gser->port.out->driver_data = NULL;
809 if (gser->port.in)
810 gser->port.in->driver_data = NULL;
811
812 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
813
814 return status;
815}
816
817static void
818gser_unbind(struct usb_configuration *c, struct usb_function *f)
819{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820#ifdef CONFIG_MODEM_SUPPORT
821 struct f_gser *gser = func_to_gser(f);
822#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700823 if (gadget_is_dualspeed(c->cdev->gadget))
824 usb_free_descriptors(f->hs_descriptors);
825 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826#ifdef CONFIG_MODEM_SUPPORT
827 gs_free_req(gser->notify, gser->notify_req);
828#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700829 kfree(func_to_gser(f));
830}
831
832/**
833 * gser_bind_config - add a generic serial function to a configuration
834 * @c: the configuration to support the serial instance
835 * @port_num: /dev/ttyGS* port this interface will use
836 * Context: single threaded during gadget setup
837 *
838 * Returns zero on success, else negative errno.
839 *
840 * Caller must have called @gserial_setup() with enough ports to
841 * handle all the ones it binds. Caller is also responsible
842 * for calling @gserial_cleanup() before module unload.
843 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530844int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700845{
846 struct f_gser *gser;
847 int status;
848
849 /* REVISIT might want instance-specific strings to help
850 * distinguish instances ...
851 */
852
853 /* maybe allocate device-global string ID */
854 if (gser_string_defs[0].id == 0) {
855 status = usb_string_id(c->cdev);
856 if (status < 0)
857 return status;
858 gser_string_defs[0].id = status;
859 }
860
861 /* allocate and initialize one new instance */
862 gser = kzalloc(sizeof *gser, GFP_KERNEL);
863 if (!gser)
864 return -ENOMEM;
865
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866#ifdef CONFIG_MODEM_SUPPORT
867 spin_lock_init(&gser->lock);
868#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700869 gser->port_num = port_num;
870
871 gser->port.func.name = "gser";
872 gser->port.func.strings = gser_strings;
873 gser->port.func.bind = gser_bind;
874 gser->port.func.unbind = gser_unbind;
875 gser->port.func.set_alt = gser_set_alt;
876 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 gser->transport = gserial_ports[port_num].transport;
878#ifdef CONFIG_MODEM_SUPPORT
879 /* We support only two ports for now */
880 if (port_num == 0)
881 gser->port.func.name = "modem";
882 else
883 gser->port.func.name = "nmea";
884 gser->port.func.setup = gser_setup;
885 gser->port.connect = gser_connect;
886 gser->port.get_dtr = gser_get_dtr;
887 gser->port.get_rts = gser_get_rts;
888 gser->port.send_carrier_detect = gser_send_carrier_detect;
889 gser->port.send_ring_indicator = gser_send_ring_indicator;
890 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
891 gser->port.disconnect = gser_disconnect;
892 gser->port.send_break = gser_send_break;
893#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700894
895 status = usb_add_function(c, &gser->port.func);
896 if (status)
897 kfree(gser);
898 return status;
899}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900
Manu Gautama4d993f2011-08-30 18:25:55 +0530901/**
902 * gserial_init_port - bind a gserial_port to its transport
903 */
904static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905{
Manu Gautama4d993f2011-08-30 18:25:55 +0530906 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907
Manu Gautama4d993f2011-08-30 18:25:55 +0530908 if (port_num >= GSERIAL_NO_PORTS)
909 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910
Hemant Kumar1b820d52011-11-03 15:08:28 -0700911 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530912 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800913 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914
Manu Gautama4d993f2011-08-30 18:25:55 +0530915 gserial_ports[port_num].transport = transport;
916 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917
Manu Gautama4d993f2011-08-30 18:25:55 +0530918 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700919 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530920 gserial_ports[port_num].client_port_num = no_tty_ports;
921 no_tty_ports++;
922 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700923 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530924 gserial_ports[port_num].client_port_num = no_sdio_ports;
925 no_sdio_ports++;
926 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700927 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530928 gserial_ports[port_num].client_port_num = no_smd_ports;
929 no_smd_ports++;
930 break;
Jack Pham427f6922011-11-23 19:42:00 -0800931 case USB_GADGET_XPORT_HSIC:
932 /*client port number will be updated in gport_setup*/
933 no_hsic_sports++;
934 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530935 default:
936 pr_err("%s: Un-supported transport transport: %u\n",
937 __func__, gserial_ports[port_num].transport);
938 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 }
940
Manu Gautama4d993f2011-08-30 18:25:55 +0530941 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942
943 return 0;
944}