This source file includes following definitions.
- has_rndis
- rndis_do_config
- eth_do_config
- eth_bind
- eth_unbind
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
82 static 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
104 USB_GADGET_COMPOSITE_OPTIONS();
105
106 USB_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
146 static 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
170 static const struct usb_descriptor_header *otg_desc[2];
171
172 static 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
179 static struct usb_gadget_strings stringtab_dev = {
180 .language = 0x0409,
181 .strings = strings_dev,
182 };
183
184 static struct usb_gadget_strings *dev_strings[] = {
185 &stringtab_dev,
186 NULL,
187 };
188
189 static struct usb_function_instance *fi_ecm;
190 static struct usb_function *f_ecm;
191
192 static struct usb_function_instance *fi_eem;
193 static struct usb_function *f_eem;
194
195 static struct usb_function_instance *fi_geth;
196 static struct usb_function *f_geth;
197
198 static struct usb_function_instance *fi_rndis;
199 static struct usb_function *f_rndis;
200
201
202
203
204
205
206
207
208 static 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
230 static 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
240 static bool use_eem = 1;
241 #else
242 static bool use_eem;
243 #endif
244 module_param(use_eem, bool, 0);
245 MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
246
247
248
249
250 static 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
295 static struct usb_configuration eth_config_driver = {
296
297 .bConfigurationValue = 1,
298
299 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
300 };
301
302
303
304 static 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 goto fail1;
408 usb_otg_descriptor_init(gadget, usb_desc);
409 otg_desc[0] = usb_desc;
410 otg_desc[1] = NULL;
411 }
412
413
414 if (has_rndis()) {
415 status = usb_add_config(cdev, &rndis_config_driver,
416 rndis_do_config);
417 if (status < 0)
418 goto fail2;
419 }
420
421 status = usb_add_config(cdev, ð_config_driver, eth_do_config);
422 if (status < 0)
423 goto fail2;
424
425 usb_composite_overwrite_options(cdev, &coverwrite);
426 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
427 DRIVER_DESC);
428
429 return 0;
430
431 fail2:
432 kfree(otg_desc[0]);
433 otg_desc[0] = NULL;
434 fail1:
435 if (has_rndis())
436 usb_put_function_instance(fi_rndis);
437 fail:
438 if (use_eem)
439 usb_put_function_instance(fi_eem);
440 else if (can_support_ecm(gadget))
441 usb_put_function_instance(fi_ecm);
442 else
443 usb_put_function_instance(fi_geth);
444 return status;
445 }
446
447 static int eth_unbind(struct usb_composite_dev *cdev)
448 {
449 if (has_rndis()) {
450 usb_put_function(f_rndis);
451 usb_put_function_instance(fi_rndis);
452 }
453 if (use_eem) {
454 usb_put_function(f_eem);
455 usb_put_function_instance(fi_eem);
456 } else if (can_support_ecm(cdev->gadget)) {
457 usb_put_function(f_ecm);
458 usb_put_function_instance(fi_ecm);
459 } else {
460 usb_put_function(f_geth);
461 usb_put_function_instance(fi_geth);
462 }
463 kfree(otg_desc[0]);
464 otg_desc[0] = NULL;
465
466 return 0;
467 }
468
469 static struct usb_composite_driver eth_driver = {
470 .name = "g_ether",
471 .dev = &device_desc,
472 .strings = dev_strings,
473 .max_speed = USB_SPEED_SUPER,
474 .bind = eth_bind,
475 .unbind = eth_unbind,
476 };
477
478 module_usb_composite_driver(eth_driver);
479
480 MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
481 MODULE_AUTHOR("David Brownell, Benedikt Spanger");
482 MODULE_LICENSE("GPL");