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