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