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.
34 * $FreeBSD: src/sys/dev/firewire/fwdev.c,v 1.36 2004/01/22 14:41:17 simokawa Exp $
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>
51 #include <sys/event.h>
54 #include <sys/ctype.h>
56 #include <sys/thread2.h>
60 #include "firewirereg.h"
65 #include <dev/firewire/firewire.h>
66 #include <dev/firewire/firewirereg.h>
67 #include <dev/firewire/fwdma.h>
68 #include <dev/firewire/fwmem.h>
69 #include <dev/firewire/iec68113.h>
72 #define FWNODE_INVAL 0xffff
74 static d_open_t fw_open;
75 static d_close_t fw_close;
76 static d_ioctl_t fw_ioctl;
77 static d_kqfilter_t fw_kqfilter;
78 static d_read_t fw_read; /* for Isochronous packet */
79 static d_write_t fw_write;
80 static d_mmap_t fw_mmap;
81 static d_strategy_t fw_strategy;
83 static void fwfilt_detach(struct knote *);
84 static int fwfilt_read(struct knote *, long);
85 static int fwfilt_write(struct knote *, long);
87 struct dev_ops firewire_ops =
95 .d_kqfilter = fw_kqfilter,
97 .d_strategy = fw_strategy,
103 struct fw_isobufreq bufreq;
107 fwdev_allocbuf(struct firewire_comm *fc, struct fw_xferq *q,
108 struct fw_bufspec *b)
112 if (q->flag & (FWXFERQ_RUNNING | FWXFERQ_EXTBUF))
115 q->bulkxfer = (struct fw_bulkxfer *) kmalloc(
116 sizeof(struct fw_bulkxfer) * b->nchunk,
119 b->psize = roundup2(b->psize, sizeof(u_int32_t));
120 q->buf = fwdma_malloc_multiseg(fc, sizeof(u_int32_t),
121 b->psize, b->nchunk * b->npacket, BUS_DMA_WAITOK);
123 if (q->buf == NULL) {
124 kfree(q->bulkxfer, M_FW);
128 q->bnchunk = b->nchunk;
129 q->bnpacket = b->npacket;
130 q->psize = (b->psize + 3) & ~3;
133 STAILQ_INIT(&q->stvalid);
134 STAILQ_INIT(&q->stfree);
135 STAILQ_INIT(&q->stdma);
138 for(i = 0 ; i < q->bnchunk; i++){
139 q->bulkxfer[i].poffset = i * q->bnpacket;
140 q->bulkxfer[i].mbuf = NULL;
141 STAILQ_INSERT_TAIL(&q->stfree, &q->bulkxfer[i], link);
144 q->flag &= ~FWXFERQ_MODEMASK;
145 q->flag |= FWXFERQ_STREAM;
146 q->flag |= FWXFERQ_EXTBUF;
152 fwdev_freebuf(struct fw_xferq *q)
154 if (q->flag & FWXFERQ_EXTBUF) {
156 fwdma_free_multiseg(q->buf);
158 kfree(q->bulkxfer, M_FW);
160 q->flag &= ~FWXFERQ_EXTBUF;
162 q->maxq = FWMAXQUEUE;
169 fw_open (struct dev_open_args *ap)
171 cdev_t dev = ap->a_head.a_dev;
175 return fwmem_open(ap);
177 if (dev->si_drv1 != NULL)
180 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
181 if ((dev->si_flags & SI_NAMED) == 0) {
182 int unit = DEV2UNIT(dev);
183 int sub = DEV2SUB(dev);
185 make_dev(&firewire_ops, minor(dev),
186 UID_ROOT, GID_OPERATOR, 0660,
187 "fw%d.%d", unit, sub);
191 dev->si_drv1 = kmalloc(sizeof(struct fw_drv1), M_FW, M_WAITOK | M_ZERO);
197 fw_close (struct dev_close_args *ap)
199 cdev_t dev = ap->a_head.a_dev;
200 struct firewire_softc *sc;
201 struct firewire_comm *fc;
203 int unit = DEV2UNIT(dev);
204 struct fw_xfer *xfer;
209 return fwmem_close(ap);
211 sc = devclass_get_softc(firewire_devclass, unit);
213 d = (struct fw_drv1 *)dev->si_drv1;
216 struct fw_xferq *ir = d->ir;
218 if ((ir->flag & FWXFERQ_OPEN) == 0)
220 if (ir->flag & FWXFERQ_RUNNING) {
221 ir->flag &= ~FWXFERQ_RUNNING;
222 fc->irx_disable(fc, ir->dmach);
226 /* drain receiving buffer */
227 for (xfer = STAILQ_FIRST(&ir->q);
228 xfer != NULL; xfer = STAILQ_FIRST(&ir->q)) {
230 STAILQ_REMOVE_HEAD(&ir->q, link);
236 for (fwb = STAILQ_FIRST(&ir->binds); fwb != NULL;
237 fwb = STAILQ_FIRST(&ir->binds)) {
238 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
239 STAILQ_REMOVE_HEAD(&ir->binds, chlist);
242 ir->flag &= ~(FWXFERQ_OPEN |
243 FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
248 struct fw_xferq *it = d->it;
250 if ((it->flag & FWXFERQ_OPEN) == 0)
252 if (it->flag & FWXFERQ_RUNNING) {
253 it->flag &= ~FWXFERQ_RUNNING;
254 fc->itx_disable(fc, it->dmach);
258 it->flag &= ~(FWXFERQ_OPEN |
259 FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
262 kfree(dev->si_drv1, M_FW);
272 fw_read (struct dev_read_args *ap)
274 cdev_t dev = ap->a_head.a_dev;
275 struct uio *uio = ap->a_uio;
276 struct firewire_softc *sc;
278 struct fw_xfer *xfer;
279 int err = 0, slept = 0;
280 int unit = DEV2UNIT(dev);
286 sc = devclass_get_softc(firewire_devclass, unit);
288 ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
289 if (ir == NULL || ir->buf == NULL)
293 xfer = STAILQ_FIRST(&ir->q);
294 if (ir->stproc == NULL) {
296 ir->stproc = STAILQ_FIRST(&ir->stvalid);
297 if (ir->stproc != NULL) {
299 STAILQ_REMOVE_HEAD(&ir->stvalid, link);
304 if (xfer == NULL && ir->stproc == NULL) {
305 /* no data avaliable */
308 ir->flag |= FWXFERQ_WAKEUP;
309 err = tsleep(ir, FWPRI, "fw_read", hz);
310 ir->flag &= ~FWXFERQ_WAKEUP;
313 } else if (slept == 1)
316 } else if(xfer != NULL) {
317 #if 0 /* XXX broken */
318 /* per packet mode or FWACT_CH bind?*/
321 STAILQ_REMOVE_HEAD(&ir->q, link);
323 fp = &xfer->recv.hdr;
324 if (sc->fc->irx_post != NULL)
325 sc->fc->irx_post(sc->fc, fp->mode.ld);
326 err = uiomove((void *)fp, 1 /* XXX header size */, uio);
327 /* XXX copy payload too */
328 /* XXX we should recycle this xfer */
331 } else if(ir->stproc != NULL) {
333 fp = (struct fw_pkt *)fwdma_v_addr(ir->buf,
334 ir->stproc->poffset + ir->queued);
335 if(sc->fc->irx_post != NULL)
336 sc->fc->irx_post(sc->fc, fp->mode.ld);
337 if(fp->mode.stream.len == 0){
341 err = uiomove((caddr_t)fp,
342 fp->mode.stream.len + sizeof(u_int32_t), uio);
344 if(ir->queued >= ir->bnpacket){
346 STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
348 sc->fc->irx_enable(sc->fc, ir->dmach);
351 if (uio->uio_resid >= ir->psize) {
360 fw_write (struct dev_write_args *ap)
362 cdev_t dev = ap->a_head.a_dev;
363 struct uio *uio = ap->a_uio;
365 struct firewire_softc *sc;
366 int unit = DEV2UNIT(dev);
372 return physwrite(ap);
374 sc = devclass_get_softc(firewire_devclass, unit);
375 it = ((struct fw_drv1 *)dev->si_drv1)->it;
376 if (it == NULL || it->buf == NULL)
379 if (it->stproc == NULL) {
380 it->stproc = STAILQ_FIRST(&it->stfree);
381 if (it->stproc != NULL) {
383 STAILQ_REMOVE_HEAD(&it->stfree, link);
386 } else if (slept == 0) {
388 err = sc->fc->itx_enable(sc->fc, it->dmach);
391 err = tsleep(it, FWPRI, "fw_write", hz);
400 fp = (struct fw_pkt *)fwdma_v_addr(it->buf,
401 it->stproc->poffset + it->queued);
402 err = uiomove((caddr_t)fp, sizeof(struct fw_isohdr), uio);
403 err = uiomove((caddr_t)fp->mode.stream.payload,
404 fp->mode.stream.len, uio);
406 if (it->queued >= it->bnpacket) {
408 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
411 err = sc->fc->itx_enable(sc->fc, it->dmach);
413 if (uio->uio_resid >= sizeof(struct fw_isohdr)) {
423 fw_ioctl (struct dev_ioctl_args *ap)
425 cdev_t dev = ap->a_head.a_dev;
426 struct firewire_softc *sc;
427 struct firewire_comm *fc;
429 int unit = DEV2UNIT(dev);
431 struct fw_device *fwdev;
433 struct fw_xferq *ir, *it;
434 struct fw_xfer *xfer;
436 struct fw_devinfo *devinfo;
439 struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)ap->a_data;
440 struct fw_asyreq *asyreq = (struct fw_asyreq *)ap->a_data;
441 struct fw_isochreq *ichreq = (struct fw_isochreq *)ap->a_data;
442 struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)ap->a_data;
443 struct fw_asybindreq *bindreq = (struct fw_asybindreq *)ap->a_data;
444 struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)ap->a_data;
447 return fwmem_ioctl(ap);
452 sc = devclass_get_softc(firewire_devclass, unit);
454 d = (struct fw_drv1 *)dev->si_drv1;
461 for (i = 0; i < fc->nisodma; i ++) {
463 if ((it->flag & FWXFERQ_OPEN) == 0)
466 if (i >= fc->nisodma) {
470 err = fwdev_allocbuf(fc, it, &d->bufreq.tx);
473 it->flag |= FWXFERQ_OPEN;
476 it->flag |= (0x3f & ichreq->ch);
477 it->flag |= ((0x3 & ichreq->tag) << 6);
482 ichreq->ch = it->flag & 0x3f;
483 ichreq->tag = it->flag >> 2 & 0x3;
489 for (i = 0; i < fc->nisodma; i ++) {
491 if ((ir->flag & FWXFERQ_OPEN) == 0)
494 if (i >= fc->nisodma) {
498 err = fwdev_allocbuf(fc, ir, &d->bufreq.rx);
501 ir->flag |= FWXFERQ_OPEN;
504 ir->flag |= (0x3f & ichreq->ch);
505 ir->flag |= ((0x3 & ichreq->tag) << 6);
507 err = fc->irx_enable(fc, ir->dmach);
511 ichreq->ch = ir->flag & 0x3f;
512 ichreq->tag = ir->flag >> 2 & 0x3;
517 bcopy(ibufreq, &d->bufreq, sizeof(d->bufreq));
520 bzero(&ibufreq->rx, sizeof(ibufreq->rx));
522 ibufreq->rx.nchunk = ir->bnchunk;
523 ibufreq->rx.npacket = ir->bnpacket;
524 ibufreq->rx.psize = ir->psize;
526 bzero(&ibufreq->tx, sizeof(ibufreq->tx));
528 ibufreq->tx.nchunk = it->bnchunk;
529 ibufreq->tx.npacket = it->bnpacket;
530 ibufreq->tx.psize = it->psize;
535 struct tcode_info *tinfo;
539 tinfo = &sc->fc->tcode[fp->mode.hdr.tcode];
541 if ((tinfo->flag & FWTI_BLOCK_ASY) != 0)
542 pay_len = MAX(0, asyreq->req.len - tinfo->hdr_len);
544 xfer = fw_xfer_alloc_buf(M_FWXFER, pay_len, PAGE_SIZE/*XXX*/);
548 switch (asyreq->req.type) {
552 fwdev = fw_noderesolve_eui64(sc->fc,
553 &asyreq->req.dst.eui);
555 device_printf(sc->fc->bdev,
556 "cannot find node\n");
560 fp->mode.hdr.dst = FWLOCALBUS | fwdev->dst;
563 /* XXX what's this? */
570 bcopy(fp, (void *)&xfer->send.hdr, tinfo->hdr_len);
572 bcopy((char *)fp + tinfo->hdr_len,
573 (void *)&xfer->send.payload, pay_len);
574 xfer->send.spd = asyreq->req.sped;
575 xfer->act.hand = fw_asy_callback;
577 if ((err = fw_asyreq(sc->fc, -1, xfer)) != 0)
579 if ((err = tsleep(xfer, FWPRI, "asyreq", hz)) != 0)
581 if (xfer->resp != 0) {
585 if ((tinfo->flag & FWTI_TLABEL) == 0)
589 tinfo = &sc->fc->tcode[xfer->recv.hdr.mode.hdr.tcode];
590 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len)
591 asyreq->req.len = xfer->recv.pay_len;
594 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len);
595 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len,
596 MAX(0, asyreq->req.len - tinfo->hdr_len));
598 fw_xfer_free_buf(xfer);
605 fwb = fw_bindlookup(sc->fc,
606 bindreq->start.hi, bindreq->start.lo);
611 STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
612 STAILQ_REMOVE(&ir->binds, fwb, fw_bind, chlist);
616 if(bindreq->len <= 0 ){
620 if(bindreq->start.hi > 0xffff ){
624 fwb = kmalloc(sizeof (struct fw_bind), M_FW, M_WAITOK);
625 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
627 fwb->end = fwb->start + bindreq->len;
629 fwb->sub = ir->dmach;
630 fwb->act_type = FWACT_CH;
633 xfer = fw_xfer_alloc(M_FWXFER);
641 /* XXX broken. need multiple xfer */
642 STAILQ_INIT(&fwb->xferlist);
643 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
645 err = fw_bindadd(sc->fc, fwb);
650 devinfo = &fwdevlst->dev[0];
651 devinfo->dst = sc->fc->nodeid;
652 devinfo->status = 0; /* XXX */
653 devinfo->eui.hi = sc->fc->eui.hi;
654 devinfo->eui.lo = sc->fc->eui.lo;
655 STAILQ_FOREACH(fwdev, &sc->fc->devices, link) {
656 if(len < FW_MAX_DEVLST){
657 devinfo = &fwdevlst->dev[len++];
658 devinfo->dst = fwdev->dst;
660 (fwdev->status == FWDEVINVAL)?0:1;
661 devinfo->eui.hi = fwdev->eui.hi;
662 devinfo->eui.lo = fwdev->eui.lo;
667 fwdevlst->info_len = len;
670 bcopy(sc->fc->topology_map, ap->a_data,
671 (sc->fc->topology_map->crc_len + 1) * 4);
674 STAILQ_FOREACH(fwdev, &sc->fc->devices, link)
675 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
678 if (!FW_EUI64_EQUAL(sc->fc->eui, crom_buf->eui)) {
683 ptr = kmalloc(CROMSIZE, M_FW, M_WAITOK);
685 for (i = 0; i < CROMSIZE/4; i++)
686 ((u_int32_t *)ptr)[i]
687 = ntohl(sc->fc->config_rom[i]);
690 ptr = (void *)&fwdev->csrrom[0];
691 if (fwdev->rommax < CSRROMOFF)
694 len = fwdev->rommax - CSRROMOFF + 4;
696 if (crom_buf->len < len && crom_buf->len >= 0)
700 err = copyout(ptr, crom_buf->ptr, len);
712 static struct filterops fw_read_filterops =
713 { FILTEROP_ISFD, NULL, fwfilt_detach, fwfilt_read };
714 static struct filterops fw_write_filterops =
715 { FILTEROP_ISFD, NULL, fwfilt_detach, fwfilt_write };
718 fw_kqfilter(struct dev_kqfilter_args *ap)
720 cdev_t dev = ap->a_head.a_dev;
722 struct knote *kn = ap->a_kn;
726 * XXX Implement filters for mem?
728 if (DEV_FWMEM(dev)) {
729 ap->a_result = EOPNOTSUPP;
733 ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
737 switch (kn->kn_filter) {
739 kn->kn_fop = &fw_read_filterops;
740 kn->kn_hook = (caddr_t)ir;
743 kn->kn_fop = &fw_write_filterops;
744 kn->kn_hook = (caddr_t)ir;
747 ap->a_result = EOPNOTSUPP;
751 klist = &ir->rkq.ki_note;
752 knote_insert(klist, kn);
758 fwfilt_detach(struct knote *kn)
760 struct fw_xferq *ir = (struct fw_xferq *)kn->kn_hook;
761 struct klist *klist = &ir->rkq.ki_note;
763 knote_remove(klist, kn);
767 fwfilt_read(struct knote *kn, long hint)
769 struct fw_xferq *ir = (struct fw_xferq *)kn->kn_hook;
772 if (STAILQ_FIRST(&ir->q) != NULL)
779 fwfilt_write(struct knote *kn, long hint)
781 /* XXX should be fixed */
786 fw_mmap (struct dev_mmap_args *ap)
788 cdev_t dev = ap->a_head.a_dev;
791 return fwmem_mmap(ap);
797 fw_strategy(struct dev_strategy_args *ap)
799 cdev_t dev = ap->a_head.a_dev;
800 struct bio *bio = ap->a_bio;
801 struct buf *bp = bio->bio_buf;
803 if (DEV_FWMEM(dev)) {
807 bp->b_error = EOPNOTSUPP;
808 bp->b_flags |= B_ERROR;
809 bp->b_resid = bp->b_bcount;
815 fwdev_makedev(struct firewire_softc *sc)
817 /*HELPME dev_ops_add(&firewire_ops, FW_UNITMASK, FW_UNIT(unit));*/
822 fwdev_destroydev(struct firewire_softc *sc)
826 unit = device_get_unit(sc->fc->bdev);
827 dev_ops_remove_minor(&firewire_ops, /*FW_UNITMASK, */FW_UNIT(unit));
831 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
834 fwdev_clone(void *arg, char *name, int namelen, cdev_t *dev)
836 struct firewire_softc *sc;
837 char *devnames[NDEVTYPE] = {"fw", "fwmem"};
839 int devflag[NDEVTYPE] = {0, FWMEM_FLAG};
840 int i, unit = 0, sub = 0;
845 for (i = 0; i < NDEVTYPE; i++)
846 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2)
852 if (subp == NULL || *subp++ != '.')
856 while (isdigit(*subp)) {
858 sub += *subp++ - '0';
863 sc = devclass_get_softc(firewire_devclass, unit);
866 *dev = make_dev(&firewire_ops, MAKEMINOR(devflag[i], unit, sub),
867 UID_ROOT, GID_OPERATOR, 0660,
868 "%s%d.%d", devnames[i], unit, sub);
869 (*dev)->si_flags |= SI_CHEAPCLONE;
870 dev_depends(sc->dev, *dev);