blob: ec837f4ea416223d8a1e816bd95256de0dda1c85 [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"
David Brownell61d8bae2008-06-19 18:18:50 -070019
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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010045static struct usb_interface_descriptor gser_interface_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070046 .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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010058static struct usb_endpoint_descriptor gser_fs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070059 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK,
63};
64
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010065static struct usb_endpoint_descriptor gser_fs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070066 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68 .bEndpointAddress = USB_DIR_OUT,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK,
70};
71
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010072static struct usb_descriptor_header *gser_fs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -070073 (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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010081static struct usb_endpoint_descriptor gser_hs_in_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070082 .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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010088static struct usb_endpoint_descriptor gser_hs_out_desc = {
David Brownell61d8bae2008-06-19 18:18:50 -070089 .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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +010095static struct usb_descriptor_header *gser_hs_function[] = {
David Brownell61d8bae2008-06-19 18:18:50 -070096 (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
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100102static struct usb_endpoint_descriptor gser_ss_in_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100103 .bLength = USB_DT_ENDPOINT_SIZE,
104 .bDescriptorType = USB_DT_ENDPOINT,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = cpu_to_le16(1024),
107};
108
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100109static struct usb_endpoint_descriptor gser_ss_out_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100110 .bLength = USB_DT_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_ENDPOINT,
112 .bmAttributes = USB_ENDPOINT_XFER_BULK,
113 .wMaxPacketSize = cpu_to_le16(1024),
114};
115
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100116static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100117 .bLength = sizeof gser_ss_bulk_comp_desc,
118 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
119};
120
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100121static struct usb_descriptor_header *gser_ss_function[] = {
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100122 (struct usb_descriptor_header *) &gser_interface_desc,
123 (struct usb_descriptor_header *) &gser_ss_in_desc,
124 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
125 (struct usb_descriptor_header *) &gser_ss_out_desc,
126 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
127 NULL,
128};
129
David Brownell61d8bae2008-06-19 18:18:50 -0700130/* string descriptors: */
131
132static struct usb_string gser_string_defs[] = {
133 [0].s = "Generic Serial",
134 { } /* end of list */
135};
136
137static struct usb_gadget_strings gser_string_table = {
138 .language = 0x0409, /* en-us */
139 .strings = gser_string_defs,
140};
141
142static struct usb_gadget_strings *gser_strings[] = {
143 &gser_string_table,
144 NULL,
145};
146
147/*-------------------------------------------------------------------------*/
148
149static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
150{
151 struct f_gser *gser = func_to_gser(f);
152 struct usb_composite_dev *cdev = f->config->cdev;
153
154 /* we know alt == 0, so this is an activation or a reset */
155
156 if (gser->port.in->driver_data) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200157 dev_dbg(&cdev->gadget->dev,
158 "reset generic ttyGS%d\n", gser->port_num);
David Brownell61d8bae2008-06-19 18:18:50 -0700159 gserial_disconnect(&gser->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300160 }
161 if (!gser->port.in->desc || !gser->port.out->desc) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200162 dev_dbg(&cdev->gadget->dev,
163 "activate generic ttyGS%d\n", gser->port_num);
Robert Jarzmikfef69642011-11-18 20:16:27 +0100164 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
165 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300166 gser->port.in->desc = NULL;
167 gser->port.out->desc = NULL;
168 return -EINVAL;
169 }
David Brownell61d8bae2008-06-19 18:18:50 -0700170 }
171 gserial_connect(&gser->port, gser->port_num);
172 return 0;
173}
174
175static void gser_disable(struct usb_function *f)
176{
177 struct f_gser *gser = func_to_gser(f);
178 struct usb_composite_dev *cdev = f->config->cdev;
179
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200180 dev_dbg(&cdev->gadget->dev,
181 "generic ttyGS%d deactivated\n", gser->port_num);
David Brownell61d8bae2008-06-19 18:18:50 -0700182 gserial_disconnect(&gser->port);
183}
184
185/*-------------------------------------------------------------------------*/
186
187/* serial function driver setup/binding */
188
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100189static int gser_bind(struct usb_configuration *c, struct usb_function *f)
David Brownell61d8bae2008-06-19 18:18:50 -0700190{
191 struct usb_composite_dev *cdev = c->cdev;
192 struct f_gser *gser = func_to_gser(f);
193 int status;
194 struct usb_ep *ep;
195
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100196 /* REVISIT might want instance-specific strings to help
197 * distinguish instances ...
198 */
199
200 /* maybe allocate device-global string ID */
201 if (gser_string_defs[0].id == 0) {
202 status = usb_string_id(c->cdev);
203 if (status < 0)
204 return status;
205 gser_string_defs[0].id = status;
206 }
207
David Brownell61d8bae2008-06-19 18:18:50 -0700208 /* allocate instance-specific interface IDs */
209 status = usb_interface_id(c, f);
210 if (status < 0)
211 goto fail;
212 gser->data_id = status;
213 gser_interface_desc.bInterfaceNumber = status;
214
215 status = -ENODEV;
216
217 /* allocate instance-specific endpoints */
218 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
219 if (!ep)
220 goto fail;
221 gser->port.in = ep;
222 ep->driver_data = cdev; /* claim */
223
224 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
225 if (!ep)
226 goto fail;
227 gser->port.out = ep;
228 ep->driver_data = cdev; /* claim */
229
David Brownell61d8bae2008-06-19 18:18:50 -0700230 /* support all relevant hardware speeds... we expect that when
231 * hardware is dual speed, all bulk-capable endpoints work at
232 * both speeds
233 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200234 gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
235 gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
David Brownell61d8bae2008-06-19 18:18:50 -0700236
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200237 gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
238 gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100239
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200240 status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
241 gser_ss_function);
242 if (status)
243 goto fail;
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200244 dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
245 gser->port_num,
246 gadget_is_superspeed(c->cdev->gadget) ? "super" :
247 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
248 gser->port.in->name, gser->port.out->name);
David Brownell61d8bae2008-06-19 18:18:50 -0700249 return 0;
250
251fail:
252 /* we might as well release our claims on endpoints */
253 if (gser->port.out)
254 gser->port.out->driver_data = NULL;
255 if (gser->port.in)
256 gser->port.in->driver_data = NULL;
257
258 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
259
260 return status;
261}
262
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100263static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
264{
265 return container_of(to_config_group(item), struct f_serial_opts,
266 func_inst.group);
267}
268
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100269static void serial_attr_release(struct config_item *item)
270{
271 struct f_serial_opts *opts = to_f_serial_opts(item);
272
273 usb_put_function_instance(&opts->func_inst);
274}
275
276static struct configfs_item_operations serial_item_ops = {
277 .release = serial_attr_release,
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100278};
279
Christoph Hellwig0b4be4f2015-10-03 15:32:52 +0200280static ssize_t f_serial_port_num_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100281{
Christoph Hellwig0b4be4f2015-10-03 15:32:52 +0200282 return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100283}
284
Christoph Hellwig0b4be4f2015-10-03 15:32:52 +0200285CONFIGFS_ATTR_RO(f_serial_, port_num);
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100286
287static struct configfs_attribute *acm_attrs[] = {
Christoph Hellwig0b4be4f2015-10-03 15:32:52 +0200288 &f_serial_attr_port_num,
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100289 NULL,
290};
291
292static struct config_item_type serial_func_type = {
293 .ct_item_ops = &serial_item_ops,
294 .ct_attrs = acm_attrs,
295 .ct_owner = THIS_MODULE,
296};
297
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100298static void gser_free_inst(struct usb_function_instance *f)
299{
300 struct f_serial_opts *opts;
301
302 opts = container_of(f, struct f_serial_opts, func_inst);
303 gserial_free_line(opts->port_num);
304 kfree(opts);
305}
306
307static struct usb_function_instance *gser_alloc_inst(void)
308{
309 struct f_serial_opts *opts;
310 int ret;
311
312 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
313 if (!opts)
314 return ERR_PTR(-ENOMEM);
315
316 opts->func_inst.free_func_inst = gser_free_inst;
317 ret = gserial_alloc_line(&opts->port_num);
318 if (ret) {
319 kfree(opts);
320 return ERR_PTR(ret);
321 }
Andrzej Pietrasiewicz0b6a1e62013-03-27 09:12:03 +0100322 config_group_init_type_name(&opts->func_inst.group, "",
323 &serial_func_type);
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100324
325 return &opts->func_inst;
326}
327
328static void gser_free(struct usb_function *f)
329{
330 struct f_gser *serial;
331
332 serial = func_to_gser(f);
333 kfree(serial);
334}
335
336static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
337{
338 usb_free_all_descriptors(f);
339}
340
Jingoo Hanb3d589f2013-12-16 18:44:25 +0900341static struct usb_function *gser_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewicz60540ea2013-03-18 09:52:57 +0100342{
343 struct f_gser *gser;
344 struct f_serial_opts *opts;
345
346 /* allocate and initialize one new instance */
347 gser = kzalloc(sizeof(*gser), GFP_KERNEL);
348 if (!gser)
349 return ERR_PTR(-ENOMEM);
350
351 opts = container_of(fi, struct f_serial_opts, func_inst);
352
353 gser->port_num = opts->port_num;
354
355 gser->port.func.name = "gser";
356 gser->port.func.strings = gser_strings;
357 gser->port.func.bind = gser_bind;
358 gser->port.func.unbind = gser_unbind;
359 gser->port.func.set_alt = gser_set_alt;
360 gser->port.func.disable = gser_disable;
361 gser->port.func.free_func = gser_free;
362
363 return &gser->port.func;
364}
365
366DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
367MODULE_LICENSE("GPL");
368MODULE_AUTHOR("Al Borchers");
369MODULE_AUTHOR("David Brownell");