]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/gadget/serial.c
usb gadget: cdc obex glue
[net-next-2.6.git] / drivers / usb / gadget / serial.c
CommitLineData
1da177e4 1/*
a7707adf 2 * serial.c -- USB gadget serial driver
1da177e4 3 *
a7707adf
DB
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
7bb5ea54 6 * Copyright (C) 2008 by Nokia Corporation
1da177e4
LT
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.
1da177e4
LT
11 */
12
1da177e4 13#include <linux/kernel.h>
1da177e4 14#include <linux/utsname.h>
1da177e4
LT
15#include <linux/device.h>
16#include <linux/tty.h>
17#include <linux/tty_flip.h>
1da177e4 18
a7707adf 19#include "u_serial.h"
1da177e4
LT
20#include "gadget_chips.h"
21
22
1da177e4
LT
23/* Defines */
24
7bb5ea54
DB
25#define GS_VERSION_STR "v2.4"
26#define GS_VERSION_NUM 0x2400
1da177e4
LT
27
28#define GS_LONG_NAME "Gadget Serial"
a7707adf
DB
29#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30
7bb5ea54 31/*-------------------------------------------------------------------------*/
1da177e4 32
4e9ba518
DB
33/*
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.
39 */
40#include "composite.c"
41#include "usbstring.c"
42#include "config.c"
43#include "epautoconf.c"
44
45#include "f_acm.c"
3086775a 46#include "f_obex.c"
4e9ba518
DB
47#include "f_serial.c"
48#include "u_serial.c"
49
50/*-------------------------------------------------------------------------*/
51
1da177e4 52/* Thanks to NetChip Technologies for donating this product ID.
7bb5ea54
DB
53*
54* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
55* Instead: allocate your own, using normal USB-IF procedures.
56*/
1da177e4
LT
57#define GS_VENDOR_ID 0x0525 /* NetChip */
58#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
59#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
3086775a 60#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
1da177e4 61
7bb5ea54 62/* string IDs are assigned dynamically */
1da177e4 63
7bb5ea54
DB
64#define STRING_MANUFACTURER_IDX 0
65#define STRING_PRODUCT_IDX 1
66#define STRING_DESCRIPTION_IDX 2
a7707adf 67
1da177e4 68static char manufacturer[50];
7bb5ea54
DB
69
70static struct usb_string strings_dev[] = {
71 [STRING_MANUFACTURER_IDX].s = manufacturer,
72 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
73 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
1da177e4
LT
74 { } /* end of list */
75};
76
7bb5ea54
DB
77static struct usb_gadget_strings stringtab_dev = {
78 .language = 0x0409, /* en-us */
79 .strings = strings_dev,
1da177e4
LT
80};
81
7bb5ea54
DB
82static struct usb_gadget_strings *dev_strings[] = {
83 &stringtab_dev,
84 NULL,
85};
86
87static struct usb_device_descriptor device_desc = {
1da177e4
LT
88 .bLength = USB_DT_DEVICE_SIZE,
89 .bDescriptorType = USB_DT_DEVICE,
90 .bcdUSB = __constant_cpu_to_le16(0x0200),
7bb5ea54 91 /* .bDeviceClass = f(use_acm) */
1da177e4
LT
92 .bDeviceSubClass = 0,
93 .bDeviceProtocol = 0,
7bb5ea54 94 /* .bMaxPacketSize0 = f(hardware) */
1da177e4 95 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
7bb5ea54
DB
96 /* .idProduct = f(use_acm) */
97 /* .bcdDevice = f(hardware) */
98 /* .iManufacturer = DYNAMIC */
99 /* .iProduct = DYNAMIC */
100 .bNumConfigurations = 1,
1da177e4
LT
101};
102
7bb5ea54
DB
103static struct usb_otg_descriptor otg_descriptor = {
104 .bLength = sizeof otg_descriptor,
1da177e4 105 .bDescriptorType = USB_DT_OTG,
1da177e4 106
7bb5ea54
DB
107 /* REVISIT SRP-only hardware is possible, although
108 * it would not be called "OTG" ...
109 */
110 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
a7707adf
DB
111};
112
7bb5ea54
DB
113static const struct usb_descriptor_header *otg_desc[] = {
114 (struct usb_descriptor_header *) &otg_descriptor,
a7707adf
DB
115 NULL,
116};
1da177e4 117
a7707adf 118/*-------------------------------------------------------------------------*/
1da177e4 119
a7707adf
DB
120/* Module */
121MODULE_DESCRIPTION(GS_VERSION_NAME);
122MODULE_AUTHOR("Al Borchers");
123MODULE_AUTHOR("David Brownell");
124MODULE_LICENSE("GPL");
1da177e4 125
7bb5ea54
DB
126static int use_acm = true;
127module_param(use_acm, bool, 0);
128MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
1da177e4 129
3086775a
FB
130static int use_obex = false;
131module_param(use_obex, bool, 0);
132MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
133
7bb5ea54
DB
134static unsigned n_ports = 1;
135module_param(n_ports, uint, 0);
136MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
9079e91b 137
7bb5ea54 138/*-------------------------------------------------------------------------*/
1da177e4 139
7bb5ea54 140static int __init serial_bind_config(struct usb_configuration *c)
9079e91b 141{
7bb5ea54
DB
142 unsigned i;
143 int status = 0;
9079e91b 144
7bb5ea54
DB
145 for (i = 0; i < n_ports && status == 0; i++) {
146 if (use_acm)
147 status = acm_bind_config(c, i);
3086775a
FB
148 else if (use_obex)
149 status = obex_bind_config(c, i);
7bb5ea54
DB
150 else
151 status = gser_bind_config(c, i);
9079e91b 152 }
7bb5ea54 153 return status;
9079e91b
DB
154}
155
7bb5ea54
DB
156static struct usb_configuration serial_config_driver = {
157 /* .label = f(use_acm) */
158 .bind = serial_bind_config,
159 /* .bConfigurationValue = f(use_acm) */
160 /* .iConfiguration = DYNAMIC */
161 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
162 .bMaxPower = 1, /* 2 mA, minimal */
163};
164
165static int __init gs_bind(struct usb_composite_dev *cdev)
1da177e4 166{
7bb5ea54
DB
167 int gcnum;
168 struct usb_gadget *gadget = cdev->gadget;
169 int status;
1da177e4 170
7bb5ea54
DB
171 status = gserial_setup(cdev->gadget, n_ports);
172 if (status < 0)
173 return status;
a7707adf 174
7bb5ea54
DB
175 /* Allocate string descriptor numbers ... note that string
176 * contents can be overridden by the composite_dev glue.
91e79c91 177 */
1da177e4 178
7bb5ea54
DB
179 /* device description: manufacturer, product */
180 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
96b644bd 181 init_utsname()->sysname, init_utsname()->release,
1da177e4 182 gadget->name);
7bb5ea54
DB
183 status = usb_string_id(cdev);
184 if (status < 0)
185 goto fail;
186 strings_dev[STRING_MANUFACTURER_IDX].id = status;
1da177e4 187
7bb5ea54 188 device_desc.iManufacturer = status;
1da177e4 189
7bb5ea54
DB
190 status = usb_string_id(cdev);
191 if (status < 0)
192 goto fail;
193 strings_dev[STRING_PRODUCT_IDX].id = status;
1da177e4 194
7bb5ea54 195 device_desc.iProduct = status;
1da177e4 196
7bb5ea54
DB
197 /* config description */
198 status = usb_string_id(cdev);
199 if (status < 0)
200 goto fail;
201 strings_dev[STRING_DESCRIPTION_IDX].id = status;
1da177e4 202
7bb5ea54 203 serial_config_driver.iConfiguration = status;
1da177e4 204
7bb5ea54
DB
205 /* set up other descriptors */
206 gcnum = usb_gadget_controller_number(gadget);
207 if (gcnum >= 0)
208 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
209 else {
210 /* this is so simple (for now, no altsettings) that it
211 * SHOULD NOT have problems with bulk-capable hardware.
212 * so warn about unrcognized controllers -- don't panic.
213 *
214 * things like configuration and altsetting numbering
215 * can need hardware-specific attention though.
f371e750 216 */
7bb5ea54
DB
217 pr_warning("gs_bind: controller '%s' not recognized\n",
218 gadget->name);
219 device_desc.bcdDevice =
220 __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
f371e750 221 }
f371e750 222
7bb5ea54
DB
223 if (gadget_is_otg(cdev->gadget)) {
224 serial_config_driver.descriptors = otg_desc;
225 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1da177e4 226 }
9079e91b 227
7bb5ea54
DB
228 /* register our configuration */
229 status = usb_add_config(cdev, &serial_config_driver);
230 if (status < 0)
231 goto fail;
1da177e4 232
7bb5ea54 233 INFO(cdev, "%s\n", GS_VERSION_NAME);
1da177e4 234
7bb5ea54 235 return 0;
1da177e4 236
7bb5ea54
DB
237fail:
238 gserial_cleanup();
239 return status;
1da177e4
LT
240}
241
7bb5ea54
DB
242static struct usb_composite_driver gserial_driver = {
243 .name = "g_serial",
244 .dev = &device_desc,
245 .strings = dev_strings,
246 .bind = gs_bind,
9079e91b
DB
247};
248
7bb5ea54 249static int __init init(void)
1da177e4 250{
7bb5ea54
DB
251 /* We *could* export two configs; that'd be much cleaner...
252 * but neither of these product IDs was defined that way.
f371e750 253 */
1da177e4 254 if (use_acm) {
7bb5ea54
DB
255 serial_config_driver.label = "CDC ACM config";
256 serial_config_driver.bConfigurationValue = 2;
257 device_desc.bDeviceClass = USB_CLASS_COMM;
258 device_desc.idProduct =
259 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
3086775a
FB
260 } else if (use_obex) {
261 serial_config_driver.label = "CDC OBEX config";
262 serial_config_driver.bConfigurationValue = 3;
263 device_desc.bDeviceClass = USB_CLASS_COMM;
264 device_desc.idProduct =
265 __constant_cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
1da177e4 266 } else {
7bb5ea54
DB
267 serial_config_driver.label = "Generic Serial config";
268 serial_config_driver.bConfigurationValue = 1;
269 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
270 device_desc.idProduct =
271 __constant_cpu_to_le16(GS_PRODUCT_ID);
1da177e4 272 }
7bb5ea54 273 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
9079e91b 274
7bb5ea54 275 return usb_composite_register(&gserial_driver);
9079e91b 276}
7bb5ea54 277module_init(init);
9079e91b 278
7bb5ea54 279static void __exit cleanup(void)
9079e91b 280{
7bb5ea54
DB
281 usb_composite_unregister(&gserial_driver);
282 gserial_cleanup();
9079e91b 283}
7bb5ea54 284module_exit(cleanup);