1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/utsname.h>
27
28
29#if defined USB_ETH_RNDIS
30# undef USB_ETH_RNDIS
31#endif
32#ifdef CONFIG_USB_ETH_RNDIS
33# define USB_ETH_RNDIS y
34#endif
35
36#include "u_ether.h"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74#define DRIVER_DESC "Ethernet Gadget"
75#define DRIVER_VERSION "Memorial Day 2008"
76
77#ifdef USB_ETH_RNDIS
78#define PREFIX "RNDIS/"
79#else
80#define PREFIX ""
81#endif
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96static inline bool has_rndis(void)
97{
98#ifdef USB_ETH_RNDIS
99 return true;
100#else
101 return false;
102#endif
103}
104
105
106
107
108
109
110
111
112
113
114#include "composite.c"
115#include "usbstring.c"
116#include "config.c"
117#include "epautoconf.c"
118
119#include "f_ecm.c"
120#include "f_subset.c"
121#ifdef USB_ETH_RNDIS
122#include "f_rndis.c"
123#include "rndis.c"
124#endif
125#include "f_eem.c"
126#include "u_ether.c"
127
128
129
130
131
132
133
134
135
136
137#define CDC_VENDOR_NUM 0x0525
138#define CDC_PRODUCT_NUM 0xa4a1
139
140
141
142
143
144
145
146
147
148
149
150
151#define SIMPLE_VENDOR_NUM 0x049f
152#define SIMPLE_PRODUCT_NUM 0x505a
153
154
155
156
157
158
159#define RNDIS_VENDOR_NUM 0x0525
160#define RNDIS_PRODUCT_NUM 0xa4a2
161
162
163#define EEM_VENDOR_NUM 0x1d6b
164#define EEM_PRODUCT_NUM 0x0102
165
166
167
168static struct usb_device_descriptor device_desc = {
169 .bLength = sizeof device_desc,
170 .bDescriptorType = USB_DT_DEVICE,
171
172 .bcdUSB = cpu_to_le16 (0x0200),
173
174 .bDeviceClass = USB_CLASS_COMM,
175 .bDeviceSubClass = 0,
176 .bDeviceProtocol = 0,
177
178
179
180
181
182
183 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
184 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
185
186
187
188
189 .bNumConfigurations = 1,
190};
191
192static struct usb_otg_descriptor otg_descriptor = {
193 .bLength = sizeof otg_descriptor,
194 .bDescriptorType = USB_DT_OTG,
195
196
197
198
199 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
200};
201
202static const struct usb_descriptor_header *otg_desc[] = {
203 (struct usb_descriptor_header *) &otg_descriptor,
204 NULL,
205};
206
207
208
209
210#define STRING_MANUFACTURER_IDX 0
211#define STRING_PRODUCT_IDX 1
212
213static char manufacturer[50];
214
215static struct usb_string strings_dev[] = {
216 [STRING_MANUFACTURER_IDX].s = manufacturer,
217 [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
218 { }
219};
220
221static struct usb_gadget_strings stringtab_dev = {
222 .language = 0x0409,
223 .strings = strings_dev,
224};
225
226static struct usb_gadget_strings *dev_strings[] = {
227 &stringtab_dev,
228 NULL,
229};
230
231static u8 hostaddr[ETH_ALEN];
232
233
234
235
236
237
238
239
240static int __init rndis_do_config(struct usb_configuration *c)
241{
242
243
244 if (gadget_is_otg(c->cdev->gadget)) {
245 c->descriptors = otg_desc;
246 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
247 }
248
249 return rndis_bind_config(c, hostaddr);
250}
251
252static struct usb_configuration rndis_config_driver = {
253 .label = "RNDIS",
254 .bConfigurationValue = 2,
255
256 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
257};
258
259
260
261#ifdef CONFIG_USB_ETH_EEM
262static int use_eem = 1;
263#else
264static int use_eem;
265#endif
266module_param(use_eem, bool, 0);
267MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
268
269
270
271
272static int __init eth_do_config(struct usb_configuration *c)
273{
274
275
276 if (gadget_is_otg(c->cdev->gadget)) {
277 c->descriptors = otg_desc;
278 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
279 }
280
281 if (use_eem)
282 return eem_bind_config(c);
283 else if (can_support_ecm(c->cdev->gadget))
284 return ecm_bind_config(c, hostaddr);
285 else
286 return geth_bind_config(c, hostaddr);
287}
288
289static struct usb_configuration eth_config_driver = {
290
291 .bConfigurationValue = 1,
292
293 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
294};
295
296
297
298static int __init eth_bind(struct usb_composite_dev *cdev)
299{
300 int gcnum;
301 struct usb_gadget *gadget = cdev->gadget;
302 int status;
303
304
305 status = gether_setup(cdev->gadget, hostaddr);
306 if (status < 0)
307 return status;
308
309
310 if (use_eem) {
311
312 eth_config_driver.label = "CDC Ethernet (EEM)";
313 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
314 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
315 } else if (can_support_ecm(cdev->gadget)) {
316
317 eth_config_driver.label = "CDC Ethernet (ECM)";
318 } else {
319
320 eth_config_driver.label = "CDC Subset/SAFE";
321
322 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
323 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
324 if (!has_rndis())
325 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
326 }
327
328 if (has_rndis()) {
329
330 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
331 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
332 device_desc.bNumConfigurations = 2;
333 }
334
335 gcnum = usb_gadget_controller_number(gadget);
336 if (gcnum >= 0)
337 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
338 else {
339
340
341
342
343 dev_warn(&gadget->dev,
344 "controller '%s' not recognized; trying %s\n",
345 gadget->name,
346 eth_config_driver.label);
347 device_desc.bcdDevice =
348 cpu_to_le16(0x0300 | 0x0099);
349 }
350
351
352
353
354
355
356
357 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
358 init_utsname()->sysname, init_utsname()->release,
359 gadget->name);
360 status = usb_string_id(cdev);
361 if (status < 0)
362 goto fail;
363 strings_dev[STRING_MANUFACTURER_IDX].id = status;
364 device_desc.iManufacturer = status;
365
366 status = usb_string_id(cdev);
367 if (status < 0)
368 goto fail;
369 strings_dev[STRING_PRODUCT_IDX].id = status;
370 device_desc.iProduct = status;
371
372
373 if (has_rndis()) {
374 status = usb_add_config(cdev, &rndis_config_driver,
375 rndis_do_config);
376 if (status < 0)
377 goto fail;
378 }
379
380 status = usb_add_config(cdev, ð_config_driver, eth_do_config);
381 if (status < 0)
382 goto fail;
383
384 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
385 DRIVER_DESC);
386
387 return 0;
388
389fail:
390 gether_cleanup();
391 return status;
392}
393
394static int __exit eth_unbind(struct usb_composite_dev *cdev)
395{
396 gether_cleanup();
397 return 0;
398}
399
400static struct usb_composite_driver eth_driver = {
401 .name = "g_ether",
402 .dev = &device_desc,
403 .strings = dev_strings,
404 .unbind = __exit_p(eth_unbind),
405};
406
407MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
408MODULE_AUTHOR("David Brownell, Benedikt Spanger");
409MODULE_LICENSE("GPL");
410
411static int __init init(void)
412{
413 return usb_composite_probe(ð_driver, eth_bind);
414}
415module_init(init);
416
417static void __exit cleanup(void)
418{
419 usb_composite_unregister(ð_driver);
420}
421module_exit(cleanup);
422