blob: 7cd0288fcea0b11f9c5d3c64958c2d49b849f08f [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;
79static unsigned int nr_ports;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
81static struct port_info {
82 enum transport_type transport;
83 unsigned port_num;
84 unsigned client_port_num;
85} gserial_ports[GSERIAL_NO_PORTS];
86
87static inline bool is_transport_sdio(enum transport_type t)
88{
Hemant Kumarbffd4142011-11-03 13:20:40 -070089 if (t == USB_GADGET_XPORT_SDIO)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 return 1;
91 return 0;
92}
93
David Brownell61d8bae2008-06-19 18:18:50 -070094static inline struct f_gser *func_to_gser(struct usb_function *f)
95{
96 return container_of(f, struct f_gser, port.func);
97}
98
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099#ifdef CONFIG_MODEM_SUPPORT
100static inline struct f_gser *port_to_gser(struct gserial *p)
101{
102 return container_of(p, struct f_gser, port);
103}
104#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
105#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
106#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700107/*-------------------------------------------------------------------------*/
108
109/* interface descriptor: */
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700112 .bLength = USB_DT_INTERFACE_SIZE,
113 .bDescriptorType = USB_DT_INTERFACE,
114 /* .bInterfaceNumber = DYNAMIC */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115#ifdef CONFIG_MODEM_SUPPORT
116 .bNumEndpoints = 3,
117#else
David Brownell61d8bae2008-06-19 18:18:50 -0700118 .bNumEndpoints = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700120 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
121 .bInterfaceSubClass = 0,
122 .bInterfaceProtocol = 0,
123 /* .iInterface = DYNAMIC */
124};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125#ifdef CONFIG_MODEM_SUPPORT
126static struct usb_cdc_header_desc gser_header_desc = {
127 .bLength = sizeof(gser_header_desc),
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
130 .bcdCDC = __constant_cpu_to_le16(0x0110),
131};
David Brownell61d8bae2008-06-19 18:18:50 -0700132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133static struct usb_cdc_call_mgmt_descriptor
134gser_call_mgmt_descriptor = {
135 .bLength = sizeof(gser_call_mgmt_descriptor),
136 .bDescriptorType = USB_DT_CS_INTERFACE,
137 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
138 .bmCapabilities = 0,
139 /* .bDataInterface = DYNAMIC */
140};
141
142static struct usb_cdc_acm_descriptor gser_descriptor = {
143 .bLength = sizeof(gser_descriptor),
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubType = USB_CDC_ACM_TYPE,
146 .bmCapabilities = USB_CDC_CAP_LINE,
147};
148
149static struct usb_cdc_union_desc gser_union_desc = {
150 .bLength = sizeof(gser_union_desc),
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_UNION_TYPE,
153 /* .bMasterInterface0 = DYNAMIC */
154 /* .bSlaveInterface0 = DYNAMIC */
155};
156#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700157/* full speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158#ifdef CONFIG_MODEM_SUPPORT
159static struct usb_endpoint_descriptor gser_fs_notify_desc = {
160 .bLength = USB_DT_ENDPOINT_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
162 .bEndpointAddress = USB_DIR_IN,
163 .bmAttributes = USB_ENDPOINT_XFER_INT,
164 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
165 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
166};
167#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700168
Manu Gautama4d993f2011-08-30 18:25:55 +0530169static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = USB_DIR_IN,
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
174};
175
Manu Gautama4d993f2011-08-30 18:25:55 +0530176static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700177 .bLength = USB_DT_ENDPOINT_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
179 .bEndpointAddress = USB_DIR_OUT,
180 .bmAttributes = USB_ENDPOINT_XFER_BULK,
181};
182
Manu Gautama4d993f2011-08-30 18:25:55 +0530183static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700184 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185#ifdef CONFIG_MODEM_SUPPORT
186 (struct usb_descriptor_header *) &gser_header_desc,
187 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
188 (struct usb_descriptor_header *) &gser_descriptor,
189 (struct usb_descriptor_header *) &gser_union_desc,
190 (struct usb_descriptor_header *) &gser_fs_notify_desc,
191#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700192 (struct usb_descriptor_header *) &gser_fs_in_desc,
193 (struct usb_descriptor_header *) &gser_fs_out_desc,
194 NULL,
195};
196
197/* high speed support: */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198#ifdef CONFIG_MODEM_SUPPORT
199static struct usb_endpoint_descriptor gser_hs_notify_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202 .bEndpointAddress = USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_INT,
204 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
205 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
206};
207#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700208
Manu Gautama4d993f2011-08-30 18:25:55 +0530209static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700214};
215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -0700217 .bLength = USB_DT_ENDPOINT_SIZE,
218 .bDescriptorType = USB_DT_ENDPOINT,
219 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 .wMaxPacketSize = __constant_cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -0700221};
222
Manu Gautama4d993f2011-08-30 18:25:55 +0530223static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -0700224 (struct usb_descriptor_header *) &gser_interface_desc,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225#ifdef CONFIG_MODEM_SUPPORT
226 (struct usb_descriptor_header *) &gser_header_desc,
227 (struct usb_descriptor_header *) &gser_call_mgmt_descriptor,
228 (struct usb_descriptor_header *) &gser_descriptor,
229 (struct usb_descriptor_header *) &gser_union_desc,
230 (struct usb_descriptor_header *) &gser_hs_notify_desc,
231#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700232 (struct usb_descriptor_header *) &gser_hs_in_desc,
233 (struct usb_descriptor_header *) &gser_hs_out_desc,
234 NULL,
235};
236
237/* string descriptors: */
238
239static struct usb_string gser_string_defs[] = {
240 [0].s = "Generic Serial",
241 { } /* end of list */
242};
243
244static struct usb_gadget_strings gser_string_table = {
245 .language = 0x0409, /* en-us */
246 .strings = gser_string_defs,
247};
248
249static struct usb_gadget_strings *gser_strings[] = {
250 &gser_string_table,
251 NULL,
252};
253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254static int gport_setup(struct usb_configuration *c)
255{
256 int ret = 0;
257
258 pr_debug("%s: no_tty_ports:%u no_sdio_ports: %u nr_ports:%u\n",
259 __func__, no_tty_ports, no_sdio_ports, nr_ports);
260
261 if (no_tty_ports)
262 ret = gserial_setup(c->cdev->gadget, no_tty_ports);
263 if (no_sdio_ports)
264 ret = gsdio_setup(c->cdev->gadget, no_sdio_ports);
265 if (no_smd_ports)
266 ret = gsmd_setup(c->cdev->gadget, no_smd_ports);
267
268 return ret;
269}
Manu Gautama4d993f2011-08-30 18:25:55 +0530270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271static int gport_connect(struct f_gser *gser)
272{
273 unsigned port_num;
274
275 pr_debug("%s: transport:%s f_gser:%p gserial:%p port_num:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700276 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 gser, &gser->port, gser->port_num);
278
279 port_num = gserial_ports[gser->port_num].client_port_num;
280
281 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700282 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 gserial_connect(&gser->port, port_num);
284 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700285 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 gsdio_connect(&gser->port, port_num);
287 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700288 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 gsmd_connect(&gser->port, port_num);
290 break;
291 default:
292 pr_err("%s: Un-supported transport: %s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700293 xport_to_str(gser->transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 return -ENODEV;
295 }
296
297 return 0;
298}
299
300static int gport_disconnect(struct f_gser *gser)
301{
302 unsigned port_num;
303
304 pr_debug("%s: transport:%s f_gser:%p gserial:%p port_num:%d\n",
Hemant Kumar1b820d52011-11-03 15:08:28 -0700305 __func__, xport_to_str(gser->transport),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 gser, &gser->port, gser->port_num);
307
308 port_num = gserial_ports[gser->port_num].client_port_num;
309
310 switch (gser->transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700311 case USB_GADGET_XPORT_TTY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 gserial_disconnect(&gser->port);
313 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700314 case USB_GADGET_XPORT_SDIO:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 gsdio_disconnect(&gser->port, port_num);
316 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700317 case USB_GADGET_XPORT_SMD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 gsmd_disconnect(&gser->port, port_num);
319 break;
320 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
329#ifdef CONFIG_MODEM_SUPPORT
330static void gser_complete_set_line_coding(struct usb_ep *ep,
331 struct usb_request *req)
332{
333 struct f_gser *gser = ep->driver_data;
334 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
335
336 if (req->status != 0) {
337 DBG(cdev, "gser ttyGS%d completion, err %d\n",
338 gser->port_num, req->status);
339 return;
340 }
341
342 /* normal completion */
343 if (req->actual != sizeof(gser->port_line_coding)) {
344 DBG(cdev, "gser ttyGS%d short resp, len %d\n",
345 gser->port_num, req->actual);
346 usb_ep_set_halt(ep);
347 } else {
348 struct usb_cdc_line_coding *value = req->buf;
349 gser->port_line_coding = *value;
350 }
351}
David Brownell61d8bae2008-06-19 18:18:50 -0700352/*-------------------------------------------------------------------------*/
353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354static int
355gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
356{
357 struct f_gser *gser = func_to_gser(f);
358 struct usb_composite_dev *cdev = f->config->cdev;
359 struct usb_request *req = cdev->req;
360 int value = -EOPNOTSUPP;
361 u16 w_index = le16_to_cpu(ctrl->wIndex);
362 u16 w_value = le16_to_cpu(ctrl->wValue);
363 u16 w_length = le16_to_cpu(ctrl->wLength);
364
365 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
366
367 /* SET_LINE_CODING ... just read and save what the host sends */
368 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
369 | USB_CDC_REQ_SET_LINE_CODING:
370 if (w_length != sizeof(struct usb_cdc_line_coding))
371 goto invalid;
372
373 value = w_length;
374 cdev->gadget->ep0->driver_data = gser;
375 req->complete = gser_complete_set_line_coding;
376 break;
377
378 /* GET_LINE_CODING ... return what host sent, or initial value */
379 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
380 | USB_CDC_REQ_GET_LINE_CODING:
381 value = min_t(unsigned, w_length,
382 sizeof(struct usb_cdc_line_coding));
383 memcpy(req->buf, &gser->port_line_coding, value);
384 break;
385
386 /* SET_CONTROL_LINE_STATE ... save what the host sent */
387 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
388 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
389
390 value = 0;
391 gser->port_handshake_bits = w_value;
392 if (gser->port.notify_modem) {
393 unsigned port_num =
394 gserial_ports[gser->port_num].client_port_num;
395
396 gser->port.notify_modem(&gser->port,
397 port_num, w_value);
398 }
399 break;
400
401 default:
402invalid:
403 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
404 ctrl->bRequestType, ctrl->bRequest,
405 w_value, w_index, w_length);
406 }
407
408 /* respond with data transfer or status phase? */
409 if (value >= 0) {
410 DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
411 gser->port_num, ctrl->bRequestType, ctrl->bRequest,
412 w_value, w_index, w_length);
413 req->zero = 0;
414 req->length = value;
415 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
416 if (value < 0)
417 ERROR(cdev, "gser response on ttyGS%d, err %d\n",
418 gser->port_num, value);
419 }
420
421 /* device either stalls (value < 0) or reports success */
422 return value;
423}
424#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700425static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
426{
427 struct f_gser *gser = func_to_gser(f);
428 struct usb_composite_dev *cdev = f->config->cdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429 int rc = 0;
David Brownell61d8bae2008-06-19 18:18:50 -0700430
431 /* we know alt == 0, so this is an activation or a reset */
432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433#ifdef CONFIG_MODEM_SUPPORT
434 if (gser->notify->driver_data) {
435 DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num);
436 usb_ep_disable(gser->notify);
David Brownell61d8bae2008-06-19 18:18:50 -0700437 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438 gser->notify_desc = ep_choose(cdev->gadget,
439 gser->hs.notify,
440 gser->fs.notify);
441 rc = usb_ep_enable(gser->notify, gser->notify_desc);
442 if (rc) {
443 ERROR(cdev, "can't enable %s, result %d\n",
444 gser->notify->name, rc);
445 return rc;
446 }
447 gser->notify->driver_data = gser;
448#endif
449
450 if (gser->port.in->driver_data) {
451 DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num);
452 gport_disconnect(gser);
453 } else {
454 DBG(cdev, "activate generic data ttyGS%d\n", gser->port_num);
455 }
456 gser->port.in_desc = ep_choose(cdev->gadget,
457 gser->hs.in, gser->fs.in);
458 gser->port.out_desc = ep_choose(cdev->gadget,
459 gser->hs.out, gser->fs.out);
460
461 gport_connect(gser);
462
463 gser->online = 1;
464 return rc;
David Brownell61d8bae2008-06-19 18:18:50 -0700465}
466
467static void gser_disable(struct usb_function *f)
468{
469 struct f_gser *gser = func_to_gser(f);
470 struct usb_composite_dev *cdev = f->config->cdev;
471
472 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473
474 gport_disconnect(gser);
475
476#ifdef CONFIG_MODEM_SUPPORT
477 usb_ep_fifo_flush(gser->notify);
478 usb_ep_disable(gser->notify);
479#endif
480 gser->online = 0;
481}
482#ifdef CONFIG_MODEM_SUPPORT
483static int gser_notify(struct f_gser *gser, u8 type, u16 value,
484 void *data, unsigned length)
485{
486 struct usb_ep *ep = gser->notify;
487 struct usb_request *req;
488 struct usb_cdc_notification *notify;
489 const unsigned len = sizeof(*notify) + length;
490 void *buf;
491 int status;
492 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
493
494 req = gser->notify_req;
495 gser->notify_req = NULL;
496 gser->pending = false;
497
498 req->length = len;
499 notify = req->buf;
500 buf = notify + 1;
501
502 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
503 | USB_RECIP_INTERFACE;
504 notify->bNotificationType = type;
505 notify->wValue = cpu_to_le16(value);
506 notify->wIndex = cpu_to_le16(gser->data_id);
507 notify->wLength = cpu_to_le16(length);
508 memcpy(buf, data, length);
509
510 status = usb_ep_queue(ep, req, GFP_ATOMIC);
511 if (status < 0) {
512 ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n",
513 gser->port_num, status);
514 gser->notify_req = req;
515 }
516
517 return status;
David Brownell61d8bae2008-06-19 18:18:50 -0700518}
519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520static int gser_notify_serial_state(struct f_gser *gser)
521{
522 int status;
523 unsigned long flags;
524 struct usb_composite_dev *cdev = gser->port.func.config->cdev;
525
526 spin_lock_irqsave(&gser->lock, flags);
527 if (gser->notify_req) {
528 DBG(cdev, "gser ttyGS%d serial state %04x\n",
529 gser->port_num, gser->serial_state);
530 status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE,
531 0, &gser->serial_state,
532 sizeof(gser->serial_state));
533 } else {
534 gser->pending = true;
535 status = 0;
536 }
537 spin_unlock_irqrestore(&gser->lock, flags);
538 return status;
539}
540
541static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req)
542{
543 struct f_gser *gser = req->context;
544 u8 doit = false;
545 unsigned long flags;
546
547 /* on this call path we do NOT hold the port spinlock,
548 * which is why ACM needs its own spinlock
549 */
550 spin_lock_irqsave(&gser->lock, flags);
551 if (req->status != -ESHUTDOWN)
552 doit = gser->pending;
553 gser->notify_req = req;
554 spin_unlock_irqrestore(&gser->lock, flags);
555
556 if (doit && gser->online)
557 gser_notify_serial_state(gser);
558}
559static void gser_connect(struct gserial *port)
560{
561 struct f_gser *gser = port_to_gser(port);
562
563 gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
564 gser_notify_serial_state(gser);
565}
566
567unsigned int gser_get_dtr(struct gserial *port)
568{
569 struct f_gser *gser = port_to_gser(port);
570
571 if (gser->port_handshake_bits & ACM_CTRL_DTR)
572 return 1;
573 else
574 return 0;
575}
576
577unsigned int gser_get_rts(struct gserial *port)
578{
579 struct f_gser *gser = port_to_gser(port);
580
581 if (gser->port_handshake_bits & ACM_CTRL_RTS)
582 return 1;
583 else
584 return 0;
585}
586
587unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes)
588{
589 struct f_gser *gser = port_to_gser(port);
590 u16 state;
591
592 state = gser->serial_state;
593 state &= ~ACM_CTRL_DCD;
594 if (yes)
595 state |= ACM_CTRL_DCD;
596
597 gser->serial_state = state;
598 return gser_notify_serial_state(gser);
599
600}
601
602unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes)
603{
604 struct f_gser *gser = port_to_gser(port);
605 u16 state;
606
607 state = gser->serial_state;
608 state &= ~ACM_CTRL_RI;
609 if (yes)
610 state |= ACM_CTRL_RI;
611
612 gser->serial_state = state;
613 return gser_notify_serial_state(gser);
614
615}
616static void gser_disconnect(struct gserial *port)
617{
618 struct f_gser *gser = port_to_gser(port);
619
620 gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
621 gser_notify_serial_state(gser);
622}
623
624static int gser_send_break(struct gserial *port, int duration)
625{
626 struct f_gser *gser = port_to_gser(port);
627 u16 state;
628
629 state = gser->serial_state;
630 state &= ~ACM_CTRL_BRK;
631 if (duration)
632 state |= ACM_CTRL_BRK;
633
634 gser->serial_state = state;
635 return gser_notify_serial_state(gser);
636}
637
638static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
639{
640 struct f_gser *gser = port_to_gser(port);
641
642 gser->serial_state = ctrl_bits;
643
644 return gser_notify_serial_state(gser);
645}
646#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700647/*-------------------------------------------------------------------------*/
648
649/* serial function driver setup/binding */
650
Manu Gautama4d993f2011-08-30 18:25:55 +0530651static int
David Brownell61d8bae2008-06-19 18:18:50 -0700652gser_bind(struct usb_configuration *c, struct usb_function *f)
653{
654 struct usb_composite_dev *cdev = c->cdev;
655 struct f_gser *gser = func_to_gser(f);
656 int status;
657 struct usb_ep *ep;
658
659 /* allocate instance-specific interface IDs */
660 status = usb_interface_id(c, f);
661 if (status < 0)
662 goto fail;
663 gser->data_id = status;
664 gser_interface_desc.bInterfaceNumber = status;
665
666 status = -ENODEV;
667
668 /* allocate instance-specific endpoints */
669 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
670 if (!ep)
671 goto fail;
672 gser->port.in = ep;
673 ep->driver_data = cdev; /* claim */
674
675 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
676 if (!ep)
677 goto fail;
678 gser->port.out = ep;
679 ep->driver_data = cdev; /* claim */
680
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681#ifdef CONFIG_MODEM_SUPPORT
682 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc);
683 if (!ep)
684 goto fail;
685 gser->notify = ep;
686 ep->driver_data = cdev; /* claim */
687 /* allocate notification */
688 gser->notify_req = gs_alloc_req(ep,
689 sizeof(struct usb_cdc_notification) + 2,
690 GFP_KERNEL);
691 if (!gser->notify_req)
692 goto fail;
693
694 gser->notify_req->complete = gser_notify_complete;
695 gser->notify_req->context = gser;
696#endif
697
David Brownell61d8bae2008-06-19 18:18:50 -0700698 /* copy descriptors, and track endpoint copies */
699 f->descriptors = usb_copy_descriptors(gser_fs_function);
700
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530701 if (!f->descriptors)
702 goto fail;
703
David Brownell61d8bae2008-06-19 18:18:50 -0700704 gser->fs.in = usb_find_endpoint(gser_fs_function,
705 f->descriptors, &gser_fs_in_desc);
706 gser->fs.out = usb_find_endpoint(gser_fs_function,
707 f->descriptors, &gser_fs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708#ifdef CONFIG_MODEM_SUPPORT
709 gser->fs.notify = usb_find_endpoint(gser_fs_function,
710 f->descriptors, &gser_fs_notify_desc);
711#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700712
713
714 /* support all relevant hardware speeds... we expect that when
715 * hardware is dual speed, all bulk-capable endpoints work at
716 * both speeds
717 */
718 if (gadget_is_dualspeed(c->cdev->gadget)) {
719 gser_hs_in_desc.bEndpointAddress =
720 gser_fs_in_desc.bEndpointAddress;
721 gser_hs_out_desc.bEndpointAddress =
722 gser_fs_out_desc.bEndpointAddress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723#ifdef CONFIG_MODEM_SUPPORT
724 gser_hs_notify_desc.bEndpointAddress =
725 gser_fs_notify_desc.bEndpointAddress;
726#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700727
728 /* copy descriptors, and track endpoint copies */
729 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
730
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530731 if (!f->hs_descriptors)
732 goto fail;
733
David Brownell61d8bae2008-06-19 18:18:50 -0700734 gser->hs.in = usb_find_endpoint(gser_hs_function,
735 f->hs_descriptors, &gser_hs_in_desc);
736 gser->hs.out = usb_find_endpoint(gser_hs_function,
737 f->hs_descriptors, &gser_hs_out_desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738#ifdef CONFIG_MODEM_SUPPORT
739 gser->hs.notify = usb_find_endpoint(gser_hs_function,
740 f->hs_descriptors, &gser_hs_notify_desc);
741#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700742 }
743
744 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
745 gser->port_num,
746 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
747 gser->port.in->name, gser->port.out->name);
748 return 0;
749
750fail:
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +0530751 if (f->descriptors)
752 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753#ifdef CONFIG_MODEM_SUPPORT
754 if (gser->notify_req)
755 gs_free_req(gser->notify, gser->notify_req);
756
757 /* we might as well release our claims on endpoints */
758 if (gser->notify)
759 gser->notify->driver_data = NULL;
760#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700761 /* we might as well release our claims on endpoints */
762 if (gser->port.out)
763 gser->port.out->driver_data = NULL;
764 if (gser->port.in)
765 gser->port.in->driver_data = NULL;
766
767 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
768
769 return status;
770}
771
772static void
773gser_unbind(struct usb_configuration *c, struct usb_function *f)
774{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775#ifdef CONFIG_MODEM_SUPPORT
776 struct f_gser *gser = func_to_gser(f);
777#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700778 if (gadget_is_dualspeed(c->cdev->gadget))
779 usb_free_descriptors(f->hs_descriptors);
780 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781#ifdef CONFIG_MODEM_SUPPORT
782 gs_free_req(gser->notify, gser->notify_req);
783#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700784 kfree(func_to_gser(f));
785}
786
787/**
788 * gser_bind_config - add a generic serial function to a configuration
789 * @c: the configuration to support the serial instance
790 * @port_num: /dev/ttyGS* port this interface will use
791 * Context: single threaded during gadget setup
792 *
793 * Returns zero on success, else negative errno.
794 *
795 * Caller must have called @gserial_setup() with enough ports to
796 * handle all the ones it binds. Caller is also responsible
797 * for calling @gserial_cleanup() before module unload.
798 */
Manu Gautama4d993f2011-08-30 18:25:55 +0530799int gser_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell61d8bae2008-06-19 18:18:50 -0700800{
801 struct f_gser *gser;
802 int status;
803
804 /* REVISIT might want instance-specific strings to help
805 * distinguish instances ...
806 */
807
808 /* maybe allocate device-global string ID */
809 if (gser_string_defs[0].id == 0) {
810 status = usb_string_id(c->cdev);
811 if (status < 0)
812 return status;
813 gser_string_defs[0].id = status;
814 }
815
816 /* allocate and initialize one new instance */
817 gser = kzalloc(sizeof *gser, GFP_KERNEL);
818 if (!gser)
819 return -ENOMEM;
820
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821#ifdef CONFIG_MODEM_SUPPORT
822 spin_lock_init(&gser->lock);
823#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700824 gser->port_num = port_num;
825
826 gser->port.func.name = "gser";
827 gser->port.func.strings = gser_strings;
828 gser->port.func.bind = gser_bind;
829 gser->port.func.unbind = gser_unbind;
830 gser->port.func.set_alt = gser_set_alt;
831 gser->port.func.disable = gser_disable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 gser->transport = gserial_ports[port_num].transport;
833#ifdef CONFIG_MODEM_SUPPORT
834 /* We support only two ports for now */
835 if (port_num == 0)
836 gser->port.func.name = "modem";
837 else
838 gser->port.func.name = "nmea";
839 gser->port.func.setup = gser_setup;
840 gser->port.connect = gser_connect;
841 gser->port.get_dtr = gser_get_dtr;
842 gser->port.get_rts = gser_get_rts;
843 gser->port.send_carrier_detect = gser_send_carrier_detect;
844 gser->port.send_ring_indicator = gser_send_ring_indicator;
845 gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits;
846 gser->port.disconnect = gser_disconnect;
847 gser->port.send_break = gser_send_break;
848#endif
David Brownell61d8bae2008-06-19 18:18:50 -0700849
850 status = usb_add_function(c, &gser->port.func);
851 if (status)
852 kfree(gser);
853 return status;
854}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855
Manu Gautama4d993f2011-08-30 18:25:55 +0530856/**
857 * gserial_init_port - bind a gserial_port to its transport
858 */
859static int gserial_init_port(int port_num, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860{
Manu Gautama4d993f2011-08-30 18:25:55 +0530861 enum transport_type transport;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
Manu Gautama4d993f2011-08-30 18:25:55 +0530863 if (port_num >= GSERIAL_NO_PORTS)
864 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865
Hemant Kumar1b820d52011-11-03 15:08:28 -0700866 transport = str_to_xport(name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530867 pr_debug("%s, port:%d, transport:%s\n", __func__,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700868 port_num, xport_to_str(transport));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869
Manu Gautama4d993f2011-08-30 18:25:55 +0530870 gserial_ports[port_num].transport = transport;
871 gserial_ports[port_num].port_num = port_num;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872
Manu Gautama4d993f2011-08-30 18:25:55 +0530873 switch (transport) {
Hemant Kumarbffd4142011-11-03 13:20:40 -0700874 case USB_GADGET_XPORT_TTY:
Manu Gautama4d993f2011-08-30 18:25:55 +0530875 gserial_ports[port_num].client_port_num = no_tty_ports;
876 no_tty_ports++;
877 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700878 case USB_GADGET_XPORT_SDIO:
Manu Gautama4d993f2011-08-30 18:25:55 +0530879 gserial_ports[port_num].client_port_num = no_sdio_ports;
880 no_sdio_ports++;
881 break;
Hemant Kumarbffd4142011-11-03 13:20:40 -0700882 case USB_GADGET_XPORT_SMD:
Manu Gautama4d993f2011-08-30 18:25:55 +0530883 gserial_ports[port_num].client_port_num = no_smd_ports;
884 no_smd_ports++;
885 break;
886 default:
887 pr_err("%s: Un-supported transport transport: %u\n",
888 __func__, gserial_ports[port_num].transport);
889 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 }
891
Manu Gautama4d993f2011-08-30 18:25:55 +0530892 nr_ports++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
894 return 0;
895}