usb: gadget: f_serial: add configfs support
[deliverable/linux.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
LT
14#include <linux/device.h>
15#include <linux/tty.h>
16#include <linux/tty_flip.h>
1da177e4 17
a7707adf 18#include "u_serial.h"
1da177e4
LT
19#include "gadget_chips.h"
20
21
1da177e4
LT
22/* Defines */
23
7bb5ea54
DB
24#define GS_VERSION_STR "v2.4"
25#define GS_VERSION_NUM 0x2400
1da177e4
LT
26
27#define GS_LONG_NAME "Gadget Serial"
a7707adf
DB
28#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29
7bb5ea54 30/*-------------------------------------------------------------------------*/
1da177e4 31
4e9ba518
DB
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 */
3086775a 39#include "f_obex.c"
4e9ba518
DB
40
41/*-------------------------------------------------------------------------*/
7d16e8d3 42USB_GADGET_COMPOSITE_OPTIONS();
4e9ba518 43
1da177e4 44/* Thanks to NetChip Technologies for donating this product ID.
7bb5ea54
DB
45*
46* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
47* Instead: allocate your own, using normal USB-IF procedures.
48*/
1da177e4
LT
49#define GS_VENDOR_ID 0x0525 /* NetChip */
50#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
51#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
3086775a 52#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
1da177e4 53
7bb5ea54 54/* string IDs are assigned dynamically */
1da177e4 55
276e2e4f 56#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
a7707adf 57
7bb5ea54 58static struct usb_string strings_dev[] = {
cc2683c3 59 [USB_GADGET_MANUFACTURER_IDX].s = "",
276e2e4f
SAS
60 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
61 [USB_GADGET_SERIAL_IDX].s = "",
7bb5ea54 62 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
1da177e4
LT
63 { } /* end of list */
64};
65
7bb5ea54
DB
66static struct usb_gadget_strings stringtab_dev = {
67 .language = 0x0409, /* en-us */
68 .strings = strings_dev,
1da177e4
LT
69};
70
7bb5ea54
DB
71static struct usb_gadget_strings *dev_strings[] = {
72 &stringtab_dev,
73 NULL,
74};
75
76static struct usb_device_descriptor device_desc = {
1da177e4
LT
77 .bLength = USB_DT_DEVICE_SIZE,
78 .bDescriptorType = USB_DT_DEVICE,
551509d2 79 .bcdUSB = cpu_to_le16(0x0200),
7bb5ea54 80 /* .bDeviceClass = f(use_acm) */
1da177e4
LT
81 .bDeviceSubClass = 0,
82 .bDeviceProtocol = 0,
7bb5ea54 83 /* .bMaxPacketSize0 = f(hardware) */
551509d2 84 .idVendor = cpu_to_le16(GS_VENDOR_ID),
7bb5ea54 85 /* .idProduct = f(use_acm) */
5c4d46eb 86 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
7bb5ea54
DB
87 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations = 1,
1da177e4
LT
90};
91
7bb5ea54
DB
92static struct usb_otg_descriptor otg_descriptor = {
93 .bLength = sizeof otg_descriptor,
1da177e4 94 .bDescriptorType = USB_DT_OTG,
1da177e4 95
7bb5ea54
DB
96 /* REVISIT SRP-only hardware is possible, although
97 * it would not be called "OTG" ...
98 */
99 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
a7707adf
DB
100};
101
7bb5ea54
DB
102static const struct usb_descriptor_header *otg_desc[] = {
103 (struct usb_descriptor_header *) &otg_descriptor,
a7707adf
DB
104 NULL,
105};
1da177e4 106
a7707adf 107/*-------------------------------------------------------------------------*/
1da177e4 108
a7707adf
DB
109/* Module */
110MODULE_DESCRIPTION(GS_VERSION_NAME);
111MODULE_AUTHOR("Al Borchers");
112MODULE_AUTHOR("David Brownell");
113MODULE_LICENSE("GPL");
1da177e4 114
90ab5ee9 115static bool use_acm = true;
7bb5ea54
DB
116module_param(use_acm, bool, 0);
117MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
1da177e4 118
90ab5ee9 119static bool use_obex = false;
3086775a
FB
120module_param(use_obex, bool, 0);
121MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
122
7bb5ea54
DB
123static unsigned n_ports = 1;
124module_param(n_ports, uint, 0);
125MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
9079e91b 126
7bb5ea54 127/*-------------------------------------------------------------------------*/
19b10a88 128static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
1da177e4 129
48177cd8
SAS
130static int __init serial_bind_obex_config(struct usb_configuration *c)
131{
132 unsigned i;
133 int status = 0;
134
135 for (i = 0; i < n_ports && status == 0; i++)
19b10a88 136 status = obex_bind_config(c, tty_lines[i]);
48177cd8
SAS
137 return status;
138}
139
7bb5ea54
DB
140static struct usb_configuration serial_config_driver = {
141 /* .label = f(use_acm) */
7bb5ea54
DB
142 /* .bConfigurationValue = f(use_acm) */
143 /* .iConfiguration = DYNAMIC */
144 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
7bb5ea54
DB
145};
146
ff47f594
SAS
147static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
148static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
149
150static int serial_register_ports(struct usb_composite_dev *cdev,
151 struct usb_configuration *c, const char *f_name)
19b10a88
SAS
152{
153 int i;
ff47f594
SAS
154 int ret;
155
156 ret = usb_add_config_only(cdev, c);
157 if (ret)
158 goto out;
159
160 for (i = 0; i < n_ports; i++) {
ff47f594
SAS
161
162 fi_serial[i] = usb_get_function_instance(f_name);
163 if (IS_ERR(fi_serial[i])) {
164 ret = PTR_ERR(fi_serial[i]);
165 goto fail;
166 }
ff47f594
SAS
167
168 f_serial[i] = usb_get_function(fi_serial[i]);
169 if (IS_ERR(f_serial[i])) {
170 ret = PTR_ERR(f_serial[i]);
171 goto err_get_func;
172 }
173
174 ret = usb_add_function(c, f_serial[i]);
175 if (ret)
176 goto err_add_func;
177 }
19b10a88 178
19b10a88 179 return 0;
ff47f594
SAS
180
181err_add_func:
182 usb_put_function(f_serial[i]);
183err_get_func:
184 usb_put_function_instance(fi_serial[i]);
185
186fail:
187 i--;
188 while (i >= 0) {
189 usb_remove_function(c, f_serial[i]);
190 usb_put_function(f_serial[i]);
191 usb_put_function_instance(fi_serial[i]);
192 i--;
193 }
194out:
195 return ret;
19b10a88
SAS
196}
197
e12995ec 198static int __init gs_bind(struct usb_composite_dev *cdev)
1da177e4 199{
7bb5ea54 200 int status;
c4ed4ac1 201 int cur_line = 0;
1da177e4 202
70cc3c02 203 if (use_obex) {
c4ed4ac1
SAS
204 for (cur_line = 0; cur_line < n_ports; cur_line++) {
205 status = gserial_alloc_line(&tty_lines[cur_line]);
206 if (status)
207 goto fail;
208 }
19b10a88 209 }
a7707adf 210
7bb5ea54
DB
211 /* Allocate string descriptor numbers ... note that string
212 * contents can be overridden by the composite_dev glue.
91e79c91 213 */
1da177e4 214
e1f15ccb 215 status = usb_string_ids_tab(cdev, strings_dev);
7bb5ea54
DB
216 if (status < 0)
217 goto fail;
276e2e4f
SAS
218 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
219 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
e1f15ccb 220 status = strings_dev[STRING_DESCRIPTION_IDX].id;
7bb5ea54 221 serial_config_driver.iConfiguration = status;
1da177e4 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 228 /* register our configuration */
ff47f594
SAS
229 if (use_acm) {
230 status = serial_register_ports(cdev, &serial_config_driver,
231 "acm");
232 usb_ep_autoconfig_reset(cdev->gadget);
233 } else if (use_obex)
48177cd8
SAS
234 status = usb_add_config(cdev, &serial_config_driver,
235 serial_bind_obex_config);
70cc3c02
AP
236 else {
237 status = serial_register_ports(cdev, &serial_config_driver,
238 "gser");
239 }
7bb5ea54
DB
240 if (status < 0)
241 goto fail;
1da177e4 242
7d16e8d3 243 usb_composite_overwrite_options(cdev, &coverwrite);
7bb5ea54 244 INFO(cdev, "%s\n", GS_VERSION_NAME);
1da177e4 245
7bb5ea54 246 return 0;
1da177e4 247
7bb5ea54 248fail:
19b10a88 249 cur_line--;
70cc3c02 250 while (cur_line >= 0 && use_obex)
19b10a88 251 gserial_free_line(tty_lines[cur_line--]);
7bb5ea54 252 return status;
1da177e4
LT
253}
254
ff47f594
SAS
255static int gs_unbind(struct usb_composite_dev *cdev)
256{
257 int i;
258
259 for (i = 0; i < n_ports; i++) {
260 usb_put_function(f_serial[i]);
261 usb_put_function_instance(fi_serial[i]);
70cc3c02 262 if (use_obex)
c4ed4ac1 263 gserial_free_line(tty_lines[i]);
ff47f594
SAS
264 }
265 return 0;
266}
267
c2ec75c2 268static __refdata struct usb_composite_driver gserial_driver = {
7bb5ea54
DB
269 .name = "g_serial",
270 .dev = &device_desc,
271 .strings = dev_strings,
6fecfb05 272 .max_speed = USB_SPEED_SUPER,
03e42bd5 273 .bind = gs_bind,
19b10a88 274 .unbind = gs_unbind,
9079e91b
DB
275};
276
7bb5ea54 277static int __init init(void)
1da177e4 278{
7bb5ea54
DB
279 /* We *could* export two configs; that'd be much cleaner...
280 * but neither of these product IDs was defined that way.
f371e750 281 */
1da177e4 282 if (use_acm) {
7bb5ea54
DB
283 serial_config_driver.label = "CDC ACM config";
284 serial_config_driver.bConfigurationValue = 2;
285 device_desc.bDeviceClass = USB_CLASS_COMM;
286 device_desc.idProduct =
551509d2 287 cpu_to_le16(GS_CDC_PRODUCT_ID);
3086775a
FB
288 } else if (use_obex) {
289 serial_config_driver.label = "CDC OBEX config";
290 serial_config_driver.bConfigurationValue = 3;
291 device_desc.bDeviceClass = USB_CLASS_COMM;
292 device_desc.idProduct =
551509d2 293 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
1da177e4 294 } else {
7bb5ea54
DB
295 serial_config_driver.label = "Generic Serial config";
296 serial_config_driver.bConfigurationValue = 1;
297 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
298 device_desc.idProduct =
551509d2 299 cpu_to_le16(GS_PRODUCT_ID);
1da177e4 300 }
7bb5ea54 301 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
9079e91b 302
03e42bd5 303 return usb_composite_probe(&gserial_driver);
9079e91b 304}
7bb5ea54 305module_init(init);
9079e91b 306
7bb5ea54 307static void __exit cleanup(void)
9079e91b 308{
7bb5ea54 309 usb_composite_unregister(&gserial_driver);
9079e91b 310}
7bb5ea54 311module_exit(cleanup);
This page took 0.765736 seconds and 5 git commands to generate.