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