1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/slab.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/etherdevice.h>
18
19#include "u_ether.h"
20#include "u_ether_configfs.h"
21#include "u_gether.h"
22
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
53struct f_gether {
54 struct gether port;
55
56 char ethaddr[14];
57};
58
59static inline struct f_gether *func_to_geth(struct usb_function *f)
60{
61 return container_of(f, struct f_gether, port.func);
62}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79static struct usb_interface_descriptor subset_data_intf = {
80 .bLength = sizeof subset_data_intf,
81 .bDescriptorType = USB_DT_INTERFACE,
82
83
84 .bAlternateSetting = 0,
85 .bNumEndpoints = 2,
86 .bInterfaceClass = USB_CLASS_COMM,
87 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
88 .bInterfaceProtocol = 0,
89
90};
91
92static struct usb_cdc_header_desc mdlm_header_desc = {
93 .bLength = sizeof mdlm_header_desc,
94 .bDescriptorType = USB_DT_CS_INTERFACE,
95 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
96
97 .bcdCDC = cpu_to_le16(0x0110),
98};
99
100static struct usb_cdc_mdlm_desc mdlm_desc = {
101 .bLength = sizeof mdlm_desc,
102 .bDescriptorType = USB_DT_CS_INTERFACE,
103 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
104
105 .bcdVersion = cpu_to_le16(0x0100),
106 .bGUID = {
107 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
108 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
109 },
110};
111
112
113
114
115
116static u8 mdlm_detail_desc[] = {
117 6,
118 USB_DT_CS_INTERFACE,
119 USB_CDC_MDLM_DETAIL_TYPE,
120
121 0,
122 0,
123 0,
124};
125
126static struct usb_cdc_ether_desc ether_desc = {
127 .bLength = sizeof ether_desc,
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
130
131
132
133 .bmEthernetStatistics = cpu_to_le32(0),
134 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
135 .wNumberMCFilters = cpu_to_le16(0),
136 .bNumberPowerFilters = 0,
137};
138
139
140
141static struct usb_endpoint_descriptor fs_subset_in_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147};
148
149static struct usb_endpoint_descriptor fs_subset_out_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152
153 .bEndpointAddress = USB_DIR_OUT,
154 .bmAttributes = USB_ENDPOINT_XFER_BULK,
155};
156
157static struct usb_descriptor_header *fs_eth_function[] = {
158 (struct usb_descriptor_header *) &subset_data_intf,
159 (struct usb_descriptor_header *) &mdlm_header_desc,
160 (struct usb_descriptor_header *) &mdlm_desc,
161 (struct usb_descriptor_header *) &mdlm_detail_desc,
162 (struct usb_descriptor_header *) ðer_desc,
163 (struct usb_descriptor_header *) &fs_subset_in_desc,
164 (struct usb_descriptor_header *) &fs_subset_out_desc,
165 NULL,
166};
167
168
169
170static struct usb_endpoint_descriptor hs_subset_in_desc = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = cpu_to_le16(512),
176};
177
178static struct usb_endpoint_descriptor hs_subset_out_desc = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181
182 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 .wMaxPacketSize = cpu_to_le16(512),
184};
185
186static struct usb_descriptor_header *hs_eth_function[] = {
187 (struct usb_descriptor_header *) &subset_data_intf,
188 (struct usb_descriptor_header *) &mdlm_header_desc,
189 (struct usb_descriptor_header *) &mdlm_desc,
190 (struct usb_descriptor_header *) &mdlm_detail_desc,
191 (struct usb_descriptor_header *) ðer_desc,
192 (struct usb_descriptor_header *) &hs_subset_in_desc,
193 (struct usb_descriptor_header *) &hs_subset_out_desc,
194 NULL,
195};
196
197
198
199static struct usb_endpoint_descriptor ss_subset_in_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(1024),
205};
206
207static struct usb_endpoint_descriptor ss_subset_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212 .wMaxPacketSize = cpu_to_le16(1024),
213};
214
215static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = {
216 .bLength = sizeof ss_subset_bulk_comp_desc,
217 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
218
219
220
221
222};
223
224static struct usb_descriptor_header *ss_eth_function[] = {
225 (struct usb_descriptor_header *) &subset_data_intf,
226 (struct usb_descriptor_header *) &mdlm_header_desc,
227 (struct usb_descriptor_header *) &mdlm_desc,
228 (struct usb_descriptor_header *) &mdlm_detail_desc,
229 (struct usb_descriptor_header *) ðer_desc,
230 (struct usb_descriptor_header *) &ss_subset_in_desc,
231 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
232 (struct usb_descriptor_header *) &ss_subset_out_desc,
233 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
234 NULL,
235};
236
237
238
239static struct usb_string geth_string_defs[] = {
240 [0].s = "CDC Ethernet Subset/SAFE",
241 [1].s = "",
242 { }
243};
244
245static struct usb_gadget_strings geth_string_table = {
246 .language = 0x0409,
247 .strings = geth_string_defs,
248};
249
250static struct usb_gadget_strings *geth_strings[] = {
251 &geth_string_table,
252 NULL,
253};
254
255
256
257static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
258{
259 struct f_gether *geth = func_to_geth(f);
260 struct usb_composite_dev *cdev = f->config->cdev;
261 struct net_device *net;
262
263
264
265 if (geth->port.in_ep->enabled) {
266 DBG(cdev, "reset cdc subset\n");
267 gether_disconnect(&geth->port);
268 }
269
270 DBG(cdev, "init + activate cdc subset\n");
271 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
272 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
273 geth->port.in_ep->desc = NULL;
274 geth->port.out_ep->desc = NULL;
275 return -EINVAL;
276 }
277
278 net = gether_connect(&geth->port);
279 return PTR_ERR_OR_ZERO(net);
280}
281
282static void geth_disable(struct usb_function *f)
283{
284 struct f_gether *geth = func_to_geth(f);
285 struct usb_composite_dev *cdev = f->config->cdev;
286
287 DBG(cdev, "net deactivated\n");
288 gether_disconnect(&geth->port);
289}
290
291
292
293
294
295static int
296geth_bind(struct usb_configuration *c, struct usb_function *f)
297{
298 struct usb_composite_dev *cdev = c->cdev;
299 struct f_gether *geth = func_to_geth(f);
300 struct usb_string *us;
301 int status;
302 struct usb_ep *ep;
303
304 struct f_gether_opts *gether_opts;
305
306 gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
307
308
309
310
311
312
313
314
315 if (!gether_opts->bound) {
316 mutex_lock(&gether_opts->lock);
317 gether_set_gadget(gether_opts->net, cdev->gadget);
318 status = gether_register_netdev(gether_opts->net);
319 mutex_unlock(&gether_opts->lock);
320 if (status)
321 return status;
322 gether_opts->bound = true;
323 }
324
325 us = usb_gstrings_attach(cdev, geth_strings,
326 ARRAY_SIZE(geth_string_defs));
327 if (IS_ERR(us))
328 return PTR_ERR(us);
329
330 subset_data_intf.iInterface = us[0].id;
331 ether_desc.iMACAddress = us[1].id;
332
333
334 status = usb_interface_id(c, f);
335 if (status < 0)
336 goto fail;
337 subset_data_intf.bInterfaceNumber = status;
338
339 status = -ENODEV;
340
341
342 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
343 if (!ep)
344 goto fail;
345 geth->port.in_ep = ep;
346
347 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
348 if (!ep)
349 goto fail;
350 geth->port.out_ep = ep;
351
352
353
354
355
356 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
357 hs_subset_out_desc.bEndpointAddress =
358 fs_subset_out_desc.bEndpointAddress;
359
360 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
361 ss_subset_out_desc.bEndpointAddress =
362 fs_subset_out_desc.bEndpointAddress;
363
364 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
365 ss_eth_function, NULL);
366 if (status)
367 goto fail;
368
369
370
371
372
373
374 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
375 gadget_is_superspeed(c->cdev->gadget) ? "super" :
376 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
377 geth->port.in_ep->name, geth->port.out_ep->name);
378 return 0;
379
380fail:
381 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
382
383 return status;
384}
385
386static inline struct f_gether_opts *to_f_gether_opts(struct config_item *item)
387{
388 return container_of(to_config_group(item), struct f_gether_opts,
389 func_inst.group);
390}
391
392
393USB_ETHERNET_CONFIGFS_ITEM(gether);
394
395
396USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(gether);
397
398
399USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(gether);
400
401
402USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(gether);
403
404
405USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(gether);
406
407static struct configfs_attribute *gether_attrs[] = {
408 &gether_opts_attr_dev_addr,
409 &gether_opts_attr_host_addr,
410 &gether_opts_attr_qmult,
411 &gether_opts_attr_ifname,
412 NULL,
413};
414
415static struct config_item_type gether_func_type = {
416 .ct_item_ops = &gether_item_ops,
417 .ct_attrs = gether_attrs,
418 .ct_owner = THIS_MODULE,
419};
420
421static void geth_free_inst(struct usb_function_instance *f)
422{
423 struct f_gether_opts *opts;
424
425 opts = container_of(f, struct f_gether_opts, func_inst);
426 if (opts->bound)
427 gether_cleanup(netdev_priv(opts->net));
428 else
429 free_netdev(opts->net);
430 kfree(opts);
431}
432
433static struct usb_function_instance *geth_alloc_inst(void)
434{
435 struct f_gether_opts *opts;
436
437 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
438 if (!opts)
439 return ERR_PTR(-ENOMEM);
440 mutex_init(&opts->lock);
441 opts->func_inst.free_func_inst = geth_free_inst;
442 opts->net = gether_setup_default();
443 if (IS_ERR(opts->net)) {
444 struct net_device *net = opts->net;
445 kfree(opts);
446 return ERR_CAST(net);
447 }
448
449 config_group_init_type_name(&opts->func_inst.group, "",
450 &gether_func_type);
451
452 return &opts->func_inst;
453}
454
455static void geth_free(struct usb_function *f)
456{
457 struct f_gether *eth;
458
459 eth = func_to_geth(f);
460 kfree(eth);
461}
462
463static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
464{
465 geth_string_defs[0].id = 0;
466 usb_free_all_descriptors(f);
467}
468
469static struct usb_function *geth_alloc(struct usb_function_instance *fi)
470{
471 struct f_gether *geth;
472 struct f_gether_opts *opts;
473 int status;
474
475
476 geth = kzalloc(sizeof(*geth), GFP_KERNEL);
477 if (!geth)
478 return ERR_PTR(-ENOMEM);
479
480 opts = container_of(fi, struct f_gether_opts, func_inst);
481
482 mutex_lock(&opts->lock);
483 opts->refcnt++;
484
485 status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
486 sizeof(geth->ethaddr));
487 if (status < 12) {
488 kfree(geth);
489 mutex_unlock(&opts->lock);
490 return ERR_PTR(-EINVAL);
491 }
492 geth_string_defs[1].s = geth->ethaddr;
493
494 geth->port.ioport = netdev_priv(opts->net);
495 mutex_unlock(&opts->lock);
496 geth->port.cdc_filter = DEFAULT_FILTER;
497
498 geth->port.func.name = "cdc_subset";
499 geth->port.func.bind = geth_bind;
500 geth->port.func.unbind = geth_unbind;
501 geth->port.func.set_alt = geth_set_alt;
502 geth->port.func.disable = geth_disable;
503 geth->port.func.free_func = geth_free;
504
505 return &geth->port.func;
506}
507
508DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
509MODULE_LICENSE("GPL");
510MODULE_AUTHOR("David Brownell");
511