xref: /netbsd-src/sys/dev/isa/isv.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: isv.c,v 1.1 2008/04/02 01:34:36 dyoung Exp $ */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by David Young.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: isv.c,v 1.1 2008/04/02 01:34:36 dyoung Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <uvm/uvm.h>
48 
49 #include <sys/bus.h>
50 
51 #include <dev/isa/isareg.h>
52 #include <dev/isa/isavar.h>
53 
54 #include <dev/isa/isvio.h>
55 
56 #define	ISV_CONTROL	0x0		/* control: write-only */
57 #define	ISV_CONTROL_MODE_MASK		__BIT(0)
58 #define	ISV_CONTROL_MODE_CAPTURE	__SHIFTIN(0, ISV_CONTROL_MODE_MASK)
59 #define	ISV_CONTROL_MODE_READ		__SHIFTIN(1, ISV_CONTROL_MODE_MASK)
60 #define	ISV_CONTROL_COUNTER_MASK	__BIT(1)
61 #define	ISV_CONTROL_COUNTER_RESET	__SHIFTIN(1, ISV_CONTROL_COUNTER_MASK)
62 #define	ISV_CONTROL_COUNTER_AUTOINC	__SHIFTIN(0, ISV_CONTROL_COUNTER_MASK)
63 
64 #define	ISV_DATA	ISV_CONTROL	/* data: read-only */
65 
66 #define ISV_STATUS	0x2		/* status: read-only */
67 #define ISV_STATUS_VIDEO_MASK		__BIT(15)
68 #define ISV_STATUS_VIDEO_RETRACE	__SHIFTIN(0, ISV_STATUS_VIDEO_MASK)
69 #define ISV_STATUS_VIDEO_WRITE		__SHIFTIN(1, ISV_STATUS_VIDEO_MASK)
70 
71 struct isv_regs {
72 	bus_space_tag_t		ir_bt;
73 	bus_space_handle_t	ir_bh;
74 };
75 
76 enum isv_state {
77 	  ISV_S_CAPTURE0 = 0
78 	, ISV_S_CAPTURE1 = 1
79 	, ISV_S_CAPTURE2 = 2
80 	, ISV_S_RETRACE = 3
81 };
82 
83 struct isv_softc {
84 	struct isv_regs	sc_ir;
85 	device_t	sc_dev;
86 	uint16_t	*sc_frame;
87 	int		sc_speed;
88 };
89 
90 extern struct cfdriver isv_cd;
91 
92 static dev_type_ioctl(isv_ioctl);
93 static dev_type_open(isv_open);
94 static dev_type_mmap(isv_mmap);
95 
96 static int	isv_capture(struct isv_softc *);
97 static int 	isv_match(device_t, cfdata_t, void *);
98 static void 	isv_attach(device_t, device_t, void *);
99 static int 	isv_detach(device_t, int);
100 static uint16_t isv_read(struct isv_regs *, bus_size_t);
101 static void	isv_write(struct isv_regs *, bus_size_t, uint16_t);
102 static bool	isv_retrace(struct isv_regs *);
103 static int	isv_retrace_wait(struct isv_regs *, int *,
104     const struct timeval *);
105 static int	isv_capture_wait(struct isv_regs *, int *,
106     const struct timeval *);
107 static bool	isv_delta(int *, bool);
108 static int	isv_probe(struct isv_regs *);
109 
110 CFATTACH_DECL_NEW(isv_isa, sizeof(struct isv_softc),
111     isv_match, isv_attach, isv_detach, NULL);
112 
113 const struct cdevsw isv_cdevsw = {
114 	isv_open, nullclose, noread, nowrite, isv_ioctl,
115 	nostop, notty, nopoll, isv_mmap, nokqfilter, D_OTHER
116 };
117 
118 static uint16_t
119 isv_read(struct isv_regs *ir, bus_size_t reg)
120 {
121 	return bus_space_read_2(ir->ir_bt, ir->ir_bh, reg);
122 }
123 
124 static void
125 isv_write(struct isv_regs *ir, bus_size_t reg, uint16_t val)
126 {
127 	bus_space_write_2(ir->ir_bt, ir->ir_bh, reg, val);
128 }
129 
130 static bool
131 isv_retrace(struct isv_regs *ir)
132 {
133 	uint16_t video;
134 
135 	video = isv_read(ir, ISV_STATUS) & ISV_STATUS_VIDEO_MASK;
136 	return video == ISV_STATUS_VIDEO_RETRACE;
137 }
138 
139 #define state_and_input(__state, __retrace)	\
140 	(((__state) << 1) | ((__retrace) ? 1 : 0))
141 
142 static bool
143 isv_delta(int *state, bool retrace)
144 {
145 	bool transition = false;
146 
147 	switch (state_and_input(*state, retrace)) {
148 	case state_and_input(ISV_S_CAPTURE0, false):
149 	case state_and_input(ISV_S_RETRACE, true):
150 		break;
151 	case state_and_input(ISV_S_CAPTURE2, true):
152 		transition = true;
153 		/*FALLTHROUGH*/
154 	case state_and_input(ISV_S_CAPTURE1, true):
155 	case state_and_input(ISV_S_CAPTURE0, true):
156 		(*state)++;
157 		break;
158 	case state_and_input(ISV_S_RETRACE, false):
159 		transition = true;
160 		/*FALLTHROUGH*/
161 	case state_and_input(ISV_S_CAPTURE2, false):
162 	case state_and_input(ISV_S_CAPTURE1, false):
163 		*state = ISV_S_CAPTURE0;
164 		break;
165 	}
166 	return transition;
167 }
168 
169 static int
170 isv_probe(struct isv_regs *ir)
171 {
172 	int state, transitions;
173 	struct timeval end, now,
174 	    wait = {.tv_sec = 0, .tv_usec = 1000000 * 4 / 30};
175 
176 	aprint_debug("%s: resetting\n", __func__);
177 	isv_write(ir, ISV_CONTROL,
178 	    ISV_CONTROL_MODE_CAPTURE|ISV_CONTROL_COUNTER_AUTOINC);
179 
180 	aprint_debug("%s: waiting\n", __func__);
181 
182 	microtime(&now);
183 	timeradd(&now, &wait, &end);
184 
185 	state = transitions = 0;
186 
187 	do {
188 		if (isv_delta(&state, isv_retrace(ir)))
189 			transitions++;
190 
191 		if (state == ISV_S_CAPTURE0 || state == ISV_S_RETRACE)
192 			microtime(&now);
193 	} while (timercmp(&now, &end, <));
194 
195 	aprint_debug("%s: %d transitions\n", __func__, transitions);
196 
197 	return transitions >= 4 && transitions <= 10;
198 }
199 
200 static int
201 isv_match(device_t parent, cfdata_t match, void *aux)
202 {
203 	struct isv_regs ir;
204 	struct isa_attach_args *ia = aux;
205 	int rv;
206 
207 	/* Must supply an address */
208 	if (ia->ia_nio < 1 || ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
209 		return 0;
210 
211 	ir.ir_bt = ia->ia_iot;
212 
213 	if (bus_space_map(ir.ir_bt, ia->ia_io[0].ir_addr, 8, 0, &ir.ir_bh))
214 		return 0;
215 
216 	rv = isv_probe(&ir);
217 
218 	bus_space_unmap(ir.ir_bt, ir.ir_bh, 8);
219 
220 	if (rv) {
221 		ia->ia_nio = 1;
222 		ia->ia_io[0].ir_size = 8;
223 
224 		ia->ia_niomem = 0;
225 		ia->ia_nirq = 0;
226 		ia->ia_ndrq = 0;
227 	}
228 
229 	return rv;
230 }
231 
232 
233 static void
234 isv_attach(device_t parent, device_t self, void *aux)
235 {
236 	struct isv_softc *sc = device_private(self);
237 	struct isv_regs *ir = &sc->sc_ir;
238 	struct isa_attach_args *ia = aux;
239 
240 	ir->ir_bt = ia->ia_iot;
241 
242 	if (bus_space_map(ir->ir_bt, ia->ia_io[0].ir_addr, 8, 0, &ir->ir_bh)) {
243 		aprint_error(": can't map i/o space\n");
244 		return;
245 	}
246 
247 	/* Bus-independent attachment */
248 	sc->sc_dev = self;
249 
250 	aprint_normal(": IDEC Supervision/16\n");
251 
252 	/* TBD */
253 }
254 
255 int
256 isv_open(dev_t dev, int flag, int devtype, lwp_t *l)
257 {
258 	vaddr_t va;
259 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
260 
261 	if (sc == NULL)
262 		return ENXIO;
263 
264 	if (sc->sc_frame != NULL)
265 		return 0;
266 
267 	if ((va = uvm_km_alloc(kernel_map, ISV_WIDTH * ISV_LINES, PAGE_SIZE,
268 	    UVM_KMF_WIRED|UVM_KMF_ZERO|UVM_KMF_CANFAIL|UVM_KMF_WAITVA)) == 0)
269 		return ENOMEM;
270 
271 	sc->sc_frame = (uint16_t *)(void *)va;
272 	return 0;
273 }
274 
275 /* wait for retrace */
276 static int
277 isv_retrace_wait(struct isv_regs *ir, int *state, const struct timeval *end)
278 {
279 	struct timeval now;
280 
281 	for (;;) {
282 		if (!isv_delta(state, isv_retrace(ir))) {
283 			microtime(&now);
284 			continue;
285 		}
286 		if (*state == ISV_S_RETRACE)
287 			break;
288 		if (*state != ISV_S_CAPTURE0)
289 			continue;
290 
291 		microtime(&now);
292 		if (timercmp(&now, end, >=))
293 			return EIO;
294 	}
295 	return 0;
296 }
297 
298 /* wait for capture mode */
299 static int
300 isv_capture_wait(struct isv_regs *ir, int *state, const struct timeval *end)
301 {
302 	struct timeval now;
303 
304 	for (;;) {
305 		if (!isv_delta(state, isv_retrace(ir))) {
306 			microtime(&now);
307 			continue;
308 		}
309 		if (*state != ISV_S_RETRACE)
310 			break;
311 
312 		microtime(&now);
313 		if (timercmp(&now, end, >=))
314 			return EIO;
315 	}
316 	return 0;
317 }
318 
319 
320 static int
321 isv_capture(struct isv_softc *sc)
322 {
323 	int speed;
324 	uint16_t discard;
325 	int rc, state = ISV_S_CAPTURE0;
326 	struct timeval diff, end, start, stop;
327 	static const struct timeval wait = {.tv_sec = 0, .tv_usec = 200000};
328 	struct isv_regs *ir = &sc->sc_ir;
329 
330 	if (sc->sc_frame == NULL)
331 		return EAGAIN;
332 
333 	microtime(&start);
334 
335 	timeradd(&start, &wait, &end);
336 
337 	speed = sc->sc_speed;
338 	sc->sc_speed = 0;
339 
340 	if (speed < 1 && (rc = isv_retrace_wait(ir, &state, &end)) != 0)
341 		return rc;
342 
343 	if (speed < 2 && (rc = isv_capture_wait(ir, &state, &end)) != 0)
344 		return rc;
345 
346 	if ((rc = isv_retrace_wait(ir, &state, &end)) != 0)
347 		return rc;
348 
349 	microtime(&stop);
350 
351 	timersub(&stop, &start, &diff);
352 
353 	aprint_debug_dev(sc->sc_dev, "%ssync in %ld.%06ld seconds\n",
354 	    (speed < 1) ? "" : ((speed < 2) ? "faster " : "fastest "),
355 	    diff.tv_sec, diff.tv_usec);
356 
357 	microtime(&start);
358 
359 	/* enter read mode, then toggle counter mode,
360 	 * autoinc -> reset -> autoinc, so that we start reading
361 	 * at the top of the frame.
362 	 */
363 	isv_write(ir, ISV_CONTROL,
364 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_AUTOINC);
365 	isv_write(ir, ISV_CONTROL,
366 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_RESET);
367 	isv_write(ir, ISV_CONTROL,
368 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_AUTOINC);
369 	/* read one dummy word to prime the state machine on the
370 	 * image capture board
371 	 */
372 	discard = isv_read(ir, ISV_DATA);
373 	bus_space_read_multi_stream_2(ir->ir_bt, ir->ir_bh, ISV_DATA,
374 	    sc->sc_frame, ISV_WIDTH * ISV_LINES / 2);
375 
376 	/* restore to initial conditions */
377 	isv_write(ir, ISV_CONTROL,
378 	    ISV_CONTROL_MODE_CAPTURE|ISV_CONTROL_COUNTER_AUTOINC);
379 
380 	microtime(&stop);
381 
382 	timersub(&stop, &start, &diff);
383 
384 	aprint_debug_dev(sc->sc_dev, "read in %ld.%06ld seconds\n",
385 		diff.tv_sec, diff.tv_usec);
386 
387 	state = 0;
388 
389 	if (isv_retrace_wait(ir, &state, &end) != 0)
390 		return 0;
391 	sc->sc_speed++;
392 
393 	if (isv_capture_wait(ir, &state, &end) != 0)
394 		return 0;
395 	sc->sc_speed++;
396 
397 	return 0;
398 }
399 
400 int
401 isv_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
402 {
403 	struct isv_cmd ic;
404 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
405 
406 	if (cmd != ISV_CMD)
407 		return ENOTTY;
408 
409 	memcpy(&ic, data, sizeof(ic));
410 
411 	if (ic.c_cmd != ISV_CMD_READ)
412 		return EINVAL;
413 
414 	ic.c_frameno = 0;
415 
416 	return isv_capture(sc);
417 }
418 
419 paddr_t
420 isv_mmap(dev_t dev, off_t offset, int prot)
421 {
422 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
423 	paddr_t pa;
424 
425 	if ((prot & ~(VM_PROT_READ)) != 0)
426 		return -1;
427 
428 	if (sc->sc_frame == NULL)
429 		return -1;
430 
431 	if (offset >= ISV_WIDTH * ISV_LINES)
432 		return -1;
433 
434 	if (!pmap_extract(pmap_kernel(), (vaddr_t)&sc->sc_frame[offset/2], &pa))
435 		return -1;
436 
437 	return atop(pa);
438 }
439 
440 static int
441 isv_detach(device_t self, int flags)
442 {
443 	struct isv_softc *sc = device_private(self);
444 	struct isv_regs *ir = &sc->sc_ir;
445 
446 	if (sc->sc_frame != NULL) {
447 		uvm_km_free(kernel_map, (vaddr_t)sc->sc_frame,
448 		    ISV_WIDTH * ISV_LINES, UVM_KMF_WIRED);
449 	}
450 	bus_space_unmap(ir->ir_bt, ir->ir_bh, 8);
451 	return 0;
452 }
453