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