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