xref: /dragonfly/sys/bus/firewire/fwmem.c (revision 4e7eb5cc)
1 /*
2  * Copyright (c) 2002-2003
3  * 	Hidetoshi Shimokawa. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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:
15  *
16  *	This product includes software developed by Hidetoshi Shimokawa.
17  *
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/dev/firewire/fwmem.c,v 1.1.2.9 2003/04/28 03:29:18 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/fwmem.c,v 1.3 2003/08/07 21:16:45 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
41 
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/conf.h>
45 #include <sys/sysctl.h>
46 
47 #include <sys/bus.h>
48 #include <machine/bus.h>
49 
50 #include <sys/signal.h>
51 #include <sys/mman.h>
52 #include <sys/ioccom.h>
53 
54 #include "firewire.h"
55 #include "firewirereg.h"
56 #include "fwmem.h"
57 
58 static int fwmem_speed=2, fwmem_debug=0;
59 static struct fw_eui64 fwmem_eui64;
60 SYSCTL_DECL(_hw_firewire);
61 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwmem, CTLFLAG_RD, 0,
62 	"FireWire Memory Access");
63 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_hi, CTLFLAG_RW,
64 	&fwmem_eui64.hi, 0, "Fwmem target EUI64 high");
65 SYSCTL_UINT(_hw_firewire_fwmem, OID_AUTO, eui64_lo, CTLFLAG_RW,
66 	&fwmem_eui64.lo, 0, "Fwmem target EUI64 low");
67 SYSCTL_INT(_hw_firewire_fwmem, OID_AUTO, speed, CTLFLAG_RW, &fwmem_speed, 0,
68 	"Fwmem link speed");
69 SYSCTL_INT(_debug, OID_AUTO, fwmem_debug, CTLFLAG_RW, &fwmem_debug, 0,
70 	"Fwmem driver debug flag");
71 
72 static struct fw_xfer *
73 fwmem_xfer_req(
74 	struct fw_device *fwdev,
75 	caddr_t sc,
76 	int spd,
77 	int slen,
78 	int rlen,
79 	void *hand)
80 {
81 	struct fw_xfer *xfer;
82 
83 	xfer = fw_xfer_alloc_buf(M_FWXFER, slen, rlen);
84 	if (xfer == NULL)
85 		return NULL;
86 
87 	xfer->fc = fwdev->fc;
88 	xfer->dst = FWLOCALBUS | fwdev->dst;
89 	if (spd < 0)
90 		xfer->spd = fwdev->speed;
91 	else
92 		xfer->spd = min(spd, fwdev->speed);
93 	xfer->act.hand = hand;
94 	xfer->retry_req = fw_asybusy;
95 	xfer->sc = sc;
96 
97 	return xfer;
98 }
99 
100 struct fw_xfer *
101 fwmem_read_quad(
102 	struct fw_device *fwdev,
103 	caddr_t	sc,
104 	u_int8_t spd,
105 	u_int16_t dst_hi,
106 	u_int32_t dst_lo,
107 	void (*hand)(struct fw_xfer *))
108 {
109 	struct fw_xfer *xfer;
110 	struct fw_pkt *fp;
111 
112 	xfer = fwmem_xfer_req(fwdev, sc, spd, 12, 16, hand);
113 	if (xfer == NULL)
114 		return NULL;
115 
116 	fp = (struct fw_pkt *)xfer->send.buf;
117 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
118 	fp->mode.rreqq.dst = xfer->dst;
119 	fp->mode.rreqq.dest_hi = dst_hi;
120 	fp->mode.rreqq.dest_lo = dst_lo;
121 
122 	if (fwmem_debug)
123 		printf("fwmem_read_quad: %d %04x:%08x\n", fwdev->dst,
124 				dst_hi, dst_lo);
125 
126 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
127 		return xfer;
128 
129 	fw_xfer_free(xfer);
130 	return NULL;
131 }
132 
133 struct fw_xfer *
134 fwmem_write_quad(
135 	struct fw_device *fwdev,
136 	caddr_t	sc,
137 	u_int8_t spd,
138 	u_int16_t dst_hi,
139 	u_int32_t dst_lo,
140 	u_int32_t data,
141 	void (*hand)(struct fw_xfer *))
142 {
143 	struct fw_xfer *xfer;
144 	struct fw_pkt *fp;
145 
146 	xfer = fwmem_xfer_req(fwdev, sc, spd, 16, 12, hand);
147 	if (xfer == NULL)
148 		return NULL;
149 
150 	fp = (struct fw_pkt *)xfer->send.buf;
151 	fp->mode.wreqq.tcode = FWTCODE_WREQQ;
152 	fp->mode.wreqq.dst = xfer->dst;
153 	fp->mode.wreqq.dest_hi = dst_hi;
154 	fp->mode.wreqq.dest_lo = dst_lo;
155 
156 	fp->mode.wreqq.data = data;
157 
158 	if (fwmem_debug)
159 		printf("fwmem_write_quad: %d %04x:%08x %08x\n", fwdev->dst,
160 			dst_hi, dst_lo, data);
161 
162 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
163 		return xfer;
164 
165 	fw_xfer_free(xfer);
166 	return NULL;
167 }
168 
169 struct fw_xfer *
170 fwmem_read_block(
171 	struct fw_device *fwdev,
172 	caddr_t	sc,
173 	u_int8_t spd,
174 	u_int16_t dst_hi,
175 	u_int32_t dst_lo,
176 	int len,
177 	void (*hand)(struct fw_xfer *))
178 {
179 	struct fw_xfer *xfer;
180 	struct fw_pkt *fp;
181 
182 	xfer = fwmem_xfer_req(fwdev, sc, spd, 16, roundup2(16+len,4), hand);
183 	if (xfer == NULL)
184 		return NULL;
185 
186 	fp = (struct fw_pkt *)xfer->send.buf;
187 	fp->mode.rreqb.tcode = FWTCODE_RREQB;
188 	fp->mode.rreqb.dst = xfer->dst;
189 	fp->mode.rreqb.dest_hi = dst_hi;
190 	fp->mode.rreqb.dest_lo = dst_lo;
191 	fp->mode.rreqb.len = len;
192 
193 	if (fwmem_debug)
194 		printf("fwmem_read_block: %d %04x:%08x %d\n", fwdev->dst,
195 				dst_hi, dst_lo, len);
196 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
197 		return xfer;
198 
199 	fw_xfer_free(xfer);
200 	return NULL;
201 }
202 
203 struct fw_xfer *
204 fwmem_write_block(
205 	struct fw_device *fwdev,
206 	caddr_t	sc,
207 	u_int8_t spd,
208 	u_int16_t dst_hi,
209 	u_int32_t dst_lo,
210 	int len,
211 	char *data,
212 	void (*hand)(struct fw_xfer *))
213 {
214 	struct fw_xfer *xfer;
215 	struct fw_pkt *fp;
216 
217 	xfer = fwmem_xfer_req(fwdev, sc, spd, roundup(16+len, 4), 12, hand);
218 	if (xfer == NULL)
219 		return NULL;
220 
221 	fp = (struct fw_pkt *)xfer->send.buf;
222 	fp->mode.wreqb.tcode = FWTCODE_WREQB;
223 	fp->mode.wreqb.dst = xfer->dst;
224 	fp->mode.wreqb.dest_hi = dst_hi;
225 	fp->mode.wreqb.dest_lo = dst_lo;
226 	fp->mode.wreqb.len = len;
227 	bcopy(data, &fp->mode.wreqb.payload[0], len);
228 
229 	if (fwmem_debug)
230 		printf("fwmem_write_block: %d %04x:%08x %d\n", fwdev->dst,
231 				dst_hi, dst_lo, len);
232 	if (fw_asyreq(xfer->fc, -1, xfer) == 0)
233 		return xfer;
234 
235 	fw_xfer_free(xfer);
236 	return NULL;
237 }
238 
239 
240 int
241 fwmem_open (dev_t dev, int flags, int fmt, fw_proc *td)
242 {
243 	struct fw_eui64 *eui;
244 
245 	eui = (struct fw_eui64 *)malloc(sizeof(struct fw_eui64),
246 							M_FW, M_WAITOK);
247 	if (eui == NULL)
248 		return ENOMEM;
249 	bcopy(&fwmem_eui64, eui, sizeof(struct fw_eui64));
250 	dev->si_drv1 = (void *)eui;
251 
252 	return (0);
253 }
254 
255 int
256 fwmem_close (dev_t dev, int flags, int fmt, fw_proc *td)
257 {
258 	free(dev->si_drv1, M_FW);
259 	return (0);
260 }
261 
262 #define MAXLEN 2048
263 #define USE_QUAD 0
264 int
265 fwmem_read (dev_t dev, struct uio *uio, int ioflag)
266 {
267 	struct firewire_softc *sc;
268 	struct fw_device *fwdev;
269 	struct fw_xfer *xfer;
270 	int err = 0;
271         int unit = DEV2UNIT(dev);
272 	u_int16_t dst_hi;
273 	u_int32_t dst_lo;
274 	off_t offset;
275 	int len;
276 
277 	sc = devclass_get_softc(firewire_devclass, unit);
278 	fwdev = fw_noderesolve_eui64(sc->fc, (struct fw_eui64 *)dev->si_drv1);
279 	if (fwdev == NULL) {
280 		if (fwmem_debug)
281 			printf("fwmem: no such device ID:%08x%08x\n",
282 					fwmem_eui64.hi, fwmem_eui64.lo);
283 		return EINVAL;
284 	}
285 
286 	while(uio->uio_resid > 0 && !err) {
287 		offset = uio->uio_offset;
288 		dst_hi = (offset >> 32) & 0xffff;
289 		dst_lo = offset & 0xffffffff;
290 		len = uio->uio_resid;
291 		if (len == 4 && (dst_lo & 3) == 0) {
292 			xfer = fwmem_read_quad(fwdev, NULL, fwmem_speed,
293 				dst_hi, dst_lo, fw_asy_callback);
294 			if (xfer == NULL) {
295 				err = EINVAL;
296 				break;
297 			}
298 			err = tsleep((caddr_t)xfer, FWPRI, "fwmrq", 0);
299 			if (xfer->recv.buf == NULL)
300 				err = EIO;
301 			else if (xfer->resp != 0)
302 				err = xfer->resp;
303 			else if (err == 0)
304 				err = uiomove(xfer->recv.buf + 4*3, 4, uio);
305 		} else {
306 			if (len > MAXLEN)
307 				len = MAXLEN;
308 			xfer = fwmem_read_block(fwdev, NULL, fwmem_speed,
309 				dst_hi, dst_lo, len, fw_asy_callback);
310 			if (xfer == NULL) {
311 				err = EINVAL;
312 				break;
313 			}
314 			err = tsleep((caddr_t)xfer, FWPRI, "fwmrb", 0);
315 			if (xfer->recv.buf == NULL)
316 				err = EIO;
317 			else if (xfer->resp != 0)
318 				err = xfer->resp;
319 			else if (err == 0)
320 				err = uiomove(xfer->recv.buf + 4*4, len, uio);
321 		}
322 		fw_xfer_free(xfer);
323 	}
324 	return err;
325 }
326 int
327 fwmem_write (dev_t dev, struct uio *uio, int ioflag)
328 {
329 	struct firewire_softc *sc;
330 	struct fw_device *fwdev;
331 	struct fw_xfer *xfer;
332 	int err = 0;
333         int unit = DEV2UNIT(dev);
334 	u_int16_t dst_hi;
335 	u_int32_t dst_lo, quad;
336 	char *data;
337 	off_t offset;
338 	int len;
339 
340 	sc = devclass_get_softc(firewire_devclass, unit);
341 	fwdev = fw_noderesolve_eui64(sc->fc, (struct fw_eui64 *)dev->si_drv1);
342 	if (fwdev == NULL) {
343 		if (fwmem_debug)
344 			printf("fwmem: no such device ID:%08x%08x\n",
345 					fwmem_eui64.hi, fwmem_eui64.lo);
346 		return EINVAL;
347 	}
348 
349 	data = malloc(MAXLEN, M_FW, M_WAITOK);
350 	if (data == NULL)
351 		return ENOMEM;
352 
353 	while(uio->uio_resid > 0 && !err) {
354 		offset = uio->uio_offset;
355 		dst_hi = (offset >> 32) & 0xffff;
356 		dst_lo = offset & 0xffffffff;
357 		len = uio->uio_resid;
358 		if (len == 4 && (dst_lo & 3) == 0) {
359 			err = uiomove((char *)&quad, sizeof(quad), uio);
360 			xfer = fwmem_write_quad(fwdev, NULL, fwmem_speed,
361 				dst_hi, dst_lo, quad, fw_asy_callback);
362 			if (xfer == NULL) {
363 				err = EINVAL;
364 				break;
365 			}
366 			err = tsleep((caddr_t)xfer, FWPRI, "fwmwq", 0);
367 			if (xfer->resp != 0)
368 				err = xfer->resp;
369 		} else {
370 			if (len > MAXLEN)
371 				len = MAXLEN;
372 			err = uiomove(data, len, uio);
373 			if (err)
374 				break;
375 			xfer = fwmem_write_block(fwdev, NULL, fwmem_speed,
376 				dst_hi, dst_lo, len, data, fw_asy_callback);
377 			if (xfer == NULL) {
378 				err = EINVAL;
379 				break;
380 			}
381 			err = tsleep((caddr_t)xfer, FWPRI, "fwmwb", 0);
382 			if (xfer->resp != 0)
383 				err = xfer->resp;
384 		}
385 		fw_xfer_free(xfer);
386 	}
387 	free(data, M_FW);
388 	return err;
389 }
390 
391 int
392 fwmem_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
393 {
394 	int err = 0;
395 	switch (cmd) {
396 	case FW_SDEUI64:
397 		bcopy(data, dev->si_drv1, sizeof(struct fw_eui64));
398 		break;
399 	case FW_GDEUI64:
400 		bcopy(dev->si_drv1, data, sizeof(struct fw_eui64));
401 		break;
402 	default:
403 		err = EINVAL;
404 	}
405 	return(err);
406 }
407 int
408 fwmem_poll (dev_t dev, int events, fw_proc *td)
409 {
410 	return EINVAL;
411 }
412 int
413 #if __FreeBSD_version < 500102
414 fwmem_mmap (dev_t dev, vm_offset_t offset, int nproto)
415 #else
416 fwmem_mmap (dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nproto)
417 #endif
418 {
419 	return EINVAL;
420 }
421