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