1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17
18#if defined USB_ETH_RNDIS
19# undef USB_ETH_RNDIS
20#endif
21#ifdef CONFIG_USB_ETH_RNDIS
22# define USB_ETH_RNDIS y
23#endif
24
25#include "u_ether.h"
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
61
62
63#define DRIVER_DESC "Ethernet Gadget"
64#define DRIVER_VERSION "Memorial Day 2008"
65
66#ifdef USB_ETH_RNDIS
67#define PREFIX "RNDIS/"
68#else
69#define PREFIX ""
70#endif
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static inline bool has_rndis(void)
86{
87#ifdef USB_ETH_RNDIS
88 return true;
89#else
90 return false;
91#endif
92}
93
94
95
96
97
98
99
100
101
102
103#include "f_ecm.c"
104#include "f_subset.c"
105#ifdef USB_ETH_RNDIS
106#include "f_rndis.c"
107#include "rndis.c"
108#endif
109#include "f_eem.c"
110#include "u_ether.c"
111
112
113USB_GADGET_COMPOSITE_OPTIONS();
114
115
116
117
118
119
120
121
122#define CDC_VENDOR_NUM 0x0525
123#define CDC_PRODUCT_NUM 0xa4a1
124
125
126
127
128
129
130
131
132
133
134
135
136#define SIMPLE_VENDOR_NUM 0x049f
137#define SIMPLE_PRODUCT_NUM 0x505a
138
139
140
141
142
143
144#define RNDIS_VENDOR_NUM 0x0525
145#define RNDIS_PRODUCT_NUM 0xa4a2
146
147
148#define EEM_VENDOR_NUM 0x1d6b
149#define EEM_PRODUCT_NUM 0x0102
150
151
152
153static struct usb_device_descriptor device_desc = {
154 .bLength = sizeof device_desc,
155 .bDescriptorType = USB_DT_DEVICE,
156
157 .bcdUSB = cpu_to_le16 (0x0200),
158
159 .bDeviceClass = USB_CLASS_COMM,
160 .bDeviceSubClass = 0,
161 .bDeviceProtocol = 0,
162
163
164
165
166
167
168 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
169 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
170
171
172
173
174 .bNumConfigurations = 1,
175};
176
177static struct usb_otg_descriptor otg_descriptor = {
178 .bLength = sizeof otg_descriptor,
179 .bDescriptorType = USB_DT_OTG,
180
181
182
183
184 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
185};
186
187static const struct usb_descriptor_header *otg_desc[] = {
188 (struct usb_descriptor_header *) &otg_descriptor,
189 NULL,
190};
191
192static struct usb_string strings_dev[] = {
193 [USB_GADGET_MANUFACTURER_IDX].s = "",
194 [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
195 [USB_GADGET_SERIAL_IDX].s = "",
196 { }
197};
198
199static struct usb_gadget_strings stringtab_dev = {
200 .language = 0x0409,
201 .strings = strings_dev,
202};
203
204static struct usb_gadget_strings *dev_strings[] = {
205 &stringtab_dev,
206 NULL,
207};
208
209static u8 hostaddr[ETH_ALEN];
210static struct eth_dev *the_dev;
211
212
213
214
215
216
217
218static int __init rndis_do_config(struct usb_configuration *c)
219{
220
221
222 if (gadget_is_otg(c->cdev->gadget)) {
223 c->descriptors = otg_desc;
224 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
225 }
226
227 return rndis_bind_config(c, hostaddr, the_dev);
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 __init eth_do_config(struct usb_configuration *c)
251{
252
253
254 if (gadget_is_otg(c->cdev->gadget)) {
255 c->descriptors = otg_desc;
256 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
257 }
258
259 if (use_eem)
260 return eem_bind_config(c, the_dev);
261 else if (can_support_ecm(c->cdev->gadget))
262 return ecm_bind_config(c, hostaddr, the_dev);
263 else
264 return geth_bind_config(c, hostaddr, the_dev);
265}
266
267static struct usb_configuration eth_config_driver = {
268
269 .bConfigurationValue = 1,
270
271 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
272};
273
274
275
276static int __init eth_bind(struct usb_composite_dev *cdev)
277{
278 struct usb_gadget *gadget = cdev->gadget;
279 int status;
280
281
282 the_dev = gether_setup(cdev->gadget, hostaddr);
283 if (IS_ERR(the_dev))
284 return PTR_ERR(the_dev);
285
286
287 if (use_eem) {
288
289 eth_config_driver.label = "CDC Ethernet (EEM)";
290 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
291 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
292 } else if (can_support_ecm(cdev->gadget)) {
293
294 eth_config_driver.label = "CDC Ethernet (ECM)";
295 } else {
296
297 eth_config_driver.label = "CDC Subset/SAFE";
298
299 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
300 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
301 if (!has_rndis())
302 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
303 }
304
305 if (has_rndis()) {
306
307 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
308 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
309 device_desc.bNumConfigurations = 2;
310 }
311
312
313
314
315
316 status = usb_string_ids_tab(cdev, strings_dev);
317 if (status < 0)
318 goto fail;
319 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
320 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
321
322
323 if (has_rndis()) {
324 status = usb_add_config(cdev, &rndis_config_driver,
325 rndis_do_config);
326 if (status < 0)
327 goto fail;
328 }
329
330 status = usb_add_config(cdev, ð_config_driver, eth_do_config);
331 if (status < 0)
332 goto fail;
333
334 usb_composite_overwrite_options(cdev, &coverwrite);
335 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
336 DRIVER_DESC);
337
338 return 0;
339
340fail:
341 gether_cleanup(the_dev);
342 return status;
343}
344
345static int __exit eth_unbind(struct usb_composite_dev *cdev)
346{
347 gether_cleanup(the_dev);
348 return 0;
349}
350
351static __refdata struct usb_composite_driver eth_driver = {
352 .name = "g_ether",
353 .dev = &device_desc,
354 .strings = dev_strings,
355 .max_speed = USB_SPEED_SUPER,
356 .bind = eth_bind,
357 .unbind = __exit_p(eth_unbind),
358};
359
360MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
361MODULE_AUTHOR("David Brownell, Benedikt Spanger");
362MODULE_LICENSE("GPL");
363
364static int __init init(void)
365{
366 return usb_composite_probe(ð_driver);
367}
368module_init(init);
369
370static void __exit cleanup(void)
371{
372 usb_composite_unregister(ð_driver);
373}
374module_exit(cleanup);
375