usb gadget serial: use composite gadget framework
[linux-2.6/mini2440.git] / drivers / usb / gadget / serial.c
blobb3699afff002f4115b4c3ffaf1b975638aa88f1f
1 /*
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>
19 #include "u_serial.h"
20 #include "gadget_chips.h"
23 /* Defines */
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 /*-------------------------------------------------------------------------*/
33 /* Thanks to NetChip Technologies for donating this product ID.
35 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
36 * Instead: allocate your own, using normal USB-IF procedures.
38 #define GS_VENDOR_ID 0x0525 /* NetChip */
39 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
40 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
42 /* string IDs are assigned dynamically */
44 #define STRING_MANUFACTURER_IDX 0
45 #define STRING_PRODUCT_IDX 1
46 #define STRING_DESCRIPTION_IDX 2
48 static char manufacturer[50];
50 static struct usb_string strings_dev[] = {
51 [STRING_MANUFACTURER_IDX].s = manufacturer,
52 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
53 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
54 { } /* end of list */
57 static struct usb_gadget_strings stringtab_dev = {
58 .language = 0x0409, /* en-us */
59 .strings = strings_dev,
62 static struct usb_gadget_strings *dev_strings[] = {
63 &stringtab_dev,
64 NULL,
67 static struct usb_device_descriptor device_desc = {
68 .bLength = USB_DT_DEVICE_SIZE,
69 .bDescriptorType = USB_DT_DEVICE,
70 .bcdUSB = __constant_cpu_to_le16(0x0200),
71 /* .bDeviceClass = f(use_acm) */
72 .bDeviceSubClass = 0,
73 .bDeviceProtocol = 0,
74 /* .bMaxPacketSize0 = f(hardware) */
75 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
76 /* .idProduct = f(use_acm) */
77 /* .bcdDevice = f(hardware) */
78 /* .iManufacturer = DYNAMIC */
79 /* .iProduct = DYNAMIC */
80 .bNumConfigurations = 1,
83 static struct usb_otg_descriptor otg_descriptor = {
84 .bLength = sizeof otg_descriptor,
85 .bDescriptorType = USB_DT_OTG,
87 /* REVISIT SRP-only hardware is possible, although
88 * it would not be called "OTG" ...
90 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
93 static const struct usb_descriptor_header *otg_desc[] = {
94 (struct usb_descriptor_header *) &otg_descriptor,
95 NULL,
98 /*-------------------------------------------------------------------------*/
100 /* Module */
101 MODULE_DESCRIPTION(GS_VERSION_NAME);
102 MODULE_AUTHOR("Al Borchers");
103 MODULE_AUTHOR("David Brownell");
104 MODULE_LICENSE("GPL");
106 static int use_acm = true;
107 module_param(use_acm, bool, 0);
108 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
110 static unsigned n_ports = 1;
111 module_param(n_ports, uint, 0);
112 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
114 /*-------------------------------------------------------------------------*/
116 static int __init serial_bind_config(struct usb_configuration *c)
118 unsigned i;
119 int status = 0;
121 for (i = 0; i < n_ports && status == 0; i++) {
122 if (use_acm)
123 status = acm_bind_config(c, i);
124 else
125 status = gser_bind_config(c, i);
127 return status;
130 static struct usb_configuration serial_config_driver = {
131 /* .label = f(use_acm) */
132 .bind = serial_bind_config,
133 /* .bConfigurationValue = f(use_acm) */
134 /* .iConfiguration = DYNAMIC */
135 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
136 .bMaxPower = 1, /* 2 mA, minimal */
139 static int __init gs_bind(struct usb_composite_dev *cdev)
141 int gcnum;
142 struct usb_gadget *gadget = cdev->gadget;
143 int status;
145 status = gserial_setup(cdev->gadget, n_ports);
146 if (status < 0)
147 return status;
149 /* Allocate string descriptor numbers ... note that string
150 * contents can be overridden by the composite_dev glue.
153 /* device description: manufacturer, product */
154 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
155 init_utsname()->sysname, init_utsname()->release,
156 gadget->name);
157 status = usb_string_id(cdev);
158 if (status < 0)
159 goto fail;
160 strings_dev[STRING_MANUFACTURER_IDX].id = status;
162 device_desc.iManufacturer = status;
164 status = usb_string_id(cdev);
165 if (status < 0)
166 goto fail;
167 strings_dev[STRING_PRODUCT_IDX].id = status;
169 device_desc.iProduct = status;
171 /* config description */
172 status = usb_string_id(cdev);
173 if (status < 0)
174 goto fail;
175 strings_dev[STRING_DESCRIPTION_IDX].id = status;
177 serial_config_driver.iConfiguration = status;
179 /* set up other descriptors */
180 gcnum = usb_gadget_controller_number(gadget);
181 if (gcnum >= 0)
182 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
183 else {
184 /* this is so simple (for now, no altsettings) that it
185 * SHOULD NOT have problems with bulk-capable hardware.
186 * so warn about unrcognized controllers -- don't panic.
188 * things like configuration and altsetting numbering
189 * can need hardware-specific attention though.
191 pr_warning("gs_bind: controller '%s' not recognized\n",
192 gadget->name);
193 device_desc.bcdDevice =
194 __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
197 if (gadget_is_otg(cdev->gadget)) {
198 serial_config_driver.descriptors = otg_desc;
199 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
202 /* register our configuration */
203 status = usb_add_config(cdev, &serial_config_driver);
204 if (status < 0)
205 goto fail;
207 INFO(cdev, "%s\n", GS_VERSION_NAME);
209 return 0;
211 fail:
212 gserial_cleanup();
213 return status;
216 static struct usb_composite_driver gserial_driver = {
217 .name = "g_serial",
218 .dev = &device_desc,
219 .strings = dev_strings,
220 .bind = gs_bind,
223 static int __init init(void)
225 /* We *could* export two configs; that'd be much cleaner...
226 * but neither of these product IDs was defined that way.
228 if (use_acm) {
229 serial_config_driver.label = "CDC ACM config";
230 serial_config_driver.bConfigurationValue = 2;
231 device_desc.bDeviceClass = USB_CLASS_COMM;
232 device_desc.idProduct =
233 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
234 } else {
235 serial_config_driver.label = "Generic Serial config";
236 serial_config_driver.bConfigurationValue = 1;
237 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
238 device_desc.idProduct =
239 __constant_cpu_to_le16(GS_PRODUCT_ID);
241 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
243 return usb_composite_register(&gserial_driver);
245 module_init(init);
247 static void __exit cleanup(void)
249 usb_composite_unregister(&gserial_driver);
250 gserial_cleanup();
252 module_exit(cleanup);