blob: 49c3d146f9b0fe66c13be9b90e7677fd2c503d94 [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>
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010015#include <linux/module.h>
David Brownell61d8bae2008-06-19 18:18:50 -070016#include <linux/device.h>
17
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 */
30
David Brownell61d8bae2008-06-19 18:18:50 -070031struct f_gser {
32 struct gserial port;
33 u8 data_id;
34 u8 port_num;
David Brownell61d8bae2008-06-19 18:18:50 -070035};
36
37static inline struct f_gser *func_to_gser(struct usb_function *f)
38{
39 return container_of(f, struct f_gser, port.func);
40}
41
42/*-------------------------------------------------------------------------*/
43
44/* interface descriptor: */
45
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010046static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070047 .bLength = USB_DT_INTERFACE_SIZE,
48 .bDescriptorType = USB_DT_INTERFACE,
49 /* .bInterfaceNumber = DYNAMIC */
50 .bNumEndpoints = 2,
51 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
52 .bInterfaceSubClass = 0,
53 .bInterfaceProtocol = 0,
54 /* .iInterface = DYNAMIC */
55};
56
57/* full speed support: */
58
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010059static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070060 .bLength = USB_DT_ENDPOINT_SIZE,
61 .bDescriptorType = USB_DT_ENDPOINT,
62 .bEndpointAddress = USB_DIR_IN,
63 .bmAttributes = USB_ENDPOINT_XFER_BULK,
64};
65
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010066static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070067 .bLength = USB_DT_ENDPOINT_SIZE,
68 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = USB_DIR_OUT,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
71};
72
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010073static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -070074 (struct usb_descriptor_header *) &gser_interface_desc,
75 (struct usb_descriptor_header *) &gser_fs_in_desc,
76 (struct usb_descriptor_header *) &gser_fs_out_desc,
77 NULL,
78};
79
80/* high speed support: */
81
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010082static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070083 .bLength = USB_DT_ENDPOINT_SIZE,
84 .bDescriptorType = USB_DT_ENDPOINT,
85 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080086 .wMaxPacketSize = cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -070087};
88
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010089static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070090 .bLength = USB_DT_ENDPOINT_SIZE,
91 .bDescriptorType = USB_DT_ENDPOINT,
92 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080093 .wMaxPacketSize = cpu_to_le16(512),
David Brownell61d8bae2008-06-19 18:18:50 -070094};
95
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010096static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -070097 (struct usb_descriptor_header *) &gser_interface_desc,
98 (struct usb_descriptor_header *) &gser_hs_in_desc,
99 (struct usb_descriptor_header *) &gser_hs_out_desc,
100 NULL,
101};
102
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100103static struct usb_endpoint_descriptor gser_ss_in_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bmAttributes = USB_ENDPOINT_XFER_BULK,
107 .wMaxPacketSize = cpu_to_le16(1024),
108};
109
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100110static struct usb_endpoint_descriptor gser_ss_out_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100111 .bLength = USB_DT_ENDPOINT_SIZE,
112 .bDescriptorType = USB_DT_ENDPOINT,
113 .bmAttributes = USB_ENDPOINT_XFER_BULK,
114 .wMaxPacketSize = cpu_to_le16(1024),
115};
116
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100117static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100118 .bLength = sizeof gser_ss_bulk_comp_desc,
119 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
120};
121
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100122static struct usb_descriptor_header *gser_ss_function[] = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100123 (struct usb_descriptor_header *) &gser_interface_desc,
124 (struct usb_descriptor_header *) &gser_ss_in_desc,
125 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
126 (struct usb_descriptor_header *) &gser_ss_out_desc,
127 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
128 NULL,
129};
130
David Brownell61d8bae2008-06-19 18:18:50 -0700131/* string descriptors: */
132
133static struct usb_string gser_string_defs[] = {
134 [0].s = "Generic Serial",
135 { } /* end of list */
136};
137
138static struct usb_gadget_strings gser_string_table = {
139 .language = 0x0409, /* en-us */
140 .strings = gser_string_defs,
141};
142
143static struct usb_gadget_strings *gser_strings[] = {
144 &gser_string_table,
145 NULL,
146};
147
148/*-------------------------------------------------------------------------*/
149
150static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
151{
152 struct f_gser *gser = func_to_gser(f);
153 struct usb_composite_dev *cdev = f->config->cdev;
154
155 /* we know alt == 0, so this is an activation or a reset */
156
157 if (gser->port.in->driver_data) {
158 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
159 gserial_disconnect(&gser->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300160 }
161 if (!gser->port.in->desc || !gser->port.out->desc) {
David Brownell61d8bae2008-06-19 18:18:50 -0700162 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
Robert Jarzmikfef69642011-11-18 20:16:27 +0100163 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
164 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300165 gser->port.in->desc = NULL;
166 gser->port.out->desc = NULL;
167 return -EINVAL;
168 }
David Brownell61d8bae2008-06-19 18:18:50 -0700169 }
170 gserial_connect(&gser->port, gser->port_num);
171 return 0;
172}
173
174static void gser_disable(struct usb_function *f)
175{
176 struct f_gser *gser = func_to_gser(f);
177 struct usb_composite_dev *cdev = f->config->cdev;
178
179 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
180 gserial_disconnect(&gser->port);
181}
182
183/*-------------------------------------------------------------------------*/
184
185/* serial function driver setup/binding */
186
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100187static int gser_bind(struct usb_configuration *c, struct usb_function *f)
David Brownell61d8bae2008-06-19 18:18:50 -0700188{
189 struct usb_composite_dev *cdev = c->cdev;
190 struct f_gser *gser = func_to_gser(f);
191 int status;
192 struct usb_ep *ep;
193
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100194 /* REVISIT might want instance-specific strings to help
195 * distinguish instances ...
196 */
197
198 /* maybe allocate device-global string ID */
199 if (gser_string_defs[0].id == 0) {
200 status = usb_string_id(c->cdev);
201 if (status < 0)
202 return status;
203 gser_string_defs[0].id = status;
204 }
205
David Brownell61d8bae2008-06-19 18:18:50 -0700206 /* allocate instance-specific interface IDs */
207 status = usb_interface_id(c, f);
208 if (status < 0)
209 goto fail;
210 gser->data_id = status;
211 gser_interface_desc.bInterfaceNumber = status;
212
213 status = -ENODEV;
214
215 /* allocate instance-specific endpoints */
216 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
217 if (!ep)
218 goto fail;
219 gser->port.in = ep;
220 ep->driver_data = cdev; /* claim */
221
222 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
223 if (!ep)
224 goto fail;
225 gser->port.out = ep;
226 ep->driver_data = cdev; /* claim */
227
David Brownell61d8bae2008-06-19 18:18:50 -0700228 /* support all relevant hardware speeds... we expect that when
229 * hardware is dual speed, all bulk-capable endpoints work at
230 * both speeds
231 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200232 gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
233 gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
David Brownell61d8bae2008-06-19 18:18:50 -0700234
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200235 gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
236 gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100237
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200238 status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
239 gser_ss_function);
240 if (status)
241 goto fail;
David Brownell61d8bae2008-06-19 18:18:50 -0700242 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
243 gser->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100244 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell61d8bae2008-06-19 18:18:50 -0700245 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
246 gser->port.in->name, gser->port.out->name);
247 return 0;
248
249fail:
250 /* we might as well release our claims on endpoints */
251 if (gser->port.out)
252 gser->port.out->driver_data = NULL;
253 if (gser->port.in)
254 gser->port.in->driver_data = NULL;
255
256 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
257
258 return status;
259}
260
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100261static void gser_free_inst(struct usb_function_instance *f)
262{
263 struct f_serial_opts *opts;
264
265 opts = container_of(f, struct f_serial_opts, func_inst);
266 gserial_free_line(opts->port_num);
267 kfree(opts);
268}
269
270static struct usb_function_instance *gser_alloc_inst(void)
271{
272 struct f_serial_opts *opts;
273 int ret;
274
275 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
276 if (!opts)
277 return ERR_PTR(-ENOMEM);
278
279 opts->func_inst.free_func_inst = gser_free_inst;
280 ret = gserial_alloc_line(&opts->port_num);
281 if (ret) {
282 kfree(opts);
283 return ERR_PTR(ret);
284 }
285
286 return &opts->func_inst;
287}
288
289static void gser_free(struct usb_function *f)
290{
291 struct f_gser *serial;
292
293 serial = func_to_gser(f);
294 kfree(serial);
295}
296
297static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
298{
299 usb_free_all_descriptors(f);
300}
301
302struct usb_function *gser_alloc(struct usb_function_instance *fi)
303{
304 struct f_gser *gser;
305 struct f_serial_opts *opts;
306
307 /* allocate and initialize one new instance */
308 gser = kzalloc(sizeof(*gser), GFP_KERNEL);
309 if (!gser)
310 return ERR_PTR(-ENOMEM);
311
312 opts = container_of(fi, struct f_serial_opts, func_inst);
313
314 gser->port_num = opts->port_num;
315
316 gser->port.func.name = "gser";
317 gser->port.func.strings = gser_strings;
318 gser->port.func.bind = gser_bind;
319 gser->port.func.unbind = gser_unbind;
320 gser->port.func.set_alt = gser_set_alt;
321 gser->port.func.disable = gser_disable;
322 gser->port.func.free_func = gser_free;
323
324 return &gser->port.func;
325}
326
327DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
328MODULE_LICENSE("GPL");
329MODULE_AUTHOR("Al Borchers");
330MODULE_AUTHOR("David Brownell");