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