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