xref: /freebsd/sys/dev/usb/net/if_ipheth.c (revision 2b833162)
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 	[IPHETH_BULK_RX] = {
97 		.type = UE_BULK,
98 		.endpoint = UE_ADDR_ANY,
99 		.direction = UE_DIR_RX,
100 		.frames = IPHETH_RX_FRAMES_MAX,
101 		.bufsize = (IPHETH_RX_FRAMES_MAX * MCLBYTES),
102 		.flags = {.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
103 		.callback = ipheth_bulk_read_callback,
104 		.timeout = 0,		/* no timeout */
105 	},
106 
107 	[IPHETH_BULK_TX] = {
108 		.type = UE_BULK,
109 		.endpoint = UE_ADDR_ANY,
110 		.direction = UE_DIR_TX,
111 		.frames = IPHETH_TX_FRAMES_MAX,
112 		.bufsize = (IPHETH_TX_FRAMES_MAX * IPHETH_BUF_SIZE),
113 		.flags = {.force_short_xfer = 1,},
114 		.callback = ipheth_bulk_write_callback,
115 		.timeout = IPHETH_TX_TIMEOUT,
116 	},
117 };
118 
119 static device_method_t ipheth_methods[] = {
120 	/* Device interface */
121 	DEVMETHOD(device_probe, ipheth_probe),
122 	DEVMETHOD(device_attach, ipheth_attach),
123 	DEVMETHOD(device_detach, ipheth_detach),
124 
125 	DEVMETHOD_END
126 };
127 
128 static driver_t ipheth_driver = {
129 	.name = "ipheth",
130 	.methods = ipheth_methods,
131 	.size = sizeof(struct ipheth_softc),
132 };
133 
134 static const STRUCT_USB_HOST_ID ipheth_devs[] = {
135 #if 0
136 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE,
137 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
138 	    IPHETH_USBINTF_PROTO)},
139 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G,
140 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
141 	    IPHETH_USBINTF_PROTO)},
142 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS,
143 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
144 	    IPHETH_USBINTF_PROTO)},
145 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4,
146 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
147 	    IPHETH_USBINTF_PROTO)},
148 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S,
149 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
150 	    IPHETH_USBINTF_PROTO)},
151 	{IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_5,
152 	    IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
153 	    IPHETH_USBINTF_PROTO)},
154 #else
155 	/* product agnostic interface match */
156 	{USB_VENDOR(USB_VENDOR_APPLE),
157 	 USB_IFACE_CLASS(IPHETH_USBINTF_CLASS),
158 	 USB_IFACE_SUBCLASS(IPHETH_USBINTF_SUBCLASS),
159 	 USB_IFACE_PROTOCOL(IPHETH_USBINTF_PROTO)},
160 #endif
161 };
162 
163 DRIVER_MODULE(ipheth, uhub, ipheth_driver, NULL, NULL);
164 MODULE_VERSION(ipheth, 1);
165 MODULE_DEPEND(ipheth, uether, 1, 1, 1);
166 MODULE_DEPEND(ipheth, usb, 1, 1, 1);
167 MODULE_DEPEND(ipheth, ether, 1, 1, 1);
168 USB_PNP_HOST_INFO(ipheth_devs);
169 
170 static const struct usb_ether_methods ipheth_ue_methods = {
171 	.ue_attach_post = ipheth_attach_post,
172 	.ue_start = ipheth_start,
173 	.ue_init = ipheth_init,
174 	.ue_tick = ipheth_tick,
175 	.ue_stop = ipheth_stop,
176 	.ue_setmulti = ipheth_setmulti,
177 	.ue_setpromisc = ipheth_setpromisc,
178 };
179 
180 #define	IPHETH_ID(v,p,c,sc,pt) \
181     USB_VENDOR(v), USB_PRODUCT(p), \
182     USB_IFACE_CLASS(c), USB_IFACE_SUBCLASS(sc), \
183     USB_IFACE_PROTOCOL(pt)
184 
185 static int
186 ipheth_get_mac_addr(struct ipheth_softc *sc)
187 {
188 	struct usb_device_request req;
189 	int error;
190 
191 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
192 	req.bRequest = IPHETH_CMD_GET_MACADDR;
193 	req.wValue[0] = 0;
194 	req.wValue[1] = 0;
195 	req.wIndex[0] = sc->sc_iface_no;
196 	req.wIndex[1] = 0;
197 	req.wLength[0] = ETHER_ADDR_LEN;
198 	req.wLength[1] = 0;
199 
200 	error = usbd_do_request(sc->sc_ue.ue_udev, NULL, &req, sc->sc_data);
201 
202 	if (error)
203 		return (error);
204 
205 	memcpy(sc->sc_ue.ue_eaddr, sc->sc_data, ETHER_ADDR_LEN);
206 
207 	return (0);
208 }
209 
210 static int
211 ipheth_probe(device_t dev)
212 {
213 	struct usb_attach_arg *uaa = device_get_ivars(dev);
214 
215 	if (uaa->usb_mode != USB_MODE_HOST)
216 		return (ENXIO);
217 
218 	return (usbd_lookup_id_by_uaa(ipheth_devs, sizeof(ipheth_devs), uaa));
219 }
220 
221 static int
222 ipheth_attach(device_t dev)
223 {
224 	struct ipheth_softc *sc = device_get_softc(dev);
225 	struct usb_ether *ue = &sc->sc_ue;
226 	struct usb_attach_arg *uaa = device_get_ivars(dev);
227 	int error;
228 
229 	sc->sc_iface_no = uaa->info.bIfaceIndex;
230 
231 	device_set_usb_desc(dev);
232 
233 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
234 
235 	error = usbd_set_alt_interface_index(uaa->device,
236 	    uaa->info.bIfaceIndex, IPHETH_ALT_INTFNUM);
237 	if (error) {
238 		device_printf(dev, "Cannot set alternate setting\n");
239 		goto detach;
240 	}
241 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_no,
242 	    sc->sc_xfer, ipheth_config, IPHETH_N_TRANSFER, sc, &sc->sc_mtx);
243 	if (error) {
244 		device_printf(dev, "Cannot setup USB transfers\n");
245 		goto detach;
246 	}
247 	ue->ue_sc = sc;
248 	ue->ue_dev = dev;
249 	ue->ue_udev = uaa->device;
250 	ue->ue_mtx = &sc->sc_mtx;
251 	ue->ue_methods = &ipheth_ue_methods;
252 
253 	error = ipheth_get_mac_addr(sc);
254 	if (error) {
255 		device_printf(dev, "Cannot get MAC address\n");
256 		goto detach;
257 	}
258 
259 	error = uether_ifattach(ue);
260 	if (error) {
261 		device_printf(dev, "could not attach interface\n");
262 		goto detach;
263 	}
264 	return (0);			/* success */
265 
266 detach:
267 	ipheth_detach(dev);
268 	return (ENXIO);			/* failure */
269 }
270 
271 static int
272 ipheth_detach(device_t dev)
273 {
274 	struct ipheth_softc *sc = device_get_softc(dev);
275 	struct usb_ether *ue = &sc->sc_ue;
276 
277 	/* stop all USB transfers first */
278 	usbd_transfer_unsetup(sc->sc_xfer, IPHETH_N_TRANSFER);
279 
280 	uether_ifdetach(ue);
281 
282 	mtx_destroy(&sc->sc_mtx);
283 
284 	return (0);
285 }
286 
287 static void
288 ipheth_start(struct usb_ether *ue)
289 {
290 	struct ipheth_softc *sc = uether_getsc(ue);
291 
292 	/*
293 	 * Start the USB transfers, if not already started:
294 	 */
295 	usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_TX]);
296 	usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_RX]);
297 }
298 
299 static void
300 ipheth_stop(struct usb_ether *ue)
301 {
302 	struct ipheth_softc *sc = uether_getsc(ue);
303 
304 	/*
305 	 * Stop the USB transfers, if not already stopped:
306 	 */
307 	usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_TX]);
308 	usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_RX]);
309 }
310 
311 static void
312 ipheth_tick(struct usb_ether *ue)
313 {
314 	struct ipheth_softc *sc = uether_getsc(ue);
315 	struct usb_device_request req;
316 	int error;
317 
318 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
319 	req.bRequest = IPHETH_CMD_CARRIER_CHECK;
320 	req.wValue[0] = 0;
321 	req.wValue[1] = 0;
322 	req.wIndex[0] = sc->sc_iface_no;
323 	req.wIndex[1] = 0;
324 	req.wLength[0] = IPHETH_CTRL_BUF_SIZE;
325 	req.wLength[1] = 0;
326 
327 	error = uether_do_request(ue, &req, sc->sc_data, IPHETH_CTRL_TIMEOUT);
328 
329 	if (error)
330 		return;
331 
332 	sc->sc_carrier_on =
333 	    (sc->sc_data[0] == IPHETH_CARRIER_ON);
334 }
335 
336 static void
337 ipheth_attach_post(struct usb_ether *ue)
338 {
339 
340 }
341 
342 static void
343 ipheth_init(struct usb_ether *ue)
344 {
345 	struct ipheth_softc *sc = uether_getsc(ue);
346 	if_t ifp = uether_getifp(ue);
347 
348 	IPHETH_LOCK_ASSERT(sc, MA_OWNED);
349 
350 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
351 
352 	/* stall data write direction, which depends on USB mode */
353 	usbd_xfer_set_stall(sc->sc_xfer[IPHETH_BULK_TX]);
354 
355 	/* start data transfers */
356 	ipheth_start(ue);
357 }
358 
359 static void
360 ipheth_setmulti(struct usb_ether *ue)
361 {
362 
363 }
364 
365 static void
366 ipheth_setpromisc(struct usb_ether *ue)
367 {
368 
369 }
370 
371 static void
372 ipheth_free_queue(struct mbuf **ppm, uint8_t n)
373 {
374 	uint8_t x;
375 
376 	for (x = 0; x != n; x++) {
377 		if (ppm[x] != NULL) {
378 			m_freem(ppm[x]);
379 			ppm[x] = NULL;
380 		}
381 	}
382 }
383 
384 static void
385 ipheth_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
386 {
387 	struct ipheth_softc *sc = usbd_xfer_softc(xfer);
388 	if_t ifp = uether_getifp(&sc->sc_ue);
389 	struct usb_page_cache *pc;
390 	struct mbuf *m;
391 	uint8_t x;
392 	int actlen;
393 	int aframes;
394 
395 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
396 
397 	DPRINTFN(1, "\n");
398 
399 	switch (USB_GET_STATE(xfer)) {
400 	case USB_ST_TRANSFERRED:
401 		DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
402 		    actlen, aframes);
403 
404 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
405 
406 		/* free all previous TX buffers */
407 		ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
408 
409 		/* FALLTHROUGH */
410 	case USB_ST_SETUP:
411 tr_setup:
412 		for (x = 0; x != IPHETH_TX_FRAMES_MAX; x++) {
413 			m = if_dequeue(ifp);
414 
415 			if (m == NULL)
416 				break;
417 
418 			usbd_xfer_set_frame_offset(xfer,
419 			    x * IPHETH_BUF_SIZE, x);
420 
421 			pc = usbd_xfer_get_frame(xfer, x);
422 
423 			sc->sc_tx_buf[x] = m;
424 
425 			if (m->m_pkthdr.len > IPHETH_BUF_SIZE)
426 				m->m_pkthdr.len = IPHETH_BUF_SIZE;
427 
428 			usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
429 
430 			usbd_xfer_set_frame_len(xfer, x, IPHETH_BUF_SIZE);
431 
432 			if (IPHETH_BUF_SIZE != m->m_pkthdr.len) {
433 				usbd_frame_zero(pc, m->m_pkthdr.len,
434 					IPHETH_BUF_SIZE - m->m_pkthdr.len);
435 			}
436 
437 			/*
438 			 * If there's a BPF listener, bounce a copy of
439 			 * this frame to him:
440 			 */
441 			BPF_MTAP(ifp, m);
442 		}
443 		if (x != 0) {
444 			usbd_xfer_set_frames(xfer, x);
445 
446 			usbd_transfer_submit(xfer);
447 		}
448 		break;
449 
450 	default:			/* Error */
451 		DPRINTFN(11, "transfer error, %s\n",
452 		    usbd_errstr(error));
453 
454 		/* free all previous TX buffers */
455 		ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
456 
457 		/* count output errors */
458 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
459 
460 		if (error != USB_ERR_CANCELLED) {
461 			/* try to clear stall first */
462 			usbd_xfer_set_stall(xfer);
463 			goto tr_setup;
464 		}
465 		break;
466 	}
467 }
468 
469 static void
470 ipheth_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
471 {
472 	struct ipheth_softc *sc = usbd_xfer_softc(xfer);
473 	struct mbuf *m;
474 	uint8_t x;
475 	int actlen;
476 	int aframes;
477 	int len;
478 
479 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
480 
481 	switch (USB_GET_STATE(xfer)) {
482 	case USB_ST_TRANSFERRED:
483 
484 		DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
485 
486 		for (x = 0; x != aframes; x++) {
487 			m = sc->sc_rx_buf[x];
488 			sc->sc_rx_buf[x] = NULL;
489 			len = usbd_xfer_frame_len(xfer, x);
490 
491 			if (len < (int)(sizeof(struct ether_header) +
492 			    IPHETH_RX_ADJ)) {
493 				m_freem(m);
494 				continue;
495 			}
496 
497 			m_adj(m, IPHETH_RX_ADJ);
498 
499 			/* queue up mbuf */
500 			uether_rxmbuf(&sc->sc_ue, m, len - IPHETH_RX_ADJ);
501 		}
502 
503 		/* FALLTHROUGH */
504 	case USB_ST_SETUP:
505 
506 		for (x = 0; x != IPHETH_RX_FRAMES_MAX; x++) {
507 			if (sc->sc_rx_buf[x] == NULL) {
508 				m = uether_newbuf();
509 				if (m == NULL)
510 					goto tr_stall;
511 
512 				/* cancel alignment for ethernet */
513 				m_adj(m, ETHER_ALIGN);
514 
515 				sc->sc_rx_buf[x] = m;
516 			} else {
517 				m = sc->sc_rx_buf[x];
518 			}
519 
520 			usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
521 		}
522 		/* set number of frames and start hardware */
523 		usbd_xfer_set_frames(xfer, x);
524 		usbd_transfer_submit(xfer);
525 		/* flush any received frames */
526 		uether_rxflush(&sc->sc_ue);
527 		break;
528 
529 	default:			/* Error */
530 		DPRINTF("error = %s\n", usbd_errstr(error));
531 
532 		if (error != USB_ERR_CANCELLED) {
533 	tr_stall:
534 			/* try to clear stall first */
535 			usbd_xfer_set_stall(xfer);
536 			usbd_xfer_set_frames(xfer, 0);
537 			usbd_transfer_submit(xfer);
538 			break;
539 		}
540 		/* need to free the RX-mbufs when we are cancelled */
541 		ipheth_free_queue(sc->sc_rx_buf, IPHETH_RX_FRAMES_MAX);
542 		break;
543 	}
544 }
545