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
44 enum 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
50 struct 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
func_to_ncm(struct usb_function * f)81 static 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
122 static 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
145 static 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
159 static 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
171 static 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
179 static 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
187 static 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
201 static 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
213 static 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
228 static 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
243 static 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
253 static 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
261 static 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
269 static 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
288 static 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 };
297 static 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
306 static 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
315 static 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
335 static 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
345 static 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
355 static 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
364 static 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
373 static 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
382 static 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
409 static 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
417 static struct usb_gadget_strings ncm_string_table = {
418 .language = 0x0409, /* en-us */
419 .strings = ncm_string_defs,
420 };
421
422 static 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
436 struct 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
452 static 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
467 static 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
put_ncm(__le16 ** p,unsigned size,unsigned val)482 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
483 {
484 switch (size) {
485 case 1:
486 put_unaligned_le16((u16)val, *p);
487 break;
488 case 2:
489 put_unaligned_le32((u32)val, *p);
490
491 break;
492 default:
493 BUG();
494 }
495
496 *p += size;
497 }
498
get_ncm(__le16 ** p,unsigned size)499 static 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);
506 break;
507 case 2:
508 tmp = get_unaligned_le32(*p);
509 break;
510 default:
511 BUG();
512 }
513
514 *p += size;
515 return tmp;
516 }
517
518 /*-------------------------------------------------------------------------*/
519
ncm_reset_values(struct f_ncm * ncm)520 static 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 */
ncm_do_notify(struct f_ncm * ncm)537 static 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(&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(&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(&ncm->lock);
594 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
595 spin_lock(&ncm->lock);
596 if (status < 0) {
597 atomic_dec(&ncm->notify_count);
598 DBG(cdev, "notify --> %d\n", status);
599 }
600 }
601
602 /*
603 * Context: ncm->lock held
604 */
ncm_notify(struct f_ncm * ncm)605 static 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
ncm_notify_complete(struct usb_ep * ep,struct usb_request * req)621 static 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(&ncm->lock);
628 switch (req->status) {
629 case 0:
630 VDBG(cdev, "Notification %02x sent\n",
631 event->bNotificationType);
632 atomic_dec(&ncm->notify_count);
633 break;
634 case -ECONNRESET:
635 case -ESHUTDOWN:
636 atomic_set(&ncm->notify_count, 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(&ncm->notify_count);
643 break;
644 }
645 ncm_do_notify(ncm);
646 spin_unlock(&ncm->lock);
647 }
648
ncm_ep0out_complete(struct usb_ep * ep,struct usb_request * req)649 static 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(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
674 invalid:
675 usb_ep_set_halt(ep);
676 return;
677 }
678
ncm_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)679 static 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(ncm->port.fixed_in_len, 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(format, 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(is_crc, 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:
832 invalid:
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(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
ncm_set_alt(struct usb_function * f,unsigned intf,unsigned alt)859 static 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(ncm->notify);
871
872 if (!(ncm->notify->desc)) {
873 DBG(cdev, "init ncm ctrl %d\n", intf);
874 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
875 goto fail;
876 }
877 usb_ep_enable(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(cdev->gadget, f,
902 ncm->port.in_ep) ||
903 config_ep_by_speed(cdev->gadget, f,
904 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(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(net))
921 return PTR_ERR(net);
922 ncm->netdev = net;
923 }
924
925 spin_lock(&ncm->lock);
926 ncm_notify(ncm);
927 spin_unlock(&ncm->lock);
928 } else
929 goto fail;
930
931 return 0;
932 fail:
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 */
ncm_get_alt(struct usb_function * f,unsigned intf)940 static 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
package_for_tx(struct f_ncm * ncm)949 static 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(&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(&ntb_iter, opts->block_length, new_len);
974 put_ncm(&ntb_iter, opts->ndp_index, 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(new_len, 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(ncm->skb_tx_data);
989 ncm->skb_tx_data = NULL;
990 }
991
992 /* Insert NDP alignment. */
993 skb_put_zero(skb2, ndp_pad);
994
995 /* Copy NTB across. */
996 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
997 dev_consume_skb_any(ncm->skb_tx_ndp);
998 ncm->skb_tx_ndp = NULL;
999
1000 /* Insert zero'd datagram. */
1001 skb_put_zero(skb2, dgram_idx_len);
1002
1003 return skb2;
1004 }
1005
ncm_wrap_ntb(struct gether * port,struct sk_buff * skb)1006 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1007 struct sk_buff *skb)
1008 {
1009 struct f_ncm *ncm = func_to_ncm(&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(~0,
1031 skb->data,
1032 skb->len);
1033 crc_pos = skb_put(skb, sizeof(uint32_t));
1034 put_unaligned_le32(crc, 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(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(ncm->skb_tx_data, ncb_len);
1065 /* dwSignature */
1066 put_unaligned_le32(opts->nth_sign, ntb_data);
1067 ntb_data += 2;
1068 /* wHeaderLength */
1069 put_unaligned_le16(opts->nth_size, 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((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(ncm->skb_tx_ndp, opts->ndp_size);
1084 memset(ntb_ndp, 0, ncb_len);
1085 /* dwSignature */
1086 put_unaligned_le32(ncm->ndp_sign, 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(&ncm->task_timer, TX_TIMEOUT_NSECS,
1096 HRTIMER_MODE_REL_SOFT);
1097 }
1098
1099 /* Add the datagram position entries */
1100 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, 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(&ntb_ndp, opts->dgram_item_len, ncb_len);
1108 /* (d)wDatagramLength */
1109 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1110 ncm->ndp_dgram_count++;
1111
1112 /* Add the new data to the skb */
1113 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1114 skb_put_data(ncm->skb_tx_data, skb->data, 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
1130 err:
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(ncm->skb_tx_data);
1137 if (ncm->skb_tx_ndp)
1138 dev_kfree_skb_any(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 */
ncm_tx_timeout(struct hrtimer * data)1147 static 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
ncm_unwrap_ntb(struct gether * port,struct sk_buff * skb,struct sk_buff_head * list)1166 static 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(&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
1191 parse_ntb:
1192 tmp = (__le16 *)ntb_ptr;
1193
1194 /* dwSignature */
1195 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1196 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1197 skb->len);
1198 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1199 skb->data, 32, false);
1200
1201 goto err;
1202 }
1203 tmp += 2;
1204 /* wHeaderLength */
1205 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1206 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1207 goto err;
1208 }
1209 tmp++; /* skip wSequence */
1210
1211 block_len = get_ncm(&tmp, 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(&tmp, 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(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(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(&tmp, opts->next_ndp_index);
1265 tmp += opts->reserved2;
1266
1267 ndp_len -= opts->ndp_size;
1268 index2 = get_ncm(&tmp, opts->dgram_item_len);
1269 dg_len2 = get_ncm(&tmp, 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(ntb_ptr +
1297 index + dg_len -
1298 crc_len);
1299 crc2 = ~crc32_le(~0,
1300 ntb_ptr + index,
1301 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(&tmp, opts->dgram_item_len);
1310 dg_len2 = get_ncm(&tmp, 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(ncm->netdev,
1324 dg_len - crc_len);
1325 if (skb2 == NULL)
1326 goto err;
1327 skb_put_data(skb2, ntb_ptr + index,
1328 dg_len - crc_len);
1329
1330 skb_queue_tail(list, 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;
1360 err:
1361 skb_queue_purge(list);
1362 dev_kfree_skb_any(skb);
1363 return ret;
1364 }
1365
ncm_disable(struct usb_function * f)1366 static 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(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
ncm_open(struct gether * geth)1404 static void ncm_open(struct gether *geth)
1405 {
1406 struct f_ncm *ncm = func_to_ncm(&geth->func);
1407
1408 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1409
1410 spin_lock(&ncm->lock);
1411 ncm->is_open = true;
1412 ncm_notify(ncm);
1413 spin_unlock(&ncm->lock);
1414 }
1415
ncm_close(struct gether * geth)1416 static void ncm_close(struct gether *geth)
1417 {
1418 struct f_ncm *ncm = func_to_ncm(&geth->func);
1419
1420 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1421
1422 spin_lock(&ncm->lock);
1423 ncm->is_open = false;
1424 ncm_notify(ncm);
1425 spin_unlock(&ncm->lock);
1426 }
1427
1428 /*-------------------------------------------------------------------------*/
1429
1430 /* ethernet function driver setup/binding */
1431
ncm_bind(struct usb_configuration * c,struct usb_function * f)1432 static 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 net_device *net __free(detach_gadget) = NULL;
1443 struct usb_request *request __free(free_usb_request) = NULL;
1444
1445 if (!can_support_ecm(cdev->gadget))
1446 return -EINVAL;
1447
1448 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1449
1450 if (cdev->use_os_string) {
1451 os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);
1452 if (!os_desc_table)
1453 return -ENOMEM;
1454 }
1455
1456 scoped_guard(mutex, &ncm_opts->lock)
1457 if (ncm_opts->bind_count == 0) {
1458 if (!device_is_registered(&ncm_opts->net->dev)) {
1459 ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN);
1460 gether_set_gadget(ncm_opts->net, cdev->gadget);
1461 status = gether_register_netdev(ncm_opts->net);
1462 } else
1463 status = gether_attach_gadget(ncm_opts->net, cdev->gadget);
1464
1465 if (status)
1466 return status;
1467 net = ncm_opts->net;
1468 }
1469
1470 ncm_string_defs[1].s = ncm->ethaddr;
1471
1472 us = usb_gstrings_attach(cdev, ncm_strings,
1473 ARRAY_SIZE(ncm_string_defs));
1474 if (IS_ERR(us))
1475 return PTR_ERR(us);
1476
1477 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1478 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1479 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1480 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1481 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1482
1483 /* allocate instance-specific interface IDs */
1484 status = usb_interface_id(c, f);
1485 if (status < 0)
1486 return status;
1487 ncm->ctrl_id = status;
1488 ncm_iad_desc.bFirstInterface = status;
1489
1490 ncm_control_intf.bInterfaceNumber = status;
1491 ncm_union_desc.bMasterInterface0 = status;
1492
1493 status = usb_interface_id(c, f);
1494 if (status < 0)
1495 return status;
1496 ncm->data_id = status;
1497
1498 ncm_data_nop_intf.bInterfaceNumber = status;
1499 ncm_data_intf.bInterfaceNumber = status;
1500 ncm_union_desc.bSlaveInterface0 = status;
1501
1502 ecm_desc.wMaxSegmentSize = cpu_to_le16(ncm_opts->max_segment_size);
1503
1504 /* allocate instance-specific endpoints */
1505 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1506 if (!ep)
1507 return -ENODEV;
1508 ncm->port.in_ep = ep;
1509
1510 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1511 if (!ep)
1512 return -ENODEV;
1513 ncm->port.out_ep = ep;
1514
1515 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1516 if (!ep)
1517 return -ENODEV;
1518 ncm->notify = ep;
1519
1520 /* allocate notification request and buffer */
1521 request = usb_ep_alloc_request(ep, GFP_KERNEL);
1522 if (!request)
1523 return -ENOMEM;
1524 request->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1525 if (!request->buf)
1526 return -ENOMEM;
1527 request->context = ncm;
1528 request->complete = ncm_notify_complete;
1529
1530 /*
1531 * support all relevant hardware speeds... we expect that when
1532 * hardware is dual speed, all bulk-capable endpoints work at
1533 * both speeds
1534 */
1535 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1536 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1537 hs_ncm_notify_desc.bEndpointAddress =
1538 fs_ncm_notify_desc.bEndpointAddress;
1539
1540 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1541 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1542 ss_ncm_notify_desc.bEndpointAddress =
1543 fs_ncm_notify_desc.bEndpointAddress;
1544
1545 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1546 ncm_ss_function, ncm_ss_function);
1547 if (status)
1548 return status;
1549
1550 /*
1551 * NOTE: all that is done without knowing or caring about
1552 * the network link ... which is unavailable to this code
1553 * until we're activated via set_alt().
1554 */
1555
1556 ncm->port.open = ncm_open;
1557 ncm->port.close = ncm_close;
1558
1559 hrtimer_setup(&ncm->task_timer, ncm_tx_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1560
1561 if (cdev->use_os_string) {
1562 os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc;
1563 os_desc_table[0].if_id = ncm_iad_desc.bFirstInterface;
1564 f->os_desc_table = no_free_ptr(os_desc_table);
1565 f->os_desc_n = 1;
1566 }
1567 ncm->notify_req = no_free_ptr(request);
1568
1569 ncm_opts->bind_count++;
1570 retain_and_null_ptr(net);
1571
1572 DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n",
1573 ncm->port.in_ep->name, ncm->port.out_ep->name,
1574 ncm->notify->name);
1575 return 0;
1576 }
1577
to_f_ncm_opts(struct config_item * item)1578 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1579 {
1580 return container_of(to_config_group(item), struct f_ncm_opts,
1581 func_inst.group);
1582 }
1583
1584 /* f_ncm_item_ops */
1585 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1586
1587 /* f_ncm_opts_dev_addr */
1588 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1589
1590 /* f_ncm_opts_host_addr */
1591 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1592
1593 /* f_ncm_opts_qmult */
1594 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1595
1596 /* f_ncm_opts_ifname */
1597 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1598
ncm_opts_max_segment_size_show(struct config_item * item,char * page)1599 static ssize_t ncm_opts_max_segment_size_show(struct config_item *item,
1600 char *page)
1601 {
1602 struct f_ncm_opts *opts = to_f_ncm_opts(item);
1603 u16 segment_size;
1604
1605 mutex_lock(&opts->lock);
1606 segment_size = opts->max_segment_size;
1607 mutex_unlock(&opts->lock);
1608
1609 return sysfs_emit(page, "%u\n", segment_size);
1610 }
1611
ncm_opts_max_segment_size_store(struct config_item * item,const char * page,size_t len)1612 static ssize_t ncm_opts_max_segment_size_store(struct config_item *item,
1613 const char *page, size_t len)
1614 {
1615 struct f_ncm_opts *opts = to_f_ncm_opts(item);
1616 u16 segment_size;
1617 int ret;
1618
1619 mutex_lock(&opts->lock);
1620 if (opts->refcnt) {
1621 ret = -EBUSY;
1622 goto out;
1623 }
1624
1625 ret = kstrtou16(page, 0, &segment_size);
1626 if (ret)
1627 goto out;
1628
1629 if (segment_size > MAX_DATAGRAM_SIZE) {
1630 ret = -EINVAL;
1631 goto out;
1632 }
1633
1634 opts->max_segment_size = segment_size;
1635 ret = len;
1636 out:
1637 mutex_unlock(&opts->lock);
1638 return ret;
1639 }
1640
1641 CONFIGFS_ATTR(ncm_opts_, max_segment_size);
1642
1643 static struct configfs_attribute *ncm_attrs[] = {
1644 &ncm_opts_attr_dev_addr,
1645 &ncm_opts_attr_host_addr,
1646 &ncm_opts_attr_qmult,
1647 &ncm_opts_attr_ifname,
1648 &ncm_opts_attr_max_segment_size,
1649 NULL,
1650 };
1651
1652 static const struct config_item_type ncm_func_type = {
1653 .ct_item_ops = &ncm_item_ops,
1654 .ct_attrs = ncm_attrs,
1655 .ct_owner = THIS_MODULE,
1656 };
1657
ncm_free_inst(struct usb_function_instance * f)1658 static void ncm_free_inst(struct usb_function_instance *f)
1659 {
1660 struct f_ncm_opts *opts;
1661
1662 opts = container_of(f, struct f_ncm_opts, func_inst);
1663 if (device_is_registered(&opts->net->dev))
1664 gether_cleanup(netdev_priv(opts->net));
1665 else
1666 free_netdev(opts->net);
1667 kfree(opts->ncm_interf_group);
1668 kfree(opts);
1669 }
1670
ncm_alloc_inst(void)1671 static struct usb_function_instance *ncm_alloc_inst(void)
1672 {
1673 struct f_ncm_opts *opts;
1674 struct usb_os_desc *descs[1];
1675 char *names[1];
1676 struct config_group *ncm_interf_group;
1677
1678 opts = kzalloc_obj(*opts);
1679 if (!opts)
1680 return ERR_PTR(-ENOMEM);
1681 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
1682
1683 mutex_init(&opts->lock);
1684 opts->func_inst.free_func_inst = ncm_free_inst;
1685 opts->net = gether_setup_default();
1686 if (IS_ERR(opts->net)) {
1687 struct net_device *net = opts->net;
1688 kfree(opts);
1689 return ERR_CAST(net);
1690 }
1691 opts->max_segment_size = ETH_FRAME_LEN;
1692 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
1693
1694 descs[0] = &opts->ncm_os_desc;
1695 names[0] = "ncm";
1696
1697 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1698 ncm_interf_group =
1699 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1700 names, THIS_MODULE);
1701 if (IS_ERR(ncm_interf_group)) {
1702 ncm_free_inst(&opts->func_inst);
1703 return ERR_CAST(ncm_interf_group);
1704 }
1705 opts->ncm_interf_group = ncm_interf_group;
1706
1707 return &opts->func_inst;
1708 }
1709
ncm_free(struct usb_function * f)1710 static void ncm_free(struct usb_function *f)
1711 {
1712 struct f_ncm *ncm;
1713 struct f_ncm_opts *opts;
1714
1715 ncm = func_to_ncm(f);
1716 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1717 kfree(ncm);
1718 mutex_lock(&opts->lock);
1719 opts->refcnt--;
1720 mutex_unlock(&opts->lock);
1721 }
1722
ncm_unbind(struct usb_configuration * c,struct usb_function * f)1723 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1724 {
1725 struct f_ncm *ncm = func_to_ncm(f);
1726 struct f_ncm_opts *ncm_opts;
1727
1728 DBG(c->cdev, "ncm unbind\n");
1729
1730 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1731
1732 hrtimer_cancel(&ncm->task_timer);
1733
1734 kfree(f->os_desc_table);
1735 f->os_desc_n = 0;
1736
1737 ncm_string_defs[0].id = 0;
1738 usb_free_all_descriptors(f);
1739
1740 if (atomic_read(&ncm->notify_count)) {
1741 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1742 atomic_set(&ncm->notify_count, 0);
1743 }
1744
1745 kfree(ncm->notify_req->buf);
1746 usb_ep_free_request(ncm->notify, ncm->notify_req);
1747
1748 ncm_opts->bind_count--;
1749 if (ncm_opts->bind_count == 0)
1750 gether_detach_gadget(ncm_opts->net);
1751 }
1752
ncm_alloc(struct usb_function_instance * fi)1753 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1754 {
1755 struct f_ncm *ncm;
1756 struct f_ncm_opts *opts;
1757 int status;
1758
1759 /* allocate and initialize one new instance */
1760 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1761 if (!ncm)
1762 return ERR_PTR(-ENOMEM);
1763
1764 opts = container_of(fi, struct f_ncm_opts, func_inst);
1765 mutex_lock(&opts->lock);
1766 opts->refcnt++;
1767
1768 /* export host's Ethernet address in CDC format */
1769 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1770 sizeof(ncm->ethaddr));
1771 if (status < 12) { /* strlen("01234567890a") */
1772 kfree(ncm);
1773 mutex_unlock(&opts->lock);
1774 return ERR_PTR(-EINVAL);
1775 }
1776
1777 spin_lock_init(&ncm->lock);
1778 ncm_reset_values(ncm);
1779 ncm->port.ioport = netdev_priv(opts->net);
1780 mutex_unlock(&opts->lock);
1781 ncm->port.is_fixed = true;
1782 ncm->port.supports_multi_frame = true;
1783
1784 ncm->port.func.name = "cdc_network";
1785 /* descriptors are per-instance copies */
1786 ncm->port.func.bind = ncm_bind;
1787 ncm->port.func.unbind = ncm_unbind;
1788 ncm->port.func.set_alt = ncm_set_alt;
1789 ncm->port.func.get_alt = ncm_get_alt;
1790 ncm->port.func.setup = ncm_setup;
1791 ncm->port.func.disable = ncm_disable;
1792 ncm->port.func.free_func = ncm_free;
1793
1794 ncm->port.wrap = ncm_wrap_ntb;
1795 ncm->port.unwrap = ncm_unwrap_ntb;
1796
1797 return &ncm->port.func;
1798 }
1799
1800 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1801 MODULE_DESCRIPTION("USB CDC Network (NCM) link function driver");
1802 MODULE_LICENSE("GPL");
1803 MODULE_AUTHOR("Yauheni Kaliuta");
1804