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