xref: /openbmc/linux/drivers/net/usb/ipheth.c (revision 493c3ca6bd754d8587604496eb814f72e933075d)
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 
58a19259c3SDiego Giagio #define IPHETH_USBINTF_CLASS    255
59a19259c3SDiego Giagio #define IPHETH_USBINTF_SUBCLASS 253
60a19259c3SDiego Giagio #define IPHETH_USBINTF_PROTO    1
61a19259c3SDiego Giagio 
62f33d9e2bSYves-Alexis Perez #define IPHETH_BUF_SIZE         1514
639c412942SBen Hutchings #define IPHETH_IP_ALIGN		2	/* padding at front of URB */
64a19259c3SDiego Giagio #define IPHETH_TX_TIMEOUT       (5 * HZ)
65a19259c3SDiego Giagio 
66a19259c3SDiego Giagio #define IPHETH_INTFNUM          2
67a19259c3SDiego Giagio #define IPHETH_ALT_INTFNUM      1
68a19259c3SDiego Giagio 
69a19259c3SDiego Giagio #define IPHETH_CTRL_ENDP        0x00
70a19259c3SDiego Giagio #define IPHETH_CTRL_BUF_SIZE    0x40
71a19259c3SDiego Giagio #define IPHETH_CTRL_TIMEOUT     (5 * HZ)
72a19259c3SDiego Giagio 
73a19259c3SDiego Giagio #define IPHETH_CMD_GET_MACADDR   0x00
74a19259c3SDiego Giagio #define IPHETH_CMD_CARRIER_CHECK 0x45
75a19259c3SDiego Giagio 
76a19259c3SDiego Giagio #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
77a19259c3SDiego Giagio #define IPHETH_CARRIER_ON       0x04
78a19259c3SDiego Giagio 
799befbe13SArvind Yadav static const struct usb_device_id ipheth_table[] = {
80f7abc061SGuenter Roeck 	{ USB_VENDOR_AND_INTERFACE_INFO(USB_VENDOR_APPLE, IPHETH_USBINTF_CLASS,
81f7abc061SGuenter Roeck 					IPHETH_USBINTF_SUBCLASS,
82af1b85e4SJay Purohit 					IPHETH_USBINTF_PROTO) },
83a19259c3SDiego Giagio 	{ }
84a19259c3SDiego Giagio };
85a19259c3SDiego Giagio MODULE_DEVICE_TABLE(usb, ipheth_table);
86a19259c3SDiego Giagio 
87a19259c3SDiego Giagio struct ipheth_device {
88a19259c3SDiego Giagio 	struct usb_device *udev;
89a19259c3SDiego Giagio 	struct usb_interface *intf;
90a19259c3SDiego Giagio 	struct net_device *net;
91a19259c3SDiego Giagio 	struct urb *tx_urb;
92a19259c3SDiego Giagio 	struct urb *rx_urb;
93a19259c3SDiego Giagio 	unsigned char *tx_buf;
94a19259c3SDiego Giagio 	unsigned char *rx_buf;
95a19259c3SDiego Giagio 	unsigned char *ctrl_buf;
96a19259c3SDiego Giagio 	u8 bulk_in;
97a19259c3SDiego Giagio 	u8 bulk_out;
98a19259c3SDiego Giagio 	struct delayed_work carrier_work;
99bb1b40c7SAlexander Kappner 	bool confirmed_pairing;
100a19259c3SDiego Giagio };
101a19259c3SDiego Giagio 
102a19259c3SDiego Giagio static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
103a19259c3SDiego Giagio 
104a19259c3SDiego Giagio static int ipheth_alloc_urbs(struct ipheth_device *iphone)
105a19259c3SDiego Giagio {
106a19259c3SDiego Giagio 	struct urb *tx_urb = NULL;
107a19259c3SDiego Giagio 	struct urb *rx_urb = NULL;
108a19259c3SDiego Giagio 	u8 *tx_buf = NULL;
109a19259c3SDiego Giagio 	u8 *rx_buf = NULL;
110a19259c3SDiego Giagio 
111a19259c3SDiego Giagio 	tx_urb = usb_alloc_urb(0, GFP_KERNEL);
112a19259c3SDiego Giagio 	if (tx_urb == NULL)
113d87ff58fSDan Carpenter 		goto error_nomem;
114a19259c3SDiego Giagio 
115a19259c3SDiego Giagio 	rx_urb = usb_alloc_urb(0, GFP_KERNEL);
116a19259c3SDiego Giagio 	if (rx_urb == NULL)
117d87ff58fSDan Carpenter 		goto free_tx_urb;
118a19259c3SDiego Giagio 
119997ea58eSDaniel Mack 	tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
120997ea58eSDaniel Mack 				    GFP_KERNEL, &tx_urb->transfer_dma);
121a19259c3SDiego Giagio 	if (tx_buf == NULL)
122d87ff58fSDan Carpenter 		goto free_rx_urb;
123a19259c3SDiego Giagio 
124997ea58eSDaniel Mack 	rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
125997ea58eSDaniel Mack 				    GFP_KERNEL, &rx_urb->transfer_dma);
126a19259c3SDiego Giagio 	if (rx_buf == NULL)
127d87ff58fSDan Carpenter 		goto free_tx_buf;
128a19259c3SDiego Giagio 
129a19259c3SDiego Giagio 
130a19259c3SDiego Giagio 	iphone->tx_urb = tx_urb;
131a19259c3SDiego Giagio 	iphone->rx_urb = rx_urb;
132a19259c3SDiego Giagio 	iphone->tx_buf = tx_buf;
133a19259c3SDiego Giagio 	iphone->rx_buf = rx_buf;
134a19259c3SDiego Giagio 	return 0;
135a19259c3SDiego Giagio 
136d87ff58fSDan Carpenter free_tx_buf:
137997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf,
138a19259c3SDiego Giagio 			  tx_urb->transfer_dma);
139d87ff58fSDan Carpenter free_rx_urb:
140a19259c3SDiego Giagio 	usb_free_urb(rx_urb);
141d87ff58fSDan Carpenter free_tx_urb:
142a19259c3SDiego Giagio 	usb_free_urb(tx_urb);
143d87ff58fSDan Carpenter error_nomem:
144a19259c3SDiego Giagio 	return -ENOMEM;
145a19259c3SDiego Giagio }
146a19259c3SDiego Giagio 
147a19259c3SDiego Giagio static void ipheth_free_urbs(struct ipheth_device *iphone)
148a19259c3SDiego Giagio {
149997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->rx_buf,
150a19259c3SDiego Giagio 			  iphone->rx_urb->transfer_dma);
151997ea58eSDaniel Mack 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf,
152a19259c3SDiego Giagio 			  iphone->tx_urb->transfer_dma);
153a19259c3SDiego Giagio 	usb_free_urb(iphone->rx_urb);
154a19259c3SDiego Giagio 	usb_free_urb(iphone->tx_urb);
155a19259c3SDiego Giagio }
156a19259c3SDiego Giagio 
157a19259c3SDiego Giagio static void ipheth_kill_urbs(struct ipheth_device *dev)
158a19259c3SDiego Giagio {
159a19259c3SDiego Giagio 	usb_kill_urb(dev->tx_urb);
160a19259c3SDiego Giagio 	usb_kill_urb(dev->rx_urb);
161a19259c3SDiego Giagio }
162a19259c3SDiego Giagio 
163a19259c3SDiego Giagio static void ipheth_rcvbulk_callback(struct urb *urb)
164a19259c3SDiego Giagio {
165a19259c3SDiego Giagio 	struct ipheth_device *dev;
166a19259c3SDiego Giagio 	struct sk_buff *skb;
167a19259c3SDiego Giagio 	int status;
168a19259c3SDiego Giagio 	char *buf;
169a19259c3SDiego Giagio 	int len;
170a19259c3SDiego Giagio 
171a19259c3SDiego Giagio 	dev = urb->context;
172a19259c3SDiego Giagio 	if (dev == NULL)
173a19259c3SDiego Giagio 		return;
174a19259c3SDiego Giagio 
175a19259c3SDiego Giagio 	status = urb->status;
176a19259c3SDiego Giagio 	switch (status) {
177a19259c3SDiego Giagio 	case -ENOENT:
178a19259c3SDiego Giagio 	case -ECONNRESET:
179a19259c3SDiego Giagio 	case -ESHUTDOWN:
18045611c61SBernd Eckstein 	case -EPROTO:
181a19259c3SDiego Giagio 		return;
182a19259c3SDiego Giagio 	case 0:
183a19259c3SDiego Giagio 		break;
184a19259c3SDiego Giagio 	default:
185495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
186495f71e2SGreg Kroah-Hartman 			__func__, status);
187a19259c3SDiego Giagio 		return;
188a19259c3SDiego Giagio 	}
189a19259c3SDiego Giagio 
1909c412942SBen Hutchings 	if (urb->actual_length <= IPHETH_IP_ALIGN) {
1919c412942SBen Hutchings 		dev->net->stats.rx_length_errors++;
1929c412942SBen Hutchings 		return;
1939c412942SBen Hutchings 	}
1949c412942SBen Hutchings 	len = urb->actual_length - IPHETH_IP_ALIGN;
1959c412942SBen Hutchings 	buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
196a19259c3SDiego Giagio 
1979c412942SBen Hutchings 	skb = dev_alloc_skb(len);
198a19259c3SDiego Giagio 	if (!skb) {
199495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n",
200495f71e2SGreg Kroah-Hartman 			__func__);
201a19259c3SDiego Giagio 		dev->net->stats.rx_dropped++;
202a19259c3SDiego Giagio 		return;
203a19259c3SDiego Giagio 	}
204a19259c3SDiego Giagio 
20559ae1d12SJohannes Berg 	skb_put_data(skb, buf, len);
206a19259c3SDiego Giagio 	skb->dev = dev->net;
207a19259c3SDiego Giagio 	skb->protocol = eth_type_trans(skb, dev->net);
208a19259c3SDiego Giagio 
209a19259c3SDiego Giagio 	dev->net->stats.rx_packets++;
210a19259c3SDiego Giagio 	dev->net->stats.rx_bytes += len;
211bb1b40c7SAlexander Kappner 	dev->confirmed_pairing = true;
212a19259c3SDiego Giagio 	netif_rx(skb);
213a19259c3SDiego Giagio 	ipheth_rx_submit(dev, GFP_ATOMIC);
214a19259c3SDiego Giagio }
215a19259c3SDiego Giagio 
216a19259c3SDiego Giagio static void ipheth_sndbulk_callback(struct urb *urb)
217a19259c3SDiego Giagio {
218a19259c3SDiego Giagio 	struct ipheth_device *dev;
219c31fd6c2SOliver Neukum 	int status = urb->status;
220a19259c3SDiego Giagio 
221a19259c3SDiego Giagio 	dev = urb->context;
222a19259c3SDiego Giagio 	if (dev == NULL)
223a19259c3SDiego Giagio 		return;
224a19259c3SDiego Giagio 
225c31fd6c2SOliver Neukum 	if (status != 0 &&
226c31fd6c2SOliver Neukum 	    status != -ENOENT &&
227c31fd6c2SOliver Neukum 	    status != -ECONNRESET &&
228c31fd6c2SOliver Neukum 	    status != -ESHUTDOWN)
229495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
230495f71e2SGreg Kroah-Hartman 		__func__, status);
231a19259c3SDiego Giagio 
232bb1b40c7SAlexander Kappner 	if (status == 0)
233a19259c3SDiego Giagio 		netif_wake_queue(dev->net);
234bb1b40c7SAlexander Kappner 	else
235bb1b40c7SAlexander Kappner 		// on URB error, trigger immediate poll
236bb1b40c7SAlexander Kappner 		schedule_delayed_work(&dev->carrier_work, 0);
237a19259c3SDiego Giagio }
238a19259c3SDiego Giagio 
239a19259c3SDiego Giagio static int ipheth_carrier_set(struct ipheth_device *dev)
240a19259c3SDiego Giagio {
24161c59355SGustavo A. R. Silva 	struct usb_device *udev;
242a19259c3SDiego Giagio 	int retval;
24361c59355SGustavo A. R. Silva 
244bb1b40c7SAlexander Kappner 	if (!dev->confirmed_pairing)
245bb1b40c7SAlexander Kappner 		return 0;
24661c59355SGustavo A. R. Silva 
24761c59355SGustavo A. R. Silva 	udev = dev->udev;
248a19259c3SDiego Giagio 	retval = usb_control_msg(udev,
249a19259c3SDiego Giagio 			usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
250a19259c3SDiego Giagio 			IPHETH_CMD_CARRIER_CHECK, /* request */
251a19259c3SDiego Giagio 			0xc0, /* request type */
252a19259c3SDiego Giagio 			0x00, /* value */
253a19259c3SDiego Giagio 			0x02, /* index */
254a19259c3SDiego Giagio 			dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
255a19259c3SDiego Giagio 			IPHETH_CTRL_TIMEOUT);
256a19259c3SDiego Giagio 	if (retval < 0) {
257495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
258495f71e2SGreg Kroah-Hartman 			__func__, retval);
259a19259c3SDiego Giagio 		return retval;
260a19259c3SDiego Giagio 	}
261a19259c3SDiego Giagio 
262bb1b40c7SAlexander Kappner 	if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) {
263a19259c3SDiego Giagio 		netif_carrier_on(dev->net);
264bb1b40c7SAlexander Kappner 		if (dev->tx_urb->status != -EINPROGRESS)
265bb1b40c7SAlexander Kappner 			netif_wake_queue(dev->net);
266bb1b40c7SAlexander Kappner 	} else {
267a19259c3SDiego Giagio 		netif_carrier_off(dev->net);
268bb1b40c7SAlexander Kappner 		netif_stop_queue(dev->net);
269bb1b40c7SAlexander Kappner 	}
270a19259c3SDiego Giagio 	return 0;
271a19259c3SDiego Giagio }
272a19259c3SDiego Giagio 
273a19259c3SDiego Giagio static void ipheth_carrier_check_work(struct work_struct *work)
274a19259c3SDiego Giagio {
275a19259c3SDiego Giagio 	struct ipheth_device *dev = container_of(work, struct ipheth_device,
276a19259c3SDiego Giagio 						 carrier_work.work);
277a19259c3SDiego Giagio 
278a19259c3SDiego Giagio 	ipheth_carrier_set(dev);
279a19259c3SDiego Giagio 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
280a19259c3SDiego Giagio }
281a19259c3SDiego Giagio 
282a19259c3SDiego Giagio static int ipheth_get_macaddr(struct ipheth_device *dev)
283a19259c3SDiego Giagio {
284a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
285a19259c3SDiego Giagio 	struct net_device *net = dev->net;
286a19259c3SDiego Giagio 	int retval;
287a19259c3SDiego Giagio 
288a19259c3SDiego Giagio 	retval = usb_control_msg(udev,
289a19259c3SDiego Giagio 				 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
290a19259c3SDiego Giagio 				 IPHETH_CMD_GET_MACADDR, /* request */
291a19259c3SDiego Giagio 				 0xc0, /* request type */
292a19259c3SDiego Giagio 				 0x00, /* value */
293a19259c3SDiego Giagio 				 0x02, /* index */
294a19259c3SDiego Giagio 				 dev->ctrl_buf,
295a19259c3SDiego Giagio 				 IPHETH_CTRL_BUF_SIZE,
296a19259c3SDiego Giagio 				 IPHETH_CTRL_TIMEOUT);
297a19259c3SDiego Giagio 	if (retval < 0) {
298495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
299495f71e2SGreg Kroah-Hartman 			__func__, retval);
300a19259c3SDiego Giagio 	} else if (retval < ETH_ALEN) {
301495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev,
302495f71e2SGreg Kroah-Hartman 			"%s: usb_control_msg: short packet: %d bytes\n",
303a19259c3SDiego Giagio 			__func__, retval);
304a19259c3SDiego Giagio 		retval = -EINVAL;
305a19259c3SDiego Giagio 	} else {
306a19259c3SDiego Giagio 		memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN);
307a19259c3SDiego Giagio 		retval = 0;
308a19259c3SDiego Giagio 	}
309a19259c3SDiego Giagio 
310a19259c3SDiego Giagio 	return retval;
311a19259c3SDiego Giagio }
312a19259c3SDiego Giagio 
313a19259c3SDiego Giagio static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
314a19259c3SDiego Giagio {
315a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
316a19259c3SDiego Giagio 	int retval;
317a19259c3SDiego Giagio 
318a19259c3SDiego Giagio 	usb_fill_bulk_urb(dev->rx_urb, udev,
319a19259c3SDiego Giagio 			  usb_rcvbulkpipe(udev, dev->bulk_in),
320a19259c3SDiego Giagio 			  dev->rx_buf, IPHETH_BUF_SIZE,
321a19259c3SDiego Giagio 			  ipheth_rcvbulk_callback,
322a19259c3SDiego Giagio 			  dev);
323a19259c3SDiego Giagio 	dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
324a19259c3SDiego Giagio 
325a19259c3SDiego Giagio 	retval = usb_submit_urb(dev->rx_urb, mem_flags);
326a19259c3SDiego Giagio 	if (retval)
327495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
328495f71e2SGreg Kroah-Hartman 			__func__, retval);
329a19259c3SDiego Giagio 	return retval;
330a19259c3SDiego Giagio }
331a19259c3SDiego Giagio 
332a19259c3SDiego Giagio static int ipheth_open(struct net_device *net)
333a19259c3SDiego Giagio {
334a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
335a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
336a19259c3SDiego Giagio 	int retval = 0;
337a19259c3SDiego Giagio 
338a19259c3SDiego Giagio 	usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
339a19259c3SDiego Giagio 
340a19259c3SDiego Giagio 	retval = ipheth_carrier_set(dev);
341a19259c3SDiego Giagio 	if (retval)
342a19259c3SDiego Giagio 		return retval;
343a19259c3SDiego Giagio 
344a19259c3SDiego Giagio 	retval = ipheth_rx_submit(dev, GFP_KERNEL);
345a19259c3SDiego Giagio 	if (retval)
346a19259c3SDiego Giagio 		return retval;
347a19259c3SDiego Giagio 
348a19259c3SDiego Giagio 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
349a19259c3SDiego Giagio 	return retval;
350a19259c3SDiego Giagio }
351a19259c3SDiego Giagio 
352a19259c3SDiego Giagio static int ipheth_close(struct net_device *net)
353a19259c3SDiego Giagio {
354a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
355a19259c3SDiego Giagio 
356a19259c3SDiego Giagio 	cancel_delayed_work_sync(&dev->carrier_work);
357a19259c3SDiego Giagio 	netif_stop_queue(net);
358a19259c3SDiego Giagio 	return 0;
359a19259c3SDiego Giagio }
360a19259c3SDiego Giagio 
361146ba9a3SLuc Van Oostenryck static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
362a19259c3SDiego Giagio {
363a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
364a19259c3SDiego Giagio 	struct usb_device *udev = dev->udev;
365a19259c3SDiego Giagio 	int retval;
366a19259c3SDiego Giagio 
367a19259c3SDiego Giagio 	/* Paranoid */
368a19259c3SDiego Giagio 	if (skb->len > IPHETH_BUF_SIZE) {
3699c413ed5SJoe Perches 		WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
370a19259c3SDiego Giagio 		dev->net->stats.tx_dropped++;
37145611c61SBernd Eckstein 		dev_kfree_skb_any(skb);
372a19259c3SDiego Giagio 		return NETDEV_TX_OK;
373a19259c3SDiego Giagio 	}
374a19259c3SDiego Giagio 
375a19259c3SDiego Giagio 	memcpy(dev->tx_buf, skb->data, skb->len);
376a19259c3SDiego Giagio 	if (skb->len < IPHETH_BUF_SIZE)
377a19259c3SDiego Giagio 		memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len);
378a19259c3SDiego Giagio 
379a19259c3SDiego Giagio 	usb_fill_bulk_urb(dev->tx_urb, udev,
380a19259c3SDiego Giagio 			  usb_sndbulkpipe(udev, dev->bulk_out),
381a19259c3SDiego Giagio 			  dev->tx_buf, IPHETH_BUF_SIZE,
382a19259c3SDiego Giagio 			  ipheth_sndbulk_callback,
383a19259c3SDiego Giagio 			  dev);
384a19259c3SDiego Giagio 	dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
385a19259c3SDiego Giagio 
38694d250faSBernd Eckstein 	netif_stop_queue(net);
387a19259c3SDiego Giagio 	retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
388a19259c3SDiego Giagio 	if (retval) {
389495f71e2SGreg Kroah-Hartman 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
390495f71e2SGreg Kroah-Hartman 			__func__, retval);
391a19259c3SDiego Giagio 		dev->net->stats.tx_errors++;
39245611c61SBernd Eckstein 		dev_kfree_skb_any(skb);
39394d250faSBernd Eckstein 		netif_wake_queue(net);
394a19259c3SDiego Giagio 	} else {
395a19259c3SDiego Giagio 		dev->net->stats.tx_packets++;
396a19259c3SDiego Giagio 		dev->net->stats.tx_bytes += skb->len;
39745611c61SBernd Eckstein 		dev_consume_skb_any(skb);
398a19259c3SDiego Giagio 	}
399a19259c3SDiego Giagio 
400a19259c3SDiego Giagio 	return NETDEV_TX_OK;
401a19259c3SDiego Giagio }
402a19259c3SDiego Giagio 
4030290bd29SMichael S. Tsirkin static void ipheth_tx_timeout(struct net_device *net, unsigned int txqueue)
404a19259c3SDiego Giagio {
405a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
406a19259c3SDiego Giagio 
407495f71e2SGreg Kroah-Hartman 	dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
408a19259c3SDiego Giagio 	dev->net->stats.tx_errors++;
409a19259c3SDiego Giagio 	usb_unlink_urb(dev->tx_urb);
410a19259c3SDiego Giagio }
411a19259c3SDiego Giagio 
412a19259c3SDiego Giagio static u32 ipheth_ethtool_op_get_link(struct net_device *net)
413a19259c3SDiego Giagio {
414a19259c3SDiego Giagio 	struct ipheth_device *dev = netdev_priv(net);
415a19259c3SDiego Giagio 	return netif_carrier_ok(dev->net);
416a19259c3SDiego Giagio }
417a19259c3SDiego Giagio 
418bc689c97Sstephen hemminger static const struct ethtool_ops ops = {
419a19259c3SDiego Giagio 	.get_link = ipheth_ethtool_op_get_link
420a19259c3SDiego Giagio };
421a19259c3SDiego Giagio 
422a19259c3SDiego Giagio static const struct net_device_ops ipheth_netdev_ops = {
4238ef207d6Sstephen hemminger 	.ndo_open = ipheth_open,
4248ef207d6Sstephen hemminger 	.ndo_stop = ipheth_close,
4258ef207d6Sstephen hemminger 	.ndo_start_xmit = ipheth_tx,
4268ef207d6Sstephen hemminger 	.ndo_tx_timeout = ipheth_tx_timeout,
427a19259c3SDiego Giagio };
428a19259c3SDiego Giagio 
429a19259c3SDiego Giagio static int ipheth_probe(struct usb_interface *intf,
430a19259c3SDiego Giagio 			const struct usb_device_id *id)
431a19259c3SDiego Giagio {
432a19259c3SDiego Giagio 	struct usb_device *udev = interface_to_usbdev(intf);
433a19259c3SDiego Giagio 	struct usb_host_interface *hintf;
434a19259c3SDiego Giagio 	struct usb_endpoint_descriptor *endp;
435a19259c3SDiego Giagio 	struct ipheth_device *dev;
436a19259c3SDiego Giagio 	struct net_device *netdev;
437a19259c3SDiego Giagio 	int i;
438a19259c3SDiego Giagio 	int retval;
439a19259c3SDiego Giagio 
440a19259c3SDiego Giagio 	netdev = alloc_etherdev(sizeof(struct ipheth_device));
441a19259c3SDiego Giagio 	if (!netdev)
442a19259c3SDiego Giagio 		return -ENOMEM;
443a19259c3SDiego Giagio 
444a19259c3SDiego Giagio 	netdev->netdev_ops = &ipheth_netdev_ops;
445a19259c3SDiego Giagio 	netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
446*493c3ca6SLen Baker 	strscpy(netdev->name, "eth%d", sizeof(netdev->name));
447a19259c3SDiego Giagio 
448a19259c3SDiego Giagio 	dev = netdev_priv(netdev);
449a19259c3SDiego Giagio 	dev->udev = udev;
450a19259c3SDiego Giagio 	dev->net = netdev;
451a19259c3SDiego Giagio 	dev->intf = intf;
452bb1b40c7SAlexander Kappner 	dev->confirmed_pairing = false;
453a19259c3SDiego Giagio 	/* Set up endpoints */
454a19259c3SDiego Giagio 	hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
455a19259c3SDiego Giagio 	if (hintf == NULL) {
456a19259c3SDiego Giagio 		retval = -ENODEV;
457495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "Unable to find alternate settings interface\n");
458a19259c3SDiego Giagio 		goto err_endpoints;
459a19259c3SDiego Giagio 	}
460a19259c3SDiego Giagio 
461a19259c3SDiego Giagio 	for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
462a19259c3SDiego Giagio 		endp = &hintf->endpoint[i].desc;
463a19259c3SDiego Giagio 		if (usb_endpoint_is_bulk_in(endp))
464a19259c3SDiego Giagio 			dev->bulk_in = endp->bEndpointAddress;
465a19259c3SDiego Giagio 		else if (usb_endpoint_is_bulk_out(endp))
466a19259c3SDiego Giagio 			dev->bulk_out = endp->bEndpointAddress;
467a19259c3SDiego Giagio 	}
468a19259c3SDiego Giagio 	if (!(dev->bulk_in && dev->bulk_out)) {
469a19259c3SDiego Giagio 		retval = -ENODEV;
470495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "Unable to find endpoints\n");
471a19259c3SDiego Giagio 		goto err_endpoints;
472a19259c3SDiego Giagio 	}
473a19259c3SDiego Giagio 
474a19259c3SDiego Giagio 	dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
475a19259c3SDiego Giagio 	if (dev->ctrl_buf == NULL) {
476a19259c3SDiego Giagio 		retval = -ENOMEM;
477a19259c3SDiego Giagio 		goto err_alloc_ctrl_buf;
478a19259c3SDiego Giagio 	}
479a19259c3SDiego Giagio 
480a19259c3SDiego Giagio 	retval = ipheth_get_macaddr(dev);
481a19259c3SDiego Giagio 	if (retval)
482a19259c3SDiego Giagio 		goto err_get_macaddr;
483a19259c3SDiego Giagio 
484a19259c3SDiego Giagio 	INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
485a19259c3SDiego Giagio 
486a19259c3SDiego Giagio 	retval = ipheth_alloc_urbs(dev);
487a19259c3SDiego Giagio 	if (retval) {
488495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
489a19259c3SDiego Giagio 		goto err_alloc_urbs;
490a19259c3SDiego Giagio 	}
491a19259c3SDiego Giagio 
492a19259c3SDiego Giagio 	usb_set_intfdata(intf, dev);
493a19259c3SDiego Giagio 
494a19259c3SDiego Giagio 	SET_NETDEV_DEV(netdev, &intf->dev);
4957ad24ea4SWilfried Klaebe 	netdev->ethtool_ops = &ops;
496a19259c3SDiego Giagio 
497a19259c3SDiego Giagio 	retval = register_netdev(netdev);
498a19259c3SDiego Giagio 	if (retval) {
499495f71e2SGreg Kroah-Hartman 		dev_err(&intf->dev, "error registering netdev: %d\n", retval);
500a19259c3SDiego Giagio 		retval = -EIO;
501a19259c3SDiego Giagio 		goto err_register_netdev;
502a19259c3SDiego Giagio 	}
503bb1b40c7SAlexander Kappner 	// carrier down and transmit queues stopped until packet from device
504bb1b40c7SAlexander Kappner 	netif_carrier_off(netdev);
505bb1b40c7SAlexander Kappner 	netif_tx_stop_all_queues(netdev);
506a19259c3SDiego Giagio 	dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
507a19259c3SDiego Giagio 	return 0;
508a19259c3SDiego Giagio 
509a19259c3SDiego Giagio err_register_netdev:
510a19259c3SDiego Giagio 	ipheth_free_urbs(dev);
511a19259c3SDiego Giagio err_alloc_urbs:
512a19259c3SDiego Giagio err_get_macaddr:
513a19259c3SDiego Giagio err_alloc_ctrl_buf:
514a19259c3SDiego Giagio 	kfree(dev->ctrl_buf);
515a19259c3SDiego Giagio err_endpoints:
516a19259c3SDiego Giagio 	free_netdev(netdev);
517a19259c3SDiego Giagio 	return retval;
518a19259c3SDiego Giagio }
519a19259c3SDiego Giagio 
520a19259c3SDiego Giagio static void ipheth_disconnect(struct usb_interface *intf)
521a19259c3SDiego Giagio {
522a19259c3SDiego Giagio 	struct ipheth_device *dev;
523a19259c3SDiego Giagio 
524a19259c3SDiego Giagio 	dev = usb_get_intfdata(intf);
525a19259c3SDiego Giagio 	if (dev != NULL) {
526a19259c3SDiego Giagio 		unregister_netdev(dev->net);
527a19259c3SDiego Giagio 		ipheth_kill_urbs(dev);
528a19259c3SDiego Giagio 		ipheth_free_urbs(dev);
529a19259c3SDiego Giagio 		kfree(dev->ctrl_buf);
530a19259c3SDiego Giagio 		free_netdev(dev->net);
531a19259c3SDiego Giagio 	}
532a19259c3SDiego Giagio 	usb_set_intfdata(intf, NULL);
533a19259c3SDiego Giagio 	dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
534a19259c3SDiego Giagio }
535a19259c3SDiego Giagio 
536a19259c3SDiego Giagio static struct usb_driver ipheth_driver = {
537a19259c3SDiego Giagio 	.name =		"ipheth",
538a19259c3SDiego Giagio 	.probe =	ipheth_probe,
539a19259c3SDiego Giagio 	.disconnect =	ipheth_disconnect,
540a19259c3SDiego Giagio 	.id_table =	ipheth_table,
541e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
542a19259c3SDiego Giagio };
543a19259c3SDiego Giagio 
544d632eb1bSGreg Kroah-Hartman module_usb_driver(ipheth_driver);
545a19259c3SDiego Giagio 
546a19259c3SDiego Giagio MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
547a19259c3SDiego Giagio MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
548a19259c3SDiego Giagio MODULE_LICENSE("Dual BSD/GPL");
549