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