staging: bcm2835-camera: Return early on errors
[platform/kernel/linux-rpi.git] / drivers / staging / vc04_services / bcm2835-camera / bcm2835-camera.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Broadcom BM2835 V4L2 driver
4  *
5  * Copyright © 2013 Raspberry Pi (Trading) Ltd.
6  *
7  * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
8  *          Dave Stevenson <dsteve@broadcom.com>
9  *          Simon Mellor <simellor@broadcom.com>
10  *          Luke Diamand <luked@broadcom.com>
11  */
12
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <media/videobuf2-vmalloc.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-common.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27
28 #include "mmal-common.h"
29 #include "mmal-encodings.h"
30 #include "mmal-vchiq.h"
31 #include "mmal-msg.h"
32 #include "mmal-parameters.h"
33 #include "bcm2835-camera.h"
34
35 #define BM2835_MMAL_VERSION "0.0.2"
36 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
37 #define MIN_WIDTH 32
38 #define MIN_HEIGHT 32
39 #define MIN_BUFFER_SIZE (80 * 1024)
40
41 #define MAX_VIDEO_MODE_WIDTH 1280
42 #define MAX_VIDEO_MODE_HEIGHT 720
43
44 #define MAX_BCM2835_CAMERAS 2
45
46 int bcm2835_v4l2_debug;
47 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
48 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
49
50 #define UNSET (-1)
51 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
52 module_param_array(video_nr, int, NULL, 0644);
53 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
54
55 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
56 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
57 module_param(max_video_width, int, 0644);
58 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
59 module_param(max_video_height, int, 0644);
60 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
61
62 /* global device data array */
63 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
64
65 #define FPS_MIN 1
66 #define FPS_MAX 90
67
68 /* timeperframe: min/max and default */
69 static const struct v4l2_fract
70         tpf_min     = {.numerator = 1,          .denominator = FPS_MAX},
71         tpf_max     = {.numerator = 1,          .denominator = FPS_MIN},
72         tpf_default = {.numerator = 1000,       .denominator = 30000};
73
74 /* video formats */
75 static struct mmal_fmt formats[] = {
76         {
77                 .name = "4:2:0, planar, YUV",
78                 .fourcc = V4L2_PIX_FMT_YUV420,
79                 .flags = 0,
80                 .mmal = MMAL_ENCODING_I420,
81                 .depth = 12,
82                 .mmal_component = MMAL_COMPONENT_CAMERA,
83                 .ybbp = 1,
84                 .remove_padding = 1,
85         }, {
86                 .name = "4:2:2, packed, YUYV",
87                 .fourcc = V4L2_PIX_FMT_YUYV,
88                 .flags = 0,
89                 .mmal = MMAL_ENCODING_YUYV,
90                 .depth = 16,
91                 .mmal_component = MMAL_COMPONENT_CAMERA,
92                 .ybbp = 2,
93                 .remove_padding = 0,
94         }, {
95                 .name = "RGB24 (LE)",
96                 .fourcc = V4L2_PIX_FMT_RGB24,
97                 .flags = 0,
98                 .mmal = MMAL_ENCODING_RGB24,
99                 .depth = 24,
100                 .mmal_component = MMAL_COMPONENT_CAMERA,
101                 .ybbp = 3,
102                 .remove_padding = 0,
103         }, {
104                 .name = "JPEG",
105                 .fourcc = V4L2_PIX_FMT_JPEG,
106                 .flags = V4L2_FMT_FLAG_COMPRESSED,
107                 .mmal = MMAL_ENCODING_JPEG,
108                 .depth = 8,
109                 .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
110                 .ybbp = 0,
111                 .remove_padding = 0,
112         }, {
113                 .name = "H264",
114                 .fourcc = V4L2_PIX_FMT_H264,
115                 .flags = V4L2_FMT_FLAG_COMPRESSED,
116                 .mmal = MMAL_ENCODING_H264,
117                 .depth = 8,
118                 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
119                 .ybbp = 0,
120                 .remove_padding = 0,
121         }, {
122                 .name = "MJPEG",
123                 .fourcc = V4L2_PIX_FMT_MJPEG,
124                 .flags = V4L2_FMT_FLAG_COMPRESSED,
125                 .mmal = MMAL_ENCODING_MJPEG,
126                 .depth = 8,
127                 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
128                 .ybbp = 0,
129                 .remove_padding = 0,
130         }, {
131                 .name = "4:2:2, packed, YVYU",
132                 .fourcc = V4L2_PIX_FMT_YVYU,
133                 .flags = 0,
134                 .mmal = MMAL_ENCODING_YVYU,
135                 .depth = 16,
136                 .mmal_component = MMAL_COMPONENT_CAMERA,
137                 .ybbp = 2,
138                 .remove_padding = 0,
139         }, {
140                 .name = "4:2:2, packed, VYUY",
141                 .fourcc = V4L2_PIX_FMT_VYUY,
142                 .flags = 0,
143                 .mmal = MMAL_ENCODING_VYUY,
144                 .depth = 16,
145                 .mmal_component = MMAL_COMPONENT_CAMERA,
146                 .ybbp = 2,
147                 .remove_padding = 0,
148         }, {
149                 .name = "4:2:2, packed, UYVY",
150                 .fourcc = V4L2_PIX_FMT_UYVY,
151                 .flags = 0,
152                 .mmal = MMAL_ENCODING_UYVY,
153                 .depth = 16,
154                 .mmal_component = MMAL_COMPONENT_CAMERA,
155                 .ybbp = 2,
156                 .remove_padding = 0,
157         }, {
158                 .name = "4:2:0, planar, NV12",
159                 .fourcc = V4L2_PIX_FMT_NV12,
160                 .flags = 0,
161                 .mmal = MMAL_ENCODING_NV12,
162                 .depth = 12,
163                 .mmal_component = MMAL_COMPONENT_CAMERA,
164                 .ybbp = 1,
165                 .remove_padding = 1,
166         }, {
167                 .name = "RGB24 (BE)",
168                 .fourcc = V4L2_PIX_FMT_BGR24,
169                 .flags = 0,
170                 .mmal = MMAL_ENCODING_BGR24,
171                 .depth = 24,
172                 .mmal_component = MMAL_COMPONENT_CAMERA,
173                 .ybbp = 3,
174                 .remove_padding = 0,
175         }, {
176                 .name = "4:2:0, planar, YVU",
177                 .fourcc = V4L2_PIX_FMT_YVU420,
178                 .flags = 0,
179                 .mmal = MMAL_ENCODING_YV12,
180                 .depth = 12,
181                 .mmal_component = MMAL_COMPONENT_CAMERA,
182                 .ybbp = 1,
183                 .remove_padding = 1,
184         }, {
185                 .name = "4:2:0, planar, NV21",
186                 .fourcc = V4L2_PIX_FMT_NV21,
187                 .flags = 0,
188                 .mmal = MMAL_ENCODING_NV21,
189                 .depth = 12,
190                 .mmal_component = MMAL_COMPONENT_CAMERA,
191                 .ybbp = 1,
192                 .remove_padding = 1,
193         }, {
194                 .name = "RGB32 (BE)",
195                 .fourcc = V4L2_PIX_FMT_BGR32,
196                 .flags = 0,
197                 .mmal = MMAL_ENCODING_BGRA,
198                 .depth = 32,
199                 .mmal_component = MMAL_COMPONENT_CAMERA,
200                 .ybbp = 4,
201                 .remove_padding = 0,
202         },
203 };
204
205 static struct mmal_fmt *get_format(struct v4l2_format *f)
206 {
207         struct mmal_fmt *fmt;
208         unsigned int k;
209
210         for (k = 0; k < ARRAY_SIZE(formats); k++) {
211                 fmt = &formats[k];
212                 if (fmt->fourcc == f->fmt.pix.pixelformat)
213                         return fmt;
214         }
215
216         return NULL;
217 }
218
219 /* ------------------------------------------------------------------
220  *      Videobuf queue operations
221  * ------------------------------------------------------------------
222  */
223
224 static int queue_setup(struct vb2_queue *vq,
225                        unsigned int *nbuffers, unsigned int *nplanes,
226                        unsigned int sizes[], struct device *alloc_ctxs[])
227 {
228         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
229         unsigned long size;
230
231         /* refuse queue setup if port is not configured */
232         if (!dev->capture.port) {
233                 v4l2_err(&dev->v4l2_dev,
234                          "%s: capture port not configured\n", __func__);
235                 return -EINVAL;
236         }
237
238         size = dev->capture.port->current_buffer.size;
239         if (size == 0) {
240                 v4l2_err(&dev->v4l2_dev,
241                          "%s: capture port buffer size is zero\n", __func__);
242                 return -EINVAL;
243         }
244
245         if (*nbuffers < dev->capture.port->minimum_buffer.num)
246                 *nbuffers = dev->capture.port->minimum_buffer.num;
247
248         dev->capture.port->current_buffer.num = *nbuffers;
249
250         *nplanes = 1;
251
252         sizes[0] = size;
253
254         /*
255          * videobuf2-vmalloc allocator is context-less so no need to set
256          * alloc_ctxs array.
257          */
258
259         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
260                  __func__, dev);
261
262         return 0;
263 }
264
265 static int buffer_init(struct vb2_buffer *vb)
266 {
267         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
268         struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
269         struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
270
271         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
272                  __func__, dev, vb);
273         buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
274         buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
275
276         return mmal_vchi_buffer_init(dev->instance, buf);
277 }
278
279 static int buffer_prepare(struct vb2_buffer *vb)
280 {
281         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
282         unsigned long size;
283
284         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
285                  __func__, dev, vb);
286
287         if (!dev->capture.port || !dev->capture.fmt)
288                 return -ENODEV;
289
290         size = dev->capture.stride * dev->capture.height;
291         if (vb2_plane_size(vb, 0) < size) {
292                 v4l2_err(&dev->v4l2_dev,
293                          "%s data will not fit into plane (%lu < %lu)\n",
294                          __func__, vb2_plane_size(vb, 0), size);
295                 return -EINVAL;
296         }
297
298         return 0;
299 }
300
301 static void buffer_cleanup(struct vb2_buffer *vb)
302 {
303         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
304         struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
305         struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
306
307         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
308                  __func__, dev, vb);
309         mmal_vchi_buffer_cleanup(buf);
310 }
311
312 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
313 {
314         return dev->capture.camera_port ==
315             &dev->
316             component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
317 }
318
319 static void buffer_cb(struct vchiq_mmal_instance *instance,
320                       struct vchiq_mmal_port *port,
321                       int status,
322                       struct mmal_buffer *buf,
323                       unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
324 {
325         struct bm2835_mmal_dev *dev = port->cb_ctx;
326
327         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
328                  "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
329                  __func__, status, buf, length, mmal_flags, pts);
330
331         if (status != 0) {
332                 /* error in transfer */
333                 if (buf) {
334                         /* there was a buffer with the error so return it */
335                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
336                 }
337                 return;
338         }
339
340         if (length == 0) {
341                 /* stream ended */
342                 if (buf) {
343                         /* this should only ever happen if the port is
344                          * disabled and there are buffers still queued
345                          */
346                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
347                         pr_debug("Empty buffer");
348                 } else if (dev->capture.frame_count) {
349                         /* grab another frame */
350                         if (is_capturing(dev)) {
351                                 pr_debug("Grab another frame");
352                                 vchiq_mmal_port_parameter_set(
353                                         instance,
354                                         dev->capture.camera_port,
355                                         MMAL_PARAMETER_CAPTURE,
356                                         &dev->capture.frame_count,
357                                         sizeof(dev->capture.frame_count));
358                         }
359                 } else {
360                         /* signal frame completion */
361                         complete(&dev->capture.frame_cmplt);
362                 }
363                 return;
364         }
365
366         if (!dev->capture.frame_count) {
367                 /* signal frame completion */
368                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
369                 complete(&dev->capture.frame_cmplt);
370                 return;
371         }
372
373         if (dev->capture.vc_start_timestamp == -1) {
374                 /*
375                  * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
376                  * kernel time, and have no latency compensation.
377                  */
378                 buf->vb.vb2_buf.timestamp = ktime_get_ns();
379                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
380                          "Buffer time set as current time - %lld",
381                          buf->vb.vb2_buf.timestamp);
382         } else if (pts != 0) {
383                 ktime_t timestamp;
384                 s64 runtime_us = pts -
385                     dev->capture.vc_start_timestamp;
386                 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
387                                          runtime_us);
388                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
389                          "Convert start time %llu and %llu with offset %llu to %llu\n",
390                          ktime_to_ns(dev->capture.kernel_start_ts),
391                          dev->capture.vc_start_timestamp, pts,
392                          ktime_to_ns(timestamp));
393                 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
394         } else {
395                 if (dev->capture.last_timestamp) {
396                         buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
397                         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
398                                  "Buffer time set as last timestamp - %lld",
399                                  buf->vb.vb2_buf.timestamp);
400                 } else {
401                         buf->vb.vb2_buf.timestamp =
402                                 ktime_to_ns(dev->capture.kernel_start_ts);
403                         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
404                                  "Buffer time set as start timestamp - %lld",
405                                  buf->vb.vb2_buf.timestamp);
406                 }
407         }
408         dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
409
410         vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
411         if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
412                 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
413
414         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
415                  "Buffer has ts %llu",
416                  dev->capture.last_timestamp);
417         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
418
419         if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
420             is_capturing(dev)) {
421                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
422                          "Grab another frame as buffer has EOS");
423                 vchiq_mmal_port_parameter_set(instance,
424                                               dev->capture.camera_port,
425                                               MMAL_PARAMETER_CAPTURE,
426                                               &dev->capture.frame_count,
427                                               sizeof(dev->capture.frame_count));
428         }
429 }
430
431 static int enable_camera(struct bm2835_mmal_dev *dev)
432 {
433         int ret;
434
435         if (!dev->camera_use_count) {
436                 ret = vchiq_mmal_port_parameter_set(
437                         dev->instance,
438                         &dev->component[MMAL_COMPONENT_CAMERA]->control,
439                         MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
440                         sizeof(dev->camera_num));
441                 if (ret < 0) {
442                         v4l2_err(&dev->v4l2_dev,
443                                  "Failed setting camera num, ret %d\n", ret);
444                         return -EINVAL;
445                 }
446
447                 ret = vchiq_mmal_component_enable(
448                                 dev->instance,
449                                 dev->component[MMAL_COMPONENT_CAMERA]);
450                 if (ret < 0) {
451                         v4l2_err(&dev->v4l2_dev,
452                                  "Failed enabling camera, ret %d\n", ret);
453                         return -EINVAL;
454                 }
455         }
456         dev->camera_use_count++;
457         v4l2_dbg(1, bcm2835_v4l2_debug,
458                  &dev->v4l2_dev, "enabled camera (refcount %d)\n",
459                         dev->camera_use_count);
460         return 0;
461 }
462
463 static int disable_camera(struct bm2835_mmal_dev *dev)
464 {
465         int ret;
466
467         if (!dev->camera_use_count) {
468                 v4l2_err(&dev->v4l2_dev,
469                          "Disabled the camera when already disabled\n");
470                 return -EINVAL;
471         }
472         dev->camera_use_count--;
473         if (!dev->camera_use_count) {
474                 unsigned int i = 0xFFFFFFFF;
475
476                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
477                          "Disabling camera\n");
478                 ret =
479                     vchiq_mmal_component_disable(
480                                 dev->instance,
481                                 dev->component[MMAL_COMPONENT_CAMERA]);
482                 if (ret < 0) {
483                         v4l2_err(&dev->v4l2_dev,
484                                  "Failed disabling camera, ret %d\n", ret);
485                         return -EINVAL;
486                 }
487                 vchiq_mmal_port_parameter_set(
488                         dev->instance,
489                         &dev->component[MMAL_COMPONENT_CAMERA]->control,
490                         MMAL_PARAMETER_CAMERA_NUM, &i,
491                         sizeof(i));
492         }
493         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
494                  "Camera refcount now %d\n", dev->camera_use_count);
495         return 0;
496 }
497
498 static void buffer_queue(struct vb2_buffer *vb)
499 {
500         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
501         struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
502         struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
503         int ret;
504
505         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
506                  "%s: dev:%p buf:%p, idx %u\n",
507                  __func__, dev, buf, vb2->vb2_buf.index);
508
509         ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
510         if (ret < 0)
511                 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
512                          __func__);
513 }
514
515 static int start_streaming(struct vb2_queue *vq, unsigned int count)
516 {
517         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
518         int ret;
519         u32 parameter_size;
520
521         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
522                  __func__, dev);
523
524         /* ensure a format has actually been set */
525         if (!dev->capture.port)
526                 return -EINVAL;
527
528         if (enable_camera(dev) < 0) {
529                 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
530                 return -EINVAL;
531         }
532
533         /*init_completion(&dev->capture.frame_cmplt); */
534
535         /* enable frame capture */
536         dev->capture.frame_count = 1;
537
538         /* if the preview is not already running, wait for a few frames for AGC
539          * to settle down.
540          */
541         if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
542                 msleep(300);
543
544         /* enable the connection from camera to encoder (if applicable) */
545         if (dev->capture.camera_port != dev->capture.port
546             && dev->capture.camera_port) {
547                 ret = vchiq_mmal_port_enable(dev->instance,
548                                              dev->capture.camera_port, NULL);
549                 if (ret) {
550                         v4l2_err(&dev->v4l2_dev,
551                                  "Failed to enable encode tunnel - error %d\n",
552                                  ret);
553                         return -1;
554                 }
555         }
556
557         /* Get VC timestamp at this point in time */
558         parameter_size = sizeof(dev->capture.vc_start_timestamp);
559         if (vchiq_mmal_port_parameter_get(dev->instance,
560                                           dev->capture.camera_port,
561                                           MMAL_PARAMETER_SYSTEM_TIME,
562                                           &dev->capture.vc_start_timestamp,
563                                           &parameter_size)) {
564                 v4l2_err(&dev->v4l2_dev,
565                          "Failed to get VC start time - update your VC f/w\n");
566
567                 /* Flag to indicate just to rely on kernel timestamps */
568                 dev->capture.vc_start_timestamp = -1;
569         } else
570                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
571                          "Start time %lld size %d\n",
572                          dev->capture.vc_start_timestamp, parameter_size);
573
574         dev->capture.kernel_start_ts = ktime_get();
575         dev->capture.last_timestamp = 0;
576
577         /* enable the camera port */
578         dev->capture.port->cb_ctx = dev;
579         ret =
580             vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
581         if (ret) {
582                 v4l2_err(&dev->v4l2_dev,
583                         "Failed to enable capture port - error %d. Disabling camera port again\n",
584                         ret);
585
586                 vchiq_mmal_port_disable(dev->instance,
587                                         dev->capture.camera_port);
588                 if (disable_camera(dev) < 0) {
589                         v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
590                         return -EINVAL;
591                 }
592                 return -1;
593         }
594
595         /* capture the first frame */
596         vchiq_mmal_port_parameter_set(dev->instance,
597                                       dev->capture.camera_port,
598                                       MMAL_PARAMETER_CAPTURE,
599                                       &dev->capture.frame_count,
600                                       sizeof(dev->capture.frame_count));
601         return 0;
602 }
603
604 /* abort streaming and wait for last buffer */
605 static void stop_streaming(struct vb2_queue *vq)
606 {
607         int ret;
608         unsigned long timeout;
609         struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
610
611         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
612                  __func__, dev);
613
614         init_completion(&dev->capture.frame_cmplt);
615         dev->capture.frame_count = 0;
616
617         /* ensure a format has actually been set */
618         if (!dev->capture.port) {
619                 v4l2_err(&dev->v4l2_dev,
620                          "no capture port - stream not started?\n");
621                 return;
622         }
623
624         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
625
626         /* stop capturing frames */
627         vchiq_mmal_port_parameter_set(dev->instance,
628                                       dev->capture.camera_port,
629                                       MMAL_PARAMETER_CAPTURE,
630                                       &dev->capture.frame_count,
631                                       sizeof(dev->capture.frame_count));
632
633         /* wait for last frame to complete */
634         timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
635         if (timeout == 0)
636                 v4l2_err(&dev->v4l2_dev,
637                          "timed out waiting for frame completion\n");
638
639         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
640                  "disabling connection\n");
641
642         /* disable the connection from camera to encoder */
643         ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
644         if (!ret && dev->capture.camera_port != dev->capture.port) {
645                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
646                          "disabling port\n");
647                 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
648         } else if (dev->capture.camera_port != dev->capture.port) {
649                 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
650                          ret);
651         }
652
653         if (disable_camera(dev) < 0)
654                 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
655 }
656
657 static const struct vb2_ops bm2835_mmal_video_qops = {
658         .queue_setup = queue_setup,
659         .buf_init = buffer_init,
660         .buf_prepare = buffer_prepare,
661         .buf_cleanup = buffer_cleanup,
662         .buf_queue = buffer_queue,
663         .start_streaming = start_streaming,
664         .stop_streaming = stop_streaming,
665         .wait_prepare = vb2_ops_wait_prepare,
666         .wait_finish = vb2_ops_wait_finish,
667 };
668
669 /* ------------------------------------------------------------------
670  *      IOCTL operations
671  * ------------------------------------------------------------------
672  */
673
674 static int set_overlay_params(struct bm2835_mmal_dev *dev,
675                               struct vchiq_mmal_port *port)
676 {
677         struct mmal_parameter_displayregion prev_config = {
678                 .set =  MMAL_DISPLAY_SET_LAYER |
679                         MMAL_DISPLAY_SET_ALPHA |
680                         MMAL_DISPLAY_SET_DEST_RECT |
681                         MMAL_DISPLAY_SET_FULLSCREEN,
682                 .layer = PREVIEW_LAYER,
683                 .alpha = dev->overlay.global_alpha,
684                 .fullscreen = 0,
685                 .dest_rect = {
686                         .x = dev->overlay.w.left,
687                         .y = dev->overlay.w.top,
688                         .width = dev->overlay.w.width,
689                         .height = dev->overlay.w.height,
690                 },
691         };
692         return vchiq_mmal_port_parameter_set(dev->instance, port,
693                                              MMAL_PARAMETER_DISPLAYREGION,
694                                              &prev_config, sizeof(prev_config));
695 }
696
697 /* overlay ioctl */
698 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
699                                        struct v4l2_fmtdesc *f)
700 {
701         struct mmal_fmt *fmt;
702
703         if (f->index >= ARRAY_SIZE(formats))
704                 return -EINVAL;
705
706         fmt = &formats[f->index];
707
708         strlcpy((char *)f->description, fmt->name, sizeof(f->description));
709         f->pixelformat = fmt->fourcc;
710         f->flags = fmt->flags;
711
712         return 0;
713 }
714
715 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
716                                     struct v4l2_format *f)
717 {
718         struct bm2835_mmal_dev *dev = video_drvdata(file);
719
720         f->fmt.win = dev->overlay;
721
722         return 0;
723 }
724
725 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
726                                       struct v4l2_format *f)
727 {
728         struct bm2835_mmal_dev *dev = video_drvdata(file);
729
730         f->fmt.win.field = V4L2_FIELD_NONE;
731         f->fmt.win.chromakey = 0;
732         f->fmt.win.clips = NULL;
733         f->fmt.win.clipcount = 0;
734         f->fmt.win.bitmap = NULL;
735
736         v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
737                               &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
738                               1, 0);
739         v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
740                               &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
741                               1, 0);
742
743         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
744                  "Overlay: Now w/h %dx%d l/t %dx%d\n",
745                 f->fmt.win.w.width, f->fmt.win.w.height,
746                 f->fmt.win.w.left, f->fmt.win.w.top);
747
748         v4l2_dump_win_format(1,
749                              bcm2835_v4l2_debug,
750                              &dev->v4l2_dev,
751                              &f->fmt.win,
752                              __func__);
753         return 0;
754 }
755
756 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
757                                     struct v4l2_format *f)
758 {
759         struct bm2835_mmal_dev *dev = video_drvdata(file);
760
761         vidioc_try_fmt_vid_overlay(file, priv, f);
762
763         dev->overlay = f->fmt.win;
764         if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
765                 set_overlay_params(dev,
766                                    &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
767         }
768
769         return 0;
770 }
771
772 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
773 {
774         int ret;
775         struct bm2835_mmal_dev *dev = video_drvdata(file);
776         struct vchiq_mmal_port *src;
777         struct vchiq_mmal_port *dst;
778
779         if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
780             (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
781                 return 0;       /* already in requested state */
782
783         src =
784             &dev->component[MMAL_COMPONENT_CAMERA]->
785             output[MMAL_CAMERA_PORT_PREVIEW];
786
787         if (!on) {
788                 /* disconnect preview ports and disable component */
789                 ret = vchiq_mmal_port_disable(dev->instance, src);
790                 if (!ret)
791                         ret =
792                             vchiq_mmal_port_connect_tunnel(dev->instance, src,
793                                                            NULL);
794                 if (ret >= 0)
795                         ret = vchiq_mmal_component_disable(
796                                         dev->instance,
797                                         dev->component[MMAL_COMPONENT_PREVIEW]);
798
799                 disable_camera(dev);
800                 return ret;
801         }
802
803         /* set preview port format and connect it to output */
804         dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
805
806         ret = vchiq_mmal_port_set_format(dev->instance, src);
807         if (ret < 0)
808                 return ret;
809
810         ret = set_overlay_params(dev, dst);
811         if (ret < 0)
812                 return ret;
813
814         if (enable_camera(dev) < 0) {
815                 ret = -EINVAL;
816                 return ret;
817         }
818
819         ret = vchiq_mmal_component_enable(
820                         dev->instance,
821                         dev->component[MMAL_COMPONENT_PREVIEW]);
822         if (ret < 0)
823                 return ret;
824
825         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
826                  src, dst);
827         ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
828         if (!ret)
829                 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
830
831         return ret;
832 }
833
834 static int vidioc_g_fbuf(struct file *file, void *fh,
835                          struct v4l2_framebuffer *a)
836 {
837         /* The video overlay must stay within the framebuffer and can't be
838          * positioned independently.
839          */
840         struct bm2835_mmal_dev *dev = video_drvdata(file);
841         struct vchiq_mmal_port *preview_port =
842                     &dev->component[MMAL_COMPONENT_CAMERA]->
843                     output[MMAL_CAMERA_PORT_PREVIEW];
844
845         a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
846                         V4L2_FBUF_CAP_GLOBAL_ALPHA;
847         a->flags = V4L2_FBUF_FLAG_OVERLAY;
848         a->fmt.width = preview_port->es.video.width;
849         a->fmt.height = preview_port->es.video.height;
850         a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
851         a->fmt.bytesperline = preview_port->es.video.width;
852         a->fmt.sizeimage = (preview_port->es.video.width *
853                                preview_port->es.video.height * 3) >> 1;
854         a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
855
856         return 0;
857 }
858
859 /* input ioctls */
860 static int vidioc_enum_input(struct file *file, void *priv,
861                              struct v4l2_input *inp)
862 {
863         /* only a single camera input */
864         if (inp->index != 0)
865                 return -EINVAL;
866
867         inp->type = V4L2_INPUT_TYPE_CAMERA;
868         sprintf((char *)inp->name, "Camera %u", inp->index);
869         return 0;
870 }
871
872 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
873 {
874         *i = 0;
875         return 0;
876 }
877
878 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
879 {
880         if (i != 0)
881                 return -EINVAL;
882
883         return 0;
884 }
885
886 /* capture ioctls */
887 static int vidioc_querycap(struct file *file, void *priv,
888                            struct v4l2_capability *cap)
889 {
890         struct bm2835_mmal_dev *dev = video_drvdata(file);
891         u32 major;
892         u32 minor;
893
894         vchiq_mmal_version(dev->instance, &major, &minor);
895
896         strcpy((char *)cap->driver, "bm2835 mmal");
897         snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
898                  major, minor);
899
900         snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
901                  "platform:%s", dev->v4l2_dev.name);
902         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
903             V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
904         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
905
906         return 0;
907 }
908
909 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
910                                    struct v4l2_fmtdesc *f)
911 {
912         struct mmal_fmt *fmt;
913
914         if (f->index >= ARRAY_SIZE(formats))
915                 return -EINVAL;
916
917         fmt = &formats[f->index];
918
919         strlcpy((char *)f->description, fmt->name, sizeof(f->description));
920         f->pixelformat = fmt->fourcc;
921         f->flags = fmt->flags;
922
923         return 0;
924 }
925
926 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
927                                 struct v4l2_format *f)
928 {
929         struct bm2835_mmal_dev *dev = video_drvdata(file);
930
931         f->fmt.pix.width = dev->capture.width;
932         f->fmt.pix.height = dev->capture.height;
933         f->fmt.pix.field = V4L2_FIELD_NONE;
934         f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
935         f->fmt.pix.bytesperline = dev->capture.stride;
936         f->fmt.pix.sizeimage = dev->capture.buffersize;
937
938         if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
939                 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
940         else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
941                 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
942         else
943                 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
944         f->fmt.pix.priv = 0;
945
946         v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
947                              __func__);
948         return 0;
949 }
950
951 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
952                                   struct v4l2_format *f)
953 {
954         struct bm2835_mmal_dev *dev = video_drvdata(file);
955         struct mmal_fmt *mfmt;
956
957         mfmt = get_format(f);
958         if (!mfmt) {
959                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
960                          "Fourcc format (0x%08x) unknown.\n",
961                          f->fmt.pix.pixelformat);
962                 f->fmt.pix.pixelformat = formats[0].fourcc;
963                 mfmt = get_format(f);
964         }
965
966         f->fmt.pix.field = V4L2_FIELD_NONE;
967
968         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
969                  "Clipping/aligning %dx%d format %08X\n",
970                  f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
971
972         v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
973                               &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
974                               1, 0);
975         f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
976         if (!mfmt->remove_padding) {
977                 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
978                 /* GPU isn't removing padding, so stride is aligned to 32 */
979                 f->fmt.pix.bytesperline =
980                         (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
981                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
982                          "Not removing padding, so bytes/line = %d, "
983                          "(align_mask %d)\n",
984                          f->fmt.pix.bytesperline, align_mask);
985         }
986
987         /* Image buffer has to be padded to allow for alignment, even though
988          * we sometimes then remove that padding before delivering the buffer.
989          */
990         f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
991                         (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
992
993         if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
994             f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
995                 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
996
997         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
998                 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
999         else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
1000                 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1001         else
1002                 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1003         f->fmt.pix.priv = 0;
1004
1005         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1006                  "Now %dx%d format %08X\n",
1007                 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1008
1009         v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1010                              __func__);
1011         return 0;
1012 }
1013
1014 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1015                                  struct v4l2_format *f)
1016 {
1017         int ret;
1018         struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1019         struct vchiq_mmal_component *encode_component = NULL;
1020         struct mmal_fmt *mfmt = get_format(f);
1021         u32 remove_padding;
1022
1023         if (!mfmt)
1024                 return -EINVAL;
1025
1026         if (dev->capture.encode_component) {
1027                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1028                          "vid_cap - disconnect previous tunnel\n");
1029
1030                 /* Disconnect any previous connection */
1031                 vchiq_mmal_port_connect_tunnel(dev->instance,
1032                                                dev->capture.camera_port, NULL);
1033                 dev->capture.camera_port = NULL;
1034                 ret = vchiq_mmal_component_disable(dev->instance,
1035                                                    dev->capture.
1036                                                    encode_component);
1037                 if (ret)
1038                         v4l2_err(&dev->v4l2_dev,
1039                                  "Failed to disable encode component %d\n",
1040                                  ret);
1041
1042                 dev->capture.encode_component = NULL;
1043         }
1044         /* format dependent port setup */
1045         switch (mfmt->mmal_component) {
1046         case MMAL_COMPONENT_CAMERA:
1047                 /* Make a further decision on port based on resolution */
1048                 if (f->fmt.pix.width <= max_video_width
1049                     && f->fmt.pix.height <= max_video_height)
1050                         camera_port = port =
1051                             &dev->component[MMAL_COMPONENT_CAMERA]->
1052                             output[MMAL_CAMERA_PORT_VIDEO];
1053                 else
1054                         camera_port = port =
1055                             &dev->component[MMAL_COMPONENT_CAMERA]->
1056                             output[MMAL_CAMERA_PORT_CAPTURE];
1057                 break;
1058         case MMAL_COMPONENT_IMAGE_ENCODE:
1059                 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1060                 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1061                 camera_port =
1062                     &dev->component[MMAL_COMPONENT_CAMERA]->
1063                     output[MMAL_CAMERA_PORT_CAPTURE];
1064                 break;
1065         case MMAL_COMPONENT_VIDEO_ENCODE:
1066                 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1067                 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1068                 camera_port =
1069                     &dev->component[MMAL_COMPONENT_CAMERA]->
1070                     output[MMAL_CAMERA_PORT_VIDEO];
1071                 break;
1072         default:
1073                 break;
1074         }
1075
1076         if (!port)
1077                 return -EINVAL;
1078
1079         if (encode_component)
1080                 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1081         else
1082                 camera_port->format.encoding = mfmt->mmal;
1083
1084         if (dev->rgb_bgr_swapped) {
1085                 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1086                         camera_port->format.encoding = MMAL_ENCODING_BGR24;
1087                 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1088                         camera_port->format.encoding = MMAL_ENCODING_RGB24;
1089         }
1090
1091         remove_padding = mfmt->remove_padding;
1092         vchiq_mmal_port_parameter_set(dev->instance,
1093                                       camera_port,
1094                                       MMAL_PARAMETER_NO_IMAGE_PADDING,
1095                                       &remove_padding, sizeof(remove_padding));
1096
1097         camera_port->format.encoding_variant = 0;
1098         camera_port->es.video.width = f->fmt.pix.width;
1099         camera_port->es.video.height = f->fmt.pix.height;
1100         camera_port->es.video.crop.x = 0;
1101         camera_port->es.video.crop.y = 0;
1102         camera_port->es.video.crop.width = f->fmt.pix.width;
1103         camera_port->es.video.crop.height = f->fmt.pix.height;
1104         camera_port->es.video.frame_rate.num = 0;
1105         camera_port->es.video.frame_rate.den = 1;
1106         camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1107
1108         ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1109
1110         if (!ret
1111             && camera_port ==
1112             &dev->component[MMAL_COMPONENT_CAMERA]->
1113             output[MMAL_CAMERA_PORT_VIDEO]) {
1114                 bool overlay_enabled =
1115                     !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1116                 struct vchiq_mmal_port *preview_port =
1117                     &dev->component[MMAL_COMPONENT_CAMERA]->
1118                     output[MMAL_CAMERA_PORT_PREVIEW];
1119                 /* Preview and encode ports need to match on resolution */
1120                 if (overlay_enabled) {
1121                         /* Need to disable the overlay before we can update
1122                          * the resolution
1123                          */
1124                         ret =
1125                             vchiq_mmal_port_disable(dev->instance,
1126                                                     preview_port);
1127                         if (!ret)
1128                                 ret =
1129                                     vchiq_mmal_port_connect_tunnel(
1130                                                 dev->instance,
1131                                                 preview_port,
1132                                                 NULL);
1133                 }
1134                 preview_port->es.video.width = f->fmt.pix.width;
1135                 preview_port->es.video.height = f->fmt.pix.height;
1136                 preview_port->es.video.crop.x = 0;
1137                 preview_port->es.video.crop.y = 0;
1138                 preview_port->es.video.crop.width = f->fmt.pix.width;
1139                 preview_port->es.video.crop.height = f->fmt.pix.height;
1140                 preview_port->es.video.frame_rate.num =
1141                                           dev->capture.timeperframe.denominator;
1142                 preview_port->es.video.frame_rate.den =
1143                                           dev->capture.timeperframe.numerator;
1144                 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1145                 if (overlay_enabled) {
1146                         ret = vchiq_mmal_port_connect_tunnel(
1147                                 dev->instance,
1148                                 preview_port,
1149                                 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1150                         if (!ret)
1151                                 ret = vchiq_mmal_port_enable(dev->instance,
1152                                                              preview_port,
1153                                                              NULL);
1154                 }
1155         }
1156
1157         if (ret) {
1158                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1159                          "%s failed to set format %dx%d %08X\n", __func__,
1160                          f->fmt.pix.width, f->fmt.pix.height,
1161                          f->fmt.pix.pixelformat);
1162                 /* ensure capture is not going to be tried */
1163                 dev->capture.port = NULL;
1164         } else {
1165                 if (encode_component) {
1166                         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1167                                  "vid_cap - set up encode comp\n");
1168
1169                         /* configure buffering */
1170                         camera_port->current_buffer.size =
1171                             camera_port->recommended_buffer.size;
1172                         camera_port->current_buffer.num =
1173                             camera_port->recommended_buffer.num;
1174
1175                         ret =
1176                             vchiq_mmal_port_connect_tunnel(
1177                                         dev->instance,
1178                                         camera_port,
1179                                         &encode_component->input[0]);
1180                         if (ret) {
1181                                 v4l2_dbg(1, bcm2835_v4l2_debug,
1182                                          &dev->v4l2_dev,
1183                                          "%s failed to create connection\n",
1184                                          __func__);
1185                                 /* ensure capture is not going to be tried */
1186                                 dev->capture.port = NULL;
1187                         } else {
1188                                 port->es.video.width = f->fmt.pix.width;
1189                                 port->es.video.height = f->fmt.pix.height;
1190                                 port->es.video.crop.x = 0;
1191                                 port->es.video.crop.y = 0;
1192                                 port->es.video.crop.width = f->fmt.pix.width;
1193                                 port->es.video.crop.height = f->fmt.pix.height;
1194                                 port->es.video.frame_rate.num =
1195                                           dev->capture.timeperframe.denominator;
1196                                 port->es.video.frame_rate.den =
1197                                           dev->capture.timeperframe.numerator;
1198
1199                                 port->format.encoding = mfmt->mmal;
1200                                 port->format.encoding_variant = 0;
1201                                 /* Set any encoding specific parameters */
1202                                 switch (mfmt->mmal_component) {
1203                                 case MMAL_COMPONENT_VIDEO_ENCODE:
1204                                         port->format.bitrate =
1205                                             dev->capture.encode_bitrate;
1206                                         break;
1207                                 case MMAL_COMPONENT_IMAGE_ENCODE:
1208                                         /* Could set EXIF parameters here */
1209                                         break;
1210                                 default:
1211                                         break;
1212                                 }
1213                                 ret = vchiq_mmal_port_set_format(dev->instance,
1214                                                                  port);
1215                                 if (ret)
1216                                         v4l2_dbg(1, bcm2835_v4l2_debug,
1217                                                  &dev->v4l2_dev,
1218                                                  "%s failed to set format %dx%d fmt %08X\n",
1219                                                  __func__,
1220                                                  f->fmt.pix.width,
1221                                                  f->fmt.pix.height,
1222                                                  f->fmt.pix.pixelformat
1223                                                  );
1224                         }
1225
1226                         if (!ret) {
1227                                 ret = vchiq_mmal_component_enable(
1228                                                 dev->instance,
1229                                                 encode_component);
1230                                 if (ret) {
1231                                         v4l2_dbg(1, bcm2835_v4l2_debug,
1232                                                  &dev->v4l2_dev,
1233                                                  "%s Failed to enable encode components\n",
1234                                                  __func__);
1235                                 }
1236                         }
1237                         if (!ret) {
1238                                 /* configure buffering */
1239                                 port->current_buffer.num = 1;
1240                                 port->current_buffer.size =
1241                                     f->fmt.pix.sizeimage;
1242                                 if (port->format.encoding ==
1243                                     MMAL_ENCODING_JPEG) {
1244                                         v4l2_dbg(1, bcm2835_v4l2_debug,
1245                                                  &dev->v4l2_dev,
1246                                                  "JPG - buf size now %d was %d\n",
1247                                                  f->fmt.pix.sizeimage,
1248                                                  port->current_buffer.size);
1249                                         port->current_buffer.size =
1250                                             (f->fmt.pix.sizeimage <
1251                                              (100 << 10))
1252                                             ? (100 << 10)
1253                                             : f->fmt.pix.sizeimage;
1254                                 }
1255                                 v4l2_dbg(1, bcm2835_v4l2_debug,
1256                                          &dev->v4l2_dev,
1257                                          "vid_cap - cur_buf.size set to %d\n",
1258                                          f->fmt.pix.sizeimage);
1259                                 port->current_buffer.alignment = 0;
1260                         }
1261                 } else {
1262                         /* configure buffering */
1263                         camera_port->current_buffer.num = 1;
1264                         camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1265                         camera_port->current_buffer.alignment = 0;
1266                 }
1267
1268                 if (!ret) {
1269                         dev->capture.fmt = mfmt;
1270                         dev->capture.stride = f->fmt.pix.bytesperline;
1271                         dev->capture.width = camera_port->es.video.crop.width;
1272                         dev->capture.height = camera_port->es.video.crop.height;
1273                         dev->capture.buffersize = port->current_buffer.size;
1274
1275                         /* select port for capture */
1276                         dev->capture.port = port;
1277                         dev->capture.camera_port = camera_port;
1278                         dev->capture.encode_component = encode_component;
1279                         v4l2_dbg(1, bcm2835_v4l2_debug,
1280                                  &dev->v4l2_dev,
1281                                 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1282                                 port->format.encoding,
1283                                 dev->capture.width, dev->capture.height,
1284                                 dev->capture.stride, dev->capture.buffersize);
1285                 }
1286         }
1287
1288         /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1289         return ret;
1290 }
1291
1292 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1293                                 struct v4l2_format *f)
1294 {
1295         int ret;
1296         struct bm2835_mmal_dev *dev = video_drvdata(file);
1297         struct mmal_fmt *mfmt;
1298
1299         /* try the format to set valid parameters */
1300         ret = vidioc_try_fmt_vid_cap(file, priv, f);
1301         if (ret) {
1302                 v4l2_err(&dev->v4l2_dev,
1303                          "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1304                 return ret;
1305         }
1306
1307         /* if a capture is running refuse to set format */
1308         if (vb2_is_busy(&dev->capture.vb_vidq)) {
1309                 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1310                 return -EBUSY;
1311         }
1312
1313         /* If the format is unsupported v4l2 says we should switch to
1314          * a supported one and not return an error.
1315          */
1316         mfmt = get_format(f);
1317         if (!mfmt) {
1318                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1319                          "Fourcc format (0x%08x) unknown.\n",
1320                          f->fmt.pix.pixelformat);
1321                 f->fmt.pix.pixelformat = formats[0].fourcc;
1322                 mfmt = get_format(f);
1323         }
1324
1325         ret = mmal_setup_components(dev, f);
1326         if (ret != 0) {
1327                 v4l2_err(&dev->v4l2_dev,
1328                          "%s: failed to setup mmal components: %d\n",
1329                          __func__, ret);
1330                 ret = -EINVAL;
1331         }
1332
1333         return ret;
1334 }
1335
1336 static int vidioc_enum_framesizes(struct file *file, void *fh,
1337                            struct v4l2_frmsizeenum *fsize)
1338 {
1339         struct bm2835_mmal_dev *dev = video_drvdata(file);
1340         static const struct v4l2_frmsize_stepwise sizes = {
1341                 MIN_WIDTH, 0, 2,
1342                 MIN_HEIGHT, 0, 2
1343         };
1344         int i;
1345
1346         if (fsize->index)
1347                 return -EINVAL;
1348         for (i = 0; i < ARRAY_SIZE(formats); i++)
1349                 if (formats[i].fourcc == fsize->pixel_format)
1350                         break;
1351         if (i == ARRAY_SIZE(formats))
1352                 return -EINVAL;
1353         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1354         fsize->stepwise = sizes;
1355         fsize->stepwise.max_width = dev->max_width;
1356         fsize->stepwise.max_height = dev->max_height;
1357         return 0;
1358 }
1359
1360 /* timeperframe is arbitrary and continuous */
1361 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1362                                       struct v4l2_frmivalenum *fival)
1363 {
1364         struct bm2835_mmal_dev *dev = video_drvdata(file);
1365         int i;
1366
1367         if (fival->index)
1368                 return -EINVAL;
1369
1370         for (i = 0; i < ARRAY_SIZE(formats); i++)
1371                 if (formats[i].fourcc == fival->pixel_format)
1372                         break;
1373         if (i == ARRAY_SIZE(formats))
1374                 return -EINVAL;
1375
1376         /* regarding width & height - we support any within range */
1377         if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1378             fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1379                 return -EINVAL;
1380
1381         fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1382
1383         /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1384         fival->stepwise.min  = tpf_min;
1385         fival->stepwise.max  = tpf_max;
1386         fival->stepwise.step = (struct v4l2_fract) {1, 1};
1387
1388         return 0;
1389 }
1390
1391 static int vidioc_g_parm(struct file *file, void *priv,
1392                          struct v4l2_streamparm *parm)
1393 {
1394         struct bm2835_mmal_dev *dev = video_drvdata(file);
1395
1396         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1397                 return -EINVAL;
1398
1399         parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1400         parm->parm.capture.timeperframe = dev->capture.timeperframe;
1401         parm->parm.capture.readbuffers  = 1;
1402         return 0;
1403 }
1404
1405 #define FRACT_CMP(a, OP, b)     \
1406         ((u64)(a).numerator * (b).denominator  OP  \
1407          (u64)(b).numerator * (a).denominator)
1408
1409 static int vidioc_s_parm(struct file *file, void *priv,
1410                          struct v4l2_streamparm *parm)
1411 {
1412         struct bm2835_mmal_dev *dev = video_drvdata(file);
1413         struct v4l2_fract tpf;
1414
1415         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1416                 return -EINVAL;
1417
1418         tpf = parm->parm.capture.timeperframe;
1419
1420         /* tpf: {*, 0} resets timing; clip to [min, max]*/
1421         tpf = tpf.denominator ? tpf : tpf_default;
1422         tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1423         tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1424
1425         dev->capture.timeperframe = tpf;
1426         parm->parm.capture.timeperframe = tpf;
1427         parm->parm.capture.readbuffers  = 1;
1428         parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1429
1430         set_framerate_params(dev);
1431
1432         return 0;
1433 }
1434
1435 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1436         /* overlay */
1437         .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1438         .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1439         .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1440         .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1441         .vidioc_overlay = vidioc_overlay,
1442         .vidioc_g_fbuf = vidioc_g_fbuf,
1443
1444         /* inputs */
1445         .vidioc_enum_input = vidioc_enum_input,
1446         .vidioc_g_input = vidioc_g_input,
1447         .vidioc_s_input = vidioc_s_input,
1448
1449         /* capture */
1450         .vidioc_querycap = vidioc_querycap,
1451         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1452         .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1453         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1454         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1455
1456         /* buffer management */
1457         .vidioc_reqbufs = vb2_ioctl_reqbufs,
1458         .vidioc_create_bufs = vb2_ioctl_create_bufs,
1459         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1460         .vidioc_querybuf = vb2_ioctl_querybuf,
1461         .vidioc_qbuf = vb2_ioctl_qbuf,
1462         .vidioc_dqbuf = vb2_ioctl_dqbuf,
1463         .vidioc_enum_framesizes = vidioc_enum_framesizes,
1464         .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1465         .vidioc_g_parm        = vidioc_g_parm,
1466         .vidioc_s_parm        = vidioc_s_parm,
1467         .vidioc_streamon = vb2_ioctl_streamon,
1468         .vidioc_streamoff = vb2_ioctl_streamoff,
1469
1470         .vidioc_log_status = v4l2_ctrl_log_status,
1471         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1472         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1473 };
1474
1475 /* ------------------------------------------------------------------
1476  *      Driver init/finalise
1477  * ------------------------------------------------------------------
1478  */
1479
1480 static const struct v4l2_file_operations camera0_fops = {
1481         .owner = THIS_MODULE,
1482         .open = v4l2_fh_open,
1483         .release = vb2_fop_release,
1484         .read = vb2_fop_read,
1485         .poll = vb2_fop_poll,
1486         .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1487         .mmap = vb2_fop_mmap,
1488 };
1489
1490 static const struct video_device vdev_template = {
1491         .name = "camera0",
1492         .fops = &camera0_fops,
1493         .ioctl_ops = &camera0_ioctl_ops,
1494         .release = video_device_release_empty,
1495 };
1496
1497 /* Returns the number of cameras, and also the max resolution supported
1498  * by those cameras.
1499  */
1500 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1501                            unsigned int resolutions[][2], int num_resolutions)
1502 {
1503         int ret;
1504         struct vchiq_mmal_component  *cam_info_component;
1505         struct mmal_parameter_camera_info_t cam_info = {0};
1506         u32 param_size = sizeof(cam_info);
1507         int i;
1508
1509         /* create a camera_info component */
1510         ret = vchiq_mmal_component_init(instance, "camera_info",
1511                                         &cam_info_component);
1512         if (ret < 0)
1513                 /* Unusual failure - let's guess one camera. */
1514                 return 1;
1515
1516         if (vchiq_mmal_port_parameter_get(instance,
1517                                           &cam_info_component->control,
1518                                           MMAL_PARAMETER_CAMERA_INFO,
1519                                           &cam_info,
1520                                           &param_size)) {
1521                 pr_info("Failed to get camera info\n");
1522         }
1523         for (i = 0;
1524              i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1525              i++) {
1526                 resolutions[i][0] = cam_info.cameras[i].max_width;
1527                 resolutions[i][1] = cam_info.cameras[i].max_height;
1528         }
1529
1530         vchiq_mmal_component_finalise(instance,
1531                                       cam_info_component);
1532
1533         return cam_info.num_cameras;
1534 }
1535
1536 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1537                                  struct vchiq_mmal_component *camera,
1538                                  struct bm2835_mmal_dev *dev)
1539 {
1540         int ret;
1541         struct mmal_parameter_camera_config cam_config = {
1542                 .max_stills_w = dev->max_width,
1543                 .max_stills_h = dev->max_height,
1544                 .stills_yuv422 = 1,
1545                 .one_shot_stills = 1,
1546                 .max_preview_video_w = (max_video_width > 1920) ?
1547                                                 max_video_width : 1920,
1548                 .max_preview_video_h = (max_video_height > 1088) ?
1549                                                 max_video_height : 1088,
1550                 .num_preview_video_frames = 3,
1551                 .stills_capture_circular_buffer_height = 0,
1552                 .fast_preview_resume = 0,
1553                 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1554         };
1555
1556         ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1557                                             MMAL_PARAMETER_CAMERA_CONFIG,
1558                                             &cam_config, sizeof(cam_config));
1559         return ret;
1560 }
1561
1562 #define MAX_SUPPORTED_ENCODINGS 20
1563
1564 /* MMAL instance and component init */
1565 static int mmal_init(struct bm2835_mmal_dev *dev)
1566 {
1567         int ret;
1568         struct mmal_es_format_local *format;
1569         u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1570         u32 param_size;
1571         struct vchiq_mmal_component  *camera;
1572
1573         ret = vchiq_mmal_init(&dev->instance);
1574         if (ret < 0) {
1575                 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1576                          __func__, ret);
1577                 return ret;
1578         }
1579
1580         /* get the camera component ready */
1581         ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1582                                         &dev->component[MMAL_COMPONENT_CAMERA]);
1583         if (ret < 0)
1584                 goto unreg_mmal;
1585
1586         camera = dev->component[MMAL_COMPONENT_CAMERA];
1587         if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1588                 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1589                          __func__, camera->outputs, MMAL_CAMERA_PORT_COUNT);
1590                 ret = -EINVAL;
1591                 goto unreg_camera;
1592         }
1593
1594         ret = set_camera_parameters(dev->instance,
1595                                     camera,
1596                                     dev);
1597         if (ret < 0) {
1598                 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1599                          __func__, ret);
1600                 goto unreg_camera;
1601         }
1602
1603         /* There was an error in the firmware that meant the camera component
1604          * produced BGR instead of RGB.
1605          * This is now fixed, but in order to support the old firmwares, we
1606          * have to check.
1607          */
1608         dev->rgb_bgr_swapped = true;
1609         param_size = sizeof(supported_encodings);
1610         ret = vchiq_mmal_port_parameter_get(dev->instance,
1611                                             &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1612                                             MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1613                                             &supported_encodings,
1614                                             &param_size);
1615         if (ret == 0) {
1616                 int i;
1617
1618                 for (i = 0; i < param_size / sizeof(u32); i++) {
1619                         if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1620                                 /* Found BGR24 first - old firmware. */
1621                                 break;
1622                         }
1623                         if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1624                                 /* Found RGB24 first
1625                                  * new firmware, so use RGB24.
1626                                  */
1627                                 dev->rgb_bgr_swapped = false;
1628                         break;
1629                         }
1630                 }
1631         }
1632         format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1633
1634         format->encoding = MMAL_ENCODING_OPAQUE;
1635         format->encoding_variant = MMAL_ENCODING_I420;
1636
1637         format->es->video.width = 1024;
1638         format->es->video.height = 768;
1639         format->es->video.crop.x = 0;
1640         format->es->video.crop.y = 0;
1641         format->es->video.crop.width = 1024;
1642         format->es->video.crop.height = 768;
1643         format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1644         format->es->video.frame_rate.den = 1;
1645
1646         format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1647
1648         format->encoding = MMAL_ENCODING_OPAQUE;
1649         format->encoding_variant = MMAL_ENCODING_I420;
1650
1651         format->es->video.width = 1024;
1652         format->es->video.height = 768;
1653         format->es->video.crop.x = 0;
1654         format->es->video.crop.y = 0;
1655         format->es->video.crop.width = 1024;
1656         format->es->video.crop.height = 768;
1657         format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1658         format->es->video.frame_rate.den = 1;
1659
1660         format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1661
1662         format->encoding = MMAL_ENCODING_OPAQUE;
1663
1664         format->es->video.width = 2592;
1665         format->es->video.height = 1944;
1666         format->es->video.crop.x = 0;
1667         format->es->video.crop.y = 0;
1668         format->es->video.crop.width = 2592;
1669         format->es->video.crop.height = 1944;
1670         format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1671         format->es->video.frame_rate.den = 1;
1672
1673         dev->capture.width = format->es->video.width;
1674         dev->capture.height = format->es->video.height;
1675         dev->capture.fmt = &formats[0];
1676         dev->capture.encode_component = NULL;
1677         dev->capture.timeperframe = tpf_default;
1678         dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1679         dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1680
1681         /* get the preview component ready */
1682         ret = vchiq_mmal_component_init(
1683                         dev->instance, "ril.video_render",
1684                         &dev->component[MMAL_COMPONENT_PREVIEW]);
1685         if (ret < 0)
1686                 goto unreg_camera;
1687
1688         if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1689                 ret = -EINVAL;
1690                 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1691                          __func__, dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1692                 goto unreg_preview;
1693         }
1694
1695         /* get the image encoder component ready */
1696         ret = vchiq_mmal_component_init(
1697                 dev->instance, "ril.image_encode",
1698                 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1699         if (ret < 0)
1700                 goto unreg_preview;
1701
1702         if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1703                 ret = -EINVAL;
1704                 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1705                          __func__, dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1706                          1);
1707                 goto unreg_image_encoder;
1708         }
1709
1710         /* get the video encoder component ready */
1711         ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1712                                         &dev->
1713                                         component[MMAL_COMPONENT_VIDEO_ENCODE]);
1714         if (ret < 0)
1715                 goto unreg_image_encoder;
1716
1717         if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1718                 ret = -EINVAL;
1719                 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1720                          __func__, dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1721                          1);
1722                 goto unreg_vid_encoder;
1723         }
1724
1725         {
1726                 struct vchiq_mmal_port *encoder_port =
1727                         &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1728                 encoder_port->format.encoding = MMAL_ENCODING_H264;
1729                 ret = vchiq_mmal_port_set_format(dev->instance,
1730                                                  encoder_port);
1731         }
1732
1733         {
1734                 unsigned int enable = 1;
1735
1736                 vchiq_mmal_port_parameter_set(
1737                         dev->instance,
1738                         &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1739                         MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1740                         &enable, sizeof(enable));
1741
1742                 vchiq_mmal_port_parameter_set(dev->instance,
1743                                               &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1744                                               MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1745                                               &enable,
1746                                               sizeof(enable));
1747         }
1748         ret = bm2835_mmal_set_all_camera_controls(dev);
1749         if (ret < 0) {
1750                 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1751                          __func__, ret);
1752                 goto unreg_vid_encoder;
1753         }
1754
1755         return 0;
1756
1757 unreg_vid_encoder:
1758         pr_err("Cleanup: Destroy video encoder\n");
1759         vchiq_mmal_component_finalise(
1760                 dev->instance,
1761                 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1762
1763 unreg_image_encoder:
1764         pr_err("Cleanup: Destroy image encoder\n");
1765         vchiq_mmal_component_finalise(
1766                 dev->instance,
1767                 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1768
1769 unreg_preview:
1770         pr_err("Cleanup: Destroy video render\n");
1771         vchiq_mmal_component_finalise(dev->instance,
1772                                       dev->component[MMAL_COMPONENT_PREVIEW]);
1773
1774 unreg_camera:
1775         pr_err("Cleanup: Destroy camera\n");
1776         vchiq_mmal_component_finalise(dev->instance,
1777                                       dev->component[MMAL_COMPONENT_CAMERA]);
1778
1779 unreg_mmal:
1780         vchiq_mmal_finalise(dev->instance);
1781         return ret;
1782 }
1783
1784 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1785                                    struct video_device *vfd)
1786 {
1787         int ret;
1788
1789         *vfd = vdev_template;
1790
1791         vfd->v4l2_dev = &dev->v4l2_dev;
1792
1793         vfd->lock = &dev->mutex;
1794
1795         vfd->queue = &dev->capture.vb_vidq;
1796
1797         /* video device needs to be able to access instance data */
1798         video_set_drvdata(vfd, dev);
1799
1800         ret = video_register_device(vfd,
1801                                     VFL_TYPE_GRABBER,
1802                                     video_nr[dev->camera_num]);
1803         if (ret < 0)
1804                 return ret;
1805
1806         v4l2_info(vfd->v4l2_dev,
1807                   "V4L2 device registered as %s - stills mode > %dx%d\n",
1808                   video_device_node_name(vfd),
1809                   max_video_width, max_video_height);
1810
1811         return 0;
1812 }
1813
1814 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1815 {
1816         if (!dev)
1817                 return;
1818
1819         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1820                   video_device_node_name(&dev->vdev));
1821
1822         video_unregister_device(&dev->vdev);
1823
1824         if (dev->capture.encode_component) {
1825                 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1826                          "mmal_exit - disconnect tunnel\n");
1827                 vchiq_mmal_port_connect_tunnel(dev->instance,
1828                                                dev->capture.camera_port, NULL);
1829                 vchiq_mmal_component_disable(dev->instance,
1830                                              dev->capture.encode_component);
1831         }
1832         vchiq_mmal_component_disable(dev->instance,
1833                                      dev->component[MMAL_COMPONENT_CAMERA]);
1834
1835         vchiq_mmal_component_finalise(dev->instance,
1836                                       dev->
1837                                       component[MMAL_COMPONENT_VIDEO_ENCODE]);
1838
1839         vchiq_mmal_component_finalise(dev->instance,
1840                                       dev->
1841                                       component[MMAL_COMPONENT_IMAGE_ENCODE]);
1842
1843         vchiq_mmal_component_finalise(dev->instance,
1844                                       dev->component[MMAL_COMPONENT_PREVIEW]);
1845
1846         vchiq_mmal_component_finalise(dev->instance,
1847                                       dev->component[MMAL_COMPONENT_CAMERA]);
1848
1849         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1850
1851         v4l2_device_unregister(&dev->v4l2_dev);
1852
1853         kfree(dev);
1854 }
1855
1856 static struct v4l2_format default_v4l2_format = {
1857         .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1858         .fmt.pix.width = 1024,
1859         .fmt.pix.bytesperline = 0,
1860         .fmt.pix.height = 768,
1861         .fmt.pix.sizeimage = 1024 * 768,
1862 };
1863
1864 static int bcm2835_mmal_probe(struct platform_device *pdev)
1865 {
1866         int ret;
1867         struct bm2835_mmal_dev *dev;
1868         struct vb2_queue *q;
1869         int camera;
1870         unsigned int num_cameras;
1871         struct vchiq_mmal_instance *instance;
1872         unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1873         int i;
1874
1875         ret = vchiq_mmal_init(&instance);
1876         if (ret < 0)
1877                 return ret;
1878
1879         num_cameras = get_num_cameras(instance,
1880                                       resolutions,
1881                                       MAX_BCM2835_CAMERAS);
1882
1883         if (num_cameras < 1) {
1884                 ret = -ENODEV;
1885                 goto cleanup_mmal;
1886         }
1887
1888         if (num_cameras > MAX_BCM2835_CAMERAS)
1889                 num_cameras = MAX_BCM2835_CAMERAS;
1890
1891         for (camera = 0; camera < num_cameras; camera++) {
1892                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1893                 if (!dev) {
1894                         ret = -ENOMEM;
1895                         goto cleanup_gdev;
1896                 }
1897
1898                 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1899                 mutex_init(&dev->mutex);
1900                 dev->camera_num = camera;
1901                 dev->max_width = resolutions[camera][0];
1902                 dev->max_height = resolutions[camera][1];
1903
1904                 /* setup device defaults */
1905                 dev->overlay.w.left = 150;
1906                 dev->overlay.w.top = 50;
1907                 dev->overlay.w.width = 1024;
1908                 dev->overlay.w.height = 768;
1909                 dev->overlay.clipcount = 0;
1910                 dev->overlay.field = V4L2_FIELD_NONE;
1911                 dev->overlay.global_alpha = 255;
1912
1913                 dev->capture.fmt = &formats[3]; /* JPEG */
1914
1915                 /* v4l device registration */
1916                 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1917                          "%s", BM2835_MMAL_MODULE_NAME);
1918                 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1919                 if (ret) {
1920                         dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1921                                 __func__, ret);
1922                         goto free_dev;
1923                 }
1924
1925                 /* setup v4l controls */
1926                 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1927                 if (ret < 0) {
1928                         v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1929                                 __func__, ret);
1930                         goto unreg_dev;
1931                 }
1932                 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1933
1934                 /* mmal init */
1935                 dev->instance = instance;
1936                 ret = mmal_init(dev);
1937                 if (ret < 0) {
1938                         v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1939                                 __func__, ret);
1940                         goto unreg_dev;
1941                 }
1942                 /* initialize queue */
1943                 q = &dev->capture.vb_vidq;
1944                 memset(q, 0, sizeof(*q));
1945                 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1946                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1947                 q->drv_priv = dev;
1948                 q->buf_struct_size = sizeof(struct mmal_buffer);
1949                 q->ops = &bm2835_mmal_video_qops;
1950                 q->mem_ops = &vb2_vmalloc_memops;
1951                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1952                 q->lock = &dev->mutex;
1953                 ret = vb2_queue_init(q);
1954                 if (ret < 0)
1955                         goto unreg_dev;
1956
1957                 /* initialise video devices */
1958                 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1959                 if (ret < 0) {
1960                         v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1961                                 __func__, ret);
1962                         goto unreg_dev;
1963                 }
1964
1965                 /* Really want to call vidioc_s_fmt_vid_cap with the default
1966                  * format, but currently the APIs don't join up.
1967                  */
1968                 ret = mmal_setup_components(dev, &default_v4l2_format);
1969                 if (ret < 0) {
1970                         v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1971                                 __func__, ret);
1972                         goto unreg_dev;
1973                 }
1974
1975                 v4l2_info(&dev->v4l2_dev,
1976                           "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1977                           BM2835_MMAL_VERSION);
1978
1979                 gdev[camera] = dev;
1980         }
1981         return 0;
1982
1983 unreg_dev:
1984         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1985         v4l2_device_unregister(&dev->v4l2_dev);
1986
1987 free_dev:
1988         kfree(dev);
1989
1990 cleanup_gdev:
1991         for (i = 0; i < camera; i++) {
1992                 bcm2835_cleanup_instance(gdev[i]);
1993                 gdev[i] = NULL;
1994         }
1995
1996 cleanup_mmal:
1997         vchiq_mmal_finalise(instance);
1998
1999         return ret;
2000 }
2001
2002 static int bcm2835_mmal_remove(struct platform_device *pdev)
2003 {
2004         int camera;
2005         struct vchiq_mmal_instance *instance = gdev[0]->instance;
2006
2007         for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2008                 bcm2835_cleanup_instance(gdev[camera]);
2009                 gdev[camera] = NULL;
2010         }
2011         vchiq_mmal_finalise(instance);
2012
2013         return 0;
2014 }
2015
2016 static struct platform_driver bcm2835_camera_driver = {
2017         .probe          = bcm2835_mmal_probe,
2018         .remove         = bcm2835_mmal_remove,
2019         .driver         = {
2020                 .name   = "bcm2835-camera",
2021         },
2022 };
2023
2024 module_platform_driver(bcm2835_camera_driver)
2025
2026 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2027 MODULE_AUTHOR("Vincent Sanders");
2028 MODULE_LICENSE("GPL");
2029 MODULE_VERSION(BM2835_MMAL_VERSION);
2030 MODULE_ALIAS("platform:bcm2835-camera");