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