Home | History | Annotate | Line # | Download | only in usb
pseye.c revision 1.21.34.8
      1 /* $NetBSD: pseye.c,v 1.21.34.8 2015/10/06 21:32:15 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.21.34.8 2015/10/06 21:32:15 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_xfer != NULL) {
    289 		usbd_destroy_xfer(sc->sc_bulkin_xfer);
    290 		sc->sc_bulkin_xfer = NULL;
    291 	}
    292 
    293 	if (sc->sc_bulkin_pipe != NULL) {
    294 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    295 		sc->sc_bulkin_pipe = NULL;
    296 	}
    297 
    298 	mutex_enter(&sc->sc_mtx);
    299 	if (sc->sc_running) {
    300 		sc->sc_running = 0;
    301 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    302 	}
    303 	mutex_exit(&sc->sc_mtx);
    304 
    305 	cv_destroy(&sc->sc_cv);
    306 	mutex_destroy(&sc->sc_mtx);
    307 
    308 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    309 	    sc->sc_dev);
    310 
    311 	return 0;
    312 }
    313 
    314 int
    315 pseye_activate(device_t self, enum devact act)
    316 {
    317 	struct pseye_softc *sc = device_private(self);
    318 
    319 	switch (act) {
    320 	case DVACT_DEACTIVATE:
    321 		sc->sc_dying = 1;
    322 		return 0;
    323 	default:
    324 		return EOPNOTSUPP;
    325 	}
    326 }
    327 
    328 static void
    329 pseye_childdet(device_t self, device_t child)
    330 {
    331 	struct pseye_softc *sc = device_private(self);
    332 
    333 	if (sc->sc_videodev) {
    334 		KASSERT(sc->sc_videodev == child);
    335 		sc->sc_videodev = NULL;
    336 	}
    337 }
    338 
    339 /*
    340  * Device access
    341  */
    342 
    343 static void
    344 pseye_init(struct pseye_softc *sc)
    345 {
    346 	pseye_sccb_init(sc);
    347 
    348 	pseye_setregv(sc, 0xc2, 0x0c);
    349 	pseye_setregv(sc, 0x88, 0xf8);
    350 	pseye_setregv(sc, 0xc3, 0x69);
    351 	pseye_setregv(sc, 0x89, 0xff);
    352 	pseye_setregv(sc, 0x76, 0x03);
    353 	pseye_setregv(sc, 0x92, 0x01);
    354 	pseye_setregv(sc, 0x93, 0x18);
    355 	pseye_setregv(sc, 0x94, 0x10);
    356 	pseye_setregv(sc, 0x95, 0x10);
    357 	pseye_setregv(sc, 0xe2, 0x00);
    358 	pseye_setregv(sc, 0xe7, 0x3e);
    359 
    360 	pseye_setregv(sc, 0x96, 0x00);
    361 
    362 	pseye_setreg(sc, 0x97, 0x20);
    363 	pseye_setreg(sc, 0x97, 0x20);
    364 	pseye_setreg(sc, 0x97, 0x20);
    365 	pseye_setreg(sc, 0x97, 0x0a);
    366 	pseye_setreg(sc, 0x97, 0x3f);
    367 	pseye_setreg(sc, 0x97, 0x4a);
    368 	pseye_setreg(sc, 0x97, 0x20);
    369 	pseye_setreg(sc, 0x97, 0x15);
    370 	pseye_setreg(sc, 0x97, 0x0b);
    371 
    372 	pseye_setregv(sc, 0x8e, 0x40);
    373 	pseye_setregv(sc, 0x1f, 0x81);
    374 	pseye_setregv(sc, 0x34, 0x05);
    375 	pseye_setregv(sc, 0xe3, 0x04);
    376 	pseye_setregv(sc, 0x88, 0x00);
    377 	pseye_setregv(sc, 0x89, 0x00);
    378 	pseye_setregv(sc, 0x76, 0x00);
    379 	pseye_setregv(sc, 0xe7, 0x2e);
    380 	pseye_setregv(sc, 0x31, 0xf9);
    381 	pseye_setregv(sc, 0x25, 0x42);
    382 	pseye_setregv(sc, 0x21, 0xf0);
    383 
    384 	pseye_setreg(sc, 0x1c, 0x00);
    385 	pseye_setreg(sc, 0x1d, 0x40);
    386 	pseye_setreg(sc, 0x1d, 0x02);	/* payload size 0x0200 * 4 == 2048 */
    387 	pseye_setreg(sc, 0x1d, 0x00);
    388 	pseye_setreg(sc, 0x1d, 0x02);	/* frame size 0x025800 * 4 == 614400 */
    389 	pseye_setreg(sc, 0x1d, 0x58);
    390 	pseye_setreg(sc, 0x1d, 0x00);
    391 
    392 	pseye_setreg(sc, 0x1c, 0x0a);
    393 	pseye_setreg(sc, 0x1d, 0x08);	/* enable UVC header */
    394 	pseye_setreg(sc, 0x1d, 0x0e);
    395 
    396 	pseye_setregv(sc, 0x8d, 0x1c);
    397 	pseye_setregv(sc, 0x8e, 0x80);
    398 	pseye_setregv(sc, 0xe5, 0x04);
    399 
    400 	pseye_sccb_setreg(sc, 0x12, 0x80);
    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 	pseye_sccb_setreg(sc, 0x11, 0x01);
    410 	pseye_sccb_setreg(sc, 0x11, 0x01);
    411 	pseye_sccb_setreg(sc, 0x11, 0x01);
    412 
    413 	pseye_sccb_setreg(sc, 0x3d, 0x03);
    414 	pseye_sccb_setreg(sc, 0x17, 0x26);
    415 	pseye_sccb_setreg(sc, 0x18, 0xa0);
    416 	pseye_sccb_setreg(sc, 0x19, 0x07);
    417 	pseye_sccb_setreg(sc, 0x1a, 0xf0);
    418 	pseye_sccb_setreg(sc, 0x32, 0x00);
    419 	pseye_sccb_setreg(sc, 0x29, 0xa0);
    420 	pseye_sccb_setreg(sc, 0x2c, 0xf0);
    421 	pseye_sccb_setreg(sc, 0x65, 0x20);
    422 	pseye_sccb_setreg(sc, 0x11, 0x01);
    423 	pseye_sccb_setreg(sc, 0x42, 0x7f);
    424 	pseye_sccb_setreg(sc, 0x63, 0xe0);
    425 	pseye_sccb_setreg(sc, 0x64, 0xff);
    426 	pseye_sccb_setreg(sc, 0x66, 0x00);
    427 	pseye_sccb_setreg(sc, 0x13, 0xf0);
    428 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    429 	pseye_sccb_setreg(sc, 0x0f, 0xc5);
    430 	pseye_sccb_setreg(sc, 0x14, 0x11);
    431 
    432 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    433 	pseye_sccb_setreg(sc, 0x23, 0x03);
    434 	pseye_sccb_setreg(sc, 0x24, 0x40);
    435 	pseye_sccb_setreg(sc, 0x25, 0x30);
    436 	pseye_sccb_setreg(sc, 0x26, 0xa1);
    437 	pseye_sccb_setreg(sc, 0x2a, 0x00);
    438 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    439 	pseye_sccb_setreg(sc, 0x6b, 0xaa);
    440 	pseye_sccb_setreg(sc, 0x13, 0xff);
    441 
    442 	pseye_sccb_setreg(sc, 0x90, 0x05);
    443 	pseye_sccb_setreg(sc, 0x91, 0x01);
    444 	pseye_sccb_setreg(sc, 0x92, 0x03);
    445 	pseye_sccb_setreg(sc, 0x93, 0x00);
    446 	pseye_sccb_setreg(sc, 0x94, 0x60);
    447 	pseye_sccb_setreg(sc, 0x95, 0x3c);
    448 	pseye_sccb_setreg(sc, 0x96, 0x24);
    449 	pseye_sccb_setreg(sc, 0x97, 0x1e);
    450 	pseye_sccb_setreg(sc, 0x98, 0x62);
    451 	pseye_sccb_setreg(sc, 0x99, 0x80);
    452 	pseye_sccb_setreg(sc, 0x9a, 0x1e);
    453 	pseye_sccb_setreg(sc, 0x9b, 0x08);
    454 	pseye_sccb_setreg(sc, 0x9c, 0x20);
    455 	pseye_sccb_setreg(sc, 0x9e, 0x81);
    456 
    457 	pseye_sccb_setreg(sc, 0xa6, 0x04);
    458 	pseye_sccb_setreg(sc, 0x7e, 0x0c);
    459 	pseye_sccb_setreg(sc, 0x7f, 0x16);
    460 
    461 	pseye_sccb_setreg(sc, 0x80, 0x2a);
    462 	pseye_sccb_setreg(sc, 0x81, 0x4e);
    463 	pseye_sccb_setreg(sc, 0x82, 0x61);
    464 	pseye_sccb_setreg(sc, 0x83, 0x6f);
    465 	pseye_sccb_setreg(sc, 0x84, 0x7b);
    466 	pseye_sccb_setreg(sc, 0x85, 0x86);
    467 	pseye_sccb_setreg(sc, 0x86, 0x8e);
    468 	pseye_sccb_setreg(sc, 0x87, 0x97);
    469 	pseye_sccb_setreg(sc, 0x88, 0xa4);
    470 	pseye_sccb_setreg(sc, 0x89, 0xaf);
    471 	pseye_sccb_setreg(sc, 0x8a, 0xc5);
    472 	pseye_sccb_setreg(sc, 0x8b, 0xd7);
    473 	pseye_sccb_setreg(sc, 0x8c, 0xe8);
    474 	pseye_sccb_setreg(sc, 0x8d, 0x20);
    475 
    476 	pseye_sccb_setreg(sc, 0x0c, 0x90);
    477 
    478 	pseye_setregv(sc, 0xc0, 0x50);
    479 	pseye_setregv(sc, 0xc1, 0x3c);
    480 	pseye_setregv(sc, 0xc2, 0x0c);
    481 
    482 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    483 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    484 	pseye_sccb_setreg(sc, 0x23, 0x03);
    485 	pseye_sccb_setreg(sc, 0x11, 0x01);
    486 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    487 	pseye_sccb_setreg(sc, 0x64, 0xff);
    488 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    489 
    490 	pseye_sccb_setreg(sc, 0x14, 0x41);
    491 	pseye_sccb_setreg(sc, 0x0e, 0xcd);
    492 	pseye_sccb_setreg(sc, 0xac, 0xbf);
    493 	pseye_sccb_setreg(sc, 0x8e, 0x00);
    494 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    495 
    496 	pseye_stop(sc);
    497 }
    498 
    499 static void
    500 pseye_sccb_init(struct pseye_softc *sc)
    501 {
    502 	pseye_setregv(sc, 0xe7, 0x3a);
    503 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    504 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    505 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    506 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
    507 }
    508 
    509 static void
    510 pseye_stop(struct pseye_softc *sc)
    511 {
    512 	pseye_led(sc, false);
    513 	pseye_setreg(sc, 0xe0, 0x09);
    514 }
    515 
    516 static void
    517 pseye_start(struct pseye_softc *sc)
    518 {
    519 	pseye_led(sc, true);
    520 	pseye_setreg(sc, 0xe0, 0x00);
    521 }
    522 
    523 static void
    524 pseye_led(struct pseye_softc *sc, bool enabled)
    525 {
    526 	uint8_t val;
    527 
    528 	val = pseye_getreg(sc, 0x21);
    529 	pseye_setreg(sc, 0x21, val | 0x80);
    530 
    531 	val = pseye_getreg(sc, 0x23);
    532 	if (enabled == true)
    533 		val |= 0x80;
    534 	else
    535 		val &= ~0x80;
    536 	pseye_setreg(sc, 0x23, val);
    537 }
    538 
    539 static uint8_t
    540 pseye_getreg(struct pseye_softc *sc, uint16_t reg)
    541 {
    542 	usb_device_request_t req;
    543 	usbd_status err;
    544 	uint8_t buf;
    545 
    546 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    547 	req.bRequest = 1;
    548 	USETW(req.wValue, 0x0000);
    549 	USETW(req.wIndex, reg);
    550 	USETW(req.wLength, 1);
    551 
    552 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    553 	if (err) {
    554 		aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
    555 		    reg, usbd_errstr(err));
    556 		return 0xff;
    557 	}
    558 
    559 	return buf;
    560 }
    561 
    562 static void
    563 pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    564 {
    565 	usb_device_request_t req;
    566 	usbd_status err;
    567 
    568 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    569 	req.bRequest = 1;
    570 	USETW(req.wValue, 0x0000);
    571 	USETW(req.wIndex, reg);
    572 	USETW(req.wLength, 1);
    573 
    574 	err = usbd_do_request(sc->sc_udev, &req, &val);
    575 	if (err)
    576 		aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
    577 		    reg, usbd_errstr(err));
    578 }
    579 
    580 static void
    581 pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    582 {
    583 	pseye_setreg(sc, reg, val);
    584 	if (pseye_getreg(sc, reg) != val)
    585 		aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
    586 		    reg);
    587 }
    588 
    589 static void
    590 pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
    591 {
    592 	pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
    593 	pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
    594 	pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
    595 
    596 	if (pseye_sccb_status(sc) == false)
    597 		aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
    598 		    reg);
    599 }
    600 
    601 static bool
    602 pseye_sccb_status(struct pseye_softc *sc)
    603 {
    604 	int retry = 5;
    605 	uint8_t reg;
    606 
    607 	while (retry-- >= 0) {
    608 		reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
    609 		if (reg == 0x00)
    610 			return true;
    611 		else if (reg == 0x04)
    612 			return false;
    613 	}
    614 
    615 	aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
    616 	return false;
    617 }
    618 
    619 static usbd_status
    620 pseye_get_frame(struct pseye_softc *sc, uint32_t *plen)
    621 {
    622 	if (sc->sc_dying)
    623 		return USBD_IOERROR;
    624 
    625 	return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
    626 	    USBD_SHORT_XFER_OK, 1000, sc->sc_bulkin_buffer, plen);
    627 }
    628 
    629 static int
    630 pseye_init_pipes(struct pseye_softc *sc)
    631 {
    632 	usbd_status err;
    633 
    634 	if (sc->sc_dying)
    635 		return EIO;
    636 
    637 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
    638 	    &sc->sc_bulkin_pipe);
    639 	if (err) {
    640 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    641 		    usbd_errstr(err));
    642 		return ENOMEM;
    643 	}
    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 	return 0;
    658 }
    659 
    660 static void
    661 pseye_submit_payload(struct pseye_softc *sc, uint32_t tlen)
    662 {
    663 	struct video_payload payload;
    664 	uvideo_payload_header_t *uvchdr;
    665 	uint8_t *buf = sc->sc_bulkin_buffer;
    666 	uint32_t len;
    667 	uint32_t brem = (640*480*2);
    668 
    669 	while (brem > 0 && tlen > 0) {
    670 		len = min(tlen, PSEYE_BULKIN_BLKLEN);
    671 		if (len < UVIDEO_PAYLOAD_HEADER_SIZE) {
    672 			printf("pseye_submit_payload: len=%u\n", len);
    673 			return;
    674 		}
    675 
    676 		uvchdr = (uvideo_payload_header_t *)buf;
    677 		if (uvchdr->bHeaderLength != UVIDEO_PAYLOAD_HEADER_SIZE)
    678 			goto next;
    679 		if (uvchdr->bHeaderLength == len &&
    680 		    !(uvchdr->bmHeaderInfo & UV_END_OF_FRAME))
    681 			goto next;
    682 		if (uvchdr->bmHeaderInfo & UV_ERROR)
    683 			return;
    684 		if ((uvchdr->bmHeaderInfo & UV_PRES_TIME) == 0)
    685 			goto next;
    686 
    687 		payload.data = buf + uvchdr->bHeaderLength;
    688 		payload.size = min(brem, len - uvchdr->bHeaderLength);
    689 		payload.frameno = UGETDW(&buf[2]);
    690 		payload.end_of_frame = uvchdr->bmHeaderInfo & UV_END_OF_FRAME;
    691 		video_submit_payload(sc->sc_videodev, &payload);
    692 
    693 next:
    694 		tlen -= len;
    695 		buf += len;
    696 		brem -= payload.size;
    697 	}
    698 }
    699 
    700 static void
    701 pseye_transfer_thread(void *opaque)
    702 {
    703 	struct pseye_softc *sc = opaque;
    704 	uint32_t len;
    705 	int error;
    706 
    707 	while (sc->sc_running) {
    708 		len = sc->sc_bulkin_bufferlen;
    709 		error = pseye_get_frame(sc, &len);
    710 		if (error == USBD_NORMAL_COMPLETION)
    711 			pseye_submit_payload(sc, len);
    712 	}
    713 
    714 	mutex_enter(&sc->sc_mtx);
    715 	cv_broadcast(&sc->sc_cv);
    716 	mutex_exit(&sc->sc_mtx);
    717 
    718 	kthread_exit(0);
    719 }
    720 
    721 /* video(9) API implementations */
    722 static int
    723 pseye_open(void *opaque, int flags)
    724 {
    725 	struct pseye_softc *sc = opaque;
    726 
    727 	if (sc->sc_dying)
    728 		return EIO;
    729 
    730 	pseye_start(sc);
    731 
    732 	return 0;
    733 }
    734 
    735 static void
    736 pseye_close(void *opaque)
    737 {
    738 	struct pseye_softc *sc = opaque;
    739 
    740 	pseye_stop(sc);
    741 }
    742 
    743 static const char *
    744 pseye_get_devname(void *opaque)
    745 {
    746 	return "PlayStation Eye";
    747 }
    748 
    749 static const char *
    750 pseye_get_businfo(void *opaque)
    751 {
    752 	struct pseye_softc *sc = opaque;
    753 
    754 	return sc->sc_businfo;
    755 }
    756 
    757 static int
    758 pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
    759 {
    760 	if (index != 0)
    761 		return EINVAL;
    762 	return pseye_get_format(opaque, format);
    763 }
    764 
    765 static int
    766 pseye_get_format(void *opaque, struct video_format *format)
    767 {
    768 	format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
    769 	format->width = 640;
    770 	format->height = 480;
    771 	format->aspect_x = 4;
    772 	format->aspect_y = 3;
    773 	format->sample_size = format->width * format->height * 2;
    774 	format->stride = format->width * 2;
    775 	format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
    776 	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
    777 	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
    778 	format->interlace_flags = VIDEO_INTERLACE_ON;
    779 	format->priv = 0;
    780 
    781 	return 0;
    782 }
    783 
    784 static int
    785 pseye_set_format(void *opaque, struct video_format *format)
    786 {
    787 #if notyet
    788 	if (format->pixel_format != VIDEO_FORMAT_YUYV)
    789 		return EINVAL;
    790 	if (format->width != 640 || format->height != 480)
    791 		return EINVAL;
    792 #endif
    793 	/* XXX */
    794 	return pseye_get_format(opaque, format);
    795 }
    796 
    797 static int
    798 pseye_try_format(void *opaque, struct video_format *format)
    799 {
    800 	return pseye_get_format(opaque, format);
    801 }
    802 
    803 static int
    804 pseye_start_transfer(void *opaque)
    805 {
    806 	struct pseye_softc *sc = opaque;
    807 	int err = 0;
    808 
    809 	mutex_enter(&sc->sc_mtx);
    810 	if (sc->sc_running == 0) {
    811 		sc->sc_running = 1;
    812 		err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
    813 		    opaque, NULL, "%s", device_xname(sc->sc_dev));
    814 	} else
    815 		aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
    816 	mutex_exit(&sc->sc_mtx);
    817 
    818 	return err;
    819 }
    820 
    821 static int
    822 pseye_stop_transfer(void *opaque)
    823 {
    824 	struct pseye_softc *sc = opaque;
    825 
    826 	mutex_enter(&sc->sc_mtx);
    827 	if (sc->sc_running) {
    828 		sc->sc_running = 0;
    829 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    830 	}
    831 	mutex_exit(&sc->sc_mtx);
    832 
    833 	return 0;
    834 }
    835 
    836 MODULE(MODULE_CLASS_DRIVER, pseye, NULL);
    837 
    838 #ifdef _MODULE
    839 #include "ioconf.c"
    840 #endif
    841 
    842 static int
    843 pseye_modcmd(modcmd_t cmd, void *opaque)
    844 {
    845 	switch (cmd) {
    846 	case MODULE_CMD_INIT:
    847 #ifdef _MODULE
    848 		return config_init_component(cfdriver_ioconf_pseye,
    849 		    cfattach_ioconf_pseye, cfdata_ioconf_pseye);
    850 #else
    851 		return 0;
    852 #endif
    853 	case MODULE_CMD_FINI:
    854 #ifdef _MODULE
    855 		return config_fini_component(cfdriver_ioconf_pseye,
    856 		    cfattach_ioconf_pseye, cfdata_ioconf_pseye);
    857 #else
    858 		return 0;
    859 #endif
    860 	default:
    861 		return ENOTTY;
    862 	}
    863 }
    864