ipheth.c 14 KB

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