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