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