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 | #include "gadget_chips.h" |
21 | ||
22 | ||
1da177e4 LT |
23 | /* Defines */ |
24 | ||
7bb5ea54 DB |
25 | #define GS_VERSION_STR "v2.4" |
26 | #define GS_VERSION_NUM 0x2400 | |
1da177e4 LT |
27 | |
28 | #define GS_LONG_NAME "Gadget Serial" | |
a7707adf DB |
29 | #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR |
30 | ||
4e9ba518 | 31 | /*-------------------------------------------------------------------------*/ |
7d16e8d3 | 32 | USB_GADGET_COMPOSITE_OPTIONS(); |
4e9ba518 | 33 | |
1da177e4 | 34 | /* Thanks to NetChip Technologies for donating this product ID. |
7bb5ea54 DB |
35 | * |
36 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
37 | * Instead: allocate your own, using normal USB-IF procedures. | |
38 | */ | |
1da177e4 LT |
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 */ | |
3086775a | 42 | #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */ |
1da177e4 | 43 | |
7bb5ea54 | 44 | /* string IDs are assigned dynamically */ |
1da177e4 | 45 | |
276e2e4f | 46 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
a7707adf | 47 | |
7bb5ea54 | 48 | static struct usb_string strings_dev[] = { |
cc2683c3 | 49 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
50 | [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME, |
51 | [USB_GADGET_SERIAL_IDX].s = "", | |
7bb5ea54 | 52 | [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, |
1da177e4 LT |
53 | { } /* end of list */ |
54 | }; | |
55 | ||
7bb5ea54 DB |
56 | static struct usb_gadget_strings stringtab_dev = { |
57 | .language = 0x0409, /* en-us */ | |
58 | .strings = strings_dev, | |
1da177e4 LT |
59 | }; |
60 | ||
7bb5ea54 DB |
61 | static struct usb_gadget_strings *dev_strings[] = { |
62 | &stringtab_dev, | |
63 | NULL, | |
64 | }; | |
65 | ||
66 | static struct usb_device_descriptor device_desc = { | |
1da177e4 LT |
67 | .bLength = USB_DT_DEVICE_SIZE, |
68 | .bDescriptorType = USB_DT_DEVICE, | |
551509d2 | 69 | .bcdUSB = cpu_to_le16(0x0200), |
7bb5ea54 | 70 | /* .bDeviceClass = f(use_acm) */ |
1da177e4 LT |
71 | .bDeviceSubClass = 0, |
72 | .bDeviceProtocol = 0, | |
7bb5ea54 | 73 | /* .bMaxPacketSize0 = f(hardware) */ |
551509d2 | 74 | .idVendor = cpu_to_le16(GS_VENDOR_ID), |
7bb5ea54 | 75 | /* .idProduct = f(use_acm) */ |
5c4d46eb | 76 | .bcdDevice = cpu_to_le16(GS_VERSION_NUM), |
7bb5ea54 DB |
77 | /* .iManufacturer = DYNAMIC */ |
78 | /* .iProduct = DYNAMIC */ | |
79 | .bNumConfigurations = 1, | |
1da177e4 LT |
80 | }; |
81 | ||
7bb5ea54 DB |
82 | static struct usb_otg_descriptor otg_descriptor = { |
83 | .bLength = sizeof otg_descriptor, | |
1da177e4 | 84 | .bDescriptorType = USB_DT_OTG, |
1da177e4 | 85 | |
7bb5ea54 DB |
86 | /* REVISIT SRP-only hardware is possible, although |
87 | * it would not be called "OTG" ... | |
88 | */ | |
89 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
a7707adf DB |
90 | }; |
91 | ||
7bb5ea54 DB |
92 | static const struct usb_descriptor_header *otg_desc[] = { |
93 | (struct usb_descriptor_header *) &otg_descriptor, | |
a7707adf DB |
94 | NULL, |
95 | }; | |
1da177e4 | 96 | |
a7707adf | 97 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 98 | |
a7707adf DB |
99 | /* Module */ |
100 | MODULE_DESCRIPTION(GS_VERSION_NAME); | |
101 | MODULE_AUTHOR("Al Borchers"); | |
102 | MODULE_AUTHOR("David Brownell"); | |
103 | MODULE_LICENSE("GPL"); | |
1da177e4 | 104 | |
90ab5ee9 | 105 | static bool use_acm = true; |
7bb5ea54 DB |
106 | module_param(use_acm, bool, 0); |
107 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); | |
1da177e4 | 108 | |
90ab5ee9 | 109 | static bool use_obex = false; |
3086775a FB |
110 | module_param(use_obex, bool, 0); |
111 | MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no"); | |
112 | ||
7bb5ea54 DB |
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"); | |
9079e91b | 116 | |
7bb5ea54 | 117 | /*-------------------------------------------------------------------------*/ |
48177cd8 | 118 | |
7bb5ea54 DB |
119 | static struct usb_configuration serial_config_driver = { |
120 | /* .label = f(use_acm) */ | |
7bb5ea54 DB |
121 | /* .bConfigurationValue = f(use_acm) */ |
122 | /* .iConfiguration = DYNAMIC */ | |
123 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
7bb5ea54 DB |
124 | }; |
125 | ||
ff47f594 SAS |
126 | static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS]; |
127 | static struct usb_function *f_serial[MAX_U_SERIAL_PORTS]; | |
128 | ||
129 | static int serial_register_ports(struct usb_composite_dev *cdev, | |
130 | struct usb_configuration *c, const char *f_name) | |
19b10a88 SAS |
131 | { |
132 | int i; | |
ff47f594 SAS |
133 | int ret; |
134 | ||
135 | ret = usb_add_config_only(cdev, c); | |
136 | if (ret) | |
137 | goto out; | |
138 | ||
139 | for (i = 0; i < n_ports; i++) { | |
ff47f594 SAS |
140 | |
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; | |
145 | } | |
ff47f594 SAS |
146 | |
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; | |
151 | } | |
152 | ||
153 | ret = usb_add_function(c, f_serial[i]); | |
154 | if (ret) | |
155 | goto err_add_func; | |
156 | } | |
19b10a88 | 157 | |
19b10a88 | 158 | return 0; |
ff47f594 SAS |
159 | |
160 | err_add_func: | |
161 | usb_put_function(f_serial[i]); | |
162 | err_get_func: | |
163 | usb_put_function_instance(fi_serial[i]); | |
164 | ||
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--; | |
172 | } | |
173 | out: | |
174 | return ret; | |
19b10a88 SAS |
175 | } |
176 | ||
e12995ec | 177 | static int __init gs_bind(struct usb_composite_dev *cdev) |
1da177e4 | 178 | { |
7bb5ea54 | 179 | int status; |
a7707adf | 180 | |
7bb5ea54 DB |
181 | /* Allocate string descriptor numbers ... note that string |
182 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 183 | */ |
1da177e4 | 184 | |
e1f15ccb | 185 | status = usb_string_ids_tab(cdev, strings_dev); |
7bb5ea54 DB |
186 | if (status < 0) |
187 | goto fail; | |
276e2e4f SAS |
188 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
189 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
e1f15ccb | 190 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
7bb5ea54 | 191 | serial_config_driver.iConfiguration = status; |
1da177e4 | 192 | |
7bb5ea54 DB |
193 | if (gadget_is_otg(cdev->gadget)) { |
194 | serial_config_driver.descriptors = otg_desc; | |
195 | serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
1da177e4 | 196 | } |
9079e91b | 197 | |
7bb5ea54 | 198 | /* register our configuration */ |
ff47f594 SAS |
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) | |
d1412794 AP |
204 | status = serial_register_ports(cdev, &serial_config_driver, |
205 | "obex"); | |
70cc3c02 AP |
206 | else { |
207 | status = serial_register_ports(cdev, &serial_config_driver, | |
208 | "gser"); | |
209 | } | |
7bb5ea54 DB |
210 | if (status < 0) |
211 | goto fail; | |
1da177e4 | 212 | |
7d16e8d3 | 213 | usb_composite_overwrite_options(cdev, &coverwrite); |
7bb5ea54 | 214 | INFO(cdev, "%s\n", GS_VERSION_NAME); |
1da177e4 | 215 | |
7bb5ea54 | 216 | return 0; |
1da177e4 | 217 | |
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 SAS |
229 | } |
230 | return 0; | |
231 | } | |
232 | ||
c2ec75c2 | 233 | static __refdata struct usb_composite_driver gserial_driver = { |
7bb5ea54 DB |
234 | .name = "g_serial", |
235 | .dev = &device_desc, | |
236 | .strings = dev_strings, | |
6fecfb05 | 237 | .max_speed = USB_SPEED_SUPER, |
03e42bd5 | 238 | .bind = gs_bind, |
19b10a88 | 239 | .unbind = gs_unbind, |
9079e91b DB |
240 | }; |
241 | ||
7bb5ea54 | 242 | static int __init init(void) |
1da177e4 | 243 | { |
7bb5ea54 DB |
244 | /* We *could* export two configs; that'd be much cleaner... |
245 | * but neither of these product IDs was defined that way. | |
f371e750 | 246 | */ |
1da177e4 | 247 | if (use_acm) { |
7bb5ea54 DB |
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 = | |
551509d2 | 252 | cpu_to_le16(GS_CDC_PRODUCT_ID); |
3086775a FB |
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 = | |
551509d2 | 258 | cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID); |
1da177e4 | 259 | } else { |
7bb5ea54 DB |
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 = | |
551509d2 | 264 | cpu_to_le16(GS_PRODUCT_ID); |
1da177e4 | 265 | } |
7bb5ea54 | 266 | strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; |
9079e91b | 267 | |
03e42bd5 | 268 | return usb_composite_probe(&gserial_driver); |
9079e91b | 269 | } |
7bb5ea54 | 270 | module_init(init); |
9079e91b | 271 | |
7bb5ea54 | 272 | static void __exit cleanup(void) |
9079e91b | 273 | { |
7bb5ea54 | 274 | usb_composite_unregister(&gserial_driver); |
9079e91b | 275 | } |
7bb5ea54 | 276 | module_exit(cleanup); |