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