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);
343 /* this should only ever happen if the port is
344 * disabled and there are buffers still queued
346 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
347 pr_debug("Empty buffer");
348 } else if (dev->capture.frame_count) {
349 /* grab another frame */
350 if (is_capturing(dev)) {
351 pr_debug("Grab another frame");
352 vchiq_mmal_port_parameter_set(
354 dev->capture.camera_port,
355 MMAL_PARAMETER_CAPTURE,
356 &dev->capture.frame_count,
357 sizeof(dev->capture.frame_count));
360 /* signal frame completion */
361 complete(&dev->capture.frame_cmplt);
366 if (!dev->capture.frame_count) {
367 /* signal frame completion */
368 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
369 complete(&dev->capture.frame_cmplt);
373 if (dev->capture.vc_start_timestamp == -1) {
375 * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
376 * kernel time, and have no latency compensation.
378 buf->vb.vb2_buf.timestamp = ktime_get_ns();
379 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
380 "Buffer time set as current time - %lld",
381 buf->vb.vb2_buf.timestamp);
382 } else if (pts != 0) {
384 s64 runtime_us = pts -
385 dev->capture.vc_start_timestamp;
386 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
388 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
389 "Convert start time %llu and %llu with offset %llu to %llu\n",
390 ktime_to_ns(dev->capture.kernel_start_ts),
391 dev->capture.vc_start_timestamp, pts,
392 ktime_to_ns(timestamp));
393 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
395 if (dev->capture.last_timestamp) {
396 buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
397 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
398 "Buffer time set as last timestamp - %lld",
399 buf->vb.vb2_buf.timestamp);
401 buf->vb.vb2_buf.timestamp =
402 ktime_to_ns(dev->capture.kernel_start_ts);
403 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
404 "Buffer time set as start timestamp - %lld",
405 buf->vb.vb2_buf.timestamp);
408 dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
410 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
411 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
412 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
414 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
415 "Buffer has ts %llu",
416 dev->capture.last_timestamp);
417 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
419 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
421 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
422 "Grab another frame as buffer has EOS");
423 vchiq_mmal_port_parameter_set(instance,
424 dev->capture.camera_port,
425 MMAL_PARAMETER_CAPTURE,
426 &dev->capture.frame_count,
427 sizeof(dev->capture.frame_count));
431 static int enable_camera(struct bm2835_mmal_dev *dev)
435 if (!dev->camera_use_count) {
436 ret = vchiq_mmal_port_parameter_set(
438 &dev->component[MMAL_COMPONENT_CAMERA]->control,
439 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
440 sizeof(dev->camera_num));
442 v4l2_err(&dev->v4l2_dev,
443 "Failed setting camera num, ret %d\n", ret);
447 ret = vchiq_mmal_component_enable(
449 dev->component[MMAL_COMPONENT_CAMERA]);
451 v4l2_err(&dev->v4l2_dev,
452 "Failed enabling camera, ret %d\n", ret);
456 dev->camera_use_count++;
457 v4l2_dbg(1, bcm2835_v4l2_debug,
458 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
459 dev->camera_use_count);
463 static int disable_camera(struct bm2835_mmal_dev *dev)
467 if (!dev->camera_use_count) {
468 v4l2_err(&dev->v4l2_dev,
469 "Disabled the camera when already disabled\n");
472 dev->camera_use_count--;
473 if (!dev->camera_use_count) {
474 unsigned int i = 0xFFFFFFFF;
476 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
477 "Disabling camera\n");
479 vchiq_mmal_component_disable(
481 dev->component[MMAL_COMPONENT_CAMERA]);
483 v4l2_err(&dev->v4l2_dev,
484 "Failed disabling camera, ret %d\n", ret);
487 vchiq_mmal_port_parameter_set(
489 &dev->component[MMAL_COMPONENT_CAMERA]->control,
490 MMAL_PARAMETER_CAMERA_NUM, &i,
493 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
494 "Camera refcount now %d\n", dev->camera_use_count);
498 static void buffer_queue(struct vb2_buffer *vb)
500 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
501 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
502 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
505 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
506 "%s: dev:%p buf:%p, idx %u\n",
507 __func__, dev, buf, vb2->vb2_buf.index);
509 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
511 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
515 static int start_streaming(struct vb2_queue *vq, unsigned int count)
517 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
521 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
524 /* ensure a format has actually been set */
525 if (!dev->capture.port)
528 if (enable_camera(dev) < 0) {
529 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
533 /*init_completion(&dev->capture.frame_cmplt); */
535 /* enable frame capture */
536 dev->capture.frame_count = 1;
538 /* if the preview is not already running, wait for a few frames for AGC
541 if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
544 /* enable the connection from camera to encoder (if applicable) */
545 if (dev->capture.camera_port != dev->capture.port
546 && dev->capture.camera_port) {
547 ret = vchiq_mmal_port_enable(dev->instance,
548 dev->capture.camera_port, NULL);
550 v4l2_err(&dev->v4l2_dev,
551 "Failed to enable encode tunnel - error %d\n",
557 /* Get VC timestamp at this point in time */
558 parameter_size = sizeof(dev->capture.vc_start_timestamp);
559 if (vchiq_mmal_port_parameter_get(dev->instance,
560 dev->capture.camera_port,
561 MMAL_PARAMETER_SYSTEM_TIME,
562 &dev->capture.vc_start_timestamp,
564 v4l2_err(&dev->v4l2_dev,
565 "Failed to get VC start time - update your VC f/w\n");
567 /* Flag to indicate just to rely on kernel timestamps */
568 dev->capture.vc_start_timestamp = -1;
570 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
571 "Start time %lld size %d\n",
572 dev->capture.vc_start_timestamp, parameter_size);
574 dev->capture.kernel_start_ts = ktime_get();
575 dev->capture.last_timestamp = 0;
577 /* enable the camera port */
578 dev->capture.port->cb_ctx = dev;
580 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
582 v4l2_err(&dev->v4l2_dev,
583 "Failed to enable capture port - error %d. Disabling camera port again\n",
586 vchiq_mmal_port_disable(dev->instance,
587 dev->capture.camera_port);
588 if (disable_camera(dev) < 0) {
589 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
595 /* capture the first frame */
596 vchiq_mmal_port_parameter_set(dev->instance,
597 dev->capture.camera_port,
598 MMAL_PARAMETER_CAPTURE,
599 &dev->capture.frame_count,
600 sizeof(dev->capture.frame_count));
604 /* abort streaming and wait for last buffer */
605 static void stop_streaming(struct vb2_queue *vq)
608 unsigned long timeout;
609 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
611 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
614 init_completion(&dev->capture.frame_cmplt);
615 dev->capture.frame_count = 0;
617 /* ensure a format has actually been set */
618 if (!dev->capture.port) {
619 v4l2_err(&dev->v4l2_dev,
620 "no capture port - stream not started?\n");
624 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
626 /* stop capturing frames */
627 vchiq_mmal_port_parameter_set(dev->instance,
628 dev->capture.camera_port,
629 MMAL_PARAMETER_CAPTURE,
630 &dev->capture.frame_count,
631 sizeof(dev->capture.frame_count));
633 /* wait for last frame to complete */
634 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
636 v4l2_err(&dev->v4l2_dev,
637 "timed out waiting for frame completion\n");
639 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
640 "disabling connection\n");
642 /* disable the connection from camera to encoder */
643 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
644 if (!ret && dev->capture.camera_port != dev->capture.port) {
645 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
647 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
648 } else if (dev->capture.camera_port != dev->capture.port) {
649 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
653 if (disable_camera(dev) < 0)
654 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
657 static const struct vb2_ops bm2835_mmal_video_qops = {
658 .queue_setup = queue_setup,
659 .buf_init = buffer_init,
660 .buf_prepare = buffer_prepare,
661 .buf_cleanup = buffer_cleanup,
662 .buf_queue = buffer_queue,
663 .start_streaming = start_streaming,
664 .stop_streaming = stop_streaming,
665 .wait_prepare = vb2_ops_wait_prepare,
666 .wait_finish = vb2_ops_wait_finish,
669 /* ------------------------------------------------------------------
671 * ------------------------------------------------------------------
674 static int set_overlay_params(struct bm2835_mmal_dev *dev,
675 struct vchiq_mmal_port *port)
677 struct mmal_parameter_displayregion prev_config = {
678 .set = MMAL_DISPLAY_SET_LAYER |
679 MMAL_DISPLAY_SET_ALPHA |
680 MMAL_DISPLAY_SET_DEST_RECT |
681 MMAL_DISPLAY_SET_FULLSCREEN,
682 .layer = PREVIEW_LAYER,
683 .alpha = dev->overlay.global_alpha,
686 .x = dev->overlay.w.left,
687 .y = dev->overlay.w.top,
688 .width = dev->overlay.w.width,
689 .height = dev->overlay.w.height,
692 return vchiq_mmal_port_parameter_set(dev->instance, port,
693 MMAL_PARAMETER_DISPLAYREGION,
694 &prev_config, sizeof(prev_config));
698 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
699 struct v4l2_fmtdesc *f)
701 struct mmal_fmt *fmt;
703 if (f->index >= ARRAY_SIZE(formats))
706 fmt = &formats[f->index];
708 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
709 f->pixelformat = fmt->fourcc;
710 f->flags = fmt->flags;
715 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
716 struct v4l2_format *f)
718 struct bm2835_mmal_dev *dev = video_drvdata(file);
720 f->fmt.win = dev->overlay;
725 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
726 struct v4l2_format *f)
728 struct bm2835_mmal_dev *dev = video_drvdata(file);
730 f->fmt.win.field = V4L2_FIELD_NONE;
731 f->fmt.win.chromakey = 0;
732 f->fmt.win.clips = NULL;
733 f->fmt.win.clipcount = 0;
734 f->fmt.win.bitmap = NULL;
736 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
737 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
739 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
740 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
743 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
744 "Overlay: Now w/h %dx%d l/t %dx%d\n",
745 f->fmt.win.w.width, f->fmt.win.w.height,
746 f->fmt.win.w.left, f->fmt.win.w.top);
748 v4l2_dump_win_format(1,
756 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
757 struct v4l2_format *f)
759 struct bm2835_mmal_dev *dev = video_drvdata(file);
761 vidioc_try_fmt_vid_overlay(file, priv, f);
763 dev->overlay = f->fmt.win;
764 if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
765 set_overlay_params(dev,
766 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
772 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
775 struct bm2835_mmal_dev *dev = video_drvdata(file);
776 struct vchiq_mmal_port *src;
777 struct vchiq_mmal_port *dst;
779 if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
780 (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
781 return 0; /* already in requested state */
784 &dev->component[MMAL_COMPONENT_CAMERA]->
785 output[MMAL_CAMERA_PORT_PREVIEW];
788 /* disconnect preview ports and disable component */
789 ret = vchiq_mmal_port_disable(dev->instance, src);
792 vchiq_mmal_port_connect_tunnel(dev->instance, src,
795 ret = vchiq_mmal_component_disable(
797 dev->component[MMAL_COMPONENT_PREVIEW]);
803 /* set preview port format and connect it to output */
804 dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
806 ret = vchiq_mmal_port_set_format(dev->instance, src);
810 ret = set_overlay_params(dev, dst);
814 if (enable_camera(dev) < 0) {
819 ret = vchiq_mmal_component_enable(
821 dev->component[MMAL_COMPONENT_PREVIEW]);
825 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
827 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
829 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
834 static int vidioc_g_fbuf(struct file *file, void *fh,
835 struct v4l2_framebuffer *a)
837 /* The video overlay must stay within the framebuffer and can't be
838 * positioned independently.
840 struct bm2835_mmal_dev *dev = video_drvdata(file);
841 struct vchiq_mmal_port *preview_port =
842 &dev->component[MMAL_COMPONENT_CAMERA]->
843 output[MMAL_CAMERA_PORT_PREVIEW];
845 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
846 V4L2_FBUF_CAP_GLOBAL_ALPHA;
847 a->flags = V4L2_FBUF_FLAG_OVERLAY;
848 a->fmt.width = preview_port->es.video.width;
849 a->fmt.height = preview_port->es.video.height;
850 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
851 a->fmt.bytesperline = preview_port->es.video.width;
852 a->fmt.sizeimage = (preview_port->es.video.width *
853 preview_port->es.video.height * 3) >> 1;
854 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
860 static int vidioc_enum_input(struct file *file, void *priv,
861 struct v4l2_input *inp)
863 /* only a single camera input */
867 inp->type = V4L2_INPUT_TYPE_CAMERA;
868 sprintf((char *)inp->name, "Camera %u", inp->index);
872 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
878 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
887 static int vidioc_querycap(struct file *file, void *priv,
888 struct v4l2_capability *cap)
890 struct bm2835_mmal_dev *dev = video_drvdata(file);
894 vchiq_mmal_version(dev->instance, &major, &minor);
896 strcpy((char *)cap->driver, "bm2835 mmal");
897 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
900 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
901 "platform:%s", dev->v4l2_dev.name);
902 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
903 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
904 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
909 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
910 struct v4l2_fmtdesc *f)
912 struct mmal_fmt *fmt;
914 if (f->index >= ARRAY_SIZE(formats))
917 fmt = &formats[f->index];
919 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
920 f->pixelformat = fmt->fourcc;
921 f->flags = fmt->flags;
926 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
927 struct v4l2_format *f)
929 struct bm2835_mmal_dev *dev = video_drvdata(file);
931 f->fmt.pix.width = dev->capture.width;
932 f->fmt.pix.height = dev->capture.height;
933 f->fmt.pix.field = V4L2_FIELD_NONE;
934 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
935 f->fmt.pix.bytesperline = dev->capture.stride;
936 f->fmt.pix.sizeimage = dev->capture.buffersize;
938 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
939 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
940 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
941 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
943 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
946 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
951 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
952 struct v4l2_format *f)
954 struct bm2835_mmal_dev *dev = video_drvdata(file);
955 struct mmal_fmt *mfmt;
957 mfmt = get_format(f);
959 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
960 "Fourcc format (0x%08x) unknown.\n",
961 f->fmt.pix.pixelformat);
962 f->fmt.pix.pixelformat = formats[0].fourcc;
963 mfmt = get_format(f);
966 f->fmt.pix.field = V4L2_FIELD_NONE;
968 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
969 "Clipping/aligning %dx%d format %08X\n",
970 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
972 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
973 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
975 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
976 if (!mfmt->remove_padding) {
977 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
978 /* GPU isn't removing padding, so stride is aligned to 32 */
979 f->fmt.pix.bytesperline =
980 (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
981 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
982 "Not removing padding, so bytes/line = %d, "
984 f->fmt.pix.bytesperline, align_mask);
987 /* Image buffer has to be padded to allow for alignment, even though
988 * we sometimes then remove that padding before delivering the buffer.
990 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
991 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
993 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
994 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
995 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
997 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
998 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
999 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
1000 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1002 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1003 f->fmt.pix.priv = 0;
1005 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1006 "Now %dx%d format %08X\n",
1007 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1009 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1014 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1015 struct v4l2_format *f)
1018 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1019 struct vchiq_mmal_component *encode_component = NULL;
1020 struct mmal_fmt *mfmt = get_format(f);
1026 if (dev->capture.encode_component) {
1027 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1028 "vid_cap - disconnect previous tunnel\n");
1030 /* Disconnect any previous connection */
1031 vchiq_mmal_port_connect_tunnel(dev->instance,
1032 dev->capture.camera_port, NULL);
1033 dev->capture.camera_port = NULL;
1034 ret = vchiq_mmal_component_disable(dev->instance,
1038 v4l2_err(&dev->v4l2_dev,
1039 "Failed to disable encode component %d\n",
1042 dev->capture.encode_component = NULL;
1044 /* format dependent port setup */
1045 switch (mfmt->mmal_component) {
1046 case MMAL_COMPONENT_CAMERA:
1047 /* Make a further decision on port based on resolution */
1048 if (f->fmt.pix.width <= max_video_width
1049 && f->fmt.pix.height <= max_video_height)
1050 camera_port = port =
1051 &dev->component[MMAL_COMPONENT_CAMERA]->
1052 output[MMAL_CAMERA_PORT_VIDEO];
1054 camera_port = port =
1055 &dev->component[MMAL_COMPONENT_CAMERA]->
1056 output[MMAL_CAMERA_PORT_CAPTURE];
1058 case MMAL_COMPONENT_IMAGE_ENCODE:
1059 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1060 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1062 &dev->component[MMAL_COMPONENT_CAMERA]->
1063 output[MMAL_CAMERA_PORT_CAPTURE];
1065 case MMAL_COMPONENT_VIDEO_ENCODE:
1066 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1067 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1069 &dev->component[MMAL_COMPONENT_CAMERA]->
1070 output[MMAL_CAMERA_PORT_VIDEO];
1079 if (encode_component)
1080 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1082 camera_port->format.encoding = mfmt->mmal;
1084 if (dev->rgb_bgr_swapped) {
1085 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1086 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1087 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1088 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1091 remove_padding = mfmt->remove_padding;
1092 vchiq_mmal_port_parameter_set(dev->instance,
1094 MMAL_PARAMETER_NO_IMAGE_PADDING,
1095 &remove_padding, sizeof(remove_padding));
1097 camera_port->format.encoding_variant = 0;
1098 camera_port->es.video.width = f->fmt.pix.width;
1099 camera_port->es.video.height = f->fmt.pix.height;
1100 camera_port->es.video.crop.x = 0;
1101 camera_port->es.video.crop.y = 0;
1102 camera_port->es.video.crop.width = f->fmt.pix.width;
1103 camera_port->es.video.crop.height = f->fmt.pix.height;
1104 camera_port->es.video.frame_rate.num = 0;
1105 camera_port->es.video.frame_rate.den = 1;
1106 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1108 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1112 &dev->component[MMAL_COMPONENT_CAMERA]->
1113 output[MMAL_CAMERA_PORT_VIDEO]) {
1114 bool overlay_enabled =
1115 !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1116 struct vchiq_mmal_port *preview_port =
1117 &dev->component[MMAL_COMPONENT_CAMERA]->
1118 output[MMAL_CAMERA_PORT_PREVIEW];
1119 /* Preview and encode ports need to match on resolution */
1120 if (overlay_enabled) {
1121 /* Need to disable the overlay before we can update
1125 vchiq_mmal_port_disable(dev->instance,
1129 vchiq_mmal_port_connect_tunnel(
1134 preview_port->es.video.width = f->fmt.pix.width;
1135 preview_port->es.video.height = f->fmt.pix.height;
1136 preview_port->es.video.crop.x = 0;
1137 preview_port->es.video.crop.y = 0;
1138 preview_port->es.video.crop.width = f->fmt.pix.width;
1139 preview_port->es.video.crop.height = f->fmt.pix.height;
1140 preview_port->es.video.frame_rate.num =
1141 dev->capture.timeperframe.denominator;
1142 preview_port->es.video.frame_rate.den =
1143 dev->capture.timeperframe.numerator;
1144 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1145 if (overlay_enabled) {
1146 ret = vchiq_mmal_port_connect_tunnel(
1149 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1151 ret = vchiq_mmal_port_enable(dev->instance,
1158 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1159 "%s failed to set format %dx%d %08X\n", __func__,
1160 f->fmt.pix.width, f->fmt.pix.height,
1161 f->fmt.pix.pixelformat);
1162 /* ensure capture is not going to be tried */
1163 dev->capture.port = NULL;
1165 if (encode_component) {
1166 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1167 "vid_cap - set up encode comp\n");
1169 /* configure buffering */
1170 camera_port->current_buffer.size =
1171 camera_port->recommended_buffer.size;
1172 camera_port->current_buffer.num =
1173 camera_port->recommended_buffer.num;
1176 vchiq_mmal_port_connect_tunnel(
1179 &encode_component->input[0]);
1181 v4l2_dbg(1, bcm2835_v4l2_debug,
1183 "%s failed to create connection\n",
1185 /* ensure capture is not going to be tried */
1186 dev->capture.port = NULL;
1188 port->es.video.width = f->fmt.pix.width;
1189 port->es.video.height = f->fmt.pix.height;
1190 port->es.video.crop.x = 0;
1191 port->es.video.crop.y = 0;
1192 port->es.video.crop.width = f->fmt.pix.width;
1193 port->es.video.crop.height = f->fmt.pix.height;
1194 port->es.video.frame_rate.num =
1195 dev->capture.timeperframe.denominator;
1196 port->es.video.frame_rate.den =
1197 dev->capture.timeperframe.numerator;
1199 port->format.encoding = mfmt->mmal;
1200 port->format.encoding_variant = 0;
1201 /* Set any encoding specific parameters */
1202 switch (mfmt->mmal_component) {
1203 case MMAL_COMPONENT_VIDEO_ENCODE:
1204 port->format.bitrate =
1205 dev->capture.encode_bitrate;
1207 case MMAL_COMPONENT_IMAGE_ENCODE:
1208 /* Could set EXIF parameters here */
1213 ret = vchiq_mmal_port_set_format(dev->instance,
1216 v4l2_dbg(1, bcm2835_v4l2_debug,
1218 "%s failed to set format %dx%d fmt %08X\n",
1222 f->fmt.pix.pixelformat
1227 ret = vchiq_mmal_component_enable(
1231 v4l2_dbg(1, bcm2835_v4l2_debug,
1233 "%s Failed to enable encode components\n",
1238 /* configure buffering */
1239 port->current_buffer.num = 1;
1240 port->current_buffer.size =
1241 f->fmt.pix.sizeimage;
1242 if (port->format.encoding ==
1243 MMAL_ENCODING_JPEG) {
1244 v4l2_dbg(1, bcm2835_v4l2_debug,
1246 "JPG - buf size now %d was %d\n",
1247 f->fmt.pix.sizeimage,
1248 port->current_buffer.size);
1249 port->current_buffer.size =
1250 (f->fmt.pix.sizeimage <
1253 : f->fmt.pix.sizeimage;
1255 v4l2_dbg(1, bcm2835_v4l2_debug,
1257 "vid_cap - cur_buf.size set to %d\n",
1258 f->fmt.pix.sizeimage);
1259 port->current_buffer.alignment = 0;
1262 /* configure buffering */
1263 camera_port->current_buffer.num = 1;
1264 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1265 camera_port->current_buffer.alignment = 0;
1269 dev->capture.fmt = mfmt;
1270 dev->capture.stride = f->fmt.pix.bytesperline;
1271 dev->capture.width = camera_port->es.video.crop.width;
1272 dev->capture.height = camera_port->es.video.crop.height;
1273 dev->capture.buffersize = port->current_buffer.size;
1275 /* select port for capture */
1276 dev->capture.port = port;
1277 dev->capture.camera_port = camera_port;
1278 dev->capture.encode_component = encode_component;
1279 v4l2_dbg(1, bcm2835_v4l2_debug,
1281 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1282 port->format.encoding,
1283 dev->capture.width, dev->capture.height,
1284 dev->capture.stride, dev->capture.buffersize);
1288 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1292 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1293 struct v4l2_format *f)
1296 struct bm2835_mmal_dev *dev = video_drvdata(file);
1297 struct mmal_fmt *mfmt;
1299 /* try the format to set valid parameters */
1300 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1302 v4l2_err(&dev->v4l2_dev,
1303 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1307 /* if a capture is running refuse to set format */
1308 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1309 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1313 /* If the format is unsupported v4l2 says we should switch to
1314 * a supported one and not return an error.
1316 mfmt = get_format(f);
1318 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1319 "Fourcc format (0x%08x) unknown.\n",
1320 f->fmt.pix.pixelformat);
1321 f->fmt.pix.pixelformat = formats[0].fourcc;
1322 mfmt = get_format(f);
1325 ret = mmal_setup_components(dev, f);
1327 v4l2_err(&dev->v4l2_dev,
1328 "%s: failed to setup mmal components: %d\n",
1336 static int vidioc_enum_framesizes(struct file *file, void *fh,
1337 struct v4l2_frmsizeenum *fsize)
1339 struct bm2835_mmal_dev *dev = video_drvdata(file);
1340 static const struct v4l2_frmsize_stepwise sizes = {
1348 for (i = 0; i < ARRAY_SIZE(formats); i++)
1349 if (formats[i].fourcc == fsize->pixel_format)
1351 if (i == ARRAY_SIZE(formats))
1353 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1354 fsize->stepwise = sizes;
1355 fsize->stepwise.max_width = dev->max_width;
1356 fsize->stepwise.max_height = dev->max_height;
1360 /* timeperframe is arbitrary and continuous */
1361 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1362 struct v4l2_frmivalenum *fival)
1364 struct bm2835_mmal_dev *dev = video_drvdata(file);
1370 for (i = 0; i < ARRAY_SIZE(formats); i++)
1371 if (formats[i].fourcc == fival->pixel_format)
1373 if (i == ARRAY_SIZE(formats))
1376 /* regarding width & height - we support any within range */
1377 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1378 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1381 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1383 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1384 fival->stepwise.min = tpf_min;
1385 fival->stepwise.max = tpf_max;
1386 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1391 static int vidioc_g_parm(struct file *file, void *priv,
1392 struct v4l2_streamparm *parm)
1394 struct bm2835_mmal_dev *dev = video_drvdata(file);
1396 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1399 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1400 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1401 parm->parm.capture.readbuffers = 1;
1405 #define FRACT_CMP(a, OP, b) \
1406 ((u64)(a).numerator * (b).denominator OP \
1407 (u64)(b).numerator * (a).denominator)
1409 static int vidioc_s_parm(struct file *file, void *priv,
1410 struct v4l2_streamparm *parm)
1412 struct bm2835_mmal_dev *dev = video_drvdata(file);
1413 struct v4l2_fract tpf;
1415 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1418 tpf = parm->parm.capture.timeperframe;
1420 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1421 tpf = tpf.denominator ? tpf : tpf_default;
1422 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1423 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1425 dev->capture.timeperframe = tpf;
1426 parm->parm.capture.timeperframe = tpf;
1427 parm->parm.capture.readbuffers = 1;
1428 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1430 set_framerate_params(dev);
1435 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1437 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1438 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1439 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1440 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1441 .vidioc_overlay = vidioc_overlay,
1442 .vidioc_g_fbuf = vidioc_g_fbuf,
1445 .vidioc_enum_input = vidioc_enum_input,
1446 .vidioc_g_input = vidioc_g_input,
1447 .vidioc_s_input = vidioc_s_input,
1450 .vidioc_querycap = vidioc_querycap,
1451 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1452 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1453 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1454 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1456 /* buffer management */
1457 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1458 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1459 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1460 .vidioc_querybuf = vb2_ioctl_querybuf,
1461 .vidioc_qbuf = vb2_ioctl_qbuf,
1462 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1463 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1464 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1465 .vidioc_g_parm = vidioc_g_parm,
1466 .vidioc_s_parm = vidioc_s_parm,
1467 .vidioc_streamon = vb2_ioctl_streamon,
1468 .vidioc_streamoff = vb2_ioctl_streamoff,
1470 .vidioc_log_status = v4l2_ctrl_log_status,
1471 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1472 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1475 /* ------------------------------------------------------------------
1476 * Driver init/finalise
1477 * ------------------------------------------------------------------
1480 static const struct v4l2_file_operations camera0_fops = {
1481 .owner = THIS_MODULE,
1482 .open = v4l2_fh_open,
1483 .release = vb2_fop_release,
1484 .read = vb2_fop_read,
1485 .poll = vb2_fop_poll,
1486 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1487 .mmap = vb2_fop_mmap,
1490 static const struct video_device vdev_template = {
1492 .fops = &camera0_fops,
1493 .ioctl_ops = &camera0_ioctl_ops,
1494 .release = video_device_release_empty,
1497 /* Returns the number of cameras, and also the max resolution supported
1500 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1501 unsigned int resolutions[][2], int num_resolutions)
1504 struct vchiq_mmal_component *cam_info_component;
1505 struct mmal_parameter_camera_info_t cam_info = {0};
1506 u32 param_size = sizeof(cam_info);
1509 /* create a camera_info component */
1510 ret = vchiq_mmal_component_init(instance, "camera_info",
1511 &cam_info_component);
1513 /* Unusual failure - let's guess one camera. */
1516 if (vchiq_mmal_port_parameter_get(instance,
1517 &cam_info_component->control,
1518 MMAL_PARAMETER_CAMERA_INFO,
1521 pr_info("Failed to get camera info\n");
1524 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1526 resolutions[i][0] = cam_info.cameras[i].max_width;
1527 resolutions[i][1] = cam_info.cameras[i].max_height;
1530 vchiq_mmal_component_finalise(instance,
1531 cam_info_component);
1533 return cam_info.num_cameras;
1536 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1537 struct vchiq_mmal_component *camera,
1538 struct bm2835_mmal_dev *dev)
1541 struct mmal_parameter_camera_config cam_config = {
1542 .max_stills_w = dev->max_width,
1543 .max_stills_h = dev->max_height,
1545 .one_shot_stills = 1,
1546 .max_preview_video_w = (max_video_width > 1920) ?
1547 max_video_width : 1920,
1548 .max_preview_video_h = (max_video_height > 1088) ?
1549 max_video_height : 1088,
1550 .num_preview_video_frames = 3,
1551 .stills_capture_circular_buffer_height = 0,
1552 .fast_preview_resume = 0,
1553 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1556 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1557 MMAL_PARAMETER_CAMERA_CONFIG,
1558 &cam_config, sizeof(cam_config));
1562 #define MAX_SUPPORTED_ENCODINGS 20
1564 /* MMAL instance and component init */
1565 static int mmal_init(struct bm2835_mmal_dev *dev)
1568 struct mmal_es_format_local *format;
1569 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1571 struct vchiq_mmal_component *camera;
1573 ret = vchiq_mmal_init(&dev->instance);
1575 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1580 /* get the camera component ready */
1581 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1582 &dev->component[MMAL_COMPONENT_CAMERA]);
1586 camera = dev->component[MMAL_COMPONENT_CAMERA];
1587 if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1588 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1589 __func__, camera->outputs, MMAL_CAMERA_PORT_COUNT);
1594 ret = set_camera_parameters(dev->instance,
1598 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1603 /* There was an error in the firmware that meant the camera component
1604 * produced BGR instead of RGB.
1605 * This is now fixed, but in order to support the old firmwares, we
1608 dev->rgb_bgr_swapped = true;
1609 param_size = sizeof(supported_encodings);
1610 ret = vchiq_mmal_port_parameter_get(dev->instance,
1611 &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1612 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1613 &supported_encodings,
1618 for (i = 0; i < param_size / sizeof(u32); i++) {
1619 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1620 /* Found BGR24 first - old firmware. */
1623 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1624 /* Found RGB24 first
1625 * new firmware, so use RGB24.
1627 dev->rgb_bgr_swapped = false;
1632 format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1634 format->encoding = MMAL_ENCODING_OPAQUE;
1635 format->encoding_variant = MMAL_ENCODING_I420;
1637 format->es->video.width = 1024;
1638 format->es->video.height = 768;
1639 format->es->video.crop.x = 0;
1640 format->es->video.crop.y = 0;
1641 format->es->video.crop.width = 1024;
1642 format->es->video.crop.height = 768;
1643 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1644 format->es->video.frame_rate.den = 1;
1646 format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1648 format->encoding = MMAL_ENCODING_OPAQUE;
1649 format->encoding_variant = MMAL_ENCODING_I420;
1651 format->es->video.width = 1024;
1652 format->es->video.height = 768;
1653 format->es->video.crop.x = 0;
1654 format->es->video.crop.y = 0;
1655 format->es->video.crop.width = 1024;
1656 format->es->video.crop.height = 768;
1657 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1658 format->es->video.frame_rate.den = 1;
1660 format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1662 format->encoding = MMAL_ENCODING_OPAQUE;
1664 format->es->video.width = 2592;
1665 format->es->video.height = 1944;
1666 format->es->video.crop.x = 0;
1667 format->es->video.crop.y = 0;
1668 format->es->video.crop.width = 2592;
1669 format->es->video.crop.height = 1944;
1670 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1671 format->es->video.frame_rate.den = 1;
1673 dev->capture.width = format->es->video.width;
1674 dev->capture.height = format->es->video.height;
1675 dev->capture.fmt = &formats[0];
1676 dev->capture.encode_component = NULL;
1677 dev->capture.timeperframe = tpf_default;
1678 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1679 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1681 /* get the preview component ready */
1682 ret = vchiq_mmal_component_init(
1683 dev->instance, "ril.video_render",
1684 &dev->component[MMAL_COMPONENT_PREVIEW]);
1688 if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1690 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1691 __func__, dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1695 /* get the image encoder component ready */
1696 ret = vchiq_mmal_component_init(
1697 dev->instance, "ril.image_encode",
1698 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1702 if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1704 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1705 __func__, dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1707 goto unreg_image_encoder;
1710 /* get the video encoder component ready */
1711 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1713 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1715 goto unreg_image_encoder;
1717 if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1719 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1720 __func__, dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1722 goto unreg_vid_encoder;
1726 struct vchiq_mmal_port *encoder_port =
1727 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1728 encoder_port->format.encoding = MMAL_ENCODING_H264;
1729 ret = vchiq_mmal_port_set_format(dev->instance,
1734 unsigned int enable = 1;
1736 vchiq_mmal_port_parameter_set(
1738 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1739 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1740 &enable, sizeof(enable));
1742 vchiq_mmal_port_parameter_set(dev->instance,
1743 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1744 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1748 ret = bm2835_mmal_set_all_camera_controls(dev);
1750 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1752 goto unreg_vid_encoder;
1758 pr_err("Cleanup: Destroy video encoder\n");
1759 vchiq_mmal_component_finalise(
1761 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1763 unreg_image_encoder:
1764 pr_err("Cleanup: Destroy image encoder\n");
1765 vchiq_mmal_component_finalise(
1767 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1770 pr_err("Cleanup: Destroy video render\n");
1771 vchiq_mmal_component_finalise(dev->instance,
1772 dev->component[MMAL_COMPONENT_PREVIEW]);
1775 pr_err("Cleanup: Destroy camera\n");
1776 vchiq_mmal_component_finalise(dev->instance,
1777 dev->component[MMAL_COMPONENT_CAMERA]);
1780 vchiq_mmal_finalise(dev->instance);
1784 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1785 struct video_device *vfd)
1789 *vfd = vdev_template;
1791 vfd->v4l2_dev = &dev->v4l2_dev;
1793 vfd->lock = &dev->mutex;
1795 vfd->queue = &dev->capture.vb_vidq;
1797 /* video device needs to be able to access instance data */
1798 video_set_drvdata(vfd, dev);
1800 ret = video_register_device(vfd,
1802 video_nr[dev->camera_num]);
1806 v4l2_info(vfd->v4l2_dev,
1807 "V4L2 device registered as %s - stills mode > %dx%d\n",
1808 video_device_node_name(vfd),
1809 max_video_width, max_video_height);
1814 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1819 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1820 video_device_node_name(&dev->vdev));
1822 video_unregister_device(&dev->vdev);
1824 if (dev->capture.encode_component) {
1825 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1826 "mmal_exit - disconnect tunnel\n");
1827 vchiq_mmal_port_connect_tunnel(dev->instance,
1828 dev->capture.camera_port, NULL);
1829 vchiq_mmal_component_disable(dev->instance,
1830 dev->capture.encode_component);
1832 vchiq_mmal_component_disable(dev->instance,
1833 dev->component[MMAL_COMPONENT_CAMERA]);
1835 vchiq_mmal_component_finalise(dev->instance,
1837 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1839 vchiq_mmal_component_finalise(dev->instance,
1841 component[MMAL_COMPONENT_IMAGE_ENCODE]);
1843 vchiq_mmal_component_finalise(dev->instance,
1844 dev->component[MMAL_COMPONENT_PREVIEW]);
1846 vchiq_mmal_component_finalise(dev->instance,
1847 dev->component[MMAL_COMPONENT_CAMERA]);
1849 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1851 v4l2_device_unregister(&dev->v4l2_dev);
1856 static struct v4l2_format default_v4l2_format = {
1857 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1858 .fmt.pix.width = 1024,
1859 .fmt.pix.bytesperline = 0,
1860 .fmt.pix.height = 768,
1861 .fmt.pix.sizeimage = 1024 * 768,
1864 static int bcm2835_mmal_probe(struct platform_device *pdev)
1867 struct bm2835_mmal_dev *dev;
1868 struct vb2_queue *q;
1870 unsigned int num_cameras;
1871 struct vchiq_mmal_instance *instance;
1872 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1875 ret = vchiq_mmal_init(&instance);
1879 num_cameras = get_num_cameras(instance,
1881 MAX_BCM2835_CAMERAS);
1883 if (num_cameras < 1) {
1888 if (num_cameras > MAX_BCM2835_CAMERAS)
1889 num_cameras = MAX_BCM2835_CAMERAS;
1891 for (camera = 0; camera < num_cameras; camera++) {
1892 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1898 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1899 mutex_init(&dev->mutex);
1900 dev->camera_num = camera;
1901 dev->max_width = resolutions[camera][0];
1902 dev->max_height = resolutions[camera][1];
1904 /* setup device defaults */
1905 dev->overlay.w.left = 150;
1906 dev->overlay.w.top = 50;
1907 dev->overlay.w.width = 1024;
1908 dev->overlay.w.height = 768;
1909 dev->overlay.clipcount = 0;
1910 dev->overlay.field = V4L2_FIELD_NONE;
1911 dev->overlay.global_alpha = 255;
1913 dev->capture.fmt = &formats[3]; /* JPEG */
1915 /* v4l device registration */
1916 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1917 "%s", BM2835_MMAL_MODULE_NAME);
1918 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1920 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1925 /* setup v4l controls */
1926 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1928 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1932 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1935 dev->instance = instance;
1936 ret = mmal_init(dev);
1938 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1942 /* initialize queue */
1943 q = &dev->capture.vb_vidq;
1944 memset(q, 0, sizeof(*q));
1945 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1946 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1948 q->buf_struct_size = sizeof(struct mmal_buffer);
1949 q->ops = &bm2835_mmal_video_qops;
1950 q->mem_ops = &vb2_vmalloc_memops;
1951 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1952 q->lock = &dev->mutex;
1953 ret = vb2_queue_init(q);
1957 /* initialise video devices */
1958 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1960 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1965 /* Really want to call vidioc_s_fmt_vid_cap with the default
1966 * format, but currently the APIs don't join up.
1968 ret = mmal_setup_components(dev, &default_v4l2_format);
1970 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1975 v4l2_info(&dev->v4l2_dev,
1976 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1977 BM2835_MMAL_VERSION);
1984 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1985 v4l2_device_unregister(&dev->v4l2_dev);
1991 for (i = 0; i < camera; i++) {
1992 bcm2835_cleanup_instance(gdev[i]);
1997 vchiq_mmal_finalise(instance);
2002 static int bcm2835_mmal_remove(struct platform_device *pdev)
2005 struct vchiq_mmal_instance *instance = gdev[0]->instance;
2007 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2008 bcm2835_cleanup_instance(gdev[camera]);
2009 gdev[camera] = NULL;
2011 vchiq_mmal_finalise(instance);
2016 static struct platform_driver bcm2835_camera_driver = {
2017 .probe = bcm2835_mmal_probe,
2018 .remove = bcm2835_mmal_remove,
2020 .name = "bcm2835-camera",
2024 module_platform_driver(bcm2835_camera_driver)
2026 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2027 MODULE_AUTHOR("Vincent Sanders");
2028 MODULE_LICENSE("GPL");
2029 MODULE_VERSION(BM2835_MMAL_VERSION);
2030 MODULE_ALIAS("platform:bcm2835-camera");