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 130585 2004-06-16 09:47:26Z phk $
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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (xfer->recv.hdr.mode.hdr.tcode == FWTCODE_RRESB ||
598 xfer->recv.hdr.mode.hdr.tcode == FWTCODE_LRES) {
599 pay_len = xfer->recv.pay_len;
600 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len) {
601 asyreq->req.len = xfer->recv.pay_len +
602 tinfo->hdr_len;
603 } else {
604 err = EINVAL;
605 pay_len = 0;
606 }
607 } else {
608 pay_len = 0;
609 }
610 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len);
611 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len, pay_len);
612out:
613 fw_xfer_free_buf(xfer);
614 break;
615 }
616 case FW_IBUSRST:
617 sc->fc->ibr(sc->fc);
618 break;
619 case FW_CBINDADDR:
620 fwb = fw_bindlookup(sc->fc,
621 bindreq->start.hi, bindreq->start.lo);
622 if(fwb == NULL){
623 err = EINVAL;
624 break;
625 }
626 STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
627 STAILQ_REMOVE(&ir->binds, fwb, fw_bind, chlist);
628 free(fwb, M_FW);
629 break;
630 case FW_SBINDADDR:
631 if(bindreq->len <= 0 ){
632 err = EINVAL;
633 break;
634 }
635 if(bindreq->start.hi > 0xffff ){
636 err = EINVAL;
637 break;
638 }
639 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
640 if(fwb == NULL){
641 err = ENOMEM;
642 break;
643 }
644 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
645 bindreq->start.lo;
646 fwb->end = fwb->start + bindreq->len;
647 /* XXX */
648 fwb->sub = ir->dmach;
649 fwb->act_type = FWACT_CH;
650
651 /* XXX alloc buf */
652 xfer = fw_xfer_alloc(M_FWXFER);
653 if(xfer == NULL){
654 free(fwb, M_FW);
655 return (ENOMEM);
656 }
657 xfer->fc = sc->fc;
658
659 s = splfw();
660 /* XXX broken. need multiple xfer */
661 STAILQ_INIT(&fwb->xferlist);
662 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
663 splx(s);
664 err = fw_bindadd(sc->fc, fwb);
665 break;
666 case FW_GDEVLST:
667 i = len = 1;
668 /* myself */
669 devinfo = &fwdevlst->dev[0];
670 devinfo->dst = sc->fc->nodeid;
671 devinfo->status = 0; /* XXX */
672 devinfo->eui.hi = sc->fc->eui.hi;
673 devinfo->eui.lo = sc->fc->eui.lo;
674 STAILQ_FOREACH(fwdev, &sc->fc->devices, link) {
675 if(len < FW_MAX_DEVLST){
676 devinfo = &fwdevlst->dev[len++];
677 devinfo->dst = fwdev->dst;
678 devinfo->status =
679 (fwdev->status == FWDEVINVAL)?0:1;
680 devinfo->eui.hi = fwdev->eui.hi;
681 devinfo->eui.lo = fwdev->eui.lo;
682 }
683 i++;
684 }
685 fwdevlst->n = i;
686 fwdevlst->info_len = len;
687 break;
688 case FW_GTPMAP:
689 bcopy(sc->fc->topology_map, data,
690 (sc->fc->topology_map->crc_len + 1) * 4);
691 break;
692 case FW_GCROM:
693 STAILQ_FOREACH(fwdev, &sc->fc->devices, link)
694 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
695 break;
696 if (fwdev == NULL) {
697 if (!FW_EUI64_EQUAL(sc->fc->eui, crom_buf->eui)) {
698 err = FWNODE_INVAL;
699 break;
700 }
701 /* myself */
702 ptr = malloc(CROMSIZE, M_FW, M_WAITOK);
703 len = CROMSIZE;
704 for (i = 0; i < CROMSIZE/4; i++)
705 ((uint32_t *)ptr)[i]
706 = ntohl(sc->fc->config_rom[i]);
707 } else {
708 /* found */
709 ptr = (void *)&fwdev->csrrom[0];
710 if (fwdev->rommax < CSRROMOFF)
711 len = 0;
712 else
713 len = fwdev->rommax - CSRROMOFF + 4;
714 }
715 if (crom_buf->len < len)
716 len = crom_buf->len;
717 else
718 crom_buf->len = len;
719 err = copyout(ptr, crom_buf->ptr, len);
720 if (fwdev == NULL)
721 /* myself */
722 free(ptr, M_FW);
723 break;
724 default:
725 sc->fc->ioctl (dev, cmd, data, flag, td);
726 break;
727 }
728 return err;
729}
730int
731fw_poll(struct cdev *dev, int events, fw_proc *td)
732{
733 struct firewire_softc *sc;
734 struct fw_xferq *ir;
735 int revents;
736 int tmp;
737 int unit = DEV2UNIT(dev);
738
739 if (DEV_FWMEM(dev))
740 return fwmem_poll(dev, events, td);
741
742 sc = devclass_get_softc(firewire_devclass, unit);
743 ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
744 revents = 0;
745 tmp = POLLIN | POLLRDNORM;
746 if (events & tmp) {
747 if (STAILQ_FIRST(&ir->q) != NULL)
748 revents |= tmp;
749 else
750 selrecord(td, &ir->rsel);
751 }
752 tmp = POLLOUT | POLLWRNORM;
753 if (events & tmp) {
754 /* XXX should be fixed */
755 revents |= tmp;
756 }
757
758 return revents;
759}
760
761static int
762#if defined(__DragonFly__) || __FreeBSD_version < 500102
763fw_mmap (struct cdev *dev, vm_offset_t offset, int nproto)
764#else
765fw_mmap (struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nproto)
766#endif
767{
768 struct firewire_softc *sc;
769 int unit = DEV2UNIT(dev);
770
771 if (DEV_FWMEM(dev))
772#if defined(__DragonFly__) || __FreeBSD_version < 500102
773 return fwmem_mmap(dev, offset, nproto);
774#else
775 return fwmem_mmap(dev, offset, paddr, nproto);
776#endif
777
778 sc = devclass_get_softc(firewire_devclass, unit);
779
780 return EINVAL;
781}
782
783static void
784fw_strategy(struct bio *bp)
785{
786 struct cdev *dev;
787
788 dev = bp->bio_dev;
789 if (DEV_FWMEM(dev)) {
790 fwmem_strategy(bp);
791 return;
792 }
793
794 bp->bio_error = EOPNOTSUPP;
795 bp->bio_flags |= BIO_ERROR;
796 bp->bio_resid = bp->bio_bcount;
797 biodone(bp);
798}
799
800int
801fwdev_makedev(struct firewire_softc *sc)
802{
803 int err = 0;
804
805#if defined(__DragonFly__) || __FreeBSD_version < 500000
806 cdevsw_add(&firewire_cdevsw);
807#else
808 struct cdev *d;
809 int unit;
810
811 unit = device_get_unit(sc->fc->bdev);
812 sc->dev = make_dev(&firewire_cdevsw, MAKEMINOR(0, unit, 0),
813 UID_ROOT, GID_OPERATOR, 0660,
814 "fw%d.%d", unit, 0);
815 d = make_dev(&firewire_cdevsw,
816 MAKEMINOR(FWMEM_FLAG, unit, 0),
817 UID_ROOT, GID_OPERATOR, 0660,
818 "fwmem%d.%d", unit, 0);
819 dev_depends(sc->dev, d);
820 make_dev_alias(sc->dev, "fw%d", unit);
821 make_dev_alias(d, "fwmem%d", unit);
822#endif
823
824 return (err);
825}
826
827int
828fwdev_destroydev(struct firewire_softc *sc)
829{
830 int err = 0;
831
832#if defined(__DragonFly__) || __FreeBSD_version < 500000
833 cdevsw_remove(&firewire_cdevsw);
834#else
835 destroy_dev(sc->dev);
836#endif
837 return (err);
838}
839
840#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
841#define NDEVTYPE 2
842void
843fwdev_clone(void *arg, char *name, int namelen, struct cdev **dev)
844{
845 struct firewire_softc *sc;
846 char *devnames[NDEVTYPE] = {"fw", "fwmem"};
847 char *subp = NULL;
848 int devflag[NDEVTYPE] = {0, FWMEM_FLAG};
849 int i, unit = 0, sub = 0;
850
851 if (*dev != NODEV)
852 return;
853
854 for (i = 0; i < NDEVTYPE; i++)
855 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2)
856 goto found;
857 /* not match */
858 return;
859found:
860
861 if (subp == NULL || *subp++ != '.')
862 return;
863
864 /* /dev/fwU.S */
865 while (isdigit(*subp)) {
866 sub *= 10;
867 sub += *subp++ - '0';
868 }
869 if (*subp != '\0')
870 return;
871
872 sc = devclass_get_softc(firewire_devclass, unit);
873 if (sc == NULL)
874 return;
875 *dev = make_dev(&firewire_cdevsw, MAKEMINOR(devflag[i], unit, sub),
876 UID_ROOT, GID_OPERATOR, 0660,
877 "%s%d.%d", devnames[i], unit, sub);
878 (*dev)->si_flags |= SI_CHEAPCLONE;
879 dev_depends(sc->dev, *dev);
880 return;
881}
882#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 130585 2004-06-16 09:47:26Z phk $
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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (struct cdev *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 (xfer->recv.hdr.mode.hdr.tcode == FWTCODE_RRESB ||
598 xfer->recv.hdr.mode.hdr.tcode == FWTCODE_LRES) {
599 pay_len = xfer->recv.pay_len;
600 if (asyreq->req.len >= xfer->recv.pay_len + tinfo->hdr_len) {
601 asyreq->req.len = xfer->recv.pay_len +
602 tinfo->hdr_len;
603 } else {
604 err = EINVAL;
605 pay_len = 0;
606 }
607 } else {
608 pay_len = 0;
609 }
610 bcopy(&xfer->recv.hdr, fp, tinfo->hdr_len);
611 bcopy(xfer->recv.payload, (char *)fp + tinfo->hdr_len, pay_len);
612out:
613 fw_xfer_free_buf(xfer);
614 break;
615 }
616 case FW_IBUSRST:
617 sc->fc->ibr(sc->fc);
618 break;
619 case FW_CBINDADDR:
620 fwb = fw_bindlookup(sc->fc,
621 bindreq->start.hi, bindreq->start.lo);
622 if(fwb == NULL){
623 err = EINVAL;
624 break;
625 }
626 STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
627 STAILQ_REMOVE(&ir->binds, fwb, fw_bind, chlist);
628 free(fwb, M_FW);
629 break;
630 case FW_SBINDADDR:
631 if(bindreq->len <= 0 ){
632 err = EINVAL;
633 break;
634 }
635 if(bindreq->start.hi > 0xffff ){
636 err = EINVAL;
637 break;
638 }
639 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
640 if(fwb == NULL){
641 err = ENOMEM;
642 break;
643 }
644 fwb->start = ((u_int64_t)bindreq->start.hi << 32) |
645 bindreq->start.lo;
646 fwb->end = fwb->start + bindreq->len;
647 /* XXX */
648 fwb->sub = ir->dmach;
649 fwb->act_type = FWACT_CH;
650
651 /* XXX alloc buf */
652 xfer = fw_xfer_alloc(M_FWXFER);
653 if(xfer == NULL){
654 free(fwb, M_FW);
655 return (ENOMEM);
656 }
657 xfer->fc = sc->fc;
658
659 s = splfw();
660 /* XXX broken. need multiple xfer */
661 STAILQ_INIT(&fwb->xferlist);
662 STAILQ_INSERT_TAIL(&fwb->xferlist, xfer, link);
663 splx(s);
664 err = fw_bindadd(sc->fc, fwb);
665 break;
666 case FW_GDEVLST:
667 i = len = 1;
668 /* myself */
669 devinfo = &fwdevlst->dev[0];
670 devinfo->dst = sc->fc->nodeid;
671 devinfo->status = 0; /* XXX */
672 devinfo->eui.hi = sc->fc->eui.hi;
673 devinfo->eui.lo = sc->fc->eui.lo;
674 STAILQ_FOREACH(fwdev, &sc->fc->devices, link) {
675 if(len < FW_MAX_DEVLST){
676 devinfo = &fwdevlst->dev[len++];
677 devinfo->dst = fwdev->dst;
678 devinfo->status =
679 (fwdev->status == FWDEVINVAL)?0:1;
680 devinfo->eui.hi = fwdev->eui.hi;
681 devinfo->eui.lo = fwdev->eui.lo;
682 }
683 i++;
684 }
685 fwdevlst->n = i;
686 fwdevlst->info_len = len;
687 break;
688 case FW_GTPMAP:
689 bcopy(sc->fc->topology_map, data,
690 (sc->fc->topology_map->crc_len + 1) * 4);
691 break;
692 case FW_GCROM:
693 STAILQ_FOREACH(fwdev, &sc->fc->devices, link)
694 if (FW_EUI64_EQUAL(fwdev->eui, crom_buf->eui))
695 break;
696 if (fwdev == NULL) {
697 if (!FW_EUI64_EQUAL(sc->fc->eui, crom_buf->eui)) {
698 err = FWNODE_INVAL;
699 break;
700 }
701 /* myself */
702 ptr = malloc(CROMSIZE, M_FW, M_WAITOK);
703 len = CROMSIZE;
704 for (i = 0; i < CROMSIZE/4; i++)
705 ((uint32_t *)ptr)[i]
706 = ntohl(sc->fc->config_rom[i]);
707 } else {
708 /* found */
709 ptr = (void *)&fwdev->csrrom[0];
710 if (fwdev->rommax < CSRROMOFF)
711 len = 0;
712 else
713 len = fwdev->rommax - CSRROMOFF + 4;
714 }
715 if (crom_buf->len < len)
716 len = crom_buf->len;
717 else
718 crom_buf->len = len;
719 err = copyout(ptr, crom_buf->ptr, len);
720 if (fwdev == NULL)
721 /* myself */
722 free(ptr, M_FW);
723 break;
724 default:
725 sc->fc->ioctl (dev, cmd, data, flag, td);
726 break;
727 }
728 return err;
729}
730int
731fw_poll(struct cdev *dev, int events, fw_proc *td)
732{
733 struct firewire_softc *sc;
734 struct fw_xferq *ir;
735 int revents;
736 int tmp;
737 int unit = DEV2UNIT(dev);
738
739 if (DEV_FWMEM(dev))
740 return fwmem_poll(dev, events, td);
741
742 sc = devclass_get_softc(firewire_devclass, unit);
743 ir = ((struct fw_drv1 *)dev->si_drv1)->ir;
744 revents = 0;
745 tmp = POLLIN | POLLRDNORM;
746 if (events & tmp) {
747 if (STAILQ_FIRST(&ir->q) != NULL)
748 revents |= tmp;
749 else
750 selrecord(td, &ir->rsel);
751 }
752 tmp = POLLOUT | POLLWRNORM;
753 if (events & tmp) {
754 /* XXX should be fixed */
755 revents |= tmp;
756 }
757
758 return revents;
759}
760
761static int
762#if defined(__DragonFly__) || __FreeBSD_version < 500102
763fw_mmap (struct cdev *dev, vm_offset_t offset, int nproto)
764#else
765fw_mmap (struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nproto)
766#endif
767{
768 struct firewire_softc *sc;
769 int unit = DEV2UNIT(dev);
770
771 if (DEV_FWMEM(dev))
772#if defined(__DragonFly__) || __FreeBSD_version < 500102
773 return fwmem_mmap(dev, offset, nproto);
774#else
775 return fwmem_mmap(dev, offset, paddr, nproto);
776#endif
777
778 sc = devclass_get_softc(firewire_devclass, unit);
779
780 return EINVAL;
781}
782
783static void
784fw_strategy(struct bio *bp)
785{
786 struct cdev *dev;
787
788 dev = bp->bio_dev;
789 if (DEV_FWMEM(dev)) {
790 fwmem_strategy(bp);
791 return;
792 }
793
794 bp->bio_error = EOPNOTSUPP;
795 bp->bio_flags |= BIO_ERROR;
796 bp->bio_resid = bp->bio_bcount;
797 biodone(bp);
798}
799
800int
801fwdev_makedev(struct firewire_softc *sc)
802{
803 int err = 0;
804
805#if defined(__DragonFly__) || __FreeBSD_version < 500000
806 cdevsw_add(&firewire_cdevsw);
807#else
808 struct cdev *d;
809 int unit;
810
811 unit = device_get_unit(sc->fc->bdev);
812 sc->dev = make_dev(&firewire_cdevsw, MAKEMINOR(0, unit, 0),
813 UID_ROOT, GID_OPERATOR, 0660,
814 "fw%d.%d", unit, 0);
815 d = make_dev(&firewire_cdevsw,
816 MAKEMINOR(FWMEM_FLAG, unit, 0),
817 UID_ROOT, GID_OPERATOR, 0660,
818 "fwmem%d.%d", unit, 0);
819 dev_depends(sc->dev, d);
820 make_dev_alias(sc->dev, "fw%d", unit);
821 make_dev_alias(d, "fwmem%d", unit);
822#endif
823
824 return (err);
825}
826
827int
828fwdev_destroydev(struct firewire_softc *sc)
829{
830 int err = 0;
831
832#if defined(__DragonFly__) || __FreeBSD_version < 500000
833 cdevsw_remove(&firewire_cdevsw);
834#else
835 destroy_dev(sc->dev);
836#endif
837 return (err);
838}
839
840#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
841#define NDEVTYPE 2
842void
843fwdev_clone(void *arg, char *name, int namelen, struct cdev **dev)
844{
845 struct firewire_softc *sc;
846 char *devnames[NDEVTYPE] = {"fw", "fwmem"};
847 char *subp = NULL;
848 int devflag[NDEVTYPE] = {0, FWMEM_FLAG};
849 int i, unit = 0, sub = 0;
850
851 if (*dev != NODEV)
852 return;
853
854 for (i = 0; i < NDEVTYPE; i++)
855 if (dev_stdclone(name, &subp, devnames[i], &unit) == 2)
856 goto found;
857 /* not match */
858 return;
859found:
860
861 if (subp == NULL || *subp++ != '.')
862 return;
863
864 /* /dev/fwU.S */
865 while (isdigit(*subp)) {
866 sub *= 10;
867 sub += *subp++ - '0';
868 }
869 if (*subp != '\0')
870 return;
871
872 sc = devclass_get_softc(firewire_devclass, unit);
873 if (sc == NULL)
874 return;
875 *dev = make_dev(&firewire_cdevsw, MAKEMINOR(devflag[i], unit, sub),
876 UID_ROOT, GID_OPERATOR, 0660,
877 "%s%d.%d", devnames[i], unit, sub);
878 (*dev)->si_flags |= SI_CHEAPCLONE;
879 dev_depends(sc->dev, *dev);
880 return;
881}
882#endif