fwdev.c revision 1.29 1 /* $NetBSD: fwdev.c,v 1.29 2014/07/25 08:02:19 dholland Exp $ */
2 /*-
3 * Copyright (c) 2003 Hidetoshi Shimokawa
4 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the acknowledgement as bellow:
17 *
18 * This product includes software developed by K. Kobayashi and H. Shimokawa
19 *
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * $FreeBSD: src/sys/dev/firewire/fwdev.c,v 1.52 2007/06/06 14:31:36 simokawa Exp $
36 *
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: fwdev.c,v 1.29 2014/07/25 08:02:19 dholland Exp $");
41
42 #include <sys/param.h>
43 #include <sys/device.h>
44 #include <sys/errno.h>
45 #include <sys/buf.h>
46 #include <sys/bus.h>
47 #include <sys/conf.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/poll.h>
52 #include <sys/proc.h>
53 #include <sys/select.h>
54
55 #include <dev/ieee1394/firewire.h>
56 #include <dev/ieee1394/firewirereg.h>
57 #include <dev/ieee1394/fwdma.h>
58 #include <dev/ieee1394/fwmem.h>
59 #include <dev/ieee1394/iec68113.h>
60
61 #include "ioconf.h"
62
63 #define FWNODE_INVAL 0xffff
64
65 dev_type_open(fw_open);
66 dev_type_close(fw_close);
67 dev_type_read(fw_read);
68 dev_type_write(fw_write);
69 dev_type_ioctl(fw_ioctl);
70 dev_type_poll(fw_poll);
71 dev_type_mmap(fw_mmap);
72 dev_type_strategy(fw_strategy);
73
74 const struct bdevsw fw_bdevsw = {
75 .d_open = fw_open,
76 .d_close = fw_close,
77 .d_strategy = fw_strategy,
78 .d_ioctl = fw_ioctl,
79 .d_dump = nodump,
80 .d_psize = nosize,
81 .d_discard = nodiscard,
82 .d_flag = D_OTHER
83 };
84
85 const struct cdevsw fw_cdevsw = {
86 .d_open = fw_open,
87 .d_close = fw_close,
88 .d_read = fw_read,
89 .d_write = fw_write,
90 .d_ioctl = fw_ioctl,
91 .d_stop = nostop,
92 .d_tty = notty,
93 .d_poll = fw_poll,
94 .d_mmap = fw_mmap,
95 .d_kqfilter = nokqfilter,
96 .d_flag = D_OTHER
97 };
98
99 struct fw_drv1 {
100 struct firewire_comm *fc;
101 struct fw_xferq *ir;
102 struct fw_xferq *it;
103 struct fw_isobufreq bufreq;
104 STAILQ_HEAD(, fw_bind) binds;
105 STAILQ_HEAD(, fw_xfer) rq;
106 };
107
108 static int fwdev_allocbuf(struct firewire_comm *, struct fw_xferq *,
109 struct fw_bufspec *);
110 static int fwdev_freebuf(struct fw_xferq *);
111 static int fw_read_async(struct fw_drv1 *, struct uio *, int);
112 static int fw_write_async(struct fw_drv1 *, struct uio *, int);
113 static void fw_hand(struct fw_xfer *);
114
115
116 int
117 fw_open(dev_t dev, int flags, int fmt, struct lwp *td)
118 {
119 struct firewire_softc *sc;
120 struct fw_drv1 *d;
121 int err = 0;
122
123 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
124 if (sc == NULL)
125 return ENXIO;
126
127 if (DEV_FWMEM(dev))
128 return fwmem_open(dev, flags, fmt, td);
129
130 mutex_enter(&sc->fc->fc_mtx);
131 if (sc->si_drv1 != NULL) {
132 mutex_exit(&sc->fc->fc_mtx);
133 return EBUSY;
134 }
135 /* set dummy value for allocation */
136 sc->si_drv1 = (void *)-1;
137 mutex_exit(&sc->fc->fc_mtx);
138
139 sc->si_drv1 = malloc(sizeof(struct fw_drv1), M_FW, M_WAITOK | M_ZERO);
140 if (sc->si_drv1 == NULL)
141 return ENOMEM;
142
143 d = (struct fw_drv1 *)sc->si_drv1;
144 d->fc = sc->fc;
145 STAILQ_INIT(&d->binds);
146 STAILQ_INIT(&d->rq);
147
148 return err;
149 }
150
151 int
152 fw_close(dev_t dev, int flags, int fmt, struct lwp *td)
153 {
154 struct firewire_softc *sc;
155 struct firewire_comm *fc;
156 struct fw_drv1 *d;
157 struct fw_xfer *xfer;
158 struct fw_bind *fwb;
159 int err = 0;
160
161 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
162 if (sc == NULL)
163 return ENXIO;
164
165 if (DEV_FWMEM(dev))
166 return fwmem_close(dev, flags, fmt, td);
167
168 d = (struct fw_drv1 *)sc->si_drv1;
169 fc = d->fc;
170
171 /* remove binding */
172 for (fwb = STAILQ_FIRST(&d->binds); fwb != NULL;
173 fwb = STAILQ_FIRST(&d->binds)) {
174 fw_bindremove(fc, fwb);
175 STAILQ_REMOVE_HEAD(&d->binds, chlist);
176 fw_xferlist_remove(&fwb->xferlist);
177 free(fwb, M_FW);
178 }
179 if (d->ir != NULL) {
180 struct fw_xferq *ir = d->ir;
181
182 if ((ir->flag & FWXFERQ_OPEN) == 0)
183 return EINVAL;
184 if (ir->flag & FWXFERQ_RUNNING) {
185 ir->flag &= ~FWXFERQ_RUNNING;
186 fc->irx_disable(fc, ir->dmach);
187 }
188 /* free extbuf */
189 fwdev_freebuf(ir);
190 /* drain receiving buffer */
191 for (xfer = STAILQ_FIRST(&ir->q); xfer != NULL;
192 xfer = STAILQ_FIRST(&ir->q)) {
193 ir->queued--;
194 STAILQ_REMOVE_HEAD(&ir->q, link);
195
196 xfer->resp = 0;
197 fw_xfer_done(xfer);
198 }
199 ir->flag &=
200 ~(FWXFERQ_OPEN | FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
201 d->ir = NULL;
202
203 }
204 if (d->it != NULL) {
205 struct fw_xferq *it = d->it;
206
207 if ((it->flag & FWXFERQ_OPEN) == 0)
208 return EINVAL;
209 if (it->flag & FWXFERQ_RUNNING) {
210 it->flag &= ~FWXFERQ_RUNNING;
211 fc->itx_disable(fc, it->dmach);
212 }
213 /* free extbuf */
214 fwdev_freebuf(it);
215 it->flag &=
216 ~(FWXFERQ_OPEN | FWXFERQ_MODEMASK | FWXFERQ_CHTAGMASK);
217 d->it = NULL;
218 }
219 free(sc->si_drv1, M_FW);
220 sc->si_drv1 = NULL;
221
222 return err;
223 }
224
225 int
226 fw_read(dev_t dev, struct uio *uio, int ioflag)
227 {
228 struct firewire_softc *sc;
229 struct firewire_comm *fc;
230 struct fw_drv1 *d;
231 struct fw_xferq *ir;
232 struct fw_pkt *fp;
233 int err = 0, slept = 0;
234
235 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
236 if (sc == NULL)
237 return ENXIO;
238
239 if (DEV_FWMEM(dev))
240 return physio(fw_strategy, NULL, dev, ioflag, minphys, uio);
241
242 d = (struct fw_drv1 *)sc->si_drv1;
243 fc = d->fc;
244 ir = d->ir;
245
246 if (ir == NULL)
247 return fw_read_async(d, uio, ioflag);
248
249 if (ir->buf == NULL)
250 return EIO;
251
252 mutex_enter(&fc->fc_mtx);
253 readloop:
254 if (ir->stproc == NULL) {
255 /* iso bulkxfer */
256 ir->stproc = STAILQ_FIRST(&ir->stvalid);
257 if (ir->stproc != NULL) {
258 STAILQ_REMOVE_HEAD(&ir->stvalid, link);
259 ir->queued = 0;
260 }
261 }
262 if (ir->stproc == NULL) {
263 /* no data avaliable */
264 if (slept == 0) {
265 slept = 1;
266 ir->flag |= FWXFERQ_WAKEUP;
267 mutex_exit(&fc->fc_mtx);
268 err = tsleep(ir, FWPRI, "fw_read", hz);
269 mutex_enter(&fc->fc_mtx);
270 ir->flag &= ~FWXFERQ_WAKEUP;
271 if (err == 0)
272 goto readloop;
273 } else if (slept == 1)
274 err = EIO;
275 mutex_exit(&fc->fc_mtx);
276 return err;
277 } else if (ir->stproc != NULL) {
278 /* iso bulkxfer */
279 mutex_exit(&fc->fc_mtx);
280 fp = (struct fw_pkt *)fwdma_v_addr(ir->buf,
281 ir->stproc->poffset + ir->queued);
282 if (fc->irx_post != NULL)
283 fc->irx_post(fc, fp->mode.ld);
284 if (fp->mode.stream.len == 0)
285 return EIO;
286 err = uiomove((void *)fp,
287 fp->mode.stream.len + sizeof(uint32_t), uio);
288 ir->queued++;
289 if (ir->queued >= ir->bnpacket) {
290 STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
291 fc->irx_enable(fc, ir->dmach);
292 ir->stproc = NULL;
293 }
294 if (uio->uio_resid >= ir->psize) {
295 slept = -1;
296 mutex_enter(&fc->fc_mtx);
297 goto readloop;
298 }
299 } else
300 mutex_exit(&fc->fc_mtx);
301 return err;
302 }
303
304 int
305 fw_write(dev_t dev, struct uio *uio, int ioflag)
306 {
307 struct firewire_softc *sc;
308 struct firewire_comm *fc;
309 struct fw_drv1 *d;
310 struct fw_pkt *fp;
311 struct fw_xferq *it;
312 int slept = 0, err = 0;
313
314 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
315 if (sc == NULL)
316 return ENXIO;
317
318 if (DEV_FWMEM(dev))
319 return physio(fw_strategy, NULL, dev, ioflag, minphys, uio);
320
321 d = (struct fw_drv1 *)sc->si_drv1;
322 fc = d->fc;
323 it = d->it;
324
325 if (it == NULL)
326 return fw_write_async(d, uio, ioflag);
327
328 if (it->buf == NULL)
329 return EIO;
330
331 mutex_enter(&fc->fc_mtx);
332 isoloop:
333 if (it->stproc == NULL) {
334 it->stproc = STAILQ_FIRST(&it->stfree);
335 if (it->stproc != NULL) {
336 STAILQ_REMOVE_HEAD(&it->stfree, link);
337 it->queued = 0;
338 } else if (slept == 0) {
339 slept = 1;
340 #if 0 /* XXX to avoid lock recursion */
341 err = fc->itx_enable(fc, it->dmach);
342 if (err)
343 goto out;
344 #endif
345 mutex_exit(&fc->fc_mtx);
346 err = tsleep(it, FWPRI, "fw_write", hz);
347 mutex_enter(&fc->fc_mtx);
348 if (err)
349 goto out;
350 goto isoloop;
351 } else {
352 err = EIO;
353 goto out;
354 }
355 }
356 mutex_exit(&fc->fc_mtx);
357 fp = (struct fw_pkt *)fwdma_v_addr(it->buf,
358 it->stproc->poffset + it->queued);
359 err = uiomove((void *)fp, sizeof(struct fw_isohdr), uio);
360 if (err != 0)
361 return err;
362 err =
363 uiomove((void *)fp->mode.stream.payload, fp->mode.stream.len, uio);
364 it->queued++;
365 if (it->queued >= it->bnpacket) {
366 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
367 it->stproc = NULL;
368 err = fc->itx_enable(fc, it->dmach);
369 }
370 if (uio->uio_resid >= sizeof(struct fw_isohdr)) {
371 slept = 0;
372 mutex_enter(&fc->fc_mtx);
373 goto isoloop;
374 }
375 return err;
376
377 out:
378 mutex_exit(&fc->fc_mtx);
379 return err;
380 }
381
382 int
383 fw_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *td)
384 {
385 struct firewire_softc *sc;
386 struct firewire_comm *fc;
387 struct fw_drv1 *d;
388 struct fw_device *fwdev;
389 struct fw_bind *fwb;
390 struct fw_xferq *ir, *it;
391 struct fw_xfer *xfer;
392 struct fw_pkt *fp;
393 struct fw_devinfo *devinfo;
394 struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data;
395 struct fw_asyreq *asyreq = (struct fw_asyreq *)data;
396 struct fw_isochreq *ichreq = (struct fw_isochreq *)data;
397 struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data;
398 struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data;
399 struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data;
400 int i, len, err = 0;
401 void *ptr;
402
403 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
404 if (sc == NULL)
405 return ENXIO;
406
407 if (DEV_FWMEM(dev))
408 return fwmem_ioctl(dev, cmd, data, flag, td);
409
410 if (!data)
411 return EINVAL;
412
413 d = (struct fw_drv1 *)sc->si_drv1;
414 fc = d->fc;
415 ir = d->ir;
416 it = d->it;
417
418 switch (cmd) {
419 case FW_STSTREAM:
420 if (it == NULL) {
421 i = fw_open_isodma(fc, /* tx */1);
422 if (i < 0) {
423 err = EBUSY;
424 break;
425 }
426 it = fc->it[i];
427 err = fwdev_allocbuf(fc, it, &d->bufreq.tx);
428 if (err) {
429 it->flag &= ~FWXFERQ_OPEN;
430 break;
431 }
432 }
433 it->flag &= ~0xff;
434 it->flag |= (0x3f & ichreq->ch);
435 it->flag |= ((0x3 & ichreq->tag) << 6);
436 d->it = it;
437 break;
438
439 case FW_GTSTREAM:
440 if (it != NULL) {
441 ichreq->ch = it->flag & 0x3f;
442 ichreq->tag = it->flag >> 2 & 0x3;
443 } else
444 err = EINVAL;
445 break;
446
447 case FW_SRSTREAM:
448 if (ir == NULL) {
449 i = fw_open_isodma(fc, /* tx */0);
450 if (i < 0) {
451 err = EBUSY;
452 break;
453 }
454 ir = fc->ir[i];
455 err = fwdev_allocbuf(fc, ir, &d->bufreq.rx);
456 if (err) {
457 ir->flag &= ~FWXFERQ_OPEN;
458 break;
459 }
460 }
461 ir->flag &= ~0xff;
462 ir->flag |= (0x3f & ichreq->ch);
463 ir->flag |= ((0x3 & ichreq->tag) << 6);
464 d->ir = ir;
465 err = fc->irx_enable(fc, ir->dmach);
466 break;
467
468 case FW_GRSTREAM:
469 if (d->ir != NULL) {
470 ichreq->ch = ir->flag & 0x3f;
471 ichreq->tag = ir->flag >> 2 & 0x3;
472 } else
473 err = EINVAL;
474 break;
475
476 case FW_SSTBUF:
477 memcpy(&d->bufreq, ibufreq, sizeof(d->bufreq));
478 break;
479
480 case FW_GSTBUF:
481 memset(&ibufreq->rx, 0, sizeof(ibufreq->rx));
482 if (ir != NULL) {
483 ibufreq->rx.nchunk = ir->bnchunk;
484 ibufreq->rx.npacket = ir->bnpacket;
485 ibufreq->rx.psize = ir->psize;
486 }
487 memset(&ibufreq->tx, 0, sizeof(ibufreq->tx));
488 if (it != NULL) {
489 ibufreq->tx.nchunk = it->bnchunk;
490 ibufreq->tx.npacket = it->bnpacket;
491 ibufreq->tx.psize = it->psize;
492 }
493 break;
494
495 case FW_ASYREQ:
496 {
497 const struct tcode_info *tinfo;
498 int pay_len = 0;
499
500 fp = &asyreq->pkt;
501 tinfo = &fc->tcode[fp->mode.hdr.tcode];
502
503 if ((tinfo->flag & FWTI_BLOCK_ASY) != 0)
504 pay_len = MAX(0, asyreq->req.len - tinfo->hdr_len);
505
506 xfer = fw_xfer_alloc_buf(M_FW, pay_len, PAGE_SIZE/*XXX*/);
507 if (xfer == NULL)
508 return ENOMEM;
509
510 switch (asyreq->req.type) {
511 case FWASREQNODE:
512 break;
513
514 case FWASREQEUI:
515 fwdev = fw_noderesolve_eui64(fc, &asyreq->req.dst.eui);
516 if (fwdev == NULL) {
517 aprint_error_dev(fc->bdev,
518 "cannot find node\n");
519 err = EINVAL;
520 goto out;
521 }
522 fp->mode.hdr.dst = FWLOCALBUS | fwdev->dst;
523 break;
524
525 case FWASRESTL:
526 /* XXX what's this? */
527 break;
528
529 case FWASREQSTREAM:
530 /* nothing to do */
531 break;
532 }
533
534 memcpy(&xfer->send.hdr, fp, tinfo->hdr_len);
535 if (pay_len > 0)
536 memcpy(xfer->send.payload, (char *)fp + tinfo->hdr_len,
537 pay_len);
538 xfer->send.spd = asyreq->req.sped;
539 xfer->hand = fw_xferwake;
540
541 if ((err = fw_asyreq(fc, -1, xfer)) != 0)
542 goto out;
543 if ((err = fw_xferwait(xfer)) != 0)
544 goto out;
545 if (xfer->resp != 0) {
546 err = EIO;
547 goto out;
548 }
549 if ((tinfo->flag & FWTI_TLABEL) == 0)
550 goto out;
551
552 /* copy response */
553 tinfo = &fc->tcode[xfer->recv.hdr.mode.hdr.tcode];
554 if (xfer->recv.hdr.mode.hdr.tcode == FWTCODE_RRESB ||
555 xfer->recv.hdr.mode.hdr.tcode == FWTCODE_LRES) {
556 pay_len = xfer->recv.pay_len;
557 if (asyreq->req.len >=
558 xfer->recv.pay_len + tinfo->hdr_len)
559 asyreq->req.len =
560 xfer->recv.pay_len + tinfo->hdr_len;
561 else {
562 err = EINVAL;
563 pay_len = 0;
564 }
565 } else
566 pay_len = 0;
567 memcpy(fp, &xfer->recv.hdr, tinfo->hdr_len);
568 memcpy((char *)fp + tinfo->hdr_len, xfer->recv.payload,
569 pay_len);
570 out:
571 fw_xfer_free_buf(xfer);
572 break;
573 }
574
575 case FW_IBUSRST:
576 fc->ibr(fc);
577 break;
578
579 case FW_CBINDADDR:
580 fwb = fw_bindlookup(fc, bindreq->start.hi, bindreq->start.lo);
581 if (fwb == NULL) {
582 err = EINVAL;
583 break;
584 }
585 fw_bindremove(fc, fwb);
586 STAILQ_REMOVE(&d->binds, fwb, fw_bind, chlist);
587 fw_xferlist_remove(&fwb->xferlist);
588 free(fwb, M_FW);
589 break;
590
591 case FW_SBINDADDR:
592 if (bindreq->len <= 0 ) {
593 err = EINVAL;
594 break;
595 }
596 if (bindreq->start.hi > 0xffff ) {
597 err = EINVAL;
598 break;
599 }
600 fwb = (struct fw_bind *)malloc(sizeof(struct fw_bind),
601 M_FW, M_WAITOK);
602 if (fwb == NULL) {
603 err = ENOMEM;
604 break;
605 }
606 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
607 bindreq->start.lo;
608 fwb->end = fwb->start + bindreq->len;
609 fwb->sc = (void *)d;
610 STAILQ_INIT(&fwb->xferlist);
611 err = fw_bindadd(fc, fwb);
612 if (err == 0) {
613 fw_xferlist_add(&fwb->xferlist, M_FW,
614 /* XXX */
615 PAGE_SIZE, PAGE_SIZE, 5, fc, (void *)fwb, fw_hand);
616 STAILQ_INSERT_TAIL(&d->binds, fwb, chlist);
617 }
618 break;
619
620 case FW_GDEVLST:
621 i = len = 1;
622 /* myself */
623 devinfo = fwdevlst->dev;
624 devinfo->dst = fc->nodeid;
625 devinfo->status = 0; /* XXX */
626 devinfo->eui.hi = fc->eui.hi;
627 devinfo->eui.lo = fc->eui.lo;
628 STAILQ_FOREACH(fwdev, &fc->devices, link) {
629 if (len < FW_MAX_DEVLST) {
630 devinfo = &fwdevlst->dev[len++];
631 devinfo->dst = fwdev->dst;
632 devinfo->status =
633 (fwdev->status == FWDEVINVAL) ? 0 : 1;
634 devinfo->eui.hi = fwdev->eui.hi;
635 devinfo->eui.lo = fwdev->eui.lo;
636 }
637 i++;
638 }
639 fwdevlst->n = i;
640 fwdevlst->info_len = len;
641 break;
642
643 case FW_GTPMAP:
644 memcpy(data, fc->topology_map,
645 (fc->topology_map->crc_len + 1) * 4);
646 break;
647
648 case FW_GCROM:
649 STAILQ_FOREACH(fwdev, &fc->devices, link)
650 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
651 break;
652 if (fwdev == NULL) {
653 if (!FW_EUI64_EQUAL(fc->eui, crom_buf->eui)) {
654 err = FWNODE_INVAL;
655 break;
656 }
657 /* myself */
658 ptr = malloc(CROMSIZE, M_FW, M_WAITOK);
659 len = CROMSIZE;
660 for (i = 0; i < CROMSIZE/4; i++)
661 ((uint32_t *)ptr)[i] = ntohl(fc->config_rom[i]);
662 } else {
663 /* found */
664 ptr = (void *)fwdev->csrrom;
665 if (fwdev->rommax < CSRROMOFF)
666 len = 0;
667 else
668 len = fwdev->rommax - CSRROMOFF + 4;
669 }
670 if (crom_buf->len < len)
671 len = crom_buf->len;
672 else
673 crom_buf->len = len;
674 err = copyout(ptr, crom_buf->ptr, len);
675 if (fwdev == NULL)
676 /* myself */
677 free(ptr, M_FW);
678 break;
679
680 default:
681 fc->ioctl(dev, cmd, data, flag, td);
682 break;
683 }
684 return err;
685 }
686
687 int
688 fw_poll(dev_t dev, int events, struct lwp *td)
689 {
690 struct firewire_softc *sc;
691 struct fw_xferq *ir;
692 int revents, tmp;
693
694 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
695 if (sc == NULL)
696 return ENXIO;
697
698 ir = ((struct fw_drv1 *)sc->si_drv1)->ir;
699 revents = 0;
700 tmp = POLLIN | POLLRDNORM;
701 if (events & tmp) {
702 if (STAILQ_FIRST(&ir->q) != NULL)
703 revents |= tmp;
704 else
705 selrecord(td, &ir->rsel);
706 }
707 tmp = POLLOUT | POLLWRNORM;
708 if (events & tmp)
709 /* XXX should be fixed */
710 revents |= tmp;
711
712 return revents;
713 }
714
715 paddr_t
716 fw_mmap(dev_t dev, off_t offset, int nproto)
717 {
718 struct firewire_softc *sc;
719
720 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
721 if (sc == NULL)
722 return ENXIO;
723
724 return EINVAL;
725 }
726
727 void
728 fw_strategy(struct bio *bp)
729 {
730 struct firewire_softc *sc;
731 dev_t dev = bp->bio_dev;
732
733 sc = device_lookup_private(&ieee1394if_cd, DEV2UNIT(dev));
734 if (sc == NULL)
735 return;
736
737 if (DEV_FWMEM(dev)) {
738 fwmem_strategy(bp);
739 return;
740 }
741
742 bp->bio_error = EOPNOTSUPP;
743 bp->bio_resid = bp->bio_bcount;
744 biodone(bp);
745 }
746
747
748 static int
749 fwdev_allocbuf(struct firewire_comm *fc, struct fw_xferq *q,
750 struct fw_bufspec *b)
751 {
752 int i;
753
754 if (q->flag & (FWXFERQ_RUNNING | FWXFERQ_EXTBUF))
755 return EBUSY;
756
757 q->bulkxfer =
758 (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * b->nchunk,
759 M_FW, M_WAITOK);
760 if (q->bulkxfer == NULL)
761 return ENOMEM;
762
763 b->psize = roundup2(b->psize, sizeof(uint32_t));
764 q->buf = fwdma_malloc_multiseg(fc, sizeof(uint32_t), b->psize,
765 b->nchunk * b->npacket, BUS_DMA_WAITOK);
766
767 if (q->buf == NULL) {
768 free(q->bulkxfer, M_FW);
769 q->bulkxfer = NULL;
770 return ENOMEM;
771 }
772 q->bnchunk = b->nchunk;
773 q->bnpacket = b->npacket;
774 q->psize = (b->psize + 3) & ~3;
775 q->queued = 0;
776
777 STAILQ_INIT(&q->stvalid);
778 STAILQ_INIT(&q->stfree);
779 STAILQ_INIT(&q->stdma);
780 q->stproc = NULL;
781
782 for (i = 0 ; i < q->bnchunk; i++) {
783 q->bulkxfer[i].poffset = i * q->bnpacket;
784 q->bulkxfer[i].mbuf = NULL;
785 STAILQ_INSERT_TAIL(&q->stfree, &q->bulkxfer[i], link);
786 }
787
788 q->flag &= ~FWXFERQ_MODEMASK;
789 q->flag |= FWXFERQ_STREAM;
790 q->flag |= FWXFERQ_EXTBUF;
791
792 return 0;
793 }
794
795 static int
796 fwdev_freebuf(struct fw_xferq *q)
797 {
798
799 if (q->flag & FWXFERQ_EXTBUF) {
800 if (q->buf != NULL)
801 fwdma_free_multiseg(q->buf);
802 q->buf = NULL;
803 free(q->bulkxfer, M_FW);
804 q->bulkxfer = NULL;
805 q->flag &= ~FWXFERQ_EXTBUF;
806 q->psize = 0;
807 q->maxq = FWMAXQUEUE;
808 }
809 return 0;
810 }
811
812 static int
813 fw_read_async(struct fw_drv1 *d, struct uio *uio, int ioflag)
814 {
815 struct fw_xfer *xfer;
816 struct fw_bind *fwb;
817 struct fw_pkt *fp;
818 const struct tcode_info *tinfo;
819 int err = 0;
820
821 mutex_enter(&d->fc->fc_mtx);
822
823 for (;;) {
824 xfer = STAILQ_FIRST(&d->rq);
825 if (xfer == NULL && err == 0) {
826 mutex_exit(&d->fc->fc_mtx);
827 err = tsleep(&d->rq, FWPRI, "fwra", 0);
828 if (err != 0)
829 return err;
830 mutex_enter(&d->fc->fc_mtx);
831 continue;
832 }
833 break;
834 }
835
836 STAILQ_REMOVE_HEAD(&d->rq, link);
837 mutex_exit(&d->fc->fc_mtx);
838 fp = &xfer->recv.hdr;
839 #if 0 /* for GASP ?? */
840 if (fc->irx_post != NULL)
841 fc->irx_post(fc, fp->mode.ld);
842 #endif
843 tinfo = &xfer->fc->tcode[fp->mode.hdr.tcode];
844 err = uiomove((void *)fp, tinfo->hdr_len, uio);
845 if (err)
846 goto out;
847 err = uiomove((void *)xfer->recv.payload, xfer->recv.pay_len, uio);
848
849 out:
850 /* recycle this xfer */
851 fwb = (struct fw_bind *)xfer->sc;
852 fw_xfer_unload(xfer);
853 xfer->recv.pay_len = PAGE_SIZE;
854 mutex_enter(&d->fc->fc_mtx);
855 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
856 mutex_exit(&d->fc->fc_mtx);
857 return err;
858 }
859
860 static int
861 fw_write_async(struct fw_drv1 *d, struct uio *uio, int ioflag)
862 {
863 struct fw_xfer *xfer;
864 struct fw_pkt pkt;
865 const struct tcode_info *tinfo;
866 int err;
867
868 memset(&pkt, 0, sizeof(struct fw_pkt));
869 if ((err = uiomove((void *)&pkt, sizeof(uint32_t), uio)))
870 return err;
871 tinfo = &d->fc->tcode[pkt.mode.hdr.tcode];
872 if ((err = uiomove((char *)&pkt + sizeof(uint32_t),
873 tinfo->hdr_len - sizeof(uint32_t), uio)))
874 return err;
875
876 if ((xfer = fw_xfer_alloc_buf(M_FW, uio->uio_resid,
877 PAGE_SIZE/*XXX*/)) == NULL)
878 return ENOMEM;
879
880 memcpy(&xfer->send.hdr, &pkt, sizeof(struct fw_pkt));
881 xfer->send.pay_len = uio->uio_resid;
882 if (uio->uio_resid > 0) {
883 if ((err =
884 uiomove((void *)xfer->send.payload, uio->uio_resid, uio)))
885 goto out;
886 }
887
888 xfer->fc = d->fc;
889 xfer->sc = NULL;
890 xfer->hand = fw_xferwake;
891 xfer->send.spd = 2 /* XXX */;
892
893 if ((err = fw_asyreq(xfer->fc, -1, xfer)))
894 goto out;
895
896 if ((err = fw_xferwait(xfer)))
897 goto out;
898
899 if (xfer->resp != 0) {
900 err = xfer->resp;
901 goto out;
902 }
903
904 if (xfer->flag == FWXF_RCVD) {
905 mutex_enter(&xfer->fc->fc_mtx);
906 STAILQ_INSERT_TAIL(&d->rq, xfer, link);
907 mutex_exit(&xfer->fc->fc_mtx);
908 return 0;
909 }
910
911 out:
912 fw_xfer_free(xfer);
913 return err;
914 }
915
916 static void
917 fw_hand(struct fw_xfer *xfer)
918 {
919 struct fw_bind *fwb;
920 struct fw_drv1 *d;
921
922 fwb = (struct fw_bind *)xfer->sc;
923 d = (struct fw_drv1 *)fwb->sc;
924 mutex_enter(&xfer->fc->fc_mtx);
925 STAILQ_INSERT_TAIL(&d->rq, xfer, link);
926 mutex_exit(&xfer->fc->fc_mtx);
927 wakeup(&d->rq);
928 }
929