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