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