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