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