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