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