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