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