1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/device.h>
18#include <linux/etherdevice.h>
19#include <linux/crc32.h>
20
21#include <linux/usb/cdc.h>
22
23#include "u_ether.h"
24#include "u_ether_configfs.h"
25#include "u_ncm.h"
26#include "configfs.h"
27
28
29
30
31
32
33
34
35
36
37
38
39#define NCM_NDP_HDR_CRC 0x01000000
40
41enum ncm_notify_state {
42 NCM_NOTIFY_NONE,
43 NCM_NOTIFY_CONNECT,
44 NCM_NOTIFY_SPEED,
45};
46
47struct f_ncm {
48 struct gether port;
49 u8 ctrl_id, data_id;
50
51 char ethaddr[14];
52
53 struct usb_ep *notify;
54 struct usb_request *notify_req;
55 u8 notify_state;
56 atomic_t notify_count;
57 bool is_open;
58
59 const struct ndp_parser_opts *parser_opts;
60 bool is_crc;
61 u32 ndp_sign;
62
63
64
65
66
67 spinlock_t lock;
68
69 struct net_device *netdev;
70
71
72 struct sk_buff *skb_tx_data;
73 struct sk_buff *skb_tx_ndp;
74 u16 ndp_dgram_count;
75 struct hrtimer task_timer;
76};
77
78static inline struct f_ncm *func_to_ncm(struct usb_function *f)
79{
80 return container_of(f, struct f_ncm, port.func);
81}
82
83
84static inline unsigned ncm_bitrate(struct usb_gadget *g)
85{
86 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER_PLUS)
87 return 4250000000U;
88 else if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
89 return 3750000000U;
90 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 return 13 * 512 * 8 * 1000 * 8;
92 else
93 return 19 * 64 * 1 * 1000 * 8;
94}
95
96
97
98
99
100
101
102
103
104#define NTB_DEFAULT_IN_SIZE 16384
105#define NTB_OUT_SIZE 16384
106
107
108
109
110
111
112
113
114#define TX_MAX_NUM_DPE 32
115
116
117#define TX_TIMEOUT_NSECS 300000
118
119#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
120 USB_CDC_NCM_NTB32_SUPPORTED)
121
122static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
123 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
124 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
125 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
126 .wNdpInDivisor = cpu_to_le16(4),
127 .wNdpInPayloadRemainder = cpu_to_le16(0),
128 .wNdpInAlignment = cpu_to_le16(4),
129
130 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
131 .wNdpOutDivisor = cpu_to_le16(4),
132 .wNdpOutPayloadRemainder = cpu_to_le16(0),
133 .wNdpOutAlignment = cpu_to_le16(4),
134};
135
136
137
138
139
140
141
142#define NCM_STATUS_INTERVAL_MS 32
143#define NCM_STATUS_BYTECOUNT 16
144
145static struct usb_interface_assoc_descriptor ncm_iad_desc = {
146 .bLength = sizeof ncm_iad_desc,
147 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
148
149
150 .bInterfaceCount = 2,
151 .bFunctionClass = USB_CLASS_COMM,
152 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
153 .bFunctionProtocol = USB_CDC_PROTO_NONE,
154
155};
156
157
158
159static struct usb_interface_descriptor ncm_control_intf = {
160 .bLength = sizeof ncm_control_intf,
161 .bDescriptorType = USB_DT_INTERFACE,
162
163
164 .bNumEndpoints = 1,
165 .bInterfaceClass = USB_CLASS_COMM,
166 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
167 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
168
169};
170
171static struct usb_cdc_header_desc ncm_header_desc = {
172 .bLength = sizeof ncm_header_desc,
173 .bDescriptorType = USB_DT_CS_INTERFACE,
174 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
175
176 .bcdCDC = cpu_to_le16(0x0110),
177};
178
179static struct usb_cdc_union_desc ncm_union_desc = {
180 .bLength = sizeof(ncm_union_desc),
181 .bDescriptorType = USB_DT_CS_INTERFACE,
182 .bDescriptorSubType = USB_CDC_UNION_TYPE,
183
184
185};
186
187static struct usb_cdc_ether_desc ecm_desc = {
188 .bLength = sizeof ecm_desc,
189 .bDescriptorType = USB_DT_CS_INTERFACE,
190 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
191
192
193
194 .bmEthernetStatistics = cpu_to_le32(0),
195 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
196 .wNumberMCFilters = cpu_to_le16(0),
197 .bNumberPowerFilters = 0,
198};
199
200#define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
201
202static struct usb_cdc_ncm_desc ncm_desc = {
203 .bLength = sizeof ncm_desc,
204 .bDescriptorType = USB_DT_CS_INTERFACE,
205 .bDescriptorSubType = USB_CDC_NCM_TYPE,
206
207 .bcdNcmVersion = cpu_to_le16(0x0100),
208
209 .bmNetworkCapabilities = NCAPS,
210};
211
212
213
214static struct usb_interface_descriptor ncm_data_nop_intf = {
215 .bLength = sizeof ncm_data_nop_intf,
216 .bDescriptorType = USB_DT_INTERFACE,
217
218 .bInterfaceNumber = 1,
219 .bAlternateSetting = 0,
220 .bNumEndpoints = 0,
221 .bInterfaceClass = USB_CLASS_CDC_DATA,
222 .bInterfaceSubClass = 0,
223 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
224
225};
226
227
228
229static struct usb_interface_descriptor ncm_data_intf = {
230 .bLength = sizeof ncm_data_intf,
231 .bDescriptorType = USB_DT_INTERFACE,
232
233 .bInterfaceNumber = 1,
234 .bAlternateSetting = 1,
235 .bNumEndpoints = 2,
236 .bInterfaceClass = USB_CLASS_CDC_DATA,
237 .bInterfaceSubClass = 0,
238 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
239
240};
241
242
243
244static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
247
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_INT,
250 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
251 .bInterval = NCM_STATUS_INTERVAL_MS,
252};
253
254static struct usb_endpoint_descriptor fs_ncm_in_desc = {
255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
257
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
260};
261
262static struct usb_endpoint_descriptor fs_ncm_out_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
265
266 .bEndpointAddress = USB_DIR_OUT,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK,
268};
269
270static struct usb_descriptor_header *ncm_fs_function[] = {
271 (struct usb_descriptor_header *) &ncm_iad_desc,
272
273 (struct usb_descriptor_header *) &ncm_control_intf,
274 (struct usb_descriptor_header *) &ncm_header_desc,
275 (struct usb_descriptor_header *) &ncm_union_desc,
276 (struct usb_descriptor_header *) &ecm_desc,
277 (struct usb_descriptor_header *) &ncm_desc,
278 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
279
280 (struct usb_descriptor_header *) &ncm_data_nop_intf,
281 (struct usb_descriptor_header *) &ncm_data_intf,
282 (struct usb_descriptor_header *) &fs_ncm_in_desc,
283 (struct usb_descriptor_header *) &fs_ncm_out_desc,
284 NULL,
285};
286
287
288
289static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
290 .bLength = USB_DT_ENDPOINT_SIZE,
291 .bDescriptorType = USB_DT_ENDPOINT,
292
293 .bEndpointAddress = USB_DIR_IN,
294 .bmAttributes = USB_ENDPOINT_XFER_INT,
295 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
296 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
297};
298static struct usb_endpoint_descriptor hs_ncm_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
301
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(512),
305};
306
307static struct usb_endpoint_descriptor hs_ncm_out_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
310
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(512),
314};
315
316static struct usb_descriptor_header *ncm_hs_function[] = {
317 (struct usb_descriptor_header *) &ncm_iad_desc,
318
319 (struct usb_descriptor_header *) &ncm_control_intf,
320 (struct usb_descriptor_header *) &ncm_header_desc,
321 (struct usb_descriptor_header *) &ncm_union_desc,
322 (struct usb_descriptor_header *) &ecm_desc,
323 (struct usb_descriptor_header *) &ncm_desc,
324 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
325
326 (struct usb_descriptor_header *) &ncm_data_nop_intf,
327 (struct usb_descriptor_header *) &ncm_data_intf,
328 (struct usb_descriptor_header *) &hs_ncm_in_desc,
329 (struct usb_descriptor_header *) &hs_ncm_out_desc,
330 NULL,
331};
332
333
334
335
336static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
337 .bLength = USB_DT_ENDPOINT_SIZE,
338 .bDescriptorType = USB_DT_ENDPOINT,
339
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
343 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
344};
345
346static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
347 .bLength = sizeof(ss_ncm_notify_comp_desc),
348 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
349
350
351
352
353 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
354};
355
356static struct usb_endpoint_descriptor ss_ncm_in_desc = {
357 .bLength = USB_DT_ENDPOINT_SIZE,
358 .bDescriptorType = USB_DT_ENDPOINT,
359
360 .bEndpointAddress = USB_DIR_IN,
361 .bmAttributes = USB_ENDPOINT_XFER_BULK,
362 .wMaxPacketSize = cpu_to_le16(1024),
363};
364
365static struct usb_endpoint_descriptor ss_ncm_out_desc = {
366 .bLength = USB_DT_ENDPOINT_SIZE,
367 .bDescriptorType = USB_DT_ENDPOINT,
368
369 .bEndpointAddress = USB_DIR_OUT,
370 .bmAttributes = USB_ENDPOINT_XFER_BULK,
371 .wMaxPacketSize = cpu_to_le16(1024),
372};
373
374static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
375 .bLength = sizeof(ss_ncm_bulk_comp_desc),
376 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
377
378
379 .bMaxBurst = 15,
380
381};
382
383static struct usb_descriptor_header *ncm_ss_function[] = {
384 (struct usb_descriptor_header *) &ncm_iad_desc,
385
386 (struct usb_descriptor_header *) &ncm_control_intf,
387 (struct usb_descriptor_header *) &ncm_header_desc,
388 (struct usb_descriptor_header *) &ncm_union_desc,
389 (struct usb_descriptor_header *) &ecm_desc,
390 (struct usb_descriptor_header *) &ncm_desc,
391 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
392 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
393
394 (struct usb_descriptor_header *) &ncm_data_nop_intf,
395 (struct usb_descriptor_header *) &ncm_data_intf,
396 (struct usb_descriptor_header *) &ss_ncm_in_desc,
397 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
398 (struct usb_descriptor_header *) &ss_ncm_out_desc,
399 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
400 NULL,
401};
402
403
404
405#define STRING_CTRL_IDX 0
406#define STRING_MAC_IDX 1
407#define STRING_DATA_IDX 2
408#define STRING_IAD_IDX 3
409
410static struct usb_string ncm_string_defs[] = {
411 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
412 [STRING_MAC_IDX].s = "",
413 [STRING_DATA_IDX].s = "CDC Network Data",
414 [STRING_IAD_IDX].s = "CDC NCM",
415 { }
416};
417
418static struct usb_gadget_strings ncm_string_table = {
419 .language = 0x0409,
420 .strings = ncm_string_defs,
421};
422
423static struct usb_gadget_strings *ncm_strings[] = {
424 &ncm_string_table,
425 NULL,
426};
427
428
429
430
431
432
433
434
435
436
437struct ndp_parser_opts {
438 u32 nth_sign;
439 u32 ndp_sign;
440 unsigned nth_size;
441 unsigned ndp_size;
442 unsigned dpe_size;
443 unsigned ndplen_align;
444
445 unsigned dgram_item_len;
446 unsigned block_length;
447 unsigned ndp_index;
448 unsigned reserved1;
449 unsigned reserved2;
450 unsigned next_ndp_index;
451};
452
453#define INIT_NDP16_OPTS { \
454 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
455 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
456 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
457 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
458 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
459 .ndplen_align = 4, \
460 .dgram_item_len = 1, \
461 .block_length = 1, \
462 .ndp_index = 1, \
463 .reserved1 = 0, \
464 .reserved2 = 0, \
465 .next_ndp_index = 1, \
466 }
467
468
469#define INIT_NDP32_OPTS { \
470 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
471 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
472 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
473 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
474 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
475 .ndplen_align = 8, \
476 .dgram_item_len = 2, \
477 .block_length = 2, \
478 .ndp_index = 2, \
479 .reserved1 = 1, \
480 .reserved2 = 2, \
481 .next_ndp_index = 2, \
482 }
483
484static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
485static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
486
487static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
488{
489 switch (size) {
490 case 1:
491 put_unaligned_le16((u16)val, *p);
492 break;
493 case 2:
494 put_unaligned_le32((u32)val, *p);
495
496 break;
497 default:
498 BUG();
499 }
500
501 *p += size;
502}
503
504static inline unsigned get_ncm(__le16 **p, unsigned size)
505{
506 unsigned tmp;
507
508 switch (size) {
509 case 1:
510 tmp = get_unaligned_le16(*p);
511 break;
512 case 2:
513 tmp = get_unaligned_le32(*p);
514 break;
515 default:
516 BUG();
517 }
518
519 *p += size;
520 return tmp;
521}
522
523
524
525static inline void ncm_reset_values(struct f_ncm *ncm)
526{
527 ncm->parser_opts = &ndp16_opts;
528 ncm->is_crc = false;
529 ncm->ndp_sign = ncm->parser_opts->ndp_sign;
530 ncm->port.cdc_filter = DEFAULT_FILTER;
531
532
533 ncm->port.header_len = 0;
534
535 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
536 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
537}
538
539
540
541
542static void ncm_do_notify(struct f_ncm *ncm)
543{
544 struct usb_request *req = ncm->notify_req;
545 struct usb_cdc_notification *event;
546 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
547 __le32 *data;
548 int status;
549
550
551 if (atomic_read(&ncm->notify_count))
552 return;
553
554 event = req->buf;
555 switch (ncm->notify_state) {
556 case NCM_NOTIFY_NONE:
557 return;
558
559 case NCM_NOTIFY_CONNECT:
560 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
561 if (ncm->is_open)
562 event->wValue = cpu_to_le16(1);
563 else
564 event->wValue = cpu_to_le16(0);
565 event->wLength = 0;
566 req->length = sizeof *event;
567
568 DBG(cdev, "notify connect %s\n",
569 ncm->is_open ? "true" : "false");
570 ncm->notify_state = NCM_NOTIFY_NONE;
571 break;
572
573 case NCM_NOTIFY_SPEED:
574 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
575 event->wValue = cpu_to_le16(0);
576 event->wLength = cpu_to_le16(8);
577 req->length = NCM_STATUS_BYTECOUNT;
578
579
580 data = req->buf + sizeof *event;
581 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
582 data[1] = data[0];
583
584 DBG(cdev, "notify speed %u\n", ncm_bitrate(cdev->gadget));
585 ncm->notify_state = NCM_NOTIFY_CONNECT;
586 break;
587 }
588 event->bmRequestType = 0xA1;
589 event->wIndex = cpu_to_le16(ncm->ctrl_id);
590
591 atomic_inc(&ncm->notify_count);
592
593
594
595
596
597
598 spin_unlock(&ncm->lock);
599 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
600 spin_lock(&ncm->lock);
601 if (status < 0) {
602 atomic_dec(&ncm->notify_count);
603 DBG(cdev, "notify --> %d\n", status);
604 }
605}
606
607
608
609
610static void ncm_notify(struct f_ncm *ncm)
611{
612
613
614
615
616
617
618
619
620
621
622 ncm->notify_state = NCM_NOTIFY_SPEED;
623 ncm_do_notify(ncm);
624}
625
626static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
627{
628 struct f_ncm *ncm = req->context;
629 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
630 struct usb_cdc_notification *event = req->buf;
631
632 spin_lock(&ncm->lock);
633 switch (req->status) {
634 case 0:
635 VDBG(cdev, "Notification %02x sent\n",
636 event->bNotificationType);
637 atomic_dec(&ncm->notify_count);
638 break;
639 case -ECONNRESET:
640 case -ESHUTDOWN:
641 atomic_set(&ncm->notify_count, 0);
642 ncm->notify_state = NCM_NOTIFY_NONE;
643 break;
644 default:
645 DBG(cdev, "event %02x --> %d\n",
646 event->bNotificationType, req->status);
647 atomic_dec(&ncm->notify_count);
648 break;
649 }
650 ncm_do_notify(ncm);
651 spin_unlock(&ncm->lock);
652}
653
654static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
655{
656
657 unsigned in_size;
658 struct usb_function *f = req->context;
659 struct f_ncm *ncm = func_to_ncm(f);
660 struct usb_composite_dev *cdev = f->config->cdev;
661
662 req->context = NULL;
663 if (req->status || req->actual != req->length) {
664 DBG(cdev, "Bad control-OUT transfer\n");
665 goto invalid;
666 }
667
668 in_size = get_unaligned_le32(req->buf);
669 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
670 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
671 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
672 goto invalid;
673 }
674
675 ncm->port.fixed_in_len = in_size;
676 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
677 return;
678
679invalid:
680 usb_ep_set_halt(ep);
681 return;
682}
683
684static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
685{
686 struct f_ncm *ncm = func_to_ncm(f);
687 struct usb_composite_dev *cdev = f->config->cdev;
688 struct usb_request *req = cdev->req;
689 int value = -EOPNOTSUPP;
690 u16 w_index = le16_to_cpu(ctrl->wIndex);
691 u16 w_value = le16_to_cpu(ctrl->wValue);
692 u16 w_length = le16_to_cpu(ctrl->wLength);
693
694
695
696
697
698 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
699 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
700 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
701
702
703
704
705 if (w_length != 0 || w_index != ncm->ctrl_id)
706 goto invalid;
707 DBG(cdev, "packet filter %02x\n", w_value);
708
709
710
711
712
713 ncm->port.cdc_filter = w_value;
714 value = 0;
715 break;
716
717
718
719
720
721
722
723
724
725
726 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
727 | USB_CDC_GET_NTB_PARAMETERS:
728
729 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
730 goto invalid;
731 value = w_length > sizeof ntb_parameters ?
732 sizeof ntb_parameters : w_length;
733 memcpy(req->buf, &ntb_parameters, value);
734 VDBG(cdev, "Host asked NTB parameters\n");
735 break;
736
737 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
738 | USB_CDC_GET_NTB_INPUT_SIZE:
739
740 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
741 goto invalid;
742 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
743 value = 4;
744 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
745 ncm->port.fixed_in_len);
746 break;
747
748 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
749 | USB_CDC_SET_NTB_INPUT_SIZE:
750 {
751 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
752 goto invalid;
753 req->complete = ncm_ep0out_complete;
754 req->length = w_length;
755 req->context = f;
756
757 value = req->length;
758 break;
759 }
760
761 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
762 | USB_CDC_GET_NTB_FORMAT:
763 {
764 uint16_t format;
765
766 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
767 goto invalid;
768 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
769 put_unaligned_le16(format, req->buf);
770 value = 2;
771 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
772 break;
773 }
774
775 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
776 | USB_CDC_SET_NTB_FORMAT:
777 {
778 if (w_length != 0 || w_index != ncm->ctrl_id)
779 goto invalid;
780 switch (w_value) {
781 case 0x0000:
782 ncm->parser_opts = &ndp16_opts;
783 DBG(cdev, "NCM16 selected\n");
784 break;
785 case 0x0001:
786 ncm->parser_opts = &ndp32_opts;
787 DBG(cdev, "NCM32 selected\n");
788 break;
789 default:
790 goto invalid;
791 }
792 value = 0;
793 break;
794 }
795 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
796 | USB_CDC_GET_CRC_MODE:
797 {
798 uint16_t is_crc;
799
800 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
801 goto invalid;
802 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
803 put_unaligned_le16(is_crc, req->buf);
804 value = 2;
805 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
806 break;
807 }
808
809 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
810 | USB_CDC_SET_CRC_MODE:
811 {
812 if (w_length != 0 || w_index != ncm->ctrl_id)
813 goto invalid;
814 switch (w_value) {
815 case 0x0000:
816 ncm->is_crc = false;
817 DBG(cdev, "non-CRC mode selected\n");
818 break;
819 case 0x0001:
820 ncm->is_crc = true;
821 DBG(cdev, "CRC mode selected\n");
822 break;
823 default:
824 goto invalid;
825 }
826 value = 0;
827 break;
828 }
829
830
831
832
833
834
835
836 default:
837invalid:
838 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
839 ctrl->bRequestType, ctrl->bRequest,
840 w_value, w_index, w_length);
841 }
842 ncm->ndp_sign = ncm->parser_opts->ndp_sign |
843 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0);
844
845
846 if (value >= 0) {
847 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
848 ctrl->bRequestType, ctrl->bRequest,
849 w_value, w_index, w_length);
850 req->zero = 0;
851 req->length = value;
852 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
853 if (value < 0)
854 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
855 ctrl->bRequestType, ctrl->bRequest,
856 value);
857 }
858
859
860 return value;
861}
862
863
864static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
865{
866 struct f_ncm *ncm = func_to_ncm(f);
867 struct usb_composite_dev *cdev = f->config->cdev;
868
869
870 if (intf == ncm->ctrl_id) {
871 if (alt != 0)
872 goto fail;
873
874 DBG(cdev, "reset ncm control %d\n", intf);
875 usb_ep_disable(ncm->notify);
876
877 if (!(ncm->notify->desc)) {
878 DBG(cdev, "init ncm ctrl %d\n", intf);
879 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
880 goto fail;
881 }
882 usb_ep_enable(ncm->notify);
883
884
885 } else if (intf == ncm->data_id) {
886 if (alt > 1)
887 goto fail;
888
889 if (ncm->port.in_ep->enabled) {
890 DBG(cdev, "reset ncm\n");
891 ncm->netdev = NULL;
892 gether_disconnect(&ncm->port);
893 ncm_reset_values(ncm);
894 }
895
896
897
898
899
900 if (alt == 1) {
901 struct net_device *net;
902
903 if (!ncm->port.in_ep->desc ||
904 !ncm->port.out_ep->desc) {
905 DBG(cdev, "init ncm\n");
906 if (config_ep_by_speed(cdev->gadget, f,
907 ncm->port.in_ep) ||
908 config_ep_by_speed(cdev->gadget, f,
909 ncm->port.out_ep)) {
910 ncm->port.in_ep->desc = NULL;
911 ncm->port.out_ep->desc = NULL;
912 goto fail;
913 }
914 }
915
916
917
918
919
920 ncm->port.is_zlp_ok =
921 gadget_is_zlp_supported(cdev->gadget);
922 ncm->port.cdc_filter = DEFAULT_FILTER;
923 DBG(cdev, "activate ncm\n");
924 net = gether_connect(&ncm->port);
925 if (IS_ERR(net))
926 return PTR_ERR(net);
927 ncm->netdev = net;
928 }
929
930 spin_lock(&ncm->lock);
931 ncm_notify(ncm);
932 spin_unlock(&ncm->lock);
933 } else
934 goto fail;
935
936 return 0;
937fail:
938 return -EINVAL;
939}
940
941
942
943
944
945static int ncm_get_alt(struct usb_function *f, unsigned intf)
946{
947 struct f_ncm *ncm = func_to_ncm(f);
948
949 if (intf == ncm->ctrl_id)
950 return 0;
951 return ncm->port.in_ep->enabled ? 1 : 0;
952}
953
954static struct sk_buff *package_for_tx(struct f_ncm *ncm)
955{
956 __le16 *ntb_iter;
957 struct sk_buff *skb2 = NULL;
958 unsigned ndp_pad;
959 unsigned ndp_index;
960 unsigned new_len;
961
962 const struct ndp_parser_opts *opts = ncm->parser_opts;
963 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
964 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
965
966
967 hrtimer_try_to_cancel(&ncm->task_timer);
968
969 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
970 ncm->skb_tx_data->len;
971 ndp_index = ncm->skb_tx_data->len + ndp_pad;
972 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
973
974
975 ntb_iter = (void *) ncm->skb_tx_data->data;
976
977 ntb_iter += 2 + 1 + 1;
978 put_ncm(&ntb_iter, opts->block_length, new_len);
979 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
980
981
982 new_len = opts->ndp_size +
983 (ncm->ndp_dgram_count * dgram_idx_len);
984 ncm->ndp_dgram_count = 0;
985
986 ntb_iter = (void *) ncm->skb_tx_ndp->data;
987 ntb_iter += 2;
988 put_unaligned_le16(new_len, ntb_iter);
989
990
991 swap(skb2, ncm->skb_tx_data);
992 if (ncm->skb_tx_data) {
993 dev_consume_skb_any(ncm->skb_tx_data);
994 ncm->skb_tx_data = NULL;
995 }
996
997
998 skb_put_zero(skb2, ndp_pad);
999
1000
1001 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1002 dev_consume_skb_any(ncm->skb_tx_ndp);
1003 ncm->skb_tx_ndp = NULL;
1004
1005
1006 skb_put_zero(skb2, dgram_idx_len);
1007
1008 return skb2;
1009}
1010
1011static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1012 struct sk_buff *skb)
1013{
1014 struct f_ncm *ncm = func_to_ncm(&port->func);
1015 struct sk_buff *skb2 = NULL;
1016
1017 if (skb) {
1018 int ncb_len = 0;
1019 __le16 *ntb_data;
1020 __le16 *ntb_ndp;
1021 int dgram_pad;
1022
1023 unsigned max_size = ncm->port.fixed_in_len;
1024 const struct ndp_parser_opts *opts = ncm->parser_opts;
1025 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1026 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1027 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1028 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1029
1030
1031 if (ncm->is_crc) {
1032 uint32_t crc;
1033 __le16 *crc_pos;
1034
1035 crc = ~crc32_le(~0,
1036 skb->data,
1037 skb->len);
1038 crc_pos = skb_put(skb, sizeof(uint32_t));
1039 put_unaligned_le32(crc, crc_pos);
1040 }
1041
1042
1043
1044
1045
1046
1047 if (ncm->skb_tx_data
1048 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1049 || (ncm->skb_tx_data->len +
1050 div + rem + skb->len +
1051 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1052 > max_size)) {
1053 skb2 = package_for_tx(ncm);
1054 if (!skb2)
1055 goto err;
1056 }
1057
1058 if (!ncm->skb_tx_data) {
1059 ncb_len = opts->nth_size;
1060 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1061 ncb_len += dgram_pad;
1062
1063
1064 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1065 if (!ncm->skb_tx_data)
1066 goto err;
1067
1068 ncm->skb_tx_data->dev = ncm->netdev;
1069 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
1070
1071 put_unaligned_le32(opts->nth_sign, ntb_data);
1072 ntb_data += 2;
1073
1074 put_unaligned_le16(opts->nth_size, ntb_data++);
1075
1076
1077
1078
1079
1080 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1081 + opts->dpe_size
1082 * TX_MAX_NUM_DPE),
1083 GFP_ATOMIC);
1084 if (!ncm->skb_tx_ndp)
1085 goto err;
1086
1087 ncm->skb_tx_ndp->dev = ncm->netdev;
1088 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
1089 memset(ntb_ndp, 0, ncb_len);
1090
1091 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1092 ntb_ndp += 2;
1093
1094
1095 ncm->ndp_dgram_count = 1;
1096
1097
1098
1099
1100 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1101 HRTIMER_MODE_REL_SOFT);
1102 }
1103
1104
1105 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
1106
1107 ncb_len = ncm->skb_tx_data->len;
1108 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1109 ncb_len += dgram_pad;
1110
1111
1112 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1113
1114 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1115 ncm->ndp_dgram_count++;
1116
1117
1118 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1119 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
1120 dev_consume_skb_any(skb);
1121 skb = NULL;
1122
1123 } else if (ncm->skb_tx_data) {
1124
1125
1126
1127
1128 skb2 = package_for_tx(ncm);
1129 if (!skb2)
1130 goto err;
1131 }
1132
1133 return skb2;
1134
1135err:
1136 ncm->netdev->stats.tx_dropped++;
1137
1138 if (skb)
1139 dev_kfree_skb_any(skb);
1140 if (ncm->skb_tx_data)
1141 dev_kfree_skb_any(ncm->skb_tx_data);
1142 if (ncm->skb_tx_ndp)
1143 dev_kfree_skb_any(ncm->skb_tx_ndp);
1144
1145 return NULL;
1146}
1147
1148
1149
1150
1151
1152static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1153{
1154 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1155 struct net_device *netdev = READ_ONCE(ncm->netdev);
1156
1157 if (netdev) {
1158
1159
1160
1161
1162
1163
1164
1165
1166 netdev->netdev_ops->ndo_start_xmit(NULL, netdev);
1167 }
1168 return HRTIMER_NORESTART;
1169}
1170
1171static int ncm_unwrap_ntb(struct gether *port,
1172 struct sk_buff *skb,
1173 struct sk_buff_head *list)
1174{
1175 struct f_ncm *ncm = func_to_ncm(&port->func);
1176 __le16 *tmp = (void *) skb->data;
1177 unsigned index, index2;
1178 int ndp_index;
1179 unsigned dg_len, dg_len2;
1180 unsigned ndp_len;
1181 unsigned block_len;
1182 struct sk_buff *skb2;
1183 int ret = -EINVAL;
1184 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1185 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
1186 const struct ndp_parser_opts *opts = ncm->parser_opts;
1187 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1188 int dgram_counter;
1189
1190
1191 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1192 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1193 skb->len);
1194 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1195 skb->data, 32, false);
1196
1197 goto err;
1198 }
1199 tmp += 2;
1200
1201 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1202 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1203 goto err;
1204 }
1205 tmp++;
1206
1207 block_len = get_ncm(&tmp, opts->block_length);
1208
1209 if (block_len > ntb_max) {
1210 INFO(port->func.config->cdev, "OUT size exceeded\n");
1211 goto err;
1212 }
1213
1214 ndp_index = get_ncm(&tmp, opts->ndp_index);
1215
1216
1217 do {
1218
1219
1220
1221
1222 if (((ndp_index % 4) != 0) ||
1223 (ndp_index < opts->nth_size) ||
1224 (ndp_index > (block_len -
1225 opts->ndp_size))) {
1226 INFO(port->func.config->cdev, "Bad index: %#X\n",
1227 ndp_index);
1228 goto err;
1229 }
1230
1231
1232
1233
1234
1235 tmp = (void *)(skb->data + ndp_index);
1236 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1237 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1238 goto err;
1239 }
1240 tmp += 2;
1241
1242 ndp_len = get_unaligned_le16(tmp++);
1243
1244
1245
1246
1247
1248
1249
1250
1251 if ((ndp_len < opts->ndp_size
1252 + 2 * 2 * (opts->dgram_item_len * 2)) ||
1253 (ndp_len % opts->ndplen_align != 0)) {
1254 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1255 ndp_len);
1256 goto err;
1257 }
1258 tmp += opts->reserved1;
1259
1260 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1261 tmp += opts->reserved2;
1262
1263 ndp_len -= opts->ndp_size;
1264 index2 = get_ncm(&tmp, opts->dgram_item_len);
1265 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1266 dgram_counter = 0;
1267
1268 do {
1269 index = index2;
1270
1271 if ((index < opts->nth_size) ||
1272 (index > block_len - opts->dpe_size)) {
1273 INFO(port->func.config->cdev,
1274 "Bad index: %#X\n", index);
1275 goto err;
1276 }
1277
1278 dg_len = dg_len2;
1279
1280
1281
1282
1283 if ((dg_len < 14 + crc_len) ||
1284 (dg_len > frame_max)) {
1285 INFO(port->func.config->cdev,
1286 "Bad dgram length: %#X\n", dg_len);
1287 goto err;
1288 }
1289 if (ncm->is_crc) {
1290 uint32_t crc, crc2;
1291
1292 crc = get_unaligned_le32(skb->data +
1293 index + dg_len -
1294 crc_len);
1295 crc2 = ~crc32_le(~0,
1296 skb->data + index,
1297 dg_len - crc_len);
1298 if (crc != crc2) {
1299 INFO(port->func.config->cdev,
1300 "Bad CRC\n");
1301 goto err;
1302 }
1303 }
1304
1305 index2 = get_ncm(&tmp, opts->dgram_item_len);
1306 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1307
1308
1309 if (index2 > block_len - opts->dpe_size) {
1310 INFO(port->func.config->cdev,
1311 "Bad index: %#X\n", index2);
1312 goto err;
1313 }
1314
1315
1316
1317
1318
1319 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1320 dg_len - crc_len);
1321 if (skb2 == NULL)
1322 goto err;
1323 skb_put_data(skb2, skb->data + index,
1324 dg_len - crc_len);
1325
1326 skb_queue_tail(list, skb2);
1327
1328 ndp_len -= 2 * (opts->dgram_item_len * 2);
1329
1330 dgram_counter++;
1331 if (index2 == 0 || dg_len2 == 0)
1332 break;
1333 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1334 } while (ndp_index);
1335
1336 dev_consume_skb_any(skb);
1337
1338 VDBG(port->func.config->cdev,
1339 "Parsed NTB with %d frames\n", dgram_counter);
1340 return 0;
1341err:
1342 skb_queue_purge(list);
1343 dev_kfree_skb_any(skb);
1344 return ret;
1345}
1346
1347static void ncm_disable(struct usb_function *f)
1348{
1349 struct f_ncm *ncm = func_to_ncm(f);
1350 struct usb_composite_dev *cdev = f->config->cdev;
1351
1352 DBG(cdev, "ncm deactivated\n");
1353
1354 if (ncm->port.in_ep->enabled) {
1355 ncm->netdev = NULL;
1356 gether_disconnect(&ncm->port);
1357 }
1358
1359 if (ncm->notify->enabled) {
1360 usb_ep_disable(ncm->notify);
1361 ncm->notify->desc = NULL;
1362 }
1363}
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385static void ncm_open(struct gether *geth)
1386{
1387 struct f_ncm *ncm = func_to_ncm(&geth->func);
1388
1389 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1390
1391 spin_lock(&ncm->lock);
1392 ncm->is_open = true;
1393 ncm_notify(ncm);
1394 spin_unlock(&ncm->lock);
1395}
1396
1397static void ncm_close(struct gether *geth)
1398{
1399 struct f_ncm *ncm = func_to_ncm(&geth->func);
1400
1401 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1402
1403 spin_lock(&ncm->lock);
1404 ncm->is_open = false;
1405 ncm_notify(ncm);
1406 spin_unlock(&ncm->lock);
1407}
1408
1409
1410
1411
1412
1413static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1414{
1415 struct usb_composite_dev *cdev = c->cdev;
1416 struct f_ncm *ncm = func_to_ncm(f);
1417 struct usb_string *us;
1418 int status;
1419 struct usb_ep *ep;
1420 struct f_ncm_opts *ncm_opts;
1421
1422 if (!can_support_ecm(cdev->gadget))
1423 return -EINVAL;
1424
1425 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1426
1427 if (cdev->use_os_string) {
1428 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
1429 GFP_KERNEL);
1430 if (!f->os_desc_table)
1431 return -ENOMEM;
1432 f->os_desc_n = 1;
1433 f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc;
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443 if (!ncm_opts->bound) {
1444 mutex_lock(&ncm_opts->lock);
1445 gether_set_gadget(ncm_opts->net, cdev->gadget);
1446 status = gether_register_netdev(ncm_opts->net);
1447 mutex_unlock(&ncm_opts->lock);
1448 if (status)
1449 goto fail;
1450 ncm_opts->bound = true;
1451 }
1452 us = usb_gstrings_attach(cdev, ncm_strings,
1453 ARRAY_SIZE(ncm_string_defs));
1454 if (IS_ERR(us)) {
1455 status = PTR_ERR(us);
1456 goto fail;
1457 }
1458 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1459 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1460 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1461 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1462 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1463
1464
1465 status = usb_interface_id(c, f);
1466 if (status < 0)
1467 goto fail;
1468 ncm->ctrl_id = status;
1469 ncm_iad_desc.bFirstInterface = status;
1470
1471 ncm_control_intf.bInterfaceNumber = status;
1472 ncm_union_desc.bMasterInterface0 = status;
1473
1474 if (cdev->use_os_string)
1475 f->os_desc_table[0].if_id =
1476 ncm_iad_desc.bFirstInterface;
1477
1478 status = usb_interface_id(c, f);
1479 if (status < 0)
1480 goto fail;
1481 ncm->data_id = status;
1482
1483 ncm_data_nop_intf.bInterfaceNumber = status;
1484 ncm_data_intf.bInterfaceNumber = status;
1485 ncm_union_desc.bSlaveInterface0 = status;
1486
1487 status = -ENODEV;
1488
1489
1490 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1491 if (!ep)
1492 goto fail;
1493 ncm->port.in_ep = ep;
1494
1495 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1496 if (!ep)
1497 goto fail;
1498 ncm->port.out_ep = ep;
1499
1500 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1501 if (!ep)
1502 goto fail;
1503 ncm->notify = ep;
1504
1505 status = -ENOMEM;
1506
1507
1508 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1509 if (!ncm->notify_req)
1510 goto fail;
1511 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1512 if (!ncm->notify_req->buf)
1513 goto fail;
1514 ncm->notify_req->context = ncm;
1515 ncm->notify_req->complete = ncm_notify_complete;
1516
1517
1518
1519
1520
1521
1522 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1523 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1524 hs_ncm_notify_desc.bEndpointAddress =
1525 fs_ncm_notify_desc.bEndpointAddress;
1526
1527 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1528 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1529 ss_ncm_notify_desc.bEndpointAddress =
1530 fs_ncm_notify_desc.bEndpointAddress;
1531
1532 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1533 ncm_ss_function, ncm_ss_function);
1534 if (status)
1535 goto fail;
1536
1537
1538
1539
1540
1541
1542
1543 ncm->port.open = ncm_open;
1544 ncm->port.close = ncm_close;
1545
1546 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1547 ncm->task_timer.function = ncm_tx_timeout;
1548
1549 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1550 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1551 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1552 ncm->port.in_ep->name, ncm->port.out_ep->name,
1553 ncm->notify->name);
1554 return 0;
1555
1556fail:
1557 kfree(f->os_desc_table);
1558 f->os_desc_n = 0;
1559
1560 if (ncm->notify_req) {
1561 kfree(ncm->notify_req->buf);
1562 usb_ep_free_request(ncm->notify, ncm->notify_req);
1563 }
1564
1565 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1566
1567 return status;
1568}
1569
1570static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1571{
1572 return container_of(to_config_group(item), struct f_ncm_opts,
1573 func_inst.group);
1574}
1575
1576
1577USB_ETHERNET_CONFIGFS_ITEM(ncm);
1578
1579
1580USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1581
1582
1583USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1584
1585
1586USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1587
1588
1589USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1590
1591static struct configfs_attribute *ncm_attrs[] = {
1592 &ncm_opts_attr_dev_addr,
1593 &ncm_opts_attr_host_addr,
1594 &ncm_opts_attr_qmult,
1595 &ncm_opts_attr_ifname,
1596 NULL,
1597};
1598
1599static const struct config_item_type ncm_func_type = {
1600 .ct_item_ops = &ncm_item_ops,
1601 .ct_attrs = ncm_attrs,
1602 .ct_owner = THIS_MODULE,
1603};
1604
1605static void ncm_free_inst(struct usb_function_instance *f)
1606{
1607 struct f_ncm_opts *opts;
1608
1609 opts = container_of(f, struct f_ncm_opts, func_inst);
1610 if (opts->bound)
1611 gether_cleanup(netdev_priv(opts->net));
1612 else
1613 free_netdev(opts->net);
1614 kfree(opts->ncm_interf_group);
1615 kfree(opts);
1616}
1617
1618static struct usb_function_instance *ncm_alloc_inst(void)
1619{
1620 struct f_ncm_opts *opts;
1621 struct usb_os_desc *descs[1];
1622 char *names[1];
1623 struct config_group *ncm_interf_group;
1624
1625 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1626 if (!opts)
1627 return ERR_PTR(-ENOMEM);
1628 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
1629
1630 mutex_init(&opts->lock);
1631 opts->func_inst.free_func_inst = ncm_free_inst;
1632 opts->net = gether_setup_default();
1633 if (IS_ERR(opts->net)) {
1634 struct net_device *net = opts->net;
1635 kfree(opts);
1636 return ERR_CAST(net);
1637 }
1638 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
1639
1640 descs[0] = &opts->ncm_os_desc;
1641 names[0] = "ncm";
1642
1643 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1644 ncm_interf_group =
1645 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1646 names, THIS_MODULE);
1647 if (IS_ERR(ncm_interf_group)) {
1648 ncm_free_inst(&opts->func_inst);
1649 return ERR_CAST(ncm_interf_group);
1650 }
1651 opts->ncm_interf_group = ncm_interf_group;
1652
1653 return &opts->func_inst;
1654}
1655
1656static void ncm_free(struct usb_function *f)
1657{
1658 struct f_ncm *ncm;
1659 struct f_ncm_opts *opts;
1660
1661 ncm = func_to_ncm(f);
1662 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1663 kfree(ncm);
1664 mutex_lock(&opts->lock);
1665 opts->refcnt--;
1666 mutex_unlock(&opts->lock);
1667}
1668
1669static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1670{
1671 struct f_ncm *ncm = func_to_ncm(f);
1672
1673 DBG(c->cdev, "ncm unbind\n");
1674
1675 hrtimer_cancel(&ncm->task_timer);
1676
1677 kfree(f->os_desc_table);
1678 f->os_desc_n = 0;
1679
1680 ncm_string_defs[0].id = 0;
1681 usb_free_all_descriptors(f);
1682
1683 if (atomic_read(&ncm->notify_count)) {
1684 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1685 atomic_set(&ncm->notify_count, 0);
1686 }
1687
1688 kfree(ncm->notify_req->buf);
1689 usb_ep_free_request(ncm->notify, ncm->notify_req);
1690}
1691
1692static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1693{
1694 struct f_ncm *ncm;
1695 struct f_ncm_opts *opts;
1696 int status;
1697
1698
1699 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1700 if (!ncm)
1701 return ERR_PTR(-ENOMEM);
1702
1703 opts = container_of(fi, struct f_ncm_opts, func_inst);
1704 mutex_lock(&opts->lock);
1705 opts->refcnt++;
1706
1707
1708 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1709 sizeof(ncm->ethaddr));
1710 if (status < 12) {
1711 kfree(ncm);
1712 mutex_unlock(&opts->lock);
1713 return ERR_PTR(-EINVAL);
1714 }
1715 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1716
1717 spin_lock_init(&ncm->lock);
1718 ncm_reset_values(ncm);
1719 ncm->port.ioport = netdev_priv(opts->net);
1720 mutex_unlock(&opts->lock);
1721 ncm->port.is_fixed = true;
1722 ncm->port.supports_multi_frame = true;
1723
1724 ncm->port.func.name = "cdc_network";
1725
1726 ncm->port.func.bind = ncm_bind;
1727 ncm->port.func.unbind = ncm_unbind;
1728 ncm->port.func.set_alt = ncm_set_alt;
1729 ncm->port.func.get_alt = ncm_get_alt;
1730 ncm->port.func.setup = ncm_setup;
1731 ncm->port.func.disable = ncm_disable;
1732 ncm->port.func.free_func = ncm_free;
1733
1734 ncm->port.wrap = ncm_wrap_ntb;
1735 ncm->port.unwrap = ncm_unwrap_ntb;
1736
1737 return &ncm->port.func;
1738}
1739
1740DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1741MODULE_LICENSE("GPL");
1742MODULE_AUTHOR("Yauheni Kaliuta");
1743