xref: /netbsd-src/sys/dev/isa/isv.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: isv.c,v 1.6 2014/03/23 02:50:51 christos 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.6 2014/03/23 02:50:51 christos 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 	.d_open = isv_open,
109 	.d_close = nullclose,
110 	.d_read = noread,
111 	.d_write = nowrite,
112 	.d_ioctl = isv_ioctl,
113 	.d_stop = nostop,
114 	.d_tty = notty,
115 	.d_poll = nopoll,
116 	.d_mmap = isv_mmap,
117 	.d_kqfilter = nokqfilter,
118 	.d_flag = D_OTHER
119 };
120 
121 static uint16_t
122 isv_read(struct isv_regs *ir, bus_size_t reg)
123 {
124 	return bus_space_read_2(ir->ir_bt, ir->ir_bh, reg);
125 }
126 
127 static void
128 isv_write(struct isv_regs *ir, bus_size_t reg, uint16_t val)
129 {
130 	bus_space_write_2(ir->ir_bt, ir->ir_bh, reg, val);
131 }
132 
133 static bool
134 isv_retrace(struct isv_regs *ir)
135 {
136 	uint16_t video;
137 
138 	video = isv_read(ir, ISV_STATUS) & ISV_STATUS_VIDEO_MASK;
139 	return video == ISV_STATUS_VIDEO_RETRACE;
140 }
141 
142 #define state_and_input(__state, __retrace)	\
143 	(((__state) << 1) | ((__retrace) ? 1 : 0))
144 
145 static bool
146 isv_delta(int *state, bool retrace)
147 {
148 	bool transition = false;
149 
150 	switch (state_and_input(*state, retrace)) {
151 	case state_and_input(ISV_S_CAPTURE0, false):
152 	case state_and_input(ISV_S_RETRACE, true):
153 		break;
154 	case state_and_input(ISV_S_CAPTURE2, true):
155 		transition = true;
156 		/*FALLTHROUGH*/
157 	case state_and_input(ISV_S_CAPTURE1, true):
158 	case state_and_input(ISV_S_CAPTURE0, true):
159 		(*state)++;
160 		break;
161 	case state_and_input(ISV_S_RETRACE, false):
162 		transition = true;
163 		/*FALLTHROUGH*/
164 	case state_and_input(ISV_S_CAPTURE2, false):
165 	case state_and_input(ISV_S_CAPTURE1, false):
166 		*state = ISV_S_CAPTURE0;
167 		break;
168 	}
169 	return transition;
170 }
171 
172 static int
173 isv_probe(struct isv_regs *ir)
174 {
175 	int state, transitions;
176 	struct timeval end, now,
177 	    wait = {.tv_sec = 0, .tv_usec = 1000000 * 4 / 30};
178 
179 	aprint_debug("%s: resetting\n", __func__);
180 	isv_write(ir, ISV_CONTROL,
181 	    ISV_CONTROL_MODE_CAPTURE|ISV_CONTROL_COUNTER_AUTOINC);
182 
183 	aprint_debug("%s: waiting\n", __func__);
184 
185 	microtime(&now);
186 	timeradd(&now, &wait, &end);
187 
188 	state = transitions = 0;
189 
190 	do {
191 		if (isv_delta(&state, isv_retrace(ir)))
192 			transitions++;
193 
194 		if (state == ISV_S_CAPTURE0 || state == ISV_S_RETRACE)
195 			microtime(&now);
196 	} while (timercmp(&now, &end, <));
197 
198 	aprint_debug("%s: %d transitions\n", __func__, transitions);
199 
200 	return transitions >= 4 && transitions <= 10;
201 }
202 
203 static int
204 isv_match(device_t parent, cfdata_t match, void *aux)
205 {
206 	struct isv_regs ir;
207 	struct isa_attach_args *ia = aux;
208 	int rv;
209 
210 	/* Must supply an address */
211 	if (ia->ia_nio < 1 || ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
212 		return 0;
213 
214 	ir.ir_bt = ia->ia_iot;
215 
216 	if (bus_space_map(ir.ir_bt, ia->ia_io[0].ir_addr, 8, 0, &ir.ir_bh))
217 		return 0;
218 
219 	rv = isv_probe(&ir);
220 
221 	bus_space_unmap(ir.ir_bt, ir.ir_bh, 8);
222 
223 	if (rv) {
224 		ia->ia_nio = 1;
225 		ia->ia_io[0].ir_size = 8;
226 
227 		ia->ia_niomem = 0;
228 		ia->ia_nirq = 0;
229 		ia->ia_ndrq = 0;
230 	}
231 
232 	return rv;
233 }
234 
235 
236 static void
237 isv_attach(device_t parent, device_t self, void *aux)
238 {
239 	struct isv_softc *sc = device_private(self);
240 	struct isv_regs *ir = &sc->sc_ir;
241 	struct isa_attach_args *ia = aux;
242 
243 	ir->ir_bt = ia->ia_iot;
244 
245 	if (bus_space_map(ir->ir_bt, ia->ia_io[0].ir_addr, 8, 0, &ir->ir_bh)) {
246 		aprint_error(": can't map i/o space\n");
247 		return;
248 	}
249 
250 	/* Bus-independent attachment */
251 	sc->sc_dev = self;
252 
253 	aprint_normal(": IDEC Supervision/16\n");
254 
255 	/* TBD */
256 }
257 
258 int
259 isv_open(dev_t dev, int flag, int devtype, lwp_t *l)
260 {
261 	vaddr_t va;
262 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
263 
264 	if (sc == NULL)
265 		return ENXIO;
266 
267 	if (sc->sc_frame != NULL)
268 		return 0;
269 
270 	if ((va = uvm_km_alloc(kernel_map, ISV_WIDTH * ISV_LINES, PAGE_SIZE,
271 	    UVM_KMF_WIRED|UVM_KMF_ZERO|UVM_KMF_CANFAIL|UVM_KMF_WAITVA)) == 0)
272 		return ENOMEM;
273 
274 	sc->sc_frame = (uint16_t *)(void *)va;
275 	return 0;
276 }
277 
278 /* wait for retrace */
279 static int
280 isv_retrace_wait(struct isv_regs *ir, int *state, const struct timeval *end)
281 {
282 	struct timeval now;
283 
284 	for (;;) {
285 		if (!isv_delta(state, isv_retrace(ir))) {
286 			microtime(&now);
287 			continue;
288 		}
289 		if (*state == ISV_S_RETRACE)
290 			break;
291 		if (*state != ISV_S_CAPTURE0)
292 			continue;
293 
294 		microtime(&now);
295 		if (timercmp(&now, end, >=))
296 			return EIO;
297 	}
298 	return 0;
299 }
300 
301 /* wait for capture mode */
302 static int
303 isv_capture_wait(struct isv_regs *ir, int *state, const struct timeval *end)
304 {
305 	struct timeval now;
306 
307 	for (;;) {
308 		if (!isv_delta(state, isv_retrace(ir))) {
309 			microtime(&now);
310 			continue;
311 		}
312 		if (*state != ISV_S_RETRACE)
313 			break;
314 
315 		microtime(&now);
316 		if (timercmp(&now, end, >=))
317 			return EIO;
318 	}
319 	return 0;
320 }
321 
322 
323 static int
324 isv_capture(struct isv_softc *sc)
325 {
326 	int speed;
327 	int rc, state = ISV_S_CAPTURE0;
328 	struct timeval diff, end, start, stop;
329 	static const struct timeval wait = {.tv_sec = 0, .tv_usec = 200000};
330 	struct isv_regs *ir = &sc->sc_ir;
331 
332 	if (sc->sc_frame == NULL)
333 		return EAGAIN;
334 
335 	microtime(&start);
336 
337 	timeradd(&start, &wait, &end);
338 
339 	speed = sc->sc_speed;
340 	sc->sc_speed = 0;
341 
342 	if (speed < 1 && (rc = isv_retrace_wait(ir, &state, &end)) != 0)
343 		return rc;
344 
345 	if (speed < 2 && (rc = isv_capture_wait(ir, &state, &end)) != 0)
346 		return rc;
347 
348 	if ((rc = isv_retrace_wait(ir, &state, &end)) != 0)
349 		return rc;
350 
351 	microtime(&stop);
352 
353 	timersub(&stop, &start, &diff);
354 
355 	aprint_debug_dev(sc->sc_dev, "%ssync in %" PRId64 ".%06d seconds\n",
356 	    (speed < 1) ? "" : ((speed < 2) ? "faster " : "fastest "),
357 	    diff.tv_sec, diff.tv_usec);
358 
359 	microtime(&start);
360 
361 	/* enter read mode, then toggle counter mode,
362 	 * autoinc -> reset -> autoinc, so that we start reading
363 	 * at the top of the frame.
364 	 */
365 	isv_write(ir, ISV_CONTROL,
366 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_AUTOINC);
367 	isv_write(ir, ISV_CONTROL,
368 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_RESET);
369 	isv_write(ir, ISV_CONTROL,
370 	    ISV_CONTROL_MODE_READ|ISV_CONTROL_COUNTER_AUTOINC);
371 	/* read one dummy word to prime the state machine on the
372 	 * image capture board
373 	 */
374 	isv_read(ir, ISV_DATA);
375 	bus_space_read_multi_stream_2(ir->ir_bt, ir->ir_bh, ISV_DATA,
376 	    sc->sc_frame, ISV_WIDTH * ISV_LINES / 2);
377 
378 	/* restore to initial conditions */
379 	isv_write(ir, ISV_CONTROL,
380 	    ISV_CONTROL_MODE_CAPTURE|ISV_CONTROL_COUNTER_AUTOINC);
381 
382 	microtime(&stop);
383 
384 	timersub(&stop, &start, &diff);
385 
386 	aprint_debug_dev(sc->sc_dev, "read in %" PRId64 ".%06d seconds\n",
387 		diff.tv_sec, diff.tv_usec);
388 
389 	state = 0;
390 
391 	if (isv_retrace_wait(ir, &state, &end) != 0)
392 		return 0;
393 	sc->sc_speed++;
394 
395 	if (isv_capture_wait(ir, &state, &end) != 0)
396 		return 0;
397 	sc->sc_speed++;
398 
399 	return 0;
400 }
401 
402 int
403 isv_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
404 {
405 	struct isv_cmd ic;
406 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
407 
408 	if (cmd != ISV_CMD)
409 		return ENOTTY;
410 
411 	memcpy(&ic, data, sizeof(ic));
412 
413 	if (ic.c_cmd != ISV_CMD_READ)
414 		return EINVAL;
415 
416 	ic.c_frameno = 0;
417 
418 	return isv_capture(sc);
419 }
420 
421 paddr_t
422 isv_mmap(dev_t dev, off_t offset, int prot)
423 {
424 	struct isv_softc *sc = device_lookup_private(&isv_cd, minor(dev));
425 	paddr_t pa;
426 
427 	if ((prot & ~(VM_PROT_READ)) != 0)
428 		return -1;
429 
430 	if (sc->sc_frame == NULL)
431 		return -1;
432 
433 	if (offset >= ISV_WIDTH * ISV_LINES)
434 		return -1;
435 
436 	if (!pmap_extract(pmap_kernel(), (vaddr_t)&sc->sc_frame[offset/2], &pa))
437 		return -1;
438 
439 	return atop(pa);
440 }
441 
442 static int
443 isv_detach(device_t self, int flags)
444 {
445 	struct isv_softc *sc = device_private(self);
446 	struct isv_regs *ir = &sc->sc_ir;
447 
448 	if (sc->sc_frame != NULL) {
449 		uvm_km_free(kernel_map, (vaddr_t)sc->sc_frame,
450 		    ISV_WIDTH * ISV_LINES, UVM_KMF_WIRED);
451 	}
452 	bus_space_unmap(ir->ir_bt, ir->ir_bh, 8);
453 	return 0;
454 }
455