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