Home | History | Annotate | Line # | Download | only in usb
pseye.c revision 1.9.2.2
      1 /* $NetBSD: pseye.c,v 1.9.2.2 2008/09/28 10:40:33 mjf 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.9.2.2 2008/09/28 10:40:33 mjf 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(self));
    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(self, "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(self, "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 	if (!pmf_device_register(self, NULL, NULL))
    245 		aprint_error_dev(self, "couldn't establish power handler\n");
    246 
    247 	sc->sc_videodev = video_attach_mi(&pseye_hw_if, self);
    248 	if (sc->sc_videodev == NULL) {
    249 		aprint_error_dev(self, "couldn't attach video layer\n");
    250 		sc->sc_dying = 1;
    251 		USB_ATTACH_ERROR_RETURN;
    252 	}
    253 
    254 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    255 	    USBDEV(self));
    256 
    257 	USB_ATTACH_SUCCESS_RETURN;
    258 }
    259 
    260 USB_DETACH(pseye)
    261 {
    262 	USB_DETACH_START(pseye, sc);
    263 
    264 	sc->sc_dying = 1;
    265 
    266 	pmf_device_deregister(self);
    267 
    268 	if (sc->sc_videodev != NULL) {
    269 		config_detach(sc->sc_videodev, flags);
    270 		sc->sc_videodev = NULL;
    271 	}
    272 
    273 	if (sc->sc_bulkin_xfer != NULL) {
    274 		usbd_free_xfer(sc->sc_bulkin_xfer);
    275 		sc->sc_bulkin_xfer = NULL;
    276 	}
    277 
    278 	if (sc->sc_bulkin_pipe != NULL) {
    279 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    280 		sc->sc_bulkin_pipe = NULL;
    281 	}
    282 
    283 	mutex_enter(&sc->sc_mtx);
    284 	if (sc->sc_running) {
    285 		sc->sc_running = 0;
    286 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    287 	}
    288 	mutex_exit(&sc->sc_mtx);
    289 
    290 	cv_destroy(&sc->sc_cv);
    291 	mutex_destroy(&sc->sc_mtx);
    292 
    293 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    294 	    USBDEV(sc->sc_dev));
    295 
    296 	return 0;
    297 }
    298 
    299 int
    300 pseye_activate(device_ptr_t self, enum devact act)
    301 {
    302 	struct pseye_softc *sc = device_private(self);
    303 	int rv;
    304 
    305 	rv = 0;
    306 
    307 	switch (act) {
    308 	case DVACT_ACTIVATE:
    309 		break;
    310 	case DVACT_DEACTIVATE:
    311 		sc->sc_dying = 1;
    312 		break;
    313 	}
    314 
    315 	return rv;
    316 }
    317 
    318 static void
    319 pseye_childdet(device_t self, device_t child)
    320 {
    321 	struct pseye_softc *sc = device_private(self);
    322 
    323 	if (sc->sc_videodev) {
    324 		KASSERT(sc->sc_videodev == child);
    325 		sc->sc_videodev = NULL;
    326 	}
    327 }
    328 
    329 /*
    330  * Device access
    331  */
    332 
    333 static void
    334 pseye_init(struct pseye_softc *sc)
    335 {
    336 	pseye_sccb_init(sc);
    337 
    338 	pseye_setregv(sc, 0xc2, 0x0c);
    339 	pseye_setregv(sc, 0x88, 0xf8);
    340 	pseye_setregv(sc, 0xc3, 0x69);
    341 	pseye_setregv(sc, 0x89, 0xff);
    342 	pseye_setregv(sc, 0x76, 0x03);
    343 	pseye_setregv(sc, 0x92, 0x01);
    344 	pseye_setregv(sc, 0x93, 0x18);
    345 	pseye_setregv(sc, 0x94, 0x10);
    346 	pseye_setregv(sc, 0x95, 0x10);
    347 	pseye_setregv(sc, 0xe2, 0x00);
    348 	pseye_setregv(sc, 0xe7, 0x3e);
    349 
    350 	pseye_setreg(sc, 0x1c, 0x0a);
    351 	pseye_setreg(sc, 0x1d, 0x22);
    352 	pseye_setreg(sc, 0x1d, 0x06);
    353 	pseye_setregv(sc, 0x96, 0x00);
    354 
    355 	pseye_setreg(sc, 0x97, 0x20);
    356 	pseye_setreg(sc, 0x97, 0x20);
    357 	pseye_setreg(sc, 0x97, 0x20);
    358 	pseye_setreg(sc, 0x97, 0x0a);
    359 	pseye_setreg(sc, 0x97, 0x3f);
    360 	pseye_setreg(sc, 0x97, 0x4a);
    361 	pseye_setreg(sc, 0x97, 0x20);
    362 	pseye_setreg(sc, 0x97, 0x15);
    363 	pseye_setreg(sc, 0x97, 0x0b);
    364 
    365 	pseye_setregv(sc, 0x8e, 0x40);
    366 	pseye_setregv(sc, 0x1f, 0x81);
    367 	pseye_setregv(sc, 0x34, 0x05);
    368 	pseye_setregv(sc, 0xe3, 0x04);
    369 	pseye_setregv(sc, 0x88, 0x00);
    370 	pseye_setregv(sc, 0x89, 0x00);
    371 	pseye_setregv(sc, 0x76, 0x00);
    372 	pseye_setregv(sc, 0xe7, 0x2e);
    373 	pseye_setregv(sc, 0x31, 0xf9);
    374 	pseye_setregv(sc, 0x25, 0x42);
    375 	pseye_setregv(sc, 0x21, 0xf0);
    376 
    377 	pseye_setreg(sc, 0x1c, 0x00);
    378 	pseye_setreg(sc, 0x1d, 0x40);
    379 	pseye_setreg(sc, 0x1d, 0x02);
    380 	pseye_setreg(sc, 0x1d, 0x00);
    381 	pseye_setreg(sc, 0x1d, 0x02);
    382 	pseye_setreg(sc, 0x1d, 0x57);
    383 	pseye_setreg(sc, 0x1d, 0xff);
    384 
    385 	pseye_setregv(sc, 0x8d, 0x1c);
    386 	pseye_setregv(sc, 0x8e, 0x80);
    387 	pseye_setregv(sc, 0xe5, 0x04);
    388 
    389 	pseye_sccb_setreg(sc, 0x12, 0x80);
    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 	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 
    402 	pseye_sccb_setreg(sc, 0x3d, 0x03);
    403 	pseye_sccb_setreg(sc, 0x17, 0x26);
    404 	pseye_sccb_setreg(sc, 0x18, 0xa0);
    405 	pseye_sccb_setreg(sc, 0x19, 0x07);
    406 	pseye_sccb_setreg(sc, 0x1a, 0xf0);
    407 	pseye_sccb_setreg(sc, 0x32, 0x00);
    408 	pseye_sccb_setreg(sc, 0x29, 0xa0);
    409 	pseye_sccb_setreg(sc, 0x2c, 0xf0);
    410 	pseye_sccb_setreg(sc, 0x65, 0x20);
    411 	pseye_sccb_setreg(sc, 0x11, 0x01);
    412 	pseye_sccb_setreg(sc, 0x42, 0x7f);
    413 	pseye_sccb_setreg(sc, 0x63, 0xe0);
    414 	pseye_sccb_setreg(sc, 0x64, 0xff);
    415 	pseye_sccb_setreg(sc, 0x66, 0x00);
    416 	pseye_sccb_setreg(sc, 0x13, 0xf0);
    417 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    418 	pseye_sccb_setreg(sc, 0x0f, 0xc5);
    419 	pseye_sccb_setreg(sc, 0x14, 0x11);
    420 
    421 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    422 	pseye_sccb_setreg(sc, 0x23, 0x03);
    423 	pseye_sccb_setreg(sc, 0x24, 0x40);
    424 	pseye_sccb_setreg(sc, 0x25, 0x30);
    425 	pseye_sccb_setreg(sc, 0x26, 0xa1);
    426 	pseye_sccb_setreg(sc, 0x2a, 0x00);
    427 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    428 	pseye_sccb_setreg(sc, 0x6b, 0xaa);
    429 	pseye_sccb_setreg(sc, 0x13, 0xff);
    430 
    431 	pseye_sccb_setreg(sc, 0x90, 0x05);
    432 	pseye_sccb_setreg(sc, 0x91, 0x01);
    433 	pseye_sccb_setreg(sc, 0x92, 0x03);
    434 	pseye_sccb_setreg(sc, 0x93, 0x00);
    435 	pseye_sccb_setreg(sc, 0x94, 0x60);
    436 	pseye_sccb_setreg(sc, 0x95, 0x3c);
    437 	pseye_sccb_setreg(sc, 0x96, 0x24);
    438 	pseye_sccb_setreg(sc, 0x97, 0x1e);
    439 	pseye_sccb_setreg(sc, 0x98, 0x62);
    440 	pseye_sccb_setreg(sc, 0x99, 0x80);
    441 	pseye_sccb_setreg(sc, 0x9a, 0x1e);
    442 	pseye_sccb_setreg(sc, 0x9b, 0x08);
    443 	pseye_sccb_setreg(sc, 0x9c, 0x20);
    444 	pseye_sccb_setreg(sc, 0x9e, 0x81);
    445 
    446 	pseye_sccb_setreg(sc, 0xa6, 0x04);
    447 	pseye_sccb_setreg(sc, 0x7e, 0x0c);
    448 	pseye_sccb_setreg(sc, 0x7f, 0x16);
    449 
    450 	pseye_sccb_setreg(sc, 0x80, 0x2a);
    451 	pseye_sccb_setreg(sc, 0x81, 0x4e);
    452 	pseye_sccb_setreg(sc, 0x82, 0x61);
    453 	pseye_sccb_setreg(sc, 0x83, 0x6f);
    454 	pseye_sccb_setreg(sc, 0x84, 0x7b);
    455 	pseye_sccb_setreg(sc, 0x85, 0x86);
    456 	pseye_sccb_setreg(sc, 0x86, 0x8e);
    457 	pseye_sccb_setreg(sc, 0x87, 0x97);
    458 	pseye_sccb_setreg(sc, 0x88, 0xa4);
    459 	pseye_sccb_setreg(sc, 0x89, 0xaf);
    460 	pseye_sccb_setreg(sc, 0x8a, 0xc5);
    461 	pseye_sccb_setreg(sc, 0x8b, 0xd7);
    462 	pseye_sccb_setreg(sc, 0x8c, 0xe8);
    463 	pseye_sccb_setreg(sc, 0x8d, 0x20);
    464 
    465 	pseye_sccb_setreg(sc, 0x0c, 0x90);
    466 
    467 	pseye_setregv(sc, 0xc0, 0x50);
    468 	pseye_setregv(sc, 0xc1, 0x3c);
    469 	pseye_setregv(sc, 0xc2, 0x0c);
    470 
    471 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    472 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    473 	pseye_sccb_setreg(sc, 0x23, 0x03);
    474 	pseye_sccb_setreg(sc, 0x11, 0x01);
    475 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    476 	pseye_sccb_setreg(sc, 0x64, 0xff);
    477 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    478 
    479 	pseye_sccb_setreg(sc, 0x14, 0x41);
    480 	pseye_sccb_setreg(sc, 0x0e, 0xcd);
    481 	pseye_sccb_setreg(sc, 0xac, 0xbf);
    482 	pseye_sccb_setreg(sc, 0x8e, 0x00);
    483 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    484 
    485 	pseye_stop(sc);
    486 }
    487 
    488 static void
    489 pseye_sccb_init(struct pseye_softc *sc)
    490 {
    491 	pseye_setregv(sc, 0xe7, 0x3a);
    492 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    493 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    494 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    495 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
    496 }
    497 
    498 static void
    499 pseye_stop(struct pseye_softc *sc)
    500 {
    501 	pseye_led(sc, false);
    502 	pseye_setreg(sc, 0xe0, 0x09);
    503 }
    504 
    505 static void
    506 pseye_start(struct pseye_softc *sc)
    507 {
    508 	pseye_led(sc, true);
    509 	pseye_setreg(sc, 0xe0, 0x00);
    510 }
    511 
    512 static void
    513 pseye_led(struct pseye_softc *sc, bool enabled)
    514 {
    515 	uint8_t val;
    516 
    517 	val = pseye_getreg(sc, 0x21);
    518 	pseye_setreg(sc, 0x21, val | 0x80);
    519 
    520 	val = pseye_getreg(sc, 0x23);
    521 	if (enabled == true)
    522 		val |= 0x80;
    523 	else
    524 		val &= ~0x80;
    525 	pseye_setreg(sc, 0x23, val);
    526 }
    527 
    528 static uint8_t
    529 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
    530 {
    531 	usb_device_request_t req;
    532 	usbd_status err;
    533 	uint8_t buf;
    534 
    535 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    536 	req.bRequest = 1;
    537 	USETW(req.wValue, 0x0000);
    538 	USETW(req.wIndex, reg);
    539 	USETW(req.wLength, 1);
    540 
    541 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    542 	if (err) {
    543 		aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
    544 		    reg, usbd_errstr(err));
    545 		return 0xff;
    546 	}
    547 
    548 	return buf;
    549 }
    550 
    551 static void
    552 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    553 {
    554 	usb_device_request_t req;
    555 	usbd_status err;
    556 
    557 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    558 	req.bRequest = 1;
    559 	USETW(req.wValue, 0x0000);
    560 	USETW(req.wIndex, reg);
    561 	USETW(req.wLength, 1);
    562 
    563 	err = usbd_do_request(sc->sc_udev, &req, &val);
    564 	if (err)
    565 		aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
    566 		    reg, usbd_errstr(err));
    567 }
    568 
    569 static void
    570 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    571 {
    572 	pseye_setreg(sc, reg, val);
    573 	if (pseye_getreg(sc, reg) != val)
    574 		aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
    575 		    reg);
    576 }
    577 
    578 static void
    579 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
    580 {
    581 	pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
    582 	pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
    583 	pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
    584 
    585 	if (pseye_sccb_status(sc) == false)
    586 		aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
    587 		    reg);
    588 }
    589 
    590 static bool
    591 pseye_sccb_status(struct pseye_softc *sc)
    592 {
    593 	int retry = 5;
    594 	uint8_t reg;
    595 
    596 	while (retry-- >= 0) {
    597 		reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
    598 		if (reg == 0x00)
    599 			return true;
    600 		else if (reg == 0x04)
    601 			return false;
    602 	}
    603 
    604 	aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
    605 	return false;
    606 }
    607 
    608 static usbd_status
    609 pseye_get_frame(struct pseye_softc *sc)
    610 {
    611 	uint32_t len = sc->sc_bulkin_bufferlen;
    612 
    613 	if (sc->sc_dying)
    614 		return USBD_IOERROR;
    615 
    616 	return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
    617 	    USBD_SHORT_XFER_OK|USBD_NO_COPY, 50,
    618 	    sc->sc_bulkin_buffer, &len, "pseyerb");
    619 }
    620 
    621 static int
    622 pseye_init_pipes(struct pseye_softc *sc)
    623 {
    624 	usbd_status err;
    625 
    626 	if (sc->sc_dying)
    627 		return EIO;
    628 
    629 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
    630 	    &sc->sc_bulkin_pipe);
    631 	if (err) {
    632 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    633 		    usbd_errstr(err));
    634 		return ENOMEM;
    635 	}
    636 
    637 	pseye_start(sc);
    638 
    639 	return 0;
    640 }
    641 
    642 int
    643 pseye_close_pipes(struct pseye_softc *sc)
    644 {
    645 	if (sc->sc_bulkin_pipe != NULL) {
    646 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    647 		usbd_close_pipe(sc->sc_bulkin_pipe);
    648 		sc->sc_bulkin_pipe = NULL;
    649 	}
    650 
    651 	pseye_stop(sc);
    652 
    653 	return 0;
    654 }
    655 
    656 static void
    657 pseye_transfer_thread(void *opaque)
    658 {
    659 	struct pseye_softc *sc = opaque;
    660 	int error;
    661 	struct video_payload payload;
    662 
    663 	payload.frameno = 0;
    664 
    665 	while (sc->sc_running) {
    666 		error = pseye_get_frame(sc);
    667 		if (error == USBD_NORMAL_COMPLETION) {
    668 			payload.data = sc->sc_bulkin_buffer;
    669 			payload.size = sc->sc_bulkin_bufferlen;
    670 			payload.frameno = (payload.frameno + 1) & 1;
    671 			payload.end_of_frame = 1;
    672 			video_submit_payload(sc->sc_videodev, &payload);
    673 		}
    674 	}
    675 
    676 	mutex_enter(&sc->sc_mtx);
    677 	cv_broadcast(&sc->sc_cv);
    678 	mutex_exit(&sc->sc_mtx);
    679 
    680 	kthread_exit(0);
    681 }
    682 
    683 /* video(9) API implementations */
    684 static int
    685 pseye_open(void *opaque, int flags)
    686 {
    687 	struct pseye_softc *sc = opaque;
    688 
    689 	if (sc->sc_dying)
    690 		return EIO;
    691 
    692 	return pseye_init_pipes(sc);
    693 }
    694 
    695 static void
    696 pseye_close(void *opaque)
    697 {
    698 	struct pseye_softc *sc = opaque;
    699 
    700 	pseye_close_pipes(sc);
    701 }
    702 
    703 static const char *
    704 pseye_get_devname(void *opaque)
    705 {
    706 	return "PLAYSTATION(R) Eye";
    707 }
    708 
    709 static int
    710 pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
    711 {
    712 	if (index != 0)
    713 		return EINVAL;
    714 	return pseye_get_format(opaque, format);
    715 }
    716 
    717 static int
    718 pseye_get_format(void *opaque, struct video_format *format)
    719 {
    720 	format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
    721 	format->width = 640;
    722 	format->height = 480;
    723 	format->aspect_x = 4;
    724 	format->aspect_y = 3;
    725 	format->sample_size = format->width * format->height * 2;
    726 	format->stride = format->width * 2;
    727 	format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
    728 	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
    729 	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
    730 	format->interlace_flags = VIDEO_INTERLACE_ON;
    731 	format->priv = 0;
    732 
    733 	return 0;
    734 }
    735 
    736 static int
    737 pseye_set_format(void *opaque, struct video_format *format)
    738 {
    739 #if notyet
    740 	if (format->pixel_format != VIDEO_FORMAT_YUYV)
    741 		return EINVAL;
    742 	if (format->width != 640 || format->height != 480)
    743 		return EINVAL;
    744 #endif
    745 	/* XXX */
    746 	return pseye_get_format(opaque, format);
    747 }
    748 
    749 static int
    750 pseye_try_format(void *opaque, struct video_format *format)
    751 {
    752 	return pseye_get_format(opaque, format);
    753 }
    754 
    755 static int
    756 pseye_start_transfer(void *opaque)
    757 {
    758 	struct pseye_softc *sc = opaque;
    759 	int err = 0;
    760 
    761 	mutex_enter(&sc->sc_mtx);
    762 	if (sc->sc_running == 0) {
    763 		sc->sc_running = 1;
    764 		err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
    765 		    opaque, NULL, device_xname(sc->sc_dev));
    766 	} else
    767 		aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
    768 	mutex_exit(&sc->sc_mtx);
    769 
    770 	return err;
    771 }
    772 
    773 static int
    774 pseye_stop_transfer(void *opaque)
    775 {
    776 	struct pseye_softc *sc = opaque;
    777 
    778 	mutex_enter(&sc->sc_mtx);
    779 	if (sc->sc_running) {
    780 		sc->sc_running = 0;
    781 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    782 	}
    783 	mutex_exit(&sc->sc_mtx);
    784 
    785 	return 0;
    786 }
    787