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