blob: 8d9f0904df1864068aea3b36c486aaf005c8957c [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 Brownell61d8bae2008-06-19 18:18:50 -070032
33struct f_gser {
34 struct gserial port;
35 u8 data_id;
36 u8 port_num;
37
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038 u8 online;
39 enum transport_type transport;
40
41#ifdef CONFIG_MODEM_SUPPORT
42 u8 pending;
43 spinlock_t lock;
44 struct usb_ep *notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045 struct usb_request *notify_req;
46
47 struct usb_cdc_line_coding port_line_coding;
48
49 /* SetControlLineState request */
50 u16 port_handshake_bits;
51#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
52#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
53
54 /* SerialState notification */
55 u16 serial_state;
56#define ACM_CTRL_OVERRUN (1 << 6)
57#define ACM_CTRL_PARITY (1 << 5)
58#define ACM_CTRL_FRAMING (1 << 4)
59#define ACM_CTRL_RI (1 << 3)
60#define ACM_CTRL_BRK (1 << 2)
61#define ACM_CTRL_DSR (1 << 1)
62#define ACM_CTRL_DCD (1 << 0)
63#endif
David Brownell61d8bae2008-06-19 18:18:50 -070064};
65
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066static unsigned int no_tty_ports;
67static unsigned int no_sdio_ports;
68static unsigned int no_smd_ports;
Jack Pham427f6922011-11-23 19:42:00 -080069static unsigned int no_hsic_sports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static unsigned int nr_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071
72static struct port_info {
73 enum transport_type transport;
74 unsigned port_num;
75 unsigned client_port_num;
76} gserial_ports[GSERIAL_NO_PORTS];
77
78static inline bool is_transport_sdio(enum transport_type t)
79{
Hemant Kumarbffd4142011-11-03 13:20:40 -070080 if (t == USB_GADGET_XPORT_SDIO)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 return 1;
82 return 0;
83}
84
David Brownell61d8bae2008-06-19 18:18:50 -070085static inline struct f_gser *func_to_gser(struct usb_function *f)
86{
87 return container_of(f, struct f_gser, port.func);
88}
89
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090#ifdef CONFIG_MODEM_SUPPORT
91static inline struct f_gser *port_to_gser(struct gserial *p)
92{
93 return container_of(p, struct f_gser, port);
94}
95#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
96#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
97#endif
David Brownell61d8bae2008-06-19 18:18:50 -070098/*-------------------------------------------------------------------------*/
99
100/* interface descriptor: */
101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700103 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE,
105 /* .bInterfaceNumber = DYNAMIC */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106#ifdef CONFIG_MODEM_SUPPORT
107 .bNumEndpoints = 3,
108#else
David Brownell61d8bae2008-06-19 18:18:50 -0700109 .bNumEndpoints = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700111 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
112 .bInterfaceSubClass = 0,
113 .bInterfaceProtocol = 0,
114 /* .iInterface = DYNAMIC */
115};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#ifdef CONFIG_MODEM_SUPPORT
117static struct usb_cdc_header_desc gser_header_desc = {
118 .bLength = sizeof(gser_header_desc),
119 .bDescriptorType = USB_DT_CS_INTERFACE,
120 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
121 .bcdCDC = __constant_cpu_to_le16(0x0110),
122};
David Brownell61d8bae2008-06-19 18:18:50 -0700123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124static struct usb_cdc_call_mgmt_descriptor
125gser_call_mgmt_descriptor = {
126 .bLength = sizeof(gser_call_mgmt_descriptor),
127 .bDescriptorType = USB_DT_CS_INTERFACE,
128 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
129 .bmCapabilities = 0,
130 /* .bDataInterface = DYNAMIC */
131};
132
133static struct usb_cdc_acm_descriptor gser_descriptor = {
134 .bLength = sizeof(gser_descriptor),
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136 .bDescriptorSubType = USB_CDC_ACM_TYPE,
137 .bmCapabilities = USB_CDC_CAP_LINE,
138};
139
140static struct usb_cdc_union_desc gser_union_desc = {
141 .bLength = sizeof(gser_union_desc),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_UNION_TYPE,
144 /* .bMasterInterface0 = DYNAMIC */
145 /* .bSlaveInterface0 = DYNAMIC */
146};
147#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700148/* full speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149#ifdef CONFIG_MODEM_SUPPORT
150static struct usb_endpoint_descriptor gser_fs_notify_desc = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_IN,
154 .bmAttributes = USB_ENDPOINT_XFER_INT,
155 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
156 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
157};
158#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700159
Manu Gautama4d993f2011-08-30 18:25:55 +0530160static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700161 .bLength = USB_DT_ENDPOINT_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165};
166
Manu Gautama4d993f2011-08-30 18:25:55 +0530167static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700168 .bLength = USB_DT_ENDPOINT_SIZE,
169 .bDescriptorType = USB_DT_ENDPOINT,
170 .bEndpointAddress = USB_DIR_OUT,
171 .bmAttributes = USB_ENDPOINT_XFER_BULK,
172};
173
Manu Gautama4d993f2011-08-30 18:25:55 +0530174static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700175 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176#ifdef CONFIG_MODEM_SUPPORT
177 (struct usb_descriptor_header *) &gser_header_desc,
178 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
179 (struct usb_descriptor_header *) &gser_descriptor,
180 (struct usb_descriptor_header *) &gser_union_desc,
181 (struct usb_descriptor_header *) &gser_fs_notify_desc,
182#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700183 (struct usb_descriptor_header *) &gser_fs_in_desc,
184 (struct usb_descriptor_header *) &gser_fs_out_desc,
185 NULL,
186};
187
188/* high speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189#ifdef CONFIG_MODEM_SUPPORT
190static struct usb_endpoint_descriptor gser_hs_notify_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT,
193 .bEndpointAddress = USB_DIR_IN,
194 .bmAttributes = USB_ENDPOINT_XFER_INT,
195 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
196 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
197};
198#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700199
Manu Gautama4d993f2011-08-30 18:25:55 +0530200static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700205};
206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700212};
213
Manu Gautama4d993f2011-08-30 18:25:55 +0530214static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700215 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216#ifdef CONFIG_MODEM_SUPPORT
217 (struct usb_descriptor_header *) &gser_header_desc,
218 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
219 (struct usb_descriptor_header *) &gser_descriptor,
220 (struct usb_descriptor_header *) &gser_union_desc,
221 (struct usb_descriptor_header *) &gser_hs_notify_desc,
222#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700223 (struct usb_descriptor_header *) &gser_hs_in_desc,
224 (struct usb_descriptor_header *) &gser_hs_out_desc,
225 NULL,
226};
227
228/* string descriptors: */
229
230static struct usb_string gser_string_defs[] = {
231 [0].s = "Generic Serial",
232 { } /* end of list */
233};
234
235static struct usb_gadget_strings gser_string_table = {
236 .language = 0x0409, /* en-us */
237 .strings = gser_string_defs,
238};
239
240static struct usb_gadget_strings *gser_strings[] = {
241 &gser_string_table,
242 NULL,
243};
244
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245static int gport_setup(struct usb_configuration *c)
246{
247 int ret = 0;
Jack Pham427f6922011-11-23 19:42:00 -0800248 int port_idx;
249 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250
Jack Pham427f6922011-11-23 19:42:00 -0800251 pr_debug("%s: no_tty_ports: %u no_sdio_ports: %u"
252 " no_smd_ports: %u no_hsic_sports: %u nr_ports: %u\n",
253 __func__, no_tty_ports, no_sdio_ports, no_smd_ports,
254 no_hsic_sports, nr_ports);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255
256 if (no_tty_ports)
257 ret = gserial_setup(c->cdev->gadget, no_tty_ports);
258 if (no_sdio_ports)
259 ret = gsdio_setup(c->cdev->gadget, no_sdio_ports);
260 if (no_smd_ports)
261 ret = gsmd_setup(c->cdev->gadget, no_smd_ports);
Jack Pham427f6922011-11-23 19:42:00 -0800262 if (no_hsic_sports) {
263 port_idx = ghsic_data_setup(no_hsic_sports, USB_GADGET_SERIAL);
264 if (port_idx < 0)
265 return port_idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266
Jack Pham427f6922011-11-23 19:42:00 -0800267 for (i = 0; i < nr_ports; i++) {
268 if (gserial_ports[i].transport ==
269 USB_GADGET_XPORT_HSIC) {
270 gserial_ports[i].client_port_num = port_idx;
271 port_idx++;
272 }
273 }
274
275 /*clinet port num is same for data setup and ctrl setup*/
276 ret = ghsic_ctrl_setup(no_hsic_sports, USB_GADGET_SERIAL);
277 if (ret < 0)
278 return ret;
279 return 0;
280 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 return ret;
282}
Manu Gautama4d993f2011-08-30 18:25:55 +0530283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284static int gport_connect(struct f_gser *gser)
285{
Jack Pham427f6922011-11-23 19:42:00 -0800286 unsigned port_num;
287 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288
Jack Pham427f6922011-11-23 19:42:00 -0800289 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700290 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 gser, &gser->port, gser->port_num);
292
293 port_num = gserial_ports[gser->port_num].client_port_num;
294
295 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700296 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 gserial_connect(&gser->port, port_num);
298 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700299 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 gsdio_connect(&gser->port, port_num);
301 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700302 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 gsmd_connect(&gser->port, port_num);
304 break;
Jack Pham427f6922011-11-23 19:42:00 -0800305 case USB_GADGET_XPORT_HSIC:
306 ret = ghsic_ctrl_connect(&gser->port, port_num);
307 if (ret) {
308 pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
309 __func__, ret);
310 return ret;
311 }
312 ret = ghsic_data_connect(&gser->port, port_num);
313 if (ret) {
314 pr_err("%s: ghsic_data_connect failed: err:%d\n",
315 __func__, ret);
316 ghsic_ctrl_disconnect(&gser->port, port_num);
317 return ret;
318 }
319 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 default:
321 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700322 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 return -ENODEV;
324 }
325
326 return 0;
327}
328
329static int gport_disconnect(struct f_gser *gser)
330{
331 unsigned port_num;
332
Jack Pham427f6922011-11-23 19:42:00 -0800333 pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700334 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 gser, &gser->port, gser->port_num);
336
337 port_num = gserial_ports[gser->port_num].client_port_num;
338
339 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700340 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 gserial_disconnect(&gser->port);
342 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700343 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 gsdio_disconnect(&gser->port, port_num);
345 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700346 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 gsmd_disconnect(&gser->port, port_num);
348 break;
Jack Pham427f6922011-11-23 19:42:00 -0800349 case USB_GADGET_XPORT_HSIC:
350 ghsic_ctrl_disconnect(&gser->port, port_num);
351 ghsic_data_disconnect(&gser->port, port_num);
352 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 default:
354 pr_err("%s: Un-supported transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700355 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 return -ENODEV;
357 }
358
359 return 0;
360}
361
362#ifdef CONFIG_MODEM_SUPPORT
363static void gser_complete_set_line_coding(struct usb_ep *ep,
364 struct usb_request *req)
365{
366 struct f_gser *gser = ep->driver_data;
367 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
368
369 if (req->status != 0) {
370 DBG(cdev, "gser ttyGS%d completion, err %d\n",
371 gser->port_num, req->status);
372 return;
373 }
374
375 /* normal completion */
376 if (req->actual != sizeof(gser->port_line_coding)) {
377 DBG(cdev, "gser ttyGS%d short resp, len %d\n",
378 gser->port_num, req->actual);
379 usb_ep_set_halt(ep);
380 } else {
381 struct usb_cdc_line_coding *value = req->buf;
382 gser->port_line_coding = *value;
383 }
384}
David Brownell61d8bae2008-06-19 18:18:50 -0700385/*-------------------------------------------------------------------------*/
386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387static int
388gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
389{
390 struct f_gser *gser = func_to_gser(f);
391 struct usb_composite_dev *cdev = f->config->cdev;
392 struct usb_request *req = cdev->req;
393 int value = -EOPNOTSUPP;
394 u16 w_index = le16_to_cpu(ctrl->wIndex);
395 u16 w_value = le16_to_cpu(ctrl->wValue);
396 u16 w_length = le16_to_cpu(ctrl->wLength);
397
398 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
399
400 /* SET_LINE_CODING ... just read and save what the host sends */
401 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
402 | USB_CDC_REQ_SET_LINE_CODING:
403 if (w_length != sizeof(struct usb_cdc_line_coding))
404 goto invalid;
405
406 value = w_length;
407 cdev->gadget->ep0->driver_data = gser;
408 req->complete = gser_complete_set_line_coding;
409 break;
410
411 /* GET_LINE_CODING ... return what host sent, or initial value */
412 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
413 | USB_CDC_REQ_GET_LINE_CODING:
414 value = min_t(unsigned, w_length,
415 sizeof(struct usb_cdc_line_coding));
416 memcpy(req->buf, &gser->port_line_coding, value);
417 break;
418
419 /* SET_CONTROL_LINE_STATE ... save what the host sent */
420 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
421 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
422
423 value = 0;
424 gser->port_handshake_bits = w_value;
425 if (gser->port.notify_modem) {
426 unsigned port_num =
427 gserial_ports[gser->port_num].client_port_num;
428
429 gser->port.notify_modem(&gser->port,
430 port_num, w_value);
431 }
432 break;
433
434 default:
435invalid:
436 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
437 ctrl->bRequestType, ctrl->bRequest,
438 w_value, w_index, w_length);
439 }
440
441 /* respond with data transfer or status phase? */
442 if (value >= 0) {
443 DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
444 gser->port_num, ctrl->bRequestType, ctrl->bRequest,
445 w_value, w_index, w_length);
446 req->zero = 0;
447 req->length = value;
448 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
449 if (value < 0)
450 ERROR(cdev, "gser response on ttyGS%d, err %d\n",
451 gser->port_num, value);
452 }
453
454 /* device either stalls (value < 0) or reports success */
455 return value;
456}
457#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700458static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
459{
460 struct f_gser *gser = func_to_gser(f);
461 struct usb_composite_dev *cdev = f->config->cdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462 int rc = 0;
David Brownell61d8bae2008-06-19 18:18:50 -0700463
464 /* we know alt == 0, so this is an activation or a reset */
465
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466#ifdef CONFIG_MODEM_SUPPORT
467 if (gser->notify->driver_data) {
468 DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num);
469 usb_ep_disable(gser->notify);
David Brownell61d8bae2008-06-19 18:18:50 -0700470 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200471
472 if (!gser->notify->desc) {
473 if (config_ep_by_speed(cdev->gadget, f, gser->notify)) {
474 gser->notify->desc = NULL;
475 return -EINVAL;
476 }
477 }
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300478 rc = usb_ep_enable(gser->notify);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200479
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 if (rc) {
481 ERROR(cdev, "can't enable %s, result %d\n",
482 gser->notify->name, rc);
483 return rc;
484 }
485 gser->notify->driver_data = gser;
486#endif
487
488 if (gser->port.in->driver_data) {
489 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
490 gport_disconnect(gser);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200492 if (!gser->port.in->desc || !gser->port.out->desc) {
493 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
494 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
495 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
496 gser->port.in->desc = NULL;
497 gser->port.out->desc = NULL;
498 return -EINVAL;
499 }
500 }
David Brownac5d1542012-02-06 10:37:22 -0800501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 gport_connect(gser);
503
504 gser->online = 1;
505 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700506}
507
508static void gser_disable(struct usb_function *f)
509{
510 struct f_gser *gser = func_to_gser(f);
511 struct usb_composite_dev *cdev = f->config->cdev;
512
513 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514
515 gport_disconnect(gser);
516
517#ifdef CONFIG_MODEM_SUPPORT
518 usb_ep_fifo_flush(gser->notify);
519 usb_ep_disable(gser->notify);
520#endif
521 gser->online = 0;
522}
523#ifdef CONFIG_MODEM_SUPPORT
524static int gser_notify(struct f_gser *gser, u8 type, u16 value,
525 void *data, unsigned length)
526{
527 struct usb_ep *ep = gser->notify;
528 struct usb_request *req;
529 struct usb_cdc_notification *notify;
530 const unsigned len = sizeof(*notify) + length;
531 void *buf;
532 int status;
533 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
534
535 req = gser->notify_req;
536 gser->notify_req = NULL;
537 gser->pending = false;
538
539 req->length = len;
540 notify = req->buf;
541 buf = notify + 1;
542
543 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
544 | USB_RECIP_INTERFACE;
545 notify->bNotificationType = type;
546 notify->wValue = cpu_to_le16(value);
547 notify->wIndex = cpu_to_le16(gser->data_id);
548 notify->wLength = cpu_to_le16(length);
549 memcpy(buf, data, length);
550
551 status = usb_ep_queue(ep, req, GFP_ATOMIC);
552 if (status < 0) {
553 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
554 gser->port_num, status);
555 gser->notify_req = req;
556 }
557
558 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700559}
560
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561static int gser_notify_serial_state(struct f_gser *gser)
562{
563 int status;
564 unsigned long flags;
565 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
566
567 spin_lock_irqsave(&gser->lock, flags);
568 if (gser->notify_req) {
569 DBG(cdev, "gser ttyGS%d serial state %04x\n",
570 gser->port_num, gser->serial_state);
571 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
572 0, &gser->serial_state,
573 sizeof(gser->serial_state));
574 } else {
575 gser->pending = true;
576 status = 0;
577 }
578 spin_unlock_irqrestore(&gser->lock, flags);
579 return status;
580}
581
582static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
583{
584 struct f_gser *gser = req->context;
585 u8 doit = false;
586 unsigned long flags;
587
588 /* on this call path we do NOT hold the port spinlock,
589 * which is why ACM needs its own spinlock
590 */
591 spin_lock_irqsave(&gser->lock, flags);
592 if (req->status != -ESHUTDOWN)
593 doit = gser->pending;
594 gser->notify_req = req;
595 spin_unlock_irqrestore(&gser->lock, flags);
596
597 if (doit && gser->online)
598 gser_notify_serial_state(gser);
599}
600static void gser_connect(struct gserial *port)
601{
602 struct f_gser *gser = port_to_gser(port);
603
604 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
605 gser_notify_serial_state(gser);
606}
607
608unsigned int gser_get_dtr(struct gserial *port)
609{
610 struct f_gser *gser = port_to_gser(port);
611
612 if (gser->port_handshake_bits & ACM_CTRL_DTR)
613 return 1;
614 else
615 return 0;
616}
617
618unsigned int gser_get_rts(struct gserial *port)
619{
620 struct f_gser *gser = port_to_gser(port);
621
622 if (gser->port_handshake_bits & ACM_CTRL_RTS)
623 return 1;
624 else
625 return 0;
626}
627
628unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
629{
630 struct f_gser *gser = port_to_gser(port);
631 u16 state;
632
633 state = gser->serial_state;
634 state &= ~ACM_CTRL_DCD;
635 if (yes)
636 state |= ACM_CTRL_DCD;
637
638 gser->serial_state = state;
639 return gser_notify_serial_state(gser);
640
641}
642
643unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
644{
645 struct f_gser *gser = port_to_gser(port);
646 u16 state;
647
648 state = gser->serial_state;
649 state &= ~ACM_CTRL_RI;
650 if (yes)
651 state |= ACM_CTRL_RI;
652
653 gser->serial_state = state;
654 return gser_notify_serial_state(gser);
655
656}
657static void gser_disconnect(struct gserial *port)
658{
659 struct f_gser *gser = port_to_gser(port);
660
661 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
662 gser_notify_serial_state(gser);
663}
664
665static int gser_send_break(struct gserial *port, int duration)
666{
667 struct f_gser *gser = port_to_gser(port);
668 u16 state;
669
670 state = gser->serial_state;
671 state &= ~ACM_CTRL_BRK;
672 if (duration)
673 state |= ACM_CTRL_BRK;
674
675 gser->serial_state = state;
676 return gser_notify_serial_state(gser);
677}
678
679static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
680{
681 struct f_gser *gser = port_to_gser(port);
682
683 gser->serial_state = ctrl_bits;
684
685 return gser_notify_serial_state(gser);
686}
687#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700688/*-------------------------------------------------------------------------*/
689
690/* serial function driver setup/binding */
691
Manu Gautama4d993f2011-08-30 18:25:55 +0530692static int
David Brownell61d8bae2008-06-19 18:18:50 -0700693gser_bind(struct usb_configuration *c, struct usb_function *f)
694{
695 struct usb_composite_dev *cdev = c->cdev;
696 struct f_gser *gser = func_to_gser(f);
697 int status;
698 struct usb_ep *ep;
699
700 /* allocate instance-specific interface IDs */
701 status = usb_interface_id(c, f);
702 if (status < 0)
703 goto fail;
704 gser->data_id = status;
705 gser_interface_desc.bInterfaceNumber = status;
706
707 status = -ENODEV;
708
709 /* allocate instance-specific endpoints */
710 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
711 if (!ep)
712 goto fail;
713 gser->port.in = ep;
714 ep->driver_data = cdev; /* claim */
715
716 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
717 if (!ep)
718 goto fail;
719 gser->port.out = ep;
720 ep->driver_data = cdev; /* claim */
721
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722#ifdef CONFIG_MODEM_SUPPORT
723 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
724 if (!ep)
725 goto fail;
726 gser->notify = ep;
727 ep->driver_data = cdev; /* claim */
728 /* allocate notification */
729 gser->notify_req = gs_alloc_req(ep,
730 sizeof(struct usb_cdc_notification) + 2,
731 GFP_KERNEL);
732 if (!gser->notify_req)
733 goto fail;
734
735 gser->notify_req->complete = gser_notify_complete;
736 gser->notify_req->context = gser;
737#endif
738
David Brownell61d8bae2008-06-19 18:18:50 -0700739 /* copy descriptors, and track endpoint copies */
740 f->descriptors = usb_copy_descriptors(gser_fs_function);
741
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530742 if (!f->descriptors)
743 goto fail;
744
David Brownell61d8bae2008-06-19 18:18:50 -0700745 /* support all relevant hardware speeds... we expect that when
746 * hardware is dual speed, all bulk-capable endpoints work at
747 * both speeds
748 */
749 if (gadget_is_dualspeed(c->cdev->gadget)) {
750 gser_hs_in_desc.bEndpointAddress =
751 gser_fs_in_desc.bEndpointAddress;
752 gser_hs_out_desc.bEndpointAddress =
753 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754#ifdef CONFIG_MODEM_SUPPORT
755 gser_hs_notify_desc.bEndpointAddress =
756 gser_fs_notify_desc.bEndpointAddress;
757#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700758
759 /* copy descriptors, and track endpoint copies */
760 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
761
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530762 if (!f->hs_descriptors)
763 goto fail;
764
David Brownell61d8bae2008-06-19 18:18:50 -0700765 }
766
767 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
768 gser->port_num,
769 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
770 gser->port.in->name, gser->port.out->name);
771 return 0;
772
773fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530774 if (f->descriptors)
775 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776#ifdef CONFIG_MODEM_SUPPORT
777 if (gser->notify_req)
778 gs_free_req(gser->notify, gser->notify_req);
779
780 /* we might as well release our claims on endpoints */
781 if (gser->notify)
782 gser->notify->driver_data = NULL;
783#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700784 /* we might as well release our claims on endpoints */
785 if (gser->port.out)
786 gser->port.out->driver_data = NULL;
787 if (gser->port.in)
788 gser->port.in->driver_data = NULL;
789
790 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
791
792 return status;
793}
794
795static void
796gser_unbind(struct usb_configuration *c, struct usb_function *f)
797{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798#ifdef CONFIG_MODEM_SUPPORT
799 struct f_gser *gser = func_to_gser(f);
800#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700801 if (gadget_is_dualspeed(c->cdev->gadget))
802 usb_free_descriptors(f->hs_descriptors);
803 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804#ifdef CONFIG_MODEM_SUPPORT
805 gs_free_req(gser->notify, gser->notify_req);
806#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700807 kfree(func_to_gser(f));
808}
809
810/**
811 * gser_bind_config - add a generic serial function to a configuration
812 * @c: the configuration to support the serial instance
813 * @port_num: /dev/ttyGS* port this interface will use
814 * Context: single threaded during gadget setup
815 *
816 * Returns zero on success, else negative errno.
817 *
818 * Caller must have called @gserial_setup() with enough ports to
819 * handle all the ones it binds. Caller is also responsible
820 * for calling @gserial_cleanup() before module unload.
821 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530822int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700823{
824 struct f_gser *gser;
825 int status;
826
827 /* REVISIT might want instance-specific strings to help
828 * distinguish instances ...
829 */
830
831 /* maybe allocate device-global string ID */
832 if (gser_string_defs[0].id == 0) {
833 status = usb_string_id(c->cdev);
834 if (status < 0)
835 return status;
836 gser_string_defs[0].id = status;
837 }
838
839 /* allocate and initialize one new instance */
840 gser = kzalloc(sizeof *gser, GFP_KERNEL);
841 if (!gser)
842 return -ENOMEM;
843
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844#ifdef CONFIG_MODEM_SUPPORT
845 spin_lock_init(&gser->lock);
846#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700847 gser->port_num = port_num;
848
849 gser->port.func.name = "gser";
850 gser->port.func.strings = gser_strings;
851 gser->port.func.bind = gser_bind;
852 gser->port.func.unbind = gser_unbind;
853 gser->port.func.set_alt = gser_set_alt;
854 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 gser->transport = gserial_ports[port_num].transport;
856#ifdef CONFIG_MODEM_SUPPORT
857 /* We support only two ports for now */
858 if (port_num == 0)
859 gser->port.func.name = "modem";
860 else
861 gser->port.func.name = "nmea";
862 gser->port.func.setup = gser_setup;
863 gser->port.connect = gser_connect;
864 gser->port.get_dtr = gser_get_dtr;
865 gser->port.get_rts = gser_get_rts;
866 gser->port.send_carrier_detect = gser_send_carrier_detect;
867 gser->port.send_ring_indicator = gser_send_ring_indicator;
868 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
869 gser->port.disconnect = gser_disconnect;
870 gser->port.send_break = gser_send_break;
871#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700872
873 status = usb_add_function(c, &gser->port.func);
874 if (status)
875 kfree(gser);
876 return status;
877}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878
Manu Gautama4d993f2011-08-30 18:25:55 +0530879/**
880 * gserial_init_port - bind a gserial_port to its transport
881 */
882static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883{
Manu Gautama4d993f2011-08-30 18:25:55 +0530884 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885
Manu Gautama4d993f2011-08-30 18:25:55 +0530886 if (port_num >= GSERIAL_NO_PORTS)
887 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888
Hemant Kumar1b820d52011-11-03 15:08:28 -0700889 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530890 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800891 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
Manu Gautama4d993f2011-08-30 18:25:55 +0530893 gserial_ports[port_num].transport = transport;
894 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895
Manu Gautama4d993f2011-08-30 18:25:55 +0530896 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700897 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530898 gserial_ports[port_num].client_port_num = no_tty_ports;
899 no_tty_ports++;
900 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700901 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530902 gserial_ports[port_num].client_port_num = no_sdio_ports;
903 no_sdio_ports++;
904 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700905 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530906 gserial_ports[port_num].client_port_num = no_smd_ports;
907 no_smd_ports++;
908 break;
Jack Pham427f6922011-11-23 19:42:00 -0800909 case USB_GADGET_XPORT_HSIC:
910 /*client port number will be updated in gport_setup*/
911 no_hsic_sports++;
912 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530913 default:
914 pr_err("%s: Un-supported transport transport: %u\n",
915 __func__, gserial_ports[port_num].transport);
916 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 }
918
Manu Gautama4d993f2011-08-30 18:25:55 +0530919 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920
921 return 0;
922}