xref: /openbmc/linux/drivers/net/usb/ipheth.c (revision 45611c61dd503454b2edae00aabe1e429ec49ebe)
1a19259c3SDiego Giagio /*
2a19259c3SDiego Giagio  * ipheth.c - Apple iPhone USB Ethernet driver
3a19259c3SDiego Giagio  *
4a19259c3SDiego Giagio  * Copyright (c) 2009 Diego Giagio <diego@giagio.com>
5a19259c3SDiego Giagio  * All rights reserved.
6a19259c3SDiego Giagio  *
7a19259c3SDiego Giagio  * Redistribution and use in source and binary forms, with or without
8a19259c3SDiego Giagio  * modification, are permitted provided that the following conditions
9a19259c3SDiego Giagio  * are met:
10a19259c3SDiego Giagio  * 1. Redistributions of source code must retain the above copyright
11a19259c3SDiego Giagio  *    notice, this list of conditions and the following disclaimer.
12a19259c3SDiego Giagio  * 2. Redistributions in binary form must reproduce the above copyright
13a19259c3SDiego Giagio  *    notice, this list of conditions and the following disclaimer in the
14a19259c3SDiego Giagio  *    documentation and/or other materials provided with the distribution.
15a19259c3SDiego Giagio  * 3. Neither the name of GIAGIO.COM nor the names of its contributors
16a19259c3SDiego Giagio  *    may be used to endorse or promote products derived from this software
17a19259c3SDiego Giagio  *    without specific prior written permission.
18a19259c3SDiego Giagio  *
19a19259c3SDiego Giagio  * Alternatively, provided that this notice is retained in full, this
20a19259c3SDiego Giagio  * software may be distributed under the terms of the GNU General
21a19259c3SDiego Giagio  * Public License ("GPL") version 2, in which case the provisions of the
22a19259c3SDiego Giagio  * GPL apply INSTEAD OF those given above.
23a19259c3SDiego Giagio  *
24a19259c3SDiego Giagio  * The provided data structures and external interfaces from this code
25a19259c3SDiego Giagio  * are not restricted to be used by modules with a GPL compatible license.
26a19259c3SDiego Giagio  *
27a19259c3SDiego Giagio  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28a19259c3SDiego Giagio  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29a19259c3SDiego Giagio  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30a19259c3SDiego Giagio  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31a19259c3SDiego Giagio  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32a19259c3SDiego Giagio  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33a19259c3SDiego Giagio  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34a19259c3SDiego Giagio  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35a19259c3SDiego Giagio  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36a19259c3SDiego Giagio  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37a19259c3SDiego Giagio  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38a19259c3SDiego Giagio  * DAMAGE.
39a19259c3SDiego Giagio  *
40a19259c3SDiego Giagio  *
41a19259c3SDiego Giagio  * Attention: iPhone device must be paired, otherwise it won't respond to our
42a19259c3SDiego Giagio  * driver. For more info: http://giagio.com/wiki/moin.cgi/iPhoneEthernetDriver
43a19259c3SDiego Giagio  *
44a19259c3SDiego Giagio  */
45a19259c3SDiego Giagio 
46a19259c3SDiego Giagio #include <linux/kernel.h>
47a19259c3SDiego Giagio #include <linux/errno.h>
48a19259c3SDiego Giagio #include <linux/slab.h>
49a19259c3SDiego Giagio #include <linux/module.h>
50a19259c3SDiego Giagio #include <linux/netdevice.h>
51a19259c3SDiego Giagio #include <linux/etherdevice.h>
52a19259c3SDiego Giagio #include <linux/ethtool.h>
53a19259c3SDiego Giagio #include <linux/usb.h>
54a19259c3SDiego Giagio #include <linux/workqueue.h>
55a19259c3SDiego Giagio 
56a19259c3SDiego Giagio #define USB_VENDOR_APPLE        0x05ac
57a19259c3SDiego Giagio #define USB_PRODUCT_IPHONE      0x1290
58a19259c3SDiego Giagio #define USB_PRODUCT_IPHONE_3G   0x1292
59a19259c3SDiego Giagio #define USB_PRODUCT_IPHONE_3GS  0x1294
60f95d76abSJens Axboe #define USB_PRODUCT_IPHONE_4	0x1297
616de0298eSDavide Gerhard #define USB_PRODUCT_IPAD 0x129a
6222fd2a52SKristian Evensen #define USB_PRODUCT_IPAD_2	0x12a2
6322fd2a52SKristian Evensen #define USB_PRODUCT_IPAD_3	0x12a6
64a7599398SAaron Marburg #define USB_PRODUCT_IPAD_MINI    0x12ab
6502009afcSKavan Smith #define USB_PRODUCT_IPHONE_4_VZW 0x129c
6672ba009bSTim Gardner #define USB_PRODUCT_IPHONE_4S	0x12a0
67af1b85e4SJay Purohit #define USB_PRODUCT_IPHONE_5	0x12a8
68a19259c3SDiego Giagio 
69a19259c3SDiego Giagio #define IPHETH_USBINTF_CLASS    255
70a19259c3SDiego Giagio #define IPHETH_USBINTF_SUBCLASS 253
71a19259c3SDiego Giagio #define IPHETH_USBINTF_PROTO    1
72a19259c3SDiego Giagio 
73a19259c3SDiego Giagio #define IPHETH_BUF_SIZE         1516
749c412942SBen Hutchings #define IPHETH_IP_ALIGN		2	/* padding at front of URB */
75a19259c3SDiego Giagio #define IPHETH_TX_TIMEOUT       (5 * HZ)
76a19259c3SDiego Giagio 
77a19259c3SDiego Giagio #define IPHETH_INTFNUM          2
78a19259c3SDiego Giagio #define IPHETH_ALT_INTFNUM      1
79a19259c3SDiego Giagio 
80a19259c3SDiego Giagio #define IPHETH_CTRL_ENDP        0x00
81a19259c3SDiego Giagio #define IPHETH_CTRL_BUF_SIZE    0x40
82a19259c3SDiego Giagio #define IPHETH_CTRL_TIMEOUT     (5 * HZ)
83a19259c3SDiego Giagio 
84a19259c3SDiego Giagio #define IPHETH_CMD_GET_MACADDR   0x00
85a19259c3SDiego Giagio #define IPHETH_CMD_CARRIER_CHECK 0x45
86a19259c3SDiego Giagio 
87a19259c3SDiego Giagio #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
88a19259c3SDiego Giagio #define IPHETH_CARRIER_ON       0x04
89a19259c3SDiego Giagio 
909befbe13SArvind Yadav static const struct usb_device_id ipheth_table[] = {
91a19259c3SDiego Giagio 	{ USB_DEVICE_AND_INTERFACE_INFO(
92a19259c3SDiego Giagio 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE,
93a19259c3SDiego Giagio 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
94a19259c3SDiego Giagio 		IPHETH_USBINTF_PROTO) },
95a19259c3SDiego Giagio 	{ USB_DEVICE_AND_INTERFACE_INFO(
96a19259c3SDiego Giagio 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3G,
97a19259c3SDiego Giagio 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
98a19259c3SDiego Giagio 		IPHETH_USBINTF_PROTO) },
99a19259c3SDiego Giagio 	{ USB_DEVICE_AND_INTERFACE_INFO(
100a19259c3SDiego Giagio 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_3GS,
101a19259c3SDiego Giagio 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
102a19259c3SDiego Giagio 		IPHETH_USBINTF_PROTO) },
103f95d76abSJens Axboe 	{ USB_DEVICE_AND_INTERFACE_INFO(
104f95d76abSJens Axboe 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4,
105f95d76abSJens Axboe 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
106f95d76abSJens Axboe 		IPHETH_USBINTF_PROTO) },
10702009afcSKavan Smith 	{ USB_DEVICE_AND_INTERFACE_INFO(
1086de0298eSDavide Gerhard 		USB_VENDOR_APPLE, USB_PRODUCT_IPAD,
1096de0298eSDavide Gerhard 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
1106de0298eSDavide Gerhard 		IPHETH_USBINTF_PROTO) },
1116de0298eSDavide Gerhard 	{ USB_DEVICE_AND_INTERFACE_INFO(
11222fd2a52SKristian Evensen 		USB_VENDOR_APPLE, USB_PRODUCT_IPAD_2,
11322fd2a52SKristian Evensen 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
11422fd2a52SKristian Evensen 		IPHETH_USBINTF_PROTO) },
11522fd2a52SKristian Evensen 	{ USB_DEVICE_AND_INTERFACE_INFO(
11622fd2a52SKristian Evensen 		USB_VENDOR_APPLE, USB_PRODUCT_IPAD_3,
11722fd2a52SKristian Evensen 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
11822fd2a52SKristian Evensen 		IPHETH_USBINTF_PROTO) },
11922fd2a52SKristian Evensen 	{ USB_DEVICE_AND_INTERFACE_INFO(
120a7599398SAaron Marburg 		USB_VENDOR_APPLE, USB_PRODUCT_IPAD_MINI,
121a7599398SAaron Marburg 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
122a7599398SAaron Marburg 		IPHETH_USBINTF_PROTO) },
123a7599398SAaron Marburg 	{ USB_DEVICE_AND_INTERFACE_INFO(
12402009afcSKavan Smith 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4_VZW,
12502009afcSKavan Smith 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
12602009afcSKavan Smith 		IPHETH_USBINTF_PROTO) },
12772ba009bSTim Gardner 	{ USB_DEVICE_AND_INTERFACE_INFO(
12872ba009bSTim Gardner 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_4S,
12972ba009bSTim Gardner 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
13072ba009bSTim Gardner 		IPHETH_USBINTF_PROTO) },
131af1b85e4SJay Purohit 	{ USB_DEVICE_AND_INTERFACE_INFO(
132af1b85e4SJay Purohit 		USB_VENDOR_APPLE, USB_PRODUCT_IPHONE_5,
133af1b85e4SJay Purohit 		IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
134af1b85e4SJay Purohit 		IPHETH_USBINTF_PROTO) },
135a19259c3SDiego Giagio 	{ }
136a19259c3SDiego Giagio };
137a19259c3SDiego Giagio MODULE_DEVICE_TABLE(usb, ipheth_table);
138a19259c3SDiego Giagio 
139a19259c3SDiego Giagio struct ipheth_device {
140a19259c3SDiego Giagio 	struct usb_device *udev;
141a19259c3SDiego Giagio 	struct usb_interface *intf;
142a19259c3SDiego Giagio 	struct net_device *net;
143a19259c3SDiego Giagio 	struct urb *tx_urb;
144a19259c3SDiego Giagio 	struct urb *rx_urb;
145a19259c3SDiego Giagio 	unsigned char *tx_buf;
146a19259c3SDiego Giagio 	unsigned char *rx_buf;
147a19259c3SDiego Giagio 	unsigned char *ctrl_buf;
148a19259c3SDiego Giagio 	u8 bulk_in;
149a19259c3SDiego Giagio 	u8 bulk_out;
150a19259c3SDiego Giagio 	struct delayed_work carrier_work;
151bb1b40c7SAlexander Kappner 	bool confirmed_pairing;
152a19259c3SDiego Giagio };
153a19259c3SDiego Giagio 
154a19259c3SDiego Giagio static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
155a19259c3SDiego Giagio 
156a19259c3SDiego Giagio static int ipheth_alloc_urbs(struct ipheth_device *iphone)
157a19259c3SDiego Giagio {
158a19259c3SDiego Giagio 	struct urb *tx_urb = NULL;
159a19259c3SDiego Giagio 	struct urb *rx_urb = NULL;
160a19259c3SDiego Giagio 	u8 *tx_buf = NULL;
161a19259c3SDiego Giagio 	u8 *rx_buf = NULL;
162a19259c3SDiego Giagio 
163a19259c3SDiego Giagio 	tx_urb = usb_alloc_urb(0, GFP_KERNEL);
164a19259c3SDiego Giagio 	if (tx_urb == NULL)
165d87ff58fSDan Carpenter 		goto error_nomem;
166a19259c3SDiego Giagio 
167a19259c3SDiego Giagio 	rx_urb = usb_alloc_urb(0, GFP_KERNEL);
168a19259c3SDiego Giagio 	if (rx_urb == NULL)
169d87ff58fSDan Carpenter 		goto free_tx_urb;
170a19259c3SDiego Giagio 
171997ea58eSDaniel Mack 	tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
172997ea58eSDaniel Mack 				    GFP_KERNEL, &tx_urb->transfer_dma);
173a19259c3SDiego Giagio 	if (tx_buf == NULL)
174d87ff58fSDan Carpenter 		goto free_rx_urb;
175a19259c3SDiego Giagio 
176997ea58eSDaniel Mack 	rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
177997ea58eSDaniel Mack 				    GFP_KERNEL, &rx_urb->transfer_dma);
178a19259c3SDiego Giagio 	if (rx_buf == NULL)
179d87ff58fSDan Carpenter 		goto free_tx_buf;
180a19259c3SDiego Giagio 
181a19259c3SDiego Giagio 
182a19259c3SDiego Giagio 	iphone->tx_urb = tx_urb;
183a19259c3SDiego Giagio 	iphone->rx_urb = rx_urb;
184a19259c3SDiego Giagio 	iphone->tx_buf = tx_buf;
185a19259c3SDiego Giagio 	iphone->rx_buf = rx_buf;
186a19259c3SDiego Giagio 	return 0;
187a19259c3SDiego Giagio 
188d87ff58fSDan Carpenter free_tx_buf:
189997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf,
190a19259c3SDiego Giagio 			  tx_urb->transfer_dma);
191d87ff58fSDan Carpenter free_rx_urb:
192a19259c3SDiego Giagio 	usb_free_urb(rx_urb);
193d87ff58fSDan Carpenter free_tx_urb:
194a19259c3SDiego Giagio 	usb_free_urb(tx_urb);
195d87ff58fSDan Carpenter error_nomem:
196a19259c3SDiego Giagio 	return -ENOMEM;
197a19259c3SDiego Giagio }
198a19259c3SDiego Giagio 
199a19259c3SDiego Giagio static void ipheth_free_urbs(struct ipheth_device *iphone)
200a19259c3SDiego Giagio {
201997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->rx_buf,
202a19259c3SDiego Giagio 			  iphone->rx_urb->transfer_dma);
203997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf,
204a19259c3SDiego Giagio 			  iphone->tx_urb->transfer_dma);
205a19259c3SDiego Giagio 	usb_free_urb(iphone->rx_urb);
206a19259c3SDiego Giagio 	usb_free_urb(iphone->tx_urb);
207a19259c3SDiego Giagio }
208a19259c3SDiego Giagio 
209a19259c3SDiego Giagio static void ipheth_kill_urbs(struct ipheth_device *dev)
210a19259c3SDiego Giagio {
211a19259c3SDiego Giagio 	usb_kill_urb(dev->tx_urb);
212a19259c3SDiego Giagio 	usb_kill_urb(dev->rx_urb);
213a19259c3SDiego Giagio }
214a19259c3SDiego Giagio 
215a19259c3SDiego Giagio static void ipheth_rcvbulk_callback(struct urb *urb)
216a19259c3SDiego Giagio {
217a19259c3SDiego Giagio 	struct ipheth_device *dev;
218a19259c3SDiego Giagio 	struct sk_buff *skb;
219a19259c3SDiego Giagio 	int status;
220a19259c3SDiego Giagio 	char *buf;
221a19259c3SDiego Giagio 	int len;
222a19259c3SDiego Giagio 
223a19259c3SDiego Giagio 	dev = urb->context;
224a19259c3SDiego Giagio 	if (dev == NULL)
225a19259c3SDiego Giagio 		return;
226a19259c3SDiego Giagio 
227a19259c3SDiego Giagio 	status = urb->status;
228a19259c3SDiego Giagio 	switch (status) {
229a19259c3SDiego Giagio 	case -ENOENT:
230a19259c3SDiego Giagio 	case -ECONNRESET:
231a19259c3SDiego Giagio 	case -ESHUTDOWN:
232*45611c61SBernd Eckstein 	case -EPROTO:
233a19259c3SDiego Giagio 		return;
234a19259c3SDiego Giagio 	case 0:
235a19259c3SDiego Giagio 		break;
236a19259c3SDiego Giagio 	default:
237495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
238495f71e2SGreg Kroah-Hartman 			__func__, status);
239a19259c3SDiego Giagio 		return;
240a19259c3SDiego Giagio 	}
241a19259c3SDiego Giagio 
2429c412942SBen Hutchings 	if (urb->actual_length <= IPHETH_IP_ALIGN) {
2439c412942SBen Hutchings 		dev->net->stats.rx_length_errors++;
2449c412942SBen Hutchings 		return;
2459c412942SBen Hutchings 	}
2469c412942SBen Hutchings 	len = urb->actual_length - IPHETH_IP_ALIGN;
2479c412942SBen Hutchings 	buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
248a19259c3SDiego Giagio 
2499c412942SBen Hutchings 	skb = dev_alloc_skb(len);
250a19259c3SDiego Giagio 	if (!skb) {
251495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n",
252495f71e2SGreg Kroah-Hartman 			__func__);
253a19259c3SDiego Giagio 		dev->net->stats.rx_dropped++;
254a19259c3SDiego Giagio 		return;
255a19259c3SDiego Giagio 	}
256a19259c3SDiego Giagio 
25759ae1d12SJohannes Berg 	skb_put_data(skb, buf, len);
258a19259c3SDiego Giagio 	skb->dev = dev->net;
259a19259c3SDiego Giagio 	skb->protocol = eth_type_trans(skb, dev->net);
260a19259c3SDiego Giagio 
261a19259c3SDiego Giagio 	dev->net->stats.rx_packets++;
262a19259c3SDiego Giagio 	dev->net->stats.rx_bytes += len;
263bb1b40c7SAlexander Kappner 	dev->confirmed_pairing = true;
264a19259c3SDiego Giagio 	netif_rx(skb);
265a19259c3SDiego Giagio 	ipheth_rx_submit(dev, GFP_ATOMIC);
266a19259c3SDiego Giagio }
267a19259c3SDiego Giagio 
268a19259c3SDiego Giagio static void ipheth_sndbulk_callback(struct urb *urb)
269a19259c3SDiego Giagio {
270a19259c3SDiego Giagio 	struct ipheth_device *dev;
271c31fd6c2SOliver Neukum 	int status = urb->status;
272a19259c3SDiego Giagio 
273a19259c3SDiego Giagio 	dev = urb->context;
274a19259c3SDiego Giagio 	if (dev == NULL)
275a19259c3SDiego Giagio 		return;
276a19259c3SDiego Giagio 
277c31fd6c2SOliver Neukum 	if (status != 0 &&
278c31fd6c2SOliver Neukum 	    status != -ENOENT &&
279c31fd6c2SOliver Neukum 	    status != -ECONNRESET &&
280c31fd6c2SOliver Neukum 	    status != -ESHUTDOWN)
281495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
282495f71e2SGreg Kroah-Hartman 		__func__, status);
283a19259c3SDiego Giagio 
284bb1b40c7SAlexander Kappner 	if (status == 0)
285a19259c3SDiego Giagio 		netif_wake_queue(dev->net);
286bb1b40c7SAlexander Kappner 	else
287bb1b40c7SAlexander Kappner 		// on URB error, trigger immediate poll
288bb1b40c7SAlexander Kappner 		schedule_delayed_work(&dev->carrier_work, 0);
289a19259c3SDiego Giagio }
290a19259c3SDiego Giagio 
291a19259c3SDiego Giagio static int ipheth_carrier_set(struct ipheth_device *dev)
292a19259c3SDiego Giagio {
29361c59355SGustavo A. R. Silva 	struct usb_device *udev;
294a19259c3SDiego Giagio 	int retval;
29561c59355SGustavo A. R. Silva 
296bb1b40c7SAlexander Kappner 	if (!dev)
297bb1b40c7SAlexander Kappner 		return 0;
298bb1b40c7SAlexander Kappner 	if (!dev->confirmed_pairing)
299bb1b40c7SAlexander Kappner 		return 0;
30061c59355SGustavo A. R. Silva 
30161c59355SGustavo A. R. Silva 	udev = dev->udev;
302a19259c3SDiego Giagio 	retval = usb_control_msg(udev,
303a19259c3SDiego Giagio 			usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
304a19259c3SDiego Giagio 			IPHETH_CMD_CARRIER_CHECK, /* request */
305a19259c3SDiego Giagio 			0xc0, /* request type */
306a19259c3SDiego Giagio 			0x00, /* value */
307a19259c3SDiego Giagio 			0x02, /* index */
308a19259c3SDiego Giagio 			dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
309a19259c3SDiego Giagio 			IPHETH_CTRL_TIMEOUT);
310a19259c3SDiego Giagio 	if (retval < 0) {
311495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
312495f71e2SGreg Kroah-Hartman 			__func__, retval);
313a19259c3SDiego Giagio 		return retval;
314a19259c3SDiego Giagio 	}
315a19259c3SDiego Giagio 
316bb1b40c7SAlexander Kappner 	if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) {
317a19259c3SDiego Giagio 		netif_carrier_on(dev->net);
318bb1b40c7SAlexander Kappner 		if (dev->tx_urb->status != -EINPROGRESS)
319bb1b40c7SAlexander Kappner 			netif_wake_queue(dev->net);
320bb1b40c7SAlexander Kappner 	} else {
321a19259c3SDiego Giagio 		netif_carrier_off(dev->net);
322bb1b40c7SAlexander Kappner 		netif_stop_queue(dev->net);
323bb1b40c7SAlexander Kappner 	}
324a19259c3SDiego Giagio 	return 0;
325a19259c3SDiego Giagio }
326a19259c3SDiego Giagio 
327a19259c3SDiego Giagio static void ipheth_carrier_check_work(struct work_struct *work)
328a19259c3SDiego Giagio {
329a19259c3SDiego Giagio 	struct ipheth_device *dev = container_of(work, struct ipheth_device,
330a19259c3SDiego Giagio 						 carrier_work.work);
331a19259c3SDiego Giagio 
332a19259c3SDiego Giagio 	ipheth_carrier_set(dev);
333a19259c3SDiego Giagio 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
334a19259c3SDiego Giagio }
335a19259c3SDiego Giagio 
336a19259c3SDiego Giagio static int ipheth_get_macaddr(struct ipheth_device *dev)
337a19259c3SDiego Giagio {
338a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
339a19259c3SDiego Giagio 	struct net_device *net = dev->net;
340a19259c3SDiego Giagio 	int retval;
341a19259c3SDiego Giagio 
342a19259c3SDiego Giagio 	retval = usb_control_msg(udev,
343a19259c3SDiego Giagio 				 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
344a19259c3SDiego Giagio 				 IPHETH_CMD_GET_MACADDR, /* request */
345a19259c3SDiego Giagio 				 0xc0, /* request type */
346a19259c3SDiego Giagio 				 0x00, /* value */
347a19259c3SDiego Giagio 				 0x02, /* index */
348a19259c3SDiego Giagio 				 dev->ctrl_buf,
349a19259c3SDiego Giagio 				 IPHETH_CTRL_BUF_SIZE,
350a19259c3SDiego Giagio 				 IPHETH_CTRL_TIMEOUT);
351a19259c3SDiego Giagio 	if (retval < 0) {
352495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
353495f71e2SGreg Kroah-Hartman 			__func__, retval);
354a19259c3SDiego Giagio 	} else if (retval < ETH_ALEN) {
355495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev,
356495f71e2SGreg Kroah-Hartman 			"%s: usb_control_msg: short packet: %d bytes\n",
357a19259c3SDiego Giagio 			__func__, retval);
358a19259c3SDiego Giagio 		retval = -EINVAL;
359a19259c3SDiego Giagio 	} else {
360a19259c3SDiego Giagio 		memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN);
361a19259c3SDiego Giagio 		retval = 0;
362a19259c3SDiego Giagio 	}
363a19259c3SDiego Giagio 
364a19259c3SDiego Giagio 	return retval;
365a19259c3SDiego Giagio }
366a19259c3SDiego Giagio 
367a19259c3SDiego Giagio static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
368a19259c3SDiego Giagio {
369a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
370a19259c3SDiego Giagio 	int retval;
371a19259c3SDiego Giagio 
372a19259c3SDiego Giagio 	usb_fill_bulk_urb(dev->rx_urb, udev,
373a19259c3SDiego Giagio 			  usb_rcvbulkpipe(udev, dev->bulk_in),
374a19259c3SDiego Giagio 			  dev->rx_buf, IPHETH_BUF_SIZE,
375a19259c3SDiego Giagio 			  ipheth_rcvbulk_callback,
376a19259c3SDiego Giagio 			  dev);
377a19259c3SDiego Giagio 	dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
378a19259c3SDiego Giagio 
379a19259c3SDiego Giagio 	retval = usb_submit_urb(dev->rx_urb, mem_flags);
380a19259c3SDiego Giagio 	if (retval)
381495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
382495f71e2SGreg Kroah-Hartman 			__func__, retval);
383a19259c3SDiego Giagio 	return retval;
384a19259c3SDiego Giagio }
385a19259c3SDiego Giagio 
386a19259c3SDiego Giagio static int ipheth_open(struct net_device *net)
387a19259c3SDiego Giagio {
388a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
389a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
390a19259c3SDiego Giagio 	int retval = 0;
391a19259c3SDiego Giagio 
392a19259c3SDiego Giagio 	usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
393a19259c3SDiego Giagio 
394a19259c3SDiego Giagio 	retval = ipheth_carrier_set(dev);
395a19259c3SDiego Giagio 	if (retval)
396a19259c3SDiego Giagio 		return retval;
397a19259c3SDiego Giagio 
398a19259c3SDiego Giagio 	retval = ipheth_rx_submit(dev, GFP_KERNEL);
399a19259c3SDiego Giagio 	if (retval)
400a19259c3SDiego Giagio 		return retval;
401a19259c3SDiego Giagio 
402a19259c3SDiego Giagio 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
403a19259c3SDiego Giagio 	return retval;
404a19259c3SDiego Giagio }
405a19259c3SDiego Giagio 
406a19259c3SDiego Giagio static int ipheth_close(struct net_device *net)
407a19259c3SDiego Giagio {
408a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
409a19259c3SDiego Giagio 
410a19259c3SDiego Giagio 	cancel_delayed_work_sync(&dev->carrier_work);
411a19259c3SDiego Giagio 	netif_stop_queue(net);
412a19259c3SDiego Giagio 	return 0;
413a19259c3SDiego Giagio }
414a19259c3SDiego Giagio 
415a19259c3SDiego Giagio static int ipheth_tx(struct sk_buff *skb, struct net_device *net)
416a19259c3SDiego Giagio {
417a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
418a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
419a19259c3SDiego Giagio 	int retval;
420a19259c3SDiego Giagio 
421a19259c3SDiego Giagio 	/* Paranoid */
422a19259c3SDiego Giagio 	if (skb->len > IPHETH_BUF_SIZE) {
4239c413ed5SJoe Perches 		WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
424a19259c3SDiego Giagio 		dev->net->stats.tx_dropped++;
425*45611c61SBernd Eckstein 		dev_kfree_skb_any(skb);
426a19259c3SDiego Giagio 		return NETDEV_TX_OK;
427a19259c3SDiego Giagio 	}
428a19259c3SDiego Giagio 
429a19259c3SDiego Giagio 	memcpy(dev->tx_buf, skb->data, skb->len);
430a19259c3SDiego Giagio 	if (skb->len < IPHETH_BUF_SIZE)
431a19259c3SDiego Giagio 		memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len);
432a19259c3SDiego Giagio 
433a19259c3SDiego Giagio 	usb_fill_bulk_urb(dev->tx_urb, udev,
434a19259c3SDiego Giagio 			  usb_sndbulkpipe(udev, dev->bulk_out),
435a19259c3SDiego Giagio 			  dev->tx_buf, IPHETH_BUF_SIZE,
436a19259c3SDiego Giagio 			  ipheth_sndbulk_callback,
437a19259c3SDiego Giagio 			  dev);
438a19259c3SDiego Giagio 	dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
439a19259c3SDiego Giagio 
440a19259c3SDiego Giagio 	retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
441a19259c3SDiego Giagio 	if (retval) {
442495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
443495f71e2SGreg Kroah-Hartman 			__func__, retval);
444a19259c3SDiego Giagio 		dev->net->stats.tx_errors++;
445*45611c61SBernd Eckstein 		dev_kfree_skb_any(skb);
446a19259c3SDiego Giagio 	} else {
447a19259c3SDiego Giagio 		dev->net->stats.tx_packets++;
448a19259c3SDiego Giagio 		dev->net->stats.tx_bytes += skb->len;
449*45611c61SBernd Eckstein 		dev_consume_skb_any(skb);
450a19259c3SDiego Giagio 		netif_stop_queue(net);
451a19259c3SDiego Giagio 	}
452a19259c3SDiego Giagio 
453a19259c3SDiego Giagio 	return NETDEV_TX_OK;
454a19259c3SDiego Giagio }
455a19259c3SDiego Giagio 
456a19259c3SDiego Giagio static void ipheth_tx_timeout(struct net_device *net)
457a19259c3SDiego Giagio {
458a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
459a19259c3SDiego Giagio 
460495f71e2SGreg Kroah-Hartman 	dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
461a19259c3SDiego Giagio 	dev->net->stats.tx_errors++;
462a19259c3SDiego Giagio 	usb_unlink_urb(dev->tx_urb);
463a19259c3SDiego Giagio }
464a19259c3SDiego Giagio 
465a19259c3SDiego Giagio static u32 ipheth_ethtool_op_get_link(struct net_device *net)
466a19259c3SDiego Giagio {
467a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
468a19259c3SDiego Giagio 	return netif_carrier_ok(dev->net);
469a19259c3SDiego Giagio }
470a19259c3SDiego Giagio 
471bc689c97Sstephen hemminger static const struct ethtool_ops ops = {
472a19259c3SDiego Giagio 	.get_link = ipheth_ethtool_op_get_link
473a19259c3SDiego Giagio };
474a19259c3SDiego Giagio 
475a19259c3SDiego Giagio static const struct net_device_ops ipheth_netdev_ops = {
4768ef207d6Sstephen hemminger 	.ndo_open = ipheth_open,
4778ef207d6Sstephen hemminger 	.ndo_stop = ipheth_close,
4788ef207d6Sstephen hemminger 	.ndo_start_xmit = ipheth_tx,
4798ef207d6Sstephen hemminger 	.ndo_tx_timeout = ipheth_tx_timeout,
480a19259c3SDiego Giagio };
481a19259c3SDiego Giagio 
482a19259c3SDiego Giagio static int ipheth_probe(struct usb_interface *intf,
483a19259c3SDiego Giagio 			const struct usb_device_id *id)
484a19259c3SDiego Giagio {
485a19259c3SDiego Giagio 	struct usb_device *udev = interface_to_usbdev(intf);
486a19259c3SDiego Giagio 	struct usb_host_interface *hintf;
487a19259c3SDiego Giagio 	struct usb_endpoint_descriptor *endp;
488a19259c3SDiego Giagio 	struct ipheth_device *dev;
489a19259c3SDiego Giagio 	struct net_device *netdev;
490a19259c3SDiego Giagio 	int i;
491a19259c3SDiego Giagio 	int retval;
492a19259c3SDiego Giagio 
493a19259c3SDiego Giagio 	netdev = alloc_etherdev(sizeof(struct ipheth_device));
494a19259c3SDiego Giagio 	if (!netdev)
495a19259c3SDiego Giagio 		return -ENOMEM;
496a19259c3SDiego Giagio 
497a19259c3SDiego Giagio 	netdev->netdev_ops = &ipheth_netdev_ops;
498a19259c3SDiego Giagio 	netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
499c9cedbbaSDan Williams 	strcpy(netdev->name, "eth%d");
500a19259c3SDiego Giagio 
501a19259c3SDiego Giagio 	dev = netdev_priv(netdev);
502a19259c3SDiego Giagio 	dev->udev = udev;
503a19259c3SDiego Giagio 	dev->net = netdev;
504a19259c3SDiego Giagio 	dev->intf = intf;
505bb1b40c7SAlexander Kappner 	dev->confirmed_pairing = false;
506a19259c3SDiego Giagio 	/* Set up endpoints */
507a19259c3SDiego Giagio 	hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
508a19259c3SDiego Giagio 	if (hintf == NULL) {
509a19259c3SDiego Giagio 		retval = -ENODEV;
510495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "Unable to find alternate settings interface\n");
511a19259c3SDiego Giagio 		goto err_endpoints;
512a19259c3SDiego Giagio 	}
513a19259c3SDiego Giagio 
514a19259c3SDiego Giagio 	for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
515a19259c3SDiego Giagio 		endp = &hintf->endpoint[i].desc;
516a19259c3SDiego Giagio 		if (usb_endpoint_is_bulk_in(endp))
517a19259c3SDiego Giagio 			dev->bulk_in = endp->bEndpointAddress;
518a19259c3SDiego Giagio 		else if (usb_endpoint_is_bulk_out(endp))
519a19259c3SDiego Giagio 			dev->bulk_out = endp->bEndpointAddress;
520a19259c3SDiego Giagio 	}
521a19259c3SDiego Giagio 	if (!(dev->bulk_in && dev->bulk_out)) {
522a19259c3SDiego Giagio 		retval = -ENODEV;
523495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "Unable to find endpoints\n");
524a19259c3SDiego Giagio 		goto err_endpoints;
525a19259c3SDiego Giagio 	}
526a19259c3SDiego Giagio 
527a19259c3SDiego Giagio 	dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
528a19259c3SDiego Giagio 	if (dev->ctrl_buf == NULL) {
529a19259c3SDiego Giagio 		retval = -ENOMEM;
530a19259c3SDiego Giagio 		goto err_alloc_ctrl_buf;
531a19259c3SDiego Giagio 	}
532a19259c3SDiego Giagio 
533a19259c3SDiego Giagio 	retval = ipheth_get_macaddr(dev);
534a19259c3SDiego Giagio 	if (retval)
535a19259c3SDiego Giagio 		goto err_get_macaddr;
536a19259c3SDiego Giagio 
537a19259c3SDiego Giagio 	INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
538a19259c3SDiego Giagio 
539a19259c3SDiego Giagio 	retval = ipheth_alloc_urbs(dev);
540a19259c3SDiego Giagio 	if (retval) {
541495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
542a19259c3SDiego Giagio 		goto err_alloc_urbs;
543a19259c3SDiego Giagio 	}
544a19259c3SDiego Giagio 
545a19259c3SDiego Giagio 	usb_set_intfdata(intf, dev);
546a19259c3SDiego Giagio 
547a19259c3SDiego Giagio 	SET_NETDEV_DEV(netdev, &intf->dev);
5487ad24ea4SWilfried Klaebe 	netdev->ethtool_ops = &ops;
549a19259c3SDiego Giagio 
550a19259c3SDiego Giagio 	retval = register_netdev(netdev);
551a19259c3SDiego Giagio 	if (retval) {
552495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "error registering netdev: %d\n", retval);
553a19259c3SDiego Giagio 		retval = -EIO;
554a19259c3SDiego Giagio 		goto err_register_netdev;
555a19259c3SDiego Giagio 	}
556bb1b40c7SAlexander Kappner 	// carrier down and transmit queues stopped until packet from device
557bb1b40c7SAlexander Kappner 	netif_carrier_off(netdev);
558bb1b40c7SAlexander Kappner 	netif_tx_stop_all_queues(netdev);
559a19259c3SDiego Giagio 	dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
560a19259c3SDiego Giagio 	return 0;
561a19259c3SDiego Giagio 
562a19259c3SDiego Giagio err_register_netdev:
563a19259c3SDiego Giagio 	ipheth_free_urbs(dev);
564a19259c3SDiego Giagio err_alloc_urbs:
565a19259c3SDiego Giagio err_get_macaddr:
566a19259c3SDiego Giagio err_alloc_ctrl_buf:
567a19259c3SDiego Giagio 	kfree(dev->ctrl_buf);
568a19259c3SDiego Giagio err_endpoints:
569a19259c3SDiego Giagio 	free_netdev(netdev);
570a19259c3SDiego Giagio 	return retval;
571a19259c3SDiego Giagio }
572a19259c3SDiego Giagio 
573a19259c3SDiego Giagio static void ipheth_disconnect(struct usb_interface *intf)
574a19259c3SDiego Giagio {
575a19259c3SDiego Giagio 	struct ipheth_device *dev;
576a19259c3SDiego Giagio 
577a19259c3SDiego Giagio 	dev = usb_get_intfdata(intf);
578a19259c3SDiego Giagio 	if (dev != NULL) {
579a19259c3SDiego Giagio 		unregister_netdev(dev->net);
580a19259c3SDiego Giagio 		ipheth_kill_urbs(dev);
581a19259c3SDiego Giagio 		ipheth_free_urbs(dev);
582a19259c3SDiego Giagio 		kfree(dev->ctrl_buf);
583a19259c3SDiego Giagio 		free_netdev(dev->net);
584a19259c3SDiego Giagio 	}
585a19259c3SDiego Giagio 	usb_set_intfdata(intf, NULL);
586a19259c3SDiego Giagio 	dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
587a19259c3SDiego Giagio }
588a19259c3SDiego Giagio 
589a19259c3SDiego Giagio static struct usb_driver ipheth_driver = {
590a19259c3SDiego Giagio 	.name =		"ipheth",
591a19259c3SDiego Giagio 	.probe =	ipheth_probe,
592a19259c3SDiego Giagio 	.disconnect =	ipheth_disconnect,
593a19259c3SDiego Giagio 	.id_table =	ipheth_table,
594e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
595a19259c3SDiego Giagio };
596a19259c3SDiego Giagio 
597d632eb1bSGreg Kroah-Hartman module_usb_driver(ipheth_driver);
598a19259c3SDiego Giagio 
599a19259c3SDiego Giagio MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
600a19259c3SDiego Giagio MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
601a19259c3SDiego Giagio MODULE_LICENSE("Dual BSD/GPL");
602