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