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