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