]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/soc_camera.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux...
[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 inode *inode, 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 inode *inode, 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 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         .llseek         = no_llseek,
412 };
413
414 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
415                                     struct v4l2_format *f)
416 {
417         struct soc_camera_file *icf = file->private_data;
418         struct soc_camera_device *icd = icf->icd;
419         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
420         struct v4l2_pix_format *pix = &f->fmt.pix;
421         __u32 pixfmt = pix->pixelformat;
422         int ret;
423         struct v4l2_rect rect;
424
425         WARN_ON(priv != file->private_data);
426
427         ret = soc_camera_try_fmt_vid_cap(file, priv, f);
428         if (ret < 0)
429                 return ret;
430
431         mutex_lock(&icf->vb_vidq.vb_lock);
432
433         if (videobuf_queue_is_busy(&icf->vb_vidq)) {
434                 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
435                 ret = -EBUSY;
436                 goto unlock;
437         }
438
439         rect.left       = icd->x_current;
440         rect.top        = icd->y_current;
441         rect.width      = pix->width;
442         rect.height     = pix->height;
443         ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
444         if (ret < 0) {
445                 goto unlock;
446         } else if (!icd->current_fmt ||
447                    icd->current_fmt->fourcc != pixfmt) {
448                 dev_err(&ici->dev,
449                         "Host driver hasn't set up current format correctly!\n");
450                 ret = -EINVAL;
451                 goto unlock;
452         }
453
454         icd->width              = rect.width;
455         icd->height             = rect.height;
456         icf->vb_vidq.field      = pix->field;
457         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
458                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
459                          f->type);
460
461         dev_dbg(&icd->dev, "set width: %d height: %d\n",
462                 icd->width, icd->height);
463
464         /* set physical bus parameters */
465         ret = ici->ops->set_bus_param(icd, pixfmt);
466
467 unlock:
468         mutex_unlock(&icf->vb_vidq.vb_lock);
469
470         return ret;
471 }
472
473 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
474                                        struct v4l2_fmtdesc *f)
475 {
476         struct soc_camera_file *icf = file->private_data;
477         struct soc_camera_device *icd = icf->icd;
478         const struct soc_camera_data_format *format;
479
480         WARN_ON(priv != file->private_data);
481
482         if (f->index >= icd->num_user_formats)
483                 return -EINVAL;
484
485         format = icd->user_formats[f->index].host_fmt;
486
487         strlcpy(f->description, format->name, sizeof(f->description));
488         f->pixelformat = format->fourcc;
489         return 0;
490 }
491
492 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
493                                     struct v4l2_format *f)
494 {
495         struct soc_camera_file *icf = file->private_data;
496         struct soc_camera_device *icd = icf->icd;
497         struct v4l2_pix_format *pix = &f->fmt.pix;
498
499         WARN_ON(priv != file->private_data);
500
501         pix->width              = icd->width;
502         pix->height             = icd->height;
503         pix->field              = icf->vb_vidq.field;
504         pix->pixelformat        = icd->current_fmt->fourcc;
505         pix->bytesperline       = pix->width *
506                 DIV_ROUND_UP(icd->current_fmt->depth, 8);
507         pix->sizeimage          = pix->height * pix->bytesperline;
508         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
509                 icd->current_fmt->fourcc);
510         return 0;
511 }
512
513 static int soc_camera_querycap(struct file *file, void  *priv,
514                                struct v4l2_capability *cap)
515 {
516         struct soc_camera_file *icf = file->private_data;
517         struct soc_camera_device *icd = icf->icd;
518         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
519
520         WARN_ON(priv != file->private_data);
521
522         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
523         return ici->ops->querycap(ici, cap);
524 }
525
526 static int soc_camera_streamon(struct file *file, void *priv,
527                                enum v4l2_buf_type i)
528 {
529         struct soc_camera_file *icf = file->private_data;
530         struct soc_camera_device *icd = icf->icd;
531         int ret;
532
533         WARN_ON(priv != file->private_data);
534
535         dev_dbg(&icd->dev, "%s\n", __func__);
536
537         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
538                 return -EINVAL;
539
540         mutex_lock(&icd->video_lock);
541
542         icd->ops->start_capture(icd);
543
544         /* This calls buf_queue from host driver's videobuf_queue_ops */
545         ret = videobuf_streamon(&icf->vb_vidq);
546
547         mutex_unlock(&icd->video_lock);
548
549         return ret;
550 }
551
552 static int soc_camera_streamoff(struct file *file, void *priv,
553                                 enum v4l2_buf_type i)
554 {
555         struct soc_camera_file *icf = file->private_data;
556         struct soc_camera_device *icd = icf->icd;
557
558         WARN_ON(priv != file->private_data);
559
560         dev_dbg(&icd->dev, "%s\n", __func__);
561
562         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
563                 return -EINVAL;
564
565         mutex_lock(&icd->video_lock);
566
567         /* This calls buf_release from host driver's videobuf_queue_ops for all
568          * remaining buffers. When the last buffer is freed, stop capture */
569         videobuf_streamoff(&icf->vb_vidq);
570
571         icd->ops->stop_capture(icd);
572
573         mutex_unlock(&icd->video_lock);
574
575         return 0;
576 }
577
578 static int soc_camera_queryctrl(struct file *file, void *priv,
579                                 struct v4l2_queryctrl *qc)
580 {
581         struct soc_camera_file *icf = file->private_data;
582         struct soc_camera_device *icd = icf->icd;
583         int i;
584
585         WARN_ON(priv != file->private_data);
586
587         if (!qc->id)
588                 return -EINVAL;
589
590         for (i = 0; i < icd->ops->num_controls; i++)
591                 if (qc->id == icd->ops->controls[i].id) {
592                         memcpy(qc, &(icd->ops->controls[i]),
593                                 sizeof(*qc));
594                         return 0;
595                 }
596
597         return -EINVAL;
598 }
599
600 static int soc_camera_g_ctrl(struct file *file, void *priv,
601                              struct v4l2_control *ctrl)
602 {
603         struct soc_camera_file *icf = file->private_data;
604         struct soc_camera_device *icd = icf->icd;
605
606         WARN_ON(priv != file->private_data);
607
608         switch (ctrl->id) {
609         case V4L2_CID_GAIN:
610                 if (icd->gain == (unsigned short)~0)
611                         return -EINVAL;
612                 ctrl->value = icd->gain;
613                 return 0;
614         case V4L2_CID_EXPOSURE:
615                 if (icd->exposure == (unsigned short)~0)
616                         return -EINVAL;
617                 ctrl->value = icd->exposure;
618                 return 0;
619         }
620
621         if (icd->ops->get_control)
622                 return icd->ops->get_control(icd, ctrl);
623         return -EINVAL;
624 }
625
626 static int soc_camera_s_ctrl(struct file *file, void *priv,
627                              struct v4l2_control *ctrl)
628 {
629         struct soc_camera_file *icf = file->private_data;
630         struct soc_camera_device *icd = icf->icd;
631
632         WARN_ON(priv != file->private_data);
633
634         if (icd->ops->set_control)
635                 return icd->ops->set_control(icd, ctrl);
636         return -EINVAL;
637 }
638
639 static int soc_camera_cropcap(struct file *file, void *fh,
640                               struct v4l2_cropcap *a)
641 {
642         struct soc_camera_file *icf = file->private_data;
643         struct soc_camera_device *icd = icf->icd;
644
645         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
646         a->bounds.left                  = icd->x_min;
647         a->bounds.top                   = icd->y_min;
648         a->bounds.width                 = icd->width_max;
649         a->bounds.height                = icd->height_max;
650         a->defrect.left                 = icd->x_min;
651         a->defrect.top                  = icd->y_min;
652         a->defrect.width                = 640;
653         a->defrect.height               = 480;
654         a->pixelaspect.numerator        = 1;
655         a->pixelaspect.denominator      = 1;
656
657         return 0;
658 }
659
660 static int soc_camera_g_crop(struct file *file, void *fh,
661                              struct v4l2_crop *a)
662 {
663         struct soc_camera_file *icf = file->private_data;
664         struct soc_camera_device *icd = icf->icd;
665
666         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
667         a->c.left       = icd->x_current;
668         a->c.top        = icd->y_current;
669         a->c.width      = icd->width;
670         a->c.height     = icd->height;
671
672         return 0;
673 }
674
675 static int soc_camera_s_crop(struct file *file, void *fh,
676                              struct v4l2_crop *a)
677 {
678         struct soc_camera_file *icf = file->private_data;
679         struct soc_camera_device *icd = icf->icd;
680         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
681         int ret;
682
683         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
684                 return -EINVAL;
685
686         /* Cropping is allowed during a running capture, guard consistency */
687         mutex_lock(&icf->vb_vidq.vb_lock);
688
689         ret = ici->ops->set_fmt(icd, 0, &a->c);
690         if (!ret) {
691                 icd->width      = a->c.width;
692                 icd->height     = a->c.height;
693                 icd->x_current  = a->c.left;
694                 icd->y_current  = a->c.top;
695         }
696
697         mutex_unlock(&icf->vb_vidq.vb_lock);
698
699         return ret;
700 }
701
702 static int soc_camera_g_chip_ident(struct file *file, void *fh,
703                                    struct v4l2_chip_ident *id)
704 {
705         struct soc_camera_file *icf = file->private_data;
706         struct soc_camera_device *icd = icf->icd;
707
708         if (!icd->ops->get_chip_id)
709                 return -EINVAL;
710
711         return icd->ops->get_chip_id(icd, id);
712 }
713
714 #ifdef CONFIG_VIDEO_ADV_DEBUG
715 static int soc_camera_g_register(struct file *file, void *fh,
716                                  struct v4l2_register *reg)
717 {
718         struct soc_camera_file *icf = file->private_data;
719         struct soc_camera_device *icd = icf->icd;
720
721         if (!icd->ops->get_register)
722                 return -EINVAL;
723
724         return icd->ops->get_register(icd, reg);
725 }
726
727 static int soc_camera_s_register(struct file *file, void *fh,
728                                  struct v4l2_register *reg)
729 {
730         struct soc_camera_file *icf = file->private_data;
731         struct soc_camera_device *icd = icf->icd;
732
733         if (!icd->ops->set_register)
734                 return -EINVAL;
735
736         return icd->ops->set_register(icd, reg);
737 }
738 #endif
739
740 static int device_register_link(struct soc_camera_device *icd)
741 {
742         int ret = device_register(&icd->dev);
743
744         if (ret < 0) {
745                 /* Prevent calling device_unregister() */
746                 icd->dev.parent = NULL;
747                 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
748         /* Even if probe() was unsuccessful for all registered drivers,
749          * device_register() returns 0, and we add the link, just to
750          * document this camera's control device */
751         } else if (icd->control)
752                 /* Have to sysfs_remove_link() before device_unregister()? */
753                 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
754                                       "control"))
755                         dev_warn(&icd->dev,
756                                  "Failed creating the control symlink\n");
757         return ret;
758 }
759
760 /* So far this function cannot fail */
761 static void scan_add_host(struct soc_camera_host *ici)
762 {
763         struct soc_camera_device *icd;
764
765         mutex_lock(&list_lock);
766
767         list_for_each_entry(icd, &devices, list) {
768                 if (icd->iface == ici->nr) {
769                         icd->dev.parent = &ici->dev;
770                         device_register_link(icd);
771                 }
772         }
773
774         mutex_unlock(&list_lock);
775 }
776
777 /* return: 0 if no match found or a match found and
778  * device_register() successful, error code otherwise */
779 static int scan_add_device(struct soc_camera_device *icd)
780 {
781         struct soc_camera_host *ici;
782         int ret = 0;
783
784         mutex_lock(&list_lock);
785
786         list_add_tail(&icd->list, &devices);
787
788         /* Watch out for class_for_each_device / class_find_device API by
789          * Dave Young <hidave.darkstar@gmail.com> */
790         list_for_each_entry(ici, &hosts, list) {
791                 if (icd->iface == ici->nr) {
792                         ret = 1;
793                         icd->dev.parent = &ici->dev;
794                         break;
795                 }
796         }
797
798         mutex_unlock(&list_lock);
799
800         if (ret)
801                 ret = device_register_link(icd);
802
803         return ret;
804 }
805
806 static int soc_camera_probe(struct device *dev)
807 {
808         struct soc_camera_device *icd = to_soc_camera_dev(dev);
809         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
810         int ret;
811
812         /*
813          * Possible race scenario:
814          * modprobe <camera-host-driver> triggers __func__
815          * at this moment respective <camera-sensor-driver> gets rmmod'ed
816          * to protect take module references.
817          */
818
819         if (!try_module_get(icd->ops->owner)) {
820                 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
821                 ret = -EINVAL;
822                 goto emgd;
823         }
824
825         if (!try_module_get(ici->ops->owner)) {
826                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
827                 ret = -EINVAL;
828                 goto emgi;
829         }
830
831         mutex_lock(&icd->video_lock);
832
833         /* We only call ->add() here to activate and probe the camera.
834          * We shall ->remove() and deactivate it immediately afterwards. */
835         ret = ici->ops->add(icd);
836         if (ret < 0)
837                 goto eiadd;
838
839         ret = icd->ops->probe(icd);
840         if (ret >= 0) {
841                 const struct v4l2_queryctrl *qctrl;
842
843                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
844                 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
845                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
846                 icd->exposure = qctrl ? qctrl->default_value :
847                         (unsigned short)~0;
848         }
849         ici->ops->remove(icd);
850
851 eiadd:
852         mutex_unlock(&icd->video_lock);
853         module_put(ici->ops->owner);
854 emgi:
855         module_put(icd->ops->owner);
856 emgd:
857         return ret;
858 }
859
860 /* This is called on device_unregister, which only means we have to disconnect
861  * from the host, but not remove ourselves from the device list */
862 static int soc_camera_remove(struct device *dev)
863 {
864         struct soc_camera_device *icd = to_soc_camera_dev(dev);
865
866         if (icd->ops->remove)
867                 icd->ops->remove(icd);
868
869         return 0;
870 }
871
872 static int soc_camera_suspend(struct device *dev, pm_message_t state)
873 {
874         struct soc_camera_device *icd = to_soc_camera_dev(dev);
875         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
876         int ret = 0;
877
878         if (ici->ops->suspend)
879                 ret = ici->ops->suspend(icd, state);
880
881         return ret;
882 }
883
884 static int soc_camera_resume(struct device *dev)
885 {
886         struct soc_camera_device *icd = to_soc_camera_dev(dev);
887         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
888         int ret = 0;
889
890         if (ici->ops->resume)
891                 ret = ici->ops->resume(icd);
892
893         return ret;
894 }
895
896 static struct bus_type soc_camera_bus_type = {
897         .name           = "soc-camera",
898         .probe          = soc_camera_probe,
899         .remove         = soc_camera_remove,
900         .suspend        = soc_camera_suspend,
901         .resume         = soc_camera_resume,
902 };
903
904 static struct device_driver ic_drv = {
905         .name   = "camera",
906         .bus    = &soc_camera_bus_type,
907         .owner  = THIS_MODULE,
908 };
909
910 static void dummy_release(struct device *dev)
911 {
912 }
913
914 int soc_camera_host_register(struct soc_camera_host *ici)
915 {
916         int ret;
917         struct soc_camera_host *ix;
918
919         if (!ici || !ici->ops ||
920             !ici->ops->try_fmt ||
921             !ici->ops->set_fmt ||
922             !ici->ops->set_bus_param ||
923             !ici->ops->querycap ||
924             !ici->ops->init_videobuf ||
925             !ici->ops->reqbufs ||
926             !ici->ops->add ||
927             !ici->ops->remove ||
928             !ici->ops->poll)
929                 return -EINVAL;
930
931         /* Number might be equal to the platform device ID */
932         dev_set_name(&ici->dev, "camera_host%d", ici->nr);
933
934         mutex_lock(&list_lock);
935         list_for_each_entry(ix, &hosts, list) {
936                 if (ix->nr == ici->nr) {
937                         mutex_unlock(&list_lock);
938                         return -EBUSY;
939                 }
940         }
941
942         list_add_tail(&ici->list, &hosts);
943         mutex_unlock(&list_lock);
944
945         ici->dev.release = dummy_release;
946
947         ret = device_register(&ici->dev);
948
949         if (ret)
950                 goto edevr;
951
952         scan_add_host(ici);
953
954         return 0;
955
956 edevr:
957         mutex_lock(&list_lock);
958         list_del(&ici->list);
959         mutex_unlock(&list_lock);
960
961         return ret;
962 }
963 EXPORT_SYMBOL(soc_camera_host_register);
964
965 /* Unregister all clients! */
966 void soc_camera_host_unregister(struct soc_camera_host *ici)
967 {
968         struct soc_camera_device *icd;
969
970         mutex_lock(&list_lock);
971
972         list_del(&ici->list);
973
974         list_for_each_entry(icd, &devices, list) {
975                 if (icd->dev.parent == &ici->dev) {
976                         device_unregister(&icd->dev);
977                         /* Not before device_unregister(), .remove
978                          * needs parent to call ici->ops->remove() */
979                         icd->dev.parent = NULL;
980                         memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
981                 }
982         }
983
984         mutex_unlock(&list_lock);
985
986         device_unregister(&ici->dev);
987 }
988 EXPORT_SYMBOL(soc_camera_host_unregister);
989
990 /* Image capture device */
991 int soc_camera_device_register(struct soc_camera_device *icd)
992 {
993         struct soc_camera_device *ix;
994         int num = -1, i;
995
996         if (!icd || !icd->ops ||
997             !icd->ops->probe ||
998             !icd->ops->init ||
999             !icd->ops->release ||
1000             !icd->ops->start_capture ||
1001             !icd->ops->stop_capture ||
1002             !icd->ops->set_fmt ||
1003             !icd->ops->try_fmt ||
1004             !icd->ops->query_bus_param ||
1005             !icd->ops->set_bus_param)
1006                 return -EINVAL;
1007
1008         for (i = 0; i < 256 && num < 0; i++) {
1009                 num = i;
1010                 list_for_each_entry(ix, &devices, list) {
1011                         if (ix->iface == icd->iface && ix->devnum == i) {
1012                                 num = -1;
1013                                 break;
1014                         }
1015                 }
1016         }
1017
1018         if (num < 0)
1019                 /* ok, we have 256 cameras on this host...
1020                  * man, stay reasonable... */
1021                 return -ENOMEM;
1022
1023         icd->devnum = num;
1024         icd->dev.bus = &soc_camera_bus_type;
1025         dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
1026
1027         icd->dev.release        = dummy_release;
1028         icd->use_count          = 0;
1029         icd->host_priv          = NULL;
1030         mutex_init(&icd->video_lock);
1031
1032         return scan_add_device(icd);
1033 }
1034 EXPORT_SYMBOL(soc_camera_device_register);
1035
1036 void soc_camera_device_unregister(struct soc_camera_device *icd)
1037 {
1038         mutex_lock(&list_lock);
1039         list_del(&icd->list);
1040
1041         /* The bus->remove will be eventually called */
1042         if (icd->dev.parent)
1043                 device_unregister(&icd->dev);
1044         mutex_unlock(&list_lock);
1045 }
1046 EXPORT_SYMBOL(soc_camera_device_unregister);
1047
1048 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1049         .vidioc_querycap         = soc_camera_querycap,
1050         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1051         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1052         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1053         .vidioc_enum_input       = soc_camera_enum_input,
1054         .vidioc_g_input          = soc_camera_g_input,
1055         .vidioc_s_input          = soc_camera_s_input,
1056         .vidioc_s_std            = soc_camera_s_std,
1057         .vidioc_reqbufs          = soc_camera_reqbufs,
1058         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1059         .vidioc_querybuf         = soc_camera_querybuf,
1060         .vidioc_qbuf             = soc_camera_qbuf,
1061         .vidioc_dqbuf            = soc_camera_dqbuf,
1062         .vidioc_streamon         = soc_camera_streamon,
1063         .vidioc_streamoff        = soc_camera_streamoff,
1064         .vidioc_queryctrl        = soc_camera_queryctrl,
1065         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1066         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1067         .vidioc_cropcap          = soc_camera_cropcap,
1068         .vidioc_g_crop           = soc_camera_g_crop,
1069         .vidioc_s_crop           = soc_camera_s_crop,
1070         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1071 #ifdef CONFIG_VIDEO_ADV_DEBUG
1072         .vidioc_g_register       = soc_camera_g_register,
1073         .vidioc_s_register       = soc_camera_s_register,
1074 #endif
1075 };
1076
1077 /*
1078  * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1079  * soc_camera_probe() above with .video_lock held
1080  */
1081 int soc_camera_video_start(struct soc_camera_device *icd)
1082 {
1083         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1084         int err = -ENOMEM;
1085         struct video_device *vdev;
1086
1087         if (!icd->dev.parent)
1088                 return -ENODEV;
1089
1090         vdev = video_device_alloc();
1091         if (!vdev)
1092                 goto evidallocd;
1093         dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1094
1095         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1096
1097         vdev->parent            = &icd->dev;
1098         vdev->current_norm      = V4L2_STD_UNKNOWN;
1099         vdev->fops              = &soc_camera_fops;
1100         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1101         vdev->release           = video_device_release;
1102         vdev->minor             = -1;
1103         vdev->tvnorms           = V4L2_STD_UNKNOWN,
1104
1105         err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1106         if (err < 0) {
1107                 dev_err(vdev->parent, "video_register_device failed\n");
1108                 goto evidregd;
1109         }
1110         icd->vdev = vdev;
1111
1112         return 0;
1113
1114 evidregd:
1115         video_device_release(vdev);
1116 evidallocd:
1117         return err;
1118 }
1119 EXPORT_SYMBOL(soc_camera_video_start);
1120
1121 void soc_camera_video_stop(struct soc_camera_device *icd)
1122 {
1123         struct video_device *vdev = icd->vdev;
1124
1125         dev_dbg(&icd->dev, "%s\n", __func__);
1126
1127         if (!icd->dev.parent || !vdev)
1128                 return;
1129
1130         mutex_lock(&icd->video_lock);
1131         video_unregister_device(vdev);
1132         icd->vdev = NULL;
1133         mutex_unlock(&icd->video_lock);
1134 }
1135 EXPORT_SYMBOL(soc_camera_video_stop);
1136
1137 static int __init soc_camera_init(void)
1138 {
1139         int ret = bus_register(&soc_camera_bus_type);
1140         if (ret)
1141                 return ret;
1142         ret = driver_register(&ic_drv);
1143         if (ret)
1144                 goto edrvr;
1145
1146         return 0;
1147
1148 edrvr:
1149         bus_unregister(&soc_camera_bus_type);
1150         return ret;
1151 }
1152
1153 static void __exit soc_camera_exit(void)
1154 {
1155         driver_unregister(&ic_drv);
1156         bus_unregister(&soc_camera_bus_type);
1157 }
1158
1159 module_init(soc_camera_init);
1160 module_exit(soc_camera_exit);
1161
1162 MODULE_DESCRIPTION("Image capture bus driver");
1163 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1164 MODULE_LICENSE("GPL");