Home | History | Annotate | Line # | Download | only in usb
pseye.c revision 1.11.4.4
      1 /* $NetBSD: pseye.c,v 1.11.4.4 2009/09/16 13:37:58 yamt 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.11.4.4 2009/09/16 13:37:58 yamt 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 #include <sys/module.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <dev/usb/usb.h>
     58 #include <dev/usb/usbdi.h>
     59 #include <dev/usb/usbdi_util.h>
     60 #include <dev/usb/usbdevs.h>
     61 #include <dev/usb/uvideoreg.h>
     62 
     63 #include <dev/video_if.h>
     64 
     65 #define PRI_PSEYE	PRI_BIO
     66 
     67 /* Bulk-in buffer length -- make room for payload + UVC headers */
     68 #define PSEYE_BULKIN_BUFLEN	((640 * 480 * 2) + 4096)
     69 #define PSEYE_BULKIN_BLKLEN	2048
     70 
     71 /* SCCB/sensor interface */
     72 #define PSEYE_SCCB_ADDRESS	0xf1
     73 #define PSEYE_SCCB_SUBADDR	0xf2
     74 #define PSEYE_SCCB_WRITE	0xf3
     75 #define PSEYE_SCCB_READ		0xf4
     76 #define PSEYE_SCCB_OPERATION	0xf5
     77 #define PSEYE_SCCB_STATUS	0xf6
     78 
     79 #define PSEYE_SCCB_OP_WRITE_3	0x37
     80 #define PSEYE_SCCB_OP_WRITE_2	0x33
     81 #define PSEYE_SCCB_OP_READ_2	0xf9
     82 
     83 struct pseye_softc {
     84 	USBBASEDEVICE		sc_dev;
     85 
     86 	usbd_device_handle	sc_udev;
     87 	usbd_interface_handle	sc_iface;
     88 
     89 	device_t		sc_videodev;
     90 	char			sc_running;
     91 
     92 	kcondvar_t		sc_cv;
     93 	kmutex_t		sc_mtx;
     94 
     95 	usbd_pipe_handle	sc_bulkin_pipe;
     96 	usbd_xfer_handle	sc_bulkin_xfer;
     97 	int			sc_bulkin;
     98 	uint8_t			*sc_bulkin_buffer;
     99 	int			sc_bulkin_bufferlen;
    100 
    101 	char			sc_dying;
    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 int		pseye_enum_format(void *, uint32_t,
    132 					  struct video_format *);
    133 static int		pseye_get_format(void *, struct video_format *);
    134 static int		pseye_set_format(void *, struct video_format *);
    135 static int		pseye_try_format(void *, struct video_format *);
    136 static int		pseye_start_transfer(void *);
    137 static int		pseye_stop_transfer(void *);
    138 
    139 CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
    140     pseye_match, pseye_attach, pseye_detach, pseye_activate,
    141     NULL, pseye_childdet);
    142 
    143 static const struct video_hw_if pseye_hw_if = {
    144 	.open = pseye_open,
    145 	.close = pseye_close,
    146 	.get_devname = pseye_get_devname,
    147 	.enum_format = pseye_enum_format,
    148 	.get_format = pseye_get_format,
    149 	.set_format = pseye_set_format,
    150 	.try_format = pseye_try_format,
    151 	.start_transfer = pseye_start_transfer,
    152 	.stop_transfer = pseye_stop_transfer,
    153 	.control_iter_init = NULL,
    154 	.control_iter_next = NULL,
    155 	.get_control_desc_group = NULL,
    156 	.get_control_group = NULL,
    157 	.set_control_group = NULL,
    158 };
    159 
    160 static int
    161 pseye_match(device_t parent, cfdata_t match, void *opaque)
    162 {
    163 	struct usbif_attach_arg *uaa = opaque;
    164 
    165 	if (uaa->class != UICLASS_VENDOR)
    166 		return UMATCH_NONE;
    167 
    168 	if (uaa->vendor == USB_VENDOR_OMNIVISION2) {
    169 		switch (uaa->product) {
    170 		case USB_PRODUCT_OMNIVISION2_PSEYE:
    171 			if (uaa->ifaceno != 0)
    172 				return UMATCH_NONE;
    173 			return UMATCH_VENDOR_PRODUCT;
    174 		}
    175 	}
    176 
    177 	return UMATCH_NONE;
    178 }
    179 
    180 static void
    181 pseye_attach(device_t parent, device_t self, void *opaque)
    182 {
    183 	struct pseye_softc *sc = device_private(self);
    184 	struct usbif_attach_arg *uaa = opaque;
    185 	usbd_device_handle dev = uaa->device;
    186 	usb_interface_descriptor_t *id = NULL;
    187 	usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
    188 	char *devinfo;
    189 	int i;
    190 
    191 	devinfo = usbd_devinfo_alloc(dev, 0);
    192 	aprint_naive("\n");
    193 	aprint_normal(": %s\n", devinfo);
    194 	usbd_devinfo_free(devinfo);
    195 
    196 	sc->sc_dev = self;
    197 	sc->sc_udev = dev;
    198 	sc->sc_iface = uaa->iface;
    199 	sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;
    200 
    201 	sc->sc_dying = sc->sc_running = 0;
    202 	cv_init(&sc->sc_cv, device_xname(self));
    203 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    204 
    205 	id = usbd_get_interface_descriptor(sc->sc_iface);
    206 	if (id == NULL) {
    207 		aprint_error_dev(self, "failed to get interface descriptor\n");
    208 		sc->sc_dying = 1;
    209 		return;
    210 	}
    211 
    212 	for (i = 0; i < id->bNumEndpoints; i++) {
    213 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    214 		if (ed == NULL) {
    215 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    216 			sc->sc_dying = 1;
    217 			return;
    218 		}
    219 
    220 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    221 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    222 			ed_bulkin = ed;
    223 			break;
    224 		}
    225 	}
    226 
    227 	if (ed_bulkin == NULL) {
    228 		aprint_error_dev(self, "no bulk-in endpoint found\n");
    229 		sc->sc_dying = 1;
    230 		return;
    231 	}
    232 
    233 	sc->sc_bulkin = ed_bulkin->bEndpointAddress;
    234 
    235 	sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
    236 	if (sc->sc_bulkin_xfer == NULL) {
    237 		sc->sc_dying = 1;
    238 		return;
    239 	}
    240 	sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
    241 	    sc->sc_bulkin_bufferlen);
    242 	if (sc->sc_bulkin_buffer == NULL) {
    243 		usbd_free_xfer(sc->sc_bulkin_xfer);
    244 		sc->sc_bulkin_xfer = NULL;
    245 		sc->sc_dying = 1;
    246 		return;
    247 	}
    248 
    249 	pseye_init(sc);
    250 
    251 	if (!pmf_device_register(self, NULL, NULL))
    252 		aprint_error_dev(self, "couldn't establish power handler\n");
    253 
    254 	sc->sc_videodev = video_attach_mi(&pseye_hw_if, self);
    255 	if (sc->sc_videodev == NULL) {
    256 		aprint_error_dev(self, "couldn't attach video layer\n");
    257 		sc->sc_dying = 1;
    258 		return;
    259 	}
    260 
    261 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    262 	    USBDEV(self));
    263 
    264 }
    265 
    266 static int
    267 pseye_detach(device_t self, int flags)
    268 {
    269 	struct pseye_softc *sc = device_private(self);
    270 
    271 	sc->sc_dying = 1;
    272 
    273 	pmf_device_deregister(self);
    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_setregv(sc, 0x96, 0x00);
    358 
    359 	pseye_setreg(sc, 0x97, 0x20);
    360 	pseye_setreg(sc, 0x97, 0x20);
    361 	pseye_setreg(sc, 0x97, 0x20);
    362 	pseye_setreg(sc, 0x97, 0x0a);
    363 	pseye_setreg(sc, 0x97, 0x3f);
    364 	pseye_setreg(sc, 0x97, 0x4a);
    365 	pseye_setreg(sc, 0x97, 0x20);
    366 	pseye_setreg(sc, 0x97, 0x15);
    367 	pseye_setreg(sc, 0x97, 0x0b);
    368 
    369 	pseye_setregv(sc, 0x8e, 0x40);
    370 	pseye_setregv(sc, 0x1f, 0x81);
    371 	pseye_setregv(sc, 0x34, 0x05);
    372 	pseye_setregv(sc, 0xe3, 0x04);
    373 	pseye_setregv(sc, 0x88, 0x00);
    374 	pseye_setregv(sc, 0x89, 0x00);
    375 	pseye_setregv(sc, 0x76, 0x00);
    376 	pseye_setregv(sc, 0xe7, 0x2e);
    377 	pseye_setregv(sc, 0x31, 0xf9);
    378 	pseye_setregv(sc, 0x25, 0x42);
    379 	pseye_setregv(sc, 0x21, 0xf0);
    380 
    381 	pseye_setreg(sc, 0x1c, 0x00);
    382 	pseye_setreg(sc, 0x1d, 0x40);
    383 	pseye_setreg(sc, 0x1d, 0x02);	/* payload size 0x0200 * 4 == 2048 */
    384 	pseye_setreg(sc, 0x1d, 0x00);
    385 	pseye_setreg(sc, 0x1d, 0x02);	/* frame size 0x025800 * 4 == 614400 */
    386 	pseye_setreg(sc, 0x1d, 0x58);
    387 	pseye_setreg(sc, 0x1d, 0x00);
    388 
    389 	pseye_setreg(sc, 0x1c, 0x0a);
    390 	pseye_setreg(sc, 0x1d, 0x08);	/* enable UVC header */
    391 	pseye_setreg(sc, 0x1d, 0x0e);
    392 
    393 	pseye_setregv(sc, 0x8d, 0x1c);
    394 	pseye_setregv(sc, 0x8e, 0x80);
    395 	pseye_setregv(sc, 0xe5, 0x04);
    396 
    397 	pseye_sccb_setreg(sc, 0x12, 0x80);
    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 	pseye_sccb_setreg(sc, 0x11, 0x01);
    409 
    410 	pseye_sccb_setreg(sc, 0x3d, 0x03);
    411 	pseye_sccb_setreg(sc, 0x17, 0x26);
    412 	pseye_sccb_setreg(sc, 0x18, 0xa0);
    413 	pseye_sccb_setreg(sc, 0x19, 0x07);
    414 	pseye_sccb_setreg(sc, 0x1a, 0xf0);
    415 	pseye_sccb_setreg(sc, 0x32, 0x00);
    416 	pseye_sccb_setreg(sc, 0x29, 0xa0);
    417 	pseye_sccb_setreg(sc, 0x2c, 0xf0);
    418 	pseye_sccb_setreg(sc, 0x65, 0x20);
    419 	pseye_sccb_setreg(sc, 0x11, 0x01);
    420 	pseye_sccb_setreg(sc, 0x42, 0x7f);
    421 	pseye_sccb_setreg(sc, 0x63, 0xe0);
    422 	pseye_sccb_setreg(sc, 0x64, 0xff);
    423 	pseye_sccb_setreg(sc, 0x66, 0x00);
    424 	pseye_sccb_setreg(sc, 0x13, 0xf0);
    425 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    426 	pseye_sccb_setreg(sc, 0x0f, 0xc5);
    427 	pseye_sccb_setreg(sc, 0x14, 0x11);
    428 
    429 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    430 	pseye_sccb_setreg(sc, 0x23, 0x03);
    431 	pseye_sccb_setreg(sc, 0x24, 0x40);
    432 	pseye_sccb_setreg(sc, 0x25, 0x30);
    433 	pseye_sccb_setreg(sc, 0x26, 0xa1);
    434 	pseye_sccb_setreg(sc, 0x2a, 0x00);
    435 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    436 	pseye_sccb_setreg(sc, 0x6b, 0xaa);
    437 	pseye_sccb_setreg(sc, 0x13, 0xff);
    438 
    439 	pseye_sccb_setreg(sc, 0x90, 0x05);
    440 	pseye_sccb_setreg(sc, 0x91, 0x01);
    441 	pseye_sccb_setreg(sc, 0x92, 0x03);
    442 	pseye_sccb_setreg(sc, 0x93, 0x00);
    443 	pseye_sccb_setreg(sc, 0x94, 0x60);
    444 	pseye_sccb_setreg(sc, 0x95, 0x3c);
    445 	pseye_sccb_setreg(sc, 0x96, 0x24);
    446 	pseye_sccb_setreg(sc, 0x97, 0x1e);
    447 	pseye_sccb_setreg(sc, 0x98, 0x62);
    448 	pseye_sccb_setreg(sc, 0x99, 0x80);
    449 	pseye_sccb_setreg(sc, 0x9a, 0x1e);
    450 	pseye_sccb_setreg(sc, 0x9b, 0x08);
    451 	pseye_sccb_setreg(sc, 0x9c, 0x20);
    452 	pseye_sccb_setreg(sc, 0x9e, 0x81);
    453 
    454 	pseye_sccb_setreg(sc, 0xa6, 0x04);
    455 	pseye_sccb_setreg(sc, 0x7e, 0x0c);
    456 	pseye_sccb_setreg(sc, 0x7f, 0x16);
    457 
    458 	pseye_sccb_setreg(sc, 0x80, 0x2a);
    459 	pseye_sccb_setreg(sc, 0x81, 0x4e);
    460 	pseye_sccb_setreg(sc, 0x82, 0x61);
    461 	pseye_sccb_setreg(sc, 0x83, 0x6f);
    462 	pseye_sccb_setreg(sc, 0x84, 0x7b);
    463 	pseye_sccb_setreg(sc, 0x85, 0x86);
    464 	pseye_sccb_setreg(sc, 0x86, 0x8e);
    465 	pseye_sccb_setreg(sc, 0x87, 0x97);
    466 	pseye_sccb_setreg(sc, 0x88, 0xa4);
    467 	pseye_sccb_setreg(sc, 0x89, 0xaf);
    468 	pseye_sccb_setreg(sc, 0x8a, 0xc5);
    469 	pseye_sccb_setreg(sc, 0x8b, 0xd7);
    470 	pseye_sccb_setreg(sc, 0x8c, 0xe8);
    471 	pseye_sccb_setreg(sc, 0x8d, 0x20);
    472 
    473 	pseye_sccb_setreg(sc, 0x0c, 0x90);
    474 
    475 	pseye_setregv(sc, 0xc0, 0x50);
    476 	pseye_setregv(sc, 0xc1, 0x3c);
    477 	pseye_setregv(sc, 0xc2, 0x0c);
    478 
    479 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    480 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    481 	pseye_sccb_setreg(sc, 0x23, 0x03);
    482 	pseye_sccb_setreg(sc, 0x11, 0x01);
    483 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    484 	pseye_sccb_setreg(sc, 0x64, 0xff);
    485 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    486 
    487 	pseye_sccb_setreg(sc, 0x14, 0x41);
    488 	pseye_sccb_setreg(sc, 0x0e, 0xcd);
    489 	pseye_sccb_setreg(sc, 0xac, 0xbf);
    490 	pseye_sccb_setreg(sc, 0x8e, 0x00);
    491 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    492 
    493 	pseye_stop(sc);
    494 }
    495 
    496 static void
    497 pseye_sccb_init(struct pseye_softc *sc)
    498 {
    499 	pseye_setregv(sc, 0xe7, 0x3a);
    500 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    501 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    502 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    503 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
    504 }
    505 
    506 static void
    507 pseye_stop(struct pseye_softc *sc)
    508 {
    509 	pseye_led(sc, false);
    510 	pseye_setreg(sc, 0xe0, 0x09);
    511 }
    512 
    513 static void
    514 pseye_start(struct pseye_softc *sc)
    515 {
    516 	pseye_led(sc, true);
    517 	pseye_setreg(sc, 0xe0, 0x00);
    518 }
    519 
    520 static void
    521 pseye_led(struct pseye_softc *sc, bool enabled)
    522 {
    523 	uint8_t val;
    524 
    525 	val = pseye_getreg(sc, 0x21);
    526 	pseye_setreg(sc, 0x21, val | 0x80);
    527 
    528 	val = pseye_getreg(sc, 0x23);
    529 	if (enabled == true)
    530 		val |= 0x80;
    531 	else
    532 		val &= ~0x80;
    533 	pseye_setreg(sc, 0x23, val);
    534 }
    535 
    536 static uint8_t
    537 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
    538 {
    539 	usb_device_request_t req;
    540 	usbd_status err;
    541 	uint8_t buf;
    542 
    543 	req.bmRequestType = UT_READ_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, &buf);
    550 	if (err) {
    551 		aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
    552 		    reg, usbd_errstr(err));
    553 		return 0xff;
    554 	}
    555 
    556 	return buf;
    557 }
    558 
    559 static void
    560 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    561 {
    562 	usb_device_request_t req;
    563 	usbd_status err;
    564 
    565 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    566 	req.bRequest = 1;
    567 	USETW(req.wValue, 0x0000);
    568 	USETW(req.wIndex, reg);
    569 	USETW(req.wLength, 1);
    570 
    571 	err = usbd_do_request(sc->sc_udev, &req, &val);
    572 	if (err)
    573 		aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
    574 		    reg, usbd_errstr(err));
    575 }
    576 
    577 static void
    578 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    579 {
    580 	pseye_setreg(sc, reg, val);
    581 	if (pseye_getreg(sc, reg) != val)
    582 		aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
    583 		    reg);
    584 }
    585 
    586 static void
    587 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
    588 {
    589 	pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
    590 	pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
    591 	pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
    592 
    593 	if (pseye_sccb_status(sc) == false)
    594 		aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
    595 		    reg);
    596 }
    597 
    598 static bool
    599 pseye_sccb_status(struct pseye_softc *sc)
    600 {
    601 	int retry = 5;
    602 	uint8_t reg;
    603 
    604 	while (retry-- >= 0) {
    605 		reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
    606 		if (reg == 0x00)
    607 			return true;
    608 		else if (reg == 0x04)
    609 			return false;
    610 	}
    611 
    612 	aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
    613 	return false;
    614 }
    615 
    616 static usbd_status
    617 pseye_get_frame(struct pseye_softc *sc, uint32_t *plen)
    618 {
    619 	if (sc->sc_dying)
    620 		return USBD_IOERROR;
    621 
    622 	return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
    623 	    USBD_SHORT_XFER_OK|USBD_NO_COPY, 1000,
    624 	    sc->sc_bulkin_buffer, plen, "pseyerb");
    625 }
    626 
    627 static int
    628 pseye_init_pipes(struct pseye_softc *sc)
    629 {
    630 	usbd_status err;
    631 
    632 	if (sc->sc_dying)
    633 		return EIO;
    634 
    635 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
    636 	    &sc->sc_bulkin_pipe);
    637 	if (err) {
    638 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    639 		    usbd_errstr(err));
    640 		return ENOMEM;
    641 	}
    642 
    643 	pseye_start(sc);
    644 
    645 	return 0;
    646 }
    647 
    648 int
    649 pseye_close_pipes(struct pseye_softc *sc)
    650 {
    651 	if (sc->sc_bulkin_pipe != NULL) {
    652 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    653 		usbd_close_pipe(sc->sc_bulkin_pipe);
    654 		sc->sc_bulkin_pipe = NULL;
    655 	}
    656 
    657 	pseye_stop(sc);
    658 
    659 	return 0;
    660 }
    661 
    662 static void
    663 pseye_submit_payload(struct pseye_softc *sc, uint32_t tlen)
    664 {
    665 	struct video_payload payload;
    666 	uvideo_payload_header_t *uvchdr;
    667 	uint8_t *buf = sc->sc_bulkin_buffer;
    668 	uint32_t len;
    669 	uint32_t brem = (640*480*2);
    670 
    671 	while (brem > 0 && tlen > 0) {
    672 		len = min(tlen, PSEYE_BULKIN_BLKLEN);
    673 		if (len < UVIDEO_PAYLOAD_HEADER_SIZE) {
    674 			printf("pseye_submit_payload: len=%u\n", len);
    675 			return;
    676 		}
    677 
    678 		uvchdr = (uvideo_payload_header_t *)buf;
    679 		if (uvchdr->bHeaderLength != UVIDEO_PAYLOAD_HEADER_SIZE)
    680 			goto next;
    681 		if (uvchdr->bHeaderLength == len &&
    682 		    !(uvchdr->bmHeaderInfo & UV_END_OF_FRAME))
    683 			goto next;
    684 		if (uvchdr->bmHeaderInfo & UV_ERROR)
    685 			return;
    686 		if ((uvchdr->bmHeaderInfo & UV_PRES_TIME) == 0)
    687 			goto next;
    688 
    689 		payload.data = buf + uvchdr->bHeaderLength;
    690 		payload.size = min(brem, len - uvchdr->bHeaderLength);
    691 		payload.frameno = UGETDW(&buf[2]);
    692 		payload.end_of_frame = uvchdr->bmHeaderInfo & UV_END_OF_FRAME;
    693 		video_submit_payload(sc->sc_videodev, &payload);
    694 
    695 next:
    696 		tlen -= len;
    697 		buf += len;
    698 		brem -= payload.size;
    699 	}
    700 }
    701 
    702 static void
    703 pseye_transfer_thread(void *opaque)
    704 {
    705 	struct pseye_softc *sc = opaque;
    706 	uint32_t len;
    707 	int error;
    708 
    709 	while (sc->sc_running) {
    710 		len = sc->sc_bulkin_bufferlen;
    711 		error = pseye_get_frame(sc, &len);
    712 		if (error == USBD_NORMAL_COMPLETION)
    713 			pseye_submit_payload(sc, len);
    714 	}
    715 
    716 	mutex_enter(&sc->sc_mtx);
    717 	cv_broadcast(&sc->sc_cv);
    718 	mutex_exit(&sc->sc_mtx);
    719 
    720 	kthread_exit(0);
    721 }
    722 
    723 /* video(9) API implementations */
    724 static int
    725 pseye_open(void *opaque, int flags)
    726 {
    727 	struct pseye_softc *sc = opaque;
    728 
    729 	if (sc->sc_dying)
    730 		return EIO;
    731 
    732 	return pseye_init_pipes(sc);
    733 }
    734 
    735 static void
    736 pseye_close(void *opaque)
    737 {
    738 	struct pseye_softc *sc = opaque;
    739 
    740 	pseye_close_pipes(sc);
    741 }
    742 
    743 static const char *
    744 pseye_get_devname(void *opaque)
    745 {
    746 	return "PlayStation Eye";
    747 }
    748 
    749 static int
    750 pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
    751 {
    752 	if (index != 0)
    753 		return EINVAL;
    754 	return pseye_get_format(opaque, format);
    755 }
    756 
    757 static int
    758 pseye_get_format(void *opaque, struct video_format *format)
    759 {
    760 	format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
    761 	format->width = 640;
    762 	format->height = 480;
    763 	format->aspect_x = 4;
    764 	format->aspect_y = 3;
    765 	format->sample_size = format->width * format->height * 2;
    766 	format->stride = format->width * 2;
    767 	format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
    768 	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
    769 	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
    770 	format->interlace_flags = VIDEO_INTERLACE_ON;
    771 	format->priv = 0;
    772 
    773 	return 0;
    774 }
    775 
    776 static int
    777 pseye_set_format(void *opaque, struct video_format *format)
    778 {
    779 #if notyet
    780 	if (format->pixel_format != VIDEO_FORMAT_YUYV)
    781 		return EINVAL;
    782 	if (format->width != 640 || format->height != 480)
    783 		return EINVAL;
    784 #endif
    785 	/* XXX */
    786 	return pseye_get_format(opaque, format);
    787 }
    788 
    789 static int
    790 pseye_try_format(void *opaque, struct video_format *format)
    791 {
    792 	return pseye_get_format(opaque, format);
    793 }
    794 
    795 static int
    796 pseye_start_transfer(void *opaque)
    797 {
    798 	struct pseye_softc *sc = opaque;
    799 	int err = 0;
    800 
    801 	mutex_enter(&sc->sc_mtx);
    802 	if (sc->sc_running == 0) {
    803 		sc->sc_running = 1;
    804 		err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
    805 		    opaque, NULL, device_xname(sc->sc_dev));
    806 	} else
    807 		aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
    808 	mutex_exit(&sc->sc_mtx);
    809 
    810 	return err;
    811 }
    812 
    813 static int
    814 pseye_stop_transfer(void *opaque)
    815 {
    816 	struct pseye_softc *sc = opaque;
    817 
    818 	mutex_enter(&sc->sc_mtx);
    819 	if (sc->sc_running) {
    820 		sc->sc_running = 0;
    821 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    822 	}
    823 	mutex_exit(&sc->sc_mtx);
    824 
    825 	return 0;
    826 }
    827 
    828 #ifdef _MODULE
    829 
    830 MODULE(MODULE_CLASS_DRIVER, pseye, NULL);
    831 
    832 static const struct cfiattrdata videobuscf_iattrdata = {
    833 	"videobus", 0, { { NULL, NULL, 0 }, }
    834 };
    835 static const struct cfiattrdata * const pseye_attrs[] = {
    836 	&videobuscf_iattrdata, NULL
    837 };
    838 CFDRIVER_DECL(pseye, DV_DULL, pseye_attrs);
    839 extern struct cfattach video_ca;
    840 static int pseyeloc[6] = { -1, -1, -1, -1, -1, -1 };
    841 static struct cfparent uhubparent = {
    842 	"usbifif", NULL, DVUNIT_ANY
    843 };
    844 static struct cfdata pseye_cfdata[] = {
    845 	{
    846 		.cf_name = "pseye",
    847 		.cf_atname = "pseye",
    848 		.cf_unit = 0,
    849 		.cf_fstate = FSTATE_STAR,
    850 		.cf_loc = pseyeloc,
    851 		.cf_flags = 0,
    852 		.cf_pspec = &uhubparent,
    853 	},
    854 	{ NULL }
    855 };
    856 
    857 static int
    858 pseye_modcmd(modcmd_t cmd, void *opaque)
    859 {
    860 	int err;
    861 
    862 	switch (cmd) {
    863 	case MODULE_CMD_INIT:
    864 		err = config_cfdriver_attach(&pseye_cd);
    865 		if (err)
    866 			return err;
    867 		err = config_cfattach_attach("pseye", &pseye_ca);
    868 		if (err) {
    869 			config_cfdriver_detach(&pseye_cd);
    870 			return err;
    871 		}
    872 		err = config_cfdata_attach(pseye_cfdata, 1);
    873 		if (err) {
    874 			config_cfattach_detach("pseye", &pseye_ca);
    875 			config_cfdriver_detach(&pseye_cd);
    876 			return err;
    877 		}
    878 		return 0;
    879 	case MODULE_CMD_FINI:
    880 		err = config_cfdata_detach(pseye_cfdata);
    881 		if (err)
    882 			return err;
    883 		config_cfattach_detach("pseye", &pseye_ca);
    884 		config_cfdriver_detach(&pseye_cd);
    885 		return 0;
    886 	default:
    887 		return ENOTTY;
    888 	}
    889 }
    890 
    891 #endif /* !_MODULE */
    892