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