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 62 #define IPHETH_USBINTF_CLASS 255 63 #define IPHETH_USBINTF_SUBCLASS 253 64 #define IPHETH_USBINTF_PROTO 1 65 66 #define IPHETH_BUF_SIZE 1516 67 #define IPHETH_TX_TIMEOUT (5 * HZ) 68 69 #define IPHETH_INTFNUM 2 70 #define IPHETH_ALT_INTFNUM 1 71 72 #define IPHETH_CTRL_ENDP 0x00 73 #define IPHETH_CTRL_BUF_SIZE 0x40 74 #define IPHETH_CTRL_TIMEOUT (5 * HZ) 75 76 #define IPHETH_CMD_GET_MACADDR 0x00 77 #define IPHETH_CMD_CARRIER_CHECK 0x45 78 79 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ) 80 #define IPHETH_CARRIER_ON 0x04 81 82 static struct usb_device_id ipheth_table[] = { 83 { USB_DEVICE_AND_INTERFACE_INFO( 84 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE, 85 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 86 IPHETH_USBINTF_PROTO) }, 87 { USB_DEVICE_AND_INTERFACE_INFO( 88 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3G, 89 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 90 IPHETH_USBINTF_PROTO) }, 91 { USB_DEVICE_AND_INTERFACE_INFO( 92 USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3GS, 93 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 94 IPHETH_USBINTF_PROTO) }, 95 { } 96 }; 97 MODULE_DEVICE_TABLE(usb, ipheth_table); 98 99 struct ipheth_device { 100 struct usb_device *udev; 101 struct usb_interface *intf; 102 struct net_device *net; 103 struct sk_buff *tx_skb; 104 struct urb *tx_urb; 105 struct urb *rx_urb; 106 unsigned char *tx_buf; 107 unsigned char *rx_buf; 108 unsigned char *ctrl_buf; 109 u8 bulk_in; 110 u8 bulk_out; 111 struct delayed_work carrier_work; 112 }; 113 114 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags); 115 116 static int ipheth_alloc_urbs(struct ipheth_device *iphone) 117 { 118 struct urb *tx_urb = NULL; 119 struct urb *rx_urb = NULL; 120 u8 *tx_buf = NULL; 121 u8 *rx_buf = NULL; 122 123 tx_urb = usb_alloc_urb(0, GFP_KERNEL); 124 if (tx_urb == NULL) 125 goto error_nomem; 126 127 rx_urb = usb_alloc_urb(0, GFP_KERNEL); 128 if (rx_urb == NULL) 129 goto free_tx_urb; 130 131 tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE, 132 GFP_KERNEL, &tx_urb->transfer_dma); 133 if (tx_buf == NULL) 134 goto free_rx_urb; 135 136 rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE, 137 GFP_KERNEL, &rx_urb->transfer_dma); 138 if (rx_buf == NULL) 139 goto free_tx_buf; 140 141 142 iphone->tx_urb = tx_urb; 143 iphone->rx_urb = rx_urb; 144 iphone->tx_buf = tx_buf; 145 iphone->rx_buf = rx_buf; 146 return 0; 147 148 free_tx_buf: 149 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf, 150 tx_urb->transfer_dma); 151 free_rx_urb: 152 usb_free_urb(rx_urb); 153 free_tx_urb: 154 usb_free_urb(tx_urb); 155 error_nomem: 156 return -ENOMEM; 157 } 158 159 static void ipheth_free_urbs(struct ipheth_device *iphone) 160 { 161 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->rx_buf, 162 iphone->rx_urb->transfer_dma); 163 usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf, 164 iphone->tx_urb->transfer_dma); 165 usb_free_urb(iphone->rx_urb); 166 usb_free_urb(iphone->tx_urb); 167 } 168 169 static void ipheth_kill_urbs(struct ipheth_device *dev) 170 { 171 usb_kill_urb(dev->tx_urb); 172 usb_kill_urb(dev->rx_urb); 173 } 174 175 static void ipheth_rcvbulk_callback(struct urb *urb) 176 { 177 struct ipheth_device *dev; 178 struct sk_buff *skb; 179 int status; 180 char *buf; 181 int len; 182 183 dev = urb->context; 184 if (dev == NULL) 185 return; 186 187 status = urb->status; 188 switch (status) { 189 case -ENOENT: 190 case -ECONNRESET: 191 case -ESHUTDOWN: 192 return; 193 case 0: 194 break; 195 default: 196 err("%s: urb status: %d", __func__, urb->status); 197 return; 198 } 199 200 len = urb->actual_length; 201 buf = urb->transfer_buffer; 202 203 skb = dev_alloc_skb(NET_IP_ALIGN + len); 204 if (!skb) { 205 err("%s: dev_alloc_skb: -ENOMEM", __func__); 206 dev->net->stats.rx_dropped++; 207 return; 208 } 209 210 skb_reserve(skb, NET_IP_ALIGN); 211 memcpy(skb_put(skb, len), buf + NET_IP_ALIGN, len - NET_IP_ALIGN); 212 skb->dev = dev->net; 213 skb->protocol = eth_type_trans(skb, dev->net); 214 215 dev->net->stats.rx_packets++; 216 dev->net->stats.rx_bytes += len; 217 218 netif_rx(skb); 219 ipheth_rx_submit(dev, GFP_ATOMIC); 220 } 221 222 static void ipheth_sndbulk_callback(struct urb *urb) 223 { 224 struct ipheth_device *dev; 225 226 dev = urb->context; 227 if (dev == NULL) 228 return; 229 230 if (urb->status != 0 && 231 urb->status != -ENOENT && 232 urb->status != -ECONNRESET && 233 urb->status != -ESHUTDOWN) 234 err("%s: urb status: %d", __func__, urb->status); 235 236 dev_kfree_skb_irq(dev->tx_skb); 237 netif_wake_queue(dev->net); 238 } 239 240 static int ipheth_carrier_set(struct ipheth_device *dev) 241 { 242 struct usb_device *udev = dev->udev; 243 int retval; 244 245 retval = usb_control_msg(udev, 246 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP), 247 IPHETH_CMD_CARRIER_CHECK, /* request */ 248 0xc0, /* request type */ 249 0x00, /* value */ 250 0x02, /* index */ 251 dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE, 252 IPHETH_CTRL_TIMEOUT); 253 if (retval < 0) { 254 err("%s: usb_control_msg: %d", __func__, retval); 255 return retval; 256 } 257 258 if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) 259 netif_carrier_on(dev->net); 260 else 261 netif_carrier_off(dev->net); 262 263 return 0; 264 } 265 266 static void ipheth_carrier_check_work(struct work_struct *work) 267 { 268 struct ipheth_device *dev = container_of(work, struct ipheth_device, 269 carrier_work.work); 270 271 ipheth_carrier_set(dev); 272 schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT); 273 } 274 275 static int ipheth_get_macaddr(struct ipheth_device *dev) 276 { 277 struct usb_device *udev = dev->udev; 278 struct net_device *net = dev->net; 279 int retval; 280 281 retval = usb_control_msg(udev, 282 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP), 283 IPHETH_CMD_GET_MACADDR, /* request */ 284 0xc0, /* request type */ 285 0x00, /* value */ 286 0x02, /* index */ 287 dev->ctrl_buf, 288 IPHETH_CTRL_BUF_SIZE, 289 IPHETH_CTRL_TIMEOUT); 290 if (retval < 0) { 291 err("%s: usb_control_msg: %d", __func__, retval); 292 } else if (retval < ETH_ALEN) { 293 err("%s: usb_control_msg: short packet: %d bytes", 294 __func__, retval); 295 retval = -EINVAL; 296 } else { 297 memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN); 298 retval = 0; 299 } 300 301 return retval; 302 } 303 304 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags) 305 { 306 struct usb_device *udev = dev->udev; 307 int retval; 308 309 usb_fill_bulk_urb(dev->rx_urb, udev, 310 usb_rcvbulkpipe(udev, dev->bulk_in), 311 dev->rx_buf, IPHETH_BUF_SIZE, 312 ipheth_rcvbulk_callback, 313 dev); 314 dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 315 316 retval = usb_submit_urb(dev->rx_urb, mem_flags); 317 if (retval) 318 err("%s: usb_submit_urb: %d", __func__, retval); 319 return retval; 320 } 321 322 static int ipheth_open(struct net_device *net) 323 { 324 struct ipheth_device *dev = netdev_priv(net); 325 struct usb_device *udev = dev->udev; 326 int retval = 0; 327 328 usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM); 329 330 retval = ipheth_carrier_set(dev); 331 if (retval) 332 return retval; 333 334 retval = ipheth_rx_submit(dev, GFP_KERNEL); 335 if (retval) 336 return retval; 337 338 schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT); 339 netif_start_queue(net); 340 return retval; 341 } 342 343 static int ipheth_close(struct net_device *net) 344 { 345 struct ipheth_device *dev = netdev_priv(net); 346 347 cancel_delayed_work_sync(&dev->carrier_work); 348 netif_stop_queue(net); 349 return 0; 350 } 351 352 static int ipheth_tx(struct sk_buff *skb, struct net_device *net) 353 { 354 struct ipheth_device *dev = netdev_priv(net); 355 struct usb_device *udev = dev->udev; 356 int retval; 357 358 /* Paranoid */ 359 if (skb->len > IPHETH_BUF_SIZE) { 360 WARN(1, "%s: skb too large: %d bytes", __func__, skb->len); 361 dev->net->stats.tx_dropped++; 362 dev_kfree_skb_irq(skb); 363 return NETDEV_TX_OK; 364 } 365 366 memcpy(dev->tx_buf, skb->data, skb->len); 367 if (skb->len < IPHETH_BUF_SIZE) 368 memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len); 369 370 usb_fill_bulk_urb(dev->tx_urb, udev, 371 usb_sndbulkpipe(udev, dev->bulk_out), 372 dev->tx_buf, IPHETH_BUF_SIZE, 373 ipheth_sndbulk_callback, 374 dev); 375 dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 376 377 retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC); 378 if (retval) { 379 err("%s: usb_submit_urb: %d", __func__, retval); 380 dev->net->stats.tx_errors++; 381 dev_kfree_skb_irq(skb); 382 } else { 383 dev->tx_skb = skb; 384 385 dev->net->stats.tx_packets++; 386 dev->net->stats.tx_bytes += skb->len; 387 netif_stop_queue(net); 388 } 389 390 return NETDEV_TX_OK; 391 } 392 393 static void ipheth_tx_timeout(struct net_device *net) 394 { 395 struct ipheth_device *dev = netdev_priv(net); 396 397 err("%s: TX timeout", __func__); 398 dev->net->stats.tx_errors++; 399 usb_unlink_urb(dev->tx_urb); 400 } 401 402 static struct net_device_stats *ipheth_stats(struct net_device *net) 403 { 404 struct ipheth_device *dev = netdev_priv(net); 405 return &dev->net->stats; 406 } 407 408 static u32 ipheth_ethtool_op_get_link(struct net_device *net) 409 { 410 struct ipheth_device *dev = netdev_priv(net); 411 return netif_carrier_ok(dev->net); 412 } 413 414 static struct ethtool_ops ops = { 415 .get_link = ipheth_ethtool_op_get_link 416 }; 417 418 static const struct net_device_ops ipheth_netdev_ops = { 419 .ndo_open = &ipheth_open, 420 .ndo_stop = &ipheth_close, 421 .ndo_start_xmit = &ipheth_tx, 422 .ndo_tx_timeout = &ipheth_tx_timeout, 423 .ndo_get_stats = &ipheth_stats, 424 }; 425 426 static struct device_type ipheth_type = { 427 .name = "wwan", 428 }; 429 430 static int ipheth_probe(struct usb_interface *intf, 431 const struct usb_device_id *id) 432 { 433 struct usb_device *udev = interface_to_usbdev(intf); 434 struct usb_host_interface *hintf; 435 struct usb_endpoint_descriptor *endp; 436 struct ipheth_device *dev; 437 struct net_device *netdev; 438 int i; 439 int retval; 440 441 netdev = alloc_etherdev(sizeof(struct ipheth_device)); 442 if (!netdev) 443 return -ENOMEM; 444 445 netdev->netdev_ops = &ipheth_netdev_ops; 446 netdev->watchdog_timeo = IPHETH_TX_TIMEOUT; 447 strcpy(netdev->name, "wwan%d"); 448 449 dev = netdev_priv(netdev); 450 dev->udev = udev; 451 dev->net = netdev; 452 dev->intf = intf; 453 454 /* Set up endpoints */ 455 hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM); 456 if (hintf == NULL) { 457 retval = -ENODEV; 458 err("Unable to find alternate settings interface"); 459 goto err_endpoints; 460 } 461 462 for (i = 0; i < hintf->desc.bNumEndpoints; i++) { 463 endp = &hintf->endpoint[i].desc; 464 if (usb_endpoint_is_bulk_in(endp)) 465 dev->bulk_in = endp->bEndpointAddress; 466 else if (usb_endpoint_is_bulk_out(endp)) 467 dev->bulk_out = endp->bEndpointAddress; 468 } 469 if (!(dev->bulk_in && dev->bulk_out)) { 470 retval = -ENODEV; 471 err("Unable to find endpoints"); 472 goto err_endpoints; 473 } 474 475 dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL); 476 if (dev->ctrl_buf == NULL) { 477 retval = -ENOMEM; 478 goto err_alloc_ctrl_buf; 479 } 480 481 retval = ipheth_get_macaddr(dev); 482 if (retval) 483 goto err_get_macaddr; 484 485 INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work); 486 487 retval = ipheth_alloc_urbs(dev); 488 if (retval) { 489 err("error allocating urbs: %d", retval); 490 goto err_alloc_urbs; 491 } 492 493 usb_set_intfdata(intf, dev); 494 495 SET_NETDEV_DEV(netdev, &intf->dev); 496 SET_ETHTOOL_OPS(netdev, &ops); 497 SET_NETDEV_DEVTYPE(netdev, &ipheth_type); 498 499 retval = register_netdev(netdev); 500 if (retval) { 501 err("error registering netdev: %d", retval); 502 retval = -EIO; 503 goto err_register_netdev; 504 } 505 506 dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n"); 507 return 0; 508 509 err_register_netdev: 510 ipheth_free_urbs(dev); 511 err_alloc_urbs: 512 err_get_macaddr: 513 err_alloc_ctrl_buf: 514 kfree(dev->ctrl_buf); 515 err_endpoints: 516 free_netdev(netdev); 517 return retval; 518 } 519 520 static void ipheth_disconnect(struct usb_interface *intf) 521 { 522 struct ipheth_device *dev; 523 524 dev = usb_get_intfdata(intf); 525 if (dev != NULL) { 526 unregister_netdev(dev->net); 527 ipheth_kill_urbs(dev); 528 ipheth_free_urbs(dev); 529 kfree(dev->ctrl_buf); 530 free_netdev(dev->net); 531 } 532 usb_set_intfdata(intf, NULL); 533 dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n"); 534 } 535 536 static struct usb_driver ipheth_driver = { 537 .name = "ipheth", 538 .probe = ipheth_probe, 539 .disconnect = ipheth_disconnect, 540 .id_table = ipheth_table, 541 }; 542 543 static int __init ipheth_init(void) 544 { 545 int retval; 546 547 retval = usb_register(&ipheth_driver); 548 if (retval) { 549 err("usb_register failed: %d", retval); 550 return retval; 551 } 552 return 0; 553 } 554 555 static void __exit ipheth_exit(void) 556 { 557 usb_deregister(&ipheth_driver); 558 } 559 560 module_init(ipheth_init); 561 module_exit(ipheth_exit); 562 563 MODULE_AUTHOR("Diego Giagio <diego@giagio.com>"); 564 MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver"); 565 MODULE_LICENSE("Dual BSD/GPL"); 566