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/utsname.h> |
1da177e4 LT |
15 | #include <linux/device.h> |
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 | ||
7bb5ea54 | 31 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 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 */ | |
41 | ||
7bb5ea54 | 42 | /* string IDs are assigned dynamically */ |
1da177e4 | 43 | |
7bb5ea54 DB |
44 | #define STRING_MANUFACTURER_IDX 0 |
45 | #define STRING_PRODUCT_IDX 1 | |
46 | #define STRING_DESCRIPTION_IDX 2 | |
a7707adf | 47 | |
1da177e4 | 48 | static char manufacturer[50]; |
7bb5ea54 DB |
49 | |
50 | static struct usb_string strings_dev[] = { | |
51 | [STRING_MANUFACTURER_IDX].s = manufacturer, | |
52 | [STRING_PRODUCT_IDX].s = GS_VERSION_NAME, | |
53 | [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, | |
1da177e4 LT |
54 | { } /* end of list */ |
55 | }; | |
56 | ||
7bb5ea54 DB |
57 | static struct usb_gadget_strings stringtab_dev = { |
58 | .language = 0x0409, /* en-us */ | |
59 | .strings = strings_dev, | |
1da177e4 LT |
60 | }; |
61 | ||
7bb5ea54 DB |
62 | static struct usb_gadget_strings *dev_strings[] = { |
63 | &stringtab_dev, | |
64 | NULL, | |
65 | }; | |
66 | ||
67 | static struct usb_device_descriptor device_desc = { | |
1da177e4 LT |
68 | .bLength = USB_DT_DEVICE_SIZE, |
69 | .bDescriptorType = USB_DT_DEVICE, | |
70 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
7bb5ea54 | 71 | /* .bDeviceClass = f(use_acm) */ |
1da177e4 LT |
72 | .bDeviceSubClass = 0, |
73 | .bDeviceProtocol = 0, | |
7bb5ea54 | 74 | /* .bMaxPacketSize0 = f(hardware) */ |
1da177e4 | 75 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), |
7bb5ea54 DB |
76 | /* .idProduct = f(use_acm) */ |
77 | /* .bcdDevice = f(hardware) */ | |
78 | /* .iManufacturer = DYNAMIC */ | |
79 | /* .iProduct = DYNAMIC */ | |
80 | .bNumConfigurations = 1, | |
1da177e4 LT |
81 | }; |
82 | ||
7bb5ea54 DB |
83 | static struct usb_otg_descriptor otg_descriptor = { |
84 | .bLength = sizeof otg_descriptor, | |
1da177e4 | 85 | .bDescriptorType = USB_DT_OTG, |
1da177e4 | 86 | |
7bb5ea54 DB |
87 | /* REVISIT SRP-only hardware is possible, although |
88 | * it would not be called "OTG" ... | |
89 | */ | |
90 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
a7707adf DB |
91 | }; |
92 | ||
7bb5ea54 DB |
93 | static const struct usb_descriptor_header *otg_desc[] = { |
94 | (struct usb_descriptor_header *) &otg_descriptor, | |
a7707adf DB |
95 | NULL, |
96 | }; | |
1da177e4 | 97 | |
a7707adf | 98 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 99 | |
a7707adf DB |
100 | /* Module */ |
101 | MODULE_DESCRIPTION(GS_VERSION_NAME); | |
102 | MODULE_AUTHOR("Al Borchers"); | |
103 | MODULE_AUTHOR("David Brownell"); | |
104 | MODULE_LICENSE("GPL"); | |
1da177e4 | 105 | |
7bb5ea54 DB |
106 | static int use_acm = true; |
107 | module_param(use_acm, bool, 0); | |
108 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); | |
1da177e4 | 109 | |
7bb5ea54 DB |
110 | static unsigned n_ports = 1; |
111 | module_param(n_ports, uint, 0); | |
112 | MODULE_PARM_DESC(n_ports, "number of ports to create, default=1"); | |
9079e91b | 113 | |
7bb5ea54 | 114 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 115 | |
7bb5ea54 | 116 | static int __init serial_bind_config(struct usb_configuration *c) |
9079e91b | 117 | { |
7bb5ea54 DB |
118 | unsigned i; |
119 | int status = 0; | |
9079e91b | 120 | |
7bb5ea54 DB |
121 | for (i = 0; i < n_ports && status == 0; i++) { |
122 | if (use_acm) | |
123 | status = acm_bind_config(c, i); | |
124 | else | |
125 | status = gser_bind_config(c, i); | |
9079e91b | 126 | } |
7bb5ea54 | 127 | return status; |
9079e91b DB |
128 | } |
129 | ||
7bb5ea54 DB |
130 | static struct usb_configuration serial_config_driver = { |
131 | /* .label = f(use_acm) */ | |
132 | .bind = serial_bind_config, | |
133 | /* .bConfigurationValue = f(use_acm) */ | |
134 | /* .iConfiguration = DYNAMIC */ | |
135 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
136 | .bMaxPower = 1, /* 2 mA, minimal */ | |
137 | }; | |
138 | ||
139 | static int __init gs_bind(struct usb_composite_dev *cdev) | |
1da177e4 | 140 | { |
7bb5ea54 DB |
141 | int gcnum; |
142 | struct usb_gadget *gadget = cdev->gadget; | |
143 | int status; | |
1da177e4 | 144 | |
7bb5ea54 DB |
145 | status = gserial_setup(cdev->gadget, n_ports); |
146 | if (status < 0) | |
147 | return status; | |
a7707adf | 148 | |
7bb5ea54 DB |
149 | /* Allocate string descriptor numbers ... note that string |
150 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 151 | */ |
1da177e4 | 152 | |
7bb5ea54 DB |
153 | /* device description: manufacturer, product */ |
154 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | |
96b644bd | 155 | init_utsname()->sysname, init_utsname()->release, |
1da177e4 | 156 | gadget->name); |
7bb5ea54 DB |
157 | status = usb_string_id(cdev); |
158 | if (status < 0) | |
159 | goto fail; | |
160 | strings_dev[STRING_MANUFACTURER_IDX].id = status; | |
1da177e4 | 161 | |
7bb5ea54 | 162 | device_desc.iManufacturer = status; |
1da177e4 | 163 | |
7bb5ea54 DB |
164 | status = usb_string_id(cdev); |
165 | if (status < 0) | |
166 | goto fail; | |
167 | strings_dev[STRING_PRODUCT_IDX].id = status; | |
1da177e4 | 168 | |
7bb5ea54 | 169 | device_desc.iProduct = status; |
1da177e4 | 170 | |
7bb5ea54 DB |
171 | /* config description */ |
172 | status = usb_string_id(cdev); | |
173 | if (status < 0) | |
174 | goto fail; | |
175 | strings_dev[STRING_DESCRIPTION_IDX].id = status; | |
1da177e4 | 176 | |
7bb5ea54 | 177 | serial_config_driver.iConfiguration = status; |
1da177e4 | 178 | |
7bb5ea54 DB |
179 | /* set up other descriptors */ |
180 | gcnum = usb_gadget_controller_number(gadget); | |
181 | if (gcnum >= 0) | |
182 | device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum); | |
183 | else { | |
184 | /* this is so simple (for now, no altsettings) that it | |
185 | * SHOULD NOT have problems with bulk-capable hardware. | |
186 | * so warn about unrcognized controllers -- don't panic. | |
187 | * | |
188 | * things like configuration and altsetting numbering | |
189 | * can need hardware-specific attention though. | |
f371e750 | 190 | */ |
7bb5ea54 DB |
191 | pr_warning("gs_bind: controller '%s' not recognized\n", |
192 | gadget->name); | |
193 | device_desc.bcdDevice = | |
194 | __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099); | |
f371e750 | 195 | } |
f371e750 | 196 | |
7bb5ea54 DB |
197 | if (gadget_is_otg(cdev->gadget)) { |
198 | serial_config_driver.descriptors = otg_desc; | |
199 | serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
1da177e4 | 200 | } |
9079e91b | 201 | |
7bb5ea54 DB |
202 | /* register our configuration */ |
203 | status = usb_add_config(cdev, &serial_config_driver); | |
204 | if (status < 0) | |
205 | goto fail; | |
1da177e4 | 206 | |
7bb5ea54 | 207 | INFO(cdev, "%s\n", GS_VERSION_NAME); |
1da177e4 | 208 | |
7bb5ea54 | 209 | return 0; |
1da177e4 | 210 | |
7bb5ea54 DB |
211 | fail: |
212 | gserial_cleanup(); | |
213 | return status; | |
1da177e4 LT |
214 | } |
215 | ||
7bb5ea54 DB |
216 | static struct usb_composite_driver gserial_driver = { |
217 | .name = "g_serial", | |
218 | .dev = &device_desc, | |
219 | .strings = dev_strings, | |
220 | .bind = gs_bind, | |
9079e91b DB |
221 | }; |
222 | ||
7bb5ea54 | 223 | static int __init init(void) |
1da177e4 | 224 | { |
7bb5ea54 DB |
225 | /* We *could* export two configs; that'd be much cleaner... |
226 | * but neither of these product IDs was defined that way. | |
f371e750 | 227 | */ |
1da177e4 | 228 | if (use_acm) { |
7bb5ea54 DB |
229 | serial_config_driver.label = "CDC ACM config"; |
230 | serial_config_driver.bConfigurationValue = 2; | |
231 | device_desc.bDeviceClass = USB_CLASS_COMM; | |
232 | device_desc.idProduct = | |
233 | __constant_cpu_to_le16(GS_CDC_PRODUCT_ID); | |
1da177e4 | 234 | } else { |
7bb5ea54 DB |
235 | serial_config_driver.label = "Generic Serial config"; |
236 | serial_config_driver.bConfigurationValue = 1; | |
237 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; | |
238 | device_desc.idProduct = | |
239 | __constant_cpu_to_le16(GS_PRODUCT_ID); | |
1da177e4 | 240 | } |
7bb5ea54 | 241 | strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; |
9079e91b | 242 | |
7bb5ea54 | 243 | return usb_composite_register(&gserial_driver); |
9079e91b | 244 | } |
7bb5ea54 | 245 | module_init(init); |
9079e91b | 246 | |
7bb5ea54 | 247 | static void __exit cleanup(void) |
9079e91b | 248 | { |
7bb5ea54 DB |
249 | usb_composite_unregister(&gserial_driver); |
250 | gserial_cleanup(); | |
9079e91b | 251 | } |
7bb5ea54 | 252 | module_exit(cleanup); |