Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1 | /* |
| 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 19 | #include <linux/module.h> |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 20 | #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 Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 27 | #include "u_ether_configfs.h" |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 28 | #include "u_ncm.h" |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 29 | |
| 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 45 | enum 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 | |
| 51 | struct f_ncm { |
| 52 | struct gether port; |
| 53 | u8 ctrl_id, data_id; |
| 54 | |
| 55 | char ethaddr[14]; |
| 56 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 57 | struct usb_ep *notify; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 58 | struct usb_request *notify_req; |
| 59 | u8 notify_state; |
Bryan O'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 60 | atomic_t notify_count; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 61 | bool is_open; |
| 62 | |
Sebastian Andrzej Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 63 | const struct ndp_parser_opts *parser_opts; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 64 | bool is_crc; |
Sebastian Andrzej Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 65 | u32 ndp_sign; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * for notification, it is accessed from both |
| 69 | * callback and ethernet open/close |
| 70 | */ |
| 71 | spinlock_t lock; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 72 | |
| 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static 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 */ |
| 92 | static inline unsigned ncm_bitrate(struct usb_gadget *g) |
| 93 | { |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 94 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 97 | 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 110 | #define NTB_DEFAULT_IN_SIZE 16384 |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 111 | #define NTB_OUT_SIZE 16384 |
| 112 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 113 | /* 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 119 | */ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 120 | #define TX_MAX_NUM_DPE 32 |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 121 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 122 | /* Delay for the transmit to wait before sending an unfilled NTB frame. */ |
| 123 | #define TX_TIMEOUT_NSECS 300000 |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 124 | |
| 125 | #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \ |
| 126 | USB_CDC_NCM_NTB32_SUPPORTED) |
| 127 | |
| 128 | static struct usb_cdc_ncm_ntb_parameters ntb_parameters = { |
Dmytro Milinevskyy | f72e3b7 | 2012-10-05 01:44:04 +0300 | [diff] [blame] | 129 | .wLength = cpu_to_le16(sizeof(ntb_parameters)), |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 130 | .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 Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 148 | #define NCM_STATUS_INTERVAL_MS 32 |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 149 | #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ |
| 150 | |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 151 | static struct usb_interface_assoc_descriptor ncm_iad_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 152 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 165 | static struct usb_interface_descriptor ncm_control_intf = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 166 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 177 | static struct usb_cdc_header_desc ncm_header_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 178 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 185 | static struct usb_cdc_union_desc ncm_union_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 186 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 193 | static struct usb_cdc_ether_desc ecm_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 194 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 208 | static struct usb_cdc_ncm_desc ncm_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 209 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 220 | static struct usb_interface_descriptor ncm_data_nop_intf = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 221 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 235 | static struct usb_interface_descriptor ncm_data_intf = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 236 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 250 | static struct usb_endpoint_descriptor fs_ncm_notify_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 251 | .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 Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 257 | .bInterval = NCM_STATUS_INTERVAL_MS, |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 258 | }; |
| 259 | |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 260 | static struct usb_endpoint_descriptor fs_ncm_in_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 261 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 268 | static struct usb_endpoint_descriptor fs_ncm_out_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 269 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 276 | static struct usb_descriptor_header *ncm_fs_function[] = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 277 | (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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 295 | static struct usb_endpoint_descriptor hs_ncm_notify_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 296 | .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 Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 302 | .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS), |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 303 | }; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 304 | static struct usb_endpoint_descriptor hs_ncm_in_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 305 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 313 | static struct usb_endpoint_descriptor hs_ncm_out_desc = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 314 | .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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 322 | static struct usb_descriptor_header *ncm_hs_function[] = { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 323 | (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 Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 339 | /* super speed support: */ |
| 340 | |
| 341 | static struct usb_endpoint_descriptor ss_ncm_notify_desc = { |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 342 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 343 | .bDescriptorType = USB_DT_ENDPOINT, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 344 | |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 345 | .bEndpointAddress = USB_DIR_IN, |
| 346 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 347 | .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 348 | .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS) |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 351 | static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = { |
| 352 | .bLength = sizeof(ss_ncm_notify_comp_desc), |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 353 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 354 | |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 355 | /* 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 Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 361 | static struct usb_endpoint_descriptor ss_ncm_in_desc = { |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 362 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 363 | .bDescriptorType = USB_DT_ENDPOINT, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 364 | |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 365 | .bEndpointAddress = USB_DIR_IN, |
| 366 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 367 | .wMaxPacketSize = cpu_to_le16(1024), |
| 368 | }; |
| 369 | |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 370 | static struct usb_endpoint_descriptor ss_ncm_out_desc = { |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 371 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 372 | .bDescriptorType = USB_DT_ENDPOINT, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 373 | |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 374 | .bEndpointAddress = USB_DIR_OUT, |
| 375 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 376 | .wMaxPacketSize = cpu_to_le16(1024), |
| 377 | }; |
| 378 | |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 379 | static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = { |
| 380 | .bLength = sizeof(ss_ncm_bulk_comp_desc), |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 381 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 382 | |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 383 | /* the following 2 values can be tweaked if necessary */ |
| 384 | /* .bMaxBurst = 0, */ |
| 385 | /* .bmAttributes = 0, */ |
| 386 | }; |
| 387 | |
| 388 | static 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 Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 396 | (struct usb_descriptor_header *) &ss_ncm_notify_desc, |
| 397 | (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc, |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 398 | /* data interface, altsettings 0 and 1 */ |
| 399 | (struct usb_descriptor_header *) &ncm_data_nop_intf, |
| 400 | (struct usb_descriptor_header *) &ncm_data_intf, |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 401 | (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 Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 405 | NULL, |
| 406 | }; |
| 407 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 408 | /* 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 | |
| 415 | static struct usb_string ncm_string_defs[] = { |
| 416 | [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)", |
Sebastian Andrzej Siewior | 1616e99 | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 417 | [STRING_MAC_IDX].s = "", |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 418 | [STRING_DATA_IDX].s = "CDC Network Data", |
| 419 | [STRING_IAD_IDX].s = "CDC NCM", |
| 420 | { } /* end of list */ |
| 421 | }; |
| 422 | |
| 423 | static struct usb_gadget_strings ncm_string_table = { |
| 424 | .language = 0x0409, /* en-us */ |
| 425 | .strings = ncm_string_defs, |
| 426 | }; |
| 427 | |
| 428 | static 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 | |
| 442 | struct ndp_parser_opts { |
| 443 | u32 nth_sign; |
| 444 | u32 ndp_sign; |
| 445 | unsigned nth_size; |
| 446 | unsigned ndp_size; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 447 | unsigned dpe_size; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 448 | unsigned ndplen_align; |
| 449 | /* sizes in u16 units */ |
| 450 | unsigned dgram_item_len; /* index or length */ |
| 451 | unsigned block_length; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 452 | unsigned ndp_index; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 453 | unsigned reserved1; |
| 454 | unsigned reserved2; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 455 | unsigned next_ndp_index; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 456 | }; |
| 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 463 | .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 464 | .ndplen_align = 4, \ |
| 465 | .dgram_item_len = 1, \ |
| 466 | .block_length = 1, \ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 467 | .ndp_index = 1, \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 468 | .reserved1 = 0, \ |
| 469 | .reserved2 = 0, \ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 470 | .next_ndp_index = 1, \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 471 | } |
| 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 479 | .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 480 | .ndplen_align = 8, \ |
| 481 | .dgram_item_len = 2, \ |
| 482 | .block_length = 2, \ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 483 | .ndp_index = 2, \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 484 | .reserved1 = 1, \ |
| 485 | .reserved2 = 2, \ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 486 | .next_ndp_index = 2, \ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Sebastian Andrzej Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 489 | static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS; |
| 490 | static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 491 | |
| 492 | static 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 | |
| 509 | static 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 | |
| 530 | static 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 | */ |
| 546 | static 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'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 555 | if (atomic_read(&ncm->notify_count)) |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 556 | 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'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 595 | atomic_inc(&ncm->notify_count); |
| 596 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 597 | /* |
| 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'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 606 | atomic_dec(&ncm->notify_count); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 607 | DBG(cdev, "notify --> %d\n", status); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Context: ncm->lock held |
| 613 | */ |
| 614 | static 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 | |
| 630 | static 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'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 641 | atomic_dec(&ncm->notify_count); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 642 | break; |
| 643 | case -ECONNRESET: |
| 644 | case -ESHUTDOWN: |
Bryan O'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 645 | atomic_set(&ncm->notify_count, 0); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 646 | ncm->notify_state = NCM_NOTIFY_NONE; |
| 647 | break; |
| 648 | default: |
| 649 | DBG(cdev, "event %02x --> %d\n", |
| 650 | event->bNotificationType, req->status); |
Bryan O'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 651 | atomic_dec(&ncm->notify_count); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 652 | break; |
| 653 | } |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 654 | ncm_do_notify(ncm); |
| 655 | spin_unlock(&ncm->lock); |
| 656 | } |
| 657 | |
| 658 | static 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 Baldyga | 35bfde3 | 2015-09-16 12:10:40 +0200 | [diff] [blame] | 664 | struct usb_composite_dev *cdev = f->config->cdev; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 665 | |
| 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 | |
| 683 | invalid: |
| 684 | usb_ep_set_halt(ep); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | static 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 Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 834 | ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 835 | 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: |
| 846 | invalid: |
| 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 | |
| 871 | static 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 Baldyga | 6b4012a | 2015-09-16 12:10:50 +0200 | [diff] [blame] | 881 | DBG(cdev, "reset ncm control %d\n", intf); |
| 882 | usb_ep_disable(ncm->notify); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 883 | |
| 884 | if (!(ncm->notify->desc)) { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 885 | DBG(cdev, "init ncm ctrl %d\n", intf); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 886 | if (config_ep_by_speed(cdev->gadget, f, ncm->notify)) |
| 887 | goto fail; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 888 | } |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 889 | usb_ep_enable(ncm->notify); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 890 | |
| 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 Baldyga | 6b4012a | 2015-09-16 12:10:50 +0200 | [diff] [blame] | 896 | if (ncm->port.in_ep->enabled) { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 897 | DBG(cdev, "reset ncm\n"); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 898 | ncm->timer_stopping = true; |
| 899 | ncm->netdev = NULL; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 900 | 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 Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 911 | if (!ncm->port.in_ep->desc || |
| 912 | !ncm->port.out_ep->desc) { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 913 | DBG(cdev, "init ncm\n"); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 914 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* TODO */ |
| 925 | /* Enable zlps by default for NCM conformance; |
| 926 | * override for musb_hdrc (avoids txdma ovhead) |
| 927 | */ |
Robert Baldyga | 7a896d4 | 2015-07-28 07:20:02 +0200 | [diff] [blame] | 928 | ncm->port.is_zlp_ok = |
| 929 | gadget_is_zlp_supported(cdev->gadget); |
Yoshihiro Shimoda | c4824f1 | 2016-08-22 17:48:27 +0900 | [diff] [blame] | 930 | ncm->port.no_skb_reserve = |
| 931 | gadget_avoids_skb_reserve(cdev->gadget); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 932 | 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 937 | ncm->netdev = net; |
| 938 | ncm->timer_stopping = false; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | spin_lock(&ncm->lock); |
| 942 | ncm_notify(ncm); |
| 943 | spin_unlock(&ncm->lock); |
| 944 | } else |
| 945 | goto fail; |
| 946 | |
| 947 | return 0; |
| 948 | fail: |
| 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 | */ |
| 956 | static 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 Baldyga | 6b4012a | 2015-09-16 12:10:50 +0200 | [diff] [blame] | 962 | return ncm->port.in_ep->enabled ? 1 : 0; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 965 | static 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1025 | static 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1029 | struct sk_buff *skb2 = NULL; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1030 | int ncb_len = 0; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1031 | __le16 *ntb_data; |
| 1032 | __le16 *ntb_ndp; |
| 1033 | int dgram_pad; |
| 1034 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1035 | unsigned max_size = ncm->port.fixed_in_len; |
Sebastian Andrzej Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 1036 | const struct ndp_parser_opts *opts = ncm->parser_opts; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1037 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1041 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1042 | if (!skb && !ncm->skb_tx_data) |
| 1043 | return NULL; |
Dmytro Milinevskyy | f72e3b7 | 2012-10-05 01:44:04 +0300 | [diff] [blame] | 1044 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1045 | if (skb) { |
| 1046 | /* Add the CRC if required up front */ |
| 1047 | if (ncm->is_crc) { |
| 1048 | uint32_t crc; |
| 1049 | __le16 *crc_pos; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1050 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1051 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1139 | dev_kfree_skb_any(skb); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1140 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1149 | return skb2; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1150 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1151 | err: |
| 1152 | ncm->netdev->stats.tx_dropped++; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1153 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1154 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1160 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1161 | return NULL; |
| 1162 | } |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1163 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1164 | /* |
| 1165 | * This transmits the NTB if there are frames waiting. |
| 1166 | */ |
| 1167 | static void ncm_tx_tasklet(unsigned long data) |
| 1168 | { |
| 1169 | struct f_ncm *ncm = (void *)data; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1170 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1171 | if (ncm->timer_stopping) |
| 1172 | return; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1173 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1174 | /* Only send if data is available. */ |
| 1175 | if (ncm->skb_tx_data) { |
| 1176 | ncm->timer_force_tx = true; |
David S. Miller | c2c0e8b | 2014-08-27 17:05:53 -0700 | [diff] [blame] | 1177 | |
| 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1186 | ncm->timer_force_tx = false; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1187 | } |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1188 | } |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1189 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1190 | /* |
| 1191 | * The transmit should only be run if no skb data has been sent |
| 1192 | * for a certain duration. |
| 1193 | */ |
| 1194 | static 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | static 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 Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1208 | int ndp_index; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1209 | 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 Siewior | 8d640ad | 2012-11-22 19:50:34 +0100 | [diff] [blame] | 1214 | const struct ndp_parser_opts *opts = ncm->parser_opts; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1215 | 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1241 | ndp_index = get_ncm(&tmp, opts->ndp_index); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1242 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1243 | /* Run through all the NDP's in the NTB */ |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1244 | do { |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1245 | /* 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1250 | goto err; |
| 1251 | } |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1252 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1253 | /* 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1258 | } |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1259 | tmp += 2; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1260 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1261 | 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1278 | ndp_index = get_ncm(&tmp, opts->next_ndp_index); |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1279 | tmp += opts->reserved2; |
| 1280 | |
| 1281 | ndp_len -= opts->ndp_size; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1282 | index2 = get_ncm(&tmp, opts->dgram_item_len); |
| 1283 | dg_len2 = get_ncm(&tmp, opts->dgram_item_len); |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1284 | dgram_counter = 0; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1285 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1286 | 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 Baxter | 6684706 | 2014-07-07 18:33:19 +0100 | [diff] [blame] | 1313 | /* |
| 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1319 | if (skb2 == NULL) |
| 1320 | goto err; |
Jim Baxter | 6684706 | 2014-07-07 18:33:19 +0100 | [diff] [blame] | 1321 | memcpy(skb_put(skb2, dg_len - crc_len), |
| 1322 | skb->data + index, dg_len - crc_len); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1323 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1324 | skb_queue_tail(list, skb2); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1325 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1326 | ndp_len -= 2 * (opts->dgram_item_len * 2); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1327 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1328 | dgram_counter++; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1329 | |
Jim Baxter | 370af73 | 2014-07-07 18:33:17 +0100 | [diff] [blame] | 1330 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1336 | |
| 1337 | VDBG(port->func.config->cdev, |
| 1338 | "Parsed NTB with %d frames\n", dgram_counter); |
| 1339 | return 0; |
| 1340 | err: |
| 1341 | skb_queue_purge(list); |
| 1342 | dev_kfree_skb_any(skb); |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | static 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 Baldyga | 6b4012a | 2015-09-16 12:10:50 +0200 | [diff] [blame] | 1353 | if (ncm->port.in_ep->enabled) { |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1354 | ncm->timer_stopping = true; |
| 1355 | ncm->netdev = NULL; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1356 | gether_disconnect(&ncm->port); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1357 | } |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1358 | |
Robert Baldyga | 6b4012a | 2015-09-16 12:10:50 +0200 | [diff] [blame] | 1359 | if (ncm->notify->enabled) { |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1360 | usb_ep_disable(ncm->notify); |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1361 | ncm->notify->desc = NULL; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1362 | } |
| 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 | |
| 1385 | static 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 | |
| 1397 | static 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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1413 | static int ncm_bind(struct usb_configuration *c, struct usb_function *f) |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1414 | { |
| 1415 | struct usb_composite_dev *cdev = c->cdev; |
| 1416 | struct f_ncm *ncm = func_to_ncm(f); |
Andrzej Pietrasiewicz | 8feffd0 | 2013-05-23 09:22:09 +0200 | [diff] [blame] | 1417 | struct usb_string *us; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1418 | int status; |
| 1419 | struct usb_ep *ep; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1420 | 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 Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1434 | mutex_lock(&ncm_opts->lock); |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1435 | 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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1441 | gether_set_gadget(ncm_opts->net, cdev->gadget); |
| 1442 | status = gether_register_netdev(ncm_opts->net); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1443 | mutex_unlock(&ncm_opts->lock); |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1444 | if (status) { |
| 1445 | free_netdev(ncm_opts->net); |
| 1446 | goto error; |
| 1447 | } |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1448 | ncm_opts->bound = true; |
| 1449 | } |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1450 | |
| 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 Pietrasiewicz | 8feffd0 | 2013-05-23 09:22:09 +0200 | [diff] [blame] | 1462 | us = usb_gstrings_attach(cdev, ncm_strings, |
| 1463 | ARRAY_SIZE(ncm_string_defs)); |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1464 | if (IS_ERR(us)) { |
| 1465 | status = PTR_ERR(us); |
| 1466 | goto netdev_cleanup; |
| 1467 | } |
Andrzej Pietrasiewicz | 8feffd0 | 2013-05-23 09:22:09 +0200 | [diff] [blame] | 1468 | 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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1473 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1474 | /* 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1500 | |
| 1501 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc); |
| 1502 | if (!ep) |
| 1503 | goto fail; |
| 1504 | ncm->port.out_ep = ep; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1505 | |
| 1506 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc); |
| 1507 | if (!ep) |
| 1508 | goto fail; |
| 1509 | ncm->notify = ep; |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1510 | |
| 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 Chiluveru | 432006e | 2015-08-05 18:14:30 +0530 | [diff] [blame] | 1517 | ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT |
| 1518 | + (cdev->gadget->extra_buf_alloc), GFP_KERNEL); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1519 | if (!ncm->notify_req->buf) |
| 1520 | goto fail; |
| 1521 | ncm->notify_req->context = ncm; |
| 1522 | ncm->notify_req->complete = ncm_notify_complete; |
| 1523 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1524 | /* |
| 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 Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 1529 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1533 | |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 1534 | 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 Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 1538 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 1539 | status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function, |
Mayank Rana | e490d55 | 2015-03-09 11:31:18 -0700 | [diff] [blame] | 1540 | ncm_ss_function, NULL); |
Pavitrakumar Managutte | 8b920f1 | 2014-10-27 22:49:26 +0530 | [diff] [blame] | 1541 | if (status) |
| 1542 | goto fail; |
| 1543 | |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1544 | /* |
| 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 Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1553 | 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 Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1557 | DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n", |
Jussi Kivilinna | 1650113 | 2016-08-12 17:28:05 +0300 | [diff] [blame] | 1558 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1559 | 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 | |
| 1564 | fail: |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1565 | if (ncm->notify_req) { |
| 1566 | kfree(ncm->notify_req->buf); |
| 1567 | usb_ep_free_request(ncm->notify, ncm->notify_req); |
| 1568 | } |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1569 | netdev_cleanup: |
| 1570 | gether_cleanup(netdev_priv(ncm_opts->net)); |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1571 | |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1572 | error: |
Yauheni Kaliuta | 9f6ce42 | 2010-12-08 13:12:05 +0200 | [diff] [blame] | 1573 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 1574 | |
| 1575 | return status; |
| 1576 | } |
| 1577 | |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1578 | static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item) |
| 1579 | { |
| 1580 | return container_of(to_config_group(item), struct f_ncm_opts, |
| 1581 | func_inst.group); |
| 1582 | } |
| 1583 | |
Andrzej Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 1584 | /* f_ncm_item_ops */ |
| 1585 | USB_ETHERNET_CONFIGFS_ITEM(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1586 | |
Andrzej Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 1587 | /* f_ncm_opts_dev_addr */ |
| 1588 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1589 | |
Andrzej Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 1590 | /* f_ncm_opts_host_addr */ |
| 1591 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1592 | |
Andrzej Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 1593 | /* f_ncm_opts_qmult */ |
| 1594 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1595 | |
Andrzej Pietrasiewicz | aa83c6a | 2013-05-23 10:32:02 +0200 | [diff] [blame] | 1596 | /* f_ncm_opts_ifname */ |
| 1597 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1598 | |
| 1599 | static struct configfs_attribute *ncm_attrs[] = { |
Christoph Hellwig | f9a63da | 2015-10-03 15:32:42 +0200 | [diff] [blame] | 1600 | &ncm_opts_attr_dev_addr, |
| 1601 | &ncm_opts_attr_host_addr, |
| 1602 | &ncm_opts_attr_qmult, |
| 1603 | &ncm_opts_attr_ifname, |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1604 | NULL, |
| 1605 | }; |
| 1606 | |
| 1607 | static 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 Chen | c35ae88 | 2017-09-26 18:48:28 +0800 | [diff] [blame] | 1613 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1614 | |
| 1615 | struct 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 | |
| 1622 | static struct ncm_setup_desc *_ncm_setup_desc; |
| 1623 | |
| 1624 | #define MIRROR_LINK_STRING_LENGTH_MAX 32 |
| 1625 | static 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 | |
| 1636 | int ncm_ctrlrequest(struct usb_composite_dev *cdev, |
| 1637 | const struct usb_ctrlrequest *ctrl) |
| 1638 | { |
| 1639 | int value = -EOPNOTSUPP; |
| 1640 | |
Liangliang Lu | 3711c22 | 2018-06-25 15:29:41 +0800 | [diff] [blame] | 1641 | if (ctrl->bRequestType == 0x40 && ctrl->bRequest == 0xF0 |
| 1642 | && _ncm_setup_desc) { |
Ziqi Chen | c35ae88 | 2017-09-26 18:48:28 +0800 | [diff] [blame] | 1643 | _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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1653 | static void ncm_free_inst(struct usb_function_instance *f) |
| 1654 | { |
| 1655 | struct f_ncm_opts *opts; |
| 1656 | |
Ziqi Chen | c35ae88 | 2017-09-26 18:48:28 +0800 | [diff] [blame] | 1657 | #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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1665 | opts = container_of(f, struct f_ncm_opts, func_inst); |
| 1666 | if (opts->bound) |
| 1667 | gether_cleanup(netdev_priv(opts->net)); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1668 | kfree(opts); |
| 1669 | } |
| 1670 | |
| 1671 | static 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 Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1678 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1679 | opts->func_inst.free_func_inst = ncm_free_inst; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1680 | |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1681 | config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type); |
| 1682 | |
Ziqi Chen | c35ae88 | 2017-09-26 18:48:28 +0800 | [diff] [blame] | 1683 | #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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1691 | return &opts->func_inst; |
| 1692 | } |
| 1693 | |
| 1694 | static void ncm_free(struct usb_function *f) |
| 1695 | { |
| 1696 | struct f_ncm *ncm; |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1697 | struct f_ncm_opts *opts; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1698 | |
| 1699 | ncm = func_to_ncm(f); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1700 | opts = container_of(f->fi, struct f_ncm_opts, func_inst); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1701 | kfree(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1702 | mutex_lock(&opts->lock); |
| 1703 | opts->refcnt--; |
| 1704 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1708 | { |
| 1709 | struct f_ncm *ncm = func_to_ncm(f); |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1710 | struct f_ncm_opts *opts = container_of(f->fi, struct f_ncm_opts, |
| 1711 | func_inst); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1712 | |
| 1713 | DBG(c->cdev, "ncm unbind\n"); |
| 1714 | |
Liangliang Lu | baa169b | 2017-10-18 16:35:18 +0800 | [diff] [blame] | 1715 | opts->bound = false; |
| 1716 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1717 | hrtimer_cancel(&ncm->task_timer); |
| 1718 | tasklet_kill(&ncm->tx_tasklet); |
| 1719 | |
| 1720 | ncm_string_defs[0].id = 0; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1721 | usb_free_all_descriptors(f); |
| 1722 | |
Bryan O'Donoghue | a5299a1 | 2020-01-09 13:17:21 +0000 | [diff] [blame] | 1723 | 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 Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1728 | kfree(ncm->notify_req->buf); |
| 1729 | usb_ep_free_request(ncm->notify, ncm->notify_req); |
Hemant Kumar | 8f909cd2 | 2017-03-08 19:05:14 -0800 | [diff] [blame] | 1730 | |
| 1731 | gether_cleanup(netdev_priv(opts->net)); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Jingoo Han | 36a6112 | 2013-12-16 18:43:32 +0900 | [diff] [blame] | 1734 | static struct usb_function *ncm_alloc(struct usb_function_instance *fi) |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1735 | { |
| 1736 | struct f_ncm *ncm; |
| 1737 | struct f_ncm_opts *opts; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1738 | |
| 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 Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1745 | mutex_lock(&opts->lock); |
| 1746 | opts->refcnt++; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1747 | ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1748 | spin_lock_init(&ncm->lock); |
| 1749 | ncm_reset_values(ncm); |
Andrzej Pietrasiewicz | e730660 | 2013-05-23 09:22:10 +0200 | [diff] [blame] | 1750 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1751 | ncm->port.is_fixed = true; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 1752 | ncm->port.supports_multi_frame = true; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1753 | |
| 1754 | ncm->port.func.name = "cdc_network"; |
Andrzej Pietrasiewicz | 40d133d | 2013-05-23 09:22:06 +0200 | [diff] [blame] | 1755 | /* 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 | |
| 1770 | DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc); |
| 1771 | MODULE_LICENSE("GPL"); |
| 1772 | MODULE_AUTHOR("Yauheni Kaliuta"); |