Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.8-rc2 1587 lines 41 kB view raw
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/device.h> 20#include <linux/err.h> 21#include <linux/i2c.h> 22#include <linux/init.h> 23#include <linux/list.h> 24#include <linux/mutex.h> 25#include <linux/module.h> 26#include <linux/platform_device.h> 27#include <linux/regulator/consumer.h> 28#include <linux/slab.h> 29#include <linux/pm_runtime.h> 30#include <linux/vmalloc.h> 31 32#include <media/soc_camera.h> 33#include <media/v4l2-common.h> 34#include <media/v4l2-ioctl.h> 35#include <media/v4l2-dev.h> 36#include <media/videobuf-core.h> 37#include <media/videobuf2-core.h> 38#include <media/soc_mediabus.h> 39 40/* Default to VGA resolution */ 41#define DEFAULT_WIDTH 640 42#define DEFAULT_HEIGHT 480 43 44#define is_streaming(ici, icd) \ 45 (((ici)->ops->init_videobuf) ? \ 46 (icd)->vb_vidq.streaming : \ 47 vb2_is_streaming(&(icd)->vb2_vidq)) 48 49static LIST_HEAD(hosts); 50static LIST_HEAD(devices); 51static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ 52 53int soc_camera_power_on(struct device *dev, struct soc_camera_link *icl) 54{ 55 int ret = regulator_bulk_enable(icl->num_regulators, 56 icl->regulators); 57 if (ret < 0) { 58 dev_err(dev, "Cannot enable regulators\n"); 59 return ret; 60 } 61 62 if (icl->power) { 63 ret = icl->power(dev, 1); 64 if (ret < 0) { 65 dev_err(dev, 66 "Platform failed to power-on the camera.\n"); 67 regulator_bulk_disable(icl->num_regulators, 68 icl->regulators); 69 } 70 } 71 72 return ret; 73} 74EXPORT_SYMBOL(soc_camera_power_on); 75 76int soc_camera_power_off(struct device *dev, struct soc_camera_link *icl) 77{ 78 int ret = 0; 79 int err; 80 81 if (icl->power) { 82 err = icl->power(dev, 0); 83 if (err < 0) { 84 dev_err(dev, 85 "Platform failed to power-off the camera.\n"); 86 ret = err; 87 } 88 } 89 90 err = regulator_bulk_disable(icl->num_regulators, 91 icl->regulators); 92 if (err < 0) { 93 dev_err(dev, "Cannot disable regulators\n"); 94 ret = ret ? : err; 95 } 96 97 return ret; 98} 99EXPORT_SYMBOL(soc_camera_power_off); 100 101static int __soc_camera_power_on(struct soc_camera_device *icd) 102{ 103 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 104 int ret; 105 106 ret = v4l2_subdev_call(sd, core, s_power, 1); 107 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 108 return ret; 109 110 return 0; 111} 112 113static int __soc_camera_power_off(struct soc_camera_device *icd) 114{ 115 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 116 int ret; 117 118 ret = v4l2_subdev_call(sd, core, s_power, 0); 119 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 120 return ret; 121 122 return 0; 123} 124 125const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc( 126 struct soc_camera_device *icd, unsigned int fourcc) 127{ 128 unsigned int i; 129 130 for (i = 0; i < icd->num_user_formats; i++) 131 if (icd->user_formats[i].host_fmt->fourcc == fourcc) 132 return icd->user_formats + i; 133 return NULL; 134} 135EXPORT_SYMBOL(soc_camera_xlate_by_fourcc); 136 137/** 138 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags 139 * @icl: camera platform parameters 140 * @cfg: media bus configuration 141 * @return: resulting flags 142 */ 143unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl, 144 const struct v4l2_mbus_config *cfg) 145{ 146 unsigned long f, flags = cfg->flags; 147 148 /* If only one of the two polarities is supported, switch to the opposite */ 149 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) { 150 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW); 151 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW) 152 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW; 153 } 154 155 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) { 156 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW); 157 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW) 158 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW; 159 } 160 161 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) { 162 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING); 163 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING) 164 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING; 165 } 166 167 return flags; 168} 169EXPORT_SYMBOL(soc_camera_apply_board_flags); 170 171#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \ 172 ((x) >> 24) & 0xff 173 174static int soc_camera_try_fmt(struct soc_camera_device *icd, 175 struct v4l2_format *f) 176{ 177 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 178 const struct soc_camera_format_xlate *xlate; 179 struct v4l2_pix_format *pix = &f->fmt.pix; 180 int ret; 181 182 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n", 183 pixfmtstr(pix->pixelformat), pix->width, pix->height); 184 185 if (pix->pixelformat != V4L2_PIX_FMT_JPEG && 186 !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) { 187 pix->bytesperline = 0; 188 pix->sizeimage = 0; 189 } 190 191 ret = ici->ops->try_fmt(icd, f); 192 if (ret < 0) 193 return ret; 194 195 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); 196 if (!xlate) 197 return -EINVAL; 198 199 ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt); 200 if (ret < 0) 201 return ret; 202 203 pix->bytesperline = max_t(u32, pix->bytesperline, ret); 204 205 ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline, 206 pix->height); 207 if (ret < 0) 208 return ret; 209 210 pix->sizeimage = max_t(u32, pix->sizeimage, ret); 211 212 return 0; 213} 214 215static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv, 216 struct v4l2_format *f) 217{ 218 struct soc_camera_device *icd = file->private_data; 219 220 WARN_ON(priv != file->private_data); 221 222 /* Only single-plane capture is supported so far */ 223 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 224 return -EINVAL; 225 226 /* limit format to hardware capabilities */ 227 return soc_camera_try_fmt(icd, f); 228} 229 230static int soc_camera_enum_input(struct file *file, void *priv, 231 struct v4l2_input *inp) 232{ 233 if (inp->index != 0) 234 return -EINVAL; 235 236 /* default is camera */ 237 inp->type = V4L2_INPUT_TYPE_CAMERA; 238 inp->std = V4L2_STD_UNKNOWN; 239 strcpy(inp->name, "Camera"); 240 241 return 0; 242} 243 244static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) 245{ 246 *i = 0; 247 248 return 0; 249} 250 251static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) 252{ 253 if (i > 0) 254 return -EINVAL; 255 256 return 0; 257} 258 259static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) 260{ 261 struct soc_camera_device *icd = file->private_data; 262 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 263 264 return v4l2_subdev_call(sd, core, s_std, *a); 265} 266 267static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a) 268{ 269 struct soc_camera_device *icd = file->private_data; 270 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 271 272 return v4l2_subdev_call(sd, core, g_std, a); 273} 274 275static int soc_camera_enum_framesizes(struct file *file, void *fh, 276 struct v4l2_frmsizeenum *fsize) 277{ 278 struct soc_camera_device *icd = file->private_data; 279 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 280 281 return ici->ops->enum_framesizes(icd, fsize); 282} 283 284static int soc_camera_reqbufs(struct file *file, void *priv, 285 struct v4l2_requestbuffers *p) 286{ 287 int ret; 288 struct soc_camera_device *icd = file->private_data; 289 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 290 291 WARN_ON(priv != file->private_data); 292 293 if (icd->streamer && icd->streamer != file) 294 return -EBUSY; 295 296 if (ici->ops->init_videobuf) { 297 ret = videobuf_reqbufs(&icd->vb_vidq, p); 298 if (ret < 0) 299 return ret; 300 301 ret = ici->ops->reqbufs(icd, p); 302 } else { 303 ret = vb2_reqbufs(&icd->vb2_vidq, p); 304 } 305 306 if (!ret && !icd->streamer) 307 icd->streamer = file; 308 309 return ret; 310} 311 312static int soc_camera_querybuf(struct file *file, void *priv, 313 struct v4l2_buffer *p) 314{ 315 struct soc_camera_device *icd = file->private_data; 316 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 317 318 WARN_ON(priv != file->private_data); 319 320 if (ici->ops->init_videobuf) 321 return videobuf_querybuf(&icd->vb_vidq, p); 322 else 323 return vb2_querybuf(&icd->vb2_vidq, p); 324} 325 326static int soc_camera_qbuf(struct file *file, void *priv, 327 struct v4l2_buffer *p) 328{ 329 struct soc_camera_device *icd = file->private_data; 330 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 331 332 WARN_ON(priv != file->private_data); 333 334 if (icd->streamer != file) 335 return -EBUSY; 336 337 if (ici->ops->init_videobuf) 338 return videobuf_qbuf(&icd->vb_vidq, p); 339 else 340 return vb2_qbuf(&icd->vb2_vidq, p); 341} 342 343static int soc_camera_dqbuf(struct file *file, void *priv, 344 struct v4l2_buffer *p) 345{ 346 struct soc_camera_device *icd = file->private_data; 347 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 348 349 WARN_ON(priv != file->private_data); 350 351 if (icd->streamer != file) 352 return -EBUSY; 353 354 if (ici->ops->init_videobuf) 355 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK); 356 else 357 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK); 358} 359 360static int soc_camera_create_bufs(struct file *file, void *priv, 361 struct v4l2_create_buffers *create) 362{ 363 struct soc_camera_device *icd = file->private_data; 364 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 365 366 /* videobuf2 only */ 367 if (ici->ops->init_videobuf) 368 return -EINVAL; 369 else 370 return vb2_create_bufs(&icd->vb2_vidq, create); 371} 372 373static int soc_camera_prepare_buf(struct file *file, void *priv, 374 struct v4l2_buffer *b) 375{ 376 struct soc_camera_device *icd = file->private_data; 377 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 378 379 /* videobuf2 only */ 380 if (ici->ops->init_videobuf) 381 return -EINVAL; 382 else 383 return vb2_prepare_buf(&icd->vb2_vidq, b); 384} 385 386/* Always entered with .video_lock held */ 387static int soc_camera_init_user_formats(struct soc_camera_device *icd) 388{ 389 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 390 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 391 unsigned int i, fmts = 0, raw_fmts = 0; 392 int ret; 393 enum v4l2_mbus_pixelcode code; 394 395 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code)) 396 raw_fmts++; 397 398 if (!ici->ops->get_formats) 399 /* 400 * Fallback mode - the host will have to serve all 401 * sensor-provided formats one-to-one to the user 402 */ 403 fmts = raw_fmts; 404 else 405 /* 406 * First pass - only count formats this host-sensor 407 * configuration can provide 408 */ 409 for (i = 0; i < raw_fmts; i++) { 410 ret = ici->ops->get_formats(icd, i, NULL); 411 if (ret < 0) 412 return ret; 413 fmts += ret; 414 } 415 416 if (!fmts) 417 return -ENXIO; 418 419 icd->user_formats = 420 vmalloc(fmts * sizeof(struct soc_camera_format_xlate)); 421 if (!icd->user_formats) 422 return -ENOMEM; 423 424 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts); 425 426 /* Second pass - actually fill data formats */ 427 fmts = 0; 428 for (i = 0; i < raw_fmts; i++) 429 if (!ici->ops->get_formats) { 430 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code); 431 icd->user_formats[fmts].host_fmt = 432 soc_mbus_get_fmtdesc(code); 433 if (icd->user_formats[fmts].host_fmt) 434 icd->user_formats[fmts++].code = code; 435 } else { 436 ret = ici->ops->get_formats(icd, i, 437 &icd->user_formats[fmts]); 438 if (ret < 0) 439 goto egfmt; 440 fmts += ret; 441 } 442 443 icd->num_user_formats = fmts; 444 icd->current_fmt = &icd->user_formats[0]; 445 446 return 0; 447 448egfmt: 449 vfree(icd->user_formats); 450 return ret; 451} 452 453/* Always entered with .video_lock held */ 454static void soc_camera_free_user_formats(struct soc_camera_device *icd) 455{ 456 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 457 458 if (ici->ops->put_formats) 459 ici->ops->put_formats(icd); 460 icd->current_fmt = NULL; 461 icd->num_user_formats = 0; 462 vfree(icd->user_formats); 463 icd->user_formats = NULL; 464} 465 466/* Called with .vb_lock held, or from the first open(2), see comment there */ 467static int soc_camera_set_fmt(struct soc_camera_device *icd, 468 struct v4l2_format *f) 469{ 470 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 471 struct v4l2_pix_format *pix = &f->fmt.pix; 472 int ret; 473 474 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n", 475 pixfmtstr(pix->pixelformat), pix->width, pix->height); 476 477 /* We always call try_fmt() before set_fmt() or set_crop() */ 478 ret = soc_camera_try_fmt(icd, f); 479 if (ret < 0) 480 return ret; 481 482 ret = ici->ops->set_fmt(icd, f); 483 if (ret < 0) { 484 return ret; 485 } else if (!icd->current_fmt || 486 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) { 487 dev_err(icd->pdev, 488 "Host driver hasn't set up current format correctly!\n"); 489 return -EINVAL; 490 } 491 492 icd->user_width = pix->width; 493 icd->user_height = pix->height; 494 icd->bytesperline = pix->bytesperline; 495 icd->sizeimage = pix->sizeimage; 496 icd->colorspace = pix->colorspace; 497 icd->field = pix->field; 498 if (ici->ops->init_videobuf) 499 icd->vb_vidq.field = pix->field; 500 501 dev_dbg(icd->pdev, "set width: %d height: %d\n", 502 icd->user_width, icd->user_height); 503 504 /* set physical bus parameters */ 505 return ici->ops->set_bus_param(icd); 506} 507 508static int soc_camera_open(struct file *file) 509{ 510 struct video_device *vdev = video_devdata(file); 511 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent); 512 struct soc_camera_link *icl = to_soc_camera_link(icd); 513 struct soc_camera_host *ici; 514 int ret; 515 516 if (!to_soc_camera_control(icd)) 517 /* No device driver attached */ 518 return -ENODEV; 519 520 ici = to_soc_camera_host(icd->parent); 521 522 if (mutex_lock_interruptible(&icd->video_lock)) 523 return -ERESTARTSYS; 524 if (!try_module_get(ici->ops->owner)) { 525 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n"); 526 ret = -EINVAL; 527 goto emodule; 528 } 529 530 icd->use_count++; 531 532 /* Now we really have to activate the camera */ 533 if (icd->use_count == 1) { 534 /* Restore parameters before the last close() per V4L2 API */ 535 struct v4l2_format f = { 536 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 537 .fmt.pix = { 538 .width = icd->user_width, 539 .height = icd->user_height, 540 .field = icd->field, 541 .colorspace = icd->colorspace, 542 .pixelformat = 543 icd->current_fmt->host_fmt->fourcc, 544 }, 545 }; 546 547 /* The camera could have been already on, try to reset */ 548 if (icl->reset) 549 icl->reset(icd->pdev); 550 551 /* Don't mess with the host during probe */ 552 mutex_lock(&ici->host_lock); 553 ret = ici->ops->add(icd); 554 mutex_unlock(&ici->host_lock); 555 if (ret < 0) { 556 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret); 557 goto eiciadd; 558 } 559 560 ret = __soc_camera_power_on(icd); 561 if (ret < 0) 562 goto epower; 563 564 pm_runtime_enable(&icd->vdev->dev); 565 ret = pm_runtime_resume(&icd->vdev->dev); 566 if (ret < 0 && ret != -ENOSYS) 567 goto eresume; 568 569 /* 570 * Try to configure with default parameters. Notice: this is the 571 * very first open, so, we cannot race against other calls, 572 * apart from someone else calling open() simultaneously, but 573 * .video_lock is protecting us against it. 574 */ 575 ret = soc_camera_set_fmt(icd, &f); 576 if (ret < 0) 577 goto esfmt; 578 579 if (ici->ops->init_videobuf) { 580 ici->ops->init_videobuf(&icd->vb_vidq, icd); 581 } else { 582 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd); 583 if (ret < 0) 584 goto einitvb; 585 } 586 v4l2_ctrl_handler_setup(&icd->ctrl_handler); 587 } 588 mutex_unlock(&icd->video_lock); 589 590 file->private_data = icd; 591 dev_dbg(icd->pdev, "camera device open\n"); 592 593 return 0; 594 595 /* 596 * First four errors are entered with the .video_lock held 597 * and use_count == 1 598 */ 599einitvb: 600esfmt: 601 pm_runtime_disable(&icd->vdev->dev); 602eresume: 603 __soc_camera_power_off(icd); 604epower: 605 ici->ops->remove(icd); 606eiciadd: 607 icd->use_count--; 608 module_put(ici->ops->owner); 609emodule: 610 mutex_unlock(&icd->video_lock); 611 612 return ret; 613} 614 615static int soc_camera_close(struct file *file) 616{ 617 struct soc_camera_device *icd = file->private_data; 618 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 619 620 mutex_lock(&icd->video_lock); 621 icd->use_count--; 622 if (!icd->use_count) { 623 pm_runtime_suspend(&icd->vdev->dev); 624 pm_runtime_disable(&icd->vdev->dev); 625 626 if (ici->ops->init_videobuf2) 627 vb2_queue_release(&icd->vb2_vidq); 628 ici->ops->remove(icd); 629 630 __soc_camera_power_off(icd); 631 } 632 633 if (icd->streamer == file) 634 icd->streamer = NULL; 635 mutex_unlock(&icd->video_lock); 636 637 module_put(ici->ops->owner); 638 639 dev_dbg(icd->pdev, "camera device close\n"); 640 641 return 0; 642} 643 644static ssize_t soc_camera_read(struct file *file, char __user *buf, 645 size_t count, loff_t *ppos) 646{ 647 struct soc_camera_device *icd = file->private_data; 648 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 649 650 dev_dbg(icd->pdev, "read called, buf %p\n", buf); 651 652 if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ) 653 return vb2_read(&icd->vb2_vidq, buf, count, ppos, 654 file->f_flags & O_NONBLOCK); 655 656 dev_err(icd->pdev, "camera device read not implemented\n"); 657 658 return -EINVAL; 659} 660 661static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) 662{ 663 struct soc_camera_device *icd = file->private_data; 664 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 665 int err; 666 667 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); 668 669 if (icd->streamer != file) 670 return -EBUSY; 671 672 if (mutex_lock_interruptible(&icd->video_lock)) 673 return -ERESTARTSYS; 674 if (ici->ops->init_videobuf) 675 err = videobuf_mmap_mapper(&icd->vb_vidq, vma); 676 else 677 err = vb2_mmap(&icd->vb2_vidq, vma); 678 mutex_unlock(&icd->video_lock); 679 680 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n", 681 (unsigned long)vma->vm_start, 682 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, 683 err); 684 685 return err; 686} 687 688static unsigned int soc_camera_poll(struct file *file, poll_table *pt) 689{ 690 struct soc_camera_device *icd = file->private_data; 691 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 692 unsigned res = POLLERR; 693 694 if (icd->streamer != file) 695 return POLLERR; 696 697 mutex_lock(&icd->video_lock); 698 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) 699 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n"); 700 else 701 res = ici->ops->poll(file, pt); 702 mutex_unlock(&icd->video_lock); 703 return res; 704} 705 706void soc_camera_lock(struct vb2_queue *vq) 707{ 708 struct soc_camera_device *icd = vb2_get_drv_priv(vq); 709 mutex_lock(&icd->video_lock); 710} 711EXPORT_SYMBOL(soc_camera_lock); 712 713void soc_camera_unlock(struct vb2_queue *vq) 714{ 715 struct soc_camera_device *icd = vb2_get_drv_priv(vq); 716 mutex_unlock(&icd->video_lock); 717} 718EXPORT_SYMBOL(soc_camera_unlock); 719 720static struct v4l2_file_operations soc_camera_fops = { 721 .owner = THIS_MODULE, 722 .open = soc_camera_open, 723 .release = soc_camera_close, 724 .unlocked_ioctl = video_ioctl2, 725 .read = soc_camera_read, 726 .mmap = soc_camera_mmap, 727 .poll = soc_camera_poll, 728}; 729 730static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv, 731 struct v4l2_format *f) 732{ 733 struct soc_camera_device *icd = file->private_data; 734 int ret; 735 736 WARN_ON(priv != file->private_data); 737 738 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { 739 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type); 740 return -EINVAL; 741 } 742 743 if (icd->streamer && icd->streamer != file) 744 return -EBUSY; 745 746 if (is_streaming(to_soc_camera_host(icd->parent), icd)) { 747 dev_err(icd->pdev, "S_FMT denied: queue initialised\n"); 748 return -EBUSY; 749 } 750 751 ret = soc_camera_set_fmt(icd, f); 752 753 if (!ret && !icd->streamer) 754 icd->streamer = file; 755 756 return ret; 757} 758 759static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv, 760 struct v4l2_fmtdesc *f) 761{ 762 struct soc_camera_device *icd = file->private_data; 763 const struct soc_mbus_pixelfmt *format; 764 765 WARN_ON(priv != file->private_data); 766 767 if (f->index >= icd->num_user_formats) 768 return -EINVAL; 769 770 format = icd->user_formats[f->index].host_fmt; 771 772 if (format->name) 773 strlcpy(f->description, format->name, sizeof(f->description)); 774 f->pixelformat = format->fourcc; 775 return 0; 776} 777 778static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv, 779 struct v4l2_format *f) 780{ 781 struct soc_camera_device *icd = file->private_data; 782 struct v4l2_pix_format *pix = &f->fmt.pix; 783 784 WARN_ON(priv != file->private_data); 785 786 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 787 return -EINVAL; 788 789 pix->width = icd->user_width; 790 pix->height = icd->user_height; 791 pix->bytesperline = icd->bytesperline; 792 pix->sizeimage = icd->sizeimage; 793 pix->field = icd->field; 794 pix->pixelformat = icd->current_fmt->host_fmt->fourcc; 795 pix->colorspace = icd->colorspace; 796 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n", 797 icd->current_fmt->host_fmt->fourcc); 798 return 0; 799} 800 801static int soc_camera_querycap(struct file *file, void *priv, 802 struct v4l2_capability *cap) 803{ 804 struct soc_camera_device *icd = file->private_data; 805 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 806 807 WARN_ON(priv != file->private_data); 808 809 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); 810 return ici->ops->querycap(ici, cap); 811} 812 813static int soc_camera_streamon(struct file *file, void *priv, 814 enum v4l2_buf_type i) 815{ 816 struct soc_camera_device *icd = file->private_data; 817 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 818 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 819 int ret; 820 821 WARN_ON(priv != file->private_data); 822 823 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) 824 return -EINVAL; 825 826 if (icd->streamer != file) 827 return -EBUSY; 828 829 /* This calls buf_queue from host driver's videobuf_queue_ops */ 830 if (ici->ops->init_videobuf) 831 ret = videobuf_streamon(&icd->vb_vidq); 832 else 833 ret = vb2_streamon(&icd->vb2_vidq, i); 834 835 if (!ret) 836 v4l2_subdev_call(sd, video, s_stream, 1); 837 838 return ret; 839} 840 841static int soc_camera_streamoff(struct file *file, void *priv, 842 enum v4l2_buf_type i) 843{ 844 struct soc_camera_device *icd = file->private_data; 845 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 846 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 847 848 WARN_ON(priv != file->private_data); 849 850 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) 851 return -EINVAL; 852 853 if (icd->streamer != file) 854 return -EBUSY; 855 856 /* 857 * This calls buf_release from host driver's videobuf_queue_ops for all 858 * remaining buffers. When the last buffer is freed, stop capture 859 */ 860 if (ici->ops->init_videobuf) 861 videobuf_streamoff(&icd->vb_vidq); 862 else 863 vb2_streamoff(&icd->vb2_vidq, i); 864 865 v4l2_subdev_call(sd, video, s_stream, 0); 866 867 return 0; 868} 869 870static int soc_camera_cropcap(struct file *file, void *fh, 871 struct v4l2_cropcap *a) 872{ 873 struct soc_camera_device *icd = file->private_data; 874 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 875 876 return ici->ops->cropcap(icd, a); 877} 878 879static int soc_camera_g_crop(struct file *file, void *fh, 880 struct v4l2_crop *a) 881{ 882 struct soc_camera_device *icd = file->private_data; 883 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 884 int ret; 885 886 ret = ici->ops->get_crop(icd, a); 887 888 return ret; 889} 890 891/* 892 * According to the V4L2 API, drivers shall not update the struct v4l2_crop 893 * argument with the actual geometry, instead, the user shall use G_CROP to 894 * retrieve it. 895 */ 896static int soc_camera_s_crop(struct file *file, void *fh, 897 const struct v4l2_crop *a) 898{ 899 struct soc_camera_device *icd = file->private_data; 900 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 901 const struct v4l2_rect *rect = &a->c; 902 struct v4l2_crop current_crop; 903 int ret; 904 905 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 906 return -EINVAL; 907 908 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n", 909 rect->width, rect->height, rect->left, rect->top); 910 911 /* If get_crop fails, we'll let host and / or client drivers decide */ 912 ret = ici->ops->get_crop(icd, &current_crop); 913 914 /* Prohibit window size change with initialised buffers */ 915 if (ret < 0) { 916 dev_err(icd->pdev, 917 "S_CROP denied: getting current crop failed\n"); 918 } else if ((a->c.width == current_crop.c.width && 919 a->c.height == current_crop.c.height) || 920 !is_streaming(ici, icd)) { 921 /* same size or not streaming - use .set_crop() */ 922 ret = ici->ops->set_crop(icd, a); 923 } else if (ici->ops->set_livecrop) { 924 ret = ici->ops->set_livecrop(icd, a); 925 } else { 926 dev_err(icd->pdev, 927 "S_CROP denied: queue initialised and sizes differ\n"); 928 ret = -EBUSY; 929 } 930 931 return ret; 932} 933 934static int soc_camera_g_selection(struct file *file, void *fh, 935 struct v4l2_selection *s) 936{ 937 struct soc_camera_device *icd = file->private_data; 938 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 939 940 /* With a wrong type no need to try to fall back to cropping */ 941 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 942 return -EINVAL; 943 944 if (!ici->ops->get_selection) 945 return -ENOTTY; 946 947 return ici->ops->get_selection(icd, s); 948} 949 950static int soc_camera_s_selection(struct file *file, void *fh, 951 struct v4l2_selection *s) 952{ 953 struct soc_camera_device *icd = file->private_data; 954 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 955 int ret; 956 957 /* In all these cases cropping emulation will not help */ 958 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 959 (s->target != V4L2_SEL_TGT_COMPOSE && 960 s->target != V4L2_SEL_TGT_CROP)) 961 return -EINVAL; 962 963 if (s->target == V4L2_SEL_TGT_COMPOSE) { 964 /* No output size change during a running capture! */ 965 if (is_streaming(ici, icd) && 966 (icd->user_width != s->r.width || 967 icd->user_height != s->r.height)) 968 return -EBUSY; 969 970 /* 971 * Only one user is allowed to change the output format, touch 972 * buffers, start / stop streaming, poll for data 973 */ 974 if (icd->streamer && icd->streamer != file) 975 return -EBUSY; 976 } 977 978 if (!ici->ops->set_selection) 979 return -ENOTTY; 980 981 ret = ici->ops->set_selection(icd, s); 982 if (!ret && 983 s->target == V4L2_SEL_TGT_COMPOSE) { 984 icd->user_width = s->r.width; 985 icd->user_height = s->r.height; 986 if (!icd->streamer) 987 icd->streamer = file; 988 } 989 990 return ret; 991} 992 993static int soc_camera_g_parm(struct file *file, void *fh, 994 struct v4l2_streamparm *a) 995{ 996 struct soc_camera_device *icd = file->private_data; 997 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 998 999 if (ici->ops->get_parm) 1000 return ici->ops->get_parm(icd, a); 1001 1002 return -ENOIOCTLCMD; 1003} 1004 1005static int soc_camera_s_parm(struct file *file, void *fh, 1006 struct v4l2_streamparm *a) 1007{ 1008 struct soc_camera_device *icd = file->private_data; 1009 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1010 1011 if (ici->ops->set_parm) 1012 return ici->ops->set_parm(icd, a); 1013 1014 return -ENOIOCTLCMD; 1015} 1016 1017static int soc_camera_g_chip_ident(struct file *file, void *fh, 1018 struct v4l2_dbg_chip_ident *id) 1019{ 1020 struct soc_camera_device *icd = file->private_data; 1021 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1022 1023 return v4l2_subdev_call(sd, core, g_chip_ident, id); 1024} 1025 1026#ifdef CONFIG_VIDEO_ADV_DEBUG 1027static int soc_camera_g_register(struct file *file, void *fh, 1028 struct v4l2_dbg_register *reg) 1029{ 1030 struct soc_camera_device *icd = file->private_data; 1031 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1032 1033 return v4l2_subdev_call(sd, core, g_register, reg); 1034} 1035 1036static int soc_camera_s_register(struct file *file, void *fh, 1037 struct v4l2_dbg_register *reg) 1038{ 1039 struct soc_camera_device *icd = file->private_data; 1040 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1041 1042 return v4l2_subdev_call(sd, core, s_register, reg); 1043} 1044#endif 1045 1046static int soc_camera_probe(struct soc_camera_device *icd); 1047 1048/* So far this function cannot fail */ 1049static void scan_add_host(struct soc_camera_host *ici) 1050{ 1051 struct soc_camera_device *icd; 1052 1053 mutex_lock(&ici->host_lock); 1054 1055 list_for_each_entry(icd, &devices, list) { 1056 if (icd->iface == ici->nr) { 1057 icd->parent = ici->v4l2_dev.dev; 1058 soc_camera_probe(icd); 1059 } 1060 } 1061 1062 mutex_unlock(&ici->host_lock); 1063} 1064 1065#ifdef CONFIG_I2C_BOARDINFO 1066static int soc_camera_init_i2c(struct soc_camera_device *icd, 1067 struct soc_camera_link *icl) 1068{ 1069 struct i2c_client *client; 1070 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1071 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id); 1072 struct v4l2_subdev *subdev; 1073 1074 if (!adap) { 1075 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n", 1076 icl->i2c_adapter_id); 1077 goto ei2cga; 1078 } 1079 1080 icl->board_info->platform_data = icl; 1081 1082 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, 1083 icl->board_info, NULL); 1084 if (!subdev) 1085 goto ei2cnd; 1086 1087 client = v4l2_get_subdevdata(subdev); 1088 1089 /* Use to_i2c_client(dev) to recover the i2c client */ 1090 icd->control = &client->dev; 1091 1092 return 0; 1093ei2cnd: 1094 i2c_put_adapter(adap); 1095ei2cga: 1096 return -ENODEV; 1097} 1098 1099static void soc_camera_free_i2c(struct soc_camera_device *icd) 1100{ 1101 struct i2c_client *client = 1102 to_i2c_client(to_soc_camera_control(icd)); 1103 struct i2c_adapter *adap = client->adapter; 1104 1105 icd->control = NULL; 1106 v4l2_device_unregister_subdev(i2c_get_clientdata(client)); 1107 i2c_unregister_device(client); 1108 i2c_put_adapter(adap); 1109} 1110#else 1111#define soc_camera_init_i2c(icd, icl) (-ENODEV) 1112#define soc_camera_free_i2c(icd) do {} while (0) 1113#endif 1114 1115static int soc_camera_video_start(struct soc_camera_device *icd); 1116static int video_dev_create(struct soc_camera_device *icd); 1117/* Called during host-driver probe */ 1118static int soc_camera_probe(struct soc_camera_device *icd) 1119{ 1120 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1121 struct soc_camera_link *icl = to_soc_camera_link(icd); 1122 struct device *control = NULL; 1123 struct v4l2_subdev *sd; 1124 struct v4l2_mbus_framefmt mf; 1125 int ret; 1126 1127 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev)); 1128 1129 /* 1130 * Currently the subdev with the largest number of controls (13) is 1131 * ov6550. So let's pick 16 as a hint for the control handler. Note 1132 * that this is a hint only: too large and you waste some memory, too 1133 * small and there is a (very) small performance hit when looking up 1134 * controls in the internal hash. 1135 */ 1136 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16); 1137 if (ret < 0) 1138 return ret; 1139 1140 ret = regulator_bulk_get(icd->pdev, icl->num_regulators, 1141 icl->regulators); 1142 if (ret < 0) 1143 goto ereg; 1144 1145 /* The camera could have been already on, try to reset */ 1146 if (icl->reset) 1147 icl->reset(icd->pdev); 1148 1149 ret = ici->ops->add(icd); 1150 if (ret < 0) 1151 goto eadd; 1152 1153 /* Must have icd->vdev before registering the device */ 1154 ret = video_dev_create(icd); 1155 if (ret < 0) 1156 goto evdc; 1157 1158 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ 1159 if (icl->board_info) { 1160 ret = soc_camera_init_i2c(icd, icl); 1161 if (ret < 0) 1162 goto eadddev; 1163 } else if (!icl->add_device || !icl->del_device) { 1164 ret = -EINVAL; 1165 goto eadddev; 1166 } else { 1167 if (icl->module_name) 1168 ret = request_module(icl->module_name); 1169 1170 ret = icl->add_device(icd); 1171 if (ret < 0) 1172 goto eadddev; 1173 1174 /* 1175 * FIXME: this is racy, have to use driver-binding notification, 1176 * when it is available 1177 */ 1178 control = to_soc_camera_control(icd); 1179 if (!control || !control->driver || !dev_get_drvdata(control) || 1180 !try_module_get(control->driver->owner)) { 1181 icl->del_device(icd); 1182 ret = -ENODEV; 1183 goto enodrv; 1184 } 1185 } 1186 1187 sd = soc_camera_to_subdev(icd); 1188 sd->grp_id = soc_camera_grp_id(icd); 1189 v4l2_set_subdev_hostdata(sd, icd); 1190 1191 ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL); 1192 if (ret < 0) 1193 goto ectrl; 1194 1195 /* At this point client .probe() should have run already */ 1196 ret = soc_camera_init_user_formats(icd); 1197 if (ret < 0) 1198 goto eiufmt; 1199 1200 icd->field = V4L2_FIELD_ANY; 1201 1202 /* 1203 * ..._video_start() will create a device node, video_register_device() 1204 * itself is protected against concurrent open() calls, but we also have 1205 * to protect our data. 1206 */ 1207 mutex_lock(&icd->video_lock); 1208 1209 ret = soc_camera_video_start(icd); 1210 if (ret < 0) 1211 goto evidstart; 1212 1213 /* Try to improve our guess of a reasonable window format */ 1214 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { 1215 icd->user_width = mf.width; 1216 icd->user_height = mf.height; 1217 icd->colorspace = mf.colorspace; 1218 icd->field = mf.field; 1219 } 1220 1221 ici->ops->remove(icd); 1222 1223 mutex_unlock(&icd->video_lock); 1224 1225 return 0; 1226 1227evidstart: 1228 mutex_unlock(&icd->video_lock); 1229 soc_camera_free_user_formats(icd); 1230eiufmt: 1231ectrl: 1232 if (icl->board_info) { 1233 soc_camera_free_i2c(icd); 1234 } else { 1235 icl->del_device(icd); 1236 module_put(control->driver->owner); 1237 } 1238enodrv: 1239eadddev: 1240 video_device_release(icd->vdev); 1241 icd->vdev = NULL; 1242evdc: 1243 ici->ops->remove(icd); 1244eadd: 1245 regulator_bulk_free(icl->num_regulators, icl->regulators); 1246ereg: 1247 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1248 return ret; 1249} 1250 1251/* 1252 * This is called on device_unregister, which only means we have to disconnect 1253 * from the host, but not remove ourselves from the device list 1254 */ 1255static int soc_camera_remove(struct soc_camera_device *icd) 1256{ 1257 struct soc_camera_link *icl = to_soc_camera_link(icd); 1258 struct video_device *vdev = icd->vdev; 1259 1260 BUG_ON(!icd->parent); 1261 1262 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1263 if (vdev) { 1264 video_unregister_device(vdev); 1265 icd->vdev = NULL; 1266 } 1267 1268 if (icl->board_info) { 1269 soc_camera_free_i2c(icd); 1270 } else { 1271 struct device_driver *drv = to_soc_camera_control(icd)->driver; 1272 if (drv) { 1273 icl->del_device(icd); 1274 module_put(drv->owner); 1275 } 1276 } 1277 soc_camera_free_user_formats(icd); 1278 1279 regulator_bulk_free(icl->num_regulators, icl->regulators); 1280 1281 return 0; 1282} 1283 1284static int default_cropcap(struct soc_camera_device *icd, 1285 struct v4l2_cropcap *a) 1286{ 1287 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1288 return v4l2_subdev_call(sd, video, cropcap, a); 1289} 1290 1291static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a) 1292{ 1293 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1294 return v4l2_subdev_call(sd, video, g_crop, a); 1295} 1296 1297static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a) 1298{ 1299 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1300 return v4l2_subdev_call(sd, video, s_crop, a); 1301} 1302 1303static int default_g_parm(struct soc_camera_device *icd, 1304 struct v4l2_streamparm *parm) 1305{ 1306 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1307 return v4l2_subdev_call(sd, video, g_parm, parm); 1308} 1309 1310static int default_s_parm(struct soc_camera_device *icd, 1311 struct v4l2_streamparm *parm) 1312{ 1313 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1314 return v4l2_subdev_call(sd, video, s_parm, parm); 1315} 1316 1317static int default_enum_framesizes(struct soc_camera_device *icd, 1318 struct v4l2_frmsizeenum *fsize) 1319{ 1320 int ret; 1321 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1322 const struct soc_camera_format_xlate *xlate; 1323 __u32 pixfmt = fsize->pixel_format; 1324 struct v4l2_frmsizeenum fsize_mbus = *fsize; 1325 1326 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); 1327 if (!xlate) 1328 return -EINVAL; 1329 /* map xlate-code to pixel_format, sensor only handle xlate-code*/ 1330 fsize_mbus.pixel_format = xlate->code; 1331 1332 ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus); 1333 if (ret < 0) 1334 return ret; 1335 1336 *fsize = fsize_mbus; 1337 fsize->pixel_format = pixfmt; 1338 1339 return 0; 1340} 1341 1342int soc_camera_host_register(struct soc_camera_host *ici) 1343{ 1344 struct soc_camera_host *ix; 1345 int ret; 1346 1347 if (!ici || !ici->ops || 1348 !ici->ops->try_fmt || 1349 !ici->ops->set_fmt || 1350 !ici->ops->set_bus_param || 1351 !ici->ops->querycap || 1352 ((!ici->ops->init_videobuf || 1353 !ici->ops->reqbufs) && 1354 !ici->ops->init_videobuf2) || 1355 !ici->ops->add || 1356 !ici->ops->remove || 1357 !ici->ops->poll || 1358 !ici->v4l2_dev.dev) 1359 return -EINVAL; 1360 1361 if (!ici->ops->set_crop) 1362 ici->ops->set_crop = default_s_crop; 1363 if (!ici->ops->get_crop) 1364 ici->ops->get_crop = default_g_crop; 1365 if (!ici->ops->cropcap) 1366 ici->ops->cropcap = default_cropcap; 1367 if (!ici->ops->set_parm) 1368 ici->ops->set_parm = default_s_parm; 1369 if (!ici->ops->get_parm) 1370 ici->ops->get_parm = default_g_parm; 1371 if (!ici->ops->enum_framesizes) 1372 ici->ops->enum_framesizes = default_enum_framesizes; 1373 1374 mutex_lock(&list_lock); 1375 list_for_each_entry(ix, &hosts, list) { 1376 if (ix->nr == ici->nr) { 1377 ret = -EBUSY; 1378 goto edevreg; 1379 } 1380 } 1381 1382 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); 1383 if (ret < 0) 1384 goto edevreg; 1385 1386 list_add_tail(&ici->list, &hosts); 1387 mutex_unlock(&list_lock); 1388 1389 mutex_init(&ici->host_lock); 1390 scan_add_host(ici); 1391 1392 return 0; 1393 1394edevreg: 1395 mutex_unlock(&list_lock); 1396 return ret; 1397} 1398EXPORT_SYMBOL(soc_camera_host_register); 1399 1400/* Unregister all clients! */ 1401void soc_camera_host_unregister(struct soc_camera_host *ici) 1402{ 1403 struct soc_camera_device *icd; 1404 1405 mutex_lock(&list_lock); 1406 1407 list_del(&ici->list); 1408 list_for_each_entry(icd, &devices, list) 1409 if (icd->iface == ici->nr && to_soc_camera_control(icd)) 1410 soc_camera_remove(icd); 1411 1412 mutex_unlock(&list_lock); 1413 1414 v4l2_device_unregister(&ici->v4l2_dev); 1415} 1416EXPORT_SYMBOL(soc_camera_host_unregister); 1417 1418/* Image capture device */ 1419static int soc_camera_device_register(struct soc_camera_device *icd) 1420{ 1421 struct soc_camera_device *ix; 1422 int num = -1, i; 1423 1424 for (i = 0; i < 256 && num < 0; i++) { 1425 num = i; 1426 /* Check if this index is available on this interface */ 1427 list_for_each_entry(ix, &devices, list) { 1428 if (ix->iface == icd->iface && ix->devnum == i) { 1429 num = -1; 1430 break; 1431 } 1432 } 1433 } 1434 1435 if (num < 0) 1436 /* 1437 * ok, we have 256 cameras on this host... 1438 * man, stay reasonable... 1439 */ 1440 return -ENOMEM; 1441 1442 icd->devnum = num; 1443 icd->use_count = 0; 1444 icd->host_priv = NULL; 1445 mutex_init(&icd->video_lock); 1446 1447 list_add_tail(&icd->list, &devices); 1448 1449 return 0; 1450} 1451 1452static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { 1453 .vidioc_querycap = soc_camera_querycap, 1454 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, 1455 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, 1456 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, 1457 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, 1458 .vidioc_enum_input = soc_camera_enum_input, 1459 .vidioc_g_input = soc_camera_g_input, 1460 .vidioc_s_input = soc_camera_s_input, 1461 .vidioc_s_std = soc_camera_s_std, 1462 .vidioc_g_std = soc_camera_g_std, 1463 .vidioc_enum_framesizes = soc_camera_enum_framesizes, 1464 .vidioc_reqbufs = soc_camera_reqbufs, 1465 .vidioc_querybuf = soc_camera_querybuf, 1466 .vidioc_qbuf = soc_camera_qbuf, 1467 .vidioc_dqbuf = soc_camera_dqbuf, 1468 .vidioc_create_bufs = soc_camera_create_bufs, 1469 .vidioc_prepare_buf = soc_camera_prepare_buf, 1470 .vidioc_streamon = soc_camera_streamon, 1471 .vidioc_streamoff = soc_camera_streamoff, 1472 .vidioc_cropcap = soc_camera_cropcap, 1473 .vidioc_g_crop = soc_camera_g_crop, 1474 .vidioc_s_crop = soc_camera_s_crop, 1475 .vidioc_g_selection = soc_camera_g_selection, 1476 .vidioc_s_selection = soc_camera_s_selection, 1477 .vidioc_g_parm = soc_camera_g_parm, 1478 .vidioc_s_parm = soc_camera_s_parm, 1479 .vidioc_g_chip_ident = soc_camera_g_chip_ident, 1480#ifdef CONFIG_VIDEO_ADV_DEBUG 1481 .vidioc_g_register = soc_camera_g_register, 1482 .vidioc_s_register = soc_camera_s_register, 1483#endif 1484}; 1485 1486static int video_dev_create(struct soc_camera_device *icd) 1487{ 1488 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1489 struct video_device *vdev = video_device_alloc(); 1490 1491 if (!vdev) 1492 return -ENOMEM; 1493 1494 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); 1495 1496 vdev->parent = icd->pdev; 1497 vdev->current_norm = V4L2_STD_UNKNOWN; 1498 vdev->fops = &soc_camera_fops; 1499 vdev->ioctl_ops = &soc_camera_ioctl_ops; 1500 vdev->release = video_device_release; 1501 vdev->tvnorms = V4L2_STD_UNKNOWN; 1502 vdev->ctrl_handler = &icd->ctrl_handler; 1503 vdev->lock = &icd->video_lock; 1504 1505 icd->vdev = vdev; 1506 1507 return 0; 1508} 1509 1510/* 1511 * Called from soc_camera_probe() above (with .video_lock held???) 1512 */ 1513static int soc_camera_video_start(struct soc_camera_device *icd) 1514{ 1515 const struct device_type *type = icd->vdev->dev.type; 1516 int ret; 1517 1518 if (!icd->parent) 1519 return -ENODEV; 1520 1521 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1); 1522 if (ret < 0) { 1523 dev_err(icd->pdev, "video_register_device failed: %d\n", ret); 1524 return ret; 1525 } 1526 1527 /* Restore device type, possibly set by the subdevice driver */ 1528 icd->vdev->dev.type = type; 1529 1530 return 0; 1531} 1532 1533static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev) 1534{ 1535 struct soc_camera_link *icl = pdev->dev.platform_data; 1536 struct soc_camera_device *icd; 1537 1538 if (!icl) 1539 return -EINVAL; 1540 1541 icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL); 1542 if (!icd) 1543 return -ENOMEM; 1544 1545 icd->iface = icl->bus_id; 1546 icd->link = icl; 1547 icd->pdev = &pdev->dev; 1548 platform_set_drvdata(pdev, icd); 1549 1550 icd->user_width = DEFAULT_WIDTH; 1551 icd->user_height = DEFAULT_HEIGHT; 1552 1553 return soc_camera_device_register(icd); 1554} 1555 1556/* 1557 * Only called on rmmod for each platform device, since they are not 1558 * hot-pluggable. Now we know, that all our users - hosts and devices have 1559 * been unloaded already 1560 */ 1561static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev) 1562{ 1563 struct soc_camera_device *icd = platform_get_drvdata(pdev); 1564 1565 if (!icd) 1566 return -EINVAL; 1567 1568 list_del(&icd->list); 1569 1570 return 0; 1571} 1572 1573static struct platform_driver __refdata soc_camera_pdrv = { 1574 .probe = soc_camera_pdrv_probe, 1575 .remove = __devexit_p(soc_camera_pdrv_remove), 1576 .driver = { 1577 .name = "soc-camera-pdrv", 1578 .owner = THIS_MODULE, 1579 }, 1580}; 1581 1582module_platform_driver(soc_camera_pdrv); 1583 1584MODULE_DESCRIPTION("Image capture bus driver"); 1585MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); 1586MODULE_LICENSE("GPL"); 1587MODULE_ALIAS("platform:soc-camera-pdrv");