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