Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / serial.c
blob8b7528f9b78eff02a7d20bca4122429d6294be6d
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/device.h>
15 #include <linux/module.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 /*-------------------------------------------------------------------------*/
32 USB_GADGET_COMPOSITE_OPTIONS();
34 /* Thanks to NetChip Technologies for donating this product ID.
36 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
37 * Instead: allocate your own, using normal USB-IF procedures.
39 #define GS_VENDOR_ID 0x0525 /* NetChip */
40 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
41 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
42 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
44 /* string IDs are assigned dynamically */
46 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
48 static struct usb_string strings_dev[] = {
49 [USB_GADGET_MANUFACTURER_IDX].s = "",
50 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
51 [USB_GADGET_SERIAL_IDX].s = "",
52 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
53 { } /* end of list */
56 static struct usb_gadget_strings stringtab_dev = {
57 .language = 0x0409, /* en-us */
58 .strings = strings_dev,
61 static struct usb_gadget_strings *dev_strings[] = {
62 &stringtab_dev,
63 NULL,
66 static struct usb_device_descriptor device_desc = {
67 .bLength = USB_DT_DEVICE_SIZE,
68 .bDescriptorType = USB_DT_DEVICE,
69 .bcdUSB = cpu_to_le16(0x0200),
70 /* .bDeviceClass = f(use_acm) */
71 .bDeviceSubClass = 0,
72 .bDeviceProtocol = 0,
73 /* .bMaxPacketSize0 = f(hardware) */
74 .idVendor = cpu_to_le16(GS_VENDOR_ID),
75 /* .idProduct = f(use_acm) */
76 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
77 /* .iManufacturer = DYNAMIC */
78 /* .iProduct = DYNAMIC */
79 .bNumConfigurations = 1,
82 static struct usb_otg_descriptor otg_descriptor = {
83 .bLength = sizeof otg_descriptor,
84 .bDescriptorType = USB_DT_OTG,
86 /* REVISIT SRP-only hardware is possible, although
87 * it would not be called "OTG" ...
89 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
92 static const struct usb_descriptor_header *otg_desc[] = {
93 (struct usb_descriptor_header *) &otg_descriptor,
94 NULL,
97 /*-------------------------------------------------------------------------*/
99 /* Module */
100 MODULE_DESCRIPTION(GS_VERSION_NAME);
101 MODULE_AUTHOR("Al Borchers");
102 MODULE_AUTHOR("David Brownell");
103 MODULE_LICENSE("GPL");
105 static bool use_acm = true;
106 module_param(use_acm, bool, 0);
107 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
109 static bool use_obex = false;
110 module_param(use_obex, bool, 0);
111 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
113 static unsigned n_ports = 1;
114 module_param(n_ports, uint, 0);
115 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
117 /*-------------------------------------------------------------------------*/
119 static struct usb_configuration serial_config_driver = {
120 /* .label = f(use_acm) */
121 /* .bConfigurationValue = f(use_acm) */
122 /* .iConfiguration = DYNAMIC */
123 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
126 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
127 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
129 static int serial_register_ports(struct usb_composite_dev *cdev,
130 struct usb_configuration *c, const char *f_name)
132 int i;
133 int ret;
135 ret = usb_add_config_only(cdev, c);
136 if (ret)
137 goto out;
139 for (i = 0; i < n_ports; i++) {
141 fi_serial[i] = usb_get_function_instance(f_name);
142 if (IS_ERR(fi_serial[i])) {
143 ret = PTR_ERR(fi_serial[i]);
144 goto fail;
147 f_serial[i] = usb_get_function(fi_serial[i]);
148 if (IS_ERR(f_serial[i])) {
149 ret = PTR_ERR(f_serial[i]);
150 goto err_get_func;
153 ret = usb_add_function(c, f_serial[i]);
154 if (ret)
155 goto err_add_func;
158 return 0;
160 err_add_func:
161 usb_put_function(f_serial[i]);
162 err_get_func:
163 usb_put_function_instance(fi_serial[i]);
165 fail:
166 i--;
167 while (i >= 0) {
168 usb_remove_function(c, f_serial[i]);
169 usb_put_function(f_serial[i]);
170 usb_put_function_instance(fi_serial[i]);
171 i--;
173 out:
174 return ret;
177 static int gs_bind(struct usb_composite_dev *cdev)
179 int status;
181 /* Allocate string descriptor numbers ... note that string
182 * contents can be overridden by the composite_dev glue.
185 status = usb_string_ids_tab(cdev, strings_dev);
186 if (status < 0)
187 goto fail;
188 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
189 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
190 status = strings_dev[STRING_DESCRIPTION_IDX].id;
191 serial_config_driver.iConfiguration = status;
193 if (gadget_is_otg(cdev->gadget)) {
194 serial_config_driver.descriptors = otg_desc;
195 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
198 /* register our configuration */
199 if (use_acm) {
200 status = serial_register_ports(cdev, &serial_config_driver,
201 "acm");
202 usb_ep_autoconfig_reset(cdev->gadget);
203 } else if (use_obex)
204 status = serial_register_ports(cdev, &serial_config_driver,
205 "obex");
206 else {
207 status = serial_register_ports(cdev, &serial_config_driver,
208 "gser");
210 if (status < 0)
211 goto fail;
213 usb_composite_overwrite_options(cdev, &coverwrite);
214 INFO(cdev, "%s\n", GS_VERSION_NAME);
216 return 0;
218 fail:
219 return status;
222 static int gs_unbind(struct usb_composite_dev *cdev)
224 int i;
226 for (i = 0; i < n_ports; i++) {
227 usb_put_function(f_serial[i]);
228 usb_put_function_instance(fi_serial[i]);
230 return 0;
233 static struct usb_composite_driver gserial_driver = {
234 .name = "g_serial",
235 .dev = &device_desc,
236 .strings = dev_strings,
237 .max_speed = USB_SPEED_SUPER,
238 .bind = gs_bind,
239 .unbind = gs_unbind,
242 static int __init init(void)
244 /* We *could* export two configs; that'd be much cleaner...
245 * but neither of these product IDs was defined that way.
247 if (use_acm) {
248 serial_config_driver.label = "CDC ACM config";
249 serial_config_driver.bConfigurationValue = 2;
250 device_desc.bDeviceClass = USB_CLASS_COMM;
251 device_desc.idProduct =
252 cpu_to_le16(GS_CDC_PRODUCT_ID);
253 } else if (use_obex) {
254 serial_config_driver.label = "CDC OBEX config";
255 serial_config_driver.bConfigurationValue = 3;
256 device_desc.bDeviceClass = USB_CLASS_COMM;
257 device_desc.idProduct =
258 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
259 } else {
260 serial_config_driver.label = "Generic Serial config";
261 serial_config_driver.bConfigurationValue = 1;
262 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
263 device_desc.idProduct =
264 cpu_to_le16(GS_PRODUCT_ID);
266 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
268 return usb_composite_probe(&gserial_driver);
270 module_init(init);
272 static void __exit cleanup(void)
274 usb_composite_unregister(&gserial_driver);
276 module_exit(cleanup);