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