1 /* 2 * ipheth.c - Apple iPhone USB Ethernet driver 3 * 4 * Copyright (c) 2009 Diego Giagio <diego@giagio.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of GIAGIO.COM nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * Alternatively, provided that this notice is retained in full, this 20 * software may be distributed under the terms of the GNU General 21 * Public License ("GPL") version 2, in which case the provisions of the 22 * GPL apply INSTEAD OF those given above. 23 * 24 * The provided data structures and external interfaces from this code 25 * are not restricted to be used by modules with a GPL compatible license. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 38 * DAMAGE. 39 * 40 * 41 * Attention: iPhone device must be paired, otherwise it won't respond to our 42 * driver. For more info: http://giagio.com/wiki/moin.cgi/iPhoneEthernetDriver 43 * 44 */ 45 46 #include <linux/kernel.h> 47 #include <linux/errno.h> 48 #include <linux/init.h> 49 #include <linux/slab.h> 50 #include <linux/module.h> 51 #include <linux/netdevice.h> 52 #include <linux/etherdevice.h> 53 #include <linux/ethtool.h> 54 #include <linux/usb.h> 55 #include <linux/workqueue.h> 56 57 #define USB_VENDOR_APPLE 0x05ac 58 #define USB_PRODUCT_IPHONE 0x1290 59 #define USB_PRODUCT_IPHONE_3G 0x1292 60 #define USB_PRODUCT_IPHONE_3GS 0x1294 61 #define USB_PRODUCT_IPHONE_4 0x1297 62 #define USB_PRODUCT_IPAD 0x129a 63 #define USB_PRODUCT_IPHONE_4_VZW 0x129c 64 #define USB_PRODUCT_IPHONE_4S 0x12a0 65 #define USB_PRODUCT_IPHONE_5 0x12a8 66 67 #define IPHETH_USBINTF_CLASS 255 68 #define IPHETH_USBINTF_SUBCLASS 253 69 #define IPHETH_USBINTF_PROTO 1 70 71 #define IPHETH_BUF_SIZE 1516 72 #define IPHETH_IP_ALIGN 2 /* padding at front of URB */ 73 #define IPHETH_TX_TIMEOUT (5 * HZ) 74 75 #define IPHETH_INTFNUM 2 76 #define IPHETH_ALT_INTFNUM 1 77 78 #define IPHETH_CTRL_ENDP 0x00 79 #define IPHETH_CTRL_BUF_SIZE 0x40 80 #define IPHETH_CTRL_TIMEOUT (5 * HZ) 81 82 #define IPHETH_CMD_GET_MACADDR 0x00 83 #define IPHETH_CMD_CARRIER_CHECK 0x45 84 85 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ) 86 #define IPHETH_CARRIER_ON 0x04 87 88 static struct usb_device_id ipheth_table[] = { 89 { USB_DEVICE_AND_INTERFACE_INFO( 90 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE, 91 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 92 IPHETH_USBINTF_PROTO) }, 93 { USB_DEVICE_AND_INTERFACE_INFO( 94 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3G, 95 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 96 IPHETH_USBINTF_PROTO) }, 97 { USB_DEVICE_AND_INTERFACE_INFO( 98 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3GS, 99 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 100 IPHETH_USBINTF_PROTO) }, 101 { USB_DEVICE_AND_INTERFACE_INFO( 102 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4, 103 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 104 IPHETH_USBINTF_PROTO) }, 105 { USB_DEVICE_AND_INTERFACE_INFO( 106 USB_VENDOR_APPLE, USB_PRODUCT_IPAD, 107 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 108 IPHETH_USBINTF_PROTO) }, 109 { USB_DEVICE_AND_INTERFACE_INFO( 110 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4_VZW, 111 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 112 IPHETH_USBINTF_PROTO) }, 113 { USB_DEVICE_AND_INTERFACE_INFO( 114 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4S, 115 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 116 IPHETH_USBINTF_PROTO) }, 117 { USB_DEVICE_AND_INTERFACE_INFO( 118 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_5, 119 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 120 IPHETH_USBINTF_PROTO) }, 121 { } 122 }; 123 MODULE_DEVICE_TABLE(usb, ipheth_table); 124 125 struct ipheth_device { 126 struct usb_device *udev; 127 struct usb_interface *intf; 128 struct net_device *net; 129 struct sk_buff *tx_skb; 130 struct urb *tx_urb; 131 struct urb *rx_urb; 132 unsigned char *tx_buf; 133 unsigned char *rx_buf; 134 unsigned char *ctrl_buf; 135 u8 bulk_in; 136 u8 bulk_out; 137 struct delayed_work carrier_work; 138 }; 139 140 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags); 141 142 static int ipheth_alloc_urbs(struct ipheth_device *iphone) 143 { 144 struct urb *tx_urb = NULL; 145 struct urb *rx_urb = NULL; 146 u8 *tx_buf = NULL; 147 u8 *rx_buf = NULL; 148 149 tx_urb = usb_alloc_urb(0, GFP_KERNEL); 150 if (tx_urb == NULL) 151 goto error_nomem; 152 153 rx_urb = usb_alloc_urb(0, GFP_KERNEL); 154 if (rx_urb == NULL) 155 goto free_tx_urb; 156 157 tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE, 158 GFP_KERNEL, &tx_urb->transfer_dma); 159 if (tx_buf == NULL) 160 goto free_rx_urb; 161 162 rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE, 163 GFP_KERNEL, &rx_urb->transfer_dma); 164 if (rx_buf == NULL) 165 goto free_tx_buf; 166 167 168 iphone->tx_urb = tx_urb; 169 iphone->rx_urb = rx_urb; 170 iphone->tx_buf = tx_buf; 171 iphone->rx_buf = rx_buf; 172 return 0; 173 174 free_tx_buf: 175 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf, 176 tx_urb->transfer_dma); 177 free_rx_urb: 178 usb_free_urb(rx_urb); 179 free_tx_urb: 180 usb_free_urb(tx_urb); 181 error_nomem: 182 return -ENOMEM; 183 } 184 185 static void ipheth_free_urbs(struct ipheth_device *iphone) 186 { 187 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->rx_buf, 188 iphone->rx_urb->transfer_dma); 189 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf, 190 iphone->tx_urb->transfer_dma); 191 usb_free_urb(iphone->rx_urb); 192 usb_free_urb(iphone->tx_urb); 193 } 194 195 static void ipheth_kill_urbs(struct ipheth_device *dev) 196 { 197 usb_kill_urb(dev->tx_urb); 198 usb_kill_urb(dev->rx_urb); 199 } 200 201 static void ipheth_rcvbulk_callback(struct urb *urb) 202 { 203 struct ipheth_device *dev; 204 struct sk_buff *skb; 205 int status; 206 char *buf; 207 int len; 208 209 dev = urb->context; 210 if (dev == NULL) 211 return; 212 213 status = urb->status; 214 switch (status) { 215 case -ENOENT: 216 case -ECONNRESET: 217 case -ESHUTDOWN: 218 return; 219 case 0: 220 break; 221 default: 222 dev_err(&dev->intf->dev, "%s: urb status: %d\n", 223 __func__, status); 224 return; 225 } 226 227 if (urb->actual_length <= IPHETH_IP_ALIGN) { 228 dev->net->stats.rx_length_errors++; 229 return; 230 } 231 len = urb->actual_length - IPHETH_IP_ALIGN; 232 buf = urb->transfer_buffer + IPHETH_IP_ALIGN; 233 234 skb = dev_alloc_skb(len); 235 if (!skb) { 236 dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n", 237 __func__); 238 dev->net->stats.rx_dropped++; 239 return; 240 } 241 242 memcpy(skb_put(skb, len), buf, len); 243 skb->dev = dev->net; 244 skb->protocol = eth_type_trans(skb, dev->net); 245 246 dev->net->stats.rx_packets++; 247 dev->net->stats.rx_bytes += len; 248 249 netif_rx(skb); 250 ipheth_rx_submit(dev, GFP_ATOMIC); 251 } 252 253 static void ipheth_sndbulk_callback(struct urb *urb) 254 { 255 struct ipheth_device *dev; 256 int status = urb->status; 257 258 dev = urb->context; 259 if (dev == NULL) 260 return; 261 262 if (status != 0 && 263 status != -ENOENT && 264 status != -ECONNRESET && 265 status != -ESHUTDOWN) 266 dev_err(&dev->intf->dev, "%s: urb status: %d\n", 267 __func__, status); 268 269 dev_kfree_skb_irq(dev->tx_skb); 270 netif_wake_queue(dev->net); 271 } 272 273 static int ipheth_carrier_set(struct ipheth_device *dev) 274 { 275 struct usb_device *udev = dev->udev; 276 int retval; 277 278 retval = usb_control_msg(udev, 279 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP), 280 IPHETH_CMD_CARRIER_CHECK, /* request */ 281 0xc0, /* request type */ 282 0x00, /* value */ 283 0x02, /* index */ 284 dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE, 285 IPHETH_CTRL_TIMEOUT); 286 if (retval < 0) { 287 dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n", 288 __func__, retval); 289 return retval; 290 } 291 292 if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) 293 netif_carrier_on(dev->net); 294 else 295 netif_carrier_off(dev->net); 296 297 return 0; 298 } 299 300 static void ipheth_carrier_check_work(struct work_struct *work) 301 { 302 struct ipheth_device *dev = container_of(work, struct ipheth_device, 303 carrier_work.work); 304 305 ipheth_carrier_set(dev); 306 schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT); 307 } 308 309 static int ipheth_get_macaddr(struct ipheth_device *dev) 310 { 311 struct usb_device *udev = dev->udev; 312 struct net_device *net = dev->net; 313 int retval; 314 315 retval = usb_control_msg(udev, 316 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP), 317 IPHETH_CMD_GET_MACADDR, /* request */ 318 0xc0, /* request type */ 319 0x00, /* value */ 320 0x02, /* index */ 321 dev->ctrl_buf, 322 IPHETH_CTRL_BUF_SIZE, 323 IPHETH_CTRL_TIMEOUT); 324 if (retval < 0) { 325 dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n", 326 __func__, retval); 327 } else if (retval < ETH_ALEN) { 328 dev_err(&dev->intf->dev, 329 "%s: usb_control_msg: short packet: %d bytes\n", 330 __func__, retval); 331 retval = -EINVAL; 332 } else { 333 memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN); 334 retval = 0; 335 } 336 337 return retval; 338 } 339 340 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags) 341 { 342 struct usb_device *udev = dev->udev; 343 int retval; 344 345 usb_fill_bulk_urb(dev->rx_urb, udev, 346 usb_rcvbulkpipe(udev, dev->bulk_in), 347 dev->rx_buf, IPHETH_BUF_SIZE, 348 ipheth_rcvbulk_callback, 349 dev); 350 dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 351 352 retval = usb_submit_urb(dev->rx_urb, mem_flags); 353 if (retval) 354 dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n", 355 __func__, retval); 356 return retval; 357 } 358 359 static int ipheth_open(struct net_device *net) 360 { 361 struct ipheth_device *dev = netdev_priv(net); 362 struct usb_device *udev = dev->udev; 363 int retval = 0; 364 365 usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM); 366 367 retval = ipheth_carrier_set(dev); 368 if (retval) 369 return retval; 370 371 retval = ipheth_rx_submit(dev, GFP_KERNEL); 372 if (retval) 373 return retval; 374 375 schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT); 376 netif_start_queue(net); 377 return retval; 378 } 379 380 static int ipheth_close(struct net_device *net) 381 { 382 struct ipheth_device *dev = netdev_priv(net); 383 384 cancel_delayed_work_sync(&dev->carrier_work); 385 netif_stop_queue(net); 386 return 0; 387 } 388 389 static int ipheth_tx(struct sk_buff *skb, struct net_device *net) 390 { 391 struct ipheth_device *dev = netdev_priv(net); 392 struct usb_device *udev = dev->udev; 393 int retval; 394 395 /* Paranoid */ 396 if (skb->len > IPHETH_BUF_SIZE) { 397 WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len); 398 dev->net->stats.tx_dropped++; 399 dev_kfree_skb_irq(skb); 400 return NETDEV_TX_OK; 401 } 402 403 memcpy(dev->tx_buf, skb->data, skb->len); 404 if (skb->len < IPHETH_BUF_SIZE) 405 memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len); 406 407 usb_fill_bulk_urb(dev->tx_urb, udev, 408 usb_sndbulkpipe(udev, dev->bulk_out), 409 dev->tx_buf, IPHETH_BUF_SIZE, 410 ipheth_sndbulk_callback, 411 dev); 412 dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 413 414 retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC); 415 if (retval) { 416 dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n", 417 __func__, retval); 418 dev->net->stats.tx_errors++; 419 dev_kfree_skb_irq(skb); 420 } else { 421 dev->tx_skb = skb; 422 423 dev->net->stats.tx_packets++; 424 dev->net->stats.tx_bytes += skb->len; 425 netif_stop_queue(net); 426 } 427 428 return NETDEV_TX_OK; 429 } 430 431 static void ipheth_tx_timeout(struct net_device *net) 432 { 433 struct ipheth_device *dev = netdev_priv(net); 434 435 dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__); 436 dev->net->stats.tx_errors++; 437 usb_unlink_urb(dev->tx_urb); 438 } 439 440 static u32 ipheth_ethtool_op_get_link(struct net_device *net) 441 { 442 struct ipheth_device *dev = netdev_priv(net); 443 return netif_carrier_ok(dev->net); 444 } 445 446 static const struct ethtool_ops ops = { 447 .get_link = ipheth_ethtool_op_get_link 448 }; 449 450 static const struct net_device_ops ipheth_netdev_ops = { 451 .ndo_open = ipheth_open, 452 .ndo_stop = ipheth_close, 453 .ndo_start_xmit = ipheth_tx, 454 .ndo_tx_timeout = ipheth_tx_timeout, 455 }; 456 457 static int ipheth_probe(struct usb_interface *intf, 458 const struct usb_device_id *id) 459 { 460 struct usb_device *udev = interface_to_usbdev(intf); 461 struct usb_host_interface *hintf; 462 struct usb_endpoint_descriptor *endp; 463 struct ipheth_device *dev; 464 struct net_device *netdev; 465 int i; 466 int retval; 467 468 netdev = alloc_etherdev(sizeof(struct ipheth_device)); 469 if (!netdev) 470 return -ENOMEM; 471 472 netdev->netdev_ops = &ipheth_netdev_ops; 473 netdev->watchdog_timeo = IPHETH_TX_TIMEOUT; 474 strcpy(netdev->name, "eth%d"); 475 476 dev = netdev_priv(netdev); 477 dev->udev = udev; 478 dev->net = netdev; 479 dev->intf = intf; 480 481 /* Set up endpoints */ 482 hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM); 483 if (hintf == NULL) { 484 retval = -ENODEV; 485 dev_err(&intf->dev, "Unable to find alternate settings interface\n"); 486 goto err_endpoints; 487 } 488 489 for (i = 0; i < hintf->desc.bNumEndpoints; i++) { 490 endp = &hintf->endpoint[i].desc; 491 if (usb_endpoint_is_bulk_in(endp)) 492 dev->bulk_in = endp->bEndpointAddress; 493 else if (usb_endpoint_is_bulk_out(endp)) 494 dev->bulk_out = endp->bEndpointAddress; 495 } 496 if (!(dev->bulk_in && dev->bulk_out)) { 497 retval = -ENODEV; 498 dev_err(&intf->dev, "Unable to find endpoints\n"); 499 goto err_endpoints; 500 } 501 502 dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL); 503 if (dev->ctrl_buf == NULL) { 504 retval = -ENOMEM; 505 goto err_alloc_ctrl_buf; 506 } 507 508 retval = ipheth_get_macaddr(dev); 509 if (retval) 510 goto err_get_macaddr; 511 512 INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work); 513 514 retval = ipheth_alloc_urbs(dev); 515 if (retval) { 516 dev_err(&intf->dev, "error allocating urbs: %d\n", retval); 517 goto err_alloc_urbs; 518 } 519 520 usb_set_intfdata(intf, dev); 521 522 SET_NETDEV_DEV(netdev, &intf->dev); 523 SET_ETHTOOL_OPS(netdev, &ops); 524 525 retval = register_netdev(netdev); 526 if (retval) { 527 dev_err(&intf->dev, "error registering netdev: %d\n", retval); 528 retval = -EIO; 529 goto err_register_netdev; 530 } 531 532 dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n"); 533 return 0; 534 535 err_register_netdev: 536 ipheth_free_urbs(dev); 537 err_alloc_urbs: 538 err_get_macaddr: 539 err_alloc_ctrl_buf: 540 kfree(dev->ctrl_buf); 541 err_endpoints: 542 free_netdev(netdev); 543 return retval; 544 } 545 546 static void ipheth_disconnect(struct usb_interface *intf) 547 { 548 struct ipheth_device *dev; 549 550 dev = usb_get_intfdata(intf); 551 if (dev != NULL) { 552 unregister_netdev(dev->net); 553 ipheth_kill_urbs(dev); 554 ipheth_free_urbs(dev); 555 kfree(dev->ctrl_buf); 556 free_netdev(dev->net); 557 } 558 usb_set_intfdata(intf, NULL); 559 dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n"); 560 } 561 562 static struct usb_driver ipheth_driver = { 563 .name = "ipheth", 564 .probe = ipheth_probe, 565 .disconnect = ipheth_disconnect, 566 .id_table = ipheth_table, 567 .disable_hub_initiated_lpm = 1, 568 }; 569 570 module_usb_driver(ipheth_driver); 571 572 MODULE_AUTHOR("Diego Giagio <diego@giagio.com>"); 573 MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver"); 574 MODULE_LICENSE("Dual BSD/GPL"); 575