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