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