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