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

source code of linux/drivers/usb/gadget/function/f_ncm.c