ipheth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include <linux/usb/cdc.h>
  55. #define USB_VENDOR_APPLE 0x05ac
  56. #define IPHETH_USBINTF_CLASS 255
  57. #define IPHETH_USBINTF_SUBCLASS 253
  58. #define IPHETH_USBINTF_PROTO 1
  59. #define IPHETH_IP_ALIGN 2 /* padding at front of URB */
  60. /* On iOS devices, NCM headers in RX have a fixed size regardless of DPE count:
  61. * - NTH16 (NCMH): 12 bytes, as per CDC NCM 1.0 spec
  62. * - NDP16 (NCM0): 96 bytes, of which
  63. * - NDP16 fixed header: 8 bytes
  64. * - maximum of 22 DPEs (21 datagrams + trailer), 4 bytes each
  65. */
  66. #define IPHETH_NDP16_MAX_DPE 22
  67. #define IPHETH_NDP16_HEADER_SIZE (sizeof(struct usb_cdc_ncm_ndp16) + \
  68. IPHETH_NDP16_MAX_DPE * \
  69. sizeof(struct usb_cdc_ncm_dpe16))
  70. #define IPHETH_NCM_HEADER_SIZE (sizeof(struct usb_cdc_ncm_nth16) + \
  71. IPHETH_NDP16_HEADER_SIZE)
  72. #define IPHETH_TX_BUF_SIZE ETH_FRAME_LEN
  73. #define IPHETH_RX_BUF_SIZE_LEGACY (IPHETH_IP_ALIGN + ETH_FRAME_LEN)
  74. #define IPHETH_RX_BUF_SIZE_NCM 65536
  75. #define IPHETH_TX_TIMEOUT (5 * HZ)
  76. #define IPHETH_INTFNUM 2
  77. #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)
  81. #define IPHETH_CMD_GET_MACADDR 0x00
  82. #define IPHETH_CMD_ENABLE_NCM 0x04
  83. #define IPHETH_CMD_CARRIER_CHECK 0x45
  84. #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
  85. #define IPHETH_CARRIER_ON 0x04
  86. static const struct usb_device_id ipheth_table[] = {
  87. { USB_VENDOR_AND_INTERFACE_INFO(USB_VENDOR_APPLE, IPHETH_USBINTF_CLASS,
  88. IPHETH_USBINTF_SUBCLASS,
  89. IPHETH_USBINTF_PROTO) },
  90. { }
  91. };
  92. MODULE_DEVICE_TABLE(usb, ipheth_table);
  93. struct ipheth_device {
  94. struct usb_device *udev;
  95. struct usb_interface *intf;
  96. struct net_device *net;
  97. struct urb *tx_urb;
  98. struct urb *rx_urb;
  99. unsigned char *tx_buf;
  100. unsigned char *rx_buf;
  101. unsigned char *ctrl_buf;
  102. u8 bulk_in;
  103. u8 bulk_out;
  104. struct delayed_work carrier_work;
  105. bool confirmed_pairing;
  106. int (*rcvbulk_callback)(struct urb *urb);
  107. size_t rx_buf_len;
  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_TX_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, iphone->rx_buf_len,
  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_TX_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, iphone->rx_buf_len, iphone->rx_buf,
  148. iphone->rx_urb->transfer_dma);
  149. usb_free_coherent(iphone->udev, IPHETH_TX_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 int ipheth_consume_skb(char *buf, int len, struct ipheth_device *dev)
  160. {
  161. struct sk_buff *skb;
  162. skb = dev_alloc_skb(len);
  163. if (!skb) {
  164. dev->net->stats.rx_dropped++;
  165. return -ENOMEM;
  166. }
  167. skb_put_data(skb, buf, len);
  168. skb->dev = dev->net;
  169. skb->protocol = eth_type_trans(skb, dev->net);
  170. dev->net->stats.rx_packets++;
  171. dev->net->stats.rx_bytes += len;
  172. netif_rx(skb);
  173. return 0;
  174. }
  175. static int ipheth_rcvbulk_callback_legacy(struct urb *urb)
  176. {
  177. struct ipheth_device *dev;
  178. char *buf;
  179. int len;
  180. dev = urb->context;
  181. if (urb->actual_length <= IPHETH_IP_ALIGN) {
  182. dev->net->stats.rx_length_errors++;
  183. return -EINVAL;
  184. }
  185. len = urb->actual_length - IPHETH_IP_ALIGN;
  186. buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
  187. return ipheth_consume_skb(buf, len, dev);
  188. }
  189. /* In "NCM mode", the iOS device encapsulates RX (phone->computer) traffic
  190. * in NCM Transfer Blocks (similarly to CDC NCM). However, unlike reverse
  191. * tethering (handled by the `cdc_ncm` driver), regular tethering is not
  192. * compliant with the CDC NCM spec, as the device is missing the necessary
  193. * descriptors, and TX (computer->phone) traffic is not encapsulated
  194. * at all. Thus `ipheth` implements a very limited subset of the spec with
  195. * the sole purpose of parsing RX URBs.
  196. */
  197. static int ipheth_rcvbulk_callback_ncm(struct urb *urb)
  198. {
  199. struct usb_cdc_ncm_nth16 *ncmh;
  200. struct usb_cdc_ncm_ndp16 *ncm0;
  201. struct usb_cdc_ncm_dpe16 *dpe;
  202. struct ipheth_device *dev;
  203. u16 dg_idx, dg_len;
  204. int retval = -EINVAL;
  205. char *buf;
  206. dev = urb->context;
  207. if (urb->actual_length < IPHETH_NCM_HEADER_SIZE) {
  208. dev->net->stats.rx_length_errors++;
  209. return retval;
  210. }
  211. ncmh = urb->transfer_buffer;
  212. if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) ||
  213. /* On iOS, NDP16 directly follows NTH16 */
  214. ncmh->wNdpIndex != cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16)))
  215. goto rx_error;
  216. ncm0 = urb->transfer_buffer + sizeof(struct usb_cdc_ncm_nth16);
  217. if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN))
  218. goto rx_error;
  219. dpe = ncm0->dpe16;
  220. for (int dpe_i = 0; dpe_i < IPHETH_NDP16_MAX_DPE; ++dpe_i, ++dpe) {
  221. dg_idx = le16_to_cpu(dpe->wDatagramIndex);
  222. dg_len = le16_to_cpu(dpe->wDatagramLength);
  223. /* Null DPE must be present after last datagram pointer entry
  224. * (3.3.1 USB CDC NCM spec v1.0)
  225. */
  226. if (dg_idx == 0 && dg_len == 0)
  227. return 0;
  228. if (dg_idx < IPHETH_NCM_HEADER_SIZE ||
  229. dg_idx >= urb->actual_length ||
  230. dg_len > urb->actual_length - dg_idx) {
  231. dev->net->stats.rx_length_errors++;
  232. return retval;
  233. }
  234. buf = urb->transfer_buffer + dg_idx;
  235. retval = ipheth_consume_skb(buf, dg_len, dev);
  236. if (retval != 0)
  237. return retval;
  238. }
  239. rx_error:
  240. dev->net->stats.rx_errors++;
  241. return retval;
  242. }
  243. static void ipheth_rcvbulk_callback(struct urb *urb)
  244. {
  245. struct ipheth_device *dev;
  246. int retval, status;
  247. dev = urb->context;
  248. if (dev == NULL)
  249. return;
  250. status = urb->status;
  251. switch (status) {
  252. case -ENOENT:
  253. case -ECONNRESET:
  254. case -ESHUTDOWN:
  255. case -EPROTO:
  256. return;
  257. case 0:
  258. break;
  259. default:
  260. dev_err(&dev->intf->dev, "%s: urb status: %d\n",
  261. __func__, status);
  262. return;
  263. }
  264. /* iPhone may periodically send URBs with no payload
  265. * on the "bulk in" endpoint. It is safe to ignore them.
  266. */
  267. if (urb->actual_length == 0)
  268. goto rx_submit;
  269. /* RX URBs starting with 0x00 0x01 do not encapsulate Ethernet frames,
  270. * but rather are control frames. Their purpose is not documented, and
  271. * they don't affect driver functionality, okay to drop them.
  272. * There is usually just one 4-byte control frame as the very first
  273. * URB received from the bulk IN endpoint.
  274. */
  275. if (unlikely
  276. (urb->actual_length == 4 &&
  277. ((char *)urb->transfer_buffer)[0] == 0 &&
  278. ((char *)urb->transfer_buffer)[1] == 1))
  279. goto rx_submit;
  280. retval = dev->rcvbulk_callback(urb);
  281. if (retval != 0) {
  282. dev_err(&dev->intf->dev, "%s: callback retval: %d\n",
  283. __func__, retval);
  284. }
  285. rx_submit:
  286. dev->confirmed_pairing = true;
  287. ipheth_rx_submit(dev, GFP_ATOMIC);
  288. }
  289. static void ipheth_sndbulk_callback(struct urb *urb)
  290. {
  291. struct ipheth_device *dev;
  292. int status = urb->status;
  293. dev = urb->context;
  294. if (dev == NULL)
  295. return;
  296. if (status != 0 &&
  297. status != -ENOENT &&
  298. status != -ECONNRESET &&
  299. status != -ESHUTDOWN)
  300. dev_err(&dev->intf->dev, "%s: urb status: %d\n",
  301. __func__, status);
  302. if (status == 0)
  303. netif_wake_queue(dev->net);
  304. else
  305. // on URB error, trigger immediate poll
  306. schedule_delayed_work(&dev->carrier_work, 0);
  307. }
  308. static int ipheth_carrier_set(struct ipheth_device *dev)
  309. {
  310. struct usb_device *udev;
  311. int retval;
  312. if (!dev->confirmed_pairing)
  313. return 0;
  314. udev = dev->udev;
  315. retval = usb_control_msg(udev,
  316. usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
  317. IPHETH_CMD_CARRIER_CHECK, /* request */
  318. 0xc0, /* request type */
  319. 0x00, /* value */
  320. 0x02, /* index */
  321. dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
  322. IPHETH_CTRL_TIMEOUT);
  323. if (retval <= 0) {
  324. dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
  325. __func__, retval);
  326. return retval;
  327. }
  328. if ((retval == 1 && dev->ctrl_buf[0] == IPHETH_CARRIER_ON) ||
  329. (retval >= 2 && dev->ctrl_buf[1] == IPHETH_CARRIER_ON)) {
  330. netif_carrier_on(dev->net);
  331. if (dev->tx_urb->status != -EINPROGRESS)
  332. netif_wake_queue(dev->net);
  333. } else {
  334. netif_carrier_off(dev->net);
  335. netif_stop_queue(dev->net);
  336. }
  337. return 0;
  338. }
  339. static void ipheth_carrier_check_work(struct work_struct *work)
  340. {
  341. struct ipheth_device *dev = container_of(work, struct ipheth_device,
  342. carrier_work.work);
  343. ipheth_carrier_set(dev);
  344. schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
  345. }
  346. static int ipheth_get_macaddr(struct ipheth_device *dev)
  347. {
  348. struct usb_device *udev = dev->udev;
  349. struct net_device *net = dev->net;
  350. int retval;
  351. retval = usb_control_msg(udev,
  352. usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
  353. IPHETH_CMD_GET_MACADDR, /* request */
  354. 0xc0, /* request type */
  355. 0x00, /* value */
  356. 0x02, /* index */
  357. dev->ctrl_buf,
  358. IPHETH_CTRL_BUF_SIZE,
  359. IPHETH_CTRL_TIMEOUT);
  360. if (retval < 0) {
  361. dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
  362. __func__, retval);
  363. } else if (retval < ETH_ALEN) {
  364. dev_err(&dev->intf->dev,
  365. "%s: usb_control_msg: short packet: %d bytes\n",
  366. __func__, retval);
  367. retval = -EINVAL;
  368. } else {
  369. eth_hw_addr_set(net, dev->ctrl_buf);
  370. retval = 0;
  371. }
  372. return retval;
  373. }
  374. static int ipheth_enable_ncm(struct ipheth_device *dev)
  375. {
  376. struct usb_device *udev = dev->udev;
  377. int retval;
  378. retval = usb_control_msg(udev,
  379. usb_sndctrlpipe(udev, IPHETH_CTRL_ENDP),
  380. IPHETH_CMD_ENABLE_NCM, /* request */
  381. 0x41, /* request type */
  382. 0x00, /* value */
  383. 0x02, /* index */
  384. NULL,
  385. 0,
  386. IPHETH_CTRL_TIMEOUT);
  387. dev_info(&dev->intf->dev, "%s: usb_control_msg: %d\n",
  388. __func__, retval);
  389. return retval;
  390. }
  391. static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
  392. {
  393. struct usb_device *udev = dev->udev;
  394. int retval;
  395. usb_fill_bulk_urb(dev->rx_urb, udev,
  396. usb_rcvbulkpipe(udev, dev->bulk_in),
  397. dev->rx_buf, dev->rx_buf_len,
  398. ipheth_rcvbulk_callback,
  399. dev);
  400. dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  401. retval = usb_submit_urb(dev->rx_urb, mem_flags);
  402. if (retval)
  403. dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
  404. __func__, retval);
  405. return retval;
  406. }
  407. static int ipheth_open(struct net_device *net)
  408. {
  409. struct ipheth_device *dev = netdev_priv(net);
  410. struct usb_device *udev = dev->udev;
  411. int retval = 0;
  412. usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
  413. retval = ipheth_carrier_set(dev);
  414. if (retval)
  415. return retval;
  416. retval = ipheth_rx_submit(dev, GFP_KERNEL);
  417. if (retval)
  418. return retval;
  419. schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
  420. return retval;
  421. }
  422. static int ipheth_close(struct net_device *net)
  423. {
  424. struct ipheth_device *dev = netdev_priv(net);
  425. netif_stop_queue(net);
  426. cancel_delayed_work_sync(&dev->carrier_work);
  427. return 0;
  428. }
  429. static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
  430. {
  431. struct ipheth_device *dev = netdev_priv(net);
  432. struct usb_device *udev = dev->udev;
  433. int retval;
  434. /* Paranoid */
  435. if (skb->len > IPHETH_TX_BUF_SIZE) {
  436. WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
  437. dev->net->stats.tx_dropped++;
  438. dev_kfree_skb_any(skb);
  439. return NETDEV_TX_OK;
  440. }
  441. memcpy(dev->tx_buf, skb->data, skb->len);
  442. usb_fill_bulk_urb(dev->tx_urb, udev,
  443. usb_sndbulkpipe(udev, dev->bulk_out),
  444. dev->tx_buf, skb->len,
  445. ipheth_sndbulk_callback,
  446. dev);
  447. dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  448. netif_stop_queue(net);
  449. retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
  450. if (retval) {
  451. dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
  452. __func__, retval);
  453. dev->net->stats.tx_errors++;
  454. dev_kfree_skb_any(skb);
  455. netif_wake_queue(net);
  456. } else {
  457. dev->net->stats.tx_packets++;
  458. dev->net->stats.tx_bytes += skb->len;
  459. dev_consume_skb_any(skb);
  460. }
  461. return NETDEV_TX_OK;
  462. }
  463. static void ipheth_tx_timeout(struct net_device *net, unsigned int txqueue)
  464. {
  465. struct ipheth_device *dev = netdev_priv(net);
  466. dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
  467. dev->net->stats.tx_errors++;
  468. usb_unlink_urb(dev->tx_urb);
  469. }
  470. static u32 ipheth_ethtool_op_get_link(struct net_device *net)
  471. {
  472. struct ipheth_device *dev = netdev_priv(net);
  473. return netif_carrier_ok(dev->net);
  474. }
  475. static const struct ethtool_ops ops = {
  476. .get_link = ipheth_ethtool_op_get_link
  477. };
  478. static const struct net_device_ops ipheth_netdev_ops = {
  479. .ndo_open = ipheth_open,
  480. .ndo_stop = ipheth_close,
  481. .ndo_start_xmit = ipheth_tx,
  482. .ndo_tx_timeout = ipheth_tx_timeout,
  483. };
  484. static int ipheth_probe(struct usb_interface *intf,
  485. const struct usb_device_id *id)
  486. {
  487. struct usb_device *udev = interface_to_usbdev(intf);
  488. struct usb_host_interface *hintf;
  489. struct usb_endpoint_descriptor *endp;
  490. struct ipheth_device *dev;
  491. struct net_device *netdev;
  492. int i;
  493. int retval;
  494. netdev = alloc_etherdev(sizeof(struct ipheth_device));
  495. if (!netdev)
  496. return -ENOMEM;
  497. netdev->netdev_ops = &ipheth_netdev_ops;
  498. netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
  499. strscpy(netdev->name, "eth%d", sizeof(netdev->name));
  500. dev = netdev_priv(netdev);
  501. dev->udev = udev;
  502. dev->net = netdev;
  503. dev->intf = intf;
  504. dev->confirmed_pairing = false;
  505. dev->rx_buf_len = IPHETH_RX_BUF_SIZE_LEGACY;
  506. dev->rcvbulk_callback = ipheth_rcvbulk_callback_legacy;
  507. /* Set up endpoints */
  508. hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
  509. if (hintf == NULL) {
  510. retval = -ENODEV;
  511. dev_err(&intf->dev, "Unable to find alternate settings interface\n");
  512. goto err_endpoints;
  513. }
  514. for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
  515. endp = &hintf->endpoint[i].desc;
  516. if (usb_endpoint_is_bulk_in(endp))
  517. dev->bulk_in = endp->bEndpointAddress;
  518. else if (usb_endpoint_is_bulk_out(endp))
  519. dev->bulk_out = endp->bEndpointAddress;
  520. }
  521. if (!(dev->bulk_in && dev->bulk_out)) {
  522. retval = -ENODEV;
  523. dev_err(&intf->dev, "Unable to find endpoints\n");
  524. goto err_endpoints;
  525. }
  526. dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
  527. if (dev->ctrl_buf == NULL) {
  528. retval = -ENOMEM;
  529. goto err_alloc_ctrl_buf;
  530. }
  531. retval = ipheth_get_macaddr(dev);
  532. if (retval)
  533. goto err_get_macaddr;
  534. retval = ipheth_enable_ncm(dev);
  535. if (!retval) {
  536. dev->rx_buf_len = IPHETH_RX_BUF_SIZE_NCM;
  537. dev->rcvbulk_callback = ipheth_rcvbulk_callback_ncm;
  538. }
  539. INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
  540. retval = ipheth_alloc_urbs(dev);
  541. if (retval) {
  542. dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
  543. goto err_alloc_urbs;
  544. }
  545. usb_set_intfdata(intf, dev);
  546. SET_NETDEV_DEV(netdev, &intf->dev);
  547. netdev->ethtool_ops = &ops;
  548. retval = register_netdev(netdev);
  549. if (retval) {
  550. dev_err(&intf->dev, "error registering netdev: %d\n", retval);
  551. retval = -EIO;
  552. goto err_register_netdev;
  553. }
  554. // carrier down and transmit queues stopped until packet from device
  555. netif_carrier_off(netdev);
  556. netif_tx_stop_all_queues(netdev);
  557. dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
  558. return 0;
  559. err_register_netdev:
  560. ipheth_free_urbs(dev);
  561. err_alloc_urbs:
  562. err_get_macaddr:
  563. kfree(dev->ctrl_buf);
  564. err_alloc_ctrl_buf:
  565. err_endpoints:
  566. free_netdev(netdev);
  567. return retval;
  568. }
  569. static void ipheth_disconnect(struct usb_interface *intf)
  570. {
  571. struct ipheth_device *dev;
  572. dev = usb_get_intfdata(intf);
  573. if (dev != NULL) {
  574. unregister_netdev(dev->net);
  575. ipheth_kill_urbs(dev);
  576. ipheth_free_urbs(dev);
  577. kfree(dev->ctrl_buf);
  578. free_netdev(dev->net);
  579. }
  580. usb_set_intfdata(intf, NULL);
  581. dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
  582. }
  583. static struct usb_driver ipheth_driver = {
  584. .name = "ipheth",
  585. .probe = ipheth_probe,
  586. .disconnect = ipheth_disconnect,
  587. .id_table = ipheth_table,
  588. .disable_hub_initiated_lpm = 1,
  589. };
  590. module_usb_driver(ipheth_driver);
  591. MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
  592. MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
  593. MODULE_LICENSE("Dual BSD/GPL");