1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders @ Collabora
8 * Dave Stevenson @ Broadcom
9 * (now dave.stevenson@raspberrypi.org)
10 * Simon Mellor @ Broadcom
11 * Luke Diamand @ Broadcom
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <media/videobuf2-vmalloc.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-common.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #include "mmal-common.h"
30 #include "mmal-encodings.h"
31 #include "mmal-vchiq.h"
33 #include "mmal-parameters.h"
34 #include "bcm2835-camera.h"
36 #define BM2835_MMAL_VERSION "0.0.2"
37 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
40 #define MIN_BUFFER_SIZE (80 * 1024)
42 #define MAX_VIDEO_MODE_WIDTH 1280
43 #define MAX_VIDEO_MODE_HEIGHT 720
45 #define MAX_BCM2835_CAMERAS 2
47 int bcm2835_v4l2_debug;
48 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
49 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
52 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
53 module_param_array(video_nr, int, NULL, 0644);
54 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
56 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
57 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
58 module_param(max_video_width, int, 0644);
59 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
60 module_param(max_video_height, int, 0644);
61 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
63 /* camera instance counter */
64 static atomic_t camera_instance = ATOMIC_INIT(0);
66 /* global device data array */
67 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
72 /* timeperframe: min/max and default */
73 static const struct v4l2_fract
74 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
75 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
76 tpf_default = {.numerator = 1000, .denominator = 30000};
78 /* Container for MMAL and VB2 buffers*/
79 struct vb2_mmal_buffer {
80 struct vb2_v4l2_buffer vb;
81 struct mmal_buffer mmal;
85 static struct mmal_fmt formats[] = {
87 .fourcc = V4L2_PIX_FMT_YUV420,
88 .mmal = MMAL_ENCODING_I420,
90 .mmal_component = COMP_CAMERA,
94 .fourcc = V4L2_PIX_FMT_YUYV,
95 .mmal = MMAL_ENCODING_YUYV,
97 .mmal_component = COMP_CAMERA,
101 .fourcc = V4L2_PIX_FMT_RGB24,
102 .mmal = MMAL_ENCODING_RGB24,
104 .mmal_component = COMP_CAMERA,
108 .fourcc = V4L2_PIX_FMT_JPEG,
109 .flags = V4L2_FMT_FLAG_COMPRESSED,
110 .mmal = MMAL_ENCODING_JPEG,
112 .mmal_component = COMP_IMAGE_ENCODE,
116 .fourcc = V4L2_PIX_FMT_H264,
117 .flags = V4L2_FMT_FLAG_COMPRESSED,
118 .mmal = MMAL_ENCODING_H264,
120 .mmal_component = COMP_VIDEO_ENCODE,
124 .fourcc = V4L2_PIX_FMT_MJPEG,
125 .flags = V4L2_FMT_FLAG_COMPRESSED,
126 .mmal = MMAL_ENCODING_MJPEG,
128 .mmal_component = COMP_VIDEO_ENCODE,
132 .fourcc = V4L2_PIX_FMT_YVYU,
133 .mmal = MMAL_ENCODING_YVYU,
135 .mmal_component = COMP_CAMERA,
139 .fourcc = V4L2_PIX_FMT_VYUY,
140 .mmal = MMAL_ENCODING_VYUY,
142 .mmal_component = COMP_CAMERA,
146 .fourcc = V4L2_PIX_FMT_UYVY,
147 .mmal = MMAL_ENCODING_UYVY,
149 .mmal_component = COMP_CAMERA,
153 .fourcc = V4L2_PIX_FMT_NV12,
154 .mmal = MMAL_ENCODING_NV12,
156 .mmal_component = COMP_CAMERA,
160 .fourcc = V4L2_PIX_FMT_BGR24,
161 .mmal = MMAL_ENCODING_BGR24,
163 .mmal_component = COMP_CAMERA,
167 .fourcc = V4L2_PIX_FMT_YVU420,
168 .mmal = MMAL_ENCODING_YV12,
170 .mmal_component = COMP_CAMERA,
174 .fourcc = V4L2_PIX_FMT_NV21,
175 .mmal = MMAL_ENCODING_NV21,
177 .mmal_component = COMP_CAMERA,
181 .fourcc = V4L2_PIX_FMT_BGR32,
182 .mmal = MMAL_ENCODING_BGRA,
184 .mmal_component = COMP_CAMERA,
190 static struct mmal_fmt *get_format(struct v4l2_format *f)
192 struct mmal_fmt *fmt;
195 for (k = 0; k < ARRAY_SIZE(formats); k++) {
197 if (fmt->fourcc == f->fmt.pix.pixelformat)
204 /* ------------------------------------------------------------------
205 * Videobuf queue operations
206 * ------------------------------------------------------------------
209 static int queue_setup(struct vb2_queue *vq,
210 unsigned int *nbuffers, unsigned int *nplanes,
211 unsigned int sizes[], struct device *alloc_ctxs[])
213 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
216 /* refuse queue setup if port is not configured */
217 if (!dev->capture.port) {
218 v4l2_err(&dev->v4l2_dev,
219 "%s: capture port not configured\n", __func__);
223 /* Handle CREATE_BUFS situation - *nplanes != 0 */
226 sizes[0] < dev->capture.port->current_buffer.size) {
227 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
228 "%s: dev:%p Invalid buffer request from CREATE_BUFS, size %u < %u, nplanes %u != 1\n",
229 __func__, dev, sizes[0],
230 dev->capture.port->current_buffer.size,
238 /* Handle REQBUFS situation */
239 size = dev->capture.port->current_buffer.size;
241 v4l2_err(&dev->v4l2_dev,
242 "%s: capture port buffer size is zero\n", __func__);
246 if (*nbuffers < dev->capture.port->minimum_buffer.num)
247 *nbuffers = dev->capture.port->minimum_buffer.num;
249 dev->capture.port->current_buffer.num = *nbuffers;
256 * videobuf2-vmalloc allocator is context-less so no need to set
260 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
266 static int buffer_init(struct vb2_buffer *vb)
268 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
269 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
270 struct vb2_mmal_buffer *buf =
271 container_of(vb2, struct vb2_mmal_buffer, vb);
273 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
275 buf->mmal.buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
276 buf->mmal.buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
278 return mmal_vchi_buffer_init(dev->instance, &buf->mmal);
281 static int buffer_prepare(struct vb2_buffer *vb)
283 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
286 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
289 if (!dev->capture.port || !dev->capture.fmt)
292 size = dev->capture.stride * dev->capture.height;
293 if (vb2_plane_size(vb, 0) < size) {
294 v4l2_err(&dev->v4l2_dev,
295 "%s data will not fit into plane (%lu < %lu)\n",
296 __func__, vb2_plane_size(vb, 0), size);
303 static void buffer_cleanup(struct vb2_buffer *vb)
305 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
306 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
307 struct vb2_mmal_buffer *buf =
308 container_of(vb2, struct vb2_mmal_buffer, vb);
310 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
313 mmal_vchi_buffer_cleanup(&buf->mmal);
316 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
318 return dev->capture.camera_port ==
319 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
322 static void buffer_cb(struct vchiq_mmal_instance *instance,
323 struct vchiq_mmal_port *port,
325 struct mmal_buffer *mmal_buf)
327 struct bm2835_mmal_dev *dev = port->cb_ctx;
328 struct vb2_mmal_buffer *buf =
329 container_of(mmal_buf, struct vb2_mmal_buffer, mmal);
331 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
332 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
333 __func__, status, buf, mmal_buf->length, mmal_buf->mmal_flags,
337 /* error in transfer */
339 /* there was a buffer with the error so return it */
340 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
345 if (mmal_buf->length == 0) {
347 if (dev->capture.frame_count) {
348 /* empty buffer whilst capturing - expected to be an
349 * EOS, so grab another frame
351 if (is_capturing(dev)) {
352 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
353 "Grab another frame");
354 vchiq_mmal_port_parameter_set(
356 dev->capture.camera_port,
357 MMAL_PARAMETER_CAPTURE,
358 &dev->capture.frame_count,
359 sizeof(dev->capture.frame_count));
361 if (vchiq_mmal_submit_buffer(instance, port,
363 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
364 "Failed to return EOS buffer");
366 /* stopping streaming.
367 * return buffer, and signal frame completion
369 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
370 complete(&dev->capture.frame_cmplt);
375 if (!dev->capture.frame_count) {
376 /* signal frame completion */
377 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
378 complete(&dev->capture.frame_cmplt);
382 if (dev->capture.vc_start_timestamp != -1 && mmal_buf->pts) {
384 s64 runtime_us = mmal_buf->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, mmal_buf->pts,
392 ktime_to_ns(timestamp));
393 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
395 buf->vb.vb2_buf.timestamp = ktime_get_ns();
397 buf->vb.sequence = dev->capture.sequence++;
398 buf->vb.field = V4L2_FIELD_NONE;
400 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, mmal_buf->length);
401 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
402 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
404 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
406 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
408 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
409 "Grab another frame as buffer has EOS");
410 vchiq_mmal_port_parameter_set(
412 dev->capture.camera_port,
413 MMAL_PARAMETER_CAPTURE,
414 &dev->capture.frame_count,
415 sizeof(dev->capture.frame_count));
419 static int enable_camera(struct bm2835_mmal_dev *dev)
423 if (!dev->camera_use_count) {
424 ret = vchiq_mmal_port_parameter_set(
426 &dev->component[COMP_CAMERA]->control,
427 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
428 sizeof(dev->camera_num));
430 v4l2_err(&dev->v4l2_dev,
431 "Failed setting camera num, ret %d\n", ret);
435 ret = vchiq_mmal_component_enable(dev->instance,
436 dev->component[COMP_CAMERA]);
438 v4l2_err(&dev->v4l2_dev,
439 "Failed enabling camera, ret %d\n", ret);
443 dev->camera_use_count++;
444 v4l2_dbg(1, bcm2835_v4l2_debug,
445 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
446 dev->camera_use_count);
450 static int disable_camera(struct bm2835_mmal_dev *dev)
454 if (!dev->camera_use_count) {
455 v4l2_err(&dev->v4l2_dev,
456 "Disabled the camera when already disabled\n");
459 dev->camera_use_count--;
460 if (!dev->camera_use_count) {
461 unsigned int i = 0xFFFFFFFF;
463 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
464 "Disabling camera\n");
465 ret = vchiq_mmal_component_disable(dev->instance,
466 dev->component[COMP_CAMERA]);
468 v4l2_err(&dev->v4l2_dev,
469 "Failed disabling camera, ret %d\n", ret);
472 vchiq_mmal_port_parameter_set(
474 &dev->component[COMP_CAMERA]->control,
475 MMAL_PARAMETER_CAMERA_NUM, &i,
478 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
479 "Camera refcount now %d\n", dev->camera_use_count);
483 static void buffer_queue(struct vb2_buffer *vb)
485 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
486 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
487 struct vb2_mmal_buffer *buf =
488 container_of(vb2, struct vb2_mmal_buffer, vb);
491 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
492 "%s: dev:%p buf:%p, idx %u\n",
493 __func__, dev, buf, vb2->vb2_buf.index);
495 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port,
498 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
502 static int start_streaming(struct vb2_queue *vq, unsigned int count)
504 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
508 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
511 /* ensure a format has actually been set */
512 if (!dev->capture.port)
515 if (enable_camera(dev) < 0) {
516 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
520 /*init_completion(&dev->capture.frame_cmplt); */
522 /* enable frame capture */
523 dev->capture.frame_count = 1;
525 /* reset sequence number */
526 dev->capture.sequence = 0;
528 /* if the preview is not already running, wait for a few frames for AGC
531 if (!dev->component[COMP_PREVIEW]->enabled)
534 /* enable the connection from camera to encoder (if applicable) */
535 if (dev->capture.camera_port != dev->capture.port &&
536 dev->capture.camera_port) {
537 ret = vchiq_mmal_port_enable(dev->instance,
538 dev->capture.camera_port, NULL);
540 v4l2_err(&dev->v4l2_dev,
541 "Failed to enable encode tunnel - error %d\n",
547 /* Get VC timestamp at this point in time */
548 parameter_size = sizeof(dev->capture.vc_start_timestamp);
549 if (vchiq_mmal_port_parameter_get(dev->instance,
550 dev->capture.camera_port,
551 MMAL_PARAMETER_SYSTEM_TIME,
552 &dev->capture.vc_start_timestamp,
554 v4l2_err(&dev->v4l2_dev,
555 "Failed to get VC start time - update your VC f/w\n");
557 /* Flag to indicate just to rely on kernel timestamps */
558 dev->capture.vc_start_timestamp = -1;
560 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
561 "Start time %lld size %d\n",
562 dev->capture.vc_start_timestamp, parameter_size);
565 dev->capture.kernel_start_ts = ktime_get();
567 /* enable the camera port */
568 dev->capture.port->cb_ctx = dev;
569 ret = vchiq_mmal_port_enable(dev->instance, dev->capture.port,
572 v4l2_err(&dev->v4l2_dev,
573 "Failed to enable capture port - error %d. Disabling camera port again\n",
576 vchiq_mmal_port_disable(dev->instance,
577 dev->capture.camera_port);
578 if (disable_camera(dev) < 0) {
579 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
585 /* capture the first frame */
586 vchiq_mmal_port_parameter_set(dev->instance,
587 dev->capture.camera_port,
588 MMAL_PARAMETER_CAPTURE,
589 &dev->capture.frame_count,
590 sizeof(dev->capture.frame_count));
594 /* abort streaming and wait for last buffer */
595 static void stop_streaming(struct vb2_queue *vq)
598 unsigned long timeout;
599 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
600 struct vchiq_mmal_port *port = dev->capture.port;
602 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
605 init_completion(&dev->capture.frame_cmplt);
606 dev->capture.frame_count = 0;
608 /* ensure a format has actually been set */
610 v4l2_err(&dev->v4l2_dev,
611 "no capture port - stream not started?\n");
615 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
617 /* stop capturing frames */
618 vchiq_mmal_port_parameter_set(dev->instance,
619 dev->capture.camera_port,
620 MMAL_PARAMETER_CAPTURE,
621 &dev->capture.frame_count,
622 sizeof(dev->capture.frame_count));
624 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
625 "disabling connection\n");
627 /* disable the connection from camera to encoder */
628 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
629 if (!ret && dev->capture.camera_port != port) {
630 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
632 ret = vchiq_mmal_port_disable(dev->instance, port);
633 } else if (dev->capture.camera_port != port) {
634 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
638 /* wait for all buffers to be returned */
639 while (atomic_read(&port->buffers_with_vpu)) {
640 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
641 "%s: Waiting for buffers to be returned - %d outstanding\n",
642 __func__, atomic_read(&port->buffers_with_vpu));
643 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt,
646 v4l2_err(&dev->v4l2_dev, "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
648 atomic_read(&port->buffers_with_vpu));
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,
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 f->pixelformat = fmt->fourcc;
713 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
714 struct v4l2_format *f)
716 struct bm2835_mmal_dev *dev = video_drvdata(file);
718 f->fmt.win = dev->overlay;
723 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
724 struct v4l2_format *f)
726 struct bm2835_mmal_dev *dev = video_drvdata(file);
728 f->fmt.win.field = V4L2_FIELD_NONE;
729 f->fmt.win.chromakey = 0;
730 f->fmt.win.clips = NULL;
731 f->fmt.win.clipcount = 0;
732 f->fmt.win.bitmap = NULL;
734 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
735 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
737 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
738 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
741 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
742 "Overlay: Now w/h %dx%d l/t %dx%d\n",
743 f->fmt.win.w.width, f->fmt.win.w.height,
744 f->fmt.win.w.left, f->fmt.win.w.top);
746 v4l2_dump_win_format(1,
754 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
755 struct v4l2_format *f)
757 struct bm2835_mmal_dev *dev = video_drvdata(file);
759 vidioc_try_fmt_vid_overlay(file, priv, f);
761 dev->overlay = f->fmt.win;
762 if (dev->component[COMP_PREVIEW]->enabled) {
763 set_overlay_params(dev,
764 &dev->component[COMP_PREVIEW]->input[0]);
770 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
773 struct bm2835_mmal_dev *dev = video_drvdata(file);
774 struct vchiq_mmal_port *src;
775 struct vchiq_mmal_port *dst;
777 if ((on && dev->component[COMP_PREVIEW]->enabled) ||
778 (!on && !dev->component[COMP_PREVIEW]->enabled))
779 return 0; /* already in requested state */
781 src = &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
784 /* disconnect preview ports and disable component */
785 ret = vchiq_mmal_port_disable(dev->instance, src);
787 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src,
790 ret = vchiq_mmal_component_disable(
792 dev->component[COMP_PREVIEW]);
798 /* set preview port format and connect it to output */
799 dst = &dev->component[COMP_PREVIEW]->input[0];
801 ret = vchiq_mmal_port_set_format(dev->instance, src);
805 ret = set_overlay_params(dev, dst);
809 if (enable_camera(dev) < 0)
812 ret = vchiq_mmal_component_enable(dev->instance,
813 dev->component[COMP_PREVIEW]);
817 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
819 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
823 return vchiq_mmal_port_enable(dev->instance, src, NULL);
826 static int vidioc_g_fbuf(struct file *file, void *fh,
827 struct v4l2_framebuffer *a)
829 /* The video overlay must stay within the framebuffer and can't be
830 * positioned independently.
832 struct bm2835_mmal_dev *dev = video_drvdata(file);
833 struct vchiq_mmal_port *preview_port =
834 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
836 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
837 V4L2_FBUF_CAP_GLOBAL_ALPHA;
838 a->flags = V4L2_FBUF_FLAG_OVERLAY;
839 a->fmt.width = preview_port->es.video.width;
840 a->fmt.height = preview_port->es.video.height;
841 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
842 a->fmt.bytesperline = preview_port->es.video.width;
843 a->fmt.sizeimage = (preview_port->es.video.width *
844 preview_port->es.video.height * 3) >> 1;
845 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
851 static int vidioc_enum_input(struct file *file, void *priv,
852 struct v4l2_input *inp)
854 /* only a single camera input */
858 inp->type = V4L2_INPUT_TYPE_CAMERA;
859 sprintf((char *)inp->name, "Camera %u", inp->index);
863 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
869 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
878 static int vidioc_querycap(struct file *file, void *priv,
879 struct v4l2_capability *cap)
881 struct bm2835_mmal_dev *dev = video_drvdata(file);
885 vchiq_mmal_version(dev->instance, &major, &minor);
887 strscpy(cap->driver, "bm2835 mmal", sizeof(cap->driver));
888 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
891 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
892 "platform:%s", dev->v4l2_dev.name);
896 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
897 struct v4l2_fmtdesc *f)
899 struct mmal_fmt *fmt;
901 if (f->index >= ARRAY_SIZE(formats))
904 fmt = &formats[f->index];
906 f->pixelformat = fmt->fourcc;
911 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
912 struct v4l2_format *f)
914 struct bm2835_mmal_dev *dev = video_drvdata(file);
916 f->fmt.pix.width = dev->capture.width;
917 f->fmt.pix.height = dev->capture.height;
918 f->fmt.pix.field = V4L2_FIELD_NONE;
919 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
920 f->fmt.pix.bytesperline = dev->capture.stride;
921 f->fmt.pix.sizeimage = dev->capture.buffersize;
923 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
924 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
925 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
926 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
928 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
931 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
936 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
937 struct v4l2_format *f)
939 struct bm2835_mmal_dev *dev = video_drvdata(file);
940 struct mmal_fmt *mfmt;
942 mfmt = get_format(f);
944 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
945 "Fourcc format (0x%08x) unknown.\n",
946 f->fmt.pix.pixelformat);
947 f->fmt.pix.pixelformat = formats[0].fourcc;
948 mfmt = get_format(f);
951 f->fmt.pix.field = V4L2_FIELD_NONE;
953 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
954 "Clipping/aligning %dx%d format %08X\n",
955 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
957 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
958 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
960 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
961 if (!mfmt->remove_padding) {
962 if (mfmt->depth == 24) {
964 * 24bpp is a pain as we can't use simple masking.
965 * Min stride is width aligned to 16, times 24bpp.
967 f->fmt.pix.bytesperline =
968 ((f->fmt.pix.width + 15) & ~15) * 3;
971 * GPU isn't removing padding, so stride is aligned to
974 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
976 f->fmt.pix.bytesperline =
977 (f->fmt.pix.bytesperline + align_mask) &
980 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
981 "Not removing padding, so bytes/line = %d\n",
982 f->fmt.pix.bytesperline);
985 /* Image buffer has to be padded to allow for alignment, even though
986 * we sometimes then remove that padding before delivering the buffer.
988 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
989 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
991 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
992 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
993 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
995 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
996 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
997 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
998 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1000 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1001 f->fmt.pix.priv = 0;
1003 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1004 "Now %dx%d format %08X\n",
1005 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1007 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1013 static int mmal_setup_video_component(struct bm2835_mmal_dev *dev,
1014 struct v4l2_format *f)
1016 bool overlay_enabled = !!dev->component[COMP_PREVIEW]->enabled;
1017 struct vchiq_mmal_port *preview_port;
1020 preview_port = &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
1022 /* Preview and encode ports need to match on resolution */
1023 if (overlay_enabled) {
1024 /* Need to disable the overlay before we can update
1027 ret = vchiq_mmal_port_disable(dev->instance, preview_port);
1029 ret = vchiq_mmal_port_connect_tunnel(dev->instance,
1034 preview_port->es.video.width = f->fmt.pix.width;
1035 preview_port->es.video.height = f->fmt.pix.height;
1036 preview_port->es.video.crop.x = 0;
1037 preview_port->es.video.crop.y = 0;
1038 preview_port->es.video.crop.width = f->fmt.pix.width;
1039 preview_port->es.video.crop.height = f->fmt.pix.height;
1040 preview_port->es.video.frame_rate.num =
1041 dev->capture.timeperframe.denominator;
1042 preview_port->es.video.frame_rate.den =
1043 dev->capture.timeperframe.numerator;
1044 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1046 if (overlay_enabled) {
1047 ret = vchiq_mmal_port_connect_tunnel(dev->instance,
1049 &dev->component[COMP_PREVIEW]->input[0]);
1053 ret = vchiq_mmal_port_enable(dev->instance, preview_port, NULL);
1059 static int mmal_setup_encode_component(struct bm2835_mmal_dev *dev,
1060 struct v4l2_format *f,
1061 struct vchiq_mmal_port *port,
1062 struct vchiq_mmal_port *camera_port,
1063 struct vchiq_mmal_component *component)
1065 struct mmal_fmt *mfmt = get_format(f);
1068 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1069 "vid_cap - set up encode comp\n");
1071 /* configure buffering */
1072 camera_port->current_buffer.size = camera_port->recommended_buffer.size;
1073 camera_port->current_buffer.num = camera_port->recommended_buffer.num;
1075 ret = vchiq_mmal_port_connect_tunnel(dev->instance, camera_port,
1076 &component->input[0]);
1078 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1079 "%s failed to create connection\n", __func__);
1080 /* ensure capture is not going to be tried */
1081 dev->capture.port = NULL;
1085 port->es.video.width = f->fmt.pix.width;
1086 port->es.video.height = f->fmt.pix.height;
1087 port->es.video.crop.x = 0;
1088 port->es.video.crop.y = 0;
1089 port->es.video.crop.width = f->fmt.pix.width;
1090 port->es.video.crop.height = f->fmt.pix.height;
1091 port->es.video.frame_rate.num =
1092 dev->capture.timeperframe.denominator;
1093 port->es.video.frame_rate.den =
1094 dev->capture.timeperframe.numerator;
1096 port->format.encoding = mfmt->mmal;
1097 port->format.encoding_variant = 0;
1098 /* Set any encoding specific parameters */
1099 switch (mfmt->mmal_component) {
1100 case COMP_VIDEO_ENCODE:
1101 port->format.bitrate = dev->capture.encode_bitrate;
1103 case COMP_IMAGE_ENCODE:
1104 /* Could set EXIF parameters here */
1110 ret = vchiq_mmal_port_set_format(dev->instance, port);
1112 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1113 "%s failed to set format %dx%d fmt %08X\n",
1117 f->fmt.pix.pixelformat);
1121 ret = vchiq_mmal_component_enable(dev->instance, component);
1123 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1124 "%s Failed to enable encode components\n", __func__);
1128 /* configure buffering */
1129 port->current_buffer.num = 1;
1130 port->current_buffer.size = f->fmt.pix.sizeimage;
1131 if (port->format.encoding == MMAL_ENCODING_JPEG) {
1132 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1133 "JPG - buf size now %d was %d\n",
1134 f->fmt.pix.sizeimage,
1135 port->current_buffer.size);
1136 port->current_buffer.size =
1137 (f->fmt.pix.sizeimage < (100 << 10)) ?
1138 (100 << 10) : f->fmt.pix.sizeimage;
1140 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1141 "vid_cap - cur_buf.size set to %d\n", f->fmt.pix.sizeimage);
1142 port->current_buffer.alignment = 0;
1147 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1148 struct v4l2_format *f)
1151 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1152 struct vchiq_mmal_component *encode_component = NULL;
1153 struct mmal_fmt *mfmt = get_format(f);
1159 if (dev->capture.encode_component) {
1160 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1161 "vid_cap - disconnect previous tunnel\n");
1163 /* Disconnect any previous connection */
1164 vchiq_mmal_port_connect_tunnel(dev->instance,
1165 dev->capture.camera_port, NULL);
1166 dev->capture.camera_port = NULL;
1167 ret = vchiq_mmal_component_disable(dev->instance,
1168 dev->capture.encode_component);
1170 v4l2_err(&dev->v4l2_dev,
1171 "Failed to disable encode component %d\n",
1174 dev->capture.encode_component = NULL;
1176 /* format dependent port setup */
1177 switch (mfmt->mmal_component) {
1179 /* Make a further decision on port based on resolution */
1180 if (f->fmt.pix.width <= max_video_width &&
1181 f->fmt.pix.height <= max_video_height)
1183 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1186 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1189 case COMP_IMAGE_ENCODE:
1190 encode_component = dev->component[COMP_IMAGE_ENCODE];
1191 port = &dev->component[COMP_IMAGE_ENCODE]->output[0];
1193 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1195 case COMP_VIDEO_ENCODE:
1196 encode_component = dev->component[COMP_VIDEO_ENCODE];
1197 port = &dev->component[COMP_VIDEO_ENCODE]->output[0];
1199 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1208 if (encode_component)
1209 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1211 camera_port->format.encoding = mfmt->mmal;
1213 if (dev->rgb_bgr_swapped) {
1214 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1215 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1216 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1217 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1220 remove_padding = mfmt->remove_padding;
1221 vchiq_mmal_port_parameter_set(dev->instance, camera_port,
1222 MMAL_PARAMETER_NO_IMAGE_PADDING,
1223 &remove_padding, sizeof(remove_padding));
1225 camera_port->format.encoding_variant = 0;
1226 camera_port->es.video.width = f->fmt.pix.width;
1227 camera_port->es.video.height = f->fmt.pix.height;
1228 camera_port->es.video.crop.x = 0;
1229 camera_port->es.video.crop.y = 0;
1230 camera_port->es.video.crop.width = f->fmt.pix.width;
1231 camera_port->es.video.crop.height = f->fmt.pix.height;
1232 camera_port->es.video.frame_rate.num = 0;
1233 camera_port->es.video.frame_rate.den = 1;
1234 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1236 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1240 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO]) {
1241 ret = mmal_setup_video_component(dev, f);
1245 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1246 "%s failed to set format %dx%d %08X\n", __func__,
1247 f->fmt.pix.width, f->fmt.pix.height,
1248 f->fmt.pix.pixelformat);
1249 /* ensure capture is not going to be tried */
1250 dev->capture.port = NULL;
1254 if (encode_component) {
1255 ret = mmal_setup_encode_component(dev, f, port,
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;
1268 dev->capture.fmt = mfmt;
1269 dev->capture.stride = f->fmt.pix.bytesperline;
1270 dev->capture.width = camera_port->es.video.crop.width;
1271 dev->capture.height = camera_port->es.video.crop.height;
1272 dev->capture.buffersize = port->current_buffer.size;
1274 /* select port for capture */
1275 dev->capture.port = port;
1276 dev->capture.camera_port = camera_port;
1277 dev->capture.encode_component = encode_component;
1278 v4l2_dbg(1, bcm2835_v4l2_debug,
1280 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1281 port->format.encoding,
1282 dev->capture.width, dev->capture.height,
1283 dev->capture.stride, dev->capture.buffersize);
1285 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1289 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1290 struct v4l2_format *f)
1293 struct bm2835_mmal_dev *dev = video_drvdata(file);
1294 struct mmal_fmt *mfmt;
1296 /* try the format to set valid parameters */
1297 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1299 v4l2_err(&dev->v4l2_dev,
1300 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1304 /* if a capture is running refuse to set format */
1305 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1306 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1310 /* If the format is unsupported v4l2 says we should switch to
1311 * a supported one and not return an error.
1313 mfmt = get_format(f);
1315 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1316 "Fourcc format (0x%08x) unknown.\n",
1317 f->fmt.pix.pixelformat);
1318 f->fmt.pix.pixelformat = formats[0].fourcc;
1319 mfmt = get_format(f);
1322 ret = mmal_setup_components(dev, f);
1324 v4l2_err(&dev->v4l2_dev,
1325 "%s: failed to setup mmal components: %d\n",
1333 static int vidioc_enum_framesizes(struct file *file, void *fh,
1334 struct v4l2_frmsizeenum *fsize)
1336 struct bm2835_mmal_dev *dev = video_drvdata(file);
1337 static const struct v4l2_frmsize_stepwise sizes = {
1345 for (i = 0; i < ARRAY_SIZE(formats); i++)
1346 if (formats[i].fourcc == fsize->pixel_format)
1348 if (i == ARRAY_SIZE(formats))
1350 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1351 fsize->stepwise = sizes;
1352 fsize->stepwise.max_width = dev->max_width;
1353 fsize->stepwise.max_height = dev->max_height;
1357 /* timeperframe is arbitrary and continuous */
1358 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1359 struct v4l2_frmivalenum *fival)
1361 struct bm2835_mmal_dev *dev = video_drvdata(file);
1367 for (i = 0; i < ARRAY_SIZE(formats); i++)
1368 if (formats[i].fourcc == fival->pixel_format)
1370 if (i == ARRAY_SIZE(formats))
1373 /* regarding width & height - we support any within range */
1374 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1375 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1378 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1380 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1381 fival->stepwise.min = tpf_min;
1382 fival->stepwise.max = tpf_max;
1383 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1388 static int vidioc_g_parm(struct file *file, void *priv,
1389 struct v4l2_streamparm *parm)
1391 struct bm2835_mmal_dev *dev = video_drvdata(file);
1393 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1396 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1397 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1398 parm->parm.capture.readbuffers = 1;
1402 static int vidioc_s_parm(struct file *file, void *priv,
1403 struct v4l2_streamparm *parm)
1405 struct bm2835_mmal_dev *dev = video_drvdata(file);
1406 struct v4l2_fract tpf;
1408 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1411 tpf = parm->parm.capture.timeperframe;
1413 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1414 tpf = tpf.denominator ? tpf : tpf_default;
1415 tpf = V4L2_FRACT_COMPARE(tpf, <, tpf_min) ? tpf_min : tpf;
1416 tpf = V4L2_FRACT_COMPARE(tpf, >, tpf_max) ? tpf_max : tpf;
1418 dev->capture.timeperframe = tpf;
1419 parm->parm.capture.timeperframe = tpf;
1420 parm->parm.capture.readbuffers = 1;
1421 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1423 set_framerate_params(dev);
1428 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1430 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1431 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1432 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1433 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1434 .vidioc_overlay = vidioc_overlay,
1435 .vidioc_g_fbuf = vidioc_g_fbuf,
1438 .vidioc_enum_input = vidioc_enum_input,
1439 .vidioc_g_input = vidioc_g_input,
1440 .vidioc_s_input = vidioc_s_input,
1443 .vidioc_querycap = vidioc_querycap,
1444 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1445 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1446 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1447 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1449 /* buffer management */
1450 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1451 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1452 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1453 .vidioc_querybuf = vb2_ioctl_querybuf,
1454 .vidioc_qbuf = vb2_ioctl_qbuf,
1455 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1456 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1457 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1458 .vidioc_g_parm = vidioc_g_parm,
1459 .vidioc_s_parm = vidioc_s_parm,
1460 .vidioc_streamon = vb2_ioctl_streamon,
1461 .vidioc_streamoff = vb2_ioctl_streamoff,
1463 .vidioc_log_status = v4l2_ctrl_log_status,
1464 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1465 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1468 /* ------------------------------------------------------------------
1469 * Driver init/finalise
1470 * ------------------------------------------------------------------
1473 static const struct v4l2_file_operations camera0_fops = {
1474 .owner = THIS_MODULE,
1475 .open = v4l2_fh_open,
1476 .release = vb2_fop_release,
1477 .read = vb2_fop_read,
1478 .poll = vb2_fop_poll,
1479 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1480 .mmap = vb2_fop_mmap,
1483 static const struct video_device vdev_template = {
1485 .fops = &camera0_fops,
1486 .ioctl_ops = &camera0_ioctl_ops,
1487 .release = video_device_release_empty,
1488 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
1489 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE,
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 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)
1535 struct mmal_parameter_camera_config cam_config = {
1536 .max_stills_w = dev->max_width,
1537 .max_stills_h = dev->max_height,
1539 .one_shot_stills = 1,
1540 .max_preview_video_w = (max_video_width > 1920) ?
1541 max_video_width : 1920,
1542 .max_preview_video_h = (max_video_height > 1088) ?
1543 max_video_height : 1088,
1544 .num_preview_video_frames = 3,
1545 .stills_capture_circular_buffer_height = 0,
1546 .fast_preview_resume = 0,
1547 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1550 return vchiq_mmal_port_parameter_set(instance, &camera->control,
1551 MMAL_PARAMETER_CAMERA_CONFIG,
1552 &cam_config, sizeof(cam_config));
1555 #define MAX_SUPPORTED_ENCODINGS 20
1557 /* MMAL instance and component init */
1558 static int mmal_init(struct bm2835_mmal_dev *dev)
1561 struct mmal_es_format_local *format;
1562 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1564 struct vchiq_mmal_component *camera;
1566 ret = vchiq_mmal_init(&dev->instance);
1568 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1573 /* get the camera component ready */
1574 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1575 &dev->component[COMP_CAMERA]);
1579 camera = dev->component[COMP_CAMERA];
1580 if (camera->outputs < CAM_PORT_COUNT) {
1581 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1582 __func__, camera->outputs, CAM_PORT_COUNT);
1587 ret = set_camera_parameters(dev->instance,
1591 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1596 /* There was an error in the firmware that meant the camera component
1597 * produced BGR instead of RGB.
1598 * This is now fixed, but in order to support the old firmwares, we
1601 dev->rgb_bgr_swapped = true;
1602 param_size = sizeof(supported_encodings);
1603 ret = vchiq_mmal_port_parameter_get(dev->instance,
1604 &camera->output[CAM_PORT_CAPTURE],
1605 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1606 &supported_encodings,
1611 for (i = 0; i < param_size / sizeof(u32); i++) {
1612 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1613 /* Found BGR24 first - old firmware. */
1616 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1617 /* Found RGB24 first
1618 * new firmware, so use RGB24.
1620 dev->rgb_bgr_swapped = false;
1625 format = &camera->output[CAM_PORT_PREVIEW].format;
1627 format->encoding = MMAL_ENCODING_OPAQUE;
1628 format->encoding_variant = MMAL_ENCODING_I420;
1630 format->es->video.width = 1024;
1631 format->es->video.height = 768;
1632 format->es->video.crop.x = 0;
1633 format->es->video.crop.y = 0;
1634 format->es->video.crop.width = 1024;
1635 format->es->video.crop.height = 768;
1636 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1637 format->es->video.frame_rate.den = 1;
1639 format = &camera->output[CAM_PORT_VIDEO].format;
1641 format->encoding = MMAL_ENCODING_OPAQUE;
1642 format->encoding_variant = MMAL_ENCODING_I420;
1644 format->es->video.width = 1024;
1645 format->es->video.height = 768;
1646 format->es->video.crop.x = 0;
1647 format->es->video.crop.y = 0;
1648 format->es->video.crop.width = 1024;
1649 format->es->video.crop.height = 768;
1650 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1651 format->es->video.frame_rate.den = 1;
1653 format = &camera->output[CAM_PORT_CAPTURE].format;
1655 format->encoding = MMAL_ENCODING_OPAQUE;
1657 format->es->video.width = 2592;
1658 format->es->video.height = 1944;
1659 format->es->video.crop.x = 0;
1660 format->es->video.crop.y = 0;
1661 format->es->video.crop.width = 2592;
1662 format->es->video.crop.height = 1944;
1663 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1664 format->es->video.frame_rate.den = 1;
1666 dev->capture.width = format->es->video.width;
1667 dev->capture.height = format->es->video.height;
1668 dev->capture.fmt = &formats[0];
1669 dev->capture.encode_component = NULL;
1670 dev->capture.timeperframe = tpf_default;
1671 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1672 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1674 /* get the preview component ready */
1675 ret = vchiq_mmal_component_init(dev->instance, "ril.video_render",
1676 &dev->component[COMP_PREVIEW]);
1680 if (dev->component[COMP_PREVIEW]->inputs < 1) {
1682 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1683 __func__, dev->component[COMP_PREVIEW]->inputs, 1);
1687 /* get the image encoder component ready */
1688 ret = vchiq_mmal_component_init(dev->instance, "ril.image_encode",
1689 &dev->component[COMP_IMAGE_ENCODE]);
1693 if (dev->component[COMP_IMAGE_ENCODE]->inputs < 1) {
1695 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1696 __func__, dev->component[COMP_IMAGE_ENCODE]->inputs,
1698 goto unreg_image_encoder;
1701 /* get the video encoder component ready */
1702 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1703 &dev->component[COMP_VIDEO_ENCODE]);
1705 goto unreg_image_encoder;
1707 if (dev->component[COMP_VIDEO_ENCODE]->inputs < 1) {
1709 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1710 __func__, dev->component[COMP_VIDEO_ENCODE]->inputs,
1712 goto unreg_vid_encoder;
1716 struct vchiq_mmal_port *encoder_port =
1717 &dev->component[COMP_VIDEO_ENCODE]->output[0];
1718 encoder_port->format.encoding = MMAL_ENCODING_H264;
1719 ret = vchiq_mmal_port_set_format(dev->instance,
1724 unsigned int enable = 1;
1726 vchiq_mmal_port_parameter_set(
1728 &dev->component[COMP_VIDEO_ENCODE]->control,
1729 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1730 &enable, sizeof(enable));
1732 vchiq_mmal_port_parameter_set(dev->instance,
1733 &dev->component[COMP_VIDEO_ENCODE]->control,
1734 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1738 ret = bm2835_mmal_set_all_camera_controls(dev);
1740 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1742 goto unreg_vid_encoder;
1748 pr_err("Cleanup: Destroy video encoder\n");
1749 vchiq_mmal_component_finalise(dev->instance,
1750 dev->component[COMP_VIDEO_ENCODE]);
1752 unreg_image_encoder:
1753 pr_err("Cleanup: Destroy image encoder\n");
1754 vchiq_mmal_component_finalise(dev->instance,
1755 dev->component[COMP_IMAGE_ENCODE]);
1758 pr_err("Cleanup: Destroy video render\n");
1759 vchiq_mmal_component_finalise(dev->instance,
1760 dev->component[COMP_PREVIEW]);
1763 pr_err("Cleanup: Destroy camera\n");
1764 vchiq_mmal_component_finalise(dev->instance,
1765 dev->component[COMP_CAMERA]);
1768 vchiq_mmal_finalise(dev->instance);
1772 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1773 struct video_device *vfd)
1777 *vfd = vdev_template;
1779 vfd->v4l2_dev = &dev->v4l2_dev;
1781 vfd->lock = &dev->mutex;
1783 vfd->queue = &dev->capture.vb_vidq;
1785 /* video device needs to be able to access instance data */
1786 video_set_drvdata(vfd, dev);
1788 ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1789 video_nr[dev->camera_num]);
1793 v4l2_info(vfd->v4l2_dev,
1794 "V4L2 device registered as %s - stills mode > %dx%d\n",
1795 video_device_node_name(vfd),
1796 max_video_width, max_video_height);
1801 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1806 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1807 video_device_node_name(&dev->vdev));
1809 video_unregister_device(&dev->vdev);
1811 if (dev->capture.encode_component) {
1812 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1813 "mmal_exit - disconnect tunnel\n");
1814 vchiq_mmal_port_connect_tunnel(dev->instance,
1815 dev->capture.camera_port, NULL);
1816 vchiq_mmal_component_disable(dev->instance,
1817 dev->capture.encode_component);
1819 vchiq_mmal_component_disable(dev->instance,
1820 dev->component[COMP_CAMERA]);
1822 vchiq_mmal_component_finalise(dev->instance,
1823 dev->component[COMP_VIDEO_ENCODE]);
1825 vchiq_mmal_component_finalise(dev->instance,
1826 dev->component[COMP_IMAGE_ENCODE]);
1828 vchiq_mmal_component_finalise(dev->instance,
1829 dev->component[COMP_PREVIEW]);
1831 vchiq_mmal_component_finalise(dev->instance,
1832 dev->component[COMP_CAMERA]);
1834 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1836 v4l2_device_unregister(&dev->v4l2_dev);
1841 static struct v4l2_format default_v4l2_format = {
1842 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1843 .fmt.pix.width = 1024,
1844 .fmt.pix.bytesperline = 0,
1845 .fmt.pix.height = 768,
1846 .fmt.pix.sizeimage = 1024 * 768,
1849 static int bcm2835_mmal_probe(struct platform_device *pdev)
1852 struct bm2835_mmal_dev *dev;
1853 struct vb2_queue *q;
1855 unsigned int num_cameras;
1856 struct vchiq_mmal_instance *instance;
1857 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1860 ret = vchiq_mmal_init(&instance);
1864 num_cameras = get_num_cameras(instance,
1866 MAX_BCM2835_CAMERAS);
1868 if (num_cameras < 1) {
1873 if (num_cameras > MAX_BCM2835_CAMERAS)
1874 num_cameras = MAX_BCM2835_CAMERAS;
1876 for (camera = 0; camera < num_cameras; camera++) {
1877 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1883 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1884 mutex_init(&dev->mutex);
1885 dev->max_width = resolutions[camera][0];
1886 dev->max_height = resolutions[camera][1];
1888 /* setup device defaults */
1889 dev->overlay.w.left = 150;
1890 dev->overlay.w.top = 50;
1891 dev->overlay.w.width = 1024;
1892 dev->overlay.w.height = 768;
1893 dev->overlay.clipcount = 0;
1894 dev->overlay.field = V4L2_FIELD_NONE;
1895 dev->overlay.global_alpha = 255;
1897 dev->capture.fmt = &formats[3]; /* JPEG */
1899 /* v4l device registration */
1900 dev->camera_num = v4l2_device_set_name(&dev->v4l2_dev,
1901 BM2835_MMAL_MODULE_NAME,
1903 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1905 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1910 /* setup v4l controls */
1911 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1913 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1917 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1920 dev->instance = instance;
1921 ret = mmal_init(dev);
1923 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1927 /* initialize queue */
1928 q = &dev->capture.vb_vidq;
1929 memset(q, 0, sizeof(*q));
1930 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1931 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1933 q->buf_struct_size = sizeof(struct vb2_mmal_buffer);
1934 q->ops = &bm2835_mmal_video_qops;
1935 q->mem_ops = &vb2_vmalloc_memops;
1936 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1937 q->lock = &dev->mutex;
1938 ret = vb2_queue_init(q);
1942 /* initialise video devices */
1943 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1945 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1950 /* Really want to call vidioc_s_fmt_vid_cap with the default
1951 * format, but currently the APIs don't join up.
1953 ret = mmal_setup_components(dev, &default_v4l2_format);
1955 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1960 v4l2_info(&dev->v4l2_dev,
1961 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1962 BM2835_MMAL_VERSION);
1969 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1970 v4l2_device_unregister(&dev->v4l2_dev);
1976 for (i = 0; i < camera; i++) {
1977 bcm2835_cleanup_instance(gdev[i]);
1982 vchiq_mmal_finalise(instance);
1987 static int bcm2835_mmal_remove(struct platform_device *pdev)
1990 struct vchiq_mmal_instance *instance = gdev[0]->instance;
1992 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
1993 bcm2835_cleanup_instance(gdev[camera]);
1994 gdev[camera] = NULL;
1996 vchiq_mmal_finalise(instance);
2001 static struct platform_driver bcm2835_camera_driver = {
2002 .probe = bcm2835_mmal_probe,
2003 .remove = bcm2835_mmal_remove,
2005 .name = "bcm2835-camera",
2009 module_platform_driver(bcm2835_camera_driver)
2011 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2012 MODULE_AUTHOR("Vincent Sanders");
2013 MODULE_LICENSE("GPL");
2014 MODULE_VERSION(BM2835_MMAL_VERSION);
2015 MODULE_ALIAS("platform:bcm2835-camera");