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