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