pseye.c revision 1.1 1 /* $NetBSD: pseye.c,v 1.1 2008/09/06 19:37:21 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pseye.c,v 1.1 2008/09/06 19:37:21 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36 #include <sys/fcntl.h>
37 #include <sys/conf.h>
38 #include <sys/poll.h>
39 #include <sys/bus.h>
40 #include <sys/mutex.h>
41 #include <sys/kthread.h>
42 #include <sys/condvar.h>
43 #include <sys/videoio.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 #include <dev/usb/usbdevs.h>
51
52 #include <dev/video_if.h>
53
54 /* Bulk-in buffer length */
55 #define PSEYE_BULKIN_BUFLEN (640 * 480 * 2)
56
57 /* SCCB/sensor interface */
58 #define PSEYE_SCCB_ADDRESS 0xf1
59 #define PSEYE_SCCB_SUBADDR 0xf2
60 #define PSEYE_SCCB_WRITE 0xf3
61 #define PSEYE_SCCB_READ 0xf4
62 #define PSEYE_SCCB_OPERATION 0xf5
63 #define PSEYE_SCCB_STATUS 0xf6
64
65 #define PSEYE_SCCB_OP_WRITE_3 0x37
66 #define PSEYE_SCCB_OP_WRITE_2 0x33
67 #define PSEYE_SCCB_OP_READ_2 0xf9
68
69 struct pseye_softc {
70 USBBASEDEVICE sc_dev;
71
72 usbd_device_handle sc_udev;
73 usbd_interface_handle sc_iface;
74
75 device_t sc_videodev;
76 char sc_running;
77
78 kcondvar_t sc_cv;
79 kmutex_t sc_mtx;
80
81 usbd_pipe_handle sc_bulkin_pipe;
82 usbd_xfer_handle sc_bulkin_xfer;
83 int sc_bulkin;
84 uint8_t *sc_bulkin_buffer;
85 int sc_bulkin_bufferlen;
86
87 char sc_dying;
88 };
89
90 static int pseye_match(device_t, cfdata_t, void *);
91 static void pseye_attach(device_t, device_t, void *);
92 static int pseye_detach(device_t, int);
93 static void pseye_childdet(device_t, device_t);
94 static int pseye_activate(device_t, enum devact);
95
96 static void pseye_init(struct pseye_softc *);
97 static void pseye_sccb_init(struct pseye_softc *);
98 static void pseye_stop(struct pseye_softc *);
99 static void pseye_start(struct pseye_softc *);
100 static void pseye_led(struct pseye_softc *, bool);
101 static uint8_t pseye_getreg(struct pseye_softc *, uint16_t);
102 static void pseye_setreg(struct pseye_softc *, uint16_t, uint8_t);
103 static void pseye_setregv(struct pseye_softc *, uint16_t, uint8_t);
104 static void pseye_sccb_setreg(struct pseye_softc *, uint8_t, uint8_t);
105 static bool pseye_sccb_status(struct pseye_softc *);
106
107 static int pseye_init_pipes(struct pseye_softc *);
108 static int pseye_close_pipes(struct pseye_softc *);
109
110 static usbd_status pseye_get_frame(struct pseye_softc *);
111
112 /* video(9) API */
113 static int pseye_open(void *, int);
114 static void pseye_close(void *);
115 static const char * pseye_get_devname(void *);
116 static int pseye_get_format(void *, struct video_format *);
117 static int pseye_set_format(void *, struct video_format *);
118 static int pseye_start_transfer(void *);
119 static int pseye_stop_transfer(void *);
120
121 CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
122 pseye_match, pseye_attach, pseye_detach, pseye_activate,
123 NULL, pseye_childdet);
124
125 static const struct video_hw_if pseye_hw_if = {
126 .open = pseye_open,
127 .close = pseye_close,
128 .get_devname = pseye_get_devname,
129 .get_format = pseye_get_format,
130 .set_format = pseye_set_format,
131 .try_format = NULL,
132 .start_transfer = pseye_start_transfer,
133 .stop_transfer = pseye_stop_transfer,
134 .control_iter_init = NULL,
135 .control_iter_next = NULL,
136 .get_control_desc_group = NULL,
137 .get_control_group = NULL,
138 .set_control_group = NULL,
139 };
140
141 USB_MATCH(pseye)
142 {
143 USB_IFMATCH_START(pseye, uaa);
144
145 if (uaa->class != UICLASS_VENDOR)
146 return UMATCH_NONE;
147
148 if (uaa->vendor == USB_VENDOR_OMNIVISION2) {
149 switch (uaa->product) {
150 case USB_PRODUCT_OMNIVISION2_PSEYE:
151 if (uaa->ifaceno != 0)
152 return UMATCH_NONE;
153 return UMATCH_VENDOR_PRODUCT;
154 }
155 }
156
157 return UMATCH_NONE;
158 }
159
160 USB_ATTACH(pseye)
161 {
162 USB_IFATTACH_START(pseye, sc, uaa);
163 usbd_device_handle dev = uaa->device;
164 usb_interface_descriptor_t *id = NULL;
165 usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
166 char *devinfo;
167 int i;
168
169 USB_ATTACH_SETUP;
170 devinfo = usbd_devinfo_alloc(dev, 0);
171 aprint_normal_dev(self, "%s\n", devinfo);
172 usbd_devinfo_free(devinfo);
173
174 sc->sc_dev = self;
175 sc->sc_udev = dev;
176 sc->sc_iface = uaa->iface;
177 sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;
178
179 sc->sc_dying = sc->sc_running = 0;
180 cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
181 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
182
183 id = usbd_get_interface_descriptor(sc->sc_iface);
184 if (id == NULL) {
185 aprint_error_dev(self, "failed to get interface descriptor\n");
186 sc->sc_dying = 1;
187 USB_ATTACH_ERROR_RETURN;
188 }
189
190 for (i = 0; i < id->bNumEndpoints; i++) {
191 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
192 if (ed == NULL) {
193 aprint_error_dev(sc->sc_dev, "couldn't get ep %d\n", i);
194 sc->sc_dying = 1;
195 USB_ATTACH_ERROR_RETURN;
196 }
197
198 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
199 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
200 ed_bulkin = ed;
201 break;
202 }
203 }
204
205 if (ed_bulkin == NULL) {
206 aprint_error_dev(sc->sc_dev, "no bulk-in endpoint found\n");
207 sc->sc_dying = 1;
208 USB_ATTACH_ERROR_RETURN;
209 }
210
211 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
212
213 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
214 if (sc->sc_bulkin_xfer == NULL) {
215 sc->sc_dying = 1;
216 USB_ATTACH_ERROR_RETURN;
217 }
218 sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
219 sc->sc_bulkin_bufferlen);
220 if (sc->sc_bulkin_buffer == NULL) {
221 usbd_free_xfer(sc->sc_bulkin_xfer);
222 sc->sc_bulkin_xfer = NULL;
223 sc->sc_dying = 1;
224 USB_ATTACH_ERROR_RETURN;
225 }
226
227 pseye_init(sc);
228
229 sc->sc_videodev = video_attach_mi(&pseye_hw_if, sc->sc_dev);
230 if (sc->sc_videodev == NULL) {
231 aprint_error_dev(sc->sc_dev, "couldn't attach video layer\n");
232 sc->sc_dying = 1;
233 USB_ATTACH_ERROR_RETURN;
234 }
235
236 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
237 USBDEV(sc->sc_dev));
238
239 USB_ATTACH_SUCCESS_RETURN;
240 }
241
242 USB_DETACH(pseye)
243 {
244 USB_DETACH_START(pseye, sc);
245
246 sc->sc_dying = 1;
247
248 if (sc->sc_videodev != NULL) {
249 config_detach(sc->sc_videodev, flags);
250 sc->sc_videodev = NULL;
251 }
252
253 if (sc->sc_bulkin_xfer != NULL) {
254 usbd_free_xfer(sc->sc_bulkin_xfer);
255 sc->sc_bulkin_xfer = NULL;
256 }
257
258 if (sc->sc_bulkin_pipe != NULL) {
259 usbd_abort_pipe(sc->sc_bulkin_pipe);
260 sc->sc_bulkin_pipe = NULL;
261 }
262
263 mutex_enter(&sc->sc_mtx);
264 if (sc->sc_running) {
265 sc->sc_running = 0;
266 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
267 }
268 mutex_exit(&sc->sc_mtx);
269
270 cv_destroy(&sc->sc_cv);
271 mutex_destroy(&sc->sc_mtx);
272
273 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
274 USBDEV(sc->sc_dev));
275
276 return 0;
277 }
278
279 int
280 pseye_activate(device_ptr_t self, enum devact act)
281 {
282 struct pseye_softc *sc = device_private(self);
283 int rv;
284
285 rv = 0;
286
287 switch (act) {
288 case DVACT_ACTIVATE:
289 break;
290 case DVACT_DEACTIVATE:
291 sc->sc_dying = 1;
292 break;
293 }
294
295 return rv;
296 }
297
298 static void
299 pseye_childdet(device_t self, device_t child)
300 {
301 struct pseye_softc *sc = device_private(self);
302
303 if (sc->sc_videodev) {
304 KASSERT(sc->sc_videodev == child);
305 sc->sc_videodev = NULL;
306 }
307 }
308
309 /*
310 * Device access
311 */
312
313 static void
314 pseye_init(struct pseye_softc *sc)
315 {
316 pseye_sccb_init(sc);
317
318 pseye_setregv(sc, 0xc2, 0x0c);
319 pseye_setregv(sc, 0x88, 0xf8);
320 pseye_setregv(sc, 0xc3, 0x69);
321 pseye_setregv(sc, 0x89, 0xff);
322 pseye_setregv(sc, 0x76, 0x03);
323 pseye_setregv(sc, 0x92, 0x01);
324 pseye_setregv(sc, 0x93, 0x18);
325 pseye_setregv(sc, 0x94, 0x10);
326 pseye_setregv(sc, 0x95, 0x10);
327 pseye_setregv(sc, 0xe2, 0x00);
328 pseye_setregv(sc, 0xe7, 0x3e);
329
330 pseye_setreg(sc, 0x1c, 0x0a);
331 pseye_setreg(sc, 0x1d, 0x22);
332 pseye_setreg(sc, 0x1d, 0x06);
333 pseye_setregv(sc, 0x96, 0x00);
334
335 pseye_setreg(sc, 0x97, 0x20);
336 pseye_setreg(sc, 0x97, 0x20);
337 pseye_setreg(sc, 0x97, 0x20);
338 pseye_setreg(sc, 0x97, 0x0a);
339 pseye_setreg(sc, 0x97, 0x3f);
340 pseye_setreg(sc, 0x97, 0x4a);
341 pseye_setreg(sc, 0x97, 0x20);
342 pseye_setreg(sc, 0x97, 0x15);
343 pseye_setreg(sc, 0x97, 0x0b);
344
345 pseye_setregv(sc, 0x8e, 0x40);
346 pseye_setregv(sc, 0x1f, 0x81);
347 pseye_setregv(sc, 0x34, 0x05);
348 pseye_setregv(sc, 0xe3, 0x04);
349 pseye_setregv(sc, 0x88, 0x00);
350 pseye_setregv(sc, 0x89, 0x00);
351 pseye_setregv(sc, 0x76, 0x00);
352 pseye_setregv(sc, 0xe7, 0x2e);
353 pseye_setregv(sc, 0x31, 0xf9);
354 pseye_setregv(sc, 0x25, 0x42);
355 pseye_setregv(sc, 0x21, 0xf0);
356
357 pseye_setreg(sc, 0x1c, 0x00);
358 pseye_setreg(sc, 0x1d, 0x40);
359 pseye_setreg(sc, 0x1d, 0x02);
360 pseye_setreg(sc, 0x1d, 0x00);
361 pseye_setreg(sc, 0x1d, 0x02);
362 pseye_setreg(sc, 0x1d, 0x57);
363 pseye_setreg(sc, 0x1d, 0xff);
364
365 pseye_setregv(sc, 0x8d, 0x1c);
366 pseye_setregv(sc, 0x8e, 0x80);
367 pseye_setregv(sc, 0xe5, 0x04);
368
369 pseye_sccb_setreg(sc, 0x12, 0x80);
370 pseye_sccb_setreg(sc, 0x11, 0x01);
371 pseye_sccb_setreg(sc, 0x11, 0x01);
372 pseye_sccb_setreg(sc, 0x11, 0x01);
373 pseye_sccb_setreg(sc, 0x11, 0x01);
374 pseye_sccb_setreg(sc, 0x11, 0x01);
375 pseye_sccb_setreg(sc, 0x11, 0x01);
376 pseye_sccb_setreg(sc, 0x11, 0x01);
377 pseye_sccb_setreg(sc, 0x11, 0x01);
378 pseye_sccb_setreg(sc, 0x11, 0x01);
379 pseye_sccb_setreg(sc, 0x11, 0x01);
380 pseye_sccb_setreg(sc, 0x11, 0x01);
381
382 pseye_sccb_setreg(sc, 0x3d, 0x03);
383 pseye_sccb_setreg(sc, 0x17, 0x26);
384 pseye_sccb_setreg(sc, 0x18, 0xa0);
385 pseye_sccb_setreg(sc, 0x19, 0x07);
386 pseye_sccb_setreg(sc, 0x1a, 0xf0);
387 pseye_sccb_setreg(sc, 0x32, 0x00);
388 pseye_sccb_setreg(sc, 0x29, 0xa0);
389 pseye_sccb_setreg(sc, 0x2c, 0xf0);
390 pseye_sccb_setreg(sc, 0x65, 0x20);
391 pseye_sccb_setreg(sc, 0x11, 0x01);
392 pseye_sccb_setreg(sc, 0x42, 0x7f);
393 pseye_sccb_setreg(sc, 0x63, 0xe0);
394 pseye_sccb_setreg(sc, 0x64, 0xff);
395 pseye_sccb_setreg(sc, 0x66, 0x00);
396 pseye_sccb_setreg(sc, 0x13, 0xf0);
397 pseye_sccb_setreg(sc, 0x0d, 0x41);
398 pseye_sccb_setreg(sc, 0x0f, 0xc5);
399 pseye_sccb_setreg(sc, 0x14, 0x11);
400
401 pseye_sccb_setreg(sc, 0x22, 0x7f);
402 pseye_sccb_setreg(sc, 0x23, 0x03);
403 pseye_sccb_setreg(sc, 0x24, 0x40);
404 pseye_sccb_setreg(sc, 0x25, 0x30);
405 pseye_sccb_setreg(sc, 0x26, 0xa1);
406 pseye_sccb_setreg(sc, 0x2a, 0x00);
407 pseye_sccb_setreg(sc, 0x2b, 0x00);
408 pseye_sccb_setreg(sc, 0x6b, 0xaa);
409 pseye_sccb_setreg(sc, 0x13, 0xff);
410
411 pseye_sccb_setreg(sc, 0x90, 0x05);
412 pseye_sccb_setreg(sc, 0x91, 0x01);
413 pseye_sccb_setreg(sc, 0x92, 0x03);
414 pseye_sccb_setreg(sc, 0x93, 0x00);
415 pseye_sccb_setreg(sc, 0x94, 0x60);
416 pseye_sccb_setreg(sc, 0x95, 0x3c);
417 pseye_sccb_setreg(sc, 0x96, 0x24);
418 pseye_sccb_setreg(sc, 0x97, 0x1e);
419 pseye_sccb_setreg(sc, 0x98, 0x62);
420 pseye_sccb_setreg(sc, 0x99, 0x80);
421 pseye_sccb_setreg(sc, 0x9a, 0x1e);
422 pseye_sccb_setreg(sc, 0x9b, 0x08);
423 pseye_sccb_setreg(sc, 0x9c, 0x20);
424 pseye_sccb_setreg(sc, 0x9e, 0x81);
425
426 pseye_sccb_setreg(sc, 0xa6, 0x04);
427 pseye_sccb_setreg(sc, 0x7e, 0x0c);
428 pseye_sccb_setreg(sc, 0x7f, 0x16);
429
430 pseye_sccb_setreg(sc, 0x80, 0x2a);
431 pseye_sccb_setreg(sc, 0x81, 0x4e);
432 pseye_sccb_setreg(sc, 0x82, 0x61);
433 pseye_sccb_setreg(sc, 0x83, 0x6f);
434 pseye_sccb_setreg(sc, 0x84, 0x7b);
435 pseye_sccb_setreg(sc, 0x85, 0x86);
436 pseye_sccb_setreg(sc, 0x86, 0x8e);
437 pseye_sccb_setreg(sc, 0x87, 0x97);
438 pseye_sccb_setreg(sc, 0x88, 0xa4);
439 pseye_sccb_setreg(sc, 0x89, 0xaf);
440 pseye_sccb_setreg(sc, 0x8a, 0xc5);
441 pseye_sccb_setreg(sc, 0x8b, 0xd7);
442 pseye_sccb_setreg(sc, 0x8c, 0xe8);
443 pseye_sccb_setreg(sc, 0x8d, 0x20);
444
445 pseye_sccb_setreg(sc, 0x0c, 0x90);
446
447 pseye_setregv(sc, 0xc0, 0x50);
448 pseye_setregv(sc, 0xc1, 0x3c);
449 pseye_setregv(sc, 0xc2, 0x0c);
450
451 pseye_sccb_setreg(sc, 0x2b, 0x00);
452 pseye_sccb_setreg(sc, 0x22, 0x7f);
453 pseye_sccb_setreg(sc, 0x23, 0x03);
454 pseye_sccb_setreg(sc, 0x11, 0x01);
455 pseye_sccb_setreg(sc, 0x0c, 0xd0);
456 pseye_sccb_setreg(sc, 0x64, 0xff);
457 pseye_sccb_setreg(sc, 0x0d, 0x41);
458
459 pseye_sccb_setreg(sc, 0x14, 0x41);
460 pseye_sccb_setreg(sc, 0x0e, 0xcd);
461 pseye_sccb_setreg(sc, 0xac, 0xbf);
462 pseye_sccb_setreg(sc, 0x8e, 0x00);
463 pseye_sccb_setreg(sc, 0x0c, 0xd0);
464
465 pseye_stop(sc);
466 }
467
468 static void
469 pseye_sccb_init(struct pseye_softc *sc)
470 {
471 pseye_setregv(sc, 0xe7, 0x3a);
472 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
473 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
474 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
475 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
476 }
477
478 static void
479 pseye_stop(struct pseye_softc *sc)
480 {
481 pseye_led(sc, false);
482 pseye_setreg(sc, 0xe0, 0x09);
483 }
484
485 static void
486 pseye_start(struct pseye_softc *sc)
487 {
488 pseye_led(sc, true);
489 pseye_setreg(sc, 0xe0, 0x00);
490 }
491
492 static void
493 pseye_led(struct pseye_softc *sc, bool enabled)
494 {
495 uint8_t val;
496
497 val = pseye_getreg(sc, 0x21);
498 pseye_setreg(sc, 0x21, val | 0x80);
499
500 val = pseye_getreg(sc, 0x23);
501 if (enabled == true)
502 val |= 0x80;
503 else
504 val &= ~0x80;
505 pseye_setreg(sc, 0x23, val);
506 }
507
508 static uint8_t
509 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
510 {
511 usb_device_request_t req;
512 usbd_status err;
513 uint8_t buf;
514
515 req.bmRequestType = UT_READ_VENDOR_DEVICE;
516 req.bRequest = 1;
517 USETW(req.wValue, 0x0000);
518 USETW(req.wIndex, reg);
519 USETW(req.wLength, 1);
520
521 err = usbd_do_request(sc->sc_udev, &req, &buf);
522 if (err) {
523 aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
524 reg, usbd_errstr(err));
525 return 0xff;
526 }
527
528 return buf;
529 }
530
531 static void
532 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
533 {
534 usb_device_request_t req;
535 usbd_status err;
536
537 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
538 req.bRequest = 1;
539 USETW(req.wValue, 0x0000);
540 USETW(req.wIndex, reg);
541 USETW(req.wLength, 1);
542
543 err = usbd_do_request(sc->sc_udev, &req, &val);
544 if (err)
545 aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
546 reg, usbd_errstr(err));
547 }
548
549 static void
550 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
551 {
552 pseye_setreg(sc, reg, val);
553 if (pseye_getreg(sc, reg) != val)
554 aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
555 reg);
556 }
557
558 static void
559 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
560 {
561 pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
562 pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
563 pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
564
565 if (pseye_sccb_status(sc) == false)
566 aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
567 reg);
568 }
569
570 static bool
571 pseye_sccb_status(struct pseye_softc *sc)
572 {
573 int retry = 5;
574 uint8_t reg;
575
576 while (retry-- >= 0) {
577 reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
578 if (reg == 0x00)
579 return true;
580 else if (reg == 0x04)
581 return false;
582 }
583
584 aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
585 return false;
586 }
587
588 static usbd_status
589 pseye_get_frame(struct pseye_softc *sc)
590 {
591 uint32_t len = sc->sc_bulkin_bufferlen;
592
593 if (sc->sc_dying)
594 return USBD_IOERROR;
595
596 return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
597 USBD_SHORT_XFER_OK|USBD_NO_COPY, 50,
598 sc->sc_bulkin_buffer, &len, "pseyerb");
599 }
600
601 static int
602 pseye_init_pipes(struct pseye_softc *sc)
603 {
604 usbd_status err;
605
606 if (sc->sc_dying)
607 return EIO;
608
609 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
610 &sc->sc_bulkin_pipe);
611 if (err) {
612 aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
613 usbd_errstr(err));
614 return ENOMEM;
615 }
616
617 pseye_start(sc);
618
619 return 0;
620 }
621
622 int
623 pseye_close_pipes(struct pseye_softc *sc)
624 {
625 if (sc->sc_bulkin_pipe != NULL) {
626 usbd_abort_pipe(sc->sc_bulkin_pipe);
627 usbd_close_pipe(sc->sc_bulkin_pipe);
628 sc->sc_bulkin_pipe = NULL;
629 }
630
631 pseye_stop(sc);
632
633 return 0;
634 }
635
636 static void
637 pseye_transfer_thread(void *opaque)
638 {
639 struct pseye_softc *sc = opaque;
640 int error;
641 struct video_payload payload;
642
643 payload.frameno = 0;
644
645 while (sc->sc_running) {
646 error = pseye_get_frame(sc);
647 if (error == USBD_NORMAL_COMPLETION) {
648 payload.data = sc->sc_bulkin_buffer;
649 payload.size = sc->sc_bulkin_bufferlen;
650 payload.frameno = (payload.frameno + 1) & 1;
651 payload.end_of_frame = 1;
652 video_submit_payload(sc->sc_videodev, &payload);
653 }
654 }
655
656 mutex_enter(&sc->sc_mtx);
657 cv_broadcast(&sc->sc_cv);
658 mutex_exit(&sc->sc_mtx);
659
660 kthread_exit(0);
661 }
662
663 /* video(9) API implementations */
664 static int
665 pseye_open(void *opaque, int flags)
666 {
667 struct pseye_softc *sc = opaque;
668
669 if (sc->sc_dying)
670 return EIO;
671
672 return pseye_init_pipes(sc);
673 }
674
675 static void
676 pseye_close(void *opaque)
677 {
678 struct pseye_softc *sc = opaque;
679
680 pseye_close_pipes(sc);
681 }
682
683 static const char *
684 pseye_get_devname(void *opaque)
685 {
686 return "PLAYSTATION(R) Eye";
687 }
688
689 static int
690 pseye_get_format(void *opaque, struct video_format *format)
691 {
692 format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
693 format->width = 640;
694 format->height = 480;
695 format->aspect_x = 4;
696 format->aspect_y = 3;
697 format->sample_size = format->width * format->height * 2;
698 format->stride = format->width * 2;
699 format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
700 format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
701 format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
702 format->interlace_flags = VIDEO_INTERLACE_ON;
703 format->priv = 0;
704
705 return 0;
706 }
707
708 static int
709 pseye_set_format(void *opaque, struct video_format *format)
710 {
711 #if notyet
712 if (format->pixel_format != VIDEO_FORMAT_YUYV)
713 return EINVAL;
714 if (format->width != 640 || format->height != 480)
715 return EINVAL;
716 #endif
717 /* XXX */
718 return pseye_get_format(opaque, format);
719 }
720
721 static int
722 pseye_start_transfer(void *opaque)
723 {
724 struct pseye_softc *sc = opaque;
725 int err = 0;
726
727 mutex_enter(&sc->sc_mtx);
728 if (sc->sc_running == 0) {
729 sc->sc_running = 1;
730 err = kthread_create(PRI_NONE, 0, NULL, pseye_transfer_thread,
731 opaque, NULL, device_xname(sc->sc_dev));
732 } else
733 aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
734 mutex_exit(&sc->sc_mtx);
735
736 return 0;
737 }
738
739 static int
740 pseye_stop_transfer(void *opaque)
741 {
742 struct pseye_softc *sc = opaque;
743
744 mutex_enter(&sc->sc_mtx);
745 if (sc->sc_running) {
746 sc->sc_running = 0;
747 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
748 }
749 mutex_exit(&sc->sc_mtx);
750
751 return 0;
752 }
753