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/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, 0, "USB iPhone ethernet"); 89 SYSCTL_INT(_hw_usb_ipheth, OID_AUTO, debug, CTLFLAG_RW, &ipheth_debug, 0, "Debug level"); 90 #endif 91 92 static const struct usb_config ipheth_config[IPHETH_N_TRANSFER] = { 93 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 devclass_t ipheth_devclass; 133 134 DRIVER_MODULE(ipheth, uhub, ipheth_driver, ipheth_devclass, NULL, 0); 135 MODULE_VERSION(ipheth, 1); 136 MODULE_DEPEND(ipheth, uether, 1, 1, 1); 137 MODULE_DEPEND(ipheth, usb, 1, 1, 1); 138 MODULE_DEPEND(ipheth, ether, 1, 1, 1); 139 140 static const struct usb_ether_methods ipheth_ue_methods = { 141 .ue_attach_post = ipheth_attach_post, 142 .ue_start = ipheth_start, 143 .ue_init = ipheth_init, 144 .ue_tick = ipheth_tick, 145 .ue_stop = ipheth_stop, 146 .ue_setmulti = ipheth_setmulti, 147 .ue_setpromisc = ipheth_setpromisc, 148 }; 149 150 #define IPHETH_ID(v,p,c,sc,pt) \ 151 USB_VENDOR(v), USB_PRODUCT(p), \ 152 USB_IFACE_CLASS(c), USB_IFACE_SUBCLASS(sc), \ 153 USB_IFACE_PROTOCOL(pt) 154 155 static const STRUCT_USB_HOST_ID ipheth_devs[] = { 156 #if 0 157 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE, 158 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 159 IPHETH_USBINTF_PROTO)}, 160 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G, 161 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 162 IPHETH_USBINTF_PROTO)}, 163 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS, 164 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 165 IPHETH_USBINTF_PROTO)}, 166 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4, 167 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 168 IPHETH_USBINTF_PROTO)}, 169 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S, 170 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 171 IPHETH_USBINTF_PROTO)}, 172 {IPHETH_ID(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_5, 173 IPHETH_USBINTF_CLASS, IPHETH_USBINTF_SUBCLASS, 174 IPHETH_USBINTF_PROTO)}, 175 #else 176 /* product agnostic interface match */ 177 {USB_VENDOR(USB_VENDOR_APPLE), 178 USB_IFACE_CLASS(IPHETH_USBINTF_CLASS), 179 USB_IFACE_SUBCLASS(IPHETH_USBINTF_SUBCLASS), 180 USB_IFACE_PROTOCOL(IPHETH_USBINTF_PROTO)}, 181 #endif 182 }; 183 184 static int 185 ipheth_get_mac_addr(struct ipheth_softc *sc) 186 { 187 struct usb_device_request req; 188 int error; 189 190 req.bmRequestType = UT_READ_VENDOR_DEVICE; 191 req.bRequest = IPHETH_CMD_GET_MACADDR; 192 req.wValue[0] = 0; 193 req.wValue[1] = 0; 194 req.wIndex[0] = sc->sc_iface_no; 195 req.wIndex[1] = 0; 196 req.wLength[0] = ETHER_ADDR_LEN; 197 req.wLength[1] = 0; 198 199 error = usbd_do_request(sc->sc_ue.ue_udev, NULL, &req, sc->sc_data); 200 201 if (error) 202 return (error); 203 204 memcpy(sc->sc_ue.ue_eaddr, sc->sc_data, ETHER_ADDR_LEN); 205 206 return (0); 207 } 208 209 static int 210 ipheth_probe(device_t dev) 211 { 212 struct usb_attach_arg *uaa = device_get_ivars(dev); 213 214 if (uaa->usb_mode != USB_MODE_HOST) 215 return (ENXIO); 216 217 return (usbd_lookup_id_by_uaa(ipheth_devs, sizeof(ipheth_devs), uaa)); 218 } 219 220 static int 221 ipheth_attach(device_t dev) 222 { 223 struct ipheth_softc *sc = device_get_softc(dev); 224 struct usb_ether *ue = &sc->sc_ue; 225 struct usb_attach_arg *uaa = device_get_ivars(dev); 226 int error; 227 228 sc->sc_iface_no = uaa->info.bIfaceIndex; 229 230 device_set_usb_desc(dev); 231 232 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 233 234 error = usbd_set_alt_interface_index(uaa->device, 235 uaa->info.bIfaceIndex, IPHETH_ALT_INTFNUM); 236 if (error) { 237 device_printf(dev, "Cannot set alternate setting\n"); 238 goto detach; 239 } 240 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_no, 241 sc->sc_xfer, ipheth_config, IPHETH_N_TRANSFER, sc, &sc->sc_mtx); 242 if (error) { 243 device_printf(dev, "Cannot setup USB transfers\n"); 244 goto detach; 245 } 246 ue->ue_sc = sc; 247 ue->ue_dev = dev; 248 ue->ue_udev = uaa->device; 249 ue->ue_mtx = &sc->sc_mtx; 250 ue->ue_methods = &ipheth_ue_methods; 251 252 error = ipheth_get_mac_addr(sc); 253 if (error) { 254 device_printf(dev, "Cannot get MAC address\n"); 255 goto detach; 256 } 257 258 error = uether_ifattach(ue); 259 if (error) { 260 device_printf(dev, "could not attach interface\n"); 261 goto detach; 262 } 263 return (0); /* success */ 264 265 detach: 266 ipheth_detach(dev); 267 return (ENXIO); /* failure */ 268 } 269 270 static int 271 ipheth_detach(device_t dev) 272 { 273 struct ipheth_softc *sc = device_get_softc(dev); 274 struct usb_ether *ue = &sc->sc_ue; 275 276 /* stop all USB transfers first */ 277 usbd_transfer_unsetup(sc->sc_xfer, IPHETH_N_TRANSFER); 278 279 uether_ifdetach(ue); 280 281 mtx_destroy(&sc->sc_mtx); 282 283 return (0); 284 } 285 286 static void 287 ipheth_start(struct usb_ether *ue) 288 { 289 struct ipheth_softc *sc = uether_getsc(ue); 290 291 /* 292 * Start the USB transfers, if not already started: 293 */ 294 usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_TX]); 295 usbd_transfer_start(sc->sc_xfer[IPHETH_BULK_RX]); 296 } 297 298 static void 299 ipheth_stop(struct usb_ether *ue) 300 { 301 struct ipheth_softc *sc = uether_getsc(ue); 302 303 /* 304 * Stop the USB transfers, if not already stopped: 305 */ 306 usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_TX]); 307 usbd_transfer_stop(sc->sc_xfer[IPHETH_BULK_RX]); 308 } 309 310 static void 311 ipheth_tick(struct usb_ether *ue) 312 { 313 struct ipheth_softc *sc = uether_getsc(ue); 314 struct usb_device_request req; 315 int error; 316 317 req.bmRequestType = UT_READ_VENDOR_DEVICE; 318 req.bRequest = IPHETH_CMD_CARRIER_CHECK; 319 req.wValue[0] = 0; 320 req.wValue[1] = 0; 321 req.wIndex[0] = sc->sc_iface_no; 322 req.wIndex[1] = 0; 323 req.wLength[0] = IPHETH_CTRL_BUF_SIZE; 324 req.wLength[1] = 0; 325 326 error = uether_do_request(ue, &req, sc->sc_data, IPHETH_CTRL_TIMEOUT); 327 328 if (error) 329 return; 330 331 sc->sc_carrier_on = 332 (sc->sc_data[0] == IPHETH_CARRIER_ON); 333 } 334 335 static void 336 ipheth_attach_post(struct usb_ether *ue) 337 { 338 339 } 340 341 static void 342 ipheth_init(struct usb_ether *ue) 343 { 344 struct ipheth_softc *sc = uether_getsc(ue); 345 struct ifnet *ifp = uether_getifp(ue); 346 347 IPHETH_LOCK_ASSERT(sc, MA_OWNED); 348 349 ifp->if_drv_flags |= IFF_DRV_RUNNING; 350 351 /* stall data write direction, which depends on USB mode */ 352 usbd_xfer_set_stall(sc->sc_xfer[IPHETH_BULK_TX]); 353 354 /* start data transfers */ 355 ipheth_start(ue); 356 } 357 358 static void 359 ipheth_setmulti(struct usb_ether *ue) 360 { 361 362 } 363 364 static void 365 ipheth_setpromisc(struct usb_ether *ue) 366 { 367 368 } 369 370 static void 371 ipheth_free_queue(struct mbuf **ppm, uint8_t n) 372 { 373 uint8_t x; 374 375 for (x = 0; x != n; x++) { 376 if (ppm[x] != NULL) { 377 m_freem(ppm[x]); 378 ppm[x] = NULL; 379 } 380 } 381 } 382 383 static void 384 ipheth_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 385 { 386 struct ipheth_softc *sc = usbd_xfer_softc(xfer); 387 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 388 struct usb_page_cache *pc; 389 struct mbuf *m; 390 uint8_t x; 391 int actlen; 392 int aframes; 393 394 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 395 396 DPRINTFN(1, "\n"); 397 398 switch (USB_GET_STATE(xfer)) { 399 case USB_ST_TRANSFERRED: 400 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n", 401 actlen, aframes); 402 403 ifp->if_opackets++; 404 405 /* free all previous TX buffers */ 406 ipheth_free_queue(sc->sc_tx_buf, IPHETH_TX_FRAMES_MAX); 407 408 /* FALLTHROUGH */ 409 case USB_ST_SETUP: 410 tr_setup: 411 for (x = 0; x != IPHETH_TX_FRAMES_MAX; x++) { 412 413 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 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 ifp->if_oerrors++; 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 488 m = sc->sc_rx_buf[x]; 489 sc->sc_rx_buf[x] = NULL; 490 len = usbd_xfer_frame_len(xfer, x); 491 492 if (len < (int)(sizeof(struct ether_header) + 493 IPHETH_RX_ADJ)) { 494 m_freem(m); 495 continue; 496 } 497 498 m_adj(m, IPHETH_RX_ADJ); 499 500 /* queue up mbuf */ 501 uether_rxmbuf(&sc->sc_ue, m, len - IPHETH_RX_ADJ); 502 } 503 504 /* FALLTHROUGH */ 505 case USB_ST_SETUP: 506 507 for (x = 0; x != IPHETH_RX_FRAMES_MAX; x++) { 508 if (sc->sc_rx_buf[x] == NULL) { 509 m = uether_newbuf(); 510 if (m == NULL) 511 goto tr_stall; 512 513 /* cancel alignment for ethernet */ 514 m_adj(m, ETHER_ALIGN); 515 516 sc->sc_rx_buf[x] = m; 517 } else { 518 m = sc->sc_rx_buf[x]; 519 } 520 521 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len); 522 } 523 /* set number of frames and start hardware */ 524 usbd_xfer_set_frames(xfer, x); 525 usbd_transfer_submit(xfer); 526 /* flush any received frames */ 527 uether_rxflush(&sc->sc_ue); 528 break; 529 530 default: /* Error */ 531 DPRINTF("error = %s\n", usbd_errstr(error)); 532 533 if (error != USB_ERR_CANCELLED) { 534 tr_stall: 535 /* try to clear stall first */ 536 usbd_xfer_set_stall(xfer); 537 usbd_xfer_set_frames(xfer, 0); 538 usbd_transfer_submit(xfer); 539 break; 540 } 541 /* need to free the RX-mbufs when we are cancelled */ 542 ipheth_free_queue(sc->sc_rx_buf, IPHETH_RX_FRAMES_MAX); 543 break; 544 } 545 } 546