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