1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/etherdevice.h>
18#include <linux/crc32.h>
19#include <linux/slab.h>
20
21#include "u_ether.h"
22#include "u_ether_configfs.h"
23#include "u_eem.h"
24
25#define EEM_HLEN 2
26
27
28
29
30
31
32struct f_eem {
33 struct gether port;
34 u8 ctrl_id;
35};
36
37static inline struct f_eem *func_to_eem(struct usb_function *f)
38{
39 return container_of(f, struct f_eem, port.func);
40}
41
42
43
44
45
46static struct usb_interface_descriptor eem_intf = {
47 .bLength = sizeof eem_intf,
48 .bDescriptorType = USB_DT_INTERFACE,
49
50
51 .bNumEndpoints = 2,
52 .bInterfaceClass = USB_CLASS_COMM,
53 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
54 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
55
56};
57
58
59
60static struct usb_endpoint_descriptor eem_fs_in_desc = {
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
63
64 .bEndpointAddress = USB_DIR_IN,
65 .bmAttributes = USB_ENDPOINT_XFER_BULK,
66};
67
68static struct usb_endpoint_descriptor eem_fs_out_desc = {
69 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74};
75
76static struct usb_descriptor_header *eem_fs_function[] = {
77
78 (struct usb_descriptor_header *) &eem_intf,
79 (struct usb_descriptor_header *) &eem_fs_in_desc,
80 (struct usb_descriptor_header *) &eem_fs_out_desc,
81 NULL,
82};
83
84
85
86static struct usb_endpoint_descriptor eem_hs_in_desc = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89
90 .bEndpointAddress = USB_DIR_IN,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = cpu_to_le16(512),
93};
94
95static struct usb_endpoint_descriptor eem_hs_out_desc = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98
99 .bEndpointAddress = USB_DIR_OUT,
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = cpu_to_le16(512),
102};
103
104static struct usb_descriptor_header *eem_hs_function[] = {
105
106 (struct usb_descriptor_header *) &eem_intf,
107 (struct usb_descriptor_header *) &eem_hs_in_desc,
108 (struct usb_descriptor_header *) &eem_hs_out_desc,
109 NULL,
110};
111
112
113
114static struct usb_endpoint_descriptor eem_ss_in_desc = {
115 .bLength = USB_DT_ENDPOINT_SIZE,
116 .bDescriptorType = USB_DT_ENDPOINT,
117
118 .bEndpointAddress = USB_DIR_IN,
119 .bmAttributes = USB_ENDPOINT_XFER_BULK,
120 .wMaxPacketSize = cpu_to_le16(1024),
121};
122
123static struct usb_endpoint_descriptor eem_ss_out_desc = {
124 .bLength = USB_DT_ENDPOINT_SIZE,
125 .bDescriptorType = USB_DT_ENDPOINT,
126
127 .bEndpointAddress = USB_DIR_OUT,
128 .bmAttributes = USB_ENDPOINT_XFER_BULK,
129 .wMaxPacketSize = cpu_to_le16(1024),
130};
131
132static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = {
133 .bLength = sizeof eem_ss_bulk_comp_desc,
134 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
135
136
137
138
139};
140
141static struct usb_descriptor_header *eem_ss_function[] = {
142
143 (struct usb_descriptor_header *) &eem_intf,
144 (struct usb_descriptor_header *) &eem_ss_in_desc,
145 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
146 (struct usb_descriptor_header *) &eem_ss_out_desc,
147 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
148 NULL,
149};
150
151
152
153static struct usb_string eem_string_defs[] = {
154 [0].s = "CDC Ethernet Emulation Model (EEM)",
155 { }
156};
157
158static struct usb_gadget_strings eem_string_table = {
159 .language = 0x0409,
160 .strings = eem_string_defs,
161};
162
163static struct usb_gadget_strings *eem_strings[] = {
164 &eem_string_table,
165 NULL,
166};
167
168
169
170static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
171{
172 struct usb_composite_dev *cdev = f->config->cdev;
173 int value = -EOPNOTSUPP;
174 u16 w_index = le16_to_cpu(ctrl->wIndex);
175 u16 w_value = le16_to_cpu(ctrl->wValue);
176 u16 w_length = le16_to_cpu(ctrl->wLength);
177
178 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
179 ctrl->bRequestType, ctrl->bRequest,
180 w_value, w_index, w_length);
181
182
183 return value;
184}
185
186
187static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
188{
189 struct f_eem *eem = func_to_eem(f);
190 struct usb_composite_dev *cdev = f->config->cdev;
191 struct net_device *net;
192
193
194 if (alt != 0)
195 goto fail;
196
197 if (intf == eem->ctrl_id) {
198
199 if (eem->port.in_ep->driver_data) {
200 DBG(cdev, "reset eem\n");
201 gether_disconnect(&eem->port);
202 }
203
204 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
205 DBG(cdev, "init eem\n");
206 if (config_ep_by_speed(cdev->gadget, f,
207 eem->port.in_ep) ||
208 config_ep_by_speed(cdev->gadget, f,
209 eem->port.out_ep)) {
210 eem->port.in_ep->desc = NULL;
211 eem->port.out_ep->desc = NULL;
212 goto fail;
213 }
214 }
215
216
217
218
219 eem->port.is_zlp_ok = 1;
220 eem->port.cdc_filter = DEFAULT_FILTER;
221 DBG(cdev, "activate eem\n");
222 net = gether_connect(&eem->port);
223 if (IS_ERR(net))
224 return PTR_ERR(net);
225 } else
226 goto fail;
227
228 return 0;
229fail:
230 return -EINVAL;
231}
232
233static void eem_disable(struct usb_function *f)
234{
235 struct f_eem *eem = func_to_eem(f);
236 struct usb_composite_dev *cdev = f->config->cdev;
237
238 DBG(cdev, "eem deactivated\n");
239
240 if (eem->port.in_ep->driver_data)
241 gether_disconnect(&eem->port);
242}
243
244
245
246
247
248static int eem_bind(struct usb_configuration *c, struct usb_function *f)
249{
250 struct usb_composite_dev *cdev = c->cdev;
251 struct f_eem *eem = func_to_eem(f);
252 struct usb_string *us;
253 int status;
254 struct usb_ep *ep;
255
256 struct f_eem_opts *eem_opts;
257
258 eem_opts = container_of(f->fi, struct f_eem_opts, func_inst);
259
260
261
262
263
264
265
266 if (!eem_opts->bound) {
267 mutex_lock(&eem_opts->lock);
268 gether_set_gadget(eem_opts->net, cdev->gadget);
269 status = gether_register_netdev(eem_opts->net);
270 mutex_unlock(&eem_opts->lock);
271 if (status)
272 return status;
273 eem_opts->bound = true;
274 }
275
276 us = usb_gstrings_attach(cdev, eem_strings,
277 ARRAY_SIZE(eem_string_defs));
278 if (IS_ERR(us))
279 return PTR_ERR(us);
280 eem_intf.iInterface = us[0].id;
281
282
283 status = usb_interface_id(c, f);
284 if (status < 0)
285 goto fail;
286 eem->ctrl_id = status;
287 eem_intf.bInterfaceNumber = status;
288
289 status = -ENODEV;
290
291
292 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
293 if (!ep)
294 goto fail;
295 eem->port.in_ep = ep;
296 ep->driver_data = cdev;
297
298 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
299 if (!ep)
300 goto fail;
301 eem->port.out_ep = ep;
302 ep->driver_data = cdev;
303
304 status = -ENOMEM;
305
306
307
308
309
310 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
311 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
312
313 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
314 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
315
316 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
317 eem_ss_function);
318 if (status)
319 goto fail;
320
321 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
322 gadget_is_superspeed(c->cdev->gadget) ? "super" :
323 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
324 eem->port.in_ep->name, eem->port.out_ep->name);
325 return 0;
326
327fail:
328 if (eem->port.out_ep)
329 eem->port.out_ep->driver_data = NULL;
330 if (eem->port.in_ep)
331 eem->port.in_ep->driver_data = NULL;
332
333 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
334
335 return status;
336}
337
338static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
339{
340 struct sk_buff *skb = (struct sk_buff *)req->context;
341
342 dev_kfree_skb_any(skb);
343}
344
345
346
347
348
349
350static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
351{
352 struct sk_buff *skb2 = NULL;
353 struct usb_ep *in = port->in_ep;
354 int padlen = 0;
355 u16 len = skb->len;
356
357 int headroom = skb_headroom(skb);
358 int tailroom = skb_tailroom(skb);
359
360
361
362
363 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
364 padlen += 2;
365
366 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
367 (headroom >= EEM_HLEN) && !skb_cloned(skb))
368 goto done;
369
370 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
371 dev_kfree_skb_any(skb);
372 skb = skb2;
373 if (!skb)
374 return skb;
375
376done:
377
378 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
379
380
381
382
383
384
385 len = skb->len;
386 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
387
388
389 if (padlen)
390 put_unaligned_le16(0, skb_put(skb, 2));
391
392 return skb;
393}
394
395
396
397
398
399static int eem_unwrap(struct gether *port,
400 struct sk_buff *skb,
401 struct sk_buff_head *list)
402{
403 struct usb_composite_dev *cdev = port->func.config->cdev;
404 int status = 0;
405
406 do {
407 struct sk_buff *skb2;
408 u16 header;
409 u16 len = 0;
410
411 if (skb->len < EEM_HLEN) {
412 status = -EINVAL;
413 DBG(cdev, "invalid EEM header\n");
414 goto error;
415 }
416
417
418 header = get_unaligned_le16(skb->data);
419 skb_pull(skb, EEM_HLEN);
420
421
422
423
424
425 if (header & BIT(15)) {
426 struct usb_request *req = cdev->req;
427 u16 bmEEMCmd;
428
429
430
431
432
433
434
435 if (header & BIT(14))
436 continue;
437
438 bmEEMCmd = (header >> 11) & 0x7;
439 switch (bmEEMCmd) {
440 case 0:
441 len = header & 0x7FF;
442 if (skb->len < len) {
443 status = -EOVERFLOW;
444 goto error;
445 }
446
447 skb2 = skb_clone(skb, GFP_ATOMIC);
448 if (unlikely(!skb2)) {
449 DBG(cdev, "EEM echo response error\n");
450 goto next;
451 }
452 skb_trim(skb2, len);
453 put_unaligned_le16(BIT(15) | BIT(11) | len,
454 skb_push(skb2, 2));
455 skb_copy_bits(skb2, 0, req->buf, skb2->len);
456 req->length = skb2->len;
457 req->complete = eem_cmd_complete;
458 req->zero = 1;
459 req->context = skb2;
460 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
461 DBG(cdev, "echo response queue fail\n");
462 break;
463
464 case 1:
465 case 2:
466 case 3:
467 case 4:
468 case 5:
469 default:
470 continue;
471 }
472 } else {
473 u32 crc, crc2;
474 struct sk_buff *skb3;
475
476
477 if (header == 0)
478 continue;
479
480
481
482
483
484
485 len = header & 0x3FFF;
486 if ((skb->len < len)
487 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
488 status = -EINVAL;
489 goto error;
490 }
491
492
493 if (header & BIT(14)) {
494 crc = get_unaligned_le32(skb->data + len
495 - ETH_FCS_LEN);
496 crc2 = ~crc32_le(~0,
497 skb->data, len - ETH_FCS_LEN);
498 } else {
499 crc = get_unaligned_be32(skb->data + len
500 - ETH_FCS_LEN);
501 crc2 = 0xdeadbeef;
502 }
503 if (crc != crc2) {
504 DBG(cdev, "invalid EEM CRC\n");
505 goto next;
506 }
507
508 skb2 = skb_clone(skb, GFP_ATOMIC);
509 if (unlikely(!skb2)) {
510 DBG(cdev, "unable to unframe EEM packet\n");
511 continue;
512 }
513 skb_trim(skb2, len - ETH_FCS_LEN);
514
515 skb3 = skb_copy_expand(skb2,
516 NET_IP_ALIGN,
517 0,
518 GFP_ATOMIC);
519 if (unlikely(!skb3)) {
520 DBG(cdev, "unable to realign EEM packet\n");
521 dev_kfree_skb_any(skb2);
522 continue;
523 }
524 dev_kfree_skb_any(skb2);
525 skb_queue_tail(list, skb3);
526 }
527next:
528 skb_pull(skb, len);
529 } while (skb->len);
530
531error:
532 dev_kfree_skb_any(skb);
533 return status;
534}
535
536static inline struct f_eem_opts *to_f_eem_opts(struct config_item *item)
537{
538 return container_of(to_config_group(item), struct f_eem_opts,
539 func_inst.group);
540}
541
542
543USB_ETHERNET_CONFIGFS_ITEM(eem);
544
545
546USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem);
547
548
549USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem);
550
551
552USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem);
553
554
555USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem);
556
557static struct configfs_attribute *eem_attrs[] = {
558 &f_eem_opts_dev_addr.attr,
559 &f_eem_opts_host_addr.attr,
560 &f_eem_opts_qmult.attr,
561 &f_eem_opts_ifname.attr,
562 NULL,
563};
564
565static struct config_item_type eem_func_type = {
566 .ct_item_ops = &eem_item_ops,
567 .ct_attrs = eem_attrs,
568 .ct_owner = THIS_MODULE,
569};
570
571static void eem_free_inst(struct usb_function_instance *f)
572{
573 struct f_eem_opts *opts;
574
575 opts = container_of(f, struct f_eem_opts, func_inst);
576 if (opts->bound)
577 gether_cleanup(netdev_priv(opts->net));
578 else
579 free_netdev(opts->net);
580 kfree(opts);
581}
582
583static struct usb_function_instance *eem_alloc_inst(void)
584{
585 struct f_eem_opts *opts;
586
587 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
588 if (!opts)
589 return ERR_PTR(-ENOMEM);
590 mutex_init(&opts->lock);
591 opts->func_inst.free_func_inst = eem_free_inst;
592 opts->net = gether_setup_default();
593 if (IS_ERR(opts->net)) {
594 struct net_device *net = opts->net;
595 kfree(opts);
596 return ERR_CAST(net);
597 }
598
599 config_group_init_type_name(&opts->func_inst.group, "", &eem_func_type);
600
601 return &opts->func_inst;
602}
603
604static void eem_free(struct usb_function *f)
605{
606 struct f_eem *eem;
607 struct f_eem_opts *opts;
608
609 eem = func_to_eem(f);
610 opts = container_of(f->fi, struct f_eem_opts, func_inst);
611 kfree(eem);
612 mutex_lock(&opts->lock);
613 opts->refcnt--;
614 mutex_unlock(&opts->lock);
615}
616
617static void eem_unbind(struct usb_configuration *c, struct usb_function *f)
618{
619 DBG(c->cdev, "eem unbind\n");
620
621 usb_free_all_descriptors(f);
622}
623
624static struct usb_function *eem_alloc(struct usb_function_instance *fi)
625{
626 struct f_eem *eem;
627 struct f_eem_opts *opts;
628
629
630 eem = kzalloc(sizeof(*eem), GFP_KERNEL);
631 if (!eem)
632 return ERR_PTR(-ENOMEM);
633
634 opts = container_of(fi, struct f_eem_opts, func_inst);
635 mutex_lock(&opts->lock);
636 opts->refcnt++;
637
638 eem->port.ioport = netdev_priv(opts->net);
639 mutex_unlock(&opts->lock);
640 eem->port.cdc_filter = DEFAULT_FILTER;
641
642 eem->port.func.name = "cdc_eem";
643
644 eem->port.func.bind = eem_bind;
645 eem->port.func.unbind = eem_unbind;
646 eem->port.func.set_alt = eem_set_alt;
647 eem->port.func.setup = eem_setup;
648 eem->port.func.disable = eem_disable;
649 eem->port.func.free_func = eem_free;
650 eem->port.wrap = eem_wrap;
651 eem->port.unwrap = eem_unwrap;
652 eem->port.header_len = EEM_HLEN;
653
654 return &eem->port.func;
655}
656
657DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc);
658MODULE_LICENSE("GPL");
659MODULE_AUTHOR("David Brownell");
660