brcm2708: update against latest rpi-3.10.y branch
[openwrt.git] / target / linux / brcm2708 / patches-3.10 / 0111-bcm2835-add-v4l2-camera-device.patch
1 From 2ba75a0486033660ce65c2dd2406d619fe73a20b Mon Sep 17 00:00:00 2001
2 From: Vincent Sanders <vincent.sanders@collabora.co.uk>
3 Date: Wed, 30 Jan 2013 12:45:18 +0000
4 Subject: [PATCH 111/174] bcm2835: add v4l2 camera device
5
6 - Supports raw YUV capture, preview, JPEG and H264.
7 - Uses videobuf2 for data transfer, using dma_buf.
8 - Uses 3.6.10 timestamping
9 - Camera power based on use
10 - Uses immutable input mode on video encoder
11
12 Signed-off-by: Daniel Stone <daniels@collabora.com>
13 Signed-off-by: Luke Diamand <luked@broadcom.com>
14 ---
15  Documentation/video4linux/bcm2835-v4l2.txt       |   60 +
16  drivers/media/platform/Kconfig                   |    1 +
17  drivers/media/platform/Makefile                  |    2 +
18  drivers/media/platform/bcm2835/Kconfig           |   25 +
19  drivers/media/platform/bcm2835/Makefile          |    5 +
20  drivers/media/platform/bcm2835/bcm2835-camera.c  | 1478 +++++++++++++++++
21  drivers/media/platform/bcm2835/bcm2835-camera.h  |  113 ++
22  drivers/media/platform/bcm2835/controls.c        |  725 ++++++++
23  drivers/media/platform/bcm2835/mmal-common.h     |   52 +
24  drivers/media/platform/bcm2835/mmal-encodings.h  |   93 ++
25  drivers/media/platform/bcm2835/mmal-msg-common.h |   50 +
26  drivers/media/platform/bcm2835/mmal-msg-format.h |   81 +
27  drivers/media/platform/bcm2835/mmal-msg-port.h   |  107 ++
28  drivers/media/platform/bcm2835/mmal-msg.h        |  404 +++++
29  drivers/media/platform/bcm2835/mmal-parameters.h |  539 ++++++
30  drivers/media/platform/bcm2835/mmal-vchiq.c      | 1916 ++++++++++++++++++++++
31  drivers/media/platform/bcm2835/mmal-vchiq.h      |  178 ++
32  17 files changed, 5829 insertions(+)
33  create mode 100644 Documentation/video4linux/bcm2835-v4l2.txt
34  create mode 100644 drivers/media/platform/bcm2835/Kconfig
35  create mode 100644 drivers/media/platform/bcm2835/Makefile
36  create mode 100644 drivers/media/platform/bcm2835/bcm2835-camera.c
37  create mode 100644 drivers/media/platform/bcm2835/bcm2835-camera.h
38  create mode 100644 drivers/media/platform/bcm2835/controls.c
39  create mode 100644 drivers/media/platform/bcm2835/mmal-common.h
40  create mode 100644 drivers/media/platform/bcm2835/mmal-encodings.h
41  create mode 100644 drivers/media/platform/bcm2835/mmal-msg-common.h
42  create mode 100644 drivers/media/platform/bcm2835/mmal-msg-format.h
43  create mode 100644 drivers/media/platform/bcm2835/mmal-msg-port.h
44  create mode 100644 drivers/media/platform/bcm2835/mmal-msg.h
45  create mode 100644 drivers/media/platform/bcm2835/mmal-parameters.h
46  create mode 100644 drivers/media/platform/bcm2835/mmal-vchiq.c
47  create mode 100644 drivers/media/platform/bcm2835/mmal-vchiq.h
48
49 --- /dev/null
50 +++ b/Documentation/video4linux/bcm2835-v4l2.txt
51 @@ -0,0 +1,60 @@
52 +
53 +BCM2835 (aka Raspberry Pi) V4L2 driver
54 +======================================
55 +
56 +1. Copyright
57 +============
58 +
59 +Copyright © 2013 Raspberry Pi (Trading) Ltd.
60 +
61 +2. License
62 +==========
63 +
64 +This program is free software; you can redistribute it and/or modify
65 +it under the terms of the GNU General Public License as published by
66 +the Free Software Foundation; either version 2 of the License, or
67 +(at your option) any later version.
68 +
69 +This program is distributed in the hope that it will be useful,
70 +but WITHOUT ANY WARRANTY; without even the implied warranty of
71 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
72 +GNU General Public License for more details.
73 +
74 +You should have received a copy of the GNU General Public License
75 +along with this program; if not, write to the Free Software
76 +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
77 +
78 +3. Quick Start
79 +==============
80 +
81 +You need a version 1.0 or later of v4l2-ctl, available from:
82 +       git://git.linuxtv.org/v4l-utils.git
83 +
84 +$ sudo modprobe bcm2835-v4l2
85 +
86 +Turn on the overlay:
87 +
88 +$ v4l2-ctl --overlay=1
89 +
90 +Turn off the overlay:
91 +
92 +$ v4l2-ctl --overlay=0
93 +
94 +Set the capture format for video:
95 +
96 +$ v4l2-ctl  --set-fmt-video=width=1920,height=1088,pixelformat=4
97 +
98 +(Note: 1088 not 1080).
99 +
100 +Capture:
101 +
102 +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
103 +
104 +Stills capture:
105 +
106 +$ v4l2-ctl  --set-fmt-video=width=2592,height=1944,pixelformat=3
107 +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
108 +
109 +List of available formats:
110 +
111 +$ v4l2-ctl --list-formats
112 --- a/drivers/media/platform/Kconfig
113 +++ b/drivers/media/platform/Kconfig
114 @@ -124,6 +124,7 @@ config VIDEO_S3C_CAMIF
115  source "drivers/media/platform/soc_camera/Kconfig"
116  source "drivers/media/platform/exynos4-is/Kconfig"
117  source "drivers/media/platform/s5p-tv/Kconfig"
118 +source "drivers/media/platform/bcm2835/Kconfig"
119  
120  endif # V4L_PLATFORM_DRIVERS
121  
122 --- a/drivers/media/platform/Makefile
123 +++ b/drivers/media/platform/Makefile
124 @@ -50,4 +50,6 @@ obj-y += davinci/
125  
126  obj-$(CONFIG_ARCH_OMAP)        += omap/
127  
128 +obj-$(CONFIG_VIDEO_BCM2835)            += bcm2835/
129 +
130  ccflags-y += -I$(srctree)/drivers/media/i2c
131 --- /dev/null
132 +++ b/drivers/media/platform/bcm2835/Kconfig
133 @@ -0,0 +1,25 @@
134 +# Broadcom VideoCore IV v4l2 camera support
135 +
136 +config VIDEO_BCM2835
137 +       bool "Broadcom BCM2835 camera interface driver"
138 +       depends on VIDEO_V4L2 && ARCH_BCM2708
139 +       ---help---
140 +         Say Y here to enable camera host interface devices for
141 +         Broadcom BCM2835 SoC. This operates over the VCHIQ interface
142 +         to a service running on VideoCore.
143 +
144 +
145 +if VIDEO_BCM2835
146 +
147 +config VIDEO_BCM2835_MMAL
148 +       tristate "Broadcom BM2835 MMAL camera interface driver"
149 +       depends on BCM2708_VCHIQ
150 +       select VIDEOBUF2_VMALLOC
151 +       ---help---
152 +         This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
153 +
154 +         To compile this driver as a module, choose M here: the
155 +         module will be called bcm2835-v4l2.o
156 +
157 +
158 +endif # VIDEO_BM2835
159 --- /dev/null
160 +++ b/drivers/media/platform/bcm2835/Makefile
161 @@ -0,0 +1,5 @@
162 +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
163 +
164 +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
165 +
166 +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
167 --- /dev/null
168 +++ b/drivers/media/platform/bcm2835/bcm2835-camera.c
169 @@ -0,0 +1,1478 @@
170 +/*
171 + * Broadcom BM2835 V4L2 driver
172 + *
173 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
174 + *
175 + * This file is subject to the terms and conditions of the GNU General Public
176 + * License.  See the file COPYING in the main directory of this archive
177 + * for more details.
178 + *
179 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
180 + *          Dave Stevenson <dsteve@broadcom.com>
181 + *          Simon Mellor <simellor@broadcom.com>
182 + *          Luke Diamand <luked@broadcom.com>
183 + */
184 +
185 +#include <linux/errno.h>
186 +#include <linux/kernel.h>
187 +#include <linux/module.h>
188 +#include <linux/slab.h>
189 +#include <media/videobuf2-vmalloc.h>
190 +#include <media/videobuf2-dma-contig.h>
191 +#include <media/v4l2-device.h>
192 +#include <media/v4l2-ioctl.h>
193 +#include <media/v4l2-ctrls.h>
194 +#include <media/v4l2-fh.h>
195 +#include <media/v4l2-event.h>
196 +#include <media/v4l2-common.h>
197 +#include <linux/delay.h>
198 +
199 +#include "mmal-common.h"
200 +#include "mmal-encodings.h"
201 +#include "mmal-vchiq.h"
202 +#include "mmal-msg.h"
203 +#include "mmal-parameters.h"
204 +#include "bcm2835-camera.h"
205 +
206 +#define BM2835_MMAL_VERSION "0.0.2"
207 +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
208 +
209 +#define MAX_WIDTH 2592
210 +#define MAX_HEIGHT 1944
211 +#define MIN_BUFFER_SIZE (80*1024)
212 +
213 +#define MAX_VIDEO_MODE_WIDTH 1280
214 +#define MAX_VIDEO_MODE_HEIGHT 720
215 +
216 +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
217 +MODULE_AUTHOR("Vincent Sanders");
218 +MODULE_LICENSE("GPL");
219 +MODULE_VERSION(BM2835_MMAL_VERSION);
220 +
221 +int bcm2835_v4l2_debug;
222 +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
223 +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
224 +
225 +static struct bm2835_mmal_dev *gdev;   /* global device data */
226 +
227 +/* video formats */
228 +static struct mmal_fmt formats[] = {
229 +       {
230 +        .name = "4:2:0, packed YUV",
231 +        .fourcc = V4L2_PIX_FMT_YUV420,
232 +        .mmal = MMAL_ENCODING_I420,
233 +        .depth = 12,
234 +        .mmal_component = MMAL_COMPONENT_CAMERA,
235 +        },
236 +       {
237 +        .name = "4:2:2, packed, YUYV",
238 +        .fourcc = V4L2_PIX_FMT_YUYV,
239 +        .mmal = MMAL_ENCODING_YUYV,
240 +        .depth = 16,
241 +        .mmal_component = MMAL_COMPONENT_CAMERA,
242 +        },
243 +       {
244 +        .name = "RGB24 (BE)",
245 +        .fourcc = V4L2_PIX_FMT_BGR24,
246 +        .mmal = MMAL_ENCODING_BGR24,
247 +        .depth = 24,
248 +        .mmal_component = MMAL_COMPONENT_CAMERA,
249 +        },
250 +       {
251 +        .name = "JPEG",
252 +        .fourcc = V4L2_PIX_FMT_JPEG,
253 +        .mmal = MMAL_ENCODING_JPEG,
254 +        .depth = 8,
255 +        .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
256 +        },
257 +       {
258 +        .name = "H264",
259 +        .fourcc = V4L2_PIX_FMT_H264,
260 +        .mmal = MMAL_ENCODING_H264,
261 +        .depth = 8,
262 +        .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
263 +        }
264 +};
265 +
266 +static struct mmal_fmt *get_format(struct v4l2_format *f)
267 +{
268 +       struct mmal_fmt *fmt;
269 +       unsigned int k;
270 +
271 +       for (k = 0; k < ARRAY_SIZE(formats); k++) {
272 +               fmt = &formats[k];
273 +               if (fmt->fourcc == f->fmt.pix.pixelformat)
274 +                       break;
275 +       }
276 +
277 +       if (k == ARRAY_SIZE(formats))
278 +               return NULL;
279 +
280 +       return &formats[k];
281 +}
282 +
283 +/* ------------------------------------------------------------------
284 +       Videobuf queue operations
285 +   ------------------------------------------------------------------*/
286 +
287 +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
288 +                      unsigned int *nbuffers, unsigned int *nplanes,
289 +                      unsigned int sizes[], void *alloc_ctxs[])
290 +{
291 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
292 +       unsigned long size;
293 +
294 +       /* refuse queue setup if port is not configured */
295 +       if (dev->capture.port == NULL) {
296 +               v4l2_err(&dev->v4l2_dev,
297 +                        "%s: capture port not configured\n", __func__);
298 +               return -EINVAL;
299 +       }
300 +
301 +       size = dev->capture.port->current_buffer.size;
302 +       if (size == 0) {
303 +               v4l2_err(&dev->v4l2_dev,
304 +                        "%s: capture port buffer size is zero\n", __func__);
305 +               return -EINVAL;
306 +       }
307 +
308 +       if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
309 +               *nbuffers = (dev->capture.port->current_buffer.num + 2);
310 +
311 +       *nplanes = 1;
312 +
313 +       sizes[0] = size;
314 +
315 +       /*
316 +        * videobuf2-vmalloc allocator is context-less so no need to set
317 +        * alloc_ctxs array.
318 +        */
319 +
320 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
321 +                __func__, dev);
322 +
323 +       return 0;
324 +}
325 +
326 +static int buffer_prepare(struct vb2_buffer *vb)
327 +{
328 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
329 +       unsigned long size;
330 +
331 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
332 +                __func__, dev);
333 +
334 +       BUG_ON(dev->capture.port == NULL);
335 +       BUG_ON(dev->capture.fmt == NULL);
336 +
337 +       size = dev->capture.stride * dev->capture.height;
338 +       if (vb2_plane_size(vb, 0) < size) {
339 +               v4l2_err(&dev->v4l2_dev,
340 +                        "%s data will not fit into plane (%lu < %lu)\n",
341 +                        __func__, vb2_plane_size(vb, 0), size);
342 +               return -EINVAL;
343 +       }
344 +
345 +       return 0;
346 +}
347 +
348 +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
349 +{
350 +       return dev->capture.camera_port ==
351 +           &dev->
352 +           component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
353 +}
354 +
355 +static void buffer_cb(struct vchiq_mmal_instance *instance,
356 +                     struct vchiq_mmal_port *port,
357 +                     int status,
358 +                     struct mmal_buffer *buf,
359 +                     unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
360 +{
361 +       struct bm2835_mmal_dev *dev = port->cb_ctx;
362 +
363 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
364 +                "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
365 +                __func__, status, buf, length, mmal_flags, pts);
366 +
367 +       if (status != 0) {
368 +               /* error in transfer */
369 +               if (buf != NULL) {
370 +                       /* there was a buffer with the error so return it */
371 +                       vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
372 +               }
373 +               return;
374 +       } else if (length == 0) {
375 +               /* stream ended */
376 +               if (buf != NULL) {
377 +                       /* this should only ever happen if the port is
378 +                        * disabled and there are buffers still queued
379 +                        */
380 +                       vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
381 +                       pr_debug("Empty buffer");
382 +               } else if (dev->capture.frame_count) {
383 +                       /* grab another frame */
384 +                       if (is_capturing(dev)) {
385 +                               pr_debug("Grab another frame");
386 +                               vchiq_mmal_port_parameter_set(
387 +                                       instance,
388 +                                       dev->capture.
389 +                                       camera_port,
390 +                                       MMAL_PARAMETER_CAPTURE,
391 +                                       &dev->capture.
392 +                                       frame_count,
393 +                                       sizeof(dev->capture.frame_count));
394 +                       }
395 +               } else {
396 +                       /* signal frame completion */
397 +                       complete(&dev->capture.frame_cmplt);
398 +               }
399 +       } else {
400 +               if (dev->capture.frame_count) {
401 +                       if (dev->capture.vc_start_timestamp != -1) {
402 +                               s64 runtime_us = pts -
403 +                                   dev->capture.vc_start_timestamp;
404 +                               u32 div = 0;
405 +                               u32 rem = 0;
406 +
407 +                               div =
408 +                                   div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
409 +                               buf->vb.v4l2_buf.timestamp.tv_sec =
410 +                                   dev->capture.kernel_start_ts.tv_sec - 1 +
411 +                                   div;
412 +                               buf->vb.v4l2_buf.timestamp.tv_usec =
413 +                                   dev->capture.kernel_start_ts.tv_usec + rem;
414 +
415 +                               if (buf->vb.v4l2_buf.timestamp.tv_usec >=
416 +                                   USEC_PER_SEC) {
417 +                                       buf->vb.v4l2_buf.timestamp.tv_sec++;
418 +                                       buf->vb.v4l2_buf.timestamp.tv_usec -=
419 +                                           USEC_PER_SEC;
420 +                               }
421 +                               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
422 +                                        "Convert start time %d.%06d and %llu"
423 +                                        "with offset %llu to %d.%06d\n",
424 +                                        (int)dev->capture.kernel_start_ts.
425 +                                        tv_sec,
426 +                                        (int)dev->capture.kernel_start_ts.
427 +                                        tv_usec,
428 +                                        dev->capture.vc_start_timestamp, pts,
429 +                                        (int)buf->vb.v4l2_buf.timestamp.tv_sec,
430 +                                        (int)buf->vb.v4l2_buf.timestamp.
431 +                                        tv_usec);
432 +                       } else {
433 +                               v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
434 +                       }
435 +
436 +                       vb2_set_plane_payload(&buf->vb, 0, length);
437 +                       vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
438 +
439 +                       if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
440 +                           is_capturing(dev)) {
441 +                               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
442 +                                        "Grab another frame as buffer has EOS");
443 +                               vchiq_mmal_port_parameter_set(
444 +                                       instance,
445 +                                       dev->capture.
446 +                                       camera_port,
447 +                                       MMAL_PARAMETER_CAPTURE,
448 +                                       &dev->capture.
449 +                                       frame_count,
450 +                                       sizeof(dev->capture.frame_count));
451 +                       }
452 +               } else {
453 +                       /* signal frame completion */
454 +                       complete(&dev->capture.frame_cmplt);
455 +               }
456 +       }
457 +}
458 +
459 +static int enable_camera(struct bm2835_mmal_dev *dev)
460 +{
461 +       int ret;
462 +       if (!dev->camera_use_count) {
463 +               ret = vchiq_mmal_component_enable(
464 +                               dev->instance,
465 +                               dev->component[MMAL_COMPONENT_CAMERA]);
466 +               if (ret < 0) {
467 +                       v4l2_err(&dev->v4l2_dev,
468 +                                "Failed enabling camera, ret %d\n", ret);
469 +                       return -EINVAL;
470 +               }
471 +       }
472 +       dev->camera_use_count++;
473 +       v4l2_dbg(1, bcm2835_v4l2_debug,
474 +                &dev->v4l2_dev, "enabled camera (refcount %d)\n",
475 +                       dev->camera_use_count);
476 +       return 0;
477 +}
478 +
479 +static int disable_camera(struct bm2835_mmal_dev *dev)
480 +{
481 +       int ret;
482 +       if (!dev->camera_use_count) {
483 +               v4l2_err(&dev->v4l2_dev,
484 +                        "Disabled the camera when already disabled\n");
485 +               return -EINVAL;
486 +       }
487 +       dev->camera_use_count--;
488 +       if (!dev->camera_use_count) {
489 +               unsigned int i = 0xFFFFFFFF;
490 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
491 +                        "Disabling camera\n");
492 +               ret =
493 +                   vchiq_mmal_component_disable(
494 +                               dev->instance,
495 +                               dev->component[MMAL_COMPONENT_CAMERA]);
496 +               if (ret < 0) {
497 +                       v4l2_err(&dev->v4l2_dev,
498 +                                "Failed disabling camera, ret %d\n", ret);
499 +                       return -EINVAL;
500 +               }
501 +               vchiq_mmal_port_parameter_set(
502 +                       dev->instance,
503 +                       &dev->component[MMAL_COMPONENT_CAMERA]->control,
504 +                       MMAL_PARAMETER_CAMERA_NUM, &i,
505 +                       sizeof(i));
506 +       }
507 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
508 +                "Camera refcount now %d\n", dev->camera_use_count);
509 +       return 0;
510 +}
511 +
512 +static void buffer_queue(struct vb2_buffer *vb)
513 +{
514 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
515 +       struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
516 +       int ret;
517 +
518 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
519 +                "%s: dev:%p buf:%p\n", __func__, dev, buf);
520 +
521 +       buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
522 +       buf->buffer_size = vb2_plane_size(&buf->vb, 0);
523 +
524 +       ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
525 +       if (ret < 0)
526 +               v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
527 +                        __func__);
528 +}
529 +
530 +static int start_streaming(struct vb2_queue *vq, unsigned int count)
531 +{
532 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
533 +       int ret;
534 +       int parameter_size;
535 +
536 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
537 +                __func__, dev);
538 +
539 +       /* ensure a format has actually been set */
540 +       if (dev->capture.port == NULL)
541 +               return -EINVAL;
542 +
543 +       if (enable_camera(dev) < 0) {
544 +               v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
545 +               return -EINVAL;
546 +       }
547 +
548 +       /*init_completion(&dev->capture.frame_cmplt); */
549 +
550 +       /* enable frame capture */
551 +       dev->capture.frame_count = 1;
552 +
553 +       /* if the preview is not already running, wait for a few frames for AGC
554 +        * to settle down.
555 +        */
556 +       if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
557 +               msleep(300);
558 +
559 +       /* enable the connection from camera to encoder (if applicable) */
560 +       if (dev->capture.camera_port != dev->capture.port
561 +           && dev->capture.camera_port) {
562 +               ret = vchiq_mmal_port_enable(dev->instance,
563 +                                            dev->capture.camera_port, NULL);
564 +               if (ret) {
565 +                       v4l2_err(&dev->v4l2_dev,
566 +                                "Failed to enable encode tunnel - error %d\n",
567 +                                ret);
568 +                       return -1;
569 +               }
570 +       }
571 +
572 +       /* Get VC timestamp at this point in time */
573 +       parameter_size = sizeof(dev->capture.vc_start_timestamp);
574 +       if (vchiq_mmal_port_parameter_get(dev->instance,
575 +                                         dev->capture.camera_port,
576 +                                         MMAL_PARAMETER_SYSTEM_TIME,
577 +                                         &dev->capture.vc_start_timestamp,
578 +                                         &parameter_size)) {
579 +               v4l2_err(&dev->v4l2_dev,
580 +                        "Failed to get VC start time - update your VC f/w\n");
581 +
582 +               /* Flag to indicate just to rely on kernel timestamps */
583 +               dev->capture.vc_start_timestamp = -1;
584 +       } else
585 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
586 +                        "Start time %lld size %d\n",
587 +                        dev->capture.vc_start_timestamp, parameter_size);
588 +
589 +       v4l2_get_timestamp(&dev->capture.kernel_start_ts);
590 +
591 +       /* enable the camera port */
592 +       dev->capture.port->cb_ctx = dev;
593 +       ret =
594 +           vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
595 +       if (ret) {
596 +               v4l2_err(&dev->v4l2_dev,
597 +                        "Failed to enable capture port - error %d\n", ret);
598 +               return -1;
599 +       }
600 +
601 +       /* capture the first frame */
602 +       vchiq_mmal_port_parameter_set(dev->instance,
603 +                                     dev->capture.camera_port,
604 +                                     MMAL_PARAMETER_CAPTURE,
605 +                                     &dev->capture.frame_count,
606 +                                     sizeof(dev->capture.frame_count));
607 +       return 0;
608 +}
609 +
610 +/* abort streaming and wait for last buffer */
611 +static int stop_streaming(struct vb2_queue *vq)
612 +{
613 +       int ret;
614 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
615 +
616 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
617 +                __func__, dev);
618 +
619 +       init_completion(&dev->capture.frame_cmplt);
620 +       dev->capture.frame_count = 0;
621 +
622 +       /* ensure a format has actually been set */
623 +       if (dev->capture.port == NULL)
624 +               return -EINVAL;
625 +
626 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
627 +
628 +       /* stop capturing frames */
629 +       vchiq_mmal_port_parameter_set(dev->instance,
630 +                                     dev->capture.camera_port,
631 +                                     MMAL_PARAMETER_CAPTURE,
632 +                                     &dev->capture.frame_count,
633 +                                     sizeof(dev->capture.frame_count));
634 +
635 +       /* wait for last frame to complete */
636 +       ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
637 +       if (ret <= 0)
638 +               v4l2_err(&dev->v4l2_dev,
639 +                        "error %d waiting for frame completion\n", ret);
640 +
641 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
642 +                "disabling connection\n");
643 +
644 +       /* disable the connection from camera to encoder */
645 +       ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
646 +       if (!ret && dev->capture.camera_port != dev->capture.port) {
647 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
648 +                        "disabling port\n");
649 +               ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
650 +       } else if (dev->capture.camera_port != dev->capture.port) {
651 +               v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
652 +                        ret);
653 +       }
654 +
655 +       if (disable_camera(dev) < 0) {
656 +               v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
657 +               return -EINVAL;
658 +       }
659 +
660 +       return ret;
661 +}
662 +
663 +static void bm2835_mmal_lock(struct vb2_queue *vq)
664 +{
665 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
666 +       mutex_lock(&dev->mutex);
667 +}
668 +
669 +static void bm2835_mmal_unlock(struct vb2_queue *vq)
670 +{
671 +       struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
672 +       mutex_unlock(&dev->mutex);
673 +}
674 +
675 +static struct vb2_ops bm2835_mmal_video_qops = {
676 +       .queue_setup = queue_setup,
677 +       .buf_prepare = buffer_prepare,
678 +       .buf_queue = buffer_queue,
679 +       .start_streaming = start_streaming,
680 +       .stop_streaming = stop_streaming,
681 +       .wait_prepare = bm2835_mmal_unlock,
682 +       .wait_finish = bm2835_mmal_lock,
683 +};
684 +
685 +/* ------------------------------------------------------------------
686 +       IOCTL operations
687 +   ------------------------------------------------------------------*/
688 +
689 +/* overlay ioctl */
690 +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
691 +                                      struct v4l2_fmtdesc *f)
692 +{
693 +       struct mmal_fmt *fmt;
694 +
695 +       if (f->index >= ARRAY_SIZE(formats))
696 +               return -EINVAL;
697 +
698 +       fmt = &formats[f->index];
699 +
700 +       strlcpy(f->description, fmt->name, sizeof(f->description));
701 +       f->pixelformat = fmt->fourcc;
702 +
703 +       return 0;
704 +}
705 +
706 +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
707 +                                   struct v4l2_format *f)
708 +{
709 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
710 +
711 +       f->fmt.win = dev->overlay;
712 +
713 +       return 0;
714 +}
715 +
716 +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
717 +                                     struct v4l2_format *f)
718 +{
719 +       /* Only support one format so get the current one. */
720 +       vidioc_g_fmt_vid_overlay(file, priv, f);
721 +
722 +       /* todo: allow the size and/or offset to be changed. */
723 +       return 0;
724 +}
725 +
726 +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
727 +                                   struct v4l2_format *f)
728 +{
729 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
730 +
731 +       vidioc_try_fmt_vid_overlay(file, priv, f);
732 +
733 +       dev->overlay = f->fmt.win;
734 +
735 +       /* todo: program the preview port parameters */
736 +       return 0;
737 +}
738 +
739 +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
740 +{
741 +       int ret;
742 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
743 +       struct vchiq_mmal_port *src;
744 +       struct vchiq_mmal_port *dst;
745 +       struct mmal_parameter_displayregion prev_config = {
746 +               .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
747 +                   MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
748 +               .layer = PREVIEW_LAYER,
749 +               .alpha = 255,
750 +               .fullscreen = 0,
751 +               .dest_rect = {
752 +                             .x = dev->overlay.w.left,
753 +                             .y = dev->overlay.w.top,
754 +                             .width = dev->overlay.w.width,
755 +                             .height = dev->overlay.w.height,
756 +                             },
757 +       };
758 +
759 +       if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
760 +           (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
761 +               return 0;       /* already in requested state */
762 +
763 +       src =
764 +           &dev->component[MMAL_COMPONENT_CAMERA]->
765 +           output[MMAL_CAMERA_PORT_PREVIEW];
766 +
767 +       if (!on) {
768 +               /* disconnect preview ports and disable component */
769 +               ret = vchiq_mmal_port_disable(dev->instance, src);
770 +               if (!ret)
771 +                       ret =
772 +                           vchiq_mmal_port_connect_tunnel(dev->instance, src,
773 +                                                          NULL);
774 +               if (ret >= 0)
775 +                       ret = vchiq_mmal_component_disable(
776 +                                       dev->instance,
777 +                                       dev->component[MMAL_COMPONENT_PREVIEW]);
778 +
779 +               disable_camera(dev);
780 +               return ret;
781 +       }
782 +
783 +       /* set preview port format and connect it to output */
784 +       dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
785 +
786 +       ret = vchiq_mmal_port_set_format(dev->instance, src);
787 +       if (ret < 0)
788 +               goto error;
789 +
790 +       ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
791 +                                           MMAL_PARAMETER_DISPLAYREGION,
792 +                                           &prev_config, sizeof(prev_config));
793 +       if (ret < 0)
794 +               goto error;
795 +
796 +       if (enable_camera(dev) < 0)
797 +               goto error;
798 +
799 +       ret = vchiq_mmal_component_enable(
800 +                       dev->instance,
801 +                       dev->component[MMAL_COMPONENT_PREVIEW]);
802 +       if (ret < 0)
803 +               goto error;
804 +
805 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
806 +                src, dst);
807 +       ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
808 +       if (!ret)
809 +               ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
810 +error:
811 +       return ret;
812 +}
813 +
814 +static int vidioc_g_fbuf(struct file *file, void *fh,
815 +                        struct v4l2_framebuffer *a)
816 +{
817 +       /* The video overlay must stay within the framebuffer and can't be
818 +          positioned independently. */
819 +       a->flags = V4L2_FBUF_FLAG_OVERLAY;
820 +
821 +       /* todo: v4l2_framebuffer still needs more info filling in
822 +        *       in order to pass the v4l2-compliance test. */
823 +
824 +       return 0;
825 +}
826 +
827 +/* input ioctls */
828 +static int vidioc_enum_input(struct file *file, void *priv,
829 +                            struct v4l2_input *inp)
830 +{
831 +       /* only a single camera input */
832 +       if (inp->index != 0)
833 +               return -EINVAL;
834 +
835 +       inp->type = V4L2_INPUT_TYPE_CAMERA;
836 +       sprintf(inp->name, "Camera %u", inp->index);
837 +       return 0;
838 +}
839 +
840 +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
841 +{
842 +       *i = 0;
843 +       return 0;
844 +}
845 +
846 +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
847 +{
848 +       if (i != 0)
849 +               return -EINVAL;
850 +
851 +       return 0;
852 +}
853 +
854 +/* capture ioctls */
855 +static int vidioc_querycap(struct file *file, void *priv,
856 +                          struct v4l2_capability *cap)
857 +{
858 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
859 +       u32 major;
860 +       u32 minor;
861 +
862 +       vchiq_mmal_version(dev->instance, &major, &minor);
863 +
864 +       strcpy(cap->driver, "bm2835 mmal");
865 +       snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
866 +                major, minor);
867 +
868 +       snprintf(cap->bus_info, sizeof(cap->bus_info),
869 +                "platform:%s", dev->v4l2_dev.name);
870 +       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
871 +           V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
872 +       cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
873 +
874 +       return 0;
875 +}
876 +
877 +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
878 +                                  struct v4l2_fmtdesc *f)
879 +{
880 +       struct mmal_fmt *fmt;
881 +
882 +       if (f->index >= ARRAY_SIZE(formats))
883 +               return -EINVAL;
884 +
885 +       fmt = &formats[f->index];
886 +
887 +       strlcpy(f->description, fmt->name, sizeof(f->description));
888 +       f->pixelformat = fmt->fourcc;
889 +       return 0;
890 +}
891 +
892 +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
893 +                               struct v4l2_format *f)
894 +{
895 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
896 +
897 +       f->fmt.pix.width = dev->capture.width;
898 +       f->fmt.pix.height = dev->capture.height;
899 +       f->fmt.pix.field = V4L2_FIELD_NONE;
900 +       f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
901 +       f->fmt.pix.bytesperline =
902 +           (f->fmt.pix.width * dev->capture.fmt->depth) >> 3;
903 +       f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
904 +       if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG
905 +           && f->fmt.pix.sizeimage < (100 << 10)) {
906 +               /* Need a minimum size for JPEG to account for EXIF. */
907 +               f->fmt.pix.sizeimage = (100 << 10);
908 +       }
909 +
910 +       if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_YUYV ||
911 +           dev->capture.fmt->fourcc == V4L2_PIX_FMT_UYVY)
912 +               f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
913 +       else
914 +               f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
915 +       f->fmt.pix.priv = 0;
916 +
917 +       v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
918 +                            __func__);
919 +       return 0;
920 +}
921 +
922 +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
923 +                                 struct v4l2_format *f)
924 +{
925 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
926 +       struct mmal_fmt *mfmt;
927 +
928 +       mfmt = get_format(f);
929 +       if (!mfmt) {
930 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
931 +                        "Fourcc format (0x%08x) unknown.\n",
932 +                        f->fmt.pix.pixelformat);
933 +               f->fmt.pix.pixelformat = formats[0].fourcc;
934 +               mfmt = get_format(f);
935 +       }
936 +
937 +       f->fmt.pix.field = V4L2_FIELD_NONE;
938 +       /* image must be a multiple of 32 pixels wide and 16 lines high */
939 +       v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 5,
940 +                             &f->fmt.pix.height, 32, MAX_HEIGHT, 4, 0);
941 +       f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth) >> 3;
942 +       f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
943 +       if (f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
944 +               f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
945 +
946 +       if (mfmt->fourcc == V4L2_PIX_FMT_YUYV ||
947 +           mfmt->fourcc == V4L2_PIX_FMT_UYVY)
948 +               f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
949 +       else
950 +               f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
951 +       f->fmt.pix.priv = 0;
952 +
953 +       v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
954 +                            __func__);
955 +       return 0;
956 +}
957 +
958 +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
959 +                                struct v4l2_format *f)
960 +{
961 +       int ret;
962 +       struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
963 +       struct vchiq_mmal_component *encode_component = NULL;
964 +       struct mmal_fmt *mfmt = get_format(f);
965 +
966 +       BUG_ON(!mfmt);
967 +
968 +       if (dev->capture.encode_component) {
969 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
970 +                        "vid_cap - disconnect previous tunnel\n");
971 +
972 +               /* Disconnect any previous connection */
973 +               vchiq_mmal_port_connect_tunnel(dev->instance,
974 +                                              dev->capture.camera_port, NULL);
975 +               dev->capture.camera_port = NULL;
976 +               ret = vchiq_mmal_component_disable(dev->instance,
977 +                                                  dev->capture.
978 +                                                  encode_component);
979 +               if (ret)
980 +                       v4l2_err(&dev->v4l2_dev,
981 +                                "Failed to disable encode component %d\n",
982 +                                ret);
983 +
984 +               dev->capture.encode_component = NULL;
985 +       }
986 +       /* format dependant port setup */
987 +       switch (mfmt->mmal_component) {
988 +       case MMAL_COMPONENT_CAMERA:
989 +               /* Make a further decision on port based on resolution */
990 +               if (f->fmt.pix.width <= MAX_VIDEO_MODE_WIDTH
991 +                   && f->fmt.pix.height <= MAX_VIDEO_MODE_HEIGHT)
992 +                       camera_port = port =
993 +                           &dev->component[MMAL_COMPONENT_CAMERA]->
994 +                           output[MMAL_CAMERA_PORT_VIDEO];
995 +               else
996 +                       camera_port = port =
997 +                           &dev->component[MMAL_COMPONENT_CAMERA]->
998 +                           output[MMAL_CAMERA_PORT_CAPTURE];
999 +               break;
1000 +       case MMAL_COMPONENT_IMAGE_ENCODE:
1001 +               encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1002 +               port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1003 +               camera_port =
1004 +                   &dev->component[MMAL_COMPONENT_CAMERA]->
1005 +                   output[MMAL_CAMERA_PORT_CAPTURE];
1006 +               break;
1007 +       case MMAL_COMPONENT_VIDEO_ENCODE:
1008 +               encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1009 +               port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1010 +               camera_port =
1011 +                   &dev->component[MMAL_COMPONENT_CAMERA]->
1012 +                   output[MMAL_CAMERA_PORT_VIDEO];
1013 +               break;
1014 +       default:
1015 +               break;
1016 +       }
1017 +
1018 +       if (!port)
1019 +               return -EINVAL;
1020 +
1021 +       if (encode_component)
1022 +               camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1023 +       else
1024 +               camera_port->format.encoding = mfmt->mmal;
1025 +
1026 +       camera_port->format.encoding_variant = 0;
1027 +       camera_port->es.video.width = f->fmt.pix.width;
1028 +       camera_port->es.video.height = f->fmt.pix.height;
1029 +       camera_port->es.video.crop.x = 0;
1030 +       camera_port->es.video.crop.y = 0;
1031 +       camera_port->es.video.crop.width = f->fmt.pix.width;
1032 +       camera_port->es.video.crop.height = f->fmt.pix.height;
1033 +       camera_port->es.video.frame_rate.num = 30;
1034 +       camera_port->es.video.frame_rate.den = 1;
1035 +
1036 +       ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1037 +
1038 +       if (!ret
1039 +           && camera_port ==
1040 +           &dev->component[MMAL_COMPONENT_CAMERA]->
1041 +           output[MMAL_CAMERA_PORT_VIDEO]) {
1042 +               bool overlay_enabled =
1043 +                   !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1044 +               struct vchiq_mmal_port *preview_port =
1045 +                   &dev->component[MMAL_COMPONENT_CAMERA]->
1046 +                   output[MMAL_CAMERA_PORT_PREVIEW];
1047 +               /* Preview and encode ports need to match on resolution */
1048 +               if (overlay_enabled) {
1049 +                       /* Need to disable the overlay before we can update
1050 +                        * the resolution
1051 +                        */
1052 +                       ret =
1053 +                           vchiq_mmal_port_disable(dev->instance,
1054 +                                                   preview_port);
1055 +                       if (!ret)
1056 +                               ret =
1057 +                                   vchiq_mmal_port_connect_tunnel(
1058 +                                               dev->instance,
1059 +                                               preview_port,
1060 +                                               NULL);
1061 +               }
1062 +               preview_port->es.video.width = f->fmt.pix.width;
1063 +               preview_port->es.video.height = f->fmt.pix.height;
1064 +               preview_port->es.video.crop.x = 0;
1065 +               preview_port->es.video.crop.y = 0;
1066 +               preview_port->es.video.crop.width = f->fmt.pix.width;
1067 +               preview_port->es.video.crop.height = f->fmt.pix.height;
1068 +               preview_port->es.video.frame_rate.num = 30;
1069 +               preview_port->es.video.frame_rate.den = 1;
1070 +               ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1071 +               if (overlay_enabled) {
1072 +                       ret = vchiq_mmal_port_connect_tunnel(
1073 +                               dev->instance,
1074 +                               preview_port,
1075 +                               &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1076 +                       if (!ret)
1077 +                               ret = vchiq_mmal_port_enable(dev->instance,
1078 +                                                            preview_port,
1079 +                                                            NULL);
1080 +               }
1081 +       }
1082 +
1083 +       if (ret) {
1084 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1085 +                        "%s failed to set format\n", __func__);
1086 +               /* ensure capture is not going to be tried */
1087 +               dev->capture.port = NULL;
1088 +       } else {
1089 +               if (encode_component) {
1090 +                       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1091 +                                "vid_cap - set up encode comp\n");
1092 +
1093 +                       /* configure buffering */
1094 +                       camera_port->current_buffer.size =
1095 +                           camera_port->recommended_buffer.size;
1096 +                       camera_port->current_buffer.num =
1097 +                           camera_port->recommended_buffer.num;
1098 +
1099 +                       port->format.encoding = mfmt->mmal;
1100 +                       port->format.encoding_variant = 0;
1101 +                       /* Set any encoding specific parameters */
1102 +                       switch (mfmt->mmal_component) {
1103 +                       case MMAL_COMPONENT_VIDEO_ENCODE:
1104 +                               port->format.bitrate =
1105 +                                   dev->capture.encode_bitrate;
1106 +                               break;
1107 +                       case MMAL_COMPONENT_IMAGE_ENCODE:
1108 +                               /* Could set EXIF parameters here */
1109 +                               break;
1110 +                       default:
1111 +                               break;
1112 +                       }
1113 +                       ret = vchiq_mmal_port_set_format(dev->instance, port);
1114 +
1115 +                       if (ret) {
1116 +                               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1117 +                                        "%s failed to set format\n", __func__);
1118 +                       } else {
1119 +                               ret = vchiq_mmal_component_enable(
1120 +                                               dev->instance,
1121 +                                               encode_component);
1122 +                               if (ret) {
1123 +                                       v4l2_dbg(1, bcm2835_v4l2_debug,
1124 +                                                &dev->v4l2_dev,
1125 +                                                "%s Failed to enable encode components\n",
1126 +                                                __func__);
1127 +                               } else {
1128 +                                       /* configure buffering */
1129 +                                       port->current_buffer.num = 1;
1130 +                                       port->current_buffer.size =
1131 +                                           f->fmt.pix.sizeimage;
1132 +                                       if (port->format.encoding ==
1133 +                                           MMAL_ENCODING_JPEG) {
1134 +                                               v4l2_dbg(1, bcm2835_v4l2_debug,
1135 +                                                        &dev->v4l2_dev,
1136 +                                                        "JPEG - fiddle buffer size\n");
1137 +                                               port->current_buffer.size =
1138 +                                                   (f->fmt.pix.sizeimage <
1139 +                                                    (100 << 10))
1140 +                                                   ? (100 << 10) : f->fmt.pix.
1141 +                                                   sizeimage;
1142 +                                       }
1143 +                                       v4l2_dbg(1, bcm2835_v4l2_debug,
1144 +                                                &dev->v4l2_dev,
1145 +                                                "vid_cap - current_buffer.size being set to %d\n",
1146 +                                                f->fmt.pix.sizeimage);
1147 +                                       port->current_buffer.alignment = 0;
1148 +                                       ret =
1149 +                                           vchiq_mmal_port_connect_tunnel(
1150 +                                                       dev->instance,
1151 +                                                       camera_port,
1152 +                                                       &encode_component->input[0]);
1153 +                                       if (ret) {
1154 +                                               v4l2_dbg(1, bcm2835_v4l2_debug,
1155 +                                                        &dev->v4l2_dev,
1156 +                                                        "%s failed to create connection\n",
1157 +                                                        __func__);
1158 +                                               /* ensure capture is not going to be tried */
1159 +                                               dev->capture.port = NULL;
1160 +                                       }
1161 +                               }
1162 +                       }
1163 +               } else {
1164 +                       /* configure buffering */
1165 +                       camera_port->current_buffer.num = 1;
1166 +                       camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1167 +                       camera_port->current_buffer.alignment = 0;
1168 +               }
1169 +
1170 +               if (!ret) {
1171 +                       dev->capture.fmt = mfmt;
1172 +                       dev->capture.stride = f->fmt.pix.bytesperline;
1173 +                       dev->capture.width = port->es.video.crop.width;
1174 +                       dev->capture.height = port->es.video.crop.height;
1175 +
1176 +                       /* select port for capture */
1177 +                       dev->capture.port = port;
1178 +                       dev->capture.camera_port = camera_port;
1179 +                       dev->capture.encode_component = encode_component;
1180 +               }
1181 +       }
1182 +
1183 +       /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1184 +       return ret;
1185 +}
1186 +
1187 +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1188 +                               struct v4l2_format *f)
1189 +{
1190 +       int ret;
1191 +       struct bm2835_mmal_dev *dev = video_drvdata(file);
1192 +       struct mmal_fmt *mfmt;
1193 +
1194 +       /* try the format to set valid parameters */
1195 +       ret = vidioc_try_fmt_vid_cap(file, priv, f);
1196 +       if (ret) {
1197 +               v4l2_err(&dev->v4l2_dev,
1198 +                        "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1199 +               return ret;
1200 +       }
1201 +
1202 +       /* if a capture is running refuse to set format */
1203 +       if (vb2_is_busy(&dev->capture.vb_vidq)) {
1204 +               v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1205 +               return -EBUSY;
1206 +       }
1207 +
1208 +       /* If the format is unsupported v4l2 says we should switch to
1209 +        * a supported one and not return an error. */
1210 +       mfmt = get_format(f);
1211 +       if (!mfmt) {
1212 +               v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1213 +                        "Fourcc format (0x%08x) unknown.\n",
1214 +                        f->fmt.pix.pixelformat);
1215 +               f->fmt.pix.pixelformat = formats[0].fourcc;
1216 +               mfmt = get_format(f);
1217 +       }
1218 +
1219 +       ret = mmal_setup_components(dev, f);
1220 +       if (ret != 0)
1221 +               v4l2_err(&dev->v4l2_dev,
1222 +                        "%s: failed to setup mmal components: %d\n",
1223 +                        __func__, ret);
1224 +
1225 +       return ret;
1226 +}
1227 +
1228 +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1229 +       /* overlay */
1230 +       .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1231 +       .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1232 +       .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1233 +       .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1234 +       .vidioc_overlay = vidioc_overlay,
1235 +       .vidioc_g_fbuf = vidioc_g_fbuf,
1236 +
1237 +       /* inputs */
1238 +       .vidioc_enum_input = vidioc_enum_input,
1239 +       .vidioc_g_input = vidioc_g_input,
1240 +       .vidioc_s_input = vidioc_s_input,
1241 +
1242 +       /* capture */
1243 +       .vidioc_querycap = vidioc_querycap,
1244 +       .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1245 +       .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1246 +       .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1247 +       .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1248 +
1249 +       /* buffer management */
1250 +       .vidioc_reqbufs = vb2_ioctl_reqbufs,
1251 +       .vidioc_create_bufs = vb2_ioctl_create_bufs,
1252 +       .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1253 +       .vidioc_querybuf = vb2_ioctl_querybuf,
1254 +       .vidioc_qbuf = vb2_ioctl_qbuf,
1255 +       .vidioc_dqbuf = vb2_ioctl_dqbuf,
1256 +       .vidioc_streamon = vb2_ioctl_streamon,
1257 +       .vidioc_streamoff = vb2_ioctl_streamoff,
1258 +
1259 +       .vidioc_log_status = v4l2_ctrl_log_status,
1260 +       .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1261 +       .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1262 +};
1263 +
1264 +/* ------------------------------------------------------------------
1265 +       Driver init/finalise
1266 +   ------------------------------------------------------------------*/
1267 +
1268 +static const struct v4l2_file_operations camera0_fops = {
1269 +       .owner = THIS_MODULE,
1270 +       .open = v4l2_fh_open,
1271 +       .release = vb2_fop_release,
1272 +       .read = vb2_fop_read,
1273 +       .poll = vb2_fop_poll,
1274 +       .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1275 +       .mmap = vb2_fop_mmap,
1276 +};
1277 +
1278 +static struct video_device vdev_template = {
1279 +       .name = "camera0",
1280 +       .fops = &camera0_fops,
1281 +       .ioctl_ops = &camera0_ioctl_ops,
1282 +       .release = video_device_release_empty,
1283 +};
1284 +
1285 +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1286 +                                struct vchiq_mmal_component *camera)
1287 +{
1288 +       int ret;
1289 +       struct mmal_parameter_camera_config cam_config = {
1290 +               .max_stills_w = MAX_WIDTH,
1291 +               .max_stills_h = MAX_HEIGHT,
1292 +               .stills_yuv422 = 1,
1293 +               .one_shot_stills = 1,
1294 +               .max_preview_video_w = 1920,
1295 +               .max_preview_video_h = 1088,
1296 +               .num_preview_video_frames = 3,
1297 +               .stills_capture_circular_buffer_height = 0,
1298 +               .fast_preview_resume = 0,
1299 +               .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1300 +       };
1301 +
1302 +       ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1303 +                                           MMAL_PARAMETER_CAMERA_CONFIG,
1304 +                                           &cam_config, sizeof(cam_config));
1305 +       return ret;
1306 +}
1307 +
1308 +/* MMAL instance and component init */
1309 +static int __init mmal_init(struct bm2835_mmal_dev *dev)
1310 +{
1311 +       int ret;
1312 +       struct mmal_es_format *format;
1313 +
1314 +       ret = vchiq_mmal_init(&dev->instance);
1315 +       if (ret < 0)
1316 +               return ret;
1317 +
1318 +       /* get the camera component ready */
1319 +       ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1320 +                                       &dev->component[MMAL_COMPONENT_CAMERA]);
1321 +       if (ret < 0)
1322 +               goto unreg_mmal;
1323 +
1324 +       if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
1325 +           MMAL_CAMERA_PORT_COUNT) {
1326 +               ret = -EINVAL;
1327 +               goto unreg_camera;
1328 +       }
1329 +
1330 +       ret = set_camera_parameters(dev->instance,
1331 +                                   dev->component[MMAL_COMPONENT_CAMERA]);
1332 +       if (ret < 0)
1333 +               goto unreg_camera;
1334 +
1335 +       format =
1336 +           &dev->component[MMAL_COMPONENT_CAMERA]->
1337 +           output[MMAL_CAMERA_PORT_PREVIEW].format;
1338 +
1339 +       format->encoding = MMAL_ENCODING_OPAQUE;
1340 +       format->encoding_variant = MMAL_ENCODING_I420;
1341 +
1342 +       format->es->video.width = 1024;
1343 +       format->es->video.height = 768;
1344 +       format->es->video.crop.x = 0;
1345 +       format->es->video.crop.y = 0;
1346 +       format->es->video.crop.width = 1024;
1347 +       format->es->video.crop.height = 768;
1348 +       format->es->video.frame_rate.num = PREVIEW_FRAME_RATE_NUM;
1349 +       format->es->video.frame_rate.den = PREVIEW_FRAME_RATE_DEN;
1350 +
1351 +       format =
1352 +           &dev->component[MMAL_COMPONENT_CAMERA]->
1353 +           output[MMAL_CAMERA_PORT_VIDEO].format;
1354 +
1355 +       format->encoding = MMAL_ENCODING_OPAQUE;
1356 +       format->encoding_variant = MMAL_ENCODING_I420;
1357 +
1358 +       format->es->video.width = 1024;
1359 +       format->es->video.height = 768;
1360 +       format->es->video.crop.x = 0;
1361 +       format->es->video.crop.y = 0;
1362 +       format->es->video.crop.width = 1024;
1363 +       format->es->video.crop.height = 768;
1364 +       format->es->video.frame_rate.num = PREVIEW_FRAME_RATE_NUM;
1365 +       format->es->video.frame_rate.den = PREVIEW_FRAME_RATE_DEN;
1366 +
1367 +       format =
1368 +           &dev->component[MMAL_COMPONENT_CAMERA]->
1369 +           output[MMAL_CAMERA_PORT_CAPTURE].format;
1370 +
1371 +       format->encoding = MMAL_ENCODING_OPAQUE;
1372 +
1373 +       format->es->video.width = 2592;
1374 +       format->es->video.height = 1944;
1375 +       format->es->video.crop.x = 0;
1376 +       format->es->video.crop.y = 0;
1377 +       format->es->video.crop.width = 2592;
1378 +       format->es->video.crop.height = 1944;
1379 +       format->es->video.frame_rate.num = 30;
1380 +       format->es->video.frame_rate.den = 1;
1381 +
1382 +       dev->capture.width = format->es->video.width;
1383 +       dev->capture.height = format->es->video.height;
1384 +       dev->capture.fmt = &formats[0];
1385 +       dev->capture.encode_component = NULL;
1386 +
1387 +       /* get the preview component ready */
1388 +       ret = vchiq_mmal_component_init(
1389 +                       dev->instance, "ril.video_render",
1390 +                       &dev->component[MMAL_COMPONENT_PREVIEW]);
1391 +       if (ret < 0)
1392 +               goto unreg_camera;
1393 +
1394 +       if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1395 +               ret = -EINVAL;
1396 +               pr_debug("too few input ports %d needed %d\n",
1397 +                        dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1398 +               goto unreg_preview;
1399 +       }
1400 +
1401 +       /* get the image encoder component ready */
1402 +       ret = vchiq_mmal_component_init(
1403 +               dev->instance, "ril.image_encode",
1404 +               &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1405 +       if (ret < 0)
1406 +               goto unreg_preview;
1407 +
1408 +       if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1409 +               ret = -EINVAL;
1410 +               v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
1411 +                        dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1412 +                        1);
1413 +               goto unreg_image_encoder;
1414 +       }
1415 +
1416 +       /* get the video encoder component ready */
1417 +       ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1418 +                                       &dev->
1419 +                                       component[MMAL_COMPONENT_VIDEO_ENCODE]);
1420 +       if (ret < 0)
1421 +               goto unreg_image_encoder;
1422 +
1423 +       if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1424 +               ret = -EINVAL;
1425 +               v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
1426 +                        dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1427 +                        1);
1428 +               goto unreg_vid_encoder;
1429 +       }
1430 +
1431 +       {
1432 +               unsigned int enable = 1;
1433 +               vchiq_mmal_port_parameter_set(
1434 +                       dev->instance,
1435 +                       &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1436 +                       MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1437 +                       &enable, sizeof(enable));
1438 +
1439 +               vchiq_mmal_port_parameter_set(dev->instance,
1440 +                       &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1441 +                       MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1442 +                       &enable,
1443 +                       sizeof(enable));
1444 +       }
1445 +       ret = bm2835_mmal_set_all_camera_controls(dev);
1446 +       if (ret < 0)
1447 +               goto unreg_vid_encoder;
1448 +
1449 +       return 0;
1450 +
1451 +unreg_vid_encoder:
1452 +       pr_err("Cleanup: Destroy video encoder\n");
1453 +       vchiq_mmal_component_finalise(
1454 +               dev->instance,
1455 +               dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1456 +
1457 +unreg_image_encoder:
1458 +       pr_err("Cleanup: Destroy image encoder\n");
1459 +       vchiq_mmal_component_finalise(
1460 +               dev->instance,
1461 +               dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1462 +
1463 +unreg_preview:
1464 +       pr_err("Cleanup: Destroy video render\n");
1465 +       vchiq_mmal_component_finalise(dev->instance,
1466 +                                     dev->component[MMAL_COMPONENT_PREVIEW]);
1467 +
1468 +unreg_camera:
1469 +       pr_err("Cleanup: Destroy camera\n");
1470 +       vchiq_mmal_component_finalise(dev->instance,
1471 +                                     dev->component[MMAL_COMPONENT_CAMERA]);
1472 +
1473 +unreg_mmal:
1474 +       vchiq_mmal_finalise(dev->instance);
1475 +       return ret;
1476 +}
1477 +
1478 +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1479 +                                         struct video_device *vfd)
1480 +{
1481 +       int ret;
1482 +
1483 +       *vfd = vdev_template;
1484 +
1485 +       vfd->v4l2_dev = &dev->v4l2_dev;
1486 +
1487 +       vfd->lock = &dev->mutex;
1488 +
1489 +       vfd->queue = &dev->capture.vb_vidq;
1490 +
1491 +       set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1492 +
1493 +       /* video device needs to be able to access instance data */
1494 +       video_set_drvdata(vfd, dev);
1495 +
1496 +       ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1497 +       if (ret < 0)
1498 +               return ret;
1499 +
1500 +       v4l2_info(vfd->v4l2_dev, "V4L2 device registered as %s\n",
1501 +                 video_device_node_name(vfd));
1502 +
1503 +       return 0;
1504 +}
1505 +
1506 +static struct v4l2_format default_v4l2_format = {
1507 +       .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1508 +       .fmt.pix.width = 1024,
1509 +       .fmt.pix.bytesperline = 1024 * 3 / 2,
1510 +       .fmt.pix.height = 768,
1511 +       .fmt.pix.sizeimage = 1<<18,
1512 +};
1513 +
1514 +static int __init bm2835_mmal_init(void)
1515 +{
1516 +       int ret;
1517 +       struct bm2835_mmal_dev *dev;
1518 +       struct vb2_queue *q;
1519 +
1520 +       dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1521 +       if (!dev)
1522 +               return -ENOMEM;
1523 +
1524 +       /* setup device defaults */
1525 +       dev->overlay.w.left = 150;
1526 +       dev->overlay.w.top = 50;
1527 +       dev->overlay.w.width = 1024;
1528 +       dev->overlay.w.height = 768;
1529 +       dev->overlay.clipcount = 0;
1530 +       dev->overlay.field = V4L2_FIELD_NONE;
1531 +
1532 +       dev->capture.fmt = &formats[3]; /* JPEG */
1533 +
1534 +       /* v4l device registration */
1535 +       snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1536 +                "%s", BM2835_MMAL_MODULE_NAME);
1537 +       ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1538 +       if (ret)
1539 +               goto free_dev;
1540 +
1541 +       /* setup v4l controls */
1542 +       ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1543 +       if (ret < 0)
1544 +               goto unreg_dev;
1545 +       dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1546 +
1547 +       /* mmal init */
1548 +       ret = mmal_init(dev);
1549 +       if (ret < 0)
1550 +               goto unreg_dev;
1551 +
1552 +       /* initialize queue */
1553 +       q = &dev->capture.vb_vidq;
1554 +       memset(q, 0, sizeof(*q));
1555 +       q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1556 +       q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1557 +       q->drv_priv = dev;
1558 +       q->buf_struct_size = sizeof(struct mmal_buffer);
1559 +       q->ops = &bm2835_mmal_video_qops;
1560 +       q->mem_ops = &vb2_vmalloc_memops;
1561 +       q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1562 +       ret = vb2_queue_init(q);
1563 +       if (ret < 0)
1564 +               goto unreg_dev;
1565 +
1566 +       /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1567 +       mutex_init(&dev->mutex);
1568 +
1569 +       /* initialise video devices */
1570 +       ret = bm2835_mmal_init_device(dev, &dev->vdev);
1571 +       if (ret < 0)
1572 +               goto unreg_dev;
1573 +
1574 +       ret = mmal_setup_components(dev, &default_v4l2_format);
1575 +       if (ret < 0) {
1576 +               v4l2_err(&dev->v4l2_dev,
1577 +                        "%s: could not setup components\n", __func__);
1578 +               goto unreg_dev;
1579 +       }
1580 +
1581 +       v4l2_info(&dev->v4l2_dev,
1582 +                 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1583 +                 BM2835_MMAL_VERSION);
1584 +
1585 +       gdev = dev;
1586 +       return 0;
1587 +
1588 +unreg_dev:
1589 +       v4l2_ctrl_handler_free(&dev->ctrl_handler);
1590 +       v4l2_device_unregister(&dev->v4l2_dev);
1591 +
1592 +free_dev:
1593 +       kfree(dev);
1594 +
1595 +       v4l2_err(&dev->v4l2_dev,
1596 +                "%s: error %d while loading driver\n",
1597 +                BM2835_MMAL_MODULE_NAME, ret);
1598 +
1599 +       return ret;
1600 +}
1601 +
1602 +static void __exit bm2835_mmal_exit(void)
1603 +{
1604 +       if (!gdev)
1605 +               return;
1606 +
1607 +       v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
1608 +                 video_device_node_name(&gdev->vdev));
1609 +
1610 +       video_unregister_device(&gdev->vdev);
1611 +
1612 +       if (gdev->capture.encode_component) {
1613 +               v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
1614 +                        "mmal_exit - disconnect tunnel\n");
1615 +               vchiq_mmal_port_connect_tunnel(gdev->instance,
1616 +                                              gdev->capture.camera_port, NULL);
1617 +               vchiq_mmal_component_disable(gdev->instance,
1618 +                                            gdev->capture.encode_component);
1619 +       }
1620 +       vchiq_mmal_component_disable(gdev->instance,
1621 +                                    gdev->component[MMAL_COMPONENT_CAMERA]);
1622 +
1623 +       vchiq_mmal_component_finalise(gdev->instance,
1624 +                                     gdev->
1625 +                                     component[MMAL_COMPONENT_VIDEO_ENCODE]);
1626 +
1627 +       vchiq_mmal_component_finalise(gdev->instance,
1628 +                                     gdev->
1629 +                                     component[MMAL_COMPONENT_IMAGE_ENCODE]);
1630 +
1631 +       vchiq_mmal_component_finalise(gdev->instance,
1632 +                                     gdev->component[MMAL_COMPONENT_PREVIEW]);
1633 +
1634 +       vchiq_mmal_component_finalise(gdev->instance,
1635 +                                     gdev->component[MMAL_COMPONENT_CAMERA]);
1636 +
1637 +       vchiq_mmal_finalise(gdev->instance);
1638 +
1639 +       v4l2_ctrl_handler_free(&gdev->ctrl_handler);
1640 +
1641 +       v4l2_device_unregister(&gdev->v4l2_dev);
1642 +
1643 +       kfree(gdev);
1644 +}
1645 +
1646 +module_init(bm2835_mmal_init);
1647 +module_exit(bm2835_mmal_exit);
1648 --- /dev/null
1649 +++ b/drivers/media/platform/bcm2835/bcm2835-camera.h
1650 @@ -0,0 +1,113 @@
1651 +/*
1652 + * Broadcom BM2835 V4L2 driver
1653 + *
1654 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
1655 + *
1656 + * This file is subject to the terms and conditions of the GNU General Public
1657 + * License.  See the file COPYING in the main directory of this archive
1658 + * for more details.
1659 + *
1660 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
1661 + *          Dave Stevenson <dsteve@broadcom.com>
1662 + *          Simon Mellor <simellor@broadcom.com>
1663 + *          Luke Diamand <luked@broadcom.com>
1664 + *
1665 + * core driver device
1666 + */
1667 +
1668 +#define V4L2_CTRL_COUNT 18 /* number of v4l controls */
1669 +
1670 +enum {
1671 +       MMAL_COMPONENT_CAMERA = 0,
1672 +       MMAL_COMPONENT_PREVIEW,
1673 +       MMAL_COMPONENT_IMAGE_ENCODE,
1674 +       MMAL_COMPONENT_VIDEO_ENCODE,
1675 +       MMAL_COMPONENT_COUNT
1676 +};
1677 +
1678 +enum {
1679 +       MMAL_CAMERA_PORT_PREVIEW = 0,
1680 +       MMAL_CAMERA_PORT_VIDEO,
1681 +       MMAL_CAMERA_PORT_CAPTURE,
1682 +       MMAL_CAMERA_PORT_COUNT
1683 +};
1684 +
1685 +#define PREVIEW_FRAME_RATE_NUM 30
1686 +#define PREVIEW_FRAME_RATE_DEN 1
1687 +
1688 +#define PREVIEW_LAYER      2
1689 +
1690 +extern int bcm2835_v4l2_debug;
1691 +
1692 +struct bm2835_mmal_dev {
1693 +       /* v4l2 devices */
1694 +       struct v4l2_device     v4l2_dev;
1695 +       struct video_device    vdev;
1696 +       struct mutex           mutex;
1697 +
1698 +       /* controls */
1699 +       struct v4l2_ctrl_handler  ctrl_handler;
1700 +       struct v4l2_ctrl          *ctrls[V4L2_CTRL_COUNT];
1701 +       struct mmal_colourfx      colourfx;
1702 +       int                       hflip;
1703 +       int                       vflip;
1704 +
1705 +       /* allocated mmal instance and components */
1706 +       struct vchiq_mmal_instance   *instance;
1707 +       struct vchiq_mmal_component  *component[MMAL_COMPONENT_COUNT];
1708 +       int camera_use_count;
1709 +
1710 +       struct v4l2_window overlay;
1711 +
1712 +       struct {
1713 +               unsigned int     width;  /* width */
1714 +               unsigned int     height;  /* height */
1715 +               unsigned int     stride;  /* stride */
1716 +               struct mmal_fmt  *fmt;
1717 +
1718 +               /* H264 encode bitrate */
1719 +               int         encode_bitrate;
1720 +               /* H264 bitrate mode. CBR/VBR */
1721 +               int         encode_bitrate_mode;
1722 +               /* JPEG Q-factor */
1723 +               int         q_factor;
1724 +
1725 +               struct vb2_queue        vb_vidq;
1726 +
1727 +               /* VC start timestamp for streaming */
1728 +               s64         vc_start_timestamp;
1729 +               /* Kernel start timestamp for streaming */
1730 +               struct timeval kernel_start_ts;
1731 +
1732 +               struct vchiq_mmal_port  *port; /* port being used for capture */
1733 +               /* camera port being used for capture */
1734 +               struct vchiq_mmal_port  *camera_port;
1735 +               /* component being used for encode */
1736 +               struct vchiq_mmal_component *encode_component;
1737 +               /* number of frames remaining which driver should capture */
1738 +               unsigned int  frame_count;
1739 +               /* last frame completion */
1740 +               struct completion  frame_cmplt;
1741 +
1742 +       } capture;
1743 +
1744 +};
1745 +
1746 +int bm2835_mmal_init_controls(
1747 +                       struct bm2835_mmal_dev *dev,
1748 +                       struct v4l2_ctrl_handler *hdl);
1749 +
1750 +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
1751 +
1752 +
1753 +/* Debug helpers */
1754 +
1755 +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
1756 +{      \
1757 +       v4l2_dbg(level, debug, dev,     \
1758 +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
1759 +               desc == NULL ? "" : desc,       \
1760 +               (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,  \
1761 +               (pix_fmt)->pixelformat, (pix_fmt)->bytesperline,        \
1762 +               (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
1763 +}
1764 --- /dev/null
1765 +++ b/drivers/media/platform/bcm2835/controls.c
1766 @@ -0,0 +1,725 @@
1767 +/*
1768 + * Broadcom BM2835 V4L2 driver
1769 + *
1770 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
1771 + *
1772 + * This file is subject to the terms and conditions of the GNU General Public
1773 + * License.  See the file COPYING in the main directory of this archive
1774 + * for more details.
1775 + *
1776 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
1777 + *          Dave Stevenson <dsteve@broadcom.com>
1778 + *          Simon Mellor <simellor@broadcom.com>
1779 + *          Luke Diamand <luked@broadcom.com>
1780 + */
1781 +
1782 +#include <linux/errno.h>
1783 +#include <linux/kernel.h>
1784 +#include <linux/module.h>
1785 +#include <linux/slab.h>
1786 +#include <media/videobuf2-vmalloc.h>
1787 +#include <media/v4l2-device.h>
1788 +#include <media/v4l2-ioctl.h>
1789 +#include <media/v4l2-ctrls.h>
1790 +#include <media/v4l2-fh.h>
1791 +#include <media/v4l2-event.h>
1792 +#include <media/v4l2-common.h>
1793 +
1794 +#include "mmal-common.h"
1795 +#include "mmal-vchiq.h"
1796 +#include "mmal-parameters.h"
1797 +#include "bcm2835-camera.h"
1798 +
1799 +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -24 to +24.
1800 + * These are in 1/6th increments so the effective range is -4.0EV to +4.0EV.
1801 + */
1802 +static const s64 ev_bias_qmenu[] = {
1803 +       -24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 21, 24
1804 +};
1805 +
1806 +/* Supported ISO values
1807 + * ISOO = auto ISO
1808 + */
1809 +static const s64 iso_qmenu[] = {
1810 +       0, 100, 200, 400, 800,
1811 +};
1812 +
1813 +/* Supported video encode modes */
1814 +static const s64 bitrate_mode_qmenu[] = {
1815 +       (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
1816 +       (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
1817 +};
1818 +
1819 +
1820 +enum bm2835_mmal_ctrl_type {
1821 +       MMAL_CONTROL_TYPE_STD,
1822 +       MMAL_CONTROL_TYPE_STD_MENU,
1823 +       MMAL_CONTROL_TYPE_INT_MENU,
1824 +       MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
1825 +};
1826 +
1827 +struct bm2835_mmal_v4l2_ctrl;
1828 +
1829 +typedef        int(bm2835_mmal_v4l2_ctrl_cb)(
1830 +                               struct bm2835_mmal_dev *dev,
1831 +                               struct v4l2_ctrl *ctrl,
1832 +                               const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
1833 +
1834 +struct bm2835_mmal_v4l2_ctrl {
1835 +       u32 id; /* v4l2 control identifier */
1836 +       enum bm2835_mmal_ctrl_type type;
1837 +       /* control minimum value or
1838 +        * mask for MMAL_CONTROL_TYPE_STD_MENU */
1839 +       s32 min;
1840 +       s32 max; /* maximum value of control */
1841 +       s32 def;  /* default value of control */
1842 +       s32 step; /* step size of the control */
1843 +       const s64 *imenu; /* integer menu array */
1844 +       u32 mmal_id; /* mmal parameter id */
1845 +       bm2835_mmal_v4l2_ctrl_cb *setter;
1846 +};
1847 +
1848 +struct v4l2_to_mmal_effects_setting {
1849 +       u32 v4l2_effect;
1850 +       u32 mmal_effect;
1851 +       s32 col_fx_enable;
1852 +       s32 col_fx_fixed_cbcr;
1853 +       u32 u;
1854 +       u32 v;
1855 +       u32 num_effect_params;
1856 +       u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
1857 +};
1858 +
1859 +static const struct v4l2_to_mmal_effects_setting
1860 +       v4l2_to_mmal_effects_values[] = {
1861 +       {  V4L2_COLORFX_NONE,         MMAL_PARAM_IMAGEFX_NONE,
1862 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1863 +       {  V4L2_COLORFX_BW,           MMAL_PARAM_IMAGEFX_NONE,
1864 +               1,   0,    128,  128, 0, {0, 0, 0, 0, 0} },
1865 +       {  V4L2_COLORFX_SEPIA,        MMAL_PARAM_IMAGEFX_NONE,
1866 +               1,   0,    87,   151, 0, {0, 0, 0, 0, 0} },
1867 +       {  V4L2_COLORFX_NEGATIVE,     MMAL_PARAM_IMAGEFX_NEGATIVE,
1868 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1869 +       {  V4L2_COLORFX_EMBOSS,       MMAL_PARAM_IMAGEFX_EMBOSS,
1870 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1871 +       {  V4L2_COLORFX_SKETCH,       MMAL_PARAM_IMAGEFX_SKETCH,
1872 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1873 +       {  V4L2_COLORFX_SKY_BLUE,     MMAL_PARAM_IMAGEFX_PASTEL,
1874 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1875 +       {  V4L2_COLORFX_GRASS_GREEN,  MMAL_PARAM_IMAGEFX_WATERCOLOUR,
1876 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1877 +       {  V4L2_COLORFX_SKIN_WHITEN,  MMAL_PARAM_IMAGEFX_WASHEDOUT,
1878 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1879 +       {  V4L2_COLORFX_VIVID,        MMAL_PARAM_IMAGEFX_SATURATION,
1880 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1881 +       {  V4L2_COLORFX_AQUA,         MMAL_PARAM_IMAGEFX_NONE,
1882 +               1,   0,    171,  121, 0, {0, 0, 0, 0, 0} },
1883 +       {  V4L2_COLORFX_ART_FREEZE,   MMAL_PARAM_IMAGEFX_HATCH,
1884 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1885 +       {  V4L2_COLORFX_SILHOUETTE,   MMAL_PARAM_IMAGEFX_FILM,
1886 +               0,   0,    0,    0,   0, {0, 0, 0, 0, 0} },
1887 +       {  V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
1888 +               0,   0,    0,    0,   5, {1, 128, 160, 160, 48} },
1889 +       {  V4L2_COLORFX_ANTIQUE,      MMAL_PARAM_IMAGEFX_COLOURBALANCE,
1890 +               0,   0,    0,    0,   3, {108, 274, 238, 0, 0} },
1891 +       {  V4L2_COLORFX_SET_CBCR,     MMAL_PARAM_IMAGEFX_NONE,
1892 +               1,   1,    0,    0,   0, {0, 0, 0, 0, 0} }
1893 +};
1894 +
1895 +
1896 +/* control handlers*/
1897 +
1898 +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
1899 +                     struct v4l2_ctrl *ctrl,
1900 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
1901 +{
1902 +       struct  {
1903 +               s32 num;    /**< Numerator */
1904 +               s32 den;    /**< Denominator */
1905 +       } rational_value;
1906 +       struct vchiq_mmal_port *control;
1907 +
1908 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
1909 +
1910 +       rational_value.num = ctrl->val;
1911 +       rational_value.den = 100;
1912 +
1913 +       return vchiq_mmal_port_parameter_set(dev->instance, control,
1914 +                                            mmal_ctrl->mmal_id,
1915 +                                            &rational_value,
1916 +                                            sizeof(rational_value));
1917 +}
1918 +
1919 +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
1920 +                     struct v4l2_ctrl *ctrl,
1921 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
1922 +{
1923 +       u32 u32_value;
1924 +       struct vchiq_mmal_port *control;
1925 +
1926 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
1927 +
1928 +       u32_value = ctrl->val;
1929 +
1930 +       return vchiq_mmal_port_parameter_set(dev->instance, control,
1931 +                                            mmal_ctrl->mmal_id,
1932 +                                            &u32_value, sizeof(u32_value));
1933 +}
1934 +
1935 +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
1936 +                     struct v4l2_ctrl *ctrl,
1937 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
1938 +{
1939 +       int ret;
1940 +       u32 u32_value;
1941 +       struct vchiq_mmal_component *camera;
1942 +
1943 +       camera = dev->component[MMAL_COMPONENT_CAMERA];
1944 +
1945 +       u32_value = ((ctrl->val % 360) / 90) * 90;
1946 +
1947 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
1948 +                                           mmal_ctrl->mmal_id,
1949 +                                           &u32_value, sizeof(u32_value));
1950 +       if (ret < 0)
1951 +               return ret;
1952 +
1953 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
1954 +                                           mmal_ctrl->mmal_id,
1955 +                                           &u32_value, sizeof(u32_value));
1956 +       if (ret < 0)
1957 +               return ret;
1958 +
1959 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
1960 +                                           mmal_ctrl->mmal_id,
1961 +                                           &u32_value, sizeof(u32_value));
1962 +
1963 +       return ret;
1964 +}
1965 +
1966 +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
1967 +                     struct v4l2_ctrl *ctrl,
1968 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
1969 +{
1970 +       int ret;
1971 +       u32 u32_value;
1972 +       struct vchiq_mmal_component *camera;
1973 +
1974 +       if (ctrl->id == V4L2_CID_HFLIP)
1975 +               dev->hflip = ctrl->val;
1976 +       else
1977 +               dev->vflip = ctrl->val;
1978 +
1979 +       camera = dev->component[MMAL_COMPONENT_CAMERA];
1980 +
1981 +       if (dev->hflip && dev->vflip)
1982 +               u32_value = MMAL_PARAM_MIRROR_BOTH;
1983 +       else if (dev->hflip)
1984 +               u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
1985 +       else if (dev->vflip)
1986 +               u32_value = MMAL_PARAM_MIRROR_VERTICAL;
1987 +       else
1988 +               u32_value = MMAL_PARAM_MIRROR_NONE;
1989 +
1990 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
1991 +                                           mmal_ctrl->mmal_id,
1992 +                                           &u32_value, sizeof(u32_value));
1993 +       if (ret < 0)
1994 +               return ret;
1995 +
1996 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
1997 +                                           mmal_ctrl->mmal_id,
1998 +                                           &u32_value, sizeof(u32_value));
1999 +       if (ret < 0)
2000 +               return ret;
2001 +
2002 +       ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
2003 +                                           mmal_ctrl->mmal_id,
2004 +                                           &u32_value, sizeof(u32_value));
2005 +
2006 +       return ret;
2007 +
2008 +}
2009 +
2010 +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
2011 +                     struct v4l2_ctrl *ctrl,
2012 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2013 +{
2014 +       u32 u32_value;
2015 +       struct vchiq_mmal_port *control;
2016 +
2017 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2018 +
2019 +       switch (ctrl->val) {
2020 +       case V4L2_EXPOSURE_AUTO:
2021 +               u32_value = MMAL_PARAM_EXPOSUREMODE_AUTO;
2022 +               break;
2023 +
2024 +       case V4L2_EXPOSURE_MANUAL:
2025 +               u32_value = MMAL_PARAM_EXPOSUREMODE_OFF;
2026 +               break;
2027 +
2028 +       case V4L2_EXPOSURE_SHUTTER_PRIORITY:
2029 +               u32_value = MMAL_PARAM_EXPOSUREMODE_SPORTS;
2030 +               break;
2031 +
2032 +       case V4L2_EXPOSURE_APERTURE_PRIORITY:
2033 +               u32_value = MMAL_PARAM_EXPOSUREMODE_NIGHT;
2034 +               break;
2035 +
2036 +       }
2037 +
2038 +       /* todo: what about the other ten modes there are MMAL parameters for */
2039 +       return vchiq_mmal_port_parameter_set(dev->instance, control,
2040 +                                            mmal_ctrl->mmal_id,
2041 +                                            &u32_value, sizeof(u32_value));
2042 +}
2043 +
2044 +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
2045 +                          struct v4l2_ctrl *ctrl,
2046 +                          const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2047 +{
2048 +       u32 u32_value;
2049 +       struct vchiq_mmal_port *control;
2050 +
2051 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2052 +
2053 +       switch (ctrl->val) {
2054 +       case V4L2_EXPOSURE_METERING_AVERAGE:
2055 +               u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
2056 +               break;
2057 +
2058 +       case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
2059 +               u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
2060 +               break;
2061 +
2062 +       case V4L2_EXPOSURE_METERING_SPOT:
2063 +               u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
2064 +               break;
2065 +
2066 +       /* todo matrix weighting not added to Linux API till 3.9
2067 +       case V4L2_EXPOSURE_METERING_MATRIX:
2068 +               u32_value = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
2069 +               break;
2070 +       */
2071 +
2072 +       }
2073 +
2074 +       return vchiq_mmal_port_parameter_set(dev->instance, control,
2075 +                                            mmal_ctrl->mmal_id,
2076 +                                            &u32_value, sizeof(u32_value));
2077 +}
2078 +
2079 +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
2080 +                     struct v4l2_ctrl *ctrl,
2081 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2082 +{
2083 +       u32 u32_value;
2084 +       struct vchiq_mmal_port *control;
2085 +
2086 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2087 +
2088 +       switch (ctrl->val) {
2089 +       case V4L2_WHITE_BALANCE_MANUAL:
2090 +               u32_value = MMAL_PARAM_AWBMODE_OFF;
2091 +               break;
2092 +
2093 +       case V4L2_WHITE_BALANCE_AUTO:
2094 +               u32_value = MMAL_PARAM_AWBMODE_AUTO;
2095 +               break;
2096 +
2097 +       case V4L2_WHITE_BALANCE_INCANDESCENT:
2098 +               u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
2099 +               break;
2100 +
2101 +       case V4L2_WHITE_BALANCE_FLUORESCENT:
2102 +               u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
2103 +               break;
2104 +
2105 +       case V4L2_WHITE_BALANCE_FLUORESCENT_H:
2106 +               u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
2107 +               break;
2108 +
2109 +       case V4L2_WHITE_BALANCE_HORIZON:
2110 +               u32_value = MMAL_PARAM_AWBMODE_HORIZON;
2111 +               break;
2112 +
2113 +       case V4L2_WHITE_BALANCE_DAYLIGHT:
2114 +               u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
2115 +               break;
2116 +
2117 +       case V4L2_WHITE_BALANCE_FLASH:
2118 +               u32_value = MMAL_PARAM_AWBMODE_FLASH;
2119 +               break;
2120 +
2121 +       case V4L2_WHITE_BALANCE_CLOUDY:
2122 +               u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
2123 +               break;
2124 +
2125 +       case V4L2_WHITE_BALANCE_SHADE:
2126 +               u32_value = MMAL_PARAM_AWBMODE_SHADE;
2127 +               break;
2128 +
2129 +       }
2130 +
2131 +       return vchiq_mmal_port_parameter_set(dev->instance, control,
2132 +                                            mmal_ctrl->mmal_id,
2133 +                                            &u32_value, sizeof(u32_value));
2134 +}
2135 +
2136 +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
2137 +                  struct v4l2_ctrl *ctrl,
2138 +                  const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2139 +{
2140 +       int ret = -EINVAL;
2141 +       int i, j;
2142 +       struct vchiq_mmal_port *control;
2143 +       struct mmal_parameter_imagefx_parameters imagefx;
2144 +
2145 +       for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
2146 +               if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
2147 +
2148 +                       imagefx.effect =
2149 +                               v4l2_to_mmal_effects_values[i].mmal_effect;
2150 +                       imagefx.num_effect_params =
2151 +                               v4l2_to_mmal_effects_values[i].num_effect_params;
2152 +
2153 +                       if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
2154 +                               imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
2155 +
2156 +                       for (j = 0; j < imagefx.num_effect_params; j++)
2157 +                               imagefx.effect_parameter[j] =
2158 +                                       v4l2_to_mmal_effects_values[i].effect_params[j];
2159 +
2160 +                       dev->colourfx.enable =
2161 +                               v4l2_to_mmal_effects_values[i].col_fx_enable;
2162 +                       if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
2163 +                               dev->colourfx.u =
2164 +                                       v4l2_to_mmal_effects_values[i].u;
2165 +                               dev->colourfx.v =
2166 +                                       v4l2_to_mmal_effects_values[i].v;
2167 +                       }
2168 +
2169 +                       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2170 +
2171 +                       ret = vchiq_mmal_port_parameter_set(
2172 +                                       dev->instance, control,
2173 +                                       MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
2174 +                                       &imagefx, sizeof(imagefx));
2175 +                       if (ret)
2176 +                               goto exit;
2177 +
2178 +                       ret = vchiq_mmal_port_parameter_set(
2179 +                                       dev->instance, control,
2180 +                                       MMAL_PARAMETER_COLOUR_EFFECT,
2181 +                                       &dev->colourfx, sizeof(dev->colourfx));
2182 +               }
2183 +       }
2184 +
2185 +exit:
2186 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
2187 +                "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
2188 +                               mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
2189 +                               dev->colourfx.enable ? "true" : "false",
2190 +                               dev->colourfx.u, dev->colourfx.v,
2191 +                               ret, (ret == 0 ? 0 : -EINVAL));
2192 +       return (ret == 0 ? 0 : EINVAL);
2193 +}
2194 +
2195 +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
2196 +                  struct v4l2_ctrl *ctrl,
2197 +                  const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2198 +{
2199 +       int ret = -EINVAL;
2200 +       struct vchiq_mmal_port *control;
2201 +
2202 +       control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2203 +
2204 +       dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
2205 +       dev->colourfx.enable = ctrl->val & 0xff;
2206 +
2207 +       ret = vchiq_mmal_port_parameter_set(dev->instance, control,
2208 +                                       MMAL_PARAMETER_COLOUR_EFFECT,
2209 +                                       &dev->colourfx, sizeof(dev->colourfx));
2210 +
2211 +       v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
2212 +                "After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
2213 +                       mmal_ctrl, ctrl->id, ctrl->val, ret,
2214 +                       (ret == 0 ? 0 : -EINVAL));
2215 +       return (ret == 0 ? 0 : EINVAL);
2216 +}
2217 +
2218 +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
2219 +                  struct v4l2_ctrl *ctrl,
2220 +                  const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2221 +{
2222 +       int ret;
2223 +       struct vchiq_mmal_port *encoder_out;
2224 +
2225 +       dev->capture.encode_bitrate = ctrl->val;
2226 +
2227 +       encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
2228 +
2229 +       ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
2230 +                                           mmal_ctrl->mmal_id,
2231 +                                           &ctrl->val, sizeof(ctrl->val));
2232 +       ret = 0;
2233 +       return ret;
2234 +}
2235 +
2236 +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
2237 +                  struct v4l2_ctrl *ctrl,
2238 +                  const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2239 +{
2240 +       u32 bitrate_mode;
2241 +       struct vchiq_mmal_port *encoder_out;
2242 +
2243 +       encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
2244 +
2245 +       dev->capture.encode_bitrate_mode = ctrl->val;
2246 +       switch (ctrl->val) {
2247 +       default:
2248 +       case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
2249 +               bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
2250 +               break;
2251 +       case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
2252 +               bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
2253 +               break;
2254 +       }
2255 +
2256 +       vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
2257 +                                            mmal_ctrl->mmal_id,
2258 +                                            &bitrate_mode,
2259 +                                            sizeof(bitrate_mode));
2260 +       return 0;
2261 +}
2262 +
2263 +static int ctrl_set_q_factor(struct bm2835_mmal_dev *dev,
2264 +                     struct v4l2_ctrl *ctrl,
2265 +                     const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2266 +{
2267 +       u32 u32_value;
2268 +       struct vchiq_mmal_port *jpeg_out;
2269 +
2270 +       jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
2271 +
2272 +       u32_value = ctrl->val;
2273 +
2274 +       return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
2275 +                                            mmal_ctrl->mmal_id,
2276 +                                            &u32_value, sizeof(u32_value));
2277 +}
2278 +
2279 +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
2280 +{
2281 +       struct bm2835_mmal_dev *dev =
2282 +               container_of(ctrl->handler, struct bm2835_mmal_dev,
2283 +                            ctrl_handler);
2284 +       const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
2285 +
2286 +       if ((mmal_ctrl == NULL) ||
2287 +           (mmal_ctrl->id != ctrl->id) ||
2288 +           (mmal_ctrl->setter == NULL)) {
2289 +               pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
2290 +               return -EINVAL;
2291 +       }
2292 +
2293 +       return mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
2294 +}
2295 +
2296 +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
2297 +       .s_ctrl = bm2835_mmal_s_ctrl,
2298 +};
2299 +
2300 +
2301 +
2302 +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
2303 +       {
2304 +               V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
2305 +               -100, 100, 0, 1, NULL,
2306 +               MMAL_PARAMETER_SATURATION, &ctrl_set_rational
2307 +       },
2308 +       {
2309 +               V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
2310 +               -100, 100, 0, 1, NULL,
2311 +               MMAL_PARAMETER_SHARPNESS, &ctrl_set_rational
2312 +       },
2313 +       {
2314 +               V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
2315 +               -100, 100, 0, 1, NULL,
2316 +               MMAL_PARAMETER_CONTRAST, &ctrl_set_rational
2317 +       },
2318 +       {
2319 +               V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
2320 +               0, 100, 50, 1, NULL,
2321 +               MMAL_PARAMETER_BRIGHTNESS, &ctrl_set_rational
2322 +       },
2323 +       {
2324 +               V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
2325 +               0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
2326 +               MMAL_PARAMETER_ISO, &ctrl_set_value
2327 +       },
2328 +       {
2329 +               V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
2330 +               0, 1, 0, 1, NULL,
2331 +               MMAL_PARAMETER_VIDEO_STABILISATION, &ctrl_set_value
2332 +       },
2333 +/*     {
2334 +               0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
2335 +       },
2336 +*/     {
2337 +               V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
2338 +               ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
2339 +               MMAL_PARAMETER_EXPOSURE_MODE, &ctrl_set_exposure
2340 +       },
2341 +/* todo this needs mixing in with set exposure
2342 +       {
2343 +              V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
2344 +       },
2345 + */
2346 +       {
2347 +               V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
2348 +               0, ARRAY_SIZE(ev_bias_qmenu) - 1,
2349 +               (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
2350 +               MMAL_PARAMETER_EXPOSURE_COMP, &ctrl_set_value
2351 +       },
2352 +       {
2353 +               V4L2_CID_EXPOSURE_METERING,
2354 +               MMAL_CONTROL_TYPE_STD_MENU,
2355 +               ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
2356 +               MMAL_PARAMETER_EXP_METERING_MODE, &ctrl_set_metering_mode
2357 +       },
2358 +       {
2359 +               V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
2360 +               MMAL_CONTROL_TYPE_STD_MENU,
2361 +               ~0x3fe, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
2362 +               MMAL_PARAMETER_AWB_MODE, &ctrl_set_awb_mode
2363 +       },
2364 +       {
2365 +               V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
2366 +               0, 15, V4L2_COLORFX_NONE, 0, NULL,
2367 +               MMAL_PARAMETER_IMAGE_EFFECT, &ctrl_set_image_effect
2368 +       },
2369 +       {
2370 +               V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
2371 +               0, 0xffff, 0x8080, 1, NULL,
2372 +               MMAL_PARAMETER_COLOUR_EFFECT, &ctrl_set_colfx
2373 +       },
2374 +       {
2375 +               V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
2376 +               0, 360, 0, 90, NULL,
2377 +               MMAL_PARAMETER_ROTATION, &ctrl_set_rotate
2378 +       },
2379 +       {
2380 +               V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
2381 +               0, 1, 0, 1, NULL,
2382 +               MMAL_PARAMETER_MIRROR, &ctrl_set_flip
2383 +       },
2384 +       {
2385 +               V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
2386 +               0, 1, 0, 1, NULL,
2387 +               MMAL_PARAMETER_MIRROR, &ctrl_set_flip
2388 +       },
2389 +       {
2390 +               V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
2391 +               0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
2392 +               0, 0, bitrate_mode_qmenu,
2393 +               MMAL_PARAMETER_RATECONTROL, &ctrl_set_bitrate_mode
2394 +       },
2395 +       {
2396 +               V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
2397 +               25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
2398 +               MMAL_PARAMETER_VIDEO_BIT_RATE, &ctrl_set_bitrate
2399 +       },
2400 +       {
2401 +               V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
2402 +               0, 100,
2403 +               30, 1, NULL,
2404 +               MMAL_PARAMETER_JPEG_Q_FACTOR, &ctrl_set_q_factor
2405 +       },
2406 +};
2407 +
2408 +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
2409 +{
2410 +       int c;
2411 +       int ret;
2412 +
2413 +       for (c = 0; c < V4L2_CTRL_COUNT; c++) {
2414 +               if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
2415 +                       ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
2416 +                                                  &v4l2_ctrls[c]);
2417 +                       if (ret)
2418 +                               break;
2419 +               }
2420 +       }
2421 +       return ret;
2422 +}
2423 +
2424 +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
2425 +                             struct v4l2_ctrl_handler *hdl)
2426 +{
2427 +       int c;
2428 +       const struct bm2835_mmal_v4l2_ctrl *ctrl;
2429 +
2430 +       v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
2431 +
2432 +       for (c = 0; c < V4L2_CTRL_COUNT; c++) {
2433 +               ctrl = &v4l2_ctrls[c];
2434 +
2435 +               switch (ctrl->type) {
2436 +               case MMAL_CONTROL_TYPE_STD:
2437 +                       dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
2438 +                               &bm2835_mmal_ctrl_ops, ctrl->id,
2439 +                               ctrl->min, ctrl->max, ctrl->step, ctrl->def);
2440 +                       break;
2441 +
2442 +               case MMAL_CONTROL_TYPE_STD_MENU:
2443 +                       dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
2444 +                       &bm2835_mmal_ctrl_ops, ctrl->id,
2445 +                       ctrl->max, ctrl->min, ctrl->def);
2446 +                       break;
2447 +
2448 +               case MMAL_CONTROL_TYPE_INT_MENU:
2449 +                       dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
2450 +                               &bm2835_mmal_ctrl_ops, ctrl->id,
2451 +                               ctrl->max, ctrl->def, ctrl->imenu);
2452 +                       break;
2453 +
2454 +               case MMAL_CONTROL_TYPE_CLUSTER:
2455 +                       /* skip this entry when constructing controls */
2456 +                       continue;
2457 +               }
2458 +
2459 +               if (hdl->error)
2460 +                       break;
2461 +
2462 +               dev->ctrls[c]->priv = (void *)ctrl;
2463 +       }
2464 +
2465 +       if (hdl->error) {
2466 +               pr_err("error adding control %d/%d id 0x%x\n", c,
2467 +                        V4L2_CTRL_COUNT, ctrl->id);
2468 +               return hdl->error;
2469 +       }
2470 +
2471 +       for (c = 0; c < V4L2_CTRL_COUNT; c++) {
2472 +               ctrl = &v4l2_ctrls[c];
2473 +
2474 +               switch (ctrl->type) {
2475 +               case MMAL_CONTROL_TYPE_CLUSTER:
2476 +                       v4l2_ctrl_auto_cluster(ctrl->min,
2477 +                                              &dev->ctrls[c+1],
2478 +                                              ctrl->max,
2479 +                                              ctrl->def);
2480 +                       break;
2481 +
2482 +               case MMAL_CONTROL_TYPE_STD:
2483 +               case MMAL_CONTROL_TYPE_STD_MENU:
2484 +               case MMAL_CONTROL_TYPE_INT_MENU:
2485 +                       break;
2486 +               }
2487 +
2488 +       }
2489 +
2490 +       return 0;
2491 +}
2492 --- /dev/null
2493 +++ b/drivers/media/platform/bcm2835/mmal-common.h
2494 @@ -0,0 +1,52 @@
2495 +/*
2496 + * Broadcom BM2835 V4L2 driver
2497 + *
2498 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2499 + *
2500 + * This file is subject to the terms and conditions of the GNU General Public
2501 + * License.  See the file COPYING in the main directory of this archive
2502 + * for more details.
2503 + *
2504 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2505 + *          Dave Stevenson <dsteve@broadcom.com>
2506 + *          Simon Mellor <simellor@broadcom.com>
2507 + *          Luke Diamand <luked@broadcom.com>
2508 + *
2509 + * MMAL structures
2510 + *
2511 + */
2512 +
2513 +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
2514 +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
2515 +
2516 +/** Special value signalling that time is not known */
2517 +#define MMAL_TIME_UNKNOWN (1LL<<63)
2518 +
2519 +/* mapping between v4l and mmal video modes */
2520 +struct mmal_fmt {
2521 +       char  *name;
2522 +       u32   fourcc;          /* v4l2 format id */
2523 +       u32   mmal;
2524 +       int   depth;
2525 +       u32   mmal_component;  /* MMAL component index to be used to encode */
2526 +};
2527 +
2528 +/* buffer for one video frame */
2529 +struct mmal_buffer {
2530 +       /* v4l buffer data -- must be first */
2531 +       struct vb2_buffer       vb;
2532 +
2533 +       /* list of buffers available */
2534 +       struct list_head        list;
2535 +
2536 +       void *buffer; /* buffer pointer */
2537 +       unsigned long buffer_size; /* size of allocated buffer */
2538 +};
2539 +
2540 +/* */
2541 +struct mmal_colourfx {
2542 +       s32 enable;
2543 +       u32 u;
2544 +       u32 v;
2545 +};
2546 +
2547 --- /dev/null
2548 +++ b/drivers/media/platform/bcm2835/mmal-encodings.h
2549 @@ -0,0 +1,93 @@
2550 +/*
2551 + * Broadcom BM2835 V4L2 driver
2552 + *
2553 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2554 + *
2555 + * This file is subject to the terms and conditions of the GNU General Public
2556 + * License.  See the file COPYING in the main directory of this archive
2557 + * for more details.
2558 + *
2559 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2560 + *          Dave Stevenson <dsteve@broadcom.com>
2561 + *          Simon Mellor <simellor@broadcom.com>
2562 + *          Luke Diamand <luked@broadcom.com>
2563 + */
2564 +
2565 +#define MMAL_ENCODING_H264             MMAL_FOURCC('H', '2', '6', '4')
2566 +#define MMAL_ENCODING_H263             MMAL_FOURCC('H', '2', '6', '3')
2567 +#define MMAL_ENCODING_MP4V             MMAL_FOURCC('M', 'P', '4', 'V')
2568 +#define MMAL_ENCODING_MP2V             MMAL_FOURCC('M', 'P', '2', 'V')
2569 +#define MMAL_ENCODING_MP1V             MMAL_FOURCC('M', 'P', '1', 'V')
2570 +#define MMAL_ENCODING_WMV3             MMAL_FOURCC('W', 'M', 'V', '3')
2571 +#define MMAL_ENCODING_WMV2             MMAL_FOURCC('W', 'M', 'V', '2')
2572 +#define MMAL_ENCODING_WMV1             MMAL_FOURCC('W', 'M', 'V', '1')
2573 +#define MMAL_ENCODING_WVC1             MMAL_FOURCC('W', 'V', 'C', '1')
2574 +#define MMAL_ENCODING_VP8              MMAL_FOURCC('V', 'P', '8', ' ')
2575 +#define MMAL_ENCODING_VP7              MMAL_FOURCC('V', 'P', '7', ' ')
2576 +#define MMAL_ENCODING_VP6              MMAL_FOURCC('V', 'P', '6', ' ')
2577 +#define MMAL_ENCODING_THEORA           MMAL_FOURCC('T', 'H', 'E', 'O')
2578 +#define MMAL_ENCODING_SPARK            MMAL_FOURCC('S', 'P', 'R', 'K')
2579 +
2580 +#define MMAL_ENCODING_JPEG             MMAL_FOURCC('J', 'P', 'E', 'G')
2581 +#define MMAL_ENCODING_GIF              MMAL_FOURCC('G', 'I', 'F', ' ')
2582 +#define MMAL_ENCODING_PNG              MMAL_FOURCC('P', 'N', 'G', ' ')
2583 +#define MMAL_ENCODING_PPM              MMAL_FOURCC('P', 'P', 'M', ' ')
2584 +#define MMAL_ENCODING_TGA              MMAL_FOURCC('T', 'G', 'A', ' ')
2585 +#define MMAL_ENCODING_BMP              MMAL_FOURCC('B', 'M', 'P', ' ')
2586 +
2587 +#define MMAL_ENCODING_I420             MMAL_FOURCC('I', '4', '2', '0')
2588 +#define MMAL_ENCODING_I420_SLICE       MMAL_FOURCC('S', '4', '2', '0')
2589 +#define MMAL_ENCODING_YV12             MMAL_FOURCC('Y', 'V', '1', '2')
2590 +#define MMAL_ENCODING_I422             MMAL_FOURCC('I', '4', '2', '2')
2591 +#define MMAL_ENCODING_I422_SLICE       MMAL_FOURCC('S', '4', '2', '2')
2592 +#define MMAL_ENCODING_YUYV             MMAL_FOURCC('Y', 'U', 'Y', 'V')
2593 +#define MMAL_ENCODING_YVYU             MMAL_FOURCC('Y', 'V', 'Y', 'U')
2594 +#define MMAL_ENCODING_UYVY             MMAL_FOURCC('U', 'Y', 'V', 'Y')
2595 +#define MMAL_ENCODING_VYUY             MMAL_FOURCC('V', 'Y', 'U', 'Y')
2596 +#define MMAL_ENCODING_NV12             MMAL_FOURCC('N', 'V', '1', '2')
2597 +#define MMAL_ENCODING_NV21             MMAL_FOURCC('N', 'V', '2', '1')
2598 +#define MMAL_ENCODING_ARGB             MMAL_FOURCC('A', 'R', 'G', 'B')
2599 +#define MMAL_ENCODING_RGBA             MMAL_FOURCC('R', 'G', 'B', 'A')
2600 +#define MMAL_ENCODING_ABGR             MMAL_FOURCC('A', 'B', 'G', 'R')
2601 +#define MMAL_ENCODING_BGRA             MMAL_FOURCC('B', 'G', 'R', 'A')
2602 +#define MMAL_ENCODING_RGB16            MMAL_FOURCC('R', 'G', 'B', '2')
2603 +#define MMAL_ENCODING_RGB24            MMAL_FOURCC('R', 'G', 'B', '3')
2604 +#define MMAL_ENCODING_RGB32            MMAL_FOURCC('R', 'G', 'B', '4')
2605 +#define MMAL_ENCODING_BGR16            MMAL_FOURCC('B', 'G', 'R', '2')
2606 +#define MMAL_ENCODING_BGR24            MMAL_FOURCC('B', 'G', 'R', '3')
2607 +#define MMAL_ENCODING_BGR32            MMAL_FOURCC('B', 'G', 'R', '4')
2608 +
2609 +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
2610 + * This format is *not* opaque - if requested you will receive full frames
2611 + * of YUV_UV video.
2612 + */
2613 +#define MMAL_ENCODING_YUVUV128         MMAL_FOURCC('S', 'A', 'N', 'D')
2614 +
2615 +/** VideoCore opaque image format, image handles are returned to
2616 + * the host but not the actual image data.
2617 + */
2618 +#define MMAL_ENCODING_OPAQUE           MMAL_FOURCC('O', 'P', 'Q', 'V')
2619 +
2620 +/** An EGL image handle
2621 + */
2622 +#define MMAL_ENCODING_EGL_IMAGE        MMAL_FOURCC('E', 'G', 'L', 'I')
2623 +
2624 +/* }@ */
2625 +
2626 +/** \name Pre-defined audio encodings */
2627 +/* @{ */
2628 +#define MMAL_ENCODING_PCM_UNSIGNED_BE  MMAL_FOURCC('P', 'C', 'M', 'U')
2629 +#define MMAL_ENCODING_PCM_UNSIGNED_LE  MMAL_FOURCC('p', 'c', 'm', 'u')
2630 +#define MMAL_ENCODING_PCM_SIGNED_BE    MMAL_FOURCC('P', 'C', 'M', 'S')
2631 +#define MMAL_ENCODING_PCM_SIGNED_LE    MMAL_FOURCC('p', 'c', 'm', 's')
2632 +#define MMAL_ENCODING_PCM_FLOAT_BE     MMAL_FOURCC('P', 'C', 'M', 'F')
2633 +#define MMAL_ENCODING_PCM_FLOAT_LE     MMAL_FOURCC('p', 'c', 'm', 'f')
2634 +
2635 +/* Pre-defined H264 encoding variants */
2636 +
2637 +/** ISO 14496-10 Annex B byte stream format */
2638 +#define MMAL_ENCODING_VARIANT_H264_DEFAULT   0
2639 +/** ISO 14496-15 AVC stream format */
2640 +#define MMAL_ENCODING_VARIANT_H264_AVC1      MMAL_FOURCC('A', 'V', 'C', '1')
2641 +/** Implicitly delineated NAL units without emulation prevention */
2642 +#define MMAL_ENCODING_VARIANT_H264_RAW       MMAL_FOURCC('R', 'A', 'W', ' ')
2643 --- /dev/null
2644 +++ b/drivers/media/platform/bcm2835/mmal-msg-common.h
2645 @@ -0,0 +1,50 @@
2646 +/*
2647 + * Broadcom BM2835 V4L2 driver
2648 + *
2649 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2650 + *
2651 + * This file is subject to the terms and conditions of the GNU General Public
2652 + * License.  See the file COPYING in the main directory of this archive
2653 + * for more details.
2654 + *
2655 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2656 + *          Dave Stevenson <dsteve@broadcom.com>
2657 + *          Simon Mellor <simellor@broadcom.com>
2658 + *          Luke Diamand <luked@broadcom.com>
2659 + */
2660 +
2661 +#ifndef MMAL_MSG_COMMON_H
2662 +#define MMAL_MSG_COMMON_H
2663 +
2664 +enum mmal_msg_status {
2665 +       MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
2666 +       MMAL_MSG_STATUS_ENOMEM,      /**< Out of memory */
2667 +       MMAL_MSG_STATUS_ENOSPC,      /**< Out of resources other than memory */
2668 +       MMAL_MSG_STATUS_EINVAL,      /**< Argument is invalid */
2669 +       MMAL_MSG_STATUS_ENOSYS,      /**< Function not implemented */
2670 +       MMAL_MSG_STATUS_ENOENT,      /**< No such file or directory */
2671 +       MMAL_MSG_STATUS_ENXIO,       /**< No such device or address */
2672 +       MMAL_MSG_STATUS_EIO,         /**< I/O error */
2673 +       MMAL_MSG_STATUS_ESPIPE,      /**< Illegal seek */
2674 +       MMAL_MSG_STATUS_ECORRUPT,    /**< Data is corrupt \attention */
2675 +       MMAL_MSG_STATUS_ENOTREADY,   /**< Component is not ready */
2676 +       MMAL_MSG_STATUS_ECONFIG,     /**< Component is not configured */
2677 +       MMAL_MSG_STATUS_EISCONN,     /**< Port is already connected */
2678 +       MMAL_MSG_STATUS_ENOTCONN,    /**< Port is disconnected */
2679 +       MMAL_MSG_STATUS_EAGAIN,      /**< Resource temporarily unavailable. */
2680 +       MMAL_MSG_STATUS_EFAULT,      /**< Bad address */
2681 +};
2682 +
2683 +struct mmal_rect {
2684 +       s32 x;      /**< x coordinate (from left) */
2685 +       s32 y;      /**< y coordinate (from top) */
2686 +       s32 width;  /**< width */
2687 +       s32 height; /**< height */
2688 +};
2689 +
2690 +struct mmal_rational {
2691 +       s32 num;    /**< Numerator */
2692 +       s32 den;    /**< Denominator */
2693 +};
2694 +
2695 +#endif /* MMAL_MSG_COMMON_H */
2696 --- /dev/null
2697 +++ b/drivers/media/platform/bcm2835/mmal-msg-format.h
2698 @@ -0,0 +1,81 @@
2699 +/*
2700 + * Broadcom BM2835 V4L2 driver
2701 + *
2702 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2703 + *
2704 + * This file is subject to the terms and conditions of the GNU General Public
2705 + * License.  See the file COPYING in the main directory of this archive
2706 + * for more details.
2707 + *
2708 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2709 + *          Dave Stevenson <dsteve@broadcom.com>
2710 + *          Simon Mellor <simellor@broadcom.com>
2711 + *          Luke Diamand <luked@broadcom.com>
2712 + */
2713 +
2714 +#ifndef MMAL_MSG_FORMAT_H
2715 +#define MMAL_MSG_FORMAT_H
2716 +
2717 +#include "mmal-msg-common.h"
2718 +
2719 +/* MMAL_ES_FORMAT_T */
2720 +
2721 +
2722 +struct mmal_audio_format {
2723 +       u32 channels;           /**< Number of audio channels */
2724 +       u32 sample_rate;        /**< Sample rate */
2725 +
2726 +       u32 bits_per_sample;    /**< Bits per sample */
2727 +       u32 block_align;        /**< Size of a block of data */
2728 +};
2729 +
2730 +struct mmal_video_format {
2731 +       u32 width;        /**< Width of frame in pixels */
2732 +       u32 height;       /**< Height of frame in rows of pixels */
2733 +       struct mmal_rect crop;         /**< Visible region of the frame */
2734 +       struct mmal_rational frame_rate;   /**< Frame rate */
2735 +       struct mmal_rational par;          /**< Pixel aspect ratio */
2736 +
2737 +       /* FourCC specifying the color space of the video stream. See the
2738 +        * \ref MmalColorSpace "pre-defined color spaces" for some examples.
2739 +        */
2740 +       u32 color_space;
2741 +};
2742 +
2743 +struct mmal_subpicture_format {
2744 +       u32 x_offset;
2745 +       u32 y_offset;
2746 +};
2747 +
2748 +union mmal_es_specific_format {
2749 +       struct mmal_audio_format audio;
2750 +       struct mmal_video_format video;
2751 +       struct mmal_subpicture_format subpicture;
2752 +};
2753 +
2754 +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
2755 +struct mmal_es_format {
2756 +       u32 type;      /* enum mmal_es_type */
2757 +
2758 +       u32 encoding;  /* FourCC specifying encoding of the elementary stream.*/
2759 +       u32 encoding_variant; /* FourCC specifying the specific
2760 +                              * encoding variant of the elementary
2761 +                              * stream.
2762 +                              */
2763 +
2764 +       union mmal_es_specific_format *es; /* TODO: pointers in
2765 +                                           * message serialisation?!?
2766 +                                           */
2767 +                                           /* Type specific
2768 +                                            * information for the
2769 +                                            * elementary stream
2770 +                                            */
2771 +
2772 +       u32 bitrate;        /**< Bitrate in bits per second */
2773 +       u32 flags; /**< Flags describing properties of the elementary stream. */
2774 +
2775 +       u32 extradata_size;       /**< Size of the codec specific data */
2776 +       u8  *extradata;           /**< Codec specific data */
2777 +};
2778 +
2779 +#endif /* MMAL_MSG_FORMAT_H */
2780 --- /dev/null
2781 +++ b/drivers/media/platform/bcm2835/mmal-msg-port.h
2782 @@ -0,0 +1,107 @@
2783 +/*
2784 + * Broadcom BM2835 V4L2 driver
2785 + *
2786 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2787 + *
2788 + * This file is subject to the terms and conditions of the GNU General Public
2789 + * License.  See the file COPYING in the main directory of this archive
2790 + * for more details.
2791 + *
2792 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2793 + *          Dave Stevenson <dsteve@broadcom.com>
2794 + *          Simon Mellor <simellor@broadcom.com>
2795 + *          Luke Diamand <luked@broadcom.com>
2796 + */
2797 +
2798 +/* MMAL_PORT_TYPE_T */
2799 +enum mmal_port_type {
2800 +       MMAL_PORT_TYPE_UNKNOWN = 0,  /**< Unknown port type */
2801 +       MMAL_PORT_TYPE_CONTROL,      /**< Control port */
2802 +       MMAL_PORT_TYPE_INPUT,        /**< Input port */
2803 +       MMAL_PORT_TYPE_OUTPUT,       /**< Output port */
2804 +       MMAL_PORT_TYPE_CLOCK,        /**< Clock port */
2805 +};
2806 +
2807 +/** The port is pass-through and doesn't need buffer headers allocated */
2808 +#define MMAL_PORT_CAPABILITY_PASSTHROUGH                       0x01
2809 +/** The port wants to allocate the buffer payloads.
2810 + * This signals a preference that payload allocation should be done
2811 + * on this port for efficiency reasons. */
2812 +#define MMAL_PORT_CAPABILITY_ALLOCATION                        0x02
2813 +/** The port supports format change events.
2814 + * This applies to input ports and is used to let the client know
2815 + * whether the port supports being reconfigured via a format
2816 + * change event (i.e. without having to disable the port). */
2817 +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE      0x04
2818 +
2819 +/* mmal port structure (MMAL_PORT_T)
2820 + *
2821 + * most elements are informational only, the pointer values for
2822 + * interogation messages are generally provided as additional
2823 + * strucures within the message. When used to set values only teh
2824 + * buffer_num, buffer_size and userdata parameters are writable.
2825 + */
2826 +struct mmal_port {
2827 +       void *priv; /* Private member used by the framework */
2828 +       const char *name; /* Port name. Used for debugging purposes (RO) */
2829 +
2830 +       u32 type;      /* Type of the port (RO) enum mmal_port_type */
2831 +       u16 index;     /* Index of the port in its type list (RO) */
2832 +       u16 index_all; /* Index of the port in the list of all ports (RO) */
2833 +
2834 +       u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
2835 +       struct mmal_es_format *format; /* Format of the elementary stream */
2836 +
2837 +       u32 buffer_num_min; /* Minimum number of buffers the port
2838 +                            *   requires (RO).  This is set by the
2839 +                            *   component.
2840 +                            */
2841 +
2842 +       u32 buffer_size_min; /* Minimum size of buffers the port
2843 +                             * requires (RO).  This is set by the
2844 +                             * component.
2845 +                             */
2846 +
2847 +       u32 buffer_alignment_min; /* Minimum alignment requirement for
2848 +                                  * the buffers (RO).  A value of
2849 +                                  * zero means no special alignment
2850 +                                  * requirements.  This is set by the
2851 +                                  * component.
2852 +                                  */
2853 +
2854 +       u32 buffer_num_recommended;  /* Number of buffers the port
2855 +                                     * recommends for optimal
2856 +                                     * performance (RO).  A value of
2857 +                                     * zero means no special
2858 +                                     * recommendation.  This is set
2859 +                                     * by the component.
2860 +                                     */
2861 +
2862 +       u32 buffer_size_recommended; /* Size of buffers the port
2863 +                                     * recommends for optimal
2864 +                                     * performance (RO).  A value of
2865 +                                     * zero means no special
2866 +                                     * recommendation.  This is set
2867 +                                     * by the component.
2868 +                                     */
2869 +
2870 +       u32 buffer_num; /* Actual number of buffers the port will use.
2871 +                        * This is set by the client.
2872 +                        */
2873 +
2874 +       u32 buffer_size; /* Actual maximum size of the buffers that
2875 +                         * will be sent to the port. This is set by
2876 +                         * the client.
2877 +                         */
2878 +
2879 +       void *component; /* Component this port belongs to (Read Only) */
2880 +
2881 +       void *userdata; /* Field reserved for use by the client */
2882 +
2883 +       u32 capabilities; /* Flags describing the capabilities of a
2884 +                          * port (RO).  Bitwise combination of \ref
2885 +                          * portcapabilities "Port capabilities"
2886 +                          * values.
2887 +                          */
2888 +
2889 +};
2890 --- /dev/null
2891 +++ b/drivers/media/platform/bcm2835/mmal-msg.h
2892 @@ -0,0 +1,404 @@
2893 +/*
2894 + * Broadcom BM2835 V4L2 driver
2895 + *
2896 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2897 + *
2898 + * This file is subject to the terms and conditions of the GNU General Public
2899 + * License.  See the file COPYING in the main directory of this archive
2900 + * for more details.
2901 + *
2902 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2903 + *          Dave Stevenson <dsteve@broadcom.com>
2904 + *          Simon Mellor <simellor@broadcom.com>
2905 + *          Luke Diamand <luked@broadcom.com>
2906 + */
2907 +
2908 +/* all the data structures which serialise the MMAL protocol. note
2909 + * these are directly mapped onto the recived message data.
2910 + *
2911 + * BEWARE: They seem to *assume* pointers are u32 and that there is no
2912 + * structure padding!
2913 + *
2914 + * NOTE: this implementation uses kernel types to ensure sizes. Rather
2915 + * than assigning values to enums to force their size the
2916 + * implementation uses fixed size types and not the enums (though the
2917 + * comments have the actual enum type
2918 + */
2919 +
2920 +#define VC_MMAL_VER 15
2921 +#define VC_MMAL_MIN_VER 10
2922 +#define VC_MMAL_SERVER_NAME  MAKE_FOURCC("mmal")
2923 +
2924 +/* max total message size is 512 bytes */
2925 +#define MMAL_MSG_MAX_SIZE 512
2926 +/* with six 32bit header elements max payload is therefore 488 bytes */
2927 +#define MMAL_MSG_MAX_PAYLOAD 488
2928 +
2929 +#include "mmal-msg-common.h"
2930 +#include "mmal-msg-format.h"
2931 +#include "mmal-msg-port.h"
2932 +
2933 +enum mmal_msg_type {
2934 +       MMAL_MSG_TYPE_QUIT = 1,
2935 +       MMAL_MSG_TYPE_SERVICE_CLOSED,
2936 +       MMAL_MSG_TYPE_GET_VERSION,
2937 +       MMAL_MSG_TYPE_COMPONENT_CREATE,
2938 +       MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
2939 +       MMAL_MSG_TYPE_COMPONENT_ENABLE,
2940 +       MMAL_MSG_TYPE_COMPONENT_DISABLE,
2941 +       MMAL_MSG_TYPE_PORT_INFO_GET,
2942 +       MMAL_MSG_TYPE_PORT_INFO_SET,
2943 +       MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
2944 +       MMAL_MSG_TYPE_BUFFER_FROM_HOST,
2945 +       MMAL_MSG_TYPE_BUFFER_TO_HOST,
2946 +       MMAL_MSG_TYPE_GET_STATS,
2947 +       MMAL_MSG_TYPE_PORT_PARAMETER_SET,
2948 +       MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
2949 +       MMAL_MSG_TYPE_EVENT_TO_HOST,
2950 +       MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
2951 +       MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
2952 +       MMAL_MSG_TYPE_CONSUME_MEM,
2953 +       MMAL_MSG_TYPE_LMK, /* 20 */
2954 +       MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
2955 +       MMAL_MSG_TYPE_DRM_GET_LHS32,
2956 +       MMAL_MSG_TYPE_DRM_GET_TIME,
2957 +       MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
2958 +       MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
2959 +       MMAL_MSG_TYPE_HOST_LOG,
2960 +       MMAL_MSG_TYPE_MSG_LAST
2961 +};
2962 +
2963 +/* port action request messages differ depending on the action type */
2964 +enum mmal_msg_port_action_type {
2965 +       MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0,      /* Unkown action */
2966 +       MMAL_MSG_PORT_ACTION_TYPE_ENABLE,           /* Enable a port */
2967 +       MMAL_MSG_PORT_ACTION_TYPE_DISABLE,          /* Disable a port */
2968 +       MMAL_MSG_PORT_ACTION_TYPE_FLUSH,            /* Flush a port */
2969 +       MMAL_MSG_PORT_ACTION_TYPE_CONNECT,          /* Connect ports */
2970 +       MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,       /* Disconnect ports */
2971 +       MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
2972 +};
2973 +
2974 +struct mmal_msg_header {
2975 +       u32 magic;
2976 +       u32 type; /** enum mmal_msg_type */
2977 +
2978 +       /* Opaque handle to the control service */
2979 +       struct mmal_control_service *control_service;
2980 +
2981 +       struct mmal_msg_context *context; /** a u32 per message context */
2982 +       u32 status; /** The status of the vchiq operation */
2983 +       u32 padding;
2984 +};
2985 +
2986 +/* Send from VC to host to report version */
2987 +struct mmal_msg_version {
2988 +       u32 flags;
2989 +       u32 major;
2990 +       u32 minor;
2991 +       u32 minimum;
2992 +};
2993 +
2994 +/* request to VC to create component */
2995 +struct mmal_msg_component_create {
2996 +       void *client_component; /* component context */
2997 +       char name[128];
2998 +       u32 pid;                /* For debug */
2999 +};
3000 +
3001 +/* reply from VC to component creation request */
3002 +struct mmal_msg_component_create_reply {
3003 +       u32 status; /** enum mmal_msg_status - how does this differ to
3004 +                    * the one in the header?
3005 +                    */
3006 +       u32 component_handle; /* VideoCore handle for component */
3007 +       u32 input_num;        /* Number of input ports */
3008 +       u32 output_num;       /* Number of output ports */
3009 +       u32 clock_num;        /* Number of clock ports */
3010 +};
3011 +
3012 +/* request to VC to destroy a component */
3013 +struct mmal_msg_component_destroy {
3014 +       u32 component_handle;
3015 +};
3016 +
3017 +struct mmal_msg_component_destroy_reply {
3018 +       u32 status; /** The component destruction status */
3019 +};
3020 +
3021 +
3022 +/* request and reply to VC to enable a component */
3023 +struct mmal_msg_component_enable {
3024 +       u32 component_handle;
3025 +};
3026 +
3027 +struct mmal_msg_component_enable_reply {
3028 +       u32 status; /** The component enable status */
3029 +};
3030 +
3031 +
3032 +/* request and reply to VC to disable a component */
3033 +struct mmal_msg_component_disable {
3034 +       u32 component_handle;
3035 +};
3036 +
3037 +struct mmal_msg_component_disable_reply {
3038 +       u32 status; /** The component disable status */
3039 +};
3040 +
3041 +/* request to VC to get port information */
3042 +struct mmal_msg_port_info_get {
3043 +       u32 component_handle;  /* component handle port is associated with */
3044 +       u32 port_type;         /* enum mmal_msg_port_type */
3045 +       u32 index;             /* port index to query */
3046 +};
3047 +
3048 +/* reply from VC to get port info request */
3049 +struct mmal_msg_port_info_get_reply {
3050 +       u32 status; /** enum mmal_msg_status */
3051 +       u32 component_handle;  /* component handle port is associated with */
3052 +       u32 port_type;         /* enum mmal_msg_port_type */
3053 +       u32 port_index;        /* port indexed in query */
3054 +       s32 found;             /* unused */
3055 +       u32 port_handle;               /**< Handle to use for this port */
3056 +       struct mmal_port port;
3057 +       struct mmal_es_format format; /* elementry stream format */
3058 +       union mmal_es_specific_format es; /* es type specific data */
3059 +       u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
3060 +};
3061 +
3062 +/* request to VC to set port information */
3063 +struct mmal_msg_port_info_set {
3064 +       u32 component_handle;
3065 +       u32 port_type;         /* enum mmal_msg_port_type */
3066 +       u32 port_index;           /* port indexed in query */
3067 +       struct mmal_port port;
3068 +       struct mmal_es_format format;
3069 +       union mmal_es_specific_format es;
3070 +       u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
3071 +};
3072 +
3073 +/* reply from VC to port info set request */
3074 +struct mmal_msg_port_info_set_reply {
3075 +       u32 status;
3076 +       u32 component_handle;  /* component handle port is associated with */
3077 +       u32 port_type;         /* enum mmal_msg_port_type */
3078 +       u32 index;             /* port indexed in query */
3079 +       s32 found;             /* unused */
3080 +       u32 port_handle;               /**< Handle to use for this port */
3081 +       struct mmal_port port;
3082 +       struct mmal_es_format format;
3083 +       union mmal_es_specific_format es;
3084 +       u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
3085 +};
3086 +
3087 +
3088 +/* port action requests that take a mmal_port as a parameter */
3089 +struct mmal_msg_port_action_port {
3090 +       u32 component_handle;
3091 +       u32 port_handle;
3092 +       u32 action; /* enum mmal_msg_port_action_type */
3093 +       struct mmal_port port;
3094 +};
3095 +
3096 +/* port action requests that take handles as a parameter */
3097 +struct mmal_msg_port_action_handle {
3098 +       u32 component_handle;
3099 +       u32 port_handle;
3100 +       u32 action; /* enum mmal_msg_port_action_type */
3101 +       u32 connect_component_handle;
3102 +       u32 connect_port_handle;
3103 +};
3104 +
3105 +struct mmal_msg_port_action_reply {
3106 +       u32 status; /** The port action operation status */
3107 +};
3108 +
3109 +
3110 +
3111 +
3112 +/* MMAL buffer transfer */
3113 +
3114 +/** Size of space reserved in a buffer message for short messages. */
3115 +#define MMAL_VC_SHORT_DATA 128
3116 +
3117 +/** Signals that the current payload is the end of the stream of data */
3118 +#define MMAL_BUFFER_HEADER_FLAG_EOS                    (1<<0)
3119 +/** Signals that the start of the current payload starts a frame */
3120 +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START            (1<<1)
3121 +/** Signals that the end of the current payload ends a frame */
3122 +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END              (1<<2)
3123 +/** Signals that the current payload contains only complete frames (>1) */
3124 +#define MMAL_BUFFER_HEADER_FLAG_FRAME                  \
3125 +       (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
3126 +/** Signals that the current payload is a keyframe (i.e. self decodable) */
3127 +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME               (1<<3)
3128 +/** Signals a discontinuity in the stream of data (e.g. after a seek).
3129 + * Can be used for instance by a decoder to reset its state */
3130 +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY          (1<<4)
3131 +/** Signals a buffer containing some kind of config data for the component
3132 + * (e.g. codec config data) */
3133 +#define MMAL_BUFFER_HEADER_FLAG_CONFIG                 (1<<5)
3134 +/** Signals an encrypted payload */
3135 +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED              (1<<6)
3136 +/** Signals a buffer containing side information */
3137 +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO          (1<<7)
3138 +/** Signals a buffer which is the snapshot/postview image from a stills
3139 + * capture
3140 + */
3141 +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT              (1<<8)
3142 +/** Signals a buffer which contains data known to be corrupted */
3143 +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED              (1<<9)
3144 +/** Signals that a buffer failed to be transmitted */
3145 +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED    (1<<10)
3146 +
3147 +struct mmal_driver_buffer {
3148 +       u32 magic;
3149 +       u32 component_handle;
3150 +       u32 port_handle;
3151 +       void *client_context;
3152 +};
3153 +
3154 +/* buffer header */
3155 +struct mmal_buffer_header {
3156 +       struct mmal_buffer_header *next; /* next header */
3157 +       void *priv; /* framework private data */
3158 +       u32 cmd;
3159 +       void *data;
3160 +       u32 alloc_size;
3161 +       u32 length;
3162 +       u32 offset;
3163 +       u32 flags;
3164 +       s64 pts;
3165 +       s64 dts;
3166 +       void *type;
3167 +       void *user_data;
3168 +};
3169 +
3170 +struct mmal_buffer_header_type_specific {
3171 +       union {
3172 +               struct {
3173 +               u32 planes;
3174 +               u32 offset[4];
3175 +               u32 pitch[4];
3176 +               u32 flags;
3177 +               } video;
3178 +       } u;
3179 +};
3180 +
3181 +struct mmal_msg_buffer_from_host {
3182 +       /* The front 32 bytes of the buffer header are copied
3183 +        * back to us in the reply to allow for context. This
3184 +        * area is used to store two mmal_driver_buffer structures to
3185 +        * allow for multiple concurrent service users.
3186 +        */
3187 +       /* control data */
3188 +       struct mmal_driver_buffer drvbuf;
3189 +
3190 +       /* referenced control data for passthrough buffer management */
3191 +       struct mmal_driver_buffer drvbuf_ref;
3192 +       struct mmal_buffer_header buffer_header; /* buffer header itself */
3193 +       struct mmal_buffer_header_type_specific buffer_header_type_specific;
3194 +       s32 is_zero_copy;
3195 +       s32 has_reference;
3196 +
3197 +       /** allows short data to be xfered in control message */
3198 +       u32 payload_in_message;
3199 +       u8 short_data[MMAL_VC_SHORT_DATA];
3200 +};
3201 +
3202 +
3203 +/* port parameter setting */
3204 +
3205 +#define MMAL_WORKER_PORT_PARAMETER_SPACE      96
3206 +
3207 +struct mmal_msg_port_parameter_set {
3208 +       u32 component_handle; /* component */
3209 +       u32 port_handle;      /* port */
3210 +       u32 id;     /* Parameter ID  */
3211 +       u32 size;      /* Parameter size */
3212 +       uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
3213 +};
3214 +
3215 +struct mmal_msg_port_parameter_set_reply {
3216 +       u32 status; /** enum mmal_msg_status todo: how does this
3217 +                    * differ to the one in the header?
3218 +                    */
3219 +};
3220 +
3221 +/* port parameter getting */
3222 +
3223 +struct mmal_msg_port_parameter_get {
3224 +       u32 component_handle; /* component */
3225 +       u32 port_handle;      /* port */
3226 +       u32 id;     /* Parameter ID  */
3227 +       u32 size;      /* Parameter size */
3228 +};
3229 +
3230 +struct mmal_msg_port_parameter_get_reply {
3231 +       u32 status;           /* Status of mmal_port_parameter_get call */
3232 +       u32 id;     /* Parameter ID  */
3233 +       u32 size;      /* Parameter size */
3234 +       uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
3235 +};
3236 +
3237 +/* event messages */
3238 +#define MMAL_WORKER_EVENT_SPACE 256
3239 +
3240 +struct mmal_msg_event_to_host {
3241 +       void *client_component; /* component context */
3242 +
3243 +       u32 port_type;
3244 +       u32 port_num;
3245 +
3246 +       u32 cmd;
3247 +       u32 length;
3248 +       u8 data[MMAL_WORKER_EVENT_SPACE];
3249 +       struct mmal_buffer_header *delayed_buffer;
3250 +};
3251 +
3252 +/* all mmal messages are serialised through this structure */
3253 +struct mmal_msg {
3254 +       /* header */
3255 +       struct mmal_msg_header h;
3256 +       /* payload */
3257 +       union {
3258 +               struct mmal_msg_version version;
3259 +
3260 +               struct mmal_msg_component_create component_create;
3261 +               struct mmal_msg_component_create_reply component_create_reply;
3262 +
3263 +               struct mmal_msg_component_destroy component_destroy;
3264 +               struct mmal_msg_component_destroy_reply component_destroy_reply;
3265 +
3266 +               struct mmal_msg_component_enable component_enable;
3267 +               struct mmal_msg_component_enable_reply component_enable_reply;
3268 +
3269 +               struct mmal_msg_component_disable component_disable;
3270 +               struct mmal_msg_component_disable_reply component_disable_reply;
3271 +
3272 +               struct mmal_msg_port_info_get port_info_get;
3273 +               struct mmal_msg_port_info_get_reply port_info_get_reply;
3274 +
3275 +               struct mmal_msg_port_info_set port_info_set;
3276 +               struct mmal_msg_port_info_set_reply port_info_set_reply;
3277 +
3278 +               struct mmal_msg_port_action_port port_action_port;
3279 +               struct mmal_msg_port_action_handle port_action_handle;
3280 +               struct mmal_msg_port_action_reply port_action_reply;
3281 +
3282 +               struct mmal_msg_buffer_from_host buffer_from_host;
3283 +
3284 +               struct mmal_msg_port_parameter_set port_parameter_set;
3285 +               struct mmal_msg_port_parameter_set_reply
3286 +                       port_parameter_set_reply;
3287 +               struct mmal_msg_port_parameter_get
3288 +                       port_parameter_get;
3289 +               struct mmal_msg_port_parameter_get_reply
3290 +                       port_parameter_get_reply;
3291 +
3292 +               struct mmal_msg_event_to_host event_to_host;
3293 +
3294 +               u8 payload[MMAL_MSG_MAX_PAYLOAD];
3295 +       } u;
3296 +};
3297 --- /dev/null
3298 +++ b/drivers/media/platform/bcm2835/mmal-parameters.h
3299 @@ -0,0 +1,539 @@
3300 +/*
3301 + * Broadcom BM2835 V4L2 driver
3302 + *
3303 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
3304 + *
3305 + * This file is subject to the terms and conditions of the GNU General Public
3306 + * License.  See the file COPYING in the main directory of this archive
3307 + * for more details.
3308 + *
3309 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
3310 + *          Dave Stevenson <dsteve@broadcom.com>
3311 + *          Simon Mellor <simellor@broadcom.com>
3312 + *          Luke Diamand <luked@broadcom.com>
3313 + */
3314 +
3315 +/* common parameters */
3316 +
3317 +/** @name Parameter groups
3318 + * Parameters are divided into groups, and then allocated sequentially within
3319 + * a group using an enum.
3320 + * @{
3321 + */
3322 +
3323 +/** Common parameter ID group, used with many types of component. */
3324 +#define MMAL_PARAMETER_GROUP_COMMON            (0<<16)
3325 +/** Camera-specific parameter ID group. */
3326 +#define MMAL_PARAMETER_GROUP_CAMERA            (1<<16)
3327 +/** Video-specific parameter ID group. */
3328 +#define MMAL_PARAMETER_GROUP_VIDEO             (2<<16)
3329 +/** Audio-specific parameter ID group. */
3330 +#define MMAL_PARAMETER_GROUP_AUDIO             (3<<16)
3331 +/** Clock-specific parameter ID group. */
3332 +#define MMAL_PARAMETER_GROUP_CLOCK             (4<<16)
3333 +/** Miracast-specific parameter ID group. */
3334 +#define MMAL_PARAMETER_GROUP_MIRACAST       (5<<16)
3335 +
3336 +/* Common parameters */
3337 +enum mmal_parameter_common_type {
3338 +       MMAL_PARAMETER_UNUSED  /**< Never a valid parameter ID */
3339 +               = MMAL_PARAMETER_GROUP_COMMON,
3340 +       MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
3341 +       MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
3342 +
3343 +       /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
3344 +       MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
3345 +
3346 +       /** MMAL_PARAMETER_BOOLEAN_T */
3347 +       MMAL_PARAMETER_ZERO_COPY,
3348 +
3349 +       /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
3350 +       MMAL_PARAMETER_BUFFER_REQUIREMENTS,
3351 +
3352 +       MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
3353 +       MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
3354 +       MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
3355 +       MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
3356 +       MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
3357 +       MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
3358 +       MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
3359 +       MMAL_PARAMETER_SYSTEM_TIME /**< MMAL_PARAMETER_UINT64_T */
3360 +};
3361 +
3362 +/* camera parameters */
3363 +
3364 +enum mmal_parameter_camera_type {
3365 +       /* 0 */
3366 +       /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
3367 +       MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
3368 +               = MMAL_PARAMETER_GROUP_CAMERA,
3369 +       MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
3370 +       MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
3371 +       MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3372 +       MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
3373 +       MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
3374 +       MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
3375 +       MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
3376 +       MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
3377 +       MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
3378 +       MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
3379 +       MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
3380 +       MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
3381 +       MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
3382 +       MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
3383 +       MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
3384 +
3385 +       /* 0x10 */
3386 +       MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
3387 +       MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3388 +       MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
3389 +       MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
3390 +       MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
3391 +       MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
3392 +       MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
3393 +       MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
3394 +       MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3395 +       MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
3396 +       MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
3397 +       MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
3398 +       MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
3399 +       MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3400 +       MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
3401 +       MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3402 +
3403 +       /* 0x20 */
3404 +       MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
3405 +       MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3406 +       MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3407 +       MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
3408 +       MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
3409 +       MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
3410 +       MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
3411 +       MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
3412 +       MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
3413 +       MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3414 +       MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
3415 +       MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
3416 +       MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
3417 +       MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
3418 +       MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
3419 +       MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
3420 +
3421 +       /* 0x30 */
3422 +       MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
3423 +       MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3424 +
3425 +       /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
3426 +       MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
3427 +
3428 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3429 +       MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
3430 +
3431 +       /** @ref MMAL_PARAMETER_UINT32_T */
3432 +       MMAL_PARAMETER_CAMERA_MIN_ISO,
3433 +
3434 +       /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
3435 +       MMAL_PARAMETER_CAMERA_USE_CASE,
3436 +
3437 +       /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3438 +       MMAL_PARAMETER_CAPTURE_STATS_PASS,
3439 +
3440 +       /** @ref MMAL_PARAMETER_UINT32_T */
3441 +       MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
3442 +
3443 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3444 +       MMAL_PARAMETER_ENABLE_REGISTER_FILE,
3445 +
3446 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3447 +       MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
3448 +
3449 +       /** @ref MMAL_PARAMETER_CONFIGFILE_T */
3450 +       MMAL_PARAMETER_CONFIGFILE_REGISTERS,
3451 +
3452 +       /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
3453 +       MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
3454 +       MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3455 +       MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
3456 +       MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
3457 +       MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
3458 +
3459 +       /* 0x40 */
3460 +       MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3461 +       MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3462 +       MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
3463 +};
3464 +
3465 +enum mmal_parameter_camera_config_timestamp_mode {
3466 +       MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
3467 +       MMAL_PARAM_TIMESTAMP_MODE_RAW_STC,  /* Use the raw STC value
3468 +                                            * for the frame timestamp
3469 +                                            */
3470 +       MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
3471 +                                             * but subtract the
3472 +                                             * timestamp of the first
3473 +                                             * frame sent to give a
3474 +                                             * zero based timestamp.
3475 +                                             */
3476 +};
3477 +
3478 +/* camera configuration parameter */
3479 +struct mmal_parameter_camera_config {
3480 +       /* Parameters for setting up the image pools */
3481 +       u32 max_stills_w; /* Max size of stills capture */
3482 +       u32 max_stills_h;
3483 +       u32 stills_yuv422; /* Allow YUV422 stills capture */
3484 +       u32 one_shot_stills; /* Continuous or one shot stills captures. */
3485 +
3486 +       u32 max_preview_video_w; /* Max size of the preview or video
3487 +                                 * capture frames
3488 +                                 */
3489 +       u32 max_preview_video_h;
3490 +       u32 num_preview_video_frames;
3491 +
3492 +       /** Sets the height of the circular buffer for stills capture. */
3493 +       u32 stills_capture_circular_buffer_height;
3494 +
3495 +       /** Allows preview/encode to resume as fast as possible after the stills
3496 +        * input frame has been received, and then processes the still frame in
3497 +        * the background whilst preview/encode has resumed.
3498 +        * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
3499 +        */
3500 +       u32 fast_preview_resume;
3501 +
3502 +       /** Selects algorithm for timestamping frames if
3503 +        * there is no clock component connected.
3504 +        * enum mmal_parameter_camera_config_timestamp_mode
3505 +        */
3506 +       s32 use_stc_timestamp;
3507 +};
3508 +
3509 +
3510 +enum mmal_parameter_exposuremode {
3511 +       MMAL_PARAM_EXPOSUREMODE_OFF,
3512 +       MMAL_PARAM_EXPOSUREMODE_AUTO,
3513 +       MMAL_PARAM_EXPOSUREMODE_NIGHT,
3514 +       MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
3515 +       MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
3516 +       MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
3517 +       MMAL_PARAM_EXPOSUREMODE_SPORTS,
3518 +       MMAL_PARAM_EXPOSUREMODE_SNOW,
3519 +       MMAL_PARAM_EXPOSUREMODE_BEACH,
3520 +       MMAL_PARAM_EXPOSUREMODE_VERYLONG,
3521 +       MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
3522 +       MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
3523 +       MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
3524 +};
3525 +
3526 +enum mmal_parameter_exposuremeteringmode {
3527 +       MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
3528 +       MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
3529 +       MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
3530 +       MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
3531 +};
3532 +
3533 +enum mmal_parameter_awbmode {
3534 +       MMAL_PARAM_AWBMODE_OFF,
3535 +       MMAL_PARAM_AWBMODE_AUTO,
3536 +       MMAL_PARAM_AWBMODE_SUNLIGHT,
3537 +       MMAL_PARAM_AWBMODE_CLOUDY,
3538 +       MMAL_PARAM_AWBMODE_SHADE,
3539 +       MMAL_PARAM_AWBMODE_TUNGSTEN,
3540 +       MMAL_PARAM_AWBMODE_FLUORESCENT,
3541 +       MMAL_PARAM_AWBMODE_INCANDESCENT,
3542 +       MMAL_PARAM_AWBMODE_FLASH,
3543 +       MMAL_PARAM_AWBMODE_HORIZON,
3544 +};
3545 +
3546 +enum mmal_parameter_imagefx {
3547 +       MMAL_PARAM_IMAGEFX_NONE,
3548 +       MMAL_PARAM_IMAGEFX_NEGATIVE,
3549 +       MMAL_PARAM_IMAGEFX_SOLARIZE,
3550 +       MMAL_PARAM_IMAGEFX_POSTERIZE,
3551 +       MMAL_PARAM_IMAGEFX_WHITEBOARD,
3552 +       MMAL_PARAM_IMAGEFX_BLACKBOARD,
3553 +       MMAL_PARAM_IMAGEFX_SKETCH,
3554 +       MMAL_PARAM_IMAGEFX_DENOISE,
3555 +       MMAL_PARAM_IMAGEFX_EMBOSS,
3556 +       MMAL_PARAM_IMAGEFX_OILPAINT,
3557 +       MMAL_PARAM_IMAGEFX_HATCH,
3558 +       MMAL_PARAM_IMAGEFX_GPEN,
3559 +       MMAL_PARAM_IMAGEFX_PASTEL,
3560 +       MMAL_PARAM_IMAGEFX_WATERCOLOUR,
3561 +       MMAL_PARAM_IMAGEFX_FILM,
3562 +       MMAL_PARAM_IMAGEFX_BLUR,
3563 +       MMAL_PARAM_IMAGEFX_SATURATION,
3564 +       MMAL_PARAM_IMAGEFX_COLOURSWAP,
3565 +       MMAL_PARAM_IMAGEFX_WASHEDOUT,
3566 +       MMAL_PARAM_IMAGEFX_POSTERISE,
3567 +       MMAL_PARAM_IMAGEFX_COLOURPOINT,
3568 +       MMAL_PARAM_IMAGEFX_COLOURBALANCE,
3569 +       MMAL_PARAM_IMAGEFX_CARTOON,
3570 +};
3571 +
3572 +/** Manner of video rate control */
3573 +enum mmal_parameter_rate_control_mode {
3574 +       MMAL_VIDEO_RATECONTROL_DEFAULT,
3575 +       MMAL_VIDEO_RATECONTROL_VARIABLE,
3576 +       MMAL_VIDEO_RATECONTROL_CONSTANT,
3577 +       MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
3578 +       MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
3579 +};
3580 +
3581 +/* video parameters */
3582 +
3583 +enum mmal_parameter_video_type {
3584 +       /** @ref MMAL_DISPLAYREGION_T */
3585 +       MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
3586 +
3587 +       /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
3588 +       MMAL_PARAMETER_SUPPORTED_PROFILES,
3589 +
3590 +       /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
3591 +       MMAL_PARAMETER_PROFILE,
3592 +
3593 +       /** @ref MMAL_PARAMETER_UINT32_T */
3594 +       MMAL_PARAMETER_INTRAPERIOD,
3595 +
3596 +       /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
3597 +       MMAL_PARAMETER_RATECONTROL,
3598 +
3599 +       /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
3600 +       MMAL_PARAMETER_NALUNITFORMAT,
3601 +
3602 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3603 +       MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
3604 +
3605 +       /** @ref MMAL_PARAMETER_UINT32_T.
3606 +        * Setting the value to zero resets to the default (one slice per frame).
3607 +        */
3608 +       MMAL_PARAMETER_MB_ROWS_PER_SLICE,
3609 +
3610 +       /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
3611 +       MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
3612 +
3613 +       /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
3614 +       MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
3615 +
3616 +       /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
3617 +       MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
3618 +
3619 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
3620 +       MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
3621 +       /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
3622 +       MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
3623 +
3624 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. */
3625 +       MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
3626 +
3627 +       /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
3628 +       MMAL_PARAMETER_VIDEO_BIT_RATE,
3629 +
3630 +       /** @ref MMAL_PARAMETER_FRAME_RATE_T */
3631 +       MMAL_PARAMETER_VIDEO_FRAME_RATE,
3632 +
3633 +       /** @ref MMAL_PARAMETER_UINT32_T. */
3634 +       MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
3635 +
3636 +       /** @ref MMAL_PARAMETER_UINT32_T. */
3637 +       MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
3638 +
3639 +       /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
3640 +       MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
3641 +
3642 +       MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
3643 +       /** @ref MMAL_PARAMETER_UINT32_T.
3644 +        * Changing this parameter from the default can reduce frame rate
3645 +        * because image buffers need to be re-pitched.
3646 +        */
3647 +       MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
3648 +
3649 +       /** @ref MMAL_PARAMETER_UINT32_T.
3650 +        * Changing this parameter from the default can reduce frame rate
3651 +        * because image buffers need to be re-pitched.
3652 +        */
3653 +       MMAL_PARAMETER_VIDEO_ALIGN_VERT,
3654 +
3655 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. */
3656 +       MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
3657 +
3658 +       /** @ref MMAL_PARAMETER_UINT32_T. */
3659 +       MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
3660 +
3661 +       /**< @ref MMAL_PARAMETER_UINT32_T. */
3662 +       MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
3663 +
3664 +       /**< @ref MMAL_PARAMETER_UINT32_T. */
3665 +       MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
3666 +
3667 +       /** @ref MMAL_PARAMETER_UINT32_T */
3668 +       MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
3669 +
3670 +       /** @ref MMAL_PARAMETER_UINT32_T. */
3671 +       MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
3672 +
3673 +       /* H264 specific parameters */
3674 +
3675 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. */
3676 +       MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
3677 +
3678 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. */
3679 +       MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
3680 +
3681 +       /** @ref MMAL_PARAMETER_BOOLEAN_T. */
3682 +       MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
3683 +
3684 +       /** @ref MMAL_PARAMETER_UINT32_T. */
3685 +       MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
3686 +
3687 +       /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
3688 +       MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
3689 +
3690 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3691 +       MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
3692 +
3693 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3694 +       MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
3695 +
3696 +       /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
3697 +       MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
3698 +
3699 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3700 +       MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
3701 +
3702 +       /** @ref MMAL_PARAMETER_BOOLEAN_T */
3703 +       MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
3704 +
3705 +       /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
3706 +       MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
3707 +
3708 +       /** @ref MMAL_PARAMETER_BYTES_T */
3709 +       MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3
3710 +};
3711 +
3712 +/** Valid mirror modes */
3713 +enum mmal_parameter_mirror {
3714 +       MMAL_PARAM_MIRROR_NONE,
3715 +       MMAL_PARAM_MIRROR_VERTICAL,
3716 +       MMAL_PARAM_MIRROR_HORIZONTAL,
3717 +       MMAL_PARAM_MIRROR_BOTH,
3718 +};
3719 +
3720 +enum mmal_parameter_displaytransform {
3721 +       MMAL_DISPLAY_ROT0 = 0,
3722 +       MMAL_DISPLAY_MIRROR_ROT0 = 1,
3723 +       MMAL_DISPLAY_MIRROR_ROT180 = 2,
3724 +       MMAL_DISPLAY_ROT180 = 3,
3725 +       MMAL_DISPLAY_MIRROR_ROT90 = 4,
3726 +       MMAL_DISPLAY_ROT270 = 5,
3727 +       MMAL_DISPLAY_ROT90 = 6,
3728 +       MMAL_DISPLAY_MIRROR_ROT270 = 7,
3729 +};
3730 +
3731 +enum mmal_parameter_displaymode {
3732 +       MMAL_DISPLAY_MODE_FILL = 0,
3733 +       MMAL_DISPLAY_MODE_LETTERBOX = 1,
3734 +};
3735 +
3736 +enum mmal_parameter_displayset {
3737 +       MMAL_DISPLAY_SET_NONE = 0,
3738 +       MMAL_DISPLAY_SET_NUM = 1,
3739 +       MMAL_DISPLAY_SET_FULLSCREEN = 2,
3740 +       MMAL_DISPLAY_SET_TRANSFORM = 4,
3741 +       MMAL_DISPLAY_SET_DEST_RECT = 8,
3742 +       MMAL_DISPLAY_SET_SRC_RECT = 0x10,
3743 +       MMAL_DISPLAY_SET_MODE = 0x20,
3744 +       MMAL_DISPLAY_SET_PIXEL = 0x40,
3745 +       MMAL_DISPLAY_SET_NOASPECT = 0x80,
3746 +       MMAL_DISPLAY_SET_LAYER = 0x100,
3747 +       MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
3748 +       MMAL_DISPLAY_SET_ALPHA = 0x400,
3749 +};
3750 +
3751 +struct mmal_parameter_displayregion {
3752 +       /** Bitfield that indicates which fields are set and should be
3753 +        * used. All other fields will maintain their current value.
3754 +        * \ref MMAL_DISPLAYSET_T defines the bits that can be
3755 +        * combined.
3756 +        */
3757 +       u32 set;
3758 +
3759 +       /** Describes the display output device, with 0 typically
3760 +        * being a directly connected LCD display.  The actual values
3761 +        * will depend on the hardware.  Code using hard-wired numbers
3762 +        * (e.g. 2) is certain to fail.
3763 +        */
3764 +
3765 +       u32 display_num;
3766 +       /** Indicates that we are using the full device screen area,
3767 +        * rather than a window of the display.  If zero, then
3768 +        * dest_rect is used to specify a region of the display to
3769 +        * use.
3770 +        */
3771 +
3772 +       s32 fullscreen;
3773 +       /** Indicates any rotation or flipping used to map frames onto
3774 +        * the natural display orientation.
3775 +        */
3776 +       u32 transform; /* enum mmal_parameter_displaytransform */
3777 +
3778 +       /** Where to display the frame within the screen, if
3779 +        * fullscreen is zero.
3780 +        */
3781 +       struct vchiq_mmal_rect dest_rect;
3782 +
3783 +       /** Indicates which area of the frame to display. If all
3784 +        * values are zero, the whole frame will be used.
3785 +        */
3786 +       struct vchiq_mmal_rect src_rect;
3787 +
3788 +       /** If set to non-zero, indicates that any display scaling
3789 +        * should disregard the aspect ratio of the frame region being
3790 +        * displayed.
3791 +        */
3792 +       s32 noaspect;
3793 +
3794 +       /** Indicates how the image should be scaled to fit the
3795 +        * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
3796 +        * that the image should fill the screen by potentially
3797 +        * cropping the frames.  Setting \code mode \endcode to \code
3798 +        * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
3799 +        * source region should be displayed and black bars added if
3800 +        * necessary.
3801 +        */
3802 +       u32 mode; /* enum mmal_parameter_displaymode */
3803 +
3804 +       /** If non-zero, defines the width of a source pixel relative
3805 +        * to \code pixel_y \endcode.  If zero, then pixels default to
3806 +        * being square.
3807 +        */
3808 +       u32 pixel_x;
3809 +
3810 +       /** If non-zero, defines the height of a source pixel relative
3811 +        * to \code pixel_x \endcode.  If zero, then pixels default to
3812 +        * being square.
3813 +        */
3814 +       u32 pixel_y;
3815 +
3816 +       /** Sets the relative depth of the images, with greater values
3817 +        * being in front of smaller values.
3818 +        */
3819 +       u32 layer;
3820 +
3821 +       /** Set to non-zero to ensure copy protection is used on
3822 +        * output.
3823 +        */
3824 +       s32 copyprotect_required;
3825 +
3826 +       /** Level of opacity of the layer, where zero is fully
3827 +        * transparent and 255 is fully opaque.
3828 +        */
3829 +       u32 alpha;
3830 +};
3831 +
3832 +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
3833 +
3834 +struct mmal_parameter_imagefx_parameters {
3835 +       enum mmal_parameter_imagefx effect;
3836 +       u32 num_effect_params;
3837 +       u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
3838 +};
3839 --- /dev/null
3840 +++ b/drivers/media/platform/bcm2835/mmal-vchiq.c
3841 @@ -0,0 +1,1916 @@
3842 +/*
3843 + * Broadcom BM2835 V4L2 driver
3844 + *
3845 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
3846 + *
3847 + * This file is subject to the terms and conditions of the GNU General Public
3848 + * License.  See the file COPYING in the main directory of this archive
3849 + * for more details.
3850 + *
3851 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
3852 + *          Dave Stevenson <dsteve@broadcom.com>
3853 + *          Simon Mellor <simellor@broadcom.com>
3854 + *          Luke Diamand <luked@broadcom.com>
3855 + *
3856 + * V4L2 driver MMAL vchiq interface code
3857 + */
3858 +
3859 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3860 +
3861 +#include <linux/errno.h>
3862 +#include <linux/kernel.h>
3863 +#include <linux/mutex.h>
3864 +#include <linux/mm.h>
3865 +#include <linux/slab.h>
3866 +#include <linux/completion.h>
3867 +#include <linux/vmalloc.h>
3868 +#include <asm/cacheflush.h>
3869 +#include <media/videobuf2-vmalloc.h>
3870 +
3871 +#include "mmal-common.h"
3872 +#include "mmal-vchiq.h"
3873 +#include "mmal-msg.h"
3874 +
3875 +#define USE_VCHIQ_ARM
3876 +#include "interface/vchi/vchi.h"
3877 +
3878 +/* maximum number of components supported */
3879 +#define VCHIQ_MMAL_MAX_COMPONENTS 4
3880 +
3881 +/*#define FULL_MSG_DUMP 1*/
3882 +
3883 +#ifdef DEBUG
3884 +static const char *const msg_type_names[] = {
3885 +       "UNKNOWN",
3886 +       "QUIT",
3887 +       "SERVICE_CLOSED",
3888 +       "GET_VERSION",
3889 +       "COMPONENT_CREATE",
3890 +       "COMPONENT_DESTROY",
3891 +       "COMPONENT_ENABLE",
3892 +       "COMPONENT_DISABLE",
3893 +       "PORT_INFO_GET",
3894 +       "PORT_INFO_SET",
3895 +       "PORT_ACTION",
3896 +       "BUFFER_FROM_HOST",
3897 +       "BUFFER_TO_HOST",
3898 +       "GET_STATS",
3899 +       "PORT_PARAMETER_SET",
3900 +       "PORT_PARAMETER_GET",
3901 +       "EVENT_TO_HOST",
3902 +       "GET_CORE_STATS_FOR_PORT",
3903 +       "OPAQUE_ALLOCATOR",
3904 +       "CONSUME_MEM",
3905 +       "LMK",
3906 +       "OPAQUE_ALLOCATOR_DESC",
3907 +       "DRM_GET_LHS32",
3908 +       "DRM_GET_TIME",
3909 +       "BUFFER_FROM_HOST_ZEROLEN",
3910 +       "PORT_FLUSH",
3911 +       "HOST_LOG",
3912 +};
3913 +#endif
3914 +
3915 +static const char *const port_action_type_names[] = {
3916 +       "UNKNOWN",
3917 +       "ENABLE",
3918 +       "DISABLE",
3919 +       "FLUSH",
3920 +       "CONNECT",
3921 +       "DISCONNECT",
3922 +       "SET_REQUIREMENTS",
3923 +};
3924 +
3925 +#if defined(DEBUG)
3926 +#if defined(FULL_MSG_DUMP)
3927 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)                              \
3928 +       do {                                                            \
3929 +               pr_debug(TITLE" type:%s(%d) length:%d\n",               \
3930 +                        msg_type_names[(MSG)->h.type],                 \
3931 +                        (MSG)->h.type, (MSG_LEN));                     \
3932 +               print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
3933 +                              16, 4, (MSG),                            \
3934 +                              sizeof(struct mmal_msg_header), 1);      \
3935 +               print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
3936 +                              16, 4,                                   \
3937 +                              ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
3938 +                              (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
3939 +       } while (0)
3940 +#else
3941 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)                              \
3942 +       {                                                               \
3943 +               pr_debug(TITLE" type:%s(%d) length:%d\n",               \
3944 +                        msg_type_names[(MSG)->h.type],                 \
3945 +                        (MSG)->h.type, (MSG_LEN));                     \
3946 +       }
3947 +#endif
3948 +#else
3949 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
3950 +#endif
3951 +
3952 +/* normal message context */
3953 +struct mmal_msg_context {
3954 +       union {
3955 +               struct {
3956 +                       /* work struct for defered callback - must come first */
3957 +                       struct work_struct work;
3958 +                       /* mmal instance */
3959 +                       struct vchiq_mmal_instance *instance;
3960 +                       /* mmal port */
3961 +                       struct vchiq_mmal_port *port;
3962 +                       /* actual buffer used to store bulk reply */
3963 +                       struct mmal_buffer *buffer;
3964 +                       /* amount of buffer used */
3965 +                       unsigned long buffer_used;
3966 +                       /* MMAL buffer flags */
3967 +                       u32 mmal_flags;
3968 +                       /* Presentation and Decode timestamps */
3969 +                       s64 pts;
3970 +                       s64 dts;
3971 +
3972 +                       int status;     /* context status */
3973 +
3974 +               } bulk;         /* bulk data */
3975 +
3976 +               struct {
3977 +                       /* message handle to release */
3978 +                       VCHI_HELD_MSG_T msg_handle;
3979 +                       /* pointer to received message */
3980 +                       struct mmal_msg *msg;
3981 +                       /* received message length */
3982 +                       u32 msg_len;
3983 +                       /* completion upon reply */
3984 +                       struct completion cmplt;
3985 +               } sync;         /* synchronous response */
3986 +       } u;
3987 +
3988 +};
3989 +
3990 +struct vchiq_mmal_instance {
3991 +       VCHI_SERVICE_HANDLE_T handle;
3992 +
3993 +       /* ensure serialised access to service */
3994 +       struct mutex vchiq_mutex;
3995 +
3996 +       /* ensure serialised access to bulk operations */
3997 +       struct mutex bulk_mutex;
3998 +
3999 +       /* vmalloc page to receive scratch bulk xfers into */
4000 +       void *bulk_scratch;
4001 +
4002 +       /* component to use next */
4003 +       int component_idx;
4004 +       struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
4005 +};
4006 +
4007 +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
4008 +                                               *instance)
4009 +{
4010 +       struct mmal_msg_context *msg_context;
4011 +
4012 +       /* todo: should this be allocated from a pool to avoid kmalloc */
4013 +       msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
4014 +       memset(msg_context, 0, sizeof(*msg_context));
4015 +
4016 +       return msg_context;
4017 +}
4018 +
4019 +static void release_msg_context(struct mmal_msg_context *msg_context)
4020 +{
4021 +       kfree(msg_context);
4022 +}
4023 +
4024 +/* deals with receipt of event to host message */
4025 +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
4026 +                            struct mmal_msg *msg, u32 msg_len)
4027 +{
4028 +       pr_debug("unhandled event\n");
4029 +       pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
4030 +                msg->u.event_to_host.client_component,
4031 +                msg->u.event_to_host.port_type,
4032 +                msg->u.event_to_host.port_num,
4033 +                msg->u.event_to_host.cmd, msg->u.event_to_host.length);
4034 +}
4035 +
4036 +/* workqueue scheduled callback
4037 + *
4038 + * we do this because it is important we do not call any other vchiq
4039 + * sync calls from witin the message delivery thread
4040 + */
4041 +static void buffer_work_cb(struct work_struct *work)
4042 +{
4043 +       struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
4044 +
4045 +       msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
4046 +                                           msg_context->u.bulk.port,
4047 +                                           msg_context->u.bulk.status,
4048 +                                           msg_context->u.bulk.buffer,
4049 +                                           msg_context->u.bulk.buffer_used,
4050 +                                           msg_context->u.bulk.mmal_flags,
4051 +                                           msg_context->u.bulk.dts,
4052 +                                           msg_context->u.bulk.pts);
4053 +
4054 +       /* release message context */
4055 +       release_msg_context(msg_context);
4056 +}
4057 +
4058 +/* enqueue a bulk receive for a given message context */
4059 +static int bulk_receive(struct vchiq_mmal_instance *instance,
4060 +                       struct mmal_msg *msg,
4061 +                       struct mmal_msg_context *msg_context)
4062 +{
4063 +       unsigned long rd_len;
4064 +       unsigned long flags = 0;
4065 +       int ret;
4066 +
4067 +       /* bulk mutex stops other bulk operations while we have a
4068 +        * receive in progress - released in callback
4069 +        */
4070 +       ret = mutex_lock_interruptible(&instance->bulk_mutex);
4071 +       if (ret != 0)
4072 +               return ret;
4073 +
4074 +       rd_len = msg->u.buffer_from_host.buffer_header.length;
4075 +
4076 +       /* take buffer from queue */
4077 +       spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
4078 +       if (list_empty(&msg_context->u.bulk.port->buffers)) {
4079 +               spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
4080 +               pr_err("buffer list empty trying to submit bulk receive\n");
4081 +
4082 +               /* todo: this is a serious error, we should never have
4083 +                * commited a buffer_to_host operation to the mmal
4084 +                * port without the buffer to back it up (underflow
4085 +                * handling) and there is no obvious way to deal with
4086 +                * this - how is the mmal servie going to react when
4087 +                * we fail to do the xfer and reschedule a buffer when
4088 +                * it arrives? perhaps a starved flag to indicate a
4089 +                * waiting bulk receive?
4090 +                */
4091 +
4092 +               mutex_unlock(&instance->bulk_mutex);
4093 +
4094 +               return -EINVAL;
4095 +       }
4096 +
4097 +       msg_context->u.bulk.buffer =
4098 +           list_entry(msg_context->u.bulk.port->buffers.next,
4099 +                      struct mmal_buffer, list);
4100 +       list_del(&msg_context->u.bulk.buffer->list);
4101 +
4102 +       spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
4103 +
4104 +       /* ensure we do not overrun the available buffer */
4105 +       if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
4106 +               rd_len = msg_context->u.bulk.buffer->buffer_size;
4107 +               pr_warn("short read as not enough receive buffer space\n");
4108 +               /* todo: is this the correct response, what happens to
4109 +                * the rest of the message data?
4110 +                */
4111 +       }
4112 +
4113 +       /* store length */
4114 +       msg_context->u.bulk.buffer_used = rd_len;
4115 +       msg_context->u.bulk.mmal_flags =
4116 +           msg->u.buffer_from_host.buffer_header.flags;
4117 +       msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
4118 +       msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
4119 +
4120 +       // only need to flush L1 cache here, as VCHIQ takes care of the L2
4121 +       // cache.
4122 +       __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
4123 +
4124 +       /* queue the bulk submission */
4125 +       vchi_service_use(instance->handle);
4126 +       ret = vchi_bulk_queue_receive(instance->handle,
4127 +                                     msg_context->u.bulk.buffer->buffer,
4128 +                                     /* Actual receive needs to be a multiple
4129 +                                      * of 4 bytes
4130 +                                      */
4131 +                                     (rd_len + 3) & ~3,
4132 +                                     VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
4133 +                                     VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
4134 +                                     msg_context);
4135 +
4136 +       vchi_service_release(instance->handle);
4137 +
4138 +       if (ret != 0) {
4139 +               /* callback will not be clearing the mutex */
4140 +               mutex_unlock(&instance->bulk_mutex);
4141 +       }
4142 +
4143 +       return ret;
4144 +}
4145 +
4146 +/* enque a dummy bulk receive for a given message context */
4147 +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
4148 +                             struct mmal_msg_context *msg_context)
4149 +{
4150 +       int ret;
4151 +
4152 +       /* bulk mutex stops other bulk operations while we have a
4153 +        * receive in progress - released in callback
4154 +        */
4155 +       ret = mutex_lock_interruptible(&instance->bulk_mutex);
4156 +       if (ret != 0)
4157 +               return ret;
4158 +
4159 +       /* zero length indicates this was a dummy transfer */
4160 +       msg_context->u.bulk.buffer_used = 0;
4161 +
4162 +       /* queue the bulk submission */
4163 +       vchi_service_use(instance->handle);
4164 +
4165 +       ret = vchi_bulk_queue_receive(instance->handle,
4166 +                                     instance->bulk_scratch,
4167 +                                     8,
4168 +                                     VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
4169 +                                     VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
4170 +                                     msg_context);
4171 +
4172 +       vchi_service_release(instance->handle);
4173 +
4174 +       if (ret != 0) {
4175 +               /* callback will not be clearing the mutex */
4176 +               mutex_unlock(&instance->bulk_mutex);
4177 +       }
4178 +
4179 +       return ret;
4180 +}
4181 +
4182 +/* data in message, memcpy from packet into output buffer */
4183 +static int inline_receive(struct vchiq_mmal_instance *instance,
4184 +                         struct mmal_msg *msg,
4185 +                         struct mmal_msg_context *msg_context)
4186 +{
4187 +       unsigned long flags = 0;
4188 +
4189 +       /* take buffer from queue */
4190 +       spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
4191 +       if (list_empty(&msg_context->u.bulk.port->buffers)) {
4192 +               spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
4193 +               pr_err("buffer list empty trying to receive inline\n");
4194 +
4195 +               /* todo: this is a serious error, we should never have
4196 +                * commited a buffer_to_host operation to the mmal
4197 +                * port without the buffer to back it up (with
4198 +                * underflow handling) and there is no obvious way to
4199 +                * deal with this. Less bad than the bulk case as we
4200 +                * can just drop this on the floor but...unhelpful
4201 +                */
4202 +               return -EINVAL;
4203 +       }
4204 +
4205 +       msg_context->u.bulk.buffer =
4206 +           list_entry(msg_context->u.bulk.port->buffers.next,
4207 +                      struct mmal_buffer, list);
4208 +       list_del(&msg_context->u.bulk.buffer->list);
4209 +
4210 +       spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
4211 +
4212 +       memcpy(msg_context->u.bulk.buffer->buffer,
4213 +              msg->u.buffer_from_host.short_data,
4214 +              msg->u.buffer_from_host.payload_in_message);
4215 +
4216 +       msg_context->u.bulk.buffer_used =
4217 +           msg->u.buffer_from_host.payload_in_message;
4218 +
4219 +       return 0;
4220 +}
4221 +
4222 +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
4223 +static int
4224 +buffer_from_host(struct vchiq_mmal_instance *instance,
4225 +                struct vchiq_mmal_port *port, struct mmal_buffer *buf)
4226 +{
4227 +       struct mmal_msg_context *msg_context;
4228 +       struct mmal_msg m;
4229 +       int ret;
4230 +
4231 +       pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
4232 +
4233 +       /* bulk mutex stops other bulk operations while we
4234 +        * have a receive in progress
4235 +        */
4236 +       if (mutex_lock_interruptible(&instance->bulk_mutex))
4237 +               return -EINTR;
4238 +
4239 +       /* get context */
4240 +       msg_context = get_msg_context(instance);
4241 +       if (msg_context == NULL)
4242 +               return -ENOMEM;
4243 +
4244 +       /* store bulk message context for when data arrives */
4245 +       msg_context->u.bulk.instance = instance;
4246 +       msg_context->u.bulk.port = port;
4247 +       msg_context->u.bulk.buffer = NULL;      /* not valid until bulk xfer */
4248 +       msg_context->u.bulk.buffer_used = 0;
4249 +
4250 +       /* initialise work structure ready to schedule callback */
4251 +       INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
4252 +
4253 +       /* prep the buffer from host message */
4254 +       memset(&m, 0xbc, sizeof(m));    /* just to make debug clearer */
4255 +
4256 +       m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
4257 +       m.h.magic = MMAL_MAGIC;
4258 +       m.h.context = msg_context;
4259 +       m.h.status = 0;
4260 +
4261 +       /* drvbuf is our private data passed back */
4262 +       m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
4263 +       m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
4264 +       m.u.buffer_from_host.drvbuf.port_handle = port->handle;
4265 +       m.u.buffer_from_host.drvbuf.client_context = msg_context;
4266 +
4267 +       /* buffer header */
4268 +       m.u.buffer_from_host.buffer_header.cmd = 0;
4269 +       m.u.buffer_from_host.buffer_header.data = buf->buffer;
4270 +       m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
4271 +       m.u.buffer_from_host.buffer_header.length = 0;  /* nothing used yet */
4272 +       m.u.buffer_from_host.buffer_header.offset = 0;  /* no offset */
4273 +       m.u.buffer_from_host.buffer_header.flags = 0;   /* no flags */
4274 +       m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
4275 +       m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
4276 +
4277 +       /* clear buffer type sepecific data */
4278 +       memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
4279 +              sizeof(m.u.buffer_from_host.buffer_header_type_specific));
4280 +
4281 +       /* no payload in message */
4282 +       m.u.buffer_from_host.payload_in_message = 0;
4283 +
4284 +       vchi_service_use(instance->handle);
4285 +
4286 +       ret = vchi_msg_queue(instance->handle, &m,
4287 +                            sizeof(struct mmal_msg_header) +
4288 +                            sizeof(m.u.buffer_from_host),
4289 +                            VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
4290 +
4291 +       if (ret != 0) {
4292 +               release_msg_context(msg_context);
4293 +               /* todo: is this correct error value? */
4294 +       }
4295 +
4296 +       vchi_service_release(instance->handle);
4297 +
4298 +       mutex_unlock(&instance->bulk_mutex);
4299 +
4300 +       return ret;
4301 +}
4302 +
4303 +/* submit a buffer to the mmal sevice
4304 + *
4305 + * the buffer_from_host uses size data from the ports next available
4306 + * mmal_buffer and deals with there being no buffer available by
4307 + * incrementing the underflow for later
4308 + */
4309 +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
4310 +                                struct vchiq_mmal_port *port)
4311 +{
4312 +       int ret;
4313 +       struct mmal_buffer *buf;
4314 +       unsigned long flags = 0;
4315 +
4316 +       if (!port->enabled)
4317 +               return -EINVAL;
4318 +
4319 +       /* peek buffer from queue */
4320 +       spin_lock_irqsave(&port->slock, flags);
4321 +       if (list_empty(&port->buffers)) {
4322 +               port->buffer_underflow++;
4323 +               spin_unlock_irqrestore(&port->slock, flags);
4324 +               return -ENOSPC;
4325 +       }
4326 +
4327 +       buf = list_entry(port->buffers.next, struct mmal_buffer, list);
4328 +
4329 +       spin_unlock_irqrestore(&port->slock, flags);
4330 +
4331 +       /* issue buffer to mmal service */
4332 +       ret = buffer_from_host(instance, port, buf);
4333 +       if (ret) {
4334 +               pr_err("adding buffer header failed\n");
4335 +               /* todo: how should this be dealt with */
4336 +       }
4337 +
4338 +       return ret;
4339 +}
4340 +
4341 +/* deals with receipt of buffer to host message */
4342 +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
4343 +                             struct mmal_msg *msg, u32 msg_len)
4344 +{
4345 +       struct mmal_msg_context *msg_context;
4346 +
4347 +       pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
4348 +                instance, msg, msg_len);
4349 +
4350 +       if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
4351 +               msg_context = msg->u.buffer_from_host.drvbuf.client_context;
4352 +       } else {
4353 +               pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
4354 +               return;
4355 +       }
4356 +
4357 +       if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
4358 +               /* message reception had an error */
4359 +               pr_warn("error %d in reply\n", msg->h.status);
4360 +
4361 +               msg_context->u.bulk.status = msg->h.status;
4362 +
4363 +       } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
4364 +               /* empty buffer */
4365 +               if (msg->u.buffer_from_host.buffer_header.flags &
4366 +                   MMAL_BUFFER_HEADER_FLAG_EOS) {
4367 +                       msg_context->u.bulk.status =
4368 +                           dummy_bulk_receive(instance, msg_context);
4369 +                       if (msg_context->u.bulk.status == 0)
4370 +                               return; /* successful bulk submission, bulk
4371 +                                        * completion will trigger callback
4372 +                                        */
4373 +               } else {
4374 +                       /* do callback with empty buffer - not EOS though */
4375 +                       msg_context->u.bulk.status = 0;
4376 +                       msg_context->u.bulk.buffer_used = 0;
4377 +               }
4378 +       } else if (msg->u.buffer_from_host.payload_in_message == 0) {
4379 +               /* data is not in message, queue a bulk receive */
4380 +               msg_context->u.bulk.status =
4381 +                   bulk_receive(instance, msg, msg_context);
4382 +               if (msg_context->u.bulk.status == 0)
4383 +                       return; /* successful bulk submission, bulk
4384 +                                * completion will trigger callback
4385 +                                */
4386 +
4387 +               /* failed to submit buffer, this will end badly */
4388 +               pr_err("error %d on bulk submission\n",
4389 +                      msg_context->u.bulk.status);
4390 +
4391 +       } else if (msg->u.buffer_from_host.payload_in_message <=
4392 +                  MMAL_VC_SHORT_DATA) {
4393 +               /* data payload within message */
4394 +               msg_context->u.bulk.status = inline_receive(instance, msg,
4395 +                                                           msg_context);
4396 +       } else {
4397 +               pr_err("message with invalid short payload\n");
4398 +
4399 +               /* signal error */
4400 +               msg_context->u.bulk.status = -EINVAL;
4401 +               msg_context->u.bulk.buffer_used =
4402 +                   msg->u.buffer_from_host.payload_in_message;
4403 +       }
4404 +
4405 +       /* replace the buffer header */
4406 +       port_buffer_from_host(instance, msg_context->u.bulk.port);
4407 +
4408 +       /* schedule the port callback */
4409 +       schedule_work(&msg_context->u.bulk.work);
4410 +}
4411 +
4412 +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
4413 +                           struct mmal_msg_context *msg_context)
4414 +{
4415 +       /* bulk receive operation complete */
4416 +       mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
4417 +
4418 +       /* replace the buffer header */
4419 +       port_buffer_from_host(msg_context->u.bulk.instance,
4420 +                             msg_context->u.bulk.port);
4421 +
4422 +       msg_context->u.bulk.status = 0;
4423 +
4424 +       /* schedule the port callback */
4425 +       schedule_work(&msg_context->u.bulk.work);
4426 +}
4427 +
4428 +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
4429 +                         struct mmal_msg_context *msg_context)
4430 +{
4431 +       pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
4432 +
4433 +       /* bulk receive operation complete */
4434 +       mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
4435 +
4436 +       /* replace the buffer header */
4437 +       port_buffer_from_host(msg_context->u.bulk.instance,
4438 +                             msg_context->u.bulk.port);
4439 +
4440 +       msg_context->u.bulk.status = -EINTR;
4441 +
4442 +       schedule_work(&msg_context->u.bulk.work);
4443 +}
4444 +
4445 +/* incoming event service callback */
4446 +static void service_callback(void *param,
4447 +                            const VCHI_CALLBACK_REASON_T reason,
4448 +                            void *bulk_ctx)
4449 +{
4450 +       struct vchiq_mmal_instance *instance = param;
4451 +       int status;
4452 +       u32 msg_len;
4453 +       struct mmal_msg *msg;
4454 +       VCHI_HELD_MSG_T msg_handle;
4455 +
4456 +       if (!instance) {
4457 +               pr_err("Message callback passed NULL instance\n");
4458 +               return;
4459 +       }
4460 +
4461 +       switch (reason) {
4462 +       case VCHI_CALLBACK_MSG_AVAILABLE:
4463 +               status = vchi_msg_hold(instance->handle, (void **)&msg,
4464 +                                      &msg_len, VCHI_FLAGS_NONE, &msg_handle);
4465 +               if (status) {
4466 +                       pr_err("Unable to dequeue a message (%d)\n", status);
4467 +                       break;
4468 +               }
4469 +
4470 +               DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
4471 +
4472 +               /* handling is different for buffer messages */
4473 +               switch (msg->h.type) {
4474 +
4475 +               case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
4476 +                       vchi_held_msg_release(&msg_handle);
4477 +                       break;
4478 +
4479 +               case MMAL_MSG_TYPE_EVENT_TO_HOST:
4480 +                       event_to_host_cb(instance, msg, msg_len);
4481 +                       vchi_held_msg_release(&msg_handle);
4482 +
4483 +                       break;
4484 +
4485 +               case MMAL_MSG_TYPE_BUFFER_TO_HOST:
4486 +                       buffer_to_host_cb(instance, msg, msg_len);
4487 +                       vchi_held_msg_release(&msg_handle);
4488 +                       break;
4489 +
4490 +               default:
4491 +                       /* messages dependant on header context to complete */
4492 +
4493 +                       /* todo: the msg.context really ought to be sanity
4494 +                        * checked before we just use it, afaict it comes back
4495 +                        * and is used raw from the videocore. Perhaps it
4496 +                        * should be verified the address lies in the kernel
4497 +                        * address space.
4498 +                        */
4499 +                       if (msg->h.context == NULL) {
4500 +                               pr_err("received message context was null!\n");
4501 +                               vchi_held_msg_release(&msg_handle);
4502 +                               break;
4503 +                       }
4504 +
4505 +                       /* fill in context values */
4506 +                       msg->h.context->u.sync.msg_handle = msg_handle;
4507 +                       msg->h.context->u.sync.msg = msg;
4508 +                       msg->h.context->u.sync.msg_len = msg_len;
4509 +
4510 +                       /* todo: should this check (completion_done()
4511 +                        * == 1) for no one waiting? or do we need a
4512 +                        * flag to tell us the completion has been
4513 +                        * interrupted so we can free the message and
4514 +                        * its context. This probably also solves the
4515 +                        * message arriving after interruption todo
4516 +                        * below
4517 +                        */
4518 +
4519 +                       /* complete message so caller knows it happened */
4520 +                       complete(&msg->h.context->u.sync.cmplt);
4521 +                       break;
4522 +               }
4523 +
4524 +               break;
4525 +
4526 +       case VCHI_CALLBACK_BULK_RECEIVED:
4527 +               bulk_receive_cb(instance, bulk_ctx);
4528 +               break;
4529 +
4530 +       case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
4531 +               bulk_abort_cb(instance, bulk_ctx);
4532 +               break;
4533 +
4534 +       case VCHI_CALLBACK_SERVICE_CLOSED:
4535 +               /* TODO: consider if this requires action if received when
4536 +                * driver is not explicitly closing the service
4537 +                */
4538 +               break;
4539 +
4540 +       default:
4541 +               pr_err("Received unhandled message reason %d\n", reason);
4542 +               break;
4543 +       }
4544 +}
4545 +
4546 +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
4547 +                                    struct mmal_msg *msg,
4548 +                                    unsigned int payload_len,
4549 +                                    struct mmal_msg **msg_out,
4550 +                                    VCHI_HELD_MSG_T *msg_handle_out)
4551 +{
4552 +       struct mmal_msg_context msg_context;
4553 +       int ret;
4554 +
4555 +       /* payload size must not cause message to exceed max size */
4556 +       if (payload_len >
4557 +           (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
4558 +               pr_err("payload length %d exceeds max:%d\n", payload_len,
4559 +                        (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
4560 +               return -EINVAL;
4561 +       }
4562 +
4563 +       init_completion(&msg_context.u.sync.cmplt);
4564 +
4565 +       msg->h.magic = MMAL_MAGIC;
4566 +       msg->h.context = &msg_context;
4567 +       msg->h.status = 0;
4568 +
4569 +       DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
4570 +                    ">>> sync message");
4571 +
4572 +       vchi_service_use(instance->handle);
4573 +
4574 +       ret = vchi_msg_queue(instance->handle,
4575 +                            msg,
4576 +                            sizeof(struct mmal_msg_header) + payload_len,
4577 +                            VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
4578 +
4579 +       vchi_service_release(instance->handle);
4580 +
4581 +       if (ret) {
4582 +               pr_err("error %d queuing message\n", ret);
4583 +               return ret;
4584 +       }
4585 +
4586 +       ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, HZ);
4587 +       if (ret <= 0) {
4588 +               pr_err("error %d waiting for sync completion\n", ret);
4589 +               if (ret == 0)
4590 +                       ret = -ETIME;
4591 +               /* todo: what happens if the message arrives after aborting */
4592 +               return ret;
4593 +       }
4594 +
4595 +       *msg_out = msg_context.u.sync.msg;
4596 +       *msg_handle_out = msg_context.u.sync.msg_handle;
4597 +
4598 +       return 0;
4599 +}
4600 +
4601 +static void dump_port_info(struct vchiq_mmal_port *port)
4602 +{
4603 +       pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
4604 +
4605 +       pr_debug("buffer minimum num:%d size:%d align:%d\n",
4606 +                port->minimum_buffer.num,
4607 +                port->minimum_buffer.size, port->minimum_buffer.alignment);
4608 +
4609 +       pr_debug("buffer recommended num:%d size:%d align:%d\n",
4610 +                port->recommended_buffer.num,
4611 +                port->recommended_buffer.size,
4612 +                port->recommended_buffer.alignment);
4613 +
4614 +       pr_debug("buffer current values num:%d size:%d align:%d\n",
4615 +                port->current_buffer.num,
4616 +                port->current_buffer.size, port->current_buffer.alignment);
4617 +
4618 +       pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
4619 +                port->format.type,
4620 +                port->format.encoding, port->format.encoding_variant);
4621 +
4622 +       pr_debug("                  bitrate:%d flags:0x%x\n",
4623 +                port->format.bitrate, port->format.flags);
4624 +
4625 +       if (port->format.type == MMAL_ES_TYPE_VIDEO) {
4626 +               pr_debug
4627 +                   ("es video format: width:%d height:%d colourspace:0x%x\n",
4628 +                    port->es.video.width, port->es.video.height,
4629 +                    port->es.video.color_space);
4630 +
4631 +               pr_debug("               : crop xywh %d,%d,%d,%d\n",
4632 +                        port->es.video.crop.x,
4633 +                        port->es.video.crop.y,
4634 +                        port->es.video.crop.width, port->es.video.crop.height);
4635 +               pr_debug("               : framerate %d/%d  aspect %d/%d\n",
4636 +                        port->es.video.frame_rate.num,
4637 +                        port->es.video.frame_rate.den,
4638 +                        port->es.video.par.num, port->es.video.par.den);
4639 +       }
4640 +}
4641 +
4642 +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
4643 +{
4644 +
4645 +       /* todo do readonly fields need setting at all? */
4646 +       p->type = port->type;
4647 +       p->index = port->index;
4648 +       p->index_all = 0;
4649 +       p->is_enabled = port->enabled;
4650 +       p->buffer_num_min = port->minimum_buffer.num;
4651 +       p->buffer_size_min = port->minimum_buffer.size;
4652 +       p->buffer_alignment_min = port->minimum_buffer.alignment;
4653 +       p->buffer_num_recommended = port->recommended_buffer.num;
4654 +       p->buffer_size_recommended = port->recommended_buffer.size;
4655 +
4656 +       /* only three writable fields in a port */
4657 +       p->buffer_num = port->current_buffer.num;
4658 +       p->buffer_size = port->current_buffer.size;
4659 +       p->userdata = port;
4660 +}
4661 +
4662 +static int port_info_set(struct vchiq_mmal_instance *instance,
4663 +                        struct vchiq_mmal_port *port)
4664 +{
4665 +       int ret;
4666 +       struct mmal_msg m;
4667 +       struct mmal_msg *rmsg;
4668 +       VCHI_HELD_MSG_T rmsg_handle;
4669 +
4670 +       pr_debug("setting port info port %p\n", port);
4671 +       if (!port)
4672 +               return -1;
4673 +       dump_port_info(port);
4674 +
4675 +       m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
4676 +
4677 +       m.u.port_info_set.component_handle = port->component->handle;
4678 +       m.u.port_info_set.port_type = port->type;
4679 +       m.u.port_info_set.port_index = port->index;
4680 +
4681 +       port_to_mmal_msg(port, &m.u.port_info_set.port);
4682 +
4683 +       /* elementry stream format setup */
4684 +       m.u.port_info_set.format.type = port->format.type;
4685 +       m.u.port_info_set.format.encoding = port->format.encoding;
4686 +       m.u.port_info_set.format.encoding_variant =
4687 +           port->format.encoding_variant;
4688 +       m.u.port_info_set.format.bitrate = port->format.bitrate;
4689 +       m.u.port_info_set.format.flags = port->format.flags;
4690 +
4691 +       memcpy(&m.u.port_info_set.es, &port->es,
4692 +              sizeof(union mmal_es_specific_format));
4693 +
4694 +       m.u.port_info_set.format.extradata_size = port->format.extradata_size;
4695 +       memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
4696 +              port->format.extradata_size);
4697 +
4698 +       ret = send_synchronous_mmal_msg(instance, &m,
4699 +                                       sizeof(m.u.port_info_set),
4700 +                                       &rmsg, &rmsg_handle);
4701 +       if (ret)
4702 +               return ret;
4703 +
4704 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
4705 +               /* got an unexpected message type in reply */
4706 +               ret = -EINVAL;
4707 +               goto release_msg;
4708 +       }
4709 +
4710 +       /* return operation status */
4711 +       ret = -rmsg->u.port_info_get_reply.status;
4712 +
4713 +       pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
4714 +                port->component->handle, port->handle);
4715 +
4716 +release_msg:
4717 +       vchi_held_msg_release(&rmsg_handle);
4718 +
4719 +       return ret;
4720 +
4721 +}
4722 +
4723 +/* use port info get message to retrive port information */
4724 +static int port_info_get(struct vchiq_mmal_instance *instance,
4725 +                        struct vchiq_mmal_port *port)
4726 +{
4727 +       int ret;
4728 +       struct mmal_msg m;
4729 +       struct mmal_msg *rmsg;
4730 +       VCHI_HELD_MSG_T rmsg_handle;
4731 +
4732 +       /* port info time */
4733 +       m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
4734 +       m.u.port_info_get.component_handle = port->component->handle;
4735 +       m.u.port_info_get.port_type = port->type;
4736 +       m.u.port_info_get.index = port->index;
4737 +
4738 +       ret = send_synchronous_mmal_msg(instance, &m,
4739 +                                       sizeof(m.u.port_info_get),
4740 +                                       &rmsg, &rmsg_handle);
4741 +       if (ret)
4742 +               return ret;
4743 +
4744 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
4745 +               /* got an unexpected message type in reply */
4746 +               ret = -EINVAL;
4747 +               goto release_msg;
4748 +       }
4749 +
4750 +       /* return operation status */
4751 +       ret = -rmsg->u.port_info_get_reply.status;
4752 +       if (ret != MMAL_MSG_STATUS_SUCCESS)
4753 +               goto release_msg;
4754 +
4755 +       if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
4756 +               port->enabled = false;
4757 +       else
4758 +               port->enabled = true;
4759 +
4760 +       /* copy the values out of the message */
4761 +       port->handle = rmsg->u.port_info_get_reply.port_handle;
4762 +
4763 +       /* port type and index cached to use on port info set becuase
4764 +        * it does not use a port handle
4765 +        */
4766 +       port->type = rmsg->u.port_info_get_reply.port_type;
4767 +       port->index = rmsg->u.port_info_get_reply.port_index;
4768 +
4769 +       port->minimum_buffer.num =
4770 +           rmsg->u.port_info_get_reply.port.buffer_num_min;
4771 +       port->minimum_buffer.size =
4772 +           rmsg->u.port_info_get_reply.port.buffer_size_min;
4773 +       port->minimum_buffer.alignment =
4774 +           rmsg->u.port_info_get_reply.port.buffer_alignment_min;
4775 +
4776 +       port->recommended_buffer.alignment =
4777 +           rmsg->u.port_info_get_reply.port.buffer_alignment_min;
4778 +       port->recommended_buffer.num =
4779 +           rmsg->u.port_info_get_reply.port.buffer_num_recommended;
4780 +
4781 +       port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
4782 +       port->current_buffer.size =
4783 +           rmsg->u.port_info_get_reply.port.buffer_size;
4784 +
4785 +       /* stream format */
4786 +       port->format.type = rmsg->u.port_info_get_reply.format.type;
4787 +       port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
4788 +       port->format.encoding_variant =
4789 +           rmsg->u.port_info_get_reply.format.encoding_variant;
4790 +       port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
4791 +       port->format.flags = rmsg->u.port_info_get_reply.format.flags;
4792 +
4793 +       /* elementry stream format */
4794 +       memcpy(&port->es,
4795 +              &rmsg->u.port_info_get_reply.es,
4796 +              sizeof(union mmal_es_specific_format));
4797 +       port->format.es = &port->es;
4798 +
4799 +       port->format.extradata_size =
4800 +           rmsg->u.port_info_get_reply.format.extradata_size;
4801 +       memcpy(port->format.extradata,
4802 +              rmsg->u.port_info_get_reply.extradata,
4803 +              port->format.extradata_size);
4804 +
4805 +       pr_debug("received port info\n");
4806 +       dump_port_info(port);
4807 +
4808 +release_msg:
4809 +
4810 +       pr_debug("%s:result:%d component:0x%x port:%d\n",
4811 +                __func__, ret, port->component->handle, port->handle);
4812 +
4813 +       vchi_held_msg_release(&rmsg_handle);
4814 +
4815 +       return ret;
4816 +}
4817 +
4818 +/* create comonent on vc */
4819 +static int create_component(struct vchiq_mmal_instance *instance,
4820 +                           struct vchiq_mmal_component *component,
4821 +                           const char *name)
4822 +{
4823 +       int ret;
4824 +       struct mmal_msg m;
4825 +       struct mmal_msg *rmsg;
4826 +       VCHI_HELD_MSG_T rmsg_handle;
4827 +
4828 +       /* build component create message */
4829 +       m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
4830 +       m.u.component_create.client_component = component;
4831 +       strncpy(m.u.component_create.name, name,
4832 +               sizeof(m.u.component_create.name));
4833 +
4834 +       ret = send_synchronous_mmal_msg(instance, &m,
4835 +                                       sizeof(m.u.component_create),
4836 +                                       &rmsg, &rmsg_handle);
4837 +       if (ret)
4838 +               return ret;
4839 +
4840 +       if (rmsg->h.type != m.h.type) {
4841 +               /* got an unexpected message type in reply */
4842 +               ret = -EINVAL;
4843 +               goto release_msg;
4844 +       }
4845 +
4846 +       ret = -rmsg->u.component_create_reply.status;
4847 +       if (ret != MMAL_MSG_STATUS_SUCCESS)
4848 +               goto release_msg;
4849 +
4850 +       /* a valid component response received */
4851 +       component->handle = rmsg->u.component_create_reply.component_handle;
4852 +       component->inputs = rmsg->u.component_create_reply.input_num;
4853 +       component->outputs = rmsg->u.component_create_reply.output_num;
4854 +       component->clocks = rmsg->u.component_create_reply.clock_num;
4855 +
4856 +       pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
4857 +                component->handle,
4858 +                component->inputs, component->outputs, component->clocks);
4859 +
4860 +release_msg:
4861 +       vchi_held_msg_release(&rmsg_handle);
4862 +
4863 +       return ret;
4864 +}
4865 +
4866 +/* destroys a component on vc */
4867 +static int destroy_component(struct vchiq_mmal_instance *instance,
4868 +                            struct vchiq_mmal_component *component)
4869 +{
4870 +       int ret;
4871 +       struct mmal_msg m;
4872 +       struct mmal_msg *rmsg;
4873 +       VCHI_HELD_MSG_T rmsg_handle;
4874 +
4875 +       m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
4876 +       m.u.component_destroy.component_handle = component->handle;
4877 +
4878 +       ret = send_synchronous_mmal_msg(instance, &m,
4879 +                                       sizeof(m.u.component_destroy),
4880 +                                       &rmsg, &rmsg_handle);
4881 +       if (ret)
4882 +               return ret;
4883 +
4884 +       if (rmsg->h.type != m.h.type) {
4885 +               /* got an unexpected message type in reply */
4886 +               ret = -EINVAL;
4887 +               goto release_msg;
4888 +       }
4889 +
4890 +       ret = -rmsg->u.component_destroy_reply.status;
4891 +
4892 +release_msg:
4893 +
4894 +       vchi_held_msg_release(&rmsg_handle);
4895 +
4896 +       return ret;
4897 +}
4898 +
4899 +/* enable a component on vc */
4900 +static int enable_component(struct vchiq_mmal_instance *instance,
4901 +                           struct vchiq_mmal_component *component)
4902 +{
4903 +       int ret;
4904 +       struct mmal_msg m;
4905 +       struct mmal_msg *rmsg;
4906 +       VCHI_HELD_MSG_T rmsg_handle;
4907 +
4908 +       m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
4909 +       m.u.component_enable.component_handle = component->handle;
4910 +
4911 +       ret = send_synchronous_mmal_msg(instance, &m,
4912 +                                       sizeof(m.u.component_enable),
4913 +                                       &rmsg, &rmsg_handle);
4914 +       if (ret)
4915 +               return ret;
4916 +
4917 +       if (rmsg->h.type != m.h.type) {
4918 +               /* got an unexpected message type in reply */
4919 +               ret = -EINVAL;
4920 +               goto release_msg;
4921 +       }
4922 +
4923 +       ret = -rmsg->u.component_enable_reply.status;
4924 +
4925 +release_msg:
4926 +       vchi_held_msg_release(&rmsg_handle);
4927 +
4928 +       return ret;
4929 +}
4930 +
4931 +/* disable a component on vc */
4932 +static int disable_component(struct vchiq_mmal_instance *instance,
4933 +                            struct vchiq_mmal_component *component)
4934 +{
4935 +       int ret;
4936 +       struct mmal_msg m;
4937 +       struct mmal_msg *rmsg;
4938 +       VCHI_HELD_MSG_T rmsg_handle;
4939 +
4940 +       m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
4941 +       m.u.component_disable.component_handle = component->handle;
4942 +
4943 +       ret = send_synchronous_mmal_msg(instance, &m,
4944 +                                       sizeof(m.u.component_disable),
4945 +                                       &rmsg, &rmsg_handle);
4946 +       if (ret)
4947 +               return ret;
4948 +
4949 +       if (rmsg->h.type != m.h.type) {
4950 +               /* got an unexpected message type in reply */
4951 +               ret = -EINVAL;
4952 +               goto release_msg;
4953 +       }
4954 +
4955 +       ret = -rmsg->u.component_disable_reply.status;
4956 +
4957 +release_msg:
4958 +
4959 +       vchi_held_msg_release(&rmsg_handle);
4960 +
4961 +       return ret;
4962 +}
4963 +
4964 +/* get version of mmal implementation */
4965 +static int get_version(struct vchiq_mmal_instance *instance,
4966 +                      u32 *major_out, u32 *minor_out)
4967 +{
4968 +       int ret;
4969 +       struct mmal_msg m;
4970 +       struct mmal_msg *rmsg;
4971 +       VCHI_HELD_MSG_T rmsg_handle;
4972 +
4973 +       m.h.type = MMAL_MSG_TYPE_GET_VERSION;
4974 +
4975 +       ret = send_synchronous_mmal_msg(instance, &m,
4976 +                                       sizeof(m.u.version),
4977 +                                       &rmsg, &rmsg_handle);
4978 +       if (ret)
4979 +               return ret;
4980 +
4981 +       if (rmsg->h.type != m.h.type) {
4982 +               /* got an unexpected message type in reply */
4983 +               ret = -EINVAL;
4984 +               goto release_msg;
4985 +       }
4986 +
4987 +       *major_out = rmsg->u.version.major;
4988 +       *minor_out = rmsg->u.version.minor;
4989 +
4990 +release_msg:
4991 +       vchi_held_msg_release(&rmsg_handle);
4992 +
4993 +       return ret;
4994 +}
4995 +
4996 +/* do a port action with a port as a parameter */
4997 +static int port_action_port(struct vchiq_mmal_instance *instance,
4998 +                           struct vchiq_mmal_port *port,
4999 +                           enum mmal_msg_port_action_type action_type)
5000 +{
5001 +       int ret;
5002 +       struct mmal_msg m;
5003 +       struct mmal_msg *rmsg;
5004 +       VCHI_HELD_MSG_T rmsg_handle;
5005 +
5006 +       m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
5007 +       m.u.port_action_port.component_handle = port->component->handle;
5008 +       m.u.port_action_port.port_handle = port->handle;
5009 +       m.u.port_action_port.action = action_type;
5010 +
5011 +       port_to_mmal_msg(port, &m.u.port_action_port.port);
5012 +
5013 +       ret = send_synchronous_mmal_msg(instance, &m,
5014 +                                       sizeof(m.u.port_action_port),
5015 +                                       &rmsg, &rmsg_handle);
5016 +       if (ret)
5017 +               return ret;
5018 +
5019 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
5020 +               /* got an unexpected message type in reply */
5021 +               ret = -EINVAL;
5022 +               goto release_msg;
5023 +       }
5024 +
5025 +       ret = -rmsg->u.port_action_reply.status;
5026 +
5027 +       pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
5028 +                __func__,
5029 +                ret, port->component->handle, port->handle,
5030 +                port_action_type_names[action_type], action_type);
5031 +
5032 +release_msg:
5033 +       vchi_held_msg_release(&rmsg_handle);
5034 +
5035 +       return ret;
5036 +}
5037 +
5038 +/* do a port action with handles as parameters */
5039 +static int port_action_handle(struct vchiq_mmal_instance *instance,
5040 +                             struct vchiq_mmal_port *port,
5041 +                             enum mmal_msg_port_action_type action_type,
5042 +                             u32 connect_component_handle,
5043 +                             u32 connect_port_handle)
5044 +{
5045 +       int ret;
5046 +       struct mmal_msg m;
5047 +       struct mmal_msg *rmsg;
5048 +       VCHI_HELD_MSG_T rmsg_handle;
5049 +
5050 +       m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
5051 +
5052 +       m.u.port_action_handle.component_handle = port->component->handle;
5053 +       m.u.port_action_handle.port_handle = port->handle;
5054 +       m.u.port_action_handle.action = action_type;
5055 +
5056 +       m.u.port_action_handle.connect_component_handle =
5057 +           connect_component_handle;
5058 +       m.u.port_action_handle.connect_port_handle = connect_port_handle;
5059 +
5060 +       ret = send_synchronous_mmal_msg(instance, &m,
5061 +                                       sizeof(m.u.port_action_handle),
5062 +                                       &rmsg, &rmsg_handle);
5063 +       if (ret)
5064 +               return ret;
5065 +
5066 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
5067 +               /* got an unexpected message type in reply */
5068 +               ret = -EINVAL;
5069 +               goto release_msg;
5070 +       }
5071 +
5072 +       ret = -rmsg->u.port_action_reply.status;
5073 +
5074 +       pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
5075 +                " connect component:0x%x connect port:%d\n",
5076 +                __func__,
5077 +                ret, port->component->handle, port->handle,
5078 +                port_action_type_names[action_type],
5079 +                action_type, connect_component_handle, connect_port_handle);
5080 +
5081 +release_msg:
5082 +       vchi_held_msg_release(&rmsg_handle);
5083 +
5084 +       return ret;
5085 +}
5086 +
5087 +static int port_parameter_set(struct vchiq_mmal_instance *instance,
5088 +                             struct vchiq_mmal_port *port,
5089 +                             u32 parameter_id, void *value, u32 value_size)
5090 +{
5091 +       int ret;
5092 +       struct mmal_msg m;
5093 +       struct mmal_msg *rmsg;
5094 +       VCHI_HELD_MSG_T rmsg_handle;
5095 +
5096 +       m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
5097 +
5098 +       m.u.port_parameter_set.component_handle = port->component->handle;
5099 +       m.u.port_parameter_set.port_handle = port->handle;
5100 +       m.u.port_parameter_set.id = parameter_id;
5101 +       m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
5102 +       memcpy(&m.u.port_parameter_set.value, value, value_size);
5103 +
5104 +       ret = send_synchronous_mmal_msg(instance, &m,
5105 +                                       (4 * sizeof(u32)) + value_size,
5106 +                                       &rmsg, &rmsg_handle);
5107 +       if (ret)
5108 +               return ret;
5109 +
5110 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
5111 +               /* got an unexpected message type in reply */
5112 +               ret = -EINVAL;
5113 +               goto release_msg;
5114 +       }
5115 +
5116 +       ret = -rmsg->u.port_parameter_set_reply.status;
5117 +
5118 +       pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
5119 +                __func__,
5120 +                ret, port->component->handle, port->handle, parameter_id);
5121 +
5122 +release_msg:
5123 +       vchi_held_msg_release(&rmsg_handle);
5124 +
5125 +       return ret;
5126 +}
5127 +
5128 +static int port_parameter_get(struct vchiq_mmal_instance *instance,
5129 +                             struct vchiq_mmal_port *port,
5130 +                             u32 parameter_id, void *value, u32 *value_size)
5131 +{
5132 +       int ret;
5133 +       struct mmal_msg m;
5134 +       struct mmal_msg *rmsg;
5135 +       VCHI_HELD_MSG_T rmsg_handle;
5136 +
5137 +       m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
5138 +
5139 +       m.u.port_parameter_get.component_handle = port->component->handle;
5140 +       m.u.port_parameter_get.port_handle = port->handle;
5141 +       m.u.port_parameter_get.id = parameter_id;
5142 +       m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
5143 +
5144 +       ret = send_synchronous_mmal_msg(instance, &m,
5145 +                                       sizeof(struct
5146 +                                              mmal_msg_port_parameter_get),
5147 +                                       &rmsg, &rmsg_handle);
5148 +       if (ret)
5149 +               return ret;
5150 +
5151 +       if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
5152 +               /* got an unexpected message type in reply */
5153 +               pr_err("Incorrect reply type %d\n", rmsg->h.type);
5154 +               ret = -EINVAL;
5155 +               goto release_msg;
5156 +       }
5157 +
5158 +       ret = -rmsg->u.port_parameter_get_reply.status;
5159 +       if (ret) {
5160 +               /* Copy only as much as we have space for
5161 +                * but report true size of parameter
5162 +                */
5163 +               memcpy(value, &rmsg->u.port_parameter_get_reply.value,
5164 +                      *value_size);
5165 +               *value_size = rmsg->u.port_parameter_get_reply.size;
5166 +       } else
5167 +               memcpy(value, &rmsg->u.port_parameter_get_reply.value,
5168 +                      rmsg->u.port_parameter_get_reply.size);
5169 +
5170 +       pr_info("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
5171 +               ret, port->component->handle, port->handle, parameter_id);
5172 +
5173 +release_msg:
5174 +       vchi_held_msg_release(&rmsg_handle);
5175 +
5176 +       return ret;
5177 +}
5178 +
5179 +/* disables a port and drains buffers from it */
5180 +static int port_disable(struct vchiq_mmal_instance *instance,
5181 +                       struct vchiq_mmal_port *port)
5182 +{
5183 +       int ret;
5184 +       struct list_head *q, *buf_head;
5185 +       unsigned long flags = 0;
5186 +
5187 +       if (!port->enabled)
5188 +               return 0;
5189 +
5190 +       port->enabled = false;
5191 +
5192 +       ret = port_action_port(instance, port,
5193 +                              MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
5194 +       if (ret == 0) {
5195 +
5196 +               /* drain all queued buffers on port */
5197 +               spin_lock_irqsave(&port->slock, flags);
5198 +
5199 +               list_for_each_safe(buf_head, q, &port->buffers) {
5200 +                       struct mmal_buffer *mmalbuf;
5201 +                       mmalbuf = list_entry(buf_head, struct mmal_buffer,
5202 +                                            list);
5203 +                       list_del(buf_head);
5204 +                       if (port->buffer_cb)
5205 +                               port->buffer_cb(instance,
5206 +                                               port, 0, mmalbuf, 0, 0,
5207 +                                               MMAL_TIME_UNKNOWN,
5208 +                                               MMAL_TIME_UNKNOWN);
5209 +               }
5210 +
5211 +               spin_unlock_irqrestore(&port->slock, flags);
5212 +
5213 +               ret = port_info_get(instance, port);
5214 +       }
5215 +
5216 +       return ret;
5217 +}
5218 +
5219 +/* enable a port */
5220 +static int port_enable(struct vchiq_mmal_instance *instance,
5221 +                      struct vchiq_mmal_port *port)
5222 +{
5223 +       unsigned int hdr_count;
5224 +       struct list_head *buf_head;
5225 +       int ret;
5226 +
5227 +       if (port->enabled)
5228 +               return 0;
5229 +
5230 +       /* ensure there are enough buffers queued to cover the buffer headers */
5231 +       if (port->buffer_cb != NULL) {
5232 +               hdr_count = 0;
5233 +               list_for_each(buf_head, &port->buffers) {
5234 +                       hdr_count++;
5235 +               }
5236 +               if (hdr_count < port->current_buffer.num)
5237 +                       return -ENOSPC;
5238 +       }
5239 +
5240 +       ret = port_action_port(instance, port,
5241 +                              MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
5242 +       if (ret)
5243 +               goto done;
5244 +
5245 +       port->enabled = true;
5246 +
5247 +       if (port->buffer_cb) {
5248 +               /* send buffer headers to videocore */
5249 +               hdr_count = 1;
5250 +               list_for_each(buf_head, &port->buffers) {
5251 +                       struct mmal_buffer *mmalbuf;
5252 +                       mmalbuf = list_entry(buf_head, struct mmal_buffer,
5253 +                                            list);
5254 +                       ret = buffer_from_host(instance, port, mmalbuf);
5255 +                       if (ret)
5256 +                               goto done;
5257 +
5258 +                       hdr_count++;
5259 +                       if (hdr_count > port->current_buffer.num)
5260 +                               break;
5261 +               }
5262 +       }
5263 +
5264 +       ret = port_info_get(instance, port);
5265 +
5266 +done:
5267 +       return ret;
5268 +}
5269 +
5270 +/* ------------------------------------------------------------------
5271 + * Exported API
5272 + *------------------------------------------------------------------*/
5273 +
5274 +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
5275 +                              struct vchiq_mmal_port *port)
5276 +{
5277 +       int ret;
5278 +
5279 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5280 +               return -EINTR;
5281 +
5282 +       ret = port_info_set(instance, port);
5283 +       if (ret)
5284 +               goto release_unlock;
5285 +
5286 +       /* read what has actually been set */
5287 +       ret = port_info_get(instance, port);
5288 +
5289 +release_unlock:
5290 +       mutex_unlock(&instance->vchiq_mutex);
5291 +
5292 +       return ret;
5293 +
5294 +}
5295 +
5296 +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
5297 +                                 struct vchiq_mmal_port *port,
5298 +                                 u32 parameter, void *value, u32 value_size)
5299 +{
5300 +       int ret;
5301 +
5302 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5303 +               return -EINTR;
5304 +
5305 +       ret = port_parameter_set(instance, port, parameter, value, value_size);
5306 +
5307 +       mutex_unlock(&instance->vchiq_mutex);
5308 +
5309 +       return ret;
5310 +}
5311 +
5312 +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
5313 +                                 struct vchiq_mmal_port *port,
5314 +                                 u32 parameter, void *value, u32 *value_size)
5315 +{
5316 +       int ret;
5317 +
5318 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5319 +               return -EINTR;
5320 +
5321 +       ret = port_parameter_get(instance, port, parameter, value, value_size);
5322 +
5323 +       mutex_unlock(&instance->vchiq_mutex);
5324 +
5325 +       return ret;
5326 +}
5327 +
5328 +/* enable a port
5329 + *
5330 + * enables a port and queues buffers for satisfying callbacks if we
5331 + * provide a callback handler
5332 + */
5333 +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
5334 +                          struct vchiq_mmal_port *port,
5335 +                          vchiq_mmal_buffer_cb buffer_cb)
5336 +{
5337 +       int ret;
5338 +
5339 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5340 +               return -EINTR;
5341 +
5342 +       /* already enabled - noop */
5343 +       if (port->enabled) {
5344 +               ret = 0;
5345 +               goto unlock;
5346 +       }
5347 +
5348 +       port->buffer_cb = buffer_cb;
5349 +
5350 +       ret = port_enable(instance, port);
5351 +
5352 +unlock:
5353 +       mutex_unlock(&instance->vchiq_mutex);
5354 +
5355 +       return ret;
5356 +}
5357 +
5358 +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
5359 +                           struct vchiq_mmal_port *port)
5360 +{
5361 +       int ret;
5362 +
5363 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5364 +               return -EINTR;
5365 +
5366 +       if (!port->enabled) {
5367 +               mutex_unlock(&instance->vchiq_mutex);
5368 +               return 0;
5369 +       }
5370 +
5371 +       ret = port_disable(instance, port);
5372 +
5373 +       mutex_unlock(&instance->vchiq_mutex);
5374 +
5375 +       return ret;
5376 +}
5377 +
5378 +/* ports will be connected in a tunneled manner so data buffers
5379 + * are not handled by client.
5380 + */
5381 +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
5382 +                                  struct vchiq_mmal_port *src,
5383 +                                  struct vchiq_mmal_port *dst)
5384 +{
5385 +       int ret;
5386 +
5387 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5388 +               return -EINTR;
5389 +
5390 +       /* disconnect ports if connected */
5391 +       if (src->connected != NULL) {
5392 +               ret = port_disable(instance, src);
5393 +               if (ret) {
5394 +                       pr_err("failed disabling src port(%d)\n", ret);
5395 +                       goto release_unlock;
5396 +               }
5397 +
5398 +               /* do not need to disable the destination port as they
5399 +                * are connected and it is done automatically
5400 +                */
5401 +
5402 +               ret = port_action_handle(instance, src,
5403 +                                        MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
5404 +                                        src->connected->component->handle,
5405 +                                        src->connected->handle);
5406 +               if (ret < 0) {
5407 +                       pr_err("failed disconnecting src port\n");
5408 +                       goto release_unlock;
5409 +               }
5410 +               src->connected->enabled = false;
5411 +               src->connected = NULL;
5412 +       }
5413 +
5414 +       if (dst == NULL) {
5415 +               /* do not make new connection */
5416 +               ret = 0;
5417 +               pr_debug("not making new connection\n");
5418 +               goto release_unlock;
5419 +       }
5420 +
5421 +       /* copy src port format to dst */
5422 +       dst->format.encoding = src->format.encoding;
5423 +       dst->es.video.width = src->es.video.width;
5424 +       dst->es.video.height = src->es.video.height;
5425 +       dst->es.video.crop.x = src->es.video.crop.x;
5426 +       dst->es.video.crop.y = src->es.video.crop.y;
5427 +       dst->es.video.crop.width = src->es.video.crop.width;
5428 +       dst->es.video.crop.height = src->es.video.crop.height;
5429 +       dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
5430 +       dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
5431 +
5432 +       /* set new format */
5433 +       ret = port_info_set(instance, dst);
5434 +       if (ret) {
5435 +               pr_debug("setting port info failed\n");
5436 +               goto release_unlock;
5437 +       }
5438 +
5439 +       /* read what has actually been set */
5440 +       ret = port_info_get(instance, dst);
5441 +       if (ret) {
5442 +               pr_debug("read back port info failed\n");
5443 +               goto release_unlock;
5444 +       }
5445 +
5446 +       /* connect two ports together */
5447 +       ret = port_action_handle(instance, src,
5448 +                                MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
5449 +                                dst->component->handle, dst->handle);
5450 +       if (ret < 0) {
5451 +               pr_debug("connecting port %d:%d to %d:%d failed\n",
5452 +                        src->component->handle, src->handle,
5453 +                        dst->component->handle, dst->handle);
5454 +               goto release_unlock;
5455 +       }
5456 +       src->connected = dst;
5457 +
5458 +release_unlock:
5459 +
5460 +       mutex_unlock(&instance->vchiq_mutex);
5461 +
5462 +       return ret;
5463 +}
5464 +
5465 +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
5466 +                            struct vchiq_mmal_port *port,
5467 +                            struct mmal_buffer *buffer)
5468 +{
5469 +       unsigned long flags = 0;
5470 +
5471 +       spin_lock_irqsave(&port->slock, flags);
5472 +       list_add_tail(&buffer->list, &port->buffers);
5473 +       spin_unlock_irqrestore(&port->slock, flags);
5474 +
5475 +       /* the port previously underflowed because it was missing a
5476 +        * mmal_buffer which has just been added, submit that buffer
5477 +        * to the mmal service.
5478 +        */
5479 +       if (port->buffer_underflow) {
5480 +               port_buffer_from_host(instance, port);
5481 +               port->buffer_underflow--;
5482 +       }
5483 +
5484 +       return 0;
5485 +}
5486 +
5487 +/* Initialise a mmal component and its ports
5488 + *
5489 + */
5490 +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
5491 +                             const char *name,
5492 +                             struct vchiq_mmal_component **component_out)
5493 +{
5494 +       int ret;
5495 +       int idx;                /* port index */
5496 +       struct vchiq_mmal_component *component;
5497 +
5498 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5499 +               return -EINTR;
5500 +
5501 +       if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
5502 +               ret = -EINVAL;  /* todo is this correct error? */
5503 +               goto unlock;
5504 +       }
5505 +
5506 +       component = &instance->component[instance->component_idx];
5507 +
5508 +       ret = create_component(instance, component, name);
5509 +       if (ret < 0)
5510 +               goto unlock;
5511 +
5512 +       /* ports info needs gathering */
5513 +       component->control.type = MMAL_PORT_TYPE_CONTROL;
5514 +       component->control.index = 0;
5515 +       component->control.component = component;
5516 +       spin_lock_init(&component->control.slock);
5517 +       INIT_LIST_HEAD(&component->control.buffers);
5518 +       ret = port_info_get(instance, &component->control);
5519 +       if (ret < 0)
5520 +               goto release_component;
5521 +
5522 +       for (idx = 0; idx < component->inputs; idx++) {
5523 +               component->input[idx].type = MMAL_PORT_TYPE_INPUT;
5524 +               component->input[idx].index = idx;
5525 +               component->input[idx].component = component;
5526 +               spin_lock_init(&component->input[idx].slock);
5527 +               INIT_LIST_HEAD(&component->input[idx].buffers);
5528 +               ret = port_info_get(instance, &component->input[idx]);
5529 +               if (ret < 0)
5530 +                       goto release_component;
5531 +       }
5532 +
5533 +       for (idx = 0; idx < component->outputs; idx++) {
5534 +               component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
5535 +               component->output[idx].index = idx;
5536 +               component->output[idx].component = component;
5537 +               spin_lock_init(&component->output[idx].slock);
5538 +               INIT_LIST_HEAD(&component->output[idx].buffers);
5539 +               ret = port_info_get(instance, &component->output[idx]);
5540 +               if (ret < 0)
5541 +                       goto release_component;
5542 +       }
5543 +
5544 +       for (idx = 0; idx < component->clocks; idx++) {
5545 +               component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
5546 +               component->clock[idx].index = idx;
5547 +               component->clock[idx].component = component;
5548 +               spin_lock_init(&component->clock[idx].slock);
5549 +               INIT_LIST_HEAD(&component->clock[idx].buffers);
5550 +               ret = port_info_get(instance, &component->clock[idx]);
5551 +               if (ret < 0)
5552 +                       goto release_component;
5553 +       }
5554 +
5555 +       instance->component_idx++;
5556 +
5557 +       *component_out = component;
5558 +
5559 +       mutex_unlock(&instance->vchiq_mutex);
5560 +
5561 +       return 0;
5562 +
5563 +release_component:
5564 +       destroy_component(instance, component);
5565 +unlock:
5566 +       mutex_unlock(&instance->vchiq_mutex);
5567 +
5568 +       return ret;
5569 +}
5570 +
5571 +/*
5572 + * cause a mmal component to be destroyed
5573 + */
5574 +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
5575 +                                 struct vchiq_mmal_component *component)
5576 +{
5577 +       int ret;
5578 +
5579 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5580 +               return -EINTR;
5581 +
5582 +       if (component->enabled)
5583 +               ret = disable_component(instance, component);
5584 +
5585 +       ret = destroy_component(instance, component);
5586 +
5587 +       mutex_unlock(&instance->vchiq_mutex);
5588 +
5589 +       return ret;
5590 +}
5591 +
5592 +/*
5593 + * cause a mmal component to be enabled
5594 + */
5595 +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
5596 +                               struct vchiq_mmal_component *component)
5597 +{
5598 +       int ret;
5599 +
5600 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5601 +               return -EINTR;
5602 +
5603 +       if (component->enabled) {
5604 +               mutex_unlock(&instance->vchiq_mutex);
5605 +               return 0;
5606 +       }
5607 +
5608 +       ret = enable_component(instance, component);
5609 +       if (ret == 0)
5610 +               component->enabled = true;
5611 +
5612 +       mutex_unlock(&instance->vchiq_mutex);
5613 +
5614 +       return ret;
5615 +}
5616 +
5617 +/*
5618 + * cause a mmal component to be enabled
5619 + */
5620 +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
5621 +                                struct vchiq_mmal_component *component)
5622 +{
5623 +       int ret;
5624 +
5625 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5626 +               return -EINTR;
5627 +
5628 +       if (!component->enabled) {
5629 +               mutex_unlock(&instance->vchiq_mutex);
5630 +               return 0;
5631 +       }
5632 +
5633 +       ret = disable_component(instance, component);
5634 +       if (ret == 0)
5635 +               component->enabled = false;
5636 +
5637 +       mutex_unlock(&instance->vchiq_mutex);
5638 +
5639 +       return ret;
5640 +}
5641 +
5642 +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
5643 +                      u32 *major_out, u32 *minor_out)
5644 +{
5645 +       int ret;
5646 +
5647 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5648 +               return -EINTR;
5649 +
5650 +       ret = get_version(instance, major_out, minor_out);
5651 +
5652 +       mutex_unlock(&instance->vchiq_mutex);
5653 +
5654 +       return ret;
5655 +}
5656 +
5657 +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
5658 +{
5659 +       int status = 0;
5660 +
5661 +       if (instance == NULL)
5662 +               return -EINVAL;
5663 +
5664 +       if (mutex_lock_interruptible(&instance->vchiq_mutex))
5665 +               return -EINTR;
5666 +
5667 +       vchi_service_use(instance->handle);
5668 +
5669 +       status = vchi_service_close(instance->handle);
5670 +       if (status != 0)
5671 +               pr_err("mmal-vchiq: VCHIQ close failed");
5672 +
5673 +       mutex_unlock(&instance->vchiq_mutex);
5674 +
5675 +       vfree(instance->bulk_scratch);
5676 +
5677 +       kfree(instance);
5678 +
5679 +       return status;
5680 +}
5681 +
5682 +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
5683 +{
5684 +       int status;
5685 +       struct vchiq_mmal_instance *instance;
5686 +       static VCHI_CONNECTION_T *vchi_connection;
5687 +       static VCHI_INSTANCE_T vchi_instance;
5688 +       SERVICE_CREATION_T params = {
5689 +               VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
5690 +               VC_MMAL_SERVER_NAME,
5691 +               vchi_connection,
5692 +               0,              /* rx fifo size (unused) */
5693 +               0,              /* tx fifo size (unused) */
5694 +               service_callback,
5695 +               NULL,           /* service callback parameter */
5696 +               1,              /* unaligned bulk receives */
5697 +               1,              /* unaligned bulk transmits */
5698 +               0               /* want crc check on bulk transfers */
5699 +       };
5700 +
5701 +       /* compile time checks to ensure structure size as they are
5702 +        * directly (de)serialised from memory.
5703 +        */
5704 +
5705 +       /* ensure the header structure has packed to the correct size */
5706 +       BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
5707 +
5708 +       /* ensure message structure does not exceed maximum length */
5709 +       BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
5710 +
5711 +       /* mmal port struct is correct size */
5712 +       BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
5713 +
5714 +       /* create a vchi instance */
5715 +       status = vchi_initialise(&vchi_instance);
5716 +       if (status) {
5717 +               pr_err("Failed to initialise VCHI instance (status=%d)\n",
5718 +                      status);
5719 +               return -EIO;
5720 +       }
5721 +
5722 +       status = vchi_connect(NULL, 0, vchi_instance);
5723 +       if (status) {
5724 +               pr_err("Failed to connect VCHI instance (status=%d)\n", status);
5725 +               return -EIO;
5726 +       }
5727 +
5728 +       instance = kmalloc(sizeof(*instance), GFP_KERNEL);
5729 +       memset(instance, 0, sizeof(*instance));
5730 +
5731 +       mutex_init(&instance->vchiq_mutex);
5732 +       mutex_init(&instance->bulk_mutex);
5733 +
5734 +       instance->bulk_scratch = vmalloc(PAGE_SIZE);
5735 +
5736 +       params.callback_param = instance;
5737 +
5738 +       status = vchi_service_open(vchi_instance, &params, &instance->handle);
5739 +       if (status) {
5740 +               pr_err("Failed to open VCHI service connection (status=%d)\n",
5741 +                      status);
5742 +               goto err_close_services;
5743 +       }
5744 +
5745 +       vchi_service_release(instance->handle);
5746 +
5747 +       *out_instance = instance;
5748 +
5749 +       return 0;
5750 +
5751 +err_close_services:
5752 +
5753 +       vchi_service_close(instance->handle);
5754 +       vfree(instance->bulk_scratch);
5755 +       kfree(instance);
5756 +       return -ENODEV;
5757 +}
5758 --- /dev/null
5759 +++ b/drivers/media/platform/bcm2835/mmal-vchiq.h
5760 @@ -0,0 +1,178 @@
5761 +/*
5762 + * Broadcom BM2835 V4L2 driver
5763 + *
5764 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
5765 + *
5766 + * This file is subject to the terms and conditions of the GNU General Public
5767 + * License.  See the file COPYING in the main directory of this archive
5768 + * for more details.
5769 + *
5770 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
5771 + *          Dave Stevenson <dsteve@broadcom.com>
5772 + *          Simon Mellor <simellor@broadcom.com>
5773 + *          Luke Diamand <luked@broadcom.com>
5774 + *
5775 + * MMAL interface to VCHIQ message passing
5776 + */
5777 +
5778 +#ifndef MMAL_VCHIQ_H
5779 +#define MMAL_VCHIQ_H
5780 +
5781 +#include "mmal-msg-format.h"
5782 +
5783 +#define MAX_PORT_COUNT 4
5784 +
5785 +/* Maximum size of the format extradata. */
5786 +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
5787 +
5788 +struct vchiq_mmal_instance;
5789 +
5790 +enum vchiq_mmal_es_type {
5791 +       MMAL_ES_TYPE_UNKNOWN,     /**< Unknown elementary stream type */
5792 +       MMAL_ES_TYPE_CONTROL,     /**< Elementary stream of control commands */
5793 +       MMAL_ES_TYPE_AUDIO,       /**< Audio elementary stream */
5794 +       MMAL_ES_TYPE_VIDEO,       /**< Video elementary stream */
5795 +       MMAL_ES_TYPE_SUBPICTURE   /**< Sub-picture elementary stream */
5796 +};
5797 +
5798 +/* rectangle, used lots so it gets its own struct */
5799 +struct vchiq_mmal_rect {
5800 +       s32 x;
5801 +       s32 y;
5802 +       s32 width;
5803 +       s32 height;
5804 +};
5805 +
5806 +struct vchiq_mmal_port_buffer {
5807 +       unsigned int num; /* number of buffers */
5808 +       u32 size; /* size of buffers */
5809 +       u32 alignment; /* alignment of buffers */
5810 +};
5811 +
5812 +struct vchiq_mmal_port;
5813 +
5814 +typedef void (*vchiq_mmal_buffer_cb)(
5815 +               struct vchiq_mmal_instance  *instance,
5816 +               struct vchiq_mmal_port *port,
5817 +               int status, struct mmal_buffer *buffer,
5818 +               unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
5819 +
5820 +struct vchiq_mmal_port {
5821 +       bool enabled;
5822 +       u32 handle;
5823 +       u32 type; /* port type, cached to use on port info set */
5824 +       u32 index; /* port index, cached to use on port info set */
5825 +
5826 +       /* component port belongs to, allows simple deref */
5827 +       struct vchiq_mmal_component *component;
5828 +
5829 +       struct vchiq_mmal_port *connected; /* port conencted to */
5830 +
5831 +       /* buffer info */
5832 +       struct vchiq_mmal_port_buffer minimum_buffer;
5833 +       struct vchiq_mmal_port_buffer recommended_buffer;
5834 +       struct vchiq_mmal_port_buffer current_buffer;
5835 +
5836 +       /* stream format */
5837 +       struct mmal_es_format format;
5838 +       /* elementry stream format */
5839 +       union mmal_es_specific_format es;
5840 +
5841 +       /* data buffers to fill */
5842 +       struct list_head buffers;
5843 +       /* lock to serialise adding and removing buffers from list */
5844 +       spinlock_t slock;
5845 +       /* count of how many buffer header refils have failed because
5846 +        * there was no buffer to satisfy them
5847 +        */
5848 +       int buffer_underflow;
5849 +       /* callback on buffer completion */
5850 +       vchiq_mmal_buffer_cb buffer_cb;
5851 +       /* callback context */
5852 +       void *cb_ctx;
5853 +};
5854 +
5855 +struct vchiq_mmal_component {
5856 +       bool enabled;
5857 +       u32 handle;  /* VideoCore handle for component */
5858 +       u32 inputs;  /* Number of input ports */
5859 +       u32 outputs; /* Number of output ports */
5860 +       u32 clocks;  /* Number of clock ports */
5861 +       struct vchiq_mmal_port control; /* control port */
5862 +       struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
5863 +       struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
5864 +       struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
5865 +};
5866 +
5867 +
5868 +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
5869 +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
5870 +
5871 +/* Initialise a mmal component and its ports
5872 +*
5873 +*/
5874 +int vchiq_mmal_component_init(
5875 +               struct vchiq_mmal_instance *instance,
5876 +               const char *name,
5877 +               struct vchiq_mmal_component **component_out);
5878 +
5879 +int vchiq_mmal_component_finalise(
5880 +               struct vchiq_mmal_instance *instance,
5881 +               struct vchiq_mmal_component *component);
5882 +
5883 +int vchiq_mmal_component_enable(
5884 +               struct vchiq_mmal_instance *instance,
5885 +               struct vchiq_mmal_component *component);
5886 +
5887 +int vchiq_mmal_component_disable(
5888 +               struct vchiq_mmal_instance *instance,
5889 +               struct vchiq_mmal_component *component);
5890 +
5891 +
5892 +
5893 +/* enable a mmal port
5894 + *
5895 + * enables a port and if a buffer callback provided enque buffer
5896 + * headers as apropriate for the port.
5897 + */
5898 +int vchiq_mmal_port_enable(
5899 +               struct vchiq_mmal_instance *instance,
5900 +               struct vchiq_mmal_port *port,
5901 +               vchiq_mmal_buffer_cb buffer_cb);
5902 +
5903 +/* disable a port
5904 + *
5905 + * disable a port will dequeue any pending buffers
5906 + */
5907 +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
5908 +                          struct vchiq_mmal_port *port);
5909 +
5910 +
5911 +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
5912 +                                 struct vchiq_mmal_port *port,
5913 +                                 u32 parameter,
5914 +                                 void *value,
5915 +                                 u32 value_size);
5916 +
5917 +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
5918 +                                 struct vchiq_mmal_port *port,
5919 +                                 u32 parameter,
5920 +                                 void *value,
5921 +                                 u32 *value_size);
5922 +
5923 +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
5924 +                              struct vchiq_mmal_port *port);
5925 +
5926 +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
5927 +                           struct vchiq_mmal_port *src,
5928 +                           struct vchiq_mmal_port *dst);
5929 +
5930 +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
5931 +                      u32 *major_out,
5932 +                      u32 *minor_out);
5933 +
5934 +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
5935 +                            struct vchiq_mmal_port *port,
5936 +                            struct mmal_buffer *buf);
5937 +
5938 +#endif /* MMAL_VCHIQ_H */