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