1 // SPDX-License-Identifier: GPL-2.0+
3 * f_serial.c - generic USB serial function driver
6 * Copyright (C) 2008 by David Brownell
7 * Copyright (C) 2008 by Nokia Corporation
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
19 * This function packages a simple "generic serial" port with no real
20 * control mechanisms, just raw data transfer over two bulk endpoints.
22 * Because it's not standardized, this isn't as interoperable as the
23 * CDC ACM driver. However, for many purposes it's just as functional
24 * if you can arrange appropriate host side drivers.
33 static inline struct f_gser *func_to_gser(struct usb_function *f)
35 return container_of(f, struct f_gser, port.func);
38 /*-------------------------------------------------------------------------*/
40 /* interface descriptor: */
42 static struct usb_interface_descriptor gser_interface_desc = {
43 .bLength = USB_DT_INTERFACE_SIZE,
44 .bDescriptorType = USB_DT_INTERFACE,
45 /* .bInterfaceNumber = DYNAMIC */
47 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
48 .bInterfaceSubClass = 0,
49 .bInterfaceProtocol = 0,
50 /* .iInterface = DYNAMIC */
53 /* full speed support: */
55 static struct usb_endpoint_descriptor gser_fs_in_desc = {
56 .bLength = USB_DT_ENDPOINT_SIZE,
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bEndpointAddress = USB_DIR_IN,
59 .bmAttributes = USB_ENDPOINT_XFER_BULK,
62 static struct usb_endpoint_descriptor gser_fs_out_desc = {
63 .bLength = USB_DT_ENDPOINT_SIZE,
64 .bDescriptorType = USB_DT_ENDPOINT,
65 .bEndpointAddress = USB_DIR_OUT,
66 .bmAttributes = USB_ENDPOINT_XFER_BULK,
69 static struct usb_descriptor_header *gser_fs_function[] = {
70 (struct usb_descriptor_header *) &gser_interface_desc,
71 (struct usb_descriptor_header *) &gser_fs_in_desc,
72 (struct usb_descriptor_header *) &gser_fs_out_desc,
76 /* high speed support: */
78 static struct usb_endpoint_descriptor gser_hs_in_desc = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bmAttributes = USB_ENDPOINT_XFER_BULK,
82 .wMaxPacketSize = cpu_to_le16(512),
85 static struct usb_endpoint_descriptor gser_hs_out_desc = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
88 .bmAttributes = USB_ENDPOINT_XFER_BULK,
89 .wMaxPacketSize = cpu_to_le16(512),
92 static struct usb_descriptor_header *gser_hs_function[] = {
93 (struct usb_descriptor_header *) &gser_interface_desc,
94 (struct usb_descriptor_header *) &gser_hs_in_desc,
95 (struct usb_descriptor_header *) &gser_hs_out_desc,
99 static struct usb_endpoint_descriptor gser_ss_in_desc = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
103 .wMaxPacketSize = cpu_to_le16(1024),
106 static struct usb_endpoint_descriptor gser_ss_out_desc = {
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bmAttributes = USB_ENDPOINT_XFER_BULK,
110 .wMaxPacketSize = cpu_to_le16(1024),
113 static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
114 .bLength = sizeof gser_ss_bulk_comp_desc,
115 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
118 static struct usb_descriptor_header *gser_ss_function[] = {
119 (struct usb_descriptor_header *) &gser_interface_desc,
120 (struct usb_descriptor_header *) &gser_ss_in_desc,
121 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
122 (struct usb_descriptor_header *) &gser_ss_out_desc,
123 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
127 /* string descriptors: */
129 static struct usb_string gser_string_defs[] = {
130 [0].s = "Generic Serial",
131 { } /* end of list */
134 static struct usb_gadget_strings gser_string_table = {
135 .language = 0x0409, /* en-us */
136 .strings = gser_string_defs,
139 static struct usb_gadget_strings *gser_strings[] = {
144 /*-------------------------------------------------------------------------*/
146 static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
148 struct f_gser *gser = func_to_gser(f);
149 struct usb_composite_dev *cdev = f->config->cdev;
151 /* we know alt == 0, so this is an activation or a reset */
153 if (gser->port.in->enabled) {
154 dev_dbg(&cdev->gadget->dev,
155 "reset generic ttyGS%d\n", gser->port_num);
156 gserial_disconnect(&gser->port);
158 if (!gser->port.in->desc || !gser->port.out->desc) {
159 dev_dbg(&cdev->gadget->dev,
160 "activate generic ttyGS%d\n", gser->port_num);
161 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
162 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
163 gser->port.in->desc = NULL;
164 gser->port.out->desc = NULL;
168 gserial_connect(&gser->port, gser->port_num);
172 static void gser_disable(struct usb_function *f)
174 struct f_gser *gser = func_to_gser(f);
175 struct usb_composite_dev *cdev = f->config->cdev;
177 dev_dbg(&cdev->gadget->dev,
178 "generic ttyGS%d deactivated\n", gser->port_num);
179 gserial_disconnect(&gser->port);
182 /*-------------------------------------------------------------------------*/
184 /* serial function driver setup/binding */
186 static int gser_bind(struct usb_configuration *c, struct usb_function *f)
188 struct usb_composite_dev *cdev = c->cdev;
189 struct f_gser *gser = func_to_gser(f);
193 /* REVISIT might want instance-specific strings to help
194 * distinguish instances ...
197 /* maybe allocate device-global string ID */
198 if (gser_string_defs[0].id == 0) {
199 status = usb_string_id(c->cdev);
202 gser_string_defs[0].id = status;
205 /* allocate instance-specific interface IDs */
206 status = usb_interface_id(c, f);
209 gser->data_id = status;
210 gser_interface_desc.bInterfaceNumber = status;
214 /* allocate instance-specific endpoints */
215 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
220 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
225 /* support all relevant hardware speeds... we expect that when
226 * hardware is dual speed, all bulk-capable endpoints work at
229 gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
230 gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
232 gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
233 gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
235 status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
236 gser_ss_function, gser_ss_function);
239 dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: IN/%s OUT/%s\n",
241 gser->port.in->name, gser->port.out->name);
245 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
250 static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
252 return container_of(to_config_group(item), struct f_serial_opts,
256 static void serial_attr_release(struct config_item *item)
258 struct f_serial_opts *opts = to_f_serial_opts(item);
260 usb_put_function_instance(&opts->func_inst);
263 static struct configfs_item_operations serial_item_ops = {
264 .release = serial_attr_release,
267 #ifdef CONFIG_U_SERIAL_CONSOLE
269 static ssize_t f_serial_console_store(struct config_item *item,
270 const char *page, size_t count)
272 return gserial_set_console(to_f_serial_opts(item)->port_num,
276 static ssize_t f_serial_console_show(struct config_item *item, char *page)
278 return gserial_get_console(to_f_serial_opts(item)->port_num, page);
281 CONFIGFS_ATTR(f_serial_, console);
283 #endif /* CONFIG_U_SERIAL_CONSOLE */
285 static ssize_t f_serial_port_num_show(struct config_item *item, char *page)
287 return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
290 CONFIGFS_ATTR_RO(f_serial_, port_num);
292 static struct configfs_attribute *acm_attrs[] = {
293 #ifdef CONFIG_U_SERIAL_CONSOLE
294 &f_serial_attr_console,
296 &f_serial_attr_port_num,
300 static const struct config_item_type serial_func_type = {
301 .ct_item_ops = &serial_item_ops,
302 .ct_attrs = acm_attrs,
303 .ct_owner = THIS_MODULE,
306 static void gser_free_inst(struct usb_function_instance *f)
308 struct f_serial_opts *opts;
310 opts = container_of(f, struct f_serial_opts, func_inst);
311 gserial_free_line(opts->port_num);
315 static struct usb_function_instance *gser_alloc_inst(void)
317 struct f_serial_opts *opts;
320 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
324 opts->func_inst.free_func_inst = gser_free_inst;
325 ret = gserial_alloc_line(&opts->port_num);
330 config_group_init_type_name(&opts->func_inst.group, "",
333 return &opts->func_inst;
336 static void gser_free(struct usb_function *f)
338 struct f_gser *serial;
340 serial = func_to_gser(f);
344 static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
346 struct f_gser *gser = func_to_gser(f);
348 /* Ensure port is disconnected before unbinding */
349 gserial_disconnect(&gser->port);
350 usb_free_all_descriptors(f);
353 static void gser_resume(struct usb_function *f)
355 struct f_gser *gser = func_to_gser(f);
357 gserial_resume(&gser->port);
360 static void gser_suspend(struct usb_function *f)
362 struct f_gser *gser = func_to_gser(f);
364 gserial_suspend(&gser->port);
367 static struct usb_function *gser_alloc(struct usb_function_instance *fi)
370 struct f_serial_opts *opts;
372 /* allocate and initialize one new instance */
373 gser = kzalloc(sizeof(*gser), GFP_KERNEL);
375 return ERR_PTR(-ENOMEM);
377 opts = container_of(fi, struct f_serial_opts, func_inst);
379 gser->port_num = opts->port_num;
381 gser->port.func.name = "gser";
382 gser->port.func.strings = gser_strings;
383 gser->port.func.bind = gser_bind;
384 gser->port.func.unbind = gser_unbind;
385 gser->port.func.set_alt = gser_set_alt;
386 gser->port.func.disable = gser_disable;
387 gser->port.func.free_func = gser_free;
388 gser->port.func.resume = gser_resume;
389 gser->port.func.suspend = gser_suspend;
391 return &gser->port.func;
394 DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
395 MODULE_DESCRIPTION("generic USB serial function driver");
396 MODULE_LICENSE("GPL");
397 MODULE_AUTHOR("Al Borchers");
398 MODULE_AUTHOR("David Brownell");