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