pseye.c revision 1.18 1 /* $NetBSD: pseye.c,v 1.18 2010/11/13 13:52:12 uebayasi 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 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.18 2010/11/13 13:52:12 uebayasi 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 #include <sys/module.h>
54
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdevs.h>
59 #include <dev/usb/uvideoreg.h>
60
61 #include <dev/video_if.h>
62
63 #define PRI_PSEYE PRI_BIO
64
65 /* Bulk-in buffer length -- make room for payload + UVC headers */
66 #define PSEYE_BULKIN_BUFLEN ((640 * 480 * 2) + 4096)
67 #define PSEYE_BULKIN_BLKLEN 2048
68
69 /* SCCB/sensor interface */
70 #define PSEYE_SCCB_ADDRESS 0xf1
71 #define PSEYE_SCCB_SUBADDR 0xf2
72 #define PSEYE_SCCB_WRITE 0xf3
73 #define PSEYE_SCCB_READ 0xf4
74 #define PSEYE_SCCB_OPERATION 0xf5
75 #define PSEYE_SCCB_STATUS 0xf6
76
77 #define PSEYE_SCCB_OP_WRITE_3 0x37
78 #define PSEYE_SCCB_OP_WRITE_2 0x33
79 #define PSEYE_SCCB_OP_READ_2 0xf9
80
81 struct pseye_softc {
82 device_t sc_dev;
83
84 usbd_device_handle sc_udev;
85 usbd_interface_handle sc_iface;
86
87 device_t sc_videodev;
88 char sc_running;
89
90 kcondvar_t sc_cv;
91 kmutex_t sc_mtx;
92
93 usbd_pipe_handle sc_bulkin_pipe;
94 usbd_xfer_handle sc_bulkin_xfer;
95 int sc_bulkin;
96 uint8_t *sc_bulkin_buffer;
97 int sc_bulkin_bufferlen;
98
99 char sc_dying;
100 };
101
102 static int pseye_match(device_t, cfdata_t, void *);
103 static void pseye_attach(device_t, device_t, void *);
104 static int pseye_detach(device_t, int);
105 static void pseye_childdet(device_t, device_t);
106 static int pseye_activate(device_t, enum devact);
107
108 static void pseye_init(struct pseye_softc *);
109 static void pseye_sccb_init(struct pseye_softc *);
110 static void pseye_stop(struct pseye_softc *);
111 static void pseye_start(struct pseye_softc *);
112 static void pseye_led(struct pseye_softc *, bool);
113 static uint8_t pseye_getreg(struct pseye_softc *, uint16_t);
114 static void pseye_setreg(struct pseye_softc *, uint16_t, uint8_t);
115 static void pseye_setregv(struct pseye_softc *, uint16_t, uint8_t);
116 static void pseye_sccb_setreg(struct pseye_softc *, uint8_t, uint8_t);
117 static bool pseye_sccb_status(struct pseye_softc *);
118
119 static int pseye_init_pipes(struct pseye_softc *);
120 static int pseye_close_pipes(struct pseye_softc *);
121
122 static usbd_status pseye_get_frame(struct pseye_softc *, uint32_t *);
123 static void pseye_submit_payload(struct pseye_softc *, uint32_t);
124
125 /* video(9) API */
126 static int pseye_open(void *, int);
127 static void pseye_close(void *);
128 static const char * pseye_get_devname(void *);
129 static int pseye_enum_format(void *, uint32_t,
130 struct video_format *);
131 static int pseye_get_format(void *, struct video_format *);
132 static int pseye_set_format(void *, struct video_format *);
133 static int pseye_try_format(void *, struct video_format *);
134 static int pseye_start_transfer(void *);
135 static int pseye_stop_transfer(void *);
136
137 CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
138 pseye_match, pseye_attach, pseye_detach, pseye_activate,
139 NULL, pseye_childdet);
140
141 static const struct video_hw_if pseye_hw_if = {
142 .open = pseye_open,
143 .close = pseye_close,
144 .get_devname = pseye_get_devname,
145 .enum_format = pseye_enum_format,
146 .get_format = pseye_get_format,
147 .set_format = pseye_set_format,
148 .try_format = pseye_try_format,
149 .start_transfer = pseye_start_transfer,
150 .stop_transfer = pseye_stop_transfer,
151 .control_iter_init = NULL,
152 .control_iter_next = NULL,
153 .get_control_desc_group = NULL,
154 .get_control_group = NULL,
155 .set_control_group = NULL,
156 };
157
158 static int
159 pseye_match(device_t parent, cfdata_t match, void *opaque)
160 {
161 struct usbif_attach_arg *uaa = opaque;
162
163 if (uaa->class != UICLASS_VENDOR)
164 return UMATCH_NONE;
165
166 if (uaa->vendor == USB_VENDOR_OMNIVISION2) {
167 switch (uaa->product) {
168 case USB_PRODUCT_OMNIVISION2_PSEYE:
169 if (uaa->ifaceno != 0)
170 return UMATCH_NONE;
171 return UMATCH_VENDOR_PRODUCT;
172 }
173 }
174
175 return UMATCH_NONE;
176 }
177
178 static void
179 pseye_attach(device_t parent, device_t self, void *opaque)
180 {
181 struct pseye_softc *sc = device_private(self);
182 struct usbif_attach_arg *uaa = opaque;
183 usbd_device_handle dev = uaa->device;
184 usb_interface_descriptor_t *id = NULL;
185 usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
186 char *devinfop;
187 int i;
188
189 aprint_naive("\n");
190 aprint_normal("\n");
191
192 devinfop = usbd_devinfo_alloc(dev, 0);
193 aprint_normal_dev(self, "%s\n", devinfop);
194 usbd_devinfo_free(devinfop);
195
196 sc->sc_dev = self;
197 sc->sc_udev = dev;
198 sc->sc_iface = uaa->iface;
199 sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;
200
201 sc->sc_dying = sc->sc_running = 0;
202 cv_init(&sc->sc_cv, device_xname(self));
203 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
204
205 id = usbd_get_interface_descriptor(sc->sc_iface);
206 if (id == NULL) {
207 aprint_error_dev(self, "failed to get interface descriptor\n");
208 sc->sc_dying = 1;
209 return;
210 }
211
212 for (i = 0; i < id->bNumEndpoints; i++) {
213 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
214 if (ed == NULL) {
215 aprint_error_dev(self, "couldn't get ep %d\n", i);
216 sc->sc_dying = 1;
217 return;
218 }
219
220 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
221 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
222 ed_bulkin = ed;
223 break;
224 }
225 }
226
227 if (ed_bulkin == NULL) {
228 aprint_error_dev(self, "no bulk-in endpoint found\n");
229 sc->sc_dying = 1;
230 return;
231 }
232
233 sc->sc_bulkin = ed_bulkin->bEndpointAddress;
234
235 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
236 if (sc->sc_bulkin_xfer == NULL) {
237 sc->sc_dying = 1;
238 return;
239 }
240 sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
241 sc->sc_bulkin_bufferlen);
242 if (sc->sc_bulkin_buffer == NULL) {
243 usbd_free_xfer(sc->sc_bulkin_xfer);
244 sc->sc_bulkin_xfer = NULL;
245 sc->sc_dying = 1;
246 return;
247 }
248
249 pseye_init(sc);
250
251 if (!pmf_device_register(self, NULL, NULL))
252 aprint_error_dev(self, "couldn't establish power handler\n");
253
254 sc->sc_videodev = video_attach_mi(&pseye_hw_if, self);
255 if (sc->sc_videodev == NULL) {
256 aprint_error_dev(self, "couldn't attach video layer\n");
257 sc->sc_dying = 1;
258 return;
259 }
260
261 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
262 self);
263
264 }
265
266 static int
267 pseye_detach(device_t self, int flags)
268 {
269 struct pseye_softc *sc = device_private(self);
270
271 sc->sc_dying = 1;
272
273 pmf_device_deregister(self);
274
275 if (sc->sc_videodev != NULL) {
276 config_detach(sc->sc_videodev, flags);
277 sc->sc_videodev = NULL;
278 }
279
280 if (sc->sc_bulkin_xfer != NULL) {
281 usbd_free_xfer(sc->sc_bulkin_xfer);
282 sc->sc_bulkin_xfer = NULL;
283 }
284
285 if (sc->sc_bulkin_pipe != NULL) {
286 usbd_abort_pipe(sc->sc_bulkin_pipe);
287 sc->sc_bulkin_pipe = NULL;
288 }
289
290 mutex_enter(&sc->sc_mtx);
291 if (sc->sc_running) {
292 sc->sc_running = 0;
293 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
294 }
295 mutex_exit(&sc->sc_mtx);
296
297 cv_destroy(&sc->sc_cv);
298 mutex_destroy(&sc->sc_mtx);
299
300 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
301 sc->sc_dev);
302
303 return 0;
304 }
305
306 int
307 pseye_activate(device_t self, enum devact act)
308 {
309 struct pseye_softc *sc = device_private(self);
310
311 switch (act) {
312 case DVACT_DEACTIVATE:
313 sc->sc_dying = 1;
314 return 0;
315 default:
316 return EOPNOTSUPP;
317 }
318 }
319
320 static void
321 pseye_childdet(device_t self, device_t child)
322 {
323 struct pseye_softc *sc = device_private(self);
324
325 if (sc->sc_videodev) {
326 KASSERT(sc->sc_videodev == child);
327 sc->sc_videodev = NULL;
328 }
329 }
330
331 /*
332 * Device access
333 */
334
335 static void
336 pseye_init(struct pseye_softc *sc)
337 {
338 pseye_sccb_init(sc);
339
340 pseye_setregv(sc, 0xc2, 0x0c);
341 pseye_setregv(sc, 0x88, 0xf8);
342 pseye_setregv(sc, 0xc3, 0x69);
343 pseye_setregv(sc, 0x89, 0xff);
344 pseye_setregv(sc, 0x76, 0x03);
345 pseye_setregv(sc, 0x92, 0x01);
346 pseye_setregv(sc, 0x93, 0x18);
347 pseye_setregv(sc, 0x94, 0x10);
348 pseye_setregv(sc, 0x95, 0x10);
349 pseye_setregv(sc, 0xe2, 0x00);
350 pseye_setregv(sc, 0xe7, 0x3e);
351
352 pseye_setregv(sc, 0x96, 0x00);
353
354 pseye_setreg(sc, 0x97, 0x20);
355 pseye_setreg(sc, 0x97, 0x20);
356 pseye_setreg(sc, 0x97, 0x20);
357 pseye_setreg(sc, 0x97, 0x0a);
358 pseye_setreg(sc, 0x97, 0x3f);
359 pseye_setreg(sc, 0x97, 0x4a);
360 pseye_setreg(sc, 0x97, 0x20);
361 pseye_setreg(sc, 0x97, 0x15);
362 pseye_setreg(sc, 0x97, 0x0b);
363
364 pseye_setregv(sc, 0x8e, 0x40);
365 pseye_setregv(sc, 0x1f, 0x81);
366 pseye_setregv(sc, 0x34, 0x05);
367 pseye_setregv(sc, 0xe3, 0x04);
368 pseye_setregv(sc, 0x88, 0x00);
369 pseye_setregv(sc, 0x89, 0x00);
370 pseye_setregv(sc, 0x76, 0x00);
371 pseye_setregv(sc, 0xe7, 0x2e);
372 pseye_setregv(sc, 0x31, 0xf9);
373 pseye_setregv(sc, 0x25, 0x42);
374 pseye_setregv(sc, 0x21, 0xf0);
375
376 pseye_setreg(sc, 0x1c, 0x00);
377 pseye_setreg(sc, 0x1d, 0x40);
378 pseye_setreg(sc, 0x1d, 0x02); /* payload size 0x0200 * 4 == 2048 */
379 pseye_setreg(sc, 0x1d, 0x00);
380 pseye_setreg(sc, 0x1d, 0x02); /* frame size 0x025800 * 4 == 614400 */
381 pseye_setreg(sc, 0x1d, 0x58);
382 pseye_setreg(sc, 0x1d, 0x00);
383
384 pseye_setreg(sc, 0x1c, 0x0a);
385 pseye_setreg(sc, 0x1d, 0x08); /* enable UVC header */
386 pseye_setreg(sc, 0x1d, 0x0e);
387
388 pseye_setregv(sc, 0x8d, 0x1c);
389 pseye_setregv(sc, 0x8e, 0x80);
390 pseye_setregv(sc, 0xe5, 0x04);
391
392 pseye_sccb_setreg(sc, 0x12, 0x80);
393 pseye_sccb_setreg(sc, 0x11, 0x01);
394 pseye_sccb_setreg(sc, 0x11, 0x01);
395 pseye_sccb_setreg(sc, 0x11, 0x01);
396 pseye_sccb_setreg(sc, 0x11, 0x01);
397 pseye_sccb_setreg(sc, 0x11, 0x01);
398 pseye_sccb_setreg(sc, 0x11, 0x01);
399 pseye_sccb_setreg(sc, 0x11, 0x01);
400 pseye_sccb_setreg(sc, 0x11, 0x01);
401 pseye_sccb_setreg(sc, 0x11, 0x01);
402 pseye_sccb_setreg(sc, 0x11, 0x01);
403 pseye_sccb_setreg(sc, 0x11, 0x01);
404
405 pseye_sccb_setreg(sc, 0x3d, 0x03);
406 pseye_sccb_setreg(sc, 0x17, 0x26);
407 pseye_sccb_setreg(sc, 0x18, 0xa0);
408 pseye_sccb_setreg(sc, 0x19, 0x07);
409 pseye_sccb_setreg(sc, 0x1a, 0xf0);
410 pseye_sccb_setreg(sc, 0x32, 0x00);
411 pseye_sccb_setreg(sc, 0x29, 0xa0);
412 pseye_sccb_setreg(sc, 0x2c, 0xf0);
413 pseye_sccb_setreg(sc, 0x65, 0x20);
414 pseye_sccb_setreg(sc, 0x11, 0x01);
415 pseye_sccb_setreg(sc, 0x42, 0x7f);
416 pseye_sccb_setreg(sc, 0x63, 0xe0);
417 pseye_sccb_setreg(sc, 0x64, 0xff);
418 pseye_sccb_setreg(sc, 0x66, 0x00);
419 pseye_sccb_setreg(sc, 0x13, 0xf0);
420 pseye_sccb_setreg(sc, 0x0d, 0x41);
421 pseye_sccb_setreg(sc, 0x0f, 0xc5);
422 pseye_sccb_setreg(sc, 0x14, 0x11);
423
424 pseye_sccb_setreg(sc, 0x22, 0x7f);
425 pseye_sccb_setreg(sc, 0x23, 0x03);
426 pseye_sccb_setreg(sc, 0x24, 0x40);
427 pseye_sccb_setreg(sc, 0x25, 0x30);
428 pseye_sccb_setreg(sc, 0x26, 0xa1);
429 pseye_sccb_setreg(sc, 0x2a, 0x00);
430 pseye_sccb_setreg(sc, 0x2b, 0x00);
431 pseye_sccb_setreg(sc, 0x6b, 0xaa);
432 pseye_sccb_setreg(sc, 0x13, 0xff);
433
434 pseye_sccb_setreg(sc, 0x90, 0x05);
435 pseye_sccb_setreg(sc, 0x91, 0x01);
436 pseye_sccb_setreg(sc, 0x92, 0x03);
437 pseye_sccb_setreg(sc, 0x93, 0x00);
438 pseye_sccb_setreg(sc, 0x94, 0x60);
439 pseye_sccb_setreg(sc, 0x95, 0x3c);
440 pseye_sccb_setreg(sc, 0x96, 0x24);
441 pseye_sccb_setreg(sc, 0x97, 0x1e);
442 pseye_sccb_setreg(sc, 0x98, 0x62);
443 pseye_sccb_setreg(sc, 0x99, 0x80);
444 pseye_sccb_setreg(sc, 0x9a, 0x1e);
445 pseye_sccb_setreg(sc, 0x9b, 0x08);
446 pseye_sccb_setreg(sc, 0x9c, 0x20);
447 pseye_sccb_setreg(sc, 0x9e, 0x81);
448
449 pseye_sccb_setreg(sc, 0xa6, 0x04);
450 pseye_sccb_setreg(sc, 0x7e, 0x0c);
451 pseye_sccb_setreg(sc, 0x7f, 0x16);
452
453 pseye_sccb_setreg(sc, 0x80, 0x2a);
454 pseye_sccb_setreg(sc, 0x81, 0x4e);
455 pseye_sccb_setreg(sc, 0x82, 0x61);
456 pseye_sccb_setreg(sc, 0x83, 0x6f);
457 pseye_sccb_setreg(sc, 0x84, 0x7b);
458 pseye_sccb_setreg(sc, 0x85, 0x86);
459 pseye_sccb_setreg(sc, 0x86, 0x8e);
460 pseye_sccb_setreg(sc, 0x87, 0x97);
461 pseye_sccb_setreg(sc, 0x88, 0xa4);
462 pseye_sccb_setreg(sc, 0x89, 0xaf);
463 pseye_sccb_setreg(sc, 0x8a, 0xc5);
464 pseye_sccb_setreg(sc, 0x8b, 0xd7);
465 pseye_sccb_setreg(sc, 0x8c, 0xe8);
466 pseye_sccb_setreg(sc, 0x8d, 0x20);
467
468 pseye_sccb_setreg(sc, 0x0c, 0x90);
469
470 pseye_setregv(sc, 0xc0, 0x50);
471 pseye_setregv(sc, 0xc1, 0x3c);
472 pseye_setregv(sc, 0xc2, 0x0c);
473
474 pseye_sccb_setreg(sc, 0x2b, 0x00);
475 pseye_sccb_setreg(sc, 0x22, 0x7f);
476 pseye_sccb_setreg(sc, 0x23, 0x03);
477 pseye_sccb_setreg(sc, 0x11, 0x01);
478 pseye_sccb_setreg(sc, 0x0c, 0xd0);
479 pseye_sccb_setreg(sc, 0x64, 0xff);
480 pseye_sccb_setreg(sc, 0x0d, 0x41);
481
482 pseye_sccb_setreg(sc, 0x14, 0x41);
483 pseye_sccb_setreg(sc, 0x0e, 0xcd);
484 pseye_sccb_setreg(sc, 0xac, 0xbf);
485 pseye_sccb_setreg(sc, 0x8e, 0x00);
486 pseye_sccb_setreg(sc, 0x0c, 0xd0);
487
488 pseye_stop(sc);
489 }
490
491 static void
492 pseye_sccb_init(struct pseye_softc *sc)
493 {
494 pseye_setregv(sc, 0xe7, 0x3a);
495 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
496 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
497 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
498 pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
499 }
500
501 static void
502 pseye_stop(struct pseye_softc *sc)
503 {
504 pseye_led(sc, false);
505 pseye_setreg(sc, 0xe0, 0x09);
506 }
507
508 static void
509 pseye_start(struct pseye_softc *sc)
510 {
511 pseye_led(sc, true);
512 pseye_setreg(sc, 0xe0, 0x00);
513 }
514
515 static void
516 pseye_led(struct pseye_softc *sc, bool enabled)
517 {
518 uint8_t val;
519
520 val = pseye_getreg(sc, 0x21);
521 pseye_setreg(sc, 0x21, val | 0x80);
522
523 val = pseye_getreg(sc, 0x23);
524 if (enabled == true)
525 val |= 0x80;
526 else
527 val &= ~0x80;
528 pseye_setreg(sc, 0x23, val);
529 }
530
531 static uint8_t
532 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
533 {
534 usb_device_request_t req;
535 usbd_status err;
536 uint8_t buf;
537
538 req.bmRequestType = UT_READ_VENDOR_DEVICE;
539 req.bRequest = 1;
540 USETW(req.wValue, 0x0000);
541 USETW(req.wIndex, reg);
542 USETW(req.wLength, 1);
543
544 err = usbd_do_request(sc->sc_udev, &req, &buf);
545 if (err) {
546 aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
547 reg, usbd_errstr(err));
548 return 0xff;
549 }
550
551 return buf;
552 }
553
554 static void
555 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
556 {
557 usb_device_request_t req;
558 usbd_status err;
559
560 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
561 req.bRequest = 1;
562 USETW(req.wValue, 0x0000);
563 USETW(req.wIndex, reg);
564 USETW(req.wLength, 1);
565
566 err = usbd_do_request(sc->sc_udev, &req, &val);
567 if (err)
568 aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
569 reg, usbd_errstr(err));
570 }
571
572 static void
573 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
574 {
575 pseye_setreg(sc, reg, val);
576 if (pseye_getreg(sc, reg) != val)
577 aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
578 reg);
579 }
580
581 static void
582 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
583 {
584 pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
585 pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
586 pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
587
588 if (pseye_sccb_status(sc) == false)
589 aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
590 reg);
591 }
592
593 static bool
594 pseye_sccb_status(struct pseye_softc *sc)
595 {
596 int retry = 5;
597 uint8_t reg;
598
599 while (retry-- >= 0) {
600 reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
601 if (reg == 0x00)
602 return true;
603 else if (reg == 0x04)
604 return false;
605 }
606
607 aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
608 return false;
609 }
610
611 static usbd_status
612 pseye_get_frame(struct pseye_softc *sc, uint32_t *plen)
613 {
614 if (sc->sc_dying)
615 return USBD_IOERROR;
616
617 return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
618 USBD_SHORT_XFER_OK|USBD_NO_COPY, 1000,
619 sc->sc_bulkin_buffer, plen, "pseyerb");
620 }
621
622 static int
623 pseye_init_pipes(struct pseye_softc *sc)
624 {
625 usbd_status err;
626
627 if (sc->sc_dying)
628 return EIO;
629
630 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
631 &sc->sc_bulkin_pipe);
632 if (err) {
633 aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
634 usbd_errstr(err));
635 return ENOMEM;
636 }
637
638 pseye_start(sc);
639
640 return 0;
641 }
642
643 int
644 pseye_close_pipes(struct pseye_softc *sc)
645 {
646 if (sc->sc_bulkin_pipe != NULL) {
647 usbd_abort_pipe(sc->sc_bulkin_pipe);
648 usbd_close_pipe(sc->sc_bulkin_pipe);
649 sc->sc_bulkin_pipe = NULL;
650 }
651
652 pseye_stop(sc);
653
654 return 0;
655 }
656
657 static void
658 pseye_submit_payload(struct pseye_softc *sc, uint32_t tlen)
659 {
660 struct video_payload payload;
661 uvideo_payload_header_t *uvchdr;
662 uint8_t *buf = sc->sc_bulkin_buffer;
663 uint32_t len;
664 uint32_t brem = (640*480*2);
665
666 while (brem > 0 && tlen > 0) {
667 len = min(tlen, PSEYE_BULKIN_BLKLEN);
668 if (len < UVIDEO_PAYLOAD_HEADER_SIZE) {
669 printf("pseye_submit_payload: len=%u\n", len);
670 return;
671 }
672
673 uvchdr = (uvideo_payload_header_t *)buf;
674 if (uvchdr->bHeaderLength != UVIDEO_PAYLOAD_HEADER_SIZE)
675 goto next;
676 if (uvchdr->bHeaderLength == len &&
677 !(uvchdr->bmHeaderInfo & UV_END_OF_FRAME))
678 goto next;
679 if (uvchdr->bmHeaderInfo & UV_ERROR)
680 return;
681 if ((uvchdr->bmHeaderInfo & UV_PRES_TIME) == 0)
682 goto next;
683
684 payload.data = buf + uvchdr->bHeaderLength;
685 payload.size = min(brem, len - uvchdr->bHeaderLength);
686 payload.frameno = UGETDW(&buf[2]);
687 payload.end_of_frame = uvchdr->bmHeaderInfo & UV_END_OF_FRAME;
688 video_submit_payload(sc->sc_videodev, &payload);
689
690 next:
691 tlen -= len;
692 buf += len;
693 brem -= payload.size;
694 }
695 }
696
697 static void
698 pseye_transfer_thread(void *opaque)
699 {
700 struct pseye_softc *sc = opaque;
701 uint32_t len;
702 int error;
703
704 while (sc->sc_running) {
705 len = sc->sc_bulkin_bufferlen;
706 error = pseye_get_frame(sc, &len);
707 if (error == USBD_NORMAL_COMPLETION)
708 pseye_submit_payload(sc, len);
709 }
710
711 mutex_enter(&sc->sc_mtx);
712 cv_broadcast(&sc->sc_cv);
713 mutex_exit(&sc->sc_mtx);
714
715 kthread_exit(0);
716 }
717
718 /* video(9) API implementations */
719 static int
720 pseye_open(void *opaque, int flags)
721 {
722 struct pseye_softc *sc = opaque;
723
724 if (sc->sc_dying)
725 return EIO;
726
727 return pseye_init_pipes(sc);
728 }
729
730 static void
731 pseye_close(void *opaque)
732 {
733 struct pseye_softc *sc = opaque;
734
735 pseye_close_pipes(sc);
736 }
737
738 static const char *
739 pseye_get_devname(void *opaque)
740 {
741 return "PlayStation Eye";
742 }
743
744 static int
745 pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
746 {
747 if (index != 0)
748 return EINVAL;
749 return pseye_get_format(opaque, format);
750 }
751
752 static int
753 pseye_get_format(void *opaque, struct video_format *format)
754 {
755 format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
756 format->width = 640;
757 format->height = 480;
758 format->aspect_x = 4;
759 format->aspect_y = 3;
760 format->sample_size = format->width * format->height * 2;
761 format->stride = format->width * 2;
762 format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
763 format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
764 format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
765 format->interlace_flags = VIDEO_INTERLACE_ON;
766 format->priv = 0;
767
768 return 0;
769 }
770
771 static int
772 pseye_set_format(void *opaque, struct video_format *format)
773 {
774 #if notyet
775 if (format->pixel_format != VIDEO_FORMAT_YUYV)
776 return EINVAL;
777 if (format->width != 640 || format->height != 480)
778 return EINVAL;
779 #endif
780 /* XXX */
781 return pseye_get_format(opaque, format);
782 }
783
784 static int
785 pseye_try_format(void *opaque, struct video_format *format)
786 {
787 return pseye_get_format(opaque, format);
788 }
789
790 static int
791 pseye_start_transfer(void *opaque)
792 {
793 struct pseye_softc *sc = opaque;
794 int err = 0;
795
796 mutex_enter(&sc->sc_mtx);
797 if (sc->sc_running == 0) {
798 sc->sc_running = 1;
799 err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
800 opaque, NULL, device_xname(sc->sc_dev));
801 } else
802 aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
803 mutex_exit(&sc->sc_mtx);
804
805 return err;
806 }
807
808 static int
809 pseye_stop_transfer(void *opaque)
810 {
811 struct pseye_softc *sc = opaque;
812
813 mutex_enter(&sc->sc_mtx);
814 if (sc->sc_running) {
815 sc->sc_running = 0;
816 cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
817 }
818 mutex_exit(&sc->sc_mtx);
819
820 return 0;
821 }
822
823 #ifdef _MODULE
824
825 MODULE(MODULE_CLASS_DRIVER, pseye, NULL);
826
827 static const struct cfiattrdata videobuscf_iattrdata = {
828 "videobus", 0, { { NULL, NULL, 0 }, }
829 };
830 static const struct cfiattrdata * const pseye_attrs[] = {
831 &videobuscf_iattrdata, NULL
832 };
833 CFDRIVER_DECL(pseye, DV_DULL, pseye_attrs);
834 extern struct cfattach video_ca;
835 static int pseyeloc[6] = { -1, -1, -1, -1, -1, -1 };
836 static struct cfparent uhubparent = {
837 "usbifif", NULL, DVUNIT_ANY
838 };
839 static struct cfdata pseye_cfdata[] = {
840 {
841 .cf_name = "pseye",
842 .cf_atname = "pseye",
843 .cf_unit = 0,
844 .cf_fstate = FSTATE_STAR,
845 .cf_loc = pseyeloc,
846 .cf_flags = 0,
847 .cf_pspec = &uhubparent,
848 },
849 { NULL, NULL, 0, 0, NULL, 0, NULL }
850 };
851
852 static int
853 pseye_modcmd(modcmd_t cmd, void *opaque)
854 {
855 int err;
856
857 switch (cmd) {
858 case MODULE_CMD_INIT:
859 err = config_cfdriver_attach(&pseye_cd);
860 if (err)
861 return err;
862 err = config_cfattach_attach("pseye", &pseye_ca);
863 if (err) {
864 config_cfdriver_detach(&pseye_cd);
865 return err;
866 }
867 err = config_cfdata_attach(pseye_cfdata, 1);
868 if (err) {
869 config_cfattach_detach("pseye", &pseye_ca);
870 config_cfdriver_detach(&pseye_cd);
871 return err;
872 }
873 return 0;
874 case MODULE_CMD_FINI:
875 err = config_cfdata_detach(pseye_cfdata);
876 if (err)
877 return err;
878 config_cfattach_detach("pseye", &pseye_ca);
879 config_cfdriver_detach(&pseye_cd);
880 return 0;
881 default:
882 return ENOTTY;
883 }
884 }
885
886 #endif /* !_MODULE */
887