1 /* $NetBSD: video.c,v 1.37 2020/05/22 11:23:51 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Patrick Mahoney <pat@polycrystal.org> 5 * All rights reserved. 6 * 7 * This code was written by Patrick Mahoney (pat@polycrystal.org) as 8 * part of Google Summer of Code 2008. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This ia a Video4Linux 2 compatible /dev/video driver for NetBSD 34 * 35 * See http://v4l2spec.bytesex.org/ for Video4Linux 2 specifications 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: video.c,v 1.37 2020/05/22 11:23:51 jmcneill Exp $"); 40 41 #include "video.h" 42 #if NVIDEO > 0 43 44 #include <sys/param.h> 45 #include <sys/ioctl.h> 46 #include <sys/fcntl.h> 47 #include <sys/vnode.h> 48 #include <sys/poll.h> 49 #include <sys/select.h> 50 #include <sys/kmem.h> 51 #include <sys/pool.h> 52 #include <sys/conf.h> 53 #include <sys/types.h> 54 #include <sys/device.h> 55 #include <sys/condvar.h> 56 #include <sys/queue.h> 57 #include <sys/videoio.h> 58 59 #include <dev/video_if.h> 60 61 #include "ioconf.h" 62 63 /* #define VIDEO_DEBUG 1 */ 64 65 #ifdef VIDEO_DEBUG 66 #define DPRINTF(x) do { if (videodebug) printf x; } while (0) 67 #define DPRINTFN(n,x) do { if (videodebug>(n)) printf x; } while (0) 68 int videodebug = VIDEO_DEBUG; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 #define PAGE_ALIGN(a) (((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) 75 76 #define VIDEO_DRIVER_VERSION \ 77 (((__NetBSD_Version__ / 100000000) << 16) | \ 78 ((__NetBSD_Version__ / 1000000 % 100) << 8) | \ 79 (__NetBSD_Version__ / 100 % 100)) 80 81 /* TODO: move to sys/intr.h */ 82 #define IPL_VIDEO IPL_VM 83 #define splvideo() splvm() 84 85 #define VIDEO_MIN_BUFS 2 86 #define VIDEO_MAX_BUFS 32 87 #define VIDEO_NUM_BUFS 4 88 89 /* Scatter Buffer - an array of fixed size (PAGE_SIZE) chunks 90 * allocated non-contiguously and functions to get data into and out 91 * of the scatter buffer. */ 92 struct scatter_buf { 93 pool_cache_t sb_pool; 94 size_t sb_size; /* size in bytes */ 95 size_t sb_npages; /* number of pages */ 96 uint8_t **sb_page_ary; /* array of page pointers */ 97 }; 98 99 struct scatter_io { 100 struct scatter_buf *sio_buf; 101 off_t sio_offset; 102 size_t sio_resid; 103 }; 104 105 static void scatter_buf_init(struct scatter_buf *); 106 static void scatter_buf_destroy(struct scatter_buf *); 107 static int scatter_buf_set_size(struct scatter_buf *, size_t); 108 static paddr_t scatter_buf_map(struct scatter_buf *, off_t); 109 110 static bool scatter_io_init(struct scatter_buf *, off_t, size_t, struct scatter_io *); 111 static bool scatter_io_next(struct scatter_io *, void **, size_t *); 112 static void scatter_io_undo(struct scatter_io *, size_t); 113 static void scatter_io_copyin(struct scatter_io *, const void *); 114 /* static void scatter_io_copyout(struct scatter_io *, void *); */ 115 static int scatter_io_uiomove(struct scatter_io *, struct uio *); 116 117 118 enum video_stream_method { 119 VIDEO_STREAM_METHOD_NONE, 120 VIDEO_STREAM_METHOD_READ, 121 VIDEO_STREAM_METHOD_MMAP, 122 VIDEO_STREAM_METHOD_USERPTR 123 }; 124 125 struct video_buffer { 126 struct v4l2_buffer *vb_buf; 127 SIMPLEQ_ENTRY(video_buffer) entries; 128 }; 129 130 SIMPLEQ_HEAD(sample_queue, video_buffer); 131 132 struct video_stream { 133 int vs_flags; /* flags given to open() */ 134 135 struct video_format vs_format; 136 137 int vs_frameno; /* toggles between 0 and 1, 138 * or -1 if new */ 139 uint32_t vs_sequence; /* absoulte frame/sample number in 140 * sequence, wraps around */ 141 bool vs_drop; /* drop payloads from current 142 * frameno? */ 143 144 enum v4l2_buf_type vs_type; 145 uint8_t vs_nbufs; 146 struct video_buffer **vs_buf; 147 148 struct scatter_buf vs_data; /* stores video data for MMAP 149 * and READ */ 150 151 /* Video samples may exist in different locations. Initially, 152 * samples are queued into the ingress queue. The driver 153 * grabs these in turn and fills them with video data. Once 154 * filled, they are moved to the egress queue. Samples are 155 * dequeued either by user with MMAP method or, with READ 156 * method, videoread() works from the fist sample in the 157 * ingress queue without dequeing. In the first case, the 158 * user re-queues the buffer when finished, and videoread() 159 * does the same when all data has been read. The sample now 160 * returns to the ingress queue. */ 161 struct sample_queue vs_ingress; /* samples under driver control */ 162 struct sample_queue vs_egress; /* samples headed for userspace */ 163 164 bool vs_streaming; 165 enum video_stream_method vs_method; /* method by which 166 * userspace will read 167 * samples */ 168 169 kmutex_t vs_lock; /* Lock to manipulate queues. 170 * Should also be held when 171 * changing number of 172 * buffers. */ 173 kcondvar_t vs_sample_cv; /* signaled on new 174 * ingress sample */ 175 struct selinfo vs_sel; 176 177 uint32_t vs_bytesread; /* bytes read() from current 178 * sample thus far */ 179 }; 180 181 struct video_softc { 182 device_t sc_dev; 183 device_t hw_dev; /* Hardware (parent) device */ 184 void * hw_softc; /* Hardware device private softc */ 185 const struct video_hw_if *hw_if; /* Hardware interface */ 186 187 u_int sc_open; 188 int sc_refcnt; 189 int sc_opencnt; 190 bool sc_dying; 191 192 struct video_stream sc_stream_in; 193 }; 194 static int video_print(void *, const char *); 195 196 static int video_match(device_t, cfdata_t, void *); 197 static void video_attach(device_t, device_t, void *); 198 static int video_detach(device_t, int); 199 static int video_activate(device_t, enum devact); 200 201 dev_type_open(videoopen); 202 dev_type_close(videoclose); 203 dev_type_read(videoread); 204 dev_type_write(videowrite); 205 dev_type_ioctl(videoioctl); 206 dev_type_poll(videopoll); 207 dev_type_mmap(videommap); 208 209 const struct cdevsw video_cdevsw = { 210 .d_open = videoopen, 211 .d_close = videoclose, 212 .d_read = videoread, 213 .d_write = videowrite, 214 .d_ioctl = videoioctl, 215 .d_stop = nostop, 216 .d_tty = notty, 217 .d_poll = videopoll, 218 .d_mmap = videommap, 219 .d_kqfilter = nokqfilter, 220 .d_discard = nodiscard, 221 .d_flag = D_OTHER 222 }; 223 224 #define VIDEOUNIT(n) (minor(n)) 225 226 CFATTACH_DECL_NEW(video, sizeof(struct video_softc), 227 video_match, video_attach, video_detach, video_activate); 228 229 static const char * video_pixel_format_str(enum video_pixel_format); 230 231 /* convert various values from V4L2 to native values of this driver */ 232 static uint16_t v4l2id_to_control_id(uint32_t); 233 static uint32_t control_flags_to_v4l2flags(uint32_t); 234 static enum v4l2_ctrl_type control_type_to_v4l2type(enum video_control_type); 235 236 static void v4l2_format_to_video_format(const struct v4l2_format *, 237 struct video_format *); 238 static void video_format_to_v4l2_format(const struct video_format *, 239 struct v4l2_format *); 240 static void v4l2_standard_to_video_standard(v4l2_std_id, 241 enum video_standard *); 242 static void video_standard_to_v4l2_standard(enum video_standard, 243 struct v4l2_standard *); 244 static void v4l2_input_to_video_input(const struct v4l2_input *, 245 struct video_input *); 246 static void video_input_to_v4l2_input(const struct video_input *, 247 struct v4l2_input *); 248 static void v4l2_audio_to_video_audio(const struct v4l2_audio *, 249 struct video_audio *); 250 static void video_audio_to_v4l2_audio(const struct video_audio *, 251 struct v4l2_audio *); 252 static void v4l2_tuner_to_video_tuner(const struct v4l2_tuner *, 253 struct video_tuner *); 254 static void video_tuner_to_v4l2_tuner(const struct video_tuner *, 255 struct v4l2_tuner *); 256 257 /* V4L2 api functions, typically called from videoioctl() */ 258 static int video_enum_format(struct video_softc *, struct v4l2_fmtdesc *); 259 static int video_get_format(struct video_softc *, 260 struct v4l2_format *); 261 static int video_set_format(struct video_softc *, 262 struct v4l2_format *); 263 static int video_try_format(struct video_softc *, 264 struct v4l2_format *); 265 static int video_get_parm(struct video_softc *, 266 struct v4l2_streamparm *); 267 static int video_set_parm(struct video_softc *, 268 struct v4l2_streamparm *); 269 static int video_enum_standard(struct video_softc *, 270 struct v4l2_standard *); 271 static int video_get_standard(struct video_softc *, v4l2_std_id *); 272 static int video_set_standard(struct video_softc *, v4l2_std_id); 273 static int video_enum_input(struct video_softc *, struct v4l2_input *); 274 static int video_get_input(struct video_softc *, int *); 275 static int video_set_input(struct video_softc *, int); 276 static int video_enum_audio(struct video_softc *, struct v4l2_audio *); 277 static int video_get_audio(struct video_softc *, struct v4l2_audio *); 278 static int video_set_audio(struct video_softc *, struct v4l2_audio *); 279 static int video_get_tuner(struct video_softc *, struct v4l2_tuner *); 280 static int video_set_tuner(struct video_softc *, struct v4l2_tuner *); 281 static int video_get_frequency(struct video_softc *, 282 struct v4l2_frequency *); 283 static int video_set_frequency(struct video_softc *, 284 struct v4l2_frequency *); 285 static int video_query_control(struct video_softc *, 286 struct v4l2_queryctrl *); 287 static int video_get_control(struct video_softc *, 288 struct v4l2_control *); 289 static int video_set_control(struct video_softc *, 290 const struct v4l2_control *); 291 static int video_request_bufs(struct video_softc *, 292 struct v4l2_requestbuffers *); 293 static int video_query_buf(struct video_softc *, struct v4l2_buffer *); 294 static int video_queue_buf(struct video_softc *, struct v4l2_buffer *); 295 static int video_dequeue_buf(struct video_softc *, struct v4l2_buffer *); 296 static int video_stream_on(struct video_softc *, enum v4l2_buf_type); 297 static int video_stream_off(struct video_softc *, enum v4l2_buf_type); 298 299 static struct video_buffer * video_buffer_alloc(void); 300 static void video_buffer_free(struct video_buffer *); 301 302 303 /* functions for video_stream */ 304 static void video_stream_init(struct video_stream *); 305 static void video_stream_fini(struct video_stream *); 306 307 static int video_stream_setup_bufs(struct video_stream *, 308 enum video_stream_method, 309 uint8_t); 310 static void video_stream_teardown_bufs(struct video_stream *); 311 312 static int video_stream_realloc_bufs(struct video_stream *, uint8_t); 313 #define video_stream_free_bufs(vs) \ 314 video_stream_realloc_bufs((vs), 0) 315 316 static void video_stream_enqueue(struct video_stream *, 317 struct video_buffer *); 318 static struct video_buffer * video_stream_dequeue(struct video_stream *); 319 static void video_stream_write(struct video_stream *, 320 const struct video_payload *); 321 static void video_stream_sample_done(struct video_stream *); 322 323 #ifdef VIDEO_DEBUG 324 /* debugging */ 325 static const char * video_ioctl_str(u_long); 326 #endif 327 328 329 static int 330 video_match(device_t parent, cfdata_t match, void *aux) 331 { 332 #ifdef VIDEO_DEBUG 333 struct video_attach_args *args; 334 335 args = aux; 336 DPRINTF(("video_match: hw=%p\n", args->hw_if)); 337 #endif 338 return 1; 339 } 340 341 342 static void 343 video_attach(device_t parent, device_t self, void *aux) 344 { 345 struct video_softc *sc; 346 struct video_attach_args *args; 347 348 sc = device_private(self); 349 args = aux; 350 351 sc->sc_dev = self; 352 sc->hw_dev = parent; 353 sc->hw_if = args->hw_if; 354 sc->hw_softc = device_private(parent); 355 356 sc->sc_open = 0; 357 sc->sc_refcnt = 0; 358 sc->sc_opencnt = 0; 359 sc->sc_dying = false; 360 361 video_stream_init(&sc->sc_stream_in); 362 363 aprint_naive("\n"); 364 aprint_normal(": %s\n", sc->hw_if->get_devname(sc->hw_softc)); 365 366 DPRINTF(("video_attach: sc=%p hwif=%p\n", sc, sc->hw_if)); 367 368 if (!pmf_device_register(self, NULL, NULL)) 369 aprint_error_dev(self, "couldn't establish power handler\n"); 370 } 371 372 373 static int 374 video_activate(device_t self, enum devact act) 375 { 376 struct video_softc *sc = device_private(self); 377 378 DPRINTF(("video_activate: sc=%p\n", sc)); 379 switch (act) { 380 case DVACT_DEACTIVATE: 381 sc->sc_dying = true; 382 return 0; 383 default: 384 return EOPNOTSUPP; 385 } 386 } 387 388 389 static int 390 video_detach(device_t self, int flags) 391 { 392 struct video_softc *sc; 393 int maj, mn; 394 395 sc = device_private(self); 396 DPRINTF(("video_detach: sc=%p flags=%d\n", sc, flags)); 397 398 sc->sc_dying = true; 399 400 pmf_device_deregister(self); 401 402 maj = cdevsw_lookup_major(&video_cdevsw); 403 mn = device_unit(self); 404 /* close open instances */ 405 vdevgone(maj, mn, mn, VCHR); 406 407 video_stream_fini(&sc->sc_stream_in); 408 409 return 0; 410 } 411 412 413 static int 414 video_print(void *aux, const char *pnp) 415 { 416 if (pnp != NULL) { 417 DPRINTF(("video_print: have pnp\n")); 418 aprint_normal("%s at %s\n", "video", pnp); 419 } else { 420 DPRINTF(("video_print: pnp is NULL\n")); 421 } 422 return UNCONF; 423 } 424 425 426 /* 427 * Called from hardware driver. This is where the MI audio driver 428 * gets probed/attached to the hardware driver. 429 */ 430 device_t 431 video_attach_mi(const struct video_hw_if *hw_if, device_t parent) 432 { 433 struct video_attach_args args; 434 435 args.hw_if = hw_if; 436 return config_found_ia(parent, "videobus", &args, video_print); 437 } 438 439 /* video_submit_payload - called by hardware driver to submit payload data */ 440 void 441 video_submit_payload(device_t self, const struct video_payload *payload) 442 { 443 struct video_softc *sc; 444 445 sc = device_private(self); 446 447 if (sc == NULL) 448 return; 449 450 video_stream_write(&sc->sc_stream_in, payload); 451 } 452 453 static const char * 454 video_pixel_format_str(enum video_pixel_format px) 455 { 456 switch (px) { 457 case VIDEO_FORMAT_UYVY: return "UYVY"; 458 case VIDEO_FORMAT_YUV420: return "YUV420"; 459 case VIDEO_FORMAT_YUY2: return "YUYV"; 460 case VIDEO_FORMAT_NV12: return "NV12"; 461 case VIDEO_FORMAT_RGB24: return "RGB24"; 462 case VIDEO_FORMAT_RGB555: return "RGB555"; 463 case VIDEO_FORMAT_RGB565: return "RGB565"; 464 case VIDEO_FORMAT_SBGGR8: return "SBGGR8"; 465 case VIDEO_FORMAT_MJPEG: return "MJPEG"; 466 case VIDEO_FORMAT_DV: return "DV"; 467 case VIDEO_FORMAT_MPEG: return "MPEG"; 468 default: return "Unknown"; 469 } 470 } 471 472 /* Takes a V4L2 id and returns a "native" video driver control id. 473 * TODO: is there a better way to do this? some kind of array? */ 474 static uint16_t 475 v4l2id_to_control_id(uint32_t v4l2id) 476 { 477 /* mask includes class bits and control id bits */ 478 switch (v4l2id & 0xffffff) { 479 case V4L2_CID_BRIGHTNESS: return VIDEO_CONTROL_BRIGHTNESS; 480 case V4L2_CID_CONTRAST: return VIDEO_CONTROL_CONTRAST; 481 case V4L2_CID_SATURATION: return VIDEO_CONTROL_SATURATION; 482 case V4L2_CID_HUE: return VIDEO_CONTROL_HUE; 483 case V4L2_CID_HUE_AUTO: return VIDEO_CONTROL_HUE_AUTO; 484 case V4L2_CID_SHARPNESS: return VIDEO_CONTROL_SHARPNESS; 485 case V4L2_CID_GAMMA: return VIDEO_CONTROL_GAMMA; 486 487 /* "black level" means the same as "brightness", but V4L2 488 * defines two separate controls that are not identical. 489 * V4L2_CID_BLACK_LEVEL is deprecated however in V4L2. */ 490 case V4L2_CID_BLACK_LEVEL: return VIDEO_CONTROL_BRIGHTNESS; 491 492 case V4L2_CID_AUDIO_VOLUME: return VIDEO_CONTROL_UNDEFINED; 493 case V4L2_CID_AUDIO_BALANCE: return VIDEO_CONTROL_UNDEFINED; 494 case V4L2_CID_AUDIO_BASS: return VIDEO_CONTROL_UNDEFINED; 495 case V4L2_CID_AUDIO_TREBLE: return VIDEO_CONTROL_UNDEFINED; 496 case V4L2_CID_AUDIO_MUTE: return VIDEO_CONTROL_UNDEFINED; 497 case V4L2_CID_AUDIO_LOUDNESS: return VIDEO_CONTROL_UNDEFINED; 498 499 case V4L2_CID_AUTO_WHITE_BALANCE: 500 return VIDEO_CONTROL_WHITE_BALANCE_AUTO; 501 case V4L2_CID_DO_WHITE_BALANCE: 502 return VIDEO_CONTROL_WHITE_BALANCE_ACTION; 503 case V4L2_CID_RED_BALANCE: 504 case V4L2_CID_BLUE_BALANCE: 505 /* This might not fit in with the control_id/value_id scheme */ 506 return VIDEO_CONTROL_WHITE_BALANCE_COMPONENT; 507 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: 508 return VIDEO_CONTROL_WHITE_BALANCE_TEMPERATURE; 509 case V4L2_CID_EXPOSURE: 510 return VIDEO_CONTROL_EXPOSURE_TIME_ABSOLUTE; 511 case V4L2_CID_GAIN: return VIDEO_CONTROL_GAIN; 512 case V4L2_CID_AUTOGAIN: return VIDEO_CONTROL_GAIN_AUTO; 513 case V4L2_CID_HFLIP: return VIDEO_CONTROL_HFLIP; 514 case V4L2_CID_VFLIP: return VIDEO_CONTROL_VFLIP; 515 case V4L2_CID_HCENTER_DEPRECATED: 516 case V4L2_CID_VCENTER_DEPRECATED: 517 return VIDEO_CONTROL_UNDEFINED; 518 case V4L2_CID_POWER_LINE_FREQUENCY: 519 return VIDEO_CONTROL_POWER_LINE_FREQUENCY; 520 case V4L2_CID_BACKLIGHT_COMPENSATION: 521 return VIDEO_CONTROL_BACKLIGHT_COMPENSATION; 522 default: return V4L2_CTRL_ID2CID(v4l2id); 523 } 524 } 525 526 527 static uint32_t 528 control_flags_to_v4l2flags(uint32_t flags) 529 { 530 uint32_t v4l2flags = 0; 531 532 if (flags & VIDEO_CONTROL_FLAG_DISABLED) 533 v4l2flags |= V4L2_CTRL_FLAG_INACTIVE; 534 535 if (!(flags & VIDEO_CONTROL_FLAG_WRITE)) 536 v4l2flags |= V4L2_CTRL_FLAG_READ_ONLY; 537 538 if (flags & VIDEO_CONTROL_FLAG_AUTOUPDATE) 539 v4l2flags |= V4L2_CTRL_FLAG_GRABBED; 540 541 return v4l2flags; 542 } 543 544 545 static enum v4l2_ctrl_type 546 control_type_to_v4l2type(enum video_control_type type) { 547 switch (type) { 548 case VIDEO_CONTROL_TYPE_INT: return V4L2_CTRL_TYPE_INTEGER; 549 case VIDEO_CONTROL_TYPE_BOOL: return V4L2_CTRL_TYPE_BOOLEAN; 550 case VIDEO_CONTROL_TYPE_LIST: return V4L2_CTRL_TYPE_MENU; 551 case VIDEO_CONTROL_TYPE_ACTION: return V4L2_CTRL_TYPE_BUTTON; 552 default: return V4L2_CTRL_TYPE_INTEGER; /* err? */ 553 } 554 } 555 556 557 static int 558 video_query_control(struct video_softc *sc, 559 struct v4l2_queryctrl *query) 560 { 561 const struct video_hw_if *hw; 562 struct video_control_desc_group desc_group; 563 struct video_control_desc desc; 564 int err; 565 566 hw = sc->hw_if; 567 if (hw->get_control_desc_group) { 568 desc.group_id = desc.control_id = 569 v4l2id_to_control_id(query->id); 570 571 desc_group.group_id = desc.group_id; 572 desc_group.length = 1; 573 desc_group.desc = &desc; 574 575 err = hw->get_control_desc_group(sc->hw_softc, &desc_group); 576 if (err != 0) 577 return err; 578 579 query->type = control_type_to_v4l2type(desc.type); 580 memcpy(query->name, desc.name, 32); 581 query->minimum = desc.min; 582 query->maximum = desc.max; 583 query->step = desc.step; 584 query->default_value = desc.def; 585 query->flags = control_flags_to_v4l2flags(desc.flags); 586 587 return 0; 588 } else { 589 return EINVAL; 590 } 591 } 592 593 594 /* Takes a single Video4Linux2 control and queries the driver for the 595 * current value. */ 596 static int 597 video_get_control(struct video_softc *sc, 598 struct v4l2_control *vcontrol) 599 { 600 const struct video_hw_if *hw; 601 struct video_control_group group; 602 struct video_control control; 603 int err; 604 605 hw = sc->hw_if; 606 if (hw->get_control_group) { 607 control.group_id = control.control_id = 608 v4l2id_to_control_id(vcontrol->id); 609 /* ?? if "control_id" is arbitrarily defined by the 610 * driver, then we need some way to store it... Maybe 611 * it doesn't matter for single value controls. */ 612 control.value = 0; 613 614 group.group_id = control.group_id; 615 group.length = 1; 616 group.control = &control; 617 618 err = hw->get_control_group(sc->hw_softc, &group); 619 if (err != 0) 620 return err; 621 622 vcontrol->value = control.value; 623 return 0; 624 } else { 625 return EINVAL; 626 } 627 } 628 629 static void 630 video_format_to_v4l2_format(const struct video_format *src, 631 struct v4l2_format *dest) 632 { 633 /* TODO: what about win and vbi formats? */ 634 dest->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 635 dest->fmt.pix.width = src->width; 636 dest->fmt.pix.height = src->height; 637 if (VIDEO_INTERLACED(src->interlace_flags)) 638 dest->fmt.pix.field = V4L2_FIELD_INTERLACED; 639 else 640 dest->fmt.pix.field = V4L2_FIELD_NONE; 641 dest->fmt.pix.bytesperline = src->stride; 642 dest->fmt.pix.sizeimage = src->sample_size; 643 dest->fmt.pix.priv = src->priv; 644 645 switch (src->color.primaries) { 646 case VIDEO_COLOR_PRIMARIES_SMPTE_170M: 647 dest->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 648 break; 649 /* XXX */ 650 case VIDEO_COLOR_PRIMARIES_UNSPECIFIED: 651 default: 652 dest->fmt.pix.colorspace = 0; 653 break; 654 } 655 656 switch (src->pixel_format) { 657 case VIDEO_FORMAT_UYVY: 658 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; 659 break; 660 case VIDEO_FORMAT_YUV420: 661 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; 662 break; 663 case VIDEO_FORMAT_YUY2: 664 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 665 break; 666 case VIDEO_FORMAT_NV12: 667 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12; 668 break; 669 case VIDEO_FORMAT_RGB24: 670 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; 671 break; 672 case VIDEO_FORMAT_RGB555: 673 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB555; 674 break; 675 case VIDEO_FORMAT_RGB565: 676 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565; 677 break; 678 case VIDEO_FORMAT_SBGGR8: 679 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8; 680 break; 681 case VIDEO_FORMAT_MJPEG: 682 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG; 683 break; 684 case VIDEO_FORMAT_DV: 685 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_DV; 686 break; 687 case VIDEO_FORMAT_MPEG: 688 dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 689 break; 690 case VIDEO_FORMAT_UNDEFINED: 691 default: 692 DPRINTF(("video_get_format: unknown pixel format %d\n", 693 src->pixel_format)); 694 dest->fmt.pix.pixelformat = 0; /* V4L2 doesn't define 695 * and "undefined" 696 * format? */ 697 break; 698 } 699 700 } 701 702 static void 703 v4l2_format_to_video_format(const struct v4l2_format *src, 704 struct video_format *dest) 705 { 706 switch (src->type) { 707 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 708 dest->width = src->fmt.pix.width; 709 dest->height = src->fmt.pix.height; 710 711 dest->stride = src->fmt.pix.bytesperline; 712 dest->sample_size = src->fmt.pix.sizeimage; 713 714 if (src->fmt.pix.field == V4L2_FIELD_INTERLACED) 715 dest->interlace_flags = VIDEO_INTERLACE_ON; 716 else 717 dest->interlace_flags = VIDEO_INTERLACE_OFF; 718 719 switch (src->fmt.pix.colorspace) { 720 case V4L2_COLORSPACE_SMPTE170M: 721 dest->color.primaries = 722 VIDEO_COLOR_PRIMARIES_SMPTE_170M; 723 break; 724 /* XXX */ 725 default: 726 dest->color.primaries = 727 VIDEO_COLOR_PRIMARIES_UNSPECIFIED; 728 break; 729 } 730 731 switch (src->fmt.pix.pixelformat) { 732 case V4L2_PIX_FMT_UYVY: 733 dest->pixel_format = VIDEO_FORMAT_UYVY; 734 break; 735 case V4L2_PIX_FMT_YUV420: 736 dest->pixel_format = VIDEO_FORMAT_YUV420; 737 break; 738 case V4L2_PIX_FMT_YUYV: 739 dest->pixel_format = VIDEO_FORMAT_YUY2; 740 break; 741 case V4L2_PIX_FMT_NV12: 742 dest->pixel_format = VIDEO_FORMAT_NV12; 743 break; 744 case V4L2_PIX_FMT_RGB24: 745 dest->pixel_format = VIDEO_FORMAT_RGB24; 746 break; 747 case V4L2_PIX_FMT_RGB555: 748 dest->pixel_format = VIDEO_FORMAT_RGB555; 749 break; 750 case V4L2_PIX_FMT_RGB565: 751 dest->pixel_format = VIDEO_FORMAT_RGB565; 752 break; 753 case V4L2_PIX_FMT_SBGGR8: 754 dest->pixel_format = VIDEO_FORMAT_SBGGR8; 755 break; 756 case V4L2_PIX_FMT_MJPEG: 757 dest->pixel_format = VIDEO_FORMAT_MJPEG; 758 break; 759 case V4L2_PIX_FMT_DV: 760 dest->pixel_format = VIDEO_FORMAT_DV; 761 break; 762 case V4L2_PIX_FMT_MPEG: 763 dest->pixel_format = VIDEO_FORMAT_MPEG; 764 break; 765 default: 766 DPRINTF(("video: unknown v4l2 pixel format %d\n", 767 src->fmt.pix.pixelformat)); 768 dest->pixel_format = VIDEO_FORMAT_UNDEFINED; 769 break; 770 } 771 break; 772 default: 773 /* TODO: other v4l2 format types */ 774 DPRINTF(("video: unsupported v4l2 format type %d\n", 775 src->type)); 776 break; 777 } 778 } 779 780 static int 781 video_enum_format(struct video_softc *sc, struct v4l2_fmtdesc *fmtdesc) 782 { 783 const struct video_hw_if *hw; 784 struct video_format vfmt; 785 struct v4l2_format fmt; 786 int err; 787 788 hw = sc->hw_if; 789 if (hw->enum_format == NULL) 790 return ENOTTY; 791 792 err = hw->enum_format(sc->hw_softc, fmtdesc->index, &vfmt); 793 if (err != 0) 794 return err; 795 796 video_format_to_v4l2_format(&vfmt, &fmt); 797 798 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* TODO: only one type for now */ 799 fmtdesc->flags = 0; 800 if (vfmt.pixel_format >= VIDEO_FORMAT_MJPEG) 801 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; 802 strlcpy(fmtdesc->description, 803 video_pixel_format_str(vfmt.pixel_format), 804 sizeof(fmtdesc->description)); 805 fmtdesc->pixelformat = fmt.fmt.pix.pixelformat; 806 807 return 0; 808 } 809 810 static int 811 video_get_format(struct video_softc *sc, 812 struct v4l2_format *format) 813 { 814 const struct video_hw_if *hw; 815 struct video_format vfmt; 816 int err; 817 818 hw = sc->hw_if; 819 if (hw->get_format == NULL) 820 return ENOTTY; 821 822 err = hw->get_format(sc->hw_softc, &vfmt); 823 if (err != 0) 824 return err; 825 826 video_format_to_v4l2_format(&vfmt, format); 827 828 return 0; 829 } 830 831 static int 832 video_set_format(struct video_softc *sc, struct v4l2_format *fmt) 833 { 834 const struct video_hw_if *hw; 835 struct video_format vfmt; 836 int err; 837 838 hw = sc->hw_if; 839 if (hw->set_format == NULL) 840 return ENOTTY; 841 842 v4l2_format_to_video_format(fmt, &vfmt); 843 844 err = hw->set_format(sc->hw_softc, &vfmt); 845 if (err != 0) 846 return err; 847 848 video_format_to_v4l2_format(&vfmt, fmt); 849 sc->sc_stream_in.vs_format = vfmt; 850 851 return 0; 852 } 853 854 855 static int 856 video_try_format(struct video_softc *sc, 857 struct v4l2_format *format) 858 { 859 const struct video_hw_if *hw; 860 struct video_format vfmt; 861 int err; 862 863 hw = sc->hw_if; 864 if (hw->try_format == NULL) 865 return ENOTTY; 866 867 v4l2_format_to_video_format(format, &vfmt); 868 869 err = hw->try_format(sc->hw_softc, &vfmt); 870 if (err != 0) 871 return err; 872 873 video_format_to_v4l2_format(&vfmt, format); 874 875 return 0; 876 } 877 878 static int 879 video_get_parm(struct video_softc *sc, struct v4l2_streamparm *parm) 880 { 881 struct video_fract fract; 882 const struct video_hw_if *hw; 883 int error; 884 885 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 886 return EINVAL; 887 888 hw = sc->hw_if; 889 if (hw == NULL) 890 return ENXIO; 891 892 memset(&parm->parm, 0, sizeof(parm->parm)); 893 if (hw->get_framerate != NULL) { 894 error = hw->get_framerate(sc->hw_softc, &fract); 895 if (error != 0) 896 return error; 897 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 898 parm->parm.capture.timeperframe.numerator = fract.numerator; 899 parm->parm.capture.timeperframe.denominator = fract.denominator; 900 } 901 902 return 0; 903 } 904 905 static int 906 video_set_parm(struct video_softc *sc, struct v4l2_streamparm *parm) 907 { 908 struct video_fract fract; 909 const struct video_hw_if *hw; 910 int error; 911 912 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 913 return EINVAL; 914 915 hw = sc->hw_if; 916 if (hw == NULL || hw->set_framerate == NULL) 917 return ENXIO; 918 919 error = hw->set_framerate(sc->hw_softc, &fract); 920 if (error != 0) 921 return error; 922 923 parm->parm.capture.timeperframe.numerator = fract.numerator; 924 parm->parm.capture.timeperframe.denominator = fract.denominator; 925 926 return 0; 927 } 928 929 static void 930 v4l2_standard_to_video_standard(v4l2_std_id stdid, 931 enum video_standard *vstd) 932 { 933 #define VSTD(id, vid) case (id): *vstd = (vid); break; 934 switch (stdid) { 935 VSTD(V4L2_STD_NTSC_M, VIDEO_STANDARD_NTSC_M) 936 default: 937 *vstd = VIDEO_STANDARD_UNKNOWN; 938 break; 939 } 940 #undef VSTD 941 } 942 943 static void 944 video_standard_to_v4l2_standard(enum video_standard vstd, 945 struct v4l2_standard *std) 946 { 947 switch (vstd) { 948 case VIDEO_STANDARD_NTSC_M: 949 std->id = V4L2_STD_NTSC_M; 950 strlcpy(std->name, "NTSC-M", sizeof(std->name)); 951 std->frameperiod.numerator = 1001; 952 std->frameperiod.denominator = 30000; 953 std->framelines = 525; 954 break; 955 default: 956 std->id = V4L2_STD_UNKNOWN; 957 strlcpy(std->name, "Unknown", sizeof(std->name)); 958 break; 959 } 960 } 961 962 static int 963 video_enum_standard(struct video_softc *sc, struct v4l2_standard *std) 964 { 965 const struct video_hw_if *hw = sc->hw_if; 966 enum video_standard vstd; 967 int err; 968 969 /* simple webcam drivers don't need to implement this callback */ 970 if (hw->enum_standard == NULL) { 971 if (std->index != 0) 972 return EINVAL; 973 std->id = V4L2_STD_UNKNOWN; 974 strlcpy(std->name, "webcam", sizeof(std->name)); 975 return 0; 976 } 977 978 v4l2_standard_to_video_standard(std->id, &vstd); 979 980 err = hw->enum_standard(sc->hw_softc, std->index, &vstd); 981 if (err != 0) 982 return err; 983 984 video_standard_to_v4l2_standard(vstd, std); 985 986 return 0; 987 } 988 989 static int 990 video_get_standard(struct video_softc *sc, v4l2_std_id *stdid) 991 { 992 const struct video_hw_if *hw = sc->hw_if; 993 struct v4l2_standard std; 994 enum video_standard vstd; 995 int err; 996 997 /* simple webcam drivers don't need to implement this callback */ 998 if (hw->get_standard == NULL) { 999 *stdid = V4L2_STD_UNKNOWN; 1000 return 0; 1001 } 1002 1003 err = hw->get_standard(sc->hw_softc, &vstd); 1004 if (err != 0) 1005 return err; 1006 1007 video_standard_to_v4l2_standard(vstd, &std); 1008 *stdid = std.id; 1009 1010 return 0; 1011 } 1012 1013 static int 1014 video_set_standard(struct video_softc *sc, v4l2_std_id stdid) 1015 { 1016 const struct video_hw_if *hw = sc->hw_if; 1017 enum video_standard vstd; 1018 1019 /* simple webcam drivers don't need to implement this callback */ 1020 if (hw->set_standard == NULL) { 1021 if (stdid != V4L2_STD_UNKNOWN) 1022 return EINVAL; 1023 return 0; 1024 } 1025 1026 v4l2_standard_to_video_standard(stdid, &vstd); 1027 1028 return hw->set_standard(sc->hw_softc, vstd); 1029 } 1030 1031 static void 1032 v4l2_input_to_video_input(const struct v4l2_input *input, 1033 struct video_input *vi) 1034 { 1035 vi->index = input->index; 1036 strlcpy(vi->name, input->name, sizeof(vi->name)); 1037 switch (input->type) { 1038 case V4L2_INPUT_TYPE_TUNER: 1039 vi->type = VIDEO_INPUT_TYPE_TUNER; 1040 break; 1041 case V4L2_INPUT_TYPE_CAMERA: 1042 vi->type = VIDEO_INPUT_TYPE_CAMERA; 1043 break; 1044 } 1045 vi->audiomask = input->audioset; 1046 vi->tuner_index = input->tuner; 1047 vi->standards = input->std; /* ... values are the same */ 1048 vi->status = 0; 1049 if (input->status & V4L2_IN_ST_NO_POWER) 1050 vi->status |= VIDEO_STATUS_NO_POWER; 1051 if (input->status & V4L2_IN_ST_NO_SIGNAL) 1052 vi->status |= VIDEO_STATUS_NO_SIGNAL; 1053 if (input->status & V4L2_IN_ST_NO_COLOR) 1054 vi->status |= VIDEO_STATUS_NO_COLOR; 1055 if (input->status & V4L2_IN_ST_NO_H_LOCK) 1056 vi->status |= VIDEO_STATUS_NO_HLOCK; 1057 if (input->status & V4L2_IN_ST_MACROVISION) 1058 vi->status |= VIDEO_STATUS_MACROVISION; 1059 } 1060 1061 static void 1062 video_input_to_v4l2_input(const struct video_input *vi, 1063 struct v4l2_input *input) 1064 { 1065 input->index = vi->index; 1066 strlcpy(input->name, vi->name, sizeof(input->name)); 1067 switch (vi->type) { 1068 case VIDEO_INPUT_TYPE_TUNER: 1069 input->type = V4L2_INPUT_TYPE_TUNER; 1070 break; 1071 case VIDEO_INPUT_TYPE_CAMERA: 1072 input->type = V4L2_INPUT_TYPE_CAMERA; 1073 break; 1074 } 1075 input->audioset = vi->audiomask; 1076 input->tuner = vi->tuner_index; 1077 input->std = vi->standards; /* ... values are the same */ 1078 input->status = 0; 1079 if (vi->status & VIDEO_STATUS_NO_POWER) 1080 input->status |= V4L2_IN_ST_NO_POWER; 1081 if (vi->status & VIDEO_STATUS_NO_SIGNAL) 1082 input->status |= V4L2_IN_ST_NO_SIGNAL; 1083 if (vi->status & VIDEO_STATUS_NO_COLOR) 1084 input->status |= V4L2_IN_ST_NO_COLOR; 1085 if (vi->status & VIDEO_STATUS_NO_HLOCK) 1086 input->status |= V4L2_IN_ST_NO_H_LOCK; 1087 if (vi->status & VIDEO_STATUS_MACROVISION) 1088 input->status |= V4L2_IN_ST_MACROVISION; 1089 } 1090 1091 static int 1092 video_enum_input(struct video_softc *sc, struct v4l2_input *input) 1093 { 1094 const struct video_hw_if *hw = sc->hw_if; 1095 struct video_input vi; 1096 int err; 1097 1098 /* simple webcam drivers don't need to implement this callback */ 1099 if (hw->enum_input == NULL) { 1100 if (input->index != 0) 1101 return EINVAL; 1102 memset(input, 0, sizeof(*input)); 1103 input->index = 0; 1104 strlcpy(input->name, "Camera", sizeof(input->name)); 1105 input->type = V4L2_INPUT_TYPE_CAMERA; 1106 return 0; 1107 } 1108 1109 v4l2_input_to_video_input(input, &vi); 1110 1111 err = hw->enum_input(sc->hw_softc, input->index, &vi); 1112 if (err != 0) 1113 return err; 1114 1115 video_input_to_v4l2_input(&vi, input); 1116 1117 return 0; 1118 } 1119 1120 static int 1121 video_get_input(struct video_softc *sc, int *index) 1122 { 1123 const struct video_hw_if *hw = sc->hw_if; 1124 struct video_input vi; 1125 struct v4l2_input input; 1126 int err; 1127 1128 /* simple webcam drivers don't need to implement this callback */ 1129 if (hw->get_input == NULL) { 1130 *index = 0; 1131 return 0; 1132 } 1133 1134 input.index = *index; 1135 v4l2_input_to_video_input(&input, &vi); 1136 1137 err = hw->get_input(sc->hw_softc, &vi); 1138 if (err != 0) 1139 return err; 1140 1141 video_input_to_v4l2_input(&vi, &input); 1142 *index = input.index; 1143 1144 return 0; 1145 } 1146 1147 static int 1148 video_set_input(struct video_softc *sc, int index) 1149 { 1150 const struct video_hw_if *hw = sc->hw_if; 1151 struct video_input vi; 1152 struct v4l2_input input; 1153 1154 /* simple webcam drivers don't need to implement this callback */ 1155 if (hw->set_input == NULL) { 1156 if (index != 0) 1157 return EINVAL; 1158 return 0; 1159 } 1160 1161 input.index = index; 1162 v4l2_input_to_video_input(&input, &vi); 1163 1164 return hw->set_input(sc->hw_softc, &vi); 1165 } 1166 1167 static void 1168 v4l2_audio_to_video_audio(const struct v4l2_audio *audio, 1169 struct video_audio *va) 1170 { 1171 va->index = audio->index; 1172 strlcpy(va->name, audio->name, sizeof(va->name)); 1173 va->caps = va->mode = 0; 1174 if (audio->capability & V4L2_AUDCAP_STEREO) 1175 va->caps |= VIDEO_AUDIO_F_STEREO; 1176 if (audio->capability & V4L2_AUDCAP_AVL) 1177 va->caps |= VIDEO_AUDIO_F_AVL; 1178 if (audio->mode & V4L2_AUDMODE_AVL) 1179 va->mode |= VIDEO_AUDIO_F_AVL; 1180 } 1181 1182 static void 1183 video_audio_to_v4l2_audio(const struct video_audio *va, 1184 struct v4l2_audio *audio) 1185 { 1186 audio->index = va->index; 1187 strlcpy(audio->name, va->name, sizeof(audio->name)); 1188 audio->capability = audio->mode = 0; 1189 if (va->caps & VIDEO_AUDIO_F_STEREO) 1190 audio->capability |= V4L2_AUDCAP_STEREO; 1191 if (va->caps & VIDEO_AUDIO_F_AVL) 1192 audio->capability |= V4L2_AUDCAP_AVL; 1193 if (va->mode & VIDEO_AUDIO_F_AVL) 1194 audio->mode |= V4L2_AUDMODE_AVL; 1195 } 1196 1197 static int 1198 video_enum_audio(struct video_softc *sc, struct v4l2_audio *audio) 1199 { 1200 const struct video_hw_if *hw = sc->hw_if; 1201 struct video_audio va; 1202 int err; 1203 1204 if (hw->enum_audio == NULL) 1205 return ENOTTY; 1206 1207 v4l2_audio_to_video_audio(audio, &va); 1208 1209 err = hw->enum_audio(sc->hw_softc, audio->index, &va); 1210 if (err != 0) 1211 return err; 1212 1213 video_audio_to_v4l2_audio(&va, audio); 1214 1215 return 0; 1216 } 1217 1218 static int 1219 video_get_audio(struct video_softc *sc, struct v4l2_audio *audio) 1220 { 1221 const struct video_hw_if *hw = sc->hw_if; 1222 struct video_audio va; 1223 int err; 1224 1225 if (hw->get_audio == NULL) 1226 return ENOTTY; 1227 1228 v4l2_audio_to_video_audio(audio, &va); 1229 1230 err = hw->get_audio(sc->hw_softc, &va); 1231 if (err != 0) 1232 return err; 1233 1234 video_audio_to_v4l2_audio(&va, audio); 1235 1236 return 0; 1237 } 1238 1239 static int 1240 video_set_audio(struct video_softc *sc, struct v4l2_audio *audio) 1241 { 1242 const struct video_hw_if *hw = sc->hw_if; 1243 struct video_audio va; 1244 1245 if (hw->set_audio == NULL) 1246 return ENOTTY; 1247 1248 v4l2_audio_to_video_audio(audio, &va); 1249 1250 return hw->set_audio(sc->hw_softc, &va); 1251 } 1252 1253 static void 1254 v4l2_tuner_to_video_tuner(const struct v4l2_tuner *tuner, 1255 struct video_tuner *vt) 1256 { 1257 vt->index = tuner->index; 1258 strlcpy(vt->name, tuner->name, sizeof(vt->name)); 1259 vt->freq_lo = tuner->rangelow; 1260 vt->freq_hi = tuner->rangehigh; 1261 vt->signal = tuner->signal; 1262 vt->afc = tuner->afc; 1263 vt->caps = 0; 1264 if (tuner->capability & V4L2_TUNER_CAP_STEREO) 1265 vt->caps |= VIDEO_TUNER_F_STEREO; 1266 if (tuner->capability & V4L2_TUNER_CAP_LANG1) 1267 vt->caps |= VIDEO_TUNER_F_LANG1; 1268 if (tuner->capability & V4L2_TUNER_CAP_LANG2) 1269 vt->caps |= VIDEO_TUNER_F_LANG2; 1270 switch (tuner->audmode) { 1271 case V4L2_TUNER_MODE_MONO: 1272 vt->mode = VIDEO_TUNER_F_MONO; 1273 break; 1274 case V4L2_TUNER_MODE_STEREO: 1275 vt->mode = VIDEO_TUNER_F_STEREO; 1276 break; 1277 case V4L2_TUNER_MODE_LANG1: 1278 vt->mode = VIDEO_TUNER_F_LANG1; 1279 break; 1280 case V4L2_TUNER_MODE_LANG2: 1281 vt->mode = VIDEO_TUNER_F_LANG2; 1282 break; 1283 case V4L2_TUNER_MODE_LANG1_LANG2: 1284 vt->mode = VIDEO_TUNER_F_LANG1 | VIDEO_TUNER_F_LANG2; 1285 break; 1286 } 1287 } 1288 1289 static void 1290 video_tuner_to_v4l2_tuner(const struct video_tuner *vt, 1291 struct v4l2_tuner *tuner) 1292 { 1293 tuner->index = vt->index; 1294 strlcpy(tuner->name, vt->name, sizeof(tuner->name)); 1295 tuner->rangelow = vt->freq_lo; 1296 tuner->rangehigh = vt->freq_hi; 1297 tuner->signal = vt->signal; 1298 tuner->afc = vt->afc; 1299 tuner->capability = 0; 1300 if (vt->caps & VIDEO_TUNER_F_STEREO) 1301 tuner->capability |= V4L2_TUNER_CAP_STEREO; 1302 if (vt->caps & VIDEO_TUNER_F_LANG1) 1303 tuner->capability |= V4L2_TUNER_CAP_LANG1; 1304 if (vt->caps & VIDEO_TUNER_F_LANG2) 1305 tuner->capability |= V4L2_TUNER_CAP_LANG2; 1306 switch (vt->mode) { 1307 case VIDEO_TUNER_F_MONO: 1308 tuner->audmode = V4L2_TUNER_MODE_MONO; 1309 break; 1310 case VIDEO_TUNER_F_STEREO: 1311 tuner->audmode = V4L2_TUNER_MODE_STEREO; 1312 break; 1313 case VIDEO_TUNER_F_LANG1: 1314 tuner->audmode = V4L2_TUNER_MODE_LANG1; 1315 break; 1316 case VIDEO_TUNER_F_LANG2: 1317 tuner->audmode = V4L2_TUNER_MODE_LANG2; 1318 break; 1319 case VIDEO_TUNER_F_LANG1|VIDEO_TUNER_F_LANG2: 1320 tuner->audmode = V4L2_TUNER_MODE_LANG1_LANG2; 1321 break; 1322 } 1323 } 1324 1325 static int 1326 video_get_tuner(struct video_softc *sc, struct v4l2_tuner *tuner) 1327 { 1328 const struct video_hw_if *hw = sc->hw_if; 1329 struct video_tuner vt; 1330 int err; 1331 1332 if (hw->get_tuner == NULL) 1333 return ENOTTY; 1334 1335 v4l2_tuner_to_video_tuner(tuner, &vt); 1336 1337 err = hw->get_tuner(sc->hw_softc, &vt); 1338 if (err != 0) 1339 return err; 1340 1341 video_tuner_to_v4l2_tuner(&vt, tuner); 1342 1343 return 0; 1344 } 1345 1346 static int 1347 video_set_tuner(struct video_softc *sc, struct v4l2_tuner *tuner) 1348 { 1349 const struct video_hw_if *hw = sc->hw_if; 1350 struct video_tuner vt; 1351 1352 if (hw->set_tuner == NULL) 1353 return ENOTTY; 1354 1355 v4l2_tuner_to_video_tuner(tuner, &vt); 1356 1357 return hw->set_tuner(sc->hw_softc, &vt); 1358 } 1359 1360 static int 1361 video_get_frequency(struct video_softc *sc, struct v4l2_frequency *freq) 1362 { 1363 const struct video_hw_if *hw = sc->hw_if; 1364 struct video_frequency vfreq; 1365 int err; 1366 1367 if (hw->get_frequency == NULL) 1368 return ENOTTY; 1369 1370 err = hw->get_frequency(sc->hw_softc, &vfreq); 1371 if (err) 1372 return err; 1373 1374 freq->tuner = vfreq.tuner_index; 1375 freq->type = V4L2_TUNER_ANALOG_TV; 1376 freq->frequency = vfreq.frequency; 1377 1378 return 0; 1379 } 1380 1381 static int 1382 video_set_frequency(struct video_softc *sc, struct v4l2_frequency *freq) 1383 { 1384 const struct video_hw_if *hw = sc->hw_if; 1385 struct video_frequency vfreq; 1386 struct video_tuner vt; 1387 int error; 1388 1389 if (hw->set_frequency == NULL || hw->get_tuner == NULL) 1390 return ENOTTY; 1391 if (freq->type != V4L2_TUNER_ANALOG_TV) 1392 return EINVAL; 1393 1394 vt.index = freq->tuner; 1395 error = hw->get_tuner(sc->hw_softc, &vt); 1396 if (error) 1397 return error; 1398 1399 if (freq->frequency < vt.freq_lo) 1400 freq->frequency = vt.freq_lo; 1401 else if (freq->frequency > vt.freq_hi) 1402 freq->frequency = vt.freq_hi; 1403 1404 vfreq.tuner_index = freq->tuner; 1405 vfreq.frequency = freq->frequency; 1406 1407 return hw->set_frequency(sc->hw_softc, &vfreq); 1408 } 1409 1410 /* Takes a single Video4Linux2 control, converts it to a struct 1411 * video_control, and calls the hardware driver. */ 1412 static int 1413 video_set_control(struct video_softc *sc, 1414 const struct v4l2_control *vcontrol) 1415 { 1416 const struct video_hw_if *hw; 1417 struct video_control_group group; 1418 struct video_control control; 1419 1420 hw = sc->hw_if; 1421 if (hw->set_control_group) { 1422 control.group_id = control.control_id = 1423 v4l2id_to_control_id(vcontrol->id); 1424 /* ?? if "control_id" is arbitrarily defined by the 1425 * driver, then we need some way to store it... Maybe 1426 * it doesn't matter for single value controls. */ 1427 control.value = vcontrol->value; 1428 1429 group.group_id = control.group_id; 1430 group.length = 1; 1431 group.control = &control; 1432 1433 return (hw->set_control_group(sc->hw_softc, &group)); 1434 } else { 1435 return EINVAL; 1436 } 1437 } 1438 1439 static int 1440 video_request_bufs(struct video_softc *sc, 1441 struct v4l2_requestbuffers *req) 1442 { 1443 struct video_stream *vs = &sc->sc_stream_in; 1444 struct v4l2_buffer *buf; 1445 int i, err; 1446 1447 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1448 return EINVAL; 1449 1450 vs->vs_type = req->type; 1451 1452 switch (req->memory) { 1453 case V4L2_MEMORY_MMAP: 1454 if (req->count < VIDEO_MIN_BUFS) 1455 req->count = VIDEO_MIN_BUFS; 1456 else if (req->count > VIDEO_MAX_BUFS) 1457 req->count = VIDEO_MAX_BUFS; 1458 1459 err = video_stream_setup_bufs(vs, 1460 VIDEO_STREAM_METHOD_MMAP, 1461 req->count); 1462 if (err != 0) 1463 return err; 1464 1465 for (i = 0; i < req->count; ++i) { 1466 buf = vs->vs_buf[i]->vb_buf; 1467 buf->memory = V4L2_MEMORY_MMAP; 1468 buf->flags |= V4L2_BUF_FLAG_MAPPED; 1469 } 1470 break; 1471 case V4L2_MEMORY_USERPTR: 1472 default: 1473 return EINVAL; 1474 } 1475 1476 return 0; 1477 } 1478 1479 static int 1480 video_query_buf(struct video_softc *sc, 1481 struct v4l2_buffer *buf) 1482 { 1483 struct video_stream *vs = &sc->sc_stream_in; 1484 1485 if (buf->type != vs->vs_type) 1486 return EINVAL; 1487 if (buf->index >= vs->vs_nbufs) 1488 return EINVAL; 1489 1490 memcpy(buf, vs->vs_buf[buf->index]->vb_buf, sizeof(*buf)); 1491 1492 return 0; 1493 } 1494 1495 /* Accept a buffer descriptor from userspace and return the indicated 1496 * buffer to the driver's queue. */ 1497 static int 1498 video_queue_buf(struct video_softc *sc, struct v4l2_buffer *userbuf) 1499 { 1500 struct video_stream *vs = &sc->sc_stream_in; 1501 struct video_buffer *vb; 1502 struct v4l2_buffer *driverbuf; 1503 1504 if (userbuf->type != vs->vs_type) { 1505 DPRINTF(("video_queue_buf: expected type=%d got type=%d\n", 1506 userbuf->type, vs->vs_type)); 1507 return EINVAL; 1508 } 1509 if (userbuf->index >= vs->vs_nbufs) { 1510 DPRINTF(("video_queue_buf: invalid index %d >= %d\n", 1511 userbuf->index, vs->vs_nbufs)); 1512 return EINVAL; 1513 } 1514 1515 switch (vs->vs_method) { 1516 case VIDEO_STREAM_METHOD_MMAP: 1517 if (userbuf->memory != V4L2_MEMORY_MMAP) { 1518 DPRINTF(("video_queue_buf: invalid memory=%d\n", 1519 userbuf->memory)); 1520 return EINVAL; 1521 } 1522 1523 mutex_enter(&vs->vs_lock); 1524 1525 vb = vs->vs_buf[userbuf->index]; 1526 driverbuf = vb->vb_buf; 1527 if (driverbuf->flags & V4L2_BUF_FLAG_QUEUED) { 1528 DPRINTF(("video_queue_buf: buf already queued; " 1529 "flags=0x%x\n", driverbuf->flags)); 1530 mutex_exit(&vs->vs_lock); 1531 return EINVAL; 1532 } 1533 video_stream_enqueue(vs, vb); 1534 memcpy(userbuf, driverbuf, sizeof(*driverbuf)); 1535 1536 mutex_exit(&vs->vs_lock); 1537 break; 1538 default: 1539 return EINVAL; 1540 } 1541 1542 return 0; 1543 } 1544 1545 /* Dequeue the described buffer from the driver queue, making it 1546 * available for reading via mmap. */ 1547 static int 1548 video_dequeue_buf(struct video_softc *sc, struct v4l2_buffer *buf) 1549 { 1550 struct video_stream *vs = &sc->sc_stream_in; 1551 struct video_buffer *vb; 1552 int err; 1553 1554 if (buf->type != vs->vs_type) { 1555 aprint_debug_dev(sc->sc_dev, 1556 "requested type %d (expected %d)\n", 1557 buf->type, vs->vs_type); 1558 return EINVAL; 1559 } 1560 1561 switch (vs->vs_method) { 1562 case VIDEO_STREAM_METHOD_MMAP: 1563 if (buf->memory != V4L2_MEMORY_MMAP) { 1564 aprint_debug_dev(sc->sc_dev, 1565 "requested memory %d (expected %d)\n", 1566 buf->memory, V4L2_MEMORY_MMAP); 1567 return EINVAL; 1568 } 1569 1570 mutex_enter(&vs->vs_lock); 1571 1572 if (vs->vs_flags & O_NONBLOCK) { 1573 vb = video_stream_dequeue(vs); 1574 if (vb == NULL) { 1575 mutex_exit(&vs->vs_lock); 1576 return EAGAIN; 1577 } 1578 } else { 1579 /* Block until we have sample */ 1580 while ((vb = video_stream_dequeue(vs)) == NULL) { 1581 if (!vs->vs_streaming) { 1582 mutex_exit(&vs->vs_lock); 1583 return EINVAL; 1584 } 1585 err = cv_wait_sig(&vs->vs_sample_cv, 1586 &vs->vs_lock); 1587 if (err != 0) { 1588 mutex_exit(&vs->vs_lock); 1589 return EINTR; 1590 } 1591 } 1592 } 1593 1594 memcpy(buf, vb->vb_buf, sizeof(*buf)); 1595 1596 mutex_exit(&vs->vs_lock); 1597 break; 1598 default: 1599 aprint_debug_dev(sc->sc_dev, "unknown vs_method %d\n", 1600 vs->vs_method); 1601 return EINVAL; 1602 } 1603 1604 return 0; 1605 } 1606 1607 static int 1608 video_stream_on(struct video_softc *sc, enum v4l2_buf_type type) 1609 { 1610 int err; 1611 struct video_stream *vs = &sc->sc_stream_in; 1612 const struct video_hw_if *hw; 1613 1614 if (vs->vs_streaming) 1615 return 0; 1616 if (type != vs->vs_type) 1617 return EINVAL; 1618 1619 hw = sc->hw_if; 1620 if (hw == NULL) 1621 return ENXIO; 1622 1623 1624 err = hw->start_transfer(sc->hw_softc); 1625 if (err != 0) 1626 return err; 1627 1628 vs->vs_streaming = true; 1629 return 0; 1630 } 1631 1632 static int 1633 video_stream_off(struct video_softc *sc, enum v4l2_buf_type type) 1634 { 1635 int err; 1636 struct video_stream *vs = &sc->sc_stream_in; 1637 const struct video_hw_if *hw; 1638 1639 if (!vs->vs_streaming) 1640 return 0; 1641 if (type != vs->vs_type) 1642 return EINVAL; 1643 1644 hw = sc->hw_if; 1645 if (hw == NULL) 1646 return ENXIO; 1647 1648 err = hw->stop_transfer(sc->hw_softc); 1649 if (err != 0) 1650 return err; 1651 1652 vs->vs_frameno = -1; 1653 vs->vs_sequence = 0; 1654 vs->vs_streaming = false; 1655 1656 return 0; 1657 } 1658 1659 int 1660 videoopen(dev_t dev, int flags, int ifmt, struct lwp *l) 1661 { 1662 struct video_softc *sc; 1663 const struct video_hw_if *hw; 1664 struct video_stream *vs; 1665 int err; 1666 1667 DPRINTF(("videoopen\n")); 1668 1669 sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev))); 1670 if (sc == NULL) { 1671 DPRINTF(("videoopen: failed to get softc for unit %d\n", 1672 VIDEOUNIT(dev))); 1673 return ENXIO; 1674 } 1675 1676 if (sc->sc_dying) { 1677 DPRINTF(("videoopen: dying\n")); 1678 return EIO; 1679 } 1680 1681 sc->sc_stream_in.vs_flags = flags; 1682 1683 DPRINTF(("videoopen: flags=0x%x sc=%p parent=%p\n", 1684 flags, sc, sc->hw_dev)); 1685 1686 hw = sc->hw_if; 1687 if (hw == NULL) 1688 return ENXIO; 1689 1690 device_active(sc->sc_dev, DVA_SYSTEM); 1691 1692 sc->sc_opencnt++; 1693 1694 if (hw->open != NULL) { 1695 err = hw->open(sc->hw_softc, flags); 1696 if (err) 1697 return err; 1698 } 1699 1700 /* set up input stream. TODO: check flags to determine if 1701 * "read" is desired? */ 1702 vs = &sc->sc_stream_in; 1703 1704 if (hw->get_format != NULL) { 1705 err = hw->get_format(sc->hw_softc, &vs->vs_format); 1706 if (err != 0) 1707 return err; 1708 } 1709 return 0; 1710 } 1711 1712 1713 int 1714 videoclose(dev_t dev, int flags, int ifmt, struct lwp *l) 1715 { 1716 struct video_softc *sc; 1717 const struct video_hw_if *hw; 1718 1719 sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev))); 1720 if (sc == NULL) 1721 return ENXIO; 1722 1723 DPRINTF(("videoclose: sc=%p\n", sc)); 1724 1725 hw = sc->hw_if; 1726 if (hw == NULL) 1727 return ENXIO; 1728 1729 device_active(sc->sc_dev, DVA_SYSTEM); 1730 1731 video_stream_off(sc, sc->sc_stream_in.vs_type); 1732 1733 /* ignore error */ 1734 if (hw->close != NULL) 1735 hw->close(sc->hw_softc); 1736 1737 video_stream_teardown_bufs(&sc->sc_stream_in); 1738 1739 sc->sc_open = 0; 1740 sc->sc_opencnt--; 1741 1742 return 0; 1743 } 1744 1745 1746 int 1747 videoread(dev_t dev, struct uio *uio, int ioflag) 1748 { 1749 struct video_softc *sc; 1750 struct video_stream *vs; 1751 struct video_buffer *vb; 1752 struct scatter_io sio; 1753 int err; 1754 size_t len; 1755 off_t offset; 1756 1757 sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev))); 1758 if (sc == NULL) 1759 return ENXIO; 1760 1761 if (sc->sc_dying) 1762 return EIO; 1763 1764 vs = &sc->sc_stream_in; 1765 1766 /* userspace has chosen read() method */ 1767 if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) { 1768 err = video_stream_setup_bufs(vs, 1769 VIDEO_STREAM_METHOD_READ, 1770 VIDEO_NUM_BUFS); 1771 if (err != 0) 1772 return err; 1773 1774 err = video_stream_on(sc, vs->vs_type); 1775 if (err != 0) 1776 return err; 1777 } else if (vs->vs_method != VIDEO_STREAM_METHOD_READ) { 1778 return EBUSY; 1779 } 1780 1781 mutex_enter(&vs->vs_lock); 1782 1783 retry: 1784 if (SIMPLEQ_EMPTY(&vs->vs_egress)) { 1785 if (vs->vs_flags & O_NONBLOCK) { 1786 mutex_exit(&vs->vs_lock); 1787 return EAGAIN; 1788 } 1789 1790 /* Block until we have a sample */ 1791 while (SIMPLEQ_EMPTY(&vs->vs_egress)) { 1792 err = cv_wait_sig(&vs->vs_sample_cv, 1793 &vs->vs_lock); 1794 if (err != 0) { 1795 mutex_exit(&vs->vs_lock); 1796 return EINTR; 1797 } 1798 } 1799 1800 vb = SIMPLEQ_FIRST(&vs->vs_egress); 1801 } else { 1802 vb = SIMPLEQ_FIRST(&vs->vs_egress); 1803 } 1804 1805 /* Oops, empty sample buffer. */ 1806 if (vb->vb_buf->bytesused == 0) { 1807 vb = video_stream_dequeue(vs); 1808 video_stream_enqueue(vs, vb); 1809 vs->vs_bytesread = 0; 1810 goto retry; 1811 } 1812 1813 mutex_exit(&vs->vs_lock); 1814 1815 len = uimin(uio->uio_resid, vb->vb_buf->bytesused - vs->vs_bytesread); 1816 offset = vb->vb_buf->m.offset + vs->vs_bytesread; 1817 1818 if (scatter_io_init(&vs->vs_data, offset, len, &sio)) { 1819 err = scatter_io_uiomove(&sio, uio); 1820 if (err == EFAULT) 1821 return EFAULT; 1822 vs->vs_bytesread += (len - sio.sio_resid); 1823 } else { 1824 DPRINTF(("video: invalid read\n")); 1825 } 1826 1827 /* Move the sample to the ingress queue if everything has 1828 * been read */ 1829 if (vs->vs_bytesread >= vb->vb_buf->bytesused) { 1830 mutex_enter(&vs->vs_lock); 1831 vb = video_stream_dequeue(vs); 1832 video_stream_enqueue(vs, vb); 1833 mutex_exit(&vs->vs_lock); 1834 1835 vs->vs_bytesread = 0; 1836 } 1837 1838 return 0; 1839 } 1840 1841 1842 int 1843 videowrite(dev_t dev, struct uio *uio, int ioflag) 1844 { 1845 return ENXIO; 1846 } 1847 1848 1849 /* 1850 * Before 64-bit time_t, timeval's tv_sec was 'long'. Thus on LP64 ports 1851 * v4l2_buffer is the same size and layout as before. However it did change 1852 * on LP32 ports, and we thus handle this difference here for "COMPAT_50". 1853 */ 1854 1855 #ifndef _LP64 1856 static void 1857 buf50tobuf(const void *data, struct v4l2_buffer *buf) 1858 { 1859 const struct v4l2_buffer50 *b50 = data; 1860 1861 buf->index = b50->index; 1862 buf->type = b50->type; 1863 buf->bytesused = b50->bytesused; 1864 buf->flags = b50->flags; 1865 buf->field = b50->field; 1866 timeval50_to_timeval(&b50->timestamp, &buf->timestamp); 1867 buf->timecode = b50->timecode; 1868 buf->sequence = b50->sequence; 1869 buf->memory = b50->memory; 1870 buf->m.offset = b50->m.offset; 1871 /* XXX: Handle userptr */ 1872 buf->length = b50->length; 1873 buf->input = b50->input; 1874 buf->reserved = b50->reserved; 1875 } 1876 1877 static void 1878 buftobuf50(void *data, const struct v4l2_buffer *buf) 1879 { 1880 struct v4l2_buffer50 *b50 = data; 1881 1882 b50->index = buf->index; 1883 b50->type = buf->type; 1884 b50->bytesused = buf->bytesused; 1885 b50->flags = buf->flags; 1886 b50->field = buf->field; 1887 timeval_to_timeval50(&buf->timestamp, &b50->timestamp); 1888 b50->timecode = buf->timecode; 1889 b50->sequence = buf->sequence; 1890 b50->memory = buf->memory; 1891 b50->m.offset = buf->m.offset; 1892 /* XXX: Handle userptr */ 1893 b50->length = buf->length; 1894 b50->input = buf->input; 1895 b50->reserved = buf->reserved; 1896 } 1897 #endif 1898 1899 int 1900 videoioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 1901 { 1902 struct video_softc *sc; 1903 const struct video_hw_if *hw; 1904 struct v4l2_capability *cap; 1905 struct v4l2_fmtdesc *fmtdesc; 1906 struct v4l2_format *fmt; 1907 struct v4l2_standard *std; 1908 struct v4l2_input *input; 1909 struct v4l2_audio *audio; 1910 struct v4l2_tuner *tuner; 1911 struct v4l2_frequency *freq; 1912 struct v4l2_control *control; 1913 struct v4l2_queryctrl *query; 1914 struct v4l2_requestbuffers *reqbufs; 1915 struct v4l2_buffer *buf; 1916 struct v4l2_streamparm *parm; 1917 v4l2_std_id *stdid; 1918 enum v4l2_buf_type *typep; 1919 int *ip; 1920 #ifndef _LP64 1921 struct v4l2_buffer bufspace; 1922 int error; 1923 #endif 1924 1925 sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev))); 1926 1927 if (sc->sc_dying) 1928 return EIO; 1929 1930 hw = sc->hw_if; 1931 if (hw == NULL) 1932 return ENXIO; 1933 1934 switch (cmd) { 1935 case VIDIOC_QUERYCAP: 1936 cap = data; 1937 memset(cap, 0, sizeof(*cap)); 1938 strlcpy(cap->driver, 1939 device_cfdriver(sc->hw_dev)->cd_name, 1940 sizeof(cap->driver)); 1941 strlcpy(cap->card, hw->get_devname(sc->hw_softc), 1942 sizeof(cap->card)); 1943 strlcpy(cap->bus_info, hw->get_businfo(sc->hw_softc), 1944 sizeof(cap->bus_info)); 1945 cap->version = VIDEO_DRIVER_VERSION; 1946 cap->capabilities = 0; 1947 if (hw->start_transfer != NULL && hw->stop_transfer != NULL) 1948 cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE | 1949 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 1950 if (hw->set_tuner != NULL && hw->get_tuner != NULL) 1951 cap->capabilities |= V4L2_CAP_TUNER; 1952 if (hw->set_audio != NULL && hw->get_audio != NULL && 1953 hw->enum_audio != NULL) 1954 cap->capabilities |= V4L2_CAP_AUDIO; 1955 return 0; 1956 case VIDIOC_ENUM_FMT: 1957 /* TODO: for now, just enumerate one default format */ 1958 fmtdesc = data; 1959 if (fmtdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1960 return EINVAL; 1961 return video_enum_format(sc, fmtdesc); 1962 case VIDIOC_G_FMT: 1963 fmt = data; 1964 return video_get_format(sc, fmt); 1965 case VIDIOC_S_FMT: 1966 fmt = data; 1967 if ((flag & FWRITE) == 0) 1968 return EPERM; 1969 return video_set_format(sc, fmt); 1970 case VIDIOC_TRY_FMT: 1971 fmt = data; 1972 return video_try_format(sc, fmt); 1973 case VIDIOC_G_PARM: 1974 parm = data; 1975 return video_get_parm(sc, parm); 1976 case VIDIOC_S_PARM: 1977 parm = data; 1978 if ((flag & FWRITE) == 0) 1979 return EPERM; 1980 return video_set_parm(sc, parm); 1981 case VIDIOC_ENUMSTD: 1982 std = data; 1983 return video_enum_standard(sc, std); 1984 case VIDIOC_G_STD: 1985 stdid = data; 1986 return video_get_standard(sc, stdid); 1987 case VIDIOC_S_STD: 1988 stdid = data; 1989 if ((flag & FWRITE) == 0) 1990 return EPERM; 1991 return video_set_standard(sc, *stdid); 1992 case VIDIOC_ENUMINPUT: 1993 input = data; 1994 return video_enum_input(sc, input); 1995 case VIDIOC_G_INPUT: 1996 ip = data; 1997 return video_get_input(sc, ip); 1998 case VIDIOC_S_INPUT: 1999 ip = data; 2000 if ((flag & FWRITE) == 0) 2001 return EPERM; 2002 return video_set_input(sc, *ip); 2003 case VIDIOC_ENUMAUDIO: 2004 audio = data; 2005 return video_enum_audio(sc, audio); 2006 case VIDIOC_G_AUDIO: 2007 audio = data; 2008 return video_get_audio(sc, audio); 2009 case VIDIOC_S_AUDIO: 2010 audio = data; 2011 if ((flag & FWRITE) == 0) 2012 return EPERM; 2013 return video_set_audio(sc, audio); 2014 case VIDIOC_G_TUNER: 2015 tuner = data; 2016 return video_get_tuner(sc, tuner); 2017 case VIDIOC_S_TUNER: 2018 tuner = data; 2019 if ((flag & FWRITE) == 0) 2020 return EPERM; 2021 return video_set_tuner(sc, tuner); 2022 case VIDIOC_G_FREQUENCY: 2023 freq = data; 2024 return video_get_frequency(sc, freq); 2025 case VIDIOC_S_FREQUENCY: 2026 freq = data; 2027 if ((flag & FWRITE) == 0) 2028 return EPERM; 2029 return video_set_frequency(sc, freq); 2030 case VIDIOC_QUERYCTRL: 2031 query = data; 2032 return (video_query_control(sc, query)); 2033 case VIDIOC_G_CTRL: 2034 control = data; 2035 return (video_get_control(sc, control)); 2036 case VIDIOC_S_CTRL: 2037 control = data; 2038 if ((flag & FWRITE) == 0) 2039 return EPERM; 2040 return (video_set_control(sc, control)); 2041 case VIDIOC_REQBUFS: 2042 reqbufs = data; 2043 return (video_request_bufs(sc, reqbufs)); 2044 case VIDIOC_QUERYBUF: 2045 buf = data; 2046 return video_query_buf(sc, buf); 2047 #ifndef _LP64 2048 case VIDIOC_QUERYBUF50: 2049 buf50tobuf(data, buf = &bufspace); 2050 if ((error = video_query_buf(sc, buf)) != 0) 2051 return error; 2052 buftobuf50(data, buf); 2053 return 0; 2054 #endif 2055 case VIDIOC_QBUF: 2056 buf = data; 2057 return video_queue_buf(sc, buf); 2058 #ifndef _LP64 2059 case VIDIOC_QBUF50: 2060 buf50tobuf(data, buf = &bufspace); 2061 return video_queue_buf(sc, buf); 2062 #endif 2063 case VIDIOC_DQBUF: 2064 buf = data; 2065 return video_dequeue_buf(sc, buf); 2066 #ifndef _LP64 2067 case VIDIOC_DQBUF50: 2068 buf50tobuf(data, buf = &bufspace); 2069 if ((error = video_dequeue_buf(sc, buf)) != 0) 2070 return error; 2071 buftobuf50(data, buf); 2072 return 0; 2073 #endif 2074 case VIDIOC_STREAMON: 2075 typep = data; 2076 return video_stream_on(sc, *typep); 2077 case VIDIOC_STREAMOFF: 2078 typep = data; 2079 return video_stream_off(sc, *typep); 2080 default: 2081 DPRINTF(("videoioctl: invalid cmd %s (%lx)\n", 2082 video_ioctl_str(cmd), cmd)); 2083 return EINVAL; 2084 } 2085 } 2086 2087 #ifdef VIDEO_DEBUG 2088 static const char * 2089 video_ioctl_str(u_long cmd) 2090 { 2091 const char *str; 2092 2093 switch (cmd) { 2094 case VIDIOC_QUERYCAP: 2095 str = "VIDIOC_QUERYCAP"; 2096 break; 2097 case VIDIOC_RESERVED: 2098 str = "VIDIOC_RESERVED"; 2099 break; 2100 case VIDIOC_ENUM_FMT: 2101 str = "VIDIOC_ENUM_FMT"; 2102 break; 2103 case VIDIOC_G_FMT: 2104 str = "VIDIOC_G_FMT"; 2105 break; 2106 case VIDIOC_S_FMT: 2107 str = "VIDIOC_S_FMT"; 2108 break; 2109 /* 6 and 7 are VIDIOC_[SG]_COMP, which are unsupported */ 2110 case VIDIOC_REQBUFS: 2111 str = "VIDIOC_REQBUFS"; 2112 break; 2113 case VIDIOC_QUERYBUF: 2114 str = "VIDIOC_QUERYBUF"; 2115 break; 2116 #ifndef _LP64 2117 case VIDIOC_QUERYBUF50: 2118 str = "VIDIOC_QUERYBUF50"; 2119 break; 2120 #endif 2121 case VIDIOC_G_FBUF: 2122 str = "VIDIOC_G_FBUF"; 2123 break; 2124 case VIDIOC_S_FBUF: 2125 str = "VIDIOC_S_FBUF"; 2126 break; 2127 case VIDIOC_OVERLAY: 2128 str = "VIDIOC_OVERLAY"; 2129 break; 2130 case VIDIOC_QBUF: 2131 str = "VIDIOC_QBUF"; 2132 break; 2133 #ifndef _LP64 2134 case VIDIOC_QBUF50: 2135 str = "VIDIOC_QBUF50"; 2136 break; 2137 #endif 2138 case VIDIOC_DQBUF: 2139 str = "VIDIOC_DQBUF"; 2140 break; 2141 #ifndef _LP64 2142 case VIDIOC_DQBUF50: 2143 str = "VIDIOC_DQBUF50"; 2144 break; 2145 #endif 2146 case VIDIOC_STREAMON: 2147 str = "VIDIOC_STREAMON"; 2148 break; 2149 case VIDIOC_STREAMOFF: 2150 str = "VIDIOC_STREAMOFF"; 2151 break; 2152 case VIDIOC_G_PARM: 2153 str = "VIDIOC_G_PARAM"; 2154 break; 2155 case VIDIOC_S_PARM: 2156 str = "VIDIOC_S_PARAM"; 2157 break; 2158 case VIDIOC_G_STD: 2159 str = "VIDIOC_G_STD"; 2160 break; 2161 case VIDIOC_S_STD: 2162 str = "VIDIOC_S_STD"; 2163 break; 2164 case VIDIOC_ENUMSTD: 2165 str = "VIDIOC_ENUMSTD"; 2166 break; 2167 case VIDIOC_ENUMINPUT: 2168 str = "VIDIOC_ENUMINPUT"; 2169 break; 2170 case VIDIOC_G_CTRL: 2171 str = "VIDIOC_G_CTRL"; 2172 break; 2173 case VIDIOC_S_CTRL: 2174 str = "VIDIOC_S_CTRL"; 2175 break; 2176 case VIDIOC_G_TUNER: 2177 str = "VIDIOC_G_TUNER"; 2178 break; 2179 case VIDIOC_S_TUNER: 2180 str = "VIDIOC_S_TUNER"; 2181 break; 2182 case VIDIOC_G_AUDIO: 2183 str = "VIDIOC_G_AUDIO"; 2184 break; 2185 case VIDIOC_S_AUDIO: 2186 str = "VIDIOC_S_AUDIO"; 2187 break; 2188 case VIDIOC_QUERYCTRL: 2189 str = "VIDIOC_QUERYCTRL"; 2190 break; 2191 case VIDIOC_QUERYMENU: 2192 str = "VIDIOC_QUERYMENU"; 2193 break; 2194 case VIDIOC_G_INPUT: 2195 str = "VIDIOC_G_INPUT"; 2196 break; 2197 case VIDIOC_S_INPUT: 2198 str = "VIDIOC_S_INPUT"; 2199 break; 2200 case VIDIOC_G_OUTPUT: 2201 str = "VIDIOC_G_OUTPUT"; 2202 break; 2203 case VIDIOC_S_OUTPUT: 2204 str = "VIDIOC_S_OUTPUT"; 2205 break; 2206 case VIDIOC_ENUMOUTPUT: 2207 str = "VIDIOC_ENUMOUTPUT"; 2208 break; 2209 case VIDIOC_G_AUDOUT: 2210 str = "VIDIOC_G_AUDOUT"; 2211 break; 2212 case VIDIOC_S_AUDOUT: 2213 str = "VIDIOC_S_AUDOUT"; 2214 break; 2215 case VIDIOC_G_MODULATOR: 2216 str = "VIDIOC_G_MODULATOR"; 2217 break; 2218 case VIDIOC_S_MODULATOR: 2219 str = "VIDIOC_S_MODULATOR"; 2220 break; 2221 case VIDIOC_G_FREQUENCY: 2222 str = "VIDIOC_G_FREQUENCY"; 2223 break; 2224 case VIDIOC_S_FREQUENCY: 2225 str = "VIDIOC_S_FREQUENCY"; 2226 break; 2227 case VIDIOC_CROPCAP: 2228 str = "VIDIOC_CROPCAP"; 2229 break; 2230 case VIDIOC_G_CROP: 2231 str = "VIDIOC_G_CROP"; 2232 break; 2233 case VIDIOC_S_CROP: 2234 str = "VIDIOC_S_CROP"; 2235 break; 2236 case VIDIOC_G_JPEGCOMP: 2237 str = "VIDIOC_G_JPEGCOMP"; 2238 break; 2239 case VIDIOC_S_JPEGCOMP: 2240 str = "VIDIOC_S_JPEGCOMP"; 2241 break; 2242 case VIDIOC_QUERYSTD: 2243 str = "VIDIOC_QUERYSTD"; 2244 break; 2245 case VIDIOC_TRY_FMT: 2246 str = "VIDIOC_TRY_FMT"; 2247 break; 2248 case VIDIOC_ENUMAUDIO: 2249 str = "VIDIOC_ENUMAUDIO"; 2250 break; 2251 case VIDIOC_ENUMAUDOUT: 2252 str = "VIDIOC_ENUMAUDOUT"; 2253 break; 2254 case VIDIOC_G_PRIORITY: 2255 str = "VIDIOC_G_PRIORITY"; 2256 break; 2257 case VIDIOC_S_PRIORITY: 2258 str = "VIDIOC_S_PRIORITY"; 2259 break; 2260 default: 2261 str = "unknown"; 2262 break; 2263 } 2264 return str; 2265 } 2266 #endif 2267 2268 2269 int 2270 videopoll(dev_t dev, int events, struct lwp *l) 2271 { 2272 struct video_softc *sc; 2273 struct video_stream *vs; 2274 int err, revents = 0; 2275 2276 sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev))); 2277 vs = &sc->sc_stream_in; 2278 2279 if (sc->sc_dying) 2280 return (POLLHUP); 2281 2282 /* userspace has chosen read() method */ 2283 if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) { 2284 err = video_stream_setup_bufs(vs, 2285 VIDEO_STREAM_METHOD_READ, 2286 VIDEO_NUM_BUFS); 2287 if (err != 0) 2288 return POLLERR; 2289 2290 err = video_stream_on(sc, vs->vs_type); 2291 if (err != 0) 2292 return POLLERR; 2293 } 2294 2295 mutex_enter(&vs->vs_lock); 2296 if (!SIMPLEQ_EMPTY(&sc->sc_stream_in.vs_egress)) 2297 revents |= events & (POLLIN | POLLRDNORM); 2298 else 2299 selrecord(l, &vs->vs_sel); 2300 mutex_exit(&vs->vs_lock); 2301 2302 return (revents); 2303 } 2304 2305 2306 paddr_t 2307 videommap(dev_t dev, off_t off, int prot) 2308 { 2309 struct video_softc *sc; 2310 struct video_stream *vs; 2311 /* paddr_t pa; */ 2312 2313 sc = device_lookup_private(&video_cd, VIDEOUNIT(dev)); 2314 if (sc->sc_dying) 2315 return -1; 2316 2317 vs = &sc->sc_stream_in; 2318 2319 return scatter_buf_map(&vs->vs_data, off); 2320 } 2321 2322 2323 /* Allocates buffers and initizlizes some fields. The format field 2324 * must already have been initialized. */ 2325 void 2326 video_stream_init(struct video_stream *vs) 2327 { 2328 vs->vs_method = VIDEO_STREAM_METHOD_NONE; 2329 vs->vs_flags = 0; 2330 vs->vs_frameno = -1; 2331 vs->vs_sequence = 0; 2332 vs->vs_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2333 vs->vs_nbufs = 0; 2334 vs->vs_buf = NULL; 2335 vs->vs_streaming = false; 2336 2337 memset(&vs->vs_format, 0, sizeof(vs->vs_format)); 2338 2339 SIMPLEQ_INIT(&vs->vs_ingress); 2340 SIMPLEQ_INIT(&vs->vs_egress); 2341 2342 mutex_init(&vs->vs_lock, MUTEX_DEFAULT, IPL_NONE); 2343 cv_init(&vs->vs_sample_cv, "video"); 2344 selinit(&vs->vs_sel); 2345 2346 scatter_buf_init(&vs->vs_data); 2347 } 2348 2349 void 2350 video_stream_fini(struct video_stream *vs) 2351 { 2352 /* Sample data in queues has already been freed */ 2353 /* while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL) 2354 SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries); 2355 while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL) 2356 SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries); */ 2357 2358 mutex_destroy(&vs->vs_lock); 2359 cv_destroy(&vs->vs_sample_cv); 2360 seldestroy(&vs->vs_sel); 2361 2362 scatter_buf_destroy(&vs->vs_data); 2363 } 2364 2365 static int 2366 video_stream_setup_bufs(struct video_stream *vs, 2367 enum video_stream_method method, 2368 uint8_t nbufs) 2369 { 2370 int i, err; 2371 2372 mutex_enter(&vs->vs_lock); 2373 2374 /* Ensure that all allocated buffers are queued and not under 2375 * userspace control. */ 2376 for (i = 0; i < vs->vs_nbufs; ++i) { 2377 if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED)) { 2378 mutex_exit(&vs->vs_lock); 2379 return EBUSY; 2380 } 2381 } 2382 2383 /* Allocate the buffers */ 2384 err = video_stream_realloc_bufs(vs, nbufs); 2385 if (err != 0) { 2386 mutex_exit(&vs->vs_lock); 2387 return err; 2388 } 2389 2390 /* Queue up buffers for read method. Other methods are queued 2391 * by VIDIOC_QBUF ioctl. */ 2392 if (method == VIDEO_STREAM_METHOD_READ) { 2393 for (i = 0; i < nbufs; ++i) 2394 if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED)) 2395 video_stream_enqueue(vs, vs->vs_buf[i]); 2396 } 2397 2398 vs->vs_method = method; 2399 mutex_exit(&vs->vs_lock); 2400 2401 return 0; 2402 } 2403 2404 /* Free all buffer memory in preparation for close(). This should 2405 * free buffers regardless of errors. Use video_stream_setup_bufs if 2406 * you need to check for errors. Streaming should be off before 2407 * calling this function. */ 2408 static void 2409 video_stream_teardown_bufs(struct video_stream *vs) 2410 { 2411 int err; 2412 2413 mutex_enter(&vs->vs_lock); 2414 2415 if (vs->vs_streaming) { 2416 DPRINTF(("video_stream_teardown_bufs: " 2417 "tearing down bufs while streaming\n")); 2418 } 2419 2420 /* dequeue all buffers */ 2421 while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL) 2422 SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries); 2423 while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL) 2424 SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries); 2425 2426 err = video_stream_free_bufs(vs); 2427 if (err != 0) { 2428 DPRINTF(("video_stream_teardown_bufs: " 2429 "error releasing buffers: %d\n", 2430 err)); 2431 } 2432 vs->vs_method = VIDEO_STREAM_METHOD_NONE; 2433 2434 mutex_exit(&vs->vs_lock); 2435 } 2436 2437 static struct video_buffer * 2438 video_buffer_alloc(void) 2439 { 2440 struct video_buffer *vb; 2441 2442 vb = kmem_alloc(sizeof(*vb), KM_SLEEP); 2443 vb->vb_buf = kmem_alloc(sizeof(*vb->vb_buf), KM_SLEEP); 2444 return vb; 2445 } 2446 2447 static void 2448 video_buffer_free(struct video_buffer *vb) 2449 { 2450 kmem_free(vb->vb_buf, sizeof(*vb->vb_buf)); 2451 vb->vb_buf = NULL; 2452 kmem_free(vb, sizeof(*vb)); 2453 } 2454 2455 /* TODO: for userptr method 2456 struct video_buffer * 2457 video_buf_alloc_with_ubuf(struct v4l2_buffer *buf) 2458 { 2459 } 2460 2461 void 2462 video_buffer_free_with_ubuf(struct video_buffer *vb) 2463 { 2464 } 2465 */ 2466 2467 static int 2468 video_stream_realloc_bufs(struct video_stream *vs, uint8_t nbufs) 2469 { 2470 int i, err; 2471 uint8_t minnbufs, oldnbufs; 2472 size_t size; 2473 off_t offset; 2474 struct video_buffer **oldbuf; 2475 struct v4l2_buffer *buf; 2476 2477 size = PAGE_ALIGN(vs->vs_format.sample_size) * nbufs; 2478 err = scatter_buf_set_size(&vs->vs_data, size); 2479 if (err != 0) 2480 return err; 2481 2482 oldnbufs = vs->vs_nbufs; 2483 oldbuf = vs->vs_buf; 2484 2485 vs->vs_nbufs = nbufs; 2486 if (nbufs > 0) { 2487 vs->vs_buf = 2488 kmem_alloc(sizeof(struct video_buffer *) * nbufs, KM_SLEEP); 2489 } else { 2490 vs->vs_buf = NULL; 2491 } 2492 2493 minnbufs = uimin(vs->vs_nbufs, oldnbufs); 2494 /* copy any bufs that will be reused */ 2495 for (i = 0; i < minnbufs; ++i) 2496 vs->vs_buf[i] = oldbuf[i]; 2497 /* allocate any necessary new bufs */ 2498 for (; i < vs->vs_nbufs; ++i) 2499 vs->vs_buf[i] = video_buffer_alloc(); 2500 /* free any bufs no longer used */ 2501 for (; i < oldnbufs; ++i) { 2502 video_buffer_free(oldbuf[i]); 2503 oldbuf[i] = NULL; 2504 } 2505 2506 /* Free old buffer metadata */ 2507 if (oldbuf != NULL) 2508 kmem_free(oldbuf, sizeof(struct video_buffer *) * oldnbufs); 2509 2510 /* initialize bufs */ 2511 offset = 0; 2512 for (i = 0; i < vs->vs_nbufs; ++i) { 2513 buf = vs->vs_buf[i]->vb_buf; 2514 buf->index = i; 2515 buf->type = vs->vs_type; 2516 buf->bytesused = 0; 2517 buf->flags = 0; 2518 buf->field = 0; 2519 buf->sequence = 0; 2520 buf->memory = V4L2_MEMORY_MMAP; 2521 buf->m.offset = offset; 2522 buf->length = PAGE_ALIGN(vs->vs_format.sample_size); 2523 buf->input = 0; 2524 buf->reserved = 0; 2525 2526 offset += buf->length; 2527 } 2528 2529 return 0; 2530 } 2531 2532 /* Accepts a video_sample into the ingress queue. Caller must hold 2533 * the stream lock. */ 2534 void 2535 video_stream_enqueue(struct video_stream *vs, struct video_buffer *vb) 2536 { 2537 if (vb->vb_buf->flags & V4L2_BUF_FLAG_QUEUED) { 2538 DPRINTF(("video_stream_enqueue: sample already queued\n")); 2539 return; 2540 } 2541 2542 vb->vb_buf->flags |= V4L2_BUF_FLAG_QUEUED; 2543 vb->vb_buf->flags &= ~V4L2_BUF_FLAG_DONE; 2544 2545 vb->vb_buf->bytesused = 0; 2546 2547 SIMPLEQ_INSERT_TAIL(&vs->vs_ingress, vb, entries); 2548 } 2549 2550 2551 /* Removes the head of the egress queue for use by userspace. Caller 2552 * must hold the stream lock. */ 2553 struct video_buffer * 2554 video_stream_dequeue(struct video_stream *vs) 2555 { 2556 struct video_buffer *vb; 2557 2558 if (!SIMPLEQ_EMPTY(&vs->vs_egress)) { 2559 vb = SIMPLEQ_FIRST(&vs->vs_egress); 2560 SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries); 2561 vb->vb_buf->flags &= ~V4L2_BUF_FLAG_QUEUED; 2562 vb->vb_buf->flags |= V4L2_BUF_FLAG_DONE; 2563 return vb; 2564 } else { 2565 return NULL; 2566 } 2567 } 2568 2569 static void 2570 v4l2buf_set_timestamp(struct v4l2_buffer *buf) 2571 { 2572 2573 getmicrotime(&buf->timestamp); 2574 } 2575 2576 /* 2577 * write payload data to the appropriate video sample, possibly moving 2578 * the sample from ingress to egress queues 2579 */ 2580 void 2581 video_stream_write(struct video_stream *vs, 2582 const struct video_payload *payload) 2583 { 2584 struct video_buffer *vb; 2585 struct v4l2_buffer *buf; 2586 struct scatter_io sio; 2587 2588 mutex_enter(&vs->vs_lock); 2589 2590 /* change of frameno implies end of current frame */ 2591 if (vs->vs_frameno >= 0 && vs->vs_frameno != payload->frameno) 2592 video_stream_sample_done(vs); 2593 2594 vs->vs_frameno = payload->frameno; 2595 2596 if (vs->vs_drop || SIMPLEQ_EMPTY(&vs->vs_ingress)) { 2597 /* DPRINTF(("video_stream_write: dropping sample %d\n", 2598 vs->vs_sequence)); */ 2599 vs->vs_drop = true; 2600 } else if (payload->size > 0) { 2601 vb = SIMPLEQ_FIRST(&vs->vs_ingress); 2602 buf = vb->vb_buf; 2603 if (!buf->bytesused) 2604 v4l2buf_set_timestamp(buf); 2605 if (payload->size > buf->length - buf->bytesused) { 2606 DPRINTF(("video_stream_write: " 2607 "payload would overflow\n")); 2608 } else if (scatter_io_init(&vs->vs_data, 2609 buf->m.offset + buf->bytesused, 2610 payload->size, 2611 &sio)) 2612 { 2613 scatter_io_copyin(&sio, payload->data); 2614 buf->bytesused += (payload->size - sio.sio_resid); 2615 } else { 2616 DPRINTF(("video_stream_write: failed to init scatter io " 2617 "vb=%p buf=%p " 2618 "buf->m.offset=%d buf->bytesused=%u " 2619 "payload->size=%zu\n", 2620 vb, buf, 2621 buf->m.offset, buf->bytesused, payload->size)); 2622 } 2623 } 2624 2625 /* if the payload marks it, we can do sample_done() early */ 2626 if (payload->end_of_frame) 2627 video_stream_sample_done(vs); 2628 2629 mutex_exit(&vs->vs_lock); 2630 } 2631 2632 2633 /* Moves the head of the ingress queue to the tail of the egress 2634 * queue, or resets drop status if we were dropping this sample. 2635 * Caller should hold the stream queue lock. */ 2636 void 2637 video_stream_sample_done(struct video_stream *vs) 2638 { 2639 struct video_buffer *vb; 2640 2641 if (vs->vs_drop) { 2642 vs->vs_drop = false; 2643 } else if (!SIMPLEQ_EMPTY(&vs->vs_ingress)) { 2644 vb = SIMPLEQ_FIRST(&vs->vs_ingress); 2645 vb->vb_buf->sequence = vs->vs_sequence; 2646 SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries); 2647 2648 SIMPLEQ_INSERT_TAIL(&vs->vs_egress, vb, entries); 2649 cv_signal(&vs->vs_sample_cv); 2650 selnotify(&vs->vs_sel, 0, 0); 2651 } else { 2652 DPRINTF(("video_stream_sample_done: no sample\n")); 2653 } 2654 2655 vs->vs_frameno ^= 1; 2656 vs->vs_sequence++; 2657 } 2658 2659 /* Check if all buffers are queued, i.e. none are under control of 2660 * userspace. */ 2661 /* 2662 static bool 2663 video_stream_all_queued(struct video_stream *vs) 2664 { 2665 } 2666 */ 2667 2668 2669 static void 2670 scatter_buf_init(struct scatter_buf *sb) 2671 { 2672 sb->sb_pool = pool_cache_init(PAGE_SIZE, 0, 0, 0, 2673 "video", NULL, IPL_VIDEO, 2674 NULL, NULL, NULL); 2675 sb->sb_size = 0; 2676 sb->sb_npages = 0; 2677 sb->sb_page_ary = NULL; 2678 } 2679 2680 static void 2681 scatter_buf_destroy(struct scatter_buf *sb) 2682 { 2683 /* Do we need to return everything to the pool first? */ 2684 scatter_buf_set_size(sb, 0); 2685 pool_cache_destroy(sb->sb_pool); 2686 sb->sb_pool = 0; 2687 sb->sb_npages = 0; 2688 sb->sb_page_ary = NULL; 2689 } 2690 2691 /* Increase or decrease the size of the buffer */ 2692 static int 2693 scatter_buf_set_size(struct scatter_buf *sb, size_t sz) 2694 { 2695 int i; 2696 size_t npages, minpages, oldnpages; 2697 uint8_t **old_ary; 2698 2699 npages = (sz >> PAGE_SHIFT) + ((sz & PAGE_MASK) > 0); 2700 2701 if (sb->sb_npages == npages) { 2702 return 0; 2703 } 2704 2705 oldnpages = sb->sb_npages; 2706 old_ary = sb->sb_page_ary; 2707 2708 sb->sb_npages = npages; 2709 if (npages > 0) { 2710 sb->sb_page_ary = 2711 kmem_alloc(sizeof(uint8_t *) * npages, KM_SLEEP); 2712 } else { 2713 sb->sb_page_ary = NULL; 2714 } 2715 2716 minpages = uimin(npages, oldnpages); 2717 /* copy any pages that will be reused */ 2718 for (i = 0; i < minpages; ++i) 2719 sb->sb_page_ary[i] = old_ary[i]; 2720 /* allocate any new pages */ 2721 for (; i < npages; ++i) 2722 sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, PR_WAITOK); 2723 /* return any pages no longer needed */ 2724 for (; i < oldnpages; ++i) 2725 pool_cache_put(sb->sb_pool, old_ary[i]); 2726 2727 if (old_ary != NULL) 2728 kmem_free(old_ary, sizeof(uint8_t *) * oldnpages); 2729 2730 sb->sb_size = sb->sb_npages << PAGE_SHIFT; 2731 2732 return 0; 2733 } 2734 2735 2736 static paddr_t 2737 scatter_buf_map(struct scatter_buf *sb, off_t off) 2738 { 2739 size_t pg; 2740 paddr_t pa; 2741 2742 pg = off >> PAGE_SHIFT; 2743 2744 if (pg >= sb->sb_npages) 2745 return -1; 2746 else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa)) 2747 return -1; 2748 2749 return atop(pa); 2750 } 2751 2752 /* Initialize data for an io operation on a scatter buffer. Returns 2753 * true if the transfer is valid, or false if out of range. */ 2754 static bool 2755 scatter_io_init(struct scatter_buf *sb, 2756 off_t off, size_t len, 2757 struct scatter_io *sio) 2758 { 2759 if ((off + len) > sb->sb_size) { 2760 DPRINTF(("video: scatter_io_init failed: off=%" PRId64 2761 " len=%zu sb->sb_size=%zu\n", 2762 off, len, sb->sb_size)); 2763 return false; 2764 } 2765 2766 sio->sio_buf = sb; 2767 sio->sio_offset = off; 2768 sio->sio_resid = len; 2769 2770 return true; 2771 } 2772 2773 /* Store the pointer and size of the next contiguous segment. Returns 2774 * true if the segment is valid, or false if all has been transferred. 2775 * Does not check for overflow. */ 2776 static bool 2777 scatter_io_next(struct scatter_io *sio, void **p, size_t *sz) 2778 { 2779 size_t pg, pgo; 2780 2781 if (sio->sio_resid == 0) 2782 return false; 2783 2784 pg = sio->sio_offset >> PAGE_SHIFT; 2785 pgo = sio->sio_offset & PAGE_MASK; 2786 2787 *sz = uimin(PAGE_SIZE - pgo, sio->sio_resid); 2788 *p = sio->sio_buf->sb_page_ary[pg] + pgo; 2789 2790 sio->sio_offset += *sz; 2791 sio->sio_resid -= *sz; 2792 2793 return true; 2794 } 2795 2796 /* Semi-undo of a failed segment copy. Updates the scatter_io 2797 * struct to the previous values prior to a failed segment copy. */ 2798 static void 2799 scatter_io_undo(struct scatter_io *sio, size_t sz) 2800 { 2801 sio->sio_offset -= sz; 2802 sio->sio_resid += sz; 2803 } 2804 2805 /* Copy data from src into the scatter_buf as described by io. */ 2806 static void 2807 scatter_io_copyin(struct scatter_io *sio, const void *p) 2808 { 2809 void *dst; 2810 const uint8_t *src = p; 2811 size_t sz; 2812 2813 while(scatter_io_next(sio, &dst, &sz)) { 2814 memcpy(dst, src, sz); 2815 src += sz; 2816 } 2817 } 2818 2819 /* --not used; commented to avoid compiler warnings-- 2820 static void 2821 scatter_io_copyout(struct scatter_io *sio, void *p) 2822 { 2823 void *src; 2824 uint8_t *dst = p; 2825 size_t sz; 2826 2827 while(scatter_io_next(sio, &src, &sz)) { 2828 memcpy(dst, src, sz); 2829 dst += sz; 2830 } 2831 } 2832 */ 2833 2834 /* Performat a series of uiomove calls on a scatter buf. Returns 2835 * EFAULT if uiomove EFAULTs on the first segment. Otherwise, returns 2836 * an incomplete transfer but with no error. */ 2837 static int 2838 scatter_io_uiomove(struct scatter_io *sio, struct uio *uio) 2839 { 2840 void *p; 2841 size_t sz; 2842 bool first = true; 2843 int err; 2844 2845 while(scatter_io_next(sio, &p, &sz)) { 2846 err = uiomove(p, sz, uio); 2847 if (err == EFAULT) { 2848 scatter_io_undo(sio, sz); 2849 if (first) 2850 return EFAULT; 2851 else 2852 return 0; 2853 } 2854 first = false; 2855 } 2856 2857 return 0; 2858 } 2859 2860 #endif /* NVIDEO > 0 */ 2861