ipheth.c 14 KB

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