xref: /linux/drivers/net/usb/ipheth.c (revision 8cb081746c031fb164089322e2336a0bf5b3070c)
1 /*
2  * ipheth.c - Apple iPhone USB Ethernet driver
3  *
4  * Copyright (c) 2009 Diego Giagio <diego@giagio.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of GIAGIO.COM nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  *
41  * Attention: iPhone device must be paired, otherwise it won't respond to our
42  * driver. For more info: http://giagio.com/wiki/moin.cgi/iPhoneEthernetDriver
43  *
44  */
45 
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
49 #include <linux/module.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/ethtool.h>
53 #include <linux/usb.h>
54 #include <linux/workqueue.h>
55 
56 #define USB_VENDOR_APPLE        0x05ac
57 
58 #define IPHETH_USBINTF_CLASS    255
59 #define IPHETH_USBINTF_SUBCLASS 253
60 #define IPHETH_USBINTF_PROTO    1
61 
62 #define IPHETH_BUF_SIZE         1516
63 #define IPHETH_IP_ALIGN		2	/* padding at front of URB */
64 #define IPHETH_TX_TIMEOUT       (5 * HZ)
65 
66 #define IPHETH_INTFNUM          2
67 #define IPHETH_ALT_INTFNUM      1
68 
69 #define IPHETH_CTRL_ENDP        0x00
70 #define IPHETH_CTRL_BUF_SIZE    0x40
71 #define IPHETH_CTRL_TIMEOUT     (5 * HZ)
72 
73 #define IPHETH_CMD_GET_MACADDR   0x00
74 #define IPHETH_CMD_CARRIER_CHECK 0x45
75 
76 #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
77 #define IPHETH_CARRIER_ON       0x04
78 
79 static const struct usb_device_id ipheth_table[] = {
80 	{ USB_VENDOR_AND_INTERFACE_INFO(USB_VENDOR_APPLE, IPHETH_USBINTF_CLASS,
81 					IPHETH_USBINTF_SUBCLASS,
82 					IPHETH_USBINTF_PROTO) },
83 	{ }
84 };
85 MODULE_DEVICE_TABLE(usb, ipheth_table);
86 
87 struct ipheth_device {
88 	struct usb_device *udev;
89 	struct usb_interface *intf;
90 	struct net_device *net;
91 	struct urb *tx_urb;
92 	struct urb *rx_urb;
93 	unsigned char *tx_buf;
94 	unsigned char *rx_buf;
95 	unsigned char *ctrl_buf;
96 	u8 bulk_in;
97 	u8 bulk_out;
98 	struct delayed_work carrier_work;
99 	bool confirmed_pairing;
100 };
101 
102 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
103 
104 static int ipheth_alloc_urbs(struct ipheth_device *iphone)
105 {
106 	struct urb *tx_urb = NULL;
107 	struct urb *rx_urb = NULL;
108 	u8 *tx_buf = NULL;
109 	u8 *rx_buf = NULL;
110 
111 	tx_urb = usb_alloc_urb(0, GFP_KERNEL);
112 	if (tx_urb == NULL)
113 		goto error_nomem;
114 
115 	rx_urb = usb_alloc_urb(0, GFP_KERNEL);
116 	if (rx_urb == NULL)
117 		goto free_tx_urb;
118 
119 	tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
120 				    GFP_KERNEL, &tx_urb->transfer_dma);
121 	if (tx_buf == NULL)
122 		goto free_rx_urb;
123 
124 	rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
125 				    GFP_KERNEL, &rx_urb->transfer_dma);
126 	if (rx_buf == NULL)
127 		goto free_tx_buf;
128 
129 
130 	iphone->tx_urb = tx_urb;
131 	iphone->rx_urb = rx_urb;
132 	iphone->tx_buf = tx_buf;
133 	iphone->rx_buf = rx_buf;
134 	return 0;
135 
136 free_tx_buf:
137 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf,
138 			  tx_urb->transfer_dma);
139 free_rx_urb:
140 	usb_free_urb(rx_urb);
141 free_tx_urb:
142 	usb_free_urb(tx_urb);
143 error_nomem:
144 	return -ENOMEM;
145 }
146 
147 static void ipheth_free_urbs(struct ipheth_device *iphone)
148 {
149 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->rx_buf,
150 			  iphone->rx_urb->transfer_dma);
151 	usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf,
152 			  iphone->tx_urb->transfer_dma);
153 	usb_free_urb(iphone->rx_urb);
154 	usb_free_urb(iphone->tx_urb);
155 }
156 
157 static void ipheth_kill_urbs(struct ipheth_device *dev)
158 {
159 	usb_kill_urb(dev->tx_urb);
160 	usb_kill_urb(dev->rx_urb);
161 }
162 
163 static void ipheth_rcvbulk_callback(struct urb *urb)
164 {
165 	struct ipheth_device *dev;
166 	struct sk_buff *skb;
167 	int status;
168 	char *buf;
169 	int len;
170 
171 	dev = urb->context;
172 	if (dev == NULL)
173 		return;
174 
175 	status = urb->status;
176 	switch (status) {
177 	case -ENOENT:
178 	case -ECONNRESET:
179 	case -ESHUTDOWN:
180 	case -EPROTO:
181 		return;
182 	case 0:
183 		break;
184 	default:
185 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
186 			__func__, status);
187 		return;
188 	}
189 
190 	if (urb->actual_length <= IPHETH_IP_ALIGN) {
191 		dev->net->stats.rx_length_errors++;
192 		return;
193 	}
194 	len = urb->actual_length - IPHETH_IP_ALIGN;
195 	buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
196 
197 	skb = dev_alloc_skb(len);
198 	if (!skb) {
199 		dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n",
200 			__func__);
201 		dev->net->stats.rx_dropped++;
202 		return;
203 	}
204 
205 	skb_put_data(skb, buf, len);
206 	skb->dev = dev->net;
207 	skb->protocol = eth_type_trans(skb, dev->net);
208 
209 	dev->net->stats.rx_packets++;
210 	dev->net->stats.rx_bytes += len;
211 	dev->confirmed_pairing = true;
212 	netif_rx(skb);
213 	ipheth_rx_submit(dev, GFP_ATOMIC);
214 }
215 
216 static void ipheth_sndbulk_callback(struct urb *urb)
217 {
218 	struct ipheth_device *dev;
219 	int status = urb->status;
220 
221 	dev = urb->context;
222 	if (dev == NULL)
223 		return;
224 
225 	if (status != 0 &&
226 	    status != -ENOENT &&
227 	    status != -ECONNRESET &&
228 	    status != -ESHUTDOWN)
229 		dev_err(&dev->intf->dev, "%s: urb status: %d\n",
230 		__func__, status);
231 
232 	if (status == 0)
233 		netif_wake_queue(dev->net);
234 	else
235 		// on URB error, trigger immediate poll
236 		schedule_delayed_work(&dev->carrier_work, 0);
237 }
238 
239 static int ipheth_carrier_set(struct ipheth_device *dev)
240 {
241 	struct usb_device *udev;
242 	int retval;
243 
244 	if (!dev)
245 		return 0;
246 	if (!dev->confirmed_pairing)
247 		return 0;
248 
249 	udev = dev->udev;
250 	retval = usb_control_msg(udev,
251 			usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
252 			IPHETH_CMD_CARRIER_CHECK, /* request */
253 			0xc0, /* request type */
254 			0x00, /* value */
255 			0x02, /* index */
256 			dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
257 			IPHETH_CTRL_TIMEOUT);
258 	if (retval < 0) {
259 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
260 			__func__, retval);
261 		return retval;
262 	}
263 
264 	if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) {
265 		netif_carrier_on(dev->net);
266 		if (dev->tx_urb->status != -EINPROGRESS)
267 			netif_wake_queue(dev->net);
268 	} else {
269 		netif_carrier_off(dev->net);
270 		netif_stop_queue(dev->net);
271 	}
272 	return 0;
273 }
274 
275 static void ipheth_carrier_check_work(struct work_struct *work)
276 {
277 	struct ipheth_device *dev = container_of(work, struct ipheth_device,
278 						 carrier_work.work);
279 
280 	ipheth_carrier_set(dev);
281 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
282 }
283 
284 static int ipheth_get_macaddr(struct ipheth_device *dev)
285 {
286 	struct usb_device *udev = dev->udev;
287 	struct net_device *net = dev->net;
288 	int retval;
289 
290 	retval = usb_control_msg(udev,
291 				 usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
292 				 IPHETH_CMD_GET_MACADDR, /* request */
293 				 0xc0, /* request type */
294 				 0x00, /* value */
295 				 0x02, /* index */
296 				 dev->ctrl_buf,
297 				 IPHETH_CTRL_BUF_SIZE,
298 				 IPHETH_CTRL_TIMEOUT);
299 	if (retval < 0) {
300 		dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
301 			__func__, retval);
302 	} else if (retval < ETH_ALEN) {
303 		dev_err(&dev->intf->dev,
304 			"%s: usb_control_msg: short packet: %d bytes\n",
305 			__func__, retval);
306 		retval = -EINVAL;
307 	} else {
308 		memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN);
309 		retval = 0;
310 	}
311 
312 	return retval;
313 }
314 
315 static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
316 {
317 	struct usb_device *udev = dev->udev;
318 	int retval;
319 
320 	usb_fill_bulk_urb(dev->rx_urb, udev,
321 			  usb_rcvbulkpipe(udev, dev->bulk_in),
322 			  dev->rx_buf, IPHETH_BUF_SIZE,
323 			  ipheth_rcvbulk_callback,
324 			  dev);
325 	dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
326 
327 	retval = usb_submit_urb(dev->rx_urb, mem_flags);
328 	if (retval)
329 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
330 			__func__, retval);
331 	return retval;
332 }
333 
334 static int ipheth_open(struct net_device *net)
335 {
336 	struct ipheth_device *dev = netdev_priv(net);
337 	struct usb_device *udev = dev->udev;
338 	int retval = 0;
339 
340 	usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
341 
342 	retval = ipheth_carrier_set(dev);
343 	if (retval)
344 		return retval;
345 
346 	retval = ipheth_rx_submit(dev, GFP_KERNEL);
347 	if (retval)
348 		return retval;
349 
350 	schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
351 	return retval;
352 }
353 
354 static int ipheth_close(struct net_device *net)
355 {
356 	struct ipheth_device *dev = netdev_priv(net);
357 
358 	cancel_delayed_work_sync(&dev->carrier_work);
359 	netif_stop_queue(net);
360 	return 0;
361 }
362 
363 static int ipheth_tx(struct sk_buff *skb, struct net_device *net)
364 {
365 	struct ipheth_device *dev = netdev_priv(net);
366 	struct usb_device *udev = dev->udev;
367 	int retval;
368 
369 	/* Paranoid */
370 	if (skb->len > IPHETH_BUF_SIZE) {
371 		WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
372 		dev->net->stats.tx_dropped++;
373 		dev_kfree_skb_any(skb);
374 		return NETDEV_TX_OK;
375 	}
376 
377 	memcpy(dev->tx_buf, skb->data, skb->len);
378 	if (skb->len < IPHETH_BUF_SIZE)
379 		memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len);
380 
381 	usb_fill_bulk_urb(dev->tx_urb, udev,
382 			  usb_sndbulkpipe(udev, dev->bulk_out),
383 			  dev->tx_buf, IPHETH_BUF_SIZE,
384 			  ipheth_sndbulk_callback,
385 			  dev);
386 	dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
387 
388 	retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
389 	if (retval) {
390 		dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
391 			__func__, retval);
392 		dev->net->stats.tx_errors++;
393 		dev_kfree_skb_any(skb);
394 	} else {
395 		dev->net->stats.tx_packets++;
396 		dev->net->stats.tx_bytes += skb->len;
397 		dev_consume_skb_any(skb);
398 		netif_stop_queue(net);
399 	}
400 
401 	return NETDEV_TX_OK;
402 }
403 
404 static void ipheth_tx_timeout(struct net_device *net)
405 {
406 	struct ipheth_device *dev = netdev_priv(net);
407 
408 	dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
409 	dev->net->stats.tx_errors++;
410 	usb_unlink_urb(dev->tx_urb);
411 }
412 
413 static u32 ipheth_ethtool_op_get_link(struct net_device *net)
414 {
415 	struct ipheth_device *dev = netdev_priv(net);
416 	return netif_carrier_ok(dev->net);
417 }
418 
419 static const struct ethtool_ops ops = {
420 	.get_link = ipheth_ethtool_op_get_link
421 };
422 
423 static const struct net_device_ops ipheth_netdev_ops = {
424 	.ndo_open = ipheth_open,
425 	.ndo_stop = ipheth_close,
426 	.ndo_start_xmit = ipheth_tx,
427 	.ndo_tx_timeout = ipheth_tx_timeout,
428 };
429 
430 static int ipheth_probe(struct usb_interface *intf,
431 			const struct usb_device_id *id)
432 {
433 	struct usb_device *udev = interface_to_usbdev(intf);
434 	struct usb_host_interface *hintf;
435 	struct usb_endpoint_descriptor *endp;
436 	struct ipheth_device *dev;
437 	struct net_device *netdev;
438 	int i;
439 	int retval;
440 
441 	netdev = alloc_etherdev(sizeof(struct ipheth_device));
442 	if (!netdev)
443 		return -ENOMEM;
444 
445 	netdev->netdev_ops = &ipheth_netdev_ops;
446 	netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
447 	strcpy(netdev->name, "eth%d");
448 
449 	dev = netdev_priv(netdev);
450 	dev->udev = udev;
451 	dev->net = netdev;
452 	dev->intf = intf;
453 	dev->confirmed_pairing = false;
454 	/* Set up endpoints */
455 	hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
456 	if (hintf == NULL) {
457 		retval = -ENODEV;
458 		dev_err(&intf->dev, "Unable to find alternate settings interface\n");
459 		goto err_endpoints;
460 	}
461 
462 	for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
463 		endp = &hintf->endpoint[i].desc;
464 		if (usb_endpoint_is_bulk_in(endp))
465 			dev->bulk_in = endp->bEndpointAddress;
466 		else if (usb_endpoint_is_bulk_out(endp))
467 			dev->bulk_out = endp->bEndpointAddress;
468 	}
469 	if (!(dev->bulk_in && dev->bulk_out)) {
470 		retval = -ENODEV;
471 		dev_err(&intf->dev, "Unable to find endpoints\n");
472 		goto err_endpoints;
473 	}
474 
475 	dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
476 	if (dev->ctrl_buf == NULL) {
477 		retval = -ENOMEM;
478 		goto err_alloc_ctrl_buf;
479 	}
480 
481 	retval = ipheth_get_macaddr(dev);
482 	if (retval)
483 		goto err_get_macaddr;
484 
485 	INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
486 
487 	retval = ipheth_alloc_urbs(dev);
488 	if (retval) {
489 		dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
490 		goto err_alloc_urbs;
491 	}
492 
493 	usb_set_intfdata(intf, dev);
494 
495 	SET_NETDEV_DEV(netdev, &intf->dev);
496 	netdev->ethtool_ops = &ops;
497 
498 	retval = register_netdev(netdev);
499 	if (retval) {
500 		dev_err(&intf->dev, "error registering netdev: %d\n", retval);
501 		retval = -EIO;
502 		goto err_register_netdev;
503 	}
504 	// carrier down and transmit queues stopped until packet from device
505 	netif_carrier_off(netdev);
506 	netif_tx_stop_all_queues(netdev);
507 	dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
508 	return 0;
509 
510 err_register_netdev:
511 	ipheth_free_urbs(dev);
512 err_alloc_urbs:
513 err_get_macaddr:
514 err_alloc_ctrl_buf:
515 	kfree(dev->ctrl_buf);
516 err_endpoints:
517 	free_netdev(netdev);
518 	return retval;
519 }
520 
521 static void ipheth_disconnect(struct usb_interface *intf)
522 {
523 	struct ipheth_device *dev;
524 
525 	dev = usb_get_intfdata(intf);
526 	if (dev != NULL) {
527 		unregister_netdev(dev->net);
528 		ipheth_kill_urbs(dev);
529 		ipheth_free_urbs(dev);
530 		kfree(dev->ctrl_buf);
531 		free_netdev(dev->net);
532 	}
533 	usb_set_intfdata(intf, NULL);
534 	dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
535 }
536 
537 static struct usb_driver ipheth_driver = {
538 	.name =		"ipheth",
539 	.probe =	ipheth_probe,
540 	.disconnect =	ipheth_disconnect,
541 	.id_table =	ipheth_table,
542 	.disable_hub_initiated_lpm = 1,
543 };
544 
545 module_usb_driver(ipheth_driver);
546 
547 MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
548 MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
549 MODULE_LICENSE("Dual BSD/GPL");
550