Commit | Line | Data |
---|---|---|
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/device.h> |
d1412794 | 15 | #include <linux/module.h> |
1da177e4 LT |
16 | #include <linux/tty.h> |
17 | #include <linux/tty_flip.h> | |
1da177e4 | 18 | |
a7707adf | 19 | #include "u_serial.h" |
1da177e4 LT |
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 | ||
4e9ba518 | 30 | /*-------------------------------------------------------------------------*/ |
7d16e8d3 | 31 | USB_GADGET_COMPOSITE_OPTIONS(); |
4e9ba518 | 32 | |
1da177e4 | 33 | /* Thanks to NetChip Technologies for donating this product ID. |
7bb5ea54 DB |
34 | * |
35 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
36 | * Instead: allocate your own, using normal USB-IF procedures. | |
37 | */ | |
1da177e4 LT |
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 */ | |
3086775a | 41 | #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */ |
1da177e4 | 42 | |
7bb5ea54 | 43 | /* string IDs are assigned dynamically */ |
1da177e4 | 44 | |
276e2e4f | 45 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
a7707adf | 46 | |
7bb5ea54 | 47 | static struct usb_string strings_dev[] = { |
cc2683c3 | 48 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
49 | [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME, |
50 | [USB_GADGET_SERIAL_IDX].s = "", | |
7bb5ea54 | 51 | [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, |
1da177e4 LT |
52 | { } /* end of list */ |
53 | }; | |
54 | ||
7bb5ea54 DB |
55 | static struct usb_gadget_strings stringtab_dev = { |
56 | .language = 0x0409, /* en-us */ | |
57 | .strings = strings_dev, | |
1da177e4 LT |
58 | }; |
59 | ||
7bb5ea54 DB |
60 | static struct usb_gadget_strings *dev_strings[] = { |
61 | &stringtab_dev, | |
62 | NULL, | |
63 | }; | |
64 | ||
65 | static struct usb_device_descriptor device_desc = { | |
1da177e4 LT |
66 | .bLength = USB_DT_DEVICE_SIZE, |
67 | .bDescriptorType = USB_DT_DEVICE, | |
0aecfc1b | 68 | /* .bcdUSB = DYNAMIC */ |
7bb5ea54 | 69 | /* .bDeviceClass = f(use_acm) */ |
1da177e4 LT |
70 | .bDeviceSubClass = 0, |
71 | .bDeviceProtocol = 0, | |
7bb5ea54 | 72 | /* .bMaxPacketSize0 = f(hardware) */ |
551509d2 | 73 | .idVendor = cpu_to_le16(GS_VENDOR_ID), |
7bb5ea54 | 74 | /* .idProduct = f(use_acm) */ |
5c4d46eb | 75 | .bcdDevice = cpu_to_le16(GS_VERSION_NUM), |
7bb5ea54 DB |
76 | /* .iManufacturer = DYNAMIC */ |
77 | /* .iProduct = DYNAMIC */ | |
78 | .bNumConfigurations = 1, | |
1da177e4 LT |
79 | }; |
80 | ||
3dcc7053 | 81 | static const struct usb_descriptor_header *otg_desc[2]; |
1da177e4 | 82 | |
a7707adf | 83 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 84 | |
a7707adf DB |
85 | /* Module */ |
86 | MODULE_DESCRIPTION(GS_VERSION_NAME); | |
87 | MODULE_AUTHOR("Al Borchers"); | |
88 | MODULE_AUTHOR("David Brownell"); | |
89 | MODULE_LICENSE("GPL"); | |
1da177e4 | 90 | |
90ab5ee9 | 91 | static bool use_acm = true; |
7bb5ea54 DB |
92 | module_param(use_acm, bool, 0); |
93 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); | |
1da177e4 | 94 | |
90ab5ee9 | 95 | static bool use_obex = false; |
3086775a FB |
96 | module_param(use_obex, bool, 0); |
97 | MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no"); | |
98 | ||
7bb5ea54 DB |
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"); | |
9079e91b | 102 | |
7bb5ea54 | 103 | /*-------------------------------------------------------------------------*/ |
48177cd8 | 104 | |
7bb5ea54 DB |
105 | static struct usb_configuration serial_config_driver = { |
106 | /* .label = f(use_acm) */ | |
7bb5ea54 DB |
107 | /* .bConfigurationValue = f(use_acm) */ |
108 | /* .iConfiguration = DYNAMIC */ | |
109 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
7bb5ea54 DB |
110 | }; |
111 | ||
ff47f594 SAS |
112 | static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS]; |
113 | static struct usb_function *f_serial[MAX_U_SERIAL_PORTS]; | |
114 | ||
115 | static int serial_register_ports(struct usb_composite_dev *cdev, | |
116 | struct usb_configuration *c, const char *f_name) | |
19b10a88 SAS |
117 | { |
118 | int i; | |
ff47f594 SAS |
119 | int ret; |
120 | ||
121 | ret = usb_add_config_only(cdev, c); | |
122 | if (ret) | |
123 | goto out; | |
124 | ||
125 | for (i = 0; i < n_ports; i++) { | |
ff47f594 SAS |
126 | |
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; | |
131 | } | |
ff47f594 SAS |
132 | |
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; | |
137 | } | |
138 | ||
139 | ret = usb_add_function(c, f_serial[i]); | |
140 | if (ret) | |
141 | goto err_add_func; | |
142 | } | |
19b10a88 | 143 | |
19b10a88 | 144 | return 0; |
ff47f594 SAS |
145 | |
146 | err_add_func: | |
147 | usb_put_function(f_serial[i]); | |
148 | err_get_func: | |
149 | usb_put_function_instance(fi_serial[i]); | |
150 | ||
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--; | |
158 | } | |
159 | out: | |
160 | return ret; | |
19b10a88 SAS |
161 | } |
162 | ||
c94e289f | 163 | static int gs_bind(struct usb_composite_dev *cdev) |
1da177e4 | 164 | { |
7bb5ea54 | 165 | int status; |
a7707adf | 166 | |
7bb5ea54 DB |
167 | /* Allocate string descriptor numbers ... note that string |
168 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 169 | */ |
1da177e4 | 170 | |
e1f15ccb | 171 | status = usb_string_ids_tab(cdev, strings_dev); |
7bb5ea54 DB |
172 | if (status < 0) |
173 | goto fail; | |
276e2e4f SAS |
174 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
175 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
e1f15ccb | 176 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
7bb5ea54 | 177 | serial_config_driver.iConfiguration = status; |
1da177e4 | 178 | |
7bb5ea54 | 179 | if (gadget_is_otg(cdev->gadget)) { |
3dcc7053 LJ |
180 | if (!otg_desc[0]) { |
181 | struct usb_descriptor_header *usb_desc; | |
182 | ||
183 | usb_desc = usb_otg_descriptor_alloc(cdev->gadget); | |
184 | if (!usb_desc) { | |
185 | status = -ENOMEM; | |
186 | goto fail; | |
187 | } | |
188 | usb_otg_descriptor_init(cdev->gadget, usb_desc); | |
189 | otg_desc[0] = usb_desc; | |
190 | otg_desc[1] = NULL; | |
191 | } | |
7bb5ea54 DB |
192 | serial_config_driver.descriptors = otg_desc; |
193 | serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
1da177e4 | 194 | } |
9079e91b | 195 | |
7bb5ea54 | 196 | /* register our configuration */ |
ff47f594 SAS |
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) | |
d1412794 AP |
202 | status = serial_register_ports(cdev, &serial_config_driver, |
203 | "obex"); | |
70cc3c02 AP |
204 | else { |
205 | status = serial_register_ports(cdev, &serial_config_driver, | |
206 | "gser"); | |
207 | } | |
7bb5ea54 | 208 | if (status < 0) |
3dcc7053 | 209 | goto fail1; |
1da177e4 | 210 | |
7d16e8d3 | 211 | usb_composite_overwrite_options(cdev, &coverwrite); |
7bb5ea54 | 212 | INFO(cdev, "%s\n", GS_VERSION_NAME); |
1da177e4 | 213 | |
7bb5ea54 | 214 | return 0; |
3dcc7053 LJ |
215 | fail1: |
216 | kfree(otg_desc[0]); | |
217 | otg_desc[0] = NULL; | |
7bb5ea54 | 218 | fail: |
7bb5ea54 | 219 | return status; |
1da177e4 LT |
220 | } |
221 | ||
ff47f594 SAS |
222 | static int gs_unbind(struct usb_composite_dev *cdev) |
223 | { | |
224 | int i; | |
225 | ||
226 | for (i = 0; i < n_ports; i++) { | |
227 | usb_put_function(f_serial[i]); | |
228 | usb_put_function_instance(fi_serial[i]); | |
ff47f594 | 229 | } |
3dcc7053 LJ |
230 | |
231 | kfree(otg_desc[0]); | |
232 | otg_desc[0] = NULL; | |
233 | ||
ff47f594 SAS |
234 | return 0; |
235 | } | |
236 | ||
c94e289f | 237 | static struct usb_composite_driver gserial_driver = { |
7bb5ea54 DB |
238 | .name = "g_serial", |
239 | .dev = &device_desc, | |
240 | .strings = dev_strings, | |
6fecfb05 | 241 | .max_speed = USB_SPEED_SUPER, |
03e42bd5 | 242 | .bind = gs_bind, |
19b10a88 | 243 | .unbind = gs_unbind, |
9079e91b DB |
244 | }; |
245 | ||
7bb5ea54 | 246 | static int __init init(void) |
1da177e4 | 247 | { |
7bb5ea54 DB |
248 | /* We *could* export two configs; that'd be much cleaner... |
249 | * but neither of these product IDs was defined that way. | |
f371e750 | 250 | */ |
1da177e4 | 251 | if (use_acm) { |
7bb5ea54 DB |
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 = | |
551509d2 | 256 | cpu_to_le16(GS_CDC_PRODUCT_ID); |
3086775a FB |
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 = | |
551509d2 | 262 | cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID); |
1da177e4 | 263 | } else { |
7bb5ea54 DB |
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 = | |
551509d2 | 268 | cpu_to_le16(GS_PRODUCT_ID); |
1da177e4 | 269 | } |
7bb5ea54 | 270 | strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; |
9079e91b | 271 | |
03e42bd5 | 272 | return usb_composite_probe(&gserial_driver); |
9079e91b | 273 | } |
7bb5ea54 | 274 | module_init(init); |
9079e91b | 275 | |
7bb5ea54 | 276 | static void __exit cleanup(void) |
9079e91b | 277 | { |
7bb5ea54 | 278 | usb_composite_unregister(&gserial_driver); |
9079e91b | 279 | } |
7bb5ea54 | 280 | module_exit(cleanup); |