brcm2708: update linux 4.9 patches to latest version
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 0051-bcm2835-add-v4l2-camera-device.patch
1 From 88f7fe5348a24191969b07537b38449a83369d32 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] 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 V4L2: Fixes from 6by9
16
17 V4L2: Fix EV values. Add manual shutter speed control
18
19 V4L2 EV values should be in units of 1/1000. Corrected.
20 Add support for V4L2_CID_EXPOSURE_ABSOLUTE which should
21 give manual shutter control. Requires manual exposure mode
22 to be selected first.
23
24 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
25
26 V4L2: Correct JPEG Q-factor range
27
28 Should be 1-100, not 0-100
29
30 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
31
32 V4L2: Fix issue of driver jamming if STREAMON failed.
33
34 Fix issue where the driver was left in a partially enabled
35 state if STREAMON failed, and would then reject many IOCTLs
36 as it thought it was streaming.
37
38 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
39
40 V4L2: Fix ISO controls.
41
42 Driver was passing the index to the GPU, and not the desired
43 ISO value.
44
45 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
46
47 V4L2: Add flicker avoidance controls
48
49 Add support for V4L2_CID_POWER_LINE_FREQUENCY to set flicker
50 avoidance frequencies.
51
52 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
53
54 V4L2: Add support for frame rate control.
55
56 Add support for frame rate (or time per frame as V4L2
57 inverts it) control via s_parm.
58
59 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
60
61 V4L2: Improve G_FBUF handling so we pass conformance
62
63 Return some sane numbers for get framebuffer so that
64 we pass conformance.
65
66 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
67
68 V4L2: Fix information advertised through g_vidfmt
69
70 Width and height were being stored based on incorrect
71 values.
72
73 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
74
75 V4L2: Add support for inline H264 headers
76
77 Add support for V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER
78 to control H264 inline headers.
79 Requires firmware fix to work correctly, otherwise format
80 has to be set to H264 before this parameter is set.
81
82 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
83
84 V4L2: Fix JPEG timestamp issue
85
86 JPEG images were coming through from the GPU with timestamp
87 of 0. Detect this and give current system time instead
88 of some invalid value.
89
90 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
91
92 V4L2: Fix issue when switching down JPEG resolution.
93
94 JPEG buffer size calculation is based on input resolution.
95 Input resolution was being configured after output port
96 format. Caused failures if switching from one JPEG resolution
97 to a smaller one.
98
99 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
100
101 V4L2: Enable MJPEG encoding
102
103 Requires GPU firmware update to support MJPEG encoder.
104
105 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
106
107 V4L2: Correct flag settings for compressed formats
108
109 Set flags field correctly on enum_fmt_vid_cap for compressed
110 image formats.
111
112 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
113
114 V4L2: H264 profile & level ctrls, FPS control and auto exp pri
115
116 Several control handling updates.
117 H264 profile and level controls.
118 Timeperframe/FPS reworked to add V4L2_CID_EXPOSURE_AUTO_PRIORITY to
119 select whether AE is allowed to override the framerate specified.
120
121 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
122
123 V4L2: Correct BGR24 to RGB24 in format table
124
125 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
126
127 V4L2: Add additional pixel formats. Correct colourspace
128
129 Adds the other flavours of YUYV, and NV12.
130 Corrects the overlay advertised colourspace.
131
132 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
133
134 V4L2: Drop logging msg from info to debug
135
136 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
137
138 V4L2: Initial pass at scene modes.
139
140 Only supports exposure mode and metering modes.
141
142 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
143
144 V4L2: Add manual white balance control.
145
146 Adds support for V4L2_CID_RED_BALANCE and
147 V4L2_CID_BLUE_BALANCE. Only has an effect if
148 V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE has
149 V4L2_WHITE_BALANCE_MANUAL selected.
150
151 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
152
153 config: Enable V4L / MMAL driver
154
155 V4L2: Increase the MMAL timeout to 3sec
156
157 MJPEG codec flush is now taking longer and results
158 in a kernel panic if the driver has stopped waiting for
159 the result when it finally completes.
160 Increase the timeout value from 1 to 3secs.
161
162 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
163
164 V4L2: Add support for setting H264_I_PERIOD
165
166 Adds support for the parameter V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
167 to set the frequency with which I frames are produced.
168
169 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
170
171 V4L2: Enable GPU function for removing padding from images.
172
173 GPU can now support arbitrary strides, although may require
174 additional processing to achieve it. Enable this feature
175 so that the images delivered are the size requested.
176
177 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
178
179 V4L2: Add support for V4L2_PIX_FMT_BGR32
180
181 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
182
183 V4L2: Set the colourspace to avoid odd YUV-RGB conversions
184
185 Removes the amiguity from the conversion routines and stops
186 them dropping back to the SD vs HD choice of coeffs.
187
188 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
189
190 V4L2: Make video/still threshold a run-time param
191
192 Move the define for at what resolution the driver
193 switches from a video mode capture to a stills mode
194 capture to module parameters.
195
196 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
197
198 V4L2: Fix incorrect pool sizing
199
200 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
201
202 V4L2: Add option to disable enum_framesizes.
203
204 Gstreamer's handling of a driver that advertises
205 V4L2_FRMSIZE_TYPE_STEPWISE to define the supported
206 resolutions is broken. See bug
207 https://bugzilla.gnome.org/show_bug.cgi?id=726521
208
209 Optional parameter of gst_v4l2src_is_broken added.
210 If non-zero, the driver claims not to support that
211 ioctl, and gstreamer should be happy again (it
212 guesses a set of defaults for itself).
213
214 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
215
216 V4L2: Add support for more image formats
217
218 Adds YVU420 (YV12), YVU420SP (NV21), and BGR888.
219
220 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
221
222 V4L2: Extend range for V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
223
224 Request to extend the range from the fairly arbitrary
225 1000 frames (33 seconds at 30fps). Extend out to the
226 max range supported (int32 value).
227 Also allow 0, which is handled by the codec as only
228 send an I-frame on the first frame and never again.
229 There may be an exception if it detects a significant
230 scene change, but there's no easy way around that.
231
232 Signed-off-by: Dave Stevenson <dsteve@broadcom.com>
233
234 bcm2835-camera: stop_streaming now has a void return
235
236 BCM2835-V4L2: Fix compliance test failures
237
238 VIDIOC_TRY_FMT and VIDIOC_S_FMT tests were faling due
239 to reporting V4L2_COLORSPACE_JPEG when the colour
240 format wasn't V4L2_PIX_FMT_JPEG.
241 Now reports V4L2_COLORSPACE_SMPTE170M for YUV formats.
242
243 bcm2835 camera planar/packed stride length
244
245 Added a field to the mmal_fmt struct used to compute the bytes per line
246 when using a particular format. This results in the correct stride being
247 calculated even when the format is planar.
248
249 Signed-off-by: Garrett Wilson <g@floft.net>
250
251 bcm2835: camera: check for scene not being found
252
253 static analysis by cppcheck detected some potential NULL pointer
254 dereference issues:
255
256 [drivers/media/platform/bcm2835/controls.c:854]: (error) Possible null
257 pointer dereference: scene
258 (and lines 858, 859 too)
259
260 it is possible that scene is not found because of an invalue ctrl->val
261 and is therefore NULL and hence causing a null pointer dereference.
262
263 Signed-off-by: Colin Ian King <colin.king@canonical.com>
264
265 bcm2835: memcpy port data to m rather than rmsg
266
267 static analysis by cppcheck detected a memcpy to rmsg which is
268 not actually initialized at that point. The memcpy should be copying
269 to variable m instead.
270
271 Signed-off-by: Colin Ian King <colin.king@canonical.com>
272
273 BCM2835-V4L2: Return buffers to videobuf2 on shutdown
274
275 https://github.com/raspberrypi/linux/issues/817
276 Fixes the kernel warning from videobuf2 as buffers
277 are now returned as they are being flushed on
278 stop_streaming.
279
280 squash: Fixup bcm2835-camera for changes in kernel 4.4 api
281
282 v4l2: Fix up driver to upstream timestamp changes
283
284 bcm2835-camera: fix a bug in computation of frame timestamp
285
286 Fixes #1318
287
288 V4L2 driver updates (#1393)
289
290 * BCM2835-V4L2: Correct ISO control and add V4L2_CID_ISO_SENSITIVITY_AUTO
291
292 https://github.com/raspberrypi/linux/issues/1251
293
294 V4L2_CID_ISO_SENSITIVITY was not advertising ISO*1000 as it should.
295 V4L2_CID_ISO_SENSITIVITY_AUTO was not implemented, so was taking
296 V4L2_CID_ISO_SENSITIVITY as 0 for auto mode.
297 Still accepts 0 for auto, but also abides by the new parameter.
298
299 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
300
301 * BCM2835-V4L2: Add a video_nr parameter.
302
303 Adds a kernel parameter "video_nr" to specify the preferred
304 /dev/videoX device node.
305 https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=136120&p=905545
306
307 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
308
309 * BCM2835-V4L2: Add support for multiple cameras
310
311 Ask GPU on load how many cameras have been detected, and
312 enumerate that number of devices.
313 Only applicable on the Compute Module as no other device
314 exposes multiple CSI2 interfaces.
315
316 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
317
318 * BCM2835-V4L2: Add control of the overlay location and alpha.
319
320 Actually do something useful in vidioc_s_fmt_vid_overlay and
321 vidioc_try_fmt_vid_overlay, rather than effectively having
322 read-only fields.
323
324 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
325
326 * BCM2835-V4L2: V4L2-Compliance failure fix
327
328 VIDIOC_TRY_FMT was failing due to bytesperline not
329 being set correctly by default.
330
331 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
332
333 * BCM2835-V4L2: Make all module parameters static
334
335 Clean up to correct variable scope
336
337 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
338
339 V4L2: Request maximum resolution from GPU
340
341 Get resolution information about the sensors from the GPU
342 and advertise it correctly.
343
344 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
345
346 BCM2835-V4L2: Increase minimum resolution to 32x32
347
348 https://github.com/raspberrypi/linux/issues/1498 showed
349 up that 16x16 is failing to work on the GPU for some reason.
350
351 GPU bug being tracked on
352 https://github.com/raspberrypi/firmware/issues/607
353 Workaround here by increasing minimum resolution via V4L2
354 to 32x32.
355
356 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
357
358 [media]: bcm2835-camera: fix compilation error
359
360 There is an error when compiling rpi-4.6.y branch:
361 CC [M] drivers/media/platform/bcm2835/bcm2835-camera.o
362 drivers/media/platform/bcm2835/bcm2835-camera.c:639:17: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
363 .queue_setup = queue_setup,
364 ^
365 drivers/media/platform/bcm2835/bcm2835-camera.c:639:17: note: (near initialization for 'bm2835_mmal_video_qops.queue_setup')
366
367 The const void *parg in setup_queue callback is not needed since commit:
368 df9ecb0cad14b952a2865f8b3af86b2bbadfab45.
369 This commit removes it.
370
371 Signed-off-by: Slawomir Stepien <sst@poczta.fm>
372
373 bcm2835-camera: Fix max/min error when looping over cameras/resolutions
374
375 See: https://github.com/raspberrypi/linux/issues/1447#issuecomment-221303506
376
377 BCM2835-V4L2: Correct handling for BGR24 vs RGB24.
378
379 There was a bug in the GPU firmware that had reversed these
380 two formats.
381 Detect the old firmware, and reverse the formats if necessary.
382
383 Signed-off-by: Dave Stevenson <6by9@users.noreply.github.com>
384
385 BCM2835-v4l2: Fix a conformance test failure
386
387 Format ioctls:
388 test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
389 warn: v4l2-test-formats.cpp(1195): S_PARM is supported but
390 doesn't report V4L2_CAP_TIMEPERFRAME.
391 fail: v4l2-test-formats.cpp(1118): node->has_frmintervals
392 && !cap->capability
393 ---
394 Documentation/video4linux/bcm2835-v4l2.txt | 60 +
395 drivers/media/platform/Kconfig | 2 +
396 drivers/media/platform/Makefile | 2 +
397 drivers/media/platform/bcm2835/Kconfig | 25 +
398 drivers/media/platform/bcm2835/Makefile | 5 +
399 drivers/media/platform/bcm2835/bcm2835-camera.c | 2016 ++++++++++++++++++++++
400 drivers/media/platform/bcm2835/bcm2835-camera.h | 145 ++
401 drivers/media/platform/bcm2835/controls.c | 1345 +++++++++++++++
402 drivers/media/platform/bcm2835/mmal-common.h | 53 +
403 drivers/media/platform/bcm2835/mmal-encodings.h | 127 ++
404 drivers/media/platform/bcm2835/mmal-msg-common.h | 50 +
405 drivers/media/platform/bcm2835/mmal-msg-format.h | 81 +
406 drivers/media/platform/bcm2835/mmal-msg-port.h | 107 ++
407 drivers/media/platform/bcm2835/mmal-msg.h | 404 +++++
408 drivers/media/platform/bcm2835/mmal-parameters.h | 689 ++++++++
409 drivers/media/platform/bcm2835/mmal-vchiq.c | 1916 ++++++++++++++++++++
410 drivers/media/platform/bcm2835/mmal-vchiq.h | 178 ++
411 17 files changed, 7205 insertions(+)
412 create mode 100644 Documentation/video4linux/bcm2835-v4l2.txt
413 create mode 100644 drivers/media/platform/bcm2835/Kconfig
414 create mode 100644 drivers/media/platform/bcm2835/Makefile
415 create mode 100644 drivers/media/platform/bcm2835/bcm2835-camera.c
416 create mode 100644 drivers/media/platform/bcm2835/bcm2835-camera.h
417 create mode 100644 drivers/media/platform/bcm2835/controls.c
418 create mode 100644 drivers/media/platform/bcm2835/mmal-common.h
419 create mode 100644 drivers/media/platform/bcm2835/mmal-encodings.h
420 create mode 100644 drivers/media/platform/bcm2835/mmal-msg-common.h
421 create mode 100644 drivers/media/platform/bcm2835/mmal-msg-format.h
422 create mode 100644 drivers/media/platform/bcm2835/mmal-msg-port.h
423 create mode 100644 drivers/media/platform/bcm2835/mmal-msg.h
424 create mode 100644 drivers/media/platform/bcm2835/mmal-parameters.h
425 create mode 100644 drivers/media/platform/bcm2835/mmal-vchiq.c
426 create mode 100644 drivers/media/platform/bcm2835/mmal-vchiq.h
427
428 --- /dev/null
429 +++ b/Documentation/video4linux/bcm2835-v4l2.txt
430 @@ -0,0 +1,60 @@
431 +
432 +BCM2835 (aka Raspberry Pi) V4L2 driver
433 +======================================
434 +
435 +1. Copyright
436 +============
437 +
438 +Copyright © 2013 Raspberry Pi (Trading) Ltd.
439 +
440 +2. License
441 +==========
442 +
443 +This program is free software; you can redistribute it and/or modify
444 +it under the terms of the GNU General Public License as published by
445 +the Free Software Foundation; either version 2 of the License, or
446 +(at your option) any later version.
447 +
448 +This program is distributed in the hope that it will be useful,
449 +but WITHOUT ANY WARRANTY; without even the implied warranty of
450 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
451 +GNU General Public License for more details.
452 +
453 +You should have received a copy of the GNU General Public License
454 +along with this program; if not, write to the Free Software
455 +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
456 +
457 +3. Quick Start
458 +==============
459 +
460 +You need a version 1.0 or later of v4l2-ctl, available from:
461 + git://git.linuxtv.org/v4l-utils.git
462 +
463 +$ sudo modprobe bcm2835-v4l2
464 +
465 +Turn on the overlay:
466 +
467 +$ v4l2-ctl --overlay=1
468 +
469 +Turn off the overlay:
470 +
471 +$ v4l2-ctl --overlay=0
472 +
473 +Set the capture format for video:
474 +
475 +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
476 +
477 +(Note: 1088 not 1080).
478 +
479 +Capture:
480 +
481 +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
482 +
483 +Stills capture:
484 +
485 +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
486 +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
487 +
488 +List of available formats:
489 +
490 +$ v4l2-ctl --list-formats
491 --- a/drivers/media/platform/Kconfig
492 +++ b/drivers/media/platform/Kconfig
493 @@ -11,6 +11,8 @@ menuconfig V4L_PLATFORM_DRIVERS
494
495 if V4L_PLATFORM_DRIVERS
496
497 +source "drivers/media/platform/bcm2835/Kconfig"
498 +
499 source "drivers/media/platform/marvell-ccic/Kconfig"
500
501 config VIDEO_VIA_CAMERA
502 --- a/drivers/media/platform/Makefile
503 +++ b/drivers/media/platform/Makefile
504 @@ -2,6 +2,8 @@
505 # Makefile for the video capture/playback device drivers.
506 #
507
508 +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
509 +
510 obj-$(CONFIG_VIDEO_M32R_AR_M64278) += arv.o
511
512 obj-$(CONFIG_VIDEO_VIA_CAMERA) += via-camera.o
513 --- /dev/null
514 +++ b/drivers/media/platform/bcm2835/Kconfig
515 @@ -0,0 +1,25 @@
516 +# Broadcom VideoCore IV v4l2 camera support
517 +
518 +config VIDEO_BCM2835
519 + bool "Broadcom BCM2835 camera interface driver"
520 + depends on VIDEO_V4L2 && ARCH_BCM2835
521 + ---help---
522 + Say Y here to enable camera host interface devices for
523 + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
524 + to a service running on VideoCore.
525 +
526 +
527 +if VIDEO_BCM2835
528 +
529 +config VIDEO_BCM2835_MMAL
530 + tristate "Broadcom BM2835 MMAL camera interface driver"
531 + depends on BCM2708_VCHIQ
532 + select VIDEOBUF2_VMALLOC
533 + ---help---
534 + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
535 +
536 + To compile this driver as a module, choose M here: the
537 + module will be called bcm2835-v4l2.o
538 +
539 +
540 +endif # VIDEO_BM2835
541 --- /dev/null
542 +++ b/drivers/media/platform/bcm2835/Makefile
543 @@ -0,0 +1,5 @@
544 +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
545 +
546 +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
547 +
548 +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/staging/vc04_services -Idrivers/staging/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
549 --- /dev/null
550 +++ b/drivers/media/platform/bcm2835/bcm2835-camera.c
551 @@ -0,0 +1,2016 @@
552 +/*
553 + * Broadcom BM2835 V4L2 driver
554 + *
555 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
556 + *
557 + * This file is subject to the terms and conditions of the GNU General Public
558 + * License. See the file COPYING in the main directory of this archive
559 + * for more details.
560 + *
561 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
562 + * Dave Stevenson <dsteve@broadcom.com>
563 + * Simon Mellor <simellor@broadcom.com>
564 + * Luke Diamand <luked@broadcom.com>
565 + */
566 +
567 +#include <linux/errno.h>
568 +#include <linux/kernel.h>
569 +#include <linux/module.h>
570 +#include <linux/slab.h>
571 +#include <media/videobuf2-vmalloc.h>
572 +#include <media/videobuf2-dma-contig.h>
573 +#include <media/v4l2-device.h>
574 +#include <media/v4l2-ioctl.h>
575 +#include <media/v4l2-ctrls.h>
576 +#include <media/v4l2-fh.h>
577 +#include <media/v4l2-event.h>
578 +#include <media/v4l2-common.h>
579 +#include <linux/delay.h>
580 +
581 +#include "mmal-common.h"
582 +#include "mmal-encodings.h"
583 +#include "mmal-vchiq.h"
584 +#include "mmal-msg.h"
585 +#include "mmal-parameters.h"
586 +#include "bcm2835-camera.h"
587 +
588 +#define BM2835_MMAL_VERSION "0.0.2"
589 +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
590 +#define MIN_WIDTH 32
591 +#define MIN_HEIGHT 32
592 +#define MIN_BUFFER_SIZE (80*1024)
593 +
594 +#define MAX_VIDEO_MODE_WIDTH 1280
595 +#define MAX_VIDEO_MODE_HEIGHT 720
596 +
597 +#define MAX_BCM2835_CAMERAS 2
598 +
599 +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
600 +MODULE_AUTHOR("Vincent Sanders");
601 +MODULE_LICENSE("GPL");
602 +MODULE_VERSION(BM2835_MMAL_VERSION);
603 +
604 +int bcm2835_v4l2_debug;
605 +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
606 +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
607 +
608 +#define UNSET (-1)
609 +static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
610 +module_param_array(video_nr, int, NULL, 0644);
611 +MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
612 +
613 +static int max_video_width = MAX_VIDEO_MODE_WIDTH;
614 +static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
615 +module_param(max_video_width, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
616 +MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
617 +module_param(max_video_height, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
618 +MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
619 +
620 +/* Gstreamer bug https://bugzilla.gnome.org/show_bug.cgi?id=726521
621 + * v4l2src does bad (and actually wrong) things when the vidioc_enum_framesizes
622 + * function says type V4L2_FRMSIZE_TYPE_STEPWISE, which we do by default.
623 + * It's happier if we just don't say anything at all, when it then
624 + * sets up a load of defaults that it thinks might work.
625 + * If gst_v4l2src_is_broken is non-zero, then we remove the function from
626 + * our function table list (actually switch to an alternate set, but same
627 + * result).
628 + */
629 +static int gst_v4l2src_is_broken;
630 +module_param(gst_v4l2src_is_broken, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
631 +MODULE_PARM_DESC(gst_v4l2src_is_broken, "If non-zero, enable workaround for Gstreamer");
632 +
633 +/* global device data array */
634 +static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
635 +
636 +#define FPS_MIN 1
637 +#define FPS_MAX 90
638 +
639 +/* timeperframe: min/max and default */
640 +static const struct v4l2_fract
641 + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
642 + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
643 + tpf_default = {.numerator = 1000, .denominator = 30000};
644 +
645 +/* video formats */
646 +static struct mmal_fmt formats[] = {
647 + {
648 + .name = "4:2:0, planar, YUV",
649 + .fourcc = V4L2_PIX_FMT_YUV420,
650 + .flags = 0,
651 + .mmal = MMAL_ENCODING_I420,
652 + .depth = 12,
653 + .mmal_component = MMAL_COMPONENT_CAMERA,
654 + .ybbp = 1,
655 + },
656 + {
657 + .name = "4:2:2, packed, YUYV",
658 + .fourcc = V4L2_PIX_FMT_YUYV,
659 + .flags = 0,
660 + .mmal = MMAL_ENCODING_YUYV,
661 + .depth = 16,
662 + .mmal_component = MMAL_COMPONENT_CAMERA,
663 + .ybbp = 2,
664 + },
665 + {
666 + .name = "RGB24 (LE)",
667 + .fourcc = V4L2_PIX_FMT_RGB24,
668 + .flags = 0,
669 + .mmal = MMAL_ENCODING_RGB24,
670 + .depth = 24,
671 + .mmal_component = MMAL_COMPONENT_CAMERA,
672 + .ybbp = 3,
673 + },
674 + {
675 + .name = "JPEG",
676 + .fourcc = V4L2_PIX_FMT_JPEG,
677 + .flags = V4L2_FMT_FLAG_COMPRESSED,
678 + .mmal = MMAL_ENCODING_JPEG,
679 + .depth = 8,
680 + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
681 + .ybbp = 0,
682 + },
683 + {
684 + .name = "H264",
685 + .fourcc = V4L2_PIX_FMT_H264,
686 + .flags = V4L2_FMT_FLAG_COMPRESSED,
687 + .mmal = MMAL_ENCODING_H264,
688 + .depth = 8,
689 + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
690 + .ybbp = 0,
691 + },
692 + {
693 + .name = "MJPEG",
694 + .fourcc = V4L2_PIX_FMT_MJPEG,
695 + .flags = V4L2_FMT_FLAG_COMPRESSED,
696 + .mmal = MMAL_ENCODING_MJPEG,
697 + .depth = 8,
698 + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
699 + .ybbp = 0,
700 + },
701 + {
702 + .name = "4:2:2, packed, YVYU",
703 + .fourcc = V4L2_PIX_FMT_YVYU,
704 + .flags = 0,
705 + .mmal = MMAL_ENCODING_YVYU,
706 + .depth = 16,
707 + .mmal_component = MMAL_COMPONENT_CAMERA,
708 + .ybbp = 2,
709 + },
710 + {
711 + .name = "4:2:2, packed, VYUY",
712 + .fourcc = V4L2_PIX_FMT_VYUY,
713 + .flags = 0,
714 + .mmal = MMAL_ENCODING_VYUY,
715 + .depth = 16,
716 + .mmal_component = MMAL_COMPONENT_CAMERA,
717 + .ybbp = 2,
718 + },
719 + {
720 + .name = "4:2:2, packed, UYVY",
721 + .fourcc = V4L2_PIX_FMT_UYVY,
722 + .flags = 0,
723 + .mmal = MMAL_ENCODING_UYVY,
724 + .depth = 16,
725 + .mmal_component = MMAL_COMPONENT_CAMERA,
726 + .ybbp = 2,
727 + },
728 + {
729 + .name = "4:2:0, planar, NV12",
730 + .fourcc = V4L2_PIX_FMT_NV12,
731 + .flags = 0,
732 + .mmal = MMAL_ENCODING_NV12,
733 + .depth = 12,
734 + .mmal_component = MMAL_COMPONENT_CAMERA,
735 + .ybbp = 1,
736 + },
737 + {
738 + .name = "RGB24 (BE)",
739 + .fourcc = V4L2_PIX_FMT_BGR24,
740 + .flags = 0,
741 + .mmal = MMAL_ENCODING_BGR24,
742 + .depth = 24,
743 + .mmal_component = MMAL_COMPONENT_CAMERA,
744 + .ybbp = 3,
745 + },
746 + {
747 + .name = "4:2:0, planar, YVU",
748 + .fourcc = V4L2_PIX_FMT_YVU420,
749 + .flags = 0,
750 + .mmal = MMAL_ENCODING_YV12,
751 + .depth = 12,
752 + .mmal_component = MMAL_COMPONENT_CAMERA,
753 + .ybbp = 1,
754 + },
755 + {
756 + .name = "4:2:0, planar, NV21",
757 + .fourcc = V4L2_PIX_FMT_NV21,
758 + .flags = 0,
759 + .mmal = MMAL_ENCODING_NV21,
760 + .depth = 12,
761 + .mmal_component = MMAL_COMPONENT_CAMERA,
762 + .ybbp = 1,
763 + },
764 + {
765 + .name = "RGB32 (BE)",
766 + .fourcc = V4L2_PIX_FMT_BGR32,
767 + .flags = 0,
768 + .mmal = MMAL_ENCODING_BGRA,
769 + .depth = 32,
770 + .mmal_component = MMAL_COMPONENT_CAMERA,
771 + .ybbp = 4,
772 + },
773 +};
774 +
775 +static struct mmal_fmt *get_format(struct v4l2_format *f)
776 +{
777 + struct mmal_fmt *fmt;
778 + unsigned int k;
779 +
780 + for (k = 0; k < ARRAY_SIZE(formats); k++) {
781 + fmt = &formats[k];
782 + if (fmt->fourcc == f->fmt.pix.pixelformat)
783 + break;
784 + }
785 +
786 + if (k == ARRAY_SIZE(formats))
787 + return NULL;
788 +
789 + return &formats[k];
790 +}
791 +
792 +/* ------------------------------------------------------------------
793 + Videobuf queue operations
794 + ------------------------------------------------------------------*/
795 +
796 +static int queue_setup(struct vb2_queue *vq,
797 + unsigned int *nbuffers, unsigned int *nplanes,
798 + unsigned int sizes[], struct device *alloc_ctxs[])
799 +{
800 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
801 + unsigned long size;
802 +
803 + /* refuse queue setup if port is not configured */
804 + if (dev->capture.port == NULL) {
805 + v4l2_err(&dev->v4l2_dev,
806 + "%s: capture port not configured\n", __func__);
807 + return -EINVAL;
808 + }
809 +
810 + size = dev->capture.port->current_buffer.size;
811 + if (size == 0) {
812 + v4l2_err(&dev->v4l2_dev,
813 + "%s: capture port buffer size is zero\n", __func__);
814 + return -EINVAL;
815 + }
816 +
817 + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
818 + *nbuffers = (dev->capture.port->current_buffer.num + 2);
819 +
820 + *nplanes = 1;
821 +
822 + sizes[0] = size;
823 +
824 + /*
825 + * videobuf2-vmalloc allocator is context-less so no need to set
826 + * alloc_ctxs array.
827 + */
828 +
829 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
830 + __func__, dev);
831 +
832 + return 0;
833 +}
834 +
835 +static int buffer_prepare(struct vb2_buffer *vb)
836 +{
837 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
838 + unsigned long size;
839 +
840 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
841 + __func__, dev);
842 +
843 + BUG_ON(dev->capture.port == NULL);
844 + BUG_ON(dev->capture.fmt == NULL);
845 +
846 + size = dev->capture.stride * dev->capture.height;
847 + if (vb2_plane_size(vb, 0) < size) {
848 + v4l2_err(&dev->v4l2_dev,
849 + "%s data will not fit into plane (%lu < %lu)\n",
850 + __func__, vb2_plane_size(vb, 0), size);
851 + return -EINVAL;
852 + }
853 +
854 + return 0;
855 +}
856 +
857 +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
858 +{
859 + return dev->capture.camera_port ==
860 + &dev->
861 + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
862 +}
863 +
864 +static void buffer_cb(struct vchiq_mmal_instance *instance,
865 + struct vchiq_mmal_port *port,
866 + int status,
867 + struct mmal_buffer *buf,
868 + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
869 +{
870 + struct bm2835_mmal_dev *dev = port->cb_ctx;
871 +
872 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
873 + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
874 + __func__, status, buf, length, mmal_flags, pts);
875 +
876 + if (status != 0) {
877 + /* error in transfer */
878 + if (buf != NULL) {
879 + /* there was a buffer with the error so return it */
880 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
881 + }
882 + return;
883 + } else if (length == 0) {
884 + /* stream ended */
885 + if (buf != NULL) {
886 + /* this should only ever happen if the port is
887 + * disabled and there are buffers still queued
888 + */
889 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
890 + pr_debug("Empty buffer");
891 + } else if (dev->capture.frame_count) {
892 + /* grab another frame */
893 + if (is_capturing(dev)) {
894 + pr_debug("Grab another frame");
895 + vchiq_mmal_port_parameter_set(
896 + instance,
897 + dev->capture.
898 + camera_port,
899 + MMAL_PARAMETER_CAPTURE,
900 + &dev->capture.
901 + frame_count,
902 + sizeof(dev->capture.frame_count));
903 + }
904 + } else {
905 + /* signal frame completion */
906 + complete(&dev->capture.frame_cmplt);
907 + }
908 + } else {
909 + if (dev->capture.frame_count) {
910 + if (dev->capture.vc_start_timestamp != -1 &&
911 + pts != 0) {
912 + struct timeval timestamp;
913 + s64 runtime_us = pts -
914 + dev->capture.vc_start_timestamp;
915 + u32 div = 0;
916 + u32 rem = 0;
917 +
918 + div =
919 + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
920 + timestamp.tv_sec =
921 + dev->capture.kernel_start_ts.tv_sec + div;
922 + timestamp.tv_usec =
923 + dev->capture.kernel_start_ts.tv_usec + rem;
924 +
925 + if (timestamp.tv_usec >=
926 + USEC_PER_SEC) {
927 + timestamp.tv_sec++;
928 + timestamp.tv_usec -=
929 + USEC_PER_SEC;
930 + }
931 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
932 + "Convert start time %d.%06d and %llu "
933 + "with offset %llu to %d.%06d\n",
934 + (int)dev->capture.kernel_start_ts.
935 + tv_sec,
936 + (int)dev->capture.kernel_start_ts.
937 + tv_usec,
938 + dev->capture.vc_start_timestamp, pts,
939 + (int)timestamp.tv_sec,
940 + (int)timestamp.tv_usec);
941 + buf->vb.vb2_buf.timestamp = timestamp.tv_sec * 1000000000ULL +
942 + timestamp.tv_usec * 1000ULL;
943 + } else {
944 + buf->vb.vb2_buf.timestamp = ktime_get_ns();
945 + }
946 +
947 + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
948 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
949 +
950 + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
951 + is_capturing(dev)) {
952 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
953 + "Grab another frame as buffer has EOS");
954 + vchiq_mmal_port_parameter_set(
955 + instance,
956 + dev->capture.
957 + camera_port,
958 + MMAL_PARAMETER_CAPTURE,
959 + &dev->capture.
960 + frame_count,
961 + sizeof(dev->capture.frame_count));
962 + }
963 + } else {
964 + /* signal frame completion */
965 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
966 + complete(&dev->capture.frame_cmplt);
967 + }
968 + }
969 +}
970 +
971 +static int enable_camera(struct bm2835_mmal_dev *dev)
972 +{
973 + int ret;
974 + if (!dev->camera_use_count) {
975 + ret = vchiq_mmal_port_parameter_set(
976 + dev->instance,
977 + &dev->component[MMAL_COMPONENT_CAMERA]->control,
978 + MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
979 + sizeof(dev->camera_num));
980 + if (ret < 0) {
981 + v4l2_err(&dev->v4l2_dev,
982 + "Failed setting camera num, ret %d\n", ret);
983 + return -EINVAL;
984 + }
985 +
986 + ret = vchiq_mmal_component_enable(
987 + dev->instance,
988 + dev->component[MMAL_COMPONENT_CAMERA]);
989 + if (ret < 0) {
990 + v4l2_err(&dev->v4l2_dev,
991 + "Failed enabling camera, ret %d\n", ret);
992 + return -EINVAL;
993 + }
994 + }
995 + dev->camera_use_count++;
996 + v4l2_dbg(1, bcm2835_v4l2_debug,
997 + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
998 + dev->camera_use_count);
999 + return 0;
1000 +}
1001 +
1002 +static int disable_camera(struct bm2835_mmal_dev *dev)
1003 +{
1004 + int ret;
1005 + if (!dev->camera_use_count) {
1006 + v4l2_err(&dev->v4l2_dev,
1007 + "Disabled the camera when already disabled\n");
1008 + return -EINVAL;
1009 + }
1010 + dev->camera_use_count--;
1011 + if (!dev->camera_use_count) {
1012 + unsigned int i = 0xFFFFFFFF;
1013 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1014 + "Disabling camera\n");
1015 + ret =
1016 + vchiq_mmal_component_disable(
1017 + dev->instance,
1018 + dev->component[MMAL_COMPONENT_CAMERA]);
1019 + if (ret < 0) {
1020 + v4l2_err(&dev->v4l2_dev,
1021 + "Failed disabling camera, ret %d\n", ret);
1022 + return -EINVAL;
1023 + }
1024 + vchiq_mmal_port_parameter_set(
1025 + dev->instance,
1026 + &dev->component[MMAL_COMPONENT_CAMERA]->control,
1027 + MMAL_PARAMETER_CAMERA_NUM, &i,
1028 + sizeof(i));
1029 + }
1030 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1031 + "Camera refcount now %d\n", dev->camera_use_count);
1032 + return 0;
1033 +}
1034 +
1035 +static void buffer_queue(struct vb2_buffer *vb)
1036 +{
1037 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
1038 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
1039 + struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
1040 + int ret;
1041 +
1042 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1043 + "%s: dev:%p buf:%p\n", __func__, dev, buf);
1044 +
1045 + buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1046 + buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
1047 +
1048 + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
1049 + if (ret < 0)
1050 + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
1051 + __func__);
1052 +}
1053 +
1054 +static int start_streaming(struct vb2_queue *vq, unsigned int count)
1055 +{
1056 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
1057 + int ret;
1058 + int parameter_size;
1059 +
1060 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
1061 + __func__, dev);
1062 +
1063 + /* ensure a format has actually been set */
1064 + if (dev->capture.port == NULL)
1065 + return -EINVAL;
1066 +
1067 + if (enable_camera(dev) < 0) {
1068 + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
1069 + return -EINVAL;
1070 + }
1071 +
1072 + /*init_completion(&dev->capture.frame_cmplt); */
1073 +
1074 + /* enable frame capture */
1075 + dev->capture.frame_count = 1;
1076 +
1077 + /* if the preview is not already running, wait for a few frames for AGC
1078 + * to settle down.
1079 + */
1080 + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
1081 + msleep(300);
1082 +
1083 + /* enable the connection from camera to encoder (if applicable) */
1084 + if (dev->capture.camera_port != dev->capture.port
1085 + && dev->capture.camera_port) {
1086 + ret = vchiq_mmal_port_enable(dev->instance,
1087 + dev->capture.camera_port, NULL);
1088 + if (ret) {
1089 + v4l2_err(&dev->v4l2_dev,
1090 + "Failed to enable encode tunnel - error %d\n",
1091 + ret);
1092 + return -1;
1093 + }
1094 + }
1095 +
1096 + /* Get VC timestamp at this point in time */
1097 + parameter_size = sizeof(dev->capture.vc_start_timestamp);
1098 + if (vchiq_mmal_port_parameter_get(dev->instance,
1099 + dev->capture.camera_port,
1100 + MMAL_PARAMETER_SYSTEM_TIME,
1101 + &dev->capture.vc_start_timestamp,
1102 + &parameter_size)) {
1103 + v4l2_err(&dev->v4l2_dev,
1104 + "Failed to get VC start time - update your VC f/w\n");
1105 +
1106 + /* Flag to indicate just to rely on kernel timestamps */
1107 + dev->capture.vc_start_timestamp = -1;
1108 + } else
1109 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1110 + "Start time %lld size %d\n",
1111 + dev->capture.vc_start_timestamp, parameter_size);
1112 +
1113 + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
1114 +
1115 + /* enable the camera port */
1116 + dev->capture.port->cb_ctx = dev;
1117 + ret =
1118 + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
1119 + if (ret) {
1120 + v4l2_err(&dev->v4l2_dev,
1121 + "Failed to enable capture port - error %d. "
1122 + "Disabling camera port again\n", ret);
1123 +
1124 + vchiq_mmal_port_disable(dev->instance,
1125 + dev->capture.camera_port);
1126 + if (disable_camera(dev) < 0) {
1127 + v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
1128 + return -EINVAL;
1129 + }
1130 + return -1;
1131 + }
1132 +
1133 + /* capture the first frame */
1134 + vchiq_mmal_port_parameter_set(dev->instance,
1135 + dev->capture.camera_port,
1136 + MMAL_PARAMETER_CAPTURE,
1137 + &dev->capture.frame_count,
1138 + sizeof(dev->capture.frame_count));
1139 + return 0;
1140 +}
1141 +
1142 +/* abort streaming and wait for last buffer */
1143 +static void stop_streaming(struct vb2_queue *vq)
1144 +{
1145 + int ret;
1146 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
1147 +
1148 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
1149 + __func__, dev);
1150 +
1151 + init_completion(&dev->capture.frame_cmplt);
1152 + dev->capture.frame_count = 0;
1153 +
1154 + /* ensure a format has actually been set */
1155 + if (dev->capture.port == NULL) {
1156 + v4l2_err(&dev->v4l2_dev,
1157 + "no capture port - stream not started?\n");
1158 + return;
1159 + }
1160 +
1161 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
1162 +
1163 + /* stop capturing frames */
1164 + vchiq_mmal_port_parameter_set(dev->instance,
1165 + dev->capture.camera_port,
1166 + MMAL_PARAMETER_CAPTURE,
1167 + &dev->capture.frame_count,
1168 + sizeof(dev->capture.frame_count));
1169 +
1170 + /* wait for last frame to complete */
1171 + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
1172 + if (ret <= 0)
1173 + v4l2_err(&dev->v4l2_dev,
1174 + "error %d waiting for frame completion\n", ret);
1175 +
1176 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1177 + "disabling connection\n");
1178 +
1179 + /* disable the connection from camera to encoder */
1180 + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
1181 + if (!ret && dev->capture.camera_port != dev->capture.port) {
1182 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1183 + "disabling port\n");
1184 + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
1185 + } else if (dev->capture.camera_port != dev->capture.port) {
1186 + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
1187 + ret);
1188 + }
1189 +
1190 + if (disable_camera(dev) < 0)
1191 + v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
1192 +}
1193 +
1194 +static void bm2835_mmal_lock(struct vb2_queue *vq)
1195 +{
1196 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
1197 + mutex_lock(&dev->mutex);
1198 +}
1199 +
1200 +static void bm2835_mmal_unlock(struct vb2_queue *vq)
1201 +{
1202 + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
1203 + mutex_unlock(&dev->mutex);
1204 +}
1205 +
1206 +static struct vb2_ops bm2835_mmal_video_qops = {
1207 + .queue_setup = queue_setup,
1208 + .buf_prepare = buffer_prepare,
1209 + .buf_queue = buffer_queue,
1210 + .start_streaming = start_streaming,
1211 + .stop_streaming = stop_streaming,
1212 + .wait_prepare = bm2835_mmal_unlock,
1213 + .wait_finish = bm2835_mmal_lock,
1214 +};
1215 +
1216 +/* ------------------------------------------------------------------
1217 + IOCTL operations
1218 + ------------------------------------------------------------------*/
1219 +
1220 +static int set_overlay_params(struct bm2835_mmal_dev *dev,
1221 + struct vchiq_mmal_port *port)
1222 +{
1223 + int ret;
1224 + struct mmal_parameter_displayregion prev_config = {
1225 + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
1226 + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
1227 + .layer = PREVIEW_LAYER,
1228 + .alpha = dev->overlay.global_alpha,
1229 + .fullscreen = 0,
1230 + .dest_rect = {
1231 + .x = dev->overlay.w.left,
1232 + .y = dev->overlay.w.top,
1233 + .width = dev->overlay.w.width,
1234 + .height = dev->overlay.w.height,
1235 + },
1236 + };
1237 + ret = vchiq_mmal_port_parameter_set(dev->instance, port,
1238 + MMAL_PARAMETER_DISPLAYREGION,
1239 + &prev_config, sizeof(prev_config));
1240 +
1241 + return ret;
1242 +}
1243 +
1244 +/* overlay ioctl */
1245 +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1246 + struct v4l2_fmtdesc *f)
1247 +{
1248 + struct mmal_fmt *fmt;
1249 +
1250 + if (f->index >= ARRAY_SIZE(formats))
1251 + return -EINVAL;
1252 +
1253 + fmt = &formats[f->index];
1254 +
1255 + strlcpy(f->description, fmt->name, sizeof(f->description));
1256 + f->pixelformat = fmt->fourcc;
1257 + f->flags = fmt->flags;
1258 +
1259 + return 0;
1260 +}
1261 +
1262 +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1263 + struct v4l2_format *f)
1264 +{
1265 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1266 +
1267 + f->fmt.win = dev->overlay;
1268 +
1269 + return 0;
1270 +}
1271 +
1272 +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1273 + struct v4l2_format *f)
1274 +{
1275 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1276 +
1277 + f->fmt.win.field = V4L2_FIELD_NONE;
1278 + f->fmt.win.chromakey = 0;
1279 + f->fmt.win.clips = NULL;
1280 + f->fmt.win.clipcount = 0;
1281 + f->fmt.win.bitmap = NULL;
1282 +
1283 + v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
1284 + &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
1285 + 1, 0);
1286 + v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
1287 + &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
1288 + 1, 0);
1289 +
1290 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1291 + "Overlay: Now w/h %dx%d l/t %dx%d\n",
1292 + f->fmt.win.w.width, f->fmt.win.w.height,
1293 + f->fmt.win.w.left, f->fmt.win.w.top);
1294 +
1295 + v4l2_dump_win_format(1,
1296 + bcm2835_v4l2_debug,
1297 + &dev->v4l2_dev,
1298 + &f->fmt.win,
1299 + __func__);
1300 + return 0;
1301 +}
1302 +
1303 +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1304 + struct v4l2_format *f)
1305 +{
1306 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1307 +
1308 + vidioc_try_fmt_vid_overlay(file, priv, f);
1309 +
1310 + dev->overlay = f->fmt.win;
1311 + if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
1312 + set_overlay_params(dev,
1313 + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1314 + }
1315 +
1316 + return 0;
1317 +}
1318 +
1319 +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
1320 +{
1321 + int ret;
1322 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1323 + struct vchiq_mmal_port *src;
1324 + struct vchiq_mmal_port *dst;
1325 + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
1326 + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
1327 + return 0; /* already in requested state */
1328 +
1329 + src =
1330 + &dev->component[MMAL_COMPONENT_CAMERA]->
1331 + output[MMAL_CAMERA_PORT_PREVIEW];
1332 +
1333 + if (!on) {
1334 + /* disconnect preview ports and disable component */
1335 + ret = vchiq_mmal_port_disable(dev->instance, src);
1336 + if (!ret)
1337 + ret =
1338 + vchiq_mmal_port_connect_tunnel(dev->instance, src,
1339 + NULL);
1340 + if (ret >= 0)
1341 + ret = vchiq_mmal_component_disable(
1342 + dev->instance,
1343 + dev->component[MMAL_COMPONENT_PREVIEW]);
1344 +
1345 + disable_camera(dev);
1346 + return ret;
1347 + }
1348 +
1349 + /* set preview port format and connect it to output */
1350 + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
1351 +
1352 + ret = vchiq_mmal_port_set_format(dev->instance, src);
1353 + if (ret < 0)
1354 + goto error;
1355 +
1356 + ret = set_overlay_params(dev, dst);
1357 + if (ret < 0)
1358 + goto error;
1359 +
1360 + if (enable_camera(dev) < 0)
1361 + goto error;
1362 +
1363 + ret = vchiq_mmal_component_enable(
1364 + dev->instance,
1365 + dev->component[MMAL_COMPONENT_PREVIEW]);
1366 + if (ret < 0)
1367 + goto error;
1368 +
1369 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
1370 + src, dst);
1371 + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
1372 + if (!ret)
1373 + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
1374 +error:
1375 + return ret;
1376 +}
1377 +
1378 +static int vidioc_g_fbuf(struct file *file, void *fh,
1379 + struct v4l2_framebuffer *a)
1380 +{
1381 + /* The video overlay must stay within the framebuffer and can't be
1382 + positioned independently. */
1383 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1384 + struct vchiq_mmal_port *preview_port =
1385 + &dev->component[MMAL_COMPONENT_CAMERA]->
1386 + output[MMAL_CAMERA_PORT_PREVIEW];
1387 +
1388 + a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
1389 + V4L2_FBUF_CAP_GLOBAL_ALPHA;
1390 + a->flags = V4L2_FBUF_FLAG_OVERLAY;
1391 + a->fmt.width = preview_port->es.video.width;
1392 + a->fmt.height = preview_port->es.video.height;
1393 + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
1394 + a->fmt.bytesperline = preview_port->es.video.width;
1395 + a->fmt.sizeimage = (preview_port->es.video.width *
1396 + preview_port->es.video.height * 3)>>1;
1397 + a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1398 +
1399 + return 0;
1400 +}
1401 +
1402 +/* input ioctls */
1403 +static int vidioc_enum_input(struct file *file, void *priv,
1404 + struct v4l2_input *inp)
1405 +{
1406 + /* only a single camera input */
1407 + if (inp->index != 0)
1408 + return -EINVAL;
1409 +
1410 + inp->type = V4L2_INPUT_TYPE_CAMERA;
1411 + sprintf(inp->name, "Camera %u", inp->index);
1412 + return 0;
1413 +}
1414 +
1415 +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1416 +{
1417 + *i = 0;
1418 + return 0;
1419 +}
1420 +
1421 +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1422 +{
1423 + if (i != 0)
1424 + return -EINVAL;
1425 +
1426 + return 0;
1427 +}
1428 +
1429 +/* capture ioctls */
1430 +static int vidioc_querycap(struct file *file, void *priv,
1431 + struct v4l2_capability *cap)
1432 +{
1433 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1434 + u32 major;
1435 + u32 minor;
1436 +
1437 + vchiq_mmal_version(dev->instance, &major, &minor);
1438 +
1439 + strcpy(cap->driver, "bm2835 mmal");
1440 + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
1441 + major, minor);
1442 +
1443 + snprintf(cap->bus_info, sizeof(cap->bus_info),
1444 + "platform:%s", dev->v4l2_dev.name);
1445 + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
1446 + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1447 + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1448 +
1449 + return 0;
1450 +}
1451 +
1452 +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1453 + struct v4l2_fmtdesc *f)
1454 +{
1455 + struct mmal_fmt *fmt;
1456 +
1457 + if (f->index >= ARRAY_SIZE(formats))
1458 + return -EINVAL;
1459 +
1460 + fmt = &formats[f->index];
1461 +
1462 + strlcpy(f->description, fmt->name, sizeof(f->description));
1463 + f->pixelformat = fmt->fourcc;
1464 + f->flags = fmt->flags;
1465 +
1466 + return 0;
1467 +}
1468 +
1469 +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1470 + struct v4l2_format *f)
1471 +{
1472 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1473 +
1474 + f->fmt.pix.width = dev->capture.width;
1475 + f->fmt.pix.height = dev->capture.height;
1476 + f->fmt.pix.field = V4L2_FIELD_NONE;
1477 + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
1478 + f->fmt.pix.bytesperline = dev->capture.stride;
1479 + f->fmt.pix.sizeimage = dev->capture.buffersize;
1480 +
1481 + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
1482 + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1483 + else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
1484 + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1485 + else
1486 + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1487 + f->fmt.pix.priv = 0;
1488 +
1489 + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1490 + __func__);
1491 + return 0;
1492 +}
1493 +
1494 +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1495 + struct v4l2_format *f)
1496 +{
1497 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1498 + struct mmal_fmt *mfmt;
1499 +
1500 + mfmt = get_format(f);
1501 + if (!mfmt) {
1502 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1503 + "Fourcc format (0x%08x) unknown.\n",
1504 + f->fmt.pix.pixelformat);
1505 + f->fmt.pix.pixelformat = formats[0].fourcc;
1506 + mfmt = get_format(f);
1507 + }
1508 +
1509 + f->fmt.pix.field = V4L2_FIELD_NONE;
1510 +
1511 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1512 + "Clipping/aligning %dx%d format %08X\n",
1513 + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1514 +
1515 + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
1516 + &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
1517 + 1, 0);
1518 + f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
1519 +
1520 + /* Image buffer has to be padded to allow for alignment, even though
1521 + * we then remove that padding before delivering the buffer.
1522 + */
1523 + f->fmt.pix.sizeimage = ((f->fmt.pix.height+15)&~15) *
1524 + (((f->fmt.pix.width+31)&~31) * mfmt->depth) >> 3;
1525 +
1526 + if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
1527 + f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
1528 + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
1529 +
1530 + if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
1531 + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1532 + else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
1533 + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1534 + else
1535 + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1536 + f->fmt.pix.priv = 0;
1537 +
1538 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1539 + "Now %dx%d format %08X\n",
1540 + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1541 +
1542 + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1543 + __func__);
1544 + return 0;
1545 +}
1546 +
1547 +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1548 + struct v4l2_format *f)
1549 +{
1550 + int ret;
1551 + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1552 + struct vchiq_mmal_component *encode_component = NULL;
1553 + struct mmal_fmt *mfmt = get_format(f);
1554 +
1555 + BUG_ON(!mfmt);
1556 +
1557 + if (dev->capture.encode_component) {
1558 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1559 + "vid_cap - disconnect previous tunnel\n");
1560 +
1561 + /* Disconnect any previous connection */
1562 + vchiq_mmal_port_connect_tunnel(dev->instance,
1563 + dev->capture.camera_port, NULL);
1564 + dev->capture.camera_port = NULL;
1565 + ret = vchiq_mmal_component_disable(dev->instance,
1566 + dev->capture.
1567 + encode_component);
1568 + if (ret)
1569 + v4l2_err(&dev->v4l2_dev,
1570 + "Failed to disable encode component %d\n",
1571 + ret);
1572 +
1573 + dev->capture.encode_component = NULL;
1574 + }
1575 + /* format dependant port setup */
1576 + switch (mfmt->mmal_component) {
1577 + case MMAL_COMPONENT_CAMERA:
1578 + /* Make a further decision on port based on resolution */
1579 + if (f->fmt.pix.width <= max_video_width
1580 + && f->fmt.pix.height <= max_video_height)
1581 + camera_port = port =
1582 + &dev->component[MMAL_COMPONENT_CAMERA]->
1583 + output[MMAL_CAMERA_PORT_VIDEO];
1584 + else
1585 + camera_port = port =
1586 + &dev->component[MMAL_COMPONENT_CAMERA]->
1587 + output[MMAL_CAMERA_PORT_CAPTURE];
1588 + break;
1589 + case MMAL_COMPONENT_IMAGE_ENCODE:
1590 + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1591 + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1592 + camera_port =
1593 + &dev->component[MMAL_COMPONENT_CAMERA]->
1594 + output[MMAL_CAMERA_PORT_CAPTURE];
1595 + break;
1596 + case MMAL_COMPONENT_VIDEO_ENCODE:
1597 + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1598 + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1599 + camera_port =
1600 + &dev->component[MMAL_COMPONENT_CAMERA]->
1601 + output[MMAL_CAMERA_PORT_VIDEO];
1602 + break;
1603 + default:
1604 + break;
1605 + }
1606 +
1607 + if (!port)
1608 + return -EINVAL;
1609 +
1610 + if (encode_component)
1611 + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1612 + else
1613 + camera_port->format.encoding = mfmt->mmal;
1614 +
1615 + if (dev->rgb_bgr_swapped) {
1616 + if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1617 + camera_port->format.encoding = MMAL_ENCODING_BGR24;
1618 + else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1619 + camera_port->format.encoding = MMAL_ENCODING_RGB24;
1620 + }
1621 +
1622 + camera_port->format.encoding_variant = 0;
1623 + camera_port->es.video.width = f->fmt.pix.width;
1624 + camera_port->es.video.height = f->fmt.pix.height;
1625 + camera_port->es.video.crop.x = 0;
1626 + camera_port->es.video.crop.y = 0;
1627 + camera_port->es.video.crop.width = f->fmt.pix.width;
1628 + camera_port->es.video.crop.height = f->fmt.pix.height;
1629 + camera_port->es.video.frame_rate.num = 0;
1630 + camera_port->es.video.frame_rate.den = 1;
1631 + camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1632 +
1633 + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1634 +
1635 + if (!ret
1636 + && camera_port ==
1637 + &dev->component[MMAL_COMPONENT_CAMERA]->
1638 + output[MMAL_CAMERA_PORT_VIDEO]) {
1639 + bool overlay_enabled =
1640 + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1641 + struct vchiq_mmal_port *preview_port =
1642 + &dev->component[MMAL_COMPONENT_CAMERA]->
1643 + output[MMAL_CAMERA_PORT_PREVIEW];
1644 + /* Preview and encode ports need to match on resolution */
1645 + if (overlay_enabled) {
1646 + /* Need to disable the overlay before we can update
1647 + * the resolution
1648 + */
1649 + ret =
1650 + vchiq_mmal_port_disable(dev->instance,
1651 + preview_port);
1652 + if (!ret)
1653 + ret =
1654 + vchiq_mmal_port_connect_tunnel(
1655 + dev->instance,
1656 + preview_port,
1657 + NULL);
1658 + }
1659 + preview_port->es.video.width = f->fmt.pix.width;
1660 + preview_port->es.video.height = f->fmt.pix.height;
1661 + preview_port->es.video.crop.x = 0;
1662 + preview_port->es.video.crop.y = 0;
1663 + preview_port->es.video.crop.width = f->fmt.pix.width;
1664 + preview_port->es.video.crop.height = f->fmt.pix.height;
1665 + preview_port->es.video.frame_rate.num =
1666 + dev->capture.timeperframe.denominator;
1667 + preview_port->es.video.frame_rate.den =
1668 + dev->capture.timeperframe.numerator;
1669 + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1670 + if (overlay_enabled) {
1671 + ret = vchiq_mmal_port_connect_tunnel(
1672 + dev->instance,
1673 + preview_port,
1674 + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1675 + if (!ret)
1676 + ret = vchiq_mmal_port_enable(dev->instance,
1677 + preview_port,
1678 + NULL);
1679 + }
1680 + }
1681 +
1682 + if (ret) {
1683 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1684 + "%s failed to set format %dx%d %08X\n", __func__,
1685 + f->fmt.pix.width, f->fmt.pix.height,
1686 + f->fmt.pix.pixelformat);
1687 + /* ensure capture is not going to be tried */
1688 + dev->capture.port = NULL;
1689 + } else {
1690 + if (encode_component) {
1691 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1692 + "vid_cap - set up encode comp\n");
1693 +
1694 + /* configure buffering */
1695 + camera_port->current_buffer.size =
1696 + camera_port->recommended_buffer.size;
1697 + camera_port->current_buffer.num =
1698 + camera_port->recommended_buffer.num;
1699 +
1700 + ret =
1701 + vchiq_mmal_port_connect_tunnel(
1702 + dev->instance,
1703 + camera_port,
1704 + &encode_component->input[0]);
1705 + if (ret) {
1706 + v4l2_dbg(1, bcm2835_v4l2_debug,
1707 + &dev->v4l2_dev,
1708 + "%s failed to create connection\n",
1709 + __func__);
1710 + /* ensure capture is not going to be tried */
1711 + dev->capture.port = NULL;
1712 + } else {
1713 + port->es.video.width = f->fmt.pix.width;
1714 + port->es.video.height = f->fmt.pix.height;
1715 + port->es.video.crop.x = 0;
1716 + port->es.video.crop.y = 0;
1717 + port->es.video.crop.width = f->fmt.pix.width;
1718 + port->es.video.crop.height = f->fmt.pix.height;
1719 + port->es.video.frame_rate.num =
1720 + dev->capture.timeperframe.denominator;
1721 + port->es.video.frame_rate.den =
1722 + dev->capture.timeperframe.numerator;
1723 +
1724 + port->format.encoding = mfmt->mmal;
1725 + port->format.encoding_variant = 0;
1726 + /* Set any encoding specific parameters */
1727 + switch (mfmt->mmal_component) {
1728 + case MMAL_COMPONENT_VIDEO_ENCODE:
1729 + port->format.bitrate =
1730 + dev->capture.encode_bitrate;
1731 + break;
1732 + case MMAL_COMPONENT_IMAGE_ENCODE:
1733 + /* Could set EXIF parameters here */
1734 + break;
1735 + default:
1736 + break;
1737 + }
1738 + ret = vchiq_mmal_port_set_format(dev->instance,
1739 + port);
1740 + if (ret)
1741 + v4l2_dbg(1, bcm2835_v4l2_debug,
1742 + &dev->v4l2_dev,
1743 + "%s failed to set format %dx%d fmt %08X\n",
1744 + __func__,
1745 + f->fmt.pix.width,
1746 + f->fmt.pix.height,
1747 + f->fmt.pix.pixelformat
1748 + );
1749 + }
1750 +
1751 + if (!ret) {
1752 + ret = vchiq_mmal_component_enable(
1753 + dev->instance,
1754 + encode_component);
1755 + if (ret) {
1756 + v4l2_dbg(1, bcm2835_v4l2_debug,
1757 + &dev->v4l2_dev,
1758 + "%s Failed to enable encode components\n",
1759 + __func__);
1760 + }
1761 + }
1762 + if (!ret) {
1763 + /* configure buffering */
1764 + port->current_buffer.num = 1;
1765 + port->current_buffer.size =
1766 + f->fmt.pix.sizeimage;
1767 + if (port->format.encoding ==
1768 + MMAL_ENCODING_JPEG) {
1769 + v4l2_dbg(1, bcm2835_v4l2_debug,
1770 + &dev->v4l2_dev,
1771 + "JPG - buf size now %d was %d\n",
1772 + f->fmt.pix.sizeimage,
1773 + port->current_buffer.size);
1774 + port->current_buffer.size =
1775 + (f->fmt.pix.sizeimage <
1776 + (100 << 10))
1777 + ? (100 << 10) : f->fmt.pix.
1778 + sizeimage;
1779 + }
1780 + v4l2_dbg(1, bcm2835_v4l2_debug,
1781 + &dev->v4l2_dev,
1782 + "vid_cap - cur_buf.size set to %d\n",
1783 + f->fmt.pix.sizeimage);
1784 + port->current_buffer.alignment = 0;
1785 + }
1786 + } else {
1787 + /* configure buffering */
1788 + camera_port->current_buffer.num = 1;
1789 + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1790 + camera_port->current_buffer.alignment = 0;
1791 + }
1792 +
1793 + if (!ret) {
1794 + dev->capture.fmt = mfmt;
1795 + dev->capture.stride = f->fmt.pix.bytesperline;
1796 + dev->capture.width = camera_port->es.video.crop.width;
1797 + dev->capture.height = camera_port->es.video.crop.height;
1798 + dev->capture.buffersize = port->current_buffer.size;
1799 +
1800 + /* select port for capture */
1801 + dev->capture.port = port;
1802 + dev->capture.camera_port = camera_port;
1803 + dev->capture.encode_component = encode_component;
1804 + v4l2_dbg(1, bcm2835_v4l2_debug,
1805 + &dev->v4l2_dev,
1806 + "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1807 + port->format.encoding,
1808 + dev->capture.width, dev->capture.height,
1809 + dev->capture.stride, dev->capture.buffersize);
1810 + }
1811 + }
1812 +
1813 + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1814 + return ret;
1815 +}
1816 +
1817 +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1818 + struct v4l2_format *f)
1819 +{
1820 + int ret;
1821 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1822 + struct mmal_fmt *mfmt;
1823 +
1824 + /* try the format to set valid parameters */
1825 + ret = vidioc_try_fmt_vid_cap(file, priv, f);
1826 + if (ret) {
1827 + v4l2_err(&dev->v4l2_dev,
1828 + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1829 + return ret;
1830 + }
1831 +
1832 + /* if a capture is running refuse to set format */
1833 + if (vb2_is_busy(&dev->capture.vb_vidq)) {
1834 + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1835 + return -EBUSY;
1836 + }
1837 +
1838 + /* If the format is unsupported v4l2 says we should switch to
1839 + * a supported one and not return an error. */
1840 + mfmt = get_format(f);
1841 + if (!mfmt) {
1842 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1843 + "Fourcc format (0x%08x) unknown.\n",
1844 + f->fmt.pix.pixelformat);
1845 + f->fmt.pix.pixelformat = formats[0].fourcc;
1846 + mfmt = get_format(f);
1847 + }
1848 +
1849 + ret = mmal_setup_components(dev, f);
1850 + if (ret != 0) {
1851 + v4l2_err(&dev->v4l2_dev,
1852 + "%s: failed to setup mmal components: %d\n",
1853 + __func__, ret);
1854 + ret = -EINVAL;
1855 + }
1856 +
1857 + return ret;
1858 +}
1859 +
1860 +int vidioc_enum_framesizes(struct file *file, void *fh,
1861 + struct v4l2_frmsizeenum *fsize)
1862 +{
1863 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1864 + static const struct v4l2_frmsize_stepwise sizes = {
1865 + MIN_WIDTH, 0, 2,
1866 + MIN_HEIGHT, 0, 2
1867 + };
1868 + int i;
1869 +
1870 + if (fsize->index)
1871 + return -EINVAL;
1872 + for (i = 0; i < ARRAY_SIZE(formats); i++)
1873 + if (formats[i].fourcc == fsize->pixel_format)
1874 + break;
1875 + if (i == ARRAY_SIZE(formats))
1876 + return -EINVAL;
1877 + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1878 + fsize->stepwise = sizes;
1879 + fsize->stepwise.max_width = dev->max_width;
1880 + fsize->stepwise.max_height = dev->max_height;
1881 + return 0;
1882 +}
1883 +
1884 +/* timeperframe is arbitrary and continous */
1885 +static int vidioc_enum_frameintervals(struct file *file, void *priv,
1886 + struct v4l2_frmivalenum *fival)
1887 +{
1888 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1889 + int i;
1890 +
1891 + if (fival->index)
1892 + return -EINVAL;
1893 +
1894 + for (i = 0; i < ARRAY_SIZE(formats); i++)
1895 + if (formats[i].fourcc == fival->pixel_format)
1896 + break;
1897 + if (i == ARRAY_SIZE(formats))
1898 + return -EINVAL;
1899 +
1900 + /* regarding width & height - we support any within range */
1901 + if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1902 + fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1903 + return -EINVAL;
1904 +
1905 + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1906 +
1907 + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
1908 + fival->stepwise.min = tpf_min;
1909 + fival->stepwise.max = tpf_max;
1910 + fival->stepwise.step = (struct v4l2_fract) {1, 1};
1911 +
1912 + return 0;
1913 +}
1914 +
1915 +static int vidioc_g_parm(struct file *file, void *priv,
1916 + struct v4l2_streamparm *parm)
1917 +{
1918 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1919 +
1920 + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1921 + return -EINVAL;
1922 +
1923 + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1924 + parm->parm.capture.timeperframe = dev->capture.timeperframe;
1925 + parm->parm.capture.readbuffers = 1;
1926 + return 0;
1927 +}
1928 +
1929 +#define FRACT_CMP(a, OP, b) \
1930 + ((u64)(a).numerator * (b).denominator OP \
1931 + (u64)(b).numerator * (a).denominator)
1932 +
1933 +static int vidioc_s_parm(struct file *file, void *priv,
1934 + struct v4l2_streamparm *parm)
1935 +{
1936 + struct bm2835_mmal_dev *dev = video_drvdata(file);
1937 + struct v4l2_fract tpf;
1938 + struct mmal_parameter_rational fps_param;
1939 +
1940 + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1941 + return -EINVAL;
1942 +
1943 + tpf = parm->parm.capture.timeperframe;
1944 +
1945 + /* tpf: {*, 0} resets timing; clip to [min, max]*/
1946 + tpf = tpf.denominator ? tpf : tpf_default;
1947 + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1948 + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1949 +
1950 + dev->capture.timeperframe = tpf;
1951 + parm->parm.capture.timeperframe = tpf;
1952 + parm->parm.capture.readbuffers = 1;
1953 + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1954 +
1955 + fps_param.num = 0; /* Select variable fps, and then use
1956 + * FPS_RANGE to select the actual limits.
1957 + */
1958 + fps_param.den = 1;
1959 + set_framerate_params(dev);
1960 +
1961 + return 0;
1962 +}
1963 +
1964 +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1965 + /* overlay */
1966 + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1967 + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1968 + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1969 + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1970 + .vidioc_overlay = vidioc_overlay,
1971 + .vidioc_g_fbuf = vidioc_g_fbuf,
1972 +
1973 + /* inputs */
1974 + .vidioc_enum_input = vidioc_enum_input,
1975 + .vidioc_g_input = vidioc_g_input,
1976 + .vidioc_s_input = vidioc_s_input,
1977 +
1978 + /* capture */
1979 + .vidioc_querycap = vidioc_querycap,
1980 + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1981 + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1982 + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1983 + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1984 +
1985 + /* buffer management */
1986 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
1987 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
1988 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1989 + .vidioc_querybuf = vb2_ioctl_querybuf,
1990 + .vidioc_qbuf = vb2_ioctl_qbuf,
1991 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
1992 + .vidioc_enum_framesizes = vidioc_enum_framesizes,
1993 + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1994 + .vidioc_g_parm = vidioc_g_parm,
1995 + .vidioc_s_parm = vidioc_s_parm,
1996 + .vidioc_streamon = vb2_ioctl_streamon,
1997 + .vidioc_streamoff = vb2_ioctl_streamoff,
1998 +
1999 + .vidioc_log_status = v4l2_ctrl_log_status,
2000 + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2001 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2002 +};
2003 +
2004 +static const struct v4l2_ioctl_ops camera0_ioctl_ops_gstreamer = {
2005 + /* overlay */
2006 + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
2007 + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
2008 + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
2009 + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
2010 + .vidioc_overlay = vidioc_overlay,
2011 + .vidioc_g_fbuf = vidioc_g_fbuf,
2012 +
2013 + /* inputs */
2014 + .vidioc_enum_input = vidioc_enum_input,
2015 + .vidioc_g_input = vidioc_g_input,
2016 + .vidioc_s_input = vidioc_s_input,
2017 +
2018 + /* capture */
2019 + .vidioc_querycap = vidioc_querycap,
2020 + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2021 + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2022 + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2023 + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2024 +
2025 + /* buffer management */
2026 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
2027 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
2028 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2029 + .vidioc_querybuf = vb2_ioctl_querybuf,
2030 + .vidioc_qbuf = vb2_ioctl_qbuf,
2031 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
2032 + /* Remove this function ptr to fix gstreamer bug
2033 + .vidioc_enum_framesizes = vidioc_enum_framesizes, */
2034 + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2035 + .vidioc_g_parm = vidioc_g_parm,
2036 + .vidioc_s_parm = vidioc_s_parm,
2037 + .vidioc_streamon = vb2_ioctl_streamon,
2038 + .vidioc_streamoff = vb2_ioctl_streamoff,
2039 +
2040 + .vidioc_log_status = v4l2_ctrl_log_status,
2041 + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2042 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2043 +};
2044 +
2045 +/* ------------------------------------------------------------------
2046 + Driver init/finalise
2047 + ------------------------------------------------------------------*/
2048 +
2049 +static const struct v4l2_file_operations camera0_fops = {
2050 + .owner = THIS_MODULE,
2051 + .open = v4l2_fh_open,
2052 + .release = vb2_fop_release,
2053 + .read = vb2_fop_read,
2054 + .poll = vb2_fop_poll,
2055 + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
2056 + .mmap = vb2_fop_mmap,
2057 +};
2058 +
2059 +static struct video_device vdev_template = {
2060 + .name = "camera0",
2061 + .fops = &camera0_fops,
2062 + .ioctl_ops = &camera0_ioctl_ops,
2063 + .release = video_device_release_empty,
2064 +};
2065 +
2066 +/* Returns the number of cameras, and also the max resolution supported
2067 + * by those cameras.
2068 + */
2069 +static int get_num_cameras(struct vchiq_mmal_instance *instance,
2070 + unsigned int resolutions[][2], int num_resolutions)
2071 +{
2072 + int ret;
2073 + struct vchiq_mmal_component *cam_info_component;
2074 + struct mmal_parameter_camera_info_t cam_info = {0};
2075 + int param_size = sizeof(cam_info);
2076 + int i;
2077 +
2078 + /* create a camera_info component */
2079 + ret = vchiq_mmal_component_init(instance, "camera_info",
2080 + &cam_info_component);
2081 + if (ret < 0)
2082 + /* Unusual failure - let's guess one camera. */
2083 + return 1;
2084 +
2085 + if (vchiq_mmal_port_parameter_get(instance,
2086 + &cam_info_component->control,
2087 + MMAL_PARAMETER_CAMERA_INFO,
2088 + &cam_info,
2089 + &param_size)) {
2090 + pr_info("Failed to get camera info\n");
2091 + }
2092 + for (i = 0;
2093 + i < (cam_info.num_cameras > num_resolutions ?
2094 + num_resolutions :
2095 + cam_info.num_cameras);
2096 + i++) {
2097 + resolutions[i][0] = cam_info.cameras[i].max_width;
2098 + resolutions[i][1] = cam_info.cameras[i].max_height;
2099 + }
2100 +
2101 + vchiq_mmal_component_finalise(instance,
2102 + cam_info_component);
2103 +
2104 + return cam_info.num_cameras;
2105 +}
2106 +
2107 +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
2108 + struct vchiq_mmal_component *camera,
2109 + struct bm2835_mmal_dev *dev)
2110 +{
2111 + int ret;
2112 + struct mmal_parameter_camera_config cam_config = {
2113 + .max_stills_w = dev->max_width,
2114 + .max_stills_h = dev->max_height,
2115 + .stills_yuv422 = 1,
2116 + .one_shot_stills = 1,
2117 + .max_preview_video_w = (max_video_width > 1920) ?
2118 + max_video_width : 1920,
2119 + .max_preview_video_h = (max_video_height > 1088) ?
2120 + max_video_height : 1088,
2121 + .num_preview_video_frames = 3,
2122 + .stills_capture_circular_buffer_height = 0,
2123 + .fast_preview_resume = 0,
2124 + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
2125 + };
2126 +
2127 + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
2128 + MMAL_PARAMETER_CAMERA_CONFIG,
2129 + &cam_config, sizeof(cam_config));
2130 + return ret;
2131 +}
2132 +
2133 +#define MAX_SUPPORTED_ENCODINGS 20
2134 +
2135 +/* MMAL instance and component init */
2136 +static int __init mmal_init(struct bm2835_mmal_dev *dev)
2137 +{
2138 + int ret;
2139 + struct mmal_es_format *format;
2140 + u32 bool_true = 1;
2141 + u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
2142 + int param_size;
2143 + struct vchiq_mmal_component *camera;
2144 +
2145 + ret = vchiq_mmal_init(&dev->instance);
2146 + if (ret < 0)
2147 + return ret;
2148 +
2149 + /* get the camera component ready */
2150 + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
2151 + &dev->component[MMAL_COMPONENT_CAMERA]);
2152 + if (ret < 0)
2153 + goto unreg_mmal;
2154 +
2155 + camera = dev->component[MMAL_COMPONENT_CAMERA];
2156 + if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
2157 + ret = -EINVAL;
2158 + goto unreg_camera;
2159 + }
2160 +
2161 + ret = set_camera_parameters(dev->instance,
2162 + camera,
2163 + dev);
2164 + if (ret < 0)
2165 + goto unreg_camera;
2166 +
2167 + /* There was an error in the firmware that meant the camera component
2168 + * produced BGR instead of RGB.
2169 + * This is now fixed, but in order to support the old firmwares, we
2170 + * have to check.
2171 + */
2172 + dev->rgb_bgr_swapped = true;
2173 + param_size = sizeof(supported_encodings);
2174 + ret = vchiq_mmal_port_parameter_get(dev->instance,
2175 + &camera->output[MMAL_CAMERA_PORT_CAPTURE],
2176 + MMAL_PARAMETER_SUPPORTED_ENCODINGS,
2177 + &supported_encodings,
2178 + &param_size);
2179 + if (ret == 0) {
2180 + int i;
2181 +
2182 + for (i = 0; i < param_size/sizeof(u32); i++) {
2183 + if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
2184 + /* Found BGR24 first - old firmware. */
2185 + break;
2186 + }
2187 + if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
2188 + /* Found RGB24 first
2189 + * new firmware, so use RGB24.
2190 + */
2191 + dev->rgb_bgr_swapped = false;
2192 + break;
2193 + }
2194 + }
2195 + }
2196 + format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
2197 +
2198 + format->encoding = MMAL_ENCODING_OPAQUE;
2199 + format->encoding_variant = MMAL_ENCODING_I420;
2200 +
2201 + format->es->video.width = 1024;
2202 + format->es->video.height = 768;
2203 + format->es->video.crop.x = 0;
2204 + format->es->video.crop.y = 0;
2205 + format->es->video.crop.width = 1024;
2206 + format->es->video.crop.height = 768;
2207 + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
2208 + format->es->video.frame_rate.den = 1;
2209 +
2210 + format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
2211 +
2212 + format->encoding = MMAL_ENCODING_OPAQUE;
2213 + format->encoding_variant = MMAL_ENCODING_I420;
2214 +
2215 + format->es->video.width = 1024;
2216 + format->es->video.height = 768;
2217 + format->es->video.crop.x = 0;
2218 + format->es->video.crop.y = 0;
2219 + format->es->video.crop.width = 1024;
2220 + format->es->video.crop.height = 768;
2221 + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
2222 + format->es->video.frame_rate.den = 1;
2223 +
2224 + vchiq_mmal_port_parameter_set(dev->instance,
2225 + &camera->output[MMAL_CAMERA_PORT_VIDEO],
2226 + MMAL_PARAMETER_NO_IMAGE_PADDING,
2227 + &bool_true, sizeof(bool_true));
2228 +
2229 + format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
2230 +
2231 + format->encoding = MMAL_ENCODING_OPAQUE;
2232 +
2233 + format->es->video.width = 2592;
2234 + format->es->video.height = 1944;
2235 + format->es->video.crop.x = 0;
2236 + format->es->video.crop.y = 0;
2237 + format->es->video.crop.width = 2592;
2238 + format->es->video.crop.height = 1944;
2239 + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
2240 + format->es->video.frame_rate.den = 1;
2241 +
2242 + dev->capture.width = format->es->video.width;
2243 + dev->capture.height = format->es->video.height;
2244 + dev->capture.fmt = &formats[0];
2245 + dev->capture.encode_component = NULL;
2246 + dev->capture.timeperframe = tpf_default;
2247 + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
2248 + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2249 +
2250 + vchiq_mmal_port_parameter_set(dev->instance,
2251 + &camera->output[MMAL_CAMERA_PORT_CAPTURE],
2252 + MMAL_PARAMETER_NO_IMAGE_PADDING,
2253 + &bool_true, sizeof(bool_true));
2254 +
2255 + /* get the preview component ready */
2256 + ret = vchiq_mmal_component_init(
2257 + dev->instance, "ril.video_render",
2258 + &dev->component[MMAL_COMPONENT_PREVIEW]);
2259 + if (ret < 0)
2260 + goto unreg_camera;
2261 +
2262 + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
2263 + ret = -EINVAL;
2264 + pr_debug("too few input ports %d needed %d\n",
2265 + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
2266 + goto unreg_preview;
2267 + }
2268 +
2269 + /* get the image encoder component ready */
2270 + ret = vchiq_mmal_component_init(
2271 + dev->instance, "ril.image_encode",
2272 + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
2273 + if (ret < 0)
2274 + goto unreg_preview;
2275 +
2276 + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
2277 + ret = -EINVAL;
2278 + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
2279 + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
2280 + 1);
2281 + goto unreg_image_encoder;
2282 + }
2283 +
2284 + /* get the video encoder component ready */
2285 + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
2286 + &dev->
2287 + component[MMAL_COMPONENT_VIDEO_ENCODE]);
2288 + if (ret < 0)
2289 + goto unreg_image_encoder;
2290 +
2291 + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
2292 + ret = -EINVAL;
2293 + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
2294 + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
2295 + 1);
2296 + goto unreg_vid_encoder;
2297 + }
2298 +
2299 + {
2300 + struct vchiq_mmal_port *encoder_port =
2301 + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
2302 + encoder_port->format.encoding = MMAL_ENCODING_H264;
2303 + ret = vchiq_mmal_port_set_format(dev->instance,
2304 + encoder_port);
2305 + }
2306 +
2307 + {
2308 + unsigned int enable = 1;
2309 + vchiq_mmal_port_parameter_set(
2310 + dev->instance,
2311 + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
2312 + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
2313 + &enable, sizeof(enable));
2314 +
2315 + vchiq_mmal_port_parameter_set(dev->instance,
2316 + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
2317 + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
2318 + &enable,
2319 + sizeof(enable));
2320 + }
2321 + ret = bm2835_mmal_set_all_camera_controls(dev);
2322 + if (ret < 0)
2323 + goto unreg_vid_encoder;
2324 +
2325 + return 0;
2326 +
2327 +unreg_vid_encoder:
2328 + pr_err("Cleanup: Destroy video encoder\n");
2329 + vchiq_mmal_component_finalise(
2330 + dev->instance,
2331 + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
2332 +
2333 +unreg_image_encoder:
2334 + pr_err("Cleanup: Destroy image encoder\n");
2335 + vchiq_mmal_component_finalise(
2336 + dev->instance,
2337 + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
2338 +
2339 +unreg_preview:
2340 + pr_err("Cleanup: Destroy video render\n");
2341 + vchiq_mmal_component_finalise(dev->instance,
2342 + dev->component[MMAL_COMPONENT_PREVIEW]);
2343 +
2344 +unreg_camera:
2345 + pr_err("Cleanup: Destroy camera\n");
2346 + vchiq_mmal_component_finalise(dev->instance,
2347 + dev->component[MMAL_COMPONENT_CAMERA]);
2348 +
2349 +unreg_mmal:
2350 + vchiq_mmal_finalise(dev->instance);
2351 + return ret;
2352 +}
2353 +
2354 +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
2355 + struct video_device *vfd)
2356 +{
2357 + int ret;
2358 +
2359 + *vfd = vdev_template;
2360 + if (gst_v4l2src_is_broken) {
2361 + v4l2_info(&dev->v4l2_dev,
2362 + "Work-around for gstreamer issue is active.\n");
2363 + vfd->ioctl_ops = &camera0_ioctl_ops_gstreamer;
2364 + }
2365 +
2366 + vfd->v4l2_dev = &dev->v4l2_dev;
2367 +
2368 + vfd->lock = &dev->mutex;
2369 +
2370 + vfd->queue = &dev->capture.vb_vidq;
2371 +
2372 + /* video device needs to be able to access instance data */
2373 + video_set_drvdata(vfd, dev);
2374 +
2375 + ret = video_register_device(vfd,
2376 + VFL_TYPE_GRABBER,
2377 + video_nr[dev->camera_num]);
2378 + if (ret < 0)
2379 + return ret;
2380 +
2381 + v4l2_info(vfd->v4l2_dev,
2382 + "V4L2 device registered as %s - stills mode > %dx%d\n",
2383 + video_device_node_name(vfd), max_video_width, max_video_height);
2384 +
2385 + return 0;
2386 +}
2387 +
2388 +void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
2389 +{
2390 + if (!dev)
2391 + return;
2392 +
2393 + v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2394 + video_device_node_name(&dev->vdev));
2395 +
2396 + video_unregister_device(&dev->vdev);
2397 +
2398 + if (dev->capture.encode_component) {
2399 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
2400 + "mmal_exit - disconnect tunnel\n");
2401 + vchiq_mmal_port_connect_tunnel(dev->instance,
2402 + dev->capture.camera_port, NULL);
2403 + vchiq_mmal_component_disable(dev->instance,
2404 + dev->capture.encode_component);
2405 + }
2406 + vchiq_mmal_component_disable(dev->instance,
2407 + dev->component[MMAL_COMPONENT_CAMERA]);
2408 +
2409 + vchiq_mmal_component_finalise(dev->instance,
2410 + dev->
2411 + component[MMAL_COMPONENT_VIDEO_ENCODE]);
2412 +
2413 + vchiq_mmal_component_finalise(dev->instance,
2414 + dev->
2415 + component[MMAL_COMPONENT_IMAGE_ENCODE]);
2416 +
2417 + vchiq_mmal_component_finalise(dev->instance,
2418 + dev->component[MMAL_COMPONENT_PREVIEW]);
2419 +
2420 + vchiq_mmal_component_finalise(dev->instance,
2421 + dev->component[MMAL_COMPONENT_CAMERA]);
2422 +
2423 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
2424 +
2425 + v4l2_device_unregister(&dev->v4l2_dev);
2426 +
2427 + kfree(dev);
2428 +}
2429 +
2430 +static struct v4l2_format default_v4l2_format = {
2431 + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
2432 + .fmt.pix.width = 1024,
2433 + .fmt.pix.bytesperline = 0,
2434 + .fmt.pix.height = 768,
2435 + .fmt.pix.sizeimage = 1024*768,
2436 +};
2437 +
2438 +static int __init bm2835_mmal_init(void)
2439 +{
2440 + int ret;
2441 + struct bm2835_mmal_dev *dev;
2442 + struct vb2_queue *q;
2443 + int camera;
2444 + unsigned int num_cameras;
2445 + struct vchiq_mmal_instance *instance;
2446 + unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
2447 +
2448 + ret = vchiq_mmal_init(&instance);
2449 + if (ret < 0)
2450 + return ret;
2451 +
2452 + num_cameras = get_num_cameras(instance,
2453 + resolutions,
2454 + MAX_BCM2835_CAMERAS);
2455 + if (num_cameras > MAX_BCM2835_CAMERAS)
2456 + num_cameras = MAX_BCM2835_CAMERAS;
2457 +
2458 + for (camera = 0; camera < num_cameras; camera++) {
2459 + dev = kzalloc(sizeof(struct bm2835_mmal_dev), GFP_KERNEL);
2460 + if (!dev)
2461 + return -ENOMEM;
2462 +
2463 + dev->camera_num = camera;
2464 + dev->max_width = resolutions[camera][0];
2465 + dev->max_height = resolutions[camera][1];
2466 +
2467 + /* setup device defaults */
2468 + dev->overlay.w.left = 150;
2469 + dev->overlay.w.top = 50;
2470 + dev->overlay.w.width = 1024;
2471 + dev->overlay.w.height = 768;
2472 + dev->overlay.clipcount = 0;
2473 + dev->overlay.field = V4L2_FIELD_NONE;
2474 + dev->overlay.global_alpha = 255;
2475 +
2476 + dev->capture.fmt = &formats[3]; /* JPEG */
2477 +
2478 + /* v4l device registration */
2479 + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2480 + "%s", BM2835_MMAL_MODULE_NAME);
2481 + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
2482 + if (ret)
2483 + goto free_dev;
2484 +
2485 + /* setup v4l controls */
2486 + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
2487 + if (ret < 0)
2488 + goto unreg_dev;
2489 + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
2490 +
2491 + /* mmal init */
2492 + dev->instance = instance;
2493 + ret = mmal_init(dev);
2494 + if (ret < 0)
2495 + goto unreg_dev;
2496 +
2497 + /* initialize queue */
2498 + q = &dev->capture.vb_vidq;
2499 + memset(q, 0, sizeof(*q));
2500 + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2501 + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
2502 + q->drv_priv = dev;
2503 + q->buf_struct_size = sizeof(struct mmal_buffer);
2504 + q->ops = &bm2835_mmal_video_qops;
2505 + q->mem_ops = &vb2_vmalloc_memops;
2506 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2507 + ret = vb2_queue_init(q);
2508 + if (ret < 0)
2509 + goto unreg_dev;
2510 +
2511 + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
2512 + mutex_init(&dev->mutex);
2513 +
2514 + /* initialise video devices */
2515 + ret = bm2835_mmal_init_device(dev, &dev->vdev);
2516 + if (ret < 0)
2517 + goto unreg_dev;
2518 +
2519 + /* Really want to call vidioc_s_fmt_vid_cap with the default
2520 + * format, but currently the APIs don't join up.
2521 + */
2522 + ret = mmal_setup_components(dev, &default_v4l2_format);
2523 + if (ret < 0) {
2524 + v4l2_err(&dev->v4l2_dev,
2525 + "%s: could not setup components\n", __func__);
2526 + goto unreg_dev;
2527 + }
2528 +
2529 + v4l2_info(&dev->v4l2_dev,
2530 + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
2531 + BM2835_MMAL_VERSION);
2532 +
2533 + gdev[camera] = dev;
2534 + }
2535 + return 0;
2536 +
2537 +unreg_dev:
2538 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
2539 + v4l2_device_unregister(&dev->v4l2_dev);
2540 +
2541 +free_dev:
2542 + kfree(dev);
2543 +
2544 + for ( ; camera > 0; camera--) {
2545 + bcm2835_cleanup_instance(gdev[camera]);
2546 + gdev[camera] = NULL;
2547 + }
2548 + pr_info("%s: error %d while loading driver\n",
2549 + BM2835_MMAL_MODULE_NAME, ret);
2550 +
2551 + return ret;
2552 +}
2553 +
2554 +static void __exit bm2835_mmal_exit(void)
2555 +{
2556 + int camera;
2557 + struct vchiq_mmal_instance *instance = gdev[0]->instance;
2558 +
2559 + for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2560 + bcm2835_cleanup_instance(gdev[camera]);
2561 + gdev[camera] = NULL;
2562 + }
2563 + vchiq_mmal_finalise(instance);
2564 +}
2565 +
2566 +module_init(bm2835_mmal_init);
2567 +module_exit(bm2835_mmal_exit);
2568 --- /dev/null
2569 +++ b/drivers/media/platform/bcm2835/bcm2835-camera.h
2570 @@ -0,0 +1,145 @@
2571 +/*
2572 + * Broadcom BM2835 V4L2 driver
2573 + *
2574 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2575 + *
2576 + * This file is subject to the terms and conditions of the GNU General Public
2577 + * License. See the file COPYING in the main directory of this archive
2578 + * for more details.
2579 + *
2580 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2581 + * Dave Stevenson <dsteve@broadcom.com>
2582 + * Simon Mellor <simellor@broadcom.com>
2583 + * Luke Diamand <luked@broadcom.com>
2584 + *
2585 + * core driver device
2586 + */
2587 +
2588 +#define V4L2_CTRL_COUNT 29 /* number of v4l controls */
2589 +
2590 +enum {
2591 + MMAL_COMPONENT_CAMERA = 0,
2592 + MMAL_COMPONENT_PREVIEW,
2593 + MMAL_COMPONENT_IMAGE_ENCODE,
2594 + MMAL_COMPONENT_VIDEO_ENCODE,
2595 + MMAL_COMPONENT_COUNT
2596 +};
2597 +
2598 +enum {
2599 + MMAL_CAMERA_PORT_PREVIEW = 0,
2600 + MMAL_CAMERA_PORT_VIDEO,
2601 + MMAL_CAMERA_PORT_CAPTURE,
2602 + MMAL_CAMERA_PORT_COUNT
2603 +};
2604 +
2605 +#define PREVIEW_LAYER 2
2606 +
2607 +extern int bcm2835_v4l2_debug;
2608 +
2609 +struct bm2835_mmal_dev {
2610 + /* v4l2 devices */
2611 + struct v4l2_device v4l2_dev;
2612 + struct video_device vdev;
2613 + struct mutex mutex;
2614 +
2615 + /* controls */
2616 + struct v4l2_ctrl_handler ctrl_handler;
2617 + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
2618 + enum v4l2_scene_mode scene_mode;
2619 + struct mmal_colourfx colourfx;
2620 + int hflip;
2621 + int vflip;
2622 + int red_gain;
2623 + int blue_gain;
2624 + enum mmal_parameter_exposuremode exposure_mode_user;
2625 + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
2626 + /* active exposure mode may differ if selected via a scene mode */
2627 + enum mmal_parameter_exposuremode exposure_mode_active;
2628 + enum mmal_parameter_exposuremeteringmode metering_mode;
2629 + unsigned int manual_shutter_speed;
2630 + bool exp_auto_priority;
2631 + bool manual_iso_enabled;
2632 + uint32_t iso;
2633 +
2634 + /* allocated mmal instance and components */
2635 + struct vchiq_mmal_instance *instance;
2636 + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
2637 + int camera_use_count;
2638 +
2639 + struct v4l2_window overlay;
2640 +
2641 + struct {
2642 + unsigned int width; /* width */
2643 + unsigned int height; /* height */
2644 + unsigned int stride; /* stride */
2645 + unsigned int buffersize; /* buffer size with padding */
2646 + struct mmal_fmt *fmt;
2647 + struct v4l2_fract timeperframe;
2648 +
2649 + /* H264 encode bitrate */
2650 + int encode_bitrate;
2651 + /* H264 bitrate mode. CBR/VBR */
2652 + int encode_bitrate_mode;
2653 + /* H264 profile */
2654 + enum v4l2_mpeg_video_h264_profile enc_profile;
2655 + /* H264 level */
2656 + enum v4l2_mpeg_video_h264_level enc_level;
2657 + /* JPEG Q-factor */
2658 + int q_factor;
2659 +
2660 + struct vb2_queue vb_vidq;
2661 +
2662 + /* VC start timestamp for streaming */
2663 + s64 vc_start_timestamp;
2664 + /* Kernel start timestamp for streaming */
2665 + struct timeval kernel_start_ts;
2666 +
2667 + struct vchiq_mmal_port *port; /* port being used for capture */
2668 + /* camera port being used for capture */
2669 + struct vchiq_mmal_port *camera_port;
2670 + /* component being used for encode */
2671 + struct vchiq_mmal_component *encode_component;
2672 + /* number of frames remaining which driver should capture */
2673 + unsigned int frame_count;
2674 + /* last frame completion */
2675 + struct completion frame_cmplt;
2676 +
2677 + } capture;
2678 +
2679 + unsigned int camera_num;
2680 + unsigned int max_width;
2681 + unsigned int max_height;
2682 + unsigned int rgb_bgr_swapped;
2683 +};
2684 +
2685 +int bm2835_mmal_init_controls(
2686 + struct bm2835_mmal_dev *dev,
2687 + struct v4l2_ctrl_handler *hdl);
2688 +
2689 +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
2690 +int set_framerate_params(struct bm2835_mmal_dev *dev);
2691 +
2692 +/* Debug helpers */
2693 +
2694 +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
2695 +{ \
2696 + v4l2_dbg(level, debug, dev, \
2697 +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
2698 + desc == NULL ? "" : desc, \
2699 + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
2700 + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
2701 + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
2702 +}
2703 +#define v4l2_dump_win_format(level, debug, dev, win_fmt, desc) \
2704 +{ \
2705 + v4l2_dbg(level, debug, dev, \
2706 +"%s: w %u h %u l %u t %u field %u chromakey %06X clip %p " \
2707 +"clipcount %u bitmap %p\n", \
2708 + desc == NULL ? "" : desc, \
2709 + (win_fmt)->w.width, (win_fmt)->w.height, \
2710 + (win_fmt)->w.left, (win_fmt)->w.top, \
2711 + (win_fmt)->field, \
2712 + (win_fmt)->chromakey, \
2713 + (win_fmt)->clips, (win_fmt)->clipcount, \
2714 + (win_fmt)->bitmap); \
2715 +}
2716 --- /dev/null
2717 +++ b/drivers/media/platform/bcm2835/controls.c
2718 @@ -0,0 +1,1345 @@
2719 +/*
2720 + * Broadcom BM2835 V4L2 driver
2721 + *
2722 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
2723 + *
2724 + * This file is subject to the terms and conditions of the GNU General Public
2725 + * License. See the file COPYING in the main directory of this archive
2726 + * for more details.
2727 + *
2728 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
2729 + * Dave Stevenson <dsteve@broadcom.com>
2730 + * Simon Mellor <simellor@broadcom.com>
2731 + * Luke Diamand <luked@broadcom.com>
2732 + */
2733 +
2734 +#include <linux/errno.h>
2735 +#include <linux/kernel.h>
2736 +#include <linux/module.h>
2737 +#include <linux/slab.h>
2738 +#include <media/videobuf2-vmalloc.h>
2739 +#include <media/v4l2-device.h>
2740 +#include <media/v4l2-ioctl.h>
2741 +#include <media/v4l2-ctrls.h>
2742 +#include <media/v4l2-fh.h>
2743 +#include <media/v4l2-event.h>
2744 +#include <media/v4l2-common.h>
2745 +
2746 +#include "mmal-common.h"
2747 +#include "mmal-vchiq.h"
2748 +#include "mmal-parameters.h"
2749 +#include "bcm2835-camera.h"
2750 +
2751 +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
2752 + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
2753 + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
2754 + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
2755 + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
2756 + * -4 to +4
2757 + */
2758 +static const s64 ev_bias_qmenu[] = {
2759 + -4000, -3667, -3333,
2760 + -3000, -2667, -2333,
2761 + -2000, -1667, -1333,
2762 + -1000, -667, -333,
2763 + 0, 333, 667,
2764 + 1000, 1333, 1667,
2765 + 2000, 2333, 2667,
2766 + 3000, 3333, 3667,
2767 + 4000
2768 +};
2769 +
2770 +/* Supported ISO values (*1000)
2771 + * ISOO = auto ISO
2772 + */
2773 +static const s64 iso_qmenu[] = {
2774 + 0, 100000, 200000, 400000, 800000,
2775 +};
2776 +static const uint32_t iso_values[] = {
2777 + 0, 100, 200, 400, 800,
2778 +};
2779 +
2780 +static const s64 mains_freq_qmenu[] = {
2781 + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
2782 + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
2783 + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
2784 + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
2785 +};
2786 +
2787 +/* Supported video encode modes */
2788 +static const s64 bitrate_mode_qmenu[] = {
2789 + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
2790 + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
2791 +};
2792 +
2793 +enum bm2835_mmal_ctrl_type {
2794 + MMAL_CONTROL_TYPE_STD,
2795 + MMAL_CONTROL_TYPE_STD_MENU,
2796 + MMAL_CONTROL_TYPE_INT_MENU,
2797 + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
2798 +};
2799 +
2800 +struct bm2835_mmal_v4l2_ctrl;
2801 +
2802 +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
2803 + struct bm2835_mmal_dev *dev,
2804 + struct v4l2_ctrl *ctrl,
2805 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
2806 +
2807 +struct bm2835_mmal_v4l2_ctrl {
2808 + u32 id; /* v4l2 control identifier */
2809 + enum bm2835_mmal_ctrl_type type;
2810 + /* control minimum value or
2811 + * mask for MMAL_CONTROL_TYPE_STD_MENU */
2812 + s32 min;
2813 + s32 max; /* maximum value of control */
2814 + s32 def; /* default value of control */
2815 + s32 step; /* step size of the control */
2816 + const s64 *imenu; /* integer menu array */
2817 + u32 mmal_id; /* mmal parameter id */
2818 + bm2835_mmal_v4l2_ctrl_cb *setter;
2819 + bool ignore_errors;
2820 +};
2821 +
2822 +struct v4l2_to_mmal_effects_setting {
2823 + u32 v4l2_effect;
2824 + u32 mmal_effect;
2825 + s32 col_fx_enable;
2826 + s32 col_fx_fixed_cbcr;
2827 + u32 u;
2828 + u32 v;
2829 + u32 num_effect_params;
2830 + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
2831 +};
2832 +
2833 +static const struct v4l2_to_mmal_effects_setting
2834 + v4l2_to_mmal_effects_values[] = {
2835 + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
2836 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2837 + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
2838 + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
2839 + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
2840 + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
2841 + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
2842 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2843 + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
2844 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2845 + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
2846 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2847 + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
2848 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2849 + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
2850 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2851 + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
2852 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2853 + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
2854 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2855 + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
2856 + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
2857 + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
2858 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2859 + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
2860 + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
2861 + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
2862 + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
2863 + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
2864 + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
2865 + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
2866 + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
2867 +};
2868 +
2869 +struct v4l2_mmal_scene_config {
2870 + enum v4l2_scene_mode v4l2_scene;
2871 + enum mmal_parameter_exposuremode exposure_mode;
2872 + enum mmal_parameter_exposuremeteringmode metering_mode;
2873 +};
2874 +
2875 +static const struct v4l2_mmal_scene_config scene_configs[] = {
2876 + /* V4L2_SCENE_MODE_NONE automatically added */
2877 + {
2878 + V4L2_SCENE_MODE_NIGHT,
2879 + MMAL_PARAM_EXPOSUREMODE_NIGHT,
2880 + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
2881 + },
2882 + {
2883 + V4L2_SCENE_MODE_SPORTS,
2884 + MMAL_PARAM_EXPOSUREMODE_SPORTS,
2885 + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
2886 + },
2887 +};
2888 +
2889 +/* control handlers*/
2890 +
2891 +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
2892 + struct v4l2_ctrl *ctrl,
2893 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2894 +{
2895 + struct mmal_parameter_rational rational_value;
2896 + struct vchiq_mmal_port *control;
2897 +
2898 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2899 +
2900 + rational_value.num = ctrl->val;
2901 + rational_value.den = 100;
2902 +
2903 + return vchiq_mmal_port_parameter_set(dev->instance, control,
2904 + mmal_ctrl->mmal_id,
2905 + &rational_value,
2906 + sizeof(rational_value));
2907 +}
2908 +
2909 +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
2910 + struct v4l2_ctrl *ctrl,
2911 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2912 +{
2913 + u32 u32_value;
2914 + struct vchiq_mmal_port *control;
2915 +
2916 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2917 +
2918 + u32_value = ctrl->val;
2919 +
2920 + return vchiq_mmal_port_parameter_set(dev->instance, control,
2921 + mmal_ctrl->mmal_id,
2922 + &u32_value, sizeof(u32_value));
2923 +}
2924 +
2925 +static int ctrl_set_iso(struct bm2835_mmal_dev *dev,
2926 + struct v4l2_ctrl *ctrl,
2927 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2928 +{
2929 + u32 u32_value;
2930 + struct vchiq_mmal_port *control;
2931 +
2932 + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
2933 + return 1;
2934 +
2935 + if (ctrl->id == V4L2_CID_ISO_SENSITIVITY)
2936 + dev->iso = iso_values[ctrl->val];
2937 + else if (ctrl->id == V4L2_CID_ISO_SENSITIVITY_AUTO)
2938 + dev->manual_iso_enabled =
2939 + (ctrl->val == V4L2_ISO_SENSITIVITY_MANUAL ?
2940 + true :
2941 + false);
2942 +
2943 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2944 +
2945 + if (dev->manual_iso_enabled)
2946 + u32_value = dev->iso;
2947 + else
2948 + u32_value = 0;
2949 +
2950 + return vchiq_mmal_port_parameter_set(dev->instance, control,
2951 + MMAL_PARAMETER_ISO,
2952 + &u32_value, sizeof(u32_value));
2953 +}
2954 +
2955 +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
2956 + struct v4l2_ctrl *ctrl,
2957 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2958 +{
2959 + s32 s32_value;
2960 + struct vchiq_mmal_port *control;
2961 +
2962 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
2963 +
2964 + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
2965 +
2966 + return vchiq_mmal_port_parameter_set(dev->instance, control,
2967 + mmal_ctrl->mmal_id,
2968 + &s32_value, sizeof(s32_value));
2969 +}
2970 +
2971 +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
2972 + struct v4l2_ctrl *ctrl,
2973 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
2974 +{
2975 + int ret;
2976 + u32 u32_value;
2977 + struct vchiq_mmal_component *camera;
2978 +
2979 + camera = dev->component[MMAL_COMPONENT_CAMERA];
2980 +
2981 + u32_value = ((ctrl->val % 360) / 90) * 90;
2982 +
2983 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
2984 + mmal_ctrl->mmal_id,
2985 + &u32_value, sizeof(u32_value));
2986 + if (ret < 0)
2987 + return ret;
2988 +
2989 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
2990 + mmal_ctrl->mmal_id,
2991 + &u32_value, sizeof(u32_value));
2992 + if (ret < 0)
2993 + return ret;
2994 +
2995 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
2996 + mmal_ctrl->mmal_id,
2997 + &u32_value, sizeof(u32_value));
2998 +
2999 + return ret;
3000 +}
3001 +
3002 +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
3003 + struct v4l2_ctrl *ctrl,
3004 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3005 +{
3006 + int ret;
3007 + u32 u32_value;
3008 + struct vchiq_mmal_component *camera;
3009 +
3010 + if (ctrl->id == V4L2_CID_HFLIP)
3011 + dev->hflip = ctrl->val;
3012 + else
3013 + dev->vflip = ctrl->val;
3014 +
3015 + camera = dev->component[MMAL_COMPONENT_CAMERA];
3016 +
3017 + if (dev->hflip && dev->vflip)
3018 + u32_value = MMAL_PARAM_MIRROR_BOTH;
3019 + else if (dev->hflip)
3020 + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
3021 + else if (dev->vflip)
3022 + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
3023 + else
3024 + u32_value = MMAL_PARAM_MIRROR_NONE;
3025 +
3026 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
3027 + mmal_ctrl->mmal_id,
3028 + &u32_value, sizeof(u32_value));
3029 + if (ret < 0)
3030 + return ret;
3031 +
3032 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
3033 + mmal_ctrl->mmal_id,
3034 + &u32_value, sizeof(u32_value));
3035 + if (ret < 0)
3036 + return ret;
3037 +
3038 + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
3039 + mmal_ctrl->mmal_id,
3040 + &u32_value, sizeof(u32_value));
3041 +
3042 + return ret;
3043 +
3044 +}
3045 +
3046 +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
3047 + struct v4l2_ctrl *ctrl,
3048 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3049 +{
3050 + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
3051 + u32 shutter_speed = 0;
3052 + struct vchiq_mmal_port *control;
3053 + int ret = 0;
3054 +
3055 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3056 +
3057 + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
3058 + /* V4L2 is in 100usec increments.
3059 + * MMAL is 1usec.
3060 + */
3061 + dev->manual_shutter_speed = ctrl->val * 100;
3062 + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
3063 + switch (ctrl->val) {
3064 + case V4L2_EXPOSURE_AUTO:
3065 + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
3066 + break;
3067 +
3068 + case V4L2_EXPOSURE_MANUAL:
3069 + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
3070 + break;
3071 + }
3072 + dev->exposure_mode_user = exp_mode;
3073 + dev->exposure_mode_v4l2_user = ctrl->val;
3074 + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
3075 + dev->exp_auto_priority = ctrl->val;
3076 + }
3077 +
3078 + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
3079 + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
3080 + shutter_speed = dev->manual_shutter_speed;
3081 +
3082 + ret = vchiq_mmal_port_parameter_set(dev->instance,
3083 + control,
3084 + MMAL_PARAMETER_SHUTTER_SPEED,
3085 + &shutter_speed,
3086 + sizeof(shutter_speed));
3087 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3088 + control,
3089 + MMAL_PARAMETER_EXPOSURE_MODE,
3090 + &exp_mode,
3091 + sizeof(u32));
3092 + dev->exposure_mode_active = exp_mode;
3093 + }
3094 + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
3095 + * always apply irrespective of scene mode.
3096 + */
3097 + ret += set_framerate_params(dev);
3098 +
3099 + return ret;
3100 +}
3101 +
3102 +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
3103 + struct v4l2_ctrl *ctrl,
3104 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3105 +{
3106 + switch (ctrl->val) {
3107 + case V4L2_EXPOSURE_METERING_AVERAGE:
3108 + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
3109 + break;
3110 +
3111 + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
3112 + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
3113 + break;
3114 +
3115 + case V4L2_EXPOSURE_METERING_SPOT:
3116 + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
3117 + break;
3118 +
3119 + /* todo matrix weighting not added to Linux API till 3.9
3120 + case V4L2_EXPOSURE_METERING_MATRIX:
3121 + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
3122 + break;
3123 + */
3124 +
3125 + }
3126 +
3127 + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
3128 + struct vchiq_mmal_port *control;
3129 + u32 u32_value = dev->metering_mode;
3130 +
3131 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3132 +
3133 + return vchiq_mmal_port_parameter_set(dev->instance, control,
3134 + mmal_ctrl->mmal_id,
3135 + &u32_value, sizeof(u32_value));
3136 + } else
3137 + return 0;
3138 +}
3139 +
3140 +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
3141 + struct v4l2_ctrl *ctrl,
3142 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3143 +{
3144 + u32 u32_value;
3145 + struct vchiq_mmal_port *control;
3146 +
3147 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3148 +
3149 + switch (ctrl->val) {
3150 + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
3151 + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
3152 + break;
3153 + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
3154 + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
3155 + break;
3156 + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
3157 + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
3158 + break;
3159 + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
3160 + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
3161 + break;
3162 + }
3163 +
3164 + return vchiq_mmal_port_parameter_set(dev->instance, control,
3165 + mmal_ctrl->mmal_id,
3166 + &u32_value, sizeof(u32_value));
3167 +}
3168 +
3169 +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
3170 + struct v4l2_ctrl *ctrl,
3171 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3172 +{
3173 + u32 u32_value;
3174 + struct vchiq_mmal_port *control;
3175 +
3176 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3177 +
3178 + switch (ctrl->val) {
3179 + case V4L2_WHITE_BALANCE_MANUAL:
3180 + u32_value = MMAL_PARAM_AWBMODE_OFF;
3181 + break;
3182 +
3183 + case V4L2_WHITE_BALANCE_AUTO:
3184 + u32_value = MMAL_PARAM_AWBMODE_AUTO;
3185 + break;
3186 +
3187 + case V4L2_WHITE_BALANCE_INCANDESCENT:
3188 + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
3189 + break;
3190 +
3191 + case V4L2_WHITE_BALANCE_FLUORESCENT:
3192 + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
3193 + break;
3194 +
3195 + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
3196 + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
3197 + break;
3198 +
3199 + case V4L2_WHITE_BALANCE_HORIZON:
3200 + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
3201 + break;
3202 +
3203 + case V4L2_WHITE_BALANCE_DAYLIGHT:
3204 + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
3205 + break;
3206 +
3207 + case V4L2_WHITE_BALANCE_FLASH:
3208 + u32_value = MMAL_PARAM_AWBMODE_FLASH;
3209 + break;
3210 +
3211 + case V4L2_WHITE_BALANCE_CLOUDY:
3212 + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
3213 + break;
3214 +
3215 + case V4L2_WHITE_BALANCE_SHADE:
3216 + u32_value = MMAL_PARAM_AWBMODE_SHADE;
3217 + break;
3218 +
3219 + }
3220 +
3221 + return vchiq_mmal_port_parameter_set(dev->instance, control,
3222 + mmal_ctrl->mmal_id,
3223 + &u32_value, sizeof(u32_value));
3224 +}
3225 +
3226 +static int ctrl_set_awb_gains(struct bm2835_mmal_dev *dev,
3227 + struct v4l2_ctrl *ctrl,
3228 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3229 +{
3230 + struct vchiq_mmal_port *control;
3231 + struct mmal_parameter_awbgains gains;
3232 +
3233 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3234 +
3235 + if (ctrl->id == V4L2_CID_RED_BALANCE)
3236 + dev->red_gain = ctrl->val;
3237 + else if (ctrl->id == V4L2_CID_BLUE_BALANCE)
3238 + dev->blue_gain = ctrl->val;
3239 +
3240 + gains.r_gain.num = dev->red_gain;
3241 + gains.b_gain.num = dev->blue_gain;
3242 + gains.r_gain.den = gains.b_gain.den = 1000;
3243 +
3244 + return vchiq_mmal_port_parameter_set(dev->instance, control,
3245 + mmal_ctrl->mmal_id,
3246 + &gains, sizeof(gains));
3247 +}
3248 +
3249 +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
3250 + struct v4l2_ctrl *ctrl,
3251 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3252 +{
3253 + int ret = -EINVAL;
3254 + int i, j;
3255 + struct vchiq_mmal_port *control;
3256 + struct mmal_parameter_imagefx_parameters imagefx;
3257 +
3258 + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
3259 + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
3260 +
3261 + imagefx.effect =
3262 + v4l2_to_mmal_effects_values[i].mmal_effect;
3263 + imagefx.num_effect_params =
3264 + v4l2_to_mmal_effects_values[i].num_effect_params;
3265 +
3266 + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
3267 + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
3268 +
3269 + for (j = 0; j < imagefx.num_effect_params; j++)
3270 + imagefx.effect_parameter[j] =
3271 + v4l2_to_mmal_effects_values[i].effect_params[j];
3272 +
3273 + dev->colourfx.enable =
3274 + v4l2_to_mmal_effects_values[i].col_fx_enable;
3275 + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
3276 + dev->colourfx.u =
3277 + v4l2_to_mmal_effects_values[i].u;
3278 + dev->colourfx.v =
3279 + v4l2_to_mmal_effects_values[i].v;
3280 + }
3281 +
3282 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3283 +
3284 + ret = vchiq_mmal_port_parameter_set(
3285 + dev->instance, control,
3286 + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
3287 + &imagefx, sizeof(imagefx));
3288 + if (ret)
3289 + goto exit;
3290 +
3291 + ret = vchiq_mmal_port_parameter_set(
3292 + dev->instance, control,
3293 + MMAL_PARAMETER_COLOUR_EFFECT,
3294 + &dev->colourfx, sizeof(dev->colourfx));
3295 + }
3296 + }
3297 +
3298 +exit:
3299 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3300 + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
3301 + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
3302 + dev->colourfx.enable ? "true" : "false",
3303 + dev->colourfx.u, dev->colourfx.v,
3304 + ret, (ret == 0 ? 0 : -EINVAL));
3305 + return (ret == 0 ? 0 : EINVAL);
3306 +}
3307 +
3308 +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
3309 + struct v4l2_ctrl *ctrl,
3310 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3311 +{
3312 + int ret = -EINVAL;
3313 + struct vchiq_mmal_port *control;
3314 +
3315 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3316 +
3317 + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
3318 + dev->colourfx.enable = ctrl->val & 0xff;
3319 +
3320 + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
3321 + MMAL_PARAMETER_COLOUR_EFFECT,
3322 + &dev->colourfx, sizeof(dev->colourfx));
3323 +
3324 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3325 + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
3326 + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
3327 + (ret == 0 ? 0 : -EINVAL));
3328 + return (ret == 0 ? 0 : EINVAL);
3329 +}
3330 +
3331 +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
3332 + struct v4l2_ctrl *ctrl,
3333 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3334 +{
3335 + int ret;
3336 + struct vchiq_mmal_port *encoder_out;
3337 +
3338 + dev->capture.encode_bitrate = ctrl->val;
3339 +
3340 + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
3341 +
3342 + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
3343 + mmal_ctrl->mmal_id,
3344 + &ctrl->val, sizeof(ctrl->val));
3345 + ret = 0;
3346 + return ret;
3347 +}
3348 +
3349 +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
3350 + struct v4l2_ctrl *ctrl,
3351 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3352 +{
3353 + u32 bitrate_mode;
3354 + struct vchiq_mmal_port *encoder_out;
3355 +
3356 + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
3357 +
3358 + dev->capture.encode_bitrate_mode = ctrl->val;
3359 + switch (ctrl->val) {
3360 + default:
3361 + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
3362 + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
3363 + break;
3364 + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
3365 + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
3366 + break;
3367 + }
3368 +
3369 + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
3370 + mmal_ctrl->mmal_id,
3371 + &bitrate_mode,
3372 + sizeof(bitrate_mode));
3373 + return 0;
3374 +}
3375 +
3376 +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
3377 + struct v4l2_ctrl *ctrl,
3378 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3379 +{
3380 + u32 u32_value;
3381 + struct vchiq_mmal_port *jpeg_out;
3382 +
3383 + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
3384 +
3385 + u32_value = ctrl->val;
3386 +
3387 + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
3388 + mmal_ctrl->mmal_id,
3389 + &u32_value, sizeof(u32_value));
3390 +}
3391 +
3392 +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
3393 + struct v4l2_ctrl *ctrl,
3394 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3395 +{
3396 + u32 u32_value;
3397 + struct vchiq_mmal_port *vid_enc_ctl;
3398 +
3399 + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
3400 +
3401 + u32_value = ctrl->val;
3402 +
3403 + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
3404 + mmal_ctrl->mmal_id,
3405 + &u32_value, sizeof(u32_value));
3406 +}
3407 +
3408 +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
3409 + struct v4l2_ctrl *ctrl,
3410 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3411 +{
3412 + struct mmal_parameter_video_profile param;
3413 + int ret = 0;
3414 +
3415 + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
3416 + switch (ctrl->val) {
3417 + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
3418 + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
3419 + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
3420 + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
3421 + dev->capture.enc_profile = ctrl->val;
3422 + break;
3423 + default:
3424 + ret = -EINVAL;
3425 + break;
3426 + }
3427 + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
3428 + switch (ctrl->val) {
3429 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
3430 + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
3431 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
3432 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
3433 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
3434 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
3435 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
3436 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
3437 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
3438 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
3439 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
3440 + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
3441 + dev->capture.enc_level = ctrl->val;
3442 + break;
3443 + default:
3444 + ret = -EINVAL;
3445 + break;
3446 + }
3447 + }
3448 +
3449 + if (!ret) {
3450 + switch (dev->capture.enc_profile) {
3451 + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
3452 + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
3453 + break;
3454 + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
3455 + param.profile =
3456 + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
3457 + break;
3458 + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
3459 + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
3460 + break;
3461 + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
3462 + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
3463 + break;
3464 + default:
3465 + /* Should never get here */
3466 + break;
3467 + }
3468 +
3469 + switch (dev->capture.enc_level) {
3470 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
3471 + param.level = MMAL_VIDEO_LEVEL_H264_1;
3472 + break;
3473 + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
3474 + param.level = MMAL_VIDEO_LEVEL_H264_1b;
3475 + break;
3476 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
3477 + param.level = MMAL_VIDEO_LEVEL_H264_11;
3478 + break;
3479 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
3480 + param.level = MMAL_VIDEO_LEVEL_H264_12;
3481 + break;
3482 + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
3483 + param.level = MMAL_VIDEO_LEVEL_H264_13;
3484 + break;
3485 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
3486 + param.level = MMAL_VIDEO_LEVEL_H264_2;
3487 + break;
3488 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
3489 + param.level = MMAL_VIDEO_LEVEL_H264_21;
3490 + break;
3491 + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
3492 + param.level = MMAL_VIDEO_LEVEL_H264_22;
3493 + break;
3494 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
3495 + param.level = MMAL_VIDEO_LEVEL_H264_3;
3496 + break;
3497 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
3498 + param.level = MMAL_VIDEO_LEVEL_H264_31;
3499 + break;
3500 + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
3501 + param.level = MMAL_VIDEO_LEVEL_H264_32;
3502 + break;
3503 + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
3504 + param.level = MMAL_VIDEO_LEVEL_H264_4;
3505 + break;
3506 + default:
3507 + /* Should never get here */
3508 + break;
3509 + }
3510 +
3511 + ret = vchiq_mmal_port_parameter_set(dev->instance,
3512 + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
3513 + mmal_ctrl->mmal_id,
3514 + &param, sizeof(param));
3515 + }
3516 + return ret;
3517 +}
3518 +
3519 +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
3520 + struct v4l2_ctrl *ctrl,
3521 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
3522 +{
3523 + int ret = 0;
3524 + int shutter_speed;
3525 + struct vchiq_mmal_port *control;
3526 +
3527 + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
3528 + "scene mode selected %d, was %d\n", ctrl->val,
3529 + dev->scene_mode);
3530 + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
3531 +
3532 + if (ctrl->val == dev->scene_mode)
3533 + return 0;
3534 +
3535 + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
3536 + /* Restore all user selections */
3537 + dev->scene_mode = V4L2_SCENE_MODE_NONE;
3538 +
3539 + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
3540 + shutter_speed = dev->manual_shutter_speed;
3541 + else
3542 + shutter_speed = 0;
3543 +
3544 + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
3545 + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
3546 + __func__, shutter_speed, dev->exposure_mode_user,
3547 + dev->metering_mode);
3548 + ret = vchiq_mmal_port_parameter_set(dev->instance,
3549 + control,
3550 + MMAL_PARAMETER_SHUTTER_SPEED,
3551 + &shutter_speed,
3552 + sizeof(shutter_speed));
3553 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3554 + control,
3555 + MMAL_PARAMETER_EXPOSURE_MODE,
3556 + &dev->exposure_mode_user,
3557 + sizeof(u32));
3558 + dev->exposure_mode_active = dev->exposure_mode_user;
3559 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3560 + control,
3561 + MMAL_PARAMETER_EXP_METERING_MODE,
3562 + &dev->metering_mode,
3563 + sizeof(u32));
3564 + ret += set_framerate_params(dev);
3565 + } else {
3566 + /* Set up scene mode */
3567 + int i;
3568 + const struct v4l2_mmal_scene_config *scene = NULL;
3569 + int shutter_speed;
3570 + enum mmal_parameter_exposuremode exposure_mode;
3571 + enum mmal_parameter_exposuremeteringmode metering_mode;
3572 +
3573 + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
3574 + if (scene_configs[i].v4l2_scene ==
3575 + ctrl->val) {
3576 + scene = &scene_configs[i];
3577 + break;
3578 + }
3579 + }
3580 + if (!scene)
3581 + return -EINVAL;
3582 + if (i >= ARRAY_SIZE(scene_configs))
3583 + return -EINVAL;
3584 +
3585 + /* Set all the values */
3586 + dev->scene_mode = ctrl->val;
3587 +
3588 + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
3589 + shutter_speed = dev->manual_shutter_speed;
3590 + else
3591 + shutter_speed = 0;
3592 + exposure_mode = scene->exposure_mode;
3593 + metering_mode = scene->metering_mode;
3594 +
3595 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3596 + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
3597 + __func__, shutter_speed, exposure_mode, metering_mode);
3598 +
3599 + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
3600 + MMAL_PARAMETER_SHUTTER_SPEED,
3601 + &shutter_speed,
3602 + sizeof(shutter_speed));
3603 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3604 + control,
3605 + MMAL_PARAMETER_EXPOSURE_MODE,
3606 + &exposure_mode,
3607 + sizeof(u32));
3608 + dev->exposure_mode_active = exposure_mode;
3609 + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
3610 + MMAL_PARAMETER_EXPOSURE_MODE,
3611 + &exposure_mode,
3612 + sizeof(u32));
3613 + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
3614 + MMAL_PARAMETER_EXP_METERING_MODE,
3615 + &metering_mode,
3616 + sizeof(u32));
3617 + ret += set_framerate_params(dev);
3618 + }
3619 + if (ret) {
3620 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3621 + "%s: Setting scene to %d, ret=%d\n",
3622 + __func__, ctrl->val, ret);
3623 + ret = -EINVAL;
3624 + }
3625 + return 0;
3626 +}
3627 +
3628 +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
3629 +{
3630 + struct bm2835_mmal_dev *dev =
3631 + container_of(ctrl->handler, struct bm2835_mmal_dev,
3632 + ctrl_handler);
3633 + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
3634 + int ret;
3635 +
3636 + if ((mmal_ctrl == NULL) ||
3637 + (mmal_ctrl->id != ctrl->id) ||
3638 + (mmal_ctrl->setter == NULL)) {
3639 + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
3640 + return -EINVAL;
3641 + }
3642 +
3643 + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
3644 + if (ret)
3645 + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
3646 + ctrl->id, mmal_ctrl->mmal_id, ret);
3647 + if (mmal_ctrl->ignore_errors)
3648 + ret = 0;
3649 + return ret;
3650 +}
3651 +
3652 +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
3653 + .s_ctrl = bm2835_mmal_s_ctrl,
3654 +};
3655 +
3656 +
3657 +
3658 +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
3659 + {
3660 + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
3661 + -100, 100, 0, 1, NULL,
3662 + MMAL_PARAMETER_SATURATION,
3663 + &ctrl_set_rational,
3664 + false
3665 + },
3666 + {
3667 + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
3668 + -100, 100, 0, 1, NULL,
3669 + MMAL_PARAMETER_SHARPNESS,
3670 + &ctrl_set_rational,
3671 + false
3672 + },
3673 + {
3674 + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
3675 + -100, 100, 0, 1, NULL,
3676 + MMAL_PARAMETER_CONTRAST,
3677 + &ctrl_set_rational,
3678 + false
3679 + },
3680 + {
3681 + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
3682 + 0, 100, 50, 1, NULL,
3683 + MMAL_PARAMETER_BRIGHTNESS,
3684 + &ctrl_set_rational,
3685 + false
3686 + },
3687 + {
3688 + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
3689 + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
3690 + MMAL_PARAMETER_ISO,
3691 + &ctrl_set_iso,
3692 + false
3693 + },
3694 + {
3695 + V4L2_CID_ISO_SENSITIVITY_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
3696 + 0, 1, V4L2_ISO_SENSITIVITY_AUTO, 1, NULL,
3697 + MMAL_PARAMETER_ISO,
3698 + &ctrl_set_iso,
3699 + false
3700 + },
3701 + {
3702 + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
3703 + 0, 1, 0, 1, NULL,
3704 + MMAL_PARAMETER_VIDEO_STABILISATION,
3705 + &ctrl_set_value,
3706 + false
3707 + },
3708 +/* {
3709 + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
3710 + }, */
3711 + {
3712 + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
3713 + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
3714 + MMAL_PARAMETER_EXPOSURE_MODE,
3715 + &ctrl_set_exposure,
3716 + false
3717 + },
3718 +/* todo this needs mixing in with set exposure
3719 + {
3720 + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
3721 + },
3722 + */
3723 + {
3724 + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
3725 + /* Units of 100usecs */
3726 + 1, 1*1000*10, 100*10, 1, NULL,
3727 + MMAL_PARAMETER_SHUTTER_SPEED,
3728 + &ctrl_set_exposure,
3729 + false
3730 + },
3731 + {
3732 + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
3733 + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
3734 + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
3735 + MMAL_PARAMETER_EXPOSURE_COMP,
3736 + &ctrl_set_value_ev,
3737 + false
3738 + },
3739 + {
3740 + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
3741 + 0, 1,
3742 + 0, 1, NULL,
3743 + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
3744 + &ctrl_set_exposure,
3745 + false
3746 + },
3747 + {
3748 + V4L2_CID_EXPOSURE_METERING,
3749 + MMAL_CONTROL_TYPE_STD_MENU,
3750 + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
3751 + MMAL_PARAMETER_EXP_METERING_MODE,
3752 + &ctrl_set_metering_mode,
3753 + false
3754 + },
3755 + {
3756 + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
3757 + MMAL_CONTROL_TYPE_STD_MENU,
3758 + ~0x3ff, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
3759 + MMAL_PARAMETER_AWB_MODE,
3760 + &ctrl_set_awb_mode,
3761 + false
3762 + },
3763 + {
3764 + V4L2_CID_RED_BALANCE, MMAL_CONTROL_TYPE_STD,
3765 + 1, 7999, 1000, 1, NULL,
3766 + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
3767 + &ctrl_set_awb_gains,
3768 + false
3769 + },
3770 + {
3771 + V4L2_CID_BLUE_BALANCE, MMAL_CONTROL_TYPE_STD,
3772 + 1, 7999, 1000, 1, NULL,
3773 + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
3774 + &ctrl_set_awb_gains,
3775 + false
3776 + },
3777 + {
3778 + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
3779 + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
3780 + MMAL_PARAMETER_IMAGE_EFFECT,
3781 + &ctrl_set_image_effect,
3782 + false
3783 + },
3784 + {
3785 + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
3786 + 0, 0xffff, 0x8080, 1, NULL,
3787 + MMAL_PARAMETER_COLOUR_EFFECT,
3788 + &ctrl_set_colfx,
3789 + false
3790 + },
3791 + {
3792 + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
3793 + 0, 360, 0, 90, NULL,
3794 + MMAL_PARAMETER_ROTATION,
3795 + &ctrl_set_rotate,
3796 + false
3797 + },
3798 + {
3799 + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
3800 + 0, 1, 0, 1, NULL,
3801 + MMAL_PARAMETER_MIRROR,
3802 + &ctrl_set_flip,
3803 + false
3804 + },
3805 + {
3806 + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
3807 + 0, 1, 0, 1, NULL,
3808 + MMAL_PARAMETER_MIRROR,
3809 + &ctrl_set_flip,
3810 + false
3811 + },
3812 + {
3813 + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
3814 + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
3815 + 0, 0, bitrate_mode_qmenu,
3816 + MMAL_PARAMETER_RATECONTROL,
3817 + &ctrl_set_bitrate_mode,
3818 + false
3819 + },
3820 + {
3821 + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
3822 + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
3823 + MMAL_PARAMETER_VIDEO_BIT_RATE,
3824 + &ctrl_set_bitrate,
3825 + false
3826 + },
3827 + {
3828 + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
3829 + 1, 100,
3830 + 30, 1, NULL,
3831 + MMAL_PARAMETER_JPEG_Q_FACTOR,
3832 + &ctrl_set_image_encode_output,
3833 + false
3834 + },
3835 + {
3836 + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
3837 + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
3838 + 1, 1, NULL,
3839 + MMAL_PARAMETER_FLICKER_AVOID,
3840 + &ctrl_set_flicker_avoidance,
3841 + false
3842 + },
3843 + {
3844 + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
3845 + 0, 1,
3846 + 0, 1, NULL,
3847 + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
3848 + &ctrl_set_video_encode_param_output,
3849 + true /* Errors ignored as requires latest firmware to work */
3850 + },
3851 + {
3852 + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
3853 + MMAL_CONTROL_TYPE_STD_MENU,
3854 + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
3855 + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
3856 + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
3857 + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
3858 + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
3859 + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
3860 + MMAL_PARAMETER_PROFILE,
3861 + &ctrl_set_video_encode_profile_level,
3862 + false
3863 + },
3864 + {
3865 + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
3866 + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
3867 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
3868 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
3869 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
3870 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
3871 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
3872 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
3873 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
3874 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
3875 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
3876 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
3877 + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
3878 + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
3879 + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
3880 + MMAL_PARAMETER_PROFILE,
3881 + &ctrl_set_video_encode_profile_level,
3882 + false
3883 + },
3884 + {
3885 + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
3886 + -1, /* Min is computed at runtime */
3887 + V4L2_SCENE_MODE_TEXT,
3888 + V4L2_SCENE_MODE_NONE, 1, NULL,
3889 + MMAL_PARAMETER_PROFILE,
3890 + &ctrl_set_scene_mode,
3891 + false
3892 + },
3893 + {
3894 + V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, MMAL_CONTROL_TYPE_STD,
3895 + 0, 0x7FFFFFFF, 60, 1, NULL,
3896 + MMAL_PARAMETER_INTRAPERIOD,
3897 + &ctrl_set_video_encode_param_output,
3898 + false
3899 + },
3900 +};
3901 +
3902 +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
3903 +{
3904 + int c;
3905 + int ret = 0;
3906 +
3907 + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
3908 + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
3909 + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
3910 + &v4l2_ctrls[c]);
3911 + if (!v4l2_ctrls[c].ignore_errors && ret) {
3912 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3913 + "Failed when setting default values for ctrl %d\n",
3914 + c);
3915 + break;
3916 + }
3917 + }
3918 + }
3919 + return ret;
3920 +}
3921 +
3922 +int set_framerate_params(struct bm2835_mmal_dev *dev)
3923 +{
3924 + struct mmal_parameter_fps_range fps_range;
3925 + int ret;
3926 +
3927 + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
3928 + (dev->exp_auto_priority)) {
3929 + /* Variable FPS. Define min FPS as 1fps.
3930 + * Max as max defined FPS.
3931 + */
3932 + fps_range.fps_low.num = 1;
3933 + fps_range.fps_low.den = 1;
3934 + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
3935 + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
3936 + } else {
3937 + /* Fixed FPS - set min and max to be the same */
3938 + fps_range.fps_low.num = fps_range.fps_high.num =
3939 + dev->capture.timeperframe.denominator;
3940 + fps_range.fps_low.den = fps_range.fps_high.den =
3941 + dev->capture.timeperframe.numerator;
3942 + }
3943 +
3944 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
3945 + "Set fps range to %d/%d to %d/%d\n",
3946 + fps_range.fps_low.num,
3947 + fps_range.fps_low.den,
3948 + fps_range.fps_high.num,
3949 + fps_range.fps_high.den
3950 + );
3951 +
3952 + ret = vchiq_mmal_port_parameter_set(dev->instance,
3953 + &dev->component[MMAL_COMPONENT_CAMERA]->
3954 + output[MMAL_CAMERA_PORT_PREVIEW],
3955 + MMAL_PARAMETER_FPS_RANGE,
3956 + &fps_range, sizeof(fps_range));
3957 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3958 + &dev->component[MMAL_COMPONENT_CAMERA]->
3959 + output[MMAL_CAMERA_PORT_VIDEO],
3960 + MMAL_PARAMETER_FPS_RANGE,
3961 + &fps_range, sizeof(fps_range));
3962 + ret += vchiq_mmal_port_parameter_set(dev->instance,
3963 + &dev->component[MMAL_COMPONENT_CAMERA]->
3964 + output[MMAL_CAMERA_PORT_CAPTURE],
3965 + MMAL_PARAMETER_FPS_RANGE,
3966 + &fps_range, sizeof(fps_range));
3967 + if (ret)
3968 + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
3969 + "Failed to set fps ret %d\n",
3970 + ret);
3971 +
3972 + return ret;
3973 +
3974 +}
3975 +
3976 +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
3977 + struct v4l2_ctrl_handler *hdl)
3978 +{
3979 + int c;
3980 + const struct bm2835_mmal_v4l2_ctrl *ctrl;
3981 +
3982 + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
3983 +
3984 + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
3985 + ctrl = &v4l2_ctrls[c];
3986 +
3987 + switch (ctrl->type) {
3988 + case MMAL_CONTROL_TYPE_STD:
3989 + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
3990 + &bm2835_mmal_ctrl_ops, ctrl->id,
3991 + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
3992 + break;
3993 +
3994 + case MMAL_CONTROL_TYPE_STD_MENU:
3995 + {
3996 + int mask = ctrl->min;
3997 +
3998 + if (ctrl->id == V4L2_CID_SCENE_MODE) {
3999 + /* Special handling to work out the mask
4000 + * value based on the scene_configs array
4001 + * at runtime. Reduces the chance of
4002 + * mismatches.
4003 + */
4004 + int i;
4005 + mask = 1<<V4L2_SCENE_MODE_NONE;
4006 + for (i = 0;
4007 + i < ARRAY_SIZE(scene_configs);
4008 + i++) {
4009 + mask |= 1<<scene_configs[i].v4l2_scene;
4010 + }
4011 + mask = ~mask;
4012 + }
4013 +
4014 + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
4015 + &bm2835_mmal_ctrl_ops, ctrl->id,
4016 + ctrl->max, mask, ctrl->def);
4017 + break;
4018 + }
4019 +
4020 + case MMAL_CONTROL_TYPE_INT_MENU:
4021 + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
4022 + &bm2835_mmal_ctrl_ops, ctrl->id,
4023 + ctrl->max, ctrl->def, ctrl->imenu);
4024 + break;
4025 +
4026 + case MMAL_CONTROL_TYPE_CLUSTER:
4027 + /* skip this entry when constructing controls */
4028 + continue;
4029 + }
4030 +
4031 + if (hdl->error)
4032 + break;
4033 +
4034 + dev->ctrls[c]->priv = (void *)ctrl;
4035 + }
4036 +
4037 + if (hdl->error) {
4038 + pr_err("error adding control %d/%d id 0x%x\n", c,
4039 + V4L2_CTRL_COUNT, ctrl->id);
4040 + return hdl->error;
4041 + }
4042 +
4043 + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
4044 + ctrl = &v4l2_ctrls[c];
4045 +
4046 + switch (ctrl->type) {
4047 + case MMAL_CONTROL_TYPE_CLUSTER:
4048 + v4l2_ctrl_auto_cluster(ctrl->min,
4049 + &dev->ctrls[c+1],
4050 + ctrl->max,
4051 + ctrl->def);
4052 + break;
4053 +
4054 + case MMAL_CONTROL_TYPE_STD:
4055 + case MMAL_CONTROL_TYPE_STD_MENU:
4056 + case MMAL_CONTROL_TYPE_INT_MENU:
4057 + break;
4058 + }
4059 +
4060 + }
4061 +
4062 + return 0;
4063 +}
4064 --- /dev/null
4065 +++ b/drivers/media/platform/bcm2835/mmal-common.h
4066 @@ -0,0 +1,53 @@
4067 +/*
4068 + * Broadcom BM2835 V4L2 driver
4069 + *
4070 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4071 + *
4072 + * This file is subject to the terms and conditions of the GNU General Public
4073 + * License. See the file COPYING in the main directory of this archive
4074 + * for more details.
4075 + *
4076 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4077 + * Dave Stevenson <dsteve@broadcom.com>
4078 + * Simon Mellor <simellor@broadcom.com>
4079 + * Luke Diamand <luked@broadcom.com>
4080 + *
4081 + * MMAL structures
4082 + *
4083 + */
4084 +
4085 +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
4086 +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
4087 +
4088 +/** Special value signalling that time is not known */
4089 +#define MMAL_TIME_UNKNOWN (1LL<<63)
4090 +
4091 +/* mapping between v4l and mmal video modes */
4092 +struct mmal_fmt {
4093 + char *name;
4094 + u32 fourcc; /* v4l2 format id */
4095 + int flags; /* v4l2 flags field */
4096 + u32 mmal;
4097 + int depth;
4098 + u32 mmal_component; /* MMAL component index to be used to encode */
4099 + u32 ybbp; /* depth of first Y plane for planar formats */
4100 +};
4101 +
4102 +/* buffer for one video frame */
4103 +struct mmal_buffer {
4104 + /* v4l buffer data -- must be first */
4105 + struct vb2_v4l2_buffer vb;
4106 +
4107 + /* list of buffers available */
4108 + struct list_head list;
4109 +
4110 + void *buffer; /* buffer pointer */
4111 + unsigned long buffer_size; /* size of allocated buffer */
4112 +};
4113 +
4114 +/* */
4115 +struct mmal_colourfx {
4116 + s32 enable;
4117 + u32 u;
4118 + u32 v;
4119 +};
4120 --- /dev/null
4121 +++ b/drivers/media/platform/bcm2835/mmal-encodings.h
4122 @@ -0,0 +1,127 @@
4123 +/*
4124 + * Broadcom BM2835 V4L2 driver
4125 + *
4126 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4127 + *
4128 + * This file is subject to the terms and conditions of the GNU General Public
4129 + * License. See the file COPYING in the main directory of this archive
4130 + * for more details.
4131 + *
4132 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4133 + * Dave Stevenson <dsteve@broadcom.com>
4134 + * Simon Mellor <simellor@broadcom.com>
4135 + * Luke Diamand <luked@broadcom.com>
4136 + */
4137 +#ifndef MMAL_ENCODINGS_H
4138 +#define MMAL_ENCODINGS_H
4139 +
4140 +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
4141 +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
4142 +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
4143 +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
4144 +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
4145 +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
4146 +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
4147 +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
4148 +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
4149 +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
4150 +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
4151 +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
4152 +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
4153 +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
4154 +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
4155 +
4156 +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
4157 +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
4158 +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
4159 +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
4160 +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
4161 +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
4162 +
4163 +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
4164 +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
4165 +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
4166 +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
4167 +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
4168 +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
4169 +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
4170 +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
4171 +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
4172 +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
4173 +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
4174 +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
4175 +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
4176 +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
4177 +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
4178 +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
4179 +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
4180 +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
4181 +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
4182 +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
4183 +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
4184 +
4185 +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
4186 + * This format is *not* opaque - if requested you will receive full frames
4187 + * of YUV_UV video.
4188 + */
4189 +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
4190 +
4191 +/** VideoCore opaque image format, image handles are returned to
4192 + * the host but not the actual image data.
4193 + */
4194 +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
4195 +
4196 +/** An EGL image handle
4197 + */
4198 +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
4199 +
4200 +/* }@ */
4201 +
4202 +/** \name Pre-defined audio encodings */
4203 +/* @{ */
4204 +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
4205 +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
4206 +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
4207 +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
4208 +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
4209 +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
4210 +
4211 +/* Pre-defined H264 encoding variants */
4212 +
4213 +/** ISO 14496-10 Annex B byte stream format */
4214 +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
4215 +/** ISO 14496-15 AVC stream format */
4216 +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
4217 +/** Implicitly delineated NAL units without emulation prevention */
4218 +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
4219 +
4220 +
4221 +/** \defgroup MmalColorSpace List of pre-defined video color spaces
4222 + * This defines a list of common color spaces. This list isn't exhaustive and
4223 + * is only provided as a convenience to avoid clients having to use FourCC
4224 + * codes directly. However components are allowed to define and use their own
4225 + * FourCC codes.
4226 + */
4227 +/* @{ */
4228 +
4229 +/** Unknown color space */
4230 +#define MMAL_COLOR_SPACE_UNKNOWN 0
4231 +/** ITU-R BT.601-5 [SDTV] */
4232 +#define MMAL_COLOR_SPACE_ITUR_BT601 MMAL_FOURCC('Y', '6', '0', '1')
4233 +/** ITU-R BT.709-3 [HDTV] */
4234 +#define MMAL_COLOR_SPACE_ITUR_BT709 MMAL_FOURCC('Y', '7', '0', '9')
4235 +/** JPEG JFIF */
4236 +#define MMAL_COLOR_SPACE_JPEG_JFIF MMAL_FOURCC('Y', 'J', 'F', 'I')
4237 +/** Title 47 Code of Federal Regulations (2003) 73.682 (a) (20) */
4238 +#define MMAL_COLOR_SPACE_FCC MMAL_FOURCC('Y', 'F', 'C', 'C')
4239 +/** Society of Motion Picture and Television Engineers 240M (1999) */
4240 +#define MMAL_COLOR_SPACE_SMPTE240M MMAL_FOURCC('Y', '2', '4', '0')
4241 +/** ITU-R BT.470-2 System M */
4242 +#define MMAL_COLOR_SPACE_BT470_2_M MMAL_FOURCC('Y', '_', '_', 'M')
4243 +/** ITU-R BT.470-2 System BG */
4244 +#define MMAL_COLOR_SPACE_BT470_2_BG MMAL_FOURCC('Y', '_', 'B', 'G')
4245 +/** JPEG JFIF, but with 16..255 luma */
4246 +#define MMAL_COLOR_SPACE_JFIF_Y16_255 MMAL_FOURCC('Y', 'Y', '1', '6')
4247 +/* @} MmalColorSpace List */
4248 +
4249 +#endif /* MMAL_ENCODINGS_H */
4250 --- /dev/null
4251 +++ b/drivers/media/platform/bcm2835/mmal-msg-common.h
4252 @@ -0,0 +1,50 @@
4253 +/*
4254 + * Broadcom BM2835 V4L2 driver
4255 + *
4256 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4257 + *
4258 + * This file is subject to the terms and conditions of the GNU General Public
4259 + * License. See the file COPYING in the main directory of this archive
4260 + * for more details.
4261 + *
4262 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4263 + * Dave Stevenson <dsteve@broadcom.com>
4264 + * Simon Mellor <simellor@broadcom.com>
4265 + * Luke Diamand <luked@broadcom.com>
4266 + */
4267 +
4268 +#ifndef MMAL_MSG_COMMON_H
4269 +#define MMAL_MSG_COMMON_H
4270 +
4271 +enum mmal_msg_status {
4272 + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
4273 + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
4274 + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
4275 + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
4276 + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
4277 + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
4278 + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
4279 + MMAL_MSG_STATUS_EIO, /**< I/O error */
4280 + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
4281 + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
4282 + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
4283 + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
4284 + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
4285 + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
4286 + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
4287 + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
4288 +};
4289 +
4290 +struct mmal_rect {
4291 + s32 x; /**< x coordinate (from left) */
4292 + s32 y; /**< y coordinate (from top) */
4293 + s32 width; /**< width */
4294 + s32 height; /**< height */
4295 +};
4296 +
4297 +struct mmal_rational {
4298 + s32 num; /**< Numerator */
4299 + s32 den; /**< Denominator */
4300 +};
4301 +
4302 +#endif /* MMAL_MSG_COMMON_H */
4303 --- /dev/null
4304 +++ b/drivers/media/platform/bcm2835/mmal-msg-format.h
4305 @@ -0,0 +1,81 @@
4306 +/*
4307 + * Broadcom BM2835 V4L2 driver
4308 + *
4309 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4310 + *
4311 + * This file is subject to the terms and conditions of the GNU General Public
4312 + * License. See the file COPYING in the main directory of this archive
4313 + * for more details.
4314 + *
4315 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4316 + * Dave Stevenson <dsteve@broadcom.com>
4317 + * Simon Mellor <simellor@broadcom.com>
4318 + * Luke Diamand <luked@broadcom.com>
4319 + */
4320 +
4321 +#ifndef MMAL_MSG_FORMAT_H
4322 +#define MMAL_MSG_FORMAT_H
4323 +
4324 +#include "mmal-msg-common.h"
4325 +
4326 +/* MMAL_ES_FORMAT_T */
4327 +
4328 +
4329 +struct mmal_audio_format {
4330 + u32 channels; /**< Number of audio channels */
4331 + u32 sample_rate; /**< Sample rate */
4332 +
4333 + u32 bits_per_sample; /**< Bits per sample */
4334 + u32 block_align; /**< Size of a block of data */
4335 +};
4336 +
4337 +struct mmal_video_format {
4338 + u32 width; /**< Width of frame in pixels */
4339 + u32 height; /**< Height of frame in rows of pixels */
4340 + struct mmal_rect crop; /**< Visible region of the frame */
4341 + struct mmal_rational frame_rate; /**< Frame rate */
4342 + struct mmal_rational par; /**< Pixel aspect ratio */
4343 +
4344 + /* FourCC specifying the color space of the video stream. See the
4345 + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
4346 + */
4347 + u32 color_space;
4348 +};
4349 +
4350 +struct mmal_subpicture_format {
4351 + u32 x_offset;
4352 + u32 y_offset;
4353 +};
4354 +
4355 +union mmal_es_specific_format {
4356 + struct mmal_audio_format audio;
4357 + struct mmal_video_format video;
4358 + struct mmal_subpicture_format subpicture;
4359 +};
4360 +
4361 +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
4362 +struct mmal_es_format {
4363 + u32 type; /* enum mmal_es_type */
4364 +
4365 + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
4366 + u32 encoding_variant; /* FourCC specifying the specific
4367 + * encoding variant of the elementary
4368 + * stream.
4369 + */
4370 +
4371 + union mmal_es_specific_format *es; /* TODO: pointers in
4372 + * message serialisation?!?
4373 + */
4374 + /* Type specific
4375 + * information for the
4376 + * elementary stream
4377 + */
4378 +
4379 + u32 bitrate; /**< Bitrate in bits per second */
4380 + u32 flags; /**< Flags describing properties of the elementary stream. */
4381 +
4382 + u32 extradata_size; /**< Size of the codec specific data */
4383 + u8 *extradata; /**< Codec specific data */
4384 +};
4385 +
4386 +#endif /* MMAL_MSG_FORMAT_H */
4387 --- /dev/null
4388 +++ b/drivers/media/platform/bcm2835/mmal-msg-port.h
4389 @@ -0,0 +1,107 @@
4390 +/*
4391 + * Broadcom BM2835 V4L2 driver
4392 + *
4393 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4394 + *
4395 + * This file is subject to the terms and conditions of the GNU General Public
4396 + * License. See the file COPYING in the main directory of this archive
4397 + * for more details.
4398 + *
4399 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4400 + * Dave Stevenson <dsteve@broadcom.com>
4401 + * Simon Mellor <simellor@broadcom.com>
4402 + * Luke Diamand <luked@broadcom.com>
4403 + */
4404 +
4405 +/* MMAL_PORT_TYPE_T */
4406 +enum mmal_port_type {
4407 + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
4408 + MMAL_PORT_TYPE_CONTROL, /**< Control port */
4409 + MMAL_PORT_TYPE_INPUT, /**< Input port */
4410 + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
4411 + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
4412 +};
4413 +
4414 +/** The port is pass-through and doesn't need buffer headers allocated */
4415 +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
4416 +/** The port wants to allocate the buffer payloads.
4417 + * This signals a preference that payload allocation should be done
4418 + * on this port for efficiency reasons. */
4419 +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
4420 +/** The port supports format change events.
4421 + * This applies to input ports and is used to let the client know
4422 + * whether the port supports being reconfigured via a format
4423 + * change event (i.e. without having to disable the port). */
4424 +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
4425 +
4426 +/* mmal port structure (MMAL_PORT_T)
4427 + *
4428 + * most elements are informational only, the pointer values for
4429 + * interogation messages are generally provided as additional
4430 + * strucures within the message. When used to set values only teh
4431 + * buffer_num, buffer_size and userdata parameters are writable.
4432 + */
4433 +struct mmal_port {
4434 + void *priv; /* Private member used by the framework */
4435 + const char *name; /* Port name. Used for debugging purposes (RO) */
4436 +
4437 + u32 type; /* Type of the port (RO) enum mmal_port_type */
4438 + u16 index; /* Index of the port in its type list (RO) */
4439 + u16 index_all; /* Index of the port in the list of all ports (RO) */
4440 +
4441 + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
4442 + struct mmal_es_format *format; /* Format of the elementary stream */
4443 +
4444 + u32 buffer_num_min; /* Minimum number of buffers the port
4445 + * requires (RO). This is set by the
4446 + * component.
4447 + */
4448 +
4449 + u32 buffer_size_min; /* Minimum size of buffers the port
4450 + * requires (RO). This is set by the
4451 + * component.
4452 + */
4453 +
4454 + u32 buffer_alignment_min; /* Minimum alignment requirement for
4455 + * the buffers (RO). A value of
4456 + * zero means no special alignment
4457 + * requirements. This is set by the
4458 + * component.
4459 + */
4460 +
4461 + u32 buffer_num_recommended; /* Number of buffers the port
4462 + * recommends for optimal
4463 + * performance (RO). A value of
4464 + * zero means no special
4465 + * recommendation. This is set
4466 + * by the component.
4467 + */
4468 +
4469 + u32 buffer_size_recommended; /* Size of buffers the port
4470 + * recommends for optimal
4471 + * performance (RO). A value of
4472 + * zero means no special
4473 + * recommendation. This is set
4474 + * by the component.
4475 + */
4476 +
4477 + u32 buffer_num; /* Actual number of buffers the port will use.
4478 + * This is set by the client.
4479 + */
4480 +
4481 + u32 buffer_size; /* Actual maximum size of the buffers that
4482 + * will be sent to the port. This is set by
4483 + * the client.
4484 + */
4485 +
4486 + void *component; /* Component this port belongs to (Read Only) */
4487 +
4488 + void *userdata; /* Field reserved for use by the client */
4489 +
4490 + u32 capabilities; /* Flags describing the capabilities of a
4491 + * port (RO). Bitwise combination of \ref
4492 + * portcapabilities "Port capabilities"
4493 + * values.
4494 + */
4495 +
4496 +};
4497 --- /dev/null
4498 +++ b/drivers/media/platform/bcm2835/mmal-msg.h
4499 @@ -0,0 +1,404 @@
4500 +/*
4501 + * Broadcom BM2835 V4L2 driver
4502 + *
4503 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4504 + *
4505 + * This file is subject to the terms and conditions of the GNU General Public
4506 + * License. See the file COPYING in the main directory of this archive
4507 + * for more details.
4508 + *
4509 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4510 + * Dave Stevenson <dsteve@broadcom.com>
4511 + * Simon Mellor <simellor@broadcom.com>
4512 + * Luke Diamand <luked@broadcom.com>
4513 + */
4514 +
4515 +/* all the data structures which serialise the MMAL protocol. note
4516 + * these are directly mapped onto the recived message data.
4517 + *
4518 + * BEWARE: They seem to *assume* pointers are u32 and that there is no
4519 + * structure padding!
4520 + *
4521 + * NOTE: this implementation uses kernel types to ensure sizes. Rather
4522 + * than assigning values to enums to force their size the
4523 + * implementation uses fixed size types and not the enums (though the
4524 + * comments have the actual enum type
4525 + */
4526 +
4527 +#define VC_MMAL_VER 15
4528 +#define VC_MMAL_MIN_VER 10
4529 +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
4530 +
4531 +/* max total message size is 512 bytes */
4532 +#define MMAL_MSG_MAX_SIZE 512
4533 +/* with six 32bit header elements max payload is therefore 488 bytes */
4534 +#define MMAL_MSG_MAX_PAYLOAD 488
4535 +
4536 +#include "mmal-msg-common.h"
4537 +#include "mmal-msg-format.h"
4538 +#include "mmal-msg-port.h"
4539 +
4540 +enum mmal_msg_type {
4541 + MMAL_MSG_TYPE_QUIT = 1,
4542 + MMAL_MSG_TYPE_SERVICE_CLOSED,
4543 + MMAL_MSG_TYPE_GET_VERSION,
4544 + MMAL_MSG_TYPE_COMPONENT_CREATE,
4545 + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
4546 + MMAL_MSG_TYPE_COMPONENT_ENABLE,
4547 + MMAL_MSG_TYPE_COMPONENT_DISABLE,
4548 + MMAL_MSG_TYPE_PORT_INFO_GET,
4549 + MMAL_MSG_TYPE_PORT_INFO_SET,
4550 + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
4551 + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
4552 + MMAL_MSG_TYPE_BUFFER_TO_HOST,
4553 + MMAL_MSG_TYPE_GET_STATS,
4554 + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
4555 + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
4556 + MMAL_MSG_TYPE_EVENT_TO_HOST,
4557 + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
4558 + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
4559 + MMAL_MSG_TYPE_CONSUME_MEM,
4560 + MMAL_MSG_TYPE_LMK, /* 20 */
4561 + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
4562 + MMAL_MSG_TYPE_DRM_GET_LHS32,
4563 + MMAL_MSG_TYPE_DRM_GET_TIME,
4564 + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
4565 + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
4566 + MMAL_MSG_TYPE_HOST_LOG,
4567 + MMAL_MSG_TYPE_MSG_LAST
4568 +};
4569 +
4570 +/* port action request messages differ depending on the action type */
4571 +enum mmal_msg_port_action_type {
4572 + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
4573 + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
4574 + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
4575 + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
4576 + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
4577 + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
4578 + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
4579 +};
4580 +
4581 +struct mmal_msg_header {
4582 + u32 magic;
4583 + u32 type; /** enum mmal_msg_type */
4584 +
4585 + /* Opaque handle to the control service */
4586 + struct mmal_control_service *control_service;
4587 +
4588 + struct mmal_msg_context *context; /** a u32 per message context */
4589 + u32 status; /** The status of the vchiq operation */
4590 + u32 padding;
4591 +};
4592 +
4593 +/* Send from VC to host to report version */
4594 +struct mmal_msg_version {
4595 + u32 flags;
4596 + u32 major;
4597 + u32 minor;
4598 + u32 minimum;
4599 +};
4600 +
4601 +/* request to VC to create component */
4602 +struct mmal_msg_component_create {
4603 + void *client_component; /* component context */
4604 + char name[128];
4605 + u32 pid; /* For debug */
4606 +};
4607 +
4608 +/* reply from VC to component creation request */
4609 +struct mmal_msg_component_create_reply {
4610 + u32 status; /** enum mmal_msg_status - how does this differ to
4611 + * the one in the header?
4612 + */
4613 + u32 component_handle; /* VideoCore handle for component */
4614 + u32 input_num; /* Number of input ports */
4615 + u32 output_num; /* Number of output ports */
4616 + u32 clock_num; /* Number of clock ports */
4617 +};
4618 +
4619 +/* request to VC to destroy a component */
4620 +struct mmal_msg_component_destroy {
4621 + u32 component_handle;
4622 +};
4623 +
4624 +struct mmal_msg_component_destroy_reply {
4625 + u32 status; /** The component destruction status */
4626 +};
4627 +
4628 +
4629 +/* request and reply to VC to enable a component */
4630 +struct mmal_msg_component_enable {
4631 + u32 component_handle;
4632 +};
4633 +
4634 +struct mmal_msg_component_enable_reply {
4635 + u32 status; /** The component enable status */
4636 +};
4637 +
4638 +
4639 +/* request and reply to VC to disable a component */
4640 +struct mmal_msg_component_disable {
4641 + u32 component_handle;
4642 +};
4643 +
4644 +struct mmal_msg_component_disable_reply {
4645 + u32 status; /** The component disable status */
4646 +};
4647 +
4648 +/* request to VC to get port information */
4649 +struct mmal_msg_port_info_get {
4650 + u32 component_handle; /* component handle port is associated with */
4651 + u32 port_type; /* enum mmal_msg_port_type */
4652 + u32 index; /* port index to query */
4653 +};
4654 +
4655 +/* reply from VC to get port info request */
4656 +struct mmal_msg_port_info_get_reply {
4657 + u32 status; /** enum mmal_msg_status */
4658 + u32 component_handle; /* component handle port is associated with */
4659 + u32 port_type; /* enum mmal_msg_port_type */
4660 + u32 port_index; /* port indexed in query */
4661 + s32 found; /* unused */
4662 + u32 port_handle; /**< Handle to use for this port */
4663 + struct mmal_port port;
4664 + struct mmal_es_format format; /* elementry stream format */
4665 + union mmal_es_specific_format es; /* es type specific data */
4666 + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
4667 +};
4668 +
4669 +/* request to VC to set port information */
4670 +struct mmal_msg_port_info_set {
4671 + u32 component_handle;
4672 + u32 port_type; /* enum mmal_msg_port_type */
4673 + u32 port_index; /* port indexed in query */
4674 + struct mmal_port port;
4675 + struct mmal_es_format format;
4676 + union mmal_es_specific_format es;
4677 + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
4678 +};
4679 +
4680 +/* reply from VC to port info set request */
4681 +struct mmal_msg_port_info_set_reply {
4682 + u32 status;
4683 + u32 component_handle; /* component handle port is associated with */
4684 + u32 port_type; /* enum mmal_msg_port_type */
4685 + u32 index; /* port indexed in query */
4686 + s32 found; /* unused */
4687 + u32 port_handle; /**< Handle to use for this port */
4688 + struct mmal_port port;
4689 + struct mmal_es_format format;
4690 + union mmal_es_specific_format es;
4691 + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
4692 +};
4693 +
4694 +
4695 +/* port action requests that take a mmal_port as a parameter */
4696 +struct mmal_msg_port_action_port {
4697 + u32 component_handle;
4698 + u32 port_handle;
4699 + u32 action; /* enum mmal_msg_port_action_type */
4700 + struct mmal_port port;
4701 +};
4702 +
4703 +/* port action requests that take handles as a parameter */
4704 +struct mmal_msg_port_action_handle {
4705 + u32 component_handle;
4706 + u32 port_handle;
4707 + u32 action; /* enum mmal_msg_port_action_type */
4708 + u32 connect_component_handle;
4709 + u32 connect_port_handle;
4710 +};
4711 +
4712 +struct mmal_msg_port_action_reply {
4713 + u32 status; /** The port action operation status */
4714 +};
4715 +
4716 +
4717 +
4718 +
4719 +/* MMAL buffer transfer */
4720 +
4721 +/** Size of space reserved in a buffer message for short messages. */
4722 +#define MMAL_VC_SHORT_DATA 128
4723 +
4724 +/** Signals that the current payload is the end of the stream of data */
4725 +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
4726 +/** Signals that the start of the current payload starts a frame */
4727 +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
4728 +/** Signals that the end of the current payload ends a frame */
4729 +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
4730 +/** Signals that the current payload contains only complete frames (>1) */
4731 +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
4732 + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
4733 +/** Signals that the current payload is a keyframe (i.e. self decodable) */
4734 +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
4735 +/** Signals a discontinuity in the stream of data (e.g. after a seek).
4736 + * Can be used for instance by a decoder to reset its state */
4737 +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
4738 +/** Signals a buffer containing some kind of config data for the component
4739 + * (e.g. codec config data) */
4740 +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
4741 +/** Signals an encrypted payload */
4742 +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
4743 +/** Signals a buffer containing side information */
4744 +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
4745 +/** Signals a buffer which is the snapshot/postview image from a stills
4746 + * capture
4747 + */
4748 +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
4749 +/** Signals a buffer which contains data known to be corrupted */
4750 +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
4751 +/** Signals that a buffer failed to be transmitted */
4752 +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
4753 +
4754 +struct mmal_driver_buffer {
4755 + u32 magic;
4756 + u32 component_handle;
4757 + u32 port_handle;
4758 + void *client_context;
4759 +};
4760 +
4761 +/* buffer header */
4762 +struct mmal_buffer_header {
4763 + struct mmal_buffer_header *next; /* next header */
4764 + void *priv; /* framework private data */
4765 + u32 cmd;
4766 + void *data;
4767 + u32 alloc_size;
4768 + u32 length;
4769 + u32 offset;
4770 + u32 flags;
4771 + s64 pts;
4772 + s64 dts;
4773 + void *type;
4774 + void *user_data;
4775 +};
4776 +
4777 +struct mmal_buffer_header_type_specific {
4778 + union {
4779 + struct {
4780 + u32 planes;
4781 + u32 offset[4];
4782 + u32 pitch[4];
4783 + u32 flags;
4784 + } video;
4785 + } u;
4786 +};
4787 +
4788 +struct mmal_msg_buffer_from_host {
4789 + /* The front 32 bytes of the buffer header are copied
4790 + * back to us in the reply to allow for context. This
4791 + * area is used to store two mmal_driver_buffer structures to
4792 + * allow for multiple concurrent service users.
4793 + */
4794 + /* control data */
4795 + struct mmal_driver_buffer drvbuf;
4796 +
4797 + /* referenced control data for passthrough buffer management */
4798 + struct mmal_driver_buffer drvbuf_ref;
4799 + struct mmal_buffer_header buffer_header; /* buffer header itself */
4800 + struct mmal_buffer_header_type_specific buffer_header_type_specific;
4801 + s32 is_zero_copy;
4802 + s32 has_reference;
4803 +
4804 + /** allows short data to be xfered in control message */
4805 + u32 payload_in_message;
4806 + u8 short_data[MMAL_VC_SHORT_DATA];
4807 +};
4808 +
4809 +
4810 +/* port parameter setting */
4811 +
4812 +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
4813 +
4814 +struct mmal_msg_port_parameter_set {
4815 + u32 component_handle; /* component */
4816 + u32 port_handle; /* port */
4817 + u32 id; /* Parameter ID */
4818 + u32 size; /* Parameter size */
4819 + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
4820 +};
4821 +
4822 +struct mmal_msg_port_parameter_set_reply {
4823 + u32 status; /** enum mmal_msg_status todo: how does this
4824 + * differ to the one in the header?
4825 + */
4826 +};
4827 +
4828 +/* port parameter getting */
4829 +
4830 +struct mmal_msg_port_parameter_get {
4831 + u32 component_handle; /* component */
4832 + u32 port_handle; /* port */
4833 + u32 id; /* Parameter ID */
4834 + u32 size; /* Parameter size */
4835 +};
4836 +
4837 +struct mmal_msg_port_parameter_get_reply {
4838 + u32 status; /* Status of mmal_port_parameter_get call */
4839 + u32 id; /* Parameter ID */
4840 + u32 size; /* Parameter size */
4841 + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
4842 +};
4843 +
4844 +/* event messages */
4845 +#define MMAL_WORKER_EVENT_SPACE 256
4846 +
4847 +struct mmal_msg_event_to_host {
4848 + void *client_component; /* component context */
4849 +
4850 + u32 port_type;
4851 + u32 port_num;
4852 +
4853 + u32 cmd;
4854 + u32 length;
4855 + u8 data[MMAL_WORKER_EVENT_SPACE];
4856 + struct mmal_buffer_header *delayed_buffer;
4857 +};
4858 +
4859 +/* all mmal messages are serialised through this structure */
4860 +struct mmal_msg {
4861 + /* header */
4862 + struct mmal_msg_header h;
4863 + /* payload */
4864 + union {
4865 + struct mmal_msg_version version;
4866 +
4867 + struct mmal_msg_component_create component_create;
4868 + struct mmal_msg_component_create_reply component_create_reply;
4869 +
4870 + struct mmal_msg_component_destroy component_destroy;
4871 + struct mmal_msg_component_destroy_reply component_destroy_reply;
4872 +
4873 + struct mmal_msg_component_enable component_enable;
4874 + struct mmal_msg_component_enable_reply component_enable_reply;
4875 +
4876 + struct mmal_msg_component_disable component_disable;
4877 + struct mmal_msg_component_disable_reply component_disable_reply;
4878 +
4879 + struct mmal_msg_port_info_get port_info_get;
4880 + struct mmal_msg_port_info_get_reply port_info_get_reply;
4881 +
4882 + struct mmal_msg_port_info_set port_info_set;
4883 + struct mmal_msg_port_info_set_reply port_info_set_reply;
4884 +
4885 + struct mmal_msg_port_action_port port_action_port;
4886 + struct mmal_msg_port_action_handle port_action_handle;
4887 + struct mmal_msg_port_action_reply port_action_reply;
4888 +
4889 + struct mmal_msg_buffer_from_host buffer_from_host;
4890 +
4891 + struct mmal_msg_port_parameter_set port_parameter_set;
4892 + struct mmal_msg_port_parameter_set_reply
4893 + port_parameter_set_reply;
4894 + struct mmal_msg_port_parameter_get
4895 + port_parameter_get;
4896 + struct mmal_msg_port_parameter_get_reply
4897 + port_parameter_get_reply;
4898 +
4899 + struct mmal_msg_event_to_host event_to_host;
4900 +
4901 + u8 payload[MMAL_MSG_MAX_PAYLOAD];
4902 + } u;
4903 +};
4904 --- /dev/null
4905 +++ b/drivers/media/platform/bcm2835/mmal-parameters.h
4906 @@ -0,0 +1,689 @@
4907 +/*
4908 + * Broadcom BM2835 V4L2 driver
4909 + *
4910 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
4911 + *
4912 + * This file is subject to the terms and conditions of the GNU General Public
4913 + * License. See the file COPYING in the main directory of this archive
4914 + * for more details.
4915 + *
4916 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
4917 + * Dave Stevenson <dsteve@broadcom.com>
4918 + * Simon Mellor <simellor@broadcom.com>
4919 + * Luke Diamand <luked@broadcom.com>
4920 + */
4921 +
4922 +/* common parameters */
4923 +
4924 +/** @name Parameter groups
4925 + * Parameters are divided into groups, and then allocated sequentially within
4926 + * a group using an enum.
4927 + * @{
4928 + */
4929 +
4930 +/** Common parameter ID group, used with many types of component. */
4931 +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
4932 +/** Camera-specific parameter ID group. */
4933 +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
4934 +/** Video-specific parameter ID group. */
4935 +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
4936 +/** Audio-specific parameter ID group. */
4937 +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
4938 +/** Clock-specific parameter ID group. */
4939 +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
4940 +/** Miracast-specific parameter ID group. */
4941 +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
4942 +
4943 +/* Common parameters */
4944 +enum mmal_parameter_common_type {
4945 + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
4946 + = MMAL_PARAMETER_GROUP_COMMON,
4947 + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
4948 + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
4949 +
4950 + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
4951 + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
4952 +
4953 + /** MMAL_PARAMETER_BOOLEAN_T */
4954 + MMAL_PARAMETER_ZERO_COPY,
4955 +
4956 + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
4957 + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
4958 +
4959 + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
4960 + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
4961 + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
4962 + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
4963 + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
4964 + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
4965 + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
4966 + MMAL_PARAMETER_SYSTEM_TIME, /**< MMAL_PARAMETER_UINT64_T */
4967 + MMAL_PARAMETER_NO_IMAGE_PADDING /**< MMAL_PARAMETER_BOOLEAN_T */
4968 +};
4969 +
4970 +/* camera parameters */
4971 +
4972 +enum mmal_parameter_camera_type {
4973 + /* 0 */
4974 + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
4975 + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
4976 + = MMAL_PARAMETER_GROUP_CAMERA,
4977 + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
4978 + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
4979 + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
4980 + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
4981 + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
4982 + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
4983 + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
4984 + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
4985 + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
4986 + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
4987 + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
4988 + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
4989 + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
4990 + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
4991 + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
4992 +
4993 + /* 0x10 */
4994 + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
4995 + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
4996 + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
4997 + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
4998 + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
4999 + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
5000 + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
5001 + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
5002 + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5003 + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
5004 + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
5005 + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
5006 + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
5007 + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5008 + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
5009 + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5010 +
5011 + /* 0x20 */
5012 + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
5013 + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5014 + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5015 + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
5016 + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
5017 + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
5018 + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
5019 + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
5020 + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
5021 + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5022 + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
5023 + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
5024 + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
5025 + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
5026 + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
5027 + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
5028 +
5029 + /* 0x30 */
5030 + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
5031 + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5032 +
5033 + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
5034 + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
5035 +
5036 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5037 + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
5038 +
5039 + /** @ref MMAL_PARAMETER_UINT32_T */
5040 + MMAL_PARAMETER_CAMERA_MIN_ISO,
5041 +
5042 + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
5043 + MMAL_PARAMETER_CAMERA_USE_CASE,
5044 +
5045 + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5046 + MMAL_PARAMETER_CAPTURE_STATS_PASS,
5047 +
5048 + /** @ref MMAL_PARAMETER_UINT32_T */
5049 + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
5050 +
5051 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5052 + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
5053 +
5054 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5055 + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
5056 +
5057 + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
5058 + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
5059 +
5060 + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
5061 + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
5062 + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5063 + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
5064 + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
5065 + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
5066 +
5067 + /* 0x40 */
5068 + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5069 + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5070 + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5071 + MMAL_PARAMETER_SHUTTER_SPEED, /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
5072 + MMAL_PARAMETER_CUSTOM_AWB_GAINS, /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
5073 +};
5074 +
5075 +struct mmal_parameter_rational {
5076 + s32 num; /**< Numerator */
5077 + s32 den; /**< Denominator */
5078 +};
5079 +
5080 +enum mmal_parameter_camera_config_timestamp_mode {
5081 + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
5082 + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
5083 + * for the frame timestamp
5084 + */
5085 + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
5086 + * but subtract the
5087 + * timestamp of the first
5088 + * frame sent to give a
5089 + * zero based timestamp.
5090 + */
5091 +};
5092 +
5093 +struct mmal_parameter_fps_range {
5094 + /**< Low end of the permitted framerate range */
5095 + struct mmal_parameter_rational fps_low;
5096 + /**< High end of the permitted framerate range */
5097 + struct mmal_parameter_rational fps_high;
5098 +};
5099 +
5100 +
5101 +/* camera configuration parameter */
5102 +struct mmal_parameter_camera_config {
5103 + /* Parameters for setting up the image pools */
5104 + u32 max_stills_w; /* Max size of stills capture */
5105 + u32 max_stills_h;
5106 + u32 stills_yuv422; /* Allow YUV422 stills capture */
5107 + u32 one_shot_stills; /* Continuous or one shot stills captures. */
5108 +
5109 + u32 max_preview_video_w; /* Max size of the preview or video
5110 + * capture frames
5111 + */
5112 + u32 max_preview_video_h;
5113 + u32 num_preview_video_frames;
5114 +
5115 + /** Sets the height of the circular buffer for stills capture. */
5116 + u32 stills_capture_circular_buffer_height;
5117 +
5118 + /** Allows preview/encode to resume as fast as possible after the stills
5119 + * input frame has been received, and then processes the still frame in
5120 + * the background whilst preview/encode has resumed.
5121 + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
5122 + */
5123 + u32 fast_preview_resume;
5124 +
5125 + /** Selects algorithm for timestamping frames if
5126 + * there is no clock component connected.
5127 + * enum mmal_parameter_camera_config_timestamp_mode
5128 + */
5129 + s32 use_stc_timestamp;
5130 +};
5131 +
5132 +
5133 +enum mmal_parameter_exposuremode {
5134 + MMAL_PARAM_EXPOSUREMODE_OFF,
5135 + MMAL_PARAM_EXPOSUREMODE_AUTO,
5136 + MMAL_PARAM_EXPOSUREMODE_NIGHT,
5137 + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
5138 + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
5139 + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
5140 + MMAL_PARAM_EXPOSUREMODE_SPORTS,
5141 + MMAL_PARAM_EXPOSUREMODE_SNOW,
5142 + MMAL_PARAM_EXPOSUREMODE_BEACH,
5143 + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
5144 + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
5145 + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
5146 + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
5147 +};
5148 +
5149 +enum mmal_parameter_exposuremeteringmode {
5150 + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
5151 + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
5152 + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
5153 + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
5154 +};
5155 +
5156 +enum mmal_parameter_awbmode {
5157 + MMAL_PARAM_AWBMODE_OFF,
5158 + MMAL_PARAM_AWBMODE_AUTO,
5159 + MMAL_PARAM_AWBMODE_SUNLIGHT,
5160 + MMAL_PARAM_AWBMODE_CLOUDY,
5161 + MMAL_PARAM_AWBMODE_SHADE,
5162 + MMAL_PARAM_AWBMODE_TUNGSTEN,
5163 + MMAL_PARAM_AWBMODE_FLUORESCENT,
5164 + MMAL_PARAM_AWBMODE_INCANDESCENT,
5165 + MMAL_PARAM_AWBMODE_FLASH,
5166 + MMAL_PARAM_AWBMODE_HORIZON,
5167 +};
5168 +
5169 +enum mmal_parameter_imagefx {
5170 + MMAL_PARAM_IMAGEFX_NONE,
5171 + MMAL_PARAM_IMAGEFX_NEGATIVE,
5172 + MMAL_PARAM_IMAGEFX_SOLARIZE,
5173 + MMAL_PARAM_IMAGEFX_POSTERIZE,
5174 + MMAL_PARAM_IMAGEFX_WHITEBOARD,
5175 + MMAL_PARAM_IMAGEFX_BLACKBOARD,
5176 + MMAL_PARAM_IMAGEFX_SKETCH,
5177 + MMAL_PARAM_IMAGEFX_DENOISE,
5178 + MMAL_PARAM_IMAGEFX_EMBOSS,
5179 + MMAL_PARAM_IMAGEFX_OILPAINT,
5180 + MMAL_PARAM_IMAGEFX_HATCH,
5181 + MMAL_PARAM_IMAGEFX_GPEN,
5182 + MMAL_PARAM_IMAGEFX_PASTEL,
5183 + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
5184 + MMAL_PARAM_IMAGEFX_FILM,
5185 + MMAL_PARAM_IMAGEFX_BLUR,
5186 + MMAL_PARAM_IMAGEFX_SATURATION,
5187 + MMAL_PARAM_IMAGEFX_COLOURSWAP,
5188 + MMAL_PARAM_IMAGEFX_WASHEDOUT,
5189 + MMAL_PARAM_IMAGEFX_POSTERISE,
5190 + MMAL_PARAM_IMAGEFX_COLOURPOINT,
5191 + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
5192 + MMAL_PARAM_IMAGEFX_CARTOON,
5193 +};
5194 +
5195 +enum MMAL_PARAM_FLICKERAVOID_T {
5196 + MMAL_PARAM_FLICKERAVOID_OFF,
5197 + MMAL_PARAM_FLICKERAVOID_AUTO,
5198 + MMAL_PARAM_FLICKERAVOID_50HZ,
5199 + MMAL_PARAM_FLICKERAVOID_60HZ,
5200 + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
5201 +};
5202 +
5203 +struct mmal_parameter_awbgains {
5204 + struct mmal_parameter_rational r_gain; /**< Red gain */
5205 + struct mmal_parameter_rational b_gain; /**< Blue gain */
5206 +};
5207 +
5208 +/** Manner of video rate control */
5209 +enum mmal_parameter_rate_control_mode {
5210 + MMAL_VIDEO_RATECONTROL_DEFAULT,
5211 + MMAL_VIDEO_RATECONTROL_VARIABLE,
5212 + MMAL_VIDEO_RATECONTROL_CONSTANT,
5213 + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
5214 + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
5215 +};
5216 +
5217 +enum mmal_video_profile {
5218 + MMAL_VIDEO_PROFILE_H263_BASELINE,
5219 + MMAL_VIDEO_PROFILE_H263_H320CODING,
5220 + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
5221 + MMAL_VIDEO_PROFILE_H263_ISWV2,
5222 + MMAL_VIDEO_PROFILE_H263_ISWV3,
5223 + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
5224 + MMAL_VIDEO_PROFILE_H263_INTERNET,
5225 + MMAL_VIDEO_PROFILE_H263_INTERLACE,
5226 + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
5227 + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
5228 + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
5229 + MMAL_VIDEO_PROFILE_MP4V_CORE,
5230 + MMAL_VIDEO_PROFILE_MP4V_MAIN,
5231 + MMAL_VIDEO_PROFILE_MP4V_NBIT,
5232 + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
5233 + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
5234 + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
5235 + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
5236 + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
5237 + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
5238 + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
5239 + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
5240 + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
5241 + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
5242 + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
5243 + MMAL_VIDEO_PROFILE_H264_BASELINE,
5244 + MMAL_VIDEO_PROFILE_H264_MAIN,
5245 + MMAL_VIDEO_PROFILE_H264_EXTENDED,
5246 + MMAL_VIDEO_PROFILE_H264_HIGH,
5247 + MMAL_VIDEO_PROFILE_H264_HIGH10,
5248 + MMAL_VIDEO_PROFILE_H264_HIGH422,
5249 + MMAL_VIDEO_PROFILE_H264_HIGH444,
5250 + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
5251 + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
5252 +};
5253 +
5254 +enum mmal_video_level {
5255 + MMAL_VIDEO_LEVEL_H263_10,
5256 + MMAL_VIDEO_LEVEL_H263_20,
5257 + MMAL_VIDEO_LEVEL_H263_30,
5258 + MMAL_VIDEO_LEVEL_H263_40,
5259 + MMAL_VIDEO_LEVEL_H263_45,
5260 + MMAL_VIDEO_LEVEL_H263_50,
5261 + MMAL_VIDEO_LEVEL_H263_60,
5262 + MMAL_VIDEO_LEVEL_H263_70,
5263 + MMAL_VIDEO_LEVEL_MP4V_0,
5264 + MMAL_VIDEO_LEVEL_MP4V_0b,
5265 + MMAL_VIDEO_LEVEL_MP4V_1,
5266 + MMAL_VIDEO_LEVEL_MP4V_2,
5267 + MMAL_VIDEO_LEVEL_MP4V_3,
5268 + MMAL_VIDEO_LEVEL_MP4V_4,
5269 + MMAL_VIDEO_LEVEL_MP4V_4a,
5270 + MMAL_VIDEO_LEVEL_MP4V_5,
5271 + MMAL_VIDEO_LEVEL_MP4V_6,
5272 + MMAL_VIDEO_LEVEL_H264_1,
5273 + MMAL_VIDEO_LEVEL_H264_1b,
5274 + MMAL_VIDEO_LEVEL_H264_11,
5275 + MMAL_VIDEO_LEVEL_H264_12,
5276 + MMAL_VIDEO_LEVEL_H264_13,
5277 + MMAL_VIDEO_LEVEL_H264_2,
5278 + MMAL_VIDEO_LEVEL_H264_21,
5279 + MMAL_VIDEO_LEVEL_H264_22,
5280 + MMAL_VIDEO_LEVEL_H264_3,
5281 + MMAL_VIDEO_LEVEL_H264_31,
5282 + MMAL_VIDEO_LEVEL_H264_32,
5283 + MMAL_VIDEO_LEVEL_H264_4,
5284 + MMAL_VIDEO_LEVEL_H264_41,
5285 + MMAL_VIDEO_LEVEL_H264_42,
5286 + MMAL_VIDEO_LEVEL_H264_5,
5287 + MMAL_VIDEO_LEVEL_H264_51,
5288 + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
5289 +};
5290 +
5291 +struct mmal_parameter_video_profile {
5292 + enum mmal_video_profile profile;
5293 + enum mmal_video_level level;
5294 +};
5295 +
5296 +/* video parameters */
5297 +
5298 +enum mmal_parameter_video_type {
5299 + /** @ref MMAL_DISPLAYREGION_T */
5300 + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
5301 +
5302 + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
5303 + MMAL_PARAMETER_SUPPORTED_PROFILES,
5304 +
5305 + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
5306 + MMAL_PARAMETER_PROFILE,
5307 +
5308 + /** @ref MMAL_PARAMETER_UINT32_T */
5309 + MMAL_PARAMETER_INTRAPERIOD,
5310 +
5311 + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
5312 + MMAL_PARAMETER_RATECONTROL,
5313 +
5314 + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
5315 + MMAL_PARAMETER_NALUNITFORMAT,
5316 +
5317 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5318 + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
5319 +
5320 + /** @ref MMAL_PARAMETER_UINT32_T.
5321 + * Setting the value to zero resets to the default (one slice per frame).
5322 + */
5323 + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
5324 +
5325 + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
5326 + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
5327 +
5328 + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
5329 + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
5330 +
5331 + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
5332 + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
5333 +
5334 + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
5335 + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
5336 + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
5337 + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
5338 +
5339 + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
5340 + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
5341 +
5342 + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
5343 + MMAL_PARAMETER_VIDEO_BIT_RATE,
5344 +
5345 + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
5346 + MMAL_PARAMETER_VIDEO_FRAME_RATE,
5347 +
5348 + /** @ref MMAL_PARAMETER_UINT32_T. */
5349 + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
5350 +
5351 + /** @ref MMAL_PARAMETER_UINT32_T. */
5352 + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
5353 +
5354 + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
5355 + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
5356 +
5357 + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
5358 + /** @ref MMAL_PARAMETER_UINT32_T.
5359 + * Changing this parameter from the default can reduce frame rate
5360 + * because image buffers need to be re-pitched.
5361 + */
5362 + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
5363 +
5364 + /** @ref MMAL_PARAMETER_UINT32_T.
5365 + * Changing this parameter from the default can reduce frame rate
5366 + * because image buffers need to be re-pitched.
5367 + */
5368 + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
5369 +
5370 + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
5371 + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
5372 +
5373 + /** @ref MMAL_PARAMETER_UINT32_T. */
5374 + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
5375 +
5376 + /**< @ref MMAL_PARAMETER_UINT32_T. */
5377 + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
5378 +
5379 + /**< @ref MMAL_PARAMETER_UINT32_T. */
5380 + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
5381 +
5382 + /** @ref MMAL_PARAMETER_UINT32_T */
5383 + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
5384 +
5385 + /** @ref MMAL_PARAMETER_UINT32_T. */
5386 + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
5387 +
5388 + /* H264 specific parameters */
5389 +
5390 + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
5391 + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
5392 +
5393 + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
5394 + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
5395 +
5396 + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
5397 + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
5398 +
5399 + /** @ref MMAL_PARAMETER_UINT32_T. */
5400 + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
5401 +
5402 + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
5403 + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
5404 +
5405 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5406 + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
5407 +
5408 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5409 + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
5410 +
5411 + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
5412 + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
5413 +
5414 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5415 + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
5416 +
5417 + /** @ref MMAL_PARAMETER_BOOLEAN_T */
5418 + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
5419 +
5420 + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
5421 + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
5422 +
5423 + /** @ref MMAL_PARAMETER_BYTES_T */
5424 + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
5425 +
5426 + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5427 + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
5428 +
5429 + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5430 + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
5431 +
5432 + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
5433 + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
5434 +};
5435 +
5436 +/** Valid mirror modes */
5437 +enum mmal_parameter_mirror {
5438 + MMAL_PARAM_MIRROR_NONE,
5439 + MMAL_PARAM_MIRROR_VERTICAL,
5440 + MMAL_PARAM_MIRROR_HORIZONTAL,
5441 + MMAL_PARAM_MIRROR_BOTH,
5442 +};
5443 +
5444 +enum mmal_parameter_displaytransform {
5445 + MMAL_DISPLAY_ROT0 = 0,
5446 + MMAL_DISPLAY_MIRROR_ROT0 = 1,
5447 + MMAL_DISPLAY_MIRROR_ROT180 = 2,
5448 + MMAL_DISPLAY_ROT180 = 3,
5449 + MMAL_DISPLAY_MIRROR_ROT90 = 4,
5450 + MMAL_DISPLAY_ROT270 = 5,
5451 + MMAL_DISPLAY_ROT90 = 6,
5452 + MMAL_DISPLAY_MIRROR_ROT270 = 7,
5453 +};
5454 +
5455 +enum mmal_parameter_displaymode {
5456 + MMAL_DISPLAY_MODE_FILL = 0,
5457 + MMAL_DISPLAY_MODE_LETTERBOX = 1,
5458 +};
5459 +
5460 +enum mmal_parameter_displayset {
5461 + MMAL_DISPLAY_SET_NONE = 0,
5462 + MMAL_DISPLAY_SET_NUM = 1,
5463 + MMAL_DISPLAY_SET_FULLSCREEN = 2,
5464 + MMAL_DISPLAY_SET_TRANSFORM = 4,
5465 + MMAL_DISPLAY_SET_DEST_RECT = 8,
5466 + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
5467 + MMAL_DISPLAY_SET_MODE = 0x20,
5468 + MMAL_DISPLAY_SET_PIXEL = 0x40,
5469 + MMAL_DISPLAY_SET_NOASPECT = 0x80,
5470 + MMAL_DISPLAY_SET_LAYER = 0x100,
5471 + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
5472 + MMAL_DISPLAY_SET_ALPHA = 0x400,
5473 +};
5474 +
5475 +struct mmal_parameter_displayregion {
5476 + /** Bitfield that indicates which fields are set and should be
5477 + * used. All other fields will maintain their current value.
5478 + * \ref MMAL_DISPLAYSET_T defines the bits that can be
5479 + * combined.
5480 + */
5481 + u32 set;
5482 +
5483 + /** Describes the display output device, with 0 typically
5484 + * being a directly connected LCD display. The actual values
5485 + * will depend on the hardware. Code using hard-wired numbers
5486 + * (e.g. 2) is certain to fail.
5487 + */
5488 +
5489 + u32 display_num;
5490 + /** Indicates that we are using the full device screen area,
5491 + * rather than a window of the display. If zero, then
5492 + * dest_rect is used to specify a region of the display to
5493 + * use.
5494 + */
5495 +
5496 + s32 fullscreen;
5497 + /** Indicates any rotation or flipping used to map frames onto
5498 + * the natural display orientation.
5499 + */
5500 + u32 transform; /* enum mmal_parameter_displaytransform */
5501 +
5502 + /** Where to display the frame within the screen, if
5503 + * fullscreen is zero.
5504 + */
5505 + struct vchiq_mmal_rect dest_rect;
5506 +
5507 + /** Indicates which area of the frame to display. If all
5508 + * values are zero, the whole frame will be used.
5509 + */
5510 + struct vchiq_mmal_rect src_rect;
5511 +
5512 + /** If set to non-zero, indicates that any display scaling
5513 + * should disregard the aspect ratio of the frame region being
5514 + * displayed.
5515 + */
5516 + s32 noaspect;
5517 +
5518 + /** Indicates how the image should be scaled to fit the
5519 + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
5520 + * that the image should fill the screen by potentially
5521 + * cropping the frames. Setting \code mode \endcode to \code
5522 + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
5523 + * source region should be displayed and black bars added if
5524 + * necessary.
5525 + */
5526 + u32 mode; /* enum mmal_parameter_displaymode */
5527 +
5528 + /** If non-zero, defines the width of a source pixel relative
5529 + * to \code pixel_y \endcode. If zero, then pixels default to
5530 + * being square.
5531 + */
5532 + u32 pixel_x;
5533 +
5534 + /** If non-zero, defines the height of a source pixel relative
5535 + * to \code pixel_x \endcode. If zero, then pixels default to
5536 + * being square.
5537 + */
5538 + u32 pixel_y;
5539 +
5540 + /** Sets the relative depth of the images, with greater values
5541 + * being in front of smaller values.
5542 + */
5543 + u32 layer;
5544 +
5545 + /** Set to non-zero to ensure copy protection is used on
5546 + * output.
5547 + */
5548 + s32 copyprotect_required;
5549 +
5550 + /** Level of opacity of the layer, where zero is fully
5551 + * transparent and 255 is fully opaque.
5552 + */
5553 + u32 alpha;
5554 +};
5555 +
5556 +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
5557 +
5558 +struct mmal_parameter_imagefx_parameters {
5559 + enum mmal_parameter_imagefx effect;
5560 + u32 num_effect_params;
5561 + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
5562 +};
5563 +
5564 +#define MMAL_PARAMETER_CAMERA_INFO_MAX_CAMERAS 4
5565 +#define MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES 2
5566 +#define MMAL_PARAMETER_CAMERA_INFO_MAX_STR_LEN 16
5567 +
5568 +struct mmal_parameter_camera_info_camera_t {
5569 + u32 port_id;
5570 + u32 max_width;
5571 + u32 max_height;
5572 + u32 lens_present;
5573 + u8 camera_name[MMAL_PARAMETER_CAMERA_INFO_MAX_STR_LEN];
5574 +};
5575 +
5576 +enum mmal_parameter_camera_info_flash_type_t {
5577 + /* Make values explicit to ensure they match values in config ini */
5578 + MMAL_PARAMETER_CAMERA_INFO_FLASH_TYPE_XENON = 0,
5579 + MMAL_PARAMETER_CAMERA_INFO_FLASH_TYPE_LED = 1,
5580 + MMAL_PARAMETER_CAMERA_INFO_FLASH_TYPE_OTHER = 2,
5581 + MMAL_PARAMETER_CAMERA_INFO_FLASH_TYPE_MAX = 0x7FFFFFFF
5582 +};
5583 +
5584 +struct mmal_parameter_camera_info_flash_t {
5585 + enum mmal_parameter_camera_info_flash_type_t flash_type;
5586 +};
5587 +
5588 +struct mmal_parameter_camera_info_t {
5589 + u32 num_cameras;
5590 + u32 num_flashes;
5591 + struct mmal_parameter_camera_info_camera_t
5592 + cameras[MMAL_PARAMETER_CAMERA_INFO_MAX_CAMERAS];
5593 + struct mmal_parameter_camera_info_flash_t
5594 + flashes[MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES];
5595 +};
5596 --- /dev/null
5597 +++ b/drivers/media/platform/bcm2835/mmal-vchiq.c
5598 @@ -0,0 +1,1916 @@
5599 +/*
5600 + * Broadcom BM2835 V4L2 driver
5601 + *
5602 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
5603 + *
5604 + * This file is subject to the terms and conditions of the GNU General Public
5605 + * License. See the file COPYING in the main directory of this archive
5606 + * for more details.
5607 + *
5608 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
5609 + * Dave Stevenson <dsteve@broadcom.com>
5610 + * Simon Mellor <simellor@broadcom.com>
5611 + * Luke Diamand <luked@broadcom.com>
5612 + *
5613 + * V4L2 driver MMAL vchiq interface code
5614 + */
5615 +
5616 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5617 +
5618 +#include <linux/errno.h>
5619 +#include <linux/kernel.h>
5620 +#include <linux/mutex.h>
5621 +#include <linux/mm.h>
5622 +#include <linux/slab.h>
5623 +#include <linux/completion.h>
5624 +#include <linux/vmalloc.h>
5625 +#include <asm/cacheflush.h>
5626 +#include <media/videobuf2-vmalloc.h>
5627 +
5628 +#include "mmal-common.h"
5629 +#include "mmal-vchiq.h"
5630 +#include "mmal-msg.h"
5631 +
5632 +#define USE_VCHIQ_ARM
5633 +#include "interface/vchi/vchi.h"
5634 +
5635 +/* maximum number of components supported */
5636 +#define VCHIQ_MMAL_MAX_COMPONENTS 4
5637 +
5638 +/*#define FULL_MSG_DUMP 1*/
5639 +
5640 +#ifdef DEBUG
5641 +static const char *const msg_type_names[] = {
5642 + "UNKNOWN",
5643 + "QUIT",
5644 + "SERVICE_CLOSED",
5645 + "GET_VERSION",
5646 + "COMPONENT_CREATE",
5647 + "COMPONENT_DESTROY",
5648 + "COMPONENT_ENABLE",
5649 + "COMPONENT_DISABLE",
5650 + "PORT_INFO_GET",
5651 + "PORT_INFO_SET",
5652 + "PORT_ACTION",
5653 + "BUFFER_FROM_HOST",
5654 + "BUFFER_TO_HOST",
5655 + "GET_STATS",
5656 + "PORT_PARAMETER_SET",
5657 + "PORT_PARAMETER_GET",
5658 + "EVENT_TO_HOST",
5659 + "GET_CORE_STATS_FOR_PORT",
5660 + "OPAQUE_ALLOCATOR",
5661 + "CONSUME_MEM",
5662 + "LMK",
5663 + "OPAQUE_ALLOCATOR_DESC",
5664 + "DRM_GET_LHS32",
5665 + "DRM_GET_TIME",
5666 + "BUFFER_FROM_HOST_ZEROLEN",
5667 + "PORT_FLUSH",
5668 + "HOST_LOG",
5669 +};
5670 +#endif
5671 +
5672 +static const char *const port_action_type_names[] = {
5673 + "UNKNOWN",
5674 + "ENABLE",
5675 + "DISABLE",
5676 + "FLUSH",
5677 + "CONNECT",
5678 + "DISCONNECT",
5679 + "SET_REQUIREMENTS",
5680 +};
5681 +
5682 +#if defined(DEBUG)
5683 +#if defined(FULL_MSG_DUMP)
5684 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
5685 + do { \
5686 + pr_debug(TITLE" type:%s(%d) length:%d\n", \
5687 + msg_type_names[(MSG)->h.type], \
5688 + (MSG)->h.type, (MSG_LEN)); \
5689 + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
5690 + 16, 4, (MSG), \
5691 + sizeof(struct mmal_msg_header), 1); \
5692 + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
5693 + 16, 4, \
5694 + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
5695 + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
5696 + } while (0)
5697 +#else
5698 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
5699 + { \
5700 + pr_debug(TITLE" type:%s(%d) length:%d\n", \
5701 + msg_type_names[(MSG)->h.type], \
5702 + (MSG)->h.type, (MSG_LEN)); \
5703 + }
5704 +#endif
5705 +#else
5706 +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
5707 +#endif
5708 +
5709 +/* normal message context */
5710 +struct mmal_msg_context {
5711 + union {
5712 + struct {
5713 + /* work struct for defered callback - must come first */
5714 + struct work_struct work;
5715 + /* mmal instance */
5716 + struct vchiq_mmal_instance *instance;
5717 + /* mmal port */
5718 + struct vchiq_mmal_port *port;
5719 + /* actual buffer used to store bulk reply */
5720 + struct mmal_buffer *buffer;
5721 + /* amount of buffer used */
5722 + unsigned long buffer_used;
5723 + /* MMAL buffer flags */
5724 + u32 mmal_flags;
5725 + /* Presentation and Decode timestamps */
5726 + s64 pts;
5727 + s64 dts;
5728 +
5729 + int status; /* context status */
5730 +
5731 + } bulk; /* bulk data */
5732 +
5733 + struct {
5734 + /* message handle to release */
5735 + VCHI_HELD_MSG_T msg_handle;
5736 + /* pointer to received message */
5737 + struct mmal_msg *msg;
5738 + /* received message length */
5739 + u32 msg_len;
5740 + /* completion upon reply */
5741 + struct completion cmplt;
5742 + } sync; /* synchronous response */
5743 + } u;
5744 +
5745 +};
5746 +
5747 +struct vchiq_mmal_instance {
5748 + VCHI_SERVICE_HANDLE_T handle;
5749 +
5750 + /* ensure serialised access to service */
5751 + struct mutex vchiq_mutex;
5752 +
5753 + /* ensure serialised access to bulk operations */
5754 + struct mutex bulk_mutex;
5755 +
5756 + /* vmalloc page to receive scratch bulk xfers into */
5757 + void *bulk_scratch;
5758 +
5759 + /* component to use next */
5760 + int component_idx;
5761 + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
5762 +};
5763 +
5764 +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
5765 + *instance)
5766 +{
5767 + struct mmal_msg_context *msg_context;
5768 +
5769 + /* todo: should this be allocated from a pool to avoid kmalloc */
5770 + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
5771 + memset(msg_context, 0, sizeof(*msg_context));
5772 +
5773 + return msg_context;
5774 +}
5775 +
5776 +static void release_msg_context(struct mmal_msg_context *msg_context)
5777 +{
5778 + kfree(msg_context);
5779 +}
5780 +
5781 +/* deals with receipt of event to host message */
5782 +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
5783 + struct mmal_msg *msg, u32 msg_len)
5784 +{
5785 + pr_debug("unhandled event\n");
5786 + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
5787 + msg->u.event_to_host.client_component,
5788 + msg->u.event_to_host.port_type,
5789 + msg->u.event_to_host.port_num,
5790 + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
5791 +}
5792 +
5793 +/* workqueue scheduled callback
5794 + *
5795 + * we do this because it is important we do not call any other vchiq
5796 + * sync calls from witin the message delivery thread
5797 + */
5798 +static void buffer_work_cb(struct work_struct *work)
5799 +{
5800 + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
5801 +
5802 + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
5803 + msg_context->u.bulk.port,
5804 + msg_context->u.bulk.status,
5805 + msg_context->u.bulk.buffer,
5806 + msg_context->u.bulk.buffer_used,
5807 + msg_context->u.bulk.mmal_flags,
5808 + msg_context->u.bulk.dts,
5809 + msg_context->u.bulk.pts);
5810 +
5811 + /* release message context */
5812 + release_msg_context(msg_context);
5813 +}
5814 +
5815 +/* enqueue a bulk receive for a given message context */
5816 +static int bulk_receive(struct vchiq_mmal_instance *instance,
5817 + struct mmal_msg *msg,
5818 + struct mmal_msg_context *msg_context)
5819 +{
5820 + unsigned long rd_len;
5821 + unsigned long flags = 0;
5822 + int ret;
5823 +
5824 + /* bulk mutex stops other bulk operations while we have a
5825 + * receive in progress - released in callback
5826 + */
5827 + ret = mutex_lock_interruptible(&instance->bulk_mutex);
5828 + if (ret != 0)
5829 + return ret;
5830 +
5831 + rd_len = msg->u.buffer_from_host.buffer_header.length;
5832 +
5833 + /* take buffer from queue */
5834 + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
5835 + if (list_empty(&msg_context->u.bulk.port->buffers)) {
5836 + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
5837 + pr_err("buffer list empty trying to submit bulk receive\n");
5838 +
5839 + /* todo: this is a serious error, we should never have
5840 + * commited a buffer_to_host operation to the mmal
5841 + * port without the buffer to back it up (underflow
5842 + * handling) and there is no obvious way to deal with
5843 + * this - how is the mmal servie going to react when
5844 + * we fail to do the xfer and reschedule a buffer when
5845 + * it arrives? perhaps a starved flag to indicate a
5846 + * waiting bulk receive?
5847 + */
5848 +
5849 + mutex_unlock(&instance->bulk_mutex);
5850 +
5851 + return -EINVAL;
5852 + }
5853 +
5854 + msg_context->u.bulk.buffer =
5855 + list_entry(msg_context->u.bulk.port->buffers.next,
5856 + struct mmal_buffer, list);
5857 + list_del(&msg_context->u.bulk.buffer->list);
5858 +
5859 + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
5860 +
5861 + /* ensure we do not overrun the available buffer */
5862 + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
5863 + rd_len = msg_context->u.bulk.buffer->buffer_size;
5864 + pr_warn("short read as not enough receive buffer space\n");
5865 + /* todo: is this the correct response, what happens to
5866 + * the rest of the message data?
5867 + */
5868 + }
5869 +
5870 + /* store length */
5871 + msg_context->u.bulk.buffer_used = rd_len;
5872 + msg_context->u.bulk.mmal_flags =
5873 + msg->u.buffer_from_host.buffer_header.flags;
5874 + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
5875 + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
5876 +
5877 + // only need to flush L1 cache here, as VCHIQ takes care of the L2
5878 + // cache.
5879 + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
5880 +
5881 + /* queue the bulk submission */
5882 + vchi_service_use(instance->handle);
5883 + ret = vchi_bulk_queue_receive(instance->handle,
5884 + msg_context->u.bulk.buffer->buffer,
5885 + /* Actual receive needs to be a multiple
5886 + * of 4 bytes
5887 + */
5888 + (rd_len + 3) & ~3,
5889 + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
5890 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
5891 + msg_context);
5892 +
5893 + vchi_service_release(instance->handle);
5894 +
5895 + if (ret != 0) {
5896 + /* callback will not be clearing the mutex */
5897 + mutex_unlock(&instance->bulk_mutex);
5898 + }
5899 +
5900 + return ret;
5901 +}
5902 +
5903 +/* enque a dummy bulk receive for a given message context */
5904 +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
5905 + struct mmal_msg_context *msg_context)
5906 +{
5907 + int ret;
5908 +
5909 + /* bulk mutex stops other bulk operations while we have a
5910 + * receive in progress - released in callback
5911 + */
5912 + ret = mutex_lock_interruptible(&instance->bulk_mutex);
5913 + if (ret != 0)
5914 + return ret;
5915 +
5916 + /* zero length indicates this was a dummy transfer */
5917 + msg_context->u.bulk.buffer_used = 0;
5918 +
5919 + /* queue the bulk submission */
5920 + vchi_service_use(instance->handle);
5921 +
5922 + ret = vchi_bulk_queue_receive(instance->handle,
5923 + instance->bulk_scratch,
5924 + 8,
5925 + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
5926 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
5927 + msg_context);
5928 +
5929 + vchi_service_release(instance->handle);
5930 +
5931 + if (ret != 0) {
5932 + /* callback will not be clearing the mutex */
5933 + mutex_unlock(&instance->bulk_mutex);
5934 + }
5935 +
5936 + return ret;
5937 +}
5938 +
5939 +/* data in message, memcpy from packet into output buffer */
5940 +static int inline_receive(struct vchiq_mmal_instance *instance,
5941 + struct mmal_msg *msg,
5942 + struct mmal_msg_context *msg_context)
5943 +{
5944 + unsigned long flags = 0;
5945 +
5946 + /* take buffer from queue */
5947 + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
5948 + if (list_empty(&msg_context->u.bulk.port->buffers)) {
5949 + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
5950 + pr_err("buffer list empty trying to receive inline\n");
5951 +
5952 + /* todo: this is a serious error, we should never have
5953 + * commited a buffer_to_host operation to the mmal
5954 + * port without the buffer to back it up (with
5955 + * underflow handling) and there is no obvious way to
5956 + * deal with this. Less bad than the bulk case as we
5957 + * can just drop this on the floor but...unhelpful
5958 + */
5959 + return -EINVAL;
5960 + }
5961 +
5962 + msg_context->u.bulk.buffer =
5963 + list_entry(msg_context->u.bulk.port->buffers.next,
5964 + struct mmal_buffer, list);
5965 + list_del(&msg_context->u.bulk.buffer->list);
5966 +
5967 + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
5968 +
5969 + memcpy(msg_context->u.bulk.buffer->buffer,
5970 + msg->u.buffer_from_host.short_data,
5971 + msg->u.buffer_from_host.payload_in_message);
5972 +
5973 + msg_context->u.bulk.buffer_used =
5974 + msg->u.buffer_from_host.payload_in_message;
5975 +
5976 + return 0;
5977 +}
5978 +
5979 +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
5980 +static int
5981 +buffer_from_host(struct vchiq_mmal_instance *instance,
5982 + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
5983 +{
5984 + struct mmal_msg_context *msg_context;
5985 + struct mmal_msg m;
5986 + int ret;
5987 +
5988 + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
5989 +
5990 + /* bulk mutex stops other bulk operations while we
5991 + * have a receive in progress
5992 + */
5993 + if (mutex_lock_interruptible(&instance->bulk_mutex))
5994 + return -EINTR;
5995 +
5996 + /* get context */
5997 + msg_context = get_msg_context(instance);
5998 + if (msg_context == NULL)
5999 + return -ENOMEM;
6000 +
6001 + /* store bulk message context for when data arrives */
6002 + msg_context->u.bulk.instance = instance;
6003 + msg_context->u.bulk.port = port;
6004 + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
6005 + msg_context->u.bulk.buffer_used = 0;
6006 +
6007 + /* initialise work structure ready to schedule callback */
6008 + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
6009 +
6010 + /* prep the buffer from host message */
6011 + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
6012 +
6013 + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
6014 + m.h.magic = MMAL_MAGIC;
6015 + m.h.context = msg_context;
6016 + m.h.status = 0;
6017 +
6018 + /* drvbuf is our private data passed back */
6019 + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
6020 + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
6021 + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
6022 + m.u.buffer_from_host.drvbuf.client_context = msg_context;
6023 +
6024 + /* buffer header */
6025 + m.u.buffer_from_host.buffer_header.cmd = 0;
6026 + m.u.buffer_from_host.buffer_header.data = buf->buffer;
6027 + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
6028 + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
6029 + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
6030 + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
6031 + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
6032 + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
6033 +
6034 + /* clear buffer type sepecific data */
6035 + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
6036 + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
6037 +
6038 + /* no payload in message */
6039 + m.u.buffer_from_host.payload_in_message = 0;
6040 +
6041 + vchi_service_use(instance->handle);
6042 +
6043 + ret = vchi_msg_queue(instance->handle, &m,
6044 + sizeof(struct mmal_msg_header) +
6045 + sizeof(m.u.buffer_from_host),
6046 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
6047 +
6048 + if (ret != 0) {
6049 + release_msg_context(msg_context);
6050 + /* todo: is this correct error value? */
6051 + }
6052 +
6053 + vchi_service_release(instance->handle);
6054 +
6055 + mutex_unlock(&instance->bulk_mutex);
6056 +
6057 + return ret;
6058 +}
6059 +
6060 +/* submit a buffer to the mmal sevice
6061 + *
6062 + * the buffer_from_host uses size data from the ports next available
6063 + * mmal_buffer and deals with there being no buffer available by
6064 + * incrementing the underflow for later
6065 + */
6066 +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
6067 + struct vchiq_mmal_port *port)
6068 +{
6069 + int ret;
6070 + struct mmal_buffer *buf;
6071 + unsigned long flags = 0;
6072 +
6073 + if (!port->enabled)
6074 + return -EINVAL;
6075 +
6076 + /* peek buffer from queue */
6077 + spin_lock_irqsave(&port->slock, flags);
6078 + if (list_empty(&port->buffers)) {
6079 + port->buffer_underflow++;
6080 + spin_unlock_irqrestore(&port->slock, flags);
6081 + return -ENOSPC;
6082 + }
6083 +
6084 + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
6085 +
6086 + spin_unlock_irqrestore(&port->slock, flags);
6087 +
6088 + /* issue buffer to mmal service */
6089 + ret = buffer_from_host(instance, port, buf);
6090 + if (ret) {
6091 + pr_err("adding buffer header failed\n");
6092 + /* todo: how should this be dealt with */
6093 + }
6094 +
6095 + return ret;
6096 +}
6097 +
6098 +/* deals with receipt of buffer to host message */
6099 +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
6100 + struct mmal_msg *msg, u32 msg_len)
6101 +{
6102 + struct mmal_msg_context *msg_context;
6103 +
6104 + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
6105 + instance, msg, msg_len);
6106 +
6107 + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
6108 + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
6109 + } else {
6110 + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
6111 + return;
6112 + }
6113 +
6114 + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
6115 + /* message reception had an error */
6116 + pr_warn("error %d in reply\n", msg->h.status);
6117 +
6118 + msg_context->u.bulk.status = msg->h.status;
6119 +
6120 + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
6121 + /* empty buffer */
6122 + if (msg->u.buffer_from_host.buffer_header.flags &
6123 + MMAL_BUFFER_HEADER_FLAG_EOS) {
6124 + msg_context->u.bulk.status =
6125 + dummy_bulk_receive(instance, msg_context);
6126 + if (msg_context->u.bulk.status == 0)
6127 + return; /* successful bulk submission, bulk
6128 + * completion will trigger callback
6129 + */
6130 + } else {
6131 + /* do callback with empty buffer - not EOS though */
6132 + msg_context->u.bulk.status = 0;
6133 + msg_context->u.bulk.buffer_used = 0;
6134 + }
6135 + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
6136 + /* data is not in message, queue a bulk receive */
6137 + msg_context->u.bulk.status =
6138 + bulk_receive(instance, msg, msg_context);
6139 + if (msg_context->u.bulk.status == 0)
6140 + return; /* successful bulk submission, bulk
6141 + * completion will trigger callback
6142 + */
6143 +
6144 + /* failed to submit buffer, this will end badly */
6145 + pr_err("error %d on bulk submission\n",
6146 + msg_context->u.bulk.status);
6147 +
6148 + } else if (msg->u.buffer_from_host.payload_in_message <=
6149 + MMAL_VC_SHORT_DATA) {
6150 + /* data payload within message */
6151 + msg_context->u.bulk.status = inline_receive(instance, msg,
6152 + msg_context);
6153 + } else {
6154 + pr_err("message with invalid short payload\n");
6155 +
6156 + /* signal error */
6157 + msg_context->u.bulk.status = -EINVAL;
6158 + msg_context->u.bulk.buffer_used =
6159 + msg->u.buffer_from_host.payload_in_message;
6160 + }
6161 +
6162 + /* replace the buffer header */
6163 + port_buffer_from_host(instance, msg_context->u.bulk.port);
6164 +
6165 + /* schedule the port callback */
6166 + schedule_work(&msg_context->u.bulk.work);
6167 +}
6168 +
6169 +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
6170 + struct mmal_msg_context *msg_context)
6171 +{
6172 + /* bulk receive operation complete */
6173 + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
6174 +
6175 + /* replace the buffer header */
6176 + port_buffer_from_host(msg_context->u.bulk.instance,
6177 + msg_context->u.bulk.port);
6178 +
6179 + msg_context->u.bulk.status = 0;
6180 +
6181 + /* schedule the port callback */
6182 + schedule_work(&msg_context->u.bulk.work);
6183 +}
6184 +
6185 +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
6186 + struct mmal_msg_context *msg_context)
6187 +{
6188 + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
6189 +
6190 + /* bulk receive operation complete */
6191 + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
6192 +
6193 + /* replace the buffer header */
6194 + port_buffer_from_host(msg_context->u.bulk.instance,
6195 + msg_context->u.bulk.port);
6196 +
6197 + msg_context->u.bulk.status = -EINTR;
6198 +
6199 + schedule_work(&msg_context->u.bulk.work);
6200 +}
6201 +
6202 +/* incoming event service callback */
6203 +static void service_callback(void *param,
6204 + const VCHI_CALLBACK_REASON_T reason,
6205 + void *bulk_ctx)
6206 +{
6207 + struct vchiq_mmal_instance *instance = param;
6208 + int status;
6209 + u32 msg_len;
6210 + struct mmal_msg *msg;
6211 + VCHI_HELD_MSG_T msg_handle;
6212 +
6213 + if (!instance) {
6214 + pr_err("Message callback passed NULL instance\n");
6215 + return;
6216 + }
6217 +
6218 + switch (reason) {
6219 + case VCHI_CALLBACK_MSG_AVAILABLE:
6220 + status = vchi_msg_hold(instance->handle, (void **)&msg,
6221 + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
6222 + if (status) {
6223 + pr_err("Unable to dequeue a message (%d)\n", status);
6224 + break;
6225 + }
6226 +
6227 + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
6228 +
6229 + /* handling is different for buffer messages */
6230 + switch (msg->h.type) {
6231 +
6232 + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
6233 + vchi_held_msg_release(&msg_handle);
6234 + break;
6235 +
6236 + case MMAL_MSG_TYPE_EVENT_TO_HOST:
6237 + event_to_host_cb(instance, msg, msg_len);
6238 + vchi_held_msg_release(&msg_handle);
6239 +
6240 + break;
6241 +
6242 + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
6243 + buffer_to_host_cb(instance, msg, msg_len);
6244 + vchi_held_msg_release(&msg_handle);
6245 + break;
6246 +
6247 + default:
6248 + /* messages dependant on header context to complete */
6249 +
6250 + /* todo: the msg.context really ought to be sanity
6251 + * checked before we just use it, afaict it comes back
6252 + * and is used raw from the videocore. Perhaps it
6253 + * should be verified the address lies in the kernel
6254 + * address space.
6255 + */
6256 + if (msg->h.context == NULL) {
6257 + pr_err("received message context was null!\n");
6258 + vchi_held_msg_release(&msg_handle);
6259 + break;
6260 + }
6261 +
6262 + /* fill in context values */
6263 + msg->h.context->u.sync.msg_handle = msg_handle;
6264 + msg->h.context->u.sync.msg = msg;
6265 + msg->h.context->u.sync.msg_len = msg_len;
6266 +
6267 + /* todo: should this check (completion_done()
6268 + * == 1) for no one waiting? or do we need a
6269 + * flag to tell us the completion has been
6270 + * interrupted so we can free the message and
6271 + * its context. This probably also solves the
6272 + * message arriving after interruption todo
6273 + * below
6274 + */
6275 +
6276 + /* complete message so caller knows it happened */
6277 + complete(&msg->h.context->u.sync.cmplt);
6278 + break;
6279 + }
6280 +
6281 + break;
6282 +
6283 + case VCHI_CALLBACK_BULK_RECEIVED:
6284 + bulk_receive_cb(instance, bulk_ctx);
6285 + break;
6286 +
6287 + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
6288 + bulk_abort_cb(instance, bulk_ctx);
6289 + break;
6290 +
6291 + case VCHI_CALLBACK_SERVICE_CLOSED:
6292 + /* TODO: consider if this requires action if received when
6293 + * driver is not explicitly closing the service
6294 + */
6295 + break;
6296 +
6297 + default:
6298 + pr_err("Received unhandled message reason %d\n", reason);
6299 + break;
6300 + }
6301 +}
6302 +
6303 +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
6304 + struct mmal_msg *msg,
6305 + unsigned int payload_len,
6306 + struct mmal_msg **msg_out,
6307 + VCHI_HELD_MSG_T *msg_handle_out)
6308 +{
6309 + struct mmal_msg_context msg_context;
6310 + int ret;
6311 +
6312 + /* payload size must not cause message to exceed max size */
6313 + if (payload_len >
6314 + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
6315 + pr_err("payload length %d exceeds max:%d\n", payload_len,
6316 + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
6317 + return -EINVAL;
6318 + }
6319 +
6320 + init_completion(&msg_context.u.sync.cmplt);
6321 +
6322 + msg->h.magic = MMAL_MAGIC;
6323 + msg->h.context = &msg_context;
6324 + msg->h.status = 0;
6325 +
6326 + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
6327 + ">>> sync message");
6328 +
6329 + vchi_service_use(instance->handle);
6330 +
6331 + ret = vchi_msg_queue(instance->handle,
6332 + msg,
6333 + sizeof(struct mmal_msg_header) + payload_len,
6334 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
6335 +
6336 + vchi_service_release(instance->handle);
6337 +
6338 + if (ret) {
6339 + pr_err("error %d queuing message\n", ret);
6340 + return ret;
6341 + }
6342 +
6343 + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
6344 + if (ret <= 0) {
6345 + pr_err("error %d waiting for sync completion\n", ret);
6346 + if (ret == 0)
6347 + ret = -ETIME;
6348 + /* todo: what happens if the message arrives after aborting */
6349 + return ret;
6350 + }
6351 +
6352 + *msg_out = msg_context.u.sync.msg;
6353 + *msg_handle_out = msg_context.u.sync.msg_handle;
6354 +
6355 + return 0;
6356 +}
6357 +
6358 +static void dump_port_info(struct vchiq_mmal_port *port)
6359 +{
6360 + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
6361 +
6362 + pr_debug("buffer minimum num:%d size:%d align:%d\n",
6363 + port->minimum_buffer.num,
6364 + port->minimum_buffer.size, port->minimum_buffer.alignment);
6365 +
6366 + pr_debug("buffer recommended num:%d size:%d align:%d\n",
6367 + port->recommended_buffer.num,
6368 + port->recommended_buffer.size,
6369 + port->recommended_buffer.alignment);
6370 +
6371 + pr_debug("buffer current values num:%d size:%d align:%d\n",
6372 + port->current_buffer.num,
6373 + port->current_buffer.size, port->current_buffer.alignment);
6374 +
6375 + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
6376 + port->format.type,
6377 + port->format.encoding, port->format.encoding_variant);
6378 +
6379 + pr_debug(" bitrate:%d flags:0x%x\n",
6380 + port->format.bitrate, port->format.flags);
6381 +
6382 + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
6383 + pr_debug
6384 + ("es video format: width:%d height:%d colourspace:0x%x\n",
6385 + port->es.video.width, port->es.video.height,
6386 + port->es.video.color_space);
6387 +
6388 + pr_debug(" : crop xywh %d,%d,%d,%d\n",
6389 + port->es.video.crop.x,
6390 + port->es.video.crop.y,
6391 + port->es.video.crop.width, port->es.video.crop.height);
6392 + pr_debug(" : framerate %d/%d aspect %d/%d\n",
6393 + port->es.video.frame_rate.num,
6394 + port->es.video.frame_rate.den,
6395 + port->es.video.par.num, port->es.video.par.den);
6396 + }
6397 +}
6398 +
6399 +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
6400 +{
6401 +
6402 + /* todo do readonly fields need setting at all? */
6403 + p->type = port->type;
6404 + p->index = port->index;
6405 + p->index_all = 0;
6406 + p->is_enabled = port->enabled;
6407 + p->buffer_num_min = port->minimum_buffer.num;
6408 + p->buffer_size_min = port->minimum_buffer.size;
6409 + p->buffer_alignment_min = port->minimum_buffer.alignment;
6410 + p->buffer_num_recommended = port->recommended_buffer.num;
6411 + p->buffer_size_recommended = port->recommended_buffer.size;
6412 +
6413 + /* only three writable fields in a port */
6414 + p->buffer_num = port->current_buffer.num;
6415 + p->buffer_size = port->current_buffer.size;
6416 + p->userdata = port;
6417 +}
6418 +
6419 +static int port_info_set(struct vchiq_mmal_instance *instance,
6420 + struct vchiq_mmal_port *port)
6421 +{
6422 + int ret;
6423 + struct mmal_msg m;
6424 + struct mmal_msg *rmsg;
6425 + VCHI_HELD_MSG_T rmsg_handle;
6426 +
6427 + pr_debug("setting port info port %p\n", port);
6428 + if (!port)
6429 + return -1;
6430 + dump_port_info(port);
6431 +
6432 + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
6433 +
6434 + m.u.port_info_set.component_handle = port->component->handle;
6435 + m.u.port_info_set.port_type = port->type;
6436 + m.u.port_info_set.port_index = port->index;
6437 +
6438 + port_to_mmal_msg(port, &m.u.port_info_set.port);
6439 +
6440 + /* elementry stream format setup */
6441 + m.u.port_info_set.format.type = port->format.type;
6442 + m.u.port_info_set.format.encoding = port->format.encoding;
6443 + m.u.port_info_set.format.encoding_variant =
6444 + port->format.encoding_variant;
6445 + m.u.port_info_set.format.bitrate = port->format.bitrate;
6446 + m.u.port_info_set.format.flags = port->format.flags;
6447 +
6448 + memcpy(&m.u.port_info_set.es, &port->es,
6449 + sizeof(union mmal_es_specific_format));
6450 +
6451 + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
6452 + memcpy(&m.u.port_info_set.extradata, port->format.extradata,
6453 + port->format.extradata_size);
6454 +
6455 + ret = send_synchronous_mmal_msg(instance, &m,
6456 + sizeof(m.u.port_info_set),
6457 + &rmsg, &rmsg_handle);
6458 + if (ret)
6459 + return ret;
6460 +
6461 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
6462 + /* got an unexpected message type in reply */
6463 + ret = -EINVAL;
6464 + goto release_msg;
6465 + }
6466 +
6467 + /* return operation status */
6468 + ret = -rmsg->u.port_info_get_reply.status;
6469 +
6470 + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
6471 + port->component->handle, port->handle);
6472 +
6473 +release_msg:
6474 + vchi_held_msg_release(&rmsg_handle);
6475 +
6476 + return ret;
6477 +
6478 +}
6479 +
6480 +/* use port info get message to retrive port information */
6481 +static int port_info_get(struct vchiq_mmal_instance *instance,
6482 + struct vchiq_mmal_port *port)
6483 +{
6484 + int ret;
6485 + struct mmal_msg m;
6486 + struct mmal_msg *rmsg;
6487 + VCHI_HELD_MSG_T rmsg_handle;
6488 +
6489 + /* port info time */
6490 + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
6491 + m.u.port_info_get.component_handle = port->component->handle;
6492 + m.u.port_info_get.port_type = port->type;
6493 + m.u.port_info_get.index = port->index;
6494 +
6495 + ret = send_synchronous_mmal_msg(instance, &m,
6496 + sizeof(m.u.port_info_get),
6497 + &rmsg, &rmsg_handle);
6498 + if (ret)
6499 + return ret;
6500 +
6501 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
6502 + /* got an unexpected message type in reply */
6503 + ret = -EINVAL;
6504 + goto release_msg;
6505 + }
6506 +
6507 + /* return operation status */
6508 + ret = -rmsg->u.port_info_get_reply.status;
6509 + if (ret != MMAL_MSG_STATUS_SUCCESS)
6510 + goto release_msg;
6511 +
6512 + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
6513 + port->enabled = false;
6514 + else
6515 + port->enabled = true;
6516 +
6517 + /* copy the values out of the message */
6518 + port->handle = rmsg->u.port_info_get_reply.port_handle;
6519 +
6520 + /* port type and index cached to use on port info set becuase
6521 + * it does not use a port handle
6522 + */
6523 + port->type = rmsg->u.port_info_get_reply.port_type;
6524 + port->index = rmsg->u.port_info_get_reply.port_index;
6525 +
6526 + port->minimum_buffer.num =
6527 + rmsg->u.port_info_get_reply.port.buffer_num_min;
6528 + port->minimum_buffer.size =
6529 + rmsg->u.port_info_get_reply.port.buffer_size_min;
6530 + port->minimum_buffer.alignment =
6531 + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
6532 +
6533 + port->recommended_buffer.alignment =
6534 + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
6535 + port->recommended_buffer.num =
6536 + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
6537 +
6538 + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
6539 + port->current_buffer.size =
6540 + rmsg->u.port_info_get_reply.port.buffer_size;
6541 +
6542 + /* stream format */
6543 + port->format.type = rmsg->u.port_info_get_reply.format.type;
6544 + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
6545 + port->format.encoding_variant =
6546 + rmsg->u.port_info_get_reply.format.encoding_variant;
6547 + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
6548 + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
6549 +
6550 + /* elementry stream format */
6551 + memcpy(&port->es,
6552 + &rmsg->u.port_info_get_reply.es,
6553 + sizeof(union mmal_es_specific_format));
6554 + port->format.es = &port->es;
6555 +
6556 + port->format.extradata_size =
6557 + rmsg->u.port_info_get_reply.format.extradata_size;
6558 + memcpy(port->format.extradata,
6559 + rmsg->u.port_info_get_reply.extradata,
6560 + port->format.extradata_size);
6561 +
6562 + pr_debug("received port info\n");
6563 + dump_port_info(port);
6564 +
6565 +release_msg:
6566 +
6567 + pr_debug("%s:result:%d component:0x%x port:%d\n",
6568 + __func__, ret, port->component->handle, port->handle);
6569 +
6570 + vchi_held_msg_release(&rmsg_handle);
6571 +
6572 + return ret;
6573 +}
6574 +
6575 +/* create comonent on vc */
6576 +static int create_component(struct vchiq_mmal_instance *instance,
6577 + struct vchiq_mmal_component *component,
6578 + const char *name)
6579 +{
6580 + int ret;
6581 + struct mmal_msg m;
6582 + struct mmal_msg *rmsg;
6583 + VCHI_HELD_MSG_T rmsg_handle;
6584 +
6585 + /* build component create message */
6586 + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
6587 + m.u.component_create.client_component = component;
6588 + strncpy(m.u.component_create.name, name,
6589 + sizeof(m.u.component_create.name));
6590 +
6591 + ret = send_synchronous_mmal_msg(instance, &m,
6592 + sizeof(m.u.component_create),
6593 + &rmsg, &rmsg_handle);
6594 + if (ret)
6595 + return ret;
6596 +
6597 + if (rmsg->h.type != m.h.type) {
6598 + /* got an unexpected message type in reply */
6599 + ret = -EINVAL;
6600 + goto release_msg;
6601 + }
6602 +
6603 + ret = -rmsg->u.component_create_reply.status;
6604 + if (ret != MMAL_MSG_STATUS_SUCCESS)
6605 + goto release_msg;
6606 +
6607 + /* a valid component response received */
6608 + component->handle = rmsg->u.component_create_reply.component_handle;
6609 + component->inputs = rmsg->u.component_create_reply.input_num;
6610 + component->outputs = rmsg->u.component_create_reply.output_num;
6611 + component->clocks = rmsg->u.component_create_reply.clock_num;
6612 +
6613 + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
6614 + component->handle,
6615 + component->inputs, component->outputs, component->clocks);
6616 +
6617 +release_msg:
6618 + vchi_held_msg_release(&rmsg_handle);
6619 +
6620 + return ret;
6621 +}
6622 +
6623 +/* destroys a component on vc */
6624 +static int destroy_component(struct vchiq_mmal_instance *instance,
6625 + struct vchiq_mmal_component *component)
6626 +{
6627 + int ret;
6628 + struct mmal_msg m;
6629 + struct mmal_msg *rmsg;
6630 + VCHI_HELD_MSG_T rmsg_handle;
6631 +
6632 + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
6633 + m.u.component_destroy.component_handle = component->handle;
6634 +
6635 + ret = send_synchronous_mmal_msg(instance, &m,
6636 + sizeof(m.u.component_destroy),
6637 + &rmsg, &rmsg_handle);
6638 + if (ret)
6639 + return ret;
6640 +
6641 + if (rmsg->h.type != m.h.type) {
6642 + /* got an unexpected message type in reply */
6643 + ret = -EINVAL;
6644 + goto release_msg;
6645 + }
6646 +
6647 + ret = -rmsg->u.component_destroy_reply.status;
6648 +
6649 +release_msg:
6650 +
6651 + vchi_held_msg_release(&rmsg_handle);
6652 +
6653 + return ret;
6654 +}
6655 +
6656 +/* enable a component on vc */
6657 +static int enable_component(struct vchiq_mmal_instance *instance,
6658 + struct vchiq_mmal_component *component)
6659 +{
6660 + int ret;
6661 + struct mmal_msg m;
6662 + struct mmal_msg *rmsg;
6663 + VCHI_HELD_MSG_T rmsg_handle;
6664 +
6665 + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
6666 + m.u.component_enable.component_handle = component->handle;
6667 +
6668 + ret = send_synchronous_mmal_msg(instance, &m,
6669 + sizeof(m.u.component_enable),
6670 + &rmsg, &rmsg_handle);
6671 + if (ret)
6672 + return ret;
6673 +
6674 + if (rmsg->h.type != m.h.type) {
6675 + /* got an unexpected message type in reply */
6676 + ret = -EINVAL;
6677 + goto release_msg;
6678 + }
6679 +
6680 + ret = -rmsg->u.component_enable_reply.status;
6681 +
6682 +release_msg:
6683 + vchi_held_msg_release(&rmsg_handle);
6684 +
6685 + return ret;
6686 +}
6687 +
6688 +/* disable a component on vc */
6689 +static int disable_component(struct vchiq_mmal_instance *instance,
6690 + struct vchiq_mmal_component *component)
6691 +{
6692 + int ret;
6693 + struct mmal_msg m;
6694 + struct mmal_msg *rmsg;
6695 + VCHI_HELD_MSG_T rmsg_handle;
6696 +
6697 + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
6698 + m.u.component_disable.component_handle = component->handle;
6699 +
6700 + ret = send_synchronous_mmal_msg(instance, &m,
6701 + sizeof(m.u.component_disable),
6702 + &rmsg, &rmsg_handle);
6703 + if (ret)
6704 + return ret;
6705 +
6706 + if (rmsg->h.type != m.h.type) {
6707 + /* got an unexpected message type in reply */
6708 + ret = -EINVAL;
6709 + goto release_msg;
6710 + }
6711 +
6712 + ret = -rmsg->u.component_disable_reply.status;
6713 +
6714 +release_msg:
6715 +
6716 + vchi_held_msg_release(&rmsg_handle);
6717 +
6718 + return ret;
6719 +}
6720 +
6721 +/* get version of mmal implementation */
6722 +static int get_version(struct vchiq_mmal_instance *instance,
6723 + u32 *major_out, u32 *minor_out)
6724 +{
6725 + int ret;
6726 + struct mmal_msg m;
6727 + struct mmal_msg *rmsg;
6728 + VCHI_HELD_MSG_T rmsg_handle;
6729 +
6730 + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
6731 +
6732 + ret = send_synchronous_mmal_msg(instance, &m,
6733 + sizeof(m.u.version),
6734 + &rmsg, &rmsg_handle);
6735 + if (ret)
6736 + return ret;
6737 +
6738 + if (rmsg->h.type != m.h.type) {
6739 + /* got an unexpected message type in reply */
6740 + ret = -EINVAL;
6741 + goto release_msg;
6742 + }
6743 +
6744 + *major_out = rmsg->u.version.major;
6745 + *minor_out = rmsg->u.version.minor;
6746 +
6747 +release_msg:
6748 + vchi_held_msg_release(&rmsg_handle);
6749 +
6750 + return ret;
6751 +}
6752 +
6753 +/* do a port action with a port as a parameter */
6754 +static int port_action_port(struct vchiq_mmal_instance *instance,
6755 + struct vchiq_mmal_port *port,
6756 + enum mmal_msg_port_action_type action_type)
6757 +{
6758 + int ret;
6759 + struct mmal_msg m;
6760 + struct mmal_msg *rmsg;
6761 + VCHI_HELD_MSG_T rmsg_handle;
6762 +
6763 + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
6764 + m.u.port_action_port.component_handle = port->component->handle;
6765 + m.u.port_action_port.port_handle = port->handle;
6766 + m.u.port_action_port.action = action_type;
6767 +
6768 + port_to_mmal_msg(port, &m.u.port_action_port.port);
6769 +
6770 + ret = send_synchronous_mmal_msg(instance, &m,
6771 + sizeof(m.u.port_action_port),
6772 + &rmsg, &rmsg_handle);
6773 + if (ret)
6774 + return ret;
6775 +
6776 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
6777 + /* got an unexpected message type in reply */
6778 + ret = -EINVAL;
6779 + goto release_msg;
6780 + }
6781 +
6782 + ret = -rmsg->u.port_action_reply.status;
6783 +
6784 + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
6785 + __func__,
6786 + ret, port->component->handle, port->handle,
6787 + port_action_type_names[action_type], action_type);
6788 +
6789 +release_msg:
6790 + vchi_held_msg_release(&rmsg_handle);
6791 +
6792 + return ret;
6793 +}
6794 +
6795 +/* do a port action with handles as parameters */
6796 +static int port_action_handle(struct vchiq_mmal_instance *instance,
6797 + struct vchiq_mmal_port *port,
6798 + enum mmal_msg_port_action_type action_type,
6799 + u32 connect_component_handle,
6800 + u32 connect_port_handle)
6801 +{
6802 + int ret;
6803 + struct mmal_msg m;
6804 + struct mmal_msg *rmsg;
6805 + VCHI_HELD_MSG_T rmsg_handle;
6806 +
6807 + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
6808 +
6809 + m.u.port_action_handle.component_handle = port->component->handle;
6810 + m.u.port_action_handle.port_handle = port->handle;
6811 + m.u.port_action_handle.action = action_type;
6812 +
6813 + m.u.port_action_handle.connect_component_handle =
6814 + connect_component_handle;
6815 + m.u.port_action_handle.connect_port_handle = connect_port_handle;
6816 +
6817 + ret = send_synchronous_mmal_msg(instance, &m,
6818 + sizeof(m.u.port_action_handle),
6819 + &rmsg, &rmsg_handle);
6820 + if (ret)
6821 + return ret;
6822 +
6823 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
6824 + /* got an unexpected message type in reply */
6825 + ret = -EINVAL;
6826 + goto release_msg;
6827 + }
6828 +
6829 + ret = -rmsg->u.port_action_reply.status;
6830 +
6831 + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
6832 + " connect component:0x%x connect port:%d\n",
6833 + __func__,
6834 + ret, port->component->handle, port->handle,
6835 + port_action_type_names[action_type],
6836 + action_type, connect_component_handle, connect_port_handle);
6837 +
6838 +release_msg:
6839 + vchi_held_msg_release(&rmsg_handle);
6840 +
6841 + return ret;
6842 +}
6843 +
6844 +static int port_parameter_set(struct vchiq_mmal_instance *instance,
6845 + struct vchiq_mmal_port *port,
6846 + u32 parameter_id, void *value, u32 value_size)
6847 +{
6848 + int ret;
6849 + struct mmal_msg m;
6850 + struct mmal_msg *rmsg;
6851 + VCHI_HELD_MSG_T rmsg_handle;
6852 +
6853 + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
6854 +
6855 + m.u.port_parameter_set.component_handle = port->component->handle;
6856 + m.u.port_parameter_set.port_handle = port->handle;
6857 + m.u.port_parameter_set.id = parameter_id;
6858 + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
6859 + memcpy(&m.u.port_parameter_set.value, value, value_size);
6860 +
6861 + ret = send_synchronous_mmal_msg(instance, &m,
6862 + (4 * sizeof(u32)) + value_size,
6863 + &rmsg, &rmsg_handle);
6864 + if (ret)
6865 + return ret;
6866 +
6867 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
6868 + /* got an unexpected message type in reply */
6869 + ret = -EINVAL;
6870 + goto release_msg;
6871 + }
6872 +
6873 + ret = -rmsg->u.port_parameter_set_reply.status;
6874 +
6875 + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
6876 + __func__,
6877 + ret, port->component->handle, port->handle, parameter_id);
6878 +
6879 +release_msg:
6880 + vchi_held_msg_release(&rmsg_handle);
6881 +
6882 + return ret;
6883 +}
6884 +
6885 +static int port_parameter_get(struct vchiq_mmal_instance *instance,
6886 + struct vchiq_mmal_port *port,
6887 + u32 parameter_id, void *value, u32 *value_size)
6888 +{
6889 + int ret;
6890 + struct mmal_msg m;
6891 + struct mmal_msg *rmsg;
6892 + VCHI_HELD_MSG_T rmsg_handle;
6893 +
6894 + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
6895 +
6896 + m.u.port_parameter_get.component_handle = port->component->handle;
6897 + m.u.port_parameter_get.port_handle = port->handle;
6898 + m.u.port_parameter_get.id = parameter_id;
6899 + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
6900 +
6901 + ret = send_synchronous_mmal_msg(instance, &m,
6902 + sizeof(struct
6903 + mmal_msg_port_parameter_get),
6904 + &rmsg, &rmsg_handle);
6905 + if (ret)
6906 + return ret;
6907 +
6908 + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
6909 + /* got an unexpected message type in reply */
6910 + pr_err("Incorrect reply type %d\n", rmsg->h.type);
6911 + ret = -EINVAL;
6912 + goto release_msg;
6913 + }
6914 +
6915 + ret = -rmsg->u.port_parameter_get_reply.status;
6916 + if (ret) {
6917 + /* Copy only as much as we have space for
6918 + * but report true size of parameter
6919 + */
6920 + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
6921 + *value_size);
6922 + *value_size = rmsg->u.port_parameter_get_reply.size;
6923 + } else
6924 + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
6925 + rmsg->u.port_parameter_get_reply.size);
6926 +
6927 + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
6928 + ret, port->component->handle, port->handle, parameter_id);
6929 +
6930 +release_msg:
6931 + vchi_held_msg_release(&rmsg_handle);
6932 +
6933 + return ret;
6934 +}
6935 +
6936 +/* disables a port and drains buffers from it */
6937 +static int port_disable(struct vchiq_mmal_instance *instance,
6938 + struct vchiq_mmal_port *port)
6939 +{
6940 + int ret;
6941 + struct list_head *q, *buf_head;
6942 + unsigned long flags = 0;
6943 +
6944 + if (!port->enabled)
6945 + return 0;
6946 +
6947 + port->enabled = false;
6948 +
6949 + ret = port_action_port(instance, port,
6950 + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
6951 + if (ret == 0) {
6952 +
6953 + /* drain all queued buffers on port */
6954 + spin_lock_irqsave(&port->slock, flags);
6955 +
6956 + list_for_each_safe(buf_head, q, &port->buffers) {
6957 + struct mmal_buffer *mmalbuf;
6958 + mmalbuf = list_entry(buf_head, struct mmal_buffer,
6959 + list);
6960 + list_del(buf_head);
6961 + if (port->buffer_cb)
6962 + port->buffer_cb(instance,
6963 + port, 0, mmalbuf, 0, 0,
6964 + MMAL_TIME_UNKNOWN,
6965 + MMAL_TIME_UNKNOWN);
6966 + }
6967 +
6968 + spin_unlock_irqrestore(&port->slock, flags);
6969 +
6970 + ret = port_info_get(instance, port);
6971 + }
6972 +
6973 + return ret;
6974 +}
6975 +
6976 +/* enable a port */
6977 +static int port_enable(struct vchiq_mmal_instance *instance,
6978 + struct vchiq_mmal_port *port)
6979 +{
6980 + unsigned int hdr_count;
6981 + struct list_head *buf_head;
6982 + int ret;
6983 +
6984 + if (port->enabled)
6985 + return 0;
6986 +
6987 + /* ensure there are enough buffers queued to cover the buffer headers */
6988 + if (port->buffer_cb != NULL) {
6989 + hdr_count = 0;
6990 + list_for_each(buf_head, &port->buffers) {
6991 + hdr_count++;
6992 + }
6993 + if (hdr_count < port->current_buffer.num)
6994 + return -ENOSPC;
6995 + }
6996 +
6997 + ret = port_action_port(instance, port,
6998 + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
6999 + if (ret)
7000 + goto done;
7001 +
7002 + port->enabled = true;
7003 +
7004 + if (port->buffer_cb) {
7005 + /* send buffer headers to videocore */
7006 + hdr_count = 1;
7007 + list_for_each(buf_head, &port->buffers) {
7008 + struct mmal_buffer *mmalbuf;
7009 + mmalbuf = list_entry(buf_head, struct mmal_buffer,
7010 + list);
7011 + ret = buffer_from_host(instance, port, mmalbuf);
7012 + if (ret)
7013 + goto done;
7014 +
7015 + hdr_count++;
7016 + if (hdr_count > port->current_buffer.num)
7017 + break;
7018 + }
7019 + }
7020 +
7021 + ret = port_info_get(instance, port);
7022 +
7023 +done:
7024 + return ret;
7025 +}
7026 +
7027 +/* ------------------------------------------------------------------
7028 + * Exported API
7029 + *------------------------------------------------------------------*/
7030 +
7031 +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
7032 + struct vchiq_mmal_port *port)
7033 +{
7034 + int ret;
7035 +
7036 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7037 + return -EINTR;
7038 +
7039 + ret = port_info_set(instance, port);
7040 + if (ret)
7041 + goto release_unlock;
7042 +
7043 + /* read what has actually been set */
7044 + ret = port_info_get(instance, port);
7045 +
7046 +release_unlock:
7047 + mutex_unlock(&instance->vchiq_mutex);
7048 +
7049 + return ret;
7050 +
7051 +}
7052 +
7053 +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
7054 + struct vchiq_mmal_port *port,
7055 + u32 parameter, void *value, u32 value_size)
7056 +{
7057 + int ret;
7058 +
7059 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7060 + return -EINTR;
7061 +
7062 + ret = port_parameter_set(instance, port, parameter, value, value_size);
7063 +
7064 + mutex_unlock(&instance->vchiq_mutex);
7065 +
7066 + return ret;
7067 +}
7068 +
7069 +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
7070 + struct vchiq_mmal_port *port,
7071 + u32 parameter, void *value, u32 *value_size)
7072 +{
7073 + int ret;
7074 +
7075 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7076 + return -EINTR;
7077 +
7078 + ret = port_parameter_get(instance, port, parameter, value, value_size);
7079 +
7080 + mutex_unlock(&instance->vchiq_mutex);
7081 +
7082 + return ret;
7083 +}
7084 +
7085 +/* enable a port
7086 + *
7087 + * enables a port and queues buffers for satisfying callbacks if we
7088 + * provide a callback handler
7089 + */
7090 +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
7091 + struct vchiq_mmal_port *port,
7092 + vchiq_mmal_buffer_cb buffer_cb)
7093 +{
7094 + int ret;
7095 +
7096 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7097 + return -EINTR;
7098 +
7099 + /* already enabled - noop */
7100 + if (port->enabled) {
7101 + ret = 0;
7102 + goto unlock;
7103 + }
7104 +
7105 + port->buffer_cb = buffer_cb;
7106 +
7107 + ret = port_enable(instance, port);
7108 +
7109 +unlock:
7110 + mutex_unlock(&instance->vchiq_mutex);
7111 +
7112 + return ret;
7113 +}
7114 +
7115 +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
7116 + struct vchiq_mmal_port *port)
7117 +{
7118 + int ret;
7119 +
7120 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7121 + return -EINTR;
7122 +
7123 + if (!port->enabled) {
7124 + mutex_unlock(&instance->vchiq_mutex);
7125 + return 0;
7126 + }
7127 +
7128 + ret = port_disable(instance, port);
7129 +
7130 + mutex_unlock(&instance->vchiq_mutex);
7131 +
7132 + return ret;
7133 +}
7134 +
7135 +/* ports will be connected in a tunneled manner so data buffers
7136 + * are not handled by client.
7137 + */
7138 +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
7139 + struct vchiq_mmal_port *src,
7140 + struct vchiq_mmal_port *dst)
7141 +{
7142 + int ret;
7143 +
7144 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7145 + return -EINTR;
7146 +
7147 + /* disconnect ports if connected */
7148 + if (src->connected != NULL) {
7149 + ret = port_disable(instance, src);
7150 + if (ret) {
7151 + pr_err("failed disabling src port(%d)\n", ret);
7152 + goto release_unlock;
7153 + }
7154 +
7155 + /* do not need to disable the destination port as they
7156 + * are connected and it is done automatically
7157 + */
7158 +
7159 + ret = port_action_handle(instance, src,
7160 + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
7161 + src->connected->component->handle,
7162 + src->connected->handle);
7163 + if (ret < 0) {
7164 + pr_err("failed disconnecting src port\n");
7165 + goto release_unlock;
7166 + }
7167 + src->connected->enabled = false;
7168 + src->connected = NULL;
7169 + }
7170 +
7171 + if (dst == NULL) {
7172 + /* do not make new connection */
7173 + ret = 0;
7174 + pr_debug("not making new connection\n");
7175 + goto release_unlock;
7176 + }
7177 +
7178 + /* copy src port format to dst */
7179 + dst->format.encoding = src->format.encoding;
7180 + dst->es.video.width = src->es.video.width;
7181 + dst->es.video.height = src->es.video.height;
7182 + dst->es.video.crop.x = src->es.video.crop.x;
7183 + dst->es.video.crop.y = src->es.video.crop.y;
7184 + dst->es.video.crop.width = src->es.video.crop.width;
7185 + dst->es.video.crop.height = src->es.video.crop.height;
7186 + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
7187 + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
7188 +
7189 + /* set new format */
7190 + ret = port_info_set(instance, dst);
7191 + if (ret) {
7192 + pr_debug("setting port info failed\n");
7193 + goto release_unlock;
7194 + }
7195 +
7196 + /* read what has actually been set */
7197 + ret = port_info_get(instance, dst);
7198 + if (ret) {
7199 + pr_debug("read back port info failed\n");
7200 + goto release_unlock;
7201 + }
7202 +
7203 + /* connect two ports together */
7204 + ret = port_action_handle(instance, src,
7205 + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
7206 + dst->component->handle, dst->handle);
7207 + if (ret < 0) {
7208 + pr_debug("connecting port %d:%d to %d:%d failed\n",
7209 + src->component->handle, src->handle,
7210 + dst->component->handle, dst->handle);
7211 + goto release_unlock;
7212 + }
7213 + src->connected = dst;
7214 +
7215 +release_unlock:
7216 +
7217 + mutex_unlock(&instance->vchiq_mutex);
7218 +
7219 + return ret;
7220 +}
7221 +
7222 +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
7223 + struct vchiq_mmal_port *port,
7224 + struct mmal_buffer *buffer)
7225 +{
7226 + unsigned long flags = 0;
7227 +
7228 + spin_lock_irqsave(&port->slock, flags);
7229 + list_add_tail(&buffer->list, &port->buffers);
7230 + spin_unlock_irqrestore(&port->slock, flags);
7231 +
7232 + /* the port previously underflowed because it was missing a
7233 + * mmal_buffer which has just been added, submit that buffer
7234 + * to the mmal service.
7235 + */
7236 + if (port->buffer_underflow) {
7237 + port_buffer_from_host(instance, port);
7238 + port->buffer_underflow--;
7239 + }
7240 +
7241 + return 0;
7242 +}
7243 +
7244 +/* Initialise a mmal component and its ports
7245 + *
7246 + */
7247 +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
7248 + const char *name,
7249 + struct vchiq_mmal_component **component_out)
7250 +{
7251 + int ret;
7252 + int idx; /* port index */
7253 + struct vchiq_mmal_component *component;
7254 +
7255 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7256 + return -EINTR;
7257 +
7258 + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
7259 + ret = -EINVAL; /* todo is this correct error? */
7260 + goto unlock;
7261 + }
7262 +
7263 + component = &instance->component[instance->component_idx];
7264 +
7265 + ret = create_component(instance, component, name);
7266 + if (ret < 0)
7267 + goto unlock;
7268 +
7269 + /* ports info needs gathering */
7270 + component->control.type = MMAL_PORT_TYPE_CONTROL;
7271 + component->control.index = 0;
7272 + component->control.component = component;
7273 + spin_lock_init(&component->control.slock);
7274 + INIT_LIST_HEAD(&component->control.buffers);
7275 + ret = port_info_get(instance, &component->control);
7276 + if (ret < 0)
7277 + goto release_component;
7278 +
7279 + for (idx = 0; idx < component->inputs; idx++) {
7280 + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
7281 + component->input[idx].index = idx;
7282 + component->input[idx].component = component;
7283 + spin_lock_init(&component->input[idx].slock);
7284 + INIT_LIST_HEAD(&component->input[idx].buffers);
7285 + ret = port_info_get(instance, &component->input[idx]);
7286 + if (ret < 0)
7287 + goto release_component;
7288 + }
7289 +
7290 + for (idx = 0; idx < component->outputs; idx++) {
7291 + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
7292 + component->output[idx].index = idx;
7293 + component->output[idx].component = component;
7294 + spin_lock_init(&component->output[idx].slock);
7295 + INIT_LIST_HEAD(&component->output[idx].buffers);
7296 + ret = port_info_get(instance, &component->output[idx]);
7297 + if (ret < 0)
7298 + goto release_component;
7299 + }
7300 +
7301 + for (idx = 0; idx < component->clocks; idx++) {
7302 + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
7303 + component->clock[idx].index = idx;
7304 + component->clock[idx].component = component;
7305 + spin_lock_init(&component->clock[idx].slock);
7306 + INIT_LIST_HEAD(&component->clock[idx].buffers);
7307 + ret = port_info_get(instance, &component->clock[idx]);
7308 + if (ret < 0)
7309 + goto release_component;
7310 + }
7311 +
7312 + instance->component_idx++;
7313 +
7314 + *component_out = component;
7315 +
7316 + mutex_unlock(&instance->vchiq_mutex);
7317 +
7318 + return 0;
7319 +
7320 +release_component:
7321 + destroy_component(instance, component);
7322 +unlock:
7323 + mutex_unlock(&instance->vchiq_mutex);
7324 +
7325 + return ret;
7326 +}
7327 +
7328 +/*
7329 + * cause a mmal component to be destroyed
7330 + */
7331 +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
7332 + struct vchiq_mmal_component *component)
7333 +{
7334 + int ret;
7335 +
7336 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7337 + return -EINTR;
7338 +
7339 + if (component->enabled)
7340 + ret = disable_component(instance, component);
7341 +
7342 + ret = destroy_component(instance, component);
7343 +
7344 + mutex_unlock(&instance->vchiq_mutex);
7345 +
7346 + return ret;
7347 +}
7348 +
7349 +/*
7350 + * cause a mmal component to be enabled
7351 + */
7352 +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
7353 + struct vchiq_mmal_component *component)
7354 +{
7355 + int ret;
7356 +
7357 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7358 + return -EINTR;
7359 +
7360 + if (component->enabled) {
7361 + mutex_unlock(&instance->vchiq_mutex);
7362 + return 0;
7363 + }
7364 +
7365 + ret = enable_component(instance, component);
7366 + if (ret == 0)
7367 + component->enabled = true;
7368 +
7369 + mutex_unlock(&instance->vchiq_mutex);
7370 +
7371 + return ret;
7372 +}
7373 +
7374 +/*
7375 + * cause a mmal component to be enabled
7376 + */
7377 +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
7378 + struct vchiq_mmal_component *component)
7379 +{
7380 + int ret;
7381 +
7382 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7383 + return -EINTR;
7384 +
7385 + if (!component->enabled) {
7386 + mutex_unlock(&instance->vchiq_mutex);
7387 + return 0;
7388 + }
7389 +
7390 + ret = disable_component(instance, component);
7391 + if (ret == 0)
7392 + component->enabled = false;
7393 +
7394 + mutex_unlock(&instance->vchiq_mutex);
7395 +
7396 + return ret;
7397 +}
7398 +
7399 +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
7400 + u32 *major_out, u32 *minor_out)
7401 +{
7402 + int ret;
7403 +
7404 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7405 + return -EINTR;
7406 +
7407 + ret = get_version(instance, major_out, minor_out);
7408 +
7409 + mutex_unlock(&instance->vchiq_mutex);
7410 +
7411 + return ret;
7412 +}
7413 +
7414 +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
7415 +{
7416 + int status = 0;
7417 +
7418 + if (instance == NULL)
7419 + return -EINVAL;
7420 +
7421 + if (mutex_lock_interruptible(&instance->vchiq_mutex))
7422 + return -EINTR;
7423 +
7424 + vchi_service_use(instance->handle);
7425 +
7426 + status = vchi_service_close(instance->handle);
7427 + if (status != 0)
7428 + pr_err("mmal-vchiq: VCHIQ close failed");
7429 +
7430 + mutex_unlock(&instance->vchiq_mutex);
7431 +
7432 + vfree(instance->bulk_scratch);
7433 +
7434 + kfree(instance);
7435 +
7436 + return status;
7437 +}
7438 +
7439 +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
7440 +{
7441 + int status;
7442 + struct vchiq_mmal_instance *instance;
7443 + static VCHI_CONNECTION_T *vchi_connection;
7444 + static VCHI_INSTANCE_T vchi_instance;
7445 + SERVICE_CREATION_T params = {
7446 + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
7447 + VC_MMAL_SERVER_NAME,
7448 + vchi_connection,
7449 + 0, /* rx fifo size (unused) */
7450 + 0, /* tx fifo size (unused) */
7451 + service_callback,
7452 + NULL, /* service callback parameter */
7453 + 1, /* unaligned bulk receives */
7454 + 1, /* unaligned bulk transmits */
7455 + 0 /* want crc check on bulk transfers */
7456 + };
7457 +
7458 + /* compile time checks to ensure structure size as they are
7459 + * directly (de)serialised from memory.
7460 + */
7461 +
7462 + /* ensure the header structure has packed to the correct size */
7463 + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
7464 +
7465 + /* ensure message structure does not exceed maximum length */
7466 + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
7467 +
7468 + /* mmal port struct is correct size */
7469 + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
7470 +
7471 + /* create a vchi instance */
7472 + status = vchi_initialise(&vchi_instance);
7473 + if (status) {
7474 + pr_err("Failed to initialise VCHI instance (status=%d)\n",
7475 + status);
7476 + return -EIO;
7477 + }
7478 +
7479 + status = vchi_connect(NULL, 0, vchi_instance);
7480 + if (status) {
7481 + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
7482 + return -EIO;
7483 + }
7484 +
7485 + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
7486 + memset(instance, 0, sizeof(*instance));
7487 +
7488 + mutex_init(&instance->vchiq_mutex);
7489 + mutex_init(&instance->bulk_mutex);
7490 +
7491 + instance->bulk_scratch = vmalloc(PAGE_SIZE);
7492 +
7493 + params.callback_param = instance;
7494 +
7495 + status = vchi_service_open(vchi_instance, &params, &instance->handle);
7496 + if (status) {
7497 + pr_err("Failed to open VCHI service connection (status=%d)\n",
7498 + status);
7499 + goto err_close_services;
7500 + }
7501 +
7502 + vchi_service_release(instance->handle);
7503 +
7504 + *out_instance = instance;
7505 +
7506 + return 0;
7507 +
7508 +err_close_services:
7509 +
7510 + vchi_service_close(instance->handle);
7511 + vfree(instance->bulk_scratch);
7512 + kfree(instance);
7513 + return -ENODEV;
7514 +}
7515 --- /dev/null
7516 +++ b/drivers/media/platform/bcm2835/mmal-vchiq.h
7517 @@ -0,0 +1,178 @@
7518 +/*
7519 + * Broadcom BM2835 V4L2 driver
7520 + *
7521 + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7522 + *
7523 + * This file is subject to the terms and conditions of the GNU General Public
7524 + * License. See the file COPYING in the main directory of this archive
7525 + * for more details.
7526 + *
7527 + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
7528 + * Dave Stevenson <dsteve@broadcom.com>
7529 + * Simon Mellor <simellor@broadcom.com>
7530 + * Luke Diamand <luked@broadcom.com>
7531 + *
7532 + * MMAL interface to VCHIQ message passing
7533 + */
7534 +
7535 +#ifndef MMAL_VCHIQ_H
7536 +#define MMAL_VCHIQ_H
7537 +
7538 +#include "mmal-msg-format.h"
7539 +
7540 +#define MAX_PORT_COUNT 4
7541 +
7542 +/* Maximum size of the format extradata. */
7543 +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
7544 +
7545 +struct vchiq_mmal_instance;
7546 +
7547 +enum vchiq_mmal_es_type {
7548 + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
7549 + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
7550 + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
7551 + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
7552 + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
7553 +};
7554 +
7555 +/* rectangle, used lots so it gets its own struct */
7556 +struct vchiq_mmal_rect {
7557 + s32 x;
7558 + s32 y;
7559 + s32 width;
7560 + s32 height;
7561 +};
7562 +
7563 +struct vchiq_mmal_port_buffer {
7564 + unsigned int num; /* number of buffers */
7565 + u32 size; /* size of buffers */
7566 + u32 alignment; /* alignment of buffers */
7567 +};
7568 +
7569 +struct vchiq_mmal_port;
7570 +
7571 +typedef void (*vchiq_mmal_buffer_cb)(
7572 + struct vchiq_mmal_instance *instance,
7573 + struct vchiq_mmal_port *port,
7574 + int status, struct mmal_buffer *buffer,
7575 + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
7576 +
7577 +struct vchiq_mmal_port {
7578 + bool enabled;
7579 + u32 handle;
7580 + u32 type; /* port type, cached to use on port info set */
7581 + u32 index; /* port index, cached to use on port info set */
7582 +
7583 + /* component port belongs to, allows simple deref */
7584 + struct vchiq_mmal_component *component;
7585 +
7586 + struct vchiq_mmal_port *connected; /* port conencted to */
7587 +
7588 + /* buffer info */
7589 + struct vchiq_mmal_port_buffer minimum_buffer;
7590 + struct vchiq_mmal_port_buffer recommended_buffer;
7591 + struct vchiq_mmal_port_buffer current_buffer;
7592 +
7593 + /* stream format */
7594 + struct mmal_es_format format;
7595 + /* elementry stream format */
7596 + union mmal_es_specific_format es;
7597 +
7598 + /* data buffers to fill */
7599 + struct list_head buffers;
7600 + /* lock to serialise adding and removing buffers from list */
7601 + spinlock_t slock;
7602 + /* count of how many buffer header refils have failed because
7603 + * there was no buffer to satisfy them
7604 + */
7605 + int buffer_underflow;
7606 + /* callback on buffer completion */
7607 + vchiq_mmal_buffer_cb buffer_cb;
7608 + /* callback context */
7609 + void *cb_ctx;
7610 +};
7611 +
7612 +struct vchiq_mmal_component {
7613 + bool enabled;
7614 + u32 handle; /* VideoCore handle for component */
7615 + u32 inputs; /* Number of input ports */
7616 + u32 outputs; /* Number of output ports */
7617 + u32 clocks; /* Number of clock ports */
7618 + struct vchiq_mmal_port control; /* control port */
7619 + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
7620 + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
7621 + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
7622 +};
7623 +
7624 +
7625 +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
7626 +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
7627 +
7628 +/* Initialise a mmal component and its ports
7629 +*
7630 +*/
7631 +int vchiq_mmal_component_init(
7632 + struct vchiq_mmal_instance *instance,
7633 + const char *name,
7634 + struct vchiq_mmal_component **component_out);
7635 +
7636 +int vchiq_mmal_component_finalise(
7637 + struct vchiq_mmal_instance *instance,
7638 + struct vchiq_mmal_component *component);
7639 +
7640 +int vchiq_mmal_component_enable(
7641 + struct vchiq_mmal_instance *instance,
7642 + struct vchiq_mmal_component *component);
7643 +
7644 +int vchiq_mmal_component_disable(
7645 + struct vchiq_mmal_instance *instance,
7646 + struct vchiq_mmal_component *component);
7647 +
7648 +
7649 +
7650 +/* enable a mmal port
7651 + *
7652 + * enables a port and if a buffer callback provided enque buffer
7653 + * headers as apropriate for the port.
7654 + */
7655 +int vchiq_mmal_port_enable(
7656 + struct vchiq_mmal_instance *instance,
7657 + struct vchiq_mmal_port *port,
7658 + vchiq_mmal_buffer_cb buffer_cb);
7659 +
7660 +/* disable a port
7661 + *
7662 + * disable a port will dequeue any pending buffers
7663 + */
7664 +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
7665 + struct vchiq_mmal_port *port);
7666 +
7667 +
7668 +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
7669 + struct vchiq_mmal_port *port,
7670 + u32 parameter,
7671 + void *value,
7672 + u32 value_size);
7673 +
7674 +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
7675 + struct vchiq_mmal_port *port,
7676 + u32 parameter,
7677 + void *value,
7678 + u32 *value_size);
7679 +
7680 +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
7681 + struct vchiq_mmal_port *port);
7682 +
7683 +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
7684 + struct vchiq_mmal_port *src,
7685 + struct vchiq_mmal_port *dst);
7686 +
7687 +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
7688 + u32 *major_out,
7689 + u32 *minor_out);
7690 +
7691 +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
7692 + struct vchiq_mmal_port *port,
7693 + struct mmal_buffer *buf);
7694 +
7695 +#endif /* MMAL_VCHIQ_H */