blob: f48b08ef31a2191f57248382dc1e0226daf9e6fa [file] [log] [blame]
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001/*
2 * f_ncm.c -- USB CDC Network (NCM) link function driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from f_ecm.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020016 */
17
18#include <linux/kernel.h>
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +020019#include <linux/module.h>
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020020#include <linux/device.h>
21#include <linux/etherdevice.h>
22#include <linux/crc32.h>
23
24#include <linux/usb/cdc.h>
25
26#include "u_ether.h"
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +020027#include "u_ether_configfs.h"
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +020028#include "u_ncm.h"
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020029
30/*
31 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
32 * NCM is intended to be used with high-speed network attachments.
33 *
34 * Note that NCM requires the use of "alternate settings" for its data
35 * interface. This means that the set_alt() method has real work to do,
36 * and also means that a get_alt() method is required.
37 */
38
39/* to trigger crc/non-crc ndp signature */
40
41#define NCM_NDP_HDR_CRC_MASK 0x01000000
42#define NCM_NDP_HDR_CRC 0x01000000
43#define NCM_NDP_HDR_NOCRC 0x00000000
44
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020045enum ncm_notify_state {
46 NCM_NOTIFY_NONE, /* don't notify */
47 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
48 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
49};
50
51struct f_ncm {
52 struct gether port;
53 u8 ctrl_id, data_id;
54
55 char ethaddr[14];
56
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020057 struct usb_ep *notify;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020058 struct usb_request *notify_req;
59 u8 notify_state;
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +000060 atomic_t notify_count;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020061 bool is_open;
62
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +010063 const struct ndp_parser_opts *parser_opts;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020064 bool is_crc;
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +010065 u32 ndp_sign;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020066
67 /*
68 * for notification, it is accessed from both
69 * callback and ethernet open/close
70 */
71 spinlock_t lock;
Jim Baxter6d3865f2014-07-07 18:33:18 +010072
73 struct net_device *netdev;
74
75 /* For multi-frame NDP TX */
76 struct sk_buff *skb_tx_data;
77 struct sk_buff *skb_tx_ndp;
78 u16 ndp_dgram_count;
79 bool timer_force_tx;
80 struct tasklet_struct tx_tasklet;
81 struct hrtimer task_timer;
82
83 bool timer_stopping;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020084};
85
86static inline struct f_ncm *func_to_ncm(struct usb_function *f)
87{
88 return container_of(f, struct f_ncm, port.func);
89}
90
91/* peak (theoretical) bulk transfer rate in bits-per-second */
92static inline unsigned ncm_bitrate(struct usb_gadget *g)
93{
Jussi Kivilinna16501132016-08-12 17:28:05 +030094 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
95 return 13 * 1024 * 8 * 1000 * 8;
96 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +020097 return 13 * 512 * 8 * 1000 * 8;
98 else
99 return 19 * 64 * 1 * 1000 * 8;
100}
101
102/*-------------------------------------------------------------------------*/
103
104/*
105 * We cannot group frames so use just the minimal size which ok to put
106 * one max-size ethernet frame.
107 * If the host can group frames, allow it to do that, 16K is selected,
108 * because it's used by default by the current linux host driver
109 */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100110#define NTB_DEFAULT_IN_SIZE 16384
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200111#define NTB_OUT_SIZE 16384
112
Jim Baxter6d3865f2014-07-07 18:33:18 +0100113/* Allocation for storing the NDP, 32 should suffice for a
114 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
115 * be transmitted in a single 16kB skb, though when sending full size
116 * packets this limit will be plenty.
117 * Smaller packets are not likely to be trying to maximize the
118 * throughput and will be mstly sending smaller infrequent frames.
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200119 */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100120#define TX_MAX_NUM_DPE 32
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200121
Jim Baxter6d3865f2014-07-07 18:33:18 +0100122/* Delay for the transmit to wait before sending an unfilled NTB frame. */
123#define TX_TIMEOUT_NSECS 300000
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200124
125#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
126 USB_CDC_NCM_NTB32_SUPPORTED)
127
128static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
Dmytro Milinevskyyf72e3b72012-10-05 01:44:04 +0300129 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200130 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
131 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
132 .wNdpInDivisor = cpu_to_le16(4),
133 .wNdpInPayloadRemainder = cpu_to_le16(0),
134 .wNdpInAlignment = cpu_to_le16(4),
135
136 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
137 .wNdpOutDivisor = cpu_to_le16(4),
138 .wNdpOutPayloadRemainder = cpu_to_le16(0),
139 .wNdpOutAlignment = cpu_to_le16(4),
140};
141
142/*
143 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
144 * packet, to simplify cancellation; and a big transfer interval, to
145 * waste less bandwidth.
146 */
147
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200148#define NCM_STATUS_INTERVAL_MS 32
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200149#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
150
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200151static struct usb_interface_assoc_descriptor ncm_iad_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200152 .bLength = sizeof ncm_iad_desc,
153 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
154
155 /* .bFirstInterface = DYNAMIC, */
156 .bInterfaceCount = 2, /* control + data */
157 .bFunctionClass = USB_CLASS_COMM,
158 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
159 .bFunctionProtocol = USB_CDC_PROTO_NONE,
160 /* .iFunction = DYNAMIC */
161};
162
163/* interface descriptor: */
164
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200165static struct usb_interface_descriptor ncm_control_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200166 .bLength = sizeof ncm_control_intf,
167 .bDescriptorType = USB_DT_INTERFACE,
168
169 /* .bInterfaceNumber = DYNAMIC */
170 .bNumEndpoints = 1,
171 .bInterfaceClass = USB_CLASS_COMM,
172 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
173 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
174 /* .iInterface = DYNAMIC */
175};
176
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200177static struct usb_cdc_header_desc ncm_header_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200178 .bLength = sizeof ncm_header_desc,
179 .bDescriptorType = USB_DT_CS_INTERFACE,
180 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
181
182 .bcdCDC = cpu_to_le16(0x0110),
183};
184
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200185static struct usb_cdc_union_desc ncm_union_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200186 .bLength = sizeof(ncm_union_desc),
187 .bDescriptorType = USB_DT_CS_INTERFACE,
188 .bDescriptorSubType = USB_CDC_UNION_TYPE,
189 /* .bMasterInterface0 = DYNAMIC */
190 /* .bSlaveInterface0 = DYNAMIC */
191};
192
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200193static struct usb_cdc_ether_desc ecm_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200194 .bLength = sizeof ecm_desc,
195 .bDescriptorType = USB_DT_CS_INTERFACE,
196 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
197
198 /* this descriptor actually adds value, surprise! */
199 /* .iMACAddress = DYNAMIC */
200 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
201 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
202 .wNumberMCFilters = cpu_to_le16(0),
203 .bNumberPowerFilters = 0,
204};
205
206#define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
207
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200208static struct usb_cdc_ncm_desc ncm_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200209 .bLength = sizeof ncm_desc,
210 .bDescriptorType = USB_DT_CS_INTERFACE,
211 .bDescriptorSubType = USB_CDC_NCM_TYPE,
212
213 .bcdNcmVersion = cpu_to_le16(0x0100),
214 /* can process SetEthernetPacketFilter */
215 .bmNetworkCapabilities = NCAPS,
216};
217
218/* the default data interface has no endpoints ... */
219
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200220static struct usb_interface_descriptor ncm_data_nop_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200221 .bLength = sizeof ncm_data_nop_intf,
222 .bDescriptorType = USB_DT_INTERFACE,
223
224 .bInterfaceNumber = 1,
225 .bAlternateSetting = 0,
226 .bNumEndpoints = 0,
227 .bInterfaceClass = USB_CLASS_CDC_DATA,
228 .bInterfaceSubClass = 0,
229 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
230 /* .iInterface = DYNAMIC */
231};
232
233/* ... but the "real" data interface has two bulk endpoints */
234
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200235static struct usb_interface_descriptor ncm_data_intf = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200236 .bLength = sizeof ncm_data_intf,
237 .bDescriptorType = USB_DT_INTERFACE,
238
239 .bInterfaceNumber = 1,
240 .bAlternateSetting = 1,
241 .bNumEndpoints = 2,
242 .bInterfaceClass = USB_CLASS_CDC_DATA,
243 .bInterfaceSubClass = 0,
244 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
245 /* .iInterface = DYNAMIC */
246};
247
248/* full speed support: */
249
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200250static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200251 .bLength = USB_DT_ENDPOINT_SIZE,
252 .bDescriptorType = USB_DT_ENDPOINT,
253
254 .bEndpointAddress = USB_DIR_IN,
255 .bmAttributes = USB_ENDPOINT_XFER_INT,
256 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200257 .bInterval = NCM_STATUS_INTERVAL_MS,
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200258};
259
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200260static struct usb_endpoint_descriptor fs_ncm_in_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200261 .bLength = USB_DT_ENDPOINT_SIZE,
262 .bDescriptorType = USB_DT_ENDPOINT,
263
264 .bEndpointAddress = USB_DIR_IN,
265 .bmAttributes = USB_ENDPOINT_XFER_BULK,
266};
267
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200268static struct usb_endpoint_descriptor fs_ncm_out_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200269 .bLength = USB_DT_ENDPOINT_SIZE,
270 .bDescriptorType = USB_DT_ENDPOINT,
271
272 .bEndpointAddress = USB_DIR_OUT,
273 .bmAttributes = USB_ENDPOINT_XFER_BULK,
274};
275
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200276static struct usb_descriptor_header *ncm_fs_function[] = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200277 (struct usb_descriptor_header *) &ncm_iad_desc,
278 /* CDC NCM control descriptors */
279 (struct usb_descriptor_header *) &ncm_control_intf,
280 (struct usb_descriptor_header *) &ncm_header_desc,
281 (struct usb_descriptor_header *) &ncm_union_desc,
282 (struct usb_descriptor_header *) &ecm_desc,
283 (struct usb_descriptor_header *) &ncm_desc,
284 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
285 /* data interface, altsettings 0 and 1 */
286 (struct usb_descriptor_header *) &ncm_data_nop_intf,
287 (struct usb_descriptor_header *) &ncm_data_intf,
288 (struct usb_descriptor_header *) &fs_ncm_in_desc,
289 (struct usb_descriptor_header *) &fs_ncm_out_desc,
290 NULL,
291};
292
293/* high speed support: */
294
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200295static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200296 .bLength = USB_DT_ENDPOINT_SIZE,
297 .bDescriptorType = USB_DT_ENDPOINT,
298
299 .bEndpointAddress = USB_DIR_IN,
300 .bmAttributes = USB_ENDPOINT_XFER_INT,
301 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200302 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200303};
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200304static struct usb_endpoint_descriptor hs_ncm_in_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200305 .bLength = USB_DT_ENDPOINT_SIZE,
306 .bDescriptorType = USB_DT_ENDPOINT,
307
308 .bEndpointAddress = USB_DIR_IN,
309 .bmAttributes = USB_ENDPOINT_XFER_BULK,
310 .wMaxPacketSize = cpu_to_le16(512),
311};
312
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200313static struct usb_endpoint_descriptor hs_ncm_out_desc = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316
317 .bEndpointAddress = USB_DIR_OUT,
318 .bmAttributes = USB_ENDPOINT_XFER_BULK,
319 .wMaxPacketSize = cpu_to_le16(512),
320};
321
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +0200322static struct usb_descriptor_header *ncm_hs_function[] = {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200323 (struct usb_descriptor_header *) &ncm_iad_desc,
324 /* CDC NCM control descriptors */
325 (struct usb_descriptor_header *) &ncm_control_intf,
326 (struct usb_descriptor_header *) &ncm_header_desc,
327 (struct usb_descriptor_header *) &ncm_union_desc,
328 (struct usb_descriptor_header *) &ecm_desc,
329 (struct usb_descriptor_header *) &ncm_desc,
330 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
331 /* data interface, altsettings 0 and 1 */
332 (struct usb_descriptor_header *) &ncm_data_nop_intf,
333 (struct usb_descriptor_header *) &ncm_data_intf,
334 (struct usb_descriptor_header *) &hs_ncm_in_desc,
335 (struct usb_descriptor_header *) &hs_ncm_out_desc,
336 NULL,
337};
338
Jussi Kivilinna16501132016-08-12 17:28:05 +0300339/* super speed support: */
340
341static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
Mayank Ranae490d552015-03-09 11:31:18 -0700342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300344
Mayank Ranae490d552015-03-09 11:31:18 -0700345 .bEndpointAddress = USB_DIR_IN,
346 .bmAttributes = USB_ENDPOINT_XFER_INT,
347 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
Jussi Kivilinna16501132016-08-12 17:28:05 +0300348 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
Mayank Ranae490d552015-03-09 11:31:18 -0700349};
350
Jussi Kivilinna16501132016-08-12 17:28:05 +0300351static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
352 .bLength = sizeof(ss_ncm_notify_comp_desc),
Mayank Ranae490d552015-03-09 11:31:18 -0700353 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300354
Mayank Ranae490d552015-03-09 11:31:18 -0700355 /* the following 3 values can be tweaked if necessary */
356 /* .bMaxBurst = 0, */
357 /* .bmAttributes = 0, */
358 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
359};
360
Jussi Kivilinna16501132016-08-12 17:28:05 +0300361static struct usb_endpoint_descriptor ss_ncm_in_desc = {
Mayank Ranae490d552015-03-09 11:31:18 -0700362 .bLength = USB_DT_ENDPOINT_SIZE,
363 .bDescriptorType = USB_DT_ENDPOINT,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300364
Mayank Ranae490d552015-03-09 11:31:18 -0700365 .bEndpointAddress = USB_DIR_IN,
366 .bmAttributes = USB_ENDPOINT_XFER_BULK,
367 .wMaxPacketSize = cpu_to_le16(1024),
368};
369
Jussi Kivilinna16501132016-08-12 17:28:05 +0300370static struct usb_endpoint_descriptor ss_ncm_out_desc = {
Mayank Ranae490d552015-03-09 11:31:18 -0700371 .bLength = USB_DT_ENDPOINT_SIZE,
372 .bDescriptorType = USB_DT_ENDPOINT,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300373
Mayank Ranae490d552015-03-09 11:31:18 -0700374 .bEndpointAddress = USB_DIR_OUT,
375 .bmAttributes = USB_ENDPOINT_XFER_BULK,
376 .wMaxPacketSize = cpu_to_le16(1024),
377};
378
Jussi Kivilinna16501132016-08-12 17:28:05 +0300379static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
380 .bLength = sizeof(ss_ncm_bulk_comp_desc),
Mayank Ranae490d552015-03-09 11:31:18 -0700381 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300382
Mayank Ranae490d552015-03-09 11:31:18 -0700383 /* the following 2 values can be tweaked if necessary */
384 /* .bMaxBurst = 0, */
385 /* .bmAttributes = 0, */
386};
387
388static struct usb_descriptor_header *ncm_ss_function[] = {
389 (struct usb_descriptor_header *) &ncm_iad_desc,
390 /* CDC NCM control descriptors */
391 (struct usb_descriptor_header *) &ncm_control_intf,
392 (struct usb_descriptor_header *) &ncm_header_desc,
393 (struct usb_descriptor_header *) &ncm_union_desc,
394 (struct usb_descriptor_header *) &ecm_desc,
395 (struct usb_descriptor_header *) &ncm_desc,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300396 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
397 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
Mayank Ranae490d552015-03-09 11:31:18 -0700398 /* data interface, altsettings 0 and 1 */
399 (struct usb_descriptor_header *) &ncm_data_nop_intf,
400 (struct usb_descriptor_header *) &ncm_data_intf,
Jussi Kivilinna16501132016-08-12 17:28:05 +0300401 (struct usb_descriptor_header *) &ss_ncm_in_desc,
402 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
403 (struct usb_descriptor_header *) &ss_ncm_out_desc,
404 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
Mayank Ranae490d552015-03-09 11:31:18 -0700405 NULL,
406};
407
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200408/* string descriptors: */
409
410#define STRING_CTRL_IDX 0
411#define STRING_MAC_IDX 1
412#define STRING_DATA_IDX 2
413#define STRING_IAD_IDX 3
414
415static struct usb_string ncm_string_defs[] = {
416 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200417 [STRING_MAC_IDX].s = "",
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200418 [STRING_DATA_IDX].s = "CDC Network Data",
419 [STRING_IAD_IDX].s = "CDC NCM",
420 { } /* end of list */
421};
422
423static struct usb_gadget_strings ncm_string_table = {
424 .language = 0x0409, /* en-us */
425 .strings = ncm_string_defs,
426};
427
428static struct usb_gadget_strings *ncm_strings[] = {
429 &ncm_string_table,
430 NULL,
431};
432
433/*
434 * Here are options for NCM Datagram Pointer table (NDP) parser.
435 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
436 * in NDP16 offsets and sizes fields are 1 16bit word wide,
437 * in NDP32 -- 2 16bit words wide. Also signatures are different.
438 * To make the parser code the same, put the differences in the structure,
439 * and switch pointers to the structures when the format is changed.
440 */
441
442struct ndp_parser_opts {
443 u32 nth_sign;
444 u32 ndp_sign;
445 unsigned nth_size;
446 unsigned ndp_size;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100447 unsigned dpe_size;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200448 unsigned ndplen_align;
449 /* sizes in u16 units */
450 unsigned dgram_item_len; /* index or length */
451 unsigned block_length;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100452 unsigned ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200453 unsigned reserved1;
454 unsigned reserved2;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100455 unsigned next_ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200456};
457
458#define INIT_NDP16_OPTS { \
459 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
460 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
461 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
462 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100463 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200464 .ndplen_align = 4, \
465 .dgram_item_len = 1, \
466 .block_length = 1, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100467 .ndp_index = 1, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200468 .reserved1 = 0, \
469 .reserved2 = 0, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100470 .next_ndp_index = 1, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200471 }
472
473
474#define INIT_NDP32_OPTS { \
475 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
476 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
477 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
478 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100479 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200480 .ndplen_align = 8, \
481 .dgram_item_len = 2, \
482 .block_length = 2, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100483 .ndp_index = 2, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200484 .reserved1 = 1, \
485 .reserved2 = 2, \
Jim Baxter6d3865f2014-07-07 18:33:18 +0100486 .next_ndp_index = 2, \
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200487 }
488
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +0100489static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
490static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200491
492static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
493{
494 switch (size) {
495 case 1:
496 put_unaligned_le16((u16)val, *p);
497 break;
498 case 2:
499 put_unaligned_le32((u32)val, *p);
500
501 break;
502 default:
503 BUG();
504 }
505
506 *p += size;
507}
508
509static inline unsigned get_ncm(__le16 **p, unsigned size)
510{
511 unsigned tmp;
512
513 switch (size) {
514 case 1:
515 tmp = get_unaligned_le16(*p);
516 break;
517 case 2:
518 tmp = get_unaligned_le32(*p);
519 break;
520 default:
521 BUG();
522 }
523
524 *p += size;
525 return tmp;
526}
527
528/*-------------------------------------------------------------------------*/
529
530static inline void ncm_reset_values(struct f_ncm *ncm)
531{
532 ncm->parser_opts = &ndp16_opts;
533 ncm->is_crc = false;
534 ncm->port.cdc_filter = DEFAULT_FILTER;
535
536 /* doesn't make sense for ncm, fixed size used */
537 ncm->port.header_len = 0;
538
539 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
540 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
541}
542
543/*
544 * Context: ncm->lock held
545 */
546static void ncm_do_notify(struct f_ncm *ncm)
547{
548 struct usb_request *req = ncm->notify_req;
549 struct usb_cdc_notification *event;
550 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
551 __le32 *data;
552 int status;
553
554 /* notification already in flight? */
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000555 if (atomic_read(&ncm->notify_count))
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200556 return;
557
558 event = req->buf;
559 switch (ncm->notify_state) {
560 case NCM_NOTIFY_NONE:
561 return;
562
563 case NCM_NOTIFY_CONNECT:
564 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
565 if (ncm->is_open)
566 event->wValue = cpu_to_le16(1);
567 else
568 event->wValue = cpu_to_le16(0);
569 event->wLength = 0;
570 req->length = sizeof *event;
571
572 DBG(cdev, "notify connect %s\n",
573 ncm->is_open ? "true" : "false");
574 ncm->notify_state = NCM_NOTIFY_NONE;
575 break;
576
577 case NCM_NOTIFY_SPEED:
578 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
579 event->wValue = cpu_to_le16(0);
580 event->wLength = cpu_to_le16(8);
581 req->length = NCM_STATUS_BYTECOUNT;
582
583 /* SPEED_CHANGE data is up/down speeds in bits/sec */
584 data = req->buf + sizeof *event;
585 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
586 data[1] = data[0];
587
588 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
589 ncm->notify_state = NCM_NOTIFY_CONNECT;
590 break;
591 }
592 event->bmRequestType = 0xA1;
593 event->wIndex = cpu_to_le16(ncm->ctrl_id);
594
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000595 atomic_inc(&ncm->notify_count);
596
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200597 /*
598 * In double buffering if there is a space in FIFO,
599 * completion callback can be called right after the call,
600 * so unlocking
601 */
602 spin_unlock(&ncm->lock);
603 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
604 spin_lock(&ncm->lock);
605 if (status < 0) {
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000606 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200607 DBG(cdev, "notify --> %d\n", status);
608 }
609}
610
611/*
612 * Context: ncm->lock held
613 */
614static void ncm_notify(struct f_ncm *ncm)
615{
616 /*
617 * NOTE on most versions of Linux, host side cdc-ethernet
618 * won't listen for notifications until its netdevice opens.
619 * The first notification then sits in the FIFO for a long
620 * time, and the second one is queued.
621 *
622 * If ncm_notify() is called before the second (CONNECT)
623 * notification is sent, then it will reset to send the SPEED
624 * notificaion again (and again, and again), but it's not a problem
625 */
626 ncm->notify_state = NCM_NOTIFY_SPEED;
627 ncm_do_notify(ncm);
628}
629
630static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
631{
632 struct f_ncm *ncm = req->context;
633 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
634 struct usb_cdc_notification *event = req->buf;
635
636 spin_lock(&ncm->lock);
637 switch (req->status) {
638 case 0:
639 VDBG(cdev, "Notification %02x sent\n",
640 event->bNotificationType);
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000641 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200642 break;
643 case -ECONNRESET:
644 case -ESHUTDOWN:
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000645 atomic_set(&ncm->notify_count, 0);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200646 ncm->notify_state = NCM_NOTIFY_NONE;
647 break;
648 default:
649 DBG(cdev, "event %02x --> %d\n",
650 event->bNotificationType, req->status);
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +0000651 atomic_dec(&ncm->notify_count);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200652 break;
653 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200654 ncm_do_notify(ncm);
655 spin_unlock(&ncm->lock);
656}
657
658static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
659{
660 /* now for SET_NTB_INPUT_SIZE only */
661 unsigned in_size;
662 struct usb_function *f = req->context;
663 struct f_ncm *ncm = func_to_ncm(f);
Robert Baldyga35bfde32015-09-16 12:10:40 +0200664 struct usb_composite_dev *cdev = f->config->cdev;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200665
666 req->context = NULL;
667 if (req->status || req->actual != req->length) {
668 DBG(cdev, "Bad control-OUT transfer\n");
669 goto invalid;
670 }
671
672 in_size = get_unaligned_le32(req->buf);
673 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
674 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
675 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
676 goto invalid;
677 }
678
679 ncm->port.fixed_in_len = in_size;
680 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
681 return;
682
683invalid:
684 usb_ep_set_halt(ep);
685 return;
686}
687
688static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
689{
690 struct f_ncm *ncm = func_to_ncm(f);
691 struct usb_composite_dev *cdev = f->config->cdev;
692 struct usb_request *req = cdev->req;
693 int value = -EOPNOTSUPP;
694 u16 w_index = le16_to_cpu(ctrl->wIndex);
695 u16 w_value = le16_to_cpu(ctrl->wValue);
696 u16 w_length = le16_to_cpu(ctrl->wLength);
697
698 /*
699 * composite driver infrastructure handles everything except
700 * CDC class messages; interface activation uses set_alt().
701 */
702 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
703 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
704 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
705 /*
706 * see 6.2.30: no data, wIndex = interface,
707 * wValue = packet filter bitmap
708 */
709 if (w_length != 0 || w_index != ncm->ctrl_id)
710 goto invalid;
711 DBG(cdev, "packet filter %02x\n", w_value);
712 /*
713 * REVISIT locking of cdc_filter. This assumes the UDC
714 * driver won't have a concurrent packet TX irq running on
715 * another CPU; or that if it does, this write is atomic...
716 */
717 ncm->port.cdc_filter = w_value;
718 value = 0;
719 break;
720 /*
721 * and optionally:
722 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
723 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
724 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
725 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
726 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
727 * case USB_CDC_GET_ETHERNET_STATISTIC:
728 */
729
730 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
731 | USB_CDC_GET_NTB_PARAMETERS:
732
733 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
734 goto invalid;
735 value = w_length > sizeof ntb_parameters ?
736 sizeof ntb_parameters : w_length;
737 memcpy(req->buf, &ntb_parameters, value);
738 VDBG(cdev, "Host asked NTB parameters\n");
739 break;
740
741 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
742 | USB_CDC_GET_NTB_INPUT_SIZE:
743
744 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
745 goto invalid;
746 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
747 value = 4;
748 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
749 ncm->port.fixed_in_len);
750 break;
751
752 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
753 | USB_CDC_SET_NTB_INPUT_SIZE:
754 {
755 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
756 goto invalid;
757 req->complete = ncm_ep0out_complete;
758 req->length = w_length;
759 req->context = f;
760
761 value = req->length;
762 break;
763 }
764
765 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
766 | USB_CDC_GET_NTB_FORMAT:
767 {
768 uint16_t format;
769
770 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
771 goto invalid;
772 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
773 put_unaligned_le16(format, req->buf);
774 value = 2;
775 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
776 break;
777 }
778
779 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
780 | USB_CDC_SET_NTB_FORMAT:
781 {
782 if (w_length != 0 || w_index != ncm->ctrl_id)
783 goto invalid;
784 switch (w_value) {
785 case 0x0000:
786 ncm->parser_opts = &ndp16_opts;
787 DBG(cdev, "NCM16 selected\n");
788 break;
789 case 0x0001:
790 ncm->parser_opts = &ndp32_opts;
791 DBG(cdev, "NCM32 selected\n");
792 break;
793 default:
794 goto invalid;
795 }
796 value = 0;
797 break;
798 }
799 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
800 | USB_CDC_GET_CRC_MODE:
801 {
802 uint16_t is_crc;
803
804 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
805 goto invalid;
806 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
807 put_unaligned_le16(is_crc, req->buf);
808 value = 2;
809 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
810 break;
811 }
812
813 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
814 | USB_CDC_SET_CRC_MODE:
815 {
816 int ndp_hdr_crc = 0;
817
818 if (w_length != 0 || w_index != ncm->ctrl_id)
819 goto invalid;
820 switch (w_value) {
821 case 0x0000:
822 ncm->is_crc = false;
823 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
824 DBG(cdev, "non-CRC mode selected\n");
825 break;
826 case 0x0001:
827 ncm->is_crc = true;
828 ndp_hdr_crc = NCM_NDP_HDR_CRC;
829 DBG(cdev, "CRC mode selected\n");
830 break;
831 default:
832 goto invalid;
833 }
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +0100834 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200835 value = 0;
836 break;
837 }
838
839 /* and disabled in ncm descriptor: */
840 /* case USB_CDC_GET_NET_ADDRESS: */
841 /* case USB_CDC_SET_NET_ADDRESS: */
842 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
843 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
844
845 default:
846invalid:
847 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
848 ctrl->bRequestType, ctrl->bRequest,
849 w_value, w_index, w_length);
850 }
851
852 /* respond with data transfer or status phase? */
853 if (value >= 0) {
854 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
855 ctrl->bRequestType, ctrl->bRequest,
856 w_value, w_index, w_length);
857 req->zero = 0;
858 req->length = value;
859 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
860 if (value < 0)
861 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
862 ctrl->bRequestType, ctrl->bRequest,
863 value);
864 }
865
866 /* device either stalls (value < 0) or reports success */
867 return value;
868}
869
870
871static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
872{
873 struct f_ncm *ncm = func_to_ncm(f);
874 struct usb_composite_dev *cdev = f->config->cdev;
875
876 /* Control interface has only altsetting 0 */
877 if (intf == ncm->ctrl_id) {
878 if (alt != 0)
879 goto fail;
880
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200881 DBG(cdev, "reset ncm control %d\n", intf);
882 usb_ep_disable(ncm->notify);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300883
884 if (!(ncm->notify->desc)) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200885 DBG(cdev, "init ncm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300886 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
887 goto fail;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200888 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300889 usb_ep_enable(ncm->notify);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200890
891 /* Data interface has two altsettings, 0 and 1 */
892 } else if (intf == ncm->data_id) {
893 if (alt > 1)
894 goto fail;
895
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200896 if (ncm->port.in_ep->enabled) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200897 DBG(cdev, "reset ncm\n");
Jim Baxter6d3865f2014-07-07 18:33:18 +0100898 ncm->timer_stopping = true;
899 ncm->netdev = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200900 gether_disconnect(&ncm->port);
901 ncm_reset_values(ncm);
902 }
903
904 /*
905 * CDC Network only sends data in non-default altsettings.
906 * Changing altsettings resets filters, statistics, etc.
907 */
908 if (alt == 1) {
909 struct net_device *net;
910
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300911 if (!ncm->port.in_ep->desc ||
912 !ncm->port.out_ep->desc) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200913 DBG(cdev, "init ncm\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300914 if (config_ep_by_speed(cdev->gadget, f,
915 ncm->port.in_ep) ||
916 config_ep_by_speed(cdev->gadget, f,
917 ncm->port.out_ep)) {
918 ncm->port.in_ep->desc = NULL;
919 ncm->port.out_ep->desc = NULL;
920 goto fail;
921 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200922 }
923
924 /* TODO */
925 /* Enable zlps by default for NCM conformance;
926 * override for musb_hdrc (avoids txdma ovhead)
927 */
Robert Baldyga7a896d42015-07-28 07:20:02 +0200928 ncm->port.is_zlp_ok =
929 gadget_is_zlp_supported(cdev->gadget);
Yoshihiro Shimodac4824f12016-08-22 17:48:27 +0900930 ncm->port.no_skb_reserve =
931 gadget_avoids_skb_reserve(cdev->gadget);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200932 ncm->port.cdc_filter = DEFAULT_FILTER;
933 DBG(cdev, "activate ncm\n");
934 net = gether_connect(&ncm->port);
935 if (IS_ERR(net))
936 return PTR_ERR(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100937 ncm->netdev = net;
938 ncm->timer_stopping = false;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200939 }
940
941 spin_lock(&ncm->lock);
942 ncm_notify(ncm);
943 spin_unlock(&ncm->lock);
944 } else
945 goto fail;
946
947 return 0;
948fail:
949 return -EINVAL;
950}
951
952/*
953 * Because the data interface supports multiple altsettings,
954 * this NCM function *MUST* implement a get_alt() method.
955 */
956static int ncm_get_alt(struct usb_function *f, unsigned intf)
957{
958 struct f_ncm *ncm = func_to_ncm(f);
959
960 if (intf == ncm->ctrl_id)
961 return 0;
Robert Baldyga6b4012a2015-09-16 12:10:50 +0200962 return ncm->port.in_ep->enabled ? 1 : 0;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +0200963}
964
Jim Baxter6d3865f2014-07-07 18:33:18 +0100965static struct sk_buff *package_for_tx(struct f_ncm *ncm)
966{
967 __le16 *ntb_iter;
968 struct sk_buff *skb2 = NULL;
969 unsigned ndp_pad;
970 unsigned ndp_index;
971 unsigned new_len;
972
973 const struct ndp_parser_opts *opts = ncm->parser_opts;
974 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
975 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
976
977 /* Stop the timer */
978 hrtimer_try_to_cancel(&ncm->task_timer);
979
980 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
981 ncm->skb_tx_data->len;
982 ndp_index = ncm->skb_tx_data->len + ndp_pad;
983 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
984
985 /* Set the final BlockLength and wNdpIndex */
986 ntb_iter = (void *) ncm->skb_tx_data->data;
987 /* Increment pointer to BlockLength */
988 ntb_iter += 2 + 1 + 1;
989 put_ncm(&ntb_iter, opts->block_length, new_len);
990 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
991
992 /* Set the final NDP wLength */
993 new_len = opts->ndp_size +
994 (ncm->ndp_dgram_count * dgram_idx_len);
995 ncm->ndp_dgram_count = 0;
996 /* Increment from start to wLength */
997 ntb_iter = (void *) ncm->skb_tx_ndp->data;
998 ntb_iter += 2;
999 put_unaligned_le16(new_len, ntb_iter);
1000
1001 /* Merge the skbs */
1002 swap(skb2, ncm->skb_tx_data);
1003 if (ncm->skb_tx_data) {
1004 dev_kfree_skb_any(ncm->skb_tx_data);
1005 ncm->skb_tx_data = NULL;
1006 }
1007
1008 /* Insert NDP alignment. */
1009 ntb_iter = (void *) skb_put(skb2, ndp_pad);
1010 memset(ntb_iter, 0, ndp_pad);
1011
1012 /* Copy NTB across. */
1013 ntb_iter = (void *) skb_put(skb2, ncm->skb_tx_ndp->len);
1014 memcpy(ntb_iter, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1015 dev_kfree_skb_any(ncm->skb_tx_ndp);
1016 ncm->skb_tx_ndp = NULL;
1017
1018 /* Insert zero'd datagram. */
1019 ntb_iter = (void *) skb_put(skb2, dgram_idx_len);
1020 memset(ntb_iter, 0, dgram_idx_len);
1021
1022 return skb2;
1023}
1024
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001025static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1026 struct sk_buff *skb)
1027{
1028 struct f_ncm *ncm = func_to_ncm(&port->func);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001029 struct sk_buff *skb2 = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001030 int ncb_len = 0;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001031 __le16 *ntb_data;
1032 __le16 *ntb_ndp;
1033 int dgram_pad;
1034
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001035 unsigned max_size = ncm->port.fixed_in_len;
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +01001036 const struct ndp_parser_opts *opts = ncm->parser_opts;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001037 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1038 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1039 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1040 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001041
Jim Baxter6d3865f2014-07-07 18:33:18 +01001042 if (!skb && !ncm->skb_tx_data)
1043 return NULL;
Dmytro Milinevskyyf72e3b72012-10-05 01:44:04 +03001044
Jim Baxter6d3865f2014-07-07 18:33:18 +01001045 if (skb) {
1046 /* Add the CRC if required up front */
1047 if (ncm->is_crc) {
1048 uint32_t crc;
1049 __le16 *crc_pos;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001050
Jim Baxter6d3865f2014-07-07 18:33:18 +01001051 crc = ~crc32_le(~0,
1052 skb->data,
1053 skb->len);
1054 crc_pos = (void *) skb_put(skb, sizeof(uint32_t));
1055 put_unaligned_le32(crc, crc_pos);
1056 }
1057
1058 /* If the new skb is too big for the current NCM NTB then
1059 * set the current stored skb to be sent now and clear it
1060 * ready for new data.
1061 * NOTE: Assume maximum align for speed of calculation.
1062 */
1063 if (ncm->skb_tx_data
1064 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1065 || (ncm->skb_tx_data->len +
1066 div + rem + skb->len +
1067 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1068 > max_size)) {
1069 skb2 = package_for_tx(ncm);
1070 if (!skb2)
1071 goto err;
1072 }
1073
1074 if (!ncm->skb_tx_data) {
1075 ncb_len = opts->nth_size;
1076 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1077 ncb_len += dgram_pad;
1078
1079 /* Create a new skb for the NTH and datagrams. */
1080 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1081 if (!ncm->skb_tx_data)
1082 goto err;
1083
1084 ntb_data = (void *) skb_put(ncm->skb_tx_data, ncb_len);
1085 memset(ntb_data, 0, ncb_len);
1086 /* dwSignature */
1087 put_unaligned_le32(opts->nth_sign, ntb_data);
1088 ntb_data += 2;
1089 /* wHeaderLength */
1090 put_unaligned_le16(opts->nth_size, ntb_data++);
1091
1092 /* Allocate an skb for storing the NDP,
1093 * TX_MAX_NUM_DPE should easily suffice for a
1094 * 16k packet.
1095 */
1096 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1097 + opts->dpe_size
1098 * TX_MAX_NUM_DPE),
1099 GFP_ATOMIC);
1100 if (!ncm->skb_tx_ndp)
1101 goto err;
1102 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp,
1103 opts->ndp_size);
1104 memset(ntb_ndp, 0, ncb_len);
1105 /* dwSignature */
1106 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1107 ntb_ndp += 2;
1108
1109 /* There is always a zeroed entry */
1110 ncm->ndp_dgram_count = 1;
1111
1112 /* Note: we skip opts->next_ndp_index */
1113 }
1114
1115 /* Delay the timer. */
1116 hrtimer_start(&ncm->task_timer,
1117 ktime_set(0, TX_TIMEOUT_NSECS),
1118 HRTIMER_MODE_REL);
1119
1120 /* Add the datagram position entries */
1121 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp, dgram_idx_len);
1122 memset(ntb_ndp, 0, dgram_idx_len);
1123
1124 ncb_len = ncm->skb_tx_data->len;
1125 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1126 ncb_len += dgram_pad;
1127
1128 /* (d)wDatagramIndex */
1129 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1130 /* (d)wDatagramLength */
1131 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1132 ncm->ndp_dgram_count++;
1133
1134 /* Add the new data to the skb */
1135 ntb_data = (void *) skb_put(ncm->skb_tx_data, dgram_pad);
1136 memset(ntb_data, 0, dgram_pad);
1137 ntb_data = (void *) skb_put(ncm->skb_tx_data, skb->len);
1138 memcpy(ntb_data, skb->data, skb->len);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001139 dev_kfree_skb_any(skb);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001140 skb = NULL;
1141
1142 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1143 /* If the tx was requested because of a timeout then send */
1144 skb2 = package_for_tx(ncm);
1145 if (!skb2)
1146 goto err;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001147 }
1148
Jim Baxter6d3865f2014-07-07 18:33:18 +01001149 return skb2;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001150
Jim Baxter6d3865f2014-07-07 18:33:18 +01001151err:
1152 ncm->netdev->stats.tx_dropped++;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001153
Jim Baxter6d3865f2014-07-07 18:33:18 +01001154 if (skb)
1155 dev_kfree_skb_any(skb);
1156 if (ncm->skb_tx_data)
1157 dev_kfree_skb_any(ncm->skb_tx_data);
1158 if (ncm->skb_tx_ndp)
1159 dev_kfree_skb_any(ncm->skb_tx_ndp);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001160
Jim Baxter6d3865f2014-07-07 18:33:18 +01001161 return NULL;
1162}
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001163
Jim Baxter6d3865f2014-07-07 18:33:18 +01001164/*
1165 * This transmits the NTB if there are frames waiting.
1166 */
1167static void ncm_tx_tasklet(unsigned long data)
1168{
1169 struct f_ncm *ncm = (void *)data;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001170
Jim Baxter6d3865f2014-07-07 18:33:18 +01001171 if (ncm->timer_stopping)
1172 return;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001173
Jim Baxter6d3865f2014-07-07 18:33:18 +01001174 /* Only send if data is available. */
1175 if (ncm->skb_tx_data) {
1176 ncm->timer_force_tx = true;
David S. Millerc2c0e8b2014-08-27 17:05:53 -07001177
1178 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1179 * XXX is not sane. The gadget layer should be redesigned so
1180 * XXX that the dev->wrap() invocations to build SKBs is transparent
1181 * XXX and performed in some way outside of the ndo_start_xmit
1182 * XXX interface.
1183 */
1184 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1185
Jim Baxter6d3865f2014-07-07 18:33:18 +01001186 ncm->timer_force_tx = false;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001187 }
Jim Baxter6d3865f2014-07-07 18:33:18 +01001188}
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001189
Jim Baxter6d3865f2014-07-07 18:33:18 +01001190/*
1191 * The transmit should only be run if no skb data has been sent
1192 * for a certain duration.
1193 */
1194static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1195{
1196 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1197 tasklet_schedule(&ncm->tx_tasklet);
1198 return HRTIMER_NORESTART;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001199}
1200
1201static int ncm_unwrap_ntb(struct gether *port,
1202 struct sk_buff *skb,
1203 struct sk_buff_head *list)
1204{
1205 struct f_ncm *ncm = func_to_ncm(&port->func);
1206 __le16 *tmp = (void *) skb->data;
1207 unsigned index, index2;
Jim Baxter370af732014-07-07 18:33:17 +01001208 int ndp_index;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001209 unsigned dg_len, dg_len2;
1210 unsigned ndp_len;
1211 struct sk_buff *skb2;
1212 int ret = -EINVAL;
1213 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
Sebastian Andrzej Siewior8d640ad2012-11-22 19:50:34 +01001214 const struct ndp_parser_opts *opts = ncm->parser_opts;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001215 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1216 int dgram_counter;
1217
1218 /* dwSignature */
1219 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1220 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1221 skb->len);
1222 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1223 skb->data, 32, false);
1224
1225 goto err;
1226 }
1227 tmp += 2;
1228 /* wHeaderLength */
1229 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1230 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1231 goto err;
1232 }
1233 tmp++; /* skip wSequence */
1234
1235 /* (d)wBlockLength */
1236 if (get_ncm(&tmp, opts->block_length) > max_size) {
1237 INFO(port->func.config->cdev, "OUT size exceeded\n");
1238 goto err;
1239 }
1240
Jim Baxter6d3865f2014-07-07 18:33:18 +01001241 ndp_index = get_ncm(&tmp, opts->ndp_index);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001242
Jim Baxter370af732014-07-07 18:33:17 +01001243 /* Run through all the NDP's in the NTB */
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001244 do {
Jim Baxter370af732014-07-07 18:33:17 +01001245 /* NCM 3.2 */
1246 if (((ndp_index % 4) != 0) &&
1247 (ndp_index < opts->nth_size)) {
1248 INFO(port->func.config->cdev, "Bad index: %#X\n",
1249 ndp_index);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001250 goto err;
1251 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001252
Jim Baxter370af732014-07-07 18:33:17 +01001253 /* walk through NDP */
1254 tmp = (void *)(skb->data + ndp_index);
1255 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1256 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1257 goto err;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001258 }
Jim Baxter370af732014-07-07 18:33:17 +01001259 tmp += 2;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001260
Jim Baxter370af732014-07-07 18:33:17 +01001261 ndp_len = get_unaligned_le16(tmp++);
1262 /*
1263 * NCM 3.3.1
1264 * entry is 2 items
1265 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1266 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1267 * Each entry is a dgram index and a dgram length.
1268 */
1269 if ((ndp_len < opts->ndp_size
1270 + 2 * 2 * (opts->dgram_item_len * 2))
1271 || (ndp_len % opts->ndplen_align != 0)) {
1272 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1273 ndp_len);
1274 goto err;
1275 }
1276 tmp += opts->reserved1;
1277 /* Check for another NDP (d)wNextNdpIndex */
Jim Baxter6d3865f2014-07-07 18:33:18 +01001278 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
Jim Baxter370af732014-07-07 18:33:17 +01001279 tmp += opts->reserved2;
1280
1281 ndp_len -= opts->ndp_size;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001282 index2 = get_ncm(&tmp, opts->dgram_item_len);
1283 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
Jim Baxter370af732014-07-07 18:33:17 +01001284 dgram_counter = 0;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001285
Jim Baxter370af732014-07-07 18:33:17 +01001286 do {
1287 index = index2;
1288 dg_len = dg_len2;
1289 if (dg_len < 14 + crc_len) { /* ethernet hdr + crc */
1290 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
Jim Baxter66847062014-07-07 18:33:19 +01001313 /*
1314 * Copy the data into a new skb.
1315 * This ensures the truesize is correct
1316 */
1317 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1318 dg_len - crc_len);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001319 if (skb2 == NULL)
1320 goto err;
Jim Baxter66847062014-07-07 18:33:19 +01001321 memcpy(skb_put(skb2, dg_len - crc_len),
1322 skb->data + index, dg_len - crc_len);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001323
Jim Baxter370af732014-07-07 18:33:17 +01001324 skb_queue_tail(list, skb2);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001325
Jim Baxter370af732014-07-07 18:33:17 +01001326 ndp_len -= 2 * (opts->dgram_item_len * 2);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001327
Jim Baxter370af732014-07-07 18:33:17 +01001328 dgram_counter++;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001329
Jim Baxter370af732014-07-07 18:33:17 +01001330 if (index2 == 0 || dg_len2 == 0)
1331 break;
1332 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1333 } while (ndp_index);
1334
1335 dev_kfree_skb_any(skb);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001336
1337 VDBG(port->func.config->cdev,
1338 "Parsed NTB with %d frames\n", dgram_counter);
1339 return 0;
1340err:
1341 skb_queue_purge(list);
1342 dev_kfree_skb_any(skb);
1343 return ret;
1344}
1345
1346static void ncm_disable(struct usb_function *f)
1347{
1348 struct f_ncm *ncm = func_to_ncm(f);
1349 struct usb_composite_dev *cdev = f->config->cdev;
1350
1351 DBG(cdev, "ncm deactivated\n");
1352
Robert Baldyga6b4012a2015-09-16 12:10:50 +02001353 if (ncm->port.in_ep->enabled) {
Jim Baxter6d3865f2014-07-07 18:33:18 +01001354 ncm->timer_stopping = true;
1355 ncm->netdev = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001356 gether_disconnect(&ncm->port);
Jim Baxter6d3865f2014-07-07 18:33:18 +01001357 }
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001358
Robert Baldyga6b4012a2015-09-16 12:10:50 +02001359 if (ncm->notify->enabled) {
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001360 usb_ep_disable(ncm->notify);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001361 ncm->notify->desc = NULL;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001362 }
1363}
1364
1365/*-------------------------------------------------------------------------*/
1366
1367/*
1368 * Callbacks let us notify the host about connect/disconnect when the
1369 * net device is opened or closed.
1370 *
1371 * For testing, note that link states on this side include both opened
1372 * and closed variants of:
1373 *
1374 * - disconnected/unconfigured
1375 * - configured but inactive (data alt 0)
1376 * - configured and active (data alt 1)
1377 *
1378 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1379 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1380 * imply the host is actually polling the notification endpoint, and
1381 * likewise that "active" doesn't imply it's actually using the data
1382 * endpoints for traffic.
1383 */
1384
1385static void ncm_open(struct gether *geth)
1386{
1387 struct f_ncm *ncm = func_to_ncm(&geth->func);
1388
1389 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1390
1391 spin_lock(&ncm->lock);
1392 ncm->is_open = true;
1393 ncm_notify(ncm);
1394 spin_unlock(&ncm->lock);
1395}
1396
1397static void ncm_close(struct gether *geth)
1398{
1399 struct f_ncm *ncm = func_to_ncm(&geth->func);
1400
1401 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1402
1403 spin_lock(&ncm->lock);
1404 ncm->is_open = false;
1405 ncm_notify(ncm);
1406 spin_unlock(&ncm->lock);
1407}
1408
1409/*-------------------------------------------------------------------------*/
1410
1411/* ethernet function driver setup/binding */
1412
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001413static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001414{
1415 struct usb_composite_dev *cdev = c->cdev;
1416 struct f_ncm *ncm = func_to_ncm(f);
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001417 struct usb_string *us;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001418 int status;
1419 struct usb_ep *ep;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001420 struct f_ncm_opts *ncm_opts;
1421
1422 if (!can_support_ecm(cdev->gadget))
1423 return -EINVAL;
1424
1425 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1426 /*
1427 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1428 * configurations are bound in sequence with list_for_each_entry,
1429 * in each configuration its functions are bound in sequence
1430 * with list_for_each_entry, so we assume no race condition
1431 * with regard to ncm_opts->bound access
1432 */
1433 if (!ncm_opts->bound) {
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001434 mutex_lock(&ncm_opts->lock);
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001435 ncm_opts->net = gether_setup_default();
1436 if (IS_ERR(ncm_opts->net)) {
1437 status = PTR_ERR(ncm_opts->net);
1438 mutex_unlock(&ncm_opts->lock);
1439 goto error;
1440 }
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001441 gether_set_gadget(ncm_opts->net, cdev->gadget);
1442 status = gether_register_netdev(ncm_opts->net);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001443 mutex_unlock(&ncm_opts->lock);
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001444 if (status) {
1445 free_netdev(ncm_opts->net);
1446 goto error;
1447 }
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001448 ncm_opts->bound = true;
1449 }
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001450
1451 /* export host's Ethernet address in CDC format */
1452 status = gether_get_host_addr_cdc(ncm_opts->net, ncm->ethaddr,
1453 sizeof(ncm->ethaddr));
1454 if (status < 12) { /* strlen("01234567890a") */
1455 ERROR(cdev, "%s: failed to get host eth addr, err %d\n",
1456 __func__, status);
1457 status = -EINVAL;
1458 goto netdev_cleanup;
1459 }
1460 ncm->port.ioport = netdev_priv(ncm_opts->net);
1461
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001462 us = usb_gstrings_attach(cdev, ncm_strings,
1463 ARRAY_SIZE(ncm_string_defs));
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001464 if (IS_ERR(us)) {
1465 status = PTR_ERR(us);
1466 goto netdev_cleanup;
1467 }
Andrzej Pietrasiewicz8feffd02013-05-23 09:22:09 +02001468 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1469 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1470 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1471 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1472 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001473
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001474 /* allocate instance-specific interface IDs */
1475 status = usb_interface_id(c, f);
1476 if (status < 0)
1477 goto fail;
1478 ncm->ctrl_id = status;
1479 ncm_iad_desc.bFirstInterface = status;
1480
1481 ncm_control_intf.bInterfaceNumber = status;
1482 ncm_union_desc.bMasterInterface0 = status;
1483
1484 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;
ChandanaKishori Chiluveru432006e2015-08-05 18:14:30 +05301517 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT
1518 + (cdev->gadget->extra_buf_alloc), GFP_KERNEL);
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001519 if (!ncm->notify_req->buf)
1520 goto fail;
1521 ncm->notify_req->context = ncm;
1522 ncm->notify_req->complete = ncm_notify_complete;
1523
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001524 /*
1525 * support all relevant hardware speeds... we expect that when
1526 * hardware is dual speed, all bulk-capable endpoints work at
1527 * both speeds
1528 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001529 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1530 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1531 hs_ncm_notify_desc.bEndpointAddress =
1532 fs_ncm_notify_desc.bEndpointAddress;
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001533
Jussi Kivilinna16501132016-08-12 17:28:05 +03001534 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1535 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1536 ss_ncm_notify_desc.bEndpointAddress =
1537 fs_ncm_notify_desc.bEndpointAddress;
Mayank Ranae490d552015-03-09 11:31:18 -07001538
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001539 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
Mayank Ranae490d552015-03-09 11:31:18 -07001540 ncm_ss_function, NULL);
Pavitrakumar Managutte8b920f12014-10-27 22:49:26 +05301541 if (status)
1542 goto fail;
1543
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001544 /*
1545 * NOTE: all that is done without knowing or caring about
1546 * the network link ... which is unavailable to this code
1547 * until we're activated via set_alt().
1548 */
1549
1550 ncm->port.open = ncm_open;
1551 ncm->port.close = ncm_close;
1552
Jim Baxter6d3865f2014-07-07 18:33:18 +01001553 tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
1554 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1555 ncm->task_timer.function = ncm_tx_timeout;
1556
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001557 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Jussi Kivilinna16501132016-08-12 17:28:05 +03001558 gadget_is_superspeed(c->cdev->gadget) ? "super" :
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001559 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1560 ncm->port.in_ep->name, ncm->port.out_ep->name,
1561 ncm->notify->name);
1562 return 0;
1563
1564fail:
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001565 if (ncm->notify_req) {
1566 kfree(ncm->notify_req->buf);
1567 usb_ep_free_request(ncm->notify, ncm->notify_req);
1568 }
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001569netdev_cleanup:
1570 gether_cleanup(netdev_priv(ncm_opts->net));
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001571
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001572error:
Yauheni Kaliuta9f6ce422010-12-08 13:12:05 +02001573 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1574
1575 return status;
1576}
1577
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001578static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1579{
1580 return container_of(to_config_group(item), struct f_ncm_opts,
1581 func_inst.group);
1582}
1583
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001584/* f_ncm_item_ops */
1585USB_ETHERNET_CONFIGFS_ITEM(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001586
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001587/* f_ncm_opts_dev_addr */
1588USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001589
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001590/* f_ncm_opts_host_addr */
1591USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001592
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001593/* f_ncm_opts_qmult */
1594USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001595
Andrzej Pietrasiewiczaa83c6a2013-05-23 10:32:02 +02001596/* f_ncm_opts_ifname */
1597USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001598
1599static struct configfs_attribute *ncm_attrs[] = {
Christoph Hellwigf9a63da2015-10-03 15:32:42 +02001600 &ncm_opts_attr_dev_addr,
1601 &ncm_opts_attr_host_addr,
1602 &ncm_opts_attr_qmult,
1603 &ncm_opts_attr_ifname,
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001604 NULL,
1605};
1606
1607static struct config_item_type ncm_func_type = {
1608 .ct_item_ops = &ncm_item_ops,
1609 .ct_attrs = ncm_attrs,
1610 .ct_owner = THIS_MODULE,
1611};
1612
Ziqi Chenc35ae882017-09-26 18:48:28 +08001613#ifdef CONFIG_USB_CONFIGFS_UEVENT
1614
1615struct ncm_setup_desc {
1616 struct work_struct work;
1617 struct device *device;
1618 uint8_t major; // Mirror Link major version
1619 uint8_t minor; // Mirror Link minor version
1620};
1621
1622static struct ncm_setup_desc *_ncm_setup_desc;
1623
1624#define MIRROR_LINK_STRING_LENGTH_MAX 32
1625static void ncm_setup_work(struct work_struct *data)
1626{
1627 char mirror_link_string[MIRROR_LINK_STRING_LENGTH_MAX];
1628 char *envp[2] = { mirror_link_string, NULL };
1629
1630 snprintf(mirror_link_string, MIRROR_LINK_STRING_LENGTH_MAX,
1631 "MirrorLink=V%d.%d",
1632 _ncm_setup_desc->major, _ncm_setup_desc->minor);
1633 kobject_uevent_env(&_ncm_setup_desc->device->kobj, KOBJ_CHANGE, envp);
1634}
1635
1636int ncm_ctrlrequest(struct usb_composite_dev *cdev,
1637 const struct usb_ctrlrequest *ctrl)
1638{
1639 int value = -EOPNOTSUPP;
1640
Liangliang Lu3711c222018-06-25 15:29:41 +08001641 if (ctrl->bRequestType == 0x40 && ctrl->bRequest == 0xF0
1642 && _ncm_setup_desc) {
Ziqi Chenc35ae882017-09-26 18:48:28 +08001643 _ncm_setup_desc->minor = (uint8_t)(ctrl->wValue >> 8);
1644 _ncm_setup_desc->major = (uint8_t)(ctrl->wValue & 0xFF);
1645 schedule_work(&_ncm_setup_desc->work);
1646 value = 0;
1647 }
1648
1649 return value;
1650}
1651#endif
1652
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001653static void ncm_free_inst(struct usb_function_instance *f)
1654{
1655 struct f_ncm_opts *opts;
1656
Ziqi Chenc35ae882017-09-26 18:48:28 +08001657#ifdef CONFIG_USB_CONFIGFS_UEVENT
1658 /* release _ncm_setup_desc related resource */
1659 device_destroy(_ncm_setup_desc->device->class,
1660 _ncm_setup_desc->device->devt);
1661 cancel_work(&_ncm_setup_desc->work);
1662 kfree(_ncm_setup_desc);
1663#endif
1664
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001665 opts = container_of(f, struct f_ncm_opts, func_inst);
1666 if (opts->bound)
1667 gether_cleanup(netdev_priv(opts->net));
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001668 kfree(opts);
1669}
1670
1671static struct usb_function_instance *ncm_alloc_inst(void)
1672{
1673 struct f_ncm_opts *opts;
1674
1675 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1676 if (!opts)
1677 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001678 mutex_init(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001679 opts->func_inst.free_func_inst = ncm_free_inst;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001680
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001681 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1682
Ziqi Chenc35ae882017-09-26 18:48:28 +08001683#ifdef CONFIG_USB_CONFIGFS_UEVENT
1684 _ncm_setup_desc = kzalloc(sizeof(*_ncm_setup_desc), GFP_KERNEL);
1685 if (!_ncm_setup_desc)
1686 return ERR_PTR(-ENOMEM);
1687 INIT_WORK(&_ncm_setup_desc->work, ncm_setup_work);
1688 _ncm_setup_desc->device = create_function_device("f_ncm");
1689#endif
1690
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001691 return &opts->func_inst;
1692}
1693
1694static void ncm_free(struct usb_function *f)
1695{
1696 struct f_ncm *ncm;
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001697 struct f_ncm_opts *opts;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001698
1699 ncm = func_to_ncm(f);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001700 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001701 kfree(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001702 mutex_lock(&opts->lock);
1703 opts->refcnt--;
1704 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001705}
1706
1707static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1708{
1709 struct f_ncm *ncm = func_to_ncm(f);
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001710 struct f_ncm_opts *opts = container_of(f->fi, struct f_ncm_opts,
1711 func_inst);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001712
1713 DBG(c->cdev, "ncm unbind\n");
1714
Liangliang Lubaa169b2017-10-18 16:35:18 +08001715 opts->bound = false;
1716
Jim Baxter6d3865f2014-07-07 18:33:18 +01001717 hrtimer_cancel(&ncm->task_timer);
1718 tasklet_kill(&ncm->tx_tasklet);
1719
1720 ncm_string_defs[0].id = 0;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001721 usb_free_all_descriptors(f);
1722
Bryan O'Donoghuea5299a12020-01-09 13:17:21 +00001723 if (atomic_read(&ncm->notify_count)) {
1724 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1725 atomic_set(&ncm->notify_count, 0);
1726 }
1727
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001728 kfree(ncm->notify_req->buf);
1729 usb_ep_free_request(ncm->notify, ncm->notify_req);
Hemant Kumar8f909cd22017-03-08 19:05:14 -08001730
1731 gether_cleanup(netdev_priv(opts->net));
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001732}
1733
Jingoo Han36a61122013-12-16 18:43:32 +09001734static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001735{
1736 struct f_ncm *ncm;
1737 struct f_ncm_opts *opts;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001738
1739 /* allocate and initialize one new instance */
1740 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1741 if (!ncm)
1742 return ERR_PTR(-ENOMEM);
1743
1744 opts = container_of(fi, struct f_ncm_opts, func_inst);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001745 mutex_lock(&opts->lock);
1746 opts->refcnt++;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001747 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001748 spin_lock_init(&ncm->lock);
1749 ncm_reset_values(ncm);
Andrzej Pietrasiewicze7306602013-05-23 09:22:10 +02001750 mutex_unlock(&opts->lock);
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001751 ncm->port.is_fixed = true;
Jim Baxter6d3865f2014-07-07 18:33:18 +01001752 ncm->port.supports_multi_frame = true;
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001753
1754 ncm->port.func.name = "cdc_network";
Andrzej Pietrasiewicz40d133d2013-05-23 09:22:06 +02001755 /* descriptors are per-instance copies */
1756 ncm->port.func.bind = ncm_bind;
1757 ncm->port.func.unbind = ncm_unbind;
1758 ncm->port.func.set_alt = ncm_set_alt;
1759 ncm->port.func.get_alt = ncm_get_alt;
1760 ncm->port.func.setup = ncm_setup;
1761 ncm->port.func.disable = ncm_disable;
1762 ncm->port.func.free_func = ncm_free;
1763
1764 ncm->port.wrap = ncm_wrap_ntb;
1765 ncm->port.unwrap = ncm_unwrap_ntb;
1766
1767 return &ncm->port.func;
1768}
1769
1770DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1771MODULE_LICENSE("GPL");
1772MODULE_AUTHOR("Yauheni Kaliuta");