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