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