1/*-
2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 2009 Diego Giagio. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Thanks to Diego Giagio for figuring out the programming details for
29 * the Apple iPhone Ethernet driver.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/stdint.h>
36#include <sys/stddef.h>
37#include <sys/param.h>
38#include <sys/queue.h>
39#include <sys/types.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/bus.h>
43#include <sys/module.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/condvar.h>
47#include <sys/sysctl.h>
48#include <sys/sx.h>
49#include <sys/unistd.h>
50#include <sys/callout.h>
51#include <sys/malloc.h>
52#include <sys/priv.h>
53
54#include <dev/usb/usb.h>
55#include <dev/usb/usbdi.h>
56#include <dev/usb/usbdi_util.h>
57#include "usbdevs.h"
58
59#define	USB_DEBUG_VAR ipheth_debug
60#include <dev/usb/usb_debug.h>
61#include <dev/usb/usb_process.h>
62
63#include <dev/usb/net/usb_ethernet.h>
64#include <dev/usb/net/if_iphethvar.h>
65
66static device_probe_t ipheth_probe;
67static device_attach_t ipheth_attach;
68static device_detach_t ipheth_detach;
69
70static usb_callback_t ipheth_bulk_write_callback;
71static usb_callback_t ipheth_bulk_read_callback;
72
73static uether_fn_t ipheth_attach_post;
74static uether_fn_t ipheth_tick;
75static uether_fn_t ipheth_init;
76static uether_fn_t ipheth_stop;
77static uether_fn_t ipheth_start;
78static uether_fn_t ipheth_setmulti;
79static uether_fn_t ipheth_setpromisc;
80
81#ifdef USB_DEBUG
82static int ipheth_debug = 0;
83
84static SYSCTL_NODE(_hw_usb, OID_AUTO, ipheth, CTLFLAG_RW, 0, "USB iPhone ethernet");
85SYSCTL_INT(_hw_usb_ipheth, OID_AUTO, debug, CTLFLAG_RW, &ipheth_debug, 0, "Debug level");
86#endif
87
88static const struct usb_config ipheth_config[IPHETH_N_TRANSFER] = {
89
90	[IPHETH_BULK_RX] = {
91		.type = UE_BULK,
92		.endpoint = UE_ADDR_ANY,
93		.direction = UE_DIR_RX,
94		.frames = IPHETH_RX_FRAMES_MAX,
95		.bufsize = (IPHETH_RX_FRAMES_MAX * MCLBYTES),
96		.flags = {.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
97		.callback = ipheth_bulk_read_callback,
98		.timeout = 0,		/* no timeout */
99	},
100
101	[IPHETH_BULK_TX] = {
102		.type = UE_BULK,
103		.endpoint = UE_ADDR_ANY,
104		.direction = UE_DIR_TX,
105		.frames = IPHETH_TX_FRAMES_MAX,
106		.bufsize = (IPHETH_TX_FRAMES_MAX * IPHETH_BUF_SIZE),
107		.flags = {.force_short_xfer = 1,},
108		.callback = ipheth_bulk_write_callback,
109		.timeout = IPHETH_TX_TIMEOUT,
110	},
111};
112
113static device_method_t ipheth_methods[] = {
114	/* Device interface */
115	DEVMETHOD(device_probe, ipheth_probe),
116	DEVMETHOD(device_attach, ipheth_attach),
117	DEVMETHOD(device_detach, ipheth_detach),
118
119	DEVMETHOD_END
120};
121
122static driver_t ipheth_driver = {
123	.name = "ipheth",
124	.methods = ipheth_methods,
125	.size = sizeof(struct ipheth_softc),
126};
127
128static devclass_t ipheth_devclass;
129
130DRIVER_MODULE(ipheth, uhub, ipheth_driver, ipheth_devclass, NULL, 0);
131MODULE_VERSION(ipheth, 1);
132MODULE_DEPEND(ipheth, uether, 1, 1, 1);
133MODULE_DEPEND(ipheth, usb, 1, 1, 1);
134MODULE_DEPEND(ipheth, ether, 1, 1, 1);
135
136static const struct usb_ether_methods ipheth_ue_methods = {
137	.ue_attach_post = ipheth_attach_post,
138	.ue_start = ipheth_start,
139	.ue_init = ipheth_init,
140	.ue_tick = ipheth_tick,
141	.ue_stop = ipheth_stop,
142	.ue_setmulti = ipheth_setmulti,
143	.ue_setpromisc = ipheth_setpromisc,
144};
145
146#define	IPHETH_ID(v,p,c,sc,pt) \
147    USB_VENDOR(v), USB_PRODUCT(p), \
148    USB_IFACE_CLASS(c), USB_IFACE_SUBCLASS(sc), \
149    USB_IFACE_PROTOCOL(pt)
150
151static const STRUCT_USB_HOST_ID ipheth_devs[] = {
152#if 0
153	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE,
154	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
155	    IPHETH_USBINTF_PROTO)},
156	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G,
157	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
158	    IPHETH_USBINTF_PROTO)},
159	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS,
160	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
161	    IPHETH_USBINTF_PROTO)},
162	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4,
163	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
164	    IPHETH_USBINTF_PROTO)},
165	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S,
166	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
167	    IPHETH_USBINTF_PROTO)},
168	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_5,
169	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
170	    IPHETH_USBINTF_PROTO)},
171#else
172	/* product agnostic interface match */
173	{USB_VENDOR(USB_VENDOR_APPLE),
174	 USB_IFACE_CLASS(IPHETH_USBINTF_CLASS),
175	 USB_IFACE_SUBCLASS(IPHETH_USBINTF_SUBCLASS),
176	 USB_IFACE_PROTOCOL(IPHETH_USBINTF_PROTO)},
177#endif
178};
179
180static int
181ipheth_get_mac_addr(struct ipheth_softc *sc)
182{
183	struct usb_device_request req;
184	int error;
185
186	req.bmRequestType = UT_READ_VENDOR_DEVICE;
187	req.bRequest = IPHETH_CMD_GET_MACADDR;
188	req.wValue[0] = 0;
189	req.wValue[1] = 0;
190	req.wIndex[0] = sc->sc_iface_no;
191	req.wIndex[1] = 0;
192	req.wLength[0] = ETHER_ADDR_LEN;
193	req.wLength[1] = 0;
194
195	error = usbd_do_request(sc->sc_ue.ue_udev, NULL, &req, sc->sc_data);
196
197	if (error)
198		return (error);
199
200	memcpy(sc->sc_ue.ue_eaddr, sc->sc_data, ETHER_ADDR_LEN);
201
202	return (0);
203}
204
205static int
206ipheth_probe(device_t dev)
207{
208	struct usb_attach_arg *uaa = device_get_ivars(dev);
209
210	if (uaa->usb_mode != USB_MODE_HOST)
211		return (ENXIO);
212
213	return (usbd_lookup_id_by_uaa(ipheth_devs, sizeof(ipheth_devs), uaa));
214}
215
216static int
217ipheth_attach(device_t dev)
218{
219	struct ipheth_softc *sc = device_get_softc(dev);
220	struct usb_ether *ue = &sc->sc_ue;
221	struct usb_attach_arg *uaa = device_get_ivars(dev);
222	int error;
223
224	sc->sc_iface_no = uaa->info.bIfaceIndex;
225
226	device_set_usb_desc(dev);
227
228	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
229
230	error = usbd_set_alt_interface_index(uaa->device,
231	    uaa->info.bIfaceIndex, IPHETH_ALT_INTFNUM);
232	if (error) {
233		device_printf(dev, "Cannot set alternate setting\n");
234		goto detach;
235	}
236	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_no,
237	    sc->sc_xfer, ipheth_config, IPHETH_N_TRANSFER, sc, &sc->sc_mtx);
238	if (error) {
239		device_printf(dev, "Cannot setup USB transfers\n");
240		goto detach;
241	}
242	ue->ue_sc = sc;
243	ue->ue_dev = dev;
244	ue->ue_udev = uaa->device;
245	ue->ue_mtx = &sc->sc_mtx;
246	ue->ue_methods = &ipheth_ue_methods;
247
248	error = ipheth_get_mac_addr(sc);
249	if (error) {
250		device_printf(dev, "Cannot get MAC address\n");
251		goto detach;
252	}
253
254	error = uether_ifattach(ue);
255	if (error) {
256		device_printf(dev, "could not attach interface\n");
257		goto detach;
258	}
259	return (0);			/* success */
260
261detach:
262	ipheth_detach(dev);
263	return (ENXIO);			/* failure */
264}
265
266static int
267ipheth_detach(device_t dev)
268{
269	struct ipheth_softc *sc = device_get_softc(dev);
270	struct usb_ether *ue = &sc->sc_ue;
271
272	/* stop all USB transfers first */
273	usbd_transfer_unsetup(sc->sc_xfer, IPHETH_N_TRANSFER);
274
275	uether_ifdetach(ue);
276
277	mtx_destroy(&sc->sc_mtx);
278
279	return (0);
280}
281
282static void
283ipheth_start(struct usb_ether *ue)
284{
285	struct ipheth_softc *sc = uether_getsc(ue);
286
287	/*
288	 * Start the USB transfers, if not already started:
289	 */
290	usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_TX]);
291	usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_RX]);
292}
293
294static void
295ipheth_stop(struct usb_ether *ue)
296{
297	struct ipheth_softc *sc = uether_getsc(ue);
298
299	/*
300	 * Stop the USB transfers, if not already stopped:
301	 */
302	usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_TX]);
303	usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_RX]);
304}
305
306static void
307ipheth_tick(struct usb_ether *ue)
308{
309	struct ipheth_softc *sc = uether_getsc(ue);
310	struct usb_device_request req;
311	int error;
312
313	req.bmRequestType = UT_READ_VENDOR_DEVICE;
314	req.bRequest = IPHETH_CMD_CARRIER_CHECK;
315	req.wValue[0] = 0;
316	req.wValue[1] = 0;
317	req.wIndex[0] = sc->sc_iface_no;
318	req.wIndex[1] = 0;
319	req.wLength[0] = IPHETH_CTRL_BUF_SIZE;
320	req.wLength[1] = 0;
321
322	error = uether_do_request(ue, &req, sc->sc_data, IPHETH_CTRL_TIMEOUT);
323
324	if (error)
325		return;
326
327	sc->sc_carrier_on =
328	    (sc->sc_data[0] == IPHETH_CARRIER_ON);
329}
330
331static void
332ipheth_attach_post(struct usb_ether *ue)
333{
334
335}
336
337static void
338ipheth_init(struct usb_ether *ue)
339{
340	struct ipheth_softc *sc = uether_getsc(ue);
341	struct ifnet *ifp = uether_getifp(ue);
342
343	IPHETH_LOCK_ASSERT(sc, MA_OWNED);
344
345	ifp->if_drv_flags |= IFF_DRV_RUNNING;
346
347	/* stall data write direction, which depends on USB mode */
348	usbd_xfer_set_stall(sc->sc_xfer[IPHETH_BULK_TX]);
349
350	/* start data transfers */
351	ipheth_start(ue);
352}
353
354static void
355ipheth_setmulti(struct usb_ether *ue)
356{
357
358}
359
360static void
361ipheth_setpromisc(struct usb_ether *ue)
362{
363
364}
365
366static void
367ipheth_free_queue(struct mbuf **ppm, uint8_t n)
368{
369	uint8_t x;
370
371	for (x = 0; x != n; x++) {
372		if (ppm[x] != NULL) {
373			m_freem(ppm[x]);
374			ppm[x] = NULL;
375		}
376	}
377}
378
379static void
380ipheth_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
381{
382	struct ipheth_softc *sc = usbd_xfer_softc(xfer);
383	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
384	struct usb_page_cache *pc;
385	struct mbuf *m;
386	uint8_t x;
387	int actlen;
388	int aframes;
389
390	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
391
392	DPRINTFN(1, "\n");
393
394	switch (USB_GET_STATE(xfer)) {
395	case USB_ST_TRANSFERRED:
396		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
397		    actlen, aframes);
398
399		ifp->if_opackets++;
400
401		/* free all previous TX buffers */
402		ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
403
404		/* FALLTHROUGH */
405	case USB_ST_SETUP:
406tr_setup:
407		for (x = 0; x != IPHETH_TX_FRAMES_MAX; x++) {
408
409			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
410
411			if (m == NULL)
412				break;
413
414			usbd_xfer_set_frame_offset(xfer,
415			    x * IPHETH_BUF_SIZE, x);
416
417			pc = usbd_xfer_get_frame(xfer, x);
418
419			sc->sc_tx_buf[x] = m;
420
421			if (m->m_pkthdr.len > IPHETH_BUF_SIZE)
422				m->m_pkthdr.len = IPHETH_BUF_SIZE;
423
424			usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
425
426			usbd_xfer_set_frame_len(xfer, x, IPHETH_BUF_SIZE);
427
428			if (IPHETH_BUF_SIZE != m->m_pkthdr.len) {
429				usbd_frame_zero(pc, m->m_pkthdr.len,
430					IPHETH_BUF_SIZE - m->m_pkthdr.len);
431			}
432
433			/*
434			 * If there's a BPF listener, bounce a copy of
435			 * this frame to him:
436			 */
437			BPF_MTAP(ifp, m);
438		}
439		if (x != 0) {
440			usbd_xfer_set_frames(xfer, x);
441
442			usbd_transfer_submit(xfer);
443		}
444		break;
445
446	default:			/* Error */
447		DPRINTFN(11, "transfer error, %s\n",
448		    usbd_errstr(error));
449
450		/* free all previous TX buffers */
451		ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
452
453		/* count output errors */
454		ifp->if_oerrors++;
455
456		if (error != USB_ERR_CANCELLED) {
457			/* try to clear stall first */
458			usbd_xfer_set_stall(xfer);
459			goto tr_setup;
460		}
461		break;
462	}
463}
464
465static void
466ipheth_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
467{
468	struct ipheth_softc *sc = usbd_xfer_softc(xfer);
469	struct mbuf *m;
470	uint8_t x;
471	int actlen;
472	int aframes;
473	int len;
474
475	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
476
477	switch (USB_GET_STATE(xfer)) {
478	case USB_ST_TRANSFERRED:
479
480		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
481
482		for (x = 0; x != aframes; x++) {
483
484			m = sc->sc_rx_buf[x];
485			sc->sc_rx_buf[x] = NULL;
486			len = usbd_xfer_frame_len(xfer, x);
487
488			if (len < (int)(sizeof(struct ether_header) +
489			    IPHETH_RX_ADJ)) {
490				m_freem(m);
491				continue;
492			}
493
494			m_adj(m, IPHETH_RX_ADJ);
495
496			/* queue up mbuf */
497			uether_rxmbuf(&sc->sc_ue, m, len - IPHETH_RX_ADJ);
498		}
499
500		/* FALLTHROUGH */
501	case USB_ST_SETUP:
502
503		for (x = 0; x != IPHETH_RX_FRAMES_MAX; x++) {
504			if (sc->sc_rx_buf[x] == NULL) {
505				m = uether_newbuf();
506				if (m == NULL)
507					goto tr_stall;
508
509				/* cancel alignment for ethernet */
510				m_adj(m, ETHER_ALIGN);
511
512				sc->sc_rx_buf[x] = m;
513			} else {
514				m = sc->sc_rx_buf[x];
515			}
516
517			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
518		}
519		/* set number of frames and start hardware */
520		usbd_xfer_set_frames(xfer, x);
521		usbd_transfer_submit(xfer);
522		/* flush any received frames */
523		uether_rxflush(&sc->sc_ue);
524		break;
525
526	default:			/* Error */
527		DPRINTF("error = %s\n", usbd_errstr(error));
528
529		if (error != USB_ERR_CANCELLED) {
530	tr_stall:
531			/* try to clear stall first */
532			usbd_xfer_set_stall(xfer);
533			usbd_xfer_set_frames(xfer, 0);
534			usbd_transfer_submit(xfer);
535			break;
536		}
537		/* need to free the RX-mbufs when we are cancelled */
538		ipheth_free_queue(sc->sc_rx_buf, IPHETH_RX_FRAMES_MAX);
539		break;
540	}
541}
542