blob: 1d7cb93854b6208ebbe12b002629657e12c79f3e [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);
Anna Perel97b8c222012-01-18 10:08:14 +0200470 gser->notify->driver_data = NULL;
David Brownell61d8bae2008-06-19 18:18:50 -0700471 }
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300472
473 if (!gser->notify->desc) {
474 if (config_ep_by_speed(cdev->gadget, f, gser->notify)) {
475 gser->notify->desc = NULL;
476 return -EINVAL;
477 }
478 }
479
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300480 rc = usb_ep_enable(gser->notify);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300481
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 if (rc) {
483 ERROR(cdev, "can't enable %s, result %d\n",
484 gser->notify->name, rc);
485 return rc;
486 }
487 gser->notify->driver_data = gser;
488#endif
489
490 if (gser->port.in->driver_data) {
491 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
492 gport_disconnect(gser);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 }
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300494 if (!gser->port.in->desc || !gser->port.out->desc) {
495 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
496 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
497 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
498 gser->port.in->desc = NULL;
499 gser->port.out->desc = NULL;
500 return -EINVAL;
501 }
502 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 gport_connect(gser);
504
505 gser->online = 1;
506 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700507}
508
509static void gser_disable(struct usb_function *f)
510{
511 struct f_gser *gser = func_to_gser(f);
512 struct usb_composite_dev *cdev = f->config->cdev;
513
514 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 gport_disconnect(gser);
517
518#ifdef CONFIG_MODEM_SUPPORT
519 usb_ep_fifo_flush(gser->notify);
520 usb_ep_disable(gser->notify);
Anna Perel97b8c222012-01-18 10:08:14 +0200521 gser->notify->driver_data = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522#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 Brownell61d8bae2008-06-19 18:18:50 -0700747 /* support all relevant hardware speeds... we expect that when
748 * hardware is dual speed, all bulk-capable endpoints work at
749 * both speeds
750 */
751 if (gadget_is_dualspeed(c->cdev->gadget)) {
752 gser_hs_in_desc.bEndpointAddress =
753 gser_fs_in_desc.bEndpointAddress;
754 gser_hs_out_desc.bEndpointAddress =
755 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756#ifdef CONFIG_MODEM_SUPPORT
757 gser_hs_notify_desc.bEndpointAddress =
758 gser_fs_notify_desc.bEndpointAddress;
759#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700760
761 /* copy descriptors, and track endpoint copies */
762 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
763
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530764 if (!f->hs_descriptors)
765 goto fail;
766
David Brownell61d8bae2008-06-19 18:18:50 -0700767 }
768
769 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
770 gser->port_num,
771 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
772 gser->port.in->name, gser->port.out->name);
773 return 0;
774
775fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530776 if (f->descriptors)
777 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778#ifdef CONFIG_MODEM_SUPPORT
779 if (gser->notify_req)
780 gs_free_req(gser->notify, gser->notify_req);
781
782 /* we might as well release our claims on endpoints */
783 if (gser->notify)
784 gser->notify->driver_data = NULL;
785#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700786 /* we might as well release our claims on endpoints */
787 if (gser->port.out)
788 gser->port.out->driver_data = NULL;
789 if (gser->port.in)
790 gser->port.in->driver_data = NULL;
791
792 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
793
794 return status;
795}
796
797static void
798gser_unbind(struct usb_configuration *c, struct usb_function *f)
799{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800#ifdef CONFIG_MODEM_SUPPORT
801 struct f_gser *gser = func_to_gser(f);
802#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700803 if (gadget_is_dualspeed(c->cdev->gadget))
804 usb_free_descriptors(f->hs_descriptors);
805 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806#ifdef CONFIG_MODEM_SUPPORT
807 gs_free_req(gser->notify, gser->notify_req);
808#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700809 kfree(func_to_gser(f));
810}
811
812/**
813 * gser_bind_config - add a generic serial function to a configuration
814 * @c: the configuration to support the serial instance
815 * @port_num: /dev/ttyGS* port this interface will use
816 * Context: single threaded during gadget setup
817 *
818 * Returns zero on success, else negative errno.
819 *
820 * Caller must have called @gserial_setup() with enough ports to
821 * handle all the ones it binds. Caller is also responsible
822 * for calling @gserial_cleanup() before module unload.
823 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530824int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700825{
826 struct f_gser *gser;
827 int status;
828
829 /* REVISIT might want instance-specific strings to help
830 * distinguish instances ...
831 */
832
833 /* maybe allocate device-global string ID */
834 if (gser_string_defs[0].id == 0) {
835 status = usb_string_id(c->cdev);
836 if (status < 0)
837 return status;
838 gser_string_defs[0].id = status;
839 }
840
841 /* allocate and initialize one new instance */
842 gser = kzalloc(sizeof *gser, GFP_KERNEL);
843 if (!gser)
844 return -ENOMEM;
845
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846#ifdef CONFIG_MODEM_SUPPORT
847 spin_lock_init(&gser->lock);
848#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700849 gser->port_num = port_num;
850
851 gser->port.func.name = "gser";
852 gser->port.func.strings = gser_strings;
853 gser->port.func.bind = gser_bind;
854 gser->port.func.unbind = gser_unbind;
855 gser->port.func.set_alt = gser_set_alt;
856 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 gser->transport = gserial_ports[port_num].transport;
858#ifdef CONFIG_MODEM_SUPPORT
859 /* We support only two ports for now */
860 if (port_num == 0)
861 gser->port.func.name = "modem";
862 else
863 gser->port.func.name = "nmea";
864 gser->port.func.setup = gser_setup;
865 gser->port.connect = gser_connect;
866 gser->port.get_dtr = gser_get_dtr;
867 gser->port.get_rts = gser_get_rts;
868 gser->port.send_carrier_detect = gser_send_carrier_detect;
869 gser->port.send_ring_indicator = gser_send_ring_indicator;
870 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
871 gser->port.disconnect = gser_disconnect;
872 gser->port.send_break = gser_send_break;
873#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700874
875 status = usb_add_function(c, &gser->port.func);
876 if (status)
877 kfree(gser);
878 return status;
879}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880
Manu Gautama4d993f2011-08-30 18:25:55 +0530881/**
882 * gserial_init_port - bind a gserial_port to its transport
883 */
884static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885{
Manu Gautama4d993f2011-08-30 18:25:55 +0530886 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
Manu Gautama4d993f2011-08-30 18:25:55 +0530888 if (port_num >= GSERIAL_NO_PORTS)
889 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
Hemant Kumar1b820d52011-11-03 15:08:28 -0700891 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530892 pr_debug("%s, port:%d, transport:%s\n", __func__,
Jack Pham427f6922011-11-23 19:42:00 -0800893 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894
Manu Gautama4d993f2011-08-30 18:25:55 +0530895 gserial_ports[port_num].transport = transport;
896 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
Manu Gautama4d993f2011-08-30 18:25:55 +0530898 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700899 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530900 gserial_ports[port_num].client_port_num = no_tty_ports;
901 no_tty_ports++;
902 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700903 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530904 gserial_ports[port_num].client_port_num = no_sdio_ports;
905 no_sdio_ports++;
906 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700907 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530908 gserial_ports[port_num].client_port_num = no_smd_ports;
909 no_smd_ports++;
910 break;
Jack Pham427f6922011-11-23 19:42:00 -0800911 case USB_GADGET_XPORT_HSIC:
912 /*client port number will be updated in gport_setup*/
913 no_hsic_sports++;
914 break;
Manu Gautama4d993f2011-08-30 18:25:55 +0530915 default:
916 pr_err("%s: Un-supported transport transport: %u\n",
917 __func__, gserial_ports[port_num].transport);
918 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 }
920
Manu Gautama4d993f2011-08-30 18:25:55 +0530921 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922
923 return 0;
924}