3aace2a60f6d80331a8c955af30908ebfc434ba8
[dragonfly.git] / sys / bus / firewire / fwdev.c
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: src/sys/dev/firewire/fwdev.c,v 1.36 2004/01/22 14:41:17 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/fwdev.c,v 1.20 2008/01/06 16:55:49 swildner Exp $
36  *
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/types.h>
42 #include <sys/mbuf.h>
43 #if defined(__DragonFly__) || __FreeBSD_version < 500000
44 #include <sys/buf.h>
45 #else
46 #include <sys/bio.h>
47 #endif
48
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/conf.h>
52 #include <sys/event.h>
53
54 #include <sys/bus.h>
55 #include <sys/ctype.h>
56
57 #include <sys/thread2.h>
58
59 #ifdef __DragonFly__
60 #include "firewire.h"
61 #include "firewirereg.h"
62 #include "fwdma.h"
63 #include "fwmem.h"
64 #include "iec68113.h"
65 #else
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>
71 #endif
72
73 #define CDEV_MAJOR 127
74 #define FWNODE_INVAL 0xffff
75
76 static  d_open_t        fw_open;
77 static  d_close_t       fw_close;
78 static  d_ioctl_t       fw_ioctl;
79 static  d_kqfilter_t    fw_kqfilter;
80 static  d_read_t        fw_read;        /* for Isochronous packet */
81 static  d_write_t       fw_write;
82 static  d_mmap_t        fw_mmap;
83 static  d_strategy_t    fw_strategy;
84
85 static void fwfilt_detach(struct knote *);
86 static int fwfilt_read(struct knote *, long);
87 static int fwfilt_write(struct knote *, long);
88
89 struct dev_ops firewire_ops = 
90 {
91         { "fw", CDEV_MAJOR, D_MEM | D_KQFILTER },
92         .d_open =       fw_open,
93         .d_close =      fw_close,
94         .d_read =       fw_read,
95         .d_write =      fw_write,
96         .d_ioctl =      fw_ioctl,
97         .d_kqfilter =   fw_kqfilter,
98         .d_mmap =       fw_mmap,
99         .d_strategy =   fw_strategy,
100 };
101
102 struct fw_drv1 {
103         struct fw_xferq *ir;
104         struct fw_xferq *it;
105         struct fw_isobufreq bufreq;
106 };
107
108 static int
109 fwdev_allocbuf(struct firewire_comm *fc, struct fw_xferq *q,
110         struct fw_bufspec *b)
111 {
112         int i;
113
114         if (q->flag & (FWXFERQ_RUNNING | FWXFERQ_EXTBUF))
115                 return(EBUSY);
116
117         q->bulkxfer = (struct fw_bulkxfer *) kmalloc(
118                 sizeof(struct fw_bulkxfer) * b->nchunk,
119                 M_FW, M_WAITOK);
120
121         b->psize = roundup2(b->psize, sizeof(u_int32_t));
122         q->buf = fwdma_malloc_multiseg(fc, sizeof(u_int32_t),
123                         b->psize, b->nchunk * b->npacket, BUS_DMA_WAITOK);
124
125         if (q->buf == NULL) {
126                 kfree(q->bulkxfer, M_FW);
127                 q->bulkxfer = NULL;
128                 return(ENOMEM);
129         }
130         q->bnchunk = b->nchunk;
131         q->bnpacket = b->npacket;
132         q->psize = (b->psize + 3) & ~3;
133         q->queued = 0;
134
135         STAILQ_INIT(&q->stvalid);
136         STAILQ_INIT(&q->stfree);
137         STAILQ_INIT(&q->stdma);
138         q->stproc = NULL;
139
140         for(i = 0 ; i < q->bnchunk; i++){
141                 q->bulkxfer[i].poffset = i * q->bnpacket;
142                 q->bulkxfer[i].mbuf = NULL;
143                 STAILQ_INSERT_TAIL(&q->stfree, &q->bulkxfer[i], link);
144         }
145
146         q->flag &= ~FWXFERQ_MODEMASK;
147         q->flag |= FWXFERQ_STREAM;
148         q->flag |= FWXFERQ_EXTBUF;
149
150         return (0);
151 }
152
153 static int
154 fwdev_freebuf(struct fw_xferq *q)
155 {
156         if (q->flag & FWXFERQ_EXTBUF) {
157                 if (q->buf != NULL)
158                         fwdma_free_multiseg(q->buf);
159                 q->buf = NULL;
160                 kfree(q->bulkxfer, M_FW);
161                 q->bulkxfer = NULL;
162                 q->flag &= ~FWXFERQ_EXTBUF;
163                 q->psize = 0;
164                 q->maxq = FWMAXQUEUE;
165         }
166         return (0);
167 }
168
169
170 static int
171 fw_open (struct dev_open_args *ap)
172 {
173         cdev_t dev = ap->a_head.a_dev;
174         int err = 0;
175
176         if (DEV_FWMEM(dev))
177                 return fwmem_open(ap);
178
179         if (dev->si_drv1 != NULL)
180                 return (EBUSY);
181
182 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
183         if ((dev->si_flags & SI_NAMED) == 0) {
184                 int unit = DEV2UNIT(dev);
185                 int sub = DEV2SUB(dev);
186
187                 make_dev(&firewire_ops, minor(dev),
188                         UID_ROOT, GID_OPERATOR, 0660,
189                         "fw%d.%d", unit, sub);
190         }
191 #endif
192
193         dev->si_drv1 = kmalloc(sizeof(struct fw_drv1), M_FW, M_WAITOK | M_ZERO);
194
195         return err;
196 }
197
198 static int
199 fw_close (struct dev_close_args *ap)
200 {
201         cdev_t dev = ap->a_head.a_dev;
202         struct firewire_softc *sc;
203         struct firewire_comm *fc;
204         struct fw_drv1 *d;
205         int unit = DEV2UNIT(dev);
206         struct fw_xfer *xfer;
207         struct fw_bind *fwb;
208         int err = 0;
209
210         if (DEV_FWMEM(dev))
211                 return fwmem_close(ap);
212
213         sc = devclass_get_softc(firewire_devclass, unit);
214         fc = sc->fc;
215         d = (struct fw_drv1 *)dev->si_drv1;
216
217         if (d->ir != NULL) {
218                 struct fw_xferq *ir = d->ir;
219
220                 if ((ir->flag & FWXFERQ_OPEN) == 0)
221                         return (EINVAL);
222                 if (ir->flag & FWXFERQ_RUNNING) {
223                         ir->flag &= ~FWXFERQ_RUNNING;
224                         fc->irx_disable(fc, ir->dmach);
225                 }
226                 /* free extbuf */
227                 fwdev_freebuf(ir);
228                 /* drain receiving buffer */
229                 for (xfer = STAILQ_FIRST(&ir->q);
230                         xfer != NULL; xfer = STAILQ_FIRST(&ir->q)) {
231                         ir->queued --;
232                         STAILQ_REMOVE_HEAD(&ir->q, link);
233
234                         xfer->resp = 0;
235                         fw_xfer_done(xfer);
236                 }
237                 /* remove binding */
238                 for (fwb = STAILQ_FIRST(&ir->binds); fwb != NULL;
239                                 fwb = STAILQ_FIRST(&ir->binds)) {
240                         STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
241                         STAILQ_REMOVE_HEAD(&ir->binds, chlist);
242                         kfree(fwb, M_FW);
243                 }
244                 ir->flag &= ~(FWXFERQ_OPEN |
245                         FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
246                 d->ir = NULL;
247
248         }
249         if (d->it != NULL) {
250                 struct fw_xferq *it = d->it;
251
252                 if ((it->flag & FWXFERQ_OPEN) == 0)
253                         return (EINVAL);
254                 if (it->flag & FWXFERQ_RUNNING) {
255                         it->flag &= ~FWXFERQ_RUNNING;
256                         fc->itx_disable(fc, it->dmach);
257                 }
258                 /* free extbuf */
259                 fwdev_freebuf(it);
260                 it->flag &= ~(FWXFERQ_OPEN |
261                         FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
262                 d->it = NULL;
263         }
264         kfree(dev->si_drv1, M_FW);
265         dev->si_drv1 = NULL;
266
267         return err;
268 }
269
270 /*
271  * read request.
272  */
273 static int
274 fw_read (struct dev_read_args *ap)
275 {
276         cdev_t dev = ap->a_head.a_dev;
277         struct uio *uio = ap->a_uio;
278         struct firewire_softc *sc;
279         struct fw_xferq *ir;
280         struct fw_xfer *xfer;
281         int err = 0, slept = 0;
282         int unit = DEV2UNIT(dev);
283         struct fw_pkt *fp;
284
285         if (DEV_FWMEM(dev))
286                 return physread(ap);
287
288         sc = devclass_get_softc(firewire_devclass, unit);
289
290         ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
291         if (ir == NULL || ir->buf == NULL)
292                 return (EIO);
293
294 readloop:
295         xfer = STAILQ_FIRST(&ir->q);
296         if (ir->stproc == NULL) {
297                 /* iso bulkxfer */
298                 ir->stproc = STAILQ_FIRST(&ir->stvalid);
299                 if (ir->stproc != NULL) {
300                         crit_enter();
301                         STAILQ_REMOVE_HEAD(&ir->stvalid, link);
302                         crit_exit();
303                         ir->queued = 0;
304                 }
305         }
306         if (xfer == NULL && ir->stproc == NULL) {
307                 /* no data avaliable */
308                 if (slept == 0) {
309                         slept = 1;
310                         ir->flag |= FWXFERQ_WAKEUP;
311                         err = tsleep(ir, FWPRI, "fw_read", hz);
312                         ir->flag &= ~FWXFERQ_WAKEUP;
313                         if (err == 0)
314                                 goto readloop;
315                 } else if (slept == 1)
316                         err = EIO;
317                 return err;
318         } else if(xfer != NULL) {
319 #if 0 /* XXX broken */
320                 /* per packet mode or FWACT_CH bind?*/
321                 crit_enter();
322                 ir->queued --;
323                 STAILQ_REMOVE_HEAD(&ir->q, link);
324                 crit_exit();
325                 fp = &xfer->recv.hdr;
326                 if (sc->fc->irx_post != NULL)
327                         sc->fc->irx_post(sc->fc, fp->mode.ld);
328                 err = uiomove((void *)fp, 1 /* XXX header size */, uio);
329                 /* XXX copy payload too */
330                 /* XXX we should recycle this xfer */
331 #endif
332                 fw_xfer_free( xfer);
333         } else if(ir->stproc != NULL) {
334                 /* iso bulkxfer */
335                 fp = (struct fw_pkt *)fwdma_v_addr(ir->buf, 
336                                 ir->stproc->poffset + ir->queued);
337                 if(sc->fc->irx_post != NULL)
338                         sc->fc->irx_post(sc->fc, fp->mode.ld);
339                 if(fp->mode.stream.len == 0){
340                         err = EIO;
341                         return err;
342                 }
343                 err = uiomove((caddr_t)fp,
344                         fp->mode.stream.len + sizeof(u_int32_t), uio);
345                 ir->queued ++;
346                 if(ir->queued >= ir->bnpacket){
347                         crit_enter();
348                         STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
349                         crit_exit();
350                         sc->fc->irx_enable(sc->fc, ir->dmach);
351                         ir->stproc = NULL;
352                 }
353                 if (uio->uio_resid >= ir->psize) {
354                         slept = -1;
355                         goto readloop;
356                 }
357         }
358         return err;
359 }
360
361 static int
362 fw_write (struct dev_write_args *ap)
363 {
364         cdev_t dev = ap->a_head.a_dev;
365         struct uio *uio = ap->a_uio;
366         int err = 0;
367         struct firewire_softc *sc;
368         int unit = DEV2UNIT(dev);
369         int slept = 0;
370         struct fw_pkt *fp;
371         struct firewire_comm *fc;
372         struct fw_xferq *it;
373
374         if (DEV_FWMEM(dev))
375                 return physwrite(ap);
376
377         sc = devclass_get_softc(firewire_devclass, unit);
378         fc = sc->fc;
379         it = ((struct fw_drv1 *)dev->si_drv1)->it;
380         if (it == NULL || it->buf == NULL)
381                 return (EIO);
382 isoloop:
383         if (it->stproc == NULL) {
384                 it->stproc = STAILQ_FIRST(&it->stfree);
385                 if (it->stproc != NULL) {
386                         crit_enter();
387                         STAILQ_REMOVE_HEAD(&it->stfree, link);
388                         crit_exit();
389                         it->queued = 0;
390                 } else if (slept == 0) {
391                         slept = 1;
392                         err = sc->fc->itx_enable(sc->fc, it->dmach);
393                         if (err)
394                                 return err;
395                         err = tsleep(it, FWPRI, "fw_write", hz);
396                         if (err)
397                                 return err;
398                         goto isoloop;
399                 } else {
400                         err = EIO;
401                         return err;
402                 }
403         }
404         fp = (struct fw_pkt *)fwdma_v_addr(it->buf,
405                         it->stproc->poffset + it->queued);
406         err = uiomove((caddr_t)fp, sizeof(struct fw_isohdr), uio);
407         err = uiomove((caddr_t)fp->mode.stream.payload,
408                                 fp->mode.stream.len, uio);
409         it->queued ++;
410         if (it->queued >= it->bnpacket) {
411                 crit_enter();
412                 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
413                 crit_exit();
414                 it->stproc = NULL;
415                 err = sc->fc->itx_enable(sc->fc, it->dmach);
416         }
417         if (uio->uio_resid >= sizeof(struct fw_isohdr)) {
418                 slept = 0;
419                 goto isoloop;
420         }
421         return err;
422 }
423 /*
424  * ioctl support.
425  */
426 int
427 fw_ioctl (struct dev_ioctl_args *ap)
428 {
429         cdev_t dev = ap->a_head.a_dev;
430         struct firewire_softc *sc;
431         struct firewire_comm *fc;
432         struct fw_drv1 *d;
433         int unit = DEV2UNIT(dev);
434         int i, len, err = 0;
435         struct fw_device *fwdev;
436         struct fw_bind *fwb;
437         struct fw_xferq *ir, *it;
438         struct fw_xfer *xfer;
439         struct fw_pkt *fp;
440         struct fw_devinfo *devinfo;
441         void *ptr;
442
443         struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)ap->a_data;
444         struct fw_asyreq *asyreq = (struct fw_asyreq *)ap->a_data;
445         struct fw_isochreq *ichreq = (struct fw_isochreq *)ap->a_data;
446         struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)ap->a_data;
447         struct fw_asybindreq *bindreq = (struct fw_asybindreq *)ap->a_data;
448         struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)ap->a_data;
449
450         if (DEV_FWMEM(dev))
451                 return fwmem_ioctl(ap);
452
453         if (!ap->a_data)
454                 return(EINVAL);
455
456         sc = devclass_get_softc(firewire_devclass, unit);
457         fc = sc->fc;
458         d = (struct fw_drv1 *)dev->si_drv1;
459         ir = d->ir;
460         it = d->it;
461
462         switch (ap->a_cmd) {
463         case FW_STSTREAM:
464                 if (it == NULL) {
465                         for (i = 0; i < fc->nisodma; i ++) {
466                                 it = fc->it[i];
467                                 if ((it->flag & FWXFERQ_OPEN) == 0)
468                                          break;
469                         }       
470                         if (i >= fc->nisodma) {
471                                 err = EBUSY;
472                                 break;
473                         }
474                         err = fwdev_allocbuf(fc, it, &d->bufreq.tx);
475                         if (err)
476                                 break;
477                         it->flag |=  FWXFERQ_OPEN;
478                 }
479                 it->flag &= ~0xff;
480                 it->flag |= (0x3f & ichreq->ch);
481                 it->flag |= ((0x3 & ichreq->tag) << 6);
482                 d->it = it;
483                 break;
484         case FW_GTSTREAM:
485                 if (it != NULL) {
486                         ichreq->ch = it->flag & 0x3f;
487                         ichreq->tag = it->flag >> 2 & 0x3;
488                 } else
489                         err = EINVAL;
490                 break;
491         case FW_SRSTREAM:
492                 if (ir == NULL) {
493                         for (i = 0; i < fc->nisodma; i ++) {
494                                 ir = fc->ir[i];
495                                 if ((ir->flag & FWXFERQ_OPEN) == 0)
496                                         break;
497                         }       
498                         if (i >= fc->nisodma) {
499                                 err = EBUSY;
500                                 break;
501                         }
502                         err = fwdev_allocbuf(fc, ir, &d->bufreq.rx);
503                         if (err)
504                                 break;
505                         ir->flag |=  FWXFERQ_OPEN;
506                 }
507                 ir->flag &= ~0xff;
508                 ir->flag |= (0x3f & ichreq->ch);
509                 ir->flag |= ((0x3 & ichreq->tag) << 6);
510                 d->ir = ir;
511                 err = fc->irx_enable(fc, ir->dmach);
512                 break;
513         case FW_GRSTREAM:
514                 if (d->ir != NULL) {
515                         ichreq->ch = ir->flag & 0x3f;
516                         ichreq->tag = ir->flag >> 2 & 0x3;
517                 } else
518                         err = EINVAL;
519                 break;
520         case FW_SSTBUF:
521                 bcopy(ibufreq, &d->bufreq, sizeof(d->bufreq));
522                 break;
523         case FW_GSTBUF:
524                 bzero(&ibufreq->rx, sizeof(ibufreq->rx));
525                 if (ir != NULL) {
526                         ibufreq->rx.nchunk = ir->bnchunk;
527                         ibufreq->rx.npacket = ir->bnpacket;
528                         ibufreq->rx.psize = ir->psize;
529                 }
530                 bzero(&ibufreq->tx, sizeof(ibufreq->tx));
531                 if (it != NULL) {
532                         ibufreq->tx.nchunk = it->bnchunk;
533                         ibufreq->tx.npacket = it->bnpacket;
534                         ibufreq->tx.psize = it->psize;
535                 }
536                 break;
537         case FW_ASYREQ:
538         {
539                 struct tcode_info *tinfo;
540                 int pay_len = 0;
541
542                 fp = &asyreq->pkt;
543                 tinfo = &sc->fc->tcode[fp->mode.hdr.tcode];
544
545                 if ((tinfo->flag & FWTI_BLOCK_ASY) != 0)
546                         pay_len = MAX(0, asyreq->req.len - tinfo->hdr_len);
547
548                 xfer = fw_xfer_alloc_buf(M_FWXFER, pay_len, PAGE_SIZE/*XXX*/);
549                 if (xfer == NULL)
550                         return (ENOMEM);
551
552                 switch (asyreq->req.type) {
553                 case FWASREQNODE:
554                         break;
555                 case FWASREQEUI:
556                         fwdev = fw_noderesolve_eui64(sc->fc,
557                                                 &asyreq->req.dst.eui);
558                         if (fwdev == NULL) {
559                                 device_printf(sc->fc->bdev,
560                                         "cannot find node\n");
561                                 err = EINVAL;
562                                 goto out;
563                         }
564                         fp->mode.hdr.dst = FWLOCALBUS | fwdev->dst;
565                         break;
566                 case FWASRESTL:
567                         /* XXX what's this? */
568                         break;
569                 case FWASREQSTREAM:
570                         /* nothing to do */
571                         break;
572                 }
573
574                 bcopy(fp, (void *)&xfer->send.hdr, tinfo->hdr_len);
575                 if (pay_len > 0)
576                         bcopy((char *)fp + tinfo->hdr_len,
577                             (void *)&xfer->send.payload, pay_len);
578                 xfer->send.spd = asyreq->req.sped;
579                 xfer->act.hand = fw_asy_callback;
580
581                 if ((err = fw_asyreq(sc->fc, -1, xfer)) != 0)
582                         goto out;
583                 if ((err = tsleep(xfer, FWPRI, "asyreq", hz)) != 0)
584                         goto out;
585                 if (xfer->resp != 0) {
586                         err = EIO;
587                         goto out;
588                 }
589                 if ((tinfo->flag & FWTI_TLABEL) == 0)
590                         goto out;
591
592                 /* copy response */
593                 tinfo = &sc->fc->tcode[xfer->recv.hdr.mode.hdr.tcode];
594                 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len)
595                         asyreq->req.len = xfer->recv.pay_len;
596                 else
597                         err = EINVAL;
598                 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len);
599                 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len,
600                     MAX(0, asyreq->req.len - tinfo->hdr_len));
601 out:
602                 fw_xfer_free_buf(xfer);
603                 break;
604         }
605         case FW_IBUSRST:
606                 sc->fc->ibr(sc->fc);
607                 break;
608         case FW_CBINDADDR:
609                 fwb = fw_bindlookup(sc->fc,
610                                 bindreq->start.hi, bindreq->start.lo);
611                 if(fwb == NULL){
612                         err = EINVAL;
613                         break;
614                 }
615                 STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
616                 STAILQ_REMOVE(&ir->binds, fwb, fw_bind, chlist);
617                 kfree(fwb, M_FW);
618                 break;
619         case FW_SBINDADDR:
620                 if(bindreq->len <= 0 ){
621                         err = EINVAL;
622                         break;
623                 }
624                 if(bindreq->start.hi > 0xffff ){
625                         err = EINVAL;
626                         break;
627                 }
628                 fwb = kmalloc(sizeof (struct fw_bind), M_FW, M_WAITOK);
629                 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
630                     bindreq->start.lo;
631                 fwb->end = fwb->start +  bindreq->len;
632                 /* XXX */
633                 fwb->sub = ir->dmach;
634                 fwb->act_type = FWACT_CH;
635
636                 /* XXX alloc buf */
637                 xfer = fw_xfer_alloc(M_FWXFER);
638                 if(xfer == NULL){
639                         kfree(fwb, M_FW);
640                         return (ENOMEM);
641                 }
642                 xfer->fc = sc->fc;
643
644                 crit_enter();
645                 /* XXX broken. need multiple xfer */
646                 STAILQ_INIT(&fwb->xferlist);
647                 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
648                 crit_exit();
649                 err = fw_bindadd(sc->fc, fwb);
650                 break;
651         case FW_GDEVLST:
652                 i = len = 1;
653                 /* myself */
654                 devinfo = &fwdevlst->dev[0];
655                 devinfo->dst = sc->fc->nodeid;
656                 devinfo->status = 0;    /* XXX */
657                 devinfo->eui.hi = sc->fc->eui.hi;
658                 devinfo->eui.lo = sc->fc->eui.lo;
659                 STAILQ_FOREACH(fwdev, &sc->fc->devices, link) {
660                         if(len < FW_MAX_DEVLST){
661                                 devinfo = &fwdevlst->dev[len++];
662                                 devinfo->dst = fwdev->dst;
663                                 devinfo->status = 
664                                         (fwdev->status == FWDEVINVAL)?0:1;
665                                 devinfo->eui.hi = fwdev->eui.hi;
666                                 devinfo->eui.lo = fwdev->eui.lo;
667                         }
668                         i++;
669                 }
670                 fwdevlst->n = i;
671                 fwdevlst->info_len = len;
672                 break;
673         case FW_GTPMAP:
674                 bcopy(sc->fc->topology_map, ap->a_data,
675                                 (sc->fc->topology_map->crc_len + 1) * 4);
676                 break;
677         case FW_GCROM:
678                 STAILQ_FOREACH(fwdev, &sc->fc->devices, link)
679                         if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
680                                 break;
681                 if (fwdev == NULL) {
682                         if (!FW_EUI64_EQUAL(sc->fc->eui, crom_buf->eui)) {
683                                 err = FWNODE_INVAL;
684                                 break;
685                         }
686                         /* myself */
687                         ptr = kmalloc(CROMSIZE, M_FW, M_WAITOK);
688                         len = CROMSIZE;
689                         for (i = 0; i < CROMSIZE/4; i++)
690                                 ((u_int32_t *)ptr)[i]
691                                         = ntohl(sc->fc->config_rom[i]);
692                 } else {
693                         /* found */
694                         ptr = (void *)&fwdev->csrrom[0];
695                         if (fwdev->rommax < CSRROMOFF)
696                                 len = 0;
697                         else
698                                 len = fwdev->rommax - CSRROMOFF + 4;
699                 }
700                 if (crom_buf->len < len && crom_buf->len >= 0)
701                         len = crom_buf->len;
702                 else
703                         crom_buf->len = len;
704                 err = copyout(ptr, crom_buf->ptr, len);
705                 if (fwdev == NULL)
706                         /* myself */
707                         kfree(ptr, M_FW);
708                 break;
709         default:
710                 sc->fc->ioctl(ap);
711                 break;
712         }
713         return err;
714 }
715
716 static struct filterops fw_read_filterops =
717         { FILTEROP_ISFD, NULL, fwfilt_detach, fwfilt_read };
718 static struct filterops fw_write_filterops =
719         { FILTEROP_ISFD, NULL, fwfilt_detach, fwfilt_write };
720
721 static int
722 fw_kqfilter(struct dev_kqfilter_args *ap)
723 {
724         cdev_t dev = ap->a_head.a_dev;
725         struct firewire_softc *sc;
726         struct fw_xferq *ir;
727         struct knote *kn = ap->a_kn;
728         int unit = DEV2UNIT(dev);
729         struct klist *klist;
730
731         /*
732          * XXX Implement filters for mem?
733          */
734         if (DEV_FWMEM(dev)) {
735                 ap->a_result = EOPNOTSUPP;
736                 return (0);
737         }
738
739         sc = devclass_get_softc(firewire_devclass, unit);
740         ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
741
742         ap->a_result = 0;
743
744         switch (kn->kn_filter) {
745         case EVFILT_READ:
746                 kn->kn_fop = &fw_read_filterops;
747                 kn->kn_hook = (caddr_t)ir;
748                 break;
749         case EVFILT_WRITE:
750                 kn->kn_fop = &fw_write_filterops;
751                 kn->kn_hook = (caddr_t)ir;
752                 break;
753         default:
754                 ap->a_result = EOPNOTSUPP;
755                 return (0);
756         }
757
758         klist = &ir->rkq.ki_note;
759         knote_insert(klist, kn);
760
761         return (0);
762 }
763
764 static void
765 fwfilt_detach(struct knote *kn)
766 {
767         struct fw_xferq *ir = (struct fw_xferq *)kn->kn_hook;
768         struct klist *klist = &ir->rkq.ki_note;
769
770         knote_remove(klist, kn);
771 }
772
773 static int
774 fwfilt_read(struct knote *kn, long hint)
775 {
776         struct fw_xferq *ir = (struct fw_xferq *)kn->kn_hook;
777         int ready = 0;
778
779         if (STAILQ_FIRST(&ir->q) != NULL)
780                 ready = 1;
781
782         return (ready);
783 }
784
785 static int
786 fwfilt_write(struct knote *kn, long hint)
787 {
788         /* XXX should be fixed */
789         return (1);
790 }
791
792 static int
793 fw_mmap (struct dev_mmap_args *ap)
794 {  
795         cdev_t dev = ap->a_head.a_dev;
796         struct firewire_softc *sc;
797         int unit = DEV2UNIT(dev);
798
799         if (DEV_FWMEM(dev))
800                 return fwmem_mmap(ap);
801         sc = devclass_get_softc(firewire_devclass, unit);
802
803         return EINVAL;
804 }
805
806 static int
807 fw_strategy(struct dev_strategy_args *ap)
808 {
809         cdev_t dev = ap->a_head.a_dev;
810         struct bio *bio = ap->a_bio;
811         struct buf *bp = bio->bio_buf;
812
813         if (DEV_FWMEM(dev)) {
814                 fwmem_strategy(ap);
815                 return(0);
816         }
817         bp->b_error = EOPNOTSUPP;
818         bp->b_flags |= B_ERROR;
819         bp->b_resid = bp->b_bcount;
820         biodone(bio);
821         return(0);
822 }
823
824 int
825 fwdev_makedev(struct firewire_softc *sc)
826 {
827         int unit;
828
829         unit = device_get_unit(sc->fc->bdev);
830         /*HELPME dev_ops_add(&firewire_ops, FW_UNITMASK, FW_UNIT(unit));*/
831         return(0);
832 }
833
834 int
835 fwdev_destroydev(struct firewire_softc *sc)
836 {
837         int unit;
838
839         unit = device_get_unit(sc->fc->bdev);
840         dev_ops_remove_minor(&firewire_ops, /*FW_UNITMASK, */FW_UNIT(unit));
841         return(0);
842 }
843
844 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
845 #define NDEVTYPE 2
846 void
847 fwdev_clone(void *arg, char *name, int namelen, cdev_t *dev)
848 {
849         struct firewire_softc *sc;
850         char *devnames[NDEVTYPE] = {"fw", "fwmem"};
851         char *subp = NULL;
852         int devflag[NDEVTYPE] = {0, FWMEM_FLAG};
853         int i, unit = 0, sub = 0;
854
855         if (*dev != NULL)
856                 return;
857
858         for (i = 0; i < NDEVTYPE; i++)
859                 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2)
860                         goto found;
861         /* not match */
862         return;
863 found:
864
865         if (subp == NULL || *subp++ != '.')
866                 return;
867
868         /* /dev/fwU.S */
869         while (isdigit(*subp)) {
870                 sub *= 10;
871                 sub += *subp++ - '0';
872         }
873         if (*subp != '\0')
874                 return;
875
876         sc = devclass_get_softc(firewire_devclass, unit);
877         if (sc == NULL)
878                 return;
879         *dev = make_dev(&firewire_ops, MAKEMINOR(devflag[i], unit, sub),
880                        UID_ROOT, GID_OPERATOR, 0660,
881                        "%s%d.%d", devnames[i], unit, sub);
882         (*dev)->si_flags |= SI_CHEAPCLONE;
883         dev_depends(sc->dev, *dev);
884         return;
885 }
886 #endif