1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/netdevice.h>
18
19#if defined USB_ETH_RNDIS
20# undef USB_ETH_RNDIS
21#endif
22#ifdef CONFIG_USB_ETH_RNDIS
23# define USB_ETH_RNDIS y
24#endif
25
26#include "u_ether.h"
27
28
29
30
31
32
33
34
35
36
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#define DRIVER_DESC "Ethernet Gadget"
65#define DRIVER_VERSION "Memorial Day 2008"
66
67#ifdef USB_ETH_RNDIS
68#define PREFIX "RNDIS/"
69#else
70#define PREFIX ""
71#endif
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86static inline bool has_rndis(void)
87{
88#ifdef USB_ETH_RNDIS
89 return true;
90#else
91 return false;
92#endif
93}
94
95#include <linux/module.h>
96
97#include "u_ecm.h"
98#include "u_gether.h"
99#ifdef USB_ETH_RNDIS
100#include "u_rndis.h"
101#include "rndis.h"
102#else
103#define rndis_borrow_net(...) do {} while (0)
104#endif
105#include "u_eem.h"
106
107
108USB_GADGET_COMPOSITE_OPTIONS();
109
110USB_ETHERNET_MODULE_PARAMETERS();
111
112
113
114
115
116
117
118
119#define CDC_VENDOR_NUM 0x0525
120#define CDC_PRODUCT_NUM 0xa4a1
121
122
123
124
125
126
127
128
129
130
131
132
133#define SIMPLE_VENDOR_NUM 0x049f
134#define SIMPLE_PRODUCT_NUM 0x505a
135
136
137
138
139
140
141#define RNDIS_VENDOR_NUM 0x0525
142#define RNDIS_PRODUCT_NUM 0xa4a2
143
144
145#define EEM_VENDOR_NUM 0x1d6b
146#define EEM_PRODUCT_NUM 0x0102
147
148
149
150static struct usb_device_descriptor device_desc = {
151 .bLength = sizeof device_desc,
152 .bDescriptorType = USB_DT_DEVICE,
153
154
155
156 .bDeviceClass = USB_CLASS_COMM,
157 .bDeviceSubClass = 0,
158 .bDeviceProtocol = 0,
159
160
161
162
163
164
165 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
166 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
167
168
169
170
171 .bNumConfigurations = 1,
172};
173
174static const struct usb_descriptor_header *otg_desc[2];
175
176static struct usb_string strings_dev[] = {
177 [USB_GADGET_MANUFACTURER_IDX].s = "",
178 [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
179 [USB_GADGET_SERIAL_IDX].s = "",
180 { }
181};
182
183static struct usb_gadget_strings stringtab_dev = {
184 .language = 0x0409,
185 .strings = strings_dev,
186};
187
188static struct usb_gadget_strings *dev_strings[] = {
189 &stringtab_dev,
190 NULL,
191};
192
193static struct usb_function_instance *fi_ecm;
194static struct usb_function *f_ecm;
195
196static struct usb_function_instance *fi_eem;
197static struct usb_function *f_eem;
198
199static struct usb_function_instance *fi_geth;
200static struct usb_function *f_geth;
201
202static struct usb_function_instance *fi_rndis;
203static struct usb_function *f_rndis;
204
205
206
207
208
209
210
211
212static int rndis_do_config(struct usb_configuration *c)
213{
214 int status;
215
216
217
218 if (gadget_is_otg(c->cdev->gadget)) {
219 c->descriptors = otg_desc;
220 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
221 }
222
223 f_rndis = usb_get_function(fi_rndis);
224 if (IS_ERR(f_rndis))
225 return PTR_ERR(f_rndis);
226
227 status = usb_add_function(c, f_rndis);
228 if (status < 0)
229 usb_put_function(f_rndis);
230
231 return status;
232}
233
234static struct usb_configuration rndis_config_driver = {
235 .label = "RNDIS",
236 .bConfigurationValue = 2,
237
238 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
239};
240
241
242
243#ifdef CONFIG_USB_ETH_EEM
244static bool use_eem = 1;
245#else
246static bool use_eem;
247#endif
248module_param(use_eem, bool, 0);
249MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
250
251
252
253
254static int eth_do_config(struct usb_configuration *c)
255{
256 int status = 0;
257
258
259
260 if (gadget_is_otg(c->cdev->gadget)) {
261 c->descriptors = otg_desc;
262 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
263 }
264
265 if (use_eem) {
266 f_eem = usb_get_function(fi_eem);
267 if (IS_ERR(f_eem))
268 return PTR_ERR(f_eem);
269
270 status = usb_add_function(c, f_eem);
271 if (status < 0)
272 usb_put_function(f_eem);
273
274 return status;
275 } else if (can_support_ecm(c->cdev->gadget)) {
276 f_ecm = usb_get_function(fi_ecm);
277 if (IS_ERR(f_ecm))
278 return PTR_ERR(f_ecm);
279
280 status = usb_add_function(c, f_ecm);
281 if (status < 0)
282 usb_put_function(f_ecm);
283
284 return status;
285 } else {
286 f_geth = usb_get_function(fi_geth);
287 if (IS_ERR(f_geth))
288 return PTR_ERR(f_geth);
289
290 status = usb_add_function(c, f_geth);
291 if (status < 0)
292 usb_put_function(f_geth);
293
294 return status;
295 }
296
297}
298
299static struct usb_configuration eth_config_driver = {
300
301 .bConfigurationValue = 1,
302
303 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
304};
305
306
307
308static int eth_bind(struct usb_composite_dev *cdev)
309{
310 struct usb_gadget *gadget = cdev->gadget;
311 struct f_eem_opts *eem_opts = NULL;
312 struct f_ecm_opts *ecm_opts = NULL;
313 struct f_gether_opts *geth_opts = NULL;
314 struct net_device *net;
315 int status;
316
317
318 if (use_eem) {
319
320 fi_eem = usb_get_function_instance("eem");
321 if (IS_ERR(fi_eem))
322 return PTR_ERR(fi_eem);
323
324 eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
325
326 net = eem_opts->net;
327
328 eth_config_driver.label = "CDC Ethernet (EEM)";
329 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
330 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
331 } else if (can_support_ecm(gadget)) {
332
333
334 fi_ecm = usb_get_function_instance("ecm");
335 if (IS_ERR(fi_ecm))
336 return PTR_ERR(fi_ecm);
337
338 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
339
340 net = ecm_opts->net;
341
342 eth_config_driver.label = "CDC Ethernet (ECM)";
343 } else {
344
345
346 fi_geth = usb_get_function_instance("geth");
347 if (IS_ERR(fi_geth))
348 return PTR_ERR(fi_geth);
349
350 geth_opts = container_of(fi_geth, struct f_gether_opts,
351 func_inst);
352
353 net = geth_opts->net;
354
355 eth_config_driver.label = "CDC Subset/SAFE";
356
357 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
358 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
359 if (!has_rndis())
360 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
361 }
362
363 gether_set_qmult(net, qmult);
364 if (!gether_set_host_addr(net, host_addr))
365 pr_info("using host ethernet address: %s", host_addr);
366 if (!gether_set_dev_addr(net, dev_addr))
367 pr_info("using self ethernet address: %s", dev_addr);
368
369 if (has_rndis()) {
370
371 gether_set_gadget(net, cdev->gadget);
372 status = gether_register_netdev(net);
373 if (status)
374 goto fail;
375
376 if (use_eem)
377 eem_opts->bound = true;
378 else if (can_support_ecm(gadget))
379 ecm_opts->bound = true;
380 else
381 geth_opts->bound = true;
382
383 fi_rndis = usb_get_function_instance("rndis");
384 if (IS_ERR(fi_rndis)) {
385 status = PTR_ERR(fi_rndis);
386 goto fail;
387 }
388
389 rndis_borrow_net(fi_rndis, net);
390
391 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
392 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
393 device_desc.bNumConfigurations = 2;
394 }
395
396
397
398
399
400 status = usb_string_ids_tab(cdev, strings_dev);
401 if (status < 0)
402 goto fail1;
403 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
404 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
405
406 if (gadget_is_otg(gadget) && !otg_desc[0]) {
407 struct usb_descriptor_header *usb_desc;
408
409 usb_desc = usb_otg_descriptor_alloc(gadget);
410 if (!usb_desc)
411 goto fail1;
412 usb_otg_descriptor_init(gadget, usb_desc);
413 otg_desc[0] = usb_desc;
414 otg_desc[1] = NULL;
415 }
416
417
418 if (has_rndis()) {
419 status = usb_add_config(cdev, &rndis_config_driver,
420 rndis_do_config);
421 if (status < 0)
422 goto fail2;
423 }
424
425 status = usb_add_config(cdev, ð_config_driver, eth_do_config);
426 if (status < 0)
427 goto fail2;
428
429 usb_composite_overwrite_options(cdev, &coverwrite);
430 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
431 DRIVER_DESC);
432
433 return 0;
434
435fail2:
436 kfree(otg_desc[0]);
437 otg_desc[0] = NULL;
438fail1:
439 if (has_rndis())
440 usb_put_function_instance(fi_rndis);
441fail:
442 if (use_eem)
443 usb_put_function_instance(fi_eem);
444 else if (can_support_ecm(gadget))
445 usb_put_function_instance(fi_ecm);
446 else
447 usb_put_function_instance(fi_geth);
448 return status;
449}
450
451static int eth_unbind(struct usb_composite_dev *cdev)
452{
453 if (has_rndis()) {
454 usb_put_function(f_rndis);
455 usb_put_function_instance(fi_rndis);
456 }
457 if (use_eem) {
458 usb_put_function(f_eem);
459 usb_put_function_instance(fi_eem);
460 } else if (can_support_ecm(cdev->gadget)) {
461 usb_put_function(f_ecm);
462 usb_put_function_instance(fi_ecm);
463 } else {
464 usb_put_function(f_geth);
465 usb_put_function_instance(fi_geth);
466 }
467 kfree(otg_desc[0]);
468 otg_desc[0] = NULL;
469
470 return 0;
471}
472
473static struct usb_composite_driver eth_driver = {
474 .name = "g_ether",
475 .dev = &device_desc,
476 .strings = dev_strings,
477 .max_speed = USB_SPEED_SUPER,
478 .bind = eth_bind,
479 .unbind = eth_unbind,
480};
481
482module_usb_composite_driver(eth_driver);
483
484MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
485MODULE_AUTHOR("David Brownell, Benedikt Spanger");
486MODULE_LICENSE("GPL");
487