V4L/DVB (9787): soc-camera: let camera host drivers decide upon pixel format
[safe/jmp/linux-2.6] / drivers / media / video / soc_camera.c
1 /*
2  * camera image capture (abstract) bus driver
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This driver provides an interface between platform-specific camera
7  * busses and camera devices. It should be used if the camera is
8  * connected not over a "proper" bus like PCI or USB, but over a
9  * special bus, like, for example, the Quick Capture interface on PXA270
10  * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11  * It can handle multiple cameras and / or multiple busses, which can
12  * be used, e.g., in stereo-vision applications.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/vmalloc.h>
26
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf-core.h>
31 #include <media/soc_camera.h>
32
33 static LIST_HEAD(hosts);
34 static LIST_HEAD(devices);
35 static DEFINE_MUTEX(list_lock);
36 static DEFINE_MUTEX(video_lock);
37
38 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
39         struct soc_camera_device *icd, unsigned int fourcc)
40 {
41         unsigned int i;
42
43         for (i = 0; i < icd->num_formats; i++)
44                 if (icd->formats[i].fourcc == fourcc)
45                         return icd->formats + i;
46         return NULL;
47 }
48 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
49
50 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
51                                       struct v4l2_format *f)
52 {
53         struct soc_camera_file *icf = file->private_data;
54         struct soc_camera_device *icd = icf->icd;
55         struct soc_camera_host *ici =
56                 to_soc_camera_host(icd->dev.parent);
57         enum v4l2_field field;
58         int ret;
59
60         WARN_ON(priv != file->private_data);
61
62         /*
63          * TODO: this might also have to migrate to host-drivers, if anyone
64          * wishes to support other fields
65          */
66         field = f->fmt.pix.field;
67
68         if (field == V4L2_FIELD_ANY) {
69                 f->fmt.pix.field = V4L2_FIELD_NONE;
70         } else if (field != V4L2_FIELD_NONE) {
71                 dev_err(&icd->dev, "Field type invalid.\n");
72                 return -EINVAL;
73         }
74
75         /* limit format to hardware capabilities */
76         ret = ici->ops->try_fmt_cap(icd, f);
77
78         return ret;
79 }
80
81 static int soc_camera_enum_input(struct file *file, void *priv,
82                                  struct v4l2_input *inp)
83 {
84         if (inp->index != 0)
85                 return -EINVAL;
86
87         inp->type = V4L2_INPUT_TYPE_CAMERA;
88         inp->std = V4L2_STD_UNKNOWN;
89         strcpy(inp->name, "Camera");
90
91         return 0;
92 }
93
94 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
95 {
96         *i = 0;
97
98         return 0;
99 }
100
101 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
102 {
103         if (i > 0)
104                 return -EINVAL;
105
106         return 0;
107 }
108
109 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
110 {
111         return 0;
112 }
113
114 static int soc_camera_reqbufs(struct file *file, void *priv,
115                               struct v4l2_requestbuffers *p)
116 {
117         int ret;
118         struct soc_camera_file *icf = file->private_data;
119         struct soc_camera_device *icd = icf->icd;
120         struct soc_camera_host *ici =
121                 to_soc_camera_host(icd->dev.parent);
122
123         WARN_ON(priv != file->private_data);
124
125         dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
126
127         ret = videobuf_reqbufs(&icf->vb_vidq, p);
128         if (ret < 0)
129                 return ret;
130
131         return ici->ops->reqbufs(icf, p);
132 }
133
134 static int soc_camera_querybuf(struct file *file, void *priv,
135                                struct v4l2_buffer *p)
136 {
137         struct soc_camera_file *icf = file->private_data;
138
139         WARN_ON(priv != file->private_data);
140
141         return videobuf_querybuf(&icf->vb_vidq, p);
142 }
143
144 static int soc_camera_qbuf(struct file *file, void *priv,
145                            struct v4l2_buffer *p)
146 {
147         struct soc_camera_file *icf = file->private_data;
148
149         WARN_ON(priv != file->private_data);
150
151         return videobuf_qbuf(&icf->vb_vidq, p);
152 }
153
154 static int soc_camera_dqbuf(struct file *file, void *priv,
155                             struct v4l2_buffer *p)
156 {
157         struct soc_camera_file *icf = file->private_data;
158
159         WARN_ON(priv != file->private_data);
160
161         return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
162 }
163
164 static int soc_camera_open(struct inode *inode, struct file *file)
165 {
166         struct video_device *vdev;
167         struct soc_camera_device *icd;
168         struct soc_camera_host *ici;
169         struct soc_camera_file *icf;
170         int ret;
171
172         icf = vmalloc(sizeof(*icf));
173         if (!icf)
174                 return -ENOMEM;
175
176         /* Protect against icd->remove() until we module_get() both drivers. */
177         mutex_lock(&video_lock);
178
179         vdev = video_devdata(file);
180         icd = container_of(vdev->parent, struct soc_camera_device, dev);
181         ici = to_soc_camera_host(icd->dev.parent);
182
183         if (!try_module_get(icd->ops->owner)) {
184                 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
185                 ret = -EINVAL;
186                 goto emgd;
187         }
188
189         if (!try_module_get(ici->ops->owner)) {
190                 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
191                 ret = -EINVAL;
192                 goto emgi;
193         }
194
195         icf->icd = icd;
196         icd->use_count++;
197
198         /* Now we really have to activate the camera */
199         if (icd->use_count == 1) {
200                 ret = ici->ops->add(icd);
201                 if (ret < 0) {
202                         dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
203                         icd->use_count--;
204                         goto eiciadd;
205                 }
206         }
207
208         mutex_unlock(&video_lock);
209
210         file->private_data = icf;
211         dev_dbg(&icd->dev, "camera device open\n");
212
213         ici->ops->init_videobuf(&icf->vb_vidq, icd);
214
215         return 0;
216
217         /* All errors are entered with the video_lock held */
218 eiciadd:
219         module_put(ici->ops->owner);
220 emgi:
221         module_put(icd->ops->owner);
222 emgd:
223         mutex_unlock(&video_lock);
224         vfree(icf);
225         return ret;
226 }
227
228 static int soc_camera_close(struct inode *inode, struct file *file)
229 {
230         struct soc_camera_file *icf = file->private_data;
231         struct soc_camera_device *icd = icf->icd;
232         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
233         struct video_device *vdev = icd->vdev;
234
235         mutex_lock(&video_lock);
236         icd->use_count--;
237         if (!icd->use_count)
238                 ici->ops->remove(icd);
239         module_put(icd->ops->owner);
240         module_put(ici->ops->owner);
241         mutex_unlock(&video_lock);
242
243         vfree(icf);
244
245         dev_dbg(vdev->parent, "camera device close\n");
246
247         return 0;
248 }
249
250 static ssize_t soc_camera_read(struct file *file, char __user *buf,
251                                size_t count, loff_t *ppos)
252 {
253         struct soc_camera_file *icf = file->private_data;
254         struct soc_camera_device *icd = icf->icd;
255         struct video_device *vdev = icd->vdev;
256         int err = -EINVAL;
257
258         dev_err(vdev->parent, "camera device read not implemented\n");
259
260         return err;
261 }
262
263 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
264 {
265         struct soc_camera_file *icf = file->private_data;
266         struct soc_camera_device *icd = icf->icd;
267         int err;
268
269         dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
270
271         err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
272
273         dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
274                 (unsigned long)vma->vm_start,
275                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
276                 err);
277
278         return err;
279 }
280
281 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
282 {
283         struct soc_camera_file *icf = file->private_data;
284         struct soc_camera_device *icd = icf->icd;
285         struct soc_camera_host *ici =
286                 to_soc_camera_host(icd->dev.parent);
287
288         if (list_empty(&icf->vb_vidq.stream)) {
289                 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
290                 return POLLERR;
291         }
292
293         return ici->ops->poll(file, pt);
294 }
295
296 static struct file_operations soc_camera_fops = {
297         .owner          = THIS_MODULE,
298         .open           = soc_camera_open,
299         .release        = soc_camera_close,
300         .ioctl          = video_ioctl2,
301         .read           = soc_camera_read,
302         .mmap           = soc_camera_mmap,
303         .poll           = soc_camera_poll,
304         .llseek         = no_llseek,
305 };
306
307 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
308                                     struct v4l2_format *f)
309 {
310         struct soc_camera_file *icf = file->private_data;
311         struct soc_camera_device *icd = icf->icd;
312         struct soc_camera_host *ici =
313                 to_soc_camera_host(icd->dev.parent);
314         int ret;
315         struct v4l2_rect rect;
316
317         WARN_ON(priv != file->private_data);
318
319         ret = soc_camera_try_fmt_vid_cap(file, priv, f);
320         if (ret < 0)
321                 return ret;
322
323         rect.left       = icd->x_current;
324         rect.top        = icd->y_current;
325         rect.width      = f->fmt.pix.width;
326         rect.height     = f->fmt.pix.height;
327         ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
328         if (ret < 0) {
329                 return ret;
330         } else if (!icd->current_fmt ||
331                    icd->current_fmt->fourcc != f->fmt.pix.pixelformat) {
332                 dev_err(&ici->dev, "Host driver hasn't set up current "
333                         "format correctly!\n");
334                 return -EINVAL;
335         }
336
337         /* buswidth may be further adjusted by the ici */
338         icd->buswidth           = icd->current_fmt->depth;
339         icd->width              = rect.width;
340         icd->height             = rect.height;
341         icf->vb_vidq.field      = f->fmt.pix.field;
342         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
343                 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
344                          f->type);
345
346         dev_dbg(&icd->dev, "set width: %d height: %d\n",
347                 icd->width, icd->height);
348
349         /* set physical bus parameters */
350         return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
351 }
352
353 static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
354                                        struct v4l2_fmtdesc *f)
355 {
356         struct soc_camera_file *icf = file->private_data;
357         struct soc_camera_device *icd = icf->icd;
358         const struct soc_camera_data_format *format;
359
360         WARN_ON(priv != file->private_data);
361
362         if (f->index >= icd->num_formats)
363                 return -EINVAL;
364
365         format = &icd->formats[f->index];
366
367         strlcpy(f->description, format->name, sizeof(f->description));
368         f->pixelformat = format->fourcc;
369         return 0;
370 }
371
372 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
373                                     struct v4l2_format *f)
374 {
375         struct soc_camera_file *icf = file->private_data;
376         struct soc_camera_device *icd = icf->icd;
377
378         WARN_ON(priv != file->private_data);
379
380         f->fmt.pix.width        = icd->width;
381         f->fmt.pix.height       = icd->height;
382         f->fmt.pix.field        = icf->vb_vidq.field;
383         f->fmt.pix.pixelformat  = icd->current_fmt->fourcc;
384         f->fmt.pix.bytesperline = f->fmt.pix.width *
385                 DIV_ROUND_UP(icd->current_fmt->depth, 8);
386         f->fmt.pix.sizeimage    = f->fmt.pix.height * f->fmt.pix.bytesperline;
387         dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
388                 icd->current_fmt->fourcc);
389         return 0;
390 }
391
392 static int soc_camera_querycap(struct file *file, void  *priv,
393                                struct v4l2_capability *cap)
394 {
395         struct soc_camera_file *icf = file->private_data;
396         struct soc_camera_device *icd = icf->icd;
397         struct soc_camera_host *ici =
398                 to_soc_camera_host(icd->dev.parent);
399
400         WARN_ON(priv != file->private_data);
401
402         strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
403         return ici->ops->querycap(ici, cap);
404 }
405
406 static int soc_camera_streamon(struct file *file, void *priv,
407                                enum v4l2_buf_type i)
408 {
409         struct soc_camera_file *icf = file->private_data;
410         struct soc_camera_device *icd = icf->icd;
411
412         WARN_ON(priv != file->private_data);
413
414         dev_dbg(&icd->dev, "%s\n", __func__);
415
416         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
417                 return -EINVAL;
418
419         icd->ops->start_capture(icd);
420
421         /* This calls buf_queue from host driver's videobuf_queue_ops */
422         return videobuf_streamon(&icf->vb_vidq);
423 }
424
425 static int soc_camera_streamoff(struct file *file, void *priv,
426                                 enum v4l2_buf_type i)
427 {
428         struct soc_camera_file *icf = file->private_data;
429         struct soc_camera_device *icd = icf->icd;
430
431         WARN_ON(priv != file->private_data);
432
433         dev_dbg(&icd->dev, "%s\n", __func__);
434
435         if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
436                 return -EINVAL;
437
438         /* This calls buf_release from host driver's videobuf_queue_ops for all
439          * remaining buffers. When the last buffer is freed, stop capture */
440         videobuf_streamoff(&icf->vb_vidq);
441
442         icd->ops->stop_capture(icd);
443
444         return 0;
445 }
446
447 static int soc_camera_queryctrl(struct file *file, void *priv,
448                                 struct v4l2_queryctrl *qc)
449 {
450         struct soc_camera_file *icf = file->private_data;
451         struct soc_camera_device *icd = icf->icd;
452         int i;
453
454         WARN_ON(priv != file->private_data);
455
456         if (!qc->id)
457                 return -EINVAL;
458
459         for (i = 0; i < icd->ops->num_controls; i++)
460                 if (qc->id == icd->ops->controls[i].id) {
461                         memcpy(qc, &(icd->ops->controls[i]),
462                                 sizeof(*qc));
463                         return 0;
464                 }
465
466         return -EINVAL;
467 }
468
469 static int soc_camera_g_ctrl(struct file *file, void *priv,
470                              struct v4l2_control *ctrl)
471 {
472         struct soc_camera_file *icf = file->private_data;
473         struct soc_camera_device *icd = icf->icd;
474
475         WARN_ON(priv != file->private_data);
476
477         switch (ctrl->id) {
478         case V4L2_CID_GAIN:
479                 if (icd->gain == (unsigned short)~0)
480                         return -EINVAL;
481                 ctrl->value = icd->gain;
482                 return 0;
483         case V4L2_CID_EXPOSURE:
484                 if (icd->exposure == (unsigned short)~0)
485                         return -EINVAL;
486                 ctrl->value = icd->exposure;
487                 return 0;
488         }
489
490         if (icd->ops->get_control)
491                 return icd->ops->get_control(icd, ctrl);
492         return -EINVAL;
493 }
494
495 static int soc_camera_s_ctrl(struct file *file, void *priv,
496                              struct v4l2_control *ctrl)
497 {
498         struct soc_camera_file *icf = file->private_data;
499         struct soc_camera_device *icd = icf->icd;
500
501         WARN_ON(priv != file->private_data);
502
503         if (icd->ops->set_control)
504                 return icd->ops->set_control(icd, ctrl);
505         return -EINVAL;
506 }
507
508 static int soc_camera_cropcap(struct file *file, void *fh,
509                               struct v4l2_cropcap *a)
510 {
511         struct soc_camera_file *icf = file->private_data;
512         struct soc_camera_device *icd = icf->icd;
513
514         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
515         a->bounds.left                  = icd->x_min;
516         a->bounds.top                   = icd->y_min;
517         a->bounds.width                 = icd->width_max;
518         a->bounds.height                = icd->height_max;
519         a->defrect.left                 = icd->x_min;
520         a->defrect.top                  = icd->y_min;
521         a->defrect.width                = 640;
522         a->defrect.height               = 480;
523         a->pixelaspect.numerator        = 1;
524         a->pixelaspect.denominator      = 1;
525
526         return 0;
527 }
528
529 static int soc_camera_g_crop(struct file *file, void *fh,
530                              struct v4l2_crop *a)
531 {
532         struct soc_camera_file *icf = file->private_data;
533         struct soc_camera_device *icd = icf->icd;
534
535         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
536         a->c.left       = icd->x_current;
537         a->c.top        = icd->y_current;
538         a->c.width      = icd->width;
539         a->c.height     = icd->height;
540
541         return 0;
542 }
543
544 static int soc_camera_s_crop(struct file *file, void *fh,
545                              struct v4l2_crop *a)
546 {
547         struct soc_camera_file *icf = file->private_data;
548         struct soc_camera_device *icd = icf->icd;
549         struct soc_camera_host *ici =
550                 to_soc_camera_host(icd->dev.parent);
551         int ret;
552
553         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
554                 return -EINVAL;
555
556         ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
557         if (!ret) {
558                 icd->width      = a->c.width;
559                 icd->height     = a->c.height;
560                 icd->x_current  = a->c.left;
561                 icd->y_current  = a->c.top;
562         }
563
564         return ret;
565 }
566
567 static int soc_camera_g_chip_ident(struct file *file, void *fh,
568                                    struct v4l2_chip_ident *id)
569 {
570         struct soc_camera_file *icf = file->private_data;
571         struct soc_camera_device *icd = icf->icd;
572
573         if (!icd->ops->get_chip_id)
574                 return -EINVAL;
575
576         return icd->ops->get_chip_id(icd, id);
577 }
578
579 #ifdef CONFIG_VIDEO_ADV_DEBUG
580 static int soc_camera_g_register(struct file *file, void *fh,
581                                  struct v4l2_register *reg)
582 {
583         struct soc_camera_file *icf = file->private_data;
584         struct soc_camera_device *icd = icf->icd;
585
586         if (!icd->ops->get_register)
587                 return -EINVAL;
588
589         return icd->ops->get_register(icd, reg);
590 }
591
592 static int soc_camera_s_register(struct file *file, void *fh,
593                                  struct v4l2_register *reg)
594 {
595         struct soc_camera_file *icf = file->private_data;
596         struct soc_camera_device *icd = icf->icd;
597
598         if (!icd->ops->set_register)
599                 return -EINVAL;
600
601         return icd->ops->set_register(icd, reg);
602 }
603 #endif
604
605 static int device_register_link(struct soc_camera_device *icd)
606 {
607         int ret = device_register(&icd->dev);
608
609         if (ret < 0) {
610                 /* Prevent calling device_unregister() */
611                 icd->dev.parent = NULL;
612                 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
613         /* Even if probe() was unsuccessful for all registered drivers,
614          * device_register() returns 0, and we add the link, just to
615          * document this camera's control device */
616         } else if (icd->control)
617                 /* Have to sysfs_remove_link() before device_unregister()? */
618                 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
619                                       "control"))
620                         dev_warn(&icd->dev,
621                                  "Failed creating the control symlink\n");
622         return ret;
623 }
624
625 /* So far this function cannot fail */
626 static void scan_add_host(struct soc_camera_host *ici)
627 {
628         struct soc_camera_device *icd;
629
630         mutex_lock(&list_lock);
631
632         list_for_each_entry(icd, &devices, list) {
633                 if (icd->iface == ici->nr) {
634                         icd->dev.parent = &ici->dev;
635                         device_register_link(icd);
636                 }
637         }
638
639         mutex_unlock(&list_lock);
640 }
641
642 /* return: 0 if no match found or a match found and
643  * device_register() successful, error code otherwise */
644 static int scan_add_device(struct soc_camera_device *icd)
645 {
646         struct soc_camera_host *ici;
647         int ret = 0;
648
649         mutex_lock(&list_lock);
650
651         list_add_tail(&icd->list, &devices);
652
653         /* Watch out for class_for_each_device / class_find_device API by
654          * Dave Young <hidave.darkstar@gmail.com> */
655         list_for_each_entry(ici, &hosts, list) {
656                 if (icd->iface == ici->nr) {
657                         ret = 1;
658                         icd->dev.parent = &ici->dev;
659                         break;
660                 }
661         }
662
663         mutex_unlock(&list_lock);
664
665         if (ret)
666                 ret = device_register_link(icd);
667
668         return ret;
669 }
670
671 static int soc_camera_probe(struct device *dev)
672 {
673         struct soc_camera_device *icd = to_soc_camera_dev(dev);
674         struct soc_camera_host *ici =
675                 to_soc_camera_host(icd->dev.parent);
676         int ret;
677
678         if (!icd->ops->probe)
679                 return -ENODEV;
680
681         /* We only call ->add() here to activate and probe the camera.
682          * We shall ->remove() and deactivate it immediately afterwards. */
683         ret = ici->ops->add(icd);
684         if (ret < 0)
685                 return ret;
686
687         ret = icd->ops->probe(icd);
688         if (ret >= 0) {
689                 const struct v4l2_queryctrl *qctrl;
690
691                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
692                 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
693                 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
694                 icd->exposure = qctrl ? qctrl->default_value :
695                         (unsigned short)~0;
696         }
697         ici->ops->remove(icd);
698
699         return ret;
700 }
701
702 /* This is called on device_unregister, which only means we have to disconnect
703  * from the host, but not remove ourselves from the device list */
704 static int soc_camera_remove(struct device *dev)
705 {
706         struct soc_camera_device *icd = to_soc_camera_dev(dev);
707
708         if (icd->ops->remove)
709                 icd->ops->remove(icd);
710
711         return 0;
712 }
713
714 static int soc_camera_suspend(struct device *dev, pm_message_t state)
715 {
716         struct soc_camera_device *icd = to_soc_camera_dev(dev);
717         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
718         int ret = 0;
719
720         if (ici->ops->suspend)
721                 ret = ici->ops->suspend(icd, state);
722
723         return ret;
724 }
725
726 static int soc_camera_resume(struct device *dev)
727 {
728         struct soc_camera_device *icd = to_soc_camera_dev(dev);
729         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
730         int ret = 0;
731
732         if (ici->ops->resume)
733                 ret = ici->ops->resume(icd);
734
735         return ret;
736 }
737
738 static struct bus_type soc_camera_bus_type = {
739         .name           = "soc-camera",
740         .probe          = soc_camera_probe,
741         .remove         = soc_camera_remove,
742         .suspend        = soc_camera_suspend,
743         .resume         = soc_camera_resume,
744 };
745
746 static struct device_driver ic_drv = {
747         .name   = "camera",
748         .bus    = &soc_camera_bus_type,
749         .owner  = THIS_MODULE,
750 };
751
752 static void dummy_release(struct device *dev)
753 {
754 }
755
756 int soc_camera_host_register(struct soc_camera_host *ici)
757 {
758         int ret;
759         struct soc_camera_host *ix;
760
761         if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
762                 return -EINVAL;
763
764         /* Number might be equal to the platform device ID */
765         dev_set_name(&ici->dev, "camera_host%d", ici->nr);
766
767         mutex_lock(&list_lock);
768         list_for_each_entry(ix, &hosts, list) {
769                 if (ix->nr == ici->nr) {
770                         mutex_unlock(&list_lock);
771                         return -EBUSY;
772                 }
773         }
774
775         list_add_tail(&ici->list, &hosts);
776         mutex_unlock(&list_lock);
777
778         ici->dev.release = dummy_release;
779
780         ret = device_register(&ici->dev);
781
782         if (ret)
783                 goto edevr;
784
785         scan_add_host(ici);
786
787         return 0;
788
789 edevr:
790         mutex_lock(&list_lock);
791         list_del(&ici->list);
792         mutex_unlock(&list_lock);
793
794         return ret;
795 }
796 EXPORT_SYMBOL(soc_camera_host_register);
797
798 /* Unregister all clients! */
799 void soc_camera_host_unregister(struct soc_camera_host *ici)
800 {
801         struct soc_camera_device *icd;
802
803         mutex_lock(&list_lock);
804
805         list_del(&ici->list);
806
807         list_for_each_entry(icd, &devices, list) {
808                 if (icd->dev.parent == &ici->dev) {
809                         device_unregister(&icd->dev);
810                         /* Not before device_unregister(), .remove
811                          * needs parent to call ici->ops->remove() */
812                         icd->dev.parent = NULL;
813                         memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
814                 }
815         }
816
817         mutex_unlock(&list_lock);
818
819         device_unregister(&ici->dev);
820 }
821 EXPORT_SYMBOL(soc_camera_host_unregister);
822
823 /* Image capture device */
824 int soc_camera_device_register(struct soc_camera_device *icd)
825 {
826         struct soc_camera_device *ix;
827         int num = -1, i;
828
829         if (!icd)
830                 return -EINVAL;
831
832         for (i = 0; i < 256 && num < 0; i++) {
833                 num = i;
834                 list_for_each_entry(ix, &devices, list) {
835                         if (ix->iface == icd->iface && ix->devnum == i) {
836                                 num = -1;
837                                 break;
838                         }
839                 }
840         }
841
842         if (num < 0)
843                 /* ok, we have 256 cameras on this host...
844                  * man, stay reasonable... */
845                 return -ENOMEM;
846
847         icd->devnum = num;
848         icd->dev.bus = &soc_camera_bus_type;
849         dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
850
851         icd->dev.release = dummy_release;
852
853         return scan_add_device(icd);
854 }
855 EXPORT_SYMBOL(soc_camera_device_register);
856
857 void soc_camera_device_unregister(struct soc_camera_device *icd)
858 {
859         mutex_lock(&list_lock);
860         list_del(&icd->list);
861
862         /* The bus->remove will be eventually called */
863         if (icd->dev.parent)
864                 device_unregister(&icd->dev);
865         mutex_unlock(&list_lock);
866 }
867 EXPORT_SYMBOL(soc_camera_device_unregister);
868
869 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
870         .vidioc_querycap         = soc_camera_querycap,
871         .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
872         .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
873         .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
874         .vidioc_enum_input       = soc_camera_enum_input,
875         .vidioc_g_input          = soc_camera_g_input,
876         .vidioc_s_input          = soc_camera_s_input,
877         .vidioc_s_std            = soc_camera_s_std,
878         .vidioc_reqbufs          = soc_camera_reqbufs,
879         .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
880         .vidioc_querybuf         = soc_camera_querybuf,
881         .vidioc_qbuf             = soc_camera_qbuf,
882         .vidioc_dqbuf            = soc_camera_dqbuf,
883         .vidioc_streamon         = soc_camera_streamon,
884         .vidioc_streamoff        = soc_camera_streamoff,
885         .vidioc_queryctrl        = soc_camera_queryctrl,
886         .vidioc_g_ctrl           = soc_camera_g_ctrl,
887         .vidioc_s_ctrl           = soc_camera_s_ctrl,
888         .vidioc_cropcap          = soc_camera_cropcap,
889         .vidioc_g_crop           = soc_camera_g_crop,
890         .vidioc_s_crop           = soc_camera_s_crop,
891         .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
892 #ifdef CONFIG_VIDEO_ADV_DEBUG
893         .vidioc_g_register       = soc_camera_g_register,
894         .vidioc_s_register       = soc_camera_s_register,
895 #endif
896 };
897
898 int soc_camera_video_start(struct soc_camera_device *icd)
899 {
900         struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
901         int err = -ENOMEM;
902         struct video_device *vdev;
903
904         if (!icd->dev.parent)
905                 return -ENODEV;
906
907         vdev = video_device_alloc();
908         if (!vdev)
909                 goto evidallocd;
910         dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
911
912         strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
913         /* Maybe better &ici->dev */
914         vdev->parent            = &icd->dev;
915         vdev->current_norm      = V4L2_STD_UNKNOWN;
916         vdev->fops              = &soc_camera_fops;
917         vdev->ioctl_ops         = &soc_camera_ioctl_ops;
918         vdev->release           = video_device_release;
919         vdev->minor             = -1;
920         vdev->tvnorms           = V4L2_STD_UNKNOWN,
921
922         icd->current_fmt = &icd->formats[0];
923
924         err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
925         if (err < 0) {
926                 dev_err(vdev->parent, "video_register_device failed\n");
927                 goto evidregd;
928         }
929         icd->vdev = vdev;
930
931         return 0;
932
933 evidregd:
934         video_device_release(vdev);
935 evidallocd:
936         return err;
937 }
938 EXPORT_SYMBOL(soc_camera_video_start);
939
940 void soc_camera_video_stop(struct soc_camera_device *icd)
941 {
942         struct video_device *vdev = icd->vdev;
943
944         dev_dbg(&icd->dev, "%s\n", __func__);
945
946         if (!icd->dev.parent || !vdev)
947                 return;
948
949         mutex_lock(&video_lock);
950         video_unregister_device(vdev);
951         icd->vdev = NULL;
952         mutex_unlock(&video_lock);
953 }
954 EXPORT_SYMBOL(soc_camera_video_stop);
955
956 static int __init soc_camera_init(void)
957 {
958         int ret = bus_register(&soc_camera_bus_type);
959         if (ret)
960                 return ret;
961         ret = driver_register(&ic_drv);
962         if (ret)
963                 goto edrvr;
964
965         return 0;
966
967 edrvr:
968         bus_unregister(&soc_camera_bus_type);
969         return ret;
970 }
971
972 static void __exit soc_camera_exit(void)
973 {
974         driver_unregister(&ic_drv);
975         bus_unregister(&soc_camera_bus_type);
976 }
977
978 module_init(soc_camera_init);
979 module_exit(soc_camera_exit);
980
981 MODULE_DESCRIPTION("Image capture bus driver");
982 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
983 MODULE_LICENSE("GPL");