46 #include <linux/kernel.h>
47 #include <linux/errno.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/netdevice.h>
53 #include <linux/ethtool.h>
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
67 #define IPHETH_USBINTF_CLASS 255
68 #define IPHETH_USBINTF_SUBCLASS 253
69 #define IPHETH_USBINTF_PROTO 1
71 #define IPHETH_BUF_SIZE 1516
72 #define IPHETH_IP_ALIGN 2
73 #define IPHETH_TX_TIMEOUT (5 * HZ)
75 #define IPHETH_INTFNUM 2
76 #define IPHETH_ALT_INTFNUM 1
78 #define IPHETH_CTRL_ENDP 0x00
79 #define IPHETH_CTRL_BUF_SIZE 0x40
80 #define IPHETH_CTRL_TIMEOUT (5 * HZ)
82 #define IPHETH_CMD_GET_MACADDR 0x00
83 #define IPHETH_CMD_CARRIER_CHECK 0x45
85 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
86 #define IPHETH_CARRIER_ON 0x04
89 { USB_DEVICE_AND_INTERFACE_INFO(
93 { USB_DEVICE_AND_INTERFACE_INFO(
97 { USB_DEVICE_AND_INTERFACE_INFO(
101 { USB_DEVICE_AND_INTERFACE_INFO(
105 { USB_DEVICE_AND_INTERFACE_INFO(
109 { USB_DEVICE_AND_INTERFACE_INFO(
113 { USB_DEVICE_AND_INTERFACE_INFO(
117 { USB_DEVICE_AND_INTERFACE_INFO(
176 tx_urb->transfer_dma);
188 iphone->
rx_urb->transfer_dma);
190 iphone->
tx_urb->transfer_dma);
201 static void ipheth_rcvbulk_callback(
struct urb *
urb)
213 status = urb->status;
228 dev->
net->stats.rx_length_errors++;
234 skb = dev_alloc_skb(len);
236 dev_err(&dev->
intf->dev,
"%s: dev_alloc_skb: -ENOMEM\n",
238 dev->
net->stats.rx_dropped++;
246 dev->
net->stats.rx_packets++;
247 dev->
net->stats.rx_bytes +=
len;
253 static void ipheth_sndbulk_callback(
struct urb *urb)
256 int status = urb->status;
270 netif_wake_queue(dev->
net);
275 struct usb_device *
udev = dev->
udev;
287 dev_err(&dev->
intf->dev,
"%s: usb_control_msg: %d\n",
305 ipheth_carrier_set(dev);
311 struct usb_device *udev = dev->
udev;
325 dev_err(&dev->
intf->dev,
"%s: usb_control_msg: %d\n",
329 "%s: usb_control_msg: short packet: %d bytes\n",
342 struct usb_device *udev = dev->
udev;
345 usb_fill_bulk_urb(dev->
rx_urb, udev,
346 usb_rcvbulkpipe(udev, dev->
bulk_in),
348 ipheth_rcvbulk_callback,
350 dev->
rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
354 dev_err(&dev->
intf->dev,
"%s: usb_submit_urb: %d\n",
359 static int ipheth_open(
struct net_device *net)
362 struct usb_device *udev = dev->
udev;
367 retval = ipheth_carrier_set(dev);
376 netif_start_queue(net);
380 static int ipheth_close(
struct net_device *net)
385 netif_stop_queue(net);
392 struct usb_device *udev = dev->
udev;
397 WARN(1,
"%s: skb too large: %d bytes\n", __func__, skb->
len);
398 dev->
net->stats.tx_dropped++;
407 usb_fill_bulk_urb(dev->
tx_urb, udev,
408 usb_sndbulkpipe(udev, dev->
bulk_out),
410 ipheth_sndbulk_callback,
412 dev->
tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
416 dev_err(&dev->
intf->dev,
"%s: usb_submit_urb: %d\n",
418 dev->
net->stats.tx_errors++;
423 dev->
net->stats.tx_packets++;
424 dev->
net->stats.tx_bytes += skb->
len;
425 netif_stop_queue(net);
431 static void ipheth_tx_timeout(
struct net_device *net)
435 dev_err(&dev->
intf->dev,
"%s: TX timeout\n", __func__);
436 dev->
net->stats.tx_errors++;
440 static u32 ipheth_ethtool_op_get_link(
struct net_device *net)
443 return netif_carrier_ok(dev->
net);
447 .get_link = ipheth_ethtool_op_get_link
451 .ndo_open = ipheth_open,
452 .ndo_stop = ipheth_close,
453 .ndo_start_xmit = ipheth_tx,
454 .ndo_tx_timeout = ipheth_tx_timeout,
460 struct usb_device *udev = interface_to_usbdev(intf);
461 struct usb_host_interface *hintf;
476 dev = netdev_priv(netdev);
485 dev_err(&intf->dev,
"Unable to find alternate settings interface\n");
489 for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
490 endp = &hintf->endpoint[
i].desc;
491 if (usb_endpoint_is_bulk_in(endp))
493 else if (usb_endpoint_is_bulk_out(endp))
498 dev_err(&intf->dev,
"Unable to find endpoints\n");
505 goto err_alloc_ctrl_buf;
508 retval = ipheth_get_macaddr(dev);
510 goto err_get_macaddr;
514 retval = ipheth_alloc_urbs(dev);
516 dev_err(&intf->dev,
"error allocating urbs: %d\n", retval);
520 usb_set_intfdata(intf, dev);
527 dev_err(&intf->dev,
"error registering netdev: %d\n", retval);
529 goto err_register_netdev;
532 dev_info(&intf->dev,
"Apple iPhone USB Ethernet device attached\n");
536 ipheth_free_urbs(dev);
550 dev = usb_get_intfdata(intf);
553 ipheth_kill_urbs(dev);
554 ipheth_free_urbs(dev);
558 usb_set_intfdata(intf,
NULL);
559 dev_info(&intf->dev,
"Apple iPhone USB Ethernet now disconnected\n");
562 static struct usb_driver ipheth_driver = {
564 .probe = ipheth_probe,
565 .disconnect = ipheth_disconnect,
566 .id_table = ipheth_table,
567 .disable_hub_initiated_lpm = 1,