2 * serial.c -- USB gadget serial driver
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
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.
13 #include <linux/kernel.h>
14 #include <linux/utsname.h>
15 #include <linux/device.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
20 #include "gadget_chips.h"
25 #define GS_VERSION_STR "v2.4"
26 #define GS_VERSION_NUM 0x2400
28 #define GS_LONG_NAME "Gadget Serial"
29 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
31 /*-------------------------------------------------------------------------*/
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
40 #include "composite.c"
41 #include "usbstring.c"
43 #include "epautoconf.c"
49 /*-------------------------------------------------------------------------*/
51 /* Thanks to NetChip Technologies for donating this product ID.
53 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
54 * Instead: allocate your own, using normal USB-IF procedures.
56 #define GS_VENDOR_ID 0x0525 /* NetChip */
57 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
58 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
60 /* string IDs are assigned dynamically */
62 #define STRING_MANUFACTURER_IDX 0
63 #define STRING_PRODUCT_IDX 1
64 #define STRING_DESCRIPTION_IDX 2
66 static char manufacturer[50];
68 static struct usb_string strings_dev[] = {
69 [STRING_MANUFACTURER_IDX].s = manufacturer,
70 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
71 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
75 static struct usb_gadget_strings stringtab_dev = {
76 .language = 0x0409, /* en-us */
77 .strings = strings_dev,
80 static struct usb_gadget_strings *dev_strings[] = {
85 static struct usb_device_descriptor device_desc = {
86 .bLength = USB_DT_DEVICE_SIZE,
87 .bDescriptorType = USB_DT_DEVICE,
88 .bcdUSB = __constant_cpu_to_le16(0x0200),
89 /* .bDeviceClass = f(use_acm) */
92 /* .bMaxPacketSize0 = f(hardware) */
93 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
94 /* .idProduct = f(use_acm) */
95 /* .bcdDevice = f(hardware) */
96 /* .iManufacturer = DYNAMIC */
97 /* .iProduct = DYNAMIC */
98 .bNumConfigurations = 1,
101 static struct usb_otg_descriptor otg_descriptor = {
102 .bLength = sizeof otg_descriptor,
103 .bDescriptorType = USB_DT_OTG,
105 /* REVISIT SRP-only hardware is possible, although
106 * it would not be called "OTG" ...
108 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
111 static const struct usb_descriptor_header *otg_desc[] = {
112 (struct usb_descriptor_header *) &otg_descriptor,
116 /*-------------------------------------------------------------------------*/
119 MODULE_DESCRIPTION(GS_VERSION_NAME);
120 MODULE_AUTHOR("Al Borchers");
121 MODULE_AUTHOR("David Brownell");
122 MODULE_LICENSE("GPL");
124 static int use_acm = true;
125 module_param(use_acm, bool, 0);
126 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
128 static unsigned n_ports = 1;
129 module_param(n_ports, uint, 0);
130 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
132 /*-------------------------------------------------------------------------*/
134 static int __init serial_bind_config(struct usb_configuration *c)
139 for (i = 0; i < n_ports && status == 0; i++) {
141 status = acm_bind_config(c, i);
143 status = gser_bind_config(c, i);
148 static struct usb_configuration serial_config_driver = {
149 /* .label = f(use_acm) */
150 .bind = serial_bind_config,
151 /* .bConfigurationValue = f(use_acm) */
152 /* .iConfiguration = DYNAMIC */
153 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
154 .bMaxPower = 1, /* 2 mA, minimal */
157 static int __init gs_bind(struct usb_composite_dev *cdev)
160 struct usb_gadget *gadget = cdev->gadget;
163 status = gserial_setup(cdev->gadget, n_ports);
167 /* Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
171 /* device description: manufacturer, product */
172 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
173 init_utsname()->sysname, init_utsname()->release,
175 status = usb_string_id(cdev);
178 strings_dev[STRING_MANUFACTURER_IDX].id = status;
180 device_desc.iManufacturer = status;
182 status = usb_string_id(cdev);
185 strings_dev[STRING_PRODUCT_IDX].id = status;
187 device_desc.iProduct = status;
189 /* config description */
190 status = usb_string_id(cdev);
193 strings_dev[STRING_DESCRIPTION_IDX].id = status;
195 serial_config_driver.iConfiguration = status;
197 /* set up other descriptors */
198 gcnum = usb_gadget_controller_number(gadget);
200 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
202 /* this is so simple (for now, no altsettings) that it
203 * SHOULD NOT have problems with bulk-capable hardware.
204 * so warn about unrcognized controllers -- don't panic.
206 * things like configuration and altsetting numbering
207 * can need hardware-specific attention though.
209 pr_warning("gs_bind: controller '%s' not recognized\n",
211 device_desc.bcdDevice =
212 __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
215 if (gadget_is_otg(cdev->gadget)) {
216 serial_config_driver.descriptors = otg_desc;
217 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
220 /* register our configuration */
221 status = usb_add_config(cdev, &serial_config_driver);
225 INFO(cdev, "%s\n", GS_VERSION_NAME);
234 static struct usb_composite_driver gserial_driver = {
237 .strings = dev_strings,
241 static int __init init(void)
243 /* We *could* export two configs; that'd be much cleaner...
244 * but neither of these product IDs was defined that way.
247 serial_config_driver.label = "CDC ACM config";
248 serial_config_driver.bConfigurationValue = 2;
249 device_desc.bDeviceClass = USB_CLASS_COMM;
250 device_desc.idProduct =
251 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
253 serial_config_driver.label = "Generic Serial config";
254 serial_config_driver.bConfigurationValue = 1;
255 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
256 device_desc.idProduct =
257 __constant_cpu_to_le16(GS_PRODUCT_ID);
259 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
261 return usb_composite_register(&gserial_driver);
265 static void __exit cleanup(void)
267 usb_composite_unregister(&gserial_driver);
270 module_exit(cleanup);