Merge remote-tracking branch 'remotes/tegra/android-tegra-2.6.36-honeycomb-mr1' into...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / soc_camera.c
1 /*
2  * camera image capture (abstract) bus driver
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This driver provides an interface between platform-specific camera
7  * busses and camera devices. It should be used if the camera is
8  * connected not over a "proper" bus like PCI or USB, but over a
9  * special bus, like, for example, the Quick Capture interface on PXA270
10  * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11  * It can handle multiple cameras and / or multiple busses, which can
12  * be used, e.g., in stereo-vision applications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vmalloc.h>
30
31 #include <media/soc_camera.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-dev.h>
35 #include <media/videobuf-core.h>
36 #include <media/soc_mediabus.h>
37
38 /* Default to VGA resolution */
39 #define DEFAULT_WIDTH   640
40 #define DEFAULT_HEIGHT  480
41
42 static LIST_HEAD(hosts);
43 static LIST_HEAD(devices);
44 static DEFINE_MUTEX(list_lock);         /* Protects the list of hosts */
45
46 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
47         struct soc_camera_device *icd, unsigned int fourcc)
48 {
49         unsigned int i;
50
51         for (i = 0; i < icd->num_user_formats; i++)
52                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
53                         return icd->user_formats + i;
54         return NULL;
55 }
56 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
57
58 /**
59  * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
60  * @icl:        camera platform parameters
61  * @flags:      flags to be inverted according to platform configuration
62  * @return:     resulting flags
63  */
64 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
65                                             unsigned long flags)
66 {
67         unsigned long f;
68
69         /* If only one of the two polarities is supported, switch to the opposite */
70         if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
71                 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
72                 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
73                         flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
74         }
75
76         if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
77                 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
78                 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
79                         flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
80         }
81
82         if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
83                 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
84                 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
85                         flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
86         }
87
88         return flags;
89 }
90 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
91
92 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
93                                       struct v4l2_format *f)
94 {
95         struct soc_camera_file *icf = file->private_data;
96         struct soc_camera_device *icd = icf->icd;
97         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
98
99         WARN_ON(priv != file->private_data);
100
101         /* limit format to hardware capabilities */
102         return ici->ops->try_fmt(icd, f);
103 }
104
105 static int soc_camera_enum_input(struct file *file, void *priv,
106                                  struct v4l2_input *inp)
107 {
108         struct soc_camera_file *icf = file->private_data;
109         struct soc_camera_device *icd = icf->icd;
110         int ret = 0;
111
112         if (inp->index != 0)
113                 return -EINVAL;
114
115         if (icd->ops->enum_input)
116                 ret = icd->ops->enum_input(icd, inp);
117         else {
118                 /* default is camera */
119                 inp->type = V4L2_INPUT_TYPE_CAMERA;
120                 inp->std  = V4L2_STD_UNKNOWN;
121                 strcpy(inp->name, "Camera");
122         }
123
124         return ret;
125 }
126
127 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
128 {
129         *i = 0;
130
131         return 0;
132 }
133
134 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
135 {
136         if (i > 0)
137                 return -EINVAL;
138
139         return 0;
140 }
141
142 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
143 {
144         struct soc_camera_file *icf = file->private_data;
145         struct soc_camera_device *icd = icf->icd;
146         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
147
148         return v4l2_subdev_call(sd, core, s_std, *a);
149 }
150
151 static int soc_camera_reqbufs(struct file *file, void *priv,
152                               struct v4l2_requestbuffers *p)
153 {
154         int ret;
155         struct soc_camera_file *icf = file->private_data;
156         struct soc_camera_device *icd = icf->icd;
157         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
158
159         WARN_ON(priv != file->private_data);
160
161         ret = videobuf_reqbufs(&icf->vb_vidq, p);
162         if (ret < 0)
163                 return ret;
164
165         return ici->ops->reqbufs(icf, p);
166 }
167
168 static int soc_camera_querybuf(struct file *file, void *priv,
169                                struct v4l2_buffer *p)
170 {
171         struct soc_camera_file *icf = file->private_data;
172
173         WARN_ON(priv != file->private_data);
174
175         return videobuf_querybuf(&icf->vb_vidq, p);
176 }
177
178 static int soc_camera_qbuf(struct file *file, void *priv,
179                            struct v4l2_buffer *p)
180 {
181         struct soc_camera_file *icf = file->private_data;
182
183         WARN_ON(priv != file->private_data);
184
185         return videobuf_qbuf(&icf->vb_vidq, p);
186 }
187
188 static int soc_camera_dqbuf(struct file *file, void *priv,
189                             struct v4l2_buffer *p)
190 {
191         struct soc_camera_file *icf = file->private_data;
192
193         WARN_ON(priv != file->private_data);
194
195         return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
196 }
197
198 /* Always entered with .video_lock held */
199 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
200 {
201         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
202         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
203         unsigned int i, fmts = 0, raw_fmts = 0;
204         int ret;
205         enum v4l2_mbus_pixelcode code;
206
207         while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
208                 raw_fmts++;
209
210         if (!ici->ops->get_formats)
211                 /*
212                  * Fallback mode - the host will have to serve all
213                  * sensor-provided formats one-to-one to the user
214                  */
215                 fmts = raw_fmts;
216         else
217                 /*
218                  * First pass - only count formats this host-sensor
219                  * configuration can provide
220                  */
221                 for (i = 0; i < raw_fmts; i++) {
222                         ret = ici->ops->get_formats(icd, i, NULL);
223                         if (ret < 0)
224                                 return ret;
225                         fmts += ret;
226                 }
227
228         if (!fmts)
229                 return -ENXIO;
230
231         icd->user_formats =
232                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
233         if (!icd->user_formats)
234                 return -ENOMEM;
235
236         icd->num_user_formats = fmts;
237
238         dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
239
240         /* Second pass - actually fill data formats */
241         fmts = 0;
242         for (i = 0; i < raw_fmts; i++)
243                 if (!ici->ops->get_formats) {
244                         v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
245                         icd->user_formats[i].host_fmt =
246                                 soc_mbus_get_fmtdesc(code);
247                         icd->user_formats[i].code = code;
248                 } else {
249                         ret = ici->ops->get_formats(icd, i,
250                                                     &icd->user_formats[fmts]);
251                         if (ret < 0)
252                                 goto egfmt;
253                         fmts += ret;
254                 }
255
256         icd->current_fmt = &icd->user_formats[0];
257
258         return 0;
259
260 egfmt:
261         icd->num_user_formats = 0;
262         vfree(icd->user_formats);
263         return ret;
264 }
265
266 /* Always entered with .video_lock held */
267 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
268 {
269         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
270
271         if (ici->ops->put_formats)
272                 ici->ops->put_formats(icd);
273         icd->current_fmt = NULL;
274         icd->num_user_formats = 0;
275         vfree(icd->user_formats);
276         icd->user_formats = NULL;
277 }
278
279 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
280         ((x) >> 24) & 0xff
281
282 /* Called with .vb_lock held, or from the first open(2), see comment there */
283 static int soc_camera_set_fmt(struct soc_camera_file *icf,
284                               struct v4l2_format *f)
285 {
286         struct soc_camera_device *icd = icf->icd;
287         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
288         struct v4l2_pix_format *pix = &f->fmt.pix;
289         int ret;
290
291         dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
292                 pixfmtstr(pix->pixelformat), pix->width, pix->height);
293
294         /* We always call try_fmt() before set_fmt() or set_crop() */
295         ret = ici->ops->try_fmt(icd, f);
296         if (ret < 0)
297                 return ret;
298
299         ret = ici->ops->set_fmt(icd, f);
300         if (ret < 0) {
301                 return ret;
302         } else if (!icd->current_fmt ||
303                    icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
304                 dev_err(&icd->dev,
305                         "Host driver hasn't set up current format correctly!\n");
306                 return -EINVAL;
307         }
308
309         icd->user_width         = pix->width;
310         icd->user_height        = pix->height;
311         icd->colorspace         = pix->colorspace;
312         icf->vb_vidq.field      =
313                 icd->field      = pix->field;
314
315         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
316                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
317                          f->type);
318
319         dev_dbg(&icd->dev, "set width: %d height: %d\n",
320                 icd->user_width, icd->user_height);
321
322         /* set physical bus parameters */
323         return ici->ops->set_bus_param(icd, pix->pixelformat);
324 }
325
326 static int soc_camera_open(struct file *file)
327 {
328         struct video_device *vdev = video_devdata(file);
329         struct soc_camera_device *icd = container_of(vdev->parent,
330                                                      struct soc_camera_device,
331                                                      dev);
332         struct soc_camera_link *icl = to_soc_camera_link(icd);
333         struct soc_camera_host *ici;
334         struct soc_camera_file *icf;
335         int ret;
336
337         if (!icd->ops)
338                 /* No device driver attached */
339                 return -ENODEV;
340
341         ici = to_soc_camera_host(icd->dev.parent);
342
343         icf = vmalloc(sizeof(*icf));
344         if (!icf)
345                 return -ENOMEM;
346
347         if (!try_module_get(ici->ops->owner)) {
348                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
349                 ret = -EINVAL;
350                 goto emgi;
351         }
352
353         /*
354          * Protect against icd->ops->remove() until we module_get() both
355          * drivers.
356          */
357         mutex_lock(&icd->video_lock);
358
359         icf->icd = icd;
360         icd->use_count++;
361
362         /* Now we really have to activate the camera */
363         if (icd->use_count == 1) {
364                 /* Restore parameters before the last close() per V4L2 API */
365                 struct v4l2_format f = {
366                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
367                         .fmt.pix = {
368                                 .width          = icd->user_width,
369                                 .height         = icd->user_height,
370                                 .field          = icd->field,
371                                 .colorspace     = icd->colorspace,
372                                 .pixelformat    =
373                                         icd->current_fmt->host_fmt->fourcc,
374                         },
375                 };
376         /* ddl@rock-chips.com : accelerate device open  */
377         if ((file->f_flags & O_ACCMODE) == O_RDWR) {
378                 if (icl->power) {
379                         ret = icl->power(icd->pdev, 1);
380                         if (ret < 0)
381                                 goto epower;
382                 }
383
384                 /* The camera could have been already on, try to reset */
385                 if (icl->reset)
386                         icl->reset(icd->pdev);
387        }
388
389                 ret = ici->ops->add(icd);
390                 if (ret < 0) {
391                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
392                         goto eiciadd;
393                 }
394
395                 pm_runtime_enable(&icd->vdev->dev);
396                 ret = pm_runtime_resume(&icd->vdev->dev);
397                 if (ret < 0 && ret != -ENOSYS)
398                         goto eresume;
399
400         if ((file->f_flags & O_ACCMODE) == O_RDWR) {
401                 /*
402                  * Try to configure with default parameters. Notice: this is the
403                  * very first open, so, we cannot race against other calls,
404                  * apart from someone else calling open() simultaneously, but
405                  * .video_lock is protecting us against it.
406                  */
407                 ret = soc_camera_set_fmt(icf, &f);
408                 if (ret < 0)
409                         goto esfmt;
410         }
411         }
412
413         file->private_data = icf;
414         dev_dbg(&icd->dev, "camera device open\n");
415
416         ici->ops->init_videobuf(&icf->vb_vidq, icd);
417
418         mutex_unlock(&icd->video_lock);
419
420         return 0;
421
422         /*
423          * First four errors are entered with the .video_lock held
424          * and use_count == 1
425          */
426 esfmt:
427         pm_runtime_disable(&icd->vdev->dev);
428 eresume:
429         ici->ops->remove(icd);
430 eiciadd:
431         if (icl->power)
432                 icl->power(icd->pdev, 0);
433 epower:
434         icd->use_count--;
435         mutex_unlock(&icd->video_lock);
436         module_put(ici->ops->owner);
437 emgi:
438         vfree(icf);
439         return ret;
440 }
441
442 static int soc_camera_close(struct file *file)
443 {
444         struct soc_camera_file *icf = file->private_data;
445         struct soc_camera_device *icd = icf->icd;
446         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
447
448         mutex_lock(&icd->video_lock);
449         icd->use_count--;
450         if (!icd->use_count) {
451                 struct soc_camera_link *icl = to_soc_camera_link(icd);
452
453                 pm_runtime_suspend(&icd->vdev->dev);
454                 pm_runtime_disable(&icd->vdev->dev);
455
456                 ici->ops->remove(icd);
457
458         if ((file->f_flags & O_ACCMODE) == O_RDWR) {
459                 if (icl->power)
460                         icl->power(icd->pdev, 0);
461         }
462         }
463
464         mutex_unlock(&icd->video_lock);
465
466         module_put(ici->ops->owner);
467
468         vfree(icf);
469
470         dev_dbg(&icd->dev, "camera device close\n");
471
472         return 0;
473 }
474
475 static ssize_t soc_camera_read(struct file *file, char __user *buf,
476                                size_t count, loff_t *ppos)
477 {
478         struct soc_camera_file *icf = file->private_data;
479         struct soc_camera_device *icd = icf->icd;
480         int err = -EINVAL;
481
482         dev_err(&icd->dev, "camera device read not implemented\n");
483
484         return err;
485 }
486
487 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
488 {
489         struct soc_camera_file *icf = file->private_data;
490         struct soc_camera_device *icd = icf->icd;
491         int err;
492
493         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
494
495         err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
496
497         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
498                 (unsigned long)vma->vm_start,
499                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
500                 err);
501
502         return err;
503 }
504
505 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
506 {
507         struct soc_camera_file *icf = file->private_data;
508         struct soc_camera_device *icd = icf->icd;
509         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
510
511         if (list_empty(&icf->vb_vidq.stream)) {
512                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
513                 return POLLERR;
514         }
515
516         return ici->ops->poll(file, pt);
517 }
518
519 static struct v4l2_file_operations soc_camera_fops = {
520         .owner          = THIS_MODULE,
521         .open           = soc_camera_open,
522         .release        = soc_camera_close,
523         .ioctl          = video_ioctl2,
524         .read           = soc_camera_read,
525         .mmap           = soc_camera_mmap,
526         .poll           = soc_camera_poll,
527 };
528
529 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
530                                     struct v4l2_format *f)
531 {
532         struct soc_camera_file *icf = file->private_data;
533         struct soc_camera_device *icd = icf->icd;
534         int ret,i;
535
536         WARN_ON(priv != file->private_data);
537
538         mutex_lock(&icf->vb_vidq.vb_lock);
539
540         #if 1
541         if (icf->vb_vidq.bufs[0]) {
542                 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
543                 ret = -EBUSY;
544                 goto unlock;
545         }
546         #else
547
548         /* ddl@rock-chips.com :
549              Judge queue  initialised by Judge icf->vb_vidq.bufs[0] whether is NULL , it is error.    */
550
551         i = 0;
552         while (icf->vb_vidq.bufs[i] && (i<VIDEO_MAX_FRAME)) {
553                 if (icf->vb_vidq.bufs[i]->state != VIDEOBUF_NEEDS_INIT) {
554                         dev_err(&icd->dev, "S_FMT denied: queue initialised, icf->vb_vidq.bufs[%d]->state:0x%x\n",i,icf->vb_vidq.bufs[i]->state);
555                         ret = -EBUSY;
556                         goto unlock;
557                 }
558                 i++;
559         }
560
561         #endif
562
563         ret = soc_camera_set_fmt(icf, f);
564
565 unlock:
566         mutex_unlock(&icf->vb_vidq.vb_lock);
567
568         return ret;
569 }
570
571 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
572                                        struct v4l2_fmtdesc *f)
573 {
574         struct soc_camera_file *icf = file->private_data;
575         struct soc_camera_device *icd = icf->icd;
576         const struct soc_mbus_pixelfmt *format;
577
578         WARN_ON(priv != file->private_data);
579
580         if (f->index >= icd->num_user_formats)
581                 return -EINVAL;
582
583         format = icd->user_formats[f->index].host_fmt;
584
585         if (format->name)
586                 strlcpy(f->description, format->name, sizeof(f->description));
587         f->pixelformat = format->fourcc;
588         return 0;
589 }
590
591 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
592                                     struct v4l2_format *f)
593 {
594         struct soc_camera_file *icf = file->private_data;
595         struct soc_camera_device *icd = icf->icd;
596         struct v4l2_pix_format *pix = &f->fmt.pix;
597
598         WARN_ON(priv != file->private_data);
599
600         pix->width              = icd->user_width;
601         pix->height             = icd->user_height;
602         pix->field              = icf->vb_vidq.field;
603         pix->pixelformat        = icd->current_fmt->host_fmt->fourcc;
604         pix->bytesperline       = soc_mbus_bytes_per_line(pix->width,
605                                                 icd->current_fmt->host_fmt);
606         pix->colorspace         = icd->colorspace;
607         if (pix->bytesperline < 0)
608                 return pix->bytesperline;
609         pix->sizeimage          = pix->height * pix->bytesperline;
610         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
611                 icd->current_fmt->host_fmt->fourcc);
612         return 0;
613 }
614
615 static int soc_camera_querycap(struct file *file, void  *priv,
616                                struct v4l2_capability *cap)
617 {
618         struct soc_camera_file *icf = file->private_data;
619         struct soc_camera_device *icd = icf->icd;
620         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
621
622         WARN_ON(priv != file->private_data);
623
624         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
625         return ici->ops->querycap(ici, cap);
626 }
627
628 static int soc_camera_streamon(struct file *file, void *priv,
629                                enum v4l2_buf_type i)
630 {
631         struct soc_camera_file *icf = file->private_data;
632         struct soc_camera_device *icd = icf->icd;
633         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
634         struct soc_camera_host *ici =
635                     to_soc_camera_host(icd->dev.parent);
636         int ret;
637
638         WARN_ON(priv != file->private_data);
639
640         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
641                 return -EINVAL;
642
643         mutex_lock(&icd->video_lock);
644
645         v4l2_subdev_call(sd, video, s_stream, 1);
646     if (ici->ops->s_stream)
647             ici->ops->s_stream(icd, 1);                         /* ddl@rock-chips.com : Add stream control for host */
648         /* This calls buf_queue from host driver's videobuf_queue_ops */
649         ret = videobuf_streamon(&icf->vb_vidq);
650
651         mutex_unlock(&icd->video_lock);
652
653         return ret;
654 }
655
656 static int soc_camera_streamoff(struct file *file, void *priv,
657                                 enum v4l2_buf_type i)
658 {
659         struct soc_camera_file *icf = file->private_data;
660         struct soc_camera_device *icd = icf->icd;
661         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
662     struct soc_camera_host *ici =
663                     to_soc_camera_host(icd->dev.parent);
664
665         WARN_ON(priv != file->private_data);
666
667         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
668                 return -EINVAL;
669     
670         mutex_lock(&icd->video_lock);
671
672         /*
673          * This calls buf_release from host driver's videobuf_queue_ops for all
674          * remaining buffers. When the last buffer is freed, stop capture
675          */
676         videobuf_streamoff(&icf->vb_vidq);
677
678         v4l2_subdev_call(sd, video, s_stream, 0);
679     if (ici->ops->s_stream)
680                 ici->ops->s_stream(icd, 0);                             /* ddl@rock-chips.com : Add stream control for host */
681
682     videobuf_mmap_free(&icf->vb_vidq);          /* ddl@rock-chips.com : free video buf */
683         mutex_unlock(&icd->video_lock);
684
685         return 0;
686 }
687
688 static int soc_camera_queryctrl(struct file *file, void *priv,
689                                 struct v4l2_queryctrl *qc)
690 {
691         struct soc_camera_file *icf = file->private_data;
692         struct soc_camera_device *icd = icf->icd;
693         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
694         int i;
695
696         WARN_ON(priv != file->private_data);
697
698         if (!qc->id)
699                 return -EINVAL;
700
701         /* First check host controls */
702         for (i = 0; i < ici->ops->num_controls; i++)
703                 if (qc->id == ici->ops->controls[i].id) {
704                         memcpy(qc, &(ici->ops->controls[i]),
705                                 sizeof(*qc));
706                         return 0;
707                 }
708
709         /* Then device controls */
710         for (i = 0; i < icd->ops->num_controls; i++)
711                 if (qc->id == icd->ops->controls[i].id) {
712                         memcpy(qc, &(icd->ops->controls[i]),
713                                 sizeof(*qc));
714                         return 0;
715                 }
716
717         return -EINVAL;
718 }
719
720 /* ddl@rock-chips.com : Add ioctrl -VIDIOC_QUERYMENU */
721 static int soc_camera_querymenu(struct file *file, void *priv,
722                                 struct v4l2_querymenu *qm)
723 {
724     struct soc_camera_file *icf = file->private_data;
725     struct soc_camera_device *icd = icf->icd;
726     struct v4l2_queryctrl qctrl;
727     int i,j;
728
729     qctrl.id = qm->id;
730
731     if (soc_camera_queryctrl(file,priv, &qctrl) == 0) {
732         for (i = 0; i < icd->ops->num_menus; i++) {
733             if (qm->id == icd->ops->menus[i].id) {
734                 for (j=0; j<=(qctrl.maximum - qctrl.minimum); j++) {
735
736                     if (qm->index == icd->ops->menus[i].index) {
737                         snprintf(qm->name, sizeof(qm->name), icd->ops->menus[i].name);
738                         qm->reserved = 0;
739
740                         return 0;
741                     } else {
742                         i++;
743                         if ( i >= icd->ops->num_menus)
744                             return -EINVAL;
745                     }
746                 }
747             }
748         }
749     }
750
751     return -EINVAL;
752 }
753
754 static int soc_camera_g_ctrl(struct file *file, void *priv,
755                              struct v4l2_control *ctrl)
756 {
757         struct soc_camera_file *icf = file->private_data;
758         struct soc_camera_device *icd = icf->icd;
759         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
760         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
761         int ret;
762
763         WARN_ON(priv != file->private_data);
764
765         if (ici->ops->get_ctrl) {
766                 ret = ici->ops->get_ctrl(icd, ctrl);
767                 if (ret != -ENOIOCTLCMD)
768                         return ret;
769         }
770
771         return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
772 }
773
774 static int soc_camera_s_ctrl(struct file *file, void *priv,
775                              struct v4l2_control *ctrl)
776 {
777         struct soc_camera_file *icf = file->private_data;
778         struct soc_camera_device *icd = icf->icd;
779         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
780         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
781         int ret;
782
783         WARN_ON(priv != file->private_data);
784
785         if (ici->ops->set_ctrl) {
786                 ret = ici->ops->set_ctrl(icd, ctrl);
787                 if (ret != -ENOIOCTLCMD)
788                         return ret;
789         }
790
791         return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
792 }
793
794
795  /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
796 static int soc_camera_try_ext_ctrl(struct file *file, void *priv,
797                              struct v4l2_ext_controls *ctrl)
798 {
799     struct soc_camera_file *icf = file->private_data;
800     struct soc_camera_device *icd = icf->icd;
801     const struct v4l2_queryctrl *qctrl;
802     int i;
803
804     WARN_ON(priv != file->private_data);
805
806     if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
807         return -EINVAL;
808
809     for (i=0; i<ctrl->count; i++) {
810         qctrl = soc_camera_find_qctrl(icd->ops, ctrl->controls[i].id);
811         if (!qctrl)
812             return -EINVAL;
813
814         if ((ctrl->controls[i].value < qctrl->minimum) ||(ctrl->controls[i].value > qctrl->minimum))
815             return -ERANGE;
816     }
817
818     return 0;
819 }
820  /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
821 static int soc_camera_g_ext_ctrl(struct file *file, void *priv,
822                              struct v4l2_ext_controls *ctrl)
823 {
824     struct soc_camera_file *icf = file->private_data;
825     struct soc_camera_device *icd = icf->icd;
826     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
827
828     WARN_ON(priv != file->private_data);
829
830     if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
831         return -EINVAL;
832
833     return v4l2_subdev_call(sd, core, g_ext_ctrls, ctrl);
834 }
835  /* ddl@rock-chips.com : Add ioctrl -VIDIOC_XXX_ext_ctrl for soc-camera */
836 static int soc_camera_s_ext_ctrl(struct file *file, void *priv,
837                              struct v4l2_ext_controls *ctrl)
838 {
839     struct soc_camera_file *icf = file->private_data;
840     struct soc_camera_device *icd = icf->icd;
841     struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
842
843     WARN_ON(priv != file->private_data);
844
845     if (ctrl->ctrl_class != V4L2_CTRL_CLASS_CAMERA)
846         return -EINVAL;
847
848     return v4l2_subdev_call(sd, core, s_ext_ctrls, ctrl);
849 }
850
851
852 static int soc_camera_cropcap(struct file *file, void *fh,
853                               struct v4l2_cropcap *a)
854 {
855         struct soc_camera_file *icf = file->private_data;
856         struct soc_camera_device *icd = icf->icd;
857         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
858
859         return ici->ops->cropcap(icd, a);
860 }
861
862 static int soc_camera_g_crop(struct file *file, void *fh,
863                              struct v4l2_crop *a)
864 {
865         struct soc_camera_file *icf = file->private_data;
866         struct soc_camera_device *icd = icf->icd;
867         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
868         int ret;
869
870         mutex_lock(&icf->vb_vidq.vb_lock);
871         ret = ici->ops->get_crop(icd, a);
872         mutex_unlock(&icf->vb_vidq.vb_lock);
873
874         return ret;
875 }
876
877 /*
878  * According to the V4L2 API, drivers shall not update the struct v4l2_crop
879  * argument with the actual geometry, instead, the user shall use G_CROP to
880  * retrieve it.
881  */
882 static int soc_camera_s_crop(struct file *file, void *fh,
883                              struct v4l2_crop *a)
884 {
885         struct soc_camera_file *icf = file->private_data;
886         struct soc_camera_device *icd = icf->icd;
887         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
888         struct v4l2_rect *rect = &a->c;
889         struct v4l2_crop current_crop;
890         int ret;
891
892         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
893                 return -EINVAL;
894
895         dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
896                 rect->width, rect->height, rect->left, rect->top);
897
898         /* Cropping is allowed during a running capture, guard consistency */
899         mutex_lock(&icf->vb_vidq.vb_lock);
900
901         /* If get_crop fails, we'll let host and / or client drivers decide */
902         ret = ici->ops->get_crop(icd, &current_crop);
903
904         /* Prohibit window size change with initialised buffers */
905         if (ret < 0) {
906                 dev_err(&icd->dev,
907                         "S_CROP denied: getting current crop failed\n");
908         } else if (icf->vb_vidq.bufs[0] &&
909                    (a->c.width != current_crop.c.width ||
910                     a->c.height != current_crop.c.height)) {
911                 dev_err(&icd->dev,
912                         "S_CROP denied: queue initialised and sizes differ\n");
913                 ret = -EBUSY;
914         } else {
915                 ret = ici->ops->set_crop(icd, a);
916         }
917
918         mutex_unlock(&icf->vb_vidq.vb_lock);
919
920         return ret;
921 }
922
923 static int soc_camera_g_parm(struct file *file, void *fh,
924                              struct v4l2_streamparm *a)
925 {
926         struct soc_camera_file *icf = file->private_data;
927         struct soc_camera_device *icd = icf->icd;
928         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
929
930         if (ici->ops->get_parm)
931                 return ici->ops->get_parm(icd, a);
932
933         return -ENOIOCTLCMD;
934 }
935
936 static int soc_camera_s_parm(struct file *file, void *fh,
937                              struct v4l2_streamparm *a)
938 {
939         struct soc_camera_file *icf = file->private_data;
940         struct soc_camera_device *icd = icf->icd;
941         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
942
943         if (ici->ops->set_parm)
944                 return ici->ops->set_parm(icd, a);
945
946         return -ENOIOCTLCMD;
947 }
948
949 static int soc_camera_g_chip_ident(struct file *file, void *fh,
950                                    struct v4l2_dbg_chip_ident *id)
951 {
952         struct soc_camera_file *icf = file->private_data;
953         struct soc_camera_device *icd = icf->icd;
954         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
955
956         return v4l2_subdev_call(sd, core, g_chip_ident, id);
957 }
958
959 #ifdef CONFIG_VIDEO_ADV_DEBUG
960 static int soc_camera_g_register(struct file *file, void *fh,
961                                  struct v4l2_dbg_register *reg)
962 {
963         struct soc_camera_file *icf = file->private_data;
964         struct soc_camera_device *icd = icf->icd;
965         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
966
967         return v4l2_subdev_call(sd, core, g_register, reg);
968 }
969
970 static int soc_camera_s_register(struct file *file, void *fh,
971                                  struct v4l2_dbg_register *reg)
972 {
973         struct soc_camera_file *icf = file->private_data;
974         struct soc_camera_device *icd = icf->icd;
975         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
976
977         return v4l2_subdev_call(sd, core, s_register, reg);
978 }
979 #endif
980
981 /* So far this function cannot fail */
982 static void scan_add_host(struct soc_camera_host *ici)
983 {
984         struct soc_camera_device *icd;
985
986         mutex_lock(&list_lock);
987
988         list_for_each_entry(icd, &devices, list) {
989                 if (icd->iface == ici->nr) {
990                         int ret;
991                         icd->dev.parent = ici->v4l2_dev.dev;
992                         dev_set_name(&icd->dev, "%u-%u", icd->iface,
993                                      icd->devnum);
994                         ret = device_register(&icd->dev);
995                         if (ret < 0) {
996                                 icd->dev.parent = NULL;
997                                 dev_err(&icd->dev,
998                                         "Cannot register device: %d\n", ret);
999                         }
1000                 }
1001         }
1002
1003         mutex_unlock(&list_lock);
1004 }
1005
1006 #ifdef CONFIG_I2C_BOARDINFO
1007 static int soc_camera_init_i2c(struct soc_camera_device *icd,
1008                                struct soc_camera_link *icl)
1009 {
1010         struct i2c_client *client;
1011         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1012         struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
1013         struct v4l2_subdev *subdev;
1014
1015         if (!adap) {
1016                 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
1017                         icl->i2c_adapter_id);
1018                 goto ei2cga;
1019         }
1020
1021         icl->board_info->platform_data = icd;
1022
1023         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
1024                                 icl->module_name, icl->board_info, NULL);
1025         if (!subdev)
1026                 goto ei2cnd;
1027
1028         client = subdev->priv;
1029
1030         /* Use to_i2c_client(dev) to recover the i2c client */
1031         dev_set_drvdata(&icd->dev, &client->dev);
1032
1033         return 0;
1034 ei2cnd:
1035         i2c_put_adapter(adap);
1036 ei2cga:
1037         return -ENODEV;
1038 }
1039
1040 static void soc_camera_free_i2c(struct soc_camera_device *icd)
1041 {
1042         struct i2c_client *client =
1043                 to_i2c_client(to_soc_camera_control(icd));
1044         dev_set_drvdata(&icd->dev, NULL);
1045         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1046         i2c_unregister_device(client);
1047         i2c_put_adapter(client->adapter);
1048 }
1049 #else
1050 #define soc_camera_init_i2c(icd, icl)   (-ENODEV)
1051 #define soc_camera_free_i2c(icd)        do {} while (0)
1052 #endif
1053
1054 static int soc_camera_video_start(struct soc_camera_device *icd);
1055 static int video_dev_create(struct soc_camera_device *icd);
1056 /* Called during host-driver probe */
1057 static int soc_camera_probe(struct device *dev)
1058 {
1059         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1060         struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1061         struct soc_camera_link *icl = to_soc_camera_link(icd);
1062         struct device *control = NULL;
1063         struct v4l2_subdev *sd;
1064         struct v4l2_mbus_framefmt mf;
1065         int ret;
1066
1067         dev_info(dev, "Probing %s\n", dev_name(dev));
1068
1069         if (icl->power) {
1070                 ret = icl->power(icd->pdev, 1);
1071                 if (ret < 0) {
1072                         dev_err(dev,
1073                                 "Platform failed to power-on the camera.\n");
1074                         goto epower;
1075                 }
1076         }
1077
1078         /* The camera could have been already on, try to reset */
1079         if (icl->reset)
1080                 icl->reset(icd->pdev);
1081
1082         ret = ici->ops->add(icd);
1083         if (ret < 0)
1084                 goto eadd;
1085
1086         /* Must have icd->vdev before registering the device */
1087         ret = video_dev_create(icd);
1088         if (ret < 0)
1089                 goto evdc;
1090
1091         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1092         if (icl->board_info) {
1093                 ret = soc_camera_init_i2c(icd, icl);
1094                 if (ret < 0)
1095                         goto eadddev;
1096         } else if (!icl->add_device || !icl->del_device) {
1097                 ret = -EINVAL;
1098                 goto eadddev;
1099         } else {
1100                 if (icl->module_name)
1101                         ret = request_module(icl->module_name);
1102
1103                 ret = icl->add_device(icl, &icd->dev);
1104                 if (ret < 0)
1105                         goto eadddev;
1106
1107                 /*
1108                  * FIXME: this is racy, have to use driver-binding notification,
1109                  * when it is available
1110                  */
1111                 control = to_soc_camera_control(icd);
1112                 if (!control || !control->driver || !dev_get_drvdata(control) ||
1113                     !try_module_get(control->driver->owner)) {
1114                         icl->del_device(icl);
1115                         goto enodrv;
1116                 }
1117         }
1118
1119         /* At this point client .probe() should have run already */
1120         ret = soc_camera_init_user_formats(icd);
1121         if (ret < 0)
1122                 goto eiufmt;
1123
1124         icd->field = V4L2_FIELD_ANY;
1125
1126         /* ..._video_start() will create a device node, so we have to protect */
1127         mutex_lock(&icd->video_lock);
1128
1129         ret = soc_camera_video_start(icd);
1130         if (ret < 0)
1131                 goto evidstart;
1132
1133         /* Try to improve our guess of a reasonable window format */
1134         sd = soc_camera_to_subdev(icd);
1135         if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1136                 icd->user_width         = mf.width;
1137                 icd->user_height        = mf.height;
1138                 icd->colorspace         = mf.colorspace;
1139                 icd->field              = mf.field;
1140         }
1141
1142         /* Do we have to sysfs_remove_link() before device_unregister()? */
1143         if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1144                               "control"))
1145                 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1146
1147         ici->ops->remove(icd);
1148
1149         if (icl->power)
1150                 icl->power(icd->pdev, 0);
1151
1152         mutex_unlock(&icd->video_lock);
1153
1154         return 0;
1155
1156 evidstart:
1157         mutex_unlock(&icd->video_lock);
1158         soc_camera_free_user_formats(icd);
1159 eiufmt:
1160         if (icl->board_info) {
1161                 soc_camera_free_i2c(icd);
1162         } else {
1163                 icl->del_device(icl);
1164                 module_put(control->driver->owner);
1165         }
1166 enodrv:
1167 eadddev:
1168         video_device_release(icd->vdev);
1169 evdc:
1170         ici->ops->remove(icd);
1171 eadd:
1172         if (icl->power)
1173                 icl->power(icd->pdev, 0);
1174 epower:
1175         return ret;
1176 }
1177
1178 /*
1179  * This is called on device_unregister, which only means we have to disconnect
1180  * from the host, but not remove ourselves from the device list
1181  */
1182 static int soc_camera_remove(struct device *dev)
1183 {
1184         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1185         struct soc_camera_link *icl = to_soc_camera_link(icd);
1186         struct video_device *vdev = icd->vdev;
1187
1188         BUG_ON(!dev->parent);
1189
1190         if (vdev) {
1191                 mutex_lock(&icd->video_lock);
1192                 video_unregister_device(vdev);
1193                 icd->vdev = NULL;
1194                 mutex_unlock(&icd->video_lock);
1195         }
1196
1197         if (icl->board_info) {
1198                 soc_camera_free_i2c(icd);
1199         } else {
1200                 struct device_driver *drv = to_soc_camera_control(icd) ?
1201                         to_soc_camera_control(icd)->driver : NULL;
1202                 if (drv) {
1203                         icl->del_device(icl);
1204                         module_put(drv->owner);
1205                 }
1206         }
1207         soc_camera_free_user_formats(icd);
1208
1209         return 0;
1210 }
1211
1212 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1213 {
1214         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1215         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1216         int ret = 0;
1217
1218         if (ici->ops->suspend)
1219                 ret = ici->ops->suspend(icd, state);
1220
1221         return ret;
1222 }
1223
1224 static int soc_camera_resume(struct device *dev)
1225 {
1226         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1227         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1228         int ret = 0;
1229
1230         if (ici->ops->resume)
1231                 ret = ici->ops->resume(icd);
1232
1233         return ret;
1234 }
1235
1236 struct bus_type soc_camera_bus_type = {
1237         .name           = "soc-camera",
1238         .probe          = soc_camera_probe,
1239         .remove         = soc_camera_remove,
1240         .suspend        = soc_camera_suspend,
1241         .resume         = soc_camera_resume,
1242 };
1243 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1244
1245 static struct device_driver ic_drv = {
1246         .name   = "camera",
1247         .bus    = &soc_camera_bus_type,
1248         .owner  = THIS_MODULE,
1249 };
1250
1251 static void dummy_release(struct device *dev)
1252 {
1253 }
1254
1255 static int default_cropcap(struct soc_camera_device *icd,
1256                            struct v4l2_cropcap *a)
1257 {
1258         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1259         return v4l2_subdev_call(sd, video, cropcap, a);
1260 }
1261
1262 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1263 {
1264         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1265         return v4l2_subdev_call(sd, video, g_crop, a);
1266 }
1267
1268 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1269 {
1270         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1271         return v4l2_subdev_call(sd, video, s_crop, a);
1272 }
1273
1274 static void soc_camera_device_init(struct device *dev, void *pdata)
1275 {
1276         dev->platform_data      = pdata;
1277         dev->bus                = &soc_camera_bus_type;
1278         dev->release            = dummy_release;
1279 }
1280
1281 int soc_camera_host_register(struct soc_camera_host *ici)
1282 {
1283         struct soc_camera_host *ix;
1284         int ret;
1285
1286         if (!ici || !ici->ops ||
1287             !ici->ops->try_fmt ||
1288             !ici->ops->set_fmt ||
1289             !ici->ops->set_bus_param ||
1290             !ici->ops->querycap ||
1291             !ici->ops->init_videobuf ||
1292             !ici->ops->reqbufs ||
1293             !ici->ops->add ||
1294             !ici->ops->remove ||
1295             !ici->ops->poll ||
1296             !ici->v4l2_dev.dev)
1297                 return -EINVAL;
1298
1299         if (!ici->ops->set_crop)
1300                 ici->ops->set_crop = default_s_crop;
1301         if (!ici->ops->get_crop)
1302                 ici->ops->get_crop = default_g_crop;
1303         if (!ici->ops->cropcap)
1304                 ici->ops->cropcap = default_cropcap;
1305
1306         mutex_lock(&list_lock);
1307         list_for_each_entry(ix, &hosts, list) {
1308                 if (ix->nr == ici->nr) {
1309                         ret = -EBUSY;
1310                         goto edevreg;
1311                 }
1312         }
1313
1314         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1315         if (ret < 0)
1316                 goto edevreg;
1317
1318         list_add_tail(&ici->list, &hosts);
1319         mutex_unlock(&list_lock);
1320
1321         scan_add_host(ici);
1322
1323         return 0;
1324
1325 edevreg:
1326         mutex_unlock(&list_lock);
1327         return ret;
1328 }
1329 EXPORT_SYMBOL(soc_camera_host_register);
1330
1331 /* Unregister all clients! */
1332 void soc_camera_host_unregister(struct soc_camera_host *ici)
1333 {
1334         struct soc_camera_device *icd;
1335
1336         mutex_lock(&list_lock);
1337
1338         list_del(&ici->list);
1339
1340         list_for_each_entry(icd, &devices, list) {
1341                 if (icd->iface == ici->nr) {
1342                         void *pdata = icd->dev.platform_data;
1343                         /* The bus->remove will be called */
1344                         device_unregister(&icd->dev);
1345                         /*
1346                          * Not before device_unregister(), .remove
1347                          * needs parent to call ici->ops->remove().
1348                          * If the host module is loaded again, device_register()
1349                          * would complain "already initialised," since 2.6.32
1350                          * this is also needed to prevent use-after-free of the
1351                          * device private data.
1352                          */
1353                         memset(&icd->dev, 0, sizeof(icd->dev));
1354                         soc_camera_device_init(&icd->dev, pdata);
1355                 }
1356         }
1357
1358         mutex_unlock(&list_lock);
1359
1360         v4l2_device_unregister(&ici->v4l2_dev);
1361 }
1362 EXPORT_SYMBOL(soc_camera_host_unregister);
1363
1364 /* Image capture device */
1365 static int soc_camera_device_register(struct soc_camera_device *icd)
1366 {
1367         struct soc_camera_device *ix;
1368         int num = -1, i;
1369
1370         for (i = 0; i < 256 && num < 0; i++) {
1371                 num = i;
1372                 /* Check if this index is available on this interface */
1373                 list_for_each_entry(ix, &devices, list) {
1374                         if (ix->iface == icd->iface && ix->devnum == i) {
1375                                 num = -1;
1376                                 break;
1377                         }
1378                 }
1379         }
1380
1381         if (num < 0)
1382                 /*
1383                  * ok, we have 256 cameras on this host...
1384                  * man, stay reasonable...
1385                  */
1386                 return -ENOMEM;
1387
1388         icd->devnum             = num;
1389         icd->use_count          = 0;
1390         icd->host_priv          = NULL;
1391         mutex_init(&icd->video_lock);
1392
1393         list_add_tail(&icd->list, &devices);
1394
1395         return 0;
1396 }
1397
1398 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1399 {
1400         list_del(&icd->list);
1401 }
1402
1403 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1404         .vidioc_querycap         = soc_camera_querycap,
1405         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1406         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1407         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1408         .vidioc_enum_input       = soc_camera_enum_input,
1409         .vidioc_g_input          = soc_camera_g_input,
1410         .vidioc_s_input          = soc_camera_s_input,
1411         .vidioc_s_std            = soc_camera_s_std,
1412         .vidioc_reqbufs          = soc_camera_reqbufs,
1413         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1414         .vidioc_querybuf         = soc_camera_querybuf,
1415         .vidioc_qbuf             = soc_camera_qbuf,
1416         .vidioc_dqbuf            = soc_camera_dqbuf,
1417         .vidioc_streamon         = soc_camera_streamon,
1418         .vidioc_streamoff        = soc_camera_streamoff,
1419         .vidioc_queryctrl        = soc_camera_queryctrl,
1420          .vidioc_querymenu       = soc_camera_querymenu,                            /* ddl@rock-chips.com:   Add ioctrl - vidioc_querymenu for soc-camera */
1421         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1422         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1423         .vidioc_g_ext_ctrls    = soc_camera_g_ext_ctrl,                                 /* ddl@rock-chips.com:   Add ioctrl - vidioc_g_ext_ctrls for soc-camera */
1424         .vidioc_s_ext_ctrls    = soc_camera_s_ext_ctrl,                                 /* ddl@rock-chips.com:   Add ioctrl - vidioc_s_ext_ctrls for soc-camera */
1425         .vidioc_try_ext_ctrls    = soc_camera_try_ext_ctrl,                         /* ddl@rock-chips.com:   Add ioctrl - vidioc_try_ext_ctrls for soc-camera */
1426
1427         .vidioc_cropcap          = soc_camera_cropcap,
1428         .vidioc_g_crop           = soc_camera_g_crop,
1429         .vidioc_s_crop           = soc_camera_s_crop,
1430         .vidioc_g_parm           = soc_camera_g_parm,
1431         .vidioc_s_parm           = soc_camera_s_parm,
1432         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1433 #ifdef CONFIG_VIDEO_ADV_DEBUG
1434         .vidioc_g_register       = soc_camera_g_register,
1435         .vidioc_s_register       = soc_camera_s_register,
1436 #endif
1437 };
1438
1439 static int video_dev_create(struct soc_camera_device *icd)
1440 {
1441         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1442         struct video_device *vdev = video_device_alloc();
1443
1444         if (!vdev)
1445                 return -ENOMEM;
1446
1447         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1448
1449         vdev->parent            = &icd->dev;
1450         vdev->current_norm      = V4L2_STD_UNKNOWN;
1451         vdev->fops              = &soc_camera_fops;
1452         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1453         vdev->release           = video_device_release;
1454         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1455
1456         icd->vdev = vdev;
1457
1458         return 0;
1459 }
1460
1461 /*
1462  * Called from soc_camera_probe() above (with .video_lock held???)
1463  */
1464 static int soc_camera_video_start(struct soc_camera_device *icd)
1465 {
1466         struct device_type *type = icd->vdev->dev.type;
1467         int ret;
1468
1469         if (!icd->dev.parent)
1470                 return -ENODEV;
1471
1472         if (!icd->ops ||
1473             !icd->ops->query_bus_param ||
1474             !icd->ops->set_bus_param)
1475                 return -EINVAL;
1476
1477         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1478         if (ret < 0) {
1479                 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1480                 return ret;
1481         }
1482
1483         /* Restore device type, possibly set by the subdevice driver */
1484         icd->vdev->dev.type = type;
1485
1486         return 0;
1487 }
1488
1489 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1490 {
1491         struct soc_camera_link *icl = pdev->dev.platform_data;
1492         struct soc_camera_device *icd;
1493         int ret;
1494
1495         if (!icl)
1496                 return -EINVAL;
1497
1498         icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1499         if (!icd)
1500                 return -ENOMEM;
1501
1502         icd->iface = icl->bus_id;
1503         icd->pdev = &pdev->dev;
1504         platform_set_drvdata(pdev, icd);
1505
1506         ret = soc_camera_device_register(icd);
1507         if (ret < 0)
1508                 goto escdevreg;
1509
1510         soc_camera_device_init(&icd->dev, icl);
1511
1512         icd->user_width         = DEFAULT_WIDTH;
1513         icd->user_height        = DEFAULT_HEIGHT;
1514
1515         return 0;
1516
1517 escdevreg:
1518         kfree(icd);
1519
1520         return ret;
1521 }
1522
1523 /*
1524  * Only called on rmmod for each platform device, since they are not
1525  * hot-pluggable. Now we know, that all our users - hosts and devices have
1526  * been unloaded already
1527  */
1528 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1529 {
1530         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1531
1532         if (!icd)
1533                 return -EINVAL;
1534
1535         soc_camera_device_unregister(icd);
1536
1537         kfree(icd);
1538
1539         return 0;
1540 }
1541
1542 static struct platform_driver __refdata soc_camera_pdrv = {
1543         .remove  = __devexit_p(soc_camera_pdrv_remove),
1544         .driver  = {
1545                 .name   = "soc-camera-pdrv",
1546                 .owner  = THIS_MODULE,
1547         },
1548 };
1549
1550 static int __init soc_camera_init(void)
1551 {
1552         int ret = bus_register(&soc_camera_bus_type);
1553         if (ret)
1554                 return ret;
1555         ret = driver_register(&ic_drv);
1556         if (ret)
1557                 goto edrvr;
1558
1559         ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1560         if (ret)
1561                 goto epdr;
1562
1563         return 0;
1564
1565 epdr:
1566         driver_unregister(&ic_drv);
1567 edrvr:
1568         bus_unregister(&soc_camera_bus_type);
1569         return ret;
1570 }
1571
1572 static void __exit soc_camera_exit(void)
1573 {
1574         platform_driver_unregister(&soc_camera_pdrv);
1575         driver_unregister(&ic_drv);
1576         bus_unregister(&soc_camera_bus_type);
1577 }
1578
1579 module_init(soc_camera_init);
1580 module_exit(soc_camera_exit);
1581
1582 MODULE_DESCRIPTION("Image capture bus driver");
1583 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1584 MODULE_LICENSE("GPL");
1585 MODULE_ALIAS("platform:soc-camera-pdrv");