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