V4L/DVB (7369): drivers/media/video/soc_camera.c: reads return size_t
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / soc_camera.c
blob91e1ab36fe5d110c44a18080bbdc31437e889e1e
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->ops->num_formats; i++)
42 if (icd->ops->formats[i].fourcc == fourcc)
43 return icd->ops->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->try_bus_param(icd, f->fmt.pix.pixelformat);
80 if (ret)
81 return ret;
83 /* limit format to hardware capabilities */
84 ret = ici->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", __FUNCTION__, p->memory);
142 ret = videobuf_reqbufs(&icf->vb_vidq, p);
143 if (ret < 0)
144 return ret;
146 return ici->reqbufs(icf, p);
148 return ret;
151 static int soc_camera_querybuf(struct file *file, void *priv,
152 struct v4l2_buffer *p)
154 struct soc_camera_file *icf = file->private_data;
156 WARN_ON(priv != file->private_data);
158 return videobuf_querybuf(&icf->vb_vidq, p);
161 static int soc_camera_qbuf(struct file *file, void *priv,
162 struct v4l2_buffer *p)
164 struct soc_camera_file *icf = file->private_data;
166 WARN_ON(priv != file->private_data);
168 return videobuf_qbuf(&icf->vb_vidq, p);
171 static int soc_camera_dqbuf(struct file *file, void *priv,
172 struct v4l2_buffer *p)
174 struct soc_camera_file *icf = file->private_data;
176 WARN_ON(priv != file->private_data);
178 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
181 static int soc_camera_open(struct inode *inode, struct file *file)
183 struct video_device *vdev;
184 struct soc_camera_device *icd;
185 struct soc_camera_host *ici;
186 struct soc_camera_file *icf;
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->owner)) {
207 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
208 ret = -EINVAL;
209 goto emgi;
212 icd->use_count++;
214 icf->icd = icd;
216 /* Now we really have to activate the camera */
217 if (icd->use_count == 1) {
218 ret = ici->add(icd);
219 if (ret < 0) {
220 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
221 icd->use_count--;
222 goto eiciadd;
226 mutex_unlock(&video_lock);
228 file->private_data = icf;
229 dev_dbg(&icd->dev, "camera device open\n");
231 /* We must pass NULL as dev pointer, then all pci_* dma operations
232 * transform to normal dma_* ones. Do we need an irqlock? */
233 videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, NULL,
234 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
235 ici->msize, icd);
237 return 0;
239 /* All errors are entered with the video_lock held */
240 eiciadd:
241 module_put(ici->owner);
242 emgi:
243 module_put(icd->ops->owner);
244 emgd:
245 mutex_unlock(&video_lock);
246 vfree(icf);
247 return ret;
250 static int soc_camera_close(struct inode *inode, struct file *file)
252 struct soc_camera_file *icf = file->private_data;
253 struct soc_camera_device *icd = icf->icd;
254 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
255 struct video_device *vdev = icd->vdev;
257 mutex_lock(&video_lock);
258 icd->use_count--;
259 if (!icd->use_count)
260 ici->remove(icd);
261 module_put(icd->ops->owner);
262 module_put(ici->owner);
263 mutex_unlock(&video_lock);
265 vfree(file->private_data);
267 dev_dbg(vdev->dev, "camera device close\n");
269 return 0;
272 static ssize_t soc_camera_read(struct file *file, char __user *buf,
273 size_t count, loff_t *ppos)
275 struct soc_camera_file *icf = file->private_data;
276 struct soc_camera_device *icd = icf->icd;
277 struct video_device *vdev = icd->vdev;
278 int err = -EINVAL;
280 dev_err(vdev->dev, "camera device read not implemented\n");
282 return err;
285 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
287 struct soc_camera_file *icf = file->private_data;
288 struct soc_camera_device *icd = icf->icd;
289 int err;
291 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
293 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
295 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
296 (unsigned long)vma->vm_start,
297 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
298 err);
300 return err;
303 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
305 struct soc_camera_file *icf = file->private_data;
306 struct soc_camera_device *icd = icf->icd;
307 struct soc_camera_host *ici =
308 to_soc_camera_host(icd->dev.parent);
310 if (list_empty(&icf->vb_vidq.stream)) {
311 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
312 return POLLERR;
315 return ici->poll(file, pt);
319 static struct file_operations soc_camera_fops = {
320 .owner = THIS_MODULE,
321 .open = soc_camera_open,
322 .release = soc_camera_close,
323 .ioctl = video_ioctl2,
324 .read = soc_camera_read,
325 .mmap = soc_camera_mmap,
326 .poll = soc_camera_poll,
327 .llseek = no_llseek,
331 static int soc_camera_s_fmt_cap(struct file *file, void *priv,
332 struct v4l2_format *f)
334 struct soc_camera_file *icf = file->private_data;
335 struct soc_camera_device *icd = icf->icd;
336 struct soc_camera_host *ici =
337 to_soc_camera_host(icd->dev.parent);
338 int ret;
339 struct v4l2_rect rect;
340 const static struct soc_camera_data_format *data_fmt;
342 WARN_ON(priv != file->private_data);
344 data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
345 if (!data_fmt)
346 return -EINVAL;
348 /* buswidth may be further adjusted by the ici */
349 icd->buswidth = data_fmt->depth;
351 ret = soc_camera_try_fmt_cap(file, icf, f);
352 if (ret < 0)
353 return ret;
355 rect.left = icd->x_current;
356 rect.top = icd->y_current;
357 rect.width = f->fmt.pix.width;
358 rect.height = f->fmt.pix.height;
359 ret = ici->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
360 if (ret < 0)
361 return ret;
363 icd->current_fmt = data_fmt;
364 icd->width = rect.width;
365 icd->height = rect.height;
366 icf->vb_vidq.field = f->fmt.pix.field;
367 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
368 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
369 f->type);
371 dev_dbg(&icd->dev, "set width: %d height: %d\n",
372 icd->width, icd->height);
374 /* set physical bus parameters */
375 return ici->set_bus_param(icd, f->fmt.pix.pixelformat);
378 static int soc_camera_enum_fmt_cap(struct file *file, void *priv,
379 struct v4l2_fmtdesc *f)
381 struct soc_camera_file *icf = file->private_data;
382 struct soc_camera_device *icd = icf->icd;
383 const struct soc_camera_data_format *format;
385 WARN_ON(priv != file->private_data);
387 if (f->index >= icd->ops->num_formats)
388 return -EINVAL;
390 format = &icd->ops->formats[f->index];
392 strlcpy(f->description, format->name, sizeof(f->description));
393 f->pixelformat = format->fourcc;
394 return 0;
397 static int soc_camera_g_fmt_cap(struct file *file, void *priv,
398 struct v4l2_format *f)
400 struct soc_camera_file *icf = file->private_data;
401 struct soc_camera_device *icd = icf->icd;
403 WARN_ON(priv != file->private_data);
405 f->fmt.pix.width = icd->width;
406 f->fmt.pix.height = icd->height;
407 f->fmt.pix.field = icf->vb_vidq.field;
408 f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
409 f->fmt.pix.bytesperline =
410 (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
411 f->fmt.pix.sizeimage =
412 f->fmt.pix.height * f->fmt.pix.bytesperline;
413 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
414 icd->current_fmt->fourcc);
415 return 0;
418 static int soc_camera_querycap(struct file *file, void *priv,
419 struct v4l2_capability *cap)
421 struct soc_camera_file *icf = file->private_data;
422 struct soc_camera_device *icd = icf->icd;
423 struct soc_camera_host *ici =
424 to_soc_camera_host(icd->dev.parent);
426 WARN_ON(priv != file->private_data);
428 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
429 return ici->querycap(ici, cap);
432 static int soc_camera_streamon(struct file *file, void *priv,
433 enum v4l2_buf_type i)
435 struct soc_camera_file *icf = file->private_data;
436 struct soc_camera_device *icd = icf->icd;
438 WARN_ON(priv != file->private_data);
440 dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
442 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
443 return -EINVAL;
445 icd->ops->start_capture(icd);
447 /* This calls buf_queue from host driver's videobuf_queue_ops */
448 return videobuf_streamon(&icf->vb_vidq);
451 static int soc_camera_streamoff(struct file *file, void *priv,
452 enum v4l2_buf_type i)
454 struct soc_camera_file *icf = file->private_data;
455 struct soc_camera_device *icd = icf->icd;
457 WARN_ON(priv != file->private_data);
459 dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
461 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
462 return -EINVAL;
464 /* This calls buf_release from host driver's videobuf_queue_ops for all
465 * remaining buffers. When the last buffer is freed, stop capture */
466 videobuf_streamoff(&icf->vb_vidq);
468 icd->ops->stop_capture(icd);
470 return 0;
473 static int soc_camera_queryctrl(struct file *file, void *priv,
474 struct v4l2_queryctrl *qc)
476 struct soc_camera_file *icf = file->private_data;
477 struct soc_camera_device *icd = icf->icd;
478 int i;
480 WARN_ON(priv != file->private_data);
482 if (!qc->id)
483 return -EINVAL;
485 for (i = 0; i < icd->ops->num_controls; i++)
486 if (qc->id == icd->ops->controls[i].id) {
487 memcpy(qc, &(icd->ops->controls[i]),
488 sizeof(*qc));
489 return 0;
492 return -EINVAL;
495 static int soc_camera_g_ctrl(struct file *file, void *priv,
496 struct v4l2_control *ctrl)
498 struct soc_camera_file *icf = file->private_data;
499 struct soc_camera_device *icd = icf->icd;
501 WARN_ON(priv != file->private_data);
503 switch (ctrl->id) {
504 case V4L2_CID_GAIN:
505 if (icd->gain == (unsigned short)~0)
506 return -EINVAL;
507 ctrl->value = icd->gain;
508 return 0;
509 case V4L2_CID_EXPOSURE:
510 if (icd->exposure == (unsigned short)~0)
511 return -EINVAL;
512 ctrl->value = icd->exposure;
513 return 0;
516 if (icd->ops->get_control)
517 return icd->ops->get_control(icd, ctrl);
518 return -EINVAL;
521 static int soc_camera_s_ctrl(struct file *file, void *priv,
522 struct v4l2_control *ctrl)
524 struct soc_camera_file *icf = file->private_data;
525 struct soc_camera_device *icd = icf->icd;
527 WARN_ON(priv != file->private_data);
529 if (icd->ops->set_control)
530 return icd->ops->set_control(icd, ctrl);
531 return -EINVAL;
534 static int soc_camera_cropcap(struct file *file, void *fh,
535 struct v4l2_cropcap *a)
537 struct soc_camera_file *icf = file->private_data;
538 struct soc_camera_device *icd = icf->icd;
540 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
541 a->bounds.left = icd->x_min;
542 a->bounds.top = icd->y_min;
543 a->bounds.width = icd->width_max;
544 a->bounds.height = icd->height_max;
545 a->defrect.left = icd->x_min;
546 a->defrect.top = icd->y_min;
547 a->defrect.width = 640;
548 a->defrect.height = 480;
549 a->pixelaspect.numerator = 1;
550 a->pixelaspect.denominator = 1;
552 return 0;
555 static int soc_camera_g_crop(struct file *file, void *fh,
556 struct v4l2_crop *a)
558 struct soc_camera_file *icf = file->private_data;
559 struct soc_camera_device *icd = icf->icd;
561 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
562 a->c.left = icd->x_current;
563 a->c.top = icd->y_current;
564 a->c.width = icd->width;
565 a->c.height = icd->height;
567 return 0;
570 static int soc_camera_s_crop(struct file *file, void *fh,
571 struct v4l2_crop *a)
573 struct soc_camera_file *icf = file->private_data;
574 struct soc_camera_device *icd = icf->icd;
575 struct soc_camera_host *ici =
576 to_soc_camera_host(icd->dev.parent);
577 int ret;
579 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
580 return -EINVAL;
582 ret = ici->set_fmt_cap(icd, 0, &a->c);
583 if (!ret) {
584 icd->width = a->c.width;
585 icd->height = a->c.height;
586 icd->x_current = a->c.left;
587 icd->y_current = a->c.top;
590 return ret;
593 static int soc_camera_g_chip_ident(struct file *file, void *fh,
594 struct v4l2_chip_ident *id)
596 struct soc_camera_file *icf = file->private_data;
597 struct soc_camera_device *icd = icf->icd;
599 if (!icd->ops->get_chip_id)
600 return -EINVAL;
602 return icd->ops->get_chip_id(icd, id);
605 #ifdef CONFIG_VIDEO_ADV_DEBUG
606 static int soc_camera_g_register(struct file *file, void *fh,
607 struct v4l2_register *reg)
609 struct soc_camera_file *icf = file->private_data;
610 struct soc_camera_device *icd = icf->icd;
612 if (!icd->ops->get_register)
613 return -EINVAL;
615 return icd->ops->get_register(icd, reg);
618 static int soc_camera_s_register(struct file *file, void *fh,
619 struct v4l2_register *reg)
621 struct soc_camera_file *icf = file->private_data;
622 struct soc_camera_device *icd = icf->icd;
624 if (!icd->ops->set_register)
625 return -EINVAL;
627 return icd->ops->set_register(icd, reg);
629 #endif
631 static int device_register_link(struct soc_camera_device *icd)
633 int ret = device_register(&icd->dev);
635 if (ret < 0) {
636 /* Prevent calling device_unregister() */
637 icd->dev.parent = NULL;
638 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
639 /* Even if probe() was unsuccessful for all registered drivers,
640 * device_register() returns 0, and we add the link, just to
641 * document this camera's control device */
642 } else if (icd->control)
643 /* Have to sysfs_remove_link() before device_unregister()? */
644 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
645 "control"))
646 dev_warn(&icd->dev,
647 "Failed creating the control symlink\n");
648 return ret;
651 /* So far this function cannot fail */
652 static void scan_add_host(struct soc_camera_host *ici)
654 struct soc_camera_device *icd;
656 mutex_lock(&list_lock);
658 list_for_each_entry(icd, &devices, list) {
659 if (icd->iface == ici->nr) {
660 icd->dev.parent = &ici->dev;
661 device_register_link(icd);
665 mutex_unlock(&list_lock);
668 /* return: 0 if no match found or a match found and
669 * device_register() successful, error code otherwise */
670 static int scan_add_device(struct soc_camera_device *icd)
672 struct soc_camera_host *ici;
673 int ret = 0;
675 mutex_lock(&list_lock);
677 list_add_tail(&icd->list, &devices);
679 /* Watch out for class_for_each_device / class_find_device API by
680 * Dave Young <hidave.darkstar@gmail.com> */
681 list_for_each_entry(ici, &hosts, list) {
682 if (icd->iface == ici->nr) {
683 ret = 1;
684 icd->dev.parent = &ici->dev;
685 break;
689 mutex_unlock(&list_lock);
691 if (ret)
692 ret = device_register_link(icd);
694 return ret;
697 static int soc_camera_probe(struct device *dev)
699 struct soc_camera_device *icd = to_soc_camera_dev(dev);
700 struct soc_camera_host *ici =
701 to_soc_camera_host(icd->dev.parent);
702 int ret;
704 if (!icd->probe)
705 return -ENODEV;
707 /* We only call ->add() here to activate and probe the camera.
708 * We shall ->remove() and deactivate it immediately afterwards. */
709 ret = ici->add(icd);
710 if (ret < 0)
711 return ret;
713 ret = icd->probe(icd);
714 if (ret >= 0) {
715 const struct v4l2_queryctrl *qctrl;
717 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
718 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
719 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
720 icd->exposure = qctrl ? qctrl->default_value :
721 (unsigned short)~0;
723 ici->remove(icd);
725 return ret;
728 /* This is called on device_unregister, which only means we have to disconnect
729 * from the host, but not remove ourselves from the device list */
730 static int soc_camera_remove(struct device *dev)
732 struct soc_camera_device *icd = to_soc_camera_dev(dev);
734 if (icd->remove)
735 icd->remove(icd);
737 return 0;
740 static struct bus_type soc_camera_bus_type = {
741 .name = "soc-camera",
742 .probe = soc_camera_probe,
743 .remove = soc_camera_remove,
746 static struct device_driver ic_drv = {
747 .name = "camera",
748 .bus = &soc_camera_bus_type,
749 .owner = THIS_MODULE,
753 * Image capture host - this is a host device, not a bus device, so,
754 * no bus reference, no probing.
756 static struct class soc_camera_host_class = {
757 .owner = THIS_MODULE,
758 .name = "camera_host",
761 static void dummy_release(struct device *dev)
765 int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner)
767 int ret;
768 struct soc_camera_host *ix;
770 if (!ici->vbq_ops || !ici->add || !ici->remove || !owner)
771 return -EINVAL;
773 /* Number might be equal to the platform device ID */
774 sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
775 ici->dev.class = &soc_camera_host_class;
777 mutex_lock(&list_lock);
778 list_for_each_entry(ix, &hosts, list) {
779 if (ix->nr == ici->nr) {
780 mutex_unlock(&list_lock);
781 return -EBUSY;
785 list_add_tail(&ici->list, &hosts);
786 mutex_unlock(&list_lock);
788 ici->owner = owner;
789 ici->dev.release = dummy_release;
791 ret = device_register(&ici->dev);
793 if (ret)
794 goto edevr;
796 scan_add_host(ici);
798 return 0;
800 edevr:
801 mutex_lock(&list_lock);
802 list_del(&ici->list);
803 mutex_unlock(&list_lock);
805 return ret;
807 EXPORT_SYMBOL(soc_camera_host_register);
809 /* Unregister all clients! */
810 void soc_camera_host_unregister(struct soc_camera_host *ici)
812 struct soc_camera_device *icd;
814 mutex_lock(&list_lock);
816 list_del(&ici->list);
818 list_for_each_entry(icd, &devices, list) {
819 if (icd->dev.parent == &ici->dev) {
820 device_unregister(&icd->dev);
821 /* Not before device_unregister(), .remove
822 * needs parent to call ici->remove() */
823 icd->dev.parent = NULL;
824 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
828 mutex_unlock(&list_lock);
830 device_unregister(&ici->dev);
832 EXPORT_SYMBOL(soc_camera_host_unregister);
834 /* Image capture device */
835 int soc_camera_device_register(struct soc_camera_device *icd)
837 struct soc_camera_device *ix;
838 int num = -1, i;
840 if (!icd)
841 return -EINVAL;
843 for (i = 0; i < 256 && num < 0; i++) {
844 num = i;
845 list_for_each_entry(ix, &devices, list) {
846 if (ix->iface == icd->iface && ix->devnum == i) {
847 num = -1;
848 break;
853 if (num < 0)
854 /* ok, we have 256 cameras on this host...
855 * man, stay reasonable... */
856 return -ENOMEM;
858 icd->devnum = num;
859 icd->dev.bus = &soc_camera_bus_type;
860 snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
861 "%u-%u", icd->iface, icd->devnum);
863 icd->dev.release = dummy_release;
865 return scan_add_device(icd);
867 EXPORT_SYMBOL(soc_camera_device_register);
869 void soc_camera_device_unregister(struct soc_camera_device *icd)
871 mutex_lock(&list_lock);
872 list_del(&icd->list);
874 /* The bus->remove will be eventually called */
875 if (icd->dev.parent)
876 device_unregister(&icd->dev);
877 mutex_unlock(&list_lock);
879 EXPORT_SYMBOL(soc_camera_device_unregister);
881 int soc_camera_video_start(struct soc_camera_device *icd)
883 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
884 int err = -ENOMEM;
885 struct video_device *vdev;
887 if (!icd->dev.parent)
888 return -ENODEV;
890 vdev = video_device_alloc();
891 if (!vdev)
892 goto evidallocd;
893 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
895 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
896 /* Maybe better &ici->dev */
897 vdev->dev = &icd->dev;
898 vdev->type = VID_TYPE_CAPTURE;
899 vdev->current_norm = V4L2_STD_UNKNOWN;
900 vdev->fops = &soc_camera_fops;
901 vdev->release = video_device_release;
902 vdev->minor = -1;
903 vdev->tvnorms = V4L2_STD_UNKNOWN,
904 vdev->vidioc_querycap = soc_camera_querycap;
905 vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap;
906 vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap;
907 vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap;
908 vdev->vidioc_enum_input = soc_camera_enum_input;
909 vdev->vidioc_g_input = soc_camera_g_input;
910 vdev->vidioc_s_input = soc_camera_s_input;
911 vdev->vidioc_s_std = soc_camera_s_std;
912 vdev->vidioc_reqbufs = soc_camera_reqbufs;
913 vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap;
914 vdev->vidioc_querybuf = soc_camera_querybuf;
915 vdev->vidioc_qbuf = soc_camera_qbuf;
916 vdev->vidioc_dqbuf = soc_camera_dqbuf;
917 vdev->vidioc_streamon = soc_camera_streamon;
918 vdev->vidioc_streamoff = soc_camera_streamoff;
919 vdev->vidioc_queryctrl = soc_camera_queryctrl;
920 vdev->vidioc_g_ctrl = soc_camera_g_ctrl;
921 vdev->vidioc_s_ctrl = soc_camera_s_ctrl;
922 vdev->vidioc_cropcap = soc_camera_cropcap;
923 vdev->vidioc_g_crop = soc_camera_g_crop;
924 vdev->vidioc_s_crop = soc_camera_s_crop;
925 vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident;
926 #ifdef CONFIG_VIDEO_ADV_DEBUG
927 vdev->vidioc_g_register = soc_camera_g_register;
928 vdev->vidioc_s_register = soc_camera_s_register;
929 #endif
931 icd->current_fmt = &icd->ops->formats[0];
933 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
934 if (err < 0) {
935 dev_err(vdev->dev, "video_register_device failed\n");
936 goto evidregd;
938 icd->vdev = vdev;
940 return 0;
942 evidregd:
943 video_device_release(vdev);
944 evidallocd:
945 return err;
947 EXPORT_SYMBOL(soc_camera_video_start);
949 void soc_camera_video_stop(struct soc_camera_device *icd)
951 struct video_device *vdev = icd->vdev;
953 dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
955 if (!icd->dev.parent || !vdev)
956 return;
958 mutex_lock(&video_lock);
959 video_unregister_device(vdev);
960 icd->vdev = NULL;
961 mutex_unlock(&video_lock);
963 EXPORT_SYMBOL(soc_camera_video_stop);
965 static int __init soc_camera_init(void)
967 int ret = bus_register(&soc_camera_bus_type);
968 if (ret)
969 return ret;
970 ret = driver_register(&ic_drv);
971 if (ret)
972 goto edrvr;
973 ret = class_register(&soc_camera_host_class);
974 if (ret)
975 goto eclr;
977 return 0;
979 eclr:
980 driver_unregister(&ic_drv);
981 edrvr:
982 bus_unregister(&soc_camera_bus_type);
983 return ret;
986 static void __exit soc_camera_exit(void)
988 class_unregister(&soc_camera_host_class);
989 driver_unregister(&ic_drv);
990 bus_unregister(&soc_camera_bus_type);
993 module_init(soc_camera_init);
994 module_exit(soc_camera_exit);
996 MODULE_DESCRIPTION("Image capture bus driver");
997 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
998 MODULE_LICENSE("GPL");