diff options
-rw-r--r-- | sys/conf/files | 1 | ||||
-rw-r--r-- | sys/dev/firewire/firewire.c | 876 | ||||
-rw-r--r-- | sys/dev/firewire/firewirereg.h | 2 | ||||
-rw-r--r-- | sys/dev/firewire/fwdev.c | 916 | ||||
-rw-r--r-- | sys/modules/firewire/firewire/Makefile | 10 |
5 files changed, 925 insertions, 880 deletions
diff --git a/sys/conf/files b/sys/conf/files index 119adf6..c2dbd2a 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -385,6 +385,7 @@ dev/firewire/firewire.c optional firewire dev/firewire/fwohci.c optional firewire dev/firewire/fwohci_pci.c optional firewire pci dev/firewire/fwmem.c optional firewire +dev/firewire/fwdev.c optional firewire dev/firewire/if_fwe.c optional fwe dev/firewire/sbp.c optional sbp dev/fxp/if_fxp.c optional fxp diff --git a/sys/dev/firewire/firewire.c b/sys/dev/firewire/firewire.c index 9c43eda..27eb16c 100644 --- a/sys/dev/firewire/firewire.c +++ b/sys/dev/firewire/firewire.c @@ -46,30 +46,14 @@ #include <sys/conf.h> #include <sys/uio.h> #include <sys/sysctl.h> -#include <sys/poll.h> #include <machine/cpufunc.h> /* for rdtsc proto for clock.h below */ #include <machine/clock.h> -#include <pci/pcivar.h> -#include <pci/pcireg.h> - -#include <vm/vm.h> -#include <vm/pmap.h> /* for vtophys proto */ -#include <vm/vm_extern.h> #include <sys/bus.h> /* used by smbus and newbus */ -#include <machine/bus.h> /* used by newbus */ -#include <sys/rman.h> /* used by newbus */ -#include <machine/resource.h> /* used by newbus */ - -#include <sys/signal.h> -#include <sys/mman.h> -#include <sys/ioccom.h> - #include <dev/firewire/firewire.h> #include <dev/firewire/firewirereg.h> -#include <dev/firewire/fwmem.h> #include <dev/firewire/iec13213.h> #include <dev/firewire/iec68113.h> @@ -78,24 +62,13 @@ SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "Firewire Subsystem"); SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, "Firewire driver debug flag"); -#define CDEV_MAJOR 127 #define FW_MAXASYRTY 4 #define FW_MAXDEVRCNT 4 -#define FWNODE_INVAL 0xffff #define XFER_TIMEOUT 0 -static d_open_t fw_open; -static d_close_t fw_close; -static d_ioctl_t fw_ioctl; -static d_poll_t fw_poll; -static d_read_t fw_read; /* for Isochronous packet */ -static d_write_t fw_write; -static d_mmap_t fw_mmap; - devclass_t firewire_devclass; - static int firewire_match __P((device_t)); static int firewire_attach __P((device_t)); static int firewire_detach __P((device_t)); @@ -103,7 +76,6 @@ static int firewire_detach __P((device_t)); static int firewire_shutdown __P((device_t)); #endif static device_t firewire_add_child __P((device_t, int, const char *, int)); -static struct fw_bind *fw_bindlookup __P((struct firewire_comm *, u_int32_t, u_int32_t)); static void fw_try_bmr __P((void *)); static void fw_try_bmr_callback __P((struct fw_xfer *)); static void fw_asystart __P((struct fw_xfer *)); @@ -137,441 +109,15 @@ u_int maxrec[6]={512,1024,2048,4096,8192,0}; #define MAX_GAPHOP 16 u_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17, 20, 23, 25, 28, 31, 33, 36, 39, 42}; -/* - * The probe routine. - */ -struct cdevsw firewire_cdevsw = -{ - fw_open, fw_close, fw_read, fw_write, fw_ioctl, - fw_poll, fw_mmap, nostrategy, "fw", CDEV_MAJOR, nodump, nopsize, D_MEM -}; + +extern struct cdevsw firewire_cdevsw; + static driver_t firewire_driver = { "firewire", firewire_methods, sizeof(struct firewire_softc), }; -static int -fw_open (dev_t dev, int flags, int fmt, fw_proc *td) -{ - struct firewire_softc *sc; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - - int err = 0; - - if (DEV_FWMEM(dev)) - return fwmem_open(dev, flags, fmt, td); - - sc = devclass_get_softc(firewire_devclass, unit); - if(sc->fc->ir[sub]->flag & FWXFERQ_OPEN){ - err = EBUSY; - return err; - } - if(sc->fc->it[sub]->flag & FWXFERQ_OPEN){ - err = EBUSY; - return err; - } - if(sc->fc->ir[sub]->flag & FWXFERQ_MODEMASK){ - err = EBUSY; - return err; - } -/* Default is per packet mode */ - sc->fc->ir[sub]->flag |= FWXFERQ_OPEN; - sc->fc->it[sub]->flag |= FWXFERQ_OPEN; - sc->fc->ir[sub]->flag |= FWXFERQ_PACKET; - return err; -} -static int -fw_close (dev_t dev, int flags, int fmt, fw_proc *td) -{ - struct firewire_softc *sc; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - struct fw_xfer *xfer; - struct fw_dvbuf *dvbuf; - struct fw_bind *fwb; - int err = 0; - - if (DEV_FWMEM(dev)) - return fwmem_close(dev, flags, fmt, td); - - sc = devclass_get_softc(firewire_devclass, unit); - if(!(sc->fc->ir[sub]->flag & FWXFERQ_OPEN)){ - err = EINVAL; - return err; - } - sc->fc->ir[sub]->flag &= ~FWXFERQ_OPEN; - if(!(sc->fc->it[sub]->flag & FWXFERQ_OPEN)){ - err = EINVAL; - return err; - } - sc->fc->it[sub]->flag &= ~FWXFERQ_OPEN; - - if(sc->fc->ir[sub]->flag & FWXFERQ_RUNNING){ - sc->fc->irx_disable(sc->fc, sub); - } - if(sc->fc->it[sub]->flag & FWXFERQ_RUNNING){ - sc->fc->it[sub]->flag &= ~FWXFERQ_RUNNING; - sc->fc->itx_disable(sc->fc, sub); - } - if(sc->fc->it[sub]->flag & FWXFERQ_DV){ - if((dvbuf = sc->fc->it[sub]->dvproc) != NULL){ - free(dvbuf->buf, M_DEVBUF); - sc->fc->it[sub]->dvproc = NULL; - } - if((dvbuf = sc->fc->it[sub]->dvdma) != NULL){ - free(dvbuf->buf, M_DEVBUF); - sc->fc->it[sub]->dvdma = NULL; - } - while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvvalid)) != NULL){ - STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvvalid, link); - free(dvbuf->buf, M_DEVBUF); - } - while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvfree)) != NULL){ - STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvfree, link); - free(dvbuf->buf, M_DEVBUF); - } - free(sc->fc->it[sub]->dvbuf, M_DEVBUF); - sc->fc->it[sub]->dvbuf = NULL; - } - if(sc->fc->ir[sub]->flag & FWXFERQ_EXTBUF){ - free(sc->fc->ir[sub]->buf, M_DEVBUF); - sc->fc->ir[sub]->buf = NULL; - free(sc->fc->ir[sub]->bulkxfer, M_DEVBUF); - sc->fc->ir[sub]->bulkxfer = NULL; - sc->fc->ir[sub]->flag &= ~FWXFERQ_EXTBUF; - sc->fc->ir[sub]->psize = FWPMAX_S400; - sc->fc->ir[sub]->maxq = FWMAXQUEUE; - } - if(sc->fc->it[sub]->flag & FWXFERQ_EXTBUF){ - free(sc->fc->it[sub]->buf, M_DEVBUF); - sc->fc->it[sub]->buf = NULL; - free(sc->fc->it[sub]->bulkxfer, M_DEVBUF); - sc->fc->it[sub]->bulkxfer = NULL; - sc->fc->it[sub]->dvbuf = NULL; - sc->fc->it[sub]->flag &= ~FWXFERQ_EXTBUF; - sc->fc->it[sub]->psize = FWPMAX_S400; - sc->fc->it[sub]->maxq = FWMAXQUEUE; - } - for(xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q); - xfer != NULL; xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q)){ - sc->fc->ir[sub]->queued--; - STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->q, link); - - xfer->resp = 0; - switch(xfer->act_type){ - case FWACT_XFER: - fw_xfer_done(xfer); - break; - default: - break; - } - fw_xfer_free(xfer); - } - for(fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds); fwb != NULL; - fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds)){ - STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist); - STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->binds, chlist); - free(fwb, M_DEVBUF); - } - sc->fc->ir[sub]->flag &= ~FWXFERQ_MODEMASK; - sc->fc->it[sub]->flag &= ~FWXFERQ_MODEMASK; - return err; -} -/* - * read request. - */ -static int -fw_read (dev_t dev, struct uio *uio, int ioflag) -{ - struct firewire_softc *sc; - struct fw_xferq *ir; - struct fw_xfer *xfer; - int err = 0, s, slept = 0; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - struct fw_pkt *fp; - - if (DEV_FWMEM(dev)) - return fwmem_read(dev, uio, ioflag); - - sc = devclass_get_softc(firewire_devclass, unit); - - ir = sc->fc->ir[sub]; - - if(ir->flag & FWXFERQ_PACKET){ - ir->stproc = NULL; - } -readloop: - xfer = STAILQ_FIRST(&ir->q); - if(!(ir->flag & FWXFERQ_PACKET) && ir->stproc == NULL){ - ir->stproc = STAILQ_FIRST(&ir->stvalid); - if(ir->stproc != NULL){ - s = splfw(); - STAILQ_REMOVE_HEAD(&ir->stvalid, link); - splx(s); - ir->queued = 0; - } - } - - if(xfer == NULL && ir->stproc == NULL){ - if(slept == 0){ - slept = 1; - if(!(ir->flag & FWXFERQ_RUNNING) - && (ir->flag & FWXFERQ_PACKET)){ - err = sc->fc->irx_enable(sc->fc, sub); - } - if(err){ - return err; - } - ir->flag |= FWXFERQ_WAKEUP; - err = tsleep((caddr_t)ir, FWPRI, "fw_read", hz); - if(err){ - ir->flag &= ~FWXFERQ_WAKEUP; - return err; - } - goto readloop; - }else{ - err = EIO; - return err; - } - }else if(xfer != NULL){ - s = splfw(); - ir->queued --; - STAILQ_REMOVE_HEAD(&ir->q, link); - splx(s); - fp = (struct fw_pkt *)(xfer->recv.buf + xfer->recv.off); - if(sc->fc->irx_post != NULL) - sc->fc->irx_post(sc->fc, fp->mode.ld); - err = uiomove(xfer->recv.buf + xfer->recv.off, xfer->recv.len, uio); - fw_xfer_free( xfer); - }else if(ir->stproc != NULL){ - fp = (struct fw_pkt *)(ir->stproc->buf + ir->queued * ir->psize); - if(sc->fc->irx_post != NULL) - sc->fc->irx_post(sc->fc, fp->mode.ld); - if(ntohs(fp->mode.stream.len) == 0){ - err = EIO; - return err; - } - err = uiomove((caddr_t)fp, ntohs(fp->mode.stream.len) + sizeof(u_int32_t), uio); - fp->mode.stream.len = 0; - ir->queued ++; - if(ir->queued >= ir->bnpacket){ - s = splfw(); - ir->stproc->flag = 0; - STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link); - splx(s); - ir->stproc = NULL; - } - } -#if 0 - if(STAILQ_FIRST(&ir->q) == NULL && - (ir->flag & FWXFERQ_RUNNING) && (ir->flag & FWXFERQ_PACKET)){ - err = sc->fc->irx_enable(sc->fc, sub); - } -#endif -#if 0 - if(STAILQ_FIRST(&ir->stvalid) == NULL && - (ir->flag & FWXFERQ_RUNNING) && !(ir->flag & FWXFERQ_PACKET)){ - err = sc->fc->irx_enable(sc->fc, sub); - } -#endif - return err; -} -static int -fw_write (dev_t dev, struct uio *uio, int ioflag) -{ - int err = 0; - struct firewire_softc *sc; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - int tl, s, slept = 0; - struct fw_pkt *fp; - struct fw_xfer *xfer; - struct fw_xferq *xferq; - struct firewire_comm *fc; - struct fw_xferq *it; - - if (DEV_FWMEM(dev)) - return fwmem_write(dev, uio, ioflag); - - sc = devclass_get_softc(firewire_devclass, unit); - fc = sc->fc; - it = sc->fc->it[sub]; - - fp = (struct fw_pkt *)uio->uio_iov->iov_base; - switch(fp->mode.common.tcode){ - case FWTCODE_RREQQ: - case FWTCODE_RREQB: - case FWTCODE_LREQ: - err = EINVAL; - return err; - case FWTCODE_WREQQ: - case FWTCODE_WREQB: - xferq = fc->atq; - break; - case FWTCODE_STREAM: - if(it->flag & FWXFERQ_PACKET){ - xferq = fc->atq; - }else{ - xferq = NULL; - } - break; - case FWTCODE_WRES: - case FWTCODE_RRESQ: - case FWTCODE_RRESB: - case FWTCODE_LRES: - xferq = fc->ats; - break; - default: - err = EINVAL; - return err; - } - /* Discard unsent buffered stream packet, when sending Asyrequrst */ - if(xferq != NULL && it->stproc != NULL){ - s = splfw(); - it->stproc->flag = 0; - STAILQ_INSERT_TAIL(&it->stfree, it->stproc, link); - splx(s); - it->stproc = NULL; - } - if(xferq == NULL && !(it->flag & FWXFERQ_DV)){ -isoloop: - if(it->stproc == NULL){ - it->stproc = STAILQ_FIRST(&it->stfree); - if(it->stproc != NULL){ - s = splfw(); - STAILQ_REMOVE_HEAD(&it->stfree, link); - splx(s); - it->queued = 0; - }else if(slept == 0){ - slept = 1; - err = sc->fc->itx_enable(sc->fc, sub); - if(err){ - return err; - } - err = tsleep((caddr_t)it, FWPRI, "fw_write", hz); - if(err){ - return err; - } - goto isoloop; - }else{ - err = EIO; - return err; - } - } - fp = (struct fw_pkt *)(it->stproc->buf + it->queued * it->psize); - fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t)); - err = uiomove(it->stproc->buf + it->queued * it->psize, - uio->uio_resid, uio); - it->queued ++; - if(it->queued >= it->btpacket){ - s = splfw(); - STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link); - splx(s); - it->stproc = NULL; - fw_tbuf_update(sc->fc, sub, 0); - err = sc->fc->itx_enable(sc->fc, sub); - } - return err; - } if(xferq == NULL && it->flag & FWXFERQ_DV){ -dvloop: - if(it->dvproc == NULL){ - it->dvproc = STAILQ_FIRST(&it->dvfree); - if(it->dvproc != NULL){ - s = splfw(); - STAILQ_REMOVE_HEAD(&it->dvfree, link); - splx(s); - it->dvptr = 0; - }else if(slept == 0){ - slept = 1; - err = sc->fc->itx_enable(sc->fc, sub); - if(err){ - return err; - } - err = tsleep((caddr_t)it, FWPRI, "fw_write", hz); - if(err){ - return err; - } - goto dvloop; - }else{ - err = EIO; - return err; - } - } - fp = (struct fw_pkt *)(it->dvproc->buf + it->queued * it->psize); - fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t)); - err = uiomove(it->dvproc->buf + it->dvptr, - uio->uio_resid, uio); - it->dvptr += it->psize; - if(err){ - return err; - } - if(it->dvptr >= it->psize * it->dvpacket){ - s = splfw(); - STAILQ_INSERT_TAIL(&it->dvvalid, it->dvproc, link); - splx(s); - it->dvproc = NULL; - err = fw_tbuf_update(sc->fc, sub, 0); - if(err){ - return err; - } - err = sc->fc->itx_enable(sc->fc, sub); - } - return err; - } - if(xferq != NULL){ - xfer = fw_xfer_alloc(); - if(xfer == NULL){ - err = ENOMEM; - return err; - } - xfer->send.buf = malloc(uio->uio_resid, M_DEVBUF, M_NOWAIT); - if(xfer->send.buf == NULL){ - fw_xfer_free( xfer); - err = ENOBUFS; - return err; - } - xfer->dst = ntohs(fp->mode.hdr.dst); - - switch(fp->mode.common.tcode){ - case FWTCODE_WREQQ: - case FWTCODE_WREQB: - if((tl = fw_get_tlabel(fc, xfer)) == -1 ){ - fw_xfer_free( xfer); - err = EAGAIN; - return err; - } - fp->mode.hdr.tlrt = tl << 2; - default: - break; - } - - xfer->tl = fp->mode.hdr.tlrt >> 2; - xfer->send.len = uio->uio_resid; - xfer->send.off = 0; - xfer->tcode = fp->mode.common.tcode; - xfer->spd = 0;/* XXX: how to setup it */ - xfer->fc = fc; - xfer->q = xferq; - xfer->act_type = FWACT_XFER; - xfer->act.hand = fw_asy_callback; - xfer->retry_req = fw_asybusy; - - err = uiomove(xfer->send.buf, uio->uio_resid, uio); - if(err){ - return err; - } - fw_asystart(xfer); - err = tsleep((caddr_t)xfer, FWPRI, "fw_write", hz); - if(xfer->resp == EBUSY) - return EBUSY; - fw_xfer_free( xfer); - return err; - } - return EINVAL; -} /* * transmitter buffer update. */ @@ -751,405 +297,6 @@ fw_rbuf_update(struct firewire_comm *fc, int sub, int flag){ ir->stdma2 = bulkxfer2; return err; } -/* - * ioctl support. - */ -int -fw_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td) -{ - struct firewire_softc *sc; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - int i, len, err = 0; - struct fw_device *fwdev; - struct fw_bind *fwb; - struct fw_xferq *ir, *it; - struct fw_xfer *xfer; - struct fw_pkt *fp; - - struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data; - struct fw_asyreq *asyreq = (struct fw_asyreq *)data; - struct fw_isochreq *ichreq = (struct fw_isochreq *)data; - struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data; - struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data; -#if 0 - struct fw_map_buf *map_buf = (struct fw_map_buf *)data; -#endif - struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data; - - if (DEV_FWMEM(dev)) - return fwmem_ioctl(dev, cmd, data, flag, td); - - sc = devclass_get_softc(firewire_devclass, unit); - if (!data) - return(EINVAL); - - switch (cmd) { - case FW_STSTREAM: - sc->fc->it[sub]->flag &= ~0xff; - sc->fc->it[sub]->flag |= (0x3f & ichreq->ch); - sc->fc->it[sub]->flag |= ((0x3 & ichreq->tag) << 6); - err = 0; - break; - case FW_GTSTREAM: - ichreq->ch = sc->fc->it[sub]->flag & 0x3f; - ichreq->tag =(sc->fc->it[sub]->flag) >> 2 & 0x3; - err = 0; - break; - case FW_SRSTREAM: - sc->fc->ir[sub]->flag &= ~0xff; - sc->fc->ir[sub]->flag |= (0x3f & ichreq->ch); - sc->fc->ir[sub]->flag |= ((0x3 & ichreq->tag) << 6); - err = sc->fc->irx_enable(sc->fc, sub); - break; - case FW_GRSTREAM: - ichreq->ch = sc->fc->ir[sub]->flag & 0x3f; - ichreq->tag =(sc->fc->ir[sub]->flag) >> 2 & 0x3; - err = 0; - break; - case FW_SSTDV: - ibufreq = (struct fw_isobufreq *) - malloc(sizeof(struct fw_isobufreq), M_DEVBUF, M_NOWAIT); - if(ibufreq == NULL){ - err = ENOMEM; - break; - } -#define FWDVPACKET 250 -#define FWDVPMAX 512 - ibufreq->rx.nchunk = 8; - ibufreq->rx.npacket = 50; - ibufreq->rx.psize = FWDVPMAX; - - ibufreq->tx.nchunk = 5; - ibufreq->tx.npacket = 300; - ibufreq->tx.psize = FWDVPMAX; - - err = fw_ioctl(dev, FW_SSTBUF, (caddr_t)ibufreq, flag, td); - sc->fc->it[sub]->dvpacket = FWDVPACKET; - free(ibufreq, M_DEVBUF); -/* reserve a buffer space */ -#define NDVCHUNK 8 - sc->fc->it[sub]->dvproc = NULL; - sc->fc->it[sub]->dvdma = NULL; - sc->fc->it[sub]->flag |= FWXFERQ_DV; - sc->fc->it[sub]->dvbuf - = (struct fw_dvbuf *)malloc(sizeof(struct fw_dvbuf) * NDVCHUNK, M_DEVBUF, M_DONTWAIT); - STAILQ_INIT(&sc->fc->it[sub]->dvvalid); - STAILQ_INIT(&sc->fc->it[sub]->dvfree); - for( i = 0 ; i < NDVCHUNK ; i++){ - sc->fc->it[sub]->dvbuf[i].buf - = malloc(FWDVPMAX * sc->fc->it[sub]->dvpacket, M_DEVBUF, M_DONTWAIT); - STAILQ_INSERT_TAIL(&sc->fc->it[sub]->dvfree, - &sc->fc->it[sub]->dvbuf[i], link); - } - break; - case FW_SSTBUF: - ir = sc->fc->ir[sub]; - it = sc->fc->it[sub]; - - if(ir->flag & FWXFERQ_RUNNING || it->flag & FWXFERQ_RUNNING){ - return(EBUSY); - } - if((ir->flag & FWXFERQ_EXTBUF) || (it->flag & FWXFERQ_EXTBUF)){ - return(EBUSY); - } - if((ibufreq->rx.nchunk * - ibufreq->rx.psize * ibufreq->rx.npacket) + - (ibufreq->tx.nchunk * - ibufreq->tx.psize * ibufreq->tx.npacket) <= 0){ - return(EINVAL); - } - if(ibufreq->rx.nchunk > FWSTMAXCHUNK || - ibufreq->tx.nchunk > FWSTMAXCHUNK){ - return(EINVAL); - } - ir->bulkxfer - = (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->rx.nchunk, M_DEVBUF, M_DONTWAIT); - if(ir->bulkxfer == NULL){ - return(ENOMEM); - } - it->bulkxfer - = (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->tx.nchunk, M_DEVBUF, M_DONTWAIT); - if(it->bulkxfer == NULL){ - return(ENOMEM); - } - ir->buf = malloc( - ibufreq->rx.nchunk * ibufreq->rx.npacket - * ((ibufreq->rx.psize + 3) &~3), - M_DEVBUF, M_DONTWAIT); - if(ir->buf == NULL){ - free(ir->bulkxfer, M_DEVBUF); - free(it->bulkxfer, M_DEVBUF); - ir->bulkxfer = NULL; - it->bulkxfer = NULL; - it->buf = NULL; - return(ENOMEM); - } - it->buf = malloc( - ibufreq->tx.nchunk * ibufreq->tx.npacket - * ((ibufreq->tx.psize + 3) &~3), - M_DEVBUF, M_DONTWAIT); - if(it->buf == NULL){ - free(ir->bulkxfer, M_DEVBUF); - free(it->bulkxfer, M_DEVBUF); - free(ir->buf, M_DEVBUF); - ir->bulkxfer = NULL; - it->bulkxfer = NULL; - it->buf = NULL; - return(ENOMEM); - } - - ir->bnchunk = ibufreq->rx.nchunk; - ir->bnpacket = ibufreq->rx.npacket; - ir->btpacket = ibufreq->rx.npacket; - ir->psize = (ibufreq->rx.psize + 3) & ~3; - ir->queued = 0; - - it->bnchunk = ibufreq->tx.nchunk; - it->bnpacket = ibufreq->tx.npacket; - it->btpacket = ibufreq->tx.npacket; - it->psize = (ibufreq->tx.psize + 3) & ~3; - ir->queued = 0; - it->dvdbc = 0; - it->dvdiff = 0; - it->dvsync = 0; - - STAILQ_INIT(&ir->stvalid); - STAILQ_INIT(&ir->stfree); - ir->stdma = NULL; - ir->stdma2 = NULL; - ir->stproc = NULL; - - STAILQ_INIT(&it->stvalid); - STAILQ_INIT(&it->stfree); - it->stdma = NULL; - it->stdma2 = NULL; - it->stproc = NULL; - - for(i = 0 ; i < sc->fc->ir[sub]->bnchunk; i++){ - ir->bulkxfer[i].buf = - ir->buf + - i * sc->fc->ir[sub]->bnpacket * - sc->fc->ir[sub]->psize; - ir->bulkxfer[i].flag = 0; - STAILQ_INSERT_TAIL(&ir->stfree, - &ir->bulkxfer[i], link); - ir->bulkxfer[i].npacket = ir->bnpacket; - } - for(i = 0 ; i < sc->fc->it[sub]->bnchunk; i++){ - it->bulkxfer[i].buf = - it->buf + - i * sc->fc->it[sub]->bnpacket * - sc->fc->it[sub]->psize; - it->bulkxfer[i].flag = 0; - STAILQ_INSERT_TAIL(&it->stfree, - &it->bulkxfer[i], link); - it->bulkxfer[i].npacket = it->bnpacket; - } - ir->flag &= ~FWXFERQ_MODEMASK; - ir->flag |= FWXFERQ_STREAM; - ir->flag |= FWXFERQ_EXTBUF; - - it->flag &= ~FWXFERQ_MODEMASK; - it->flag |= FWXFERQ_STREAM; - it->flag |= FWXFERQ_EXTBUF; - err = 0; - break; - case FW_GSTBUF: - ibufreq->rx.nchunk = sc->fc->ir[sub]->bnchunk; - ibufreq->rx.npacket = sc->fc->ir[sub]->bnpacket; - ibufreq->rx.psize = sc->fc->ir[sub]->psize; - - ibufreq->tx.nchunk = sc->fc->it[sub]->bnchunk; - ibufreq->tx.npacket = sc->fc->it[sub]->bnpacket; - ibufreq->tx.psize = sc->fc->it[sub]->psize; - break; - case FW_ASYREQ: - xfer = fw_xfer_alloc(); - if(xfer == NULL){ - err = ENOMEM; - return err; - } - fp = &asyreq->pkt; - switch (asyreq->req.type) { - case FWASREQNODE: - xfer->dst = ntohs(fp->mode.hdr.dst); - break; - case FWASREQEUI: - fwdev = fw_noderesolve(sc->fc, asyreq->req.dst.eui); - if (fwdev == NULL) { - printf("%s:cannot found node\n", - device_get_nameunit(sc->fc->dev)); - err = EINVAL; - goto error; - } - xfer->dst = fwdev->dst; - fp->mode.hdr.dst = htons(FWLOCALBUS | xfer->dst); - break; - case FWASRESTL: - /* XXX what's this? */ - break; - case FWASREQSTREAM: - /* nothing to do */ - break; - } - xfer->spd = asyreq->req.sped; - xfer->send.len = asyreq->req.len; - xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT); - if(xfer->send.buf == NULL){ - return ENOMEM; - } - xfer->send.off = 0; - bcopy(fp, xfer->send.buf, xfer->send.len); - xfer->act.hand = fw_asy_callback; - err = fw_asyreq(sc->fc, sub, xfer); - if(err){ - fw_xfer_free( xfer); - return err; - } - err = tsleep((caddr_t)xfer, FWPRI, "asyreq", hz); - if(err == 0){ - if(asyreq->req.len >= xfer->recv.len){ - asyreq->req.len = xfer->recv.len; - }else{ - err = EINVAL; - } - bcopy(xfer->recv.buf + xfer->recv.off, fp, asyreq->req.len); - } -error: - fw_xfer_free( xfer); - break; - case FW_IBUSRST: - sc->fc->ibr(sc->fc); - break; - case FW_CBINDADDR: - fwb = fw_bindlookup(sc->fc, - bindreq->start.hi, bindreq->start.lo); - if(fwb == NULL){ - err = EINVAL; - break; - } - STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist); - STAILQ_REMOVE(&sc->fc->ir[sub]->binds, fwb, fw_bind, chlist); - free(fwb, M_DEVBUF); - break; - case FW_SBINDADDR: - if(bindreq->len <= 0 ){ - err = EINVAL; - break; - } - if(bindreq->start.hi > 0xffff ){ - err = EINVAL; - break; - } - fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT); - if(fwb == NULL){ - err = ENOMEM; - break; - } - fwb->start_hi = bindreq->start.hi; - fwb->start_lo = bindreq->start.lo; - fwb->addrlen = bindreq->len; - - xfer = fw_xfer_alloc(); - if(xfer == NULL){ - err = ENOMEM; - return err; - } - xfer->act_type = FWACT_CH; - xfer->sub = sub; - xfer->fc = sc->fc; - - fwb->xfer = xfer; - err = fw_bindadd(sc->fc, fwb); - break; - case FW_GDEVLST: - i = 0; - for(fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL; - fwdev = TAILQ_NEXT(fwdev, link)){ - if(i < fwdevlst->n){ - fwdevlst->dst[i] = fwdev->dst; - fwdevlst->status[i] = - (fwdev->status == FWDEVATTACHED)?1:0; - fwdevlst->eui[i].hi = fwdev->eui.hi; - fwdevlst->eui[i].lo = fwdev->eui.lo; - } - i++; - } - fwdevlst->n = i; - break; - case FW_GTPMAP: - bcopy(sc->fc->topology_map, data, - (sc->fc->topology_map->crc_len + 1) * 4); - break; - case FW_GSPMAP: - /* speed_map is larger than a page */ - err = copyout(sc->fc->speed_map, *(void **)data, - (sc->fc->speed_map->crc_len + 1) * 4); - break; - case FW_GCROM: - for (fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL; - fwdev = TAILQ_NEXT(fwdev, link)) { - if (fwdev->eui.hi == crom_buf->eui.hi && - fwdev->eui.lo == crom_buf->eui.lo) - break; - } - if (fwdev == NULL) { - err = FWNODE_INVAL; - break; - } -#if 0 - if (fwdev->csrrom[0] >> 24 == 1) - len = 4; - else - len = (1 + ((fwdev->csrrom[0] >> 16) & 0xff)) * 4; -#else - if (fwdev->rommax < CSRROMOFF) - len = 0; - else - len = fwdev->rommax - CSRROMOFF + 4; -#endif - if (crom_buf->len < len) - len = crom_buf->len; - else - crom_buf->len = len; - err = copyout(&fwdev->csrrom[0], crom_buf->ptr, len); - break; - default: - sc->fc->ioctl (dev, cmd, data, flag, td); - break; - } - return err; -} -int -fw_poll(dev_t dev, int events, fw_proc *td) -{ - int revents; - int tmp; - int unit = DEV2UNIT(dev); - int sub = DEV2DMACH(dev); - struct firewire_softc *sc; - - if (DEV_FWMEM(dev)) - return fwmem_poll(dev, events, td); - - sc = devclass_get_softc(firewire_devclass, unit); - revents = 0; - tmp = POLLIN | POLLRDNORM; - if (events & tmp) { - if (STAILQ_FIRST(&sc->fc->ir[sub]->q) != NULL) - revents |= tmp; - else - selrecord(td, &sc->fc->ir[sub]->rsel); - } - tmp = POLLOUT | POLLWRNORM; - if (events & tmp) { - /* XXX should be fixed */ - revents |= tmp; - } - - return revents; -} /* * To lookup node id. from EUI64. @@ -1168,6 +315,7 @@ fw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui) if(fwdev->status == FWDEVINVAL) return NULL; return fwdev; } + /* * Async. request procedure for userland application. */ @@ -1324,20 +472,6 @@ fw_asystart(struct fw_xfer *xfer) } static int -fw_mmap (dev_t dev, vm_offset_t offset, int nproto) -{ - struct firewire_softc *fc; - int unit = DEV2UNIT(dev); - - if (DEV_FWMEM(dev)) - return fwmem_mmap(dev, offset, nproto); - - fc = devclass_get_softc(firewire_devclass, unit); - - return EINVAL; -} - -static int firewire_match( device_t dev ) { device_set_desc(dev, "IEEE1394(Firewire) bus"); @@ -1684,7 +818,7 @@ void fw_init(struct firewire_comm *fc) /* * To lookup binded process from IEEE1394 address. */ -static struct fw_bind * +struct fw_bind * fw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo) { struct fw_bind *tfw; diff --git a/sys/dev/firewire/firewirereg.h b/sys/dev/firewire/firewirereg.h index 17e1aea..86bd43b 100644 --- a/sys/dev/firewire/firewirereg.h +++ b/sys/dev/firewire/firewirereg.h @@ -313,6 +313,8 @@ void fw_xfer_timeout __P((void *)); void fw_xfer_done __P((struct fw_xfer *)); void fw_asy_callback __P((struct fw_xfer *)); struct fw_device *fw_noderesolve __P((struct firewire_comm *, struct fw_eui64)); +struct fw_bind *fw_bindlookup __P((struct firewire_comm *, u_int32_t, u_int32_t)); + extern int firewire_debug; extern devclass_t firewire_devclass; diff --git a/sys/dev/firewire/fwdev.c b/sys/dev/firewire/fwdev.c new file mode 100644 index 0000000..66286a2 --- /dev/null +++ b/sys/dev/firewire/fwdev.c @@ -0,0 +1,916 @@ +/* + * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the acknowledgement as bellow: + * + * This product includes software developed by K. Kobayashi and H. Shimokawa + * + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + * + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/types.h> +#include <sys/mbuf.h> + +#include <sys/kernel.h> +#include <sys/malloc.h> +#include <sys/conf.h> +#include <sys/uio.h> +#include <sys/poll.h> + +#include <sys/bus.h> + +#include <sys/ioccom.h> + +#include <dev/firewire/firewire.h> +#include <dev/firewire/firewirereg.h> +#include <dev/firewire/fwmem.h> + +#define CDEV_MAJOR 127 +#define FWNODE_INVAL 0xffff + +static d_open_t fw_open; +static d_close_t fw_close; +static d_ioctl_t fw_ioctl; +static d_poll_t fw_poll; +static d_read_t fw_read; /* for Isochronous packet */ +static d_write_t fw_write; +static d_mmap_t fw_mmap; + +struct cdevsw firewire_cdevsw = +{ + fw_open, fw_close, fw_read, fw_write, fw_ioctl, + fw_poll, fw_mmap, nostrategy, "fw", CDEV_MAJOR, nodump, nopsize, D_MEM +}; + +static int +fw_open (dev_t dev, int flags, int fmt, fw_proc *td) +{ + struct firewire_softc *sc; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + + int err = 0; + + if (DEV_FWMEM(dev)) + return fwmem_open(dev, flags, fmt, td); + + sc = devclass_get_softc(firewire_devclass, unit); + if(sc->fc->ir[sub]->flag & FWXFERQ_OPEN){ + err = EBUSY; + return err; + } + if(sc->fc->it[sub]->flag & FWXFERQ_OPEN){ + err = EBUSY; + return err; + } + if(sc->fc->ir[sub]->flag & FWXFERQ_MODEMASK){ + err = EBUSY; + return err; + } +/* Default is per packet mode */ + sc->fc->ir[sub]->flag |= FWXFERQ_OPEN; + sc->fc->it[sub]->flag |= FWXFERQ_OPEN; + sc->fc->ir[sub]->flag |= FWXFERQ_PACKET; + return err; +} + +static int +fw_close (dev_t dev, int flags, int fmt, fw_proc *td) +{ + struct firewire_softc *sc; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + struct fw_xfer *xfer; + struct fw_dvbuf *dvbuf; + struct fw_bind *fwb; + int err = 0; + + if (DEV_FWMEM(dev)) + return fwmem_close(dev, flags, fmt, td); + + sc = devclass_get_softc(firewire_devclass, unit); + if(!(sc->fc->ir[sub]->flag & FWXFERQ_OPEN)){ + err = EINVAL; + return err; + } + sc->fc->ir[sub]->flag &= ~FWXFERQ_OPEN; + if(!(sc->fc->it[sub]->flag & FWXFERQ_OPEN)){ + err = EINVAL; + return err; + } + sc->fc->it[sub]->flag &= ~FWXFERQ_OPEN; + + if(sc->fc->ir[sub]->flag & FWXFERQ_RUNNING){ + sc->fc->irx_disable(sc->fc, sub); + } + if(sc->fc->it[sub]->flag & FWXFERQ_RUNNING){ + sc->fc->it[sub]->flag &= ~FWXFERQ_RUNNING; + sc->fc->itx_disable(sc->fc, sub); + } + if(sc->fc->it[sub]->flag & FWXFERQ_DV){ + if((dvbuf = sc->fc->it[sub]->dvproc) != NULL){ + free(dvbuf->buf, M_DEVBUF); + sc->fc->it[sub]->dvproc = NULL; + } + if((dvbuf = sc->fc->it[sub]->dvdma) != NULL){ + free(dvbuf->buf, M_DEVBUF); + sc->fc->it[sub]->dvdma = NULL; + } + while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvvalid)) != NULL){ + STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvvalid, link); + free(dvbuf->buf, M_DEVBUF); + } + while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvfree)) != NULL){ + STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvfree, link); + free(dvbuf->buf, M_DEVBUF); + } + free(sc->fc->it[sub]->dvbuf, M_DEVBUF); + sc->fc->it[sub]->dvbuf = NULL; + } + if(sc->fc->ir[sub]->flag & FWXFERQ_EXTBUF){ + free(sc->fc->ir[sub]->buf, M_DEVBUF); + sc->fc->ir[sub]->buf = NULL; + free(sc->fc->ir[sub]->bulkxfer, M_DEVBUF); + sc->fc->ir[sub]->bulkxfer = NULL; + sc->fc->ir[sub]->flag &= ~FWXFERQ_EXTBUF; + sc->fc->ir[sub]->psize = FWPMAX_S400; + sc->fc->ir[sub]->maxq = FWMAXQUEUE; + } + if(sc->fc->it[sub]->flag & FWXFERQ_EXTBUF){ + free(sc->fc->it[sub]->buf, M_DEVBUF); + sc->fc->it[sub]->buf = NULL; + free(sc->fc->it[sub]->bulkxfer, M_DEVBUF); + sc->fc->it[sub]->bulkxfer = NULL; + sc->fc->it[sub]->dvbuf = NULL; + sc->fc->it[sub]->flag &= ~FWXFERQ_EXTBUF; + sc->fc->it[sub]->psize = FWPMAX_S400; + sc->fc->it[sub]->maxq = FWMAXQUEUE; + } + for(xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q); + xfer != NULL; xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q)){ + sc->fc->ir[sub]->queued--; + STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->q, link); + + xfer->resp = 0; + switch(xfer->act_type){ + case FWACT_XFER: + fw_xfer_done(xfer); + break; + default: + break; + } + fw_xfer_free(xfer); + } + for(fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds); fwb != NULL; + fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds)){ + STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist); + STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->binds, chlist); + free(fwb, M_DEVBUF); + } + sc->fc->ir[sub]->flag &= ~FWXFERQ_MODEMASK; + sc->fc->it[sub]->flag &= ~FWXFERQ_MODEMASK; + return err; +} + +/* + * read request. + */ +static int +fw_read (dev_t dev, struct uio *uio, int ioflag) +{ + struct firewire_softc *sc; + struct fw_xferq *ir; + struct fw_xfer *xfer; + int err = 0, s, slept = 0; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + struct fw_pkt *fp; + + if (DEV_FWMEM(dev)) + return fwmem_read(dev, uio, ioflag); + + sc = devclass_get_softc(firewire_devclass, unit); + + ir = sc->fc->ir[sub]; + + if(ir->flag & FWXFERQ_PACKET){ + ir->stproc = NULL; + } +readloop: + xfer = STAILQ_FIRST(&ir->q); + if(!(ir->flag & FWXFERQ_PACKET) && ir->stproc == NULL){ + ir->stproc = STAILQ_FIRST(&ir->stvalid); + if(ir->stproc != NULL){ + s = splfw(); + STAILQ_REMOVE_HEAD(&ir->stvalid, link); + splx(s); + ir->queued = 0; + } + } + + if(xfer == NULL && ir->stproc == NULL){ + if(slept == 0){ + slept = 1; + if(!(ir->flag & FWXFERQ_RUNNING) + && (ir->flag & FWXFERQ_PACKET)){ + err = sc->fc->irx_enable(sc->fc, sub); + } + if(err){ + return err; + } + ir->flag |= FWXFERQ_WAKEUP; + err = tsleep((caddr_t)ir, FWPRI, "fw_read", hz); + if(err){ + ir->flag &= ~FWXFERQ_WAKEUP; + return err; + } + goto readloop; + }else{ + err = EIO; + return err; + } + }else if(xfer != NULL){ + s = splfw(); + ir->queued --; + STAILQ_REMOVE_HEAD(&ir->q, link); + splx(s); + fp = (struct fw_pkt *)(xfer->recv.buf + xfer->recv.off); + if(sc->fc->irx_post != NULL) + sc->fc->irx_post(sc->fc, fp->mode.ld); + err = uiomove(xfer->recv.buf + xfer->recv.off, xfer->recv.len, uio); + fw_xfer_free( xfer); + }else if(ir->stproc != NULL){ + fp = (struct fw_pkt *)(ir->stproc->buf + ir->queued * ir->psize); + if(sc->fc->irx_post != NULL) + sc->fc->irx_post(sc->fc, fp->mode.ld); + if(ntohs(fp->mode.stream.len) == 0){ + err = EIO; + return err; + } + err = uiomove((caddr_t)fp, ntohs(fp->mode.stream.len) + sizeof(u_int32_t), uio); + fp->mode.stream.len = 0; + ir->queued ++; + if(ir->queued >= ir->bnpacket){ + s = splfw(); + ir->stproc->flag = 0; + STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link); + splx(s); + ir->stproc = NULL; + } + } +#if 0 + if(STAILQ_FIRST(&ir->q) == NULL && + (ir->flag & FWXFERQ_RUNNING) && (ir->flag & FWXFERQ_PACKET)){ + err = sc->fc->irx_enable(sc->fc, sub); + } +#endif +#if 0 + if(STAILQ_FIRST(&ir->stvalid) == NULL && + (ir->flag & FWXFERQ_RUNNING) && !(ir->flag & FWXFERQ_PACKET)){ + err = sc->fc->irx_enable(sc->fc, sub); + } +#endif + return err; +} + +static int +fw_write (dev_t dev, struct uio *uio, int ioflag) +{ + int err = 0; + struct firewire_softc *sc; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + int s, slept = 0; + struct fw_pkt *fp; + struct fw_xfer *xfer; + struct fw_xferq *xferq; + struct firewire_comm *fc; + struct fw_xferq *it; + + if (DEV_FWMEM(dev)) + return fwmem_write(dev, uio, ioflag); + + sc = devclass_get_softc(firewire_devclass, unit); + fc = sc->fc; + it = sc->fc->it[sub]; + + fp = (struct fw_pkt *)uio->uio_iov->iov_base; + switch(fp->mode.common.tcode){ + case FWTCODE_RREQQ: + case FWTCODE_RREQB: + case FWTCODE_LREQ: + err = EINVAL; + return err; + case FWTCODE_WREQQ: + case FWTCODE_WREQB: + xferq = fc->atq; + break; + case FWTCODE_STREAM: + if(it->flag & FWXFERQ_PACKET){ + xferq = fc->atq; + }else{ + xferq = NULL; + } + break; + case FWTCODE_WRES: + case FWTCODE_RRESQ: + case FWTCODE_RRESB: + case FWTCODE_LRES: + xferq = fc->ats; + break; + default: + err = EINVAL; + return err; + } + /* Discard unsent buffered stream packet, when sending Asyrequrst */ + if(xferq != NULL && it->stproc != NULL){ + s = splfw(); + it->stproc->flag = 0; + STAILQ_INSERT_TAIL(&it->stfree, it->stproc, link); + splx(s); + it->stproc = NULL; + } + if(xferq == NULL && !(it->flag & FWXFERQ_DV)){ +isoloop: + if(it->stproc == NULL){ + it->stproc = STAILQ_FIRST(&it->stfree); + if(it->stproc != NULL){ + s = splfw(); + STAILQ_REMOVE_HEAD(&it->stfree, link); + splx(s); + it->queued = 0; + }else if(slept == 0){ + slept = 1; + err = sc->fc->itx_enable(sc->fc, sub); + if(err){ + return err; + } + err = tsleep((caddr_t)it, FWPRI, "fw_write", hz); + if(err){ + return err; + } + goto isoloop; + }else{ + err = EIO; + return err; + } + } + fp = (struct fw_pkt *)(it->stproc->buf + it->queued * it->psize); + fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t)); + err = uiomove(it->stproc->buf + it->queued * it->psize, + uio->uio_resid, uio); + it->queued ++; + if(it->queued >= it->btpacket){ + s = splfw(); + STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link); + splx(s); + it->stproc = NULL; + fw_tbuf_update(sc->fc, sub, 0); + err = sc->fc->itx_enable(sc->fc, sub); + } + return err; + } if(xferq == NULL && it->flag & FWXFERQ_DV){ +dvloop: + if(it->dvproc == NULL){ + it->dvproc = STAILQ_FIRST(&it->dvfree); + if(it->dvproc != NULL){ + s = splfw(); + STAILQ_REMOVE_HEAD(&it->dvfree, link); + splx(s); + it->dvptr = 0; + }else if(slept == 0){ + slept = 1; + err = sc->fc->itx_enable(sc->fc, sub); + if(err){ + return err; + } + err = tsleep((caddr_t)it, FWPRI, "fw_write", hz); + if(err){ + return err; + } + goto dvloop; + }else{ + err = EIO; + return err; + } + } + fp = (struct fw_pkt *)(it->dvproc->buf + it->queued * it->psize); + fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t)); + err = uiomove(it->dvproc->buf + it->dvptr, + uio->uio_resid, uio); + it->dvptr += it->psize; + if(err){ + return err; + } + if(it->dvptr >= it->psize * it->dvpacket){ + s = splfw(); + STAILQ_INSERT_TAIL(&it->dvvalid, it->dvproc, link); + splx(s); + it->dvproc = NULL; + err = fw_tbuf_update(sc->fc, sub, 0); + if(err){ + return err; + } + err = sc->fc->itx_enable(sc->fc, sub); + } + return err; + } + if(xferq != NULL){ + xfer = fw_xfer_alloc(); + if(xfer == NULL){ + err = ENOMEM; + return err; + } + xfer->send.buf = malloc(uio->uio_resid, M_DEVBUF, M_NOWAIT); + if(xfer->send.buf == NULL){ + fw_xfer_free( xfer); + err = ENOBUFS; + return err; + } + xfer->dst = ntohs(fp->mode.hdr.dst); +#if 0 + switch(fp->mode.common.tcode){ + case FWTCODE_WREQQ: + case FWTCODE_WREQB: + if((tl = fw_get_tlabel(fc, xfer)) == -1 ){ + fw_xfer_free( xfer); + err = EAGAIN; + return err; + } + fp->mode.hdr.tlrt = tl << 2; + default: + break; + } + + xfer->tl = fp->mode.hdr.tlrt >> 2; + xfer->tcode = fp->mode.common.tcode; + xfer->fc = fc; + xfer->q = xferq; + xfer->act_type = FWACT_XFER; + xfer->retry_req = fw_asybusy; +#endif + xfer->send.len = uio->uio_resid; + xfer->send.off = 0; + xfer->spd = 0;/* XXX: how to setup it */ + xfer->act.hand = fw_asy_callback; + + err = uiomove(xfer->send.buf, uio->uio_resid, uio); + if(err){ + fw_xfer_free( xfer); + return err; + } +#if 0 + fw_asystart(xfer); +#else + fw_asyreq(fc, -1, xfer); +#endif + err = tsleep((caddr_t)xfer, FWPRI, "fw_write", hz); + if(xfer->resp == EBUSY) + return EBUSY; + fw_xfer_free( xfer); + return err; + } + return EINVAL; +} + +/* + * ioctl support. + */ +int +fw_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td) +{ + struct firewire_softc *sc; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + int i, len, err = 0; + struct fw_device *fwdev; + struct fw_bind *fwb; + struct fw_xferq *ir, *it; + struct fw_xfer *xfer; + struct fw_pkt *fp; + + struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data; + struct fw_asyreq *asyreq = (struct fw_asyreq *)data; + struct fw_isochreq *ichreq = (struct fw_isochreq *)data; + struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data; + struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data; +#if 0 + struct fw_map_buf *map_buf = (struct fw_map_buf *)data; +#endif + struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data; + + if (DEV_FWMEM(dev)) + return fwmem_ioctl(dev, cmd, data, flag, td); + + sc = devclass_get_softc(firewire_devclass, unit); + if (!data) + return(EINVAL); + + switch (cmd) { + case FW_STSTREAM: + sc->fc->it[sub]->flag &= ~0xff; + sc->fc->it[sub]->flag |= (0x3f & ichreq->ch); + sc->fc->it[sub]->flag |= ((0x3 & ichreq->tag) << 6); + err = 0; + break; + case FW_GTSTREAM: + ichreq->ch = sc->fc->it[sub]->flag & 0x3f; + ichreq->tag =(sc->fc->it[sub]->flag) >> 2 & 0x3; + err = 0; + break; + case FW_SRSTREAM: + sc->fc->ir[sub]->flag &= ~0xff; + sc->fc->ir[sub]->flag |= (0x3f & ichreq->ch); + sc->fc->ir[sub]->flag |= ((0x3 & ichreq->tag) << 6); + err = sc->fc->irx_enable(sc->fc, sub); + break; + case FW_GRSTREAM: + ichreq->ch = sc->fc->ir[sub]->flag & 0x3f; + ichreq->tag =(sc->fc->ir[sub]->flag) >> 2 & 0x3; + err = 0; + break; + case FW_SSTDV: + ibufreq = (struct fw_isobufreq *) + malloc(sizeof(struct fw_isobufreq), M_DEVBUF, M_NOWAIT); + if(ibufreq == NULL){ + err = ENOMEM; + break; + } +#define FWDVPACKET 250 +#define FWDVPMAX 512 + ibufreq->rx.nchunk = 8; + ibufreq->rx.npacket = 50; + ibufreq->rx.psize = FWDVPMAX; + + ibufreq->tx.nchunk = 5; + ibufreq->tx.npacket = 300; + ibufreq->tx.psize = FWDVPMAX; + + err = fw_ioctl(dev, FW_SSTBUF, (caddr_t)ibufreq, flag, td); + sc->fc->it[sub]->dvpacket = FWDVPACKET; + free(ibufreq, M_DEVBUF); +/* reserve a buffer space */ +#define NDVCHUNK 8 + sc->fc->it[sub]->dvproc = NULL; + sc->fc->it[sub]->dvdma = NULL; + sc->fc->it[sub]->flag |= FWXFERQ_DV; + sc->fc->it[sub]->dvbuf + = (struct fw_dvbuf *)malloc(sizeof(struct fw_dvbuf) * NDVCHUNK, M_DEVBUF, M_DONTWAIT); + STAILQ_INIT(&sc->fc->it[sub]->dvvalid); + STAILQ_INIT(&sc->fc->it[sub]->dvfree); + for( i = 0 ; i < NDVCHUNK ; i++){ + sc->fc->it[sub]->dvbuf[i].buf + = malloc(FWDVPMAX * sc->fc->it[sub]->dvpacket, M_DEVBUF, M_DONTWAIT); + STAILQ_INSERT_TAIL(&sc->fc->it[sub]->dvfree, + &sc->fc->it[sub]->dvbuf[i], link); + } + break; + case FW_SSTBUF: + ir = sc->fc->ir[sub]; + it = sc->fc->it[sub]; + + if(ir->flag & FWXFERQ_RUNNING || it->flag & FWXFERQ_RUNNING){ + return(EBUSY); + } + if((ir->flag & FWXFERQ_EXTBUF) || (it->flag & FWXFERQ_EXTBUF)){ + return(EBUSY); + } + if((ibufreq->rx.nchunk * + ibufreq->rx.psize * ibufreq->rx.npacket) + + (ibufreq->tx.nchunk * + ibufreq->tx.psize * ibufreq->tx.npacket) <= 0){ + return(EINVAL); + } + if(ibufreq->rx.nchunk > FWSTMAXCHUNK || + ibufreq->tx.nchunk > FWSTMAXCHUNK){ + return(EINVAL); + } + ir->bulkxfer + = (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->rx.nchunk, M_DEVBUF, M_DONTWAIT); + if(ir->bulkxfer == NULL){ + return(ENOMEM); + } + it->bulkxfer + = (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->tx.nchunk, M_DEVBUF, M_DONTWAIT); + if(it->bulkxfer == NULL){ + return(ENOMEM); + } + ir->buf = malloc( + ibufreq->rx.nchunk * ibufreq->rx.npacket + * ((ibufreq->rx.psize + 3) &~3), + M_DEVBUF, M_DONTWAIT); + if(ir->buf == NULL){ + free(ir->bulkxfer, M_DEVBUF); + free(it->bulkxfer, M_DEVBUF); + ir->bulkxfer = NULL; + it->bulkxfer = NULL; + it->buf = NULL; + return(ENOMEM); + } + it->buf = malloc( + ibufreq->tx.nchunk * ibufreq->tx.npacket + * ((ibufreq->tx.psize + 3) &~3), + M_DEVBUF, M_DONTWAIT); + if(it->buf == NULL){ + free(ir->bulkxfer, M_DEVBUF); + free(it->bulkxfer, M_DEVBUF); + free(ir->buf, M_DEVBUF); + ir->bulkxfer = NULL; + it->bulkxfer = NULL; + it->buf = NULL; + return(ENOMEM); + } + + ir->bnchunk = ibufreq->rx.nchunk; + ir->bnpacket = ibufreq->rx.npacket; + ir->btpacket = ibufreq->rx.npacket; + ir->psize = (ibufreq->rx.psize + 3) & ~3; + ir->queued = 0; + + it->bnchunk = ibufreq->tx.nchunk; + it->bnpacket = ibufreq->tx.npacket; + it->btpacket = ibufreq->tx.npacket; + it->psize = (ibufreq->tx.psize + 3) & ~3; + ir->queued = 0; + it->dvdbc = 0; + it->dvdiff = 0; + it->dvsync = 0; + + STAILQ_INIT(&ir->stvalid); + STAILQ_INIT(&ir->stfree); + ir->stdma = NULL; + ir->stdma2 = NULL; + ir->stproc = NULL; + + STAILQ_INIT(&it->stvalid); + STAILQ_INIT(&it->stfree); + it->stdma = NULL; + it->stdma2 = NULL; + it->stproc = NULL; + + for(i = 0 ; i < sc->fc->ir[sub]->bnchunk; i++){ + ir->bulkxfer[i].buf = + ir->buf + + i * sc->fc->ir[sub]->bnpacket * + sc->fc->ir[sub]->psize; + ir->bulkxfer[i].flag = 0; + STAILQ_INSERT_TAIL(&ir->stfree, + &ir->bulkxfer[i], link); + ir->bulkxfer[i].npacket = ir->bnpacket; + } + for(i = 0 ; i < sc->fc->it[sub]->bnchunk; i++){ + it->bulkxfer[i].buf = + it->buf + + i * sc->fc->it[sub]->bnpacket * + sc->fc->it[sub]->psize; + it->bulkxfer[i].flag = 0; + STAILQ_INSERT_TAIL(&it->stfree, + &it->bulkxfer[i], link); + it->bulkxfer[i].npacket = it->bnpacket; + } + ir->flag &= ~FWXFERQ_MODEMASK; + ir->flag |= FWXFERQ_STREAM; + ir->flag |= FWXFERQ_EXTBUF; + + it->flag &= ~FWXFERQ_MODEMASK; + it->flag |= FWXFERQ_STREAM; + it->flag |= FWXFERQ_EXTBUF; + err = 0; + break; + case FW_GSTBUF: + ibufreq->rx.nchunk = sc->fc->ir[sub]->bnchunk; + ibufreq->rx.npacket = sc->fc->ir[sub]->bnpacket; + ibufreq->rx.psize = sc->fc->ir[sub]->psize; + + ibufreq->tx.nchunk = sc->fc->it[sub]->bnchunk; + ibufreq->tx.npacket = sc->fc->it[sub]->bnpacket; + ibufreq->tx.psize = sc->fc->it[sub]->psize; + break; + case FW_ASYREQ: + xfer = fw_xfer_alloc(); + if(xfer == NULL){ + err = ENOMEM; + return err; + } + fp = &asyreq->pkt; + switch (asyreq->req.type) { + case FWASREQNODE: + xfer->dst = ntohs(fp->mode.hdr.dst); + break; + case FWASREQEUI: + fwdev = fw_noderesolve(sc->fc, asyreq->req.dst.eui); + if (fwdev == NULL) { + printf("%s:cannot found node\n", + device_get_nameunit(sc->fc->dev)); + err = EINVAL; + goto error; + } + xfer->dst = fwdev->dst; + fp->mode.hdr.dst = htons(FWLOCALBUS | xfer->dst); + break; + case FWASRESTL: + /* XXX what's this? */ + break; + case FWASREQSTREAM: + /* nothing to do */ + break; + } + xfer->spd = asyreq->req.sped; + xfer->send.len = asyreq->req.len; + xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT); + if(xfer->send.buf == NULL){ + return ENOMEM; + } + xfer->send.off = 0; + bcopy(fp, xfer->send.buf, xfer->send.len); + xfer->act.hand = fw_asy_callback; + err = fw_asyreq(sc->fc, sub, xfer); + if(err){ + fw_xfer_free( xfer); + return err; + } + err = tsleep((caddr_t)xfer, FWPRI, "asyreq", hz); + if(err == 0){ + if(asyreq->req.len >= xfer->recv.len){ + asyreq->req.len = xfer->recv.len; + }else{ + err = EINVAL; + } + bcopy(xfer->recv.buf + xfer->recv.off, fp, asyreq->req.len); + } +error: + fw_xfer_free( xfer); + break; + case FW_IBUSRST: + sc->fc->ibr(sc->fc); + break; + case FW_CBINDADDR: + fwb = fw_bindlookup(sc->fc, + bindreq->start.hi, bindreq->start.lo); + if(fwb == NULL){ + err = EINVAL; + break; + } + STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist); + STAILQ_REMOVE(&sc->fc->ir[sub]->binds, fwb, fw_bind, chlist); + free(fwb, M_DEVBUF); + break; + case FW_SBINDADDR: + if(bindreq->len <= 0 ){ + err = EINVAL; + break; + } + if(bindreq->start.hi > 0xffff ){ + err = EINVAL; + break; + } + fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT); + if(fwb == NULL){ + err = ENOMEM; + break; + } + fwb->start_hi = bindreq->start.hi; + fwb->start_lo = bindreq->start.lo; + fwb->addrlen = bindreq->len; + + xfer = fw_xfer_alloc(); + if(xfer == NULL){ + err = ENOMEM; + return err; + } + xfer->act_type = FWACT_CH; + xfer->sub = sub; + xfer->fc = sc->fc; + + fwb->xfer = xfer; + err = fw_bindadd(sc->fc, fwb); + break; + case FW_GDEVLST: + i = 0; + for(fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL; + fwdev = TAILQ_NEXT(fwdev, link)){ + if(i < fwdevlst->n){ + fwdevlst->dst[i] = fwdev->dst; + fwdevlst->status[i] = + (fwdev->status == FWDEVATTACHED)?1:0; + fwdevlst->eui[i].hi = fwdev->eui.hi; + fwdevlst->eui[i].lo = fwdev->eui.lo; + } + i++; + } + fwdevlst->n = i; + break; + case FW_GTPMAP: + bcopy(sc->fc->topology_map, data, + (sc->fc->topology_map->crc_len + 1) * 4); + break; + case FW_GSPMAP: + /* speed_map is larger than a page */ + err = copyout(sc->fc->speed_map, *(void **)data, + (sc->fc->speed_map->crc_len + 1) * 4); + break; + case FW_GCROM: + for (fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL; + fwdev = TAILQ_NEXT(fwdev, link)) { + if (fwdev->eui.hi == crom_buf->eui.hi && + fwdev->eui.lo == crom_buf->eui.lo) + break; + } + if (fwdev == NULL) { + err = FWNODE_INVAL; + break; + } +#if 0 + if (fwdev->csrrom[0] >> 24 == 1) + len = 4; + else + len = (1 + ((fwdev->csrrom[0] >> 16) & 0xff)) * 4; +#else + if (fwdev->rommax < CSRROMOFF) + len = 0; + else + len = fwdev->rommax - CSRROMOFF + 4; +#endif + if (crom_buf->len < len) + len = crom_buf->len; + else + crom_buf->len = len; + err = copyout(&fwdev->csrrom[0], crom_buf->ptr, len); + break; + default: + sc->fc->ioctl (dev, cmd, data, flag, td); + break; + } + return err; +} +int +fw_poll(dev_t dev, int events, fw_proc *td) +{ + int revents; + int tmp; + int unit = DEV2UNIT(dev); + int sub = DEV2DMACH(dev); + struct firewire_softc *sc; + + if (DEV_FWMEM(dev)) + return fwmem_poll(dev, events, td); + + sc = devclass_get_softc(firewire_devclass, unit); + revents = 0; + tmp = POLLIN | POLLRDNORM; + if (events & tmp) { + if (STAILQ_FIRST(&sc->fc->ir[sub]->q) != NULL) + revents |= tmp; + else + selrecord(td, &sc->fc->ir[sub]->rsel); + } + tmp = POLLOUT | POLLWRNORM; + if (events & tmp) { + /* XXX should be fixed */ + revents |= tmp; + } + + return revents; +} + +static int +fw_mmap (dev_t dev, vm_offset_t offset, int nproto) +{ + struct firewire_softc *fc; + int unit = DEV2UNIT(dev); + + if (DEV_FWMEM(dev)) + return fwmem_mmap(dev, offset, nproto); + + fc = devclass_get_softc(firewire_devclass, unit); + + return EINVAL; +} diff --git a/sys/modules/firewire/firewire/Makefile b/sys/modules/firewire/firewire/Makefile index 28f074f..aaa7598 100644 --- a/sys/modules/firewire/firewire/Makefile +++ b/sys/modules/firewire/firewire/Makefile @@ -9,15 +9,7 @@ SRCS = bus_if.h device_if.h pci_if.h \ firewire.c firewire.h firewire_phy.h firewirebusreg.h firewirereg.h \ fwohci.c fwohci_pci.c fwohcireg.h fwohcivar.h \ iec13213.h iec68113.h \ - fwmem.c fwmem.h - -#EXPORT_SYMS= fw_asybusy \ -# fw_asyreq \ -# fw_bindadd \ -# fw_bindremove \ -# getcsrdata \ -# fw_xfer_alloc \ -# fw_xfer_free \ + fwmem.c fwmem.h fwdev.c EXPORT_SYMS= YES |