V4L/DVB (12521): soc-camera: use .s_std() from struct v4l2_subdev_core_ops
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / soc_camera.c
1 /*
2  * camera image capture (abstract) bus driver
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This driver provides an interface between platform-specific camera
7  * busses and camera devices. It should be used if the camera is
8  * connected not over a "proper" bus like PCI or USB, but over a
9  * special bus, like, for example, the Quick Capture interface on PXA270
10  * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11  * It can handle multiple cameras and / or multiple busses, which can
12  * be used, e.g., in stereo-vision applications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/vmalloc.h>
28
29 #include <media/soc_camera.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-core.h>
34
35 /* Default to VGA resolution */
36 #define DEFAULT_WIDTH   640
37 #define DEFAULT_HEIGHT  480
38
39 static LIST_HEAD(hosts);
40 static LIST_HEAD(devices);
41 static DEFINE_MUTEX(list_lock);         /* Protects the list of hosts */
42
43 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
44         struct soc_camera_device *icd, unsigned int fourcc)
45 {
46         unsigned int i;
47
48         for (i = 0; i < icd->num_formats; i++)
49                 if (icd->formats[i].fourcc == fourcc)
50                         return icd->formats + i;
51         return NULL;
52 }
53 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
54
55 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56         struct soc_camera_device *icd, unsigned int fourcc)
57 {
58         unsigned int i;
59
60         for (i = 0; i < icd->num_user_formats; i++)
61                 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62                         return icd->user_formats + i;
63         return NULL;
64 }
65 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
66
67 /**
68  * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69  * @icl:        camera platform parameters
70  * @flags:      flags to be inverted according to platform configuration
71  * @return:     resulting flags
72  */
73 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74                                             unsigned long flags)
75 {
76         unsigned long f;
77
78         /* If only one of the two polarities is supported, switch to the opposite */
79         if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80                 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81                 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82                         flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
83         }
84
85         if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86                 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87                 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88                         flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
89         }
90
91         if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92                 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93                 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94                         flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
95         }
96
97         return flags;
98 }
99 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
100
101 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
102                                       struct v4l2_format *f)
103 {
104         struct soc_camera_file *icf = file->private_data;
105         struct soc_camera_device *icd = icf->icd;
106         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
107
108         WARN_ON(priv != file->private_data);
109
110         /* limit format to hardware capabilities */
111         return ici->ops->try_fmt(icd, f);
112 }
113
114 static int soc_camera_enum_input(struct file *file, void *priv,
115                                  struct v4l2_input *inp)
116 {
117         struct soc_camera_file *icf = file->private_data;
118         struct soc_camera_device *icd = icf->icd;
119         int ret = 0;
120
121         if (inp->index != 0)
122                 return -EINVAL;
123
124         if (icd->ops->enum_input)
125                 ret = icd->ops->enum_input(icd, inp);
126         else {
127                 /* default is camera */
128                 inp->type = V4L2_INPUT_TYPE_CAMERA;
129                 inp->std  = V4L2_STD_UNKNOWN;
130                 strcpy(inp->name, "Camera");
131         }
132
133         return ret;
134 }
135
136 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
137 {
138         *i = 0;
139
140         return 0;
141 }
142
143 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
144 {
145         if (i > 0)
146                 return -EINVAL;
147
148         return 0;
149 }
150
151 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
152 {
153         struct soc_camera_file *icf = file->private_data;
154         struct soc_camera_device *icd = icf->icd;
155         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
156
157         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_std, *a);
158 }
159
160 static int soc_camera_reqbufs(struct file *file, void *priv,
161                               struct v4l2_requestbuffers *p)
162 {
163         int ret;
164         struct soc_camera_file *icf = file->private_data;
165         struct soc_camera_device *icd = icf->icd;
166         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
167
168         WARN_ON(priv != file->private_data);
169
170         ret = videobuf_reqbufs(&icf->vb_vidq, p);
171         if (ret < 0)
172                 return ret;
173
174         return ici->ops->reqbufs(icf, p);
175 }
176
177 static int soc_camera_querybuf(struct file *file, void *priv,
178                                struct v4l2_buffer *p)
179 {
180         struct soc_camera_file *icf = file->private_data;
181
182         WARN_ON(priv != file->private_data);
183
184         return videobuf_querybuf(&icf->vb_vidq, p);
185 }
186
187 static int soc_camera_qbuf(struct file *file, void *priv,
188                            struct v4l2_buffer *p)
189 {
190         struct soc_camera_file *icf = file->private_data;
191
192         WARN_ON(priv != file->private_data);
193
194         return videobuf_qbuf(&icf->vb_vidq, p);
195 }
196
197 static int soc_camera_dqbuf(struct file *file, void *priv,
198                             struct v4l2_buffer *p)
199 {
200         struct soc_camera_file *icf = file->private_data;
201
202         WARN_ON(priv != file->private_data);
203
204         return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
205 }
206
207 /* Always entered with .video_lock held */
208 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
209 {
210         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
211         int i, fmts = 0, ret;
212
213         if (!ici->ops->get_formats)
214                 /*
215                  * Fallback mode - the host will have to serve all
216                  * sensor-provided formats one-to-one to the user
217                  */
218                 fmts = icd->num_formats;
219         else
220                 /*
221                  * First pass - only count formats this host-sensor
222                  * configuration can provide
223                  */
224                 for (i = 0; i < icd->num_formats; i++) {
225                         ret = ici->ops->get_formats(icd, i, NULL);
226                         if (ret < 0)
227                                 return ret;
228                         fmts += ret;
229                 }
230
231         if (!fmts)
232                 return -ENXIO;
233
234         icd->user_formats =
235                 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
236         if (!icd->user_formats)
237                 return -ENOMEM;
238
239         icd->num_user_formats = fmts;
240
241         dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
242
243         /* Second pass - actually fill data formats */
244         fmts = 0;
245         for (i = 0; i < icd->num_formats; i++)
246                 if (!ici->ops->get_formats) {
247                         icd->user_formats[i].host_fmt = icd->formats + i;
248                         icd->user_formats[i].cam_fmt = icd->formats + i;
249                         icd->user_formats[i].buswidth = icd->formats[i].depth;
250                 } else {
251                         ret = ici->ops->get_formats(icd, i,
252                                                     &icd->user_formats[fmts]);
253                         if (ret < 0)
254                                 goto egfmt;
255                         fmts += ret;
256                 }
257
258         icd->current_fmt = icd->user_formats[0].host_fmt;
259
260         return 0;
261
262 egfmt:
263         icd->num_user_formats = 0;
264         vfree(icd->user_formats);
265         return ret;
266 }
267
268 /* Always entered with .video_lock held */
269 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
270 {
271         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
272
273         if (ici->ops->put_formats)
274                 ici->ops->put_formats(icd);
275         icd->current_fmt = NULL;
276         icd->num_user_formats = 0;
277         vfree(icd->user_formats);
278         icd->user_formats = NULL;
279 }
280
281 /* Called with .vb_lock held */
282 static int soc_camera_set_fmt(struct soc_camera_file *icf,
283                               struct v4l2_format *f)
284 {
285         struct soc_camera_device *icd = icf->icd;
286         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
287         struct v4l2_pix_format *pix = &f->fmt.pix;
288         int ret;
289
290         /* We always call try_fmt() before set_fmt() or set_crop() */
291         ret = ici->ops->try_fmt(icd, f);
292         if (ret < 0)
293                 return ret;
294
295         ret = ici->ops->set_fmt(icd, f);
296         if (ret < 0) {
297                 return ret;
298         } else if (!icd->current_fmt ||
299                    icd->current_fmt->fourcc != pix->pixelformat) {
300                 dev_err(ici->v4l2_dev.dev,
301                         "Host driver hasn't set up current format correctly!\n");
302                 return -EINVAL;
303         }
304
305         icd->rect_current.width         = pix->width;
306         icd->rect_current.height        = pix->height;
307         icf->vb_vidq.field              =
308                 icd->field              = pix->field;
309
310         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
311                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
312                          f->type);
313
314         dev_dbg(&icd->dev, "set width: %d height: %d\n",
315                 icd->rect_current.width, icd->rect_current.height);
316
317         /* set physical bus parameters */
318         return ici->ops->set_bus_param(icd, pix->pixelformat);
319 }
320
321 static int soc_camera_open(struct file *file)
322 {
323         struct video_device *vdev = video_devdata(file);
324         struct soc_camera_device *icd = container_of(vdev->parent, struct soc_camera_device, dev);
325         struct soc_camera_link *icl = to_soc_camera_link(icd);
326         struct soc_camera_host *ici;
327         struct soc_camera_file *icf;
328         int ret;
329
330         if (!icd->ops)
331                 /* No device driver attached */
332                 return -ENODEV;
333
334         ici = to_soc_camera_host(icd->dev.parent);
335
336         icf = vmalloc(sizeof(*icf));
337         if (!icf)
338                 return -ENOMEM;
339
340         if (!try_module_get(ici->ops->owner)) {
341                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
342                 ret = -EINVAL;
343                 goto emgi;
344         }
345
346         /* Protect against icd->ops->remove() until we module_get() both drivers. */
347         mutex_lock(&icd->video_lock);
348
349         icf->icd = icd;
350         icd->use_count++;
351
352         /* Now we really have to activate the camera */
353         if (icd->use_count == 1) {
354                 /* Restore parameters before the last close() per V4L2 API */
355                 struct v4l2_format f = {
356                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
357                         .fmt.pix = {
358                                 .width          = icd->rect_current.width,
359                                 .height         = icd->rect_current.height,
360                                 .field          = icd->field,
361                                 .pixelformat    = icd->current_fmt->fourcc,
362                                 .colorspace     = icd->current_fmt->colorspace,
363                         },
364                 };
365
366                 if (icl->power) {
367                         ret = icl->power(icd->pdev, 1);
368                         if (ret < 0)
369                                 goto epower;
370                 }
371
372                 /* The camera could have been already on, try to reset */
373                 if (icl->reset)
374                         icl->reset(icd->pdev);
375
376                 ret = ici->ops->add(icd);
377                 if (ret < 0) {
378                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
379                         goto eiciadd;
380                 }
381
382                 if (icd->ops->init) {
383                         ret = icd->ops->init(icd);
384                         if (ret < 0)
385                                 goto einit;
386                 }
387
388                 /* Try to configure with default parameters */
389                 ret = soc_camera_set_fmt(icf, &f);
390                 if (ret < 0)
391                         goto esfmt;
392         }
393
394         file->private_data = icf;
395         dev_dbg(&icd->dev, "camera device open\n");
396
397         ici->ops->init_videobuf(&icf->vb_vidq, icd);
398
399         mutex_unlock(&icd->video_lock);
400
401         return 0;
402
403         /*
404          * First five errors are entered with the .video_lock held
405          * and use_count == 1
406          */
407 esfmt:
408         if (icd->ops->release)
409                 icd->ops->release(icd);
410 einit:
411         ici->ops->remove(icd);
412 eiciadd:
413         if (icl->power)
414                 icl->power(icd->pdev, 0);
415 epower:
416         icd->use_count--;
417         mutex_unlock(&icd->video_lock);
418         module_put(ici->ops->owner);
419 emgi:
420         vfree(icf);
421         return ret;
422 }
423
424 static int soc_camera_close(struct file *file)
425 {
426         struct soc_camera_file *icf = file->private_data;
427         struct soc_camera_device *icd = icf->icd;
428         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
429         struct video_device *vdev = icd->vdev;
430
431         mutex_lock(&icd->video_lock);
432         icd->use_count--;
433         if (!icd->use_count) {
434                 struct soc_camera_link *icl = to_soc_camera_link(icd);
435
436                 if (icd->ops->release)
437                         icd->ops->release(icd);
438                 ici->ops->remove(icd);
439                 if (icl->power)
440                         icl->power(icd->pdev, 0);
441         }
442
443         mutex_unlock(&icd->video_lock);
444
445         module_put(ici->ops->owner);
446
447         vfree(icf);
448
449         dev_dbg(vdev->parent, "camera device close\n");
450
451         return 0;
452 }
453
454 static ssize_t soc_camera_read(struct file *file, char __user *buf,
455                                size_t count, loff_t *ppos)
456 {
457         struct soc_camera_file *icf = file->private_data;
458         struct soc_camera_device *icd = icf->icd;
459         struct video_device *vdev = icd->vdev;
460         int err = -EINVAL;
461
462         dev_err(vdev->parent, "camera device read not implemented\n");
463
464         return err;
465 }
466
467 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
468 {
469         struct soc_camera_file *icf = file->private_data;
470         struct soc_camera_device *icd = icf->icd;
471         int err;
472
473         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
474
475         err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
476
477         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
478                 (unsigned long)vma->vm_start,
479                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
480                 err);
481
482         return err;
483 }
484
485 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
486 {
487         struct soc_camera_file *icf = file->private_data;
488         struct soc_camera_device *icd = icf->icd;
489         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
490
491         if (list_empty(&icf->vb_vidq.stream)) {
492                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
493                 return POLLERR;
494         }
495
496         return ici->ops->poll(file, pt);
497 }
498
499 static struct v4l2_file_operations soc_camera_fops = {
500         .owner          = THIS_MODULE,
501         .open           = soc_camera_open,
502         .release        = soc_camera_close,
503         .ioctl          = video_ioctl2,
504         .read           = soc_camera_read,
505         .mmap           = soc_camera_mmap,
506         .poll           = soc_camera_poll,
507 };
508
509 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
510                                     struct v4l2_format *f)
511 {
512         struct soc_camera_file *icf = file->private_data;
513         struct soc_camera_device *icd = icf->icd;
514         int ret;
515
516         WARN_ON(priv != file->private_data);
517
518         mutex_lock(&icf->vb_vidq.vb_lock);
519
520         if (videobuf_queue_is_busy(&icf->vb_vidq)) {
521                 dev_err(&icd->dev, "S_FMT denied: queue busy\n");
522                 ret = -EBUSY;
523                 goto unlock;
524         }
525
526         ret = soc_camera_set_fmt(icf, f);
527
528 unlock:
529         mutex_unlock(&icf->vb_vidq.vb_lock);
530
531         return ret;
532 }
533
534 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
535                                        struct v4l2_fmtdesc *f)
536 {
537         struct soc_camera_file *icf = file->private_data;
538         struct soc_camera_device *icd = icf->icd;
539         const struct soc_camera_data_format *format;
540
541         WARN_ON(priv != file->private_data);
542
543         if (f->index >= icd->num_user_formats)
544                 return -EINVAL;
545
546         format = icd->user_formats[f->index].host_fmt;
547
548         strlcpy(f->description, format->name, sizeof(f->description));
549         f->pixelformat = format->fourcc;
550         return 0;
551 }
552
553 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
554                                     struct v4l2_format *f)
555 {
556         struct soc_camera_file *icf = file->private_data;
557         struct soc_camera_device *icd = icf->icd;
558         struct v4l2_pix_format *pix = &f->fmt.pix;
559
560         WARN_ON(priv != file->private_data);
561
562         pix->width              = icd->rect_current.width;
563         pix->height             = icd->rect_current.height;
564         pix->field              = icf->vb_vidq.field;
565         pix->pixelformat        = icd->current_fmt->fourcc;
566         pix->bytesperline       = pix->width *
567                 DIV_ROUND_UP(icd->current_fmt->depth, 8);
568         pix->sizeimage          = pix->height * pix->bytesperline;
569         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
570                 icd->current_fmt->fourcc);
571         return 0;
572 }
573
574 static int soc_camera_querycap(struct file *file, void  *priv,
575                                struct v4l2_capability *cap)
576 {
577         struct soc_camera_file *icf = file->private_data;
578         struct soc_camera_device *icd = icf->icd;
579         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
580
581         WARN_ON(priv != file->private_data);
582
583         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
584         return ici->ops->querycap(ici, cap);
585 }
586
587 static int soc_camera_streamon(struct file *file, void *priv,
588                                enum v4l2_buf_type i)
589 {
590         struct soc_camera_file *icf = file->private_data;
591         struct soc_camera_device *icd = icf->icd;
592         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
593         int ret;
594
595         WARN_ON(priv != file->private_data);
596
597         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
598                 return -EINVAL;
599
600         mutex_lock(&icd->video_lock);
601
602         v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 1);
603
604         /* This calls buf_queue from host driver's videobuf_queue_ops */
605         ret = videobuf_streamon(&icf->vb_vidq);
606
607         mutex_unlock(&icd->video_lock);
608
609         return ret;
610 }
611
612 static int soc_camera_streamoff(struct file *file, void *priv,
613                                 enum v4l2_buf_type i)
614 {
615         struct soc_camera_file *icf = file->private_data;
616         struct soc_camera_device *icd = icf->icd;
617         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
618
619         WARN_ON(priv != file->private_data);
620
621         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
622                 return -EINVAL;
623
624         mutex_lock(&icd->video_lock);
625
626         /* This calls buf_release from host driver's videobuf_queue_ops for all
627          * remaining buffers. When the last buffer is freed, stop capture */
628         videobuf_streamoff(&icf->vb_vidq);
629
630         v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 0);
631
632         mutex_unlock(&icd->video_lock);
633
634         return 0;
635 }
636
637 static int soc_camera_queryctrl(struct file *file, void *priv,
638                                 struct v4l2_queryctrl *qc)
639 {
640         struct soc_camera_file *icf = file->private_data;
641         struct soc_camera_device *icd = icf->icd;
642         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
643         int i;
644
645         WARN_ON(priv != file->private_data);
646
647         if (!qc->id)
648                 return -EINVAL;
649
650         /* First check host controls */
651         for (i = 0; i < ici->ops->num_controls; i++)
652                 if (qc->id == ici->ops->controls[i].id) {
653                         memcpy(qc, &(ici->ops->controls[i]),
654                                 sizeof(*qc));
655                         return 0;
656                 }
657
658         /* Then device controls */
659         for (i = 0; i < icd->ops->num_controls; i++)
660                 if (qc->id == icd->ops->controls[i].id) {
661                         memcpy(qc, &(icd->ops->controls[i]),
662                                 sizeof(*qc));
663                         return 0;
664                 }
665
666         return -EINVAL;
667 }
668
669 static int soc_camera_g_ctrl(struct file *file, void *priv,
670                              struct v4l2_control *ctrl)
671 {
672         struct soc_camera_file *icf = file->private_data;
673         struct soc_camera_device *icd = icf->icd;
674         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
675         int ret;
676
677         WARN_ON(priv != file->private_data);
678
679         switch (ctrl->id) {
680         case V4L2_CID_GAIN:
681                 if (icd->gain == (unsigned short)~0)
682                         return -EINVAL;
683                 ctrl->value = icd->gain;
684                 return 0;
685         case V4L2_CID_EXPOSURE:
686                 if (icd->exposure == (unsigned short)~0)
687                         return -EINVAL;
688                 ctrl->value = icd->exposure;
689                 return 0;
690         }
691
692         if (ici->ops->get_ctrl) {
693                 ret = ici->ops->get_ctrl(icd, ctrl);
694                 if (ret != -ENOIOCTLCMD)
695                         return ret;
696         }
697
698         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_ctrl, ctrl);
699 }
700
701 static int soc_camera_s_ctrl(struct file *file, void *priv,
702                              struct v4l2_control *ctrl)
703 {
704         struct soc_camera_file *icf = file->private_data;
705         struct soc_camera_device *icd = icf->icd;
706         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
707         int ret;
708
709         WARN_ON(priv != file->private_data);
710
711         if (ici->ops->set_ctrl) {
712                 ret = ici->ops->set_ctrl(icd, ctrl);
713                 if (ret != -ENOIOCTLCMD)
714                         return ret;
715         }
716
717         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_ctrl, ctrl);
718 }
719
720 static int soc_camera_cropcap(struct file *file, void *fh,
721                               struct v4l2_cropcap *a)
722 {
723         struct soc_camera_file *icf = file->private_data;
724         struct soc_camera_device *icd = icf->icd;
725
726         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
727         a->bounds                       = icd->rect_max;
728         a->defrect.left                 = icd->rect_max.left;
729         a->defrect.top                  = icd->rect_max.top;
730         a->defrect.width                = DEFAULT_WIDTH;
731         a->defrect.height               = DEFAULT_HEIGHT;
732         a->pixelaspect.numerator        = 1;
733         a->pixelaspect.denominator      = 1;
734
735         return 0;
736 }
737
738 static int soc_camera_g_crop(struct file *file, void *fh,
739                              struct v4l2_crop *a)
740 {
741         struct soc_camera_file *icf = file->private_data;
742         struct soc_camera_device *icd = icf->icd;
743
744         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
745         a->c    = icd->rect_current;
746
747         return 0;
748 }
749
750 static int soc_camera_s_crop(struct file *file, void *fh,
751                              struct v4l2_crop *a)
752 {
753         struct soc_camera_file *icf = file->private_data;
754         struct soc_camera_device *icd = icf->icd;
755         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
756         int ret;
757
758         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
759                 return -EINVAL;
760
761         /* Cropping is allowed during a running capture, guard consistency */
762         mutex_lock(&icf->vb_vidq.vb_lock);
763
764         if (a->c.width > icd->rect_max.width)
765                 a->c.width = icd->rect_max.width;
766
767         if (a->c.width < icd->width_min)
768                 a->c.width = icd->width_min;
769
770         if (a->c.height > icd->rect_max.height)
771                 a->c.height = icd->rect_max.height;
772
773         if (a->c.height < icd->height_min)
774                 a->c.height = icd->height_min;
775
776         if (a->c.width + a->c.left > icd->rect_max.width + icd->rect_max.left)
777                 a->c.left = icd->rect_max.width + icd->rect_max.left -
778                         a->c.width;
779
780         if (a->c.height + a->c.top > icd->rect_max.height + icd->rect_max.top)
781                 a->c.top = icd->rect_max.height + icd->rect_max.top -
782                         a->c.height;
783
784         ret = ici->ops->set_crop(icd, &a->c);
785         if (!ret)
786                 icd->rect_current = a->c;
787
788         mutex_unlock(&icf->vb_vidq.vb_lock);
789
790         return ret;
791 }
792
793 static int soc_camera_g_chip_ident(struct file *file, void *fh,
794                                    struct v4l2_dbg_chip_ident *id)
795 {
796         struct soc_camera_file *icf = file->private_data;
797         struct soc_camera_device *icd = icf->icd;
798         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
799
800         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_chip_ident, id);
801 }
802
803 #ifdef CONFIG_VIDEO_ADV_DEBUG
804 static int soc_camera_g_register(struct file *file, void *fh,
805                                  struct v4l2_dbg_register *reg)
806 {
807         struct soc_camera_file *icf = file->private_data;
808         struct soc_camera_device *icd = icf->icd;
809         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
810
811         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_register, reg);
812 }
813
814 static int soc_camera_s_register(struct file *file, void *fh,
815                                  struct v4l2_dbg_register *reg)
816 {
817         struct soc_camera_file *icf = file->private_data;
818         struct soc_camera_device *icd = icf->icd;
819         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
820
821         return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_register, reg);
822 }
823 #endif
824
825 /* So far this function cannot fail */
826 static void scan_add_host(struct soc_camera_host *ici)
827 {
828         struct soc_camera_device *icd;
829
830         mutex_lock(&list_lock);
831
832         list_for_each_entry(icd, &devices, list) {
833                 if (icd->iface == ici->nr) {
834                         int ret;
835                         icd->dev.parent = ici->v4l2_dev.dev;
836                         dev_set_name(&icd->dev, "%u-%u", icd->iface,
837                                      icd->devnum);
838                         ret = device_register(&icd->dev);
839                         if (ret < 0) {
840                                 icd->dev.parent = NULL;
841                                 dev_err(&icd->dev,
842                                         "Cannot register device: %d\n", ret);
843                         }
844                 }
845         }
846
847         mutex_unlock(&list_lock);
848 }
849
850 #ifdef CONFIG_I2C_BOARDINFO
851 static int soc_camera_init_i2c(struct soc_camera_device *icd,
852                                struct soc_camera_link *icl)
853 {
854         struct i2c_client *client;
855         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
856         struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
857         struct v4l2_subdev *subdev;
858         int ret;
859
860         if (!adap) {
861                 ret = -ENODEV;
862                 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
863                         icl->i2c_adapter_id);
864                 goto ei2cga;
865         }
866
867         icl->board_info->platform_data = icd;
868
869         subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
870                                 icl->module_name, icl->board_info, NULL);
871         if (!subdev) {
872                 ret = -ENOMEM;
873                 goto ei2cnd;
874         }
875
876         subdev->grp_id = (__u32)icd;
877         client = subdev->priv;
878
879         /* Use to_i2c_client(dev) to recover the i2c client */
880         dev_set_drvdata(&icd->dev, &client->dev);
881
882         return 0;
883 ei2cnd:
884         i2c_put_adapter(adap);
885 ei2cga:
886         return ret;
887 }
888
889 static void soc_camera_free_i2c(struct soc_camera_device *icd)
890 {
891         struct i2c_client *client =
892                 to_i2c_client(to_soc_camera_control(icd));
893         dev_set_drvdata(&icd->dev, NULL);
894         v4l2_device_unregister_subdev(i2c_get_clientdata(client));
895         i2c_unregister_device(client);
896         i2c_put_adapter(client->adapter);
897 }
898 #else
899 #define soc_camera_init_i2c(icd, icl)   (-ENODEV)
900 #define soc_camera_free_i2c(icd)        do {} while (0)
901 #endif
902
903 static int soc_camera_video_start(struct soc_camera_device *icd);
904 static int video_dev_create(struct soc_camera_device *icd);
905 /* Called during host-driver probe */
906 static int soc_camera_probe(struct device *dev)
907 {
908         struct soc_camera_device *icd = to_soc_camera_dev(dev);
909         struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
910         struct soc_camera_link *icl = to_soc_camera_link(icd);
911         struct device *control = NULL;
912         int ret;
913
914         dev_info(dev, "Probing %s\n", dev_name(dev));
915
916         if (icl->power) {
917                 ret = icl->power(icd->pdev, 1);
918                 if (ret < 0) {
919                         dev_err(dev,
920                                 "Platform failed to power-on the camera.\n");
921                         goto epower;
922                 }
923         }
924
925         /* The camera could have been already on, try to reset */
926         if (icl->reset)
927                 icl->reset(icd->pdev);
928
929         ret = ici->ops->add(icd);
930         if (ret < 0)
931                 goto eadd;
932
933         /* Must have icd->vdev before registering the device */
934         ret = video_dev_create(icd);
935         if (ret < 0)
936                 goto evdc;
937
938         /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
939         if (icl->board_info) {
940                 ret = soc_camera_init_i2c(icd, icl);
941                 if (ret < 0)
942                         goto eadddev;
943         } else if (!icl->add_device || !icl->del_device) {
944                 ret = -EINVAL;
945                 goto eadddev;
946         } else {
947                 if (icl->module_name)
948                         ret = request_module(icl->module_name);
949
950                 ret = icl->add_device(icl, &icd->dev);
951                 if (ret < 0)
952                         goto eadddev;
953
954                 /* FIXME: this is racy, have to use driver-binding notification */
955                 control = to_soc_camera_control(icd);
956                 if (!control || !control->driver ||
957                     !try_module_get(control->driver->owner)) {
958                         icl->del_device(icl);
959                         goto enodrv;
960                 }
961         }
962
963         /* At this point client .probe() should have run already */
964         ret = soc_camera_init_user_formats(icd);
965         if (ret < 0)
966                 goto eiufmt;
967
968         icd->rect_current = icd->rect_max;
969         icd->field = V4L2_FIELD_ANY;
970
971         /* ..._video_start() will create a device node, so we have to protect */
972         mutex_lock(&icd->video_lock);
973
974         ret = soc_camera_video_start(icd);
975         if (ret < 0)
976                 goto evidstart;
977
978         /* Do we have to sysfs_remove_link() before device_unregister()? */
979         if (to_soc_camera_control(icd) &&
980             sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
981                               "control"))
982                 dev_warn(&icd->dev, "Failed creating the control symlink\n");
983
984         ici->ops->remove(icd);
985
986         if (icl->power)
987                 icl->power(icd->pdev, 0);
988
989         mutex_unlock(&icd->video_lock);
990
991         return 0;
992
993 evidstart:
994         mutex_unlock(&icd->video_lock);
995         soc_camera_free_user_formats(icd);
996 eiufmt:
997         if (icl->board_info) {
998                 soc_camera_free_i2c(icd);
999         } else {
1000                 icl->del_device(icl);
1001                 module_put(control->driver->owner);
1002         }
1003 enodrv:
1004 eadddev:
1005         video_device_release(icd->vdev);
1006 evdc:
1007         ici->ops->remove(icd);
1008 eadd:
1009         if (icl->power)
1010                 icl->power(icd->pdev, 0);
1011 epower:
1012         return ret;
1013 }
1014
1015 /* This is called on device_unregister, which only means we have to disconnect
1016  * from the host, but not remove ourselves from the device list */
1017 static int soc_camera_remove(struct device *dev)
1018 {
1019         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1020         struct soc_camera_link *icl = to_soc_camera_link(icd);
1021         struct video_device *vdev = icd->vdev;
1022
1023         BUG_ON(!dev->parent);
1024
1025         if (vdev) {
1026                 mutex_lock(&icd->video_lock);
1027                 video_unregister_device(vdev);
1028                 icd->vdev = NULL;
1029                 mutex_unlock(&icd->video_lock);
1030         }
1031
1032         if (icl->board_info) {
1033                 soc_camera_free_i2c(icd);
1034         } else {
1035                 struct device_driver *drv = to_soc_camera_control(icd) ?
1036                         to_soc_camera_control(icd)->driver : NULL;
1037                 if (drv) {
1038                         icl->del_device(icl);
1039                         module_put(drv->owner);
1040                 }
1041         }
1042         soc_camera_free_user_formats(icd);
1043
1044         return 0;
1045 }
1046
1047 static int soc_camera_suspend(struct device *dev, pm_message_t state)
1048 {
1049         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1050         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1051         int ret = 0;
1052
1053         if (ici->ops->suspend)
1054                 ret = ici->ops->suspend(icd, state);
1055
1056         return ret;
1057 }
1058
1059 static int soc_camera_resume(struct device *dev)
1060 {
1061         struct soc_camera_device *icd = to_soc_camera_dev(dev);
1062         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1063         int ret = 0;
1064
1065         if (ici->ops->resume)
1066                 ret = ici->ops->resume(icd);
1067
1068         return ret;
1069 }
1070
1071 static struct bus_type soc_camera_bus_type = {
1072         .name           = "soc-camera",
1073         .probe          = soc_camera_probe,
1074         .remove         = soc_camera_remove,
1075         .suspend        = soc_camera_suspend,
1076         .resume         = soc_camera_resume,
1077 };
1078
1079 static struct device_driver ic_drv = {
1080         .name   = "camera",
1081         .bus    = &soc_camera_bus_type,
1082         .owner  = THIS_MODULE,
1083 };
1084
1085 static void dummy_release(struct device *dev)
1086 {
1087 }
1088
1089 int soc_camera_host_register(struct soc_camera_host *ici)
1090 {
1091         struct soc_camera_host *ix;
1092         int ret;
1093
1094         if (!ici || !ici->ops ||
1095             !ici->ops->try_fmt ||
1096             !ici->ops->set_fmt ||
1097             !ici->ops->set_crop ||
1098             !ici->ops->set_bus_param ||
1099             !ici->ops->querycap ||
1100             !ici->ops->init_videobuf ||
1101             !ici->ops->reqbufs ||
1102             !ici->ops->add ||
1103             !ici->ops->remove ||
1104             !ici->ops->poll ||
1105             !ici->v4l2_dev.dev)
1106                 return -EINVAL;
1107
1108         mutex_lock(&list_lock);
1109         list_for_each_entry(ix, &hosts, list) {
1110                 if (ix->nr == ici->nr) {
1111                         ret = -EBUSY;
1112                         goto edevreg;
1113                 }
1114         }
1115
1116         ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1117         if (ret < 0)
1118                 goto edevreg;
1119
1120         list_add_tail(&ici->list, &hosts);
1121         mutex_unlock(&list_lock);
1122
1123         scan_add_host(ici);
1124
1125         return 0;
1126
1127 edevreg:
1128         mutex_unlock(&list_lock);
1129         return ret;
1130 }
1131 EXPORT_SYMBOL(soc_camera_host_register);
1132
1133 /* Unregister all clients! */
1134 void soc_camera_host_unregister(struct soc_camera_host *ici)
1135 {
1136         struct soc_camera_device *icd;
1137
1138         mutex_lock(&list_lock);
1139
1140         list_del(&ici->list);
1141
1142         list_for_each_entry(icd, &devices, list) {
1143                 if (icd->iface == ici->nr) {
1144                         /* The bus->remove will be called */
1145                         device_unregister(&icd->dev);
1146                         /* Not before device_unregister(), .remove
1147                          * needs parent to call ici->ops->remove() */
1148                         icd->dev.parent = NULL;
1149
1150                         /* If the host module is loaded again, device_register()
1151                          * would complain "already initialised" */
1152                         memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
1153                 }
1154         }
1155
1156         mutex_unlock(&list_lock);
1157
1158         v4l2_device_unregister(&ici->v4l2_dev);
1159 }
1160 EXPORT_SYMBOL(soc_camera_host_unregister);
1161
1162 /* Image capture device */
1163 static int soc_camera_device_register(struct soc_camera_device *icd)
1164 {
1165         struct soc_camera_device *ix;
1166         int num = -1, i;
1167
1168         for (i = 0; i < 256 && num < 0; i++) {
1169                 num = i;
1170                 /* Check if this index is available on this interface */
1171                 list_for_each_entry(ix, &devices, list) {
1172                         if (ix->iface == icd->iface && ix->devnum == i) {
1173                                 num = -1;
1174                                 break;
1175                         }
1176                 }
1177         }
1178
1179         if (num < 0)
1180                 /* ok, we have 256 cameras on this host...
1181                  * man, stay reasonable... */
1182                 return -ENOMEM;
1183
1184         icd->devnum = num;
1185         icd->dev.bus = &soc_camera_bus_type;
1186
1187         icd->dev.release        = dummy_release;
1188         icd->use_count          = 0;
1189         icd->host_priv          = NULL;
1190         mutex_init(&icd->video_lock);
1191
1192         list_add_tail(&icd->list, &devices);
1193
1194         return 0;
1195 }
1196
1197 static void soc_camera_device_unregister(struct soc_camera_device *icd)
1198 {
1199         list_del(&icd->list);
1200 }
1201
1202 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1203         .vidioc_querycap         = soc_camera_querycap,
1204         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
1205         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1206         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1207         .vidioc_enum_input       = soc_camera_enum_input,
1208         .vidioc_g_input          = soc_camera_g_input,
1209         .vidioc_s_input          = soc_camera_s_input,
1210         .vidioc_s_std            = soc_camera_s_std,
1211         .vidioc_reqbufs          = soc_camera_reqbufs,
1212         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1213         .vidioc_querybuf         = soc_camera_querybuf,
1214         .vidioc_qbuf             = soc_camera_qbuf,
1215         .vidioc_dqbuf            = soc_camera_dqbuf,
1216         .vidioc_streamon         = soc_camera_streamon,
1217         .vidioc_streamoff        = soc_camera_streamoff,
1218         .vidioc_queryctrl        = soc_camera_queryctrl,
1219         .vidioc_g_ctrl           = soc_camera_g_ctrl,
1220         .vidioc_s_ctrl           = soc_camera_s_ctrl,
1221         .vidioc_cropcap          = soc_camera_cropcap,
1222         .vidioc_g_crop           = soc_camera_g_crop,
1223         .vidioc_s_crop           = soc_camera_s_crop,
1224         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
1225 #ifdef CONFIG_VIDEO_ADV_DEBUG
1226         .vidioc_g_register       = soc_camera_g_register,
1227         .vidioc_s_register       = soc_camera_s_register,
1228 #endif
1229 };
1230
1231 static int video_dev_create(struct soc_camera_device *icd)
1232 {
1233         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1234         struct video_device *vdev = video_device_alloc();
1235
1236         if (!vdev)
1237                 return -ENOMEM;
1238
1239         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1240
1241         vdev->parent            = &icd->dev;
1242         vdev->current_norm      = V4L2_STD_UNKNOWN;
1243         vdev->fops              = &soc_camera_fops;
1244         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
1245         vdev->release           = video_device_release;
1246         vdev->minor             = -1;
1247         vdev->tvnorms           = V4L2_STD_UNKNOWN;
1248
1249         icd->vdev = vdev;
1250
1251         return 0;
1252 }
1253
1254 /*
1255  * Called from soc_camera_probe() above (with .video_lock held???)
1256  */
1257 static int soc_camera_video_start(struct soc_camera_device *icd)
1258 {
1259         const struct v4l2_queryctrl *qctrl;
1260         int ret;
1261
1262         if (!icd->dev.parent)
1263                 return -ENODEV;
1264
1265         if (!icd->ops ||
1266             !icd->ops->query_bus_param ||
1267             !icd->ops->set_bus_param)
1268                 return -EINVAL;
1269
1270         ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
1271                                     icd->vdev->minor);
1272         if (ret < 0) {
1273                 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1274                 return ret;
1275         }
1276
1277         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
1278         icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
1279         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
1280         icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
1281
1282         return 0;
1283 }
1284
1285 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1286 {
1287         struct soc_camera_link *icl = pdev->dev.platform_data;
1288         struct soc_camera_device *icd;
1289         int ret;
1290
1291         if (!icl)
1292                 return -EINVAL;
1293
1294         icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1295         if (!icd)
1296                 return -ENOMEM;
1297
1298         icd->iface = icl->bus_id;
1299         icd->pdev = &pdev->dev;
1300         platform_set_drvdata(pdev, icd);
1301         icd->dev.platform_data = icl;
1302
1303         ret = soc_camera_device_register(icd);
1304         if (ret < 0)
1305                 goto escdevreg;
1306
1307         return 0;
1308
1309 escdevreg:
1310         kfree(icd);
1311
1312         return ret;
1313 }
1314
1315 /* Only called on rmmod for each platform device, since they are not
1316  * hot-pluggable. Now we know, that all our users - hosts and devices have
1317  * been unloaded already */
1318 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1319 {
1320         struct soc_camera_device *icd = platform_get_drvdata(pdev);
1321
1322         if (!icd)
1323                 return -EINVAL;
1324
1325         soc_camera_device_unregister(icd);
1326
1327         kfree(icd);
1328
1329         return 0;
1330 }
1331
1332 static struct platform_driver __refdata soc_camera_pdrv = {
1333         .remove  = __devexit_p(soc_camera_pdrv_remove),
1334         .driver  = {
1335                 .name   = "soc-camera-pdrv",
1336                 .owner  = THIS_MODULE,
1337         },
1338 };
1339
1340 static int __init soc_camera_init(void)
1341 {
1342         int ret = bus_register(&soc_camera_bus_type);
1343         if (ret)
1344                 return ret;
1345         ret = driver_register(&ic_drv);
1346         if (ret)
1347                 goto edrvr;
1348
1349         ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1350         if (ret)
1351                 goto epdr;
1352
1353         return 0;
1354
1355 epdr:
1356         driver_unregister(&ic_drv);
1357 edrvr:
1358         bus_unregister(&soc_camera_bus_type);
1359         return ret;
1360 }
1361
1362 static void __exit soc_camera_exit(void)
1363 {
1364         platform_driver_unregister(&soc_camera_pdrv);
1365         driver_unregister(&ic_drv);
1366         bus_unregister(&soc_camera_bus_type);
1367 }
1368
1369 module_init(soc_camera_init);
1370 module_exit(soc_camera_exit);
1371
1372 MODULE_DESCRIPTION("Image capture bus driver");
1373 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1374 MODULE_LICENSE("GPL");
1375 MODULE_ALIAS("platform:soc-camera-pdrv");