2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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:
17 * This product includes software developed by K. Kobayashi and H. Shimokawa
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.
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.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
42 #if defined(__DragonFly__) || __FreeBSD_version < 500000
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
54 #include <sys/ctype.h>
55 #include <machine/bus.h>
57 #include <sys/ioccom.h>
61 #include "firewirereg.h"
66 #include <dev/firewire/firewire.h>
67 #include <dev/firewire/firewirereg.h>
68 #include <dev/firewire/fwdma.h>
69 #include <dev/firewire/fwmem.h>
70 #include <dev/firewire/iec68113.h>
73 #define FWNODE_INVAL 0xffff
75 static d_open_t fw_open;
76 static d_close_t fw_close;
77 static d_ioctl_t fw_ioctl;
78 static d_poll_t fw_poll;
79 static d_read_t fw_read; /* for Isochronous packet */
80 static d_write_t fw_write;
81 static d_mmap_t fw_mmap;
82 static d_strategy_t fw_strategy;
84 struct cdevsw firewire_cdevsw = {
86 #define CDEV_MAJOR 127
87 "fw", CDEV_MAJOR, D_MEM, NULL, 0,
88 fw_open, fw_close, fw_read, fw_write, fw_ioctl,
89 fw_poll, fw_mmap, fw_strategy, nodump, nopsize,
90 #elif __FreeBSD_version >= 500104
91 .d_version = D_VERSION,
99 .d_strategy = fw_strategy,
102 #define CDEV_MAJOR 127
103 fw_open, fw_close, fw_read, fw_write, fw_ioctl,
104 fw_poll, fw_mmap, fw_strategy, "fw", CDEV_MAJOR,
105 nodump, nopsize, D_MEM, -1
110 struct firewire_comm *fc;
113 struct fw_isobufreq bufreq;
114 STAILQ_HEAD(, fw_bind) binds;
115 STAILQ_HEAD(, fw_xfer) rq;
119 fwdev_allocbuf(struct firewire_comm *fc, struct fw_xferq *q,
120 struct fw_bufspec *b)
124 if (q->flag & (FWXFERQ_RUNNING | FWXFERQ_EXTBUF))
127 q->bulkxfer = (struct fw_bulkxfer *) malloc(
128 sizeof(struct fw_bulkxfer) * b->nchunk,
130 if (q->bulkxfer == NULL)
133 b->psize = roundup2(b->psize, sizeof(uint32_t));
134 q->buf = fwdma_malloc_multiseg(fc, sizeof(uint32_t),
135 b->psize, b->nchunk * b->npacket, BUS_DMA_WAITOK);
137 if (q->buf == NULL) {
138 free(q->bulkxfer, M_FW);
142 q->bnchunk = b->nchunk;
143 q->bnpacket = b->npacket;
144 q->psize = (b->psize + 3) & ~3;
147 STAILQ_INIT(&q->stvalid);
148 STAILQ_INIT(&q->stfree);
149 STAILQ_INIT(&q->stdma);
152 for(i = 0 ; i < q->bnchunk; i++){
153 q->bulkxfer[i].poffset = i * q->bnpacket;
154 q->bulkxfer[i].mbuf = NULL;
155 STAILQ_INSERT_TAIL(&q->stfree, &q->bulkxfer[i], link);
158 q->flag &= ~FWXFERQ_MODEMASK;
159 q->flag |= FWXFERQ_STREAM;
160 q->flag |= FWXFERQ_EXTBUF;
166 fwdev_freebuf(struct fw_xferq *q)
168 if (q->flag & FWXFERQ_EXTBUF) {
170 fwdma_free_multiseg(q->buf);
172 free(q->bulkxfer, M_FW);
174 q->flag &= ~FWXFERQ_EXTBUF;
176 q->maxq = FWMAXQUEUE;
183 fw_open (struct cdev *dev, int flags, int fmt, fw_proc *td)
186 int unit = DEV2UNIT(dev);
188 struct firewire_softc *sc;
191 return fwmem_open(dev, flags, fmt, td);
193 sc = devclass_get_softc(firewire_devclass, unit);
198 if (dev->si_drv1 != NULL) {
202 /* set dummy value for allocation */
203 dev->si_drv1 = (void *)-1;
206 dev->si_drv1 = malloc(sizeof(struct fw_drv1), M_FW, M_WAITOK | M_ZERO);
207 if (dev->si_drv1 == NULL)
210 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
211 if ((dev->si_flags & SI_NAMED) == 0) {
212 int unit = DEV2UNIT(dev);
213 int sub = DEV2SUB(dev);
215 make_dev(&firewire_cdevsw, dev2unit(dev),
216 UID_ROOT, GID_OPERATOR, 0660,
217 "fw%d.%d", unit, sub);
220 d = (struct fw_drv1 *)dev->si_drv1;
222 STAILQ_INIT(&d->binds);
229 fw_close (struct cdev *dev, int flags, int fmt, fw_proc *td)
231 struct firewire_comm *fc;
233 struct fw_xfer *xfer;
238 return fwmem_close(dev, flags, fmt, td);
240 d = (struct fw_drv1 *)dev->si_drv1;
244 for (fwb = STAILQ_FIRST(&d->binds); fwb != NULL;
245 fwb = STAILQ_FIRST(&d->binds)) {
246 fw_bindremove(fc, fwb);
247 STAILQ_REMOVE_HEAD(&d->binds, chlist);
248 fw_xferlist_remove(&fwb->xferlist);
252 struct fw_xferq *ir = d->ir;
254 if ((ir->flag & FWXFERQ_OPEN) == 0)
256 if (ir->flag & FWXFERQ_RUNNING) {
257 ir->flag &= ~FWXFERQ_RUNNING;
258 fc->irx_disable(fc, ir->dmach);
262 /* drain receiving buffer */
263 for (xfer = STAILQ_FIRST(&ir->q);
264 xfer != NULL; xfer = STAILQ_FIRST(&ir->q)) {
266 STAILQ_REMOVE_HEAD(&ir->q, link);
271 ir->flag &= ~(FWXFERQ_OPEN |
272 FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
277 struct fw_xferq *it = d->it;
279 if ((it->flag & FWXFERQ_OPEN) == 0)
281 if (it->flag & FWXFERQ_RUNNING) {
282 it->flag &= ~FWXFERQ_RUNNING;
283 fc->itx_disable(fc, it->dmach);
287 it->flag &= ~(FWXFERQ_OPEN |
288 FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
291 free(dev->si_drv1, M_FW);
298 fw_read_async(struct fw_drv1 *d, struct uio *uio, int ioflag)
301 struct fw_xfer *xfer;
304 struct tcode_info *tinfo;
307 while ((xfer = STAILQ_FIRST(&d->rq)) == NULL && err == 0)
308 err = msleep(&d->rq, FW_GMTX(d->fc), FWPRI, "fwra", 0);
316 STAILQ_REMOVE_HEAD(&d->rq, link);
317 FW_GUNLOCK(xfer->fc);
319 fp = &xfer->recv.hdr;
320 #if 0 /* for GASP ?? */
321 if (fc->irx_post != NULL)
322 fc->irx_post(fc, fp->mode.ld);
324 tinfo = &xfer->fc->tcode[fp->mode.hdr.tcode];
325 err = uiomove((void *)fp, tinfo->hdr_len, uio);
328 err = uiomove((void *)xfer->recv.payload, xfer->recv.pay_len, uio);
331 /* recycle this xfer */
332 fwb = (struct fw_bind *)xfer->sc;
333 fw_xfer_unload(xfer);
334 xfer->recv.pay_len = PAGE_SIZE;
336 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
337 FW_GUNLOCK(xfer->fc);
345 fw_read (struct cdev *dev, struct uio *uio, int ioflag)
349 struct firewire_comm *fc;
350 int err = 0, s, slept = 0;
354 return (physio(dev, uio, ioflag));
356 d = (struct fw_drv1 *)dev->si_drv1;
361 return (fw_read_async(d, uio, ioflag));
368 if (ir->stproc == NULL) {
370 ir->stproc = STAILQ_FIRST(&ir->stvalid);
371 if (ir->stproc != NULL) {
373 STAILQ_REMOVE_HEAD(&ir->stvalid, link);
378 if (ir->stproc == NULL) {
379 /* no data avaliable */
382 ir->flag |= FWXFERQ_WAKEUP;
383 err = msleep(ir, FW_GMTX(fc), FWPRI, "fw_read", hz);
384 ir->flag &= ~FWXFERQ_WAKEUP;
387 } else if (slept == 1)
391 } else if(ir->stproc != NULL) {
394 fp = (struct fw_pkt *)fwdma_v_addr(ir->buf,
395 ir->stproc->poffset + ir->queued);
396 if(fc->irx_post != NULL)
397 fc->irx_post(fc, fp->mode.ld);
398 if(fp->mode.stream.len == 0){
402 err = uiomove((caddr_t)fp,
403 fp->mode.stream.len + sizeof(uint32_t), uio);
405 if(ir->queued >= ir->bnpacket){
407 STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
409 fc->irx_enable(fc, ir->dmach);
412 if (uio->uio_resid >= ir->psize) {
422 fw_write_async(struct fw_drv1 *d, struct uio *uio, int ioflag)
424 struct fw_xfer *xfer;
426 struct tcode_info *tinfo;
429 bzero(&pkt, sizeof(struct fw_pkt));
430 if ((err = uiomove((caddr_t)&pkt, sizeof(uint32_t), uio)))
432 tinfo = &d->fc->tcode[pkt.mode.hdr.tcode];
433 if ((err = uiomove((caddr_t)&pkt + sizeof(uint32_t),
434 tinfo->hdr_len - sizeof(uint32_t), uio)))
437 if ((xfer = fw_xfer_alloc_buf(M_FWXFER, uio->uio_resid,
438 PAGE_SIZE/*XXX*/)) == NULL)
441 bcopy(&pkt, &xfer->send.hdr, sizeof(struct fw_pkt));
442 xfer->send.pay_len = uio->uio_resid;
443 if (uio->uio_resid > 0) {
444 if ((err = uiomove((caddr_t)&xfer->send.payload[0],
445 uio->uio_resid, uio)))
451 xfer->hand = fw_xferwake;
452 xfer->send.spd = 2 /* XXX */;
454 if ((err = fw_asyreq(xfer->fc, -1, xfer)))
457 if ((err = fw_xferwait(xfer)))
460 if (xfer->resp != 0) {
465 if (xfer->flag & FWXF_RCVD) {
467 STAILQ_INSERT_TAIL(&d->rq, xfer, link);
468 FW_GUNLOCK(xfer->fc);
478 fw_write (struct cdev *dev, struct uio *uio, int ioflag)
484 struct firewire_comm *fc;
488 return (physio(dev, uio, ioflag));
490 d = (struct fw_drv1 *)dev->si_drv1;
495 return (fw_write_async(d, uio, ioflag));
502 if (it->stproc == NULL) {
503 it->stproc = STAILQ_FIRST(&it->stfree);
504 if (it->stproc != NULL) {
506 STAILQ_REMOVE_HEAD(&it->stfree, link);
509 } else if (slept == 0) {
511 #if 0 /* XXX to avoid lock recursion */
512 err = fc->itx_enable(fc, it->dmach);
516 err = msleep(it, FW_GMTX(fc), FWPRI, "fw_write", hz);
526 fp = (struct fw_pkt *)fwdma_v_addr(it->buf,
527 it->stproc->poffset + it->queued);
528 err = uiomove((caddr_t)fp, sizeof(struct fw_isohdr), uio);
529 err = uiomove((caddr_t)fp->mode.stream.payload,
530 fp->mode.stream.len, uio);
532 if (it->queued >= it->bnpacket) {
534 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
537 err = fc->itx_enable(fc, it->dmach);
539 if (uio->uio_resid >= sizeof(struct fw_isohdr)) {
552 fw_hand(struct fw_xfer *xfer)
557 fwb = (struct fw_bind *)xfer->sc;
558 d = (struct fw_drv1 *)fwb->sc;
560 STAILQ_INSERT_TAIL(&d->rq, xfer, link);
561 FW_GUNLOCK(xfer->fc);
569 fw_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
571 struct firewire_comm *fc;
574 struct fw_device *fwdev;
576 struct fw_xferq *ir, *it;
577 struct fw_xfer *xfer;
579 struct fw_devinfo *devinfo;
582 struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data;
583 struct fw_asyreq *asyreq = (struct fw_asyreq *)data;
584 struct fw_isochreq *ichreq = (struct fw_isochreq *)data;
585 struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data;
586 struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data;
587 struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data;
590 return fwmem_ioctl(dev, cmd, data, flag, td);
595 d = (struct fw_drv1 *)dev->si_drv1;
603 i = fw_open_isodma(fc, /* tx */1);
609 err = fwdev_allocbuf(fc, it, &d->bufreq.tx);
611 it->flag &= ~FWXFERQ_OPEN;
616 it->flag |= (0x3f & ichreq->ch);
617 it->flag |= ((0x3 & ichreq->tag) << 6);
622 ichreq->ch = it->flag & 0x3f;
623 ichreq->tag = it->flag >> 2 & 0x3;
629 i = fw_open_isodma(fc, /* tx */0);
635 err = fwdev_allocbuf(fc, ir, &d->bufreq.rx);
637 ir->flag &= ~FWXFERQ_OPEN;
642 ir->flag |= (0x3f & ichreq->ch);
643 ir->flag |= ((0x3 & ichreq->tag) << 6);
645 err = fc->irx_enable(fc, ir->dmach);
649 ichreq->ch = ir->flag & 0x3f;
650 ichreq->tag = ir->flag >> 2 & 0x3;
655 bcopy(ibufreq, &d->bufreq, sizeof(d->bufreq));
658 bzero(&ibufreq->rx, sizeof(ibufreq->rx));
660 ibufreq->rx.nchunk = ir->bnchunk;
661 ibufreq->rx.npacket = ir->bnpacket;
662 ibufreq->rx.psize = ir->psize;
664 bzero(&ibufreq->tx, sizeof(ibufreq->tx));
666 ibufreq->tx.nchunk = it->bnchunk;
667 ibufreq->tx.npacket = it->bnpacket;
668 ibufreq->tx.psize = it->psize;
673 struct tcode_info *tinfo;
677 tinfo = &fc->tcode[fp->mode.hdr.tcode];
679 if ((tinfo->flag & FWTI_BLOCK_ASY) != 0)
680 pay_len = MAX(0, asyreq->req.len - tinfo->hdr_len);
682 xfer = fw_xfer_alloc_buf(M_FWXFER, pay_len, PAGE_SIZE/*XXX*/);
686 switch (asyreq->req.type) {
690 fwdev = fw_noderesolve_eui64(fc,
691 &asyreq->req.dst.eui);
693 device_printf(fc->bdev,
694 "cannot find node\n");
698 fp->mode.hdr.dst = FWLOCALBUS | fwdev->dst;
701 /* XXX what's this? */
708 bcopy(fp, (void *)&xfer->send.hdr, tinfo->hdr_len);
710 bcopy((char *)fp + tinfo->hdr_len,
711 (void *)xfer->send.payload, pay_len);
712 xfer->send.spd = asyreq->req.sped;
713 xfer->hand = fw_xferwake;
715 if ((err = fw_asyreq(fc, -1, xfer)) != 0)
717 if ((err = fw_xferwait(xfer)) != 0)
719 if (xfer->resp != 0) {
723 if ((tinfo->flag & FWTI_TLABEL) == 0)
727 tinfo = &fc->tcode[xfer->recv.hdr.mode.hdr.tcode];
728 if (xfer->recv.hdr.mode.hdr.tcode == FWTCODE_RRESB ||
729 xfer->recv.hdr.mode.hdr.tcode == FWTCODE_LRES) {
730 pay_len = xfer->recv.pay_len;
731 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len) {
732 asyreq->req.len = xfer->recv.pay_len +
741 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len);
742 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len, pay_len);
744 fw_xfer_free_buf(xfer);
751 fwb = fw_bindlookup(fc,
752 bindreq->start.hi, bindreq->start.lo);
757 fw_bindremove(fc, fwb);
758 STAILQ_REMOVE(&d->binds, fwb, fw_bind, chlist);
759 fw_xferlist_remove(&fwb->xferlist);
763 if(bindreq->len <= 0 ){
767 if(bindreq->start.hi > 0xffff ){
771 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_WAITOK);
776 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
778 fwb->end = fwb->start + bindreq->len;
780 STAILQ_INIT(&fwb->xferlist);
781 err = fw_bindadd(fc, fwb);
783 fw_xferlist_add(&fwb->xferlist, M_FWXFER,
785 PAGE_SIZE, PAGE_SIZE, 5,
786 fc, (void *)fwb, fw_hand);
787 STAILQ_INSERT_TAIL(&d->binds, fwb, chlist);
793 devinfo = &fwdevlst->dev[0];
794 devinfo->dst = fc->nodeid;
795 devinfo->status = 0; /* XXX */
796 devinfo->eui.hi = fc->eui.hi;
797 devinfo->eui.lo = fc->eui.lo;
798 STAILQ_FOREACH(fwdev, &fc->devices, link) {
799 if(len < FW_MAX_DEVLST){
800 devinfo = &fwdevlst->dev[len++];
801 devinfo->dst = fwdev->dst;
803 (fwdev->status == FWDEVINVAL)?0:1;
804 devinfo->eui.hi = fwdev->eui.hi;
805 devinfo->eui.lo = fwdev->eui.lo;
810 fwdevlst->info_len = len;
813 bcopy(fc->topology_map, data,
814 (fc->topology_map->crc_len + 1) * 4);
817 STAILQ_FOREACH(fwdev, &fc->devices, link)
818 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
821 if (!FW_EUI64_EQUAL(fc->eui, crom_buf->eui)) {
826 ptr = malloc(CROMSIZE, M_FW, M_WAITOK);
828 for (i = 0; i < CROMSIZE/4; i++)
830 = ntohl(fc->config_rom[i]);
833 ptr = (void *)&fwdev->csrrom[0];
834 if (fwdev->rommax < CSRROMOFF)
837 len = fwdev->rommax - CSRROMOFF + 4;
839 if (crom_buf->len < len)
843 err = copyout(ptr, crom_buf->ptr, len);
849 fc->ioctl (dev, cmd, data, flag, td);
855 fw_poll(struct cdev *dev, int events, fw_proc *td)
862 return fwmem_poll(dev, events, td);
864 ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
866 tmp = POLLIN | POLLRDNORM;
868 if (STAILQ_FIRST(&ir->q) != NULL)
871 selrecord(td, &ir->rsel);
873 tmp = POLLOUT | POLLWRNORM;
875 /* XXX should be fixed */
883 #if defined(__DragonFly__) || __FreeBSD_version < 500102
884 fw_mmap (struct cdev *dev, vm_offset_t offset, int nproto)
886 fw_mmap (struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
887 int nproto, vm_memattr_t *memattr)
892 #if defined(__DragonFly__) || __FreeBSD_version < 500102
893 return fwmem_mmap(dev, offset, nproto);
895 return fwmem_mmap(dev, offset, paddr, nproto, memattr);
902 fw_strategy(struct bio *bp)
907 if (DEV_FWMEM(dev)) {
912 bp->bio_error = EOPNOTSUPP;
913 bp->bio_flags |= BIO_ERROR;
914 bp->bio_resid = bp->bio_bcount;
919 fwdev_makedev(struct firewire_softc *sc)
923 #if defined(__DragonFly__) || __FreeBSD_version < 500000
924 cdevsw_add(&firewire_cdevsw);
929 unit = device_get_unit(sc->fc->bdev);
930 sc->dev = make_dev(&firewire_cdevsw, MAKEMINOR(0, unit, 0),
931 UID_ROOT, GID_OPERATOR, 0660,
933 d = make_dev(&firewire_cdevsw,
934 MAKEMINOR(FWMEM_FLAG, unit, 0),
935 UID_ROOT, GID_OPERATOR, 0660,
936 "fwmem%d.%d", unit, 0);
937 dev_depends(sc->dev, d);
938 make_dev_alias(sc->dev, "fw%d", unit);
939 make_dev_alias(d, "fwmem%d", unit);
946 fwdev_destroydev(struct firewire_softc *sc)
950 #if defined(__DragonFly__) || __FreeBSD_version < 500000
951 cdevsw_remove(&firewire_cdevsw);
953 destroy_dev(sc->dev);
958 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
961 fwdev_clone(void *arg, struct ucred *cred, char *name, int namelen,
964 struct firewire_softc *sc;
965 char *devnames[NDEVTYPE] = {"fw", "fwmem"};
967 int devflag[NDEVTYPE] = {0, FWMEM_FLAG};
968 int i, unit = 0, sub = 0;
973 for (i = 0; i < NDEVTYPE; i++)
974 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2)
980 if (subp == NULL || *subp++ != '.')
984 while (isdigit(*subp)) {
986 sub += *subp++ - '0';
991 sc = devclass_get_softc(firewire_devclass, unit);
994 *dev = make_dev_credf(MAKEDEV_REF, &firewire_cdevsw,
995 MAKEMINOR(devflag[i], unit, sub), cred, UID_ROOT, GID_OPERATOR,
996 0660, "%s%d.%d", devnames[i], unit, sub);
997 dev_depends(sc->dev, *dev);