usb: gadget: libcomposite: move composite.c into libcomposite
[deliverable/linux.git] / drivers / usb / gadget / serial.c
1 /*
2 * serial.c -- USB gadget serial 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
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17
18 #include "u_serial.h"
19 #include "gadget_chips.h"
20
21
22 /* Defines */
23
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
26
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29
30 /*-------------------------------------------------------------------------*/
31
32 /*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39 #include "f_acm.c"
40 #include "f_obex.c"
41 #include "f_serial.c"
42 #include "u_serial.c"
43
44 /*-------------------------------------------------------------------------*/
45 USB_GADGET_COMPOSITE_OPTIONS();
46
47 /* Thanks to NetChip Technologies for donating this product ID.
48 *
49 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
51 */
52 #define GS_VENDOR_ID 0x0525 /* NetChip */
53 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
54 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
55 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
56
57 /* string IDs are assigned dynamically */
58
59 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
60
61 static struct usb_string strings_dev[] = {
62 [USB_GADGET_MANUFACTURER_IDX].s = "",
63 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
65 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
66 { } /* end of list */
67 };
68
69 static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
72 };
73
74 static struct usb_gadget_strings *dev_strings[] = {
75 &stringtab_dev,
76 NULL,
77 };
78
79 static struct usb_device_descriptor device_desc = {
80 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
82 .bcdUSB = cpu_to_le16(0x0200),
83 /* .bDeviceClass = f(use_acm) */
84 .bDeviceSubClass = 0,
85 .bDeviceProtocol = 0,
86 /* .bMaxPacketSize0 = f(hardware) */
87 .idVendor = cpu_to_le16(GS_VENDOR_ID),
88 /* .idProduct = f(use_acm) */
89 /* .bcdDevice = f(hardware) */
90 /* .iManufacturer = DYNAMIC */
91 /* .iProduct = DYNAMIC */
92 .bNumConfigurations = 1,
93 };
94
95 static struct usb_otg_descriptor otg_descriptor = {
96 .bLength = sizeof otg_descriptor,
97 .bDescriptorType = USB_DT_OTG,
98
99 /* REVISIT SRP-only hardware is possible, although
100 * it would not be called "OTG" ...
101 */
102 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
103 };
104
105 static const struct usb_descriptor_header *otg_desc[] = {
106 (struct usb_descriptor_header *) &otg_descriptor,
107 NULL,
108 };
109
110 /*-------------------------------------------------------------------------*/
111
112 /* Module */
113 MODULE_DESCRIPTION(GS_VERSION_NAME);
114 MODULE_AUTHOR("Al Borchers");
115 MODULE_AUTHOR("David Brownell");
116 MODULE_LICENSE("GPL");
117
118 static bool use_acm = true;
119 module_param(use_acm, bool, 0);
120 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
121
122 static bool use_obex = false;
123 module_param(use_obex, bool, 0);
124 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
125
126 static unsigned n_ports = 1;
127 module_param(n_ports, uint, 0);
128 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
129
130 /*-------------------------------------------------------------------------*/
131
132 static int __init serial_bind_config(struct usb_configuration *c)
133 {
134 unsigned i;
135 int status = 0;
136
137 for (i = 0; i < n_ports && status == 0; i++) {
138 if (use_acm)
139 status = acm_bind_config(c, i);
140 else if (use_obex)
141 status = obex_bind_config(c, i);
142 else
143 status = gser_bind_config(c, i);
144 }
145 return status;
146 }
147
148 static struct usb_configuration serial_config_driver = {
149 /* .label = f(use_acm) */
150 /* .bConfigurationValue = f(use_acm) */
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
153 };
154
155 static int __init gs_bind(struct usb_composite_dev *cdev)
156 {
157 int gcnum;
158 struct usb_gadget *gadget = cdev->gadget;
159 int status;
160
161 status = gserial_setup(cdev->gadget, n_ports);
162 if (status < 0)
163 return status;
164
165 /* Allocate string descriptor numbers ... note that string
166 * contents can be overridden by the composite_dev glue.
167 */
168
169 status = usb_string_ids_tab(cdev, strings_dev);
170 if (status < 0)
171 goto fail;
172 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
173 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
174 status = strings_dev[STRING_DESCRIPTION_IDX].id;
175 serial_config_driver.iConfiguration = status;
176
177 /* set up other descriptors */
178 gcnum = usb_gadget_controller_number(gadget);
179 if (gcnum >= 0)
180 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
181 else {
182 /* this is so simple (for now, no altsettings) that it
183 * SHOULD NOT have problems with bulk-capable hardware.
184 * so warn about unrcognized controllers -- don't panic.
185 *
186 * things like configuration and altsetting numbering
187 * can need hardware-specific attention though.
188 */
189 pr_warning("gs_bind: controller '%s' not recognized\n",
190 gadget->name);
191 device_desc.bcdDevice =
192 cpu_to_le16(GS_VERSION_NUM | 0x0099);
193 }
194
195 if (gadget_is_otg(cdev->gadget)) {
196 serial_config_driver.descriptors = otg_desc;
197 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
198 }
199
200 /* register our configuration */
201 status = usb_add_config(cdev, &serial_config_driver,
202 serial_bind_config);
203 if (status < 0)
204 goto fail;
205
206 usb_composite_overwrite_options(cdev, &coverwrite);
207 INFO(cdev, "%s\n", GS_VERSION_NAME);
208
209 return 0;
210
211 fail:
212 gserial_cleanup();
213 return status;
214 }
215
216 static __refdata struct usb_composite_driver gserial_driver = {
217 .name = "g_serial",
218 .dev = &device_desc,
219 .strings = dev_strings,
220 .max_speed = USB_SPEED_SUPER,
221 .bind = gs_bind,
222 };
223
224 static int __init init(void)
225 {
226 /* We *could* export two configs; that'd be much cleaner...
227 * but neither of these product IDs was defined that way.
228 */
229 if (use_acm) {
230 serial_config_driver.label = "CDC ACM config";
231 serial_config_driver.bConfigurationValue = 2;
232 device_desc.bDeviceClass = USB_CLASS_COMM;
233 device_desc.idProduct =
234 cpu_to_le16(GS_CDC_PRODUCT_ID);
235 } else if (use_obex) {
236 serial_config_driver.label = "CDC OBEX config";
237 serial_config_driver.bConfigurationValue = 3;
238 device_desc.bDeviceClass = USB_CLASS_COMM;
239 device_desc.idProduct =
240 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
241 } else {
242 serial_config_driver.label = "Generic Serial config";
243 serial_config_driver.bConfigurationValue = 1;
244 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
245 device_desc.idProduct =
246 cpu_to_le16(GS_PRODUCT_ID);
247 }
248 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
249
250 return usb_composite_probe(&gserial_driver);
251 }
252 module_init(init);
253
254 static void __exit cleanup(void)
255 {
256 usb_composite_unregister(&gserial_driver);
257 gserial_cleanup();
258 }
259 module_exit(cleanup);
This page took 0.036127 seconds and 5 git commands to generate.