Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1 | /* |
| 2 | * camera image capture (abstract) bus driver |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> |
| 5 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | */ |
| 18 | |
| 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> |
| 26 | |
| 27 | #include <media/v4l2-common.h> |
| 28 | #include <media/v4l2-dev.h> |
| 29 | #include <media/soc_camera.h> |
| 30 | |
| 31 | static LIST_HEAD(hosts); |
| 32 | static LIST_HEAD(devices); |
| 33 | static DEFINE_MUTEX(list_lock); |
| 34 | static DEFINE_MUTEX(video_lock); |
| 35 | |
| 36 | const static struct soc_camera_data_format* |
| 37 | format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc) |
| 38 | { |
| 39 | unsigned int i; |
| 40 | |
| 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; |
| 45 | } |
| 46 | |
| 47 | static int soc_camera_try_fmt_cap(struct file *file, void *priv, |
| 48 | struct v4l2_format *f) |
| 49 | { |
| 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; |
| 57 | |
| 58 | WARN_ON(priv != file->private_data); |
| 59 | |
| 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; |
| 65 | } |
| 66 | |
| 67 | dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc); |
| 68 | |
| 69 | field = f->fmt.pix.field; |
| 70 | |
| 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; |
| 76 | } |
| 77 | |
| 78 | /* limit to host capabilities */ |
| 79 | ret = ici->try_fmt_cap(ici, f); |
| 80 | |
| 81 | /* limit to sensor capabilities */ |
| 82 | if (!ret) |
| 83 | ret = icd->ops->try_fmt_cap(icd, f); |
| 84 | |
| 85 | /* calculate missing fields */ |
| 86 | f->fmt.pix.field = field; |
| 87 | f->fmt.pix.bytesperline = |
| 88 | (f->fmt.pix.width * fmt->depth) >> 3; |
| 89 | f->fmt.pix.sizeimage = |
| 90 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static int soc_camera_enum_input(struct file *file, void *priv, |
| 96 | struct v4l2_input *inp) |
| 97 | { |
| 98 | if (inp->index != 0) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 102 | inp->std = V4L2_STD_UNKNOWN; |
| 103 | strcpy(inp->name, "Camera"); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) |
| 109 | { |
| 110 | *i = 0; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) |
| 116 | { |
| 117 | if (i > 0) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int soc_camera_reqbufs(struct file *file, void *priv, |
| 129 | struct v4l2_requestbuffers *p) |
| 130 | { |
| 131 | int ret; |
| 132 | struct soc_camera_file *icf = file->private_data; |
| 133 | struct soc_camera_device *icd = icf->icd; |
| 134 | struct soc_camera_host *ici = |
| 135 | to_soc_camera_host(icd->dev.parent); |
| 136 | |
| 137 | WARN_ON(priv != file->private_data); |
| 138 | |
| 139 | dev_dbg(&icd->dev, "%s: %d\n", __FUNCTION__, p->memory); |
| 140 | |
| 141 | ret = videobuf_reqbufs(&icf->vb_vidq, p); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
| 145 | return ici->reqbufs(icf, p); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static int soc_camera_querybuf(struct file *file, void *priv, |
| 151 | struct v4l2_buffer *p) |
| 152 | { |
| 153 | struct soc_camera_file *icf = file->private_data; |
| 154 | |
| 155 | WARN_ON(priv != file->private_data); |
| 156 | |
| 157 | return videobuf_querybuf(&icf->vb_vidq, p); |
| 158 | } |
| 159 | |
| 160 | static int soc_camera_qbuf(struct file *file, void *priv, |
| 161 | struct v4l2_buffer *p) |
| 162 | { |
| 163 | struct soc_camera_file *icf = file->private_data; |
| 164 | |
| 165 | WARN_ON(priv != file->private_data); |
| 166 | |
| 167 | return videobuf_qbuf(&icf->vb_vidq, p); |
| 168 | } |
| 169 | |
| 170 | static int soc_camera_dqbuf(struct file *file, void *priv, |
| 171 | struct v4l2_buffer *p) |
| 172 | { |
| 173 | struct soc_camera_file *icf = file->private_data; |
| 174 | |
| 175 | WARN_ON(priv != file->private_data); |
| 176 | |
| 177 | return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK); |
| 178 | } |
| 179 | |
| 180 | static int soc_camera_open(struct inode *inode, struct file *file) |
| 181 | { |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 182 | struct video_device *vdev; |
| 183 | struct soc_camera_device *icd; |
| 184 | struct soc_camera_host *ici; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 185 | struct soc_camera_file *icf; |
| 186 | int ret; |
| 187 | |
| 188 | icf = vmalloc(sizeof(*icf)); |
| 189 | if (!icf) |
| 190 | return -ENOMEM; |
| 191 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 192 | /* Protect against icd->remove() until we module_get() both drivers. */ |
| 193 | mutex_lock(&video_lock); |
| 194 | |
| 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); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 198 | |
| 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; |
| 203 | } |
| 204 | |
| 205 | if (!try_module_get(ici->owner)) { |
| 206 | dev_err(&icd->dev, "Couldn't lock capture bus driver.\n"); |
| 207 | ret = -EINVAL; |
| 208 | goto emgi; |
| 209 | } |
| 210 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 211 | icd->use_count++; |
| 212 | |
| 213 | icf->icd = icd; |
| 214 | |
| 215 | /* Now we really have to activate the camera */ |
| 216 | if (icd->use_count == 1) { |
| 217 | ret = ici->add(icd); |
| 218 | if (ret < 0) { |
| 219 | dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret); |
| 220 | icd->use_count--; |
| 221 | goto eiciadd; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | mutex_unlock(&video_lock); |
| 226 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 227 | file->private_data = icf; |
| 228 | dev_dbg(&icd->dev, "camera device open\n"); |
| 229 | |
| 230 | /* We must pass NULL as dev pointer, then all pci_* dma operations |
| 231 | * transform to normal dma_* ones. Do we need an irqlock? */ |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 232 | videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, NULL, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 233 | V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE, |
| 234 | ici->msize, icd); |
| 235 | |
| 236 | return 0; |
| 237 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 238 | /* All errors are entered with the video_lock held */ |
| 239 | eiciadd: |
| 240 | module_put(ici->owner); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 241 | emgi: |
| 242 | module_put(icd->ops->owner); |
| 243 | emgd: |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 244 | mutex_unlock(&video_lock); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 245 | vfree(icf); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int soc_camera_close(struct inode *inode, struct file *file) |
| 250 | { |
| 251 | struct soc_camera_file *icf = file->private_data; |
| 252 | struct soc_camera_device *icd = icf->icd; |
| 253 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 254 | struct video_device *vdev = icd->vdev; |
| 255 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 256 | mutex_lock(&video_lock); |
| 257 | icd->use_count--; |
| 258 | if (!icd->use_count) |
| 259 | ici->remove(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 260 | module_put(icd->ops->owner); |
| 261 | module_put(ici->owner); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 262 | mutex_unlock(&video_lock); |
| 263 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 264 | vfree(file->private_data); |
| 265 | |
| 266 | dev_dbg(vdev->dev, "camera device close\n"); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int soc_camera_read(struct file *file, char __user *buf, |
| 272 | size_t count, loff_t *ppos) |
| 273 | { |
| 274 | struct soc_camera_file *icf = file->private_data; |
| 275 | struct soc_camera_device *icd = icf->icd; |
| 276 | struct video_device *vdev = icd->vdev; |
| 277 | int err = -EINVAL; |
| 278 | |
| 279 | dev_err(vdev->dev, "camera device read not implemented\n"); |
| 280 | |
| 281 | return err; |
| 282 | } |
| 283 | |
| 284 | static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) |
| 285 | { |
| 286 | struct soc_camera_file *icf = file->private_data; |
| 287 | struct soc_camera_device *icd = icf->icd; |
| 288 | int err; |
| 289 | |
| 290 | dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
| 291 | |
| 292 | err = videobuf_mmap_mapper(&icf->vb_vidq, vma); |
| 293 | |
| 294 | dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n", |
| 295 | (unsigned long)vma->vm_start, |
| 296 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
| 297 | err); |
| 298 | |
| 299 | return err; |
| 300 | } |
| 301 | |
| 302 | static unsigned int soc_camera_poll(struct file *file, poll_table *pt) |
| 303 | { |
| 304 | struct soc_camera_file *icf = file->private_data; |
| 305 | struct soc_camera_device *icd = icf->icd; |
| 306 | struct soc_camera_host *ici = |
| 307 | to_soc_camera_host(icd->dev.parent); |
| 308 | |
| 309 | if (list_empty(&icf->vb_vidq.stream)) { |
| 310 | dev_err(&icd->dev, "Trying to poll with no queued buffers!\n"); |
| 311 | return POLLERR; |
| 312 | } |
| 313 | |
| 314 | return ici->poll(file, pt); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | static struct file_operations soc_camera_fops = { |
| 319 | .owner = THIS_MODULE, |
| 320 | .open = soc_camera_open, |
| 321 | .release = soc_camera_close, |
| 322 | .ioctl = video_ioctl2, |
| 323 | .read = soc_camera_read, |
| 324 | .mmap = soc_camera_mmap, |
| 325 | .poll = soc_camera_poll, |
| 326 | .llseek = no_llseek, |
| 327 | }; |
| 328 | |
| 329 | |
| 330 | static int soc_camera_s_fmt_cap(struct file *file, void *priv, |
| 331 | struct v4l2_format *f) |
| 332 | { |
| 333 | struct soc_camera_file *icf = file->private_data; |
| 334 | struct soc_camera_device *icd = icf->icd; |
| 335 | struct soc_camera_host *ici = |
| 336 | to_soc_camera_host(icd->dev.parent); |
| 337 | int ret; |
| 338 | struct v4l2_rect rect; |
| 339 | const static struct soc_camera_data_format *data_fmt; |
| 340 | |
| 341 | WARN_ON(priv != file->private_data); |
| 342 | |
| 343 | data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat); |
| 344 | if (!data_fmt) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | /* cached_datawidth may be further adjusted by the ici */ |
| 348 | icd->cached_datawidth = data_fmt->depth; |
| 349 | |
| 350 | ret = soc_camera_try_fmt_cap(file, icf, f); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
| 354 | rect.left = icd->x_current; |
| 355 | rect.top = icd->y_current; |
| 356 | rect.width = f->fmt.pix.width; |
| 357 | rect.height = f->fmt.pix.height; |
| 358 | ret = ici->set_capture_format(icd, f->fmt.pix.pixelformat, &rect); |
| 359 | |
| 360 | if (!ret) { |
| 361 | icd->current_fmt = data_fmt; |
| 362 | icd->width = rect.width; |
| 363 | icd->height = rect.height; |
| 364 | icf->vb_vidq.field = f->fmt.pix.field; |
| 365 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type) |
| 366 | dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n", |
| 367 | f->type); |
| 368 | |
| 369 | dev_dbg(&icd->dev, "set width: %d height: %d\n", |
| 370 | icd->width, icd->height); |
| 371 | } |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static int soc_camera_enum_fmt_cap(struct file *file, void *priv, |
| 377 | struct v4l2_fmtdesc *f) |
| 378 | { |
| 379 | struct soc_camera_file *icf = file->private_data; |
| 380 | struct soc_camera_device *icd = icf->icd; |
| 381 | const struct soc_camera_data_format *format; |
| 382 | |
| 383 | WARN_ON(priv != file->private_data); |
| 384 | |
| 385 | if (f->index >= icd->ops->num_formats) |
| 386 | return -EINVAL; |
| 387 | |
| 388 | format = &icd->ops->formats[f->index]; |
| 389 | |
| 390 | strlcpy(f->description, format->name, sizeof(f->description)); |
| 391 | f->pixelformat = format->fourcc; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int soc_camera_g_fmt_cap(struct file *file, void *priv, |
| 396 | struct v4l2_format *f) |
| 397 | { |
| 398 | struct soc_camera_file *icf = file->private_data; |
| 399 | struct soc_camera_device *icd = icf->icd; |
| 400 | |
| 401 | WARN_ON(priv != file->private_data); |
| 402 | |
| 403 | f->fmt.pix.width = icd->width; |
| 404 | f->fmt.pix.height = icd->height; |
| 405 | f->fmt.pix.field = icf->vb_vidq.field; |
| 406 | f->fmt.pix.pixelformat = icd->current_fmt->fourcc; |
| 407 | f->fmt.pix.bytesperline = |
| 408 | (f->fmt.pix.width * icd->current_fmt->depth) >> 3; |
| 409 | f->fmt.pix.sizeimage = |
| 410 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 411 | dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n", |
| 412 | icd->current_fmt->fourcc); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int soc_camera_querycap(struct file *file, void *priv, |
| 417 | struct v4l2_capability *cap) |
| 418 | { |
| 419 | struct soc_camera_file *icf = file->private_data; |
| 420 | struct soc_camera_device *icd = icf->icd; |
| 421 | struct soc_camera_host *ici = |
| 422 | to_soc_camera_host(icd->dev.parent); |
| 423 | |
| 424 | WARN_ON(priv != file->private_data); |
| 425 | |
| 426 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); |
| 427 | return ici->querycap(ici, cap); |
| 428 | } |
| 429 | |
| 430 | static int soc_camera_streamon(struct file *file, void *priv, |
| 431 | enum v4l2_buf_type i) |
| 432 | { |
| 433 | struct soc_camera_file *icf = file->private_data; |
| 434 | struct soc_camera_device *icd = icf->icd; |
| 435 | |
| 436 | WARN_ON(priv != file->private_data); |
| 437 | |
| 438 | dev_dbg(&icd->dev, "%s\n", __FUNCTION__); |
| 439 | |
| 440 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | icd->ops->start_capture(icd); |
| 444 | |
| 445 | /* This calls buf_queue from host driver's videobuf_queue_ops */ |
| 446 | return videobuf_streamon(&icf->vb_vidq); |
| 447 | } |
| 448 | |
| 449 | static int soc_camera_streamoff(struct file *file, void *priv, |
| 450 | enum v4l2_buf_type i) |
| 451 | { |
| 452 | struct soc_camera_file *icf = file->private_data; |
| 453 | struct soc_camera_device *icd = icf->icd; |
| 454 | |
| 455 | WARN_ON(priv != file->private_data); |
| 456 | |
| 457 | dev_dbg(&icd->dev, "%s\n", __FUNCTION__); |
| 458 | |
| 459 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | /* This calls buf_release from host driver's videobuf_queue_ops for all |
| 463 | * remaining buffers. When the last buffer is freed, stop capture */ |
| 464 | videobuf_streamoff(&icf->vb_vidq); |
| 465 | |
| 466 | icd->ops->stop_capture(icd); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int soc_camera_queryctrl(struct file *file, void *priv, |
| 472 | struct v4l2_queryctrl *qc) |
| 473 | { |
| 474 | struct soc_camera_file *icf = file->private_data; |
| 475 | struct soc_camera_device *icd = icf->icd; |
| 476 | int i; |
| 477 | |
| 478 | WARN_ON(priv != file->private_data); |
| 479 | |
| 480 | if (!qc->id) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | for (i = 0; i < icd->ops->num_controls; i++) |
| 484 | if (qc->id == icd->ops->controls[i].id) { |
| 485 | memcpy(qc, &(icd->ops->controls[i]), |
| 486 | sizeof(*qc)); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | static int soc_camera_g_ctrl(struct file *file, void *priv, |
| 494 | struct v4l2_control *ctrl) |
| 495 | { |
| 496 | struct soc_camera_file *icf = file->private_data; |
| 497 | struct soc_camera_device *icd = icf->icd; |
| 498 | |
| 499 | WARN_ON(priv != file->private_data); |
| 500 | |
| 501 | switch (ctrl->id) { |
| 502 | case V4L2_CID_GAIN: |
| 503 | if (icd->gain == (unsigned short)~0) |
| 504 | return -EINVAL; |
| 505 | ctrl->value = icd->gain; |
| 506 | return 0; |
| 507 | case V4L2_CID_EXPOSURE: |
| 508 | if (icd->exposure == (unsigned short)~0) |
| 509 | return -EINVAL; |
| 510 | ctrl->value = icd->exposure; |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | if (icd->ops->get_control) |
| 515 | return icd->ops->get_control(icd, ctrl); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | static int soc_camera_s_ctrl(struct file *file, void *priv, |
| 520 | struct v4l2_control *ctrl) |
| 521 | { |
| 522 | struct soc_camera_file *icf = file->private_data; |
| 523 | struct soc_camera_device *icd = icf->icd; |
| 524 | |
| 525 | WARN_ON(priv != file->private_data); |
| 526 | |
| 527 | if (icd->ops->set_control) |
| 528 | return icd->ops->set_control(icd, ctrl); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | static int soc_camera_cropcap(struct file *file, void *fh, |
| 533 | struct v4l2_cropcap *a) |
| 534 | { |
| 535 | struct soc_camera_file *icf = file->private_data; |
| 536 | struct soc_camera_device *icd = icf->icd; |
| 537 | |
| 538 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 539 | a->bounds.left = icd->x_min; |
| 540 | a->bounds.top = icd->y_min; |
| 541 | a->bounds.width = icd->width_max; |
| 542 | a->bounds.height = icd->height_max; |
| 543 | a->defrect.left = icd->x_min; |
| 544 | a->defrect.top = icd->y_min; |
| 545 | a->defrect.width = 640; |
| 546 | a->defrect.height = 480; |
| 547 | a->pixelaspect.numerator = 1; |
| 548 | a->pixelaspect.denominator = 1; |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int soc_camera_g_crop(struct file *file, void *fh, |
| 554 | struct v4l2_crop *a) |
| 555 | { |
| 556 | struct soc_camera_file *icf = file->private_data; |
| 557 | struct soc_camera_device *icd = icf->icd; |
| 558 | |
| 559 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 560 | a->c.left = icd->x_current; |
| 561 | a->c.top = icd->y_current; |
| 562 | a->c.width = icd->width; |
| 563 | a->c.height = icd->height; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int soc_camera_s_crop(struct file *file, void *fh, |
| 569 | struct v4l2_crop *a) |
| 570 | { |
| 571 | struct soc_camera_file *icf = file->private_data; |
| 572 | struct soc_camera_device *icd = icf->icd; |
| 573 | struct soc_camera_host *ici = |
| 574 | to_soc_camera_host(icd->dev.parent); |
| 575 | int ret; |
| 576 | |
| 577 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 578 | return -EINVAL; |
| 579 | |
| 580 | ret = ici->set_capture_format(icd, 0, &a->c); |
| 581 | if (!ret) { |
| 582 | icd->width = a->c.width; |
| 583 | icd->height = a->c.height; |
| 584 | icd->x_current = a->c.left; |
| 585 | icd->y_current = a->c.top; |
| 586 | } |
| 587 | |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static int soc_camera_g_chip_ident(struct file *file, void *fh, |
| 592 | struct v4l2_chip_ident *id) |
| 593 | { |
| 594 | struct soc_camera_file *icf = file->private_data; |
| 595 | struct soc_camera_device *icd = icf->icd; |
| 596 | |
| 597 | if (!icd->ops->get_chip_id) |
| 598 | return -EINVAL; |
| 599 | |
| 600 | return icd->ops->get_chip_id(icd, id); |
| 601 | } |
| 602 | |
| 603 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 604 | static int soc_camera_g_register(struct file *file, void *fh, |
| 605 | struct v4l2_register *reg) |
| 606 | { |
| 607 | struct soc_camera_file *icf = file->private_data; |
| 608 | struct soc_camera_device *icd = icf->icd; |
| 609 | |
| 610 | if (!icd->ops->get_register) |
| 611 | return -EINVAL; |
| 612 | |
| 613 | return icd->ops->get_register(icd, reg); |
| 614 | } |
| 615 | |
| 616 | static int soc_camera_s_register(struct file *file, void *fh, |
| 617 | struct v4l2_register *reg) |
| 618 | { |
| 619 | struct soc_camera_file *icf = file->private_data; |
| 620 | struct soc_camera_device *icd = icf->icd; |
| 621 | |
| 622 | if (!icd->ops->set_register) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | return icd->ops->set_register(icd, reg); |
| 626 | } |
| 627 | #endif |
| 628 | |
| 629 | static int device_register_link(struct soc_camera_device *icd) |
| 630 | { |
| 631 | int ret = device_register(&icd->dev); |
| 632 | |
| 633 | if (ret < 0) { |
| 634 | /* Prevent calling device_unregister() */ |
| 635 | icd->dev.parent = NULL; |
| 636 | dev_err(&icd->dev, "Cannot register device: %d\n", ret); |
| 637 | /* Even if probe() was unsuccessful for all registered drivers, |
| 638 | * device_register() returns 0, and we add the link, just to |
| 639 | * document this camera's control device */ |
| 640 | } else if (icd->control) |
| 641 | /* Have to sysfs_remove_link() before device_unregister()? */ |
| 642 | if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj, |
| 643 | "control")) |
| 644 | dev_warn(&icd->dev, |
| 645 | "Failed creating the control symlink\n"); |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | /* So far this function cannot fail */ |
| 650 | static void scan_add_host(struct soc_camera_host *ici) |
| 651 | { |
| 652 | struct soc_camera_device *icd; |
| 653 | |
| 654 | mutex_lock(&list_lock); |
| 655 | |
| 656 | list_for_each_entry(icd, &devices, list) { |
| 657 | if (icd->iface == ici->nr) { |
| 658 | icd->dev.parent = &ici->dev; |
| 659 | device_register_link(icd); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | mutex_unlock(&list_lock); |
| 664 | } |
| 665 | |
| 666 | /* return: 0 if no match found or a match found and |
| 667 | * device_register() successful, error code otherwise */ |
| 668 | static int scan_add_device(struct soc_camera_device *icd) |
| 669 | { |
| 670 | struct soc_camera_host *ici; |
| 671 | int ret = 0; |
| 672 | |
| 673 | mutex_lock(&list_lock); |
| 674 | |
| 675 | list_add_tail(&icd->list, &devices); |
| 676 | |
| 677 | /* Watch out for class_for_each_device / class_find_device API by |
| 678 | * Dave Young <hidave.darkstar@gmail.com> */ |
| 679 | list_for_each_entry(ici, &hosts, list) { |
| 680 | if (icd->iface == ici->nr) { |
| 681 | ret = 1; |
| 682 | icd->dev.parent = &ici->dev; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | mutex_unlock(&list_lock); |
| 688 | |
| 689 | if (ret) |
| 690 | ret = device_register_link(icd); |
| 691 | |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static int soc_camera_probe(struct device *dev) |
| 696 | { |
| 697 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
| 698 | struct soc_camera_host *ici = |
| 699 | to_soc_camera_host(icd->dev.parent); |
| 700 | int ret; |
| 701 | |
| 702 | if (!icd->probe) |
| 703 | return -ENODEV; |
| 704 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 705 | /* We only call ->add() here to activate and probe the camera. |
| 706 | * We shall ->remove() and deactivate it immediately afterwards. */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 707 | ret = ici->add(icd); |
| 708 | if (ret < 0) |
| 709 | return ret; |
| 710 | |
| 711 | ret = icd->probe(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 712 | if (ret >= 0) { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 713 | const struct v4l2_queryctrl *qctrl; |
| 714 | |
| 715 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN); |
| 716 | icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0; |
| 717 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); |
| 718 | icd->exposure = qctrl ? qctrl->default_value : |
| 719 | (unsigned short)~0; |
| 720 | } |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame^] | 721 | ici->remove(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 722 | |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | /* This is called on device_unregister, which only means we have to disconnect |
| 727 | * from the host, but not remove ourselves from the device list */ |
| 728 | static int soc_camera_remove(struct device *dev) |
| 729 | { |
| 730 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 731 | |
| 732 | if (icd->remove) |
| 733 | icd->remove(icd); |
| 734 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static struct bus_type soc_camera_bus_type = { |
| 739 | .name = "soc-camera", |
| 740 | .probe = soc_camera_probe, |
| 741 | .remove = soc_camera_remove, |
| 742 | }; |
| 743 | |
| 744 | static struct device_driver ic_drv = { |
| 745 | .name = "camera", |
| 746 | .bus = &soc_camera_bus_type, |
| 747 | .owner = THIS_MODULE, |
| 748 | }; |
| 749 | |
| 750 | /* |
| 751 | * Image capture host - this is a host device, not a bus device, so, |
| 752 | * no bus reference, no probing. |
| 753 | */ |
| 754 | static struct class soc_camera_host_class = { |
| 755 | .owner = THIS_MODULE, |
| 756 | .name = "camera_host", |
| 757 | }; |
| 758 | |
| 759 | static void dummy_release(struct device *dev) |
| 760 | { |
| 761 | } |
| 762 | |
| 763 | int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner) |
| 764 | { |
| 765 | int ret; |
| 766 | struct soc_camera_host *ix; |
| 767 | |
| 768 | if (!ici->vbq_ops || !ici->add || !ici->remove || !owner) |
| 769 | return -EINVAL; |
| 770 | |
| 771 | /* Number might be equal to the platform device ID */ |
| 772 | sprintf(ici->dev.bus_id, "camera_host%d", ici->nr); |
| 773 | ici->dev.class = &soc_camera_host_class; |
| 774 | |
| 775 | mutex_lock(&list_lock); |
| 776 | list_for_each_entry(ix, &hosts, list) { |
| 777 | if (ix->nr == ici->nr) { |
| 778 | mutex_unlock(&list_lock); |
| 779 | return -EBUSY; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | list_add_tail(&ici->list, &hosts); |
| 784 | mutex_unlock(&list_lock); |
| 785 | |
| 786 | ici->owner = owner; |
| 787 | ici->dev.release = dummy_release; |
| 788 | |
| 789 | ret = device_register(&ici->dev); |
| 790 | |
| 791 | if (ret) |
| 792 | goto edevr; |
| 793 | |
| 794 | scan_add_host(ici); |
| 795 | |
| 796 | return 0; |
| 797 | |
| 798 | edevr: |
| 799 | mutex_lock(&list_lock); |
| 800 | list_del(&ici->list); |
| 801 | mutex_unlock(&list_lock); |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | EXPORT_SYMBOL(soc_camera_host_register); |
| 806 | |
| 807 | /* Unregister all clients! */ |
| 808 | void soc_camera_host_unregister(struct soc_camera_host *ici) |
| 809 | { |
| 810 | struct soc_camera_device *icd; |
| 811 | |
| 812 | mutex_lock(&list_lock); |
| 813 | |
| 814 | list_del(&ici->list); |
| 815 | |
| 816 | list_for_each_entry(icd, &devices, list) { |
| 817 | if (icd->dev.parent == &ici->dev) { |
| 818 | device_unregister(&icd->dev); |
| 819 | /* Not before device_unregister(), .remove |
| 820 | * needs parent to call ici->remove() */ |
| 821 | icd->dev.parent = NULL; |
| 822 | memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj)); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | mutex_unlock(&list_lock); |
| 827 | |
| 828 | device_unregister(&ici->dev); |
| 829 | } |
| 830 | EXPORT_SYMBOL(soc_camera_host_unregister); |
| 831 | |
| 832 | /* Image capture device */ |
| 833 | int soc_camera_device_register(struct soc_camera_device *icd) |
| 834 | { |
| 835 | struct soc_camera_device *ix; |
| 836 | int num = -1, i; |
| 837 | |
| 838 | if (!icd) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | for (i = 0; i < 256 && num < 0; i++) { |
| 842 | num = i; |
| 843 | list_for_each_entry(ix, &devices, list) { |
| 844 | if (ix->iface == icd->iface && ix->devnum == i) { |
| 845 | num = -1; |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (num < 0) |
| 852 | /* ok, we have 256 cameras on this host... |
| 853 | * man, stay reasonable... */ |
| 854 | return -ENOMEM; |
| 855 | |
| 856 | icd->devnum = num; |
| 857 | icd->dev.bus = &soc_camera_bus_type; |
| 858 | snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id), |
| 859 | "%u-%u", icd->iface, icd->devnum); |
| 860 | |
| 861 | icd->dev.release = dummy_release; |
| 862 | |
| 863 | if (icd->ops->get_datawidth) |
| 864 | icd->cached_datawidth = icd->ops->get_datawidth(icd); |
| 865 | |
| 866 | return scan_add_device(icd); |
| 867 | } |
| 868 | EXPORT_SYMBOL(soc_camera_device_register); |
| 869 | |
| 870 | void soc_camera_device_unregister(struct soc_camera_device *icd) |
| 871 | { |
| 872 | mutex_lock(&list_lock); |
| 873 | list_del(&icd->list); |
| 874 | |
| 875 | /* The bus->remove will be eventually called */ |
| 876 | if (icd->dev.parent) |
| 877 | device_unregister(&icd->dev); |
| 878 | mutex_unlock(&list_lock); |
| 879 | } |
| 880 | EXPORT_SYMBOL(soc_camera_device_unregister); |
| 881 | |
| 882 | int soc_camera_video_start(struct soc_camera_device *icd) |
| 883 | { |
| 884 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 885 | int err = -ENOMEM; |
| 886 | struct video_device *vdev; |
| 887 | |
| 888 | if (!icd->dev.parent) |
| 889 | return -ENODEV; |
| 890 | |
| 891 | vdev = video_device_alloc(); |
| 892 | if (!vdev) |
| 893 | goto evidallocd; |
| 894 | dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev); |
| 895 | |
| 896 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); |
| 897 | /* Maybe better &ici->dev */ |
| 898 | vdev->dev = &icd->dev; |
| 899 | vdev->type = VID_TYPE_CAPTURE; |
| 900 | vdev->current_norm = V4L2_STD_UNKNOWN; |
| 901 | vdev->fops = &soc_camera_fops; |
| 902 | vdev->release = video_device_release; |
| 903 | vdev->minor = -1; |
| 904 | vdev->tvnorms = V4L2_STD_UNKNOWN, |
| 905 | vdev->vidioc_querycap = soc_camera_querycap; |
| 906 | vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap; |
| 907 | vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap; |
| 908 | vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap; |
| 909 | vdev->vidioc_enum_input = soc_camera_enum_input; |
| 910 | vdev->vidioc_g_input = soc_camera_g_input; |
| 911 | vdev->vidioc_s_input = soc_camera_s_input; |
| 912 | vdev->vidioc_s_std = soc_camera_s_std; |
| 913 | vdev->vidioc_reqbufs = soc_camera_reqbufs; |
| 914 | vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap; |
| 915 | vdev->vidioc_querybuf = soc_camera_querybuf; |
| 916 | vdev->vidioc_qbuf = soc_camera_qbuf; |
| 917 | vdev->vidioc_dqbuf = soc_camera_dqbuf; |
| 918 | vdev->vidioc_streamon = soc_camera_streamon; |
| 919 | vdev->vidioc_streamoff = soc_camera_streamoff; |
| 920 | vdev->vidioc_queryctrl = soc_camera_queryctrl; |
| 921 | vdev->vidioc_g_ctrl = soc_camera_g_ctrl; |
| 922 | vdev->vidioc_s_ctrl = soc_camera_s_ctrl; |
| 923 | vdev->vidioc_cropcap = soc_camera_cropcap; |
| 924 | vdev->vidioc_g_crop = soc_camera_g_crop; |
| 925 | vdev->vidioc_s_crop = soc_camera_s_crop; |
| 926 | vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident; |
| 927 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 928 | vdev->vidioc_g_register = soc_camera_g_register; |
| 929 | vdev->vidioc_s_register = soc_camera_s_register; |
| 930 | #endif |
| 931 | |
| 932 | icd->current_fmt = &icd->ops->formats[0]; |
| 933 | |
| 934 | err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor); |
| 935 | if (err < 0) { |
| 936 | dev_err(vdev->dev, "video_register_device failed\n"); |
| 937 | goto evidregd; |
| 938 | } |
| 939 | icd->vdev = vdev; |
| 940 | |
| 941 | return 0; |
| 942 | |
| 943 | evidregd: |
| 944 | video_device_release(vdev); |
| 945 | evidallocd: |
| 946 | return err; |
| 947 | } |
| 948 | EXPORT_SYMBOL(soc_camera_video_start); |
| 949 | |
| 950 | void soc_camera_video_stop(struct soc_camera_device *icd) |
| 951 | { |
| 952 | struct video_device *vdev = icd->vdev; |
| 953 | |
| 954 | dev_dbg(&icd->dev, "%s\n", __FUNCTION__); |
| 955 | |
| 956 | if (!icd->dev.parent || !vdev) |
| 957 | return; |
| 958 | |
| 959 | mutex_lock(&video_lock); |
| 960 | video_unregister_device(vdev); |
| 961 | icd->vdev = NULL; |
| 962 | mutex_unlock(&video_lock); |
| 963 | } |
| 964 | EXPORT_SYMBOL(soc_camera_video_stop); |
| 965 | |
| 966 | static int __init soc_camera_init(void) |
| 967 | { |
| 968 | int ret = bus_register(&soc_camera_bus_type); |
| 969 | if (ret) |
| 970 | return ret; |
| 971 | ret = driver_register(&ic_drv); |
| 972 | if (ret) |
| 973 | goto edrvr; |
| 974 | ret = class_register(&soc_camera_host_class); |
| 975 | if (ret) |
| 976 | goto eclr; |
| 977 | |
| 978 | return 0; |
| 979 | |
| 980 | eclr: |
| 981 | driver_unregister(&ic_drv); |
| 982 | edrvr: |
| 983 | bus_unregister(&soc_camera_bus_type); |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | static void __exit soc_camera_exit(void) |
| 988 | { |
| 989 | class_unregister(&soc_camera_host_class); |
| 990 | driver_unregister(&ic_drv); |
| 991 | bus_unregister(&soc_camera_bus_type); |
| 992 | } |
| 993 | |
| 994 | module_init(soc_camera_init); |
| 995 | module_exit(soc_camera_exit); |
| 996 | |
| 997 | MODULE_DESCRIPTION("Image capture bus driver"); |
| 998 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 999 | MODULE_LICENSE("GPL"); |