blob: cf33a8d0fd5df46ec339f5b481b68dc61121243a [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>
16
17#include "u_serial.h"
18#include "gadget_chips.h"
19
20
21/*
22 * This function packages a simple "generic serial" port with no real
23 * control mechanisms, just raw data transfer over two bulk endpoints.
24 *
25 * Because it's not standardized, this isn't as interoperable as the
26 * CDC ACM driver. However, for many purposes it's just as functional
27 * if you can arrange appropriate host side drivers.
28 */
29
David Brownell61d8bae2008-06-19 18:18:50 -070030struct f_gser {
31 struct gserial port;
32 u8 data_id;
33 u8 port_num;
David Brownell61d8bae2008-06-19 18:18:50 -070034};
35
36static inline struct f_gser *func_to_gser(struct usb_function *f)
37{
38 return container_of(f, struct f_gser, port.func);
39}
40
41/*-------------------------------------------------------------------------*/
42
43/* interface descriptor: */
44
45static struct usb_interface_descriptor gser_interface_desc __initdata = {
46 .bLength = USB_DT_INTERFACE_SIZE,
47 .bDescriptorType = USB_DT_INTERFACE,
48 /* .bInterfaceNumber = DYNAMIC */
49 .bNumEndpoints = 2,
50 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
51 .bInterfaceSubClass = 0,
52 .bInterfaceProtocol = 0,
53 /* .iInterface = DYNAMIC */
54};
55
56/* full speed support: */
57
58static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = {
59 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK,
63};
64
65static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68 .bEndpointAddress = USB_DIR_OUT,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK,
70};
71
72static struct usb_descriptor_header *gser_fs_function[] __initdata = {
73 (struct usb_descriptor_header *) &gser_interface_desc,
74 (struct usb_descriptor_header *) &gser_fs_in_desc,
75 (struct usb_descriptor_header *) &gser_fs_out_desc,
76 NULL,
77};
78
79/* high speed support: */
80
81static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080085 .wMaxPacketSize = cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -070086};
87
88static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080092 .wMaxPacketSize = cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -070093};
94
95static struct usb_descriptor_header *gser_hs_function[] __initdata = {
96 (struct usb_descriptor_header *) &gser_interface_desc,
97 (struct usb_descriptor_header *) &gser_hs_in_desc,
98 (struct usb_descriptor_header *) &gser_hs_out_desc,
99 NULL,
100};
101
102/* string descriptors: */
103
104static struct usb_string gser_string_defs[] = {
105 [0].s = "Generic Serial",
106 { } /* end of list */
107};
108
109static struct usb_gadget_strings gser_string_table = {
110 .language = 0x0409, /* en-us */
111 .strings = gser_string_defs,
112};
113
114static struct usb_gadget_strings *gser_strings[] = {
115 &gser_string_table,
116 NULL,
117};
118
119/*-------------------------------------------------------------------------*/
120
121static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
122{
123 struct f_gser *gser = func_to_gser(f);
124 struct usb_composite_dev *cdev = f->config->cdev;
125
126 /* we know alt == 0, so this is an activation or a reset */
127
128 if (gser->port.in->driver_data) {
129 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
130 gserial_disconnect(&gser->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300131 }
132 if (!gser->port.in->desc || !gser->port.out->desc) {
David Brownell61d8bae2008-06-19 18:18:50 -0700133 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
Robert Jarzmikfef69642011-11-18 20:16:27 +0100134 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
135 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300136 gser->port.in->desc = NULL;
137 gser->port.out->desc = NULL;
138 return -EINVAL;
139 }
David Brownell61d8bae2008-06-19 18:18:50 -0700140 }
141 gserial_connect(&gser->port, gser->port_num);
142 return 0;
143}
144
145static void gser_disable(struct usb_function *f)
146{
147 struct f_gser *gser = func_to_gser(f);
148 struct usb_composite_dev *cdev = f->config->cdev;
149
150 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
151 gserial_disconnect(&gser->port);
152}
153
154/*-------------------------------------------------------------------------*/
155
156/* serial function driver setup/binding */
157
158static int __init
159gser_bind(struct usb_configuration *c, struct usb_function *f)
160{
161 struct usb_composite_dev *cdev = c->cdev;
162 struct f_gser *gser = func_to_gser(f);
163 int status;
164 struct usb_ep *ep;
165
166 /* allocate instance-specific interface IDs */
167 status = usb_interface_id(c, f);
168 if (status < 0)
169 goto fail;
170 gser->data_id = status;
171 gser_interface_desc.bInterfaceNumber = status;
172
173 status = -ENODEV;
174
175 /* allocate instance-specific endpoints */
176 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
177 if (!ep)
178 goto fail;
179 gser->port.in = ep;
180 ep->driver_data = cdev; /* claim */
181
182 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
183 if (!ep)
184 goto fail;
185 gser->port.out = ep;
186 ep->driver_data = cdev; /* claim */
187
188 /* copy descriptors, and track endpoint copies */
189 f->descriptors = usb_copy_descriptors(gser_fs_function);
190
David Brownell61d8bae2008-06-19 18:18:50 -0700191 /* support all relevant hardware speeds... we expect that when
192 * hardware is dual speed, all bulk-capable endpoints work at
193 * both speeds
194 */
195 if (gadget_is_dualspeed(c->cdev->gadget)) {
196 gser_hs_in_desc.bEndpointAddress =
197 gser_fs_in_desc.bEndpointAddress;
198 gser_hs_out_desc.bEndpointAddress =
199 gser_fs_out_desc.bEndpointAddress;
200
201 /* copy descriptors, and track endpoint copies */
202 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
David Brownell61d8bae2008-06-19 18:18:50 -0700203 }
204
205 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
206 gser->port_num,
207 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
208 gser->port.in->name, gser->port.out->name);
209 return 0;
210
211fail:
212 /* we might as well release our claims on endpoints */
213 if (gser->port.out)
214 gser->port.out->driver_data = NULL;
215 if (gser->port.in)
216 gser->port.in->driver_data = NULL;
217
218 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
219
220 return status;
221}
222
223static void
224gser_unbind(struct usb_configuration *c, struct usb_function *f)
225{
226 if (gadget_is_dualspeed(c->cdev->gadget))
227 usb_free_descriptors(f->hs_descriptors);
228 usb_free_descriptors(f->descriptors);
229 kfree(func_to_gser(f));
230}
231
232/**
233 * gser_bind_config - add a generic serial function to a configuration
234 * @c: the configuration to support the serial instance
235 * @port_num: /dev/ttyGS* port this interface will use
236 * Context: single threaded during gadget setup
237 *
238 * Returns zero on success, else negative errno.
239 *
240 * Caller must have called @gserial_setup() with enough ports to
241 * handle all the ones it binds. Caller is also responsible
242 * for calling @gserial_cleanup() before module unload.
243 */
244int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
245{
246 struct f_gser *gser;
247 int status;
248
249 /* REVISIT might want instance-specific strings to help
250 * distinguish instances ...
251 */
252
253 /* maybe allocate device-global string ID */
254 if (gser_string_defs[0].id == 0) {
255 status = usb_string_id(c->cdev);
256 if (status < 0)
257 return status;
258 gser_string_defs[0].id = status;
259 }
260
261 /* allocate and initialize one new instance */
262 gser = kzalloc(sizeof *gser, GFP_KERNEL);
263 if (!gser)
264 return -ENOMEM;
265
266 gser->port_num = port_num;
267
268 gser->port.func.name = "gser";
269 gser->port.func.strings = gser_strings;
270 gser->port.func.bind = gser_bind;
271 gser->port.func.unbind = gser_unbind;
272 gser->port.func.set_alt = gser_set_alt;
273 gser->port.func.disable = gser_disable;
274
275 status = usb_add_function(c, &gser->port.func);
276 if (status)
277 kfree(gser);
278 return status;
279}