1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/device.h>
24#include <linux/etherdevice.h>
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
59struct geth_descs {
60 struct usb_endpoint_descriptor *in;
61 struct usb_endpoint_descriptor *out;
62};
63
64struct f_gether {
65 struct gether port;
66
67 char ethaddr[14];
68
69 struct geth_descs fs;
70 struct geth_descs hs;
71};
72
73static inline struct f_gether *func_to_geth(struct usb_function *f)
74{
75 return container_of(f, struct f_gether, port.func);
76}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93static struct usb_interface_descriptor subset_data_intf __initdata = {
94 .bLength = sizeof subset_data_intf,
95 .bDescriptorType = USB_DT_INTERFACE,
96
97
98 .bAlternateSetting = 0,
99 .bNumEndpoints = 2,
100 .bInterfaceClass = USB_CLASS_COMM,
101 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
102 .bInterfaceProtocol = 0,
103
104};
105
106static struct usb_cdc_header_desc mdlm_header_desc __initdata = {
107 .bLength = sizeof mdlm_header_desc,
108 .bDescriptorType = USB_DT_CS_INTERFACE,
109 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
110
111 .bcdCDC = cpu_to_le16(0x0110),
112};
113
114static struct usb_cdc_mdlm_desc mdlm_desc __initdata = {
115 .bLength = sizeof mdlm_desc,
116 .bDescriptorType = USB_DT_CS_INTERFACE,
117 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
118
119 .bcdVersion = cpu_to_le16(0x0100),
120 .bGUID = {
121 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
122 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
123 },
124};
125
126
127
128
129
130static u8 mdlm_detail_desc[] __initdata = {
131 6,
132 USB_DT_CS_INTERFACE,
133 USB_CDC_MDLM_DETAIL_TYPE,
134
135 0,
136 0,
137 0,
138};
139
140static struct usb_cdc_ether_desc ether_desc __initdata = {
141 .bLength = sizeof ether_desc,
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
144
145
146
147 .bmEthernetStatistics = cpu_to_le32(0),
148 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
149 .wNumberMCFilters = cpu_to_le16(0),
150 .bNumberPowerFilters = 0,
151};
152
153
154
155static struct usb_endpoint_descriptor fs_subset_in_desc __initdata = {
156 .bLength = USB_DT_ENDPOINT_SIZE,
157 .bDescriptorType = USB_DT_ENDPOINT,
158
159 .bEndpointAddress = USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK,
161};
162
163static struct usb_endpoint_descriptor fs_subset_out_desc __initdata = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166
167 .bEndpointAddress = USB_DIR_OUT,
168 .bmAttributes = USB_ENDPOINT_XFER_BULK,
169};
170
171static struct usb_descriptor_header *fs_eth_function[] __initdata = {
172 (struct usb_descriptor_header *) &subset_data_intf,
173 (struct usb_descriptor_header *) &mdlm_header_desc,
174 (struct usb_descriptor_header *) &mdlm_desc,
175 (struct usb_descriptor_header *) &mdlm_detail_desc,
176 (struct usb_descriptor_header *) ðer_desc,
177 (struct usb_descriptor_header *) &fs_subset_in_desc,
178 (struct usb_descriptor_header *) &fs_subset_out_desc,
179 NULL,
180};
181
182
183
184static struct usb_endpoint_descriptor hs_subset_in_desc __initdata = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bmAttributes = USB_ENDPOINT_XFER_BULK,
189 .wMaxPacketSize = cpu_to_le16(512),
190};
191
192static struct usb_endpoint_descriptor hs_subset_out_desc __initdata = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
195
196 .bmAttributes = USB_ENDPOINT_XFER_BULK,
197 .wMaxPacketSize = cpu_to_le16(512),
198};
199
200static struct usb_descriptor_header *hs_eth_function[] __initdata = {
201 (struct usb_descriptor_header *) &subset_data_intf,
202 (struct usb_descriptor_header *) &mdlm_header_desc,
203 (struct usb_descriptor_header *) &mdlm_desc,
204 (struct usb_descriptor_header *) &mdlm_detail_desc,
205 (struct usb_descriptor_header *) ðer_desc,
206 (struct usb_descriptor_header *) &hs_subset_in_desc,
207 (struct usb_descriptor_header *) &hs_subset_out_desc,
208 NULL,
209};
210
211
212
213static struct usb_string geth_string_defs[] = {
214 [0].s = "CDC Ethernet Subset/SAFE",
215 [1].s = NULL ,
216 { }
217};
218
219static struct usb_gadget_strings geth_string_table = {
220 .language = 0x0409,
221 .strings = geth_string_defs,
222};
223
224static struct usb_gadget_strings *geth_strings[] = {
225 &geth_string_table,
226 NULL,
227};
228
229
230
231static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
232{
233 struct f_gether *geth = func_to_geth(f);
234 struct usb_composite_dev *cdev = f->config->cdev;
235 struct net_device *net;
236
237
238
239 if (geth->port.in_ep->driver_data) {
240 DBG(cdev, "reset cdc subset\n");
241 gether_disconnect(&geth->port);
242 }
243
244 DBG(cdev, "init + activate cdc subset\n");
245 geth->port.in = ep_choose(cdev->gadget,
246 geth->hs.in, geth->fs.in);
247 geth->port.out = ep_choose(cdev->gadget,
248 geth->hs.out, geth->fs.out);
249
250 net = gether_connect(&geth->port);
251 return IS_ERR(net) ? PTR_ERR(net) : 0;
252}
253
254static void geth_disable(struct usb_function *f)
255{
256 struct f_gether *geth = func_to_geth(f);
257 struct usb_composite_dev *cdev = f->config->cdev;
258
259 DBG(cdev, "net deactivated\n");
260 gether_disconnect(&geth->port);
261}
262
263
264
265
266
267static int __init
268geth_bind(struct usb_configuration *c, struct usb_function *f)
269{
270 struct usb_composite_dev *cdev = c->cdev;
271 struct f_gether *geth = func_to_geth(f);
272 int status;
273 struct usb_ep *ep;
274
275
276 status = usb_interface_id(c, f);
277 if (status < 0)
278 goto fail;
279 subset_data_intf.bInterfaceNumber = status;
280
281 status = -ENODEV;
282
283
284 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
285 if (!ep)
286 goto fail;
287 geth->port.in_ep = ep;
288 ep->driver_data = cdev;
289
290 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
291 if (!ep)
292 goto fail;
293 geth->port.out_ep = ep;
294 ep->driver_data = cdev;
295
296
297 f->descriptors = usb_copy_descriptors(fs_eth_function);
298
299 geth->fs.in = usb_find_endpoint(fs_eth_function,
300 f->descriptors, &fs_subset_in_desc);
301 geth->fs.out = usb_find_endpoint(fs_eth_function,
302 f->descriptors, &fs_subset_out_desc);
303
304
305
306
307
308
309 if (gadget_is_dualspeed(c->cdev->gadget)) {
310 hs_subset_in_desc.bEndpointAddress =
311 fs_subset_in_desc.bEndpointAddress;
312 hs_subset_out_desc.bEndpointAddress =
313 fs_subset_out_desc.bEndpointAddress;
314
315
316 f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
317
318 geth->hs.in = usb_find_endpoint(hs_eth_function,
319 f->hs_descriptors, &hs_subset_in_desc);
320 geth->hs.out = usb_find_endpoint(hs_eth_function,
321 f->hs_descriptors, &hs_subset_out_desc);
322 }
323
324
325
326
327
328
329 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
330 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
331 geth->port.in_ep->name, geth->port.out_ep->name);
332 return 0;
333
334fail:
335
336 if (geth->port.out)
337 geth->port.out_ep->driver_data = NULL;
338 if (geth->port.in)
339 geth->port.in_ep->driver_data = NULL;
340
341 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
342
343 return status;
344}
345
346static void
347geth_unbind(struct usb_configuration *c, struct usb_function *f)
348{
349 if (gadget_is_dualspeed(c->cdev->gadget))
350 usb_free_descriptors(f->hs_descriptors);
351 usb_free_descriptors(f->descriptors);
352 geth_string_defs[1].s = NULL;
353 kfree(func_to_geth(f));
354}
355
356
357
358
359
360
361
362
363
364
365
366
367
368int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
369{
370 struct f_gether *geth;
371 int status;
372
373 if (!ethaddr)
374 return -EINVAL;
375
376
377 if (geth_string_defs[0].id == 0) {
378
379
380 status = usb_string_id(c->cdev);
381 if (status < 0)
382 return status;
383 geth_string_defs[0].id = status;
384 subset_data_intf.iInterface = status;
385
386
387 status = usb_string_id(c->cdev);
388 if (status < 0)
389 return status;
390 geth_string_defs[1].id = status;
391 ether_desc.iMACAddress = status;
392 }
393
394
395 geth = kzalloc(sizeof *geth, GFP_KERNEL);
396 if (!geth)
397 return -ENOMEM;
398
399
400 snprintf(geth->ethaddr, sizeof geth->ethaddr,
401 "%02X%02X%02X%02X%02X%02X",
402 ethaddr[0], ethaddr[1], ethaddr[2],
403 ethaddr[3], ethaddr[4], ethaddr[5]);
404 geth_string_defs[1].s = geth->ethaddr;
405
406 geth->port.cdc_filter = DEFAULT_FILTER;
407
408 geth->port.func.name = "cdc_subset";
409 geth->port.func.strings = geth_strings;
410 geth->port.func.bind = geth_bind;
411 geth->port.func.unbind = geth_unbind;
412 geth->port.func.set_alt = geth_set_alt;
413 geth->port.func.disable = geth_disable;
414
415 status = usb_add_function(c, &geth->port.func);
416 if (status) {
417 geth_string_defs[1].s = NULL;
418 kfree(geth);
419 }
420 return status;
421}
422