1 /* 2 * cdc_ncm.c 3 * 4 * Copyright (C) ST-Ericsson 2010-2011 5 * Contact: Alexey Orishko <alexey.orishko@stericsson.com> 6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com> 7 * 8 * USB Host Driver for Network Control Model (NCM) 9 * http://www.usb.org/developers/devclass_docs/NCM10.zip 10 * 11 * The NCM encoding, decoding and initialization logic 12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h 13 * 14 * This software is available to you under a choice of one of two 15 * licenses. You may choose this file to be licensed under the terms 16 * of the GNU General Public License (GPL) Version 2 or the 2-clause 17 * BSD license listed below: 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #include <linux/module.h> 42 #include <linux/init.h> 43 #include <linux/netdevice.h> 44 #include <linux/ctype.h> 45 #include <linux/ethtool.h> 46 #include <linux/workqueue.h> 47 #include <linux/mii.h> 48 #include <linux/crc32.h> 49 #include <linux/usb.h> 50 #include <linux/version.h> 51 #include <linux/timer.h> 52 #include <linux/spinlock.h> 53 #include <linux/atomic.h> 54 #include <linux/usb/usbnet.h> 55 #include <linux/usb/cdc.h> 56 57 #define DRIVER_VERSION "23-Apr-2011" 58 59 /* CDC NCM subclass 3.2.1 */ 60 #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10 61 62 /* Maximum NTB length */ 63 #define CDC_NCM_NTB_MAX_SIZE_TX (16384 + 4) /* bytes, must be short terminated */ 64 #define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */ 65 66 /* Minimum value for MaxDatagramSize, ch. 6.2.9 */ 67 #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */ 68 69 #define CDC_NCM_MIN_TX_PKT 512 /* bytes */ 70 71 /* Default value for MaxDatagramSize */ 72 #define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */ 73 74 /* 75 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting 76 * the last NULL entry. Any additional datagrams in NTB would be discarded. 77 */ 78 #define CDC_NCM_DPT_DATAGRAMS_MAX 32 79 80 /* Maximum amount of IN datagrams in NTB */ 81 #define CDC_NCM_DPT_DATAGRAMS_IN_MAX 0 /* unlimited */ 82 83 /* Restart the timer, if amount of datagrams is less than given value */ 84 #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3 85 86 /* The following macro defines the minimum header space */ 87 #define CDC_NCM_MIN_HDR_SIZE \ 88 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \ 89 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16)) 90 91 struct cdc_ncm_data { 92 struct usb_cdc_ncm_nth16 nth16; 93 struct usb_cdc_ncm_ndp16 ndp16; 94 struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1]; 95 }; 96 97 struct cdc_ncm_ctx { 98 struct cdc_ncm_data rx_ncm; 99 struct cdc_ncm_data tx_ncm; 100 struct usb_cdc_ncm_ntb_parameters ncm_parm; 101 struct timer_list tx_timer; 102 103 const struct usb_cdc_ncm_desc *func_desc; 104 const struct usb_cdc_header_desc *header_desc; 105 const struct usb_cdc_union_desc *union_desc; 106 const struct usb_cdc_ether_desc *ether_desc; 107 108 struct net_device *netdev; 109 struct usb_device *udev; 110 struct usb_host_endpoint *in_ep; 111 struct usb_host_endpoint *out_ep; 112 struct usb_host_endpoint *status_ep; 113 struct usb_interface *intf; 114 struct usb_interface *control; 115 struct usb_interface *data; 116 117 struct sk_buff *tx_curr_skb; 118 struct sk_buff *tx_rem_skb; 119 120 spinlock_t mtx; 121 122 u32 tx_timer_pending; 123 u32 tx_curr_offset; 124 u32 tx_curr_last_offset; 125 u32 tx_curr_frame_num; 126 u32 rx_speed; 127 u32 tx_speed; 128 u32 rx_max; 129 u32 tx_max; 130 u32 max_datagram_size; 131 u16 tx_max_datagrams; 132 u16 tx_remainder; 133 u16 tx_modulus; 134 u16 tx_ndp_modulus; 135 u16 tx_seq; 136 u16 connected; 137 u8 data_claimed; 138 u8 control_claimed; 139 }; 140 141 static void cdc_ncm_tx_timeout(unsigned long arg); 142 static const struct driver_info cdc_ncm_info; 143 static struct usb_driver cdc_ncm_driver; 144 static struct ethtool_ops cdc_ncm_ethtool_ops; 145 146 static const struct usb_device_id cdc_devs[] = { 147 { USB_INTERFACE_INFO(USB_CLASS_COMM, 148 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), 149 .driver_info = (unsigned long)&cdc_ncm_info, 150 }, 151 { 152 }, 153 }; 154 155 MODULE_DEVICE_TABLE(usb, cdc_devs); 156 157 static void 158 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 159 { 160 struct usbnet *dev = netdev_priv(net); 161 162 strncpy(info->driver, dev->driver_name, sizeof(info->driver)); 163 strncpy(info->version, DRIVER_VERSION, sizeof(info->version)); 164 strncpy(info->fw_version, dev->driver_info->description, 165 sizeof(info->fw_version)); 166 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info)); 167 } 168 169 static int 170 cdc_ncm_do_request(struct cdc_ncm_ctx *ctx, struct usb_cdc_notification *req, 171 void *data, u16 flags, u16 *actlen, u16 timeout) 172 { 173 int err; 174 175 err = usb_control_msg(ctx->udev, (req->bmRequestType & USB_DIR_IN) ? 176 usb_rcvctrlpipe(ctx->udev, 0) : 177 usb_sndctrlpipe(ctx->udev, 0), 178 req->bNotificationType, req->bmRequestType, 179 req->wValue, 180 req->wIndex, data, 181 req->wLength, timeout); 182 183 if (err < 0) { 184 if (actlen) 185 *actlen = 0; 186 return err; 187 } 188 189 if (actlen) 190 *actlen = err; 191 192 return 0; 193 } 194 195 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx) 196 { 197 struct usb_cdc_notification req; 198 u32 val; 199 u8 flags; 200 u8 iface_no; 201 int err; 202 u16 ntb_fmt_supported; 203 204 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; 205 206 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE; 207 req.bNotificationType = USB_CDC_GET_NTB_PARAMETERS; 208 req.wValue = 0; 209 req.wIndex = cpu_to_le16(iface_no); 210 req.wLength = cpu_to_le16(sizeof(ctx->ncm_parm)); 211 212 err = cdc_ncm_do_request(ctx, &req, &ctx->ncm_parm, 0, NULL, 1000); 213 if (err) { 214 pr_debug("failed GET_NTB_PARAMETERS\n"); 215 return 1; 216 } 217 218 /* read correct set of parameters according to device mode */ 219 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize); 220 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize); 221 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder); 222 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor); 223 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment); 224 /* devices prior to NCM Errata shall set this field to zero */ 225 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams); 226 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported); 227 228 if (ctx->func_desc != NULL) 229 flags = ctx->func_desc->bmNetworkCapabilities; 230 else 231 flags = 0; 232 233 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u " 234 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u " 235 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n", 236 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus, 237 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags); 238 239 /* max count of tx datagrams */ 240 if ((ctx->tx_max_datagrams == 0) || 241 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX)) 242 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX; 243 244 /* verify maximum size of received NTB in bytes */ 245 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) { 246 pr_debug("Using min receive length=%d\n", 247 USB_CDC_NCM_NTB_MIN_IN_SIZE); 248 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE; 249 } 250 251 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) { 252 pr_debug("Using default maximum receive length=%d\n", 253 CDC_NCM_NTB_MAX_SIZE_RX); 254 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX; 255 } 256 257 /* inform device about NTB input size changes */ 258 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) { 259 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | 260 USB_RECIP_INTERFACE; 261 req.bNotificationType = USB_CDC_SET_NTB_INPUT_SIZE; 262 req.wValue = 0; 263 req.wIndex = cpu_to_le16(iface_no); 264 265 if (flags & USB_CDC_NCM_NCAP_NTB_INPUT_SIZE) { 266 struct usb_cdc_ncm_ndp_input_size ndp_in_sz; 267 268 req.wLength = 8; 269 ndp_in_sz.dwNtbInMaxSize = cpu_to_le32(ctx->rx_max); 270 ndp_in_sz.wNtbInMaxDatagrams = 271 cpu_to_le16(CDC_NCM_DPT_DATAGRAMS_MAX); 272 ndp_in_sz.wReserved = 0; 273 err = cdc_ncm_do_request(ctx, &req, &ndp_in_sz, 0, NULL, 274 1000); 275 } else { 276 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max); 277 278 req.wLength = 4; 279 err = cdc_ncm_do_request(ctx, &req, &dwNtbInMaxSize, 0, 280 NULL, 1000); 281 } 282 283 if (err) 284 pr_debug("Setting NTB Input Size failed\n"); 285 } 286 287 /* verify maximum size of transmitted NTB in bytes */ 288 if ((ctx->tx_max < 289 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) || 290 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) { 291 pr_debug("Using default maximum transmit length=%d\n", 292 CDC_NCM_NTB_MAX_SIZE_TX); 293 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX; 294 } 295 296 /* 297 * verify that the structure alignment is: 298 * - power of two 299 * - not greater than the maximum transmit length 300 * - not less than four bytes 301 */ 302 val = ctx->tx_ndp_modulus; 303 304 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || 305 (val != ((-val) & val)) || (val >= ctx->tx_max)) { 306 pr_debug("Using default alignment: 4 bytes\n"); 307 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; 308 } 309 310 /* 311 * verify that the payload alignment is: 312 * - power of two 313 * - not greater than the maximum transmit length 314 * - not less than four bytes 315 */ 316 val = ctx->tx_modulus; 317 318 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || 319 (val != ((-val) & val)) || (val >= ctx->tx_max)) { 320 pr_debug("Using default transmit modulus: 4 bytes\n"); 321 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; 322 } 323 324 /* verify the payload remainder */ 325 if (ctx->tx_remainder >= ctx->tx_modulus) { 326 pr_debug("Using default transmit remainder: 0 bytes\n"); 327 ctx->tx_remainder = 0; 328 } 329 330 /* adjust TX-remainder according to NCM specification. */ 331 ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) & 332 (ctx->tx_modulus - 1)); 333 334 /* additional configuration */ 335 336 /* set CRC Mode */ 337 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) { 338 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | 339 USB_RECIP_INTERFACE; 340 req.bNotificationType = USB_CDC_SET_CRC_MODE; 341 req.wValue = cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED); 342 req.wIndex = cpu_to_le16(iface_no); 343 req.wLength = 0; 344 345 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000); 346 if (err) 347 pr_debug("Setting CRC mode off failed\n"); 348 } 349 350 /* set NTB format, if both formats are supported */ 351 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) { 352 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | 353 USB_RECIP_INTERFACE; 354 req.bNotificationType = USB_CDC_SET_NTB_FORMAT; 355 req.wValue = cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT); 356 req.wIndex = cpu_to_le16(iface_no); 357 req.wLength = 0; 358 359 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000); 360 if (err) 361 pr_debug("Setting NTB format to 16-bit failed\n"); 362 } 363 364 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE; 365 366 /* set Max Datagram Size (MTU) */ 367 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) { 368 __le16 max_datagram_size; 369 u16 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize); 370 371 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | 372 USB_RECIP_INTERFACE; 373 req.bNotificationType = USB_CDC_GET_MAX_DATAGRAM_SIZE; 374 req.wValue = 0; 375 req.wIndex = cpu_to_le16(iface_no); 376 req.wLength = cpu_to_le16(2); 377 378 err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 0, NULL, 379 1000); 380 if (err) { 381 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n", 382 CDC_NCM_MIN_DATAGRAM_SIZE); 383 } else { 384 ctx->max_datagram_size = le16_to_cpu(max_datagram_size); 385 /* Check Eth descriptor value */ 386 if (eth_max_sz < CDC_NCM_MAX_DATAGRAM_SIZE) { 387 if (ctx->max_datagram_size > eth_max_sz) 388 ctx->max_datagram_size = eth_max_sz; 389 } else { 390 if (ctx->max_datagram_size > 391 CDC_NCM_MAX_DATAGRAM_SIZE) 392 ctx->max_datagram_size = 393 CDC_NCM_MAX_DATAGRAM_SIZE; 394 } 395 396 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE) 397 ctx->max_datagram_size = 398 CDC_NCM_MIN_DATAGRAM_SIZE; 399 400 /* if value changed, update device */ 401 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | 402 USB_RECIP_INTERFACE; 403 req.bNotificationType = USB_CDC_SET_MAX_DATAGRAM_SIZE; 404 req.wValue = 0; 405 req.wIndex = cpu_to_le16(iface_no); 406 req.wLength = 2; 407 max_datagram_size = cpu_to_le16(ctx->max_datagram_size); 408 409 err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 410 0, NULL, 1000); 411 if (err) 412 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n"); 413 } 414 415 } 416 417 if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN)) 418 ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN; 419 420 return 0; 421 } 422 423 static void 424 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf) 425 { 426 struct usb_host_endpoint *e; 427 u8 ep; 428 429 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) { 430 431 e = intf->cur_altsetting->endpoint + ep; 432 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 433 case USB_ENDPOINT_XFER_INT: 434 if (usb_endpoint_dir_in(&e->desc)) { 435 if (ctx->status_ep == NULL) 436 ctx->status_ep = e; 437 } 438 break; 439 440 case USB_ENDPOINT_XFER_BULK: 441 if (usb_endpoint_dir_in(&e->desc)) { 442 if (ctx->in_ep == NULL) 443 ctx->in_ep = e; 444 } else { 445 if (ctx->out_ep == NULL) 446 ctx->out_ep = e; 447 } 448 break; 449 450 default: 451 break; 452 } 453 } 454 } 455 456 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx) 457 { 458 if (ctx == NULL) 459 return; 460 461 del_timer_sync(&ctx->tx_timer); 462 463 if (ctx->data_claimed) { 464 usb_set_intfdata(ctx->data, NULL); 465 usb_driver_release_interface(driver_of(ctx->intf), ctx->data); 466 } 467 468 if (ctx->control_claimed) { 469 usb_set_intfdata(ctx->control, NULL); 470 usb_driver_release_interface(driver_of(ctx->intf), 471 ctx->control); 472 } 473 474 if (ctx->tx_rem_skb != NULL) { 475 dev_kfree_skb_any(ctx->tx_rem_skb); 476 ctx->tx_rem_skb = NULL; 477 } 478 479 if (ctx->tx_curr_skb != NULL) { 480 dev_kfree_skb_any(ctx->tx_curr_skb); 481 ctx->tx_curr_skb = NULL; 482 } 483 484 kfree(ctx); 485 } 486 487 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf) 488 { 489 struct cdc_ncm_ctx *ctx; 490 struct usb_driver *driver; 491 u8 *buf; 492 int len; 493 int temp; 494 u8 iface_no; 495 496 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 497 if (ctx == NULL) 498 goto error; 499 500 memset(ctx, 0, sizeof(*ctx)); 501 502 init_timer(&ctx->tx_timer); 503 spin_lock_init(&ctx->mtx); 504 ctx->netdev = dev->net; 505 506 /* store ctx pointer in device data field */ 507 dev->data[0] = (unsigned long)ctx; 508 509 /* get some pointers */ 510 driver = driver_of(intf); 511 buf = intf->cur_altsetting->extra; 512 len = intf->cur_altsetting->extralen; 513 514 ctx->udev = dev->udev; 515 ctx->intf = intf; 516 517 /* parse through descriptors associated with control interface */ 518 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) { 519 520 if (buf[1] != USB_DT_CS_INTERFACE) 521 goto advance; 522 523 switch (buf[2]) { 524 case USB_CDC_UNION_TYPE: 525 if (buf[0] < sizeof(*(ctx->union_desc))) 526 break; 527 528 ctx->union_desc = 529 (const struct usb_cdc_union_desc *)buf; 530 531 ctx->control = usb_ifnum_to_if(dev->udev, 532 ctx->union_desc->bMasterInterface0); 533 ctx->data = usb_ifnum_to_if(dev->udev, 534 ctx->union_desc->bSlaveInterface0); 535 break; 536 537 case USB_CDC_ETHERNET_TYPE: 538 if (buf[0] < sizeof(*(ctx->ether_desc))) 539 break; 540 541 ctx->ether_desc = 542 (const struct usb_cdc_ether_desc *)buf; 543 dev->hard_mtu = 544 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize); 545 546 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE) 547 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE; 548 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE) 549 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE; 550 break; 551 552 case USB_CDC_NCM_TYPE: 553 if (buf[0] < sizeof(*(ctx->func_desc))) 554 break; 555 556 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf; 557 break; 558 559 default: 560 break; 561 } 562 advance: 563 /* advance to next descriptor */ 564 temp = buf[0]; 565 buf += temp; 566 len -= temp; 567 } 568 569 /* check if we got everything */ 570 if ((ctx->control == NULL) || (ctx->data == NULL) || 571 (ctx->ether_desc == NULL)) 572 goto error; 573 574 /* claim interfaces, if any */ 575 if (ctx->data != intf) { 576 temp = usb_driver_claim_interface(driver, ctx->data, dev); 577 if (temp) 578 goto error; 579 ctx->data_claimed = 1; 580 } 581 582 if (ctx->control != intf) { 583 temp = usb_driver_claim_interface(driver, ctx->control, dev); 584 if (temp) 585 goto error; 586 ctx->control_claimed = 1; 587 } 588 589 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber; 590 591 /* reset data interface */ 592 temp = usb_set_interface(dev->udev, iface_no, 0); 593 if (temp) 594 goto error; 595 596 /* initialize data interface */ 597 if (cdc_ncm_setup(ctx)) 598 goto error; 599 600 /* configure data interface */ 601 temp = usb_set_interface(dev->udev, iface_no, 1); 602 if (temp) 603 goto error; 604 605 cdc_ncm_find_endpoints(ctx, ctx->data); 606 cdc_ncm_find_endpoints(ctx, ctx->control); 607 608 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) || 609 (ctx->status_ep == NULL)) 610 goto error; 611 612 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops; 613 614 usb_set_intfdata(ctx->data, dev); 615 usb_set_intfdata(ctx->control, dev); 616 usb_set_intfdata(ctx->intf, dev); 617 618 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress); 619 if (temp) 620 goto error; 621 622 dev_info(&dev->udev->dev, "MAC-Address: " 623 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n", 624 dev->net->dev_addr[0], dev->net->dev_addr[1], 625 dev->net->dev_addr[2], dev->net->dev_addr[3], 626 dev->net->dev_addr[4], dev->net->dev_addr[5]); 627 628 dev->in = usb_rcvbulkpipe(dev->udev, 629 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 630 dev->out = usb_sndbulkpipe(dev->udev, 631 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 632 dev->status = ctx->status_ep; 633 dev->rx_urb_size = ctx->rx_max; 634 635 /* 636 * We should get an event when network connection is "connected" or 637 * "disconnected". Set network connection in "disconnected" state 638 * (carrier is OFF) during attach, so the IP network stack does not 639 * start IPv6 negotiation and more. 640 */ 641 netif_carrier_off(dev->net); 642 ctx->tx_speed = ctx->rx_speed = 0; 643 return 0; 644 645 error: 646 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]); 647 dev->data[0] = 0; 648 dev_info(&dev->udev->dev, "Descriptor failure\n"); 649 return -ENODEV; 650 } 651 652 static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf) 653 { 654 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; 655 struct usb_driver *driver; 656 657 if (ctx == NULL) 658 return; /* no setup */ 659 660 driver = driver_of(intf); 661 662 usb_set_intfdata(ctx->data, NULL); 663 usb_set_intfdata(ctx->control, NULL); 664 usb_set_intfdata(ctx->intf, NULL); 665 666 /* release interfaces, if any */ 667 if (ctx->data_claimed) { 668 usb_driver_release_interface(driver, ctx->data); 669 ctx->data_claimed = 0; 670 } 671 672 if (ctx->control_claimed) { 673 usb_driver_release_interface(driver, ctx->control); 674 ctx->control_claimed = 0; 675 } 676 677 cdc_ncm_free(ctx); 678 } 679 680 static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max) 681 { 682 if (first >= max) 683 return; 684 if (first >= end) 685 return; 686 if (end > max) 687 end = max; 688 memset(ptr + first, 0, end - first); 689 } 690 691 static struct sk_buff * 692 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb) 693 { 694 struct sk_buff *skb_out; 695 u32 rem; 696 u32 offset; 697 u32 last_offset; 698 u16 n = 0; 699 u8 ready2send = 0; 700 701 /* if there is a remaining skb, it gets priority */ 702 if (skb != NULL) 703 swap(skb, ctx->tx_rem_skb); 704 else 705 ready2send = 1; 706 707 /* 708 * +----------------+ 709 * | skb_out | 710 * +----------------+ 711 * ^ offset 712 * ^ last_offset 713 */ 714 715 /* check if we are resuming an OUT skb */ 716 if (ctx->tx_curr_skb != NULL) { 717 /* pop variables */ 718 skb_out = ctx->tx_curr_skb; 719 offset = ctx->tx_curr_offset; 720 last_offset = ctx->tx_curr_last_offset; 721 n = ctx->tx_curr_frame_num; 722 723 } else { 724 /* reset variables */ 725 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC); 726 if (skb_out == NULL) { 727 if (skb != NULL) { 728 dev_kfree_skb_any(skb); 729 ctx->netdev->stats.tx_dropped++; 730 } 731 goto exit_no_skb; 732 } 733 734 /* make room for NTH and NDP */ 735 offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16), 736 ctx->tx_ndp_modulus) + 737 sizeof(struct usb_cdc_ncm_ndp16) + 738 (ctx->tx_max_datagrams + 1) * 739 sizeof(struct usb_cdc_ncm_dpe16); 740 741 /* store last valid offset before alignment */ 742 last_offset = offset; 743 /* align first Datagram offset correctly */ 744 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder; 745 /* zero buffer till the first IP datagram */ 746 cdc_ncm_zero_fill(skb_out->data, 0, offset, offset); 747 n = 0; 748 ctx->tx_curr_frame_num = 0; 749 } 750 751 for (; n < ctx->tx_max_datagrams; n++) { 752 /* check if end of transmit buffer is reached */ 753 if (offset >= ctx->tx_max) { 754 ready2send = 1; 755 break; 756 } 757 /* compute maximum buffer size */ 758 rem = ctx->tx_max - offset; 759 760 if (skb == NULL) { 761 skb = ctx->tx_rem_skb; 762 ctx->tx_rem_skb = NULL; 763 764 /* check for end of skb */ 765 if (skb == NULL) 766 break; 767 } 768 769 if (skb->len > rem) { 770 if (n == 0) { 771 /* won't fit, MTU problem? */ 772 dev_kfree_skb_any(skb); 773 skb = NULL; 774 ctx->netdev->stats.tx_dropped++; 775 } else { 776 /* no room for skb - store for later */ 777 if (ctx->tx_rem_skb != NULL) { 778 dev_kfree_skb_any(ctx->tx_rem_skb); 779 ctx->netdev->stats.tx_dropped++; 780 } 781 ctx->tx_rem_skb = skb; 782 skb = NULL; 783 ready2send = 1; 784 } 785 break; 786 } 787 788 memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len); 789 790 ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len); 791 ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset); 792 793 /* update offset */ 794 offset += skb->len; 795 796 /* store last valid offset before alignment */ 797 last_offset = offset; 798 799 /* align offset correctly */ 800 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder; 801 802 /* zero padding */ 803 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, 804 ctx->tx_max); 805 dev_kfree_skb_any(skb); 806 skb = NULL; 807 } 808 809 /* free up any dangling skb */ 810 if (skb != NULL) { 811 dev_kfree_skb_any(skb); 812 skb = NULL; 813 ctx->netdev->stats.tx_dropped++; 814 } 815 816 ctx->tx_curr_frame_num = n; 817 818 if (n == 0) { 819 /* wait for more frames */ 820 /* push variables */ 821 ctx->tx_curr_skb = skb_out; 822 ctx->tx_curr_offset = offset; 823 ctx->tx_curr_last_offset = last_offset; 824 goto exit_no_skb; 825 826 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) { 827 /* wait for more frames */ 828 /* push variables */ 829 ctx->tx_curr_skb = skb_out; 830 ctx->tx_curr_offset = offset; 831 ctx->tx_curr_last_offset = last_offset; 832 /* set the pending count */ 833 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT) 834 ctx->tx_timer_pending = 2; 835 goto exit_no_skb; 836 837 } else { 838 /* frame goes out */ 839 /* variables will be reset at next call */ 840 } 841 842 /* check for overflow */ 843 if (last_offset > ctx->tx_max) 844 last_offset = ctx->tx_max; 845 846 /* revert offset */ 847 offset = last_offset; 848 849 /* 850 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes, 851 * we send buffers as it is. If we get more data, it would be more 852 * efficient for USB HS mobile device with DMA engine to receive a full 853 * size NTB, than canceling DMA transfer and receiving a short packet. 854 */ 855 if (offset > CDC_NCM_MIN_TX_PKT) 856 offset = ctx->tx_max; 857 858 /* final zero padding */ 859 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max); 860 861 /* store last offset */ 862 last_offset = offset; 863 864 if ((last_offset < ctx->tx_max) && ((last_offset % 865 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) { 866 /* force short packet */ 867 *(((u8 *)skb_out->data) + last_offset) = 0; 868 last_offset++; 869 } 870 871 /* zero the rest of the DPEs plus the last NULL entry */ 872 for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) { 873 ctx->tx_ncm.dpe16[n].wDatagramLength = 0; 874 ctx->tx_ncm.dpe16[n].wDatagramIndex = 0; 875 } 876 877 /* fill out 16-bit NTB header */ 878 ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN); 879 ctx->tx_ncm.nth16.wHeaderLength = 880 cpu_to_le16(sizeof(ctx->tx_ncm.nth16)); 881 ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq); 882 ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset); 883 ctx->tx_ncm.nth16.wNdpIndex = ALIGN(sizeof(struct usb_cdc_ncm_nth16), 884 ctx->tx_ndp_modulus); 885 886 memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16)); 887 ctx->tx_seq++; 888 889 /* fill out 16-bit NDP table */ 890 ctx->tx_ncm.ndp16.dwSignature = 891 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN); 892 rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) * 893 sizeof(struct usb_cdc_ncm_dpe16)); 894 ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem); 895 ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */ 896 897 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex, 898 &(ctx->tx_ncm.ndp16), 899 sizeof(ctx->tx_ncm.ndp16)); 900 901 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex + 902 sizeof(ctx->tx_ncm.ndp16), 903 &(ctx->tx_ncm.dpe16), 904 (ctx->tx_curr_frame_num + 1) * 905 sizeof(struct usb_cdc_ncm_dpe16)); 906 907 /* set frame length */ 908 skb_put(skb_out, last_offset); 909 910 /* return skb */ 911 ctx->tx_curr_skb = NULL; 912 return skb_out; 913 914 exit_no_skb: 915 return NULL; 916 } 917 918 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx) 919 { 920 /* start timer, if not already started */ 921 if (timer_pending(&ctx->tx_timer) == 0) { 922 ctx->tx_timer.function = &cdc_ncm_tx_timeout; 923 ctx->tx_timer.data = (unsigned long)ctx; 924 ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000); 925 add_timer(&ctx->tx_timer); 926 } 927 } 928 929 static void cdc_ncm_tx_timeout(unsigned long arg) 930 { 931 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg; 932 u8 restart; 933 934 spin_lock(&ctx->mtx); 935 if (ctx->tx_timer_pending != 0) { 936 ctx->tx_timer_pending--; 937 restart = 1; 938 } else { 939 restart = 0; 940 } 941 942 spin_unlock(&ctx->mtx); 943 944 if (restart) { 945 spin_lock(&ctx->mtx); 946 cdc_ncm_tx_timeout_start(ctx); 947 spin_unlock(&ctx->mtx); 948 } else if (ctx->netdev != NULL) { 949 usbnet_start_xmit(NULL, ctx->netdev); 950 } 951 } 952 953 static struct sk_buff * 954 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) 955 { 956 struct sk_buff *skb_out; 957 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; 958 u8 need_timer = 0; 959 960 /* 961 * The Ethernet API we are using does not support transmitting 962 * multiple Ethernet frames in a single call. This driver will 963 * accumulate multiple Ethernet frames and send out a larger 964 * USB frame when the USB buffer is full or when a single jiffies 965 * timeout happens. 966 */ 967 if (ctx == NULL) 968 goto error; 969 970 spin_lock(&ctx->mtx); 971 skb_out = cdc_ncm_fill_tx_frame(ctx, skb); 972 if (ctx->tx_curr_skb != NULL) 973 need_timer = 1; 974 975 /* Start timer, if there is a remaining skb */ 976 if (need_timer) 977 cdc_ncm_tx_timeout_start(ctx); 978 979 if (skb_out) 980 dev->net->stats.tx_packets += ctx->tx_curr_frame_num; 981 982 spin_unlock(&ctx->mtx); 983 return skb_out; 984 985 error: 986 if (skb != NULL) 987 dev_kfree_skb_any(skb); 988 989 return NULL; 990 } 991 992 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in) 993 { 994 struct sk_buff *skb; 995 struct cdc_ncm_ctx *ctx; 996 int sumlen; 997 int actlen; 998 int temp; 999 int nframes; 1000 int x; 1001 int offset; 1002 1003 ctx = (struct cdc_ncm_ctx *)dev->data[0]; 1004 if (ctx == NULL) 1005 goto error; 1006 1007 actlen = skb_in->len; 1008 sumlen = CDC_NCM_NTB_MAX_SIZE_RX; 1009 1010 if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) { 1011 pr_debug("frame too short\n"); 1012 goto error; 1013 } 1014 1015 memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data), 1016 sizeof(ctx->rx_ncm.nth16)); 1017 1018 if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) != 1019 USB_CDC_NCM_NTH16_SIGN) { 1020 pr_debug("invalid NTH16 signature <%u>\n", 1021 le32_to_cpu(ctx->rx_ncm.nth16.dwSignature)); 1022 goto error; 1023 } 1024 1025 temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength); 1026 if (temp > sumlen) { 1027 pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen); 1028 goto error; 1029 } 1030 1031 temp = le16_to_cpu(ctx->rx_ncm.nth16.wNdpIndex); 1032 if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) { 1033 pr_debug("invalid DPT16 index\n"); 1034 goto error; 1035 } 1036 1037 memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp, 1038 sizeof(ctx->rx_ncm.ndp16)); 1039 1040 if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) != 1041 USB_CDC_NCM_NDP16_NOCRC_SIGN) { 1042 pr_debug("invalid DPT16 signature <%u>\n", 1043 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature)); 1044 goto error; 1045 } 1046 1047 if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) < 1048 USB_CDC_NCM_NDP16_LENGTH_MIN) { 1049 pr_debug("invalid DPT16 length <%u>\n", 1050 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature)); 1051 goto error; 1052 } 1053 1054 nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) - 1055 sizeof(struct usb_cdc_ncm_ndp16)) / 1056 sizeof(struct usb_cdc_ncm_dpe16)); 1057 nframes--; /* we process NDP entries except for the last one */ 1058 1059 pr_debug("nframes = %u\n", nframes); 1060 1061 temp += sizeof(ctx->rx_ncm.ndp16); 1062 1063 if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) { 1064 pr_debug("Invalid nframes = %d\n", nframes); 1065 goto error; 1066 } 1067 1068 if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) { 1069 pr_debug("Truncating number of frames from %u to %u\n", 1070 nframes, CDC_NCM_DPT_DATAGRAMS_MAX); 1071 nframes = CDC_NCM_DPT_DATAGRAMS_MAX; 1072 } 1073 1074 memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp, 1075 nframes * (sizeof(struct usb_cdc_ncm_dpe16))); 1076 1077 for (x = 0; x < nframes; x++) { 1078 offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex); 1079 temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength); 1080 1081 /* 1082 * CDC NCM ch. 3.7 1083 * All entries after first NULL entry are to be ignored 1084 */ 1085 if ((offset == 0) || (temp == 0)) { 1086 if (!x) 1087 goto error; /* empty NTB */ 1088 break; 1089 } 1090 1091 /* sanity checking */ 1092 if (((offset + temp) > actlen) || 1093 (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) { 1094 pr_debug("invalid frame detected (ignored)" 1095 "offset[%u]=%u, length=%u, skb=%p\n", 1096 x, offset, temp, skb_in); 1097 if (!x) 1098 goto error; 1099 break; 1100 1101 } else { 1102 skb = skb_clone(skb_in, GFP_ATOMIC); 1103 if (!skb) 1104 goto error; 1105 skb->len = temp; 1106 skb->data = ((u8 *)skb_in->data) + offset; 1107 skb_set_tail_pointer(skb, temp); 1108 usbnet_skb_return(dev, skb); 1109 } 1110 } 1111 return 1; 1112 error: 1113 return 0; 1114 } 1115 1116 static void 1117 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx, 1118 struct usb_cdc_speed_change *data) 1119 { 1120 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate); 1121 uint32_t tx_speed = le32_to_cpu(data->ULBitRate); 1122 1123 /* 1124 * Currently the USB-NET API does not support reporting the actual 1125 * device speed. Do print it instead. 1126 */ 1127 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) { 1128 ctx->tx_speed = tx_speed; 1129 ctx->rx_speed = rx_speed; 1130 1131 if ((tx_speed > 1000000) && (rx_speed > 1000000)) { 1132 printk(KERN_INFO KBUILD_MODNAME 1133 ": %s: %u mbit/s downlink " 1134 "%u mbit/s uplink\n", 1135 ctx->netdev->name, 1136 (unsigned int)(rx_speed / 1000000U), 1137 (unsigned int)(tx_speed / 1000000U)); 1138 } else { 1139 printk(KERN_INFO KBUILD_MODNAME 1140 ": %s: %u kbit/s downlink " 1141 "%u kbit/s uplink\n", 1142 ctx->netdev->name, 1143 (unsigned int)(rx_speed / 1000U), 1144 (unsigned int)(tx_speed / 1000U)); 1145 } 1146 } 1147 } 1148 1149 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb) 1150 { 1151 struct cdc_ncm_ctx *ctx; 1152 struct usb_cdc_notification *event; 1153 1154 ctx = (struct cdc_ncm_ctx *)dev->data[0]; 1155 1156 if (urb->actual_length < sizeof(*event)) 1157 return; 1158 1159 /* test for split data in 8-byte chunks */ 1160 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) { 1161 cdc_ncm_speed_change(ctx, 1162 (struct usb_cdc_speed_change *)urb->transfer_buffer); 1163 return; 1164 } 1165 1166 event = urb->transfer_buffer; 1167 1168 switch (event->bNotificationType) { 1169 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 1170 /* 1171 * According to the CDC NCM specification ch.7.1 1172 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be 1173 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE. 1174 */ 1175 ctx->connected = event->wValue; 1176 1177 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:" 1178 " %sconnected\n", 1179 ctx->netdev->name, ctx->connected ? "" : "dis"); 1180 1181 if (ctx->connected) 1182 netif_carrier_on(dev->net); 1183 else { 1184 netif_carrier_off(dev->net); 1185 ctx->tx_speed = ctx->rx_speed = 0; 1186 } 1187 break; 1188 1189 case USB_CDC_NOTIFY_SPEED_CHANGE: 1190 if (urb->actual_length < (sizeof(*event) + 1191 sizeof(struct usb_cdc_speed_change))) 1192 set_bit(EVENT_STS_SPLIT, &dev->flags); 1193 else 1194 cdc_ncm_speed_change(ctx, 1195 (struct usb_cdc_speed_change *) &event[1]); 1196 break; 1197 1198 default: 1199 dev_err(&dev->udev->dev, "NCM: unexpected " 1200 "notification 0x%02x!\n", event->bNotificationType); 1201 break; 1202 } 1203 } 1204 1205 static int cdc_ncm_check_connect(struct usbnet *dev) 1206 { 1207 struct cdc_ncm_ctx *ctx; 1208 1209 ctx = (struct cdc_ncm_ctx *)dev->data[0]; 1210 if (ctx == NULL) 1211 return 1; /* disconnected */ 1212 1213 return !ctx->connected; 1214 } 1215 1216 static int 1217 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod) 1218 { 1219 return usbnet_probe(udev, prod); 1220 } 1221 1222 static void cdc_ncm_disconnect(struct usb_interface *intf) 1223 { 1224 struct usbnet *dev = usb_get_intfdata(intf); 1225 1226 if (dev == NULL) 1227 return; /* already disconnected */ 1228 1229 usbnet_disconnect(intf); 1230 } 1231 1232 static int cdc_ncm_manage_power(struct usbnet *dev, int status) 1233 { 1234 dev->intf->needs_remote_wakeup = status; 1235 return 0; 1236 } 1237 1238 static const struct driver_info cdc_ncm_info = { 1239 .description = "CDC NCM", 1240 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET, 1241 .bind = cdc_ncm_bind, 1242 .unbind = cdc_ncm_unbind, 1243 .check_connect = cdc_ncm_check_connect, 1244 .manage_power = cdc_ncm_manage_power, 1245 .status = cdc_ncm_status, 1246 .rx_fixup = cdc_ncm_rx_fixup, 1247 .tx_fixup = cdc_ncm_tx_fixup, 1248 }; 1249 1250 static struct usb_driver cdc_ncm_driver = { 1251 .name = "cdc_ncm", 1252 .id_table = cdc_devs, 1253 .probe = cdc_ncm_probe, 1254 .disconnect = cdc_ncm_disconnect, 1255 .suspend = usbnet_suspend, 1256 .resume = usbnet_resume, 1257 .supports_autosuspend = 1, 1258 }; 1259 1260 static struct ethtool_ops cdc_ncm_ethtool_ops = { 1261 .get_drvinfo = cdc_ncm_get_drvinfo, 1262 .get_link = usbnet_get_link, 1263 .get_msglevel = usbnet_get_msglevel, 1264 .set_msglevel = usbnet_set_msglevel, 1265 .get_settings = usbnet_get_settings, 1266 .set_settings = usbnet_set_settings, 1267 .nway_reset = usbnet_nway_reset, 1268 }; 1269 1270 static int __init cdc_ncm_init(void) 1271 { 1272 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n"); 1273 return usb_register(&cdc_ncm_driver); 1274 } 1275 1276 module_init(cdc_ncm_init); 1277 1278 static void __exit cdc_ncm_exit(void) 1279 { 1280 usb_deregister(&cdc_ncm_driver); 1281 } 1282 1283 module_exit(cdc_ncm_exit); 1284 1285 MODULE_AUTHOR("Hans Petter Selasky"); 1286 MODULE_DESCRIPTION("USB CDC NCM host driver"); 1287 MODULE_LICENSE("Dual BSD/GPL"); 1288