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