1 /*- 2 * Copyright (c) 2003 Hidetoshi Shimokawa 3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the acknowledgement as bellow: 16 * 17 * This product includes software developed by K. Kobayashi and H. Shimokawa 18 * 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 * 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/types.h> 41 #include <sys/mbuf.h> 42 #include <sys/bio.h> 43 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/conf.h> 47 #include <sys/poll.h> 48 49 #include <sys/bus.h> 50 #include <sys/ctype.h> 51 #include <machine/bus.h> 52 53 #include <sys/ioccom.h> 54 55 #include <dev/firewire/firewire.h> 56 #include <dev/firewire/firewirereg.h> 57 #include <dev/firewire/fwdma.h> 58 #include <dev/firewire/fwmem.h> 59 #include <dev/firewire/iec68113.h> 60 61 #define FWNODE_INVAL 0xffff 62 63 static d_open_t fw_open; 64 static d_close_t fw_close; 65 static d_ioctl_t fw_ioctl; 66 static d_poll_t fw_poll; 67 static d_read_t fw_read; /* for Isochronous packet */ 68 static d_write_t fw_write; 69 static d_mmap_t fw_mmap; 70 static d_strategy_t fw_strategy; 71 72 struct cdevsw firewire_cdevsw = { 73 .d_version = D_VERSION, 74 .d_open = fw_open, 75 .d_close = fw_close, 76 .d_read = fw_read, 77 .d_write = fw_write, 78 .d_ioctl = fw_ioctl, 79 .d_poll = fw_poll, 80 .d_mmap = fw_mmap, 81 .d_strategy = fw_strategy, 82 .d_name = "fw", 83 }; 84 85 struct fw_drv1 { 86 struct firewire_comm *fc; 87 struct fw_xferq *ir; 88 struct fw_xferq *it; 89 struct fw_isobufreq bufreq; 90 STAILQ_HEAD(, fw_bind) binds; 91 STAILQ_HEAD(, fw_xfer) rq; 92 }; 93 94 static int 95 fwdev_allocbuf(struct firewire_comm *fc, struct fw_xferq *q, 96 struct fw_bufspec *b) 97 { 98 int i; 99 100 if (q->flag & (FWXFERQ_RUNNING | FWXFERQ_EXTBUF)) 101 return (EBUSY); 102 103 q->bulkxfer = malloc(sizeof(struct fw_bulkxfer) * b->nchunk, 104 M_FW, M_WAITOK); 105 106 b->psize = roundup2(b->psize, sizeof(uint32_t)); 107 q->buf = fwdma_malloc_multiseg(fc, sizeof(uint32_t), 108 b->psize, b->nchunk * b->npacket, BUS_DMA_WAITOK); 109 110 if (q->buf == NULL) { 111 free(q->bulkxfer, M_FW); 112 q->bulkxfer = NULL; 113 return (ENOMEM); 114 } 115 q->bnchunk = b->nchunk; 116 q->bnpacket = b->npacket; 117 q->psize = (b->psize + 3) & ~3; 118 q->queued = 0; 119 120 STAILQ_INIT(&q->stvalid); 121 STAILQ_INIT(&q->stfree); 122 STAILQ_INIT(&q->stdma); 123 q->stproc = NULL; 124 125 for (i = 0; i < q->bnchunk; i++) { 126 q->bulkxfer[i].poffset = i * q->bnpacket; 127 q->bulkxfer[i].mbuf = NULL; 128 STAILQ_INSERT_TAIL(&q->stfree, &q->bulkxfer[i], link); 129 } 130 131 q->flag &= ~FWXFERQ_MODEMASK; 132 q->flag |= FWXFERQ_STREAM; 133 q->flag |= FWXFERQ_EXTBUF; 134 135 return (0); 136 } 137 138 static int 139 fwdev_freebuf(struct fw_xferq *q) 140 { 141 if (q->flag & FWXFERQ_EXTBUF) { 142 if (q->buf != NULL) 143 fwdma_free_multiseg(q->buf); 144 q->buf = NULL; 145 free(q->bulkxfer, M_FW); 146 q->bulkxfer = NULL; 147 q->flag &= ~FWXFERQ_EXTBUF; 148 q->psize = 0; 149 q->maxq = FWMAXQUEUE; 150 } 151 return (0); 152 } 153 154 155 static int 156 fw_open(struct cdev *dev, int flags, int fmt, fw_proc *td) 157 { 158 int err = 0; 159 int unit = DEV2UNIT(dev); 160 struct fw_drv1 *d; 161 struct firewire_softc *sc; 162 163 if (DEV_FWMEM(dev)) 164 return fwmem_open(dev, flags, fmt, td); 165 166 sc = devclass_get_softc(firewire_devclass, unit); 167 if (sc == NULL) 168 return (ENXIO); 169 170 FW_GLOCK(sc->fc); 171 if (dev->si_drv1 != NULL) { 172 FW_GUNLOCK(sc->fc); 173 return (EBUSY); 174 } 175 /* set dummy value for allocation */ 176 dev->si_drv1 = (void *)-1; 177 FW_GUNLOCK(sc->fc); 178 179 dev->si_drv1 = malloc(sizeof(struct fw_drv1), M_FW, M_WAITOK | M_ZERO); 180 181 if ((dev->si_flags & SI_NAMED) == 0) { 182 int unit = DEV2UNIT(dev); 183 int sub = DEV2SUB(dev); 184 185 make_dev(&firewire_cdevsw, dev2unit(dev), 186 UID_ROOT, GID_OPERATOR, 0660, "fw%d.%d", unit, sub); 187 } 188 189 d = dev->si_drv1; 190 d->fc = sc->fc; 191 STAILQ_INIT(&d->binds); 192 STAILQ_INIT(&d->rq); 193 194 return err; 195 } 196 197 static int 198 fw_close(struct cdev *dev, int flags, int fmt, fw_proc *td) 199 { 200 struct firewire_comm *fc; 201 struct fw_drv1 *d; 202 struct fw_xfer *xfer; 203 struct fw_bind *fwb; 204 int err = 0; 205 206 if (DEV_FWMEM(dev)) 207 return fwmem_close(dev, flags, fmt, td); 208 209 d = dev->si_drv1; 210 fc = d->fc; 211 212 /* remove binding */ 213 for (fwb = STAILQ_FIRST(&d->binds); fwb != NULL; 214 fwb = STAILQ_FIRST(&d->binds)) { 215 fw_bindremove(fc, fwb); 216 STAILQ_REMOVE_HEAD(&d->binds, chlist); 217 fw_xferlist_remove(&fwb->xferlist); 218 free(fwb, M_FW); 219 } 220 if (d->ir != NULL) { 221 struct fw_xferq *ir = d->ir; 222 223 if ((ir->flag & FWXFERQ_OPEN) == 0) 224 return (EINVAL); 225 if (ir->flag & FWXFERQ_RUNNING) { 226 ir->flag &= ~FWXFERQ_RUNNING; 227 fc->irx_disable(fc, ir->dmach); 228 } 229 /* free extbuf */ 230 fwdev_freebuf(ir); 231 /* drain receiving buffer */ 232 for (xfer = STAILQ_FIRST(&ir->q); 233 xfer != NULL; xfer = STAILQ_FIRST(&ir->q)) { 234 ir->queued--; 235 STAILQ_REMOVE_HEAD(&ir->q, link); 236 237 xfer->resp = 0; 238 fw_xfer_done(xfer); 239 } 240 ir->flag &= ~(FWXFERQ_OPEN | FWXFERQ_MODEMASK | 241 FWXFERQ_CHTAGMASK); 242 d->ir = NULL; 243 244 } 245 if (d->it != NULL) { 246 struct fw_xferq *it = d->it; 247 248 if ((it->flag & FWXFERQ_OPEN) == 0) 249 return (EINVAL); 250 if (it->flag & FWXFERQ_RUNNING) { 251 it->flag &= ~FWXFERQ_RUNNING; 252 fc->itx_disable(fc, it->dmach); 253 } 254 /* free extbuf */ 255 fwdev_freebuf(it); 256 it->flag &= ~(FWXFERQ_OPEN | 257 FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK); 258 d->it = NULL; 259 } 260 free(dev->si_drv1, M_FW); 261 dev->si_drv1 = NULL; 262 263 return err; 264 } 265 266 static int 267 fw_read_async(struct fw_drv1 *d, struct uio *uio, int ioflag) 268 { 269 int err = 0, s; 270 struct fw_xfer *xfer; 271 struct fw_bind *fwb; 272 struct fw_pkt *fp; 273 struct tcode_info *tinfo; 274 275 FW_GLOCK(d->fc); 276 while ((xfer = STAILQ_FIRST(&d->rq)) == NULL && err == 0) 277 err = msleep(&d->rq, FW_GMTX(d->fc), FWPRI, "fwra", 0); 278 279 if (err != 0) { 280 FW_GUNLOCK(d->fc); 281 return (err); 282 } 283 284 s = splfw(); 285 STAILQ_REMOVE_HEAD(&d->rq, link); 286 FW_GUNLOCK(xfer->fc); 287 splx(s); 288 fp = &xfer->recv.hdr; 289 #if 0 /* for GASP ?? */ 290 if (fc->irx_post != NULL) 291 fc->irx_post(fc, fp->mode.ld); 292 #endif 293 tinfo = &xfer->fc->tcode[fp->mode.hdr.tcode]; 294 err = uiomove(fp, tinfo->hdr_len, uio); 295 if (err) 296 goto out; 297 err = uiomove(xfer->recv.payload, xfer->recv.pay_len, uio); 298 299 out: 300 /* recycle this xfer */ 301 fwb = (struct fw_bind *)xfer->sc; 302 fw_xfer_unload(xfer); 303 xfer->recv.pay_len = PAGE_SIZE; 304 FW_GLOCK(xfer->fc); 305 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link); 306 FW_GUNLOCK(xfer->fc); 307 return (err); 308 } 309 310 /* 311 * read request. 312 */ 313 static int 314 fw_read(struct cdev *dev, struct uio *uio, int ioflag) 315 { 316 struct fw_drv1 *d; 317 struct fw_xferq *ir; 318 struct firewire_comm *fc; 319 int err = 0, s, slept = 0; 320 struct fw_pkt *fp; 321 322 if (DEV_FWMEM(dev)) 323 return (physio(dev, uio, ioflag)); 324 325 d = dev->si_drv1; 326 fc = d->fc; 327 ir = d->ir; 328 329 if (ir == NULL) 330 return (fw_read_async(d, uio, ioflag)); 331 332 if (ir->buf == NULL) 333 return (EIO); 334 335 FW_GLOCK(fc); 336 readloop: 337 if (ir->stproc == NULL) { 338 /* iso bulkxfer */ 339 ir->stproc = STAILQ_FIRST(&ir->stvalid); 340 if (ir->stproc != NULL) { 341 s = splfw(); 342 STAILQ_REMOVE_HEAD(&ir->stvalid, link); 343 splx(s); 344 ir->queued = 0; 345 } 346 } 347 if (ir->stproc == NULL) { 348 /* no data available */ 349 if (slept == 0) { 350 slept = 1; 351 ir->flag |= FWXFERQ_WAKEUP; 352 err = msleep(ir, FW_GMTX(fc), FWPRI, "fw_read", hz); 353 ir->flag &= ~FWXFERQ_WAKEUP; 354 if (err == 0) 355 goto readloop; 356 } else if (slept == 1) 357 err = EIO; 358 FW_GUNLOCK(fc); 359 return err; 360 } else if (ir->stproc != NULL) { 361 /* iso bulkxfer */ 362 FW_GUNLOCK(fc); 363 fp = (struct fw_pkt *)fwdma_v_addr(ir->buf, 364 ir->stproc->poffset + ir->queued); 365 if (fc->irx_post != NULL) 366 fc->irx_post(fc, fp->mode.ld); 367 if (fp->mode.stream.len == 0) { 368 err = EIO; 369 return err; 370 } 371 err = uiomove((caddr_t)fp, 372 fp->mode.stream.len + sizeof(uint32_t), uio); 373 ir->queued++; 374 if (ir->queued >= ir->bnpacket) { 375 s = splfw(); 376 STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link); 377 splx(s); 378 fc->irx_enable(fc, ir->dmach); 379 ir->stproc = NULL; 380 } 381 if (uio->uio_resid >= ir->psize) { 382 slept = -1; 383 FW_GLOCK(fc); 384 goto readloop; 385 } 386 } 387 return err; 388 } 389 390 static int 391 fw_write_async(struct fw_drv1 *d, struct uio *uio, int ioflag) 392 { 393 struct fw_xfer *xfer; 394 struct fw_pkt pkt; 395 struct tcode_info *tinfo; 396 int err; 397 398 bzero(&pkt, sizeof(struct fw_pkt)); 399 if ((err = uiomove((caddr_t)&pkt, sizeof(uint32_t), uio))) 400 return (err); 401 tinfo = &d->fc->tcode[pkt.mode.hdr.tcode]; 402 if ((err = uiomove((caddr_t)&pkt + sizeof(uint32_t), 403 tinfo->hdr_len - sizeof(uint32_t), uio))) 404 return (err); 405 406 if ((xfer = fw_xfer_alloc_buf(M_FWXFER, uio->uio_resid, 407 PAGE_SIZE/*XXX*/)) == NULL) 408 return (ENOMEM); 409 410 bcopy(&pkt, &xfer->send.hdr, sizeof(struct fw_pkt)); 411 xfer->send.pay_len = uio->uio_resid; 412 if (uio->uio_resid > 0) { 413 if ((err = uiomove((caddr_t)&xfer->send.payload[0], 414 uio->uio_resid, uio))) 415 goto out; 416 } 417 418 xfer->fc = d->fc; 419 xfer->sc = NULL; 420 xfer->hand = fw_xferwake; 421 xfer->send.spd = 2 /* XXX */; 422 423 if ((err = fw_asyreq(xfer->fc, -1, xfer))) 424 goto out; 425 426 if ((err = fw_xferwait(xfer))) 427 goto out; 428 429 if (xfer->resp != 0) { 430 err = xfer->resp; 431 goto out; 432 } 433 434 if (xfer->flag & FWXF_RCVD) { 435 FW_GLOCK(xfer->fc); 436 STAILQ_INSERT_TAIL(&d->rq, xfer, link); 437 FW_GUNLOCK(xfer->fc); 438 return (0); 439 } 440 441 out: 442 fw_xfer_free(xfer); 443 return (err); 444 } 445 446 static int 447 fw_write(struct cdev *dev, struct uio *uio, int ioflag) 448 { 449 int err = 0; 450 int s, slept = 0; 451 struct fw_drv1 *d; 452 struct fw_pkt *fp; 453 struct firewire_comm *fc; 454 struct fw_xferq *it; 455 456 if (DEV_FWMEM(dev)) 457 return (physio(dev, uio, ioflag)); 458 459 d = dev->si_drv1; 460 fc = d->fc; 461 it = d->it; 462 463 if (it == NULL) 464 return (fw_write_async(d, uio, ioflag)); 465 466 if (it->buf == NULL) 467 return (EIO); 468 469 FW_GLOCK(fc); 470 isoloop: 471 if (it->stproc == NULL) { 472 it->stproc = STAILQ_FIRST(&it->stfree); 473 if (it->stproc != NULL) { 474 s = splfw(); 475 STAILQ_REMOVE_HEAD(&it->stfree, link); 476 splx(s); 477 it->queued = 0; 478 } else if (slept == 0) { 479 slept = 1; 480 #if 0 /* XXX to avoid lock recursion */ 481 err = fc->itx_enable(fc, it->dmach); 482 if (err) 483 goto out; 484 #endif 485 err = msleep(it, FW_GMTX(fc), FWPRI, "fw_write", hz); 486 if (err) 487 goto out; 488 goto isoloop; 489 } else { 490 err = EIO; 491 goto out; 492 } 493 } 494 FW_GUNLOCK(fc); 495 fp = (struct fw_pkt *)fwdma_v_addr(it->buf, 496 it->stproc->poffset + it->queued); 497 err = uiomove((caddr_t)fp, sizeof(struct fw_isohdr), uio); 498 err = uiomove((caddr_t)fp->mode.stream.payload, 499 fp->mode.stream.len, uio); 500 it->queued++; 501 if (it->queued >= it->bnpacket) { 502 s = splfw(); 503 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link); 504 splx(s); 505 it->stproc = NULL; 506 err = fc->itx_enable(fc, it->dmach); 507 } 508 if (uio->uio_resid >= sizeof(struct fw_isohdr)) { 509 slept = 0; 510 FW_GLOCK(fc); 511 goto isoloop; 512 } 513 return err; 514 515 out: 516 FW_GUNLOCK(fc); 517 return err; 518 } 519 520 static void 521 fw_hand(struct fw_xfer *xfer) 522 { 523 struct fw_bind *fwb; 524 struct fw_drv1 *d; 525 526 fwb = (struct fw_bind *)xfer->sc; 527 d = fwb->sc; 528 FW_GLOCK(xfer->fc); 529 STAILQ_INSERT_TAIL(&d->rq, xfer, link); 530 FW_GUNLOCK(xfer->fc); 531 wakeup(&d->rq); 532 } 533 534 /* 535 * ioctl support. 536 */ 537 int 538 fw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td) 539 { 540 struct firewire_comm *fc; 541 struct fw_drv1 *d; 542 int i, len, err = 0; 543 struct fw_device *fwdev; 544 struct fw_bind *fwb; 545 struct fw_xferq *ir, *it; 546 struct fw_xfer *xfer; 547 struct fw_pkt *fp; 548 struct fw_devinfo *devinfo; 549 void *ptr; 550 551 struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data; 552 struct fw_asyreq *asyreq = (struct fw_asyreq *)data; 553 struct fw_isochreq *ichreq = (struct fw_isochreq *)data; 554 struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data; 555 struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data; 556 struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data; 557 558 if (DEV_FWMEM(dev)) 559 return fwmem_ioctl(dev, cmd, data, flag, td); 560 561 if (!data) 562 return (EINVAL); 563 564 d = dev->si_drv1; 565 fc = d->fc; 566 ir = d->ir; 567 it = d->it; 568 569 switch (cmd) { 570 case FW_STSTREAM: 571 if (it == NULL) { 572 i = fw_open_isodma(fc, /* tx */1); 573 if (i < 0) { 574 err = EBUSY; 575 break; 576 } 577 it = fc->it[i]; 578 err = fwdev_allocbuf(fc, it, &d->bufreq.tx); 579 if (err) { 580 it->flag &= ~FWXFERQ_OPEN; 581 break; 582 } 583 } 584 it->flag &= ~0xff; 585 it->flag |= (0x3f & ichreq->ch); 586 it->flag |= ((0x3 & ichreq->tag) << 6); 587 d->it = it; 588 break; 589 case FW_GTSTREAM: 590 if (it != NULL) { 591 ichreq->ch = it->flag & 0x3f; 592 ichreq->tag = it->flag >> 2 & 0x3; 593 } else 594 err = EINVAL; 595 break; 596 case FW_SRSTREAM: 597 if (ir == NULL) { 598 i = fw_open_isodma(fc, /* tx */0); 599 if (i < 0) { 600 err = EBUSY; 601 break; 602 } 603 ir = fc->ir[i]; 604 err = fwdev_allocbuf(fc, ir, &d->bufreq.rx); 605 if (err) { 606 ir->flag &= ~FWXFERQ_OPEN; 607 break; 608 } 609 } 610 ir->flag &= ~0xff; 611 ir->flag |= (0x3f & ichreq->ch); 612 ir->flag |= ((0x3 & ichreq->tag) << 6); 613 d->ir = ir; 614 err = fc->irx_enable(fc, ir->dmach); 615 break; 616 case FW_GRSTREAM: 617 if (d->ir != NULL) { 618 ichreq->ch = ir->flag & 0x3f; 619 ichreq->tag = ir->flag >> 2 & 0x3; 620 } else 621 err = EINVAL; 622 break; 623 case FW_SSTBUF: 624 bcopy(ibufreq, &d->bufreq, sizeof(d->bufreq)); 625 break; 626 case FW_GSTBUF: 627 bzero(&ibufreq->rx, sizeof(ibufreq->rx)); 628 if (ir != NULL) { 629 ibufreq->rx.nchunk = ir->bnchunk; 630 ibufreq->rx.npacket = ir->bnpacket; 631 ibufreq->rx.psize = ir->psize; 632 } 633 bzero(&ibufreq->tx, sizeof(ibufreq->tx)); 634 if (it != NULL) { 635 ibufreq->tx.nchunk = it->bnchunk; 636 ibufreq->tx.npacket = it->bnpacket; 637 ibufreq->tx.psize = it->psize; 638 } 639 break; 640 case FW_ASYREQ: 641 { 642 struct tcode_info *tinfo; 643 int pay_len = 0; 644 645 fp = &asyreq->pkt; 646 tinfo = &fc->tcode[fp->mode.hdr.tcode]; 647 648 if ((tinfo->flag & FWTI_BLOCK_ASY) != 0) 649 pay_len = MAX(0, asyreq->req.len - tinfo->hdr_len); 650 651 xfer = fw_xfer_alloc_buf(M_FWXFER, pay_len, PAGE_SIZE/*XXX*/); 652 if (xfer == NULL) 653 return (ENOMEM); 654 655 switch (asyreq->req.type) { 656 case FWASREQNODE: 657 break; 658 case FWASREQEUI: 659 fwdev = fw_noderesolve_eui64(fc, 660 &asyreq->req.dst.eui); 661 if (fwdev == NULL) { 662 device_printf(fc->bdev, 663 "cannot find node\n"); 664 err = EINVAL; 665 goto out; 666 } 667 fp->mode.hdr.dst = FWLOCALBUS | fwdev->dst; 668 break; 669 case FWASRESTL: 670 /* XXX what's this? */ 671 break; 672 case FWASREQSTREAM: 673 /* nothing to do */ 674 break; 675 } 676 677 bcopy(fp, (void *)&xfer->send.hdr, tinfo->hdr_len); 678 if (pay_len > 0) 679 bcopy((char *)fp + tinfo->hdr_len, 680 xfer->send.payload, pay_len); 681 xfer->send.spd = asyreq->req.sped; 682 xfer->hand = fw_xferwake; 683 684 if ((err = fw_asyreq(fc, -1, xfer)) != 0) 685 goto out; 686 if ((err = fw_xferwait(xfer)) != 0) 687 goto out; 688 if (xfer->resp != 0) { 689 err = EIO; 690 goto out; 691 } 692 if ((tinfo->flag & FWTI_TLABEL) == 0) 693 goto out; 694 695 /* copy response */ 696 tinfo = &fc->tcode[xfer->recv.hdr.mode.hdr.tcode]; 697 if (xfer->recv.hdr.mode.hdr.tcode == FWTCODE_RRESB || 698 xfer->recv.hdr.mode.hdr.tcode == FWTCODE_LRES) { 699 pay_len = xfer->recv.pay_len; 700 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len) { 701 asyreq->req.len = xfer->recv.pay_len + 702 tinfo->hdr_len; 703 } else { 704 err = EINVAL; 705 pay_len = 0; 706 } 707 } else { 708 pay_len = 0; 709 } 710 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len); 711 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len, pay_len); 712 out: 713 fw_xfer_free_buf(xfer); 714 break; 715 } 716 case FW_IBUSRST: 717 fc->ibr(fc); 718 break; 719 case FW_CBINDADDR: 720 fwb = fw_bindlookup(fc, 721 bindreq->start.hi, bindreq->start.lo); 722 if (fwb == NULL) { 723 err = EINVAL; 724 break; 725 } 726 fw_bindremove(fc, fwb); 727 STAILQ_REMOVE(&d->binds, fwb, fw_bind, chlist); 728 fw_xferlist_remove(&fwb->xferlist); 729 free(fwb, M_FW); 730 break; 731 case FW_SBINDADDR: 732 if (bindreq->len <= 0) { 733 err = EINVAL; 734 break; 735 } 736 if (bindreq->start.hi > 0xffff) { 737 err = EINVAL; 738 break; 739 } 740 fwb = malloc(sizeof(struct fw_bind), M_FW, M_WAITOK); 741 fwb->start = ((u_int64_t)bindreq->start.hi << 32) | 742 bindreq->start.lo; 743 fwb->end = fwb->start + bindreq->len; 744 fwb->sc = d; 745 STAILQ_INIT(&fwb->xferlist); 746 err = fw_bindadd(fc, fwb); 747 if (err == 0) { 748 fw_xferlist_add(&fwb->xferlist, M_FWXFER, 749 /* XXX */ 750 PAGE_SIZE, PAGE_SIZE, 5, 751 fc, fwb, fw_hand); 752 STAILQ_INSERT_TAIL(&d->binds, fwb, chlist); 753 } 754 break; 755 case FW_GDEVLST: 756 i = len = 1; 757 /* myself */ 758 devinfo = &fwdevlst->dev[0]; 759 devinfo->dst = fc->nodeid; 760 devinfo->status = 0; /* XXX */ 761 devinfo->eui.hi = fc->eui.hi; 762 devinfo->eui.lo = fc->eui.lo; 763 STAILQ_FOREACH(fwdev, &fc->devices, link) { 764 if (len < FW_MAX_DEVLST) { 765 devinfo = &fwdevlst->dev[len++]; 766 devinfo->dst = fwdev->dst; 767 devinfo->status = 768 (fwdev->status == FWDEVINVAL) ? 0 : 1; 769 devinfo->eui.hi = fwdev->eui.hi; 770 devinfo->eui.lo = fwdev->eui.lo; 771 } 772 i++; 773 } 774 fwdevlst->n = i; 775 fwdevlst->info_len = len; 776 break; 777 case FW_GTPMAP: 778 bcopy(fc->topology_map, data, 779 (fc->topology_map->crc_len + 1) * 4); 780 break; 781 case FW_GCROM: 782 STAILQ_FOREACH(fwdev, &fc->devices, link) 783 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui)) 784 break; 785 if (fwdev == NULL) { 786 if (!FW_EUI64_EQUAL(fc->eui, crom_buf->eui)) { 787 err = FWNODE_INVAL; 788 break; 789 } 790 /* myself */ 791 ptr = malloc(CROMSIZE, M_FW, M_WAITOK); 792 len = CROMSIZE; 793 for (i = 0; i < CROMSIZE/4; i++) 794 ((uint32_t *)ptr)[i] 795 = ntohl(fc->config_rom[i]); 796 } else { 797 /* found */ 798 ptr = (void *)&fwdev->csrrom[0]; 799 if (fwdev->rommax < CSRROMOFF) 800 len = 0; 801 else 802 len = fwdev->rommax - CSRROMOFF + 4; 803 } 804 if (crom_buf->len < len) 805 len = crom_buf->len; 806 else 807 crom_buf->len = len; 808 err = copyout(ptr, crom_buf->ptr, len); 809 if (fwdev == NULL) 810 /* myself */ 811 free(ptr, M_FW); 812 break; 813 default: 814 fc->ioctl(dev, cmd, data, flag, td); 815 break; 816 } 817 return err; 818 } 819 int 820 fw_poll(struct cdev *dev, int events, fw_proc *td) 821 { 822 struct fw_xferq *ir; 823 int revents; 824 int tmp; 825 826 if (DEV_FWMEM(dev)) 827 return fwmem_poll(dev, events, td); 828 829 ir = ((struct fw_drv1 *)dev->si_drv1)->ir; 830 revents = 0; 831 tmp = POLLIN | POLLRDNORM; 832 if (events & tmp) { 833 if (STAILQ_FIRST(&ir->q) != NULL) 834 revents |= tmp; 835 else 836 selrecord(td, &ir->rsel); 837 } 838 tmp = POLLOUT | POLLWRNORM; 839 if (events & tmp) { 840 /* XXX should be fixed */ 841 revents |= tmp; 842 } 843 844 return revents; 845 } 846 847 static int 848 fw_mmap (struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 849 int nproto, vm_memattr_t *memattr) 850 { 851 852 if (DEV_FWMEM(dev)) 853 return fwmem_mmap(dev, offset, paddr, nproto, memattr); 854 855 return EINVAL; 856 } 857 858 static void 859 fw_strategy(struct bio *bp) 860 { 861 struct cdev *dev; 862 863 dev = bp->bio_dev; 864 if (DEV_FWMEM(dev)) { 865 fwmem_strategy(bp); 866 return; 867 } 868 869 bp->bio_error = EOPNOTSUPP; 870 bp->bio_flags |= BIO_ERROR; 871 bp->bio_resid = bp->bio_bcount; 872 biodone(bp); 873 } 874 875 int 876 fwdev_makedev(struct firewire_softc *sc) 877 { 878 int err = 0; 879 880 struct cdev *d; 881 int unit; 882 883 unit = device_get_unit(sc->fc->bdev); 884 sc->dev = make_dev(&firewire_cdevsw, MAKEMINOR(0, unit, 0), 885 UID_ROOT, GID_OPERATOR, 0660, "fw%d.%d", unit, 0); 886 d = make_dev(&firewire_cdevsw, MAKEMINOR(FWMEM_FLAG, unit, 0), 887 UID_ROOT, GID_OPERATOR, 0660, "fwmem%d.%d", unit, 0); 888 dev_depends(sc->dev, d); 889 make_dev_alias(sc->dev, "fw%d", unit); 890 make_dev_alias(d, "fwmem%d", unit); 891 892 return (err); 893 } 894 895 int 896 fwdev_destroydev(struct firewire_softc *sc) 897 { 898 int err = 0; 899 900 destroy_dev(sc->dev); 901 return (err); 902 } 903 904 #define NDEVTYPE 2 905 void 906 fwdev_clone(void *arg, struct ucred *cred, char *name, int namelen, 907 struct cdev **dev) 908 { 909 struct firewire_softc *sc; 910 char *devnames[NDEVTYPE] = {"fw", "fwmem"}; 911 char *subp = NULL; 912 int devflag[NDEVTYPE] = {0, FWMEM_FLAG}; 913 int i, unit = 0, sub = 0; 914 915 if (*dev != NULL) 916 return; 917 918 for (i = 0; i < NDEVTYPE; i++) 919 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2) 920 goto found; 921 /* not match */ 922 return; 923 found: 924 925 if (subp == NULL || *subp++ != '.') 926 return; 927 928 /* /dev/fwU.S */ 929 while (isdigit(*subp)) { 930 sub *= 10; 931 sub += *subp++ - '0'; 932 } 933 if (*subp != '\0') 934 return; 935 936 sc = devclass_get_softc(firewire_devclass, unit); 937 if (sc == NULL) 938 return; 939 *dev = make_dev_credf(MAKEDEV_REF, &firewire_cdevsw, 940 MAKEMINOR(devflag[i], unit, sub), cred, UID_ROOT, GID_OPERATOR, 941 0660, "%s%d.%d", devnames[i], unit, sub); 942 dev_depends(sc->dev, *dev); 943 return; 944 } 945