ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / media / video / soc_camera.c
bloba1b92446c8b4ac78052a4849e23a9f428ccba2c1
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/soc_camera.h>
31 static LIST_HEAD(hosts);
32 static LIST_HEAD(devices);
33 static DEFINE_MUTEX(list_lock);
34 static DEFINE_MUTEX(video_lock);
36 const static struct soc_camera_data_format*
37 format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
39 unsigned int i;
41 for (i = 0; i < icd->num_formats; i++)
42 if (icd->formats[i].fourcc == fourcc)
43 return icd->formats + i;
44 return NULL;
47 static int soc_camera_try_fmt_cap(struct file *file, void *priv,
48 struct v4l2_format *f)
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;
58 WARN_ON(priv != file->private_data);
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;
67 dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc);
69 field = f->fmt.pix.field;
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;
78 /* test physical bus parameters */
79 ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
80 if (ret)
81 return ret;
83 /* limit format to hardware capabilities */
84 ret = ici->ops->try_fmt_cap(icd, f);
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;
93 return ret;
96 static int soc_camera_enum_input(struct file *file, void *priv,
97 struct v4l2_input *inp)
99 if (inp->index != 0)
100 return -EINVAL;
102 inp->type = V4L2_INPUT_TYPE_CAMERA;
103 inp->std = V4L2_STD_UNKNOWN;
104 strcpy(inp->name, "Camera");
106 return 0;
109 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
111 *i = 0;
113 return 0;
116 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
118 if (i > 0)
119 return -EINVAL;
121 return 0;
124 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
126 return 0;
129 static int soc_camera_reqbufs(struct file *file, void *priv,
130 struct v4l2_requestbuffers *p)
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);
138 WARN_ON(priv != file->private_data);
140 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
142 ret = videobuf_reqbufs(&icf->vb_vidq, p);
143 if (ret < 0)
144 return ret;
146 return ici->ops->reqbufs(icf, p);
149 static int soc_camera_querybuf(struct file *file, void *priv,
150 struct v4l2_buffer *p)
152 struct soc_camera_file *icf = file->private_data;
154 WARN_ON(priv != file->private_data);
156 return videobuf_querybuf(&icf->vb_vidq, p);
159 static int soc_camera_qbuf(struct file *file, void *priv,
160 struct v4l2_buffer *p)
162 struct soc_camera_file *icf = file->private_data;
164 WARN_ON(priv != file->private_data);
166 return videobuf_qbuf(&icf->vb_vidq, p);
169 static int soc_camera_dqbuf(struct file *file, void *priv,
170 struct v4l2_buffer *p)
172 struct soc_camera_file *icf = file->private_data;
174 WARN_ON(priv != file->private_data);
176 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
179 static int soc_camera_open(struct inode *inode, struct file *file)
181 struct video_device *vdev;
182 struct soc_camera_device *icd;
183 struct soc_camera_host *ici;
184 struct soc_camera_file *icf;
185 spinlock_t *lock;
186 int ret;
188 icf = vmalloc(sizeof(*icf));
189 if (!icf)
190 return -ENOMEM;
192 /* Protect against icd->remove() until we module_get() both drivers. */
193 mutex_lock(&video_lock);
195 vdev = video_devdata(file);
196 icd = container_of(vdev->dev, struct soc_camera_device, dev);
197 ici = to_soc_camera_host(icd->dev.parent);
199 if (!try_module_get(icd->ops->owner)) {
200 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
201 ret = -EINVAL;
202 goto emgd;
205 if (!try_module_get(ici->ops->owner)) {
206 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
207 ret = -EINVAL;
208 goto emgi;
211 icf->icd = icd;
213 icf->lock = ici->ops->spinlock_alloc(icf);
214 if (!icf->lock) {
215 ret = -ENOMEM;
216 goto esla;
219 icd->use_count++;
221 /* Now we really have to activate the camera */
222 if (icd->use_count == 1) {
223 ret = ici->ops->add(icd);
224 if (ret < 0) {
225 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
226 icd->use_count--;
227 goto eiciadd;
231 mutex_unlock(&video_lock);
233 file->private_data = icf;
234 dev_dbg(&icd->dev, "camera device open\n");
236 /* We must pass NULL as dev pointer, then all pci_* dma operations
237 * transform to normal dma_* ones. */
238 videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, icf->lock,
239 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
240 ici->msize, icd);
242 return 0;
244 /* All errors are entered with the video_lock held */
245 eiciadd:
246 lock = icf->lock;
247 icf->lock = NULL;
248 if (ici->ops->spinlock_free)
249 ici->ops->spinlock_free(lock);
250 esla:
251 module_put(ici->ops->owner);
252 emgi:
253 module_put(icd->ops->owner);
254 emgd:
255 mutex_unlock(&video_lock);
256 vfree(icf);
257 return ret;
260 static int soc_camera_close(struct inode *inode, struct file *file)
262 struct soc_camera_file *icf = file->private_data;
263 struct soc_camera_device *icd = icf->icd;
264 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
265 struct video_device *vdev = icd->vdev;
266 spinlock_t *lock = icf->lock;
268 mutex_lock(&video_lock);
269 icd->use_count--;
270 if (!icd->use_count)
271 ici->ops->remove(icd);
272 icf->lock = NULL;
273 if (ici->ops->spinlock_free)
274 ici->ops->spinlock_free(lock);
275 module_put(icd->ops->owner);
276 module_put(ici->ops->owner);
277 mutex_unlock(&video_lock);
279 vfree(icf);
281 dev_dbg(vdev->dev, "camera device close\n");
283 return 0;
286 static ssize_t soc_camera_read(struct file *file, char __user *buf,
287 size_t count, loff_t *ppos)
289 struct soc_camera_file *icf = file->private_data;
290 struct soc_camera_device *icd = icf->icd;
291 struct video_device *vdev = icd->vdev;
292 int err = -EINVAL;
294 dev_err(vdev->dev, "camera device read not implemented\n");
296 return err;
299 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
301 struct soc_camera_file *icf = file->private_data;
302 struct soc_camera_device *icd = icf->icd;
303 int err;
305 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
307 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
309 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
310 (unsigned long)vma->vm_start,
311 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
312 err);
314 return err;
317 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
319 struct soc_camera_file *icf = file->private_data;
320 struct soc_camera_device *icd = icf->icd;
321 struct soc_camera_host *ici =
322 to_soc_camera_host(icd->dev.parent);
324 if (list_empty(&icf->vb_vidq.stream)) {
325 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
326 return POLLERR;
329 return ici->ops->poll(file, pt);
333 static struct file_operations soc_camera_fops = {
334 .owner = THIS_MODULE,
335 .open = soc_camera_open,
336 .release = soc_camera_close,
337 .ioctl = video_ioctl2,
338 .read = soc_camera_read,
339 .mmap = soc_camera_mmap,
340 .poll = soc_camera_poll,
341 .llseek = no_llseek,
345 static int soc_camera_s_fmt_cap(struct file *file, void *priv,
346 struct v4l2_format *f)
348 struct soc_camera_file *icf = file->private_data;
349 struct soc_camera_device *icd = icf->icd;
350 struct soc_camera_host *ici =
351 to_soc_camera_host(icd->dev.parent);
352 int ret;
353 struct v4l2_rect rect;
354 const static struct soc_camera_data_format *data_fmt;
356 WARN_ON(priv != file->private_data);
358 data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
359 if (!data_fmt)
360 return -EINVAL;
362 /* buswidth may be further adjusted by the ici */
363 icd->buswidth = data_fmt->depth;
365 ret = soc_camera_try_fmt_cap(file, icf, f);
366 if (ret < 0)
367 return ret;
369 rect.left = icd->x_current;
370 rect.top = icd->y_current;
371 rect.width = f->fmt.pix.width;
372 rect.height = f->fmt.pix.height;
373 ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
374 if (ret < 0)
375 return ret;
377 icd->current_fmt = data_fmt;
378 icd->width = rect.width;
379 icd->height = rect.height;
380 icf->vb_vidq.field = f->fmt.pix.field;
381 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
382 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
383 f->type);
385 dev_dbg(&icd->dev, "set width: %d height: %d\n",
386 icd->width, icd->height);
388 /* set physical bus parameters */
389 return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
392 static int soc_camera_enum_fmt_cap(struct file *file, void *priv,
393 struct v4l2_fmtdesc *f)
395 struct soc_camera_file *icf = file->private_data;
396 struct soc_camera_device *icd = icf->icd;
397 const struct soc_camera_data_format *format;
399 WARN_ON(priv != file->private_data);
401 if (f->index >= icd->num_formats)
402 return -EINVAL;
404 format = &icd->formats[f->index];
406 strlcpy(f->description, format->name, sizeof(f->description));
407 f->pixelformat = format->fourcc;
408 return 0;
411 static int soc_camera_g_fmt_cap(struct file *file, void *priv,
412 struct v4l2_format *f)
414 struct soc_camera_file *icf = file->private_data;
415 struct soc_camera_device *icd = icf->icd;
417 WARN_ON(priv != file->private_data);
419 f->fmt.pix.width = icd->width;
420 f->fmt.pix.height = icd->height;
421 f->fmt.pix.field = icf->vb_vidq.field;
422 f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
423 f->fmt.pix.bytesperline =
424 (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
425 f->fmt.pix.sizeimage =
426 f->fmt.pix.height * f->fmt.pix.bytesperline;
427 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
428 icd->current_fmt->fourcc);
429 return 0;
432 static int soc_camera_querycap(struct file *file, void *priv,
433 struct v4l2_capability *cap)
435 struct soc_camera_file *icf = file->private_data;
436 struct soc_camera_device *icd = icf->icd;
437 struct soc_camera_host *ici =
438 to_soc_camera_host(icd->dev.parent);
440 WARN_ON(priv != file->private_data);
442 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
443 return ici->ops->querycap(ici, cap);
446 static int soc_camera_streamon(struct file *file, void *priv,
447 enum v4l2_buf_type i)
449 struct soc_camera_file *icf = file->private_data;
450 struct soc_camera_device *icd = icf->icd;
452 WARN_ON(priv != file->private_data);
454 dev_dbg(&icd->dev, "%s\n", __func__);
456 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
457 return -EINVAL;
459 icd->ops->start_capture(icd);
461 /* This calls buf_queue from host driver's videobuf_queue_ops */
462 return videobuf_streamon(&icf->vb_vidq);
465 static int soc_camera_streamoff(struct file *file, void *priv,
466 enum v4l2_buf_type i)
468 struct soc_camera_file *icf = file->private_data;
469 struct soc_camera_device *icd = icf->icd;
471 WARN_ON(priv != file->private_data);
473 dev_dbg(&icd->dev, "%s\n", __func__);
475 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
476 return -EINVAL;
478 /* This calls buf_release from host driver's videobuf_queue_ops for all
479 * remaining buffers. When the last buffer is freed, stop capture */
480 videobuf_streamoff(&icf->vb_vidq);
482 icd->ops->stop_capture(icd);
484 return 0;
487 static int soc_camera_queryctrl(struct file *file, void *priv,
488 struct v4l2_queryctrl *qc)
490 struct soc_camera_file *icf = file->private_data;
491 struct soc_camera_device *icd = icf->icd;
492 int i;
494 WARN_ON(priv != file->private_data);
496 if (!qc->id)
497 return -EINVAL;
499 for (i = 0; i < icd->ops->num_controls; i++)
500 if (qc->id == icd->ops->controls[i].id) {
501 memcpy(qc, &(icd->ops->controls[i]),
502 sizeof(*qc));
503 return 0;
506 return -EINVAL;
509 static int soc_camera_g_ctrl(struct file *file, void *priv,
510 struct v4l2_control *ctrl)
512 struct soc_camera_file *icf = file->private_data;
513 struct soc_camera_device *icd = icf->icd;
515 WARN_ON(priv != file->private_data);
517 switch (ctrl->id) {
518 case V4L2_CID_GAIN:
519 if (icd->gain == (unsigned short)~0)
520 return -EINVAL;
521 ctrl->value = icd->gain;
522 return 0;
523 case V4L2_CID_EXPOSURE:
524 if (icd->exposure == (unsigned short)~0)
525 return -EINVAL;
526 ctrl->value = icd->exposure;
527 return 0;
530 if (icd->ops->get_control)
531 return icd->ops->get_control(icd, ctrl);
532 return -EINVAL;
535 static int soc_camera_s_ctrl(struct file *file, void *priv,
536 struct v4l2_control *ctrl)
538 struct soc_camera_file *icf = file->private_data;
539 struct soc_camera_device *icd = icf->icd;
541 WARN_ON(priv != file->private_data);
543 if (icd->ops->set_control)
544 return icd->ops->set_control(icd, ctrl);
545 return -EINVAL;
548 static int soc_camera_cropcap(struct file *file, void *fh,
549 struct v4l2_cropcap *a)
551 struct soc_camera_file *icf = file->private_data;
552 struct soc_camera_device *icd = icf->icd;
554 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
555 a->bounds.left = icd->x_min;
556 a->bounds.top = icd->y_min;
557 a->bounds.width = icd->width_max;
558 a->bounds.height = icd->height_max;
559 a->defrect.left = icd->x_min;
560 a->defrect.top = icd->y_min;
561 a->defrect.width = 640;
562 a->defrect.height = 480;
563 a->pixelaspect.numerator = 1;
564 a->pixelaspect.denominator = 1;
566 return 0;
569 static int soc_camera_g_crop(struct file *file, void *fh,
570 struct v4l2_crop *a)
572 struct soc_camera_file *icf = file->private_data;
573 struct soc_camera_device *icd = icf->icd;
575 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
576 a->c.left = icd->x_current;
577 a->c.top = icd->y_current;
578 a->c.width = icd->width;
579 a->c.height = icd->height;
581 return 0;
584 static int soc_camera_s_crop(struct file *file, void *fh,
585 struct v4l2_crop *a)
587 struct soc_camera_file *icf = file->private_data;
588 struct soc_camera_device *icd = icf->icd;
589 struct soc_camera_host *ici =
590 to_soc_camera_host(icd->dev.parent);
591 int ret;
593 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
594 return -EINVAL;
596 ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
597 if (!ret) {
598 icd->width = a->c.width;
599 icd->height = a->c.height;
600 icd->x_current = a->c.left;
601 icd->y_current = a->c.top;
604 return ret;
607 static int soc_camera_g_chip_ident(struct file *file, void *fh,
608 struct v4l2_chip_ident *id)
610 struct soc_camera_file *icf = file->private_data;
611 struct soc_camera_device *icd = icf->icd;
613 if (!icd->ops->get_chip_id)
614 return -EINVAL;
616 return icd->ops->get_chip_id(icd, id);
619 #ifdef CONFIG_VIDEO_ADV_DEBUG
620 static int soc_camera_g_register(struct file *file, void *fh,
621 struct v4l2_register *reg)
623 struct soc_camera_file *icf = file->private_data;
624 struct soc_camera_device *icd = icf->icd;
626 if (!icd->ops->get_register)
627 return -EINVAL;
629 return icd->ops->get_register(icd, reg);
632 static int soc_camera_s_register(struct file *file, void *fh,
633 struct v4l2_register *reg)
635 struct soc_camera_file *icf = file->private_data;
636 struct soc_camera_device *icd = icf->icd;
638 if (!icd->ops->set_register)
639 return -EINVAL;
641 return icd->ops->set_register(icd, reg);
643 #endif
645 static int device_register_link(struct soc_camera_device *icd)
647 int ret = device_register(&icd->dev);
649 if (ret < 0) {
650 /* Prevent calling device_unregister() */
651 icd->dev.parent = NULL;
652 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
653 /* Even if probe() was unsuccessful for all registered drivers,
654 * device_register() returns 0, and we add the link, just to
655 * document this camera's control device */
656 } else if (icd->control)
657 /* Have to sysfs_remove_link() before device_unregister()? */
658 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
659 "control"))
660 dev_warn(&icd->dev,
661 "Failed creating the control symlink\n");
662 return ret;
665 /* So far this function cannot fail */
666 static void scan_add_host(struct soc_camera_host *ici)
668 struct soc_camera_device *icd;
670 mutex_lock(&list_lock);
672 list_for_each_entry(icd, &devices, list) {
673 if (icd->iface == ici->nr) {
674 icd->dev.parent = &ici->dev;
675 device_register_link(icd);
679 mutex_unlock(&list_lock);
682 /* return: 0 if no match found or a match found and
683 * device_register() successful, error code otherwise */
684 static int scan_add_device(struct soc_camera_device *icd)
686 struct soc_camera_host *ici;
687 int ret = 0;
689 mutex_lock(&list_lock);
691 list_add_tail(&icd->list, &devices);
693 /* Watch out for class_for_each_device / class_find_device API by
694 * Dave Young <hidave.darkstar@gmail.com> */
695 list_for_each_entry(ici, &hosts, list) {
696 if (icd->iface == ici->nr) {
697 ret = 1;
698 icd->dev.parent = &ici->dev;
699 break;
703 mutex_unlock(&list_lock);
705 if (ret)
706 ret = device_register_link(icd);
708 return ret;
711 static int soc_camera_probe(struct device *dev)
713 struct soc_camera_device *icd = to_soc_camera_dev(dev);
714 struct soc_camera_host *ici =
715 to_soc_camera_host(icd->dev.parent);
716 int ret;
718 if (!icd->ops->probe)
719 return -ENODEV;
721 /* We only call ->add() here to activate and probe the camera.
722 * We shall ->remove() and deactivate it immediately afterwards. */
723 ret = ici->ops->add(icd);
724 if (ret < 0)
725 return ret;
727 ret = icd->ops->probe(icd);
728 if (ret >= 0) {
729 const struct v4l2_queryctrl *qctrl;
731 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
732 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
733 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
734 icd->exposure = qctrl ? qctrl->default_value :
735 (unsigned short)~0;
737 ici->ops->remove(icd);
739 return ret;
742 /* This is called on device_unregister, which only means we have to disconnect
743 * from the host, but not remove ourselves from the device list */
744 static int soc_camera_remove(struct device *dev)
746 struct soc_camera_device *icd = to_soc_camera_dev(dev);
748 if (icd->ops->remove)
749 icd->ops->remove(icd);
751 return 0;
754 static struct bus_type soc_camera_bus_type = {
755 .name = "soc-camera",
756 .probe = soc_camera_probe,
757 .remove = soc_camera_remove,
760 static struct device_driver ic_drv = {
761 .name = "camera",
762 .bus = &soc_camera_bus_type,
763 .owner = THIS_MODULE,
767 * Image capture host - this is a host device, not a bus device, so,
768 * no bus reference, no probing.
770 static struct class soc_camera_host_class = {
771 .owner = THIS_MODULE,
772 .name = "camera_host",
775 static void dummy_release(struct device *dev)
779 static spinlock_t *spinlock_alloc(struct soc_camera_file *icf)
781 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
783 if (lock)
784 spin_lock_init(lock);
786 return lock;
789 static void spinlock_free(spinlock_t *lock)
791 kfree(lock);
794 int soc_camera_host_register(struct soc_camera_host *ici)
796 int ret;
797 struct soc_camera_host *ix;
799 if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove)
800 return -EINVAL;
802 /* Number might be equal to the platform device ID */
803 sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
804 ici->dev.class = &soc_camera_host_class;
806 mutex_lock(&list_lock);
807 list_for_each_entry(ix, &hosts, list) {
808 if (ix->nr == ici->nr) {
809 mutex_unlock(&list_lock);
810 return -EBUSY;
814 list_add_tail(&ici->list, &hosts);
815 mutex_unlock(&list_lock);
817 ici->dev.release = dummy_release;
819 ret = device_register(&ici->dev);
821 if (ret)
822 goto edevr;
824 if (!ici->ops->spinlock_alloc) {
825 ici->ops->spinlock_alloc = spinlock_alloc;
826 ici->ops->spinlock_free = spinlock_free;
829 scan_add_host(ici);
831 return 0;
833 edevr:
834 mutex_lock(&list_lock);
835 list_del(&ici->list);
836 mutex_unlock(&list_lock);
838 return ret;
840 EXPORT_SYMBOL(soc_camera_host_register);
842 /* Unregister all clients! */
843 void soc_camera_host_unregister(struct soc_camera_host *ici)
845 struct soc_camera_device *icd;
847 mutex_lock(&list_lock);
849 list_del(&ici->list);
851 list_for_each_entry(icd, &devices, list) {
852 if (icd->dev.parent == &ici->dev) {
853 device_unregister(&icd->dev);
854 /* Not before device_unregister(), .remove
855 * needs parent to call ici->ops->remove() */
856 icd->dev.parent = NULL;
857 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
861 mutex_unlock(&list_lock);
863 device_unregister(&ici->dev);
865 EXPORT_SYMBOL(soc_camera_host_unregister);
867 /* Image capture device */
868 int soc_camera_device_register(struct soc_camera_device *icd)
870 struct soc_camera_device *ix;
871 int num = -1, i;
873 if (!icd)
874 return -EINVAL;
876 for (i = 0; i < 256 && num < 0; i++) {
877 num = i;
878 list_for_each_entry(ix, &devices, list) {
879 if (ix->iface == icd->iface && ix->devnum == i) {
880 num = -1;
881 break;
886 if (num < 0)
887 /* ok, we have 256 cameras on this host...
888 * man, stay reasonable... */
889 return -ENOMEM;
891 icd->devnum = num;
892 icd->dev.bus = &soc_camera_bus_type;
893 snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
894 "%u-%u", icd->iface, icd->devnum);
896 icd->dev.release = dummy_release;
898 return scan_add_device(icd);
900 EXPORT_SYMBOL(soc_camera_device_register);
902 void soc_camera_device_unregister(struct soc_camera_device *icd)
904 mutex_lock(&list_lock);
905 list_del(&icd->list);
907 /* The bus->remove will be eventually called */
908 if (icd->dev.parent)
909 device_unregister(&icd->dev);
910 mutex_unlock(&list_lock);
912 EXPORT_SYMBOL(soc_camera_device_unregister);
914 int soc_camera_video_start(struct soc_camera_device *icd)
916 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
917 int err = -ENOMEM;
918 struct video_device *vdev;
920 if (!icd->dev.parent)
921 return -ENODEV;
923 vdev = video_device_alloc();
924 if (!vdev)
925 goto evidallocd;
926 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
928 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
929 /* Maybe better &ici->dev */
930 vdev->dev = &icd->dev;
931 vdev->type = VID_TYPE_CAPTURE;
932 vdev->current_norm = V4L2_STD_UNKNOWN;
933 vdev->fops = &soc_camera_fops;
934 vdev->release = video_device_release;
935 vdev->minor = -1;
936 vdev->tvnorms = V4L2_STD_UNKNOWN,
937 vdev->vidioc_querycap = soc_camera_querycap;
938 vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap;
939 vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap;
940 vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap;
941 vdev->vidioc_enum_input = soc_camera_enum_input;
942 vdev->vidioc_g_input = soc_camera_g_input;
943 vdev->vidioc_s_input = soc_camera_s_input;
944 vdev->vidioc_s_std = soc_camera_s_std;
945 vdev->vidioc_reqbufs = soc_camera_reqbufs;
946 vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap;
947 vdev->vidioc_querybuf = soc_camera_querybuf;
948 vdev->vidioc_qbuf = soc_camera_qbuf;
949 vdev->vidioc_dqbuf = soc_camera_dqbuf;
950 vdev->vidioc_streamon = soc_camera_streamon;
951 vdev->vidioc_streamoff = soc_camera_streamoff;
952 vdev->vidioc_queryctrl = soc_camera_queryctrl;
953 vdev->vidioc_g_ctrl = soc_camera_g_ctrl;
954 vdev->vidioc_s_ctrl = soc_camera_s_ctrl;
955 vdev->vidioc_cropcap = soc_camera_cropcap;
956 vdev->vidioc_g_crop = soc_camera_g_crop;
957 vdev->vidioc_s_crop = soc_camera_s_crop;
958 vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident;
959 #ifdef CONFIG_VIDEO_ADV_DEBUG
960 vdev->vidioc_g_register = soc_camera_g_register;
961 vdev->vidioc_s_register = soc_camera_s_register;
962 #endif
964 icd->current_fmt = &icd->formats[0];
966 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
967 if (err < 0) {
968 dev_err(vdev->dev, "video_register_device failed\n");
969 goto evidregd;
971 icd->vdev = vdev;
973 return 0;
975 evidregd:
976 video_device_release(vdev);
977 evidallocd:
978 return err;
980 EXPORT_SYMBOL(soc_camera_video_start);
982 void soc_camera_video_stop(struct soc_camera_device *icd)
984 struct video_device *vdev = icd->vdev;
986 dev_dbg(&icd->dev, "%s\n", __func__);
988 if (!icd->dev.parent || !vdev)
989 return;
991 mutex_lock(&video_lock);
992 video_unregister_device(vdev);
993 icd->vdev = NULL;
994 mutex_unlock(&video_lock);
996 EXPORT_SYMBOL(soc_camera_video_stop);
998 static int __init soc_camera_init(void)
1000 int ret = bus_register(&soc_camera_bus_type);
1001 if (ret)
1002 return ret;
1003 ret = driver_register(&ic_drv);
1004 if (ret)
1005 goto edrvr;
1006 ret = class_register(&soc_camera_host_class);
1007 if (ret)
1008 goto eclr;
1010 return 0;
1012 eclr:
1013 driver_unregister(&ic_drv);
1014 edrvr:
1015 bus_unregister(&soc_camera_bus_type);
1016 return ret;
1019 static void __exit soc_camera_exit(void)
1021 class_unregister(&soc_camera_host_class);
1022 driver_unregister(&ic_drv);
1023 bus_unregister(&soc_camera_bus_type);
1026 module_init(soc_camera_init);
1027 module_exit(soc_camera_exit);
1029 MODULE_DESCRIPTION("Image capture bus driver");
1030 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1031 MODULE_LICENSE("GPL");