ipheth.c 14 KB

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