[PARISC] slub: fix panic with DISCONTIGMEM
[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         dev_set_drvdata(&icd->dev, NULL);
1000         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1001         i2c_unregister_device(client);
1002         i2c_put_adapter(client->adapter);
1003 }
1004 #else
1005 #define soc_camera_init_i2c(icd, icl)   (-ENODEV)
1006 #define soc_camera_free_i2c(icd)        do {} while (0)
1007 #endif
1008
1009 static int soc_camera_video_start(struct soc_camera_device *icd);
1010 static int video_dev_create(struct soc_camera_device *icd);
1011 /* Called during host-driver probe */
1012 static int soc_camera_probe(struct device *dev)
1013 {
1014         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1015         struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
1016         struct soc_camera_link *icl = to_soc_camera_link(icd);
1017         struct device *control = NULL;
1018         struct v4l2_subdev *sd;
1019         struct v4l2_mbus_framefmt mf;
1020         int ret;
1021
1022         dev_info(dev, "Probing %s\n", dev_name(dev));
1023
1024         ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1025                                  icl->regulators);
1026         if (ret < 0)
1027                 goto ereg;
1028
1029         ret = soc_camera_power_set(icd, icl, 1);
1030         if (ret < 0)
1031                 goto epower;
1032
1033         /* The camera could have been already on, try to reset */
1034         if (icl->reset)
1035                 icl->reset(icd->pdev);
1036
1037         ret = ici->ops->add(icd);
1038         if (ret < 0)
1039                 goto eadd;
1040
1041         /* Must have icd->vdev before registering the device */
1042         ret = video_dev_create(icd);
1043         if (ret < 0)
1044                 goto evdc;
1045
1046         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1047         if (icl->board_info) {
1048                 ret = soc_camera_init_i2c(icd, icl);
1049                 if (ret < 0)
1050                         goto eadddev;
1051         } else if (!icl->add_device || !icl->del_device) {
1052                 ret = -EINVAL;
1053                 goto eadddev;
1054         } else {
1055                 if (icl->module_name)
1056                         ret = request_module(icl->module_name);
1057
1058                 ret = icl->add_device(icl, &icd->dev);
1059                 if (ret < 0)
1060                         goto eadddev;
1061
1062                 /*
1063                  * FIXME: this is racy, have to use driver-binding notification,
1064                  * when it is available
1065                  */
1066                 control = to_soc_camera_control(icd);
1067                 if (!control || !control->driver || !dev_get_drvdata(control) ||
1068                     !try_module_get(control->driver->owner)) {
1069                         icl->del_device(icl);
1070                         goto enodrv;
1071                 }
1072         }
1073
1074         /* At this point client .probe() should have run already */
1075         ret = soc_camera_init_user_formats(icd);
1076         if (ret < 0)
1077                 goto eiufmt;
1078
1079         icd->field = V4L2_FIELD_ANY;
1080
1081         icd->vdev->lock = &icd->video_lock;
1082
1083         /*
1084          * ..._video_start() will create a device node, video_register_device()
1085          * itself is protected against concurrent open() calls, but we also have
1086          * to protect our data.
1087          */
1088         mutex_lock(&icd->video_lock);
1089
1090         ret = soc_camera_video_start(icd);
1091         if (ret < 0)
1092                 goto evidstart;
1093
1094         /* Try to improve our guess of a reasonable window format */
1095         sd = soc_camera_to_subdev(icd);
1096         if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1097                 icd->user_width         = mf.width;
1098                 icd->user_height        = mf.height;
1099                 icd->colorspace         = mf.colorspace;
1100                 icd->field              = mf.field;
1101         }
1102
1103         /* Do we have to sysfs_remove_link() before device_unregister()? */
1104         if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
1105                               "control"))
1106                 dev_warn(&icd->dev, "Failed creating the control symlink\n");
1107
1108         ici->ops->remove(icd);
1109
1110         soc_camera_power_set(icd, icl, 0);
1111
1112         mutex_unlock(&icd->video_lock);
1113
1114         return 0;
1115
1116 evidstart:
1117         mutex_unlock(&icd->video_lock);
1118         soc_camera_free_user_formats(icd);
1119 eiufmt:
1120         if (icl->board_info) {
1121                 soc_camera_free_i2c(icd);
1122         } else {
1123                 icl->del_device(icl);
1124                 module_put(control->driver->owner);
1125         }
1126 enodrv:
1127 eadddev:
1128         video_device_release(icd->vdev);
1129 evdc:
1130         ici->ops->remove(icd);
1131 eadd:
1132         soc_camera_power_set(icd, icl, 0);
1133 epower:
1134         regulator_bulk_free(icl->num_regulators, icl->regulators);
1135 ereg:
1136         return ret;
1137 }
1138
1139 /*
1140  * This is called on device_unregister, which only means we have to disconnect
1141  * from the host, but not remove ourselves from the device list
1142  */
1143 static int soc_camera_remove(struct device *dev)
1144 {
1145         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1146         struct soc_camera_link *icl = to_soc_camera_link(icd);
1147         struct video_device *vdev = icd->vdev;
1148
1149         BUG_ON(!dev->parent);
1150
1151         if (vdev) {
1152                 video_unregister_device(vdev);
1153                 icd->vdev = NULL;
1154         }
1155
1156         if (icl->board_info) {
1157                 soc_camera_free_i2c(icd);
1158         } else {
1159                 struct device_driver *drv = to_soc_camera_control(icd) ?
1160                         to_soc_camera_control(icd)->driver : NULL;
1161                 if (drv) {
1162                         icl->del_device(icl);
1163                         module_put(drv->owner);
1164                 }
1165         }
1166         soc_camera_free_user_formats(icd);
1167
1168         regulator_bulk_free(icl->num_regulators, icl->regulators);
1169
1170         return 0;
1171 }
1172
1173 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1174 {
1175         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1176         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1177         int ret = 0;
1178
1179         if (ici->ops->suspend)
1180                 ret = ici->ops->suspend(icd, state);
1181
1182         return ret;
1183 }
1184
1185 static int soc_camera_resume(struct device *dev)
1186 {
1187         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1188         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1189         int ret = 0;
1190
1191         if (ici->ops->resume)
1192                 ret = ici->ops->resume(icd);
1193
1194         return ret;
1195 }
1196
1197 struct bus_type soc_camera_bus_type = {
1198         .name           = "soc-camera",
1199         .probe          = soc_camera_probe,
1200         .remove         = soc_camera_remove,
1201         .suspend        = soc_camera_suspend,
1202         .resume         = soc_camera_resume,
1203 };
1204 EXPORT_SYMBOL_GPL(soc_camera_bus_type);
1205
1206 static struct device_driver ic_drv = {
1207         .name   = "camera",
1208         .bus    = &soc_camera_bus_type,
1209         .owner  = THIS_MODULE,
1210 };
1211
1212 static void dummy_release(struct device *dev)
1213 {
1214 }
1215
1216 static int default_cropcap(struct soc_camera_device *icd,
1217                            struct v4l2_cropcap *a)
1218 {
1219         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1220         return v4l2_subdev_call(sd, video, cropcap, a);
1221 }
1222
1223 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1224 {
1225         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1226         return v4l2_subdev_call(sd, video, g_crop, a);
1227 }
1228
1229 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1230 {
1231         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1232         return v4l2_subdev_call(sd, video, s_crop, a);
1233 }
1234
1235 static int default_g_parm(struct soc_camera_device *icd,
1236                           struct v4l2_streamparm *parm)
1237 {
1238         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1239         return v4l2_subdev_call(sd, video, g_parm, parm);
1240 }
1241
1242 static int default_s_parm(struct soc_camera_device *icd,
1243                           struct v4l2_streamparm *parm)
1244 {
1245         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1246         return v4l2_subdev_call(sd, video, s_parm, parm);
1247 }
1248
1249 static int default_enum_fsizes(struct soc_camera_device *icd,
1250                           struct v4l2_frmsizeenum *fsize)
1251 {
1252         int ret;
1253         struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1254         const struct soc_camera_format_xlate *xlate;
1255         __u32 pixfmt = fsize->pixel_format;
1256         struct v4l2_frmsizeenum fsize_mbus = *fsize;
1257
1258         xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1259         if (!xlate)
1260                 return -EINVAL;
1261         /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1262         fsize_mbus.pixel_format = xlate->code;
1263
1264         ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1265         if (ret < 0)
1266                 return ret;
1267
1268         *fsize = fsize_mbus;
1269         fsize->pixel_format = pixfmt;
1270
1271         return 0;
1272 }
1273
1274 static void soc_camera_device_init(struct device *dev, void *pdata)
1275 {
1276         dev->platform_data      = pdata;
1277         dev->bus                = &soc_camera_bus_type;
1278         dev->release            = dummy_release;
1279 }
1280
1281 int soc_camera_host_register(struct soc_camera_host *ici)
1282 {
1283         struct soc_camera_host *ix;
1284         int ret;
1285
1286         if (!ici || !ici->ops ||
1287             !ici->ops->try_fmt ||
1288             !ici->ops->set_fmt ||
1289             !ici->ops->set_bus_param ||
1290             !ici->ops->querycap ||
1291             ((!ici->ops->init_videobuf ||
1292               !ici->ops->reqbufs) &&
1293              !ici->ops->init_videobuf2) ||
1294             !ici->ops->add ||
1295             !ici->ops->remove ||
1296             !ici->ops->poll ||
1297             !ici->v4l2_dev.dev)
1298                 return -EINVAL;
1299
1300         if (!ici->ops->set_crop)
1301                 ici->ops->set_crop = default_s_crop;
1302         if (!ici->ops->get_crop)
1303                 ici->ops->get_crop = default_g_crop;
1304         if (!ici->ops->cropcap)
1305                 ici->ops->cropcap = default_cropcap;
1306         if (!ici->ops->set_parm)
1307                 ici->ops->set_parm = default_s_parm;
1308         if (!ici->ops->get_parm)
1309                 ici->ops->get_parm = default_g_parm;
1310         if (!ici->ops->enum_fsizes)
1311                 ici->ops->enum_fsizes = default_enum_fsizes;
1312
1313         mutex_lock(&list_lock);
1314         list_for_each_entry(ix, &hosts, list) {
1315                 if (ix->nr == ici->nr) {
1316                         ret = -EBUSY;
1317                         goto edevreg;
1318                 }
1319         }
1320
1321         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1322         if (ret < 0)
1323                 goto edevreg;
1324
1325         list_add_tail(&ici->list, &hosts);
1326         mutex_unlock(&list_lock);
1327
1328         scan_add_host(ici);
1329
1330         return 0;
1331
1332 edevreg:
1333         mutex_unlock(&list_lock);
1334         return ret;
1335 }
1336 EXPORT_SYMBOL(soc_camera_host_register);
1337
1338 /* Unregister all clients! */
1339 void soc_camera_host_unregister(struct soc_camera_host *ici)
1340 {
1341         struct soc_camera_device *icd;
1342
1343         mutex_lock(&list_lock);
1344
1345         list_del(&ici->list);
1346
1347         list_for_each_entry(icd, &devices, list) {
1348                 if (icd->iface == ici->nr) {
1349                         void *pdata = icd->dev.platform_data;
1350                         /* The bus->remove will be called */
1351                         device_unregister(&icd->dev);
1352                         /*
1353                          * Not before device_unregister(), .remove
1354                          * needs parent to call ici->ops->remove().
1355                          * If the host module is loaded again, device_register()
1356                          * would complain "already initialised," since 2.6.32
1357                          * this is also needed to prevent use-after-free of the
1358                          * device private data.
1359                          */
1360                         memset(&icd->dev, 0, sizeof(icd->dev));
1361                         soc_camera_device_init(&icd->dev, pdata);
1362                 }
1363         }
1364
1365         mutex_unlock(&list_lock);
1366
1367         v4l2_device_unregister(&ici->v4l2_dev);
1368 }
1369 EXPORT_SYMBOL(soc_camera_host_unregister);
1370
1371 /* Image capture device */
1372 static int soc_camera_device_register(struct soc_camera_device *icd)
1373 {
1374         struct soc_camera_device *ix;
1375         int num = -1, i;
1376
1377         for (i = 0; i < 256 && num < 0; i++) {
1378                 num = i;
1379                 /* Check if this index is available on this interface */
1380                 list_for_each_entry(ix, &devices, list) {
1381                         if (ix->iface == icd->iface && ix->devnum == i) {
1382                                 num = -1;
1383                                 break;
1384                         }
1385                 }
1386         }
1387
1388         if (num < 0)
1389                 /*
1390                  * ok, we have 256 cameras on this host...
1391                  * man, stay reasonable...
1392                  */
1393                 return -ENOMEM;
1394
1395         icd->devnum             = num;
1396         icd->use_count          = 0;
1397         icd->host_priv          = NULL;
1398         mutex_init(&icd->video_lock);
1399
1400         list_add_tail(&icd->list, &devices);
1401
1402         return 0;
1403 }
1404
1405 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1406 {
1407         list_del(&icd->list);
1408 }
1409
1410 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1411         .vidioc_querycap         = soc_camera_querycap,
1412         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1413         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1414         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1415         .vidioc_enum_input       = soc_camera_enum_input,
1416         .vidioc_g_input          = soc_camera_g_input,
1417         .vidioc_s_input          = soc_camera_s_input,
1418         .vidioc_s_std            = soc_camera_s_std,
1419         .vidioc_enum_framesizes  = soc_camera_enum_fsizes,
1420         .vidioc_reqbufs          = soc_camera_reqbufs,
1421         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1422         .vidioc_querybuf         = soc_camera_querybuf,
1423         .vidioc_qbuf             = soc_camera_qbuf,
1424         .vidioc_dqbuf            = soc_camera_dqbuf,
1425         .vidioc_streamon         = soc_camera_streamon,
1426         .vidioc_streamoff        = soc_camera_streamoff,
1427         .vidioc_queryctrl        = soc_camera_queryctrl,
1428         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1429         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1430         .vidioc_cropcap          = soc_camera_cropcap,
1431         .vidioc_g_crop           = soc_camera_g_crop,
1432         .vidioc_s_crop           = soc_camera_s_crop,
1433         .vidioc_g_parm           = soc_camera_g_parm,
1434         .vidioc_s_parm           = soc_camera_s_parm,
1435         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1436 #ifdef CONFIG_VIDEO_ADV_DEBUG
1437         .vidioc_g_register       = soc_camera_g_register,
1438         .vidioc_s_register       = soc_camera_s_register,
1439 #endif
1440 };
1441
1442 static int video_dev_create(struct soc_camera_device *icd)
1443 {
1444         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1445         struct video_device *vdev = video_device_alloc();
1446
1447         if (!vdev)
1448                 return -ENOMEM;
1449
1450         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1451
1452         vdev->parent            = &icd->dev;
1453         vdev->current_norm      = V4L2_STD_UNKNOWN;
1454         vdev->fops              = &soc_camera_fops;
1455         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1456         vdev->release           = video_device_release;
1457         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1458
1459         icd->vdev = vdev;
1460
1461         return 0;
1462 }
1463
1464 /*
1465  * Called from soc_camera_probe() above (with .video_lock held???)
1466  */
1467 static int soc_camera_video_start(struct soc_camera_device *icd)
1468 {
1469         struct device_type *type = icd->vdev->dev.type;
1470         int ret;
1471
1472         if (!icd->dev.parent)
1473                 return -ENODEV;
1474
1475         if (!icd->ops ||
1476             !icd->ops->query_bus_param ||
1477             !icd->ops->set_bus_param)
1478                 return -EINVAL;
1479
1480         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1481         if (ret < 0) {
1482                 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1483                 return ret;
1484         }
1485
1486         /* Restore device type, possibly set by the subdevice driver */
1487         icd->vdev->dev.type = type;
1488
1489         return 0;
1490 }
1491
1492 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1493 {
1494         struct soc_camera_link *icl = pdev->dev.platform_data;
1495         struct soc_camera_device *icd;
1496         int ret;
1497
1498         if (!icl)
1499                 return -EINVAL;
1500
1501         icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1502         if (!icd)
1503                 return -ENOMEM;
1504
1505         icd->iface = icl->bus_id;
1506         icd->pdev = &pdev->dev;
1507         platform_set_drvdata(pdev, icd);
1508
1509         ret = soc_camera_device_register(icd);
1510         if (ret < 0)
1511                 goto escdevreg;
1512
1513         soc_camera_device_init(&icd->dev, icl);
1514
1515         icd->user_width         = DEFAULT_WIDTH;
1516         icd->user_height        = DEFAULT_HEIGHT;
1517
1518         return 0;
1519
1520 escdevreg:
1521         kfree(icd);
1522
1523         return ret;
1524 }
1525
1526 /*
1527  * Only called on rmmod for each platform device, since they are not
1528  * hot-pluggable. Now we know, that all our users - hosts and devices have
1529  * been unloaded already
1530  */
1531 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1532 {
1533         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1534
1535         if (!icd)
1536                 return -EINVAL;
1537
1538         soc_camera_device_unregister(icd);
1539
1540         kfree(icd);
1541
1542         return 0;
1543 }
1544
1545 static struct platform_driver __refdata soc_camera_pdrv = {
1546         .remove  = __devexit_p(soc_camera_pdrv_remove),
1547         .driver  = {
1548                 .name   = "soc-camera-pdrv",
1549                 .owner  = THIS_MODULE,
1550         },
1551 };
1552
1553 static int __init soc_camera_init(void)
1554 {
1555         int ret = bus_register(&soc_camera_bus_type);
1556         if (ret)
1557                 return ret;
1558         ret = driver_register(&ic_drv);
1559         if (ret)
1560                 goto edrvr;
1561
1562         ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1563         if (ret)
1564                 goto epdr;
1565
1566         return 0;
1567
1568 epdr:
1569         driver_unregister(&ic_drv);
1570 edrvr:
1571         bus_unregister(&soc_camera_bus_type);
1572         return ret;
1573 }
1574
1575 static void __exit soc_camera_exit(void)
1576 {
1577         platform_driver_unregister(&soc_camera_pdrv);
1578         driver_unregister(&ic_drv);
1579         bus_unregister(&soc_camera_bus_type);
1580 }
1581
1582 module_init(soc_camera_init);
1583 module_exit(soc_camera_exit);
1584
1585 MODULE_DESCRIPTION("Image capture bus driver");
1586 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1587 MODULE_LICENSE("GPL");
1588 MODULE_ALIAS("platform:soc-camera-pdrv");