1 /* $NetBSD: isv.c,v 1.2 2008/04/28 20:23:52 martin Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: isv.c,v 1.2 2008/04/28 20:23:52 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
44 #include <dev/isa/isareg.h>
45 #include <dev/isa/isavar.h>
47 #include <dev/isa/isvio.h>
49 #define ISV_CONTROL 0x0 /* control: write-only */
50 #define ISV_CONTROL_MODE_MASK __BIT(0)
51 #define ISV_CONTROL_MODE_CAPTURE __SHIFTIN(0, ISV_CONTROL_MODE_MASK)
52 #define ISV_CONTROL_MODE_READ __SHIFTIN(1, ISV_CONTROL_MODE_MASK)
53 #define ISV_CONTROL_COUNTER_MASK __BIT(1)
54 #define ISV_CONTROL_COUNTER_RESET __SHIFTIN(1, ISV_CONTROL_COUNTER_MASK)
55 #define ISV_CONTROL_COUNTER_AUTOINC __SHIFTIN(0, ISV_CONTROL_COUNTER_MASK)
57 #define ISV_DATA ISV_CONTROL /* data: read-only */
59 #define ISV_STATUS 0x2 /* status: read-only */
60 #define ISV_STATUS_VIDEO_MASK __BIT(15)
61 #define ISV_STATUS_VIDEO_RETRACE __SHIFTIN(0, ISV_STATUS_VIDEO_MASK)
62 #define ISV_STATUS_VIDEO_WRITE __SHIFTIN(1, ISV_STATUS_VIDEO_MASK)
65 bus_space_tag_t ir_bt
;
66 bus_space_handle_t ir_bh
;
77 struct isv_regs sc_ir
;
83 extern struct cfdriver isv_cd
;
85 static dev_type_ioctl(isv_ioctl
);
86 static dev_type_open(isv_open
);
87 static dev_type_mmap(isv_mmap
);
89 static int isv_capture(struct isv_softc
*);
90 static int isv_match(device_t
, cfdata_t
, void *);
91 static void isv_attach(device_t
, device_t
, void *);
92 static int isv_detach(device_t
, int);
93 static uint16_t isv_read(struct isv_regs
*, bus_size_t
);
94 static void isv_write(struct isv_regs
*, bus_size_t
, uint16_t);
95 static bool isv_retrace(struct isv_regs
*);
96 static int isv_retrace_wait(struct isv_regs
*, int *,
97 const struct timeval
*);
98 static int isv_capture_wait(struct isv_regs
*, int *,
99 const struct timeval
*);
100 static bool isv_delta(int *, bool);
101 static int isv_probe(struct isv_regs
*);
103 CFATTACH_DECL_NEW(isv_isa
, sizeof(struct isv_softc
),
104 isv_match
, isv_attach
, isv_detach
, NULL
);
106 const struct cdevsw isv_cdevsw
= {
107 isv_open
, nullclose
, noread
, nowrite
, isv_ioctl
,
108 nostop
, notty
, nopoll
, isv_mmap
, nokqfilter
, D_OTHER
112 isv_read(struct isv_regs
*ir
, bus_size_t reg
)
114 return bus_space_read_2(ir
->ir_bt
, ir
->ir_bh
, reg
);
118 isv_write(struct isv_regs
*ir
, bus_size_t reg
, uint16_t val
)
120 bus_space_write_2(ir
->ir_bt
, ir
->ir_bh
, reg
, val
);
124 isv_retrace(struct isv_regs
*ir
)
128 video
= isv_read(ir
, ISV_STATUS
) & ISV_STATUS_VIDEO_MASK
;
129 return video
== ISV_STATUS_VIDEO_RETRACE
;
132 #define state_and_input(__state, __retrace) \
133 (((__state) << 1) | ((__retrace) ? 1 : 0))
136 isv_delta(int *state
, bool retrace
)
138 bool transition
= false;
140 switch (state_and_input(*state
, retrace
)) {
141 case state_and_input(ISV_S_CAPTURE0
, false):
142 case state_and_input(ISV_S_RETRACE
, true):
144 case state_and_input(ISV_S_CAPTURE2
, true):
147 case state_and_input(ISV_S_CAPTURE1
, true):
148 case state_and_input(ISV_S_CAPTURE0
, true):
151 case state_and_input(ISV_S_RETRACE
, false):
154 case state_and_input(ISV_S_CAPTURE2
, false):
155 case state_and_input(ISV_S_CAPTURE1
, false):
156 *state
= ISV_S_CAPTURE0
;
163 isv_probe(struct isv_regs
*ir
)
165 int state
, transitions
;
166 struct timeval end
, now
,
167 wait
= {.tv_sec
= 0, .tv_usec
= 1000000 * 4 / 30};
169 aprint_debug("%s: resetting\n", __func__
);
170 isv_write(ir
, ISV_CONTROL
,
171 ISV_CONTROL_MODE_CAPTURE
|ISV_CONTROL_COUNTER_AUTOINC
);
173 aprint_debug("%s: waiting\n", __func__
);
176 timeradd(&now
, &wait
, &end
);
178 state
= transitions
= 0;
181 if (isv_delta(&state
, isv_retrace(ir
)))
184 if (state
== ISV_S_CAPTURE0
|| state
== ISV_S_RETRACE
)
186 } while (timercmp(&now
, &end
, <));
188 aprint_debug("%s: %d transitions\n", __func__
, transitions
);
190 return transitions
>= 4 && transitions
<= 10;
194 isv_match(device_t parent
, cfdata_t match
, void *aux
)
197 struct isa_attach_args
*ia
= aux
;
200 /* Must supply an address */
201 if (ia
->ia_nio
< 1 || ia
->ia_io
[0].ir_addr
== ISA_UNKNOWN_PORT
)
204 ir
.ir_bt
= ia
->ia_iot
;
206 if (bus_space_map(ir
.ir_bt
, ia
->ia_io
[0].ir_addr
, 8, 0, &ir
.ir_bh
))
211 bus_space_unmap(ir
.ir_bt
, ir
.ir_bh
, 8);
215 ia
->ia_io
[0].ir_size
= 8;
227 isv_attach(device_t parent
, device_t self
, void *aux
)
229 struct isv_softc
*sc
= device_private(self
);
230 struct isv_regs
*ir
= &sc
->sc_ir
;
231 struct isa_attach_args
*ia
= aux
;
233 ir
->ir_bt
= ia
->ia_iot
;
235 if (bus_space_map(ir
->ir_bt
, ia
->ia_io
[0].ir_addr
, 8, 0, &ir
->ir_bh
)) {
236 aprint_error(": can't map i/o space\n");
240 /* Bus-independent attachment */
243 aprint_normal(": IDEC Supervision/16\n");
249 isv_open(dev_t dev
, int flag
, int devtype
, lwp_t
*l
)
252 struct isv_softc
*sc
= device_lookup_private(&isv_cd
, minor(dev
));
257 if (sc
->sc_frame
!= NULL
)
260 if ((va
= uvm_km_alloc(kernel_map
, ISV_WIDTH
* ISV_LINES
, PAGE_SIZE
,
261 UVM_KMF_WIRED
|UVM_KMF_ZERO
|UVM_KMF_CANFAIL
|UVM_KMF_WAITVA
)) == 0)
264 sc
->sc_frame
= (uint16_t *)(void *)va
;
268 /* wait for retrace */
270 isv_retrace_wait(struct isv_regs
*ir
, int *state
, const struct timeval
*end
)
275 if (!isv_delta(state
, isv_retrace(ir
))) {
279 if (*state
== ISV_S_RETRACE
)
281 if (*state
!= ISV_S_CAPTURE0
)
285 if (timercmp(&now
, end
, >=))
291 /* wait for capture mode */
293 isv_capture_wait(struct isv_regs
*ir
, int *state
, const struct timeval
*end
)
298 if (!isv_delta(state
, isv_retrace(ir
))) {
302 if (*state
!= ISV_S_RETRACE
)
306 if (timercmp(&now
, end
, >=))
314 isv_capture(struct isv_softc
*sc
)
318 int rc
, state
= ISV_S_CAPTURE0
;
319 struct timeval diff
, end
, start
, stop
;
320 static const struct timeval wait
= {.tv_sec
= 0, .tv_usec
= 200000};
321 struct isv_regs
*ir
= &sc
->sc_ir
;
323 if (sc
->sc_frame
== NULL
)
328 timeradd(&start
, &wait
, &end
);
330 speed
= sc
->sc_speed
;
333 if (speed
< 1 && (rc
= isv_retrace_wait(ir
, &state
, &end
)) != 0)
336 if (speed
< 2 && (rc
= isv_capture_wait(ir
, &state
, &end
)) != 0)
339 if ((rc
= isv_retrace_wait(ir
, &state
, &end
)) != 0)
344 timersub(&stop
, &start
, &diff
);
346 aprint_debug_dev(sc
->sc_dev
, "%ssync in %" PRId64
".%06d seconds\n",
347 (speed
< 1) ? "" : ((speed
< 2) ? "faster " : "fastest "),
348 diff
.tv_sec
, diff
.tv_usec
);
352 /* enter read mode, then toggle counter mode,
353 * autoinc -> reset -> autoinc, so that we start reading
354 * at the top of the frame.
356 isv_write(ir
, ISV_CONTROL
,
357 ISV_CONTROL_MODE_READ
|ISV_CONTROL_COUNTER_AUTOINC
);
358 isv_write(ir
, ISV_CONTROL
,
359 ISV_CONTROL_MODE_READ
|ISV_CONTROL_COUNTER_RESET
);
360 isv_write(ir
, ISV_CONTROL
,
361 ISV_CONTROL_MODE_READ
|ISV_CONTROL_COUNTER_AUTOINC
);
362 /* read one dummy word to prime the state machine on the
363 * image capture board
365 discard
= isv_read(ir
, ISV_DATA
);
366 bus_space_read_multi_stream_2(ir
->ir_bt
, ir
->ir_bh
, ISV_DATA
,
367 sc
->sc_frame
, ISV_WIDTH
* ISV_LINES
/ 2);
369 /* restore to initial conditions */
370 isv_write(ir
, ISV_CONTROL
,
371 ISV_CONTROL_MODE_CAPTURE
|ISV_CONTROL_COUNTER_AUTOINC
);
375 timersub(&stop
, &start
, &diff
);
377 aprint_debug_dev(sc
->sc_dev
, "read in %" PRId64
".%06d seconds\n",
378 diff
.tv_sec
, diff
.tv_usec
);
382 if (isv_retrace_wait(ir
, &state
, &end
) != 0)
386 if (isv_capture_wait(ir
, &state
, &end
) != 0)
394 isv_ioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, lwp_t
*l
)
397 struct isv_softc
*sc
= device_lookup_private(&isv_cd
, minor(dev
));
402 memcpy(&ic
, data
, sizeof(ic
));
404 if (ic
.c_cmd
!= ISV_CMD_READ
)
409 return isv_capture(sc
);
413 isv_mmap(dev_t dev
, off_t offset
, int prot
)
415 struct isv_softc
*sc
= device_lookup_private(&isv_cd
, minor(dev
));
418 if ((prot
& ~(VM_PROT_READ
)) != 0)
421 if (sc
->sc_frame
== NULL
)
424 if (offset
>= ISV_WIDTH
* ISV_LINES
)
427 if (!pmap_extract(pmap_kernel(), (vaddr_t
)&sc
->sc_frame
[offset
/2], &pa
))
434 isv_detach(device_t self
, int flags
)
436 struct isv_softc
*sc
= device_private(self
);
437 struct isv_regs
*ir
= &sc
->sc_ir
;
439 if (sc
->sc_frame
!= NULL
) {
440 uvm_km_free(kernel_map
, (vaddr_t
)sc
->sc_frame
,
441 ISV_WIDTH
* ISV_LINES
, UVM_KMF_WIRED
);
443 bus_space_unmap(ir
->ir_bt
, ir
->ir_bh
, 8);