blob: 019bea8e09cceacd069f8b12dc25efb625cc5e5c [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02002/*
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
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020012 */
13
14#include <linux/kernel.h>
Florian Westphal282ccf62017-03-29 17:17:31 +020015#include <linux/interrupt.h>
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +020016#include <linux/module.h>
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020017#include <linux/device.h>
18#include <linux/etherdevice.h>
19#include <linux/crc32.h>
20
21#include <linux/usb/cdc.h>
22
23#include "u_ether.h"
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +020024#include "u_ether_configfs.h"
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +020025#include "u_ncm.h"
Romain Izard79340922019-04-16 16:07:32 +020026#include "configfs.h"
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020027
28/*
29 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
30 * NCM is intended to be used with high-speed network attachments.
31 *
32 * Note that NCM requires the use of "alternate settings" for its data
33 * interface. This means that the set_alt() method has real work to do,
34 * and also means that a get_alt() method is required.
35 */
36
37/* to trigger crc/non-crc ndp signature */
38
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020039#define NCM_NDP_HDR_CRC 0x01000000
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020040
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020041enum ncm_notify_state {
42 NCM_NOTIFY_NONE, /* don't notify */
43 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
44 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
45};
46
47struct f_ncm {
48 struct gether port;
49 u8 ctrl_id, data_id;
50
51 char ethaddr[14];
52
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020053 struct usb_ep *notify;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020054 struct usb_request *notify_req;
55 u8 notify_state;
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +000056 atomic_t notify_count;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020057 bool is_open;
58
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +010059 const struct ndp_parser_opts *parser_opts;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020060 bool is_crc;
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +010061 u32 ndp_sign;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020062
63 /*
64 * for notification, it is accessed from both
65 * callback and ethernet open/close
66 */
67 spinlock_t lock;
Jim Baxter6d3865f2014-07-07 18:33:18 +010068
69 struct net_device *netdev;
70
71 /* For multi-frame NDP TX */
72 struct sk_buff *skb_tx_data;
73 struct sk_buff *skb_tx_ndp;
74 u16 ndp_dgram_count;
75 bool timer_force_tx;
Jim Baxter6d3865f2014-07-07 18:33:18 +010076 struct hrtimer task_timer;
Jim Baxter6d3865f2014-07-07 18:33:18 +010077 bool timer_stopping;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020078};
79
80static inline struct f_ncm *func_to_ncm(struct usb_function *f)
81{
82 return container_of(f, struct f_ncm, port.func);
83}
84
85/* peak (theoretical) bulk transfer rate in bits-per-second */
86static inline unsigned ncm_bitrate(struct usb_gadget *g)
87{
Lorenzo Colitti986499b2020-08-25 14:55:03 +090088 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER_PLUS)
89 return 4250000000U;
90 else if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
91 return 3750000000U;
Jussi Kivilinna16501132016-08-12 17:28:05 +030092 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020093 return 13 * 512 * 8 * 1000 * 8;
94 else
95 return 19 * 64 * 1 * 1000 * 8;
96}
97
98/*-------------------------------------------------------------------------*/
99
100/*
101 * We cannot group frames so use just the minimal size which ok to put
102 * one max-size ethernet frame.
103 * If the host can group frames, allow it to do that, 16K is selected,
104 * because it's used by default by the current linux host driver
105 */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100106#define NTB_DEFAULT_IN_SIZE 16384
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200107#define NTB_OUT_SIZE 16384
108
Jim Baxter6d3865f2014-07-07 18:33:18 +0100109/* Allocation for storing the NDP, 32 should suffice for a
110 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
111 * be transmitted in a single 16kB skb, though when sending full size
112 * packets this limit will be plenty.
113 * Smaller packets are not likely to be trying to maximize the
114 * throughput and will be mstly sending smaller infrequent frames.
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200115 */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100116#define TX_MAX_NUM_DPE 32
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200117
Jim Baxter6d3865f2014-07-07 18:33:18 +0100118/* Delay for the transmit to wait before sending an unfilled NTB frame. */
119#define TX_TIMEOUT_NSECS 300000
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200120
121#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
122 USB_CDC_NCM_NTB32_SUPPORTED)
123
124static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
Dmytro Milinevskyyf72e3b72012-10-05 01:44:04 +0300125 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200126 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
127 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
128 .wNdpInDivisor = cpu_to_le16(4),
129 .wNdpInPayloadRemainder = cpu_to_le16(0),
130 .wNdpInAlignment = cpu_to_le16(4),
131
132 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
133 .wNdpOutDivisor = cpu_to_le16(4),
134 .wNdpOutPayloadRemainder = cpu_to_le16(0),
135 .wNdpOutAlignment = cpu_to_le16(4),
136};
137
138/*
139 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
140 * packet, to simplify cancellation; and a big transfer interval, to
141 * waste less bandwidth.
142 */
143
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200144#define NCM_STATUS_INTERVAL_MS 32
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200145#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
146
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200147static struct usb_interface_assoc_descriptor ncm_iad_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200148 .bLength = sizeof ncm_iad_desc,
149 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
150
151 /* .bFirstInterface = DYNAMIC, */
152 .bInterfaceCount = 2, /* control + data */
153 .bFunctionClass = USB_CLASS_COMM,
154 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
155 .bFunctionProtocol = USB_CDC_PROTO_NONE,
156 /* .iFunction = DYNAMIC */
157};
158
159/* interface descriptor: */
160
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200161static struct usb_interface_descriptor ncm_control_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200162 .bLength = sizeof ncm_control_intf,
163 .bDescriptorType = USB_DT_INTERFACE,
164
165 /* .bInterfaceNumber = DYNAMIC */
166 .bNumEndpoints = 1,
167 .bInterfaceClass = USB_CLASS_COMM,
168 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
169 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
170 /* .iInterface = DYNAMIC */
171};
172
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200173static struct usb_cdc_header_desc ncm_header_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200174 .bLength = sizeof ncm_header_desc,
175 .bDescriptorType = USB_DT_CS_INTERFACE,
176 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
177
178 .bcdCDC = cpu_to_le16(0x0110),
179};
180
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200181static struct usb_cdc_union_desc ncm_union_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200182 .bLength = sizeof(ncm_union_desc),
183 .bDescriptorType = USB_DT_CS_INTERFACE,
184 .bDescriptorSubType = USB_CDC_UNION_TYPE,
185 /* .bMasterInterface0 = DYNAMIC */
186 /* .bSlaveInterface0 = DYNAMIC */
187};
188
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200189static struct usb_cdc_ether_desc ecm_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200190 .bLength = sizeof ecm_desc,
191 .bDescriptorType = USB_DT_CS_INTERFACE,
192 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
193
194 /* this descriptor actually adds value, surprise! */
195 /* .iMACAddress = DYNAMIC */
196 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
197 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
198 .wNumberMCFilters = cpu_to_le16(0),
199 .bNumberPowerFilters = 0,
200};
201
202#define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
203
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200204static struct usb_cdc_ncm_desc ncm_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200205 .bLength = sizeof ncm_desc,
206 .bDescriptorType = USB_DT_CS_INTERFACE,
207 .bDescriptorSubType = USB_CDC_NCM_TYPE,
208
209 .bcdNcmVersion = cpu_to_le16(0x0100),
210 /* can process SetEthernetPacketFilter */
211 .bmNetworkCapabilities = NCAPS,
212};
213
214/* the default data interface has no endpoints ... */
215
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200216static struct usb_interface_descriptor ncm_data_nop_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200217 .bLength = sizeof ncm_data_nop_intf,
218 .bDescriptorType = USB_DT_INTERFACE,
219
220 .bInterfaceNumber = 1,
221 .bAlternateSetting = 0,
222 .bNumEndpoints = 0,
223 .bInterfaceClass = USB_CLASS_CDC_DATA,
224 .bInterfaceSubClass = 0,
225 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
226 /* .iInterface = DYNAMIC */
227};
228
229/* ... but the "real" data interface has two bulk endpoints */
230
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200231static struct usb_interface_descriptor ncm_data_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200232 .bLength = sizeof ncm_data_intf,
233 .bDescriptorType = USB_DT_INTERFACE,
234
235 .bInterfaceNumber = 1,
236 .bAlternateSetting = 1,
237 .bNumEndpoints = 2,
238 .bInterfaceClass = USB_CLASS_CDC_DATA,
239 .bInterfaceSubClass = 0,
240 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
241 /* .iInterface = DYNAMIC */
242};
243
244/* full speed support: */
245
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200246static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249
250 .bEndpointAddress = USB_DIR_IN,
251 .bmAttributes = USB_ENDPOINT_XFER_INT,
252 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200253 .bInterval = NCM_STATUS_INTERVAL_MS,
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200254};
255
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200256static struct usb_endpoint_descriptor fs_ncm_in_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200257 .bLength = USB_DT_ENDPOINT_SIZE,
258 .bDescriptorType = USB_DT_ENDPOINT,
259
260 .bEndpointAddress = USB_DIR_IN,
261 .bmAttributes = USB_ENDPOINT_XFER_BULK,
262};
263
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200264static struct usb_endpoint_descriptor fs_ncm_out_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200265 .bLength = USB_DT_ENDPOINT_SIZE,
266 .bDescriptorType = USB_DT_ENDPOINT,
267
268 .bEndpointAddress = USB_DIR_OUT,
269 .bmAttributes = USB_ENDPOINT_XFER_BULK,
270};
271
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200272static struct usb_descriptor_header *ncm_fs_function[] = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200273 (struct usb_descriptor_header *) &ncm_iad_desc,
274 /* CDC NCM control descriptors */
275 (struct usb_descriptor_header *) &ncm_control_intf,
276 (struct usb_descriptor_header *) &ncm_header_desc,
277 (struct usb_descriptor_header *) &ncm_union_desc,
278 (struct usb_descriptor_header *) &ecm_desc,
279 (struct usb_descriptor_header *) &ncm_desc,
280 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
281 /* data interface, altsettings 0 and 1 */
282 (struct usb_descriptor_header *) &ncm_data_nop_intf,
283 (struct usb_descriptor_header *) &ncm_data_intf,
284 (struct usb_descriptor_header *) &fs_ncm_in_desc,
285 (struct usb_descriptor_header *) &fs_ncm_out_desc,
286 NULL,
287};
288
289/* high speed support: */
290
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200291static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200292 .bLength = USB_DT_ENDPOINT_SIZE,
293 .bDescriptorType = USB_DT_ENDPOINT,
294
295 .bEndpointAddress = USB_DIR_IN,
296 .bmAttributes = USB_ENDPOINT_XFER_INT,
297 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200298 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200299};
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200300static struct usb_endpoint_descriptor hs_ncm_in_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200301 .bLength = USB_DT_ENDPOINT_SIZE,
302 .bDescriptorType = USB_DT_ENDPOINT,
303
304 .bEndpointAddress = USB_DIR_IN,
305 .bmAttributes = USB_ENDPOINT_XFER_BULK,
306 .wMaxPacketSize = cpu_to_le16(512),
307};
308
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200309static struct usb_endpoint_descriptor hs_ncm_out_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200310 .bLength = USB_DT_ENDPOINT_SIZE,
311 .bDescriptorType = USB_DT_ENDPOINT,
312
313 .bEndpointAddress = USB_DIR_OUT,
314 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 .wMaxPacketSize = cpu_to_le16(512),
316};
317
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200318static struct usb_descriptor_header *ncm_hs_function[] = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200319 (struct usb_descriptor_header *) &ncm_iad_desc,
320 /* CDC NCM control descriptors */
321 (struct usb_descriptor_header *) &ncm_control_intf,
322 (struct usb_descriptor_header *) &ncm_header_desc,
323 (struct usb_descriptor_header *) &ncm_union_desc,
324 (struct usb_descriptor_header *) &ecm_desc,
325 (struct usb_descriptor_header *) &ncm_desc,
326 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
327 /* data interface, altsettings 0 and 1 */
328 (struct usb_descriptor_header *) &ncm_data_nop_intf,
329 (struct usb_descriptor_header *) &ncm_data_intf,
330 (struct usb_descriptor_header *) &hs_ncm_in_desc,
331 (struct usb_descriptor_header *) &hs_ncm_out_desc,
332 NULL,
333};
334
Jussi Kivilinna16501132016-08-12 17:28:05 +0300335
336/* super speed support: */
337
338static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
339 .bLength = USB_DT_ENDPOINT_SIZE,
340 .bDescriptorType = USB_DT_ENDPOINT,
341
342 .bEndpointAddress = USB_DIR_IN,
343 .bmAttributes = USB_ENDPOINT_XFER_INT,
344 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
345 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
346};
347
348static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
349 .bLength = sizeof(ss_ncm_notify_comp_desc),
350 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
351
352 /* the following 3 values can be tweaked if necessary */
353 /* .bMaxBurst = 0, */
354 /* .bmAttributes = 0, */
355 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
356};
357
358static struct usb_endpoint_descriptor ss_ncm_in_desc = {
359 .bLength = USB_DT_ENDPOINT_SIZE,
360 .bDescriptorType = USB_DT_ENDPOINT,
361
362 .bEndpointAddress = USB_DIR_IN,
363 .bmAttributes = USB_ENDPOINT_XFER_BULK,
364 .wMaxPacketSize = cpu_to_le16(1024),
365};
366
367static struct usb_endpoint_descriptor ss_ncm_out_desc = {
368 .bLength = USB_DT_ENDPOINT_SIZE,
369 .bDescriptorType = USB_DT_ENDPOINT,
370
371 .bEndpointAddress = USB_DIR_OUT,
372 .bmAttributes = USB_ENDPOINT_XFER_BULK,
373 .wMaxPacketSize = cpu_to_le16(1024),
374};
375
376static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
377 .bLength = sizeof(ss_ncm_bulk_comp_desc),
378 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
379
380 /* the following 2 values can be tweaked if necessary */
Lorenzo Colittia176b1a2020-08-25 14:55:04 +0900381 .bMaxBurst = 15,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300382 /* .bmAttributes = 0, */
383};
384
385static struct usb_descriptor_header *ncm_ss_function[] = {
386 (struct usb_descriptor_header *) &ncm_iad_desc,
387 /* CDC NCM control descriptors */
388 (struct usb_descriptor_header *) &ncm_control_intf,
389 (struct usb_descriptor_header *) &ncm_header_desc,
390 (struct usb_descriptor_header *) &ncm_union_desc,
391 (struct usb_descriptor_header *) &ecm_desc,
392 (struct usb_descriptor_header *) &ncm_desc,
393 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
394 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
395 /* data interface, altsettings 0 and 1 */
396 (struct usb_descriptor_header *) &ncm_data_nop_intf,
397 (struct usb_descriptor_header *) &ncm_data_intf,
398 (struct usb_descriptor_header *) &ss_ncm_in_desc,
399 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
400 (struct usb_descriptor_header *) &ss_ncm_out_desc,
401 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
402 NULL,
403};
404
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200405/* string descriptors: */
406
407#define STRING_CTRL_IDX 0
408#define STRING_MAC_IDX 1
409#define STRING_DATA_IDX 2
410#define STRING_IAD_IDX 3
411
412static struct usb_string ncm_string_defs[] = {
413 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
Sebastian Andrzej Siewior1616e99d2012-10-22 22:15:10 +0200414 [STRING_MAC_IDX].s = "",
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200415 [STRING_DATA_IDX].s = "CDC Network Data",
416 [STRING_IAD_IDX].s = "CDC NCM",
417 { } /* end of list */
418};
419
420static struct usb_gadget_strings ncm_string_table = {
421 .language = 0x0409, /* en-us */
422 .strings = ncm_string_defs,
423};
424
425static struct usb_gadget_strings *ncm_strings[] = {
426 &ncm_string_table,
427 NULL,
428};
429
430/*
431 * Here are options for NCM Datagram Pointer table (NDP) parser.
432 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
433 * in NDP16 offsets and sizes fields are 1 16bit word wide,
434 * in NDP32 -- 2 16bit words wide. Also signatures are different.
435 * To make the parser code the same, put the differences in the structure,
436 * and switch pointers to the structures when the format is changed.
437 */
438
439struct ndp_parser_opts {
440 u32 nth_sign;
441 u32 ndp_sign;
442 unsigned nth_size;
443 unsigned ndp_size;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100444 unsigned dpe_size;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200445 unsigned ndplen_align;
446 /* sizes in u16 units */
447 unsigned dgram_item_len; /* index or length */
448 unsigned block_length;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100449 unsigned ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200450 unsigned reserved1;
451 unsigned reserved2;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100452 unsigned next_ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200453};
454
455#define INIT_NDP16_OPTS { \
456 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
457 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
458 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
459 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100460 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200461 .ndplen_align = 4, \
462 .dgram_item_len = 1, \
463 .block_length = 1, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100464 .ndp_index = 1, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200465 .reserved1 = 0, \
466 .reserved2 = 0, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100467 .next_ndp_index = 1, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200468 }
469
470
471#define INIT_NDP32_OPTS { \
472 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
473 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
474 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
475 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100476 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200477 .ndplen_align = 8, \
478 .dgram_item_len = 2, \
479 .block_length = 2, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100480 .ndp_index = 2, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200481 .reserved1 = 1, \
482 .reserved2 = 2, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100483 .next_ndp_index = 2, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200484 }
485
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +0100486static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
487static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200488
489static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
490{
491 switch (size) {
492 case 1:
493 put_unaligned_le16((u16)val, *p);
494 break;
495 case 2:
496 put_unaligned_le32((u32)val, *p);
497
498 break;
499 default:
500 BUG();
501 }
502
503 *p += size;
504}
505
506static inline unsigned get_ncm(__le16 **p, unsigned size)
507{
508 unsigned tmp;
509
510 switch (size) {
511 case 1:
512 tmp = get_unaligned_le16(*p);
513 break;
514 case 2:
515 tmp = get_unaligned_le32(*p);
516 break;
517 default:
518 BUG();
519 }
520
521 *p += size;
522 return tmp;
523}
524
525/*-------------------------------------------------------------------------*/
526
527static inline void ncm_reset_values(struct f_ncm *ncm)
528{
529 ncm->parser_opts = &ndp16_opts;
530 ncm->is_crc = false;
Romain Izard550eef02019-04-16 16:07:31 +0200531 ncm->ndp_sign = ncm->parser_opts->ndp_sign;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200532 ncm->port.cdc_filter = DEFAULT_FILTER;
533
534 /* doesn't make sense for ncm, fixed size used */
535 ncm->port.header_len = 0;
536
537 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
538 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
539}
540
541/*
542 * Context: ncm->lock held
543 */
544static void ncm_do_notify(struct f_ncm *ncm)
545{
546 struct usb_request *req = ncm->notify_req;
547 struct usb_cdc_notification *event;
548 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
549 __le32 *data;
550 int status;
551
552 /* notification already in flight? */
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000553 if (atomic_read(&ncm->notify_count))
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200554 return;
555
556 event = req->buf;
557 switch (ncm->notify_state) {
558 case NCM_NOTIFY_NONE:
559 return;
560
561 case NCM_NOTIFY_CONNECT:
562 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
563 if (ncm->is_open)
564 event->wValue = cpu_to_le16(1);
565 else
566 event->wValue = cpu_to_le16(0);
567 event->wLength = 0;
568 req->length = sizeof *event;
569
570 DBG(cdev, "notify connect %s\n",
571 ncm->is_open ? "true" : "false");
572 ncm->notify_state = NCM_NOTIFY_NONE;
573 break;
574
575 case NCM_NOTIFY_SPEED:
576 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
577 event->wValue = cpu_to_le16(0);
578 event->wLength = cpu_to_le16(8);
579 req->length = NCM_STATUS_BYTECOUNT;
580
581 /* SPEED_CHANGE data is up/down speeds in bits/sec */
582 data = req->buf + sizeof *event;
583 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
584 data[1] = data[0];
585
586 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
587 ncm->notify_state = NCM_NOTIFY_CONNECT;
588 break;
589 }
590 event->bmRequestType = 0xA1;
591 event->wIndex = cpu_to_le16(ncm->ctrl_id);
592
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000593 atomic_inc(&ncm->notify_count);
594
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200595 /*
596 * In double buffering if there is a space in FIFO,
597 * completion callback can be called right after the call,
598 * so unlocking
599 */
600 spin_unlock(&ncm->lock);
601 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
602 spin_lock(&ncm->lock);
603 if (status < 0) {
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000604 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200605 DBG(cdev, "notify --> %d\n", status);
606 }
607}
608
609/*
610 * Context: ncm->lock held
611 */
612static void ncm_notify(struct f_ncm *ncm)
613{
614 /*
615 * NOTE on most versions of Linux, host side cdc-ethernet
616 * won't listen for notifications until its netdevice opens.
617 * The first notification then sits in the FIFO for a long
618 * time, and the second one is queued.
619 *
620 * If ncm_notify() is called before the second (CONNECT)
621 * notification is sent, then it will reset to send the SPEED
622 * notificaion again (and again, and again), but it's not a problem
623 */
624 ncm->notify_state = NCM_NOTIFY_SPEED;
625 ncm_do_notify(ncm);
626}
627
628static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
629{
630 struct f_ncm *ncm = req->context;
631 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
632 struct usb_cdc_notification *event = req->buf;
633
634 spin_lock(&ncm->lock);
635 switch (req->status) {
636 case 0:
637 VDBG(cdev, "Notification %02x sent\n",
638 event->bNotificationType);
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000639 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200640 break;
641 case -ECONNRESET:
642 case -ESHUTDOWN:
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000643 atomic_set(&ncm->notify_count, 0);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200644 ncm->notify_state = NCM_NOTIFY_NONE;
645 break;
646 default:
647 DBG(cdev, "event %02x --> %d\n",
648 event->bNotificationType, req->status);
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +0000649 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200650 break;
651 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200652 ncm_do_notify(ncm);
653 spin_unlock(&ncm->lock);
654}
655
656static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
657{
658 /* now for SET_NTB_INPUT_SIZE only */
659 unsigned in_size;
660 struct usb_function *f = req->context;
661 struct f_ncm *ncm = func_to_ncm(f);
Robert Baldyga35bfde32015-09-16 12:10:40 +0200662 struct usb_composite_dev *cdev = f->config->cdev;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200663
664 req->context = NULL;
665 if (req->status || req->actual != req->length) {
666 DBG(cdev, "Bad control-OUT transfer\n");
667 goto invalid;
668 }
669
670 in_size = get_unaligned_le32(req->buf);
671 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
672 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
673 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
674 goto invalid;
675 }
676
677 ncm->port.fixed_in_len = in_size;
678 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
679 return;
680
681invalid:
682 usb_ep_set_halt(ep);
683 return;
684}
685
686static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
687{
688 struct f_ncm *ncm = func_to_ncm(f);
689 struct usb_composite_dev *cdev = f->config->cdev;
690 struct usb_request *req = cdev->req;
691 int value = -EOPNOTSUPP;
692 u16 w_index = le16_to_cpu(ctrl->wIndex);
693 u16 w_value = le16_to_cpu(ctrl->wValue);
694 u16 w_length = le16_to_cpu(ctrl->wLength);
695
696 /*
697 * composite driver infrastructure handles everything except
698 * CDC class messages; interface activation uses set_alt().
699 */
700 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
701 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
702 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
703 /*
704 * see 6.2.30: no data, wIndex = interface,
705 * wValue = packet filter bitmap
706 */
707 if (w_length != 0 || w_index != ncm->ctrl_id)
708 goto invalid;
709 DBG(cdev, "packet filter %02x\n", w_value);
710 /*
711 * REVISIT locking of cdc_filter. This assumes the UDC
712 * driver won't have a concurrent packet TX irq running on
713 * another CPU; or that if it does, this write is atomic...
714 */
715 ncm->port.cdc_filter = w_value;
716 value = 0;
717 break;
718 /*
719 * and optionally:
720 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
721 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
722 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
723 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
724 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
725 * case USB_CDC_GET_ETHERNET_STATISTIC:
726 */
727
728 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
729 | USB_CDC_GET_NTB_PARAMETERS:
730
731 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
732 goto invalid;
733 value = w_length > sizeof ntb_parameters ?
734 sizeof ntb_parameters : w_length;
735 memcpy(req->buf, &ntb_parameters, value);
736 VDBG(cdev, "Host asked NTB parameters\n");
737 break;
738
739 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
740 | USB_CDC_GET_NTB_INPUT_SIZE:
741
742 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
743 goto invalid;
744 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
745 value = 4;
746 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
747 ncm->port.fixed_in_len);
748 break;
749
750 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
751 | USB_CDC_SET_NTB_INPUT_SIZE:
752 {
753 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
754 goto invalid;
755 req->complete = ncm_ep0out_complete;
756 req->length = w_length;
757 req->context = f;
758
759 value = req->length;
760 break;
761 }
762
763 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
764 | USB_CDC_GET_NTB_FORMAT:
765 {
766 uint16_t format;
767
768 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
769 goto invalid;
770 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
771 put_unaligned_le16(format, req->buf);
772 value = 2;
773 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
774 break;
775 }
776
777 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
778 | USB_CDC_SET_NTB_FORMAT:
779 {
780 if (w_length != 0 || w_index != ncm->ctrl_id)
781 goto invalid;
782 switch (w_value) {
783 case 0x0000:
784 ncm->parser_opts = &ndp16_opts;
785 DBG(cdev, "NCM16 selected\n");
786 break;
787 case 0x0001:
788 ncm->parser_opts = &ndp32_opts;
789 DBG(cdev, "NCM32 selected\n");
790 break;
791 default:
792 goto invalid;
793 }
794 value = 0;
795 break;
796 }
797 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
798 | USB_CDC_GET_CRC_MODE:
799 {
800 uint16_t is_crc;
801
802 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
803 goto invalid;
804 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
805 put_unaligned_le16(is_crc, req->buf);
806 value = 2;
807 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
808 break;
809 }
810
811 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
812 | USB_CDC_SET_CRC_MODE:
813 {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200814 if (w_length != 0 || w_index != ncm->ctrl_id)
815 goto invalid;
816 switch (w_value) {
817 case 0x0000:
818 ncm->is_crc = false;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200819 DBG(cdev, "non-CRC mode selected\n");
820 break;
821 case 0x0001:
822 ncm->is_crc = true;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200823 DBG(cdev, "CRC mode selected\n");
824 break;
825 default:
826 goto invalid;
827 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200828 value = 0;
829 break;
830 }
831
832 /* and disabled in ncm descriptor: */
833 /* case USB_CDC_GET_NET_ADDRESS: */
834 /* case USB_CDC_SET_NET_ADDRESS: */
835 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
836 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
837
838 default:
839invalid:
840 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
841 ctrl->bRequestType, ctrl->bRequest,
842 w_value, w_index, w_length);
843 }
Romain Izard550eef02019-04-16 16:07:31 +0200844 ncm->ndp_sign = ncm->parser_opts->ndp_sign |
845 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200846
847 /* respond with data transfer or status phase? */
848 if (value >= 0) {
849 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
850 ctrl->bRequestType, ctrl->bRequest,
851 w_value, w_index, w_length);
852 req->zero = 0;
853 req->length = value;
854 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
855 if (value < 0)
856 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
857 ctrl->bRequestType, ctrl->bRequest,
858 value);
859 }
860
861 /* device either stalls (value < 0) or reports success */
862 return value;
863}
864
865
866static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
867{
868 struct f_ncm *ncm = func_to_ncm(f);
869 struct usb_composite_dev *cdev = f->config->cdev;
870
871 /* Control interface has only altsetting 0 */
872 if (intf == ncm->ctrl_id) {
873 if (alt != 0)
874 goto fail;
875
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200876 DBG(cdev, "reset ncm control %d\n", intf);
877 usb_ep_disable(ncm->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300878
879 if (!(ncm->notify->desc)) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200880 DBG(cdev, "init ncm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300881 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
882 goto fail;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200883 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300884 usb_ep_enable(ncm->notify);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200885
886 /* Data interface has two altsettings, 0 and 1 */
887 } else if (intf == ncm->data_id) {
888 if (alt > 1)
889 goto fail;
890
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200891 if (ncm->port.in_ep->enabled) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200892 DBG(cdev, "reset ncm\n");
Jim Baxter6d3865f2014-07-07 18:33:18 +0100893 ncm->timer_stopping = true;
894 ncm->netdev = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200895 gether_disconnect(&ncm->port);
896 ncm_reset_values(ncm);
897 }
898
899 /*
900 * CDC Network only sends data in non-default altsettings.
901 * Changing altsettings resets filters, statistics, etc.
902 */
903 if (alt == 1) {
904 struct net_device *net;
905
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300906 if (!ncm->port.in_ep->desc ||
907 !ncm->port.out_ep->desc) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200908 DBG(cdev, "init ncm\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300909 if (config_ep_by_speed(cdev->gadget, f,
910 ncm->port.in_ep) ||
911 config_ep_by_speed(cdev->gadget, f,
912 ncm->port.out_ep)) {
913 ncm->port.in_ep->desc = NULL;
914 ncm->port.out_ep->desc = NULL;
915 goto fail;
916 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200917 }
918
919 /* TODO */
920 /* Enable zlps by default for NCM conformance;
921 * override for musb_hdrc (avoids txdma ovhead)
922 */
Robert Baldyga7a896d42015-07-28 07:20:02 +0200923 ncm->port.is_zlp_ok =
924 gadget_is_zlp_supported(cdev->gadget);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200925 ncm->port.cdc_filter = DEFAULT_FILTER;
926 DBG(cdev, "activate ncm\n");
927 net = gether_connect(&ncm->port);
928 if (IS_ERR(net))
929 return PTR_ERR(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100930 ncm->netdev = net;
931 ncm->timer_stopping = false;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200932 }
933
934 spin_lock(&ncm->lock);
935 ncm_notify(ncm);
936 spin_unlock(&ncm->lock);
937 } else
938 goto fail;
939
940 return 0;
941fail:
942 return -EINVAL;
943}
944
945/*
946 * Because the data interface supports multiple altsettings,
947 * this NCM function *MUST* implement a get_alt() method.
948 */
949static int ncm_get_alt(struct usb_function *f, unsigned intf)
950{
951 struct f_ncm *ncm = func_to_ncm(f);
952
953 if (intf == ncm->ctrl_id)
954 return 0;
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200955 return ncm->port.in_ep->enabled ? 1 : 0;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200956}
957
Jim Baxter6d3865f2014-07-07 18:33:18 +0100958static struct sk_buff *package_for_tx(struct f_ncm *ncm)
959{
960 __le16 *ntb_iter;
961 struct sk_buff *skb2 = NULL;
962 unsigned ndp_pad;
963 unsigned ndp_index;
964 unsigned new_len;
965
966 const struct ndp_parser_opts *opts = ncm->parser_opts;
967 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
968 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
969
970 /* Stop the timer */
971 hrtimer_try_to_cancel(&ncm->task_timer);
972
973 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
974 ncm->skb_tx_data->len;
975 ndp_index = ncm->skb_tx_data->len + ndp_pad;
976 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
977
978 /* Set the final BlockLength and wNdpIndex */
979 ntb_iter = (void *) ncm->skb_tx_data->data;
980 /* Increment pointer to BlockLength */
981 ntb_iter += 2 + 1 + 1;
982 put_ncm(&ntb_iter, opts->block_length, new_len);
983 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
984
985 /* Set the final NDP wLength */
986 new_len = opts->ndp_size +
987 (ncm->ndp_dgram_count * dgram_idx_len);
988 ncm->ndp_dgram_count = 0;
989 /* Increment from start to wLength */
990 ntb_iter = (void *) ncm->skb_tx_ndp->data;
991 ntb_iter += 2;
992 put_unaligned_le16(new_len, ntb_iter);
993
994 /* Merge the skbs */
995 swap(skb2, ncm->skb_tx_data);
996 if (ncm->skb_tx_data) {
Torsten Polle38314e52016-09-19 10:05:42 +0200997 dev_consume_skb_any(ncm->skb_tx_data);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100998 ncm->skb_tx_data = NULL;
999 }
1000
1001 /* Insert NDP alignment. */
yuan linyub952f4d2017-06-18 22:52:04 +08001002 skb_put_zero(skb2, ndp_pad);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001003
1004 /* Copy NTB across. */
yuan linyub952f4d2017-06-18 22:52:04 +08001005 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
Torsten Polle38314e52016-09-19 10:05:42 +02001006 dev_consume_skb_any(ncm->skb_tx_ndp);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001007 ncm->skb_tx_ndp = NULL;
1008
1009 /* Insert zero'd datagram. */
yuan linyub952f4d2017-06-18 22:52:04 +08001010 skb_put_zero(skb2, dgram_idx_len);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001011
1012 return skb2;
1013}
1014
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001015static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1016 struct sk_buff *skb)
1017{
1018 struct f_ncm *ncm = func_to_ncm(&port->func);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001019 struct sk_buff *skb2 = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001020 int ncb_len = 0;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001021 __le16 *ntb_data;
1022 __le16 *ntb_ndp;
1023 int dgram_pad;
1024
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001025 unsigned max_size = ncm->port.fixed_in_len;
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +01001026 const struct ndp_parser_opts *opts = ncm->parser_opts;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001027 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1028 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1029 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1030 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001031
Jim Baxter6d3865f2014-07-07 18:33:18 +01001032 if (!skb && !ncm->skb_tx_data)
1033 return NULL;
Dmytro Milinevskyyf72e3b72012-10-05 01:44:04 +03001034
Jim Baxter6d3865f2014-07-07 18:33:18 +01001035 if (skb) {
1036 /* Add the CRC if required up front */
1037 if (ncm->is_crc) {
1038 uint32_t crc;
1039 __le16 *crc_pos;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001040
Jim Baxter6d3865f2014-07-07 18:33:18 +01001041 crc = ~crc32_le(~0,
1042 skb->data,
1043 skb->len);
Johannes Berg4df864c2017-06-16 14:29:21 +02001044 crc_pos = skb_put(skb, sizeof(uint32_t));
Jim Baxter6d3865f2014-07-07 18:33:18 +01001045 put_unaligned_le32(crc, crc_pos);
1046 }
1047
1048 /* If the new skb is too big for the current NCM NTB then
1049 * set the current stored skb to be sent now and clear it
1050 * ready for new data.
1051 * NOTE: Assume maximum align for speed of calculation.
1052 */
1053 if (ncm->skb_tx_data
1054 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1055 || (ncm->skb_tx_data->len +
1056 div + rem + skb->len +
1057 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1058 > max_size)) {
1059 skb2 = package_for_tx(ncm);
1060 if (!skb2)
1061 goto err;
1062 }
1063
1064 if (!ncm->skb_tx_data) {
1065 ncb_len = opts->nth_size;
1066 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1067 ncb_len += dgram_pad;
1068
1069 /* Create a new skb for the NTH and datagrams. */
1070 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1071 if (!ncm->skb_tx_data)
1072 goto err;
1073
Torsten Polle9a5380c2016-09-19 10:05:40 +02001074 ncm->skb_tx_data->dev = ncm->netdev;
Johannes Bergb080db52017-06-16 14:29:19 +02001075 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001076 /* dwSignature */
1077 put_unaligned_le32(opts->nth_sign, ntb_data);
1078 ntb_data += 2;
1079 /* wHeaderLength */
1080 put_unaligned_le16(opts->nth_size, ntb_data++);
1081
1082 /* Allocate an skb for storing the NDP,
1083 * TX_MAX_NUM_DPE should easily suffice for a
1084 * 16k packet.
1085 */
1086 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1087 + opts->dpe_size
1088 * TX_MAX_NUM_DPE),
1089 GFP_ATOMIC);
1090 if (!ncm->skb_tx_ndp)
1091 goto err;
Torsten Polle9a5380c2016-09-19 10:05:40 +02001092
1093 ncm->skb_tx_ndp->dev = ncm->netdev;
Johannes Berg4df864c2017-06-16 14:29:21 +02001094 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001095 memset(ntb_ndp, 0, ncb_len);
1096 /* dwSignature */
1097 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1098 ntb_ndp += 2;
1099
1100 /* There is always a zeroed entry */
1101 ncm->ndp_dgram_count = 1;
1102
1103 /* Note: we skip opts->next_ndp_index */
1104 }
1105
1106 /* Delay the timer. */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001107 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001108 HRTIMER_MODE_REL_SOFT);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001109
1110 /* Add the datagram position entries */
Johannes Bergb080db52017-06-16 14:29:19 +02001111 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001112
1113 ncb_len = ncm->skb_tx_data->len;
1114 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1115 ncb_len += dgram_pad;
1116
1117 /* (d)wDatagramIndex */
1118 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1119 /* (d)wDatagramLength */
1120 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1121 ncm->ndp_dgram_count++;
1122
1123 /* Add the new data to the skb */
yuan linyub952f4d2017-06-18 22:52:04 +08001124 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1125 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
Torsten Polle38314e52016-09-19 10:05:42 +02001126 dev_consume_skb_any(skb);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001127 skb = NULL;
1128
1129 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1130 /* If the tx was requested because of a timeout then send */
1131 skb2 = package_for_tx(ncm);
1132 if (!skb2)
1133 goto err;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001134 }
1135
Jim Baxter6d3865f2014-07-07 18:33:18 +01001136 return skb2;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001137
Jim Baxter6d3865f2014-07-07 18:33:18 +01001138err:
1139 ncm->netdev->stats.tx_dropped++;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001140
Jim Baxter6d3865f2014-07-07 18:33:18 +01001141 if (skb)
1142 dev_kfree_skb_any(skb);
1143 if (ncm->skb_tx_data)
1144 dev_kfree_skb_any(ncm->skb_tx_data);
1145 if (ncm->skb_tx_ndp)
1146 dev_kfree_skb_any(ncm->skb_tx_ndp);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001147
Jim Baxter6d3865f2014-07-07 18:33:18 +01001148 return NULL;
1149}
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001150
Jim Baxter6d3865f2014-07-07 18:33:18 +01001151/*
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001152 * The transmit should only be run if no skb data has been sent
1153 * for a certain duration.
Jim Baxter6d3865f2014-07-07 18:33:18 +01001154 */
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001155static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
Jim Baxter6d3865f2014-07-07 18:33:18 +01001156{
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001157 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001158
Jim Baxter6d3865f2014-07-07 18:33:18 +01001159 /* Only send if data is available. */
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001160 if (!ncm->timer_stopping && ncm->skb_tx_data) {
Jim Baxter6d3865f2014-07-07 18:33:18 +01001161 ncm->timer_force_tx = true;
David S. Millerc2c0e8b2014-08-27 17:05:53 -07001162
1163 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1164 * XXX is not sane. The gadget layer should be redesigned so
1165 * XXX that the dev->wrap() invocations to build SKBs is transparent
1166 * XXX and performed in some way outside of the ndo_start_xmit
1167 * XXX interface.
1168 */
1169 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1170
Jim Baxter6d3865f2014-07-07 18:33:18 +01001171 ncm->timer_force_tx = false;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001172 }
Jim Baxter6d3865f2014-07-07 18:33:18 +01001173 return HRTIMER_NORESTART;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001174}
1175
1176static int ncm_unwrap_ntb(struct gether *port,
1177 struct sk_buff *skb,
1178 struct sk_buff_head *list)
1179{
1180 struct f_ncm *ncm = func_to_ncm(&port->func);
1181 __le16 *tmp = (void *) skb->data;
1182 unsigned index, index2;
Jim Baxter370af732014-07-07 18:33:17 +01001183 int ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001184 unsigned dg_len, dg_len2;
1185 unsigned ndp_len;
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001186 unsigned block_len;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001187 struct sk_buff *skb2;
1188 int ret = -EINVAL;
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001189 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1190 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +01001191 const struct ndp_parser_opts *opts = ncm->parser_opts;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001192 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1193 int dgram_counter;
1194
1195 /* dwSignature */
1196 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1197 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1198 skb->len);
1199 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1200 skb->data, 32, false);
1201
1202 goto err;
1203 }
1204 tmp += 2;
1205 /* wHeaderLength */
1206 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1207 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1208 goto err;
1209 }
1210 tmp++; /* skip wSequence */
1211
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001212 block_len = get_ncm(&tmp, opts->block_length);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001213 /* (d)wBlockLength */
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001214 if (block_len > ntb_max) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001215 INFO(port->func.config->cdev, "OUT size exceeded\n");
1216 goto err;
1217 }
1218
Jim Baxter6d3865f2014-07-07 18:33:18 +01001219 ndp_index = get_ncm(&tmp, opts->ndp_index);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001220
Jim Baxter370af732014-07-07 18:33:17 +01001221 /* Run through all the NDP's in the NTB */
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001222 do {
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001223 /*
1224 * NCM 3.2
1225 * dwNdpIndex
1226 */
1227 if (((ndp_index % 4) != 0) ||
1228 (ndp_index < opts->nth_size) ||
1229 (ndp_index > (block_len -
1230 opts->ndp_size))) {
Jim Baxter370af732014-07-07 18:33:17 +01001231 INFO(port->func.config->cdev, "Bad index: %#X\n",
1232 ndp_index);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001233 goto err;
1234 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001235
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001236 /*
1237 * walk through NDP
1238 * dwSignature
1239 */
Jim Baxter370af732014-07-07 18:33:17 +01001240 tmp = (void *)(skb->data + ndp_index);
1241 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1242 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1243 goto err;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001244 }
Jim Baxter370af732014-07-07 18:33:17 +01001245 tmp += 2;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001246
Jim Baxter370af732014-07-07 18:33:17 +01001247 ndp_len = get_unaligned_le16(tmp++);
1248 /*
1249 * NCM 3.3.1
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001250 * wLength
Jim Baxter370af732014-07-07 18:33:17 +01001251 * entry is 2 items
1252 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1253 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1254 * Each entry is a dgram index and a dgram length.
1255 */
1256 if ((ndp_len < opts->ndp_size
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001257 + 2 * 2 * (opts->dgram_item_len * 2)) ||
1258 (ndp_len % opts->ndplen_align != 0)) {
Jim Baxter370af732014-07-07 18:33:17 +01001259 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1260 ndp_len);
1261 goto err;
1262 }
1263 tmp += opts->reserved1;
1264 /* Check for another NDP (d)wNextNdpIndex */
Jim Baxter6d3865f2014-07-07 18:33:18 +01001265 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
Jim Baxter370af732014-07-07 18:33:17 +01001266 tmp += opts->reserved2;
1267
1268 ndp_len -= opts->ndp_size;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001269 index2 = get_ncm(&tmp, opts->dgram_item_len);
1270 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
Jim Baxter370af732014-07-07 18:33:17 +01001271 dgram_counter = 0;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001272
Jim Baxter370af732014-07-07 18:33:17 +01001273 do {
1274 index = index2;
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001275 /* wDatagramIndex[0] */
1276 if ((index < opts->nth_size) ||
1277 (index > block_len - opts->dpe_size)) {
1278 INFO(port->func.config->cdev,
1279 "Bad index: %#X\n", index);
1280 goto err;
1281 }
1282
Jim Baxter370af732014-07-07 18:33:17 +01001283 dg_len = dg_len2;
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001284 /*
1285 * wDatagramLength[0]
1286 * ethernet hdr + crc or larger than max frame size
1287 */
1288 if ((dg_len < 14 + crc_len) ||
1289 (dg_len > frame_max)) {
Jim Baxter370af732014-07-07 18:33:17 +01001290 INFO(port->func.config->cdev,
1291 "Bad dgram length: %#X\n", dg_len);
1292 goto err;
1293 }
1294 if (ncm->is_crc) {
1295 uint32_t crc, crc2;
1296
1297 crc = get_unaligned_le32(skb->data +
1298 index + dg_len -
1299 crc_len);
1300 crc2 = ~crc32_le(~0,
1301 skb->data + index,
1302 dg_len - crc_len);
1303 if (crc != crc2) {
1304 INFO(port->func.config->cdev,
1305 "Bad CRC\n");
1306 goto err;
1307 }
1308 }
1309
1310 index2 = get_ncm(&tmp, opts->dgram_item_len);
1311 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1312
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001313 /* wDatagramIndex[1] */
Brooke Basile2b74b0a02020-08-25 09:07:27 -04001314 if (index2 > block_len - opts->dpe_size) {
1315 INFO(port->func.config->cdev,
1316 "Bad index: %#X\n", index2);
1317 goto err;
1318 }
1319
Jim Baxter66847062014-07-07 18:33:19 +01001320 /*
1321 * Copy the data into a new skb.
1322 * This ensures the truesize is correct
1323 */
1324 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1325 dg_len - crc_len);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001326 if (skb2 == NULL)
1327 goto err;
Johannes Berg59ae1d12017-06-16 14:29:20 +02001328 skb_put_data(skb2, skb->data + index,
1329 dg_len - crc_len);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001330
Jim Baxter370af732014-07-07 18:33:17 +01001331 skb_queue_tail(list, skb2);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001332
Jim Baxter370af732014-07-07 18:33:17 +01001333 ndp_len -= 2 * (opts->dgram_item_len * 2);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001334
Jim Baxter370af732014-07-07 18:33:17 +01001335 dgram_counter++;
Bryan O'Donoghue028296e2020-09-20 18:01:58 +01001336 if (index2 == 0 || dg_len2 == 0)
1337 break;
Jim Baxter370af732014-07-07 18:33:17 +01001338 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1339 } while (ndp_index);
1340
Torsten Polle38314e52016-09-19 10:05:42 +02001341 dev_consume_skb_any(skb);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001342
1343 VDBG(port->func.config->cdev,
1344 "Parsed NTB with %d frames\n", dgram_counter);
1345 return 0;
1346err:
1347 skb_queue_purge(list);
1348 dev_kfree_skb_any(skb);
1349 return ret;
1350}
1351
1352static void ncm_disable(struct usb_function *f)
1353{
1354 struct f_ncm *ncm = func_to_ncm(f);
1355 struct usb_composite_dev *cdev = f->config->cdev;
1356
1357 DBG(cdev, "ncm deactivated\n");
1358
Robert Baldyga6b4012a2015-09-16 12:10:50 +02001359 if (ncm->port.in_ep->enabled) {
Jim Baxter6d3865f2014-07-07 18:33:18 +01001360 ncm->timer_stopping = true;
1361 ncm->netdev = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001362 gether_disconnect(&ncm->port);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001363 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001364
Robert Baldyga6b4012a2015-09-16 12:10:50 +02001365 if (ncm->notify->enabled) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001366 usb_ep_disable(ncm->notify);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001367 ncm->notify->desc = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001368 }
1369}
1370
1371/*-------------------------------------------------------------------------*/
1372
1373/*
1374 * Callbacks let us notify the host about connect/disconnect when the
1375 * net device is opened or closed.
1376 *
1377 * For testing, note that link states on this side include both opened
1378 * and closed variants of:
1379 *
1380 * - disconnected/unconfigured
1381 * - configured but inactive (data alt 0)
1382 * - configured and active (data alt 1)
1383 *
1384 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1385 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1386 * imply the host is actually polling the notification endpoint, and
1387 * likewise that "active" doesn't imply it's actually using the data
1388 * endpoints for traffic.
1389 */
1390
1391static void ncm_open(struct gether *geth)
1392{
1393 struct f_ncm *ncm = func_to_ncm(&geth->func);
1394
1395 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1396
1397 spin_lock(&ncm->lock);
1398 ncm->is_open = true;
1399 ncm_notify(ncm);
1400 spin_unlock(&ncm->lock);
1401}
1402
1403static void ncm_close(struct gether *geth)
1404{
1405 struct f_ncm *ncm = func_to_ncm(&geth->func);
1406
1407 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1408
1409 spin_lock(&ncm->lock);
1410 ncm->is_open = false;
1411 ncm_notify(ncm);
1412 spin_unlock(&ncm->lock);
1413}
1414
1415/*-------------------------------------------------------------------------*/
1416
1417/* ethernet function driver setup/binding */
1418
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001419static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001420{
1421 struct usb_composite_dev *cdev = c->cdev;
1422 struct f_ncm *ncm = func_to_ncm(f);
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001423 struct usb_string *us;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001424 int status;
1425 struct usb_ep *ep;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001426 struct f_ncm_opts *ncm_opts;
1427
1428 if (!can_support_ecm(cdev->gadget))
1429 return -EINVAL;
1430
1431 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
Romain Izard79340922019-04-16 16:07:32 +02001432
1433 if (cdev->use_os_string) {
1434 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
1435 GFP_KERNEL);
1436 if (!f->os_desc_table)
1437 return -ENOMEM;
1438 f->os_desc_n = 1;
1439 f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc;
1440 }
1441
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001442 /*
1443 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1444 * configurations are bound in sequence with list_for_each_entry,
1445 * in each configuration its functions are bound in sequence
1446 * with list_for_each_entry, so we assume no race condition
1447 * with regard to ncm_opts->bound access
1448 */
1449 if (!ncm_opts->bound) {
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001450 mutex_lock(&ncm_opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001451 gether_set_gadget(ncm_opts->net, cdev->gadget);
1452 status = gether_register_netdev(ncm_opts->net);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001453 mutex_unlock(&ncm_opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001454 if (status)
Romain Izard79340922019-04-16 16:07:32 +02001455 goto fail;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001456 ncm_opts->bound = true;
1457 }
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001458 us = usb_gstrings_attach(cdev, ncm_strings,
1459 ARRAY_SIZE(ncm_string_defs));
Romain Izard79340922019-04-16 16:07:32 +02001460 if (IS_ERR(us)) {
1461 status = PTR_ERR(us);
1462 goto fail;
1463 }
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001464 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1465 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1466 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1467 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1468 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001469
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001470 /* allocate instance-specific interface IDs */
1471 status = usb_interface_id(c, f);
1472 if (status < 0)
1473 goto fail;
1474 ncm->ctrl_id = status;
1475 ncm_iad_desc.bFirstInterface = status;
1476
1477 ncm_control_intf.bInterfaceNumber = status;
1478 ncm_union_desc.bMasterInterface0 = status;
1479
Romain Izard79340922019-04-16 16:07:32 +02001480 if (cdev->use_os_string)
1481 f->os_desc_table[0].if_id =
1482 ncm_iad_desc.bFirstInterface;
1483
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001484 status = usb_interface_id(c, f);
1485 if (status < 0)
1486 goto fail;
1487 ncm->data_id = status;
1488
1489 ncm_data_nop_intf.bInterfaceNumber = status;
1490 ncm_data_intf.bInterfaceNumber = status;
1491 ncm_union_desc.bSlaveInterface0 = status;
1492
1493 status = -ENODEV;
1494
1495 /* allocate instance-specific endpoints */
1496 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1497 if (!ep)
1498 goto fail;
1499 ncm->port.in_ep = ep;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001500
1501 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1502 if (!ep)
1503 goto fail;
1504 ncm->port.out_ep = ep;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001505
1506 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1507 if (!ep)
1508 goto fail;
1509 ncm->notify = ep;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001510
1511 status = -ENOMEM;
1512
1513 /* allocate notification request and buffer */
1514 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1515 if (!ncm->notify_req)
1516 goto fail;
1517 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1518 if (!ncm->notify_req->buf)
1519 goto fail;
1520 ncm->notify_req->context = ncm;
1521 ncm->notify_req->complete = ncm_notify_complete;
1522
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001523 /*
1524 * support all relevant hardware speeds... we expect that when
1525 * hardware is dual speed, all bulk-capable endpoints work at
1526 * both speeds
1527 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001528 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1529 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1530 hs_ncm_notify_desc.bEndpointAddress =
1531 fs_ncm_notify_desc.bEndpointAddress;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001532
Jussi Kivilinna16501132016-08-12 17:28:05 +03001533 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1534 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1535 ss_ncm_notify_desc.bEndpointAddress =
1536 fs_ncm_notify_desc.bEndpointAddress;
1537
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001538 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
Lorenzo Colitti7974ecd2020-08-25 14:55:05 +09001539 ncm_ss_function, ncm_ss_function);
Pavitrakumar Managutte8b920f12014-10-27 22:49:26 +05301540 if (status)
1541 goto fail;
1542
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001543 /*
1544 * NOTE: all that is done without knowing or caring about
1545 * the network link ... which is unavailable to this code
1546 * until we're activated via set_alt().
1547 */
1548
1549 ncm->port.open = ncm_open;
1550 ncm->port.close = ncm_close;
1551
Thomas Gleixnerb1a31a52017-12-21 11:42:04 +01001552 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001553 ncm->task_timer.function = ncm_tx_timeout;
1554
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001555 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Jussi Kivilinna16501132016-08-12 17:28:05 +03001556 gadget_is_superspeed(c->cdev->gadget) ? "super" :
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001557 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1558 ncm->port.in_ep->name, ncm->port.out_ep->name,
1559 ncm->notify->name);
1560 return 0;
1561
1562fail:
Romain Izard79340922019-04-16 16:07:32 +02001563 kfree(f->os_desc_table);
1564 f->os_desc_n = 0;
1565
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001566 if (ncm->notify_req) {
1567 kfree(ncm->notify_req->buf);
1568 usb_ep_free_request(ncm->notify, ncm->notify_req);
1569 }
1570
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001571 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1572
1573 return status;
1574}
1575
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001576static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1577{
1578 return container_of(to_config_group(item), struct f_ncm_opts,
1579 func_inst.group);
1580}
1581
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001582/* f_ncm_item_ops */
1583USB_ETHERNET_CONFIGFS_ITEM(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001584
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001585/* f_ncm_opts_dev_addr */
1586USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001587
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001588/* f_ncm_opts_host_addr */
1589USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001590
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001591/* f_ncm_opts_qmult */
1592USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001593
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001594/* f_ncm_opts_ifname */
1595USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001596
1597static struct configfs_attribute *ncm_attrs[] = {
Christoph Hellwigf9a63da2015-10-03 15:32:42 +02001598 &ncm_opts_attr_dev_addr,
1599 &ncm_opts_attr_host_addr,
1600 &ncm_opts_attr_qmult,
1601 &ncm_opts_attr_ifname,
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001602 NULL,
1603};
1604
Bhumika Goyal97363902017-10-16 17:18:41 +02001605static const struct config_item_type ncm_func_type = {
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001606 .ct_item_ops = &ncm_item_ops,
1607 .ct_attrs = ncm_attrs,
1608 .ct_owner = THIS_MODULE,
1609};
1610
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001611static void ncm_free_inst(struct usb_function_instance *f)
1612{
1613 struct f_ncm_opts *opts;
1614
1615 opts = container_of(f, struct f_ncm_opts, func_inst);
1616 if (opts->bound)
1617 gether_cleanup(netdev_priv(opts->net));
1618 else
1619 free_netdev(opts->net);
Romain Izard79340922019-04-16 16:07:32 +02001620 kfree(opts->ncm_interf_group);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001621 kfree(opts);
1622}
1623
1624static struct usb_function_instance *ncm_alloc_inst(void)
1625{
1626 struct f_ncm_opts *opts;
Romain Izard79340922019-04-16 16:07:32 +02001627 struct usb_os_desc *descs[1];
1628 char *names[1];
1629 struct config_group *ncm_interf_group;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001630
1631 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1632 if (!opts)
1633 return ERR_PTR(-ENOMEM);
Romain Izard79340922019-04-16 16:07:32 +02001634 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
1635
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001636 mutex_init(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001637 opts->func_inst.free_func_inst = ncm_free_inst;
1638 opts->net = gether_setup_default();
Andrzej Pietrasiewicz172d9342013-07-25 09:13:18 +02001639 if (IS_ERR(opts->net)) {
1640 struct net_device *net = opts->net;
1641 kfree(opts);
1642 return ERR_CAST(net);
1643 }
Romain Izard79340922019-04-16 16:07:32 +02001644 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
1645
1646 descs[0] = &opts->ncm_os_desc;
1647 names[0] = "ncm";
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001648
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001649 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
Romain Izard79340922019-04-16 16:07:32 +02001650 ncm_interf_group =
1651 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1652 names, THIS_MODULE);
1653 if (IS_ERR(ncm_interf_group)) {
1654 ncm_free_inst(&opts->func_inst);
1655 return ERR_CAST(ncm_interf_group);
1656 }
1657 opts->ncm_interf_group = ncm_interf_group;
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001658
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001659 return &opts->func_inst;
1660}
1661
1662static void ncm_free(struct usb_function *f)
1663{
1664 struct f_ncm *ncm;
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001665 struct f_ncm_opts *opts;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001666
1667 ncm = func_to_ncm(f);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001668 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001669 kfree(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001670 mutex_lock(&opts->lock);
1671 opts->refcnt--;
1672 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001673}
1674
1675static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1676{
1677 struct f_ncm *ncm = func_to_ncm(f);
1678
1679 DBG(c->cdev, "ncm unbind\n");
1680
Jim Baxter6d3865f2014-07-07 18:33:18 +01001681 hrtimer_cancel(&ncm->task_timer);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001682
Romain Izard79340922019-04-16 16:07:32 +02001683 kfree(f->os_desc_table);
1684 f->os_desc_n = 0;
1685
Jim Baxter6d3865f2014-07-07 18:33:18 +01001686 ncm_string_defs[0].id = 0;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001687 usb_free_all_descriptors(f);
1688
Bryan O'Donoghue5b24c282020-01-09 13:17:21 +00001689 if (atomic_read(&ncm->notify_count)) {
1690 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1691 atomic_set(&ncm->notify_count, 0);
1692 }
1693
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001694 kfree(ncm->notify_req->buf);
1695 usb_ep_free_request(ncm->notify, ncm->notify_req);
1696}
1697
Jingoo Han36a61122013-12-16 18:43:32 +09001698static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001699{
1700 struct f_ncm *ncm;
1701 struct f_ncm_opts *opts;
1702 int status;
1703
1704 /* allocate and initialize one new instance */
1705 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1706 if (!ncm)
1707 return ERR_PTR(-ENOMEM);
1708
1709 opts = container_of(fi, struct f_ncm_opts, func_inst);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001710 mutex_lock(&opts->lock);
1711 opts->refcnt++;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001712
1713 /* export host's Ethernet address in CDC format */
1714 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1715 sizeof(ncm->ethaddr));
1716 if (status < 12) { /* strlen("01234567890a") */
1717 kfree(ncm);
Wei Yongjunc92834c2013-06-18 11:43:29 +08001718 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001719 return ERR_PTR(-EINVAL);
1720 }
1721 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1722
1723 spin_lock_init(&ncm->lock);
1724 ncm_reset_values(ncm);
1725 ncm->port.ioport = netdev_priv(opts->net);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001726 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001727 ncm->port.is_fixed = true;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001728 ncm->port.supports_multi_frame = true;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001729
1730 ncm->port.func.name = "cdc_network";
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001731 /* descriptors are per-instance copies */
1732 ncm->port.func.bind = ncm_bind;
1733 ncm->port.func.unbind = ncm_unbind;
1734 ncm->port.func.set_alt = ncm_set_alt;
1735 ncm->port.func.get_alt = ncm_get_alt;
1736 ncm->port.func.setup = ncm_setup;
1737 ncm->port.func.disable = ncm_disable;
1738 ncm->port.func.free_func = ncm_free;
1739
1740 ncm->port.wrap = ncm_wrap_ntb;
1741 ncm->port.unwrap = ncm_unwrap_ntb;
1742
1743 return &ncm->port.func;
1744}
1745
1746DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1747MODULE_LICENSE("GPL");
1748MODULE_AUTHOR("Yauheni Kaliuta");