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