firewire.c revision 106790
1103285Sikob/*
2103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the acknowledgement as bellow:
15103285Sikob *
16103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
17103285Sikob *
18103285Sikob * 4. The name of the author may not be used to endorse or promote products
19103285Sikob *    derived from this software without specific prior written permission.
20103285Sikob *
21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31103285Sikob * POSSIBILITY OF SUCH DAMAGE.
32103285Sikob *
33103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 106790 2002-11-12 05:52:01Z simokawa $
34103285Sikob *
35103285Sikob */
36103285Sikob
37103285Sikob#include <sys/param.h>
38103285Sikob#include <sys/systm.h>
39103285Sikob#include <sys/types.h>
40103285Sikob#include <sys/mbuf.h>
41103285Sikob#include <sys/socket.h>
42103285Sikob#include <sys/socketvar.h>
43103285Sikob
44103285Sikob#include <sys/kernel.h>
45103285Sikob#include <sys/malloc.h>
46103285Sikob#include <sys/conf.h>
47103285Sikob#include <sys/uio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob#include <sys/poll.h>
50103285Sikob
51103285Sikob#include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
52103285Sikob#include <machine/clock.h>
53103285Sikob#include <pci/pcivar.h>
54103285Sikob#include <pci/pcireg.h>
55103285Sikob
56103285Sikob#include <vm/vm.h>
57103285Sikob#include <vm/pmap.h>            /* for vtophys proto */
58103285Sikob#include <vm/vm_extern.h>
59103285Sikob
60103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
61103285Sikob
62103285Sikob#include <machine/bus.h>	/* used by newbus */
63103285Sikob#include <sys/rman.h>		/* used by newbus */
64103285Sikob#include <machine/resource.h>	/* used by newbus */
65103285Sikob
66103285Sikob#include <sys/signal.h>
67103285Sikob#include <sys/mman.h>
68103285Sikob#include <sys/ioccom.h>
69103285Sikob
70103285Sikob#include <dev/firewire/firewire.h>
71103285Sikob#include <dev/firewire/firewirereg.h>
72103285Sikob#include <dev/firewire/fwmem.h>
73103285Sikob#include <dev/firewire/iec13213.h>
74103285Sikob#include <dev/firewire/iec68113.h>
75103285Sikob
76103285Sikobint firewire_debug=0;
77103285SikobSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "Firewire Subsystem");
78103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
79103285Sikob	"Firewire driver debug flag");
80103285Sikob
81103285Sikob#define CDEV_MAJOR 127
82103285Sikob#define FW_MAXASYRTY 4
83103285Sikob#define FW_MAXDEVRCNT 4
84103285Sikob#define	FWNODE_INVAL 0xffff
85103285Sikob
86103285Sikob#define XFER_TIMEOUT 0
87103285Sikob
88103285Sikobstatic	d_open_t	fw_open;
89103285Sikobstatic	d_close_t	fw_close;
90103285Sikobstatic	d_ioctl_t	fw_ioctl;
91103285Sikobstatic	d_poll_t	fw_poll;
92103285Sikobstatic	d_read_t	fw_read;	/* for Isochronous packet */
93103285Sikobstatic	d_write_t	fw_write;
94103285Sikobstatic	d_mmap_t	fw_mmap;
95103285Sikob
96103285Sikobdevclass_t firewire_devclass;
97103285Sikob
98103285Sikob
99103285Sikobstatic int firewire_match      __P((device_t));
100103285Sikobstatic int firewire_attach      __P((device_t));
101103285Sikobstatic int firewire_detach      __P((device_t));
102103285Sikob#if 0
103103285Sikobstatic int firewire_shutdown    __P((device_t));
104103285Sikob#endif
105103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
106103285Sikobstatic struct fw_bind *fw_bindlookup __P((struct firewire_comm *, u_int32_t, u_int32_t));
107103285Sikobstatic void fw_try_bmr __P((void *));
108103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
109103285Sikobstatic u_int16_t fw_noderesolve __P((struct firewire_comm *, struct fw_eui64));
110103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
111103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
112103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
113103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
114103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
115103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
116106543Ssimokawa#ifdef FW_VMACCESS
117103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
118106543Ssimokawa#endif
119103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
120105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
121103285Sikob
122103285Sikobstatic device_method_t firewire_methods[] = {
123103285Sikob	/* Device interface */
124103285Sikob	DEVMETHOD(device_probe,		firewire_match),
125103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
126103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
127103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
128103285Sikob
129103285Sikob	/* Bus interface */
130103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
131103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
132103285Sikob
133103285Sikob	{ 0, 0 }
134103285Sikob};
135103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
136103285Sikobu_int maxrec[6]={512,1024,2048,4096,8192,0};
137103285Sikob
138103285Sikob#define MAX_GAPHOP  16
139103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
140103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
141103285Sikob/*
142103285Sikob * The probe routine.
143103285Sikob */
144103285Sikobstruct cdevsw firewire_cdevsw =
145103285Sikob{
146103285Sikob	fw_open, fw_close, fw_read, fw_write, fw_ioctl,
147103285Sikob	fw_poll, fw_mmap, nostrategy, "fw", CDEV_MAJOR, nodump, nopsize, D_MEM
148103285Sikob};
149103285Sikobstatic driver_t firewire_driver = {
150103285Sikob	"firewire",
151103285Sikob	firewire_methods,
152103285Sikob	sizeof(struct firewire_softc),
153103285Sikob};
154103285Sikob
155103285Sikobstatic int
156103285Sikobfw_open (dev_t dev, int flags, int fmt, fw_proc *td)
157103285Sikob{
158103285Sikob	struct firewire_softc *sc;
159103285Sikob	int unit = DEV2UNIT(dev);
160103285Sikob	int sub = DEV2DMACH(dev);
161103285Sikob
162103285Sikob	int err = 0;
163103285Sikob
164103285Sikob	if (DEV_FWMEM(dev))
165103285Sikob		return fwmem_open(dev, flags, fmt, td);
166103285Sikob
167103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
168103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_OPEN){
169103285Sikob		err = EBUSY;
170103285Sikob		return err;
171103285Sikob	}
172103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_OPEN){
173103285Sikob		err = EBUSY;
174103285Sikob		return err;
175103285Sikob	}
176103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_MODEMASK){
177103285Sikob		err = EBUSY;
178103285Sikob		return err;
179103285Sikob	}
180103285Sikob/* Default is per packet mode */
181103285Sikob	sc->fc->ir[sub]->flag |= FWXFERQ_OPEN;
182103285Sikob	sc->fc->it[sub]->flag |= FWXFERQ_OPEN;
183103285Sikob	sc->fc->ir[sub]->flag |= FWXFERQ_PACKET;
184103285Sikob	return err;
185103285Sikob}
186103285Sikobstatic int
187103285Sikobfw_close (dev_t dev, int flags, int fmt, fw_proc *td)
188103285Sikob{
189103285Sikob	struct firewire_softc *sc;
190103285Sikob	int unit = DEV2UNIT(dev);
191103285Sikob	int sub = DEV2DMACH(dev);
192103285Sikob	struct fw_xfer *xfer;
193103285Sikob	struct fw_dvbuf *dvbuf;
194103285Sikob	struct fw_bind *fwb;
195103285Sikob	int err = 0;
196103285Sikob
197103285Sikob	if (DEV_FWMEM(dev))
198103285Sikob		return fwmem_close(dev, flags, fmt, td);
199103285Sikob
200103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
201103285Sikob	if(!(sc->fc->ir[sub]->flag & FWXFERQ_OPEN)){
202103285Sikob		err = EINVAL;
203103285Sikob		return err;
204103285Sikob	}
205103285Sikob	sc->fc->ir[sub]->flag &= ~FWXFERQ_OPEN;
206103285Sikob	if(!(sc->fc->it[sub]->flag & FWXFERQ_OPEN)){
207103285Sikob		err = EINVAL;
208103285Sikob		return err;
209103285Sikob	}
210103285Sikob	sc->fc->it[sub]->flag &= ~FWXFERQ_OPEN;
211103285Sikob
212103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_RUNNING){
213103285Sikob		sc->fc->irx_disable(sc->fc, sub);
214103285Sikob	}
215103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_RUNNING){
216103285Sikob		sc->fc->it[sub]->flag &= ~FWXFERQ_RUNNING;
217103285Sikob		sc->fc->itx_disable(sc->fc, sub);
218103285Sikob	}
219103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_DV){
220103285Sikob		if((dvbuf = sc->fc->it[sub]->dvproc) != NULL){
221103285Sikob			free(dvbuf->buf, M_DEVBUF);
222103285Sikob			sc->fc->it[sub]->dvproc = NULL;
223103285Sikob		}
224103285Sikob		if((dvbuf = sc->fc->it[sub]->dvdma) != NULL){
225103285Sikob			free(dvbuf->buf, M_DEVBUF);
226103285Sikob			sc->fc->it[sub]->dvdma = NULL;
227103285Sikob		}
228103285Sikob		while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvvalid)) != NULL){
229103285Sikob			STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvvalid, link);
230103285Sikob			free(dvbuf->buf, M_DEVBUF);
231103285Sikob		}
232103285Sikob		while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvfree)) != NULL){
233103285Sikob			STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvfree, link);
234103285Sikob			free(dvbuf->buf, M_DEVBUF);
235103285Sikob		}
236103285Sikob		free(sc->fc->it[sub]->dvbuf, M_DEVBUF);
237103285Sikob		sc->fc->it[sub]->dvbuf = NULL;
238103285Sikob	}
239103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_EXTBUF){
240103285Sikob		free(sc->fc->ir[sub]->buf, M_DEVBUF);
241103285Sikob		sc->fc->ir[sub]->buf = NULL;
242103285Sikob		free(sc->fc->ir[sub]->bulkxfer, M_DEVBUF);
243103285Sikob		sc->fc->ir[sub]->bulkxfer = NULL;
244103285Sikob		sc->fc->ir[sub]->flag &= ~FWXFERQ_EXTBUF;
245103285Sikob		sc->fc->ir[sub]->psize = FWPMAX_S400;
246103285Sikob		sc->fc->ir[sub]->maxq = FWMAXQUEUE;
247103285Sikob	}
248103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_EXTBUF){
249103285Sikob		free(sc->fc->it[sub]->buf, M_DEVBUF);
250103285Sikob		sc->fc->it[sub]->buf = NULL;
251103285Sikob		free(sc->fc->it[sub]->bulkxfer, M_DEVBUF);
252103285Sikob		sc->fc->it[sub]->bulkxfer = NULL;
253103285Sikob		sc->fc->it[sub]->dvbuf = NULL;
254103285Sikob		sc->fc->it[sub]->flag &= ~FWXFERQ_EXTBUF;
255103285Sikob		sc->fc->it[sub]->psize = FWPMAX_S400;
256103285Sikob		sc->fc->it[sub]->maxq = FWMAXQUEUE;
257103285Sikob	}
258103285Sikob	for(xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q);
259103285Sikob		xfer != NULL; xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q)){
260103285Sikob		sc->fc->ir[sub]->queued--;
261103285Sikob		STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->q, link);
262103285Sikob
263103285Sikob		xfer->resp = 0;
264103285Sikob		switch(xfer->act_type){
265103285Sikob		case FWACT_XFER:
266103285Sikob			fw_xfer_done(xfer);
267103285Sikob			break;
268103285Sikob		default:
269103285Sikob			break;
270103285Sikob		}
271103285Sikob		fw_xfer_free(xfer);
272103285Sikob	}
273103285Sikob	for(fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds); fwb != NULL;
274103285Sikob		fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds)){
275103285Sikob		STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
276103285Sikob		STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->binds, chlist);
277103285Sikob		free(fwb, M_DEVBUF);
278103285Sikob	}
279103285Sikob	sc->fc->ir[sub]->flag &= ~FWXFERQ_MODEMASK;
280103285Sikob	sc->fc->it[sub]->flag &= ~FWXFERQ_MODEMASK;
281103285Sikob	return err;
282103285Sikob}
283103285Sikob/*
284103285Sikob * read request.
285103285Sikob */
286103285Sikobstatic int
287103285Sikobfw_read (dev_t dev, struct uio *uio, int ioflag)
288103285Sikob{
289103285Sikob	struct firewire_softc *sc;
290103285Sikob	struct fw_xferq *ir;
291103285Sikob	struct fw_xfer *xfer;
292103285Sikob	int err = 0, s, slept = 0;
293103285Sikob	int unit = DEV2UNIT(dev);
294103285Sikob	int sub = DEV2DMACH(dev);
295103285Sikob	struct fw_pkt *fp;
296103285Sikob
297103285Sikob	if (DEV_FWMEM(dev))
298103285Sikob		return fwmem_read(dev, uio, ioflag);
299103285Sikob
300103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
301103285Sikob
302103285Sikob	ir = sc->fc->ir[sub];
303103285Sikob
304103285Sikob	if(ir->flag & FWXFERQ_PACKET){
305103285Sikob		ir->stproc = NULL;
306103285Sikob	}
307103285Sikobreadloop:
308103285Sikob	xfer = STAILQ_FIRST(&ir->q);
309103285Sikob	if(!(ir->flag & FWXFERQ_PACKET) && ir->stproc == NULL){
310103285Sikob		ir->stproc = STAILQ_FIRST(&ir->stvalid);
311103285Sikob		if(ir->stproc != NULL){
312103285Sikob			s = splfw();
313103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
314103285Sikob			splx(s);
315103285Sikob			ir->queued = 0;
316103285Sikob		}
317103285Sikob	}
318103285Sikob
319103285Sikob	if(xfer == NULL && ir->stproc == NULL){
320103285Sikob		if(slept == 0){
321103285Sikob			slept = 1;
322103285Sikob			if(!(ir->flag & FWXFERQ_RUNNING)
323103285Sikob				&& (ir->flag & FWXFERQ_PACKET)){
324103285Sikob				err = sc->fc->irx_enable(sc->fc, sub);
325103285Sikob			}
326103285Sikob			if(err){
327103285Sikob				return err;
328103285Sikob			}
329103285Sikob			ir->flag |= FWXFERQ_WAKEUP;
330103285Sikob			err = tsleep((caddr_t)ir, FWPRI, "fw_read", hz);
331103285Sikob			if(err){
332103285Sikob				ir->flag &= ~FWXFERQ_WAKEUP;
333103285Sikob				return err;
334103285Sikob			}
335103285Sikob			goto readloop;
336103285Sikob		}else{
337103285Sikob			err = EIO;
338103285Sikob			return err;
339103285Sikob		}
340103285Sikob	}else if(xfer != NULL){
341103285Sikob		s = splfw();
342103285Sikob		ir->queued --;
343103285Sikob		STAILQ_REMOVE_HEAD(&ir->q, link);
344103285Sikob		splx(s);
345103285Sikob		fp = (struct fw_pkt *)(xfer->recv.buf + xfer->recv.off);
346103285Sikob		if(sc->fc->irx_post != NULL)
347103285Sikob			sc->fc->irx_post(sc->fc, fp->mode.ld);
348103285Sikob		err = uiomove(xfer->recv.buf + xfer->recv.off, xfer->recv.len, uio);
349103285Sikob		fw_xfer_free( xfer);
350103285Sikob	}else if(ir->stproc != NULL){
351103285Sikob		fp = (struct fw_pkt *)(ir->stproc->buf + ir->queued * ir->psize);
352103285Sikob		if(sc->fc->irx_post != NULL)
353103285Sikob			sc->fc->irx_post(sc->fc, fp->mode.ld);
354103285Sikob		if(ntohs(fp->mode.stream.len) == 0){
355103285Sikob			err = EIO;
356103285Sikob			return err;
357103285Sikob		}
358103285Sikob		err = uiomove((caddr_t)fp, ntohs(fp->mode.stream.len) + sizeof(u_int32_t), uio);
359103285Sikob		fp->mode.stream.len = 0;
360103285Sikob		ir->queued ++;
361103285Sikob		if(ir->queued >= ir->bnpacket){
362103285Sikob			s = splfw();
363103285Sikob			ir->stproc->flag = 0;
364103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
365103285Sikob			splx(s);
366103285Sikob			ir->stproc = NULL;
367103285Sikob		}
368103285Sikob	}
369103285Sikob#if 0
370103285Sikob	if(STAILQ_FIRST(&ir->q) == NULL &&
371103285Sikob		(ir->flag & FWXFERQ_RUNNING) && (ir->flag & FWXFERQ_PACKET)){
372103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
373103285Sikob	}
374103285Sikob#endif
375103285Sikob#if 0
376103285Sikob	if(STAILQ_FIRST(&ir->stvalid) == NULL &&
377103285Sikob		(ir->flag & FWXFERQ_RUNNING) && !(ir->flag & FWXFERQ_PACKET)){
378103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
379103285Sikob	}
380103285Sikob#endif
381103285Sikob	return err;
382103285Sikob}
383103285Sikobstatic int
384103285Sikobfw_write (dev_t dev, struct uio *uio, int ioflag)
385103285Sikob{
386103285Sikob	int err = 0;
387103285Sikob	struct firewire_softc *sc;
388103285Sikob	int unit = DEV2UNIT(dev);
389103285Sikob	int sub = DEV2DMACH(dev);
390103285Sikob	int tl, s, slept = 0;
391103285Sikob	struct fw_pkt *fp;
392103285Sikob	struct fw_xfer *xfer;
393103285Sikob	struct fw_xferq *xferq;
394103285Sikob	struct firewire_comm *fc;
395103285Sikob	struct fw_xferq *it;
396103285Sikob
397103285Sikob	if (DEV_FWMEM(dev))
398103285Sikob		return fwmem_write(dev, uio, ioflag);
399103285Sikob
400103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
401103285Sikob	fc = sc->fc;
402103285Sikob	it = sc->fc->it[sub];
403103285Sikob
404103285Sikob	fp = (struct fw_pkt *)uio->uio_iov->iov_base;
405103285Sikob	switch(fp->mode.common.tcode){
406103285Sikob	case FWTCODE_RREQQ:
407103285Sikob	case FWTCODE_RREQB:
408103285Sikob	case FWTCODE_LREQ:
409103285Sikob		err = EINVAL;
410103285Sikob		return err;
411103285Sikob	case FWTCODE_WREQQ:
412103285Sikob	case FWTCODE_WREQB:
413103285Sikob		xferq = fc->atq;
414103285Sikob		break;
415103285Sikob	case FWTCODE_STREAM:
416103285Sikob		if(it->flag & FWXFERQ_PACKET){
417103285Sikob			xferq = fc->atq;
418103285Sikob		}else{
419103285Sikob			xferq = NULL;
420103285Sikob		}
421103285Sikob		break;
422103285Sikob	case FWTCODE_WRES:
423103285Sikob	case FWTCODE_RRESQ:
424103285Sikob	case FWTCODE_RRESB:
425103285Sikob	case FWTCODE_LRES:
426103285Sikob		xferq = fc->ats;
427103285Sikob		break;
428103285Sikob	default:
429103285Sikob		err = EINVAL;
430103285Sikob		return err;
431103285Sikob	}
432103285Sikob	/* Discard unsent buffered stream packet, when sending Asyrequrst */
433103285Sikob	if(xferq != NULL && it->stproc != NULL){
434103285Sikob		s = splfw();
435103285Sikob		it->stproc->flag = 0;
436103285Sikob		STAILQ_INSERT_TAIL(&it->stfree, it->stproc, link);
437103285Sikob		splx(s);
438103285Sikob		it->stproc = NULL;
439103285Sikob	}
440103285Sikob	if(xferq == NULL && !(it->flag & FWXFERQ_DV)){
441103285Sikobisoloop:
442103285Sikob		if(it->stproc == NULL){
443103285Sikob			it->stproc = STAILQ_FIRST(&it->stfree);
444103285Sikob			if(it->stproc != NULL){
445103285Sikob				s = splfw();
446103285Sikob				STAILQ_REMOVE_HEAD(&it->stfree, link);
447103285Sikob				splx(s);
448103285Sikob				it->queued = 0;
449103285Sikob			}else if(slept == 0){
450103285Sikob				slept = 1;
451103285Sikob				err = sc->fc->itx_enable(sc->fc, sub);
452103285Sikob				if(err){
453103285Sikob					return err;
454103285Sikob				}
455103285Sikob				err = tsleep((caddr_t)it, FWPRI, "fw_write", hz);
456103285Sikob				if(err){
457103285Sikob					return err;
458103285Sikob				}
459103285Sikob				goto isoloop;
460103285Sikob			}else{
461103285Sikob				err = EIO;
462103285Sikob				return err;
463103285Sikob			}
464103285Sikob		}
465103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf + it->queued * it->psize);
466103285Sikob		fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t));
467103285Sikob		err = uiomove(it->stproc->buf + it->queued * it->psize,
468103285Sikob							uio->uio_resid, uio);
469103285Sikob		it->queued ++;
470103285Sikob		if(it->queued >= it->btpacket){
471103285Sikob			s = splfw();
472103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
473103285Sikob			splx(s);
474103285Sikob			it->stproc = NULL;
475103285Sikob			fw_tbuf_update(sc->fc, sub, 0);
476103285Sikob			err = sc->fc->itx_enable(sc->fc, sub);
477103285Sikob		}
478103285Sikob		return err;
479103285Sikob	} if(xferq == NULL && it->flag & FWXFERQ_DV){
480103285Sikobdvloop:
481103285Sikob		if(it->dvproc == NULL){
482103285Sikob			it->dvproc = STAILQ_FIRST(&it->dvfree);
483103285Sikob			if(it->dvproc != NULL){
484103285Sikob				s = splfw();
485103285Sikob				STAILQ_REMOVE_HEAD(&it->dvfree, link);
486103285Sikob				splx(s);
487103285Sikob				it->dvptr = 0;
488103285Sikob			}else if(slept == 0){
489103285Sikob				slept = 1;
490103285Sikob				err = sc->fc->itx_enable(sc->fc, sub);
491103285Sikob				if(err){
492103285Sikob					return err;
493103285Sikob				}
494103285Sikob				err = tsleep((caddr_t)it, FWPRI, "fw_write", hz);
495103285Sikob				if(err){
496103285Sikob					return err;
497103285Sikob				}
498103285Sikob				goto dvloop;
499103285Sikob			}else{
500103285Sikob				err = EIO;
501103285Sikob				return err;
502103285Sikob			}
503103285Sikob		}
504103285Sikob		fp = (struct fw_pkt *)(it->dvproc->buf + it->queued * it->psize);
505103285Sikob		fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t));
506103285Sikob		err = uiomove(it->dvproc->buf + it->dvptr,
507103285Sikob							uio->uio_resid, uio);
508103285Sikob		it->dvptr += it->psize;
509103285Sikob		if(err){
510103285Sikob			return err;
511103285Sikob		}
512103285Sikob		if(it->dvptr >= it->psize * it->dvpacket){
513103285Sikob			s = splfw();
514103285Sikob			STAILQ_INSERT_TAIL(&it->dvvalid, it->dvproc, link);
515103285Sikob			splx(s);
516103285Sikob			it->dvproc = NULL;
517103285Sikob			err = fw_tbuf_update(sc->fc, sub, 0);
518103285Sikob			if(err){
519103285Sikob				return err;
520103285Sikob			}
521103285Sikob			err = sc->fc->itx_enable(sc->fc, sub);
522103285Sikob		}
523103285Sikob		return err;
524103285Sikob	}
525103285Sikob	if(xferq != NULL){
526103285Sikob		xfer = fw_xfer_alloc();
527103285Sikob		if(xfer == NULL){
528103285Sikob			err = ENOMEM;
529103285Sikob			return err;
530103285Sikob		}
531103285Sikob		xfer->send.buf = malloc(uio->uio_resid, M_DEVBUF, M_NOWAIT);
532103285Sikob		if(xfer->send.buf == NULL){
533103285Sikob			fw_xfer_free( xfer);
534103285Sikob			err = ENOBUFS;
535103285Sikob			return err;
536103285Sikob		}
537103285Sikob		xfer->dst = ntohs(fp->mode.hdr.dst);
538103285Sikob
539103285Sikob		switch(fp->mode.common.tcode){
540103285Sikob		case FWTCODE_WREQQ:
541103285Sikob		case FWTCODE_WREQB:
542103285Sikob			if((tl = fw_get_tlabel(fc, xfer)) == -1 ){
543103285Sikob				fw_xfer_free( xfer);
544103285Sikob				err = EAGAIN;
545103285Sikob				return err;
546103285Sikob			}
547103285Sikob			fp->mode.hdr.tlrt = tl << 2;
548103285Sikob		default:
549103285Sikob			break;
550103285Sikob		}
551103285Sikob
552103285Sikob		xfer->tl = fp->mode.hdr.tlrt >> 2;
553103285Sikob		xfer->send.len = uio->uio_resid;
554103285Sikob		xfer->send.off = 0;
555103285Sikob		xfer->tcode = fp->mode.common.tcode;
556103285Sikob		xfer->spd = 0;/* XXX: how to setup it */
557103285Sikob		xfer->fc = fc;
558103285Sikob		xfer->q = xferq;
559103285Sikob		xfer->act_type = FWACT_XFER;
560103285Sikob		xfer->act.hand = fw_asy_callback;
561103285Sikob		xfer->retry_req = fw_asybusy;
562103285Sikob
563103285Sikob		err = uiomove(xfer->send.buf, uio->uio_resid, uio);
564103285Sikob		if(err){
565103285Sikob			return err;
566103285Sikob		}
567103285Sikob		fw_asystart(xfer);
568103285Sikob		err = tsleep((caddr_t)xfer, FWPRI, "fw_write", hz);
569103285Sikob		if(xfer->resp == EBUSY)
570103285Sikob			return EBUSY;
571103285Sikob		fw_xfer_free( xfer);
572103285Sikob		return err;
573103285Sikob	}
574103285Sikob	return EINVAL;
575103285Sikob}
576103285Sikob/*
577103285Sikob * transmitter buffer update.
578103285Sikob */
579103285Sikobint
580103285Sikobfw_tbuf_update(struct firewire_comm *fc, int sub, int flag){
581103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
582103285Sikob	struct fw_dvbuf *dvbuf = NULL;
583103285Sikob	struct fw_xferq *it;
584103285Sikob	int s, err = 0, i, j, chtag;
585103285Sikob	struct fw_pkt *fp;
586103285Sikob	u_int64_t tmpsync, dvsync;
587103285Sikob
588103285Sikob	it = fc->it[sub];
589103285Sikob
590103285Sikob	s = splfw();
591103285Sikob	if(it->stdma == NULL){
592103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
593103285Sikob	}else if(flag != 0){
594103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
595103285Sikob		if(bulkxfer == it->stdma){
596103285Sikob			STAILQ_REMOVE_HEAD(&it->stvalid, link);
597103285Sikob			it->stdma->flag = 0;
598103285Sikob			STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link);
599103285Sikob			if(!(it->flag & FWXFERQ_DV))
600103285Sikob				wakeup(it);
601103285Sikob		}
602103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
603103285Sikob	}else{
604103285Sikob		bulkxfer = it->stdma;
605103285Sikob	}
606103285Sikob	splx(s);
607103285Sikob	if(bulkxfer != NULL){
608103285Sikob		s = splfw();
609103285Sikob		bulkxfer2 = STAILQ_NEXT(bulkxfer, link);
610103285Sikob#if 0
611103285Sikob		if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){
612103285Sikob			bulkxfer2 = STAILQ_FIRST(&it->stfree);
613103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
614103285Sikob			splx(s);
615103285Sikob			bcopy(bulkxfer->buf, bulkxfer2->buf,
616103285Sikob					it->psize * it->btpacket);
617103285Sikob			s = splfw();
618103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link);
619103285Sikob		}
620103285Sikob#endif
621103285Sikob		splx(s);
622103285Sikob	}
623103285Sikob	it->stdma = bulkxfer;
624103285Sikob	it->stdma2 = bulkxfer2;
625103285Sikob
626103285Sikob	if(it->flag & FWXFERQ_DV){
627103285Sikob		chtag = it->flag & 0xff;
628103285Sikobdvloop:
629103285Sikob		if(it->dvdma == NULL){
630103285Sikob			dvbuf = STAILQ_FIRST(&it->dvvalid);
631103285Sikob			if(dvbuf != NULL){
632103285Sikob				s = splfw();
633103285Sikob				STAILQ_REMOVE_HEAD(&it->dvvalid, link);
634103285Sikob				it->dvdma = dvbuf;
635103285Sikob				splx(s);
636103285Sikob				it->queued = 0;
637103285Sikob			}
638103285Sikob		}
639103285Sikob		if(it->dvdma == NULL)
640103285Sikob			return err;
641103285Sikob
642103285Sikob		it->stproc = STAILQ_FIRST(&it->stfree);
643103285Sikob		if(it->stproc != NULL){
644103285Sikob			s = splfw();
645103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
646103285Sikob			splx(s);
647103285Sikob		}else{
648103285Sikob			return err;
649103285Sikob		}
650103285Sikob/*
651103285Sikob * Insert least significant 12 bits timestamp value by computation.
652103285Sikob * Highest significant 4 bits is insert at just before packet sending.
653103285Sikob */
654103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf);
655103285Sikob/* XXX: Parameter relies on NTSC type DV video */
656103485Sikob		tmpsync = (u_int64_t)3072 * 8000 * 100 / 2997;
657103285Sikob		tmpsync *= it->dvsync;
658103285Sikob		dvsync = tmpsync;
659103285Sikob		dvsync %= 0xc00;
660103285Sikob		fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00));
661103285Sikob		it->dvsync ++;
662103285Sikob		it->dvsync %= 2997;
663103285Sikob
664103285Sikob		for( i = 0, j = 0 ; i < it->dvpacket ; i++){
665103285Sikob			bcopy(it->dvdma->buf + it->queued * it->psize,
666103285Sikob				it->stproc->buf + j * it->psize, it->psize);
667103285Sikob			fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
668103285Sikob			fp->mode.stream.len = htons(488);
669103285Sikob			fp->mode.stream.chtag = chtag;
670103285Sikob			fp->mode.stream.tcode = FWTCODE_STREAM;
671103285Sikob			fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc);
672103285Sikob			it->dvdbc++;
673103285Sikob			it->dvdbc %= 256;
674103285Sikob			it->queued ++;
675103285Sikob			j++;
676103285Sikob/* XXX: Parameter relies on NTSC type DV video */
677103285Sikob#if 1
678103285Sikob#define DVDIFF 203
679103285Sikob#define DVFRAC 2997
680103285Sikob#else
681103285Sikob#define DVDIFF 127
682103285Sikob#define DVFRAC 1875
683103285Sikob#endif
684103285Sikob			it->dvdiff += DVDIFF;
685103285Sikob			if(it->dvdiff >= DVFRAC){
686103285Sikob				it->dvdiff %= DVFRAC;
687103285Sikob				fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
688103285Sikob
689103285Sikob				fp->mode.stream.len = htons(0x8);
690103285Sikob				fp->mode.stream.chtag = chtag;
691103285Sikob				fp->mode.stream.tcode = FWTCODE_STREAM;
692103285Sikob				fp->mode.ld[1] = htonl((fc->nodeid << 24) |
693103285Sikob					 0x00780000 | it->dvdbc);
694103285Sikob				j++;
695103285Sikob			}
696103285Sikob		}
697103285Sikob		it->stproc->npacket = j;
698103285Sikob		s = splfw();
699103285Sikob		STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
700103285Sikob		splx(s);
701103285Sikob		if(it->queued >= it->dvpacket){
702103285Sikob			s = splfw();
703103285Sikob			STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link);
704103285Sikob			it->dvdma = NULL;
705103285Sikob			splx(s);
706103285Sikob			wakeup(it);
707103285Sikob			goto dvloop;
708103285Sikob		}
709103285Sikob	}
710103285Sikob	return err;
711103285Sikob}
712103285Sikob/*
713103285Sikob * receving buffer update.
714103285Sikob */
715103285Sikobint
716103285Sikobfw_rbuf_update(struct firewire_comm *fc, int sub, int flag){
717103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
718103285Sikob	struct fw_xferq *ir;
719103285Sikob	int s, err = 0;
720103285Sikob
721103285Sikob	ir = fc->ir[sub];
722103285Sikob	s = splfw();
723103285Sikob	if(ir->stdma != NULL){
724103285Sikob		if(flag != 0){
725103285Sikob			STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link);
726103285Sikob		}else{
727103285Sikob			ir->stdma->flag = 0;
728103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link);
729103285Sikob		}
730103285Sikob	}
731103285Sikob	if(ir->stdma2 != NULL){
732103285Sikob		bulkxfer = ir->stdma2;
733103285Sikob		bulkxfer2 = STAILQ_FIRST(&ir->stfree);
734103285Sikob		if(bulkxfer2 != NULL){
735103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
736103285Sikob		}
737103285Sikob	}else{
738103285Sikob		bulkxfer = STAILQ_FIRST(&ir->stfree);
739103285Sikob		if(bulkxfer != NULL){
740103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
741103285Sikob			bulkxfer2 = STAILQ_FIRST(&ir->stfree);
742103285Sikob			if(bulkxfer2 != NULL){
743103285Sikob				STAILQ_REMOVE_HEAD(&ir->stfree, link);
744103285Sikob			}
745103285Sikob		}else{
746103285Sikob			bulkxfer = STAILQ_FIRST(&ir->stvalid);
747103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
748103285Sikob		}
749103285Sikob	}
750103285Sikob	splx(s);
751103285Sikob	ir->stdma = bulkxfer;
752103285Sikob	ir->stdma2 = bulkxfer2;
753103285Sikob	return err;
754103285Sikob}
755103285Sikob/*
756103285Sikob * ioctl support.
757103285Sikob */
758103285Sikobint
759103285Sikobfw_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
760103285Sikob{
761103285Sikob	struct firewire_softc *sc;
762103285Sikob	int unit = DEV2UNIT(dev);
763103285Sikob	int sub = DEV2DMACH(dev);
764103285Sikob	int i, len, err = 0;
765103285Sikob	struct fw_device *fwdev;
766103285Sikob	struct fw_bind *fwb;
767103285Sikob	struct fw_xferq *ir, *it;
768103285Sikob	struct fw_xfer *xfer;
769103285Sikob	struct fw_pkt *fp;
770103285Sikob
771103285Sikob	struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data;
772103285Sikob	struct fw_asyreq *asyreq = (struct fw_asyreq *)data;
773103285Sikob	struct fw_isochreq *ichreq = (struct fw_isochreq *)data;
774103285Sikob	struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data;
775103285Sikob	struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data;
776103285Sikob#if 0
777103285Sikob	struct fw_map_buf *map_buf = (struct fw_map_buf *)data;
778103285Sikob#endif
779103285Sikob	struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data;
780103285Sikob
781103285Sikob	if (DEV_FWMEM(dev))
782103285Sikob		return fwmem_ioctl(dev, cmd, data, flag, td);
783103285Sikob
784103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
785103285Sikob	if (!data)
786103285Sikob		return(EINVAL);
787103285Sikob
788103285Sikob	switch (cmd) {
789103285Sikob	case FW_STSTREAM:
790103285Sikob		sc->fc->it[sub]->flag &= ~0xff;
791103285Sikob		sc->fc->it[sub]->flag |= (0x3f & ichreq->ch);
792103285Sikob		sc->fc->it[sub]->flag |= ((0x3 & ichreq->tag) << 6);
793103285Sikob		err = 0;
794103285Sikob		break;
795103285Sikob	case FW_GTSTREAM:
796103285Sikob		ichreq->ch = sc->fc->it[sub]->flag & 0x3f;
797103285Sikob		ichreq->tag =(sc->fc->it[sub]->flag) >> 2 & 0x3;
798103285Sikob		err = 0;
799103285Sikob		break;
800103285Sikob	case FW_SRSTREAM:
801103285Sikob		sc->fc->ir[sub]->flag &= ~0xff;
802103285Sikob		sc->fc->ir[sub]->flag |= (0x3f & ichreq->ch);
803103285Sikob		sc->fc->ir[sub]->flag |= ((0x3 & ichreq->tag) << 6);
804103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
805103285Sikob		break;
806103285Sikob	case FW_GRSTREAM:
807103285Sikob		ichreq->ch = sc->fc->ir[sub]->flag & 0x3f;
808103285Sikob		ichreq->tag =(sc->fc->ir[sub]->flag) >> 2 & 0x3;
809103285Sikob		err = 0;
810103285Sikob		break;
811103285Sikob	case FW_SSTDV:
812103285Sikob		ibufreq = (struct fw_isobufreq *)
813103285Sikob			malloc(sizeof(struct fw_isobufreq), M_DEVBUF, M_NOWAIT);
814103285Sikob		if(ibufreq == NULL){
815103285Sikob			err = ENOMEM;
816103285Sikob			break;
817103285Sikob		}
818103285Sikob#define FWDVPACKET 250
819103285Sikob#define FWDVPMAX 512
820103285Sikob		ibufreq->rx.nchunk = 8;
821103285Sikob		ibufreq->rx.npacket = 50;
822103285Sikob		ibufreq->rx.psize = FWDVPMAX;
823103285Sikob
824103285Sikob		ibufreq->tx.nchunk = 5;
825103285Sikob		ibufreq->tx.npacket = 300;
826103285Sikob		ibufreq->tx.psize = FWDVPMAX;
827103285Sikob
828103285Sikob		err = fw_ioctl(dev, FW_SSTBUF, (caddr_t)ibufreq, flag, td);
829103285Sikob		sc->fc->it[sub]->dvpacket = FWDVPACKET;
830103285Sikob		free(ibufreq, M_DEVBUF);
831103285Sikob/* reserve a buffer space */
832103285Sikob#define NDVCHUNK 8
833103285Sikob		sc->fc->it[sub]->dvproc = NULL;
834103285Sikob		sc->fc->it[sub]->dvdma = NULL;
835103285Sikob		sc->fc->it[sub]->flag |= FWXFERQ_DV;
836103285Sikob		sc->fc->it[sub]->dvbuf
837103285Sikob			= (struct fw_dvbuf *)malloc(sizeof(struct fw_dvbuf) * NDVCHUNK, M_DEVBUF, M_DONTWAIT);
838103285Sikob		STAILQ_INIT(&sc->fc->it[sub]->dvvalid);
839103285Sikob		STAILQ_INIT(&sc->fc->it[sub]->dvfree);
840103285Sikob		for( i = 0 ; i < NDVCHUNK ; i++){
841103285Sikob			sc->fc->it[sub]->dvbuf[i].buf
842103285Sikob				= malloc(FWDVPMAX * sc->fc->it[sub]->dvpacket, M_DEVBUF, M_DONTWAIT);
843103285Sikob			STAILQ_INSERT_TAIL(&sc->fc->it[sub]->dvfree,
844103285Sikob					&sc->fc->it[sub]->dvbuf[i], link);
845103285Sikob		}
846103285Sikob		break;
847103285Sikob	case FW_SSTBUF:
848103285Sikob		ir = sc->fc->ir[sub];
849103285Sikob		it = sc->fc->it[sub];
850103285Sikob
851103285Sikob		if(ir->flag & FWXFERQ_RUNNING || it->flag & FWXFERQ_RUNNING){
852103285Sikob			return(EBUSY);
853103285Sikob		}
854103285Sikob		if((ir->flag & FWXFERQ_EXTBUF) || (it->flag & FWXFERQ_EXTBUF)){
855103285Sikob			return(EBUSY);
856103285Sikob		}
857103285Sikob		if((ibufreq->rx.nchunk *
858103285Sikob			ibufreq->rx.psize * ibufreq->rx.npacket) +
859103285Sikob		   (ibufreq->tx.nchunk *
860103285Sikob			ibufreq->tx.psize * ibufreq->tx.npacket) <= 0){
861103285Sikob				return(EINVAL);
862103285Sikob		}
863103285Sikob		if(ibufreq->rx.nchunk > FWSTMAXCHUNK ||
864103285Sikob				ibufreq->tx.nchunk > FWSTMAXCHUNK){
865103285Sikob			return(EINVAL);
866103285Sikob		}
867103285Sikob		ir->bulkxfer
868103285Sikob			= (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->rx.nchunk, M_DEVBUF, M_DONTWAIT);
869103285Sikob		if(ir->bulkxfer == NULL){
870103285Sikob			return(ENOMEM);
871103285Sikob		}
872103285Sikob		it->bulkxfer
873103285Sikob			= (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->tx.nchunk, M_DEVBUF, M_DONTWAIT);
874103285Sikob		if(it->bulkxfer == NULL){
875103285Sikob			return(ENOMEM);
876103285Sikob		}
877103285Sikob		ir->buf = malloc(
878103285Sikob			ibufreq->rx.nchunk * ibufreq->rx.npacket
879103285Sikob			* ((ibufreq->rx.psize + 3) &~3),
880103285Sikob			M_DEVBUF, M_DONTWAIT);
881103285Sikob		if(ir->buf == NULL){
882103285Sikob			free(ir->bulkxfer, M_DEVBUF);
883103285Sikob			free(it->bulkxfer, M_DEVBUF);
884103285Sikob			ir->bulkxfer = NULL;
885103285Sikob			it->bulkxfer = NULL;
886103285Sikob			it->buf = NULL;
887103285Sikob			return(ENOMEM);
888103285Sikob		}
889103285Sikob		it->buf = malloc(
890103285Sikob			ibufreq->tx.nchunk * ibufreq->tx.npacket
891103285Sikob			* ((ibufreq->tx.psize + 3) &~3),
892103285Sikob			M_DEVBUF, M_DONTWAIT);
893103285Sikob		if(it->buf == NULL){
894103285Sikob			free(ir->bulkxfer, M_DEVBUF);
895103285Sikob			free(it->bulkxfer, M_DEVBUF);
896103285Sikob			free(ir->buf, M_DEVBUF);
897103285Sikob			ir->bulkxfer = NULL;
898103285Sikob			it->bulkxfer = NULL;
899103285Sikob			it->buf = NULL;
900103285Sikob			return(ENOMEM);
901103285Sikob		}
902103285Sikob
903103285Sikob		ir->bnchunk = ibufreq->rx.nchunk;
904103285Sikob		ir->bnpacket = ibufreq->rx.npacket;
905103285Sikob		ir->btpacket = ibufreq->rx.npacket;
906103285Sikob		ir->psize = (ibufreq->rx.psize + 3) & ~3;
907103285Sikob		ir->queued = 0;
908103285Sikob
909103285Sikob		it->bnchunk = ibufreq->tx.nchunk;
910103285Sikob		it->bnpacket = ibufreq->tx.npacket;
911103285Sikob		it->btpacket = ibufreq->tx.npacket;
912103285Sikob		it->psize = (ibufreq->tx.psize + 3) & ~3;
913103285Sikob		ir->queued = 0;
914103285Sikob		it->dvdbc = 0;
915103285Sikob		it->dvdiff = 0;
916103285Sikob		it->dvsync = 0;
917103285Sikob
918103285Sikob		STAILQ_INIT(&ir->stvalid);
919103285Sikob		STAILQ_INIT(&ir->stfree);
920103285Sikob		ir->stdma = NULL;
921103285Sikob		ir->stdma2 = NULL;
922103285Sikob		ir->stproc = NULL;
923103285Sikob
924103285Sikob		STAILQ_INIT(&it->stvalid);
925103285Sikob		STAILQ_INIT(&it->stfree);
926103285Sikob		it->stdma = NULL;
927103285Sikob		it->stdma2 = NULL;
928103285Sikob		it->stproc = NULL;
929103285Sikob
930103285Sikob		for(i = 0 ; i < sc->fc->ir[sub]->bnchunk; i++){
931103285Sikob			ir->bulkxfer[i].buf =
932103285Sikob				ir->buf +
933103285Sikob				i * sc->fc->ir[sub]->bnpacket *
934103285Sikob			  	sc->fc->ir[sub]->psize;
935103285Sikob			ir->bulkxfer[i].flag = 0;
936103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree,
937103285Sikob					&ir->bulkxfer[i], link);
938103285Sikob			ir->bulkxfer[i].npacket = ir->bnpacket;
939103285Sikob		}
940103285Sikob		for(i = 0 ; i < sc->fc->it[sub]->bnchunk; i++){
941103285Sikob			it->bulkxfer[i].buf =
942103285Sikob				it->buf +
943103285Sikob				i * sc->fc->it[sub]->bnpacket *
944103285Sikob			  	sc->fc->it[sub]->psize;
945103285Sikob			it->bulkxfer[i].flag = 0;
946103285Sikob			STAILQ_INSERT_TAIL(&it->stfree,
947103285Sikob					&it->bulkxfer[i], link);
948103285Sikob			it->bulkxfer[i].npacket = it->bnpacket;
949103285Sikob		}
950103285Sikob		ir->flag &= ~FWXFERQ_MODEMASK;
951103285Sikob		ir->flag |= FWXFERQ_STREAM;
952103285Sikob		ir->flag |= FWXFERQ_EXTBUF;
953103285Sikob
954103285Sikob		it->flag &= ~FWXFERQ_MODEMASK;
955103285Sikob		it->flag |= FWXFERQ_STREAM;
956103285Sikob		it->flag |= FWXFERQ_EXTBUF;
957103285Sikob		err = 0;
958103285Sikob		break;
959103285Sikob	case FW_GSTBUF:
960103285Sikob		ibufreq->rx.nchunk = sc->fc->ir[sub]->bnchunk;
961103285Sikob		ibufreq->rx.npacket = sc->fc->ir[sub]->bnpacket;
962103285Sikob		ibufreq->rx.psize = sc->fc->ir[sub]->psize;
963103285Sikob
964103285Sikob		ibufreq->tx.nchunk = sc->fc->it[sub]->bnchunk;
965103285Sikob		ibufreq->tx.npacket = sc->fc->it[sub]->bnpacket;
966103285Sikob		ibufreq->tx.psize = sc->fc->it[sub]->psize;
967103285Sikob		break;
968103285Sikob	case FW_ASYREQ:
969103285Sikob		xfer = fw_xfer_alloc();
970103285Sikob		if(xfer == NULL){
971103285Sikob			err = ENOMEM;
972103285Sikob			return err;
973103285Sikob		}
974103285Sikob		fp = &asyreq->pkt;
975103285Sikob		switch (asyreq->req.type) {
976103285Sikob		case FWASREQNODE:
977103285Sikob			xfer->dst = ntohs(fp->mode.hdr.dst);
978103285Sikob			break;
979103285Sikob		case FWASREQEUI:
980103285Sikob			xfer->dst = fw_noderesolve(sc->fc, asyreq->req.dst.eui);
981103285Sikob			if(xfer->dst == FWNODE_INVAL ){
982103285Sikob				printf("%s:cannot found node\n",
983103285Sikob					device_get_nameunit(sc->fc->dev));
984103285Sikob				err = EINVAL;
985103285Sikob				goto error;
986103285Sikob			}
987103285Sikob			fp->mode.hdr.dst = htons(FWLOCALBUS | xfer->dst);
988103285Sikob			break;
989103285Sikob		case FWASRESTL:
990103285Sikob			/* XXX what's this? */
991103285Sikob			break;
992103285Sikob		case FWASREQSTREAM:
993103285Sikob			/* nothing to do */
994103285Sikob			break;
995103285Sikob		}
996103285Sikob		xfer->spd = asyreq->req.sped;
997103285Sikob		xfer->send.len = asyreq->req.len;
998103285Sikob		xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT);
999103285Sikob		if(xfer->send.buf == NULL){
1000103285Sikob			return ENOMEM;
1001103285Sikob		}
1002103285Sikob		xfer->send.off = 0;
1003103285Sikob		bcopy(fp, xfer->send.buf, xfer->send.len);
1004103285Sikob		xfer->act.hand = fw_asy_callback;
1005103285Sikob		err = fw_asyreq(sc->fc, sub, xfer);
1006103285Sikob		if(err){
1007103285Sikob			fw_xfer_free( xfer);
1008103285Sikob			return err;
1009103285Sikob		}
1010103285Sikob		err = tsleep((caddr_t)xfer, FWPRI, "asyreq", hz);
1011103285Sikob		if(err == 0){
1012103285Sikob			if(asyreq->req.len >= xfer->recv.len){
1013103285Sikob				asyreq->req.len = xfer->recv.len;
1014103285Sikob			}else{
1015103285Sikob				err = EINVAL;
1016103285Sikob			}
1017103285Sikob			bcopy(xfer->recv.buf + xfer->recv.off, fp, asyreq->req.len);
1018103285Sikob		}
1019103285Sikoberror:
1020103285Sikob		fw_xfer_free( xfer);
1021103285Sikob		break;
1022103285Sikob	case FW_IBUSRST:
1023103285Sikob		sc->fc->ibr(sc->fc);
1024103285Sikob		break;
1025103285Sikob	case FW_CBINDADDR:
1026103285Sikob		fwb = fw_bindlookup(sc->fc,
1027103285Sikob				bindreq->start.hi, bindreq->start.lo);
1028103285Sikob		if(fwb == NULL){
1029103285Sikob			err = EINVAL;
1030103285Sikob			break;
1031103285Sikob		}
1032103285Sikob		STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
1033103285Sikob		STAILQ_REMOVE(&sc->fc->ir[sub]->binds, fwb, fw_bind, chlist);
1034103285Sikob		free(fwb, M_DEVBUF);
1035103285Sikob		break;
1036103285Sikob	case FW_SBINDADDR:
1037103285Sikob		if(bindreq->len <= 0 ){
1038103285Sikob			err = EINVAL;
1039103285Sikob			break;
1040103285Sikob		}
1041103285Sikob		if(bindreq->start.hi > 0xffff ){
1042103285Sikob			err = EINVAL;
1043103285Sikob			break;
1044103285Sikob		}
1045103285Sikob		fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
1046103285Sikob		if(fwb == NULL){
1047103285Sikob			err = ENOMEM;
1048103285Sikob			break;
1049103285Sikob		}
1050103285Sikob		fwb->start_hi = bindreq->start.hi;
1051103285Sikob		fwb->start_lo = bindreq->start.lo;
1052103285Sikob		fwb->addrlen = bindreq->len;
1053103285Sikob
1054103285Sikob		xfer = fw_xfer_alloc();
1055103285Sikob		if(xfer == NULL){
1056103285Sikob			err = ENOMEM;
1057103285Sikob			return err;
1058103285Sikob		}
1059103285Sikob		xfer->act_type = FWACT_CH;
1060103285Sikob		xfer->sub = sub;
1061103285Sikob		xfer->fc = sc->fc;
1062103285Sikob
1063103285Sikob		fwb->xfer = xfer;
1064103285Sikob		err = fw_bindadd(sc->fc, fwb);
1065103285Sikob		break;
1066103285Sikob	case FW_GDEVLST:
1067103285Sikob		i = 0;
1068103285Sikob		for(fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
1069103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1070103285Sikob			if(i < fwdevlst->n){
1071103285Sikob				fwdevlst->dst[i] = fwdev->dst;
1072103285Sikob				fwdevlst->status[i] =
1073103285Sikob					(fwdev->status == FWDEVATTACHED)?1:0;
1074103285Sikob				fwdevlst->eui[i].hi = fwdev->eui.hi;
1075103285Sikob				fwdevlst->eui[i].lo = fwdev->eui.lo;
1076103285Sikob			}
1077103285Sikob			i++;
1078103285Sikob		}
1079103285Sikob		fwdevlst->n = i;
1080103285Sikob		break;
1081103285Sikob	case FW_GTPMAP:
1082103285Sikob		bcopy(sc->fc->topology_map, data,
1083103285Sikob				(sc->fc->topology_map->crc_len + 1) * 4);
1084103285Sikob		break;
1085103285Sikob	case FW_GSPMAP:
1086103285Sikob		/* speed_map is larger than a page */
1087103285Sikob		err = copyout(sc->fc->speed_map, *(void **)data,
1088103285Sikob				(sc->fc->speed_map->crc_len + 1) * 4);
1089103285Sikob		break;
1090103285Sikob	case FW_GCROM:
1091103285Sikob		for (fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
1092103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)) {
1093103285Sikob			if (fwdev->eui.hi == crom_buf->eui.hi &&
1094103285Sikob					fwdev->eui.lo == crom_buf->eui.lo)
1095103285Sikob				break;
1096103285Sikob		}
1097103285Sikob		if (fwdev == NULL) {
1098103285Sikob			err = FWNODE_INVAL;
1099103285Sikob			break;
1100103285Sikob		}
1101103285Sikob#if 0
1102103285Sikob		if (fwdev->csrrom[0] >> 24 == 1)
1103103285Sikob			len = 4;
1104103285Sikob		else
1105103285Sikob			len = (1 + ((fwdev->csrrom[0] >> 16) & 0xff)) * 4;
1106103285Sikob#else
1107103285Sikob		if (fwdev->rommax < CSRROMOFF)
1108103285Sikob			len = 0;
1109103285Sikob		else
1110103285Sikob			len = fwdev->rommax - CSRROMOFF + 4;
1111103285Sikob#endif
1112103285Sikob		if (crom_buf->len < len)
1113103285Sikob			len = crom_buf->len;
1114103285Sikob		else
1115103285Sikob			crom_buf->len = len;
1116103285Sikob		err = copyout(&fwdev->csrrom[0], crom_buf->ptr, len);
1117103285Sikob		break;
1118103285Sikob	default:
1119103285Sikob		sc->fc->ioctl (dev, cmd, data, flag, td);
1120103285Sikob		break;
1121103285Sikob	}
1122103285Sikob	return err;
1123103285Sikob}
1124103285Sikobint
1125103285Sikobfw_poll(dev_t dev, int events, fw_proc *td)
1126103285Sikob{
1127103285Sikob	int revents;
1128103285Sikob	int tmp;
1129103285Sikob	int unit = DEV2UNIT(dev);
1130103285Sikob	int sub = DEV2DMACH(dev);
1131103285Sikob	struct firewire_softc *sc;
1132103285Sikob
1133103285Sikob	if (DEV_FWMEM(dev))
1134103285Sikob		return fwmem_poll(dev, events, td);
1135103285Sikob
1136103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
1137103285Sikob	revents = 0;
1138103285Sikob	tmp = POLLIN | POLLRDNORM;
1139103285Sikob	if (events & tmp) {
1140103285Sikob		if (STAILQ_FIRST(&sc->fc->ir[sub]->q) != NULL)
1141103285Sikob			revents |= tmp;
1142103285Sikob		else
1143103285Sikob			selrecord(td, &sc->fc->ir[sub]->rsel);
1144103285Sikob	}
1145103285Sikob	tmp = POLLOUT | POLLWRNORM;
1146103285Sikob	if (events & tmp) {
1147103285Sikob		/* XXX should be fixed */
1148103285Sikob		revents |= tmp;
1149103285Sikob	}
1150103285Sikob
1151103285Sikob	return revents;
1152103285Sikob}
1153103285Sikob
1154103285Sikob/*
1155103285Sikob * To lookup node id. from EUI64.
1156103285Sikob */
1157106790Ssimokawau_int16_t
1158106790Ssimokawafw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui)
1159103285Sikob{
1160103285Sikob	struct fw_device *fwdev;
1161103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1162103285Sikob		fwdev = TAILQ_NEXT(fwdev, link)){
1163103285Sikob		if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){
1164103285Sikob			break;
1165103285Sikob		}
1166103285Sikob	}
1167103285Sikob	if(fwdev == NULL) return FWNODE_INVAL;
1168103285Sikob	if(fwdev->status != FWDEVATTACHED) return FWNODE_INVAL;
1169103285Sikob	return fwdev->dst;
1170103285Sikob}
1171103285Sikob/*
1172103285Sikob * Async. request procedure for userland application.
1173103285Sikob */
1174103285Sikobint
1175103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
1176103285Sikob{
1177103285Sikob	int err = 0;
1178103285Sikob	struct fw_xferq *xferq;
1179103285Sikob	int tl = 0, len;
1180103285Sikob	struct fw_pkt *fp;
1181103285Sikob	int tcode;
1182103285Sikob	struct tcode_info *info;
1183103285Sikob
1184103285Sikob	if(xfer == NULL) return EINVAL;
1185103285Sikob	if(xfer->send.len > fc->maxrec){
1186103285Sikob		printf("send.len > maxrec\n");
1187103285Sikob		return EINVAL;
1188103285Sikob	}
1189103285Sikob	if(xfer->act.hand == NULL){
1190103285Sikob		printf("act.hand == NULL\n");
1191103285Sikob		return EINVAL;
1192103285Sikob	}
1193103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1194103285Sikob
1195103285Sikob	tcode = fp->mode.common.tcode & 0xf;
1196103285Sikob	info = &fc->tcode[tcode];
1197103285Sikob	if (info->flag == 0) {
1198103285Sikob		printf("invalid tcode=%d\n", tcode);
1199103285Sikob		return EINVAL;
1200103285Sikob	}
1201103285Sikob	if (info->flag & FWTI_REQ)
1202103285Sikob		xferq = fc->atq;
1203103285Sikob	else
1204103285Sikob		xferq = fc->ats;
1205103285Sikob	len = info->hdr_len;
1206103285Sikob	if (info->flag & FWTI_BLOCK_STR)
1207103285Sikob		len += ntohs(fp->mode.stream.len);
1208103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
1209103285Sikob		len += ntohs(fp->mode.rresb.len);
1210103285Sikob	if( len >  xfer->send.len ){
1211103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
1212103285Sikob				len, xfer->send.len, tcode);
1213103285Sikob		return EINVAL;
1214103285Sikob	}
1215103285Sikob	xfer->send.len = len;
1216106790Ssimokawa
1217103285Sikob	if(xferq->start == NULL){
1218103285Sikob		printf("xferq->start == NULL\n");
1219103285Sikob		return EINVAL;
1220103285Sikob	}
1221103285Sikob	if(!(xferq->queued < xferq->maxq)){
1222103285Sikob		printf("%s:Discard a packet (queued=%d)\n",
1223103285Sikob			device_get_nameunit(fc->dev), xferq->queued);
1224103285Sikob		return EINVAL;
1225103285Sikob	}
1226103285Sikob
1227103285Sikob
1228103285Sikob	if (info->flag & FWTI_TLABEL) {
1229103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
1230103285Sikob			return EIO;
1231103285Sikob		fp->mode.hdr.tlrt = tl << 2;
1232103285Sikob	}
1233103285Sikob
1234103285Sikob	xfer->tl = tl;
1235103285Sikob	xfer->tcode = tcode;
1236103285Sikob	xfer->resp = 0;
1237103285Sikob	xfer->fc = fc;
1238103285Sikob	xfer->q = xferq;
1239103285Sikob	xfer->act_type = FWACT_XFER;
1240103285Sikob	xfer->retry_req = fw_asybusy;
1241103285Sikob
1242103285Sikob	fw_asystart(xfer);
1243103285Sikob	return err;
1244103285Sikob}
1245103285Sikob/*
1246103285Sikob * Wakeup blocked process.
1247103285Sikob */
1248103285Sikobvoid
1249103285Sikobfw_asy_callback(struct fw_xfer *xfer){
1250103285Sikob	wakeup(xfer);
1251103285Sikob	return;
1252103285Sikob}
1253103285Sikob/*
1254103285Sikob * Postpone to later retry.
1255103285Sikob */
1256103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
1257103285Sikob#if 0
1258103285Sikob	printf("fw_asybusy\n");
1259103285Sikob#endif
1260103285Sikob#if XFER_TIMEOUT
1261103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
1262103285Sikob#endif
1263103285Sikob/*
1264103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
1265103285Sikob*/
1266103285Sikob	DELAY(20000);
1267103285Sikob	fw_asystart(xfer);
1268103285Sikob	return;
1269103285Sikob}
1270103285Sikob#if XFER_TIMEOUT
1271103285Sikob/*
1272103285Sikob * Post timeout for async. request.
1273103285Sikob */
1274103285Sikobvoid
1275103285Sikobfw_xfer_timeout(void *arg)
1276103285Sikob{
1277103285Sikob	int s;
1278103285Sikob	struct fw_xfer *xfer;
1279103285Sikob
1280103285Sikob	xfer = (struct fw_xfer *)arg;
1281103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
1282103285Sikob	/* XXX set error code */
1283103285Sikob	s = splfw();
1284103285Sikob	xfer->act.hand(xfer);
1285103285Sikob	splx(s);
1286103285Sikob}
1287103285Sikob#endif
1288103285Sikob/*
1289103285Sikob * Async. request with given xfer structure.
1290103285Sikob */
1291106790Ssimokawastatic void
1292106790Ssimokawafw_asystart(struct fw_xfer *xfer)
1293106790Ssimokawa{
1294103285Sikob	struct firewire_comm *fc = xfer->fc;
1295103285Sikob	int s;
1296103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
1297103285Sikob		xfer->resp = EBUSY;
1298103285Sikob		xfer->state = FWXF_BUSY;
1299103285Sikob		xfer->act.hand(xfer);
1300103285Sikob		return;
1301103285Sikob	}
1302103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
1303103285Sikob	if (fc->status < FWBUSEXPLORE) {
1304103285Sikob		xfer->resp = EAGAIN;
1305103285Sikob		xfer->state = FWXF_BUSY;
1306103285Sikob		if (xfer->act.hand != NULL)
1307103285Sikob			xfer->act.hand(xfer);
1308103285Sikob		return;
1309103285Sikob	}
1310103285Sikob#endif
1311103285Sikob	s = splfw();
1312103285Sikob	xfer->state = FWXF_INQ;
1313103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
1314103285Sikob	xfer->q->queued ++;
1315103285Sikob	splx(s);
1316103285Sikob	/* XXX just queue for mbuf */
1317103285Sikob	if (xfer->mbuf == NULL)
1318103285Sikob		xfer->q->start(fc);
1319103285Sikob#if XFER_TIMEOUT
1320103285Sikob	if (xfer->act.hand != NULL)
1321103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
1322103285Sikob#endif
1323103285Sikob	return;
1324103285Sikob}
1325106790Ssimokawa
1326103285Sikobstatic int
1327103285Sikobfw_mmap (dev_t dev, vm_offset_t offset, int nproto)
1328103285Sikob{
1329103285Sikob	struct firewire_softc *fc;
1330103285Sikob	int unit = DEV2UNIT(dev);
1331103285Sikob
1332103285Sikob	if (DEV_FWMEM(dev))
1333103285Sikob		return fwmem_mmap(dev, offset, nproto);
1334103285Sikob
1335103285Sikob	fc = devclass_get_softc(firewire_devclass, unit);
1336103285Sikob
1337103285Sikob	return EINVAL;
1338103285Sikob}
1339106790Ssimokawa
1340103285Sikobstatic int
1341103285Sikobfirewire_match( device_t dev )
1342103285Sikob{
1343103285Sikob	device_set_desc(dev, "IEEE1394(Firewire) bus");
1344103285Sikob	return -140;
1345103285Sikob}
1346106790Ssimokawa
1347103285Sikob/*
1348103285Sikob * The attach routine.
1349103285Sikob */
1350103285Sikobstatic int
1351103285Sikobfirewire_attach( device_t dev )
1352103285Sikob{
1353103285Sikob	int i, unitmask, mn;
1354103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
1355103285Sikob	device_t pa = device_get_parent(dev);
1356103285Sikob	struct firewire_comm *fc;
1357103285Sikob	dev_t d;
1358103285Sikob
1359103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
1360103285Sikob	sc->fc = fc;
1361103285Sikob	sc->fc->dev = dev;
1362103285Sikob
1363103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
1364103285Sikob
1365103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
1366103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
1367103285Sikob		mn = unitmask | i;
1368103285Sikob		/* XXX device name should be improved */
1369103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
1370103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
1371103285Sikob			"fw%x", mn);
1372103285Sikob#if __FreeBSD_version >= 500000
1373103285Sikob		if (i == 0)
1374103285Sikob			sc->dev = d;
1375103285Sikob		else
1376103285Sikob			dev_depends(sc->dev, d);
1377103285Sikob#else
1378103285Sikob		sc->dev[i] = d;
1379103285Sikob#endif
1380103285Sikob	}
1381103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
1382103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
1383103285Sikob			"fwmem%d", device_get_unit(dev));
1384103285Sikob#if __FreeBSD_version >= 500000
1385103285Sikob	dev_depends(sc->dev, d);
1386103285Sikob#else
1387103285Sikob	sc->dev[i] = d;
1388103285Sikob#endif
1389103285Sikob	printf("%s: firewire bus attach\n", device_get_nameunit(sc->fc->dev));
1390103285Sikob	sc->fc->timeouthandle = timeout((timeout_t *)sc->fc->timeout, (void *)sc->fc, hz * 10);
1391103285Sikob
1392103285Sikob	/* Locate our children */
1393103285Sikob	bus_generic_probe(dev);
1394103285Sikob
1395103285Sikob	/* launch attachement of the added children */
1396103285Sikob	bus_generic_attach(dev);
1397103285Sikob
1398103285Sikob	/* bus_reset */
1399103285Sikob	fc->ibr(fc);
1400103285Sikob
1401103285Sikob	return 0;
1402103285Sikob}
1403103285Sikob
1404103285Sikob/*
1405103285Sikob * Attach it as child.
1406103285Sikob */
1407103285Sikobstatic device_t
1408103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
1409103285Sikob{
1410103285Sikob        device_t child;
1411103285Sikob	struct firewire_softc *sc;
1412103285Sikob
1413103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
1414103285Sikob	child = device_add_child(dev, name, unit);
1415103285Sikob	if (child) {
1416103285Sikob		device_set_ivars(child, sc->fc);
1417103285Sikob		device_probe_and_attach(child);
1418103285Sikob	}
1419103285Sikob
1420103285Sikob	return child;
1421103285Sikob}
1422106790Ssimokawa
1423103285Sikob/*
1424103285Sikob * Dettach it.
1425103285Sikob */
1426103285Sikobstatic int
1427103285Sikobfirewire_detach( device_t dev )
1428103285Sikob{
1429103285Sikob	struct firewire_softc *sc;
1430103285Sikob
1431103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
1432106790Ssimokawa
1433103285Sikob#if __FreeBSD_version >= 500000
1434103285Sikob	destroy_dev(sc->dev);
1435103285Sikob#else
1436103285Sikob	{
1437103285Sikob		int j;
1438103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
1439103285Sikob			destroy_dev(sc->dev[j]);
1440103285Sikob	}
1441103285Sikob#endif
1442103285Sikob	/* XXX xfree_free and untimeout on all xfers */
1443103285Sikob	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
1444103285Sikob	free(sc->fc->topology_map, M_DEVBUF);
1445103285Sikob	free(sc->fc->speed_map, M_DEVBUF);
1446103285Sikob	bus_generic_detach(dev);
1447103285Sikob	return(0);
1448103285Sikob}
1449103285Sikob#if 0
1450103285Sikobstatic int
1451103285Sikobfirewire_shutdown( device_t dev )
1452103285Sikob{
1453103285Sikob	return 0;
1454103285Sikob}
1455103285Sikob#endif
1456106790Ssimokawa
1457103285Sikob/*
1458106790Ssimokawa * Called after bus reset.
1459103285Sikob */
1460106790Ssimokawavoid
1461106790Ssimokawafw_busreset(struct firewire_comm *fc)
1462103285Sikob{
1463103285Sikob	int i;
1464103285Sikob	struct fw_xfer *xfer;
1465103285Sikob
1466103285Sikob	switch(fc->status){
1467103285Sikob	case FWBUSMGRELECT:
1468103285Sikob		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
1469103285Sikob		break;
1470103285Sikob	default:
1471103285Sikob		break;
1472103285Sikob	}
1473103285Sikob	fc->status = FWBUSRESET;
1474103285Sikob/* XXX: discard all queued packet */
1475103285Sikob	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
1476103285Sikob		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
1477103285Sikob		xfer->resp = EAGAIN;
1478103285Sikob		switch(xfer->act_type){
1479103285Sikob		case FWACT_XFER:
1480103285Sikob			fw_xfer_done(xfer);
1481103285Sikob			break;
1482103285Sikob		default:
1483103285Sikob			break;
1484103285Sikob		}
1485103285Sikob		fw_xfer_free( xfer);
1486103285Sikob	}
1487103285Sikob	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
1488103285Sikob		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
1489103285Sikob		xfer->resp = EAGAIN;
1490103285Sikob		switch(xfer->act_type){
1491103285Sikob		case FWACT_XFER:
1492103285Sikob			fw_xfer_done(xfer);
1493103285Sikob		default:
1494103285Sikob			break;
1495103285Sikob		}
1496103285Sikob		fw_xfer_free( xfer);
1497103285Sikob	}
1498103285Sikob	for(i = 0; i < fc->nisodma; i++)
1499103285Sikob		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
1500103285Sikob			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
1501103285Sikob			xfer->resp = 0;
1502103285Sikob			switch(xfer->act_type){
1503103285Sikob			case FWACT_XFER:
1504103285Sikob				fw_xfer_done(xfer);
1505103285Sikob				break;
1506103285Sikob			default:
1507103285Sikob				break;
1508103285Sikob			}
1509103285Sikob			fw_xfer_free( xfer);
1510103285Sikob		}
1511103285Sikob
1512103285Sikob	CSRARC(fc, STATE_CLEAR)
1513103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
1514103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1515103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
1516103285Sikob
1517103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
1518103285Sikob	fc->irm = -1;
1519103285Sikob
1520103285Sikob	fc->max_node = -1;
1521103285Sikob
1522103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
1523103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
1524103285Sikob	}
1525103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
1526103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1527103285Sikob	CSRARC(fc, RESET_START) = 0;
1528103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
1529103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
1530103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
1531103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
1532103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
1533103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
1534103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
1535103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
1536103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
1537103285Sikob
1538103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
1539103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
1540103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
1541103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
1542103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
1543103285Sikob
1544103285Sikob/* DV depend CSRs see blue book */
1545103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
1546103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
1547103285Sikob
1548103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
1549103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1550103285Sikob}
1551106790Ssimokawa
1552103285Sikob/* Call once after reboot */
1553106790Ssimokawavoid fw_init(struct firewire_comm *fc)
1554103285Sikob{
1555103285Sikob	int i;
1556106543Ssimokawa	struct csrdir *csrd;
1557106543Ssimokawa#ifdef FW_VMACCESS
1558103285Sikob	struct fw_xfer *xfer;
1559103285Sikob	struct fw_bind *fwb;
1560106543Ssimokawa#endif
1561103285Sikob
1562103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
1563103285Sikob
1564103285Sikob	fc->arq->queued = 0;
1565103285Sikob	fc->ars->queued = 0;
1566103285Sikob	fc->atq->queued = 0;
1567103285Sikob	fc->ats->queued = 0;
1568103285Sikob
1569103285Sikob	fc->arq->psize = FWPMAX_S400;
1570103285Sikob	fc->ars->psize = FWPMAX_S400;
1571103285Sikob	fc->atq->psize = FWPMAX_S400;
1572103285Sikob	fc->ats->psize = FWPMAX_S400;
1573103285Sikob
1574103285Sikob
1575103285Sikob	fc->arq->buf = NULL;
1576103285Sikob	fc->ars->buf = NULL;
1577103285Sikob	fc->atq->buf = NULL;
1578103285Sikob	fc->ats->buf = NULL;
1579103285Sikob
1580103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
1581103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
1582103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
1583103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
1584103285Sikob
1585103285Sikob	STAILQ_INIT(&fc->atq->q);
1586103285Sikob	STAILQ_INIT(&fc->ats->q);
1587103285Sikob
1588103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
1589103285Sikob		fc->it[i]->queued = 0;
1590103285Sikob		fc->ir[i]->queued = 0;
1591103285Sikob
1592103285Sikob		fc->it[i]->start = NULL;
1593103285Sikob		fc->ir[i]->start = NULL;
1594103285Sikob
1595103285Sikob		fc->it[i]->buf = NULL;
1596103285Sikob		fc->ir[i]->buf = NULL;
1597103285Sikob
1598103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
1599103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
1600103285Sikob
1601103285Sikob		STAILQ_INIT(&fc->it[i]->q);
1602103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
1603103285Sikob
1604103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
1605103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
1606103285Sikob	}
1607103285Sikob
1608103285Sikob	fc->arq->maxq = FWMAXQUEUE;
1609103285Sikob	fc->ars->maxq = FWMAXQUEUE;
1610103285Sikob	fc->atq->maxq = FWMAXQUEUE;
1611103285Sikob	fc->ats->maxq = FWMAXQUEUE;
1612103285Sikob
1613103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
1614103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
1615103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
1616103285Sikob	}
1617103285Sikob/* Initialize csr registers */
1618103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
1619103285Sikob				sizeof(struct fw_topology_map),
1620103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
1621103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
1622103285Sikob				sizeof(struct fw_speed_map),
1623103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
1624103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
1625103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
1626103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
1627103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
1628103285Sikob
1629103285Sikob	TAILQ_INIT(&fc->devices);
1630103285Sikob	STAILQ_INIT(&fc->pending);
1631103285Sikob
1632103285Sikob/* Initialize csr ROM work space */
1633103285Sikob	SLIST_INIT(&fc->ongocsr);
1634103285Sikob	SLIST_INIT(&fc->csrfree);
1635103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
1636103285Sikob		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_DONTWAIT);
1637103285Sikob		if(csrd == NULL) break;
1638103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1639103285Sikob	}
1640103285Sikob
1641103285Sikob/* Initialize Async handlers */
1642103285Sikob	STAILQ_INIT(&fc->binds);
1643103285Sikob	for( i = 0 ; i < 0x40 ; i++){
1644103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
1645103285Sikob	}
1646103285Sikob
1647103285Sikob/* DV depend CSRs see blue book */
1648103285Sikob#if 0
1649103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
1650103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
1651103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
1652103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
1653103285Sikob	}
1654103285Sikob
1655103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
1656103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
1657103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
1658103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
1659103285Sikob	}
1660103285Sikob#endif
1661103285Sikob
1662103285Sikob
1663106543Ssimokawa#ifdef FW_VMACCESS
1664103285Sikob	xfer = fw_xfer_alloc();
1665103285Sikob	if(xfer == NULL) return;
1666103285Sikob
1667103285Sikob	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
1668103285Sikob	if(fwb == NULL){
1669103285Sikob		fw_xfer_free(xfer);
1670103285Sikob	}
1671103285Sikob	xfer->act.hand = fw_vmaccess;
1672103285Sikob	xfer->act_type = FWACT_XFER;
1673103285Sikob	xfer->fc = fc;
1674103285Sikob	xfer->sc = NULL;
1675103285Sikob
1676103285Sikob	fwb->start_hi = 0x2;
1677103285Sikob	fwb->start_lo = 0;
1678103285Sikob	fwb->addrlen = 0xffffffff;
1679103285Sikob	fwb->xfer = xfer;
1680103285Sikob	fw_bindadd(fc, fwb);
1681106543Ssimokawa#endif
1682103285Sikob}
1683106790Ssimokawa
1684103285Sikob/*
1685103285Sikob * To lookup binded process from IEEE1394 address.
1686103285Sikob */
1687103285Sikobstatic struct fw_bind *
1688106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
1689103285Sikob{
1690103285Sikob	struct fw_bind *tfw;
1691103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
1692103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
1693103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
1694103285Sikob			tfw->start_hi == dest_hi &&
1695103285Sikob			tfw->start_lo <= dest_lo &&
1696103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
1697103285Sikob			return(tfw);
1698103285Sikob		}
1699103285Sikob	}
1700103285Sikob	return(NULL);
1701103285Sikob}
1702106790Ssimokawa
1703103285Sikob/*
1704103285Sikob * To bind IEEE1394 address block to process.
1705103285Sikob */
1706106790Ssimokawaint
1707106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
1708103285Sikob{
1709103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
1710103285Sikob	int err = 0;
1711103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
1712103285Sikob	if(tfw == NULL){
1713103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
1714103285Sikob		goto out;
1715103285Sikob	}
1716103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
1717103285Sikob		(tfw->start_hi == fwb->start_hi &&
1718103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
1719103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
1720103285Sikob		goto out;
1721103285Sikob	}
1722103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
1723103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
1724103285Sikob		   (tfw->start_hi == fwb->start_hi &&
1725103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
1726103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
1727103285Sikob			if(tfw2 == NULL)
1728103285Sikob				break;
1729103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
1730103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
1731103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
1732103285Sikob				break;
1733103285Sikob			}else{
1734103285Sikob				err = EBUSY;
1735103285Sikob				goto out;
1736103285Sikob			}
1737103285Sikob		}
1738103285Sikob	}
1739103285Sikob	if(tfw != NULL){
1740103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
1741103285Sikob	}else{
1742103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
1743103285Sikob	}
1744103285Sikobout:
1745103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
1746103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
1747103285Sikob	}
1748103285Sikob	return err;
1749103285Sikob}
1750103285Sikob
1751103285Sikob/*
1752103285Sikob * To free IEEE1394 address block.
1753103285Sikob */
1754106790Ssimokawaint
1755106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
1756103285Sikob{
1757103285Sikob	int s;
1758103285Sikob
1759103285Sikob	s = splfw();
1760103285Sikob	/* shall we check the existance? */
1761103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
1762103285Sikob	splx(s);
1763103285Sikob	if (fwb->xfer)
1764103285Sikob		fw_xfer_free(fwb->xfer);
1765103285Sikob
1766103285Sikob	return 0;
1767103285Sikob}
1768103285Sikob
1769103285Sikob/*
1770103285Sikob * To free transaction label.
1771103285Sikob */
1772106790Ssimokawastatic void
1773106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
1774103285Sikob{
1775103285Sikob	struct tlabel *tl;
1776103285Sikob	int s = splfw();
1777103285Sikob
1778103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
1779103285Sikob		tl = STAILQ_NEXT(tl, link)){
1780103285Sikob		if(tl->xfer == xfer){
1781103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
1782103285Sikob			free(tl, M_DEVBUF);
1783103285Sikob			splx(s);
1784103285Sikob			return;
1785103285Sikob		}
1786103285Sikob	}
1787103285Sikob	splx(s);
1788103285Sikob	return;
1789103285Sikob}
1790106790Ssimokawa
1791103285Sikob/*
1792103285Sikob * To obtain XFER structure by transaction label.
1793103285Sikob */
1794106790Ssimokawastatic struct fw_xfer *
1795106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
1796103285Sikob{
1797103285Sikob	struct fw_xfer *xfer;
1798103285Sikob	struct tlabel *tl;
1799103285Sikob	int s = splfw();
1800103285Sikob
1801103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
1802103285Sikob		tl = STAILQ_NEXT(tl, link)){
1803103285Sikob		if(tl->xfer->dst == node){
1804103285Sikob			xfer = tl->xfer;
1805103285Sikob			splx(s);
1806103285Sikob			return(xfer);
1807103285Sikob		}
1808103285Sikob	}
1809103285Sikob	splx(s);
1810103285Sikob	return(NULL);
1811103285Sikob}
1812106790Ssimokawa
1813103285Sikob/*
1814103285Sikob * To allocate IEEE1394 XFER structure.
1815103285Sikob */
1816106790Ssimokawastruct fw_xfer *
1817106790Ssimokawafw_xfer_alloc()
1818103285Sikob{
1819103285Sikob	struct fw_xfer *xfer;
1820106790Ssimokawa
1821103285Sikob	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT | M_ZERO);
1822106790Ssimokawa	if (xfer == NULL)
1823106790Ssimokawa		return xfer;
1824106790Ssimokawa
1825103285Sikob	xfer->time = time_second;
1826103285Sikob	xfer->sub = -1;
1827106790Ssimokawa
1828103285Sikob	return xfer;
1829103285Sikob}
1830106790Ssimokawa
1831103285Sikob/*
1832103285Sikob * IEEE1394 XFER post process.
1833103285Sikob */
1834103285Sikobvoid
1835103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1836103285Sikob{
1837103285Sikob	if (xfer->act.hand == NULL)
1838103285Sikob		return;
1839103285Sikob
1840103285Sikob#if XFER_TIMEOUT
1841103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
1842103285Sikob#endif
1843103285Sikob
1844103285Sikob	if (xfer->fc->status != FWBUSRESET)
1845103285Sikob		xfer->act.hand(xfer);
1846103285Sikob	else {
1847103285Sikob		printf("fw_xfer_done: pending\n");
1848103285Sikob		if (xfer->fc != NULL)
1849103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
1850103285Sikob		else
1851103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
1852103285Sikob	}
1853103285Sikob}
1854103285Sikob
1855103285Sikob/*
1856103285Sikob * To free IEEE1394 XFER structure.
1857103285Sikob */
1858106790Ssimokawavoid
1859106790Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1860103285Sikob{
1861103285Sikob	int s;
1862103285Sikob	if(xfer == NULL ) return;
1863103285Sikob	if(xfer->state == FWXF_INQ){
1864103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1865103285Sikob		s = splfw();
1866103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1867103285Sikob		xfer->q->queued --;
1868103285Sikob		splx(s);
1869103285Sikob	}
1870103285Sikob	if(xfer->fc != NULL){
1871103285Sikob		if(xfer->state == FWXF_START){
1872103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
1873103285Sikob			printf("fw_xfer_free FWXF_START\n");
1874103285Sikob#endif
1875103285Sikob			s = splfw();
1876103285Sikob			xfer->q->drain(xfer->fc, xfer);
1877103285Sikob			splx(s);
1878103285Sikob		}
1879103285Sikob	}
1880103285Sikob	if(xfer->send.buf != NULL){
1881103285Sikob		free(xfer->send.buf, M_DEVBUF);
1882103285Sikob	}
1883103285Sikob	if(xfer->recv.buf != NULL){
1884103285Sikob		free(xfer->recv.buf, M_DEVBUF);
1885103285Sikob	}
1886103285Sikob	if(xfer->fc != NULL){
1887103285Sikob		fw_tl_free(xfer->fc, xfer);
1888103285Sikob	}
1889103285Sikob	free(xfer, M_DEVBUF);
1890103285Sikob}
1891103285Sikob
1892103285Sikob/*
1893103285Sikob * Callback for PHY configuration.
1894103285Sikob */
1895103285Sikobstatic void
1896103285Sikobfw_phy_config_callback(struct fw_xfer *xfer)
1897103285Sikob{
1898103285Sikob#if 0
1899103285Sikob	printf("phy_config done state=%d resp=%d\n",
1900103285Sikob				xfer->state, xfer->resp);
1901103285Sikob#endif
1902103285Sikob	fw_xfer_free(xfer);
1903103285Sikob	/* XXX need bus reset ?? */
1904103285Sikob	/* sc->fc->ibr(xfer->fc);  LOOP */
1905103285Sikob}
1906103285Sikob
1907103285Sikob/*
1908103285Sikob * To configure PHY.
1909103285Sikob */
1910103285Sikobstatic void
1911103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1912103285Sikob{
1913103285Sikob	struct fw_xfer *xfer;
1914103285Sikob	struct fw_pkt *fp;
1915103285Sikob
1916103285Sikob	fc->status = FWBUSPHYCONF;
1917103285Sikob
1918103285Sikob	DELAY(100000);
1919103285Sikob	xfer = fw_xfer_alloc();
1920103285Sikob	xfer->send.len = 12;
1921103285Sikob	xfer->send.off = 0;
1922103285Sikob	xfer->fc = fc;
1923103285Sikob	xfer->retry_req = fw_asybusy;
1924103285Sikob	xfer->act.hand = fw_phy_config_callback;
1925103285Sikob
1926103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
1927103285Sikob					M_DEVBUF, M_DONTWAIT | M_ZERO);
1928103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1929103285Sikob	fp->mode.ld[1] = 0;
1930103285Sikob	if (root_node >= 0)
1931103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1932103285Sikob	if (gap_count >= 0)
1933103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1934103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1935103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1936103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1937103285Sikob
1938103285Sikob	printf("send phy_config root_node=%d gap_count=%d\n",
1939103285Sikob						root_node, gap_count);
1940103285Sikob	fw_asyreq(fc, -1, xfer);
1941103285Sikob}
1942103285Sikob
1943103285Sikob#if 0
1944103285Sikob/*
1945103285Sikob * Dump self ID.
1946103285Sikob */
1947103285Sikobstatic void
1948103285Sikobfw_print_sid(u_int32_t sid)
1949103285Sikob{
1950103285Sikob	union fw_self_id *s;
1951103285Sikob	s = (union fw_self_id *) &sid;
1952103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1953103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1954103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1955103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1956103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1957103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1958103285Sikob}
1959103285Sikob#endif
1960103285Sikob
1961103285Sikob/*
1962103285Sikob * To receive self ID.
1963103285Sikob */
1964103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1965103285Sikob{
1966103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1967103285Sikob	union fw_self_id *self_id;
1968103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1969103285Sikob
1970103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1971103285Sikob	fc->status = FWBUSINIT;
1972103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1973103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1974103285Sikob	fc->status = FWBUSCYMELECT;
1975103285Sikob	fc->topology_map->crc_len = 2;
1976103285Sikob	fc->topology_map->generation ++;
1977103285Sikob	fc->topology_map->self_id_count = 0;
1978103285Sikob	fc->topology_map->node_count = 0;
1979103285Sikob	fc->speed_map->generation ++;
1980103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1981103285Sikob	self_id = &fc->topology_map->self_id[0];
1982103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1983103285Sikob		if (sid[1] != ~sid[0]) {
1984103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1985103285Sikob			sid += 2;
1986103285Sikob			continue;
1987103285Sikob		}
1988103285Sikob		*self_id = *((union fw_self_id *)sid);
1989103285Sikob		fc->topology_map->crc_len++;
1990103285Sikob		if(self_id->p0.sequel == 0){
1991103285Sikob			fc->topology_map->node_count ++;
1992103285Sikob			c_port = 0;
1993103285Sikob#if 0
1994103285Sikob			fw_print_sid(sid[0]);
1995103285Sikob#endif
1996103285Sikob			node = self_id->p0.phy_id;
1997103285Sikob			if(fc->max_node < node){
1998103285Sikob				fc->max_node = self_id->p0.phy_id;
1999103285Sikob			}
2000103285Sikob			/* XXX I'm not sure this is the right speed_map */
2001103285Sikob			fc->speed_map->speed[node][node]
2002103285Sikob					= self_id->p0.phy_speed;
2003103285Sikob			for (j = 0; j < node; j ++) {
2004103285Sikob				fc->speed_map->speed[j][node]
2005103285Sikob					= fc->speed_map->speed[node][j]
2006103285Sikob					= min(fc->speed_map->speed[j][j],
2007103285Sikob							self_id->p0.phy_speed);
2008103285Sikob			}
2009103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
2010103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
2011103285Sikob				fc->irm = self_id->p0.phy_id;
2012103285Sikob			}
2013103285Sikob			if(self_id->p0.port0 >= 0x2){
2014103285Sikob				c_port++;
2015103285Sikob			}
2016103285Sikob			if(self_id->p0.port1 >= 0x2){
2017103285Sikob				c_port++;
2018103285Sikob			}
2019103285Sikob			if(self_id->p0.port2 >= 0x2){
2020103285Sikob				c_port++;
2021103285Sikob			}
2022103285Sikob		}
2023103285Sikob		if(c_port > 2){
2024103285Sikob			i_branch += (c_port - 2);
2025103285Sikob		}
2026103285Sikob		sid += 2;
2027103285Sikob		self_id++;
2028103285Sikob		fc->topology_map->self_id_count ++;
2029103285Sikob	}
2030103285Sikob	printf("%s: %d nodes", device_get_nameunit(fc->dev), fc->max_node + 1);
2031103285Sikob	/* CRC */
2032103285Sikob	fc->topology_map->crc = fw_crc16(
2033103285Sikob			(u_int32_t *)&fc->topology_map->generation,
2034103285Sikob			fc->topology_map->crc_len * 4);
2035103285Sikob	fc->speed_map->crc = fw_crc16(
2036103285Sikob			(u_int32_t *)&fc->speed_map->generation,
2037103285Sikob			fc->speed_map->crc_len * 4);
2038103285Sikob	/* byteswap and copy to CSR */
2039103285Sikob	p = (u_int32_t *)fc->topology_map;
2040103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
2041103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
2042103285Sikob	p = (u_int32_t *)fc->speed_map;
2043103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
2044103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
2045103285Sikob	/* don't byte-swap u_int8_t array */
2046103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
2047103285Sikob
2048103285Sikob	fc->max_hop = fc->max_node - i_branch;
2049103285Sikob#if 1
2050103285Sikob	printf(", maxhop <= %d", fc->max_hop);
2051103285Sikob#endif
2052103285Sikob
2053103285Sikob	if(fc->irm == -1 ){
2054103285Sikob		printf(", Not found IRM capable node");
2055103285Sikob	}else{
2056103285Sikob		printf(", cable IRM = %d", fc->irm);
2057103285Sikob		if (fc->irm == fc->nodeid)
2058103285Sikob			printf(" (me)\n");
2059103285Sikob		else
2060103285Sikob			printf("\n");
2061103285Sikob	}
2062103285Sikob
2063103285Sikob	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
2064103285Sikob		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
2065103285Sikob			fc->status = FWBUSMGRDONE;
2066103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
2067103285Sikob		}else{
2068103285Sikob			fc->status = FWBUSMGRELECT;
2069103285Sikob			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
2070103285Sikob		}
2071103285Sikob	}else{
2072103285Sikob		fc->status = FWBUSMGRDONE;
2073103285Sikob		printf("%s: BMR = %x\n", device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
2074103285Sikob	}
2075103285Sikob	free(buf, M_DEVBUF);
2076103285Sikob#if 1
2077103285Sikob	/* XXX optimize gap_count, if I am BMGR */
2078103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
2079103285Sikob		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
2080103285Sikob	}
2081103285Sikob#endif
2082103285Sikob#if 1
2083103285Sikob	timeout((timeout_t *)fw_bus_probe, (void *)fc, hz/4);
2084103285Sikob#else
2085103285Sikob	fw_bus_probe(fc);
2086103285Sikob#endif
2087103285Sikob}
2088106790Ssimokawa
2089103285Sikob/*
2090103285Sikob * To probe devices on the IEEE1394 bus.
2091103285Sikob */
2092106790Ssimokawastatic void
2093106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
2094103285Sikob{
2095103285Sikob	int s;
2096103285Sikob	struct fw_device *fwdev, *next;
2097103285Sikob
2098103285Sikob	s = splfw();
2099103285Sikob	fc->status = FWBUSEXPLORE;
2100103285Sikob	fc->retry_count = 0;
2101103285Sikob
2102103285Sikob/*
2103103285Sikob * Invalidate all devices, just after bus reset. Devices
2104103285Sikob * to be removed has not been seen longer time.
2105103285Sikob */
2106103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
2107103285Sikob		next = TAILQ_NEXT(fwdev, link);
2108103285Sikob		if(fwdev->status != FWDEVINVAL){
2109103285Sikob			fwdev->status = FWDEVINVAL;
2110103285Sikob			fwdev->rcnt = 0;
2111103285Sikob		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
2112103285Sikob			fwdev->rcnt ++;
2113103285Sikob		}else{
2114103285Sikob			TAILQ_REMOVE(&fc->devices, fwdev, link);
2115103285Sikob			free(fwdev, M_DEVBUF);
2116103285Sikob		}
2117103285Sikob	}
2118103285Sikob	fc->ongonode = 0;
2119103285Sikob	fc->ongoaddr = CSRROMOFF;
2120103285Sikob	fc->ongodev = NULL;
2121103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2122103285Sikob	fw_bus_explore(fc);
2123103285Sikob	splx(s);
2124103285Sikob}
2125106790Ssimokawa
2126103285Sikob/*
2127103285Sikob * To collect device informations on the IEEE1394 bus.
2128103285Sikob */
2129106790Ssimokawastatic void
2130106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
2131103285Sikob{
2132103285Sikob	int err = 0;
2133103285Sikob	struct fw_device *fwdev, *tfwdev;
2134103285Sikob	u_int32_t addr;
2135103285Sikob	struct fw_xfer *xfer;
2136103285Sikob	struct fw_pkt *fp;
2137103285Sikob
2138103285Sikob	if(fc->status != FWBUSEXPLORE)
2139103285Sikob		return;
2140103285Sikob
2141103285Sikobloop:
2142103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
2143103285Sikob
2144103285Sikob	if(fc->ongonode > fc->max_node) goto done;
2145103285Sikob	if(fc->ongonode >= 0x3f) goto done;
2146103285Sikob
2147103285Sikob	/* check link */
2148103285Sikob	/* XXX we need to check phy_id first */
2149103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
2150103285Sikob		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
2151103285Sikob		fc->ongonode++;
2152103285Sikob		goto loop;
2153103285Sikob	}
2154103285Sikob
2155103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
2156103285Sikob		fc->ongoeui.hi == 0xffffffff &&
2157103285Sikob		fc->ongoeui.lo == 0xffffffff ){
2158103285Sikob		fc->ongoaddr = CSRROMOFF;
2159103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2160103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
2161103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
2162103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2163103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
2164103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
2165103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2166103285Sikob	}else if(fc->ongodev == NULL){
2167103285Sikob		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
2168103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
2169103285Sikob			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
2170103285Sikob				break;
2171103285Sikob			}
2172103285Sikob		}
2173103285Sikob		if(fwdev != NULL){
2174103285Sikob			fwdev->dst = fc->ongonode;
2175103285Sikob			fwdev->status = FWDEVATTACHED;
2176103285Sikob			fc->ongonode++;
2177103285Sikob			fc->ongoaddr = CSRROMOFF;
2178103285Sikob			fc->ongodev = NULL;
2179103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2180103285Sikob			goto loop;
2181103285Sikob		}
2182103285Sikob		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
2183103285Sikob		if(fwdev == NULL)
2184103285Sikob			return;
2185103285Sikob		fwdev->rommax = 0;
2186103285Sikob		fwdev->dst = fc->ongonode;
2187103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
2188103285Sikob		fwdev->status = FWDEVINIT;
2189103285Sikob#if 0
2190103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
2191103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
2192103285Sikob#else
2193103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
2194103285Sikob#endif
2195103285Sikob
2196103285Sikob		tfwdev = TAILQ_FIRST(&fc->devices);
2197103285Sikob		while( tfwdev != NULL &&
2198103285Sikob			(tfwdev->eui.hi > fwdev->eui.hi) &&
2199103285Sikob			((tfwdev->eui.hi == fwdev->eui.hi) &&
2200103285Sikob				tfwdev->eui.lo > fwdev->eui.lo)){
2201103285Sikob			tfwdev = TAILQ_NEXT( tfwdev, link);
2202103285Sikob		}
2203103285Sikob		if(tfwdev == NULL){
2204103285Sikob			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
2205103285Sikob		}else{
2206103285Sikob			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
2207103285Sikob		}
2208103285Sikob
2209103285Sikob		printf("%s:Discover new %s device ID:%08x%08x\n", device_get_nameunit(fc->dev), linkspeed[fwdev->speed], fc->ongoeui.hi, fc->ongoeui.lo);
2210103285Sikob
2211103285Sikob		fc->ongodev = fwdev;
2212103285Sikob		fc->ongoaddr = CSRROMOFF;
2213103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2214103285Sikob	}else{
2215103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2216103285Sikob	}
2217103285Sikob#if 0
2218103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
2219103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
2220103285Sikob		fw_bus_explore_callback);
2221103285Sikob	if(xfer == NULL) goto done;
2222103285Sikob#else
2223103285Sikob	xfer = fw_xfer_alloc();
2224103285Sikob	if(xfer == NULL){
2225103285Sikob		goto done;
2226103285Sikob	}
2227103285Sikob	xfer->send.len = 16;
2228103285Sikob	xfer->spd = 0;
2229103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2230103285Sikob	if(xfer->send.buf == NULL){
2231103285Sikob		fw_xfer_free( xfer);
2232103285Sikob		return;
2233103285Sikob	}
2234103285Sikob
2235103285Sikob	xfer->send.off = 0;
2236103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2237103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
2238103285Sikob	fp->mode.rreqq.tlrt = 0;
2239103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2240103285Sikob	fp->mode.rreqq.pri = 0;
2241103285Sikob	fp->mode.rreqq.src = 0;
2242103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
2243103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2244103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
2245103285Sikob	xfer->act.hand = fw_bus_explore_callback;
2246103285Sikob
2247103285Sikob	err = fw_asyreq(fc, -1, xfer);
2248103285Sikob	if(err){
2249103285Sikob		fw_xfer_free( xfer);
2250103285Sikob		return;
2251103285Sikob	}
2252103285Sikob#endif
2253103285Sikob	return;
2254103285Sikobdone:
2255103285Sikob	/* fw_attach_devs */
2256103285Sikob	fc->status = FWBUSEXPDONE;
2257103285Sikob	printf("bus_explore done\n");
2258103285Sikob	fw_attach_dev(fc);
2259103285Sikob	return;
2260103285Sikob
2261103285Sikob}
2262106790Ssimokawa
2263103285Sikob/* Portable Async. request read quad */
2264106790Ssimokawastruct fw_xfer *
2265106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
2266105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
2267105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
2268103285Sikob{
2269103285Sikob	struct fw_xfer *xfer;
2270103285Sikob	struct fw_pkt *fp;
2271103285Sikob	int err;
2272103285Sikob
2273103285Sikob	xfer = fw_xfer_alloc();
2274103285Sikob	if(xfer == NULL){
2275103285Sikob		return NULL;
2276103285Sikob	}
2277103285Sikob	xfer->send.len = 16;
2278103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
2279103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2280103285Sikob	if(xfer->send.buf == NULL){
2281103285Sikob		fw_xfer_free( xfer);
2282103285Sikob		return NULL;
2283103285Sikob	}
2284103285Sikob
2285103285Sikob	xfer->send.off = 0;
2286103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2287103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
2288103285Sikob	if(tl & FWP_TL_VALID){
2289103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
2290103285Sikob	}else{
2291103285Sikob		fp->mode.rreqq.tlrt = 0;
2292103285Sikob	}
2293103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
2294103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2295103285Sikob	fp->mode.rreqq.pri = 0;
2296103285Sikob	fp->mode.rreqq.src = 0;
2297103285Sikob	xfer->dst = addr_hi >> 16;
2298103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2299103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
2300103285Sikob	xfer->act.hand = hand;
2301103285Sikob
2302103285Sikob	err = fw_asyreq(fc, -1, xfer);
2303103285Sikob	if(err){
2304103285Sikob		fw_xfer_free( xfer);
2305103285Sikob		return NULL;
2306103285Sikob	}
2307103285Sikob	return xfer;
2308103285Sikob}
2309106790Ssimokawa
2310103285Sikob/*
2311103285Sikob * Callback for the IEEE1394 bus information collection.
2312103285Sikob */
2313106790Ssimokawastatic void
2314106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
2315106790Ssimokawa{
2316103285Sikob	struct firewire_comm *fc;
2317103285Sikob	struct fw_pkt *sfp,*rfp;
2318103285Sikob	struct csrhdr *chdr;
2319103285Sikob	struct csrdir *csrd;
2320103285Sikob	struct csrreg *csrreg;
2321103285Sikob	u_int32_t offset;
2322103285Sikob
2323103285Sikob
2324103285Sikob	if(xfer == NULL) return;
2325103285Sikob	fc = xfer->fc;
2326103285Sikob	if(xfer->resp != 0){
2327103285Sikob		printf("resp != 0: node=%d addr=0x%x\n",
2328103285Sikob			fc->ongonode, fc->ongoaddr);
2329103285Sikob		fc->retry_count++;
2330103285Sikob		goto nextnode;
2331103285Sikob	}
2332103285Sikob
2333103285Sikob	if(xfer->send.buf == NULL){
2334103285Sikob		printf("send.buf == NULL: node=%d addr=0x%x\n",
2335103285Sikob			fc->ongonode, fc->ongoaddr);
2336103285Sikob		printf("send.buf == NULL\n");
2337103285Sikob		fc->retry_count++;
2338103285Sikob		goto nextnode;
2339103285Sikob	}
2340103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
2341103285Sikob
2342103285Sikob	if(xfer->recv.buf == NULL){
2343103285Sikob		printf("recv.buf == NULL: node=%d addr=0x%x\n",
2344103285Sikob			fc->ongonode, fc->ongoaddr);
2345103285Sikob		fc->retry_count++;
2346103285Sikob		goto nextnode;
2347103285Sikob	}
2348103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2349103285Sikob#if 0
2350103285Sikob	{
2351103285Sikob		u_int32_t *qld;
2352103285Sikob		int i;
2353103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
2354103285Sikob		printf("len:%d\n", xfer->recv.len);
2355103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
2356103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
2357103285Sikob			if((i % 16) == 15) printf("\n");
2358103285Sikob		}
2359103285Sikob		if((i % 16) != 15) printf("\n");
2360103285Sikob	}
2361103285Sikob#endif
2362103285Sikob	if(fc->ongodev == NULL){
2363103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
2364103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
2365103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
2366103285Sikob/* If CSR is minimul confinguration, more investgation is not needed. */
2367103285Sikob			if(chdr->info_len == 1){
2368103285Sikob				goto nextnode;
2369103285Sikob			}else{
2370103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
2371103285Sikob			}
2372103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
2373103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
2374103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
2375103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
2376103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
2377103285Sikob			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
2378103285Sikob				goto nextnode;
2379103285Sikob			fc->ongoaddr = CSRROMOFF;
2380103285Sikob		}
2381103285Sikob	}else{
2382103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
2383103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
2384103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
2385103285Sikob		}
2386103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
2387103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
2388103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
2389103285Sikob			offset = CSRROMOFF;
2390103285Sikob		}else{
2391103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
2392103285Sikob			offset = csrd->off;
2393103285Sikob		}
2394103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
2395103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
2396103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
2397103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
2398103285Sikob				if(csrd == NULL){
2399103285Sikob					goto nextnode;
2400103285Sikob				}else{
2401103285Sikob					csrd->ongoaddr = fc->ongoaddr;
2402103285Sikob					fc->ongoaddr += csrreg->val * 4;
2403103285Sikob					csrd->off = fc->ongoaddr;
2404103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
2405103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
2406103285Sikob					goto nextaddr;
2407103285Sikob				}
2408103285Sikob			}
2409103285Sikob		}
2410103285Sikob		fc->ongoaddr += 4;
2411103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
2412103285Sikob				(fc->ongodev->rommax < 0x414)){
2413103285Sikob			if(fc->ongodev->rommax <= 0x414){
2414103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
2415103285Sikob				if(csrd == NULL) goto nextnode;
2416103285Sikob				csrd->off = fc->ongoaddr;
2417103285Sikob				csrd->ongoaddr = fc->ongoaddr;
2418103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
2419103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
2420103285Sikob			}
2421103285Sikob			goto nextaddr;
2422103285Sikob		}
2423103285Sikob
2424103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
2425103285Sikob			if(csrd == NULL){
2426103285Sikob				goto nextnode;
2427103285Sikob			};
2428103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
2429103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
2430103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
2431103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
2432103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
2433103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
2434103285Sikob				offset = CSRROMOFF;
2435103285Sikob			}else{
2436103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
2437103285Sikob				offset = csrd->off;
2438103285Sikob			}
2439103285Sikob		}
2440103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
2441103285Sikob			goto nextnode;
2442103285Sikob		}
2443103285Sikob	}
2444103285Sikobnextaddr:
2445103285Sikob	fw_xfer_free( xfer);
2446103285Sikob	fw_bus_explore(fc);
2447103285Sikob	return;
2448103285Sikobnextnode:
2449103285Sikob	fw_xfer_free( xfer);
2450103285Sikob	fc->ongonode++;
2451103285Sikob/* housekeeping work space */
2452103285Sikob	fc->ongoaddr = CSRROMOFF;
2453103285Sikob	fc->ongodev = NULL;
2454103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2455103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
2456103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
2457103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
2458103285Sikob	}
2459103285Sikob	fw_bus_explore(fc);
2460103285Sikob	return;
2461103285Sikob}
2462106790Ssimokawa#if 0
2463103285Sikob/*
2464103285Sikob * Async. write responce support for kernel internal use.
2465103285Sikob */
2466106790Ssimokawaint
2467106790Ssimokawafw_writeres(struct firewire_comm *fc, u_int32_t dst, u_int32_t tlrt)
2468106790Ssimokawa{
2469103285Sikob	int err = 0;
2470103285Sikob	struct fw_xfer *xfer;
2471103285Sikob	struct fw_pkt *fp;
2472103285Sikob
2473103285Sikob	xfer = fw_xfer_alloc();
2474103285Sikob	if(xfer == NULL){
2475103285Sikob		err = ENOMEM;
2476103285Sikob		return err;
2477103285Sikob	}
2478103285Sikob	xfer->send.len = 12;
2479103285Sikob	xfer->spd = 0;
2480103285Sikob	xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT);
2481103285Sikob	if(xfer->send.buf == NULL){
2482103285Sikob		return ENOMEM;
2483103285Sikob	}
2484103285Sikob	xfer->send.off = 0;
2485103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2486103285Sikob
2487103285Sikob	fp->mode.wres.tlrt = tlrt;
2488103285Sikob	fp->mode.wres.tcode = FWTCODE_WRES;
2489103285Sikob	fp->mode.wres.pri = 0;
2490103285Sikob	fp->mode.wres.dst = htons(dst);
2491103285Sikob
2492103285Sikob	xfer->act.hand = fw_asy_callback;
2493103285Sikob	err = fw_asyreq(fc, -1, xfer);
2494103285Sikob	if(err){
2495103285Sikob		fw_xfer_free( xfer);
2496103285Sikob		return err;
2497103285Sikob	}
2498103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2499103285Sikob	fw_xfer_free( xfer);
2500103285Sikob
2501103285Sikob	return err;
2502103285Sikob}
2503106790Ssimokawa
2504103285Sikob/*
2505103285Sikob * Async. read responce block support for kernel internal use.
2506103285Sikob */
2507106790Ssimokawaint
2508106790Ssimokawafw_readresb(struct firewire_comm *fc, u_int32_t dst, u_int32_t tlrt,
2509106790Ssimokawa	u_int32_t len, u_int32_t *buf)
2510106790Ssimokawa{
2511103285Sikob	int err = 0;
2512103285Sikob	struct fw_xfer *xfer ;
2513103285Sikob	struct fw_pkt *fp;
2514103285Sikob
2515103285Sikob	xfer = fw_xfer_alloc();
2516103285Sikob	if(xfer == NULL){
2517103285Sikob		err = ENOMEM;
2518103285Sikob		return err;
2519103285Sikob	}
2520103285Sikob	xfer->send.len = sizeof(struct fw_pkt) + len;
2521103285Sikob	xfer->spd = 0;
2522103285Sikob	xfer->send.buf = malloc(sizeof(struct fw_pkt) + 1024, M_DEVBUF, M_DONTWAIT);
2523103285Sikob	if(xfer->send.buf == NULL){
2524103285Sikob		return ENOMEM;
2525103285Sikob	}
2526103285Sikob	xfer->send.off = 0;
2527103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2528103285Sikob	fp->mode.rresb.tlrt = tlrt;
2529103285Sikob	fp->mode.rresb.tcode = FWTCODE_RRESB;
2530103285Sikob	fp->mode.rresb.pri = 0;
2531103285Sikob	fp->mode.rresb.dst = htons(dst);
2532103285Sikob	fp->mode.rresb.rtcode = 0;
2533103285Sikob	fp->mode.rresb.extcode = 0;
2534103285Sikob	fp->mode.rresb.len = htons(len);
2535103285Sikob	bcopy(buf, fp->mode.rresb.payload, len);
2536103285Sikob	xfer->act.hand = fw_asy_callback;
2537103285Sikob	err = fw_asyreq(fc, -1, xfer);
2538103285Sikob	if(err){
2539103285Sikob		fw_xfer_free( xfer);
2540103285Sikob		return err;
2541103285Sikob	}
2542103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2543103285Sikob
2544103285Sikob	fw_xfer_free( xfer);
2545103285Sikob	return err;
2546103285Sikob}
2547106790Ssimokawa
2548103285Sikob/*
2549103285Sikob * Async. write request block support for kernel internal use.
2550103285Sikob */
2551106790Ssimokawaint
2552106790Ssimokawafw_writereqb(struct firewire_comm *fc, u_int32_t addr_hi, u_int32_t addr_lo,
2553106790Ssimokawa	u_int len, u_int32_t *buf)
2554106790Ssimokawa{
2555103285Sikob	int err = 0;
2556103285Sikob	struct fw_xfer *xfer ;
2557103285Sikob	struct fw_pkt *fp;
2558103285Sikob
2559103285Sikob	xfer = fw_xfer_alloc();
2560103285Sikob	if(xfer == NULL){
2561103285Sikob		err = ENOMEM;
2562103285Sikob		return err;
2563103285Sikob	}
2564103285Sikob	xfer->send.len = sizeof(struct fw_pkt) + len;
2565103285Sikob	xfer->spd = 0;
2566103285Sikob	xfer->send.buf = malloc(sizeof(struct fw_pkt) + 1024, M_DEVBUF, M_DONTWAIT);
2567103285Sikob	if(xfer->send.buf == NULL){
2568103285Sikob		return ENOMEM;
2569103285Sikob	}
2570103285Sikob	xfer->send.off = 0;
2571103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2572103285Sikob	fp->mode.wreqb.dest_hi = htonl(addr_hi & 0xffff);
2573103285Sikob	fp->mode.wreqb.tlrt = 0;
2574103285Sikob	fp->mode.wreqb.tcode = FWTCODE_WREQB;
2575103285Sikob	fp->mode.wreqb.pri = 0;
2576103285Sikob	fp->mode.wreqb.dst = htons(addr_hi >> 16);
2577103285Sikob	fp->mode.wreqb.dest_lo = htonl(addr_lo);
2578103285Sikob	fp->mode.wreqb.len = htons(len);
2579103285Sikob	fp->mode.wreqb.extcode = 0;
2580103285Sikob	bcopy(buf, fp->mode.wreqb.payload, len);
2581103285Sikob	xfer->act.hand = fw_asy_callback;
2582103285Sikob	err = fw_asyreq(fc, -1, xfer);
2583103285Sikob	if(err){
2584103285Sikob		fw_xfer_free( xfer);
2585103285Sikob		return err;
2586103285Sikob	}
2587103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2588103285Sikob
2589103285Sikob	fw_xfer_free( xfer);
2590103285Sikob	return err;
2591103285Sikob}
2592106790Ssimokawa
2593103285Sikob/*
2594103285Sikob * Async. read request support for kernel internal use.
2595103285Sikob */
2596106790Ssimokawaint
2597106790Ssimokawafw_readreqq(struct firewire_comm *fc, u_int32_t addr_hi, u_int32_t addr_lo, u_int32_t *ret){
2598103285Sikob	int err = 0;
2599103285Sikob	struct fw_xfer *xfer ;
2600103285Sikob	struct fw_pkt *fp, *rfp;
2601103285Sikob
2602103285Sikob	xfer = fw_xfer_alloc();
2603103285Sikob	if(xfer == NULL){
2604103285Sikob		err = ENOMEM;
2605103285Sikob		return err;
2606103285Sikob	}
2607103285Sikob	xfer->send.len = 16;
2608103285Sikob	xfer->spd = 0;
2609103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2610103285Sikob	if(xfer->send.buf == NULL){
2611103285Sikob		return ENOMEM;
2612103285Sikob	}
2613103285Sikob	xfer->send.off = 0;
2614103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2615103285Sikob	fp->mode.rreqq.dest_hi = htonl(addr_hi & 0xffff);
2616103285Sikob	fp->mode.rreqq.tlrt = 0;
2617103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2618103285Sikob	fp->mode.rreqq.pri = 0;
2619103285Sikob	xfer->dst = addr_hi >> 16;
2620103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2621103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
2622103285Sikob	xfer->act.hand = fw_asy_callback;
2623103285Sikob	err = fw_asyreq(fc, -1, xfer);
2624103285Sikob	if(err){
2625103285Sikob		fw_xfer_free( xfer);
2626103285Sikob		return err;
2627103285Sikob	}
2628103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2629103285Sikob
2630103285Sikob	if(err == 0 && xfer->recv.buf != NULL){
2631103285Sikob		rfp = (struct fw_pkt *)xfer->recv.buf;
2632103285Sikob		*ret = ntohl(rfp->mode.rresq.data);
2633103285Sikob	}
2634103285Sikob	fw_xfer_free( xfer);
2635103285Sikob	return err;
2636103285Sikob}
2637106790Ssimokawa#endif
2638103285Sikob/*
2639103285Sikob * To obtain CSR register values.
2640103285Sikob */
2641103285Sikobu_int32_t getcsrdata(struct fw_device *fwdev, u_int8_t key)
2642103285Sikob{
2643103285Sikob	int i;
2644103285Sikob	struct csrhdr *chdr;
2645103285Sikob	struct csrreg *creg;
2646103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
2647103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
2648103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
2649103285Sikob		if(creg->key == key){
2650103285Sikob			return (u_int32_t)creg->val;
2651103285Sikob		}
2652103285Sikob	}
2653103285Sikob	return 0;
2654103285Sikob}
2655103285Sikob/*
2656103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
2657103285Sikob */
2658103285Sikobstatic void fw_attach_dev(struct firewire_comm *fc)
2659103285Sikob{
2660103285Sikob	struct fw_device *fwdev;
2661103285Sikob	struct fw_xfer *xfer;
2662103285Sikob	int i, err;
2663103285Sikob	device_t *devlistp;
2664103285Sikob	int devcnt;
2665103285Sikob	struct firewire_dev_comm *fdc;
2666103285Sikob
2667103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
2668103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
2669103285Sikob		if(fwdev->status == FWDEVINIT){
2670103285Sikob			fwdev->spec = getcsrdata(fwdev, CSRKEY_SPEC);
2671103285Sikob			if(fwdev->spec == 0)
2672103285Sikob				continue;
2673103285Sikob			fwdev->ver = getcsrdata(fwdev, CSRKEY_VER);
2674103285Sikob			if(fwdev->ver == 0)
2675103285Sikob				continue;
2676103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
2677103285Sikob
2678103285Sikob			switch(fwdev->spec){
2679103285Sikob			case CSRVAL_ANSIT10:
2680103285Sikob				switch(fwdev->ver){
2681103285Sikob				case CSRVAL_T10SBP2:
2682103285Sikob					printf("Device SBP-II");
2683103285Sikob					break;
2684103285Sikob				default:
2685103285Sikob					break;
2686103285Sikob				}
2687103285Sikob				break;
2688103285Sikob			case CSRVAL_1394TA:
2689103285Sikob				switch(fwdev->ver){
2690103285Sikob				case CSR_PROTAVC:
2691103285Sikob					printf("Device AV/C");
2692103285Sikob					break;
2693103285Sikob				case CSR_PROTCAL:
2694103285Sikob					printf("Device CAL");
2695103285Sikob					break;
2696103285Sikob				case CSR_PROTEHS:
2697103285Sikob					printf("Device EHS");
2698103285Sikob					break;
2699103285Sikob				case CSR_PROTHAVI:
2700103285Sikob					printf("Device HAVi");
2701103285Sikob					break;
2702103285Sikob				case CSR_PROTCAM104:
2703103285Sikob					printf("Device 1394 Cam 1.04");
2704103285Sikob					break;
2705103285Sikob				case CSR_PROTCAM120:
2706103285Sikob					printf("Device 1394 Cam 1.20");
2707103285Sikob					break;
2708103285Sikob				case CSR_PROTCAM130:
2709103285Sikob					printf("Device 1394 Cam 1.30");
2710103285Sikob					break;
2711103285Sikob				case CSR_PROTDPP:
2712103285Sikob					printf("Device 1394 Direct print");
2713103285Sikob					break;
2714103285Sikob				case CSR_PROTIICP:
2715103285Sikob					printf("Device Industrial & Instrument");
2716103285Sikob					break;
2717103285Sikob				default:
2718103285Sikob					printf("Device unkwon 1394TA");
2719103285Sikob					break;
2720103285Sikob				}
2721103285Sikob				break;
2722103285Sikob			default:
2723103285Sikob				break;
2724103285Sikob			}
2725103285Sikob			fwdev->status = FWDEVATTACHED;
2726103285Sikob			printf("\n");
2727103285Sikob		}
2728103285Sikob	}
2729103285Sikob	err = device_get_children(fc->dev, &devlistp, &devcnt);
2730103285Sikob	if( err != 0 )
2731103285Sikob		return;
2732103285Sikob	for( i = 0 ; i < devcnt ; i++){
2733103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
2734103285Sikob			fdc = device_get_softc(devlistp[i]);
2735103285Sikob			if (fdc->post_explore != NULL)
2736103285Sikob				fdc->post_explore(fdc);
2737103285Sikob		}
2738103285Sikob	}
2739103285Sikob	free(devlistp, M_TEMP);
2740103285Sikob
2741103285Sikob	/* call pending handlers */
2742103285Sikob	i = 0;
2743103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
2744103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
2745103285Sikob		i++;
2746103285Sikob		if (xfer->act.hand)
2747103285Sikob			xfer->act.hand(xfer);
2748103285Sikob	}
2749103285Sikob	if (i > 0)
2750103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
2751103285Sikob	if (fc->retry_count > 0) {
2752103285Sikob		printf("retry_count = %d\n", fc->retry_count);
2753103285Sikob		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
2754103285Sikob							(void *)fc, hz*2);
2755103285Sikob	}
2756103285Sikob	return;
2757103285Sikob}
2758103285Sikob/*
2759103285Sikob * To allocate uniq transaction label.
2760103285Sikob */
2761103285Sikobstatic int fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
2762103285Sikob{
2763103285Sikob	u_int i;
2764103285Sikob	struct tlabel *tl, *tmptl;
2765103285Sikob	int s;
2766103285Sikob	static u_int32_t label = 0;
2767103285Sikob
2768103285Sikob	s = splfw();
2769103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
2770103285Sikob		label = (label + 1) & 0x3f;
2771103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
2772103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
2773103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
2774103285Sikob		}
2775103285Sikob		if(tmptl == NULL) {
2776103285Sikob			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT);
2777103285Sikob			if (tl == NULL) {
2778103285Sikob				splx(s);
2779103285Sikob				return (-1);
2780103285Sikob			}
2781103285Sikob			tl->xfer = xfer;
2782103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
2783103285Sikob			splx(s);
2784103285Sikob			return(label);
2785103285Sikob		}
2786103285Sikob	}
2787103285Sikob	splx(s);
2788103285Sikob
2789103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
2790103285Sikob	return(-1);
2791103285Sikob}
2792103285Sikob/*
2793103285Sikob * Generic packet receving process.
2794103285Sikob */
2795103285Sikobvoid fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
2796103285Sikob{
2797103285Sikob	struct fw_pkt *fp, *resfp;
2798103285Sikob	struct fw_xfer *xfer;
2799103285Sikob	struct fw_bind *bind;
2800103285Sikob	struct firewire_softc *sc;
2801103285Sikob	int s;
2802103285Sikob#if 0
2803103285Sikob	{
2804103285Sikob		u_int32_t *qld;
2805103285Sikob		int i;
2806103285Sikob		qld = (u_int32_t *)buf;
2807103285Sikob		printf("spd %d len:%d\n", spd, len);
2808103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
2809103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
2810103285Sikob			if((i % 16) == 15) printf("\n");
2811103285Sikob		}
2812103285Sikob		if((i % 16) != 15) printf("\n");
2813103285Sikob	}
2814103285Sikob#endif
2815103285Sikob	fp = (struct fw_pkt *)(buf + off);
2816103285Sikob	switch(fp->mode.common.tcode){
2817103285Sikob	case FWTCODE_WRES:
2818103285Sikob	case FWTCODE_RRESQ:
2819103285Sikob	case FWTCODE_RRESB:
2820103285Sikob	case FWTCODE_LRES:
2821103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
2822103285Sikob					fp->mode.hdr.tlrt >> 2);
2823103285Sikob		if(xfer == NULL) {
2824103285Sikob			printf("fw_rcv: unknown response "
2825103285Sikob					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
2826103285Sikob					fp->mode.common.tcode,
2827103285Sikob					ntohs(fp->mode.hdr.src),
2828103285Sikob					fp->mode.hdr.tlrt >> 2,
2829103285Sikob					fp->mode.hdr.tlrt & 3,
2830103285Sikob					fp->mode.rresq.data);
2831103285Sikob#if 1
2832103285Sikob			printf("try ad-hoc work around!!\n");
2833103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
2834103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
2835103285Sikob			if (xfer == NULL) {
2836103285Sikob				printf("no use...\n");
2837103285Sikob				goto err;
2838103285Sikob			}
2839103285Sikob#else
2840103285Sikob			goto err;
2841103285Sikob#endif
2842103285Sikob		}
2843103285Sikob		switch(xfer->act_type){
2844103285Sikob		case FWACT_XFER:
2845103285Sikob			if((xfer->sub >= 0) &&
2846103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
2847103285Sikob				xfer->resp = EINVAL;
2848103285Sikob				fw_xfer_done(xfer);
2849103285Sikob				goto err;
2850103285Sikob			}
2851103285Sikob			xfer->recv.len = len;
2852103285Sikob			xfer->recv.off = off;
2853103285Sikob			xfer->recv.buf = buf;
2854103285Sikob			xfer->resp = 0;
2855103285Sikob			fw_xfer_done(xfer);
2856103285Sikob			return;
2857103285Sikob			break;
2858103285Sikob		case FWACT_CH:
2859103285Sikob		default:
2860103285Sikob			goto err;
2861103285Sikob			break;
2862103285Sikob		}
2863103285Sikob		break;
2864103285Sikob	case FWTCODE_WREQQ:
2865103285Sikob	case FWTCODE_WREQB:
2866103285Sikob	case FWTCODE_RREQQ:
2867103285Sikob	case FWTCODE_RREQB:
2868103285Sikob	case FWTCODE_LREQ:
2869103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
2870103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
2871103285Sikob		if(bind == NULL){
2872103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
2873103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
2874103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
2875103285Sikob				fp->mode.common.tcode);
2876103285Sikob			if (fc->status == FWBUSRESET) {
2877103285Sikob				printf("fw_rcv: cannot response(bus reset)!\n");
2878103285Sikob				goto err;
2879103285Sikob			}
2880103285Sikob			xfer = fw_xfer_alloc();
2881103285Sikob			if(xfer == NULL){
2882103285Sikob				return;
2883103285Sikob			}
2884103285Sikob			xfer->spd = spd;
2885103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2886103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
2887103285Sikob			switch(fp->mode.common.tcode){
2888103285Sikob			case FWTCODE_WREQQ:
2889103285Sikob			case FWTCODE_WREQB:
2890103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
2891103285Sikob				xfer->send.len = 12;
2892103285Sikob				break;
2893103285Sikob			case FWTCODE_RREQQ:
2894103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
2895103285Sikob				xfer->send.len = 16;
2896103285Sikob				break;
2897103285Sikob			case FWTCODE_RREQB:
2898103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
2899103285Sikob				xfer->send.len = 16;
2900103285Sikob				break;
2901103285Sikob			case FWTCODE_LREQ:
2902103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
2903103285Sikob				xfer->send.len = 16;
2904103285Sikob				break;
2905103285Sikob			}
2906103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
2907103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
2908103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
2909103285Sikob			resfp->mode.rresb.rtcode = 7;
2910103285Sikob			resfp->mode.rresb.extcode = 0;
2911103285Sikob			resfp->mode.rresb.len = 0;
2912103285Sikob/*
2913103285Sikob			xfer->act.hand = fw_asy_callback;
2914103285Sikob*/
2915103285Sikob			xfer->act.hand = fw_xfer_free;
2916103285Sikob			if(fw_asyreq(fc, -1, xfer)){
2917103285Sikob				fw_xfer_free( xfer);
2918103285Sikob				return;
2919103285Sikob			}
2920103285Sikob			goto err;
2921103285Sikob		}
2922103285Sikob		switch(bind->xfer->act_type){
2923103285Sikob		case FWACT_XFER:
2924103285Sikob			xfer = fw_xfer_alloc();
2925103285Sikob			if(xfer == NULL) goto err;
2926103285Sikob			xfer->fc = bind->xfer->fc;
2927103285Sikob			xfer->sc = bind->xfer->sc;
2928103285Sikob			xfer->recv.buf = buf;
2929103285Sikob			xfer->recv.len = len;
2930103285Sikob			xfer->recv.off = off;
2931103285Sikob			xfer->spd = spd;
2932103285Sikob			xfer->act.hand = bind->xfer->act.hand;
2933103285Sikob			if (fc->status != FWBUSRESET)
2934103285Sikob				xfer->act.hand(xfer);
2935103285Sikob			else
2936103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
2937103285Sikob			return;
2938103285Sikob			break;
2939103285Sikob		case FWACT_CH:
2940103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
2941103285Sikob				fc->ir[bind->xfer->sub]->maxq){
2942103285Sikob				printf("%s:Discard a packet %x %d\n",
2943103285Sikob					device_get_nameunit(fc->dev),
2944103285Sikob					bind->xfer->sub,
2945103285Sikob					fc->ir[bind->xfer->sub]->queued);
2946103285Sikob				goto err;
2947103285Sikob			}
2948103285Sikob			xfer = fw_xfer_alloc();
2949103285Sikob			if(xfer == NULL) goto err;
2950103285Sikob			xfer->recv.buf = buf;
2951103285Sikob			xfer->recv.len = len;
2952103285Sikob			xfer->recv.off = off;
2953103285Sikob			xfer->spd = spd;
2954103285Sikob			s = splfw();
2955103285Sikob			fc->ir[bind->xfer->sub]->queued++;
2956103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
2957103285Sikob			splx(s);
2958103285Sikob
2959103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
2960103285Sikob
2961103285Sikob			return;
2962103285Sikob			break;
2963103285Sikob		default:
2964103285Sikob			goto err;
2965103285Sikob			break;
2966103285Sikob		}
2967103285Sikob		break;
2968103285Sikob	case FWTCODE_STREAM:
2969103285Sikob	{
2970103285Sikob		struct fw_xferq *xferq;
2971103285Sikob
2972103285Sikob		xferq = fc->ir[sub];
2973103285Sikob#if 0
2974103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
2975103285Sikob			sub, len, off, spd);
2976103285Sikob#endif
2977103285Sikob		if(xferq->queued >= xferq->maxq) {
2978103285Sikob			printf("receive queue is full\n");
2979103285Sikob			goto err;
2980103285Sikob		}
2981103285Sikob		xfer = fw_xfer_alloc();
2982103285Sikob		if(xfer == NULL) goto err;
2983103285Sikob		xfer->recv.buf = buf;
2984103285Sikob		xfer->recv.len = len;
2985103285Sikob		xfer->recv.off = off;
2986103285Sikob		xfer->spd = spd;
2987103285Sikob		s = splfw();
2988103285Sikob		xferq->queued++;
2989103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
2990103285Sikob		splx(s);
2991103285Sikob		sc = device_get_softc(fc->bdev);
2992103285Sikob#if __FreeBSD_version >= 500000
2993103285Sikob		if (SEL_WAITING(&xferq->rsel))
2994103285Sikob#else
2995103285Sikob		if (&xferq->rsel.si_pid != 0)
2996103285Sikob#endif
2997103285Sikob			selwakeup(&xferq->rsel);
2998103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
2999103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
3000103285Sikob			wakeup((caddr_t)xferq);
3001103285Sikob		}
3002103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
3003103285Sikob			xferq->hand(xferq);
3004103285Sikob		}
3005103285Sikob		return;
3006103285Sikob		break;
3007103285Sikob	}
3008103285Sikob	default:
3009103285Sikob		printf("fw_rcv: unknow tcode\n");
3010103285Sikob		break;
3011103285Sikob	}
3012103285Sikoberr:
3013103285Sikob	free(buf, M_DEVBUF);
3014103285Sikob}
3015103285Sikob/*
3016103285Sikob * Post process for Bus Manager election process.
3017103285Sikob */
3018103285Sikobstatic void
3019103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
3020103285Sikob{
3021103285Sikob	struct fw_pkt *sfp,*rfp;
3022103285Sikob	struct firewire_comm *fc;
3023103285Sikob
3024103285Sikob	if(xfer == NULL) return;
3025103285Sikob	fc = xfer->fc;
3026103285Sikob	if(xfer->resp != 0){
3027103285Sikob		goto error;
3028103285Sikob	}
3029103285Sikob
3030103285Sikob	if(xfer->send.buf == NULL){
3031103285Sikob		goto error;
3032103285Sikob	}
3033103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
3034103285Sikob
3035103285Sikob	if(xfer->recv.buf == NULL){
3036103285Sikob		goto error;
3037103285Sikob	}
3038103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
3039103285Sikob	CSRARC(fc, BUS_MGR_ID)
3040103285Sikob		= fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f);
3041103285Sikob	printf("%s: new bus manager %d ",
3042103285Sikob		device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
3043103285Sikob	if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){
3044103285Sikob		printf("(me)\n");
3045103285Sikob/* If I am bus manager, optimize gapcount */
3046103285Sikob		if(fc->max_hop <= MAX_GAPHOP ){
3047103285Sikob			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
3048103285Sikob		}
3049103285Sikob	}else{
3050103285Sikob		printf("\n");
3051103285Sikob	}
3052103285Sikoberror:
3053103285Sikob	fw_xfer_free(xfer);
3054103285Sikob}
3055103285Sikob/*
3056103285Sikob * To candidate Bus Manager election process.
3057103285Sikob */
3058103285Sikobvoid fw_try_bmr(void *arg)
3059103285Sikob{
3060103285Sikob	struct fw_xfer *xfer;
3061103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
3062103285Sikob	struct fw_pkt *fp;
3063103285Sikob	int err = 0;
3064103285Sikob
3065103285Sikob	xfer = fw_xfer_alloc();
3066103285Sikob	if(xfer == NULL){
3067103285Sikob		return;
3068103285Sikob	}
3069103285Sikob	xfer->send.len = 24;
3070103285Sikob	xfer->spd = 0;
3071103285Sikob	xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT);
3072103285Sikob	if(xfer->send.buf == NULL){
3073103285Sikob		fw_xfer_free( xfer);
3074103285Sikob		return;
3075103285Sikob	}
3076103285Sikob
3077103285Sikob	fc->status = FWBUSMGRELECT;
3078103285Sikob
3079103285Sikob	xfer->send.off = 0;
3080103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
3081103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
3082103285Sikob	fp->mode.lreq.tlrt = 0;
3083103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
3084103285Sikob	fp->mode.lreq.pri = 0;
3085103285Sikob	fp->mode.lreq.src = 0;
3086103285Sikob	fp->mode.lreq.len = htons(8);
3087103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
3088103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
3089103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
3090103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
3091103285Sikob	fp->mode.lreq.payload[0] = 0x3f;
3092103285Sikob	fp->mode.lreq.payload[1] = fc->nodeid;
3093103285Sikob	xfer->act_type = FWACT_XFER;
3094103285Sikob	xfer->act.hand = fw_try_bmr_callback;
3095103285Sikob
3096103285Sikob	err = fw_asyreq(fc, -1, xfer);
3097103285Sikob	if(err){
3098103285Sikob		fw_xfer_free( xfer);
3099103285Sikob		return;
3100103285Sikob	}
3101103285Sikob	return;
3102103285Sikob}
3103106543Ssimokawa
3104106543Ssimokawa#ifdef FW_VMACCESS
3105103285Sikob/*
3106103285Sikob * Software implementation for physical memory block access.
3107103285Sikob * XXX:Too slow, usef for debug purpose only.
3108103285Sikob */
3109103285Sikobstatic void fw_vmaccess(struct fw_xfer *xfer){
3110103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
3111103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
3112103285Sikob
3113103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
3114103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
3115103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
3116103285Sikob	if(xfer->resp != 0){
3117103285Sikob		fw_xfer_free( xfer);
3118103285Sikob		return;
3119103285Sikob	}
3120103285Sikob	if(xfer->recv.buf == NULL){
3121103285Sikob		fw_xfer_free( xfer);
3122103285Sikob		return;
3123103285Sikob	}
3124103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
3125103285Sikob	switch(rfp->mode.hdr.tcode){
3126103285Sikob		/* XXX need fix for 64bit arch */
3127103285Sikob		case FWTCODE_WREQB:
3128103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
3129103285Sikob			xfer->send.len = 12;
3130103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3131103285Sikob			bcopy(rfp->mode.wreqb.payload,
3132103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
3133103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
3134103285Sikob			sfp->mode.wres.rtcode = 0;
3135103285Sikob			break;
3136103285Sikob		case FWTCODE_WREQQ:
3137103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
3138103285Sikob			xfer->send.len = 12;
3139103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
3140103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
3141103285Sikob			sfp->mode.wres.rtcode = 0;
3142103285Sikob			break;
3143103285Sikob		case FWTCODE_RREQB:
3144103285Sikob			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
3145103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
3146103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3147103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
3148103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
3149103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
3150103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
3151103285Sikob			sfp->mode.rresb.rtcode = 0;
3152103285Sikob			sfp->mode.rresb.extcode = 0;
3153103285Sikob			break;
3154103285Sikob		case FWTCODE_RREQQ:
3155103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
3156103285Sikob			xfer->send.len = 16;
3157103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3158103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
3159103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
3160103285Sikob			sfp->mode.rresb.rtcode = 0;
3161103285Sikob			break;
3162103285Sikob		default:
3163103285Sikob			fw_xfer_free( xfer);
3164103285Sikob			return;
3165103285Sikob	}
3166103285Sikob	xfer->send.off = 0;
3167103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
3168103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
3169103285Sikob	xfer->act.hand = fw_xfer_free;
3170103285Sikob	xfer->retry_req = fw_asybusy;
3171103285Sikob
3172103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
3173103285Sikob	sfp->mode.hdr.pri = 0;
3174103285Sikob
3175103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
3176103285Sikob/**/
3177103285Sikob	return;
3178103285Sikob}
3179106543Ssimokawa#endif
3180106543Ssimokawa
3181103285Sikob/*
3182103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
3183103285Sikob */
3184103285Sikobu_int16_t fw_crc16(u_int32_t *ptr, u_int32_t len){
3185103285Sikob	u_int32_t i, sum, crc = 0;
3186103285Sikob	int shift;
3187103285Sikob	len = (len + 3) & ~3;
3188103285Sikob	for(i = 0 ; i < len ; i+= 4){
3189103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
3190103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
3191103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
3192103285Sikob		}
3193103285Sikob		crc &= 0xffff;
3194103285Sikob	}
3195103285Sikob	return((u_int16_t) crc);
3196103285Sikob}
3197103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
3198103285SikobMODULE_VERSION(firewire, 1);
3199