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