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