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-ioctl.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf-core.h>
31 #include <media/soc_camera.h>
33 static LIST_HEAD(hosts);
34 static LIST_HEAD(devices);
35 static DEFINE_MUTEX(list_lock);
36 static DEFINE_MUTEX(video_lock);
38 const struct soc_camera_data_format *soc_camera_format_by_fourcc(
39 struct soc_camera_device *icd, unsigned int fourcc)
43 for (i = 0; i < icd->num_formats; i++)
44 if (icd->formats[i].fourcc == fourcc)
45 return icd->formats + i;
48 EXPORT_SYMBOL(soc_camera_format_by_fourcc);
50 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
51 struct soc_camera_device *icd, unsigned int fourcc)
55 for (i = 0; i < icd->num_user_formats; i++)
56 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
57 return icd->user_formats + i;
60 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
63 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
64 * @icl: camera platform parameters
65 * @flags: flags to be inverted according to platform configuration
66 * @return: resulting flags
68 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
73 /* If only one of the two polarities is supported, switch to the opposite */
74 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
75 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
76 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
77 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
80 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
81 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
82 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
83 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
86 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
87 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
88 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
89 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
94 EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
96 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
97 struct v4l2_format *f)
99 struct soc_camera_file *icf = file->private_data;
100 struct soc_camera_device *icd = icf->icd;
101 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
102 enum v4l2_field field;
105 WARN_ON(priv != file->private_data);
108 * TODO: this might also have to migrate to host-drivers, if anyone
109 * wishes to support other fields
111 field = f->fmt.pix.field;
113 if (field == V4L2_FIELD_ANY) {
114 f->fmt.pix.field = V4L2_FIELD_NONE;
115 } else if (field != V4L2_FIELD_NONE) {
116 dev_err(&icd->dev, "Field type invalid.\n");
120 /* limit format to hardware capabilities */
121 ret = ici->ops->try_fmt(icd, f);
126 static int soc_camera_enum_input(struct file *file, void *priv,
127 struct v4l2_input *inp)
132 inp->type = V4L2_INPUT_TYPE_CAMERA;
133 inp->std = V4L2_STD_UNKNOWN;
134 strcpy(inp->name, "Camera");
139 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
146 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
154 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
159 static int soc_camera_reqbufs(struct file *file, void *priv,
160 struct v4l2_requestbuffers *p)
163 struct soc_camera_file *icf = file->private_data;
164 struct soc_camera_device *icd = icf->icd;
165 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
167 WARN_ON(priv != file->private_data);
169 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
171 ret = videobuf_reqbufs(&icf->vb_vidq, p);
175 return ici->ops->reqbufs(icf, p);
178 static int soc_camera_querybuf(struct file *file, void *priv,
179 struct v4l2_buffer *p)
181 struct soc_camera_file *icf = file->private_data;
183 WARN_ON(priv != file->private_data);
185 return videobuf_querybuf(&icf->vb_vidq, p);
188 static int soc_camera_qbuf(struct file *file, void *priv,
189 struct v4l2_buffer *p)
191 struct soc_camera_file *icf = file->private_data;
193 WARN_ON(priv != file->private_data);
195 return videobuf_qbuf(&icf->vb_vidq, p);
198 static int soc_camera_dqbuf(struct file *file, void *priv,
199 struct v4l2_buffer *p)
201 struct soc_camera_file *icf = file->private_data;
203 WARN_ON(priv != file->private_data);
205 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
208 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
210 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
213 if (!ici->ops->get_formats)
215 * Fallback mode - the host will have to serve all
216 * sensor-provided formats one-to-one to the user
218 fmts = icd->num_formats;
221 * First pass - only count formats this host-sensor
222 * configuration can provide
224 for (i = 0; i < icd->num_formats; i++)
225 fmts += ici->ops->get_formats(icd, i, NULL);
231 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
232 if (!icd->user_formats)
235 icd->num_user_formats = fmts;
238 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
240 /* Second pass - actually fill data formats */
241 for (i = 0; i < icd->num_formats; i++)
242 if (!ici->ops->get_formats) {
243 icd->user_formats[i].host_fmt = icd->formats + i;
244 icd->user_formats[i].cam_fmt = icd->formats + i;
245 icd->user_formats[i].buswidth = icd->formats[i].depth;
247 fmts += ici->ops->get_formats(icd, i,
248 &icd->user_formats[fmts]);
251 icd->current_fmt = icd->user_formats[0].host_fmt;
256 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
258 vfree(icd->user_formats);
261 static int soc_camera_open(struct inode *inode, struct file *file)
263 struct video_device *vdev;
264 struct soc_camera_device *icd;
265 struct soc_camera_host *ici;
266 struct soc_camera_file *icf;
269 icf = vmalloc(sizeof(*icf));
273 /* Protect against icd->remove() until we module_get() both drivers. */
274 mutex_lock(&video_lock);
276 vdev = video_devdata(file);
277 icd = container_of(vdev->parent, struct soc_camera_device, dev);
278 ici = to_soc_camera_host(icd->dev.parent);
280 if (!try_module_get(icd->ops->owner)) {
281 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
286 if (!try_module_get(ici->ops->owner)) {
287 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
295 /* Now we really have to activate the camera */
296 if (icd->use_count == 1) {
297 ret = soc_camera_init_user_formats(icd);
300 ret = ici->ops->add(icd);
302 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
307 mutex_unlock(&video_lock);
309 file->private_data = icf;
310 dev_dbg(&icd->dev, "camera device open\n");
312 ici->ops->init_videobuf(&icf->vb_vidq, icd);
316 /* All errors are entered with the video_lock held */
318 soc_camera_free_user_formats(icd);
321 module_put(ici->ops->owner);
323 module_put(icd->ops->owner);
325 mutex_unlock(&video_lock);
330 static int soc_camera_close(struct inode *inode, struct file *file)
332 struct soc_camera_file *icf = file->private_data;
333 struct soc_camera_device *icd = icf->icd;
334 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
335 struct video_device *vdev = icd->vdev;
337 mutex_lock(&video_lock);
339 if (!icd->use_count) {
340 ici->ops->remove(icd);
341 soc_camera_free_user_formats(icd);
343 module_put(icd->ops->owner);
344 module_put(ici->ops->owner);
345 mutex_unlock(&video_lock);
349 dev_dbg(vdev->parent, "camera device close\n");
354 static ssize_t soc_camera_read(struct file *file, char __user *buf,
355 size_t count, loff_t *ppos)
357 struct soc_camera_file *icf = file->private_data;
358 struct soc_camera_device *icd = icf->icd;
359 struct video_device *vdev = icd->vdev;
362 dev_err(vdev->parent, "camera device read not implemented\n");
367 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
369 struct soc_camera_file *icf = file->private_data;
370 struct soc_camera_device *icd = icf->icd;
373 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
375 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
377 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
378 (unsigned long)vma->vm_start,
379 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
385 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
387 struct soc_camera_file *icf = file->private_data;
388 struct soc_camera_device *icd = icf->icd;
389 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
391 if (list_empty(&icf->vb_vidq.stream)) {
392 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
396 return ici->ops->poll(file, pt);
399 static struct file_operations soc_camera_fops = {
400 .owner = THIS_MODULE,
401 .open = soc_camera_open,
402 .release = soc_camera_close,
403 .ioctl = video_ioctl2,
404 .read = soc_camera_read,
405 .mmap = soc_camera_mmap,
406 .poll = soc_camera_poll,
410 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
411 struct v4l2_format *f)
413 struct soc_camera_file *icf = file->private_data;
414 struct soc_camera_device *icd = icf->icd;
415 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
416 struct v4l2_pix_format *pix = &f->fmt.pix;
417 __u32 pixfmt = pix->pixelformat;
419 struct v4l2_rect rect;
421 WARN_ON(priv != file->private_data);
423 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
427 rect.left = icd->x_current;
428 rect.top = icd->y_current;
429 rect.width = pix->width;
430 rect.height = pix->height;
431 ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
434 } else if (!icd->current_fmt ||
435 icd->current_fmt->fourcc != pixfmt) {
437 "Host driver hasn't set up current format correctly!\n");
441 icd->width = rect.width;
442 icd->height = rect.height;
443 icf->vb_vidq.field = pix->field;
444 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
445 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
448 dev_dbg(&icd->dev, "set width: %d height: %d\n",
449 icd->width, icd->height);
451 /* set physical bus parameters */
452 return ici->ops->set_bus_param(icd, pixfmt);
455 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
456 struct v4l2_fmtdesc *f)
458 struct soc_camera_file *icf = file->private_data;
459 struct soc_camera_device *icd = icf->icd;
460 const struct soc_camera_data_format *format;
462 WARN_ON(priv != file->private_data);
464 if (f->index >= icd->num_user_formats)
467 format = icd->user_formats[f->index].host_fmt;
469 strlcpy(f->description, format->name, sizeof(f->description));
470 f->pixelformat = format->fourcc;
474 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
475 struct v4l2_format *f)
477 struct soc_camera_file *icf = file->private_data;
478 struct soc_camera_device *icd = icf->icd;
479 struct v4l2_pix_format *pix = &f->fmt.pix;
481 WARN_ON(priv != file->private_data);
483 pix->width = icd->width;
484 pix->height = icd->height;
485 pix->field = icf->vb_vidq.field;
486 pix->pixelformat = icd->current_fmt->fourcc;
487 pix->bytesperline = pix->width *
488 DIV_ROUND_UP(icd->current_fmt->depth, 8);
489 pix->sizeimage = pix->height * pix->bytesperline;
490 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
491 icd->current_fmt->fourcc);
495 static int soc_camera_querycap(struct file *file, void *priv,
496 struct v4l2_capability *cap)
498 struct soc_camera_file *icf = file->private_data;
499 struct soc_camera_device *icd = icf->icd;
500 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
502 WARN_ON(priv != file->private_data);
504 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
505 return ici->ops->querycap(ici, cap);
508 static int soc_camera_streamon(struct file *file, void *priv,
509 enum v4l2_buf_type i)
511 struct soc_camera_file *icf = file->private_data;
512 struct soc_camera_device *icd = icf->icd;
514 WARN_ON(priv != file->private_data);
516 dev_dbg(&icd->dev, "%s\n", __func__);
518 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
521 icd->ops->start_capture(icd);
523 /* This calls buf_queue from host driver's videobuf_queue_ops */
524 return videobuf_streamon(&icf->vb_vidq);
527 static int soc_camera_streamoff(struct file *file, void *priv,
528 enum v4l2_buf_type i)
530 struct soc_camera_file *icf = file->private_data;
531 struct soc_camera_device *icd = icf->icd;
533 WARN_ON(priv != file->private_data);
535 dev_dbg(&icd->dev, "%s\n", __func__);
537 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
540 /* This calls buf_release from host driver's videobuf_queue_ops for all
541 * remaining buffers. When the last buffer is freed, stop capture */
542 videobuf_streamoff(&icf->vb_vidq);
544 icd->ops->stop_capture(icd);
549 static int soc_camera_queryctrl(struct file *file, void *priv,
550 struct v4l2_queryctrl *qc)
552 struct soc_camera_file *icf = file->private_data;
553 struct soc_camera_device *icd = icf->icd;
556 WARN_ON(priv != file->private_data);
561 for (i = 0; i < icd->ops->num_controls; i++)
562 if (qc->id == icd->ops->controls[i].id) {
563 memcpy(qc, &(icd->ops->controls[i]),
571 static int soc_camera_g_ctrl(struct file *file, void *priv,
572 struct v4l2_control *ctrl)
574 struct soc_camera_file *icf = file->private_data;
575 struct soc_camera_device *icd = icf->icd;
577 WARN_ON(priv != file->private_data);
581 if (icd->gain == (unsigned short)~0)
583 ctrl->value = icd->gain;
585 case V4L2_CID_EXPOSURE:
586 if (icd->exposure == (unsigned short)~0)
588 ctrl->value = icd->exposure;
592 if (icd->ops->get_control)
593 return icd->ops->get_control(icd, ctrl);
597 static int soc_camera_s_ctrl(struct file *file, void *priv,
598 struct v4l2_control *ctrl)
600 struct soc_camera_file *icf = file->private_data;
601 struct soc_camera_device *icd = icf->icd;
603 WARN_ON(priv != file->private_data);
605 if (icd->ops->set_control)
606 return icd->ops->set_control(icd, ctrl);
610 static int soc_camera_cropcap(struct file *file, void *fh,
611 struct v4l2_cropcap *a)
613 struct soc_camera_file *icf = file->private_data;
614 struct soc_camera_device *icd = icf->icd;
616 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
617 a->bounds.left = icd->x_min;
618 a->bounds.top = icd->y_min;
619 a->bounds.width = icd->width_max;
620 a->bounds.height = icd->height_max;
621 a->defrect.left = icd->x_min;
622 a->defrect.top = icd->y_min;
623 a->defrect.width = 640;
624 a->defrect.height = 480;
625 a->pixelaspect.numerator = 1;
626 a->pixelaspect.denominator = 1;
631 static int soc_camera_g_crop(struct file *file, void *fh,
634 struct soc_camera_file *icf = file->private_data;
635 struct soc_camera_device *icd = icf->icd;
637 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
638 a->c.left = icd->x_current;
639 a->c.top = icd->y_current;
640 a->c.width = icd->width;
641 a->c.height = icd->height;
646 static int soc_camera_s_crop(struct file *file, void *fh,
649 struct soc_camera_file *icf = file->private_data;
650 struct soc_camera_device *icd = icf->icd;
651 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
654 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
657 ret = ici->ops->set_fmt(icd, 0, &a->c);
659 icd->width = a->c.width;
660 icd->height = a->c.height;
661 icd->x_current = a->c.left;
662 icd->y_current = a->c.top;
668 static int soc_camera_g_chip_ident(struct file *file, void *fh,
669 struct v4l2_chip_ident *id)
671 struct soc_camera_file *icf = file->private_data;
672 struct soc_camera_device *icd = icf->icd;
674 if (!icd->ops->get_chip_id)
677 return icd->ops->get_chip_id(icd, id);
680 #ifdef CONFIG_VIDEO_ADV_DEBUG
681 static int soc_camera_g_register(struct file *file, void *fh,
682 struct v4l2_register *reg)
684 struct soc_camera_file *icf = file->private_data;
685 struct soc_camera_device *icd = icf->icd;
687 if (!icd->ops->get_register)
690 return icd->ops->get_register(icd, reg);
693 static int soc_camera_s_register(struct file *file, void *fh,
694 struct v4l2_register *reg)
696 struct soc_camera_file *icf = file->private_data;
697 struct soc_camera_device *icd = icf->icd;
699 if (!icd->ops->set_register)
702 return icd->ops->set_register(icd, reg);
706 static int device_register_link(struct soc_camera_device *icd)
708 int ret = device_register(&icd->dev);
711 /* Prevent calling device_unregister() */
712 icd->dev.parent = NULL;
713 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
714 /* Even if probe() was unsuccessful for all registered drivers,
715 * device_register() returns 0, and we add the link, just to
716 * document this camera's control device */
717 } else if (icd->control)
718 /* Have to sysfs_remove_link() before device_unregister()? */
719 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
722 "Failed creating the control symlink\n");
726 /* So far this function cannot fail */
727 static void scan_add_host(struct soc_camera_host *ici)
729 struct soc_camera_device *icd;
731 mutex_lock(&list_lock);
733 list_for_each_entry(icd, &devices, list) {
734 if (icd->iface == ici->nr) {
735 icd->dev.parent = &ici->dev;
736 device_register_link(icd);
740 mutex_unlock(&list_lock);
743 /* return: 0 if no match found or a match found and
744 * device_register() successful, error code otherwise */
745 static int scan_add_device(struct soc_camera_device *icd)
747 struct soc_camera_host *ici;
750 mutex_lock(&list_lock);
752 list_add_tail(&icd->list, &devices);
754 /* Watch out for class_for_each_device / class_find_device API by
755 * Dave Young <hidave.darkstar@gmail.com> */
756 list_for_each_entry(ici, &hosts, list) {
757 if (icd->iface == ici->nr) {
759 icd->dev.parent = &ici->dev;
764 mutex_unlock(&list_lock);
767 ret = device_register_link(icd);
772 static int soc_camera_probe(struct device *dev)
774 struct soc_camera_device *icd = to_soc_camera_dev(dev);
775 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
778 /* We only call ->add() here to activate and probe the camera.
779 * We shall ->remove() and deactivate it immediately afterwards. */
780 ret = ici->ops->add(icd);
784 ret = icd->ops->probe(icd);
786 const struct v4l2_queryctrl *qctrl;
788 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
789 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
790 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
791 icd->exposure = qctrl ? qctrl->default_value :
794 ici->ops->remove(icd);
799 /* This is called on device_unregister, which only means we have to disconnect
800 * from the host, but not remove ourselves from the device list */
801 static int soc_camera_remove(struct device *dev)
803 struct soc_camera_device *icd = to_soc_camera_dev(dev);
805 if (icd->ops->remove)
806 icd->ops->remove(icd);
811 static int soc_camera_suspend(struct device *dev, pm_message_t state)
813 struct soc_camera_device *icd = to_soc_camera_dev(dev);
814 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
817 if (ici->ops->suspend)
818 ret = ici->ops->suspend(icd, state);
823 static int soc_camera_resume(struct device *dev)
825 struct soc_camera_device *icd = to_soc_camera_dev(dev);
826 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
829 if (ici->ops->resume)
830 ret = ici->ops->resume(icd);
835 static struct bus_type soc_camera_bus_type = {
836 .name = "soc-camera",
837 .probe = soc_camera_probe,
838 .remove = soc_camera_remove,
839 .suspend = soc_camera_suspend,
840 .resume = soc_camera_resume,
843 static struct device_driver ic_drv = {
845 .bus = &soc_camera_bus_type,
846 .owner = THIS_MODULE,
849 static void dummy_release(struct device *dev)
853 int soc_camera_host_register(struct soc_camera_host *ici)
856 struct soc_camera_host *ix;
858 if (!ici || !ici->ops ||
859 !ici->ops->try_fmt ||
860 !ici->ops->set_fmt ||
861 !ici->ops->set_bus_param ||
862 !ici->ops->querycap ||
863 !ici->ops->init_videobuf ||
864 !ici->ops->reqbufs ||
870 /* Number might be equal to the platform device ID */
871 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
873 mutex_lock(&list_lock);
874 list_for_each_entry(ix, &hosts, list) {
875 if (ix->nr == ici->nr) {
876 mutex_unlock(&list_lock);
881 list_add_tail(&ici->list, &hosts);
882 mutex_unlock(&list_lock);
884 ici->dev.release = dummy_release;
886 ret = device_register(&ici->dev);
896 mutex_lock(&list_lock);
897 list_del(&ici->list);
898 mutex_unlock(&list_lock);
902 EXPORT_SYMBOL(soc_camera_host_register);
904 /* Unregister all clients! */
905 void soc_camera_host_unregister(struct soc_camera_host *ici)
907 struct soc_camera_device *icd;
909 mutex_lock(&list_lock);
911 list_del(&ici->list);
913 list_for_each_entry(icd, &devices, list) {
914 if (icd->dev.parent == &ici->dev) {
915 device_unregister(&icd->dev);
916 /* Not before device_unregister(), .remove
917 * needs parent to call ici->ops->remove() */
918 icd->dev.parent = NULL;
919 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
923 mutex_unlock(&list_lock);
925 device_unregister(&ici->dev);
927 EXPORT_SYMBOL(soc_camera_host_unregister);
929 /* Image capture device */
930 int soc_camera_device_register(struct soc_camera_device *icd)
932 struct soc_camera_device *ix;
935 if (!icd || !icd->ops ||
938 !icd->ops->release ||
939 !icd->ops->start_capture ||
940 !icd->ops->stop_capture ||
941 !icd->ops->set_fmt ||
942 !icd->ops->try_fmt ||
943 !icd->ops->query_bus_param ||
944 !icd->ops->set_bus_param)
947 for (i = 0; i < 256 && num < 0; i++) {
949 list_for_each_entry(ix, &devices, list) {
950 if (ix->iface == icd->iface && ix->devnum == i) {
958 /* ok, we have 256 cameras on this host...
959 * man, stay reasonable... */
963 icd->dev.bus = &soc_camera_bus_type;
964 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
966 icd->dev.release = dummy_release;
968 icd->host_priv = NULL;
970 return scan_add_device(icd);
972 EXPORT_SYMBOL(soc_camera_device_register);
974 void soc_camera_device_unregister(struct soc_camera_device *icd)
976 mutex_lock(&list_lock);
977 list_del(&icd->list);
979 /* The bus->remove will be eventually called */
981 device_unregister(&icd->dev);
982 mutex_unlock(&list_lock);
984 EXPORT_SYMBOL(soc_camera_device_unregister);
986 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
987 .vidioc_querycap = soc_camera_querycap,
988 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
989 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
990 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
991 .vidioc_enum_input = soc_camera_enum_input,
992 .vidioc_g_input = soc_camera_g_input,
993 .vidioc_s_input = soc_camera_s_input,
994 .vidioc_s_std = soc_camera_s_std,
995 .vidioc_reqbufs = soc_camera_reqbufs,
996 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
997 .vidioc_querybuf = soc_camera_querybuf,
998 .vidioc_qbuf = soc_camera_qbuf,
999 .vidioc_dqbuf = soc_camera_dqbuf,
1000 .vidioc_streamon = soc_camera_streamon,
1001 .vidioc_streamoff = soc_camera_streamoff,
1002 .vidioc_queryctrl = soc_camera_queryctrl,
1003 .vidioc_g_ctrl = soc_camera_g_ctrl,
1004 .vidioc_s_ctrl = soc_camera_s_ctrl,
1005 .vidioc_cropcap = soc_camera_cropcap,
1006 .vidioc_g_crop = soc_camera_g_crop,
1007 .vidioc_s_crop = soc_camera_s_crop,
1008 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1009 #ifdef CONFIG_VIDEO_ADV_DEBUG
1010 .vidioc_g_register = soc_camera_g_register,
1011 .vidioc_s_register = soc_camera_s_register,
1015 int soc_camera_video_start(struct soc_camera_device *icd)
1017 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1019 struct video_device *vdev;
1021 if (!icd->dev.parent)
1024 vdev = video_device_alloc();
1027 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
1029 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1030 /* Maybe better &ici->dev */
1031 vdev->parent = &icd->dev;
1032 vdev->current_norm = V4L2_STD_UNKNOWN;
1033 vdev->fops = &soc_camera_fops;
1034 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1035 vdev->release = video_device_release;
1037 vdev->tvnorms = V4L2_STD_UNKNOWN,
1039 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
1041 dev_err(vdev->parent, "video_register_device failed\n");
1049 video_device_release(vdev);
1053 EXPORT_SYMBOL(soc_camera_video_start);
1055 void soc_camera_video_stop(struct soc_camera_device *icd)
1057 struct video_device *vdev = icd->vdev;
1059 dev_dbg(&icd->dev, "%s\n", __func__);
1061 if (!icd->dev.parent || !vdev)
1064 mutex_lock(&video_lock);
1065 video_unregister_device(vdev);
1067 mutex_unlock(&video_lock);
1069 EXPORT_SYMBOL(soc_camera_video_stop);
1071 static int __init soc_camera_init(void)
1073 int ret = bus_register(&soc_camera_bus_type);
1076 ret = driver_register(&ic_drv);
1083 bus_unregister(&soc_camera_bus_type);
1087 static void __exit soc_camera_exit(void)
1089 driver_unregister(&ic_drv);
1090 bus_unregister(&soc_camera_bus_type);
1093 module_init(soc_camera_init);
1094 module_exit(soc_camera_exit);
1096 MODULE_DESCRIPTION("Image capture bus driver");
1097 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1098 MODULE_LICENSE("GPL");