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