V4L/DVB (8337): soc_camera: make videobuf independent
[linux-2.6/verdex.git] / drivers / media / video / soc_camera.c
blob38a89f1331644c48cfd7c44a1f23ca86dfa24957
1 /*
2 * camera image capture (abstract) bus driver
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
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.
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>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-dev.h>
29 #include <media/videobuf-core.h>
30 #include <media/soc_camera.h>
32 static LIST_HEAD(hosts);
33 static LIST_HEAD(devices);
34 static DEFINE_MUTEX(list_lock);
35 static DEFINE_MUTEX(video_lock);
37 const static struct soc_camera_data_format*
38 format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
40 unsigned int i;
42 for (i = 0; i < icd->num_formats; i++)
43 if (icd->formats[i].fourcc == fourcc)
44 return icd->formats + i;
45 return NULL;
48 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
49 struct v4l2_format *f)
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;
59 WARN_ON(priv != file->private_data);
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;
68 dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc);
70 field = f->fmt.pix.field;
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;
79 /* test physical bus parameters */
80 ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
81 if (ret)
82 return ret;
84 /* limit format to hardware capabilities */
85 ret = ici->ops->try_fmt_cap(icd, f);
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;
94 return ret;
97 static int soc_camera_enum_input(struct file *file, void *priv,
98 struct v4l2_input *inp)
100 if (inp->index != 0)
101 return -EINVAL;
103 inp->type = V4L2_INPUT_TYPE_CAMERA;
104 inp->std = V4L2_STD_UNKNOWN;
105 strcpy(inp->name, "Camera");
107 return 0;
110 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
112 *i = 0;
114 return 0;
117 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
119 if (i > 0)
120 return -EINVAL;
122 return 0;
125 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
127 return 0;
130 static int soc_camera_reqbufs(struct file *file, void *priv,
131 struct v4l2_requestbuffers *p)
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);
139 WARN_ON(priv != file->private_data);
141 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
143 ret = videobuf_reqbufs(&icf->vb_vidq, p);
144 if (ret < 0)
145 return ret;
147 return ici->ops->reqbufs(icf, p);
150 static int soc_camera_querybuf(struct file *file, void *priv,
151 struct v4l2_buffer *p)
153 struct soc_camera_file *icf = file->private_data;
155 WARN_ON(priv != file->private_data);
157 return videobuf_querybuf(&icf->vb_vidq, p);
160 static int soc_camera_qbuf(struct file *file, void *priv,
161 struct v4l2_buffer *p)
163 struct soc_camera_file *icf = file->private_data;
165 WARN_ON(priv != file->private_data);
167 return videobuf_qbuf(&icf->vb_vidq, p);
170 static int soc_camera_dqbuf(struct file *file, void *priv,
171 struct v4l2_buffer *p)
173 struct soc_camera_file *icf = file->private_data;
175 WARN_ON(priv != file->private_data);
177 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
180 static int soc_camera_open(struct inode *inode, struct file *file)
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;
189 icf = vmalloc(sizeof(*icf));
190 if (!icf)
191 return -ENOMEM;
193 /* Protect against icd->remove() until we module_get() both drivers. */
194 mutex_lock(&video_lock);
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);
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;
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;
212 icf->icd = icd;
214 icf->lock = ici->ops->spinlock_alloc(icf);
215 if (!icf->lock) {
216 ret = -ENOMEM;
217 goto esla;
220 icd->use_count++;
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;
232 mutex_unlock(&video_lock);
234 file->private_data = icf;
235 dev_dbg(&icd->dev, "camera device open\n");
237 ici->ops->init_videobuf(&icf->vb_vidq, icf->lock, icd);
239 return 0;
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;
257 static int soc_camera_close(struct inode *inode, struct file *file)
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;
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);
276 vfree(icf);
278 dev_dbg(vdev->dev, "camera device close\n");
280 return 0;
283 static ssize_t soc_camera_read(struct file *file, char __user *buf,
284 size_t count, loff_t *ppos)
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;
291 dev_err(vdev->dev, "camera device read not implemented\n");
293 return err;
296 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
298 struct soc_camera_file *icf = file->private_data;
299 struct soc_camera_device *icd = icf->icd;
300 int err;
302 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
304 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
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);
311 return err;
314 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
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);
321 if (list_empty(&icf->vb_vidq.stream)) {
322 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
323 return POLLERR;
326 return ici->ops->poll(file, pt);
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,
342 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
343 struct v4l2_format *f)
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;
353 WARN_ON(priv != file->private_data);
355 data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
356 if (!data_fmt)
357 return -EINVAL;
359 /* buswidth may be further adjusted by the ici */
360 icd->buswidth = data_fmt->depth;
362 ret = soc_camera_try_fmt_vid_cap(file, icf, f);
363 if (ret < 0)
364 return ret;
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;
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);
382 dev_dbg(&icd->dev, "set width: %d height: %d\n",
383 icd->width, icd->height);
385 /* set physical bus parameters */
386 return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
389 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
390 struct v4l2_fmtdesc *f)
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;
396 WARN_ON(priv != file->private_data);
398 if (f->index >= icd->num_formats)
399 return -EINVAL;
401 format = &icd->formats[f->index];
403 strlcpy(f->description, format->name, sizeof(f->description));
404 f->pixelformat = format->fourcc;
405 return 0;
408 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
409 struct v4l2_format *f)
411 struct soc_camera_file *icf = file->private_data;
412 struct soc_camera_device *icd = icf->icd;
414 WARN_ON(priv != file->private_data);
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;
429 static int soc_camera_querycap(struct file *file, void *priv,
430 struct v4l2_capability *cap)
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);
437 WARN_ON(priv != file->private_data);
439 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
440 return ici->ops->querycap(ici, cap);
443 static int soc_camera_streamon(struct file *file, void *priv,
444 enum v4l2_buf_type i)
446 struct soc_camera_file *icf = file->private_data;
447 struct soc_camera_device *icd = icf->icd;
449 WARN_ON(priv != file->private_data);
451 dev_dbg(&icd->dev, "%s\n", __func__);
453 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
454 return -EINVAL;
456 icd->ops->start_capture(icd);
458 /* This calls buf_queue from host driver's videobuf_queue_ops */
459 return videobuf_streamon(&icf->vb_vidq);
462 static int soc_camera_streamoff(struct file *file, void *priv,
463 enum v4l2_buf_type i)
465 struct soc_camera_file *icf = file->private_data;
466 struct soc_camera_device *icd = icf->icd;
468 WARN_ON(priv != file->private_data);
470 dev_dbg(&icd->dev, "%s\n", __func__);
472 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
473 return -EINVAL;
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);
479 icd->ops->stop_capture(icd);
481 return 0;
484 static int soc_camera_queryctrl(struct file *file, void *priv,
485 struct v4l2_queryctrl *qc)
487 struct soc_camera_file *icf = file->private_data;
488 struct soc_camera_device *icd = icf->icd;
489 int i;
491 WARN_ON(priv != file->private_data);
493 if (!qc->id)
494 return -EINVAL;
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;
503 return -EINVAL;
506 static int soc_camera_g_ctrl(struct file *file, void *priv,
507 struct v4l2_control *ctrl)
509 struct soc_camera_file *icf = file->private_data;
510 struct soc_camera_device *icd = icf->icd;
512 WARN_ON(priv != file->private_data);
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;
527 if (icd->ops->get_control)
528 return icd->ops->get_control(icd, ctrl);
529 return -EINVAL;
532 static int soc_camera_s_ctrl(struct file *file, void *priv,
533 struct v4l2_control *ctrl)
535 struct soc_camera_file *icf = file->private_data;
536 struct soc_camera_device *icd = icf->icd;
538 WARN_ON(priv != file->private_data);
540 if (icd->ops->set_control)
541 return icd->ops->set_control(icd, ctrl);
542 return -EINVAL;
545 static int soc_camera_cropcap(struct file *file, void *fh,
546 struct v4l2_cropcap *a)
548 struct soc_camera_file *icf = file->private_data;
549 struct soc_camera_device *icd = icf->icd;
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;
563 return 0;
566 static int soc_camera_g_crop(struct file *file, void *fh,
567 struct v4l2_crop *a)
569 struct soc_camera_file *icf = file->private_data;
570 struct soc_camera_device *icd = icf->icd;
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;
578 return 0;
581 static int soc_camera_s_crop(struct file *file, void *fh,
582 struct v4l2_crop *a)
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;
590 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
591 return -EINVAL;
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;
601 return ret;
604 static int soc_camera_g_chip_ident(struct file *file, void *fh,
605 struct v4l2_chip_ident *id)
607 struct soc_camera_file *icf = file->private_data;
608 struct soc_camera_device *icd = icf->icd;
610 if (!icd->ops->get_chip_id)
611 return -EINVAL;
613 return icd->ops->get_chip_id(icd, id);
616 #ifdef CONFIG_VIDEO_ADV_DEBUG
617 static int soc_camera_g_register(struct file *file, void *fh,
618 struct v4l2_register *reg)
620 struct soc_camera_file *icf = file->private_data;
621 struct soc_camera_device *icd = icf->icd;
623 if (!icd->ops->get_register)
624 return -EINVAL;
626 return icd->ops->get_register(icd, reg);
629 static int soc_camera_s_register(struct file *file, void *fh,
630 struct v4l2_register *reg)
632 struct soc_camera_file *icf = file->private_data;
633 struct soc_camera_device *icd = icf->icd;
635 if (!icd->ops->set_register)
636 return -EINVAL;
638 return icd->ops->set_register(icd, reg);
640 #endif
642 static int device_register_link(struct soc_camera_device *icd)
644 int ret = device_register(&icd->dev);
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;
662 /* So far this function cannot fail */
663 static void scan_add_host(struct soc_camera_host *ici)
665 struct soc_camera_device *icd;
667 mutex_lock(&list_lock);
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);
676 mutex_unlock(&list_lock);
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)
683 struct soc_camera_host *ici;
684 int ret = 0;
686 mutex_lock(&list_lock);
688 list_add_tail(&icd->list, &devices);
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;
700 mutex_unlock(&list_lock);
702 if (ret)
703 ret = device_register_link(icd);
705 return ret;
708 static int soc_camera_probe(struct device *dev)
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;
715 if (!icd->ops->probe)
716 return -ENODEV;
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;
724 ret = icd->ops->probe(icd);
725 if (ret >= 0) {
726 const struct v4l2_queryctrl *qctrl;
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;
734 ici->ops->remove(icd);
736 return ret;
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)
743 struct soc_camera_device *icd = to_soc_camera_dev(dev);
745 if (icd->ops->remove)
746 icd->ops->remove(icd);
748 return 0;
751 static struct bus_type soc_camera_bus_type = {
752 .name = "soc-camera",
753 .probe = soc_camera_probe,
754 .remove = soc_camera_remove,
757 static struct device_driver ic_drv = {
758 .name = "camera",
759 .bus = &soc_camera_bus_type,
760 .owner = THIS_MODULE,
763 static void dummy_release(struct device *dev)
767 static spinlock_t *spinlock_alloc(struct soc_camera_file *icf)
769 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
771 if (lock)
772 spin_lock_init(lock);
774 return lock;
777 static void spinlock_free(spinlock_t *lock)
779 kfree(lock);
782 int soc_camera_host_register(struct soc_camera_host *ici)
784 int ret;
785 struct soc_camera_host *ix;
787 if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
788 return -EINVAL;
790 /* Number might be equal to the platform device ID */
791 sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
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;
801 list_add_tail(&ici->list, &hosts);
802 mutex_unlock(&list_lock);
804 ici->dev.release = dummy_release;
806 ret = device_register(&ici->dev);
808 if (ret)
809 goto edevr;
811 if (!ici->ops->spinlock_alloc) {
812 ici->ops->spinlock_alloc = spinlock_alloc;
813 ici->ops->spinlock_free = spinlock_free;
816 scan_add_host(ici);
818 return 0;
820 edevr:
821 mutex_lock(&list_lock);
822 list_del(&ici->list);
823 mutex_unlock(&list_lock);
825 return ret;
827 EXPORT_SYMBOL(soc_camera_host_register);
829 /* Unregister all clients! */
830 void soc_camera_host_unregister(struct soc_camera_host *ici)
832 struct soc_camera_device *icd;
834 mutex_lock(&list_lock);
836 list_del(&ici->list);
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));
848 mutex_unlock(&list_lock);
850 device_unregister(&ici->dev);
852 EXPORT_SYMBOL(soc_camera_host_unregister);
854 /* Image capture device */
855 int soc_camera_device_register(struct soc_camera_device *icd)
857 struct soc_camera_device *ix;
858 int num = -1, i;
860 if (!icd)
861 return -EINVAL;
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;
873 if (num < 0)
874 /* ok, we have 256 cameras on this host...
875 * man, stay reasonable... */
876 return -ENOMEM;
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);
883 icd->dev.release = dummy_release;
885 return scan_add_device(icd);
887 EXPORT_SYMBOL(soc_camera_device_register);
889 void soc_camera_device_unregister(struct soc_camera_device *icd)
891 mutex_lock(&list_lock);
892 list_del(&icd->list);
894 /* The bus->remove will be eventually called */
895 if (icd->dev.parent)
896 device_unregister(&icd->dev);
897 mutex_unlock(&list_lock);
899 EXPORT_SYMBOL(soc_camera_device_unregister);
901 int soc_camera_video_start(struct soc_camera_device *icd)
903 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
904 int err = -ENOMEM;
905 struct video_device *vdev;
907 if (!icd->dev.parent)
908 return -ENODEV;
910 vdev = video_device_alloc();
911 if (!vdev)
912 goto evidallocd;
913 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
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
951 icd->current_fmt = &icd->formats[0];
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;
958 icd->vdev = vdev;
960 return 0;
962 evidregd:
963 video_device_release(vdev);
964 evidallocd:
965 return err;
967 EXPORT_SYMBOL(soc_camera_video_start);
969 void soc_camera_video_stop(struct soc_camera_device *icd)
971 struct video_device *vdev = icd->vdev;
973 dev_dbg(&icd->dev, "%s\n", __func__);
975 if (!icd->dev.parent || !vdev)
976 return;
978 mutex_lock(&video_lock);
979 video_unregister_device(vdev);
980 icd->vdev = NULL;
981 mutex_unlock(&video_lock);
983 EXPORT_SYMBOL(soc_camera_video_stop);
985 static int __init soc_camera_init(void)
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;
994 return 0;
996 edrvr:
997 bus_unregister(&soc_camera_bus_type);
998 return ret;
1001 static void __exit soc_camera_exit(void)
1003 driver_unregister(&ic_drv);
1004 bus_unregister(&soc_camera_bus_type);
1007 module_init(soc_camera_init);
1008 module_exit(soc_camera_exit);
1010 MODULE_DESCRIPTION("Image capture bus driver");
1011 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1012 MODULE_LICENSE("GPL");