dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / serial.c
blobc5d42e0347a94cbd190c7162626ec9177f130662
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"
22 /* Defines */
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30 /*-------------------------------------------------------------------------*/
31 USB_GADGET_COMPOSITE_OPTIONS();
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 */
41 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
43 /* string IDs are assigned dynamically */
45 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
47 static struct usb_string strings_dev[] = {
48 [USB_GADGET_MANUFACTURER_IDX].s = "",
49 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
50 [USB_GADGET_SERIAL_IDX].s = "",
51 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
52 { } /* end of list */
55 static struct usb_gadget_strings stringtab_dev = {
56 .language = 0x0409, /* en-us */
57 .strings = strings_dev,
60 static struct usb_gadget_strings *dev_strings[] = {
61 &stringtab_dev,
62 NULL,
65 static struct usb_device_descriptor device_desc = {
66 .bLength = USB_DT_DEVICE_SIZE,
67 .bDescriptorType = USB_DT_DEVICE,
68 .bcdUSB = cpu_to_le16(0x0200),
69 /* .bDeviceClass = f(use_acm) */
70 .bDeviceSubClass = 0,
71 .bDeviceProtocol = 0,
72 /* .bMaxPacketSize0 = f(hardware) */
73 .idVendor = cpu_to_le16(GS_VENDOR_ID),
74 /* .idProduct = f(use_acm) */
75 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
76 /* .iManufacturer = DYNAMIC */
77 /* .iProduct = DYNAMIC */
78 .bNumConfigurations = 1,
81 static const struct usb_descriptor_header *otg_desc[2];
83 /*-------------------------------------------------------------------------*/
85 /* Module */
86 MODULE_DESCRIPTION(GS_VERSION_NAME);
87 MODULE_AUTHOR("Al Borchers");
88 MODULE_AUTHOR("David Brownell");
89 MODULE_LICENSE("GPL");
91 static bool use_acm = true;
92 module_param(use_acm, bool, 0);
93 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
95 static bool use_obex = false;
96 module_param(use_obex, bool, 0);
97 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
99 static unsigned n_ports = 1;
100 module_param(n_ports, uint, 0);
101 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
103 /*-------------------------------------------------------------------------*/
105 static struct usb_configuration serial_config_driver = {
106 /* .label = f(use_acm) */
107 /* .bConfigurationValue = f(use_acm) */
108 /* .iConfiguration = DYNAMIC */
109 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
112 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
113 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
115 static int serial_register_ports(struct usb_composite_dev *cdev,
116 struct usb_configuration *c, const char *f_name)
118 int i;
119 int ret;
121 ret = usb_add_config_only(cdev, c);
122 if (ret)
123 goto out;
125 for (i = 0; i < n_ports; i++) {
127 fi_serial[i] = usb_get_function_instance(f_name);
128 if (IS_ERR(fi_serial[i])) {
129 ret = PTR_ERR(fi_serial[i]);
130 goto fail;
133 f_serial[i] = usb_get_function(fi_serial[i]);
134 if (IS_ERR(f_serial[i])) {
135 ret = PTR_ERR(f_serial[i]);
136 goto err_get_func;
139 ret = usb_add_function(c, f_serial[i]);
140 if (ret)
141 goto err_add_func;
144 return 0;
146 err_add_func:
147 usb_put_function(f_serial[i]);
148 err_get_func:
149 usb_put_function_instance(fi_serial[i]);
151 fail:
152 i--;
153 while (i >= 0) {
154 usb_remove_function(c, f_serial[i]);
155 usb_put_function(f_serial[i]);
156 usb_put_function_instance(fi_serial[i]);
157 i--;
159 out:
160 return ret;
163 static int gs_bind(struct usb_composite_dev *cdev)
165 int status;
167 /* Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
171 status = usb_string_ids_tab(cdev, strings_dev);
172 if (status < 0)
173 goto fail;
174 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
175 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
176 status = strings_dev[STRING_DESCRIPTION_IDX].id;
177 serial_config_driver.iConfiguration = status;
179 if (gadget_is_otg(cdev->gadget)) {
180 if (!otg_desc[0]) {
181 struct usb_descriptor_header *usb_desc;
183 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
184 if (!usb_desc) {
185 status = -ENOMEM;
186 goto fail;
188 usb_otg_descriptor_init(cdev->gadget, usb_desc);
189 otg_desc[0] = usb_desc;
190 otg_desc[1] = NULL;
192 serial_config_driver.descriptors = otg_desc;
193 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
196 /* register our configuration */
197 if (use_acm) {
198 status = serial_register_ports(cdev, &serial_config_driver,
199 "acm");
200 usb_ep_autoconfig_reset(cdev->gadget);
201 } else if (use_obex)
202 status = serial_register_ports(cdev, &serial_config_driver,
203 "obex");
204 else {
205 status = serial_register_ports(cdev, &serial_config_driver,
206 "gser");
208 if (status < 0)
209 goto fail1;
211 usb_composite_overwrite_options(cdev, &coverwrite);
212 INFO(cdev, "%s\n", GS_VERSION_NAME);
214 return 0;
215 fail1:
216 kfree(otg_desc[0]);
217 otg_desc[0] = NULL;
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]);
231 kfree(otg_desc[0]);
232 otg_desc[0] = NULL;
234 return 0;
237 static struct usb_composite_driver gserial_driver = {
238 .name = "g_serial",
239 .dev = &device_desc,
240 .strings = dev_strings,
241 .max_speed = USB_SPEED_SUPER,
242 .bind = gs_bind,
243 .unbind = gs_unbind,
246 static int __init init(void)
248 /* We *could* export two configs; that'd be much cleaner...
249 * but neither of these product IDs was defined that way.
251 if (use_acm) {
252 serial_config_driver.label = "CDC ACM config";
253 serial_config_driver.bConfigurationValue = 2;
254 device_desc.bDeviceClass = USB_CLASS_COMM;
255 device_desc.idProduct =
256 cpu_to_le16(GS_CDC_PRODUCT_ID);
257 } else if (use_obex) {
258 serial_config_driver.label = "CDC OBEX config";
259 serial_config_driver.bConfigurationValue = 3;
260 device_desc.bDeviceClass = USB_CLASS_COMM;
261 device_desc.idProduct =
262 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
263 } else {
264 serial_config_driver.label = "Generic Serial config";
265 serial_config_driver.bConfigurationValue = 1;
266 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
267 device_desc.idProduct =
268 cpu_to_le16(GS_PRODUCT_ID);
270 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
272 return usb_composite_probe(&gserial_driver);
274 module_init(init);
276 static void __exit cleanup(void)
278 usb_composite_unregister(&gserial_driver);
280 module_exit(cleanup);