2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 2009 Diego Giagio. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Thanks to Diego Giagio for figuring out the programming details for
29 * the Apple iPhone Ethernet driver.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR ipheth_debug
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_process.h>
63 #include <dev/usb/net/usb_ethernet.h>
64 #include <dev/usb/net/if_iphethvar.h>
66 static device_probe_t ipheth_probe;
67 static device_attach_t ipheth_attach;
68 static device_detach_t ipheth_detach;
70 static usb_callback_t ipheth_bulk_write_callback;
71 static usb_callback_t ipheth_bulk_read_callback;
73 static uether_fn_t ipheth_attach_post;
74 static uether_fn_t ipheth_tick;
75 static uether_fn_t ipheth_init;
76 static uether_fn_t ipheth_stop;
77 static uether_fn_t ipheth_start;
78 static uether_fn_t ipheth_setmulti;
79 static uether_fn_t ipheth_setpromisc;
82 static int ipheth_debug = 0;
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, ipheth, CTLFLAG_RW, 0, "USB iPhone ethernet");
85 SYSCTL_INT(_hw_usb_ipheth, OID_AUTO, debug, CTLFLAG_RW, &ipheth_debug, 0, "Debug level");
88 static const struct usb_config ipheth_config[IPHETH_N_TRANSFER] = {
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 */
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,
113 static 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),
122 static driver_t ipheth_driver = {
124 .methods = ipheth_methods,
125 .size = sizeof(struct ipheth_softc),
128 static devclass_t ipheth_devclass;
130 DRIVER_MODULE(ipheth, uhub, ipheth_driver, ipheth_devclass, NULL, 0);
131 MODULE_VERSION(ipheth, 1);
132 MODULE_DEPEND(ipheth, uether, 1, 1, 1);
133 MODULE_DEPEND(ipheth, usb, 1, 1, 1);
134 MODULE_DEPEND(ipheth, ether, 1, 1, 1);
136 static 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,
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)
151 static const STRUCT_USB_HOST_ID ipheth_devs[] = {
152 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE,
153 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
154 IPHETH_USBINTF_PROTO)},
155 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G,
156 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
157 IPHETH_USBINTF_PROTO)},
158 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS,
159 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
160 IPHETH_USBINTF_PROTO)},
161 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4,
162 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS,
163 IPHETH_USBINTF_PROTO)},
167 ipheth_get_mac_addr(struct ipheth_softc *sc)
169 struct usb_device_request req;
172 req.bmRequestType = UT_READ_VENDOR_DEVICE;
173 req.bRequest = IPHETH_CMD_GET_MACADDR;
176 req.wIndex[0] = sc->sc_iface_no;
178 req.wLength[0] = ETHER_ADDR_LEN;
181 error = usbd_do_request(sc->sc_ue.ue_udev, NULL, &req, sc->sc_data);
186 memcpy(sc->sc_ue.ue_eaddr, sc->sc_data, ETHER_ADDR_LEN);
192 ipheth_probe(device_t dev)
194 struct usb_attach_arg *uaa = device_get_ivars(dev);
196 if (uaa->usb_mode != USB_MODE_HOST)
199 return (usbd_lookup_id_by_uaa(ipheth_devs, sizeof(ipheth_devs), uaa));
203 ipheth_attach(device_t dev)
205 struct ipheth_softc *sc = device_get_softc(dev);
206 struct usb_ether *ue = &sc->sc_ue;
207 struct usb_attach_arg *uaa = device_get_ivars(dev);
210 sc->sc_iface_no = uaa->info.bIfaceIndex;
212 device_set_usb_desc(dev);
214 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
216 error = usbd_set_alt_interface_index(uaa->device,
217 uaa->info.bIfaceIndex, IPHETH_ALT_INTFNUM);
219 device_printf(dev, "Cannot set alternate setting\n");
222 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_no,
223 sc->sc_xfer, ipheth_config, IPHETH_N_TRANSFER, sc, &sc->sc_mtx);
225 device_printf(dev, "Cannot setup USB transfers\n");
230 ue->ue_udev = uaa->device;
231 ue->ue_mtx = &sc->sc_mtx;
232 ue->ue_methods = &ipheth_ue_methods;
234 error = ipheth_get_mac_addr(sc);
236 device_printf(dev, "Cannot get MAC address\n");
240 error = uether_ifattach(ue);
242 device_printf(dev, "could not attach interface\n");
245 return (0); /* success */
249 return (ENXIO); /* failure */
253 ipheth_detach(device_t dev)
255 struct ipheth_softc *sc = device_get_softc(dev);
256 struct usb_ether *ue = &sc->sc_ue;
258 /* stop all USB transfers first */
259 usbd_transfer_unsetup(sc->sc_xfer, IPHETH_N_TRANSFER);
263 mtx_destroy(&sc->sc_mtx);
269 ipheth_start(struct usb_ether *ue)
271 struct ipheth_softc *sc = uether_getsc(ue);
274 * Start the USB transfers, if not already started:
276 usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_TX]);
277 usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_RX]);
281 ipheth_stop(struct usb_ether *ue)
283 struct ipheth_softc *sc = uether_getsc(ue);
286 * Stop the USB transfers, if not already stopped:
288 usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_TX]);
289 usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_RX]);
293 ipheth_tick(struct usb_ether *ue)
295 struct ipheth_softc *sc = uether_getsc(ue);
296 struct usb_device_request req;
299 req.bmRequestType = UT_READ_VENDOR_DEVICE;
300 req.bRequest = IPHETH_CMD_CARRIER_CHECK;
303 req.wIndex[0] = sc->sc_iface_no;
305 req.wLength[0] = IPHETH_CTRL_BUF_SIZE;
308 error = uether_do_request(ue, &req, sc->sc_data, IPHETH_CTRL_TIMEOUT);
314 (sc->sc_data[0] == IPHETH_CARRIER_ON);
318 ipheth_attach_post(struct usb_ether *ue)
324 ipheth_init(struct usb_ether *ue)
326 struct ipheth_softc *sc = uether_getsc(ue);
327 struct ifnet *ifp = uether_getifp(ue);
329 IPHETH_LOCK_ASSERT(sc, MA_OWNED);
331 ifp->if_drv_flags |= IFF_DRV_RUNNING;
333 /* stall data write direction, which depends on USB mode */
334 usbd_xfer_set_stall(sc->sc_xfer[IPHETH_BULK_TX]);
336 /* start data transfers */
341 ipheth_setmulti(struct usb_ether *ue)
347 ipheth_setpromisc(struct usb_ether *ue)
353 ipheth_free_queue(struct mbuf **ppm, uint8_t n)
357 for (x = 0; x != n; x++) {
358 if (ppm[x] != NULL) {
366 ipheth_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
368 struct ipheth_softc *sc = usbd_xfer_softc(xfer);
369 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
370 struct usb_page_cache *pc;
376 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
380 switch (USB_GET_STATE(xfer)) {
381 case USB_ST_TRANSFERRED:
382 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n",
387 /* free all previous TX buffers */
388 ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
393 for (x = 0; x != IPHETH_TX_FRAMES_MAX; x++) {
395 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
400 usbd_xfer_set_frame_offset(xfer,
401 x * IPHETH_BUF_SIZE, x);
403 pc = usbd_xfer_get_frame(xfer, x);
405 sc->sc_tx_buf[x] = m;
407 if (m->m_pkthdr.len > IPHETH_BUF_SIZE)
408 m->m_pkthdr.len = IPHETH_BUF_SIZE;
410 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
412 usbd_xfer_set_frame_len(xfer, x, IPHETH_BUF_SIZE);
414 if (IPHETH_BUF_SIZE != m->m_pkthdr.len) {
415 usbd_frame_zero(pc, m->m_pkthdr.len,
416 IPHETH_BUF_SIZE - m->m_pkthdr.len);
420 * If there's a BPF listener, bounce a copy of
426 usbd_xfer_set_frames(xfer, x);
428 usbd_transfer_submit(xfer);
433 DPRINTFN(11, "transfer error, %s\n",
436 /* free all previous TX buffers */
437 ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX);
439 /* count output errors */
442 if (error != USB_ERR_CANCELLED) {
443 /* try to clear stall first */
444 usbd_xfer_set_stall(xfer);
452 ipheth_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
454 struct ipheth_softc *sc = usbd_xfer_softc(xfer);
461 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
463 switch (USB_GET_STATE(xfer)) {
464 case USB_ST_TRANSFERRED:
466 DPRINTF("received %u bytes in %u frames\n", actlen, aframes);
468 for (x = 0; x != aframes; x++) {
470 m = sc->sc_rx_buf[x];
471 sc->sc_rx_buf[x] = NULL;
472 len = usbd_xfer_frame_len(xfer, x);
474 if (len < (sizeof(struct ether_header) +
480 m_adj(m, IPHETH_RX_ADJ);
483 uether_rxmbuf(&sc->sc_ue, m, len - IPHETH_RX_ADJ);
489 for (x = 0; x != IPHETH_RX_FRAMES_MAX; x++) {
490 if (sc->sc_rx_buf[x] == NULL) {
495 /* cancel alignment for ethernet */
496 m_adj(m, ETHER_ALIGN);
498 sc->sc_rx_buf[x] = m;
500 m = sc->sc_rx_buf[x];
503 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len);
505 /* set number of frames and start hardware */
506 usbd_xfer_set_frames(xfer, x);
507 usbd_transfer_submit(xfer);
508 /* flush any received frames */
509 uether_rxflush(&sc->sc_ue);
513 DPRINTF("error = %s\n", usbd_errstr(error));
515 if (error != USB_ERR_CANCELLED) {
517 /* try to clear stall first */
518 usbd_xfer_set_stall(xfer);
519 usbd_xfer_set_frames(xfer, 0);
520 usbd_transfer_submit(xfer);
523 /* need to free the RX-mbufs when we are cancelled */
524 ipheth_free_queue(sc->sc_rx_buf, IPHETH_RX_FRAMES_MAX);