Home | History | Annotate | Line # | Download | only in usb
pseye.c revision 1.10.2.2
      1  1.10.2.2  haad /* $NetBSD: pseye.c,v 1.10.2.2 2008/10/19 22:17:10 haad Exp $ */
      2  1.10.2.2  haad 
      3  1.10.2.2  haad /*-
      4  1.10.2.2  haad  * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.10.2.2  haad  * All rights reserved.
      6  1.10.2.2  haad  *
      7  1.10.2.2  haad  * Redistribution and use in source and binary forms, with or without
      8  1.10.2.2  haad  * modification, are permitted provided that the following conditions
      9  1.10.2.2  haad  * are met:
     10  1.10.2.2  haad  * 1. Redistributions of source code must retain the above copyright
     11  1.10.2.2  haad  *    notice, this list of conditions and the following disclaimer.
     12  1.10.2.2  haad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.10.2.2  haad  *    notice, this list of conditions and the following disclaimer in the
     14  1.10.2.2  haad  *    documentation and/or other materials provided with the distribution.
     15  1.10.2.2  haad  *
     16  1.10.2.2  haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.10.2.2  haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.10.2.2  haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.10.2.2  haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.10.2.2  haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.10.2.2  haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.10.2.2  haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.10.2.2  haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.10.2.2  haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.10.2.2  haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.10.2.2  haad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.10.2.2  haad  */
     28  1.10.2.2  haad 
     29  1.10.2.2  haad /*
     30  1.10.2.2  haad  * Sony PLAYSTATION(R) Eye Driver
     31  1.10.2.2  haad  *
     32  1.10.2.2  haad  * The only documentation we have for this part is based on a series
     33  1.10.2.2  haad  * of forum postings by Jim Paris on ps2dev.org. Many thanks for
     34  1.10.2.2  haad  * figuring this one out.
     35  1.10.2.2  haad  *
     36  1.10.2.2  haad  * URL: http://forums.ps2dev.org/viewtopic.php?t=9238
     37  1.10.2.2  haad  */
     38  1.10.2.2  haad 
     39  1.10.2.2  haad #include <sys/cdefs.h>
     40  1.10.2.2  haad __KERNEL_RCSID(0, "$NetBSD: pseye.c,v 1.10.2.2 2008/10/19 22:17:10 haad Exp $");
     41  1.10.2.2  haad 
     42  1.10.2.2  haad #include <sys/param.h>
     43  1.10.2.2  haad #include <sys/systm.h>
     44  1.10.2.2  haad #include <sys/device.h>
     45  1.10.2.2  haad #include <sys/malloc.h>
     46  1.10.2.2  haad #include <sys/fcntl.h>
     47  1.10.2.2  haad #include <sys/conf.h>
     48  1.10.2.2  haad #include <sys/poll.h>
     49  1.10.2.2  haad #include <sys/bus.h>
     50  1.10.2.2  haad #include <sys/mutex.h>
     51  1.10.2.2  haad #include <sys/kthread.h>
     52  1.10.2.2  haad #include <sys/condvar.h>
     53  1.10.2.2  haad 
     54  1.10.2.2  haad #include <uvm/uvm_extern.h>
     55  1.10.2.2  haad 
     56  1.10.2.2  haad #include <dev/usb/usb.h>
     57  1.10.2.2  haad #include <dev/usb/usbdi.h>
     58  1.10.2.2  haad #include <dev/usb/usbdi_util.h>
     59  1.10.2.2  haad #include <dev/usb/usbdevs.h>
     60  1.10.2.2  haad 
     61  1.10.2.2  haad #include <dev/video_if.h>
     62  1.10.2.2  haad 
     63  1.10.2.2  haad #define PRI_PSEYE	PRI_BIO
     64  1.10.2.2  haad 
     65  1.10.2.2  haad /* Bulk-in buffer length */
     66  1.10.2.2  haad #define PSEYE_BULKIN_BUFLEN	(640 * 480 * 2)
     67  1.10.2.2  haad 
     68  1.10.2.2  haad /* SCCB/sensor interface */
     69  1.10.2.2  haad #define PSEYE_SCCB_ADDRESS	0xf1
     70  1.10.2.2  haad #define PSEYE_SCCB_SUBADDR	0xf2
     71  1.10.2.2  haad #define PSEYE_SCCB_WRITE	0xf3
     72  1.10.2.2  haad #define PSEYE_SCCB_READ		0xf4
     73  1.10.2.2  haad #define PSEYE_SCCB_OPERATION	0xf5
     74  1.10.2.2  haad #define PSEYE_SCCB_STATUS	0xf6
     75  1.10.2.2  haad 
     76  1.10.2.2  haad #define PSEYE_SCCB_OP_WRITE_3	0x37
     77  1.10.2.2  haad #define PSEYE_SCCB_OP_WRITE_2	0x33
     78  1.10.2.2  haad #define PSEYE_SCCB_OP_READ_2	0xf9
     79  1.10.2.2  haad 
     80  1.10.2.2  haad struct pseye_softc {
     81  1.10.2.2  haad 	USBBASEDEVICE		sc_dev;
     82  1.10.2.2  haad 
     83  1.10.2.2  haad 	usbd_device_handle	sc_udev;
     84  1.10.2.2  haad 	usbd_interface_handle	sc_iface;
     85  1.10.2.2  haad 
     86  1.10.2.2  haad 	device_t		sc_videodev;
     87  1.10.2.2  haad 	char			sc_running;
     88  1.10.2.2  haad 
     89  1.10.2.2  haad 	kcondvar_t		sc_cv;
     90  1.10.2.2  haad 	kmutex_t		sc_mtx;
     91  1.10.2.2  haad 
     92  1.10.2.2  haad 	usbd_pipe_handle	sc_bulkin_pipe;
     93  1.10.2.2  haad 	usbd_xfer_handle	sc_bulkin_xfer;
     94  1.10.2.2  haad 	int			sc_bulkin;
     95  1.10.2.2  haad 	uint8_t			*sc_bulkin_buffer;
     96  1.10.2.2  haad 	int			sc_bulkin_bufferlen;
     97  1.10.2.2  haad 
     98  1.10.2.2  haad 	char			sc_dying;
     99  1.10.2.2  haad };
    100  1.10.2.2  haad 
    101  1.10.2.2  haad static int	pseye_match(device_t, cfdata_t, void *);
    102  1.10.2.2  haad static void	pseye_attach(device_t, device_t, void *);
    103  1.10.2.2  haad static int	pseye_detach(device_t, int);
    104  1.10.2.2  haad static void	pseye_childdet(device_t, device_t);
    105  1.10.2.2  haad static int	pseye_activate(device_t, enum devact);
    106  1.10.2.2  haad 
    107  1.10.2.2  haad static void	pseye_init(struct pseye_softc *);
    108  1.10.2.2  haad static void	pseye_sccb_init(struct pseye_softc *);
    109  1.10.2.2  haad static void	pseye_stop(struct pseye_softc *);
    110  1.10.2.2  haad static void	pseye_start(struct pseye_softc *);
    111  1.10.2.2  haad static void	pseye_led(struct pseye_softc *, bool);
    112  1.10.2.2  haad static uint8_t	pseye_getreg(struct pseye_softc *, uint16_t);
    113  1.10.2.2  haad static void	pseye_setreg(struct pseye_softc *, uint16_t, uint8_t);
    114  1.10.2.2  haad static void	pseye_setregv(struct pseye_softc *, uint16_t, uint8_t);
    115  1.10.2.2  haad static void	pseye_sccb_setreg(struct pseye_softc *, uint8_t, uint8_t);
    116  1.10.2.2  haad static bool	pseye_sccb_status(struct pseye_softc *);
    117  1.10.2.2  haad 
    118  1.10.2.2  haad static int	pseye_init_pipes(struct pseye_softc *);
    119  1.10.2.2  haad static int	pseye_close_pipes(struct pseye_softc *);
    120  1.10.2.2  haad 
    121  1.10.2.2  haad static usbd_status	pseye_get_frame(struct pseye_softc *);
    122  1.10.2.2  haad 
    123  1.10.2.2  haad /* video(9) API */
    124  1.10.2.2  haad static int		pseye_open(void *, int);
    125  1.10.2.2  haad static void		pseye_close(void *);
    126  1.10.2.2  haad static const char *	pseye_get_devname(void *);
    127  1.10.2.2  haad static int		pseye_enum_format(void *, uint32_t,
    128  1.10.2.2  haad 					  struct video_format *);
    129  1.10.2.2  haad static int		pseye_get_format(void *, struct video_format *);
    130  1.10.2.2  haad static int		pseye_set_format(void *, struct video_format *);
    131  1.10.2.2  haad static int		pseye_try_format(void *, struct video_format *);
    132  1.10.2.2  haad static int		pseye_start_transfer(void *);
    133  1.10.2.2  haad static int		pseye_stop_transfer(void *);
    134  1.10.2.2  haad 
    135  1.10.2.2  haad CFATTACH_DECL2_NEW(pseye, sizeof(struct pseye_softc),
    136  1.10.2.2  haad     pseye_match, pseye_attach, pseye_detach, pseye_activate,
    137  1.10.2.2  haad     NULL, pseye_childdet);
    138  1.10.2.2  haad 
    139  1.10.2.2  haad static const struct video_hw_if pseye_hw_if = {
    140  1.10.2.2  haad 	.open = pseye_open,
    141  1.10.2.2  haad 	.close = pseye_close,
    142  1.10.2.2  haad 	.get_devname = pseye_get_devname,
    143  1.10.2.2  haad 	.enum_format = pseye_enum_format,
    144  1.10.2.2  haad 	.get_format = pseye_get_format,
    145  1.10.2.2  haad 	.set_format = pseye_set_format,
    146  1.10.2.2  haad 	.try_format = pseye_try_format,
    147  1.10.2.2  haad 	.start_transfer = pseye_start_transfer,
    148  1.10.2.2  haad 	.stop_transfer = pseye_stop_transfer,
    149  1.10.2.2  haad 	.control_iter_init = NULL,
    150  1.10.2.2  haad 	.control_iter_next = NULL,
    151  1.10.2.2  haad 	.get_control_desc_group = NULL,
    152  1.10.2.2  haad 	.get_control_group = NULL,
    153  1.10.2.2  haad 	.set_control_group = NULL,
    154  1.10.2.2  haad };
    155  1.10.2.2  haad 
    156  1.10.2.2  haad static int
    157  1.10.2.2  haad pseye_match(device_t parent, cfdata_t match, void *opaque)
    158  1.10.2.2  haad {
    159  1.10.2.2  haad 	struct usbif_attach_arg *uaa = opaque;
    160  1.10.2.2  haad 
    161  1.10.2.2  haad 	if (uaa->class != UICLASS_VENDOR)
    162  1.10.2.2  haad 		return UMATCH_NONE;
    163  1.10.2.2  haad 
    164  1.10.2.2  haad 	if (uaa->vendor == USB_VENDOR_OMNIVISION2) {
    165  1.10.2.2  haad 		switch (uaa->product) {
    166  1.10.2.2  haad 		case USB_PRODUCT_OMNIVISION2_PSEYE:
    167  1.10.2.2  haad 			if (uaa->ifaceno != 0)
    168  1.10.2.2  haad 				return UMATCH_NONE;
    169  1.10.2.2  haad 			return UMATCH_VENDOR_PRODUCT;
    170  1.10.2.2  haad 		}
    171  1.10.2.2  haad 	}
    172  1.10.2.2  haad 
    173  1.10.2.2  haad 	return UMATCH_NONE;
    174  1.10.2.2  haad }
    175  1.10.2.2  haad 
    176  1.10.2.2  haad static void
    177  1.10.2.2  haad pseye_attach(device_t parent, device_t self, void *opaque)
    178  1.10.2.2  haad {
    179  1.10.2.2  haad 	struct pseye_softc *sc = device_private(self);
    180  1.10.2.2  haad 	struct usbif_attach_arg *uaa = opaque;
    181  1.10.2.2  haad 	usbd_device_handle dev = uaa->device;
    182  1.10.2.2  haad 	usb_interface_descriptor_t *id = NULL;
    183  1.10.2.2  haad 	usb_endpoint_descriptor_t *ed = NULL, *ed_bulkin = NULL;
    184  1.10.2.2  haad 	char *devinfo;
    185  1.10.2.2  haad 	int i;
    186  1.10.2.2  haad 
    187  1.10.2.2  haad 	devinfo = usbd_devinfo_alloc(dev, 0);
    188  1.10.2.2  haad 	aprint_naive("\n");
    189  1.10.2.2  haad 	aprint_normal(": %s\n", devinfo);
    190  1.10.2.2  haad 	usbd_devinfo_free(devinfo);
    191  1.10.2.2  haad 
    192  1.10.2.2  haad 	sc->sc_dev = self;
    193  1.10.2.2  haad 	sc->sc_udev = dev;
    194  1.10.2.2  haad 	sc->sc_iface = uaa->iface;
    195  1.10.2.2  haad 	sc->sc_bulkin_bufferlen = PSEYE_BULKIN_BUFLEN;
    196  1.10.2.2  haad 
    197  1.10.2.2  haad 	sc->sc_dying = sc->sc_running = 0;
    198  1.10.2.2  haad 	cv_init(&sc->sc_cv, device_xname(self));
    199  1.10.2.2  haad 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    200  1.10.2.2  haad 
    201  1.10.2.2  haad 	id = usbd_get_interface_descriptor(sc->sc_iface);
    202  1.10.2.2  haad 	if (id == NULL) {
    203  1.10.2.2  haad 		aprint_error_dev(self, "failed to get interface descriptor\n");
    204  1.10.2.2  haad 		sc->sc_dying = 1;
    205  1.10.2.2  haad 		return;
    206  1.10.2.2  haad 	}
    207  1.10.2.2  haad 
    208  1.10.2.2  haad 	for (i = 0; i < id->bNumEndpoints; i++) {
    209  1.10.2.2  haad 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    210  1.10.2.2  haad 		if (ed == NULL) {
    211  1.10.2.2  haad 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    212  1.10.2.2  haad 			sc->sc_dying = 1;
    213  1.10.2.2  haad 			return;
    214  1.10.2.2  haad 		}
    215  1.10.2.2  haad 
    216  1.10.2.2  haad 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    217  1.10.2.2  haad 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    218  1.10.2.2  haad 			ed_bulkin = ed;
    219  1.10.2.2  haad 			break;
    220  1.10.2.2  haad 		}
    221  1.10.2.2  haad 	}
    222  1.10.2.2  haad 
    223  1.10.2.2  haad 	if (ed_bulkin == NULL) {
    224  1.10.2.2  haad 		aprint_error_dev(self, "no bulk-in endpoint found\n");
    225  1.10.2.2  haad 		sc->sc_dying = 1;
    226  1.10.2.2  haad 		return;
    227  1.10.2.2  haad 	}
    228  1.10.2.2  haad 
    229  1.10.2.2  haad 	sc->sc_bulkin = ed_bulkin->bEndpointAddress;
    230  1.10.2.2  haad 
    231  1.10.2.2  haad 	sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
    232  1.10.2.2  haad 	if (sc->sc_bulkin_xfer == NULL) {
    233  1.10.2.2  haad 		sc->sc_dying = 1;
    234  1.10.2.2  haad 		return;
    235  1.10.2.2  haad 	}
    236  1.10.2.2  haad 	sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
    237  1.10.2.2  haad 	    sc->sc_bulkin_bufferlen);
    238  1.10.2.2  haad 	if (sc->sc_bulkin_buffer == NULL) {
    239  1.10.2.2  haad 		usbd_free_xfer(sc->sc_bulkin_xfer);
    240  1.10.2.2  haad 		sc->sc_bulkin_xfer = NULL;
    241  1.10.2.2  haad 		sc->sc_dying = 1;
    242  1.10.2.2  haad 		return;
    243  1.10.2.2  haad 	}
    244  1.10.2.2  haad 
    245  1.10.2.2  haad 	pseye_init(sc);
    246  1.10.2.2  haad 
    247  1.10.2.2  haad 	if (!pmf_device_register(self, NULL, NULL))
    248  1.10.2.2  haad 		aprint_error_dev(self, "couldn't establish power handler\n");
    249  1.10.2.2  haad 
    250  1.10.2.2  haad 	sc->sc_videodev = video_attach_mi(&pseye_hw_if, self);
    251  1.10.2.2  haad 	if (sc->sc_videodev == NULL) {
    252  1.10.2.2  haad 		aprint_error_dev(self, "couldn't attach video layer\n");
    253  1.10.2.2  haad 		sc->sc_dying = 1;
    254  1.10.2.2  haad 		return;
    255  1.10.2.2  haad 	}
    256  1.10.2.2  haad 
    257  1.10.2.2  haad 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    258  1.10.2.2  haad 	    USBDEV(self));
    259  1.10.2.2  haad 
    260  1.10.2.2  haad }
    261  1.10.2.2  haad 
    262  1.10.2.2  haad static int
    263  1.10.2.2  haad pseye_detach(device_t self, int flags)
    264  1.10.2.2  haad {
    265  1.10.2.2  haad 	struct pseye_softc *sc = device_private(self);
    266  1.10.2.2  haad 
    267  1.10.2.2  haad 	sc->sc_dying = 1;
    268  1.10.2.2  haad 
    269  1.10.2.2  haad 	pmf_device_deregister(self);
    270  1.10.2.2  haad 
    271  1.10.2.2  haad 	if (sc->sc_videodev != NULL) {
    272  1.10.2.2  haad 		config_detach(sc->sc_videodev, flags);
    273  1.10.2.2  haad 		sc->sc_videodev = NULL;
    274  1.10.2.2  haad 	}
    275  1.10.2.2  haad 
    276  1.10.2.2  haad 	if (sc->sc_bulkin_xfer != NULL) {
    277  1.10.2.2  haad 		usbd_free_xfer(sc->sc_bulkin_xfer);
    278  1.10.2.2  haad 		sc->sc_bulkin_xfer = NULL;
    279  1.10.2.2  haad 	}
    280  1.10.2.2  haad 
    281  1.10.2.2  haad 	if (sc->sc_bulkin_pipe != NULL) {
    282  1.10.2.2  haad 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    283  1.10.2.2  haad 		sc->sc_bulkin_pipe = NULL;
    284  1.10.2.2  haad 	}
    285  1.10.2.2  haad 
    286  1.10.2.2  haad 	mutex_enter(&sc->sc_mtx);
    287  1.10.2.2  haad 	if (sc->sc_running) {
    288  1.10.2.2  haad 		sc->sc_running = 0;
    289  1.10.2.2  haad 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    290  1.10.2.2  haad 	}
    291  1.10.2.2  haad 	mutex_exit(&sc->sc_mtx);
    292  1.10.2.2  haad 
    293  1.10.2.2  haad 	cv_destroy(&sc->sc_cv);
    294  1.10.2.2  haad 	mutex_destroy(&sc->sc_mtx);
    295  1.10.2.2  haad 
    296  1.10.2.2  haad 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    297  1.10.2.2  haad 	    USBDEV(sc->sc_dev));
    298  1.10.2.2  haad 
    299  1.10.2.2  haad 	return 0;
    300  1.10.2.2  haad }
    301  1.10.2.2  haad 
    302  1.10.2.2  haad int
    303  1.10.2.2  haad pseye_activate(device_ptr_t self, enum devact act)
    304  1.10.2.2  haad {
    305  1.10.2.2  haad 	struct pseye_softc *sc = device_private(self);
    306  1.10.2.2  haad 	int rv;
    307  1.10.2.2  haad 
    308  1.10.2.2  haad 	rv = 0;
    309  1.10.2.2  haad 
    310  1.10.2.2  haad 	switch (act) {
    311  1.10.2.2  haad 	case DVACT_ACTIVATE:
    312  1.10.2.2  haad 		break;
    313  1.10.2.2  haad 	case DVACT_DEACTIVATE:
    314  1.10.2.2  haad 		sc->sc_dying = 1;
    315  1.10.2.2  haad 		break;
    316  1.10.2.2  haad 	}
    317  1.10.2.2  haad 
    318  1.10.2.2  haad 	return rv;
    319  1.10.2.2  haad }
    320  1.10.2.2  haad 
    321  1.10.2.2  haad static void
    322  1.10.2.2  haad pseye_childdet(device_t self, device_t child)
    323  1.10.2.2  haad {
    324  1.10.2.2  haad 	struct pseye_softc *sc = device_private(self);
    325  1.10.2.2  haad 
    326  1.10.2.2  haad 	if (sc->sc_videodev) {
    327  1.10.2.2  haad 		KASSERT(sc->sc_videodev == child);
    328  1.10.2.2  haad 		sc->sc_videodev = NULL;
    329  1.10.2.2  haad 	}
    330  1.10.2.2  haad }
    331  1.10.2.2  haad 
    332  1.10.2.2  haad /*
    333  1.10.2.2  haad  * Device access
    334  1.10.2.2  haad  */
    335  1.10.2.2  haad 
    336  1.10.2.2  haad static void
    337  1.10.2.2  haad pseye_init(struct pseye_softc *sc)
    338  1.10.2.2  haad {
    339  1.10.2.2  haad 	pseye_sccb_init(sc);
    340  1.10.2.2  haad 
    341  1.10.2.2  haad 	pseye_setregv(sc, 0xc2, 0x0c);
    342  1.10.2.2  haad 	pseye_setregv(sc, 0x88, 0xf8);
    343  1.10.2.2  haad 	pseye_setregv(sc, 0xc3, 0x69);
    344  1.10.2.2  haad 	pseye_setregv(sc, 0x89, 0xff);
    345  1.10.2.2  haad 	pseye_setregv(sc, 0x76, 0x03);
    346  1.10.2.2  haad 	pseye_setregv(sc, 0x92, 0x01);
    347  1.10.2.2  haad 	pseye_setregv(sc, 0x93, 0x18);
    348  1.10.2.2  haad 	pseye_setregv(sc, 0x94, 0x10);
    349  1.10.2.2  haad 	pseye_setregv(sc, 0x95, 0x10);
    350  1.10.2.2  haad 	pseye_setregv(sc, 0xe2, 0x00);
    351  1.10.2.2  haad 	pseye_setregv(sc, 0xe7, 0x3e);
    352  1.10.2.2  haad 
    353  1.10.2.2  haad 	pseye_setreg(sc, 0x1c, 0x0a);
    354  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x22);
    355  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x06);
    356  1.10.2.2  haad 	pseye_setregv(sc, 0x96, 0x00);
    357  1.10.2.2  haad 
    358  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x20);
    359  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x20);
    360  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x20);
    361  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x0a);
    362  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x3f);
    363  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x4a);
    364  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x20);
    365  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x15);
    366  1.10.2.2  haad 	pseye_setreg(sc, 0x97, 0x0b);
    367  1.10.2.2  haad 
    368  1.10.2.2  haad 	pseye_setregv(sc, 0x8e, 0x40);
    369  1.10.2.2  haad 	pseye_setregv(sc, 0x1f, 0x81);
    370  1.10.2.2  haad 	pseye_setregv(sc, 0x34, 0x05);
    371  1.10.2.2  haad 	pseye_setregv(sc, 0xe3, 0x04);
    372  1.10.2.2  haad 	pseye_setregv(sc, 0x88, 0x00);
    373  1.10.2.2  haad 	pseye_setregv(sc, 0x89, 0x00);
    374  1.10.2.2  haad 	pseye_setregv(sc, 0x76, 0x00);
    375  1.10.2.2  haad 	pseye_setregv(sc, 0xe7, 0x2e);
    376  1.10.2.2  haad 	pseye_setregv(sc, 0x31, 0xf9);
    377  1.10.2.2  haad 	pseye_setregv(sc, 0x25, 0x42);
    378  1.10.2.2  haad 	pseye_setregv(sc, 0x21, 0xf0);
    379  1.10.2.2  haad 
    380  1.10.2.2  haad 	pseye_setreg(sc, 0x1c, 0x00);
    381  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x40);
    382  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x02);
    383  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x00);
    384  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x02);
    385  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0x57);
    386  1.10.2.2  haad 	pseye_setreg(sc, 0x1d, 0xff);
    387  1.10.2.2  haad 
    388  1.10.2.2  haad 	pseye_setregv(sc, 0x8d, 0x1c);
    389  1.10.2.2  haad 	pseye_setregv(sc, 0x8e, 0x80);
    390  1.10.2.2  haad 	pseye_setregv(sc, 0xe5, 0x04);
    391  1.10.2.2  haad 
    392  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x12, 0x80);
    393  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    394  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    395  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    396  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    397  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    398  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    399  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    400  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    401  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    402  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    403  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    404  1.10.2.2  haad 
    405  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x3d, 0x03);
    406  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x17, 0x26);
    407  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x18, 0xa0);
    408  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x19, 0x07);
    409  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x1a, 0xf0);
    410  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x32, 0x00);
    411  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x29, 0xa0);
    412  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x2c, 0xf0);
    413  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x65, 0x20);
    414  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    415  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x42, 0x7f);
    416  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x63, 0xe0);
    417  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x64, 0xff);
    418  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x66, 0x00);
    419  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x13, 0xf0);
    420  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    421  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0f, 0xc5);
    422  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x14, 0x11);
    423  1.10.2.2  haad 
    424  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    425  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x23, 0x03);
    426  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x24, 0x40);
    427  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x25, 0x30);
    428  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x26, 0xa1);
    429  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x2a, 0x00);
    430  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    431  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x6b, 0xaa);
    432  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x13, 0xff);
    433  1.10.2.2  haad 
    434  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x90, 0x05);
    435  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x91, 0x01);
    436  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x92, 0x03);
    437  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x93, 0x00);
    438  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x94, 0x60);
    439  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x95, 0x3c);
    440  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x96, 0x24);
    441  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x97, 0x1e);
    442  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x98, 0x62);
    443  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x99, 0x80);
    444  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x9a, 0x1e);
    445  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x9b, 0x08);
    446  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x9c, 0x20);
    447  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x9e, 0x81);
    448  1.10.2.2  haad 
    449  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0xa6, 0x04);
    450  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x7e, 0x0c);
    451  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x7f, 0x16);
    452  1.10.2.2  haad 
    453  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x80, 0x2a);
    454  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x81, 0x4e);
    455  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x82, 0x61);
    456  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x83, 0x6f);
    457  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x84, 0x7b);
    458  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x85, 0x86);
    459  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x86, 0x8e);
    460  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x87, 0x97);
    461  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x88, 0xa4);
    462  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x89, 0xaf);
    463  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x8a, 0xc5);
    464  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x8b, 0xd7);
    465  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x8c, 0xe8);
    466  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x8d, 0x20);
    467  1.10.2.2  haad 
    468  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0c, 0x90);
    469  1.10.2.2  haad 
    470  1.10.2.2  haad 	pseye_setregv(sc, 0xc0, 0x50);
    471  1.10.2.2  haad 	pseye_setregv(sc, 0xc1, 0x3c);
    472  1.10.2.2  haad 	pseye_setregv(sc, 0xc2, 0x0c);
    473  1.10.2.2  haad 
    474  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x2b, 0x00);
    475  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x22, 0x7f);
    476  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x23, 0x03);
    477  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x11, 0x01);
    478  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    479  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x64, 0xff);
    480  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0d, 0x41);
    481  1.10.2.2  haad 
    482  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x14, 0x41);
    483  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0e, 0xcd);
    484  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0xac, 0xbf);
    485  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x8e, 0x00);
    486  1.10.2.2  haad 	pseye_sccb_setreg(sc, 0x0c, 0xd0);
    487  1.10.2.2  haad 
    488  1.10.2.2  haad 	pseye_stop(sc);
    489  1.10.2.2  haad }
    490  1.10.2.2  haad 
    491  1.10.2.2  haad static void
    492  1.10.2.2  haad pseye_sccb_init(struct pseye_softc *sc)
    493  1.10.2.2  haad {
    494  1.10.2.2  haad 	pseye_setregv(sc, 0xe7, 0x3a);
    495  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    496  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    497  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x60);
    498  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_ADDRESS, 0x42);
    499  1.10.2.2  haad }
    500  1.10.2.2  haad 
    501  1.10.2.2  haad static void
    502  1.10.2.2  haad pseye_stop(struct pseye_softc *sc)
    503  1.10.2.2  haad {
    504  1.10.2.2  haad 	pseye_led(sc, false);
    505  1.10.2.2  haad 	pseye_setreg(sc, 0xe0, 0x09);
    506  1.10.2.2  haad }
    507  1.10.2.2  haad 
    508  1.10.2.2  haad static void
    509  1.10.2.2  haad pseye_start(struct pseye_softc *sc)
    510  1.10.2.2  haad {
    511  1.10.2.2  haad 	pseye_led(sc, true);
    512  1.10.2.2  haad 	pseye_setreg(sc, 0xe0, 0x00);
    513  1.10.2.2  haad }
    514  1.10.2.2  haad 
    515  1.10.2.2  haad static void
    516  1.10.2.2  haad pseye_led(struct pseye_softc *sc, bool enabled)
    517  1.10.2.2  haad {
    518  1.10.2.2  haad 	uint8_t val;
    519  1.10.2.2  haad 
    520  1.10.2.2  haad 	val = pseye_getreg(sc, 0x21);
    521  1.10.2.2  haad 	pseye_setreg(sc, 0x21, val | 0x80);
    522  1.10.2.2  haad 
    523  1.10.2.2  haad 	val = pseye_getreg(sc, 0x23);
    524  1.10.2.2  haad 	if (enabled == true)
    525  1.10.2.2  haad 		val |= 0x80;
    526  1.10.2.2  haad 	else
    527  1.10.2.2  haad 		val &= ~0x80;
    528  1.10.2.2  haad 	pseye_setreg(sc, 0x23, val);
    529  1.10.2.2  haad }
    530  1.10.2.2  haad 
    531  1.10.2.2  haad static uint8_t
    532  1.10.2.2  haad pseye_getreg(struct pseye_softc *sc, uint16_t reg)
    533  1.10.2.2  haad {
    534  1.10.2.2  haad 	usb_device_request_t req;
    535  1.10.2.2  haad 	usbd_status err;
    536  1.10.2.2  haad 	uint8_t buf;
    537  1.10.2.2  haad 
    538  1.10.2.2  haad 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    539  1.10.2.2  haad 	req.bRequest = 1;
    540  1.10.2.2  haad 	USETW(req.wValue, 0x0000);
    541  1.10.2.2  haad 	USETW(req.wIndex, reg);
    542  1.10.2.2  haad 	USETW(req.wLength, 1);
    543  1.10.2.2  haad 
    544  1.10.2.2  haad 	err = usbd_do_request(sc->sc_udev, &req, &buf);
    545  1.10.2.2  haad 	if (err) {
    546  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "couldn't read reg 0x%04x: %s\n",
    547  1.10.2.2  haad 		    reg, usbd_errstr(err));
    548  1.10.2.2  haad 		return 0xff;
    549  1.10.2.2  haad 	}
    550  1.10.2.2  haad 
    551  1.10.2.2  haad 	return buf;
    552  1.10.2.2  haad }
    553  1.10.2.2  haad 
    554  1.10.2.2  haad static void
    555  1.10.2.2  haad pseye_setreg(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    556  1.10.2.2  haad {
    557  1.10.2.2  haad 	usb_device_request_t req;
    558  1.10.2.2  haad 	usbd_status err;
    559  1.10.2.2  haad 
    560  1.10.2.2  haad 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    561  1.10.2.2  haad 	req.bRequest = 1;
    562  1.10.2.2  haad 	USETW(req.wValue, 0x0000);
    563  1.10.2.2  haad 	USETW(req.wIndex, reg);
    564  1.10.2.2  haad 	USETW(req.wLength, 1);
    565  1.10.2.2  haad 
    566  1.10.2.2  haad 	err = usbd_do_request(sc->sc_udev, &req, &val);
    567  1.10.2.2  haad 	if (err)
    568  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "couldn't write reg 0x%04x: %s\n",
    569  1.10.2.2  haad 		    reg, usbd_errstr(err));
    570  1.10.2.2  haad }
    571  1.10.2.2  haad 
    572  1.10.2.2  haad static void
    573  1.10.2.2  haad pseye_setregv(struct pseye_softc *sc, uint16_t reg, uint8_t val)
    574  1.10.2.2  haad {
    575  1.10.2.2  haad 	pseye_setreg(sc, reg, val);
    576  1.10.2.2  haad 	if (pseye_getreg(sc, reg) != val)
    577  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "couldn't verify reg 0x%04x\n",
    578  1.10.2.2  haad 		    reg);
    579  1.10.2.2  haad }
    580  1.10.2.2  haad 
    581  1.10.2.2  haad static void
    582  1.10.2.2  haad pseye_sccb_setreg(struct pseye_softc *sc, uint8_t reg, uint8_t val)
    583  1.10.2.2  haad {
    584  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_SUBADDR, reg);
    585  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_WRITE, val);
    586  1.10.2.2  haad 	pseye_setreg(sc, PSEYE_SCCB_OPERATION, PSEYE_SCCB_OP_WRITE_3);
    587  1.10.2.2  haad 
    588  1.10.2.2  haad 	if (pseye_sccb_status(sc) == false)
    589  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "couldn't write sccb reg 0x%04x\n",
    590  1.10.2.2  haad 		    reg);
    591  1.10.2.2  haad }
    592  1.10.2.2  haad 
    593  1.10.2.2  haad static bool
    594  1.10.2.2  haad pseye_sccb_status(struct pseye_softc *sc)
    595  1.10.2.2  haad {
    596  1.10.2.2  haad 	int retry = 5;
    597  1.10.2.2  haad 	uint8_t reg;
    598  1.10.2.2  haad 
    599  1.10.2.2  haad 	while (retry-- >= 0) {
    600  1.10.2.2  haad 		reg = pseye_getreg(sc, PSEYE_SCCB_STATUS);
    601  1.10.2.2  haad 		if (reg == 0x00)
    602  1.10.2.2  haad 			return true;
    603  1.10.2.2  haad 		else if (reg == 0x04)
    604  1.10.2.2  haad 			return false;
    605  1.10.2.2  haad 	}
    606  1.10.2.2  haad 
    607  1.10.2.2  haad 	aprint_error_dev(sc->sc_dev, "timeout reading sccb status\n");
    608  1.10.2.2  haad 	return false;
    609  1.10.2.2  haad }
    610  1.10.2.2  haad 
    611  1.10.2.2  haad static usbd_status
    612  1.10.2.2  haad pseye_get_frame(struct pseye_softc *sc)
    613  1.10.2.2  haad {
    614  1.10.2.2  haad 	uint32_t len = sc->sc_bulkin_bufferlen;
    615  1.10.2.2  haad 
    616  1.10.2.2  haad 	if (sc->sc_dying)
    617  1.10.2.2  haad 		return USBD_IOERROR;
    618  1.10.2.2  haad 
    619  1.10.2.2  haad 	return usbd_bulk_transfer(sc->sc_bulkin_xfer, sc->sc_bulkin_pipe,
    620  1.10.2.2  haad 	    USBD_SHORT_XFER_OK|USBD_NO_COPY, 50,
    621  1.10.2.2  haad 	    sc->sc_bulkin_buffer, &len, "pseyerb");
    622  1.10.2.2  haad }
    623  1.10.2.2  haad 
    624  1.10.2.2  haad static int
    625  1.10.2.2  haad pseye_init_pipes(struct pseye_softc *sc)
    626  1.10.2.2  haad {
    627  1.10.2.2  haad 	usbd_status err;
    628  1.10.2.2  haad 
    629  1.10.2.2  haad 	if (sc->sc_dying)
    630  1.10.2.2  haad 		return EIO;
    631  1.10.2.2  haad 
    632  1.10.2.2  haad 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, 0,
    633  1.10.2.2  haad 	    &sc->sc_bulkin_pipe);
    634  1.10.2.2  haad 	if (err) {
    635  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    636  1.10.2.2  haad 		    usbd_errstr(err));
    637  1.10.2.2  haad 		return ENOMEM;
    638  1.10.2.2  haad 	}
    639  1.10.2.2  haad 
    640  1.10.2.2  haad 	pseye_start(sc);
    641  1.10.2.2  haad 
    642  1.10.2.2  haad 	return 0;
    643  1.10.2.2  haad }
    644  1.10.2.2  haad 
    645  1.10.2.2  haad int
    646  1.10.2.2  haad pseye_close_pipes(struct pseye_softc *sc)
    647  1.10.2.2  haad {
    648  1.10.2.2  haad 	if (sc->sc_bulkin_pipe != NULL) {
    649  1.10.2.2  haad 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    650  1.10.2.2  haad 		usbd_close_pipe(sc->sc_bulkin_pipe);
    651  1.10.2.2  haad 		sc->sc_bulkin_pipe = NULL;
    652  1.10.2.2  haad 	}
    653  1.10.2.2  haad 
    654  1.10.2.2  haad 	pseye_stop(sc);
    655  1.10.2.2  haad 
    656  1.10.2.2  haad 	return 0;
    657  1.10.2.2  haad }
    658  1.10.2.2  haad 
    659  1.10.2.2  haad static void
    660  1.10.2.2  haad pseye_transfer_thread(void *opaque)
    661  1.10.2.2  haad {
    662  1.10.2.2  haad 	struct pseye_softc *sc = opaque;
    663  1.10.2.2  haad 	int error;
    664  1.10.2.2  haad 	struct video_payload payload;
    665  1.10.2.2  haad 
    666  1.10.2.2  haad 	payload.frameno = 0;
    667  1.10.2.2  haad 
    668  1.10.2.2  haad 	while (sc->sc_running) {
    669  1.10.2.2  haad 		error = pseye_get_frame(sc);
    670  1.10.2.2  haad 		if (error == USBD_NORMAL_COMPLETION) {
    671  1.10.2.2  haad 			payload.data = sc->sc_bulkin_buffer;
    672  1.10.2.2  haad 			payload.size = sc->sc_bulkin_bufferlen;
    673  1.10.2.2  haad 			payload.frameno = (payload.frameno + 1) & 1;
    674  1.10.2.2  haad 			payload.end_of_frame = 1;
    675  1.10.2.2  haad 			video_submit_payload(sc->sc_videodev, &payload);
    676  1.10.2.2  haad 		}
    677  1.10.2.2  haad 	}
    678  1.10.2.2  haad 
    679  1.10.2.2  haad 	mutex_enter(&sc->sc_mtx);
    680  1.10.2.2  haad 	cv_broadcast(&sc->sc_cv);
    681  1.10.2.2  haad 	mutex_exit(&sc->sc_mtx);
    682  1.10.2.2  haad 
    683  1.10.2.2  haad 	kthread_exit(0);
    684  1.10.2.2  haad }
    685  1.10.2.2  haad 
    686  1.10.2.2  haad /* video(9) API implementations */
    687  1.10.2.2  haad static int
    688  1.10.2.2  haad pseye_open(void *opaque, int flags)
    689  1.10.2.2  haad {
    690  1.10.2.2  haad 	struct pseye_softc *sc = opaque;
    691  1.10.2.2  haad 
    692  1.10.2.2  haad 	if (sc->sc_dying)
    693  1.10.2.2  haad 		return EIO;
    694  1.10.2.2  haad 
    695  1.10.2.2  haad 	return pseye_init_pipes(sc);
    696  1.10.2.2  haad }
    697  1.10.2.2  haad 
    698  1.10.2.2  haad static void
    699  1.10.2.2  haad pseye_close(void *opaque)
    700  1.10.2.2  haad {
    701  1.10.2.2  haad 	struct pseye_softc *sc = opaque;
    702  1.10.2.2  haad 
    703  1.10.2.2  haad 	pseye_close_pipes(sc);
    704  1.10.2.2  haad }
    705  1.10.2.2  haad 
    706  1.10.2.2  haad static const char *
    707  1.10.2.2  haad pseye_get_devname(void *opaque)
    708  1.10.2.2  haad {
    709  1.10.2.2  haad 	return "PLAYSTATION(R) Eye";
    710  1.10.2.2  haad }
    711  1.10.2.2  haad 
    712  1.10.2.2  haad static int
    713  1.10.2.2  haad pseye_enum_format(void *opaque, uint32_t index, struct video_format *format)
    714  1.10.2.2  haad {
    715  1.10.2.2  haad 	if (index != 0)
    716  1.10.2.2  haad 		return EINVAL;
    717  1.10.2.2  haad 	return pseye_get_format(opaque, format);
    718  1.10.2.2  haad }
    719  1.10.2.2  haad 
    720  1.10.2.2  haad static int
    721  1.10.2.2  haad pseye_get_format(void *opaque, struct video_format *format)
    722  1.10.2.2  haad {
    723  1.10.2.2  haad 	format->pixel_format = VIDEO_FORMAT_YUY2; /* XXX actually YUYV */
    724  1.10.2.2  haad 	format->width = 640;
    725  1.10.2.2  haad 	format->height = 480;
    726  1.10.2.2  haad 	format->aspect_x = 4;
    727  1.10.2.2  haad 	format->aspect_y = 3;
    728  1.10.2.2  haad 	format->sample_size = format->width * format->height * 2;
    729  1.10.2.2  haad 	format->stride = format->width * 2;
    730  1.10.2.2  haad 	format->color.primaries = VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
    731  1.10.2.2  haad 	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
    732  1.10.2.2  haad 	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
    733  1.10.2.2  haad 	format->interlace_flags = VIDEO_INTERLACE_ON;
    734  1.10.2.2  haad 	format->priv = 0;
    735  1.10.2.2  haad 
    736  1.10.2.2  haad 	return 0;
    737  1.10.2.2  haad }
    738  1.10.2.2  haad 
    739  1.10.2.2  haad static int
    740  1.10.2.2  haad pseye_set_format(void *opaque, struct video_format *format)
    741  1.10.2.2  haad {
    742  1.10.2.2  haad #if notyet
    743  1.10.2.2  haad 	if (format->pixel_format != VIDEO_FORMAT_YUYV)
    744  1.10.2.2  haad 		return EINVAL;
    745  1.10.2.2  haad 	if (format->width != 640 || format->height != 480)
    746  1.10.2.2  haad 		return EINVAL;
    747  1.10.2.2  haad #endif
    748  1.10.2.2  haad 	/* XXX */
    749  1.10.2.2  haad 	return pseye_get_format(opaque, format);
    750  1.10.2.2  haad }
    751  1.10.2.2  haad 
    752  1.10.2.2  haad static int
    753  1.10.2.2  haad pseye_try_format(void *opaque, struct video_format *format)
    754  1.10.2.2  haad {
    755  1.10.2.2  haad 	return pseye_get_format(opaque, format);
    756  1.10.2.2  haad }
    757  1.10.2.2  haad 
    758  1.10.2.2  haad static int
    759  1.10.2.2  haad pseye_start_transfer(void *opaque)
    760  1.10.2.2  haad {
    761  1.10.2.2  haad 	struct pseye_softc *sc = opaque;
    762  1.10.2.2  haad 	int err = 0;
    763  1.10.2.2  haad 
    764  1.10.2.2  haad 	mutex_enter(&sc->sc_mtx);
    765  1.10.2.2  haad 	if (sc->sc_running == 0) {
    766  1.10.2.2  haad 		sc->sc_running = 1;
    767  1.10.2.2  haad 		err = kthread_create(PRI_PSEYE, 0, NULL, pseye_transfer_thread,
    768  1.10.2.2  haad 		    opaque, NULL, device_xname(sc->sc_dev));
    769  1.10.2.2  haad 	} else
    770  1.10.2.2  haad 		aprint_error_dev(sc->sc_dev, "transfer already in progress\n");
    771  1.10.2.2  haad 	mutex_exit(&sc->sc_mtx);
    772  1.10.2.2  haad 
    773  1.10.2.2  haad 	return err;
    774  1.10.2.2  haad }
    775  1.10.2.2  haad 
    776  1.10.2.2  haad static int
    777  1.10.2.2  haad pseye_stop_transfer(void *opaque)
    778  1.10.2.2  haad {
    779  1.10.2.2  haad 	struct pseye_softc *sc = opaque;
    780  1.10.2.2  haad 
    781  1.10.2.2  haad 	mutex_enter(&sc->sc_mtx);
    782  1.10.2.2  haad 	if (sc->sc_running) {
    783  1.10.2.2  haad 		sc->sc_running = 0;
    784  1.10.2.2  haad 		cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
    785  1.10.2.2  haad 	}
    786  1.10.2.2  haad 	mutex_exit(&sc->sc_mtx);
    787  1.10.2.2  haad 
    788  1.10.2.2  haad 	return 0;
    789  1.10.2.2  haad }
    790