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