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 /* Default to VGA resolution */
34 #define DEFAULT_WIDTH 640
35 #define DEFAULT_HEIGHT 480
37 static LIST_HEAD(hosts
);
38 static LIST_HEAD(devices
);
39 static DEFINE_MUTEX(list_lock
);
41 const struct soc_camera_data_format
*soc_camera_format_by_fourcc(
42 struct soc_camera_device
*icd
, unsigned int fourcc
)
46 for (i
= 0; i
< icd
->num_formats
; i
++)
47 if (icd
->formats
[i
].fourcc
== fourcc
)
48 return icd
->formats
+ i
;
51 EXPORT_SYMBOL(soc_camera_format_by_fourcc
);
53 const struct soc_camera_format_xlate
*soc_camera_xlate_by_fourcc(
54 struct soc_camera_device
*icd
, unsigned int fourcc
)
58 for (i
= 0; i
< icd
->num_user_formats
; i
++)
59 if (icd
->user_formats
[i
].host_fmt
->fourcc
== fourcc
)
60 return icd
->user_formats
+ i
;
63 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc
);
66 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
67 * @icl: camera platform parameters
68 * @flags: flags to be inverted according to platform configuration
69 * @return: resulting flags
71 unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link
*icl
,
76 /* If only one of the two polarities is supported, switch to the opposite */
77 if (icl
->flags
& SOCAM_SENSOR_INVERT_HSYNC
) {
78 f
= flags
& (SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
);
79 if (f
== SOCAM_HSYNC_ACTIVE_HIGH
|| f
== SOCAM_HSYNC_ACTIVE_LOW
)
80 flags
^= SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
;
83 if (icl
->flags
& SOCAM_SENSOR_INVERT_VSYNC
) {
84 f
= flags
& (SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
);
85 if (f
== SOCAM_VSYNC_ACTIVE_HIGH
|| f
== SOCAM_VSYNC_ACTIVE_LOW
)
86 flags
^= SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
;
89 if (icl
->flags
& SOCAM_SENSOR_INVERT_PCLK
) {
90 f
= flags
& (SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
);
91 if (f
== SOCAM_PCLK_SAMPLE_RISING
|| f
== SOCAM_PCLK_SAMPLE_FALLING
)
92 flags
^= SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
;
97 EXPORT_SYMBOL(soc_camera_apply_sensor_flags
);
99 static int soc_camera_try_fmt_vid_cap(struct file
*file
, void *priv
,
100 struct v4l2_format
*f
)
102 struct soc_camera_file
*icf
= file
->private_data
;
103 struct soc_camera_device
*icd
= icf
->icd
;
104 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
106 WARN_ON(priv
!= file
->private_data
);
108 /* limit format to hardware capabilities */
109 return ici
->ops
->try_fmt(icd
, f
);
112 static int soc_camera_enum_input(struct file
*file
, void *priv
,
113 struct v4l2_input
*inp
)
115 struct soc_camera_file
*icf
= file
->private_data
;
116 struct soc_camera_device
*icd
= icf
->icd
;
122 if (icd
->ops
->enum_input
)
123 ret
= icd
->ops
->enum_input(icd
, inp
);
125 /* default is camera */
126 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
127 inp
->std
= V4L2_STD_UNKNOWN
;
128 strcpy(inp
->name
, "Camera");
134 static int soc_camera_g_input(struct file
*file
, void *priv
, unsigned int *i
)
141 static int soc_camera_s_input(struct file
*file
, void *priv
, unsigned int i
)
149 static int soc_camera_s_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
151 struct soc_camera_file
*icf
= file
->private_data
;
152 struct soc_camera_device
*icd
= icf
->icd
;
155 if (icd
->ops
->set_std
)
156 ret
= icd
->ops
->set_std(icd
, a
);
161 static int soc_camera_reqbufs(struct file
*file
, void *priv
,
162 struct v4l2_requestbuffers
*p
)
165 struct soc_camera_file
*icf
= file
->private_data
;
166 struct soc_camera_device
*icd
= icf
->icd
;
167 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
169 WARN_ON(priv
!= file
->private_data
);
171 dev_dbg(&icd
->dev
, "%s: %d\n", __func__
, p
->memory
);
173 ret
= videobuf_reqbufs(&icf
->vb_vidq
, p
);
177 return ici
->ops
->reqbufs(icf
, p
);
180 static int soc_camera_querybuf(struct file
*file
, void *priv
,
181 struct v4l2_buffer
*p
)
183 struct soc_camera_file
*icf
= file
->private_data
;
185 WARN_ON(priv
!= file
->private_data
);
187 return videobuf_querybuf(&icf
->vb_vidq
, p
);
190 static int soc_camera_qbuf(struct file
*file
, void *priv
,
191 struct v4l2_buffer
*p
)
193 struct soc_camera_file
*icf
= file
->private_data
;
195 WARN_ON(priv
!= file
->private_data
);
197 return videobuf_qbuf(&icf
->vb_vidq
, p
);
200 static int soc_camera_dqbuf(struct file
*file
, void *priv
,
201 struct v4l2_buffer
*p
)
203 struct soc_camera_file
*icf
= file
->private_data
;
205 WARN_ON(priv
!= file
->private_data
);
207 return videobuf_dqbuf(&icf
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
210 static int soc_camera_init_user_formats(struct soc_camera_device
*icd
)
212 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
215 if (!ici
->ops
->get_formats
)
217 * Fallback mode - the host will have to serve all
218 * sensor-provided formats one-to-one to the user
220 fmts
= icd
->num_formats
;
223 * First pass - only count formats this host-sensor
224 * configuration can provide
226 for (i
= 0; i
< icd
->num_formats
; i
++)
227 fmts
+= ici
->ops
->get_formats(icd
, i
, NULL
);
233 vmalloc(fmts
* sizeof(struct soc_camera_format_xlate
));
234 if (!icd
->user_formats
)
237 icd
->num_user_formats
= fmts
;
240 dev_dbg(&icd
->dev
, "Found %d supported formats.\n", fmts
);
242 /* Second pass - actually fill data formats */
243 for (i
= 0; i
< icd
->num_formats
; i
++)
244 if (!ici
->ops
->get_formats
) {
245 icd
->user_formats
[i
].host_fmt
= icd
->formats
+ i
;
246 icd
->user_formats
[i
].cam_fmt
= icd
->formats
+ i
;
247 icd
->user_formats
[i
].buswidth
= icd
->formats
[i
].depth
;
249 fmts
+= ici
->ops
->get_formats(icd
, i
,
250 &icd
->user_formats
[fmts
]);
253 icd
->current_fmt
= icd
->user_formats
[0].host_fmt
;
258 static void soc_camera_free_user_formats(struct soc_camera_device
*icd
)
260 vfree(icd
->user_formats
);
263 /* Called with .vb_lock held */
264 static int soc_camera_set_fmt(struct soc_camera_file
*icf
,
265 struct v4l2_format
*f
)
267 struct soc_camera_device
*icd
= icf
->icd
;
268 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
269 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
272 /* We always call try_fmt() before set_fmt() or set_crop() */
273 ret
= ici
->ops
->try_fmt(icd
, f
);
277 ret
= ici
->ops
->set_fmt(icd
, f
);
280 } else if (!icd
->current_fmt
||
281 icd
->current_fmt
->fourcc
!= pix
->pixelformat
) {
283 "Host driver hasn't set up current format correctly!\n");
287 icd
->width
= pix
->width
;
288 icd
->height
= pix
->height
;
290 icd
->field
= pix
->field
;
292 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
293 dev_warn(&icd
->dev
, "Attention! Wrong buf-type %d\n",
296 dev_dbg(&icd
->dev
, "set width: %d height: %d\n",
297 icd
->width
, icd
->height
);
299 /* set physical bus parameters */
300 return ici
->ops
->set_bus_param(icd
, pix
->pixelformat
);
303 static int soc_camera_open(struct file
*file
)
305 struct video_device
*vdev
;
306 struct soc_camera_device
*icd
;
307 struct soc_camera_host
*ici
;
308 struct soc_camera_file
*icf
;
311 icf
= vmalloc(sizeof(*icf
));
316 * It is safe to dereference these pointers now as long as a user has
317 * the video device open - we are protected by the held cdev reference.
320 vdev
= video_devdata(file
);
321 icd
= container_of(vdev
->parent
, struct soc_camera_device
, dev
);
322 ici
= to_soc_camera_host(icd
->dev
.parent
);
324 if (!try_module_get(icd
->ops
->owner
)) {
325 dev_err(&icd
->dev
, "Couldn't lock sensor driver.\n");
330 if (!try_module_get(ici
->ops
->owner
)) {
331 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
336 /* Protect against icd->remove() until we module_get() both drivers. */
337 mutex_lock(&icd
->video_lock
);
342 /* Now we really have to activate the camera */
343 if (icd
->use_count
== 1) {
344 /* Restore parameters before the last close() per V4L2 API */
345 struct v4l2_format f
= {
346 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
349 .height
= icd
->height
,
351 .pixelformat
= icd
->current_fmt
->fourcc
,
352 .colorspace
= icd
->current_fmt
->colorspace
,
356 ret
= ici
->ops
->add(icd
);
358 dev_err(&icd
->dev
, "Couldn't activate the camera: %d\n", ret
);
362 /* Try to configure with default parameters */
363 ret
= soc_camera_set_fmt(icf
, &f
);
368 mutex_unlock(&icd
->video_lock
);
370 file
->private_data
= icf
;
371 dev_dbg(&icd
->dev
, "camera device open\n");
373 ici
->ops
->init_videobuf(&icf
->vb_vidq
, icd
);
378 * First three errors are entered with the .video_lock held
382 ici
->ops
->remove(icd
);
385 mutex_unlock(&icd
->video_lock
);
386 module_put(ici
->ops
->owner
);
388 module_put(icd
->ops
->owner
);
394 static int soc_camera_close(struct file
*file
)
396 struct soc_camera_file
*icf
= file
->private_data
;
397 struct soc_camera_device
*icd
= icf
->icd
;
398 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
399 struct video_device
*vdev
= icd
->vdev
;
401 mutex_lock(&icd
->video_lock
);
404 ici
->ops
->remove(icd
);
406 mutex_unlock(&icd
->video_lock
);
408 module_put(icd
->ops
->owner
);
409 module_put(ici
->ops
->owner
);
413 dev_dbg(vdev
->parent
, "camera device close\n");
418 static ssize_t
soc_camera_read(struct file
*file
, char __user
*buf
,
419 size_t count
, loff_t
*ppos
)
421 struct soc_camera_file
*icf
= file
->private_data
;
422 struct soc_camera_device
*icd
= icf
->icd
;
423 struct video_device
*vdev
= icd
->vdev
;
426 dev_err(vdev
->parent
, "camera device read not implemented\n");
431 static int soc_camera_mmap(struct file
*file
, struct vm_area_struct
*vma
)
433 struct soc_camera_file
*icf
= file
->private_data
;
434 struct soc_camera_device
*icd
= icf
->icd
;
437 dev_dbg(&icd
->dev
, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
439 err
= videobuf_mmap_mapper(&icf
->vb_vidq
, vma
);
441 dev_dbg(&icd
->dev
, "vma start=0x%08lx, size=%ld, ret=%d\n",
442 (unsigned long)vma
->vm_start
,
443 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
449 static unsigned int soc_camera_poll(struct file
*file
, poll_table
*pt
)
451 struct soc_camera_file
*icf
= file
->private_data
;
452 struct soc_camera_device
*icd
= icf
->icd
;
453 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
455 if (list_empty(&icf
->vb_vidq
.stream
)) {
456 dev_err(&icd
->dev
, "Trying to poll with no queued buffers!\n");
460 return ici
->ops
->poll(file
, pt
);
463 static struct v4l2_file_operations soc_camera_fops
= {
464 .owner
= THIS_MODULE
,
465 .open
= soc_camera_open
,
466 .release
= soc_camera_close
,
467 .ioctl
= video_ioctl2
,
468 .read
= soc_camera_read
,
469 .mmap
= soc_camera_mmap
,
470 .poll
= soc_camera_poll
,
473 static int soc_camera_s_fmt_vid_cap(struct file
*file
, void *priv
,
474 struct v4l2_format
*f
)
476 struct soc_camera_file
*icf
= file
->private_data
;
477 struct soc_camera_device
*icd
= icf
->icd
;
480 WARN_ON(priv
!= file
->private_data
);
482 mutex_lock(&icf
->vb_vidq
.vb_lock
);
484 if (videobuf_queue_is_busy(&icf
->vb_vidq
)) {
485 dev_err(&icd
->dev
, "S_FMT denied: queue busy\n");
490 ret
= soc_camera_set_fmt(icf
, f
);
493 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
498 static int soc_camera_enum_fmt_vid_cap(struct file
*file
, void *priv
,
499 struct v4l2_fmtdesc
*f
)
501 struct soc_camera_file
*icf
= file
->private_data
;
502 struct soc_camera_device
*icd
= icf
->icd
;
503 const struct soc_camera_data_format
*format
;
505 WARN_ON(priv
!= file
->private_data
);
507 if (f
->index
>= icd
->num_user_formats
)
510 format
= icd
->user_formats
[f
->index
].host_fmt
;
512 strlcpy(f
->description
, format
->name
, sizeof(f
->description
));
513 f
->pixelformat
= format
->fourcc
;
517 static int soc_camera_g_fmt_vid_cap(struct file
*file
, void *priv
,
518 struct v4l2_format
*f
)
520 struct soc_camera_file
*icf
= file
->private_data
;
521 struct soc_camera_device
*icd
= icf
->icd
;
522 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
524 WARN_ON(priv
!= file
->private_data
);
526 pix
->width
= icd
->width
;
527 pix
->height
= icd
->height
;
528 pix
->field
= icf
->vb_vidq
.field
;
529 pix
->pixelformat
= icd
->current_fmt
->fourcc
;
530 pix
->bytesperline
= pix
->width
*
531 DIV_ROUND_UP(icd
->current_fmt
->depth
, 8);
532 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
533 dev_dbg(&icd
->dev
, "current_fmt->fourcc: 0x%08x\n",
534 icd
->current_fmt
->fourcc
);
538 static int soc_camera_querycap(struct file
*file
, void *priv
,
539 struct v4l2_capability
*cap
)
541 struct soc_camera_file
*icf
= file
->private_data
;
542 struct soc_camera_device
*icd
= icf
->icd
;
543 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
545 WARN_ON(priv
!= file
->private_data
);
547 strlcpy(cap
->driver
, ici
->drv_name
, sizeof(cap
->driver
));
548 return ici
->ops
->querycap(ici
, cap
);
551 static int soc_camera_streamon(struct file
*file
, void *priv
,
552 enum v4l2_buf_type i
)
554 struct soc_camera_file
*icf
= file
->private_data
;
555 struct soc_camera_device
*icd
= icf
->icd
;
558 WARN_ON(priv
!= file
->private_data
);
560 dev_dbg(&icd
->dev
, "%s\n", __func__
);
562 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
565 mutex_lock(&icd
->video_lock
);
567 icd
->ops
->start_capture(icd
);
569 /* This calls buf_queue from host driver's videobuf_queue_ops */
570 ret
= videobuf_streamon(&icf
->vb_vidq
);
572 mutex_unlock(&icd
->video_lock
);
577 static int soc_camera_streamoff(struct file
*file
, void *priv
,
578 enum v4l2_buf_type i
)
580 struct soc_camera_file
*icf
= file
->private_data
;
581 struct soc_camera_device
*icd
= icf
->icd
;
583 WARN_ON(priv
!= file
->private_data
);
585 dev_dbg(&icd
->dev
, "%s\n", __func__
);
587 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
590 mutex_lock(&icd
->video_lock
);
592 /* This calls buf_release from host driver's videobuf_queue_ops for all
593 * remaining buffers. When the last buffer is freed, stop capture */
594 videobuf_streamoff(&icf
->vb_vidq
);
596 icd
->ops
->stop_capture(icd
);
598 mutex_unlock(&icd
->video_lock
);
603 static int soc_camera_queryctrl(struct file
*file
, void *priv
,
604 struct v4l2_queryctrl
*qc
)
606 struct soc_camera_file
*icf
= file
->private_data
;
607 struct soc_camera_device
*icd
= icf
->icd
;
610 WARN_ON(priv
!= file
->private_data
);
615 for (i
= 0; i
< icd
->ops
->num_controls
; i
++)
616 if (qc
->id
== icd
->ops
->controls
[i
].id
) {
617 memcpy(qc
, &(icd
->ops
->controls
[i
]),
625 static int soc_camera_g_ctrl(struct file
*file
, void *priv
,
626 struct v4l2_control
*ctrl
)
628 struct soc_camera_file
*icf
= file
->private_data
;
629 struct soc_camera_device
*icd
= icf
->icd
;
631 WARN_ON(priv
!= file
->private_data
);
635 if (icd
->gain
== (unsigned short)~0)
637 ctrl
->value
= icd
->gain
;
639 case V4L2_CID_EXPOSURE
:
640 if (icd
->exposure
== (unsigned short)~0)
642 ctrl
->value
= icd
->exposure
;
646 if (icd
->ops
->get_control
)
647 return icd
->ops
->get_control(icd
, ctrl
);
651 static int soc_camera_s_ctrl(struct file
*file
, void *priv
,
652 struct v4l2_control
*ctrl
)
654 struct soc_camera_file
*icf
= file
->private_data
;
655 struct soc_camera_device
*icd
= icf
->icd
;
657 WARN_ON(priv
!= file
->private_data
);
659 if (icd
->ops
->set_control
)
660 return icd
->ops
->set_control(icd
, ctrl
);
664 static int soc_camera_cropcap(struct file
*file
, void *fh
,
665 struct v4l2_cropcap
*a
)
667 struct soc_camera_file
*icf
= file
->private_data
;
668 struct soc_camera_device
*icd
= icf
->icd
;
670 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
671 a
->bounds
.left
= icd
->x_min
;
672 a
->bounds
.top
= icd
->y_min
;
673 a
->bounds
.width
= icd
->width_max
;
674 a
->bounds
.height
= icd
->height_max
;
675 a
->defrect
.left
= icd
->x_min
;
676 a
->defrect
.top
= icd
->y_min
;
677 a
->defrect
.width
= DEFAULT_WIDTH
;
678 a
->defrect
.height
= DEFAULT_HEIGHT
;
679 a
->pixelaspect
.numerator
= 1;
680 a
->pixelaspect
.denominator
= 1;
685 static int soc_camera_g_crop(struct file
*file
, void *fh
,
688 struct soc_camera_file
*icf
= file
->private_data
;
689 struct soc_camera_device
*icd
= icf
->icd
;
691 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
692 a
->c
.left
= icd
->x_current
;
693 a
->c
.top
= icd
->y_current
;
694 a
->c
.width
= icd
->width
;
695 a
->c
.height
= icd
->height
;
700 static int soc_camera_s_crop(struct file
*file
, void *fh
,
703 struct soc_camera_file
*icf
= file
->private_data
;
704 struct soc_camera_device
*icd
= icf
->icd
;
705 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
708 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
711 /* Cropping is allowed during a running capture, guard consistency */
712 mutex_lock(&icf
->vb_vidq
.vb_lock
);
714 ret
= ici
->ops
->set_crop(icd
, &a
->c
);
716 icd
->width
= a
->c
.width
;
717 icd
->height
= a
->c
.height
;
718 icd
->x_current
= a
->c
.left
;
719 icd
->y_current
= a
->c
.top
;
722 mutex_unlock(&icf
->vb_vidq
.vb_lock
);
727 static int soc_camera_g_chip_ident(struct file
*file
, void *fh
,
728 struct v4l2_dbg_chip_ident
*id
)
730 struct soc_camera_file
*icf
= file
->private_data
;
731 struct soc_camera_device
*icd
= icf
->icd
;
733 if (!icd
->ops
->get_chip_id
)
736 return icd
->ops
->get_chip_id(icd
, id
);
739 #ifdef CONFIG_VIDEO_ADV_DEBUG
740 static int soc_camera_g_register(struct file
*file
, void *fh
,
741 struct v4l2_dbg_register
*reg
)
743 struct soc_camera_file
*icf
= file
->private_data
;
744 struct soc_camera_device
*icd
= icf
->icd
;
746 if (!icd
->ops
->get_register
)
749 return icd
->ops
->get_register(icd
, reg
);
752 static int soc_camera_s_register(struct file
*file
, void *fh
,
753 struct v4l2_dbg_register
*reg
)
755 struct soc_camera_file
*icf
= file
->private_data
;
756 struct soc_camera_device
*icd
= icf
->icd
;
758 if (!icd
->ops
->set_register
)
761 return icd
->ops
->set_register(icd
, reg
);
765 static int device_register_link(struct soc_camera_device
*icd
)
767 int ret
= device_register(&icd
->dev
);
770 /* Prevent calling device_unregister() */
771 icd
->dev
.parent
= NULL
;
772 dev_err(&icd
->dev
, "Cannot register device: %d\n", ret
);
773 /* Even if probe() was unsuccessful for all registered drivers,
774 * device_register() returns 0, and we add the link, just to
775 * document this camera's control device */
776 } else if (icd
->control
)
777 /* Have to sysfs_remove_link() before device_unregister()? */
778 if (sysfs_create_link(&icd
->dev
.kobj
, &icd
->control
->kobj
,
781 "Failed creating the control symlink\n");
785 /* So far this function cannot fail */
786 static void scan_add_host(struct soc_camera_host
*ici
)
788 struct soc_camera_device
*icd
;
790 mutex_lock(&list_lock
);
792 list_for_each_entry(icd
, &devices
, list
) {
793 if (icd
->iface
== ici
->nr
) {
794 icd
->dev
.parent
= &ici
->dev
;
795 device_register_link(icd
);
799 mutex_unlock(&list_lock
);
802 /* return: 0 if no match found or a match found and
803 * device_register() successful, error code otherwise */
804 static int scan_add_device(struct soc_camera_device
*icd
)
806 struct soc_camera_host
*ici
;
809 mutex_lock(&list_lock
);
811 list_add_tail(&icd
->list
, &devices
);
813 /* Watch out for class_for_each_device / class_find_device API by
814 * Dave Young <hidave.darkstar@gmail.com> */
815 list_for_each_entry(ici
, &hosts
, list
) {
816 if (icd
->iface
== ici
->nr
) {
818 icd
->dev
.parent
= &ici
->dev
;
823 mutex_unlock(&list_lock
);
826 ret
= device_register_link(icd
);
831 static int soc_camera_probe(struct device
*dev
)
833 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
834 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
838 * Possible race scenario:
839 * modprobe <camera-host-driver> triggers __func__
840 * at this moment respective <camera-sensor-driver> gets rmmod'ed
841 * to protect take module references.
844 if (!try_module_get(icd
->ops
->owner
)) {
845 dev_err(&icd
->dev
, "Couldn't lock sensor driver.\n");
850 if (!try_module_get(ici
->ops
->owner
)) {
851 dev_err(&icd
->dev
, "Couldn't lock capture bus driver.\n");
856 mutex_lock(&icd
->video_lock
);
858 /* We only call ->add() here to activate and probe the camera.
859 * We shall ->remove() and deactivate it immediately afterwards. */
860 ret
= ici
->ops
->add(icd
);
864 ret
= icd
->ops
->probe(icd
);
866 const struct v4l2_queryctrl
*qctrl
;
868 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_GAIN
);
869 icd
->gain
= qctrl
? qctrl
->default_value
: (unsigned short)~0;
870 qctrl
= soc_camera_find_qctrl(icd
->ops
, V4L2_CID_EXPOSURE
);
871 icd
->exposure
= qctrl
? qctrl
->default_value
:
874 ret
= soc_camera_init_user_formats(icd
);
878 icd
->height
= DEFAULT_HEIGHT
;
879 icd
->width
= DEFAULT_WIDTH
;
880 icd
->field
= V4L2_FIELD_ANY
;
884 ici
->ops
->remove(icd
);
886 mutex_unlock(&icd
->video_lock
);
887 module_put(ici
->ops
->owner
);
889 module_put(icd
->ops
->owner
);
894 /* This is called on device_unregister, which only means we have to disconnect
895 * from the host, but not remove ourselves from the device list */
896 static int soc_camera_remove(struct device
*dev
)
898 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
900 if (icd
->ops
->remove
)
901 icd
->ops
->remove(icd
);
903 soc_camera_free_user_formats(icd
);
908 static int soc_camera_suspend(struct device
*dev
, pm_message_t state
)
910 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
911 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
914 if (ici
->ops
->suspend
)
915 ret
= ici
->ops
->suspend(icd
, state
);
920 static int soc_camera_resume(struct device
*dev
)
922 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
923 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
926 if (ici
->ops
->resume
)
927 ret
= ici
->ops
->resume(icd
);
932 static struct bus_type soc_camera_bus_type
= {
933 .name
= "soc-camera",
934 .probe
= soc_camera_probe
,
935 .remove
= soc_camera_remove
,
936 .suspend
= soc_camera_suspend
,
937 .resume
= soc_camera_resume
,
940 static struct device_driver ic_drv
= {
942 .bus
= &soc_camera_bus_type
,
943 .owner
= THIS_MODULE
,
946 static void dummy_release(struct device
*dev
)
950 int soc_camera_host_register(struct soc_camera_host
*ici
)
953 struct soc_camera_host
*ix
;
955 if (!ici
|| !ici
->ops
||
956 !ici
->ops
->try_fmt
||
957 !ici
->ops
->set_fmt
||
958 !ici
->ops
->set_crop
||
959 !ici
->ops
->set_bus_param
||
960 !ici
->ops
->querycap
||
961 !ici
->ops
->init_videobuf
||
962 !ici
->ops
->reqbufs
||
968 /* Number might be equal to the platform device ID */
969 dev_set_name(&ici
->dev
, "camera_host%d", ici
->nr
);
971 mutex_lock(&list_lock
);
972 list_for_each_entry(ix
, &hosts
, list
) {
973 if (ix
->nr
== ici
->nr
) {
974 mutex_unlock(&list_lock
);
979 list_add_tail(&ici
->list
, &hosts
);
980 mutex_unlock(&list_lock
);
982 ici
->dev
.release
= dummy_release
;
984 ret
= device_register(&ici
->dev
);
994 mutex_lock(&list_lock
);
995 list_del(&ici
->list
);
996 mutex_unlock(&list_lock
);
1000 EXPORT_SYMBOL(soc_camera_host_register
);
1002 /* Unregister all clients! */
1003 void soc_camera_host_unregister(struct soc_camera_host
*ici
)
1005 struct soc_camera_device
*icd
;
1007 mutex_lock(&list_lock
);
1009 list_del(&ici
->list
);
1011 list_for_each_entry(icd
, &devices
, list
) {
1012 if (icd
->dev
.parent
== &ici
->dev
) {
1013 device_unregister(&icd
->dev
);
1014 /* Not before device_unregister(), .remove
1015 * needs parent to call ici->ops->remove() */
1016 icd
->dev
.parent
= NULL
;
1017 memset(&icd
->dev
.kobj
, 0, sizeof(icd
->dev
.kobj
));
1021 mutex_unlock(&list_lock
);
1023 device_unregister(&ici
->dev
);
1025 EXPORT_SYMBOL(soc_camera_host_unregister
);
1027 /* Image capture device */
1028 int soc_camera_device_register(struct soc_camera_device
*icd
)
1030 struct soc_camera_device
*ix
;
1033 if (!icd
|| !icd
->ops
||
1036 !icd
->ops
->release
||
1037 !icd
->ops
->start_capture
||
1038 !icd
->ops
->stop_capture
||
1039 !icd
->ops
->set_crop
||
1040 !icd
->ops
->set_fmt
||
1041 !icd
->ops
->try_fmt
||
1042 !icd
->ops
->query_bus_param
||
1043 !icd
->ops
->set_bus_param
)
1046 for (i
= 0; i
< 256 && num
< 0; i
++) {
1048 list_for_each_entry(ix
, &devices
, list
) {
1049 if (ix
->iface
== icd
->iface
&& ix
->devnum
== i
) {
1057 /* ok, we have 256 cameras on this host...
1058 * man, stay reasonable... */
1062 icd
->dev
.bus
= &soc_camera_bus_type
;
1063 dev_set_name(&icd
->dev
, "%u-%u", icd
->iface
, icd
->devnum
);
1065 icd
->dev
.release
= dummy_release
;
1067 icd
->host_priv
= NULL
;
1068 mutex_init(&icd
->video_lock
);
1070 return scan_add_device(icd
);
1072 EXPORT_SYMBOL(soc_camera_device_register
);
1074 void soc_camera_device_unregister(struct soc_camera_device
*icd
)
1076 mutex_lock(&list_lock
);
1077 list_del(&icd
->list
);
1079 /* The bus->remove will be eventually called */
1080 if (icd
->dev
.parent
)
1081 device_unregister(&icd
->dev
);
1082 mutex_unlock(&list_lock
);
1084 EXPORT_SYMBOL(soc_camera_device_unregister
);
1086 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops
= {
1087 .vidioc_querycap
= soc_camera_querycap
,
1088 .vidioc_g_fmt_vid_cap
= soc_camera_g_fmt_vid_cap
,
1089 .vidioc_enum_fmt_vid_cap
= soc_camera_enum_fmt_vid_cap
,
1090 .vidioc_s_fmt_vid_cap
= soc_camera_s_fmt_vid_cap
,
1091 .vidioc_enum_input
= soc_camera_enum_input
,
1092 .vidioc_g_input
= soc_camera_g_input
,
1093 .vidioc_s_input
= soc_camera_s_input
,
1094 .vidioc_s_std
= soc_camera_s_std
,
1095 .vidioc_reqbufs
= soc_camera_reqbufs
,
1096 .vidioc_try_fmt_vid_cap
= soc_camera_try_fmt_vid_cap
,
1097 .vidioc_querybuf
= soc_camera_querybuf
,
1098 .vidioc_qbuf
= soc_camera_qbuf
,
1099 .vidioc_dqbuf
= soc_camera_dqbuf
,
1100 .vidioc_streamon
= soc_camera_streamon
,
1101 .vidioc_streamoff
= soc_camera_streamoff
,
1102 .vidioc_queryctrl
= soc_camera_queryctrl
,
1103 .vidioc_g_ctrl
= soc_camera_g_ctrl
,
1104 .vidioc_s_ctrl
= soc_camera_s_ctrl
,
1105 .vidioc_cropcap
= soc_camera_cropcap
,
1106 .vidioc_g_crop
= soc_camera_g_crop
,
1107 .vidioc_s_crop
= soc_camera_s_crop
,
1108 .vidioc_g_chip_ident
= soc_camera_g_chip_ident
,
1109 #ifdef CONFIG_VIDEO_ADV_DEBUG
1110 .vidioc_g_register
= soc_camera_g_register
,
1111 .vidioc_s_register
= soc_camera_s_register
,
1116 * Usually called from the struct soc_camera_ops .probe() method, i.e., from
1117 * soc_camera_probe() above with .video_lock held
1119 int soc_camera_video_start(struct soc_camera_device
*icd
)
1121 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1123 struct video_device
*vdev
;
1125 if (!icd
->dev
.parent
)
1128 vdev
= video_device_alloc();
1131 dev_dbg(&ici
->dev
, "Allocated video_device %p\n", vdev
);
1133 strlcpy(vdev
->name
, ici
->drv_name
, sizeof(vdev
->name
));
1135 vdev
->parent
= &icd
->dev
;
1136 vdev
->current_norm
= V4L2_STD_UNKNOWN
;
1137 vdev
->fops
= &soc_camera_fops
;
1138 vdev
->ioctl_ops
= &soc_camera_ioctl_ops
;
1139 vdev
->release
= video_device_release
;
1141 vdev
->tvnorms
= V4L2_STD_UNKNOWN
,
1143 err
= video_register_device(vdev
, VFL_TYPE_GRABBER
, vdev
->minor
);
1145 dev_err(vdev
->parent
, "video_register_device failed\n");
1153 video_device_release(vdev
);
1157 EXPORT_SYMBOL(soc_camera_video_start
);
1159 void soc_camera_video_stop(struct soc_camera_device
*icd
)
1161 struct video_device
*vdev
= icd
->vdev
;
1163 dev_dbg(&icd
->dev
, "%s\n", __func__
);
1165 if (!icd
->dev
.parent
|| !vdev
)
1168 mutex_lock(&icd
->video_lock
);
1169 video_unregister_device(vdev
);
1171 mutex_unlock(&icd
->video_lock
);
1173 EXPORT_SYMBOL(soc_camera_video_stop
);
1175 static int __init
soc_camera_init(void)
1177 int ret
= bus_register(&soc_camera_bus_type
);
1180 ret
= driver_register(&ic_drv
);
1187 bus_unregister(&soc_camera_bus_type
);
1191 static void __exit
soc_camera_exit(void)
1193 driver_unregister(&ic_drv
);
1194 bus_unregister(&soc_camera_bus_type
);
1197 module_init(soc_camera_init
);
1198 module_exit(soc_camera_exit
);
1200 MODULE_DESCRIPTION("Image capture bus driver");
1201 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1202 MODULE_LICENSE("GPL");