V4L/DVB (10087): Add new enum_input function on soc_camera
[safe/jmp/linux-2.6] / 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/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/vmalloc.h>
26
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf-core.h>
31 #include <media/soc_camera.h>
32
33 static LIST_HEAD(hosts);
34 static LIST_HEAD(devices);
35 static DEFINE_MUTEX(list_lock);
36
37 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
38         struct soc_camera_device *icd, unsigned int fourcc)
39 {
40         unsigned int i;
41
42         for (i = 0; i < icd->num_formats; i++)
43                 if (icd->formats[i].fourcc == fourcc)
44                         return icd->formats + i;
45         return NULL;
46 }
47 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
48
49 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
50         struct soc_camera_device *icd, unsigned int fourcc)
51 {
52         unsigned int i;
53
54         for (i = 0; i < icd->num_user_formats; i++)
55                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
56                         return icd->user_formats + i;
57         return NULL;
58 }
59 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
60
61 /**
62  * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
63  * @icl:        camera platform parameters
64  * @flags:      flags to be inverted according to platform configuration
65  * @return:     resulting flags
66  */
67 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
68                                             unsigned long flags)
69 {
70         unsigned long f;
71
72         /* If only one of the two polarities is supported, switch to the opposite */
73         if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
74                 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
75                 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
76                         flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
77         }
78
79         if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
80                 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
81                 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
82                         flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
83         }
84
85         if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
86                 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
87                 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
88                         flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
89         }
90
91         return flags;
92 }
93 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
94
95 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
96                                       struct v4l2_format *f)
97 {
98         struct soc_camera_file *icf = file->private_data;
99         struct soc_camera_device *icd = icf->icd;
100         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
101         enum v4l2_field field;
102         int ret;
103
104         WARN_ON(priv != file->private_data);
105
106         /*
107          * TODO: this might also have to migrate to host-drivers, if anyone
108          * wishes to support other fields
109          */
110         field = f->fmt.pix.field;
111
112         if (field == V4L2_FIELD_ANY) {
113                 f->fmt.pix.field = V4L2_FIELD_NONE;
114         } else if (field != V4L2_FIELD_NONE) {
115                 dev_err(&icd->dev, "Field type invalid.\n");
116                 return -EINVAL;
117         }
118
119         /* limit format to hardware capabilities */
120         ret = ici->ops->try_fmt(icd, f);
121
122         return ret;
123 }
124
125 static int soc_camera_enum_input(struct file *file, void *priv,
126                                  struct v4l2_input *inp)
127 {
128         struct soc_camera_file *icf = file->private_data;
129         struct soc_camera_device *icd = icf->icd;
130         int ret = 0;
131
132         if (inp->index != 0)
133                 return -EINVAL;
134
135         if (icd->ops->enum_input)
136                 ret = icd->ops->enum_input(icd, inp);
137         else {
138                 /* default is camera */
139                 inp->type = V4L2_INPUT_TYPE_CAMERA;
140                 inp->std  = V4L2_STD_UNKNOWN;
141                 strcpy(inp->name, "Camera");
142         }
143
144         return ret;
145 }
146
147 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
148 {
149         *i = 0;
150
151         return 0;
152 }
153
154 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
155 {
156         if (i > 0)
157                 return -EINVAL;
158
159         return 0;
160 }
161
162 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
163 {
164         struct soc_camera_file *icf = file->private_data;
165         struct soc_camera_device *icd = icf->icd;
166         int ret = 0;
167
168         if (icd->ops->set_std)
169                 ret = icd->ops->set_std(icd, a);
170
171         return ret;
172 }
173
174 static int soc_camera_reqbufs(struct file *file, void *priv,
175                               struct v4l2_requestbuffers *p)
176 {
177         int ret;
178         struct soc_camera_file *icf = file->private_data;
179         struct soc_camera_device *icd = icf->icd;
180         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
181
182         WARN_ON(priv != file->private_data);
183
184         dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
185
186         ret = videobuf_reqbufs(&icf->vb_vidq, p);
187         if (ret < 0)
188                 return ret;
189
190         return ici->ops->reqbufs(icf, p);
191 }
192
193 static int soc_camera_querybuf(struct file *file, void *priv,
194                                struct v4l2_buffer *p)
195 {
196         struct soc_camera_file *icf = file->private_data;
197
198         WARN_ON(priv != file->private_data);
199
200         return videobuf_querybuf(&icf->vb_vidq, p);
201 }
202
203 static int soc_camera_qbuf(struct file *file, void *priv,
204                            struct v4l2_buffer *p)
205 {
206         struct soc_camera_file *icf = file->private_data;
207
208         WARN_ON(priv != file->private_data);
209
210         return videobuf_qbuf(&icf->vb_vidq, p);
211 }
212
213 static int soc_camera_dqbuf(struct file *file, void *priv,
214                             struct v4l2_buffer *p)
215 {
216         struct soc_camera_file *icf = file->private_data;
217
218         WARN_ON(priv != file->private_data);
219
220         return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
221 }
222
223 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
224 {
225         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
226         int i, fmts = 0;
227
228         if (!ici->ops->get_formats)
229                 /*
230                  * Fallback mode - the host will have to serve all
231                  * sensor-provided formats one-to-one to the user
232                  */
233                 fmts = icd->num_formats;
234         else
235                 /*
236                  * First pass - only count formats this host-sensor
237                  * configuration can provide
238                  */
239                 for (i = 0; i < icd->num_formats; i++)
240                         fmts += ici->ops->get_formats(icd, i, NULL);
241
242         if (!fmts)
243                 return -ENXIO;
244
245         icd->user_formats =
246                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
247         if (!icd->user_formats)
248                 return -ENOMEM;
249
250         icd->num_user_formats = fmts;
251         fmts = 0;
252
253         dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
254
255         /* Second pass - actually fill data formats */
256         for (i = 0; i < icd->num_formats; i++)
257                 if (!ici->ops->get_formats) {
258                         icd->user_formats[i].host_fmt = icd->formats + i;
259                         icd->user_formats[i].cam_fmt = icd->formats + i;
260                         icd->user_formats[i].buswidth = icd->formats[i].depth;
261                 } else {
262                         fmts += ici->ops->get_formats(icd, i,
263                                                       &icd->user_formats[fmts]);
264                 }
265
266         icd->current_fmt = icd->user_formats[0].host_fmt;
267
268         return 0;
269 }
270
271 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
272 {
273         vfree(icd->user_formats);
274 }
275
276 static int soc_camera_open(struct inode *inode, struct file *file)
277 {
278         struct video_device *vdev;
279         struct soc_camera_device *icd;
280         struct soc_camera_host *ici;
281         struct soc_camera_file *icf;
282         int ret;
283
284         icf = vmalloc(sizeof(*icf));
285         if (!icf)
286                 return -ENOMEM;
287
288         /*
289          * It is safe to dereference these pointers now as long as a user has
290          * the video device open - we are protected by the held cdev reference.
291          */
292
293         vdev = video_devdata(file);
294         icd = container_of(vdev->parent, struct soc_camera_device, dev);
295         ici = to_soc_camera_host(icd->dev.parent);
296
297         if (!try_module_get(icd->ops->owner)) {
298                 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
299                 ret = -EINVAL;
300                 goto emgd;
301         }
302
303         if (!try_module_get(ici->ops->owner)) {
304                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
305                 ret = -EINVAL;
306                 goto emgi;
307         }
308
309         /* Protect against icd->remove() until we module_get() both drivers. */
310         mutex_lock(&icd->video_lock);
311
312         icf->icd = icd;
313         icd->use_count++;
314
315         /* Now we really have to activate the camera */
316         if (icd->use_count == 1) {
317                 ret = soc_camera_init_user_formats(icd);
318                 if (ret < 0)
319                         goto eiufmt;
320                 ret = ici->ops->add(icd);
321                 if (ret < 0) {
322                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
323                         goto eiciadd;
324                 }
325         }
326
327         mutex_unlock(&icd->video_lock);
328
329         file->private_data = icf;
330         dev_dbg(&icd->dev, "camera device open\n");
331
332         ici->ops->init_videobuf(&icf->vb_vidq, icd);
333
334         return 0;
335
336         /* First two errors are entered with the .video_lock held */
337 eiciadd:
338         soc_camera_free_user_formats(icd);
339 eiufmt:
340         icd->use_count--;
341         mutex_unlock(&icd->video_lock);
342         module_put(ici->ops->owner);
343 emgi:
344         module_put(icd->ops->owner);
345 emgd:
346         vfree(icf);
347         return ret;
348 }
349
350 static int soc_camera_close(struct inode *inode, struct file *file)
351 {
352         struct soc_camera_file *icf = file->private_data;
353         struct soc_camera_device *icd = icf->icd;
354         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
355         struct video_device *vdev = icd->vdev;
356
357         mutex_lock(&icd->video_lock);
358         icd->use_count--;
359         if (!icd->use_count) {
360                 ici->ops->remove(icd);
361                 soc_camera_free_user_formats(icd);
362         }
363         mutex_unlock(&icd->video_lock);
364
365         module_put(icd->ops->owner);
366         module_put(ici->ops->owner);
367
368         vfree(icf);
369
370         dev_dbg(vdev->parent, "camera device close\n");
371
372         return 0;
373 }
374
375 static ssize_t soc_camera_read(struct file *file, char __user *buf,
376                                size_t count, loff_t *ppos)
377 {
378         struct soc_camera_file *icf = file->private_data;
379         struct soc_camera_device *icd = icf->icd;
380         struct video_device *vdev = icd->vdev;
381         int err = -EINVAL;
382
383         dev_err(vdev->parent, "camera device read not implemented\n");
384
385         return err;
386 }
387
388 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
389 {
390         struct soc_camera_file *icf = file->private_data;
391         struct soc_camera_device *icd = icf->icd;
392         int err;
393
394         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
395
396         err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
397
398         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
399                 (unsigned long)vma->vm_start,
400                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
401                 err);
402
403         return err;
404 }
405
406 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
407 {
408         struct soc_camera_file *icf = file->private_data;
409         struct soc_camera_device *icd = icf->icd;
410         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
411
412         if (list_empty(&icf->vb_vidq.stream)) {
413                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
414                 return POLLERR;
415         }
416
417         return ici->ops->poll(file, pt);
418 }
419
420 static struct file_operations soc_camera_fops = {
421         .owner          = THIS_MODULE,
422         .open           = soc_camera_open,
423         .release        = soc_camera_close,
424         .ioctl          = video_ioctl2,
425         .read           = soc_camera_read,
426         .mmap           = soc_camera_mmap,
427         .poll           = soc_camera_poll,
428         .llseek         = no_llseek,
429 };
430
431 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
432                                     struct v4l2_format *f)
433 {
434         struct soc_camera_file *icf = file->private_data;
435         struct soc_camera_device *icd = icf->icd;
436         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
437         struct v4l2_pix_format *pix = &f->fmt.pix;
438         __u32 pixfmt = pix->pixelformat;
439         int ret;
440         struct v4l2_rect rect;
441
442         WARN_ON(priv != file->private_data);
443
444         ret = soc_camera_try_fmt_vid_cap(file, priv, f);
445         if (ret < 0)
446                 return ret;
447
448         mutex_lock(&icf->vb_vidq.vb_lock);
449
450         if (videobuf_queue_is_busy(&icf->vb_vidq)) {
451                 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
452                 ret = -EBUSY;
453                 goto unlock;
454         }
455
456         rect.left       = icd->x_current;
457         rect.top        = icd->y_current;
458         rect.width      = pix->width;
459         rect.height     = pix->height;
460         ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
461         if (ret < 0) {
462                 goto unlock;
463         } else if (!icd->current_fmt ||
464                    icd->current_fmt->fourcc != pixfmt) {
465                 dev_err(&ici->dev,
466                         "Host driver hasn't set up current format correctly!\n");
467                 ret = -EINVAL;
468                 goto unlock;
469         }
470
471         icd->width              = rect.width;
472         icd->height             = rect.height;
473         icf->vb_vidq.field      = pix->field;
474         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
475                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
476                          f->type);
477
478         dev_dbg(&icd->dev, "set width: %d height: %d\n",
479                 icd->width, icd->height);
480
481         /* set physical bus parameters */
482         ret = ici->ops->set_bus_param(icd, pixfmt);
483
484 unlock:
485         mutex_unlock(&icf->vb_vidq.vb_lock);
486
487         return ret;
488 }
489
490 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
491                                        struct v4l2_fmtdesc *f)
492 {
493         struct soc_camera_file *icf = file->private_data;
494         struct soc_camera_device *icd = icf->icd;
495         const struct soc_camera_data_format *format;
496
497         WARN_ON(priv != file->private_data);
498
499         if (f->index >= icd->num_user_formats)
500                 return -EINVAL;
501
502         format = icd->user_formats[f->index].host_fmt;
503
504         strlcpy(f->description, format->name, sizeof(f->description));
505         f->pixelformat = format->fourcc;
506         return 0;
507 }
508
509 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
510                                     struct v4l2_format *f)
511 {
512         struct soc_camera_file *icf = file->private_data;
513         struct soc_camera_device *icd = icf->icd;
514         struct v4l2_pix_format *pix = &f->fmt.pix;
515
516         WARN_ON(priv != file->private_data);
517
518         pix->width              = icd->width;
519         pix->height             = icd->height;
520         pix->field              = icf->vb_vidq.field;
521         pix->pixelformat        = icd->current_fmt->fourcc;
522         pix->bytesperline       = pix->width *
523                 DIV_ROUND_UP(icd->current_fmt->depth, 8);
524         pix->sizeimage          = pix->height * pix->bytesperline;
525         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
526                 icd->current_fmt->fourcc);
527         return 0;
528 }
529
530 static int soc_camera_querycap(struct file *file, void  *priv,
531                                struct v4l2_capability *cap)
532 {
533         struct soc_camera_file *icf = file->private_data;
534         struct soc_camera_device *icd = icf->icd;
535         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
536
537         WARN_ON(priv != file->private_data);
538
539         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
540         return ici->ops->querycap(ici, cap);
541 }
542
543 static int soc_camera_streamon(struct file *file, void *priv,
544                                enum v4l2_buf_type i)
545 {
546         struct soc_camera_file *icf = file->private_data;
547         struct soc_camera_device *icd = icf->icd;
548         int ret;
549
550         WARN_ON(priv != file->private_data);
551
552         dev_dbg(&icd->dev, "%s\n", __func__);
553
554         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
555                 return -EINVAL;
556
557         mutex_lock(&icd->video_lock);
558
559         icd->ops->start_capture(icd);
560
561         /* This calls buf_queue from host driver's videobuf_queue_ops */
562         ret = videobuf_streamon(&icf->vb_vidq);
563
564         mutex_unlock(&icd->video_lock);
565
566         return ret;
567 }
568
569 static int soc_camera_streamoff(struct file *file, void *priv,
570                                 enum v4l2_buf_type i)
571 {
572         struct soc_camera_file *icf = file->private_data;
573         struct soc_camera_device *icd = icf->icd;
574
575         WARN_ON(priv != file->private_data);
576
577         dev_dbg(&icd->dev, "%s\n", __func__);
578
579         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
580                 return -EINVAL;
581
582         mutex_lock(&icd->video_lock);
583
584         /* This calls buf_release from host driver's videobuf_queue_ops for all
585          * remaining buffers. When the last buffer is freed, stop capture */
586         videobuf_streamoff(&icf->vb_vidq);
587
588         icd->ops->stop_capture(icd);
589
590         mutex_unlock(&icd->video_lock);
591
592         return 0;
593 }
594
595 static int soc_camera_queryctrl(struct file *file, void *priv,
596                                 struct v4l2_queryctrl *qc)
597 {
598         struct soc_camera_file *icf = file->private_data;
599         struct soc_camera_device *icd = icf->icd;
600         int i;
601
602         WARN_ON(priv != file->private_data);
603
604         if (!qc->id)
605                 return -EINVAL;
606
607         for (i = 0; i < icd->ops->num_controls; i++)
608                 if (qc->id == icd->ops->controls[i].id) {
609                         memcpy(qc, &(icd->ops->controls[i]),
610                                 sizeof(*qc));
611                         return 0;
612                 }
613
614         return -EINVAL;
615 }
616
617 static int soc_camera_g_ctrl(struct file *file, void *priv,
618                              struct v4l2_control *ctrl)
619 {
620         struct soc_camera_file *icf = file->private_data;
621         struct soc_camera_device *icd = icf->icd;
622
623         WARN_ON(priv != file->private_data);
624
625         switch (ctrl->id) {
626         case V4L2_CID_GAIN:
627                 if (icd->gain == (unsigned short)~0)
628                         return -EINVAL;
629                 ctrl->value = icd->gain;
630                 return 0;
631         case V4L2_CID_EXPOSURE:
632                 if (icd->exposure == (unsigned short)~0)
633                         return -EINVAL;
634                 ctrl->value = icd->exposure;
635                 return 0;
636         }
637
638         if (icd->ops->get_control)
639                 return icd->ops->get_control(icd, ctrl);
640         return -EINVAL;
641 }
642
643 static int soc_camera_s_ctrl(struct file *file, void *priv,
644                              struct v4l2_control *ctrl)
645 {
646         struct soc_camera_file *icf = file->private_data;
647         struct soc_camera_device *icd = icf->icd;
648
649         WARN_ON(priv != file->private_data);
650
651         if (icd->ops->set_control)
652                 return icd->ops->set_control(icd, ctrl);
653         return -EINVAL;
654 }
655
656 static int soc_camera_cropcap(struct file *file, void *fh,
657                               struct v4l2_cropcap *a)
658 {
659         struct soc_camera_file *icf = file->private_data;
660         struct soc_camera_device *icd = icf->icd;
661
662         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
663         a->bounds.left                  = icd->x_min;
664         a->bounds.top                   = icd->y_min;
665         a->bounds.width                 = icd->width_max;
666         a->bounds.height                = icd->height_max;
667         a->defrect.left                 = icd->x_min;
668         a->defrect.top                  = icd->y_min;
669         a->defrect.width                = 640;
670         a->defrect.height               = 480;
671         a->pixelaspect.numerator        = 1;
672         a->pixelaspect.denominator      = 1;
673
674         return 0;
675 }
676
677 static int soc_camera_g_crop(struct file *file, void *fh,
678                              struct v4l2_crop *a)
679 {
680         struct soc_camera_file *icf = file->private_data;
681         struct soc_camera_device *icd = icf->icd;
682
683         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
684         a->c.left       = icd->x_current;
685         a->c.top        = icd->y_current;
686         a->c.width      = icd->width;
687         a->c.height     = icd->height;
688
689         return 0;
690 }
691
692 static int soc_camera_s_crop(struct file *file, void *fh,
693                              struct v4l2_crop *a)
694 {
695         struct soc_camera_file *icf = file->private_data;
696         struct soc_camera_device *icd = icf->icd;
697         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
698         int ret;
699
700         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
701                 return -EINVAL;
702
703         /* Cropping is allowed during a running capture, guard consistency */
704         mutex_lock(&icf->vb_vidq.vb_lock);
705
706         ret = ici->ops->set_fmt(icd, 0, &a->c);
707         if (!ret) {
708                 icd->width      = a->c.width;
709                 icd->height     = a->c.height;
710                 icd->x_current  = a->c.left;
711                 icd->y_current  = a->c.top;
712         }
713
714         mutex_unlock(&icf->vb_vidq.vb_lock);
715
716         return ret;
717 }
718
719 static int soc_camera_g_chip_ident(struct file *file, void *fh,
720                                    struct v4l2_chip_ident *id)
721 {
722         struct soc_camera_file *icf = file->private_data;
723         struct soc_camera_device *icd = icf->icd;
724
725         if (!icd->ops->get_chip_id)
726                 return -EINVAL;
727
728         return icd->ops->get_chip_id(icd, id);
729 }
730
731 #ifdef CONFIG_VIDEO_ADV_DEBUG
732 static int soc_camera_g_register(struct file *file, void *fh,
733                                  struct v4l2_register *reg)
734 {
735         struct soc_camera_file *icf = file->private_data;
736         struct soc_camera_device *icd = icf->icd;
737
738         if (!icd->ops->get_register)
739                 return -EINVAL;
740
741         return icd->ops->get_register(icd, reg);
742 }
743
744 static int soc_camera_s_register(struct file *file, void *fh,
745                                  struct v4l2_register *reg)
746 {
747         struct soc_camera_file *icf = file->private_data;
748         struct soc_camera_device *icd = icf->icd;
749
750         if (!icd->ops->set_register)
751                 return -EINVAL;
752
753         return icd->ops->set_register(icd, reg);
754 }
755 #endif
756
757 static int device_register_link(struct soc_camera_device *icd)
758 {
759         int ret = device_register(&icd->dev);
760
761         if (ret < 0) {
762                 /* Prevent calling device_unregister() */
763                 icd->dev.parent = NULL;
764                 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
765         /* Even if probe() was unsuccessful for all registered drivers,
766          * device_register() returns 0, and we add the link, just to
767          * document this camera's control device */
768         } else if (icd->control)
769                 /* Have to sysfs_remove_link() before device_unregister()? */
770                 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
771                                       "control"))
772                         dev_warn(&icd->dev,
773                                  "Failed creating the control symlink\n");
774         return ret;
775 }
776
777 /* So far this function cannot fail */
778 static void scan_add_host(struct soc_camera_host *ici)
779 {
780         struct soc_camera_device *icd;
781
782         mutex_lock(&list_lock);
783
784         list_for_each_entry(icd, &devices, list) {
785                 if (icd->iface == ici->nr) {
786                         icd->dev.parent = &ici->dev;
787                         device_register_link(icd);
788                 }
789         }
790
791         mutex_unlock(&list_lock);
792 }
793
794 /* return: 0 if no match found or a match found and
795  * device_register() successful, error code otherwise */
796 static int scan_add_device(struct soc_camera_device *icd)
797 {
798         struct soc_camera_host *ici;
799         int ret = 0;
800
801         mutex_lock(&list_lock);
802
803         list_add_tail(&icd->list, &devices);
804
805         /* Watch out for class_for_each_device / class_find_device API by
806          * Dave Young <hidave.darkstar@gmail.com> */
807         list_for_each_entry(ici, &hosts, list) {
808                 if (icd->iface == ici->nr) {
809                         ret = 1;
810                         icd->dev.parent = &ici->dev;
811                         break;
812                 }
813         }
814
815         mutex_unlock(&list_lock);
816
817         if (ret)
818                 ret = device_register_link(icd);
819
820         return ret;
821 }
822
823 static int soc_camera_probe(struct device *dev)
824 {
825         struct soc_camera_device *icd = to_soc_camera_dev(dev);
826         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
827         int ret;
828
829         /*
830          * Possible race scenario:
831          * modprobe <camera-host-driver> triggers __func__
832          * at this moment respective <camera-sensor-driver> gets rmmod'ed
833          * to protect take module references.
834          */
835
836         if (!try_module_get(icd->ops->owner)) {
837                 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
838                 ret = -EINVAL;
839                 goto emgd;
840         }
841
842         if (!try_module_get(ici->ops->owner)) {
843                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
844                 ret = -EINVAL;
845                 goto emgi;
846         }
847
848         mutex_lock(&icd->video_lock);
849
850         /* We only call ->add() here to activate and probe the camera.
851          * We shall ->remove() and deactivate it immediately afterwards. */
852         ret = ici->ops->add(icd);
853         if (ret < 0)
854                 goto eiadd;
855
856         ret = icd->ops->probe(icd);
857         if (ret >= 0) {
858                 const struct v4l2_queryctrl *qctrl;
859
860                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
861                 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
862                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
863                 icd->exposure = qctrl ? qctrl->default_value :
864                         (unsigned short)~0;
865         }
866         ici->ops->remove(icd);
867
868 eiadd:
869         mutex_unlock(&icd->video_lock);
870         module_put(ici->ops->owner);
871 emgi:
872         module_put(icd->ops->owner);
873 emgd:
874         return ret;
875 }
876
877 /* This is called on device_unregister, which only means we have to disconnect
878  * from the host, but not remove ourselves from the device list */
879 static int soc_camera_remove(struct device *dev)
880 {
881         struct soc_camera_device *icd = to_soc_camera_dev(dev);
882
883         if (icd->ops->remove)
884                 icd->ops->remove(icd);
885
886         return 0;
887 }
888
889 static int soc_camera_suspend(struct device *dev, pm_message_t state)
890 {
891         struct soc_camera_device *icd = to_soc_camera_dev(dev);
892         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
893         int ret = 0;
894
895         if (ici->ops->suspend)
896                 ret = ici->ops->suspend(icd, state);
897
898         return ret;
899 }
900
901 static int soc_camera_resume(struct device *dev)
902 {
903         struct soc_camera_device *icd = to_soc_camera_dev(dev);
904         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
905         int ret = 0;
906
907         if (ici->ops->resume)
908                 ret = ici->ops->resume(icd);
909
910         return ret;
911 }
912
913 static struct bus_type soc_camera_bus_type = {
914         .name           = "soc-camera",
915         .probe          = soc_camera_probe,
916         .remove         = soc_camera_remove,
917         .suspend        = soc_camera_suspend,
918         .resume         = soc_camera_resume,
919 };
920
921 static struct device_driver ic_drv = {
922         .name   = "camera",
923         .bus    = &soc_camera_bus_type,
924         .owner  = THIS_MODULE,
925 };
926
927 static void dummy_release(struct device *dev)
928 {
929 }
930
931 int soc_camera_host_register(struct soc_camera_host *ici)
932 {
933         int ret;
934         struct soc_camera_host *ix;
935
936         if (!ici || !ici->ops ||
937             !ici->ops->try_fmt ||
938             !ici->ops->set_fmt ||
939             !ici->ops->set_bus_param ||
940             !ici->ops->querycap ||
941             !ici->ops->init_videobuf ||
942             !ici->ops->reqbufs ||
943             !ici->ops->add ||
944             !ici->ops->remove ||
945             !ici->ops->poll)
946                 return -EINVAL;
947
948         /* Number might be equal to the platform device ID */
949         dev_set_name(&ici->dev, "camera_host%d", ici->nr);
950
951         mutex_lock(&list_lock);
952         list_for_each_entry(ix, &hosts, list) {
953                 if (ix->nr == ici->nr) {
954                         mutex_unlock(&list_lock);
955                         return -EBUSY;
956                 }
957         }
958
959         list_add_tail(&ici->list, &hosts);
960         mutex_unlock(&list_lock);
961
962         ici->dev.release = dummy_release;
963
964         ret = device_register(&ici->dev);
965
966         if (ret)
967                 goto edevr;
968
969         scan_add_host(ici);
970
971         return 0;
972
973 edevr:
974         mutex_lock(&list_lock);
975         list_del(&ici->list);
976         mutex_unlock(&list_lock);
977
978         return ret;
979 }
980 EXPORT_SYMBOL(soc_camera_host_register);
981
982 /* Unregister all clients! */
983 void soc_camera_host_unregister(struct soc_camera_host *ici)
984 {
985         struct soc_camera_device *icd;
986
987         mutex_lock(&list_lock);
988
989         list_del(&ici->list);
990
991         list_for_each_entry(icd, &devices, list) {
992                 if (icd->dev.parent == &ici->dev) {
993                         device_unregister(&icd->dev);
994                         /* Not before device_unregister(), .remove
995                          * needs parent to call ici->ops->remove() */
996                         icd->dev.parent = NULL;
997                         memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
998                 }
999         }
1000
1001         mutex_unlock(&list_lock);
1002
1003         device_unregister(&ici->dev);
1004 }
1005 EXPORT_SYMBOL(soc_camera_host_unregister);
1006
1007 /* Image capture device */
1008 int soc_camera_device_register(struct soc_camera_device *icd)
1009 {
1010         struct soc_camera_device *ix;
1011         int num = -1, i;
1012
1013         if (!icd || !icd->ops ||
1014             !icd->ops->probe ||
1015             !icd->ops->init ||
1016             !icd->ops->release ||
1017             !icd->ops->start_capture ||
1018             !icd->ops->stop_capture ||
1019             !icd->ops->set_fmt ||
1020             !icd->ops->try_fmt ||
1021             !icd->ops->query_bus_param ||
1022             !icd->ops->set_bus_param)
1023                 return -EINVAL;
1024
1025         for (i = 0; i < 256 && num < 0; i++) {
1026                 num = i;
1027                 list_for_each_entry(ix, &devices, list) {
1028                         if (ix->iface == icd->iface && ix->devnum == i) {
1029                                 num = -1;
1030                                 break;
1031                         }
1032                 }
1033         }
1034
1035         if (num < 0)
1036                 /* ok, we have 256 cameras on this host...
1037                  * man, stay reasonable... */
1038                 return -ENOMEM;
1039
1040         icd->devnum = num;
1041         icd->dev.bus = &soc_camera_bus_type;
1042         dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
1043
1044         icd->dev.release        = dummy_release;
1045         icd->use_count          = 0;
1046         icd->host_priv          = NULL;
1047         mutex_init(&icd->video_lock);
1048
1049         return scan_add_device(icd);
1050 }
1051 EXPORT_SYMBOL(soc_camera_device_register);
1052
1053 void soc_camera_device_unregister(struct soc_camera_device *icd)
1054 {
1055         mutex_lock(&list_lock);
1056         list_del(&icd->list);
1057
1058         /* The bus->remove will be eventually called */
1059         if (icd->dev.parent)
1060                 device_unregister(&icd->dev);
1061         mutex_unlock(&list_lock);
1062 }
1063 EXPORT_SYMBOL(soc_camera_device_unregister);
1064
1065 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1066         .vidioc_querycap         = soc_camera_querycap,
1067         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1068         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1069         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1070         .vidioc_enum_input       = soc_camera_enum_input,
1071         .vidioc_g_input          = soc_camera_g_input,
1072         .vidioc_s_input          = soc_camera_s_input,
1073         .vidioc_s_std            = soc_camera_s_std,
1074         .vidioc_reqbufs          = soc_camera_reqbufs,
1075         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1076         .vidioc_querybuf         = soc_camera_querybuf,
1077         .vidioc_qbuf             = soc_camera_qbuf,
1078         .vidioc_dqbuf            = soc_camera_dqbuf,
1079         .vidioc_streamon         = soc_camera_streamon,
1080         .vidioc_streamoff        = soc_camera_streamoff,
1081         .vidioc_queryctrl        = soc_camera_queryctrl,
1082         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1083         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1084         .vidioc_cropcap          = soc_camera_cropcap,
1085         .vidioc_g_crop           = soc_camera_g_crop,
1086         .vidioc_s_crop           = soc_camera_s_crop,
1087         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1088 #ifdef CONFIG_VIDEO_ADV_DEBUG
1089         .vidioc_g_register       = soc_camera_g_register,
1090         .vidioc_s_register       = soc_camera_s_register,
1091 #endif
1092 };
1093
1094 /*
1095  * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1096  * soc_camera_probe() above with .video_lock held
1097  */
1098 int soc_camera_video_start(struct soc_camera_device *icd)
1099 {
1100         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1101         int err = -ENOMEM;
1102         struct video_device *vdev;
1103
1104         if (!icd->dev.parent)
1105                 return -ENODEV;
1106
1107         vdev = video_device_alloc();
1108         if (!vdev)
1109                 goto evidallocd;
1110         dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1111
1112         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1113
1114         vdev->parent            = &icd->dev;
1115         vdev->current_norm      = V4L2_STD_UNKNOWN;
1116         vdev->fops              = &soc_camera_fops;
1117         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1118         vdev->release           = video_device_release;
1119         vdev->minor             = -1;
1120         vdev->tvnorms           = V4L2_STD_UNKNOWN,
1121
1122         err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1123         if (err < 0) {
1124                 dev_err(vdev->parent, "video_register_device failed\n");
1125                 goto evidregd;
1126         }
1127         icd->vdev = vdev;
1128
1129         return 0;
1130
1131 evidregd:
1132         video_device_release(vdev);
1133 evidallocd:
1134         return err;
1135 }
1136 EXPORT_SYMBOL(soc_camera_video_start);
1137
1138 void soc_camera_video_stop(struct soc_camera_device *icd)
1139 {
1140         struct video_device *vdev = icd->vdev;
1141
1142         dev_dbg(&icd->dev, "%s\n", __func__);
1143
1144         if (!icd->dev.parent || !vdev)
1145                 return;
1146
1147         mutex_lock(&icd->video_lock);
1148         video_unregister_device(vdev);
1149         icd->vdev = NULL;
1150         mutex_unlock(&icd->video_lock);
1151 }
1152 EXPORT_SYMBOL(soc_camera_video_stop);
1153
1154 static int __init soc_camera_init(void)
1155 {
1156         int ret = bus_register(&soc_camera_bus_type);
1157         if (ret)
1158                 return ret;
1159         ret = driver_register(&ic_drv);
1160         if (ret)
1161                 goto edrvr;
1162
1163         return 0;
1164
1165 edrvr:
1166         bus_unregister(&soc_camera_bus_type);
1167         return ret;
1168 }
1169
1170 static void __exit soc_camera_exit(void)
1171 {
1172         driver_unregister(&ic_drv);
1173         bus_unregister(&soc_camera_bus_type);
1174 }
1175
1176 module_init(soc_camera_init);
1177 module_exit(soc_camera_exit);
1178
1179 MODULE_DESCRIPTION("Image capture bus driver");
1180 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1181 MODULE_LICENSE("GPL");