xref: /linux/drivers/net/usb/ipheth.c (revision 655b46d7a39ac6f049698b27c1568c0f7ff85d1e)
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 
46 #include <linux/kernel.h>
47 #include <linux/errno.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 #include <linux/usb/cdc.h>
56 
57 #define USB_VENDOR_APPLE        0x05ac
58 
59 #define IPHETH_USBINTF_CLASS    255
60 #define IPHETH_USBINTF_SUBCLASS 253
61 #define IPHETH_USBINTF_PROTO    1
62 
63 #define IPHETH_IP_ALIGN		2	/* padding at front of URB */
64 #define IPHETH_NCM_HEADER_SIZE  (12 + 96) /* NCMH + NCM0 */
65 #define IPHETH_TX_BUF_SIZE      ETH_FRAME_LEN
66 #define IPHETH_RX_BUF_SIZE_LEGACY (IPHETH_IP_ALIGN + ETH_FRAME_LEN)
67 #define IPHETH_RX_BUF_SIZE_NCM	65536
68 
69 #define IPHETH_TX_TIMEOUT       (5 * HZ)
70 
71 #define IPHETH_INTFNUM          2
72 #define IPHETH_ALT_INTFNUM      1
73 
74 #define IPHETH_CTRL_ENDP        0x00
75 #define IPHETH_CTRL_BUF_SIZE    0x40
76 #define IPHETH_CTRL_TIMEOUT     (5 * HZ)
77 
78 #define IPHETH_CMD_GET_MACADDR   0x00
79 #define IPHETH_CMD_ENABLE_NCM    0x04
80 #define IPHETH_CMD_CARRIER_CHECK 0x45
81 
82 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
83 #define IPHETH_CARRIER_ON       0x04
84 
85 static const struct usb_device_id ipheth_table[] = {
86 	{ USB_VENDOR_AND_INTERFACE_INFO(USB_VENDOR_APPLE, IPHETH_USBINTF_CLASS,
87 					IPHETH_USBINTF_SUBCLASS,
88 					IPHETH_USBINTF_PROTO) },
89 	{ }
90 };
91 MODULE_DEVICE_TABLE(usb, ipheth_table);
92 
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 
110 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
111 
112 static int ipheth_alloc_urbs(struct ipheth_device *iphone)
113 {
114 	struct urb *tx_urb = NULL;
115 	struct urb *rx_urb = NULL;
116 	u8 *tx_buf = NULL;
117 	u8 *rx_buf = NULL;
118 
119 	tx_urb = usb_alloc_urb(0, GFP_KERNEL);
120 	if (tx_urb == NULL)
121 		goto error_nomem;
122 
123 	rx_urb = usb_alloc_urb(0, GFP_KERNEL);
124 	if (rx_urb == NULL)
125 		goto free_tx_urb;
126 
127 	tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_TX_BUF_SIZE,
128 				    GFP_KERNEL, &tx_urb->transfer_dma);
129 	if (tx_buf == NULL)
130 		goto free_rx_urb;
131 
132 	rx_buf = usb_alloc_coherent(iphone->udev, iphone->rx_buf_len,
133 				    GFP_KERNEL, &rx_urb->transfer_dma);
134 	if (rx_buf == NULL)
135 		goto free_tx_buf;
136 
137 
138 	iphone->tx_urb = tx_urb;
139 	iphone->rx_urb = rx_urb;
140 	iphone->tx_buf = tx_buf;
141 	iphone->rx_buf = rx_buf;
142 	return 0;
143 
144 free_tx_buf:
145 	usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, tx_buf,
146 			  tx_urb->transfer_dma);
147 free_rx_urb:
148 	usb_free_urb(rx_urb);
149 free_tx_urb:
150 	usb_free_urb(tx_urb);
151 error_nomem:
152 	return -ENOMEM;
153 }
154 
155 static void ipheth_free_urbs(struct ipheth_device *iphone)
156 {
157 	usb_free_coherent(iphone->udev, iphone->rx_buf_len, iphone->rx_buf,
158 			  iphone->rx_urb->transfer_dma);
159 	usb_free_coherent(iphone->udev, IPHETH_TX_BUF_SIZE, iphone->tx_buf,
160 			  iphone->tx_urb->transfer_dma);
161 	usb_free_urb(iphone->rx_urb);
162 	usb_free_urb(iphone->tx_urb);
163 }
164 
165 static void ipheth_kill_urbs(struct ipheth_device *dev)
166 {
167 	usb_kill_urb(dev->tx_urb);
168 	usb_kill_urb(dev->rx_urb);
169 }
170 
171 static int ipheth_consume_skb(char *buf, int len, struct ipheth_device *dev)
172 {
173 	struct sk_buff *skb;
174 
175 	skb = dev_alloc_skb(len);
176 	if (!skb) {
177 		dev->net->stats.rx_dropped++;
178 		return -ENOMEM;
179 	}
180 
181 	skb_put_data(skb, buf, len);
182 	skb->dev = dev->net;
183 	skb->protocol = eth_type_trans(skb, dev->net);
184 
185 	dev->net->stats.rx_packets++;
186 	dev->net->stats.rx_bytes += len;
187 	netif_rx(skb);
188 
189 	return 0;
190 }
191 
192 static int ipheth_rcvbulk_callback_legacy(struct urb *urb)
193 {
194 	struct ipheth_device *dev;
195 	char *buf;
196 	int len;
197 
198 	dev = urb->context;
199 
200 	if (urb->actual_length <= IPHETH_IP_ALIGN) {
201 		dev->net->stats.rx_length_errors++;
202 		return -EINVAL;
203 	}
204 	len = urb->actual_length - IPHETH_IP_ALIGN;
205 	buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
206 
207 	return ipheth_consume_skb(buf, len, dev);
208 }
209 
210 static int ipheth_rcvbulk_callback_ncm(struct urb *urb)
211 {
212 	struct usb_cdc_ncm_nth16 *ncmh;
213 	struct usb_cdc_ncm_ndp16 *ncm0;
214 	struct usb_cdc_ncm_dpe16 *dpe;
215 	struct ipheth_device *dev;
216 	int retval = -EINVAL;
217 	char *buf;
218 	int len;
219 
220 	dev = urb->context;
221 
222 	if (urb->actual_length < IPHETH_NCM_HEADER_SIZE) {
223 		dev->net->stats.rx_length_errors++;
224 		return retval;
225 	}
226 
227 	ncmh = urb->transfer_buffer;
228 	if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) ||
229 	    le16_to_cpu(ncmh->wNdpIndex) >= urb->actual_length) {
230 		dev->net->stats.rx_errors++;
231 		return retval;
232 	}
233 
234 	ncm0 = urb->transfer_buffer + le16_to_cpu(ncmh->wNdpIndex);
235 	if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN) ||
236 	    le16_to_cpu(ncmh->wHeaderLength) + le16_to_cpu(ncm0->wLength) >=
237 	    urb->actual_length) {
238 		dev->net->stats.rx_errors++;
239 		return retval;
240 	}
241 
242 	dpe = ncm0->dpe16;
243 	while (le16_to_cpu(dpe->wDatagramIndex) != 0 &&
244 	       le16_to_cpu(dpe->wDatagramLength) != 0) {
245 		if (le16_to_cpu(dpe->wDatagramIndex) >= urb->actual_length ||
246 		    le16_to_cpu(dpe->wDatagramIndex) +
247 		    le16_to_cpu(dpe->wDatagramLength) > urb->actual_length) {
248 			dev->net->stats.rx_length_errors++;
249 			return retval;
250 		}
251 
252 		buf = urb->transfer_buffer + le16_to_cpu(dpe->wDatagramIndex);
253 		len = le16_to_cpu(dpe->wDatagramLength);
254 
255 		retval = ipheth_consume_skb(buf, len, dev);
256 		if (retval != 0)
257 			return retval;
258 
259 		dpe++;
260 	}
261 
262 	return 0;
263 }
264 
265 static void ipheth_rcvbulk_callback(struct urb *urb)
266 {
267 	struct ipheth_device *dev;
268 	int retval, status;
269 
270 	dev = urb->context;
271 	if (dev == NULL)
272 		return;
273 
274 	status = urb->status;
275 	switch (status) {
276 	case -ENOENT:
277 	case -ECONNRESET:
278 	case -ESHUTDOWN:
279 	case -EPROTO:
280 		return;
281 	case 0:
282 		break;
283 	default:
284 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
285 			__func__, status);
286 		return;
287 	}
288 
289 	/* RX URBs starting with 0x00 0x01 do not encapsulate Ethernet frames,
290 	 * but rather are control frames. Their purpose is not documented, and
291 	 * they don't affect driver functionality, okay to drop them.
292 	 * There is usually just one 4-byte control frame as the very first
293 	 * URB received from the bulk IN endpoint.
294 	 */
295 	if (unlikely
296 		(urb->actual_length == 4 &&
297 		 ((char *)urb->transfer_buffer)[0] == 0 &&
298 		 ((char *)urb->transfer_buffer)[1] == 1))
299 		goto rx_submit;
300 
301 	retval = dev->rcvbulk_callback(urb);
302 	if (retval != 0) {
303 		dev_err(&dev->intf->dev, "%s: callback retval: %d\n",
304 			__func__, retval);
305 		return;
306 	}
307 
308 rx_submit:
309 	dev->confirmed_pairing = true;
310 	ipheth_rx_submit(dev, GFP_ATOMIC);
311 }
312 
313 static void ipheth_sndbulk_callback(struct urb *urb)
314 {
315 	struct ipheth_device *dev;
316 	int status = urb->status;
317 
318 	dev = urb->context;
319 	if (dev == NULL)
320 		return;
321 
322 	if (status != 0 &&
323 	    status != -ENOENT &&
324 	    status != -ECONNRESET &&
325 	    status != -ESHUTDOWN)
326 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
327 		__func__, status);
328 
329 	if (status == 0)
330 		netif_wake_queue(dev->net);
331 	else
332 		// on URB error, trigger immediate poll
333 		schedule_delayed_work(&dev->carrier_work, 0);
334 }
335 
336 static int ipheth_carrier_set(struct ipheth_device *dev)
337 {
338 	struct usb_device *udev;
339 	int retval;
340 
341 	if (!dev->confirmed_pairing)
342 		return 0;
343 
344 	udev = dev->udev;
345 	retval = usb_control_msg(udev,
346 			usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
347 			IPHETH_CMD_CARRIER_CHECK, /* request */
348 			0xc0, /* request type */
349 			0x00, /* value */
350 			0x02, /* index */
351 			dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
352 			IPHETH_CTRL_TIMEOUT);
353 	if (retval < 0) {
354 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
355 			__func__, retval);
356 		return retval;
357 	}
358 
359 	if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) {
360 		netif_carrier_on(dev->net);
361 		if (dev->tx_urb->status != -EINPROGRESS)
362 			netif_wake_queue(dev->net);
363 	} else {
364 		netif_carrier_off(dev->net);
365 		netif_stop_queue(dev->net);
366 	}
367 	return 0;
368 }
369 
370 static void ipheth_carrier_check_work(struct work_struct *work)
371 {
372 	struct ipheth_device *dev = container_of(work, struct ipheth_device,
373 						 carrier_work.work);
374 
375 	ipheth_carrier_set(dev);
376 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
377 }
378 
379 static int ipheth_get_macaddr(struct ipheth_device *dev)
380 {
381 	struct usb_device *udev = dev->udev;
382 	struct net_device *net = dev->net;
383 	int retval;
384 
385 	retval = usb_control_msg(udev,
386 				 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
387 				 IPHETH_CMD_GET_MACADDR, /* request */
388 				 0xc0, /* request type */
389 				 0x00, /* value */
390 				 0x02, /* index */
391 				 dev->ctrl_buf,
392 				 IPHETH_CTRL_BUF_SIZE,
393 				 IPHETH_CTRL_TIMEOUT);
394 	if (retval < 0) {
395 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
396 			__func__, retval);
397 	} else if (retval < ETH_ALEN) {
398 		dev_err(&dev->intf->dev,
399 			"%s: usb_control_msg: short packet: %d bytes\n",
400 			__func__, retval);
401 		retval = -EINVAL;
402 	} else {
403 		eth_hw_addr_set(net, dev->ctrl_buf);
404 		retval = 0;
405 	}
406 
407 	return retval;
408 }
409 
410 static int ipheth_enable_ncm(struct ipheth_device *dev)
411 {
412 	struct usb_device *udev = dev->udev;
413 	int retval;
414 
415 	retval = usb_control_msg(udev,
416 				 usb_sndctrlpipe(udev, IPHETH_CTRL_ENDP),
417 				 IPHETH_CMD_ENABLE_NCM, /* request */
418 				 0x41, /* request type */
419 				 0x00, /* value */
420 				 0x02, /* index */
421 				 NULL,
422 				 0,
423 				 IPHETH_CTRL_TIMEOUT);
424 
425 	dev_info(&dev->intf->dev, "%s: usb_control_msg: %d\n",
426 		 __func__, retval);
427 
428 	return retval;
429 }
430 
431 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
432 {
433 	struct usb_device *udev = dev->udev;
434 	int retval;
435 
436 	usb_fill_bulk_urb(dev->rx_urb, udev,
437 			  usb_rcvbulkpipe(udev, dev->bulk_in),
438 			  dev->rx_buf, dev->rx_buf_len,
439 			  ipheth_rcvbulk_callback,
440 			  dev);
441 	dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
442 
443 	retval = usb_submit_urb(dev->rx_urb, mem_flags);
444 	if (retval)
445 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
446 			__func__, retval);
447 	return retval;
448 }
449 
450 static int ipheth_open(struct net_device *net)
451 {
452 	struct ipheth_device *dev = netdev_priv(net);
453 	struct usb_device *udev = dev->udev;
454 	int retval = 0;
455 
456 	usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
457 
458 	retval = ipheth_carrier_set(dev);
459 	if (retval)
460 		return retval;
461 
462 	retval = ipheth_rx_submit(dev, GFP_KERNEL);
463 	if (retval)
464 		return retval;
465 
466 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
467 	return retval;
468 }
469 
470 static int ipheth_close(struct net_device *net)
471 {
472 	struct ipheth_device *dev = netdev_priv(net);
473 
474 	netif_stop_queue(net);
475 	cancel_delayed_work_sync(&dev->carrier_work);
476 	return 0;
477 }
478 
479 static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
480 {
481 	struct ipheth_device *dev = netdev_priv(net);
482 	struct usb_device *udev = dev->udev;
483 	int retval;
484 
485 	/* Paranoid */
486 	if (skb->len > IPHETH_TX_BUF_SIZE) {
487 		WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
488 		dev->net->stats.tx_dropped++;
489 		dev_kfree_skb_any(skb);
490 		return NETDEV_TX_OK;
491 	}
492 
493 	memcpy(dev->tx_buf, skb->data, skb->len);
494 
495 	usb_fill_bulk_urb(dev->tx_urb, udev,
496 			  usb_sndbulkpipe(udev, dev->bulk_out),
497 			  dev->tx_buf, skb->len,
498 			  ipheth_sndbulk_callback,
499 			  dev);
500 	dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
501 
502 	netif_stop_queue(net);
503 	retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
504 	if (retval) {
505 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
506 			__func__, retval);
507 		dev->net->stats.tx_errors++;
508 		dev_kfree_skb_any(skb);
509 		netif_wake_queue(net);
510 	} else {
511 		dev->net->stats.tx_packets++;
512 		dev->net->stats.tx_bytes += skb->len;
513 		dev_consume_skb_any(skb);
514 	}
515 
516 	return NETDEV_TX_OK;
517 }
518 
519 static void ipheth_tx_timeout(struct net_device *net, unsigned int txqueue)
520 {
521 	struct ipheth_device *dev = netdev_priv(net);
522 
523 	dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
524 	dev->net->stats.tx_errors++;
525 	usb_unlink_urb(dev->tx_urb);
526 }
527 
528 static u32 ipheth_ethtool_op_get_link(struct net_device *net)
529 {
530 	struct ipheth_device *dev = netdev_priv(net);
531 	return netif_carrier_ok(dev->net);
532 }
533 
534 static const struct ethtool_ops ops = {
535 	.get_link = ipheth_ethtool_op_get_link
536 };
537 
538 static const struct net_device_ops ipheth_netdev_ops = {
539 	.ndo_open = ipheth_open,
540 	.ndo_stop = ipheth_close,
541 	.ndo_start_xmit = ipheth_tx,
542 	.ndo_tx_timeout = ipheth_tx_timeout,
543 };
544 
545 static int ipheth_probe(struct usb_interface *intf,
546 			const struct usb_device_id *id)
547 {
548 	struct usb_device *udev = interface_to_usbdev(intf);
549 	struct usb_host_interface *hintf;
550 	struct usb_endpoint_descriptor *endp;
551 	struct ipheth_device *dev;
552 	struct net_device *netdev;
553 	int i;
554 	int retval;
555 
556 	netdev = alloc_etherdev(sizeof(struct ipheth_device));
557 	if (!netdev)
558 		return -ENOMEM;
559 
560 	netdev->netdev_ops = &ipheth_netdev_ops;
561 	netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
562 	strscpy(netdev->name, "eth%d", sizeof(netdev->name));
563 
564 	dev = netdev_priv(netdev);
565 	dev->udev = udev;
566 	dev->net = netdev;
567 	dev->intf = intf;
568 	dev->confirmed_pairing = false;
569 	dev->rx_buf_len = IPHETH_RX_BUF_SIZE_LEGACY;
570 	dev->rcvbulk_callback = ipheth_rcvbulk_callback_legacy;
571 	/* Set up endpoints */
572 	hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
573 	if (hintf == NULL) {
574 		retval = -ENODEV;
575 		dev_err(&intf->dev, "Unable to find alternate settings interface\n");
576 		goto err_endpoints;
577 	}
578 
579 	for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
580 		endp = &hintf->endpoint[i].desc;
581 		if (usb_endpoint_is_bulk_in(endp))
582 			dev->bulk_in = endp->bEndpointAddress;
583 		else if (usb_endpoint_is_bulk_out(endp))
584 			dev->bulk_out = endp->bEndpointAddress;
585 	}
586 	if (!(dev->bulk_in && dev->bulk_out)) {
587 		retval = -ENODEV;
588 		dev_err(&intf->dev, "Unable to find endpoints\n");
589 		goto err_endpoints;
590 	}
591 
592 	dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
593 	if (dev->ctrl_buf == NULL) {
594 		retval = -ENOMEM;
595 		goto err_alloc_ctrl_buf;
596 	}
597 
598 	retval = ipheth_get_macaddr(dev);
599 	if (retval)
600 		goto err_get_macaddr;
601 
602 	retval = ipheth_enable_ncm(dev);
603 	if (!retval) {
604 		dev->rx_buf_len = IPHETH_RX_BUF_SIZE_NCM;
605 		dev->rcvbulk_callback = ipheth_rcvbulk_callback_ncm;
606 	}
607 
608 	INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
609 
610 	retval = ipheth_alloc_urbs(dev);
611 	if (retval) {
612 		dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
613 		goto err_alloc_urbs;
614 	}
615 
616 	usb_set_intfdata(intf, dev);
617 
618 	SET_NETDEV_DEV(netdev, &intf->dev);
619 	netdev->ethtool_ops = &ops;
620 
621 	retval = register_netdev(netdev);
622 	if (retval) {
623 		dev_err(&intf->dev, "error registering netdev: %d\n", retval);
624 		retval = -EIO;
625 		goto err_register_netdev;
626 	}
627 	// carrier down and transmit queues stopped until packet from device
628 	netif_carrier_off(netdev);
629 	netif_tx_stop_all_queues(netdev);
630 	dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
631 	return 0;
632 
633 err_register_netdev:
634 	ipheth_free_urbs(dev);
635 err_alloc_urbs:
636 err_get_macaddr:
637 	kfree(dev->ctrl_buf);
638 err_alloc_ctrl_buf:
639 err_endpoints:
640 	free_netdev(netdev);
641 	return retval;
642 }
643 
644 static void ipheth_disconnect(struct usb_interface *intf)
645 {
646 	struct ipheth_device *dev;
647 
648 	dev = usb_get_intfdata(intf);
649 	if (dev != NULL) {
650 		unregister_netdev(dev->net);
651 		ipheth_kill_urbs(dev);
652 		ipheth_free_urbs(dev);
653 		kfree(dev->ctrl_buf);
654 		free_netdev(dev->net);
655 	}
656 	usb_set_intfdata(intf, NULL);
657 	dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
658 }
659 
660 static struct usb_driver ipheth_driver = {
661 	.name =		"ipheth",
662 	.probe =	ipheth_probe,
663 	.disconnect =	ipheth_disconnect,
664 	.id_table =	ipheth_table,
665 	.disable_hub_initiated_lpm = 1,
666 };
667 
668 module_usb_driver(ipheth_driver);
669 
670 MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
671 MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
672 MODULE_LICENSE("Dual BSD/GPL");
673