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