1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
8 * Dave Stevenson <dsteve@broadcom.com>
9 * Simon Mellor <simellor@broadcom.com>
10 * Luke Diamand <luked@broadcom.com>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <media/videobuf2-vmalloc.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-common.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
28 #include "mmal-common.h"
29 #include "mmal-encodings.h"
30 #include "mmal-vchiq.h"
32 #include "mmal-parameters.h"
33 #include "bcm2835-camera.h"
35 #define BM2835_MMAL_VERSION "0.0.2"
36 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
39 #define MIN_BUFFER_SIZE (80 * 1024)
41 #define MAX_VIDEO_MODE_WIDTH 1280
42 #define MAX_VIDEO_MODE_HEIGHT 720
44 #define MAX_BCM2835_CAMERAS 2
46 int bcm2835_v4l2_debug;
47 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
48 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
51 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
52 module_param_array(video_nr, int, NULL, 0644);
53 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
55 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
56 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
57 module_param(max_video_width, int, 0644);
58 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
59 module_param(max_video_height, int, 0644);
60 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
62 /* global device data array */
63 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
68 /* timeperframe: min/max and default */
69 static const struct v4l2_fract
70 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
71 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
72 tpf_default = {.numerator = 1000, .denominator = 30000};
75 static struct mmal_fmt formats[] = {
77 .name = "4:2:0, planar, YUV",
78 .fourcc = V4L2_PIX_FMT_YUV420,
80 .mmal = MMAL_ENCODING_I420,
82 .mmal_component = MMAL_COMPONENT_CAMERA,
86 .name = "4:2:2, packed, YUYV",
87 .fourcc = V4L2_PIX_FMT_YUYV,
89 .mmal = MMAL_ENCODING_YUYV,
91 .mmal_component = MMAL_COMPONENT_CAMERA,
96 .fourcc = V4L2_PIX_FMT_RGB24,
98 .mmal = MMAL_ENCODING_RGB24,
100 .mmal_component = MMAL_COMPONENT_CAMERA,
105 .fourcc = V4L2_PIX_FMT_JPEG,
106 .flags = V4L2_FMT_FLAG_COMPRESSED,
107 .mmal = MMAL_ENCODING_JPEG,
109 .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
114 .fourcc = V4L2_PIX_FMT_H264,
115 .flags = V4L2_FMT_FLAG_COMPRESSED,
116 .mmal = MMAL_ENCODING_H264,
118 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
123 .fourcc = V4L2_PIX_FMT_MJPEG,
124 .flags = V4L2_FMT_FLAG_COMPRESSED,
125 .mmal = MMAL_ENCODING_MJPEG,
127 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
131 .name = "4:2:2, packed, YVYU",
132 .fourcc = V4L2_PIX_FMT_YVYU,
134 .mmal = MMAL_ENCODING_YVYU,
136 .mmal_component = MMAL_COMPONENT_CAMERA,
140 .name = "4:2:2, packed, VYUY",
141 .fourcc = V4L2_PIX_FMT_VYUY,
143 .mmal = MMAL_ENCODING_VYUY,
145 .mmal_component = MMAL_COMPONENT_CAMERA,
149 .name = "4:2:2, packed, UYVY",
150 .fourcc = V4L2_PIX_FMT_UYVY,
152 .mmal = MMAL_ENCODING_UYVY,
154 .mmal_component = MMAL_COMPONENT_CAMERA,
158 .name = "4:2:0, planar, NV12",
159 .fourcc = V4L2_PIX_FMT_NV12,
161 .mmal = MMAL_ENCODING_NV12,
163 .mmal_component = MMAL_COMPONENT_CAMERA,
167 .name = "RGB24 (BE)",
168 .fourcc = V4L2_PIX_FMT_BGR24,
170 .mmal = MMAL_ENCODING_BGR24,
172 .mmal_component = MMAL_COMPONENT_CAMERA,
176 .name = "4:2:0, planar, YVU",
177 .fourcc = V4L2_PIX_FMT_YVU420,
179 .mmal = MMAL_ENCODING_YV12,
181 .mmal_component = MMAL_COMPONENT_CAMERA,
185 .name = "4:2:0, planar, NV21",
186 .fourcc = V4L2_PIX_FMT_NV21,
188 .mmal = MMAL_ENCODING_NV21,
190 .mmal_component = MMAL_COMPONENT_CAMERA,
194 .name = "RGB32 (BE)",
195 .fourcc = V4L2_PIX_FMT_BGR32,
197 .mmal = MMAL_ENCODING_BGRA,
199 .mmal_component = MMAL_COMPONENT_CAMERA,
205 static struct mmal_fmt *get_format(struct v4l2_format *f)
207 struct mmal_fmt *fmt;
210 for (k = 0; k < ARRAY_SIZE(formats); k++) {
212 if (fmt->fourcc == f->fmt.pix.pixelformat)
219 /* ------------------------------------------------------------------
220 * Videobuf queue operations
221 * ------------------------------------------------------------------
224 static int queue_setup(struct vb2_queue *vq,
225 unsigned int *nbuffers, unsigned int *nplanes,
226 unsigned int sizes[], struct device *alloc_ctxs[])
228 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
231 /* refuse queue setup if port is not configured */
232 if (!dev->capture.port) {
233 v4l2_err(&dev->v4l2_dev,
234 "%s: capture port not configured\n", __func__);
238 size = dev->capture.port->current_buffer.size;
240 v4l2_err(&dev->v4l2_dev,
241 "%s: capture port buffer size is zero\n", __func__);
245 if (*nbuffers < dev->capture.port->minimum_buffer.num)
246 *nbuffers = dev->capture.port->minimum_buffer.num;
248 dev->capture.port->current_buffer.num = *nbuffers;
255 * videobuf2-vmalloc allocator is context-less so no need to set
259 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
265 static int buffer_init(struct vb2_buffer *vb)
267 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
268 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
269 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
271 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
273 buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
274 buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
276 return mmal_vchi_buffer_init(dev->instance, buf);
279 static int buffer_prepare(struct vb2_buffer *vb)
281 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
284 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
287 if (!dev->capture.port || !dev->capture.fmt)
290 size = dev->capture.stride * dev->capture.height;
291 if (vb2_plane_size(vb, 0) < size) {
292 v4l2_err(&dev->v4l2_dev,
293 "%s data will not fit into plane (%lu < %lu)\n",
294 __func__, vb2_plane_size(vb, 0), size);
301 static void buffer_cleanup(struct vb2_buffer *vb)
303 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
304 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
305 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
307 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
309 mmal_vchi_buffer_cleanup(buf);
312 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
314 return dev->capture.camera_port ==
316 component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
319 static void buffer_cb(struct vchiq_mmal_instance *instance,
320 struct vchiq_mmal_port *port,
322 struct mmal_buffer *buf,
323 unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
325 struct bm2835_mmal_dev *dev = port->cb_ctx;
327 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
328 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
329 __func__, status, buf, length, mmal_flags, pts);
332 /* error in transfer */
334 /* there was a buffer with the error so return it */
335 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
338 } else if (length == 0) {
341 /* this should only ever happen if the port is
342 * disabled and there are buffers still queued
344 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
345 pr_debug("Empty buffer");
346 } else if (dev->capture.frame_count) {
347 /* grab another frame */
348 if (is_capturing(dev)) {
349 pr_debug("Grab another frame");
350 vchiq_mmal_port_parameter_set(
352 dev->capture.camera_port,
353 MMAL_PARAMETER_CAPTURE,
354 &dev->capture.frame_count,
355 sizeof(dev->capture.frame_count));
358 /* signal frame completion */
359 complete(&dev->capture.frame_cmplt);
362 if (dev->capture.frame_count) {
363 if (dev->capture.vc_start_timestamp == -1) {
364 buf->vb.vb2_buf.timestamp = ktime_get_ns();
365 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
366 "Buffer time set as current time - %lld",
367 buf->vb.vb2_buf.timestamp);
369 } else if (pts != 0) {
371 s64 runtime_us = pts -
372 dev->capture.vc_start_timestamp;
373 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
375 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
376 "Convert start time %llu and %llu with offset %llu to %llu\n",
377 ktime_to_ns(dev->capture.kernel_start_ts),
378 dev->capture.vc_start_timestamp, pts,
379 ktime_to_ns(timestamp));
380 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
382 if (dev->capture.last_timestamp) {
383 buf->vb.vb2_buf.timestamp =
384 dev->capture.last_timestamp;
385 v4l2_dbg(1, bcm2835_v4l2_debug,
387 "Buffer time set as last timestamp - %lld",
388 buf->vb.vb2_buf.timestamp);
390 buf->vb.vb2_buf.timestamp =
391 ktime_to_ns(dev->capture.kernel_start_ts);
392 v4l2_dbg(1, bcm2835_v4l2_debug,
394 "Buffer time set as start timestamp - %lld",
395 buf->vb.vb2_buf.timestamp);
398 dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
400 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
401 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
402 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
404 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
405 "Buffer has ts %llu",
406 dev->capture.last_timestamp);
407 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
409 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
411 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
412 "Grab another frame as buffer has EOS");
413 vchiq_mmal_port_parameter_set(
415 dev->capture.camera_port,
416 MMAL_PARAMETER_CAPTURE,
417 &dev->capture.frame_count,
418 sizeof(dev->capture.frame_count));
421 /* signal frame completion */
422 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
423 complete(&dev->capture.frame_cmplt);
428 static int enable_camera(struct bm2835_mmal_dev *dev)
432 if (!dev->camera_use_count) {
433 ret = vchiq_mmal_port_parameter_set(
435 &dev->component[MMAL_COMPONENT_CAMERA]->control,
436 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
437 sizeof(dev->camera_num));
439 v4l2_err(&dev->v4l2_dev,
440 "Failed setting camera num, ret %d\n", ret);
444 ret = vchiq_mmal_component_enable(
446 dev->component[MMAL_COMPONENT_CAMERA]);
448 v4l2_err(&dev->v4l2_dev,
449 "Failed enabling camera, ret %d\n", ret);
453 dev->camera_use_count++;
454 v4l2_dbg(1, bcm2835_v4l2_debug,
455 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
456 dev->camera_use_count);
460 static int disable_camera(struct bm2835_mmal_dev *dev)
464 if (!dev->camera_use_count) {
465 v4l2_err(&dev->v4l2_dev,
466 "Disabled the camera when already disabled\n");
469 dev->camera_use_count--;
470 if (!dev->camera_use_count) {
471 unsigned int i = 0xFFFFFFFF;
473 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
474 "Disabling camera\n");
476 vchiq_mmal_component_disable(
478 dev->component[MMAL_COMPONENT_CAMERA]);
480 v4l2_err(&dev->v4l2_dev,
481 "Failed disabling camera, ret %d\n", ret);
484 vchiq_mmal_port_parameter_set(
486 &dev->component[MMAL_COMPONENT_CAMERA]->control,
487 MMAL_PARAMETER_CAMERA_NUM, &i,
490 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
491 "Camera refcount now %d\n", dev->camera_use_count);
495 static void buffer_queue(struct vb2_buffer *vb)
497 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
498 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
499 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
502 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
503 "%s: dev:%p buf:%p, idx %u\n",
504 __func__, dev, buf, vb2->vb2_buf.index);
506 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
508 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
512 static int start_streaming(struct vb2_queue *vq, unsigned int count)
514 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
518 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
521 /* ensure a format has actually been set */
522 if (!dev->capture.port)
525 if (enable_camera(dev) < 0) {
526 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
530 /*init_completion(&dev->capture.frame_cmplt); */
532 /* enable frame capture */
533 dev->capture.frame_count = 1;
535 /* if the preview is not already running, wait for a few frames for AGC
538 if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
541 /* enable the connection from camera to encoder (if applicable) */
542 if (dev->capture.camera_port != dev->capture.port
543 && dev->capture.camera_port) {
544 ret = vchiq_mmal_port_enable(dev->instance,
545 dev->capture.camera_port, NULL);
547 v4l2_err(&dev->v4l2_dev,
548 "Failed to enable encode tunnel - error %d\n",
554 /* Get VC timestamp at this point in time */
555 parameter_size = sizeof(dev->capture.vc_start_timestamp);
556 if (vchiq_mmal_port_parameter_get(dev->instance,
557 dev->capture.camera_port,
558 MMAL_PARAMETER_SYSTEM_TIME,
559 &dev->capture.vc_start_timestamp,
561 v4l2_err(&dev->v4l2_dev,
562 "Failed to get VC start time - update your VC f/w\n");
564 /* Flag to indicate just to rely on kernel timestamps */
565 dev->capture.vc_start_timestamp = -1;
567 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
568 "Start time %lld size %d\n",
569 dev->capture.vc_start_timestamp, parameter_size);
571 dev->capture.kernel_start_ts = ktime_get();
572 dev->capture.last_timestamp = 0;
574 /* enable the camera port */
575 dev->capture.port->cb_ctx = dev;
577 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
579 v4l2_err(&dev->v4l2_dev,
580 "Failed to enable capture port - error %d. Disabling camera port again\n",
583 vchiq_mmal_port_disable(dev->instance,
584 dev->capture.camera_port);
585 if (disable_camera(dev) < 0) {
586 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
592 /* capture the first frame */
593 vchiq_mmal_port_parameter_set(dev->instance,
594 dev->capture.camera_port,
595 MMAL_PARAMETER_CAPTURE,
596 &dev->capture.frame_count,
597 sizeof(dev->capture.frame_count));
601 /* abort streaming and wait for last buffer */
602 static void stop_streaming(struct vb2_queue *vq)
605 unsigned long timeout;
606 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
608 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
611 init_completion(&dev->capture.frame_cmplt);
612 dev->capture.frame_count = 0;
614 /* ensure a format has actually been set */
615 if (!dev->capture.port) {
616 v4l2_err(&dev->v4l2_dev,
617 "no capture port - stream not started?\n");
621 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
623 /* stop capturing frames */
624 vchiq_mmal_port_parameter_set(dev->instance,
625 dev->capture.camera_port,
626 MMAL_PARAMETER_CAPTURE,
627 &dev->capture.frame_count,
628 sizeof(dev->capture.frame_count));
630 /* wait for last frame to complete */
631 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
633 v4l2_err(&dev->v4l2_dev,
634 "timed out waiting for frame completion\n");
636 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
637 "disabling connection\n");
639 /* disable the connection from camera to encoder */
640 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
641 if (!ret && dev->capture.camera_port != dev->capture.port) {
642 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
644 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
645 } else if (dev->capture.camera_port != dev->capture.port) {
646 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
650 if (disable_camera(dev) < 0)
651 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
654 static const struct vb2_ops bm2835_mmal_video_qops = {
655 .queue_setup = queue_setup,
656 .buf_init = buffer_init,
657 .buf_prepare = buffer_prepare,
658 .buf_cleanup = buffer_cleanup,
659 .buf_queue = buffer_queue,
660 .start_streaming = start_streaming,
661 .stop_streaming = stop_streaming,
662 .wait_prepare = vb2_ops_wait_prepare,
663 .wait_finish = vb2_ops_wait_finish,
666 /* ------------------------------------------------------------------
668 * ------------------------------------------------------------------
671 static int set_overlay_params(struct bm2835_mmal_dev *dev,
672 struct vchiq_mmal_port *port)
674 struct mmal_parameter_displayregion prev_config = {
675 .set = MMAL_DISPLAY_SET_LAYER |
676 MMAL_DISPLAY_SET_ALPHA |
677 MMAL_DISPLAY_SET_DEST_RECT |
678 MMAL_DISPLAY_SET_FULLSCREEN,
679 .layer = PREVIEW_LAYER,
680 .alpha = dev->overlay.global_alpha,
683 .x = dev->overlay.w.left,
684 .y = dev->overlay.w.top,
685 .width = dev->overlay.w.width,
686 .height = dev->overlay.w.height,
689 return vchiq_mmal_port_parameter_set(dev->instance, port,
690 MMAL_PARAMETER_DISPLAYREGION,
691 &prev_config, sizeof(prev_config));
695 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
696 struct v4l2_fmtdesc *f)
698 struct mmal_fmt *fmt;
700 if (f->index >= ARRAY_SIZE(formats))
703 fmt = &formats[f->index];
705 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
706 f->pixelformat = fmt->fourcc;
707 f->flags = fmt->flags;
712 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
713 struct v4l2_format *f)
715 struct bm2835_mmal_dev *dev = video_drvdata(file);
717 f->fmt.win = dev->overlay;
722 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
723 struct v4l2_format *f)
725 struct bm2835_mmal_dev *dev = video_drvdata(file);
727 f->fmt.win.field = V4L2_FIELD_NONE;
728 f->fmt.win.chromakey = 0;
729 f->fmt.win.clips = NULL;
730 f->fmt.win.clipcount = 0;
731 f->fmt.win.bitmap = NULL;
733 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
734 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
736 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
737 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
740 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
741 "Overlay: Now w/h %dx%d l/t %dx%d\n",
742 f->fmt.win.w.width, f->fmt.win.w.height,
743 f->fmt.win.w.left, f->fmt.win.w.top);
745 v4l2_dump_win_format(1,
753 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
754 struct v4l2_format *f)
756 struct bm2835_mmal_dev *dev = video_drvdata(file);
758 vidioc_try_fmt_vid_overlay(file, priv, f);
760 dev->overlay = f->fmt.win;
761 if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
762 set_overlay_params(dev,
763 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
769 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
772 struct bm2835_mmal_dev *dev = video_drvdata(file);
773 struct vchiq_mmal_port *src;
774 struct vchiq_mmal_port *dst;
776 if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
777 (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
778 return 0; /* already in requested state */
781 &dev->component[MMAL_COMPONENT_CAMERA]->
782 output[MMAL_CAMERA_PORT_PREVIEW];
785 /* disconnect preview ports and disable component */
786 ret = vchiq_mmal_port_disable(dev->instance, src);
789 vchiq_mmal_port_connect_tunnel(dev->instance, src,
792 ret = vchiq_mmal_component_disable(
794 dev->component[MMAL_COMPONENT_PREVIEW]);
800 /* set preview port format and connect it to output */
801 dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
803 ret = vchiq_mmal_port_set_format(dev->instance, src);
807 ret = set_overlay_params(dev, dst);
811 if (enable_camera(dev) < 0)
814 ret = vchiq_mmal_component_enable(
816 dev->component[MMAL_COMPONENT_PREVIEW]);
820 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
822 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
824 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
829 static int vidioc_g_fbuf(struct file *file, void *fh,
830 struct v4l2_framebuffer *a)
832 /* The video overlay must stay within the framebuffer and can't be
833 * positioned independently.
835 struct bm2835_mmal_dev *dev = video_drvdata(file);
836 struct vchiq_mmal_port *preview_port =
837 &dev->component[MMAL_COMPONENT_CAMERA]->
838 output[MMAL_CAMERA_PORT_PREVIEW];
840 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
841 V4L2_FBUF_CAP_GLOBAL_ALPHA;
842 a->flags = V4L2_FBUF_FLAG_OVERLAY;
843 a->fmt.width = preview_port->es.video.width;
844 a->fmt.height = preview_port->es.video.height;
845 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
846 a->fmt.bytesperline = preview_port->es.video.width;
847 a->fmt.sizeimage = (preview_port->es.video.width *
848 preview_port->es.video.height * 3) >> 1;
849 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
855 static int vidioc_enum_input(struct file *file, void *priv,
856 struct v4l2_input *inp)
858 /* only a single camera input */
862 inp->type = V4L2_INPUT_TYPE_CAMERA;
863 sprintf((char *)inp->name, "Camera %u", inp->index);
867 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
873 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
882 static int vidioc_querycap(struct file *file, void *priv,
883 struct v4l2_capability *cap)
885 struct bm2835_mmal_dev *dev = video_drvdata(file);
889 vchiq_mmal_version(dev->instance, &major, &minor);
891 strcpy((char *)cap->driver, "bm2835 mmal");
892 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
895 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
896 "platform:%s", dev->v4l2_dev.name);
897 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
898 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
899 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
904 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
905 struct v4l2_fmtdesc *f)
907 struct mmal_fmt *fmt;
909 if (f->index >= ARRAY_SIZE(formats))
912 fmt = &formats[f->index];
914 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
915 f->pixelformat = fmt->fourcc;
916 f->flags = fmt->flags;
921 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
922 struct v4l2_format *f)
924 struct bm2835_mmal_dev *dev = video_drvdata(file);
926 f->fmt.pix.width = dev->capture.width;
927 f->fmt.pix.height = dev->capture.height;
928 f->fmt.pix.field = V4L2_FIELD_NONE;
929 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
930 f->fmt.pix.bytesperline = dev->capture.stride;
931 f->fmt.pix.sizeimage = dev->capture.buffersize;
933 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
934 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
935 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
936 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
938 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
941 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
946 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
947 struct v4l2_format *f)
949 struct bm2835_mmal_dev *dev = video_drvdata(file);
950 struct mmal_fmt *mfmt;
952 mfmt = get_format(f);
954 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
955 "Fourcc format (0x%08x) unknown.\n",
956 f->fmt.pix.pixelformat);
957 f->fmt.pix.pixelformat = formats[0].fourcc;
958 mfmt = get_format(f);
961 f->fmt.pix.field = V4L2_FIELD_NONE;
963 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
964 "Clipping/aligning %dx%d format %08X\n",
965 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
967 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
968 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
970 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
971 if (!mfmt->remove_padding) {
972 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
973 /* GPU isn't removing padding, so stride is aligned to 32 */
974 f->fmt.pix.bytesperline =
975 (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
976 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
977 "Not removing padding, so bytes/line = %d, "
979 f->fmt.pix.bytesperline, align_mask);
982 /* Image buffer has to be padded to allow for alignment, even though
983 * we sometimes then remove that padding before delivering the buffer.
985 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
986 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
988 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
989 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
990 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
992 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
993 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
994 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
995 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
997 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1000 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1001 "Now %dx%d format %08X\n",
1002 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1004 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1009 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1010 struct v4l2_format *f)
1013 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1014 struct vchiq_mmal_component *encode_component = NULL;
1015 struct mmal_fmt *mfmt = get_format(f);
1021 if (dev->capture.encode_component) {
1022 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1023 "vid_cap - disconnect previous tunnel\n");
1025 /* Disconnect any previous connection */
1026 vchiq_mmal_port_connect_tunnel(dev->instance,
1027 dev->capture.camera_port, NULL);
1028 dev->capture.camera_port = NULL;
1029 ret = vchiq_mmal_component_disable(dev->instance,
1033 v4l2_err(&dev->v4l2_dev,
1034 "Failed to disable encode component %d\n",
1037 dev->capture.encode_component = NULL;
1039 /* format dependent port setup */
1040 switch (mfmt->mmal_component) {
1041 case MMAL_COMPONENT_CAMERA:
1042 /* Make a further decision on port based on resolution */
1043 if (f->fmt.pix.width <= max_video_width
1044 && f->fmt.pix.height <= max_video_height)
1045 camera_port = port =
1046 &dev->component[MMAL_COMPONENT_CAMERA]->
1047 output[MMAL_CAMERA_PORT_VIDEO];
1049 camera_port = port =
1050 &dev->component[MMAL_COMPONENT_CAMERA]->
1051 output[MMAL_CAMERA_PORT_CAPTURE];
1053 case MMAL_COMPONENT_IMAGE_ENCODE:
1054 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1055 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1057 &dev->component[MMAL_COMPONENT_CAMERA]->
1058 output[MMAL_CAMERA_PORT_CAPTURE];
1060 case MMAL_COMPONENT_VIDEO_ENCODE:
1061 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1062 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1064 &dev->component[MMAL_COMPONENT_CAMERA]->
1065 output[MMAL_CAMERA_PORT_VIDEO];
1074 if (encode_component)
1075 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1077 camera_port->format.encoding = mfmt->mmal;
1079 if (dev->rgb_bgr_swapped) {
1080 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1081 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1082 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1083 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1086 remove_padding = mfmt->remove_padding;
1087 vchiq_mmal_port_parameter_set(dev->instance,
1089 MMAL_PARAMETER_NO_IMAGE_PADDING,
1090 &remove_padding, sizeof(remove_padding));
1092 camera_port->format.encoding_variant = 0;
1093 camera_port->es.video.width = f->fmt.pix.width;
1094 camera_port->es.video.height = f->fmt.pix.height;
1095 camera_port->es.video.crop.x = 0;
1096 camera_port->es.video.crop.y = 0;
1097 camera_port->es.video.crop.width = f->fmt.pix.width;
1098 camera_port->es.video.crop.height = f->fmt.pix.height;
1099 camera_port->es.video.frame_rate.num = 0;
1100 camera_port->es.video.frame_rate.den = 1;
1101 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1103 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1107 &dev->component[MMAL_COMPONENT_CAMERA]->
1108 output[MMAL_CAMERA_PORT_VIDEO]) {
1109 bool overlay_enabled =
1110 !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1111 struct vchiq_mmal_port *preview_port =
1112 &dev->component[MMAL_COMPONENT_CAMERA]->
1113 output[MMAL_CAMERA_PORT_PREVIEW];
1114 /* Preview and encode ports need to match on resolution */
1115 if (overlay_enabled) {
1116 /* Need to disable the overlay before we can update
1120 vchiq_mmal_port_disable(dev->instance,
1124 vchiq_mmal_port_connect_tunnel(
1129 preview_port->es.video.width = f->fmt.pix.width;
1130 preview_port->es.video.height = f->fmt.pix.height;
1131 preview_port->es.video.crop.x = 0;
1132 preview_port->es.video.crop.y = 0;
1133 preview_port->es.video.crop.width = f->fmt.pix.width;
1134 preview_port->es.video.crop.height = f->fmt.pix.height;
1135 preview_port->es.video.frame_rate.num =
1136 dev->capture.timeperframe.denominator;
1137 preview_port->es.video.frame_rate.den =
1138 dev->capture.timeperframe.numerator;
1139 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1140 if (overlay_enabled) {
1141 ret = vchiq_mmal_port_connect_tunnel(
1144 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1146 ret = vchiq_mmal_port_enable(dev->instance,
1153 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1154 "%s failed to set format %dx%d %08X\n", __func__,
1155 f->fmt.pix.width, f->fmt.pix.height,
1156 f->fmt.pix.pixelformat);
1157 /* ensure capture is not going to be tried */
1158 dev->capture.port = NULL;
1160 if (encode_component) {
1161 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1162 "vid_cap - set up encode comp\n");
1164 /* configure buffering */
1165 camera_port->current_buffer.size =
1166 camera_port->recommended_buffer.size;
1167 camera_port->current_buffer.num =
1168 camera_port->recommended_buffer.num;
1171 vchiq_mmal_port_connect_tunnel(
1174 &encode_component->input[0]);
1176 v4l2_dbg(1, bcm2835_v4l2_debug,
1178 "%s failed to create connection\n",
1180 /* ensure capture is not going to be tried */
1181 dev->capture.port = NULL;
1183 port->es.video.width = f->fmt.pix.width;
1184 port->es.video.height = f->fmt.pix.height;
1185 port->es.video.crop.x = 0;
1186 port->es.video.crop.y = 0;
1187 port->es.video.crop.width = f->fmt.pix.width;
1188 port->es.video.crop.height = f->fmt.pix.height;
1189 port->es.video.frame_rate.num =
1190 dev->capture.timeperframe.denominator;
1191 port->es.video.frame_rate.den =
1192 dev->capture.timeperframe.numerator;
1194 port->format.encoding = mfmt->mmal;
1195 port->format.encoding_variant = 0;
1196 /* Set any encoding specific parameters */
1197 switch (mfmt->mmal_component) {
1198 case MMAL_COMPONENT_VIDEO_ENCODE:
1199 port->format.bitrate =
1200 dev->capture.encode_bitrate;
1202 case MMAL_COMPONENT_IMAGE_ENCODE:
1203 /* Could set EXIF parameters here */
1208 ret = vchiq_mmal_port_set_format(dev->instance,
1211 v4l2_dbg(1, bcm2835_v4l2_debug,
1213 "%s failed to set format %dx%d fmt %08X\n",
1217 f->fmt.pix.pixelformat
1222 ret = vchiq_mmal_component_enable(
1226 v4l2_dbg(1, bcm2835_v4l2_debug,
1228 "%s Failed to enable encode components\n",
1233 /* configure buffering */
1234 port->current_buffer.num = 1;
1235 port->current_buffer.size =
1236 f->fmt.pix.sizeimage;
1237 if (port->format.encoding ==
1238 MMAL_ENCODING_JPEG) {
1239 v4l2_dbg(1, bcm2835_v4l2_debug,
1241 "JPG - buf size now %d was %d\n",
1242 f->fmt.pix.sizeimage,
1243 port->current_buffer.size);
1244 port->current_buffer.size =
1245 (f->fmt.pix.sizeimage <
1248 : f->fmt.pix.sizeimage;
1250 v4l2_dbg(1, bcm2835_v4l2_debug,
1252 "vid_cap - cur_buf.size set to %d\n",
1253 f->fmt.pix.sizeimage);
1254 port->current_buffer.alignment = 0;
1257 /* configure buffering */
1258 camera_port->current_buffer.num = 1;
1259 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1260 camera_port->current_buffer.alignment = 0;
1264 dev->capture.fmt = mfmt;
1265 dev->capture.stride = f->fmt.pix.bytesperline;
1266 dev->capture.width = camera_port->es.video.crop.width;
1267 dev->capture.height = camera_port->es.video.crop.height;
1268 dev->capture.buffersize = port->current_buffer.size;
1270 /* select port for capture */
1271 dev->capture.port = port;
1272 dev->capture.camera_port = camera_port;
1273 dev->capture.encode_component = encode_component;
1274 v4l2_dbg(1, bcm2835_v4l2_debug,
1276 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1277 port->format.encoding,
1278 dev->capture.width, dev->capture.height,
1279 dev->capture.stride, dev->capture.buffersize);
1283 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1287 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1288 struct v4l2_format *f)
1291 struct bm2835_mmal_dev *dev = video_drvdata(file);
1292 struct mmal_fmt *mfmt;
1294 /* try the format to set valid parameters */
1295 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1297 v4l2_err(&dev->v4l2_dev,
1298 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1302 /* if a capture is running refuse to set format */
1303 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1304 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1308 /* If the format is unsupported v4l2 says we should switch to
1309 * a supported one and not return an error.
1311 mfmt = get_format(f);
1313 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1314 "Fourcc format (0x%08x) unknown.\n",
1315 f->fmt.pix.pixelformat);
1316 f->fmt.pix.pixelformat = formats[0].fourcc;
1317 mfmt = get_format(f);
1320 ret = mmal_setup_components(dev, f);
1322 v4l2_err(&dev->v4l2_dev,
1323 "%s: failed to setup mmal components: %d\n",
1331 static int vidioc_enum_framesizes(struct file *file, void *fh,
1332 struct v4l2_frmsizeenum *fsize)
1334 struct bm2835_mmal_dev *dev = video_drvdata(file);
1335 static const struct v4l2_frmsize_stepwise sizes = {
1343 for (i = 0; i < ARRAY_SIZE(formats); i++)
1344 if (formats[i].fourcc == fsize->pixel_format)
1346 if (i == ARRAY_SIZE(formats))
1348 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1349 fsize->stepwise = sizes;
1350 fsize->stepwise.max_width = dev->max_width;
1351 fsize->stepwise.max_height = dev->max_height;
1355 /* timeperframe is arbitrary and continuous */
1356 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1357 struct v4l2_frmivalenum *fival)
1359 struct bm2835_mmal_dev *dev = video_drvdata(file);
1365 for (i = 0; i < ARRAY_SIZE(formats); i++)
1366 if (formats[i].fourcc == fival->pixel_format)
1368 if (i == ARRAY_SIZE(formats))
1371 /* regarding width & height - we support any within range */
1372 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1373 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1376 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1378 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1379 fival->stepwise.min = tpf_min;
1380 fival->stepwise.max = tpf_max;
1381 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1386 static int vidioc_g_parm(struct file *file, void *priv,
1387 struct v4l2_streamparm *parm)
1389 struct bm2835_mmal_dev *dev = video_drvdata(file);
1391 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1395 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1396 parm->parm.capture.readbuffers = 1;
1400 #define FRACT_CMP(a, OP, b) \
1401 ((u64)(a).numerator * (b).denominator OP \
1402 (u64)(b).numerator * (a).denominator)
1404 static int vidioc_s_parm(struct file *file, void *priv,
1405 struct v4l2_streamparm *parm)
1407 struct bm2835_mmal_dev *dev = video_drvdata(file);
1408 struct v4l2_fract tpf;
1410 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1413 tpf = parm->parm.capture.timeperframe;
1415 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1416 tpf = tpf.denominator ? tpf : tpf_default;
1417 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1418 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1420 dev->capture.timeperframe = tpf;
1421 parm->parm.capture.timeperframe = tpf;
1422 parm->parm.capture.readbuffers = 1;
1423 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1425 set_framerate_params(dev);
1430 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1432 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1433 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1434 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1435 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1436 .vidioc_overlay = vidioc_overlay,
1437 .vidioc_g_fbuf = vidioc_g_fbuf,
1440 .vidioc_enum_input = vidioc_enum_input,
1441 .vidioc_g_input = vidioc_g_input,
1442 .vidioc_s_input = vidioc_s_input,
1445 .vidioc_querycap = vidioc_querycap,
1446 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1447 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1448 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1449 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1451 /* buffer management */
1452 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1453 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1454 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1455 .vidioc_querybuf = vb2_ioctl_querybuf,
1456 .vidioc_qbuf = vb2_ioctl_qbuf,
1457 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1458 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1459 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1460 .vidioc_g_parm = vidioc_g_parm,
1461 .vidioc_s_parm = vidioc_s_parm,
1462 .vidioc_streamon = vb2_ioctl_streamon,
1463 .vidioc_streamoff = vb2_ioctl_streamoff,
1465 .vidioc_log_status = v4l2_ctrl_log_status,
1466 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1467 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1470 /* ------------------------------------------------------------------
1471 * Driver init/finalise
1472 * ------------------------------------------------------------------
1475 static const struct v4l2_file_operations camera0_fops = {
1476 .owner = THIS_MODULE,
1477 .open = v4l2_fh_open,
1478 .release = vb2_fop_release,
1479 .read = vb2_fop_read,
1480 .poll = vb2_fop_poll,
1481 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1482 .mmap = vb2_fop_mmap,
1485 static const struct video_device vdev_template = {
1487 .fops = &camera0_fops,
1488 .ioctl_ops = &camera0_ioctl_ops,
1489 .release = video_device_release_empty,
1492 /* Returns the number of cameras, and also the max resolution supported
1495 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1496 unsigned int resolutions[][2], int num_resolutions)
1499 struct vchiq_mmal_component *cam_info_component;
1500 struct mmal_parameter_camera_info_t cam_info = {0};
1501 u32 param_size = sizeof(cam_info);
1504 /* create a camera_info component */
1505 ret = vchiq_mmal_component_init(instance, "camera_info",
1506 &cam_info_component);
1508 /* Unusual failure - let's guess one camera. */
1511 if (vchiq_mmal_port_parameter_get(instance,
1512 &cam_info_component->control,
1513 MMAL_PARAMETER_CAMERA_INFO,
1516 pr_info("Failed to get camera info\n");
1519 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1521 resolutions[i][0] = cam_info.cameras[i].max_width;
1522 resolutions[i][1] = cam_info.cameras[i].max_height;
1525 vchiq_mmal_component_finalise(instance,
1526 cam_info_component);
1528 return cam_info.num_cameras;
1531 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1532 struct vchiq_mmal_component *camera,
1533 struct bm2835_mmal_dev *dev)
1536 struct mmal_parameter_camera_config cam_config = {
1537 .max_stills_w = dev->max_width,
1538 .max_stills_h = dev->max_height,
1540 .one_shot_stills = 1,
1541 .max_preview_video_w = (max_video_width > 1920) ?
1542 max_video_width : 1920,
1543 .max_preview_video_h = (max_video_height > 1088) ?
1544 max_video_height : 1088,
1545 .num_preview_video_frames = 3,
1546 .stills_capture_circular_buffer_height = 0,
1547 .fast_preview_resume = 0,
1548 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1551 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1552 MMAL_PARAMETER_CAMERA_CONFIG,
1553 &cam_config, sizeof(cam_config));
1557 #define MAX_SUPPORTED_ENCODINGS 20
1559 /* MMAL instance and component init */
1560 static int mmal_init(struct bm2835_mmal_dev *dev)
1563 struct mmal_es_format_local *format;
1564 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1566 struct vchiq_mmal_component *camera;
1568 ret = vchiq_mmal_init(&dev->instance);
1570 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1575 /* get the camera component ready */
1576 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1577 &dev->component[MMAL_COMPONENT_CAMERA]);
1581 camera = dev->component[MMAL_COMPONENT_CAMERA];
1582 if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1583 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1584 __func__, camera->outputs, MMAL_CAMERA_PORT_COUNT);
1589 ret = set_camera_parameters(dev->instance,
1593 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1598 /* There was an error in the firmware that meant the camera component
1599 * produced BGR instead of RGB.
1600 * This is now fixed, but in order to support the old firmwares, we
1603 dev->rgb_bgr_swapped = true;
1604 param_size = sizeof(supported_encodings);
1605 ret = vchiq_mmal_port_parameter_get(dev->instance,
1606 &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1607 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1608 &supported_encodings,
1613 for (i = 0; i < param_size / sizeof(u32); i++) {
1614 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1615 /* Found BGR24 first - old firmware. */
1618 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1619 /* Found RGB24 first
1620 * new firmware, so use RGB24.
1622 dev->rgb_bgr_swapped = false;
1627 format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1629 format->encoding = MMAL_ENCODING_OPAQUE;
1630 format->encoding_variant = MMAL_ENCODING_I420;
1632 format->es->video.width = 1024;
1633 format->es->video.height = 768;
1634 format->es->video.crop.x = 0;
1635 format->es->video.crop.y = 0;
1636 format->es->video.crop.width = 1024;
1637 format->es->video.crop.height = 768;
1638 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1639 format->es->video.frame_rate.den = 1;
1641 format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1643 format->encoding = MMAL_ENCODING_OPAQUE;
1644 format->encoding_variant = MMAL_ENCODING_I420;
1646 format->es->video.width = 1024;
1647 format->es->video.height = 768;
1648 format->es->video.crop.x = 0;
1649 format->es->video.crop.y = 0;
1650 format->es->video.crop.width = 1024;
1651 format->es->video.crop.height = 768;
1652 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1653 format->es->video.frame_rate.den = 1;
1655 format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1657 format->encoding = MMAL_ENCODING_OPAQUE;
1659 format->es->video.width = 2592;
1660 format->es->video.height = 1944;
1661 format->es->video.crop.x = 0;
1662 format->es->video.crop.y = 0;
1663 format->es->video.crop.width = 2592;
1664 format->es->video.crop.height = 1944;
1665 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1666 format->es->video.frame_rate.den = 1;
1668 dev->capture.width = format->es->video.width;
1669 dev->capture.height = format->es->video.height;
1670 dev->capture.fmt = &formats[0];
1671 dev->capture.encode_component = NULL;
1672 dev->capture.timeperframe = tpf_default;
1673 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1674 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1676 /* get the preview component ready */
1677 ret = vchiq_mmal_component_init(
1678 dev->instance, "ril.video_render",
1679 &dev->component[MMAL_COMPONENT_PREVIEW]);
1683 if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1685 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1686 __func__, dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1690 /* get the image encoder component ready */
1691 ret = vchiq_mmal_component_init(
1692 dev->instance, "ril.image_encode",
1693 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1697 if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1699 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1700 __func__, dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1702 goto unreg_image_encoder;
1705 /* get the video encoder component ready */
1706 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1708 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1710 goto unreg_image_encoder;
1712 if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1714 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1715 __func__, dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1717 goto unreg_vid_encoder;
1721 struct vchiq_mmal_port *encoder_port =
1722 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1723 encoder_port->format.encoding = MMAL_ENCODING_H264;
1724 ret = vchiq_mmal_port_set_format(dev->instance,
1729 unsigned int enable = 1;
1731 vchiq_mmal_port_parameter_set(
1733 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1734 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1735 &enable, sizeof(enable));
1737 vchiq_mmal_port_parameter_set(dev->instance,
1738 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1739 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1743 ret = bm2835_mmal_set_all_camera_controls(dev);
1745 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1747 goto unreg_vid_encoder;
1753 pr_err("Cleanup: Destroy video encoder\n");
1754 vchiq_mmal_component_finalise(
1756 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1758 unreg_image_encoder:
1759 pr_err("Cleanup: Destroy image encoder\n");
1760 vchiq_mmal_component_finalise(
1762 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1765 pr_err("Cleanup: Destroy video render\n");
1766 vchiq_mmal_component_finalise(dev->instance,
1767 dev->component[MMAL_COMPONENT_PREVIEW]);
1770 pr_err("Cleanup: Destroy camera\n");
1771 vchiq_mmal_component_finalise(dev->instance,
1772 dev->component[MMAL_COMPONENT_CAMERA]);
1775 vchiq_mmal_finalise(dev->instance);
1779 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1780 struct video_device *vfd)
1784 *vfd = vdev_template;
1786 vfd->v4l2_dev = &dev->v4l2_dev;
1788 vfd->lock = &dev->mutex;
1790 vfd->queue = &dev->capture.vb_vidq;
1792 /* video device needs to be able to access instance data */
1793 video_set_drvdata(vfd, dev);
1795 ret = video_register_device(vfd,
1797 video_nr[dev->camera_num]);
1801 v4l2_info(vfd->v4l2_dev,
1802 "V4L2 device registered as %s - stills mode > %dx%d\n",
1803 video_device_node_name(vfd),
1804 max_video_width, max_video_height);
1809 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1814 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1815 video_device_node_name(&dev->vdev));
1817 video_unregister_device(&dev->vdev);
1819 if (dev->capture.encode_component) {
1820 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1821 "mmal_exit - disconnect tunnel\n");
1822 vchiq_mmal_port_connect_tunnel(dev->instance,
1823 dev->capture.camera_port, NULL);
1824 vchiq_mmal_component_disable(dev->instance,
1825 dev->capture.encode_component);
1827 vchiq_mmal_component_disable(dev->instance,
1828 dev->component[MMAL_COMPONENT_CAMERA]);
1830 vchiq_mmal_component_finalise(dev->instance,
1832 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1834 vchiq_mmal_component_finalise(dev->instance,
1836 component[MMAL_COMPONENT_IMAGE_ENCODE]);
1838 vchiq_mmal_component_finalise(dev->instance,
1839 dev->component[MMAL_COMPONENT_PREVIEW]);
1841 vchiq_mmal_component_finalise(dev->instance,
1842 dev->component[MMAL_COMPONENT_CAMERA]);
1844 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1846 v4l2_device_unregister(&dev->v4l2_dev);
1851 static struct v4l2_format default_v4l2_format = {
1852 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1853 .fmt.pix.width = 1024,
1854 .fmt.pix.bytesperline = 0,
1855 .fmt.pix.height = 768,
1856 .fmt.pix.sizeimage = 1024 * 768,
1859 static int bcm2835_mmal_probe(struct platform_device *pdev)
1862 struct bm2835_mmal_dev *dev;
1863 struct vb2_queue *q;
1865 unsigned int num_cameras;
1866 struct vchiq_mmal_instance *instance;
1867 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1870 ret = vchiq_mmal_init(&instance);
1874 num_cameras = get_num_cameras(instance,
1876 MAX_BCM2835_CAMERAS);
1878 if (num_cameras < 1) {
1883 if (num_cameras > MAX_BCM2835_CAMERAS)
1884 num_cameras = MAX_BCM2835_CAMERAS;
1886 for (camera = 0; camera < num_cameras; camera++) {
1887 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1893 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1894 mutex_init(&dev->mutex);
1895 dev->camera_num = camera;
1896 dev->max_width = resolutions[camera][0];
1897 dev->max_height = resolutions[camera][1];
1899 /* setup device defaults */
1900 dev->overlay.w.left = 150;
1901 dev->overlay.w.top = 50;
1902 dev->overlay.w.width = 1024;
1903 dev->overlay.w.height = 768;
1904 dev->overlay.clipcount = 0;
1905 dev->overlay.field = V4L2_FIELD_NONE;
1906 dev->overlay.global_alpha = 255;
1908 dev->capture.fmt = &formats[3]; /* JPEG */
1910 /* v4l device registration */
1911 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1912 "%s", BM2835_MMAL_MODULE_NAME);
1913 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1915 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1920 /* setup v4l controls */
1921 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1923 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1927 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1930 dev->instance = instance;
1931 ret = mmal_init(dev);
1933 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1937 /* initialize queue */
1938 q = &dev->capture.vb_vidq;
1939 memset(q, 0, sizeof(*q));
1940 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1941 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1943 q->buf_struct_size = sizeof(struct mmal_buffer);
1944 q->ops = &bm2835_mmal_video_qops;
1945 q->mem_ops = &vb2_vmalloc_memops;
1946 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1947 q->lock = &dev->mutex;
1948 ret = vb2_queue_init(q);
1952 /* initialise video devices */
1953 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1955 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1960 /* Really want to call vidioc_s_fmt_vid_cap with the default
1961 * format, but currently the APIs don't join up.
1963 ret = mmal_setup_components(dev, &default_v4l2_format);
1965 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1970 v4l2_info(&dev->v4l2_dev,
1971 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1972 BM2835_MMAL_VERSION);
1979 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1980 v4l2_device_unregister(&dev->v4l2_dev);
1986 for (i = 0; i < camera; i++) {
1987 bcm2835_cleanup_instance(gdev[i]);
1992 vchiq_mmal_finalise(instance);
1997 static int bcm2835_mmal_remove(struct platform_device *pdev)
2000 struct vchiq_mmal_instance *instance = gdev[0]->instance;
2002 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2003 bcm2835_cleanup_instance(gdev[camera]);
2004 gdev[camera] = NULL;
2006 vchiq_mmal_finalise(instance);
2011 static struct platform_driver bcm2835_camera_driver = {
2012 .probe = bcm2835_mmal_probe,
2013 .remove = bcm2835_mmal_remove,
2015 .name = "bcm2835-camera",
2019 module_platform_driver(bcm2835_camera_driver)
2021 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2022 MODULE_AUTHOR("Vincent Sanders");
2023 MODULE_LICENSE("GPL");
2024 MODULE_VERSION(BM2835_MMAL_VERSION);
2025 MODULE_ALIAS("platform:bcm2835-camera");