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 /* global device data array */
64 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
69 /* timeperframe: min/max and default */
70 static const struct v4l2_fract
71 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
72 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
73 tpf_default = {.numerator = 1000, .denominator = 30000};
75 /* Container for MMAL and VB2 buffers*/
76 struct vb2_mmal_buffer {
77 struct vb2_v4l2_buffer vb;
78 struct mmal_buffer mmal;
82 static struct mmal_fmt formats[] = {
84 .name = "4:2:0, planar, YUV",
85 .fourcc = V4L2_PIX_FMT_YUV420,
87 .mmal = MMAL_ENCODING_I420,
89 .mmal_component = COMP_CAMERA,
93 .name = "4:2:2, packed, YUYV",
94 .fourcc = V4L2_PIX_FMT_YUYV,
96 .mmal = MMAL_ENCODING_YUYV,
98 .mmal_component = COMP_CAMERA,
102 .name = "RGB24 (LE)",
103 .fourcc = V4L2_PIX_FMT_RGB24,
105 .mmal = MMAL_ENCODING_RGB24,
107 .mmal_component = COMP_CAMERA,
112 .fourcc = V4L2_PIX_FMT_JPEG,
113 .flags = V4L2_FMT_FLAG_COMPRESSED,
114 .mmal = MMAL_ENCODING_JPEG,
116 .mmal_component = COMP_IMAGE_ENCODE,
121 .fourcc = V4L2_PIX_FMT_H264,
122 .flags = V4L2_FMT_FLAG_COMPRESSED,
123 .mmal = MMAL_ENCODING_H264,
125 .mmal_component = COMP_VIDEO_ENCODE,
130 .fourcc = V4L2_PIX_FMT_MJPEG,
131 .flags = V4L2_FMT_FLAG_COMPRESSED,
132 .mmal = MMAL_ENCODING_MJPEG,
134 .mmal_component = COMP_VIDEO_ENCODE,
138 .name = "4:2:2, packed, YVYU",
139 .fourcc = V4L2_PIX_FMT_YVYU,
141 .mmal = MMAL_ENCODING_YVYU,
143 .mmal_component = COMP_CAMERA,
147 .name = "4:2:2, packed, VYUY",
148 .fourcc = V4L2_PIX_FMT_VYUY,
150 .mmal = MMAL_ENCODING_VYUY,
152 .mmal_component = COMP_CAMERA,
156 .name = "4:2:2, packed, UYVY",
157 .fourcc = V4L2_PIX_FMT_UYVY,
159 .mmal = MMAL_ENCODING_UYVY,
161 .mmal_component = COMP_CAMERA,
165 .name = "4:2:0, planar, NV12",
166 .fourcc = V4L2_PIX_FMT_NV12,
168 .mmal = MMAL_ENCODING_NV12,
170 .mmal_component = COMP_CAMERA,
174 .name = "RGB24 (BE)",
175 .fourcc = V4L2_PIX_FMT_BGR24,
177 .mmal = MMAL_ENCODING_BGR24,
179 .mmal_component = COMP_CAMERA,
183 .name = "4:2:0, planar, YVU",
184 .fourcc = V4L2_PIX_FMT_YVU420,
186 .mmal = MMAL_ENCODING_YV12,
188 .mmal_component = COMP_CAMERA,
192 .name = "4:2:0, planar, NV21",
193 .fourcc = V4L2_PIX_FMT_NV21,
195 .mmal = MMAL_ENCODING_NV21,
197 .mmal_component = COMP_CAMERA,
201 .name = "RGB32 (BE)",
202 .fourcc = V4L2_PIX_FMT_BGR32,
204 .mmal = MMAL_ENCODING_BGRA,
206 .mmal_component = COMP_CAMERA,
212 static struct mmal_fmt *get_format(struct v4l2_format *f)
214 struct mmal_fmt *fmt;
217 for (k = 0; k < ARRAY_SIZE(formats); k++) {
219 if (fmt->fourcc == f->fmt.pix.pixelformat)
226 /* ------------------------------------------------------------------
227 * Videobuf queue operations
228 * ------------------------------------------------------------------
231 static int queue_setup(struct vb2_queue *vq,
232 unsigned int *nbuffers, unsigned int *nplanes,
233 unsigned int sizes[], struct device *alloc_ctxs[])
235 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
238 /* refuse queue setup if port is not configured */
239 if (!dev->capture.port) {
240 v4l2_err(&dev->v4l2_dev,
241 "%s: capture port not configured\n", __func__);
245 /* Handle CREATE_BUFS situation - *nplanes != 0 */
248 sizes[0] < dev->capture.port->current_buffer.size) {
249 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
250 "%s: dev:%p Invalid buffer request from CREATE_BUFS, size %u < %u, nplanes %u != 1\n",
251 __func__, dev, sizes[0],
252 dev->capture.port->current_buffer.size,
260 /* Handle REQBUFS situation */
261 size = dev->capture.port->current_buffer.size;
263 v4l2_err(&dev->v4l2_dev,
264 "%s: capture port buffer size is zero\n", __func__);
268 if (*nbuffers < dev->capture.port->minimum_buffer.num)
269 *nbuffers = dev->capture.port->minimum_buffer.num;
271 dev->capture.port->current_buffer.num = *nbuffers;
278 * videobuf2-vmalloc allocator is context-less so no need to set
282 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
288 static int buffer_init(struct vb2_buffer *vb)
290 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
291 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
292 struct vb2_mmal_buffer *buf =
293 container_of(vb2, struct vb2_mmal_buffer, vb);
295 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
297 buf->mmal.buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
298 buf->mmal.buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
300 return mmal_vchi_buffer_init(dev->instance, &buf->mmal);
303 static int buffer_prepare(struct vb2_buffer *vb)
305 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
308 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
311 if (!dev->capture.port || !dev->capture.fmt)
314 size = dev->capture.stride * dev->capture.height;
315 if (vb2_plane_size(vb, 0) < size) {
316 v4l2_err(&dev->v4l2_dev,
317 "%s data will not fit into plane (%lu < %lu)\n",
318 __func__, vb2_plane_size(vb, 0), size);
325 static void buffer_cleanup(struct vb2_buffer *vb)
327 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
328 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
329 struct vb2_mmal_buffer *buf =
330 container_of(vb2, struct vb2_mmal_buffer, vb);
332 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
335 mmal_vchi_buffer_cleanup(&buf->mmal);
338 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
340 return dev->capture.camera_port ==
341 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
344 static void buffer_cb(struct vchiq_mmal_instance *instance,
345 struct vchiq_mmal_port *port,
347 struct mmal_buffer *mmal_buf)
349 struct bm2835_mmal_dev *dev = port->cb_ctx;
350 struct vb2_mmal_buffer *buf =
351 container_of(mmal_buf, struct vb2_mmal_buffer, mmal);
353 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
354 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
355 __func__, status, buf, mmal_buf->length, mmal_buf->mmal_flags,
359 /* error in transfer */
361 /* there was a buffer with the error so return it */
362 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
367 if (mmal_buf->length == 0) {
369 if (dev->capture.frame_count) {
370 /* empty buffer whilst capturing - expected to be an
371 * EOS, so grab another frame
373 if (is_capturing(dev)) {
374 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
375 "Grab another frame");
376 vchiq_mmal_port_parameter_set(
378 dev->capture.camera_port,
379 MMAL_PARAMETER_CAPTURE,
380 &dev->capture.frame_count,
381 sizeof(dev->capture.frame_count));
383 if (vchiq_mmal_submit_buffer(instance, port,
385 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
386 "Failed to return EOS buffer");
388 /* stopping streaming.
389 * return buffer, and signal frame completion
391 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
392 complete(&dev->capture.frame_cmplt);
397 if (!dev->capture.frame_count) {
398 /* signal frame completion */
399 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
400 complete(&dev->capture.frame_cmplt);
404 if (dev->capture.vc_start_timestamp == -1) {
406 * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
407 * kernel time, and have no latency compensation.
409 buf->vb.vb2_buf.timestamp = ktime_get_ns();
410 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
411 "Buffer time set as current time - %lld",
412 buf->vb.vb2_buf.timestamp);
413 } else if (mmal_buf->pts != 0) {
415 s64 runtime_us = mmal_buf->pts -
416 dev->capture.vc_start_timestamp;
417 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
419 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
420 "Convert start time %llu and %llu with offset %llu to %llu\n",
421 ktime_to_ns(dev->capture.kernel_start_ts),
422 dev->capture.vc_start_timestamp, mmal_buf->pts,
423 ktime_to_ns(timestamp));
424 if (timestamp < dev->capture.last_timestamp) {
425 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
426 "Negative delta - using last time\n");
427 timestamp = dev->capture.last_timestamp;
429 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
431 if (dev->capture.last_timestamp) {
432 buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
433 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
434 "Buffer time set as last timestamp - %lld",
435 buf->vb.vb2_buf.timestamp);
437 buf->vb.vb2_buf.timestamp =
438 ktime_to_ns(dev->capture.kernel_start_ts);
439 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
440 "Buffer time set as start timestamp - %lld",
441 buf->vb.vb2_buf.timestamp);
444 dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
445 buf->vb.sequence = dev->capture.sequence++;
446 buf->vb.field = V4L2_FIELD_NONE;
448 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, mmal_buf->length);
449 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
450 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
452 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
453 "Buffer has ts %llu", dev->capture.last_timestamp);
454 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
456 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
458 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
459 "Grab another frame as buffer has EOS");
460 vchiq_mmal_port_parameter_set(instance,
461 dev->capture.camera_port,
462 MMAL_PARAMETER_CAPTURE,
463 &dev->capture.frame_count,
464 sizeof(dev->capture.frame_count));
468 static int enable_camera(struct bm2835_mmal_dev *dev)
472 if (!dev->camera_use_count) {
473 ret = vchiq_mmal_port_parameter_set(
475 &dev->component[COMP_CAMERA]->control,
476 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
477 sizeof(dev->camera_num));
479 v4l2_err(&dev->v4l2_dev,
480 "Failed setting camera num, ret %d\n", ret);
484 ret = vchiq_mmal_component_enable(
486 dev->component[COMP_CAMERA]);
488 v4l2_err(&dev->v4l2_dev,
489 "Failed enabling camera, ret %d\n", ret);
493 dev->camera_use_count++;
494 v4l2_dbg(1, bcm2835_v4l2_debug,
495 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
496 dev->camera_use_count);
500 static int disable_camera(struct bm2835_mmal_dev *dev)
504 if (!dev->camera_use_count) {
505 v4l2_err(&dev->v4l2_dev,
506 "Disabled the camera when already disabled\n");
509 dev->camera_use_count--;
510 if (!dev->camera_use_count) {
511 unsigned int i = 0xFFFFFFFF;
513 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
514 "Disabling camera\n");
516 vchiq_mmal_component_disable(
518 dev->component[COMP_CAMERA]);
520 v4l2_err(&dev->v4l2_dev,
521 "Failed disabling camera, ret %d\n", ret);
524 vchiq_mmal_port_parameter_set(
526 &dev->component[COMP_CAMERA]->control,
527 MMAL_PARAMETER_CAMERA_NUM, &i,
530 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
531 "Camera refcount now %d\n", dev->camera_use_count);
535 static void buffer_queue(struct vb2_buffer *vb)
537 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
538 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
539 struct vb2_mmal_buffer *buf =
540 container_of(vb2, struct vb2_mmal_buffer, vb);
543 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
544 "%s: dev:%p buf:%p, idx %u\n",
545 __func__, dev, buf, vb2->vb2_buf.index);
547 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port,
550 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
554 static int start_streaming(struct vb2_queue *vq, unsigned int count)
556 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
560 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
563 /* ensure a format has actually been set */
564 if (!dev->capture.port)
567 if (enable_camera(dev) < 0) {
568 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
572 /*init_completion(&dev->capture.frame_cmplt); */
574 /* enable frame capture */
575 dev->capture.frame_count = 1;
577 /* reset sequence number */
578 dev->capture.sequence = 0;
580 /* if the preview is not already running, wait for a few frames for AGC
583 if (!dev->component[COMP_PREVIEW]->enabled)
586 /* enable the connection from camera to encoder (if applicable) */
587 if (dev->capture.camera_port != dev->capture.port &&
588 dev->capture.camera_port) {
589 ret = vchiq_mmal_port_enable(dev->instance,
590 dev->capture.camera_port, NULL);
592 v4l2_err(&dev->v4l2_dev,
593 "Failed to enable encode tunnel - error %d\n",
599 /* Get VC timestamp at this point in time */
600 parameter_size = sizeof(dev->capture.vc_start_timestamp);
601 if (vchiq_mmal_port_parameter_get(dev->instance,
602 dev->capture.camera_port,
603 MMAL_PARAMETER_SYSTEM_TIME,
604 &dev->capture.vc_start_timestamp,
606 v4l2_err(&dev->v4l2_dev,
607 "Failed to get VC start time - update your VC f/w\n");
609 /* Flag to indicate just to rely on kernel timestamps */
610 dev->capture.vc_start_timestamp = -1;
612 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
613 "Start time %lld size %d\n",
614 dev->capture.vc_start_timestamp, parameter_size);
617 dev->capture.kernel_start_ts = ktime_get();
618 dev->capture.last_timestamp = 0;
620 /* enable the camera port */
621 dev->capture.port->cb_ctx = dev;
623 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
625 v4l2_err(&dev->v4l2_dev,
626 "Failed to enable capture port - error %d. Disabling camera port again\n",
629 vchiq_mmal_port_disable(dev->instance,
630 dev->capture.camera_port);
631 if (disable_camera(dev) < 0) {
632 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
638 /* capture the first frame */
639 vchiq_mmal_port_parameter_set(dev->instance,
640 dev->capture.camera_port,
641 MMAL_PARAMETER_CAPTURE,
642 &dev->capture.frame_count,
643 sizeof(dev->capture.frame_count));
647 /* abort streaming and wait for last buffer */
648 static void stop_streaming(struct vb2_queue *vq)
651 unsigned long timeout;
652 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
653 struct vchiq_mmal_port *port = dev->capture.port;
655 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
658 init_completion(&dev->capture.frame_cmplt);
659 dev->capture.frame_count = 0;
661 /* ensure a format has actually been set */
663 v4l2_err(&dev->v4l2_dev,
664 "no capture port - stream not started?\n");
668 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
670 /* stop capturing frames */
671 vchiq_mmal_port_parameter_set(dev->instance,
672 dev->capture.camera_port,
673 MMAL_PARAMETER_CAPTURE,
674 &dev->capture.frame_count,
675 sizeof(dev->capture.frame_count));
677 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
678 "disabling connection\n");
680 /* disable the connection from camera to encoder */
681 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
682 if (!ret && dev->capture.camera_port != port) {
683 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
685 ret = vchiq_mmal_port_disable(dev->instance, port);
686 } else if (dev->capture.camera_port != port) {
687 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
691 /* wait for all buffers to be returned */
692 while (atomic_read(&port->buffers_with_vpu)) {
693 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
694 "%s: Waiting for buffers to be returned - %d outstanding\n",
695 __func__, atomic_read(&port->buffers_with_vpu));
696 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt,
699 v4l2_err(&dev->v4l2_dev, "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
701 atomic_read(&port->buffers_with_vpu));
706 if (disable_camera(dev) < 0)
707 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
710 static const struct vb2_ops bm2835_mmal_video_qops = {
711 .queue_setup = queue_setup,
712 .buf_init = buffer_init,
713 .buf_prepare = buffer_prepare,
714 .buf_cleanup = buffer_cleanup,
715 .buf_queue = buffer_queue,
716 .start_streaming = start_streaming,
717 .stop_streaming = stop_streaming,
718 .wait_prepare = vb2_ops_wait_prepare,
719 .wait_finish = vb2_ops_wait_finish,
722 /* ------------------------------------------------------------------
724 * ------------------------------------------------------------------
727 static int set_overlay_params(struct bm2835_mmal_dev *dev,
728 struct vchiq_mmal_port *port)
730 struct mmal_parameter_displayregion prev_config = {
731 .set = MMAL_DISPLAY_SET_LAYER |
732 MMAL_DISPLAY_SET_ALPHA |
733 MMAL_DISPLAY_SET_DEST_RECT |
734 MMAL_DISPLAY_SET_FULLSCREEN,
735 .layer = PREVIEW_LAYER,
736 .alpha = dev->overlay.global_alpha,
739 .x = dev->overlay.w.left,
740 .y = dev->overlay.w.top,
741 .width = dev->overlay.w.width,
742 .height = dev->overlay.w.height,
745 return vchiq_mmal_port_parameter_set(dev->instance, port,
746 MMAL_PARAMETER_DISPLAYREGION,
747 &prev_config, sizeof(prev_config));
751 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
752 struct v4l2_fmtdesc *f)
754 struct mmal_fmt *fmt;
756 if (f->index >= ARRAY_SIZE(formats))
759 fmt = &formats[f->index];
761 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
762 f->pixelformat = fmt->fourcc;
763 f->flags = fmt->flags;
768 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
769 struct v4l2_format *f)
771 struct bm2835_mmal_dev *dev = video_drvdata(file);
773 f->fmt.win = dev->overlay;
778 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
779 struct v4l2_format *f)
781 struct bm2835_mmal_dev *dev = video_drvdata(file);
783 f->fmt.win.field = V4L2_FIELD_NONE;
784 f->fmt.win.chromakey = 0;
785 f->fmt.win.clips = NULL;
786 f->fmt.win.clipcount = 0;
787 f->fmt.win.bitmap = NULL;
789 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
790 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
792 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
793 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
796 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
797 "Overlay: Now w/h %dx%d l/t %dx%d\n",
798 f->fmt.win.w.width, f->fmt.win.w.height,
799 f->fmt.win.w.left, f->fmt.win.w.top);
801 v4l2_dump_win_format(1,
809 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
810 struct v4l2_format *f)
812 struct bm2835_mmal_dev *dev = video_drvdata(file);
814 vidioc_try_fmt_vid_overlay(file, priv, f);
816 dev->overlay = f->fmt.win;
817 if (dev->component[COMP_PREVIEW]->enabled) {
818 set_overlay_params(dev,
819 &dev->component[COMP_PREVIEW]->input[0]);
825 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
828 struct bm2835_mmal_dev *dev = video_drvdata(file);
829 struct vchiq_mmal_port *src;
830 struct vchiq_mmal_port *dst;
832 if ((on && dev->component[COMP_PREVIEW]->enabled) ||
833 (!on && !dev->component[COMP_PREVIEW]->enabled))
834 return 0; /* already in requested state */
837 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
840 /* disconnect preview ports and disable component */
841 ret = vchiq_mmal_port_disable(dev->instance, src);
844 vchiq_mmal_port_connect_tunnel(dev->instance, src,
847 ret = vchiq_mmal_component_disable(
849 dev->component[COMP_PREVIEW]);
855 /* set preview port format and connect it to output */
856 dst = &dev->component[COMP_PREVIEW]->input[0];
858 ret = vchiq_mmal_port_set_format(dev->instance, src);
862 ret = set_overlay_params(dev, dst);
866 if (enable_camera(dev) < 0) {
871 ret = vchiq_mmal_component_enable(
873 dev->component[COMP_PREVIEW]);
877 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
879 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
881 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
886 static int vidioc_g_fbuf(struct file *file, void *fh,
887 struct v4l2_framebuffer *a)
889 /* The video overlay must stay within the framebuffer and can't be
890 * positioned independently.
892 struct bm2835_mmal_dev *dev = video_drvdata(file);
893 struct vchiq_mmal_port *preview_port =
894 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
896 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
897 V4L2_FBUF_CAP_GLOBAL_ALPHA;
898 a->flags = V4L2_FBUF_FLAG_OVERLAY;
899 a->fmt.width = preview_port->es.video.width;
900 a->fmt.height = preview_port->es.video.height;
901 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
902 a->fmt.bytesperline = preview_port->es.video.width;
903 a->fmt.sizeimage = (preview_port->es.video.width *
904 preview_port->es.video.height * 3) >> 1;
905 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
911 static int vidioc_enum_input(struct file *file, void *priv,
912 struct v4l2_input *inp)
914 /* only a single camera input */
918 inp->type = V4L2_INPUT_TYPE_CAMERA;
919 sprintf((char *)inp->name, "Camera %u", inp->index);
923 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
929 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
938 static int vidioc_querycap(struct file *file, void *priv,
939 struct v4l2_capability *cap)
941 struct bm2835_mmal_dev *dev = video_drvdata(file);
945 vchiq_mmal_version(dev->instance, &major, &minor);
947 strcpy((char *)cap->driver, "bm2835 mmal");
948 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
951 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
952 "platform:%s", dev->v4l2_dev.name);
953 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
954 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
955 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
960 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
961 struct v4l2_fmtdesc *f)
963 struct mmal_fmt *fmt;
965 if (f->index >= ARRAY_SIZE(formats))
968 fmt = &formats[f->index];
970 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
971 f->pixelformat = fmt->fourcc;
972 f->flags = fmt->flags;
977 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
978 struct v4l2_format *f)
980 struct bm2835_mmal_dev *dev = video_drvdata(file);
982 f->fmt.pix.width = dev->capture.width;
983 f->fmt.pix.height = dev->capture.height;
984 f->fmt.pix.field = V4L2_FIELD_NONE;
985 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
986 f->fmt.pix.bytesperline = dev->capture.stride;
987 f->fmt.pix.sizeimage = dev->capture.buffersize;
989 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
990 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
991 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
992 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
994 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
997 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1002 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1003 struct v4l2_format *f)
1005 struct bm2835_mmal_dev *dev = video_drvdata(file);
1006 struct mmal_fmt *mfmt;
1008 mfmt = get_format(f);
1010 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1011 "Fourcc format (0x%08x) unknown.\n",
1012 f->fmt.pix.pixelformat);
1013 f->fmt.pix.pixelformat = formats[0].fourcc;
1014 mfmt = get_format(f);
1017 f->fmt.pix.field = V4L2_FIELD_NONE;
1019 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1020 "Clipping/aligning %dx%d format %08X\n",
1021 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1023 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
1024 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
1026 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
1027 if (!mfmt->remove_padding) {
1028 if (mfmt->depth == 24) {
1030 * 24bpp is a pain as we can't use simple masking.
1031 * Min stride is width aligned to 16, times 24bpp.
1033 f->fmt.pix.bytesperline =
1034 ((f->fmt.pix.width + 15) & ~15) * 3;
1037 * GPU isn't removing padding, so stride is aligned to
1040 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
1042 f->fmt.pix.bytesperline =
1043 (f->fmt.pix.bytesperline + align_mask) &
1046 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1047 "Not removing padding, so bytes/line = %d\n",
1048 f->fmt.pix.bytesperline);
1051 /* Image buffer has to be padded to allow for alignment, even though
1052 * we sometimes then remove that padding before delivering the buffer.
1054 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
1055 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
1057 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
1058 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
1059 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
1061 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
1062 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1063 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
1064 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1066 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1067 f->fmt.pix.priv = 0;
1069 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1070 "Now %dx%d format %08X\n",
1071 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1073 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1078 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1079 struct v4l2_format *f)
1082 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1083 struct vchiq_mmal_component *encode_component = NULL;
1084 struct mmal_fmt *mfmt = get_format(f);
1090 if (dev->capture.encode_component) {
1091 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1092 "vid_cap - disconnect previous tunnel\n");
1094 /* Disconnect any previous connection */
1095 vchiq_mmal_port_connect_tunnel(dev->instance,
1096 dev->capture.camera_port, NULL);
1097 dev->capture.camera_port = NULL;
1098 ret = vchiq_mmal_component_disable(dev->instance,
1099 dev->capture.encode_component);
1101 v4l2_err(&dev->v4l2_dev,
1102 "Failed to disable encode component %d\n",
1105 dev->capture.encode_component = NULL;
1107 /* format dependent port setup */
1108 switch (mfmt->mmal_component) {
1110 /* Make a further decision on port based on resolution */
1111 if (f->fmt.pix.width <= max_video_width &&
1112 f->fmt.pix.height <= max_video_height)
1114 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1117 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1120 case COMP_IMAGE_ENCODE:
1121 encode_component = dev->component[COMP_IMAGE_ENCODE];
1122 port = &dev->component[COMP_IMAGE_ENCODE]->output[0];
1124 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1126 case COMP_VIDEO_ENCODE:
1127 encode_component = dev->component[COMP_VIDEO_ENCODE];
1128 port = &dev->component[COMP_VIDEO_ENCODE]->output[0];
1130 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1139 if (encode_component)
1140 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1142 camera_port->format.encoding = mfmt->mmal;
1144 if (dev->rgb_bgr_swapped) {
1145 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1146 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1147 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1148 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1151 remove_padding = mfmt->remove_padding;
1152 vchiq_mmal_port_parameter_set(dev->instance,
1154 MMAL_PARAMETER_NO_IMAGE_PADDING,
1155 &remove_padding, sizeof(remove_padding));
1157 camera_port->format.encoding_variant = 0;
1158 camera_port->es.video.width = f->fmt.pix.width;
1159 camera_port->es.video.height = f->fmt.pix.height;
1160 camera_port->es.video.crop.x = 0;
1161 camera_port->es.video.crop.y = 0;
1162 camera_port->es.video.crop.width = f->fmt.pix.width;
1163 camera_port->es.video.crop.height = f->fmt.pix.height;
1164 camera_port->es.video.frame_rate.num = 0;
1165 camera_port->es.video.frame_rate.den = 1;
1166 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1168 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1172 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO]) {
1173 bool overlay_enabled =
1174 !!dev->component[COMP_PREVIEW]->enabled;
1175 struct vchiq_mmal_port *preview_port =
1176 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
1177 /* Preview and encode ports need to match on resolution */
1178 if (overlay_enabled) {
1179 /* Need to disable the overlay before we can update
1183 vchiq_mmal_port_disable(dev->instance,
1187 vchiq_mmal_port_connect_tunnel(
1192 preview_port->es.video.width = f->fmt.pix.width;
1193 preview_port->es.video.height = f->fmt.pix.height;
1194 preview_port->es.video.crop.x = 0;
1195 preview_port->es.video.crop.y = 0;
1196 preview_port->es.video.crop.width = f->fmt.pix.width;
1197 preview_port->es.video.crop.height = f->fmt.pix.height;
1198 preview_port->es.video.frame_rate.num =
1199 dev->capture.timeperframe.denominator;
1200 preview_port->es.video.frame_rate.den =
1201 dev->capture.timeperframe.numerator;
1202 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1203 if (overlay_enabled) {
1204 ret = vchiq_mmal_port_connect_tunnel(
1207 &dev->component[COMP_PREVIEW]->input[0]);
1209 ret = vchiq_mmal_port_enable(dev->instance,
1216 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1217 "%s failed to set format %dx%d %08X\n", __func__,
1218 f->fmt.pix.width, f->fmt.pix.height,
1219 f->fmt.pix.pixelformat);
1220 /* ensure capture is not going to be tried */
1221 dev->capture.port = NULL;
1223 if (encode_component) {
1224 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1225 "vid_cap - set up encode comp\n");
1227 /* configure buffering */
1228 camera_port->current_buffer.size =
1229 camera_port->recommended_buffer.size;
1230 camera_port->current_buffer.num =
1231 camera_port->recommended_buffer.num;
1234 vchiq_mmal_port_connect_tunnel(
1237 &encode_component->input[0]);
1239 v4l2_dbg(1, bcm2835_v4l2_debug,
1241 "%s failed to create connection\n",
1243 /* ensure capture is not going to be tried */
1244 dev->capture.port = NULL;
1246 port->es.video.width = f->fmt.pix.width;
1247 port->es.video.height = f->fmt.pix.height;
1248 port->es.video.crop.x = 0;
1249 port->es.video.crop.y = 0;
1250 port->es.video.crop.width = f->fmt.pix.width;
1251 port->es.video.crop.height = f->fmt.pix.height;
1252 port->es.video.frame_rate.num =
1253 dev->capture.timeperframe.denominator;
1254 port->es.video.frame_rate.den =
1255 dev->capture.timeperframe.numerator;
1257 port->format.encoding = mfmt->mmal;
1258 port->format.encoding_variant = 0;
1259 /* Set any encoding specific parameters */
1260 switch (mfmt->mmal_component) {
1261 case COMP_VIDEO_ENCODE:
1262 port->format.bitrate =
1263 dev->capture.encode_bitrate;
1265 case COMP_IMAGE_ENCODE:
1266 /* Could set EXIF parameters here */
1271 ret = vchiq_mmal_port_set_format(dev->instance,
1274 v4l2_dbg(1, bcm2835_v4l2_debug,
1276 "%s failed to set format %dx%d fmt %08X\n",
1280 f->fmt.pix.pixelformat
1285 ret = vchiq_mmal_component_enable(
1289 v4l2_dbg(1, bcm2835_v4l2_debug,
1291 "%s Failed to enable encode components\n",
1296 /* configure buffering */
1297 port->current_buffer.num = 1;
1298 port->current_buffer.size =
1299 f->fmt.pix.sizeimage;
1300 if (port->format.encoding ==
1301 MMAL_ENCODING_JPEG) {
1302 v4l2_dbg(1, bcm2835_v4l2_debug,
1304 "JPG - buf size now %d was %d\n",
1305 f->fmt.pix.sizeimage,
1306 port->current_buffer.size);
1307 port->current_buffer.size =
1308 (f->fmt.pix.sizeimage <
1310 (100 << 10) : f->fmt.pix.sizeimage;
1312 v4l2_dbg(1, bcm2835_v4l2_debug,
1314 "vid_cap - cur_buf.size set to %d\n",
1315 f->fmt.pix.sizeimage);
1316 port->current_buffer.alignment = 0;
1319 /* configure buffering */
1320 camera_port->current_buffer.num = 1;
1321 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1322 camera_port->current_buffer.alignment = 0;
1326 dev->capture.fmt = mfmt;
1327 dev->capture.stride = f->fmt.pix.bytesperline;
1328 dev->capture.width = camera_port->es.video.crop.width;
1329 dev->capture.height = camera_port->es.video.crop.height;
1330 dev->capture.buffersize = port->current_buffer.size;
1332 /* select port for capture */
1333 dev->capture.port = port;
1334 dev->capture.camera_port = camera_port;
1335 dev->capture.encode_component = encode_component;
1336 v4l2_dbg(1, bcm2835_v4l2_debug,
1338 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1339 port->format.encoding,
1340 dev->capture.width, dev->capture.height,
1341 dev->capture.stride, dev->capture.buffersize);
1345 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1349 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1350 struct v4l2_format *f)
1353 struct bm2835_mmal_dev *dev = video_drvdata(file);
1354 struct mmal_fmt *mfmt;
1356 /* try the format to set valid parameters */
1357 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1359 v4l2_err(&dev->v4l2_dev,
1360 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1364 /* if a capture is running refuse to set format */
1365 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1366 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1370 /* If the format is unsupported v4l2 says we should switch to
1371 * a supported one and not return an error.
1373 mfmt = get_format(f);
1375 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1376 "Fourcc format (0x%08x) unknown.\n",
1377 f->fmt.pix.pixelformat);
1378 f->fmt.pix.pixelformat = formats[0].fourcc;
1379 mfmt = get_format(f);
1382 ret = mmal_setup_components(dev, f);
1384 v4l2_err(&dev->v4l2_dev,
1385 "%s: failed to setup mmal components: %d\n",
1393 static int vidioc_enum_framesizes(struct file *file, void *fh,
1394 struct v4l2_frmsizeenum *fsize)
1396 struct bm2835_mmal_dev *dev = video_drvdata(file);
1397 static const struct v4l2_frmsize_stepwise sizes = {
1405 for (i = 0; i < ARRAY_SIZE(formats); i++)
1406 if (formats[i].fourcc == fsize->pixel_format)
1408 if (i == ARRAY_SIZE(formats))
1410 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1411 fsize->stepwise = sizes;
1412 fsize->stepwise.max_width = dev->max_width;
1413 fsize->stepwise.max_height = dev->max_height;
1417 /* timeperframe is arbitrary and continuous */
1418 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1419 struct v4l2_frmivalenum *fival)
1421 struct bm2835_mmal_dev *dev = video_drvdata(file);
1427 for (i = 0; i < ARRAY_SIZE(formats); i++)
1428 if (formats[i].fourcc == fival->pixel_format)
1430 if (i == ARRAY_SIZE(formats))
1433 /* regarding width & height - we support any within range */
1434 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1435 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1438 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1440 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1441 fival->stepwise.min = tpf_min;
1442 fival->stepwise.max = tpf_max;
1443 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1448 static int vidioc_g_parm(struct file *file, void *priv,
1449 struct v4l2_streamparm *parm)
1451 struct bm2835_mmal_dev *dev = video_drvdata(file);
1453 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1456 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1457 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1458 parm->parm.capture.readbuffers = 1;
1462 #define FRACT_CMP(a, OP, b) \
1463 ((u64)(a).numerator * (b).denominator OP \
1464 (u64)(b).numerator * (a).denominator)
1466 static int vidioc_s_parm(struct file *file, void *priv,
1467 struct v4l2_streamparm *parm)
1469 struct bm2835_mmal_dev *dev = video_drvdata(file);
1470 struct v4l2_fract tpf;
1472 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1475 tpf = parm->parm.capture.timeperframe;
1477 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1478 tpf = tpf.denominator ? tpf : tpf_default;
1479 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1480 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1482 dev->capture.timeperframe = tpf;
1483 parm->parm.capture.timeperframe = tpf;
1484 parm->parm.capture.readbuffers = 1;
1485 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1487 set_framerate_params(dev);
1492 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1494 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1495 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1496 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1497 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1498 .vidioc_overlay = vidioc_overlay,
1499 .vidioc_g_fbuf = vidioc_g_fbuf,
1502 .vidioc_enum_input = vidioc_enum_input,
1503 .vidioc_g_input = vidioc_g_input,
1504 .vidioc_s_input = vidioc_s_input,
1507 .vidioc_querycap = vidioc_querycap,
1508 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1509 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1510 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1511 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1513 /* buffer management */
1514 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1515 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1516 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1517 .vidioc_querybuf = vb2_ioctl_querybuf,
1518 .vidioc_qbuf = vb2_ioctl_qbuf,
1519 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1520 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1521 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1522 .vidioc_g_parm = vidioc_g_parm,
1523 .vidioc_s_parm = vidioc_s_parm,
1524 .vidioc_streamon = vb2_ioctl_streamon,
1525 .vidioc_streamoff = vb2_ioctl_streamoff,
1527 .vidioc_log_status = v4l2_ctrl_log_status,
1528 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1529 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1532 /* ------------------------------------------------------------------
1533 * Driver init/finalise
1534 * ------------------------------------------------------------------
1537 static const struct v4l2_file_operations camera0_fops = {
1538 .owner = THIS_MODULE,
1539 .open = v4l2_fh_open,
1540 .release = vb2_fop_release,
1541 .read = vb2_fop_read,
1542 .poll = vb2_fop_poll,
1543 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1544 .mmap = vb2_fop_mmap,
1547 static const struct video_device vdev_template = {
1549 .fops = &camera0_fops,
1550 .ioctl_ops = &camera0_ioctl_ops,
1551 .release = video_device_release_empty,
1554 /* Returns the number of cameras, and also the max resolution supported
1557 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1558 unsigned int resolutions[][2], int num_resolutions)
1561 struct vchiq_mmal_component *cam_info_component;
1562 struct mmal_parameter_camera_info_t cam_info = {0};
1563 u32 param_size = sizeof(cam_info);
1566 /* create a camera_info component */
1567 ret = vchiq_mmal_component_init(instance, "camera_info",
1568 &cam_info_component);
1570 /* Unusual failure - let's guess one camera. */
1573 if (vchiq_mmal_port_parameter_get(instance,
1574 &cam_info_component->control,
1575 MMAL_PARAMETER_CAMERA_INFO,
1578 pr_info("Failed to get camera info\n");
1581 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1583 resolutions[i][0] = cam_info.cameras[i].max_width;
1584 resolutions[i][1] = cam_info.cameras[i].max_height;
1587 vchiq_mmal_component_finalise(instance,
1588 cam_info_component);
1590 return cam_info.num_cameras;
1593 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1594 struct vchiq_mmal_component *camera,
1595 struct bm2835_mmal_dev *dev)
1598 struct mmal_parameter_camera_config cam_config = {
1599 .max_stills_w = dev->max_width,
1600 .max_stills_h = dev->max_height,
1602 .one_shot_stills = 1,
1603 .max_preview_video_w = (max_video_width > 1920) ?
1604 max_video_width : 1920,
1605 .max_preview_video_h = (max_video_height > 1088) ?
1606 max_video_height : 1088,
1607 .num_preview_video_frames = 3,
1608 .stills_capture_circular_buffer_height = 0,
1609 .fast_preview_resume = 0,
1610 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1613 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1614 MMAL_PARAMETER_CAMERA_CONFIG,
1615 &cam_config, sizeof(cam_config));
1619 #define MAX_SUPPORTED_ENCODINGS 20
1621 /* MMAL instance and component init */
1622 static int mmal_init(struct bm2835_mmal_dev *dev)
1625 struct mmal_es_format_local *format;
1626 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1628 struct vchiq_mmal_component *camera;
1630 ret = vchiq_mmal_init(&dev->instance);
1632 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1637 /* get the camera component ready */
1638 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1639 &dev->component[COMP_CAMERA]);
1643 camera = dev->component[COMP_CAMERA];
1644 if (camera->outputs < CAM_PORT_COUNT) {
1645 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1646 __func__, camera->outputs, CAM_PORT_COUNT);
1651 ret = set_camera_parameters(dev->instance,
1655 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1660 /* There was an error in the firmware that meant the camera component
1661 * produced BGR instead of RGB.
1662 * This is now fixed, but in order to support the old firmwares, we
1665 dev->rgb_bgr_swapped = true;
1666 param_size = sizeof(supported_encodings);
1667 ret = vchiq_mmal_port_parameter_get(dev->instance,
1668 &camera->output[CAM_PORT_CAPTURE],
1669 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1670 &supported_encodings,
1675 for (i = 0; i < param_size / sizeof(u32); i++) {
1676 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1677 /* Found BGR24 first - old firmware. */
1680 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1681 /* Found RGB24 first
1682 * new firmware, so use RGB24.
1684 dev->rgb_bgr_swapped = false;
1689 format = &camera->output[CAM_PORT_PREVIEW].format;
1691 format->encoding = MMAL_ENCODING_OPAQUE;
1692 format->encoding_variant = MMAL_ENCODING_I420;
1694 format->es->video.width = 1024;
1695 format->es->video.height = 768;
1696 format->es->video.crop.x = 0;
1697 format->es->video.crop.y = 0;
1698 format->es->video.crop.width = 1024;
1699 format->es->video.crop.height = 768;
1700 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1701 format->es->video.frame_rate.den = 1;
1703 format = &camera->output[CAM_PORT_VIDEO].format;
1705 format->encoding = MMAL_ENCODING_OPAQUE;
1706 format->encoding_variant = MMAL_ENCODING_I420;
1708 format->es->video.width = 1024;
1709 format->es->video.height = 768;
1710 format->es->video.crop.x = 0;
1711 format->es->video.crop.y = 0;
1712 format->es->video.crop.width = 1024;
1713 format->es->video.crop.height = 768;
1714 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1715 format->es->video.frame_rate.den = 1;
1717 format = &camera->output[CAM_PORT_CAPTURE].format;
1719 format->encoding = MMAL_ENCODING_OPAQUE;
1721 format->es->video.width = 2592;
1722 format->es->video.height = 1944;
1723 format->es->video.crop.x = 0;
1724 format->es->video.crop.y = 0;
1725 format->es->video.crop.width = 2592;
1726 format->es->video.crop.height = 1944;
1727 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1728 format->es->video.frame_rate.den = 1;
1730 dev->capture.width = format->es->video.width;
1731 dev->capture.height = format->es->video.height;
1732 dev->capture.fmt = &formats[0];
1733 dev->capture.encode_component = NULL;
1734 dev->capture.timeperframe = tpf_default;
1735 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1736 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1738 /* get the preview component ready */
1739 ret = vchiq_mmal_component_init(
1740 dev->instance, "ril.video_render",
1741 &dev->component[COMP_PREVIEW]);
1745 if (dev->component[COMP_PREVIEW]->inputs < 1) {
1747 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1748 __func__, dev->component[COMP_PREVIEW]->inputs, 1);
1752 /* get the image encoder component ready */
1753 ret = vchiq_mmal_component_init(
1754 dev->instance, "ril.image_encode",
1755 &dev->component[COMP_IMAGE_ENCODE]);
1759 if (dev->component[COMP_IMAGE_ENCODE]->inputs < 1) {
1761 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1762 __func__, dev->component[COMP_IMAGE_ENCODE]->inputs,
1764 goto unreg_image_encoder;
1767 /* get the video encoder component ready */
1768 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1769 &dev->component[COMP_VIDEO_ENCODE]);
1771 goto unreg_image_encoder;
1773 if (dev->component[COMP_VIDEO_ENCODE]->inputs < 1) {
1775 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1776 __func__, dev->component[COMP_VIDEO_ENCODE]->inputs,
1778 goto unreg_vid_encoder;
1782 struct vchiq_mmal_port *encoder_port =
1783 &dev->component[COMP_VIDEO_ENCODE]->output[0];
1784 encoder_port->format.encoding = MMAL_ENCODING_H264;
1785 ret = vchiq_mmal_port_set_format(dev->instance,
1790 unsigned int enable = 1;
1792 vchiq_mmal_port_parameter_set(
1794 &dev->component[COMP_VIDEO_ENCODE]->control,
1795 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1796 &enable, sizeof(enable));
1798 vchiq_mmal_port_parameter_set(dev->instance,
1799 &dev->component[COMP_VIDEO_ENCODE]->control,
1800 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1804 ret = bm2835_mmal_set_all_camera_controls(dev);
1806 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1808 goto unreg_vid_encoder;
1814 pr_err("Cleanup: Destroy video encoder\n");
1815 vchiq_mmal_component_finalise(
1817 dev->component[COMP_VIDEO_ENCODE]);
1819 unreg_image_encoder:
1820 pr_err("Cleanup: Destroy image encoder\n");
1821 vchiq_mmal_component_finalise(
1823 dev->component[COMP_IMAGE_ENCODE]);
1826 pr_err("Cleanup: Destroy video render\n");
1827 vchiq_mmal_component_finalise(dev->instance,
1828 dev->component[COMP_PREVIEW]);
1831 pr_err("Cleanup: Destroy camera\n");
1832 vchiq_mmal_component_finalise(dev->instance,
1833 dev->component[COMP_CAMERA]);
1836 vchiq_mmal_finalise(dev->instance);
1840 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1841 struct video_device *vfd)
1845 *vfd = vdev_template;
1847 vfd->v4l2_dev = &dev->v4l2_dev;
1849 vfd->lock = &dev->mutex;
1851 vfd->queue = &dev->capture.vb_vidq;
1853 /* video device needs to be able to access instance data */
1854 video_set_drvdata(vfd, dev);
1856 ret = video_register_device(vfd,
1858 video_nr[dev->camera_num]);
1862 v4l2_info(vfd->v4l2_dev,
1863 "V4L2 device registered as %s - stills mode > %dx%d\n",
1864 video_device_node_name(vfd),
1865 max_video_width, max_video_height);
1870 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1875 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1876 video_device_node_name(&dev->vdev));
1878 video_unregister_device(&dev->vdev);
1880 if (dev->capture.encode_component) {
1881 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1882 "mmal_exit - disconnect tunnel\n");
1883 vchiq_mmal_port_connect_tunnel(dev->instance,
1884 dev->capture.camera_port, NULL);
1885 vchiq_mmal_component_disable(dev->instance,
1886 dev->capture.encode_component);
1888 vchiq_mmal_component_disable(dev->instance,
1889 dev->component[COMP_CAMERA]);
1891 vchiq_mmal_component_finalise(dev->instance,
1892 dev->component[COMP_VIDEO_ENCODE]);
1894 vchiq_mmal_component_finalise(dev->instance,
1895 dev->component[COMP_IMAGE_ENCODE]);
1897 vchiq_mmal_component_finalise(dev->instance,
1898 dev->component[COMP_PREVIEW]);
1900 vchiq_mmal_component_finalise(dev->instance,
1901 dev->component[COMP_CAMERA]);
1903 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1905 v4l2_device_unregister(&dev->v4l2_dev);
1910 static struct v4l2_format default_v4l2_format = {
1911 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1912 .fmt.pix.width = 1024,
1913 .fmt.pix.bytesperline = 0,
1914 .fmt.pix.height = 768,
1915 .fmt.pix.sizeimage = 1024 * 768,
1918 static int bcm2835_mmal_probe(struct platform_device *pdev)
1921 struct bm2835_mmal_dev *dev;
1922 struct vb2_queue *q;
1924 unsigned int num_cameras;
1925 struct vchiq_mmal_instance *instance;
1926 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1929 ret = vchiq_mmal_init(&instance);
1933 num_cameras = get_num_cameras(instance,
1935 MAX_BCM2835_CAMERAS);
1937 if (num_cameras < 1) {
1942 if (num_cameras > MAX_BCM2835_CAMERAS)
1943 num_cameras = MAX_BCM2835_CAMERAS;
1945 for (camera = 0; camera < num_cameras; camera++) {
1946 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1952 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1953 mutex_init(&dev->mutex);
1954 dev->camera_num = camera;
1955 dev->max_width = resolutions[camera][0];
1956 dev->max_height = resolutions[camera][1];
1958 /* setup device defaults */
1959 dev->overlay.w.left = 150;
1960 dev->overlay.w.top = 50;
1961 dev->overlay.w.width = 1024;
1962 dev->overlay.w.height = 768;
1963 dev->overlay.clipcount = 0;
1964 dev->overlay.field = V4L2_FIELD_NONE;
1965 dev->overlay.global_alpha = 255;
1967 dev->capture.fmt = &formats[3]; /* JPEG */
1969 /* v4l device registration */
1970 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1971 "%s", BM2835_MMAL_MODULE_NAME);
1972 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1974 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1979 /* setup v4l controls */
1980 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1982 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1986 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1989 dev->instance = instance;
1990 ret = mmal_init(dev);
1992 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1996 /* initialize queue */
1997 q = &dev->capture.vb_vidq;
1998 memset(q, 0, sizeof(*q));
1999 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2000 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
2002 q->buf_struct_size = sizeof(struct vb2_mmal_buffer);
2003 q->ops = &bm2835_mmal_video_qops;
2004 q->mem_ops = &vb2_vmalloc_memops;
2005 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2006 q->lock = &dev->mutex;
2007 ret = vb2_queue_init(q);
2011 /* initialise video devices */
2012 ret = bm2835_mmal_init_device(dev, &dev->vdev);
2014 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
2019 /* Really want to call vidioc_s_fmt_vid_cap with the default
2020 * format, but currently the APIs don't join up.
2022 ret = mmal_setup_components(dev, &default_v4l2_format);
2024 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
2029 v4l2_info(&dev->v4l2_dev,
2030 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
2031 BM2835_MMAL_VERSION);
2038 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2039 v4l2_device_unregister(&dev->v4l2_dev);
2045 for (i = 0; i < camera; i++) {
2046 bcm2835_cleanup_instance(gdev[i]);
2051 vchiq_mmal_finalise(instance);
2056 static int bcm2835_mmal_remove(struct platform_device *pdev)
2059 struct vchiq_mmal_instance *instance = gdev[0]->instance;
2061 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2062 bcm2835_cleanup_instance(gdev[camera]);
2063 gdev[camera] = NULL;
2065 vchiq_mmal_finalise(instance);
2070 static struct platform_driver bcm2835_camera_driver = {
2071 .probe = bcm2835_mmal_probe,
2072 .remove = bcm2835_mmal_remove,
2074 .name = "bcm2835-camera",
2078 module_platform_driver(bcm2835_camera_driver)
2080 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2081 MODULE_AUTHOR("Vincent Sanders");
2082 MODULE_LICENSE("GPL");
2083 MODULE_VERSION(BM2835_MMAL_VERSION);
2084 MODULE_ALIAS("platform:bcm2835-camera");