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