2 * Copyright (c) 2002-2003
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/bus/firewire/fwmem.c,v 1.4 2004/02/05 13:32:08 joerg Exp $
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/sys/dev/firewire/fwmem.c,v 1.26 2004/01/05 14:21:18 simokawa Exp $");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #if __FreeBSD_version < 500000
57 #include <machine/bus.h>
59 #include <sys/signal.h>
61 #include <sys/ioccom.h>
62 #include <sys/fcntl.h>
66 #include "firewirereg.h"
69 #include <dev/firewire/firewire.h>
70 #include <dev/firewire/firewirereg.h>
71 #include <dev/firewire/fwmem.h>
74 static int fwmem_speed=2, fwmem_debug=0;
75 static struct fw_eui64 fwmem_eui64;
76 SYSCTL_DECL(_hw_firewire);
77 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem, CTLFLAG_RD, 0,
78 "FireWire Memory Access");
79 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
80 &fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
81 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
82 &fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
83 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
85 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
86 "Fwmem driver debug flag");
88 MALLOC_DEFINE(M_FWMEM, "fwmem", "fwmem/FireWire");
90 #define MAXLEN (512 << fwmem_speed)
97 static struct fw_xfer *
99 struct fw_device *fwdev,
106 struct fw_xfer *xfer;
108 xfer = fw_xfer_alloc(M_FWMEM);
112 xfer->fc = fwdev->fc;
113 xfer->send.hdr.mode.hdr.dst = FWLOCALBUS | fwdev->dst;
115 xfer->send.spd = fwdev->speed;
117 xfer->send.spd = min(spd, fwdev->speed);
118 xfer->act.hand = hand;
119 xfer->retry_req = fw_asybusy;
121 xfer->send.pay_len = slen;
122 xfer->recv.pay_len = rlen;
129 struct fw_device *fwdev,
135 void (*hand)(struct fw_xfer *))
137 struct fw_xfer *xfer;
140 xfer = fwmem_xfer_req(fwdev, (void *)sc, spd, 0, 4, hand);
145 fp = &xfer->send.hdr;
146 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
147 fp->mode.rreqq.dest_hi = dst_hi;
148 fp->mode.rreqq.dest_lo = dst_lo;
150 xfer->send.payload = NULL;
151 xfer->recv.payload = (u_int32_t *)data;
154 printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
157 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
166 struct fw_device *fwdev,
172 void (*hand)(struct fw_xfer *))
174 struct fw_xfer *xfer;
177 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, 0, hand);
181 fp = &xfer->send.hdr;
182 fp->mode.wreqq.tcode = FWTCODE_WREQQ;
183 fp->mode.wreqq.dest_hi = dst_hi;
184 fp->mode.wreqq.dest_lo = dst_lo;
185 fp->mode.wreqq.data = *(u_int32_t *)data;
187 xfer->send.payload = xfer->recv.payload = NULL;
190 printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
191 dst_hi, dst_lo, *(u_int32_t *)data);
193 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
202 struct fw_device *fwdev,
209 void (*hand)(struct fw_xfer *))
211 struct fw_xfer *xfer;
214 xfer = fwmem_xfer_req(fwdev, sc, spd, 0, roundup2(len, 4), hand);
218 fp = &xfer->send.hdr;
219 fp->mode.rreqb.tcode = FWTCODE_RREQB;
220 fp->mode.rreqb.dest_hi = dst_hi;
221 fp->mode.rreqb.dest_lo = dst_lo;
222 fp->mode.rreqb.len = len;
223 fp->mode.rreqb.extcode = 0;
225 xfer->send.payload = NULL;
226 xfer->recv.payload = data;
229 printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
230 dst_hi, dst_lo, len);
231 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
240 struct fw_device *fwdev,
247 void (*hand)(struct fw_xfer *))
249 struct fw_xfer *xfer;
252 xfer = fwmem_xfer_req(fwdev, sc, spd, len, 0, hand);
256 fp = &xfer->send.hdr;
257 fp->mode.wreqb.tcode = FWTCODE_WREQB;
258 fp->mode.wreqb.dest_hi = dst_hi;
259 fp->mode.wreqb.dest_lo = dst_lo;
260 fp->mode.wreqb.len = len;
261 fp->mode.wreqb.extcode = 0;
263 xfer->send.payload = data;
264 xfer->recv.payload = NULL;
267 printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
268 dst_hi, dst_lo, len);
269 if (fw_asyreq(xfer->fc, -1, xfer) == 0)
278 fwmem_open (dev_t dev, int flags, int fmt, fw_proc *td)
280 struct fwmem_softc *fms;
282 if (dev->si_drv1 != NULL) {
283 if ((flags & FWRITE) != 0)
285 fms = (struct fwmem_softc *)dev->si_drv1;
288 fms = (struct fwmem_softc *)malloc(sizeof(struct fwmem_softc),
292 bcopy(&fwmem_eui64, &fms->eui, sizeof(struct fw_eui64));
293 dev->si_drv1 = (void *)fms;
294 dev->si_iosize_max = DFLTPHYS;
298 printf("%s: refcount=%d\n", __FUNCTION__, fms->refcount);
304 fwmem_close (dev_t dev, int flags, int fmt, fw_proc *td)
306 struct fwmem_softc *fms;
308 fms = (struct fwmem_softc *)dev->si_drv1;
311 printf("%s: refcount=%d\n", __FUNCTION__, fms->refcount);
312 if (fms->refcount < 1) {
313 free(dev->si_drv1, M_FW);
322 fwmem_biodone(struct fw_xfer *xfer)
326 bp = (struct bio *)xfer->sc;
327 bp->bio_error = xfer->resp;
329 if (bp->bio_error != 0) {
331 printf("%s: err=%d\n", __FUNCTION__, bp->bio_error);
332 bp->bio_flags |= BIO_ERROR;
333 bp->bio_resid = bp->bio_bcount;
341 fwmem_strategy(struct bio *bp)
343 struct firewire_softc *sc;
344 struct fwmem_softc *fms;
345 struct fw_device *fwdev;
346 struct fw_xfer *xfer;
348 int unit, err=0, s, iolen;
351 /* XXX check request length */
353 unit = DEV2UNIT(dev);
354 sc = devclass_get_softc(firewire_devclass, unit);
357 fms = (struct fwmem_softc *)dev->si_drv1;
358 fwdev = fw_noderesolve_eui64(sc->fc, &fms->eui);
361 printf("fwmem: no such device ID:%08x%08x\n",
362 fms->eui.hi, fms->eui.lo);
367 iolen = MIN(bp->bio_bcount, MAXLEN);
368 if ((bp->bio_cmd & BIO_READ) == BIO_READ) {
369 if (iolen == 4 && (bp->bio_offset & 3) == 0)
370 xfer = fwmem_read_quad(fwdev,
371 (void *) bp, fwmem_speed,
372 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
373 bp->bio_data, fwmem_biodone);
375 xfer = fwmem_read_block(fwdev,
376 (void *) bp, fwmem_speed,
377 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
378 iolen, bp->bio_data, fwmem_biodone);
380 if (iolen == 4 && (bp->bio_offset & 3) == 0)
381 xfer = fwmem_write_quad(fwdev,
382 (void *)bp, fwmem_speed,
383 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
384 bp->bio_data, fwmem_biodone);
386 xfer = fwmem_write_block(fwdev,
387 (void *)bp, fwmem_speed,
388 bp->bio_offset >> 32, bp->bio_offset & 0xffffffff,
389 iolen, bp->bio_data, fwmem_biodone);
396 bp->bio_resid = bp->bio_bcount - iolen;
401 printf("%s: err=%d\n", __FUNCTION__, err);
403 bp->bio_flags |= BIO_ERROR;
404 bp->bio_resid = bp->bio_bcount;
410 fwmem_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
412 struct fwmem_softc *fms;
415 fms = (struct fwmem_softc *)dev->si_drv1;
418 bcopy(data, &fms->eui, sizeof(struct fw_eui64));
421 bcopy(&fms->eui, data, sizeof(struct fw_eui64));
429 fwmem_poll (dev_t dev, int events, fw_proc *td)
434 #if __FreeBSD_version < 500102
435 fwmem_mmap (dev_t dev, vm_offset_t offset, int nproto)
437 fwmem_mmap (dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nproto)