soc_camera.c 38.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * camera image capture (abstract) bus driver
 *
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * This driver provides an interface between platform-specific camera
 * busses and camera devices. It should be used if the camera is
 * connected not over a "proper" bus like PCI or USB, but over a
 * special bus, like, for example, the Quick Capture interface on PXA270
 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
 * It can handle multiple cameras and / or multiple busses, which can
 * be used, e.g., in stereo-vision applications.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
#include <linux/err.h>
21 22 23
#include <linux/i2c.h>
#include <linux/init.h>
#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 31
#include <linux/vmalloc.h>

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 41 42 43
/* Default to VGA resolution */
#define DEFAULT_WIDTH	640
#define DEFAULT_HEIGHT	480

44 45 46 47 48
#define is_streaming(ici, icd)				\
	(((ici)->ops->init_videobuf) ?			\
	 (icd)->vb_vidq.streaming :			\
	 vb2_is_streaming(&(icd)->vb2_vidq))

49 50
static LIST_HEAD(hosts);
static LIST_HEAD(devices);
51
static DEFINE_MUTEX(list_lock);		/* Protects the list of hosts */
52

53 54
static int soc_camera_power_on(struct soc_camera_device *icd,
			       struct soc_camera_link *icl)
55
{
56 57 58
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	int ret = regulator_bulk_enable(icl->num_regulators,
					icl->regulators);
59 60 61 62
	if (ret < 0) {
		dev_err(icd->pdev, "Cannot enable regulators\n");
		return ret;
	}
63

64 65
	if (icl->power) {
		ret = icl->power(icd->pdev, 1);
66
		if (ret < 0) {
67
			dev_err(icd->pdev,
68
				"Platform failed to power-on the camera.\n");
69
			goto elinkpwr;
70
		}
71 72
	}

73 74 75 76 77 78 79 80 81 82 83 84
	ret = v4l2_subdev_call(sd, core, s_power, 1);
	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
		goto esdpwr;

	return 0;

esdpwr:
	if (icl->power)
		icl->power(icd->pdev, 0);
elinkpwr:
	regulator_bulk_disable(icl->num_regulators,
			       icl->regulators);
85 86 87 88 89 90
	return ret;
}

static int soc_camera_power_off(struct soc_camera_device *icd,
				struct soc_camera_link *icl)
{
91 92 93 94 95
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	int ret = v4l2_subdev_call(sd, core, s_power, 0);

	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
		return ret;
96 97 98

	if (icl->power) {
		ret = icl->power(icd->pdev, 0);
99
		if (ret < 0) {
100
			dev_err(icd->pdev,
101 102 103 104 105
				"Platform failed to power-off the camera.\n");
			return ret;
		}
	}

106 107 108 109 110 111
	ret = regulator_bulk_disable(icl->num_regulators,
				     icl->regulators);
	if (ret < 0)
		dev_err(icd->pdev, "Cannot disable regulators\n");

	return ret;
112 113
}

114 115 116 117 118 119 120 121 122 123 124 125
const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
	struct soc_camera_device *icd, unsigned int fourcc)
{
	unsigned int i;

	for (i = 0; i < icd->num_user_formats; i++)
		if (icd->user_formats[i].host_fmt->fourcc == fourcc)
			return icd->user_formats + i;
	return NULL;
}
EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
 * @icl:	camera platform parameters
 * @cfg:	media bus configuration
 * @return:	resulting flags
 */
unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl,
					   const struct v4l2_mbus_config *cfg)
{
	unsigned long f, flags = cfg->flags;

	/* If only one of the two polarities is supported, switch to the opposite */
	if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
		f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
		if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
			flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
	}

	if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
		f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
		if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
			flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
	}

	if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
		f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
		if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
			flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
	}

	return flags;
}
EXPORT_SYMBOL(soc_camera_apply_board_flags);

160 161 162 163 164 165
#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
	((x) >> 24) & 0xff

static int soc_camera_try_fmt(struct soc_camera_device *icd,
			      struct v4l2_format *f)
{
166
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
167
	const struct soc_camera_format_xlate *xlate;
168 169 170
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

171
	dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
172 173 174 175 176 177 178 179 180
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

	pix->bytesperline = 0;
	pix->sizeimage = 0;

	ret = ici->ops->try_fmt(icd, f);
	if (ret < 0)
		return ret;

181 182 183
	xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
	if (!xlate)
		return -EINVAL;
184

185 186 187
	ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
	if (ret < 0)
		return ret;
188

189 190 191 192 193 194 195 196
	pix->bytesperline = max_t(u32, pix->bytesperline, ret);

	ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
				  pix->height);
	if (ret < 0)
		return ret;

	pix->sizeimage = max_t(u32, pix->sizeimage, ret);
197 198 199 200

	return 0;
}

201
static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
202
				      struct v4l2_format *f)
203
{
204
	struct soc_camera_device *icd = file->private_data;
205 206 207

	WARN_ON(priv != file->private_data);

208 209 210 211
	/* Only single-plane capture is supported so far */
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

212
	/* limit format to hardware capabilities */
213
	return soc_camera_try_fmt(icd, f);
214 215 216 217 218 219 220 221
}

static int soc_camera_enum_input(struct file *file, void *priv,
				 struct v4l2_input *inp)
{
	if (inp->index != 0)
		return -EINVAL;

222 223 224 225
	/* default is camera */
	inp->type = V4L2_INPUT_TYPE_CAMERA;
	inp->std  = V4L2_STD_UNKNOWN;
	strcpy(inp->name, "Camera");
226

227
	return 0;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
{
	*i = 0;

	return 0;
}

static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
{
	if (i > 0)
		return -EINVAL;

	return 0;
}

static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
{
247
	struct soc_camera_device *icd = file->private_data;
248
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
249

250
	return v4l2_subdev_call(sd, core, s_std, *a);
251 252
}

253 254 255 256 257 258 259 260
static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
{
	struct soc_camera_device *icd = file->private_data;
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);

	return v4l2_subdev_call(sd, core, g_std, a);
}

261
static int soc_camera_enum_framesizes(struct file *file, void *fh,
262 263 264
					 struct v4l2_frmsizeenum *fsize)
{
	struct soc_camera_device *icd = file->private_data;
265
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
266

267
	return ici->ops->enum_framesizes(icd, fsize);
268 269
}

270 271 272 273
static int soc_camera_reqbufs(struct file *file, void *priv,
			      struct v4l2_requestbuffers *p)
{
	int ret;
274
	struct soc_camera_device *icd = file->private_data;
275
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
276 277 278

	WARN_ON(priv != file->private_data);

279 280 281
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;

282 283 284 285 286 287 288 289 290
	if (ici->ops->init_videobuf) {
		ret = videobuf_reqbufs(&icd->vb_vidq, p);
		if (ret < 0)
			return ret;

		ret = ici->ops->reqbufs(icd, p);
	} else {
		ret = vb2_reqbufs(&icd->vb2_vidq, p);
	}
291

292 293 294 295
	if (!ret && !icd->streamer)
		icd->streamer = file;

	return ret;
296 297 298 299 300
}

static int soc_camera_querybuf(struct file *file, void *priv,
			       struct v4l2_buffer *p)
{
301
	struct soc_camera_device *icd = file->private_data;
302
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
303 304 305

	WARN_ON(priv != file->private_data);

306 307 308 309
	if (ici->ops->init_videobuf)
		return videobuf_querybuf(&icd->vb_vidq, p);
	else
		return vb2_querybuf(&icd->vb2_vidq, p);
310 311 312 313 314
}

static int soc_camera_qbuf(struct file *file, void *priv,
			   struct v4l2_buffer *p)
{
315
	struct soc_camera_device *icd = file->private_data;
316
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
317 318 319

	WARN_ON(priv != file->private_data);

320 321 322
	if (icd->streamer != file)
		return -EBUSY;

323 324 325 326
	if (ici->ops->init_videobuf)
		return videobuf_qbuf(&icd->vb_vidq, p);
	else
		return vb2_qbuf(&icd->vb2_vidq, p);
327 328 329 330 331
}

static int soc_camera_dqbuf(struct file *file, void *priv,
			    struct v4l2_buffer *p)
{
332
	struct soc_camera_device *icd = file->private_data;
333
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
334 335 336

	WARN_ON(priv != file->private_data);

337 338 339
	if (icd->streamer != file)
		return -EBUSY;

340 341 342 343
	if (ici->ops->init_videobuf)
		return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
	else
		return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
344 345
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static int soc_camera_create_bufs(struct file *file, void *priv,
			    struct v4l2_create_buffers *create)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	/* videobuf2 only */
	if (ici->ops->init_videobuf)
		return -EINVAL;
	else
		return vb2_create_bufs(&icd->vb2_vidq, create);
}

static int soc_camera_prepare_buf(struct file *file, void *priv,
				  struct v4l2_buffer *b)
{
	struct soc_camera_device *icd = file->private_data;
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);

	/* videobuf2 only */
	if (ici->ops->init_videobuf)
		return -EINVAL;
	else
		return vb2_prepare_buf(&icd->vb2_vidq, b);
}

372
/* Always entered with .video_lock held */
373 374
static int soc_camera_init_user_formats(struct soc_camera_device *icd)
{
375
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
376
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
377 378
	unsigned int i, fmts = 0, raw_fmts = 0;
	int ret;
379 380 381 382
	enum v4l2_mbus_pixelcode code;

	while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
		raw_fmts++;
383 384 385 386 387 388

	if (!ici->ops->get_formats)
		/*
		 * Fallback mode - the host will have to serve all
		 * sensor-provided formats one-to-one to the user
		 */
389
		fmts = raw_fmts;
390 391 392 393 394
	else
		/*
		 * First pass - only count formats this host-sensor
		 * configuration can provide
		 */
395
		for (i = 0; i < raw_fmts; i++) {
396 397 398 399 400
			ret = ici->ops->get_formats(icd, i, NULL);
			if (ret < 0)
				return ret;
			fmts += ret;
		}
401 402 403 404 405 406 407 408 409

	if (!fmts)
		return -ENXIO;

	icd->user_formats =
		vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
	if (!icd->user_formats)
		return -ENOMEM;

410
	dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
411 412

	/* Second pass - actually fill data formats */
413
	fmts = 0;
414
	for (i = 0; i < raw_fmts; i++)
415
		if (!ici->ops->get_formats) {
416
			v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
417
			icd->user_formats[fmts].host_fmt =
418
				soc_mbus_get_fmtdesc(code);
419 420
			if (icd->user_formats[fmts].host_fmt)
				icd->user_formats[fmts++].code = code;
421
		} else {
422 423 424 425 426
			ret = ici->ops->get_formats(icd, i,
						    &icd->user_formats[fmts]);
			if (ret < 0)
				goto egfmt;
			fmts += ret;
427 428
		}

429
	icd->num_user_formats = fmts;
430
	icd->current_fmt = &icd->user_formats[0];
431 432

	return 0;
433 434 435 436

egfmt:
	vfree(icd->user_formats);
	return ret;
437 438
}

439
/* Always entered with .video_lock held */
440 441
static void soc_camera_free_user_formats(struct soc_camera_device *icd)
{
442
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
443 444 445

	if (ici->ops->put_formats)
		ici->ops->put_formats(icd);
446
	icd->current_fmt = NULL;
447
	icd->num_user_formats = 0;
448
	vfree(icd->user_formats);
449
	icd->user_formats = NULL;
450 451
}

452
/* Called with .vb_lock held, or from the first open(2), see comment there */
453
static int soc_camera_set_fmt(struct soc_camera_device *icd,
454 455
			      struct v4l2_format *f)
{
456
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
457 458 459
	struct v4l2_pix_format *pix = &f->fmt.pix;
	int ret;

460
	dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
461 462
		pixfmtstr(pix->pixelformat), pix->width, pix->height);

463
	/* We always call try_fmt() before set_fmt() or set_crop() */
464
	ret = soc_camera_try_fmt(icd, f);
465 466 467 468 469 470 471
	if (ret < 0)
		return ret;

	ret = ici->ops->set_fmt(icd, f);
	if (ret < 0) {
		return ret;
	} else if (!icd->current_fmt ||
472
		   icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
473
		dev_err(icd->pdev,
474 475 476 477
			"Host driver hasn't set up current format correctly!\n");
		return -EINVAL;
	}

478 479
	icd->user_width		= pix->width;
	icd->user_height	= pix->height;
480 481
	icd->bytesperline	= pix->bytesperline;
	icd->sizeimage		= pix->sizeimage;
482
	icd->colorspace		= pix->colorspace;
483 484 485
	icd->field		= pix->field;
	if (ici->ops->init_videobuf)
		icd->vb_vidq.field = pix->field;
486

487
	dev_dbg(icd->pdev, "set width: %d height: %d\n",
488
		icd->user_width, icd->user_height);
489 490

	/* set physical bus parameters */
491
	return ici->ops->set_bus_param(icd);
492 493
}

494
static int soc_camera_open(struct file *file)
495
{
496
	struct video_device *vdev = video_devdata(file);
497
	struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
498
	struct soc_camera_link *icl = to_soc_camera_link(icd);
499
	struct soc_camera_host *ici;
500 501
	int ret;

502
	if (!to_soc_camera_control(icd))
503 504 505
		/* No device driver attached */
		return -ENODEV;

506
	ici = to_soc_camera_host(icd->parent);
507

508
	if (!try_module_get(ici->ops->owner)) {
509
		dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
510
		return -EINVAL;
511 512
	}

513 514
	icd->use_count++;

515 516
	/* Now we really have to activate the camera */
	if (icd->use_count == 1) {
517
		/* Restore parameters before the last close() per V4L2 API */
518 519 520
		struct v4l2_format f = {
			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
			.fmt.pix = {
521 522
				.width		= icd->user_width,
				.height		= icd->user_height,
523
				.field		= icd->field,
524 525 526
				.colorspace	= icd->colorspace,
				.pixelformat	=
					icd->current_fmt->host_fmt->fourcc,
527 528 529
			},
		};

530 531 532 533
		/* The camera could have been already on, try to reset */
		if (icl->reset)
			icl->reset(icd->pdev);

534 535
		/* Don't mess with the host during probe */
		mutex_lock(&ici->host_lock);
536
		ret = ici->ops->add(icd);
537
		mutex_unlock(&ici->host_lock);
538
		if (ret < 0) {
539
			dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
540 541
			goto eiciadd;
		}
542

543 544 545 546
		ret = soc_camera_power_on(icd, icl);
		if (ret < 0)
			goto epower;

547 548 549 550 551
		pm_runtime_enable(&icd->vdev->dev);
		ret = pm_runtime_resume(&icd->vdev->dev);
		if (ret < 0 && ret != -ENOSYS)
			goto eresume;

552 553 554 555 556 557
		/*
		 * Try to configure with default parameters. Notice: this is the
		 * very first open, so, we cannot race against other calls,
		 * apart from someone else calling open() simultaneously, but
		 * .video_lock is protecting us against it.
		 */
558
		ret = soc_camera_set_fmt(icd, &f);
559 560
		if (ret < 0)
			goto esfmt;
561

562 563 564 565 566 567 568
		if (ici->ops->init_videobuf) {
			ici->ops->init_videobuf(&icd->vb_vidq, icd);
		} else {
			ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
			if (ret < 0)
				goto einitvb;
		}
569
		v4l2_ctrl_handler_setup(&icd->ctrl_handler);
570 571
	}

572
	file->private_data = icd;
573
	dev_dbg(icd->pdev, "camera device open\n");
574 575 576

	return 0;

577
	/*
578
	 * First four errors are entered with the .video_lock held
579 580
	 * and use_count == 1
	 */
581
einitvb:
582
esfmt:
583 584
	pm_runtime_disable(&icd->vdev->dev);
eresume:
585
	soc_camera_power_off(icd, icl);
586
epower:
587 588
	ici->ops->remove(icd);
eiciadd:
589
	icd->use_count--;
590
	module_put(ici->ops->owner);
591

592 593 594
	return ret;
}

595
static int soc_camera_close(struct file *file)
596
{
597
	struct soc_camera_device *icd = file->private_data;
598
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
599

600
	icd->use_count--;
601
	if (!icd->use_count) {
602 603
		struct soc_camera_link *icl = to_soc_camera_link(icd);

604 605 606
		pm_runtime_suspend(&icd->vdev->dev);
		pm_runtime_disable(&icd->vdev->dev);

607 608
		if (ici->ops->init_videobuf2)
			vb2_queue_release(&icd->vb2_vidq);
609
		ici->ops->remove(icd);
610

611
		soc_camera_power_off(icd, icl);
612
	}
613

614 615 616
	if (icd->streamer == file)
		icd->streamer = NULL;

617
	module_put(ici->ops->owner);
618

619
	dev_dbg(icd->pdev, "camera device close\n");
620 621 622 623

	return 0;
}

624
static ssize_t soc_camera_read(struct file *file, char __user *buf,
625
			       size_t count, loff_t *ppos)
626
{
627
	struct soc_camera_device *icd = file->private_data;
628 629
	int err = -EINVAL;

630
	dev_err(icd->pdev, "camera device read not implemented\n");
631 632 633 634 635 636

	return err;
}

static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
{
637
	struct soc_camera_device *icd = file->private_data;
638
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
639 640
	int err;

641
	dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
642

643 644 645
	if (icd->streamer != file)
		return -EBUSY;

646 647 648 649
	if (ici->ops->init_videobuf)
		err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
	else
		err = vb2_mmap(&icd->vb2_vidq, vma);
650

651
	dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
652 653 654 655 656 657 658 659 660
		(unsigned long)vma->vm_start,
		(unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
		err);

	return err;
}

static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
{
661
	struct soc_camera_device *icd = file->private_data;
662
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
663

664 665 666
	if (icd->streamer != file)
		return -EBUSY;

667
	if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
668
		dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
669 670 671
		return POLLERR;
	}

672
	return ici->ops->poll(file, pt);
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688
void soc_camera_lock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
	mutex_lock(&icd->video_lock);
}
EXPORT_SYMBOL(soc_camera_lock);

void soc_camera_unlock(struct vb2_queue *vq)
{
	struct soc_camera_device *icd = vb2_get_drv_priv(vq);
	mutex_unlock(&icd->video_lock);
}
EXPORT_SYMBOL(soc_camera_unlock);

689
static struct v4l2_file_operations soc_camera_fops = {
690 691 692
	.owner		= THIS_MODULE,
	.open		= soc_camera_open,
	.release	= soc_camera_close,
693
	.unlocked_ioctl	= video_ioctl2,
694 695 696 697 698
	.read		= soc_camera_read,
	.mmap		= soc_camera_mmap,
	.poll		= soc_camera_poll,
};

699
static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
700
				    struct v4l2_format *f)
701
{
702
	struct soc_camera_device *icd = file->private_data;
703 704 705 706
	int ret;

	WARN_ON(priv != file->private_data);

707
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
708
		dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
709 710 711
		return -EINVAL;
	}

712 713
	if (icd->streamer && icd->streamer != file)
		return -EBUSY;
714

715 716
	if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
		dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
717
		return -EBUSY;
718 719
	}

720 721 722 723
	ret = soc_camera_set_fmt(icd, f);

	if (!ret && !icd->streamer)
		icd->streamer = file;
724 725

	return ret;
726 727
}

728
static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
729
				       struct v4l2_fmtdesc *f)
730
{
731
	struct soc_camera_device *icd = file->private_data;
732
	const struct soc_mbus_pixelfmt *format;
733 734 735

	WARN_ON(priv != file->private_data);

736
	if (f->index >= icd->num_user_formats)
737 738
		return -EINVAL;

739
	format = icd->user_formats[f->index].host_fmt;
740

741 742
	if (format->name)
		strlcpy(f->description, format->name, sizeof(f->description));
743 744 745 746
	f->pixelformat = format->fourcc;
	return 0;
}

747
static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
748
				    struct v4l2_format *f)
749
{
750
	struct soc_camera_device *icd = file->private_data;
751
	struct v4l2_pix_format *pix = &f->fmt.pix;
752 753 754

	WARN_ON(priv != file->private_data);

755 756 757
	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

758 759
	pix->width		= icd->user_width;
	pix->height		= icd->user_height;
760 761
	pix->bytesperline	= icd->bytesperline;
	pix->sizeimage		= icd->sizeimage;
762
	pix->field		= icd->field;
763 764
	pix->pixelformat	= icd->current_fmt->host_fmt->fourcc;
	pix->colorspace		= icd->colorspace;
765
	dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
766
		icd->current_fmt->host_fmt->fourcc);
767 768 769 770 771 772
	return 0;
}

static int soc_camera_querycap(struct file *file, void  *priv,
			       struct v4l2_capability *cap)
{
773
	struct soc_camera_device *icd = file->private_data;
774
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
775 776 777 778

	WARN_ON(priv != file->private_data);

	strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
779
	return ici->ops->querycap(ici, cap);
780 781 782 783 784
}

static int soc_camera_streamon(struct file *file, void *priv,
			       enum v4l2_buf_type i)
{
785
	struct soc_camera_device *icd = file->private_data;
786
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
787
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
788
	int ret;
789 790 791 792 793 794

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

795 796 797
	if (icd->streamer != file)
		return -EBUSY;

798
	/* This calls buf_queue from host driver's videobuf_queue_ops */
799 800 801 802 803
	if (ici->ops->init_videobuf)
		ret = videobuf_streamon(&icd->vb_vidq);
	else
		ret = vb2_streamon(&icd->vb2_vidq, i);

804 805
	if (!ret)
		v4l2_subdev_call(sd, video, s_stream, 1);
806 807

	return ret;
808 809 810 811 812
}

static int soc_camera_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type i)
{
813
	struct soc_camera_device *icd = file->private_data;
814
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
815
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
816 817 818 819 820 821

	WARN_ON(priv != file->private_data);

	if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

822 823 824
	if (icd->streamer != file)
		return -EBUSY;

825 826 827 828
	/*
	 * This calls buf_release from host driver's videobuf_queue_ops for all
	 * remaining buffers. When the last buffer is freed, stop capture
	 */
829 830 831 832
	if (ici->ops->init_videobuf)
		videobuf_streamoff(&icd->vb_vidq);
	else
		vb2_streamoff(&icd->vb2_vidq, i);
833

834
	v4l2_subdev_call(sd, video, s_stream, 0);
835 836 837 838 839 840 841

	return 0;
}

static int soc_camera_cropcap(struct file *file, void *fh,
			      struct v4l2_cropcap *a)
{
842
	struct soc_camera_device *icd = file->private_data;
843
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
844

845
	return ici->ops->cropcap(icd, a);
846 847 848 849 850
}

static int soc_camera_g_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
851
	struct soc_camera_device *icd = file->private_data;
852
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
853
	int ret;
854

855
	ret = ici->ops->get_crop(icd, a);
856

857
	return ret;
858 859
}

860 861 862
/*
 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
 * argument with the actual geometry, instead, the user shall use G_CROP to
863
 * retrieve it.
864
 */
865 866 867
static int soc_camera_s_crop(struct file *file, void *fh,
			     struct v4l2_crop *a)
{
868
	struct soc_camera_device *icd = file->private_data;
869
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
870 871
	struct v4l2_rect *rect = &a->c;
	struct v4l2_crop current_crop;
872 873 874 875 876
	int ret;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

877
	dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
878 879 880 881 882
		rect->width, rect->height, rect->left, rect->top);

	/* If get_crop fails, we'll let host and / or client drivers decide */
	ret = ici->ops->get_crop(icd, &current_crop);

883
	/* Prohibit window size change with initialised buffers */
884
	if (ret < 0) {
885
		dev_err(icd->pdev,
886
			"S_CROP denied: getting current crop failed\n");
887 888 889 890 891 892 893 894
	} else if ((a->c.width == current_crop.c.width &&
		    a->c.height == current_crop.c.height) ||
		   !is_streaming(ici, icd)) {
		/* same size or not streaming - use .set_crop() */
		ret = ici->ops->set_crop(icd, a);
	} else if (ici->ops->set_livecrop) {
		ret = ici->ops->set_livecrop(icd, a);
	} else {
895
		dev_err(icd->pdev,
896 897 898 899
			"S_CROP denied: queue initialised and sizes differ\n");
		ret = -EBUSY;
	}

900 901 902
	return ret;
}

903 904 905
static int soc_camera_g_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
906
	struct soc_camera_device *icd = file->private_data;
907
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
908 909 910 911 912 913 914 915 916 917

	if (ici->ops->get_parm)
		return ici->ops->get_parm(icd, a);

	return -ENOIOCTLCMD;
}

static int soc_camera_s_parm(struct file *file, void *fh,
			     struct v4l2_streamparm *a)
{
918
	struct soc_camera_device *icd = file->private_data;
919
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
920 921 922 923 924 925 926

	if (ici->ops->set_parm)
		return ici->ops->set_parm(icd, a);

	return -ENOIOCTLCMD;
}

927
static int soc_camera_g_chip_ident(struct file *file, void *fh,
928
				   struct v4l2_dbg_chip_ident *id)
929
{
930
	struct soc_camera_device *icd = file->private_data;
931
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
932

933
	return v4l2_subdev_call(sd, core, g_chip_ident, id);
934 935 936 937
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
static int soc_camera_g_register(struct file *file, void *fh,
938
				 struct v4l2_dbg_register *reg)
939
{
940
	struct soc_camera_device *icd = file->private_data;
941
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
942

943
	return v4l2_subdev_call(sd, core, g_register, reg);
944 945 946
}

static int soc_camera_s_register(struct file *file, void *fh,
947
				 struct v4l2_dbg_register *reg)
948
{
949
	struct soc_camera_device *icd = file->private_data;
950
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
951

952
	return v4l2_subdev_call(sd, core, s_register, reg);
953 954 955
}
#endif

956 957
static int soc_camera_probe(struct soc_camera_device *icd);

958 959 960 961 962
/* So far this function cannot fail */
static void scan_add_host(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

963
	mutex_lock(&ici->host_lock);
964 965 966

	list_for_each_entry(icd, &devices, list) {
		if (icd->iface == ici->nr) {
967
			int ret;
968 969 970

			icd->parent = ici->v4l2_dev.dev;
			ret = soc_camera_probe(icd);
971 972 973
		}
	}

974
	mutex_unlock(&ici->host_lock);
975 976
}

977 978 979
#ifdef CONFIG_I2C_BOARDINFO
static int soc_camera_init_i2c(struct soc_camera_device *icd,
			       struct soc_camera_link *icl)
980
{
981
	struct i2c_client *client;
982
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
983
	struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
984
	struct v4l2_subdev *subdev;
985

986
	if (!adap) {
987
		dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
988 989 990
			icl->i2c_adapter_id);
		goto ei2cga;
	}
991

992
	icl->board_info->platform_data = icl;
993

994
	subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
995
				icl->board_info, NULL);
996
	if (!subdev)
997
		goto ei2cnd;
998

999
	client = v4l2_get_subdevdata(subdev);
1000

1001
	/* Use to_i2c_client(dev) to recover the i2c client */
1002
	icd->control = &client->dev;
1003

1004 1005 1006 1007
	return 0;
ei2cnd:
	i2c_put_adapter(adap);
ei2cga:
1008
	return -ENODEV;
1009 1010
}

1011 1012 1013 1014
static void soc_camera_free_i2c(struct soc_camera_device *icd)
{
	struct i2c_client *client =
		to_i2c_client(to_soc_camera_control(icd));
1015
	struct i2c_adapter *adap = client->adapter;
1016 1017

	icd->control = NULL;
1018
	v4l2_device_unregister_subdev(i2c_get_clientdata(client));
1019
	i2c_unregister_device(client);
1020
	i2c_put_adapter(adap);
1021 1022 1023 1024 1025 1026
}
#else
#define soc_camera_init_i2c(icd, icl)	(-ENODEV)
#define soc_camera_free_i2c(icd)	do {} while (0)
#endif

1027
static int soc_camera_video_start(struct soc_camera_device *icd);
1028 1029
static int video_dev_create(struct soc_camera_device *icd);
/* Called during host-driver probe */
1030
static int soc_camera_probe(struct soc_camera_device *icd)
1031
{
1032
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1033
	struct soc_camera_link *icl = to_soc_camera_link(icd);
1034
	struct device *control = NULL;
1035
	struct v4l2_subdev *sd;
1036
	struct v4l2_mbus_framefmt mf;
1037 1038
	int ret;

1039
	dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1040

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	/*
	 * Currently the subdev with the largest number of controls (13) is
	 * ov6550. So let's pick 16 as a hint for the control handler. Note
	 * that this is a hint only: too large and you waste some memory, too
	 * small and there is a (very) small performance hit when looking up
	 * controls in the internal hash.
	 */
	ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
	if (ret < 0)
		return ret;

1052 1053 1054 1055 1056
	ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
				 icl->regulators);
	if (ret < 0)
		goto ereg;

1057 1058 1059 1060 1061 1062 1063 1064
	/* The camera could have been already on, try to reset */
	if (icl->reset)
		icl->reset(icd->pdev);

	ret = ici->ops->add(icd);
	if (ret < 0)
		goto eadd;

1065 1066 1067 1068 1069 1070
	/*
	 * This will not yet call v4l2_subdev_core_ops::s_power(1), because the
	 * subdevice has not been initialised yet. We'll have to call it once
	 * again after initialisation, even though it shouldn't be needed, we
	 * don't do any IO here.
	 */
1071
	ret = soc_camera_power_on(icd, icl);
1072 1073
	if (ret < 0)
		goto epower;
1074 1075

	/* Must have icd->vdev before registering the device */
1076 1077 1078
	ret = video_dev_create(icd);
	if (ret < 0)
		goto evdc;
1079

1080 1081 1082 1083 1084 1085
	/* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
	if (icl->board_info) {
		ret = soc_camera_init_i2c(icd, icl);
		if (ret < 0)
			goto eadddev;
	} else if (!icl->add_device || !icl->del_device) {
1086
		ret = -EINVAL;
1087 1088
		goto eadddev;
	} else {
1089 1090 1091
		if (icl->module_name)
			ret = request_module(icl->module_name);

1092
		ret = icl->add_device(icd);
1093 1094
		if (ret < 0)
			goto eadddev;
1095

1096 1097 1098 1099
		/*
		 * FIXME: this is racy, have to use driver-binding notification,
		 * when it is available
		 */
1100
		control = to_soc_camera_control(icd);
1101
		if (!control || !control->driver || !dev_get_drvdata(control) ||
1102
		    !try_module_get(control->driver->owner)) {
1103
			icl->del_device(icd);
1104
			ret = -ENODEV;
1105 1106
			goto enodrv;
		}
1107
	}
1108

1109
	sd = soc_camera_to_subdev(icd);
1110 1111
	sd->grp_id = soc_camera_grp_id(icd);
	v4l2_set_subdev_hostdata(sd, icd);
1112

1113 1114 1115
	if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler))
		goto ectrl;

1116 1117 1118 1119 1120 1121 1122
	/* At this point client .probe() should have run already */
	ret = soc_camera_init_user_formats(icd);
	if (ret < 0)
		goto eiufmt;

	icd->field = V4L2_FIELD_ANY;

1123 1124 1125 1126 1127
	/*
	 * ..._video_start() will create a device node, video_register_device()
	 * itself is protected against concurrent open() calls, but we also have
	 * to protect our data.
	 */
1128 1129 1130 1131 1132 1133
	mutex_lock(&icd->video_lock);

	ret = soc_camera_video_start(icd);
	if (ret < 0)
		goto evidstart;

1134 1135 1136 1137
	ret = v4l2_subdev_call(sd, core, s_power, 1);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		goto esdpwr;

1138
	/* Try to improve our guess of a reasonable window format */
1139 1140 1141 1142 1143
	if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
		icd->user_width		= mf.width;
		icd->user_height	= mf.height;
		icd->colorspace		= mf.colorspace;
		icd->field		= mf.field;
1144 1145
	}

1146 1147
	ici->ops->remove(icd);

1148
	soc_camera_power_off(icd, icl);
1149 1150

	mutex_unlock(&icd->video_lock);
1151

1152
	return 0;
1153

1154 1155
esdpwr:
	video_unregister_device(icd->vdev);
1156 1157
evidstart:
	mutex_unlock(&icd->video_lock);
1158 1159
	soc_camera_free_user_formats(icd);
eiufmt:
1160
ectrl:
1161
	if (icl->board_info) {
1162
		soc_camera_free_i2c(icd);
1163
	} else {
1164
		icl->del_device(icd);
1165 1166 1167
		module_put(control->driver->owner);
	}
enodrv:
1168 1169
eadddev:
	video_device_release(icd->vdev);
1170
	icd->vdev = NULL;
1171
evdc:
1172
	soc_camera_power_off(icd, icl);
1173
epower:
1174 1175
	ici->ops->remove(icd);
eadd:
1176 1177
	regulator_bulk_free(icl->num_regulators, icl->regulators);
ereg:
1178
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1179 1180 1181
	return ret;
}

1182 1183 1184 1185
/*
 * This is called on device_unregister, which only means we have to disconnect
 * from the host, but not remove ourselves from the device list
 */
1186
static int soc_camera_remove(struct soc_camera_device *icd)
1187
{
1188 1189
	struct soc_camera_link *icl = to_soc_camera_link(icd);
	struct video_device *vdev = icd->vdev;
1190

1191
	BUG_ON(!icd->parent);
1192

1193
	v4l2_ctrl_handler_free(&icd->ctrl_handler);
1194 1195 1196 1197 1198
	if (vdev) {
		video_unregister_device(vdev);
		icd->vdev = NULL;
	}

1199
	if (icl->board_info) {
1200
		soc_camera_free_i2c(icd);
1201
	} else {
1202
		struct device_driver *drv = to_soc_camera_control(icd)->driver;
1203
		if (drv) {
1204
			icl->del_device(icd);
1205 1206 1207
			module_put(drv->owner);
		}
	}
1208
	soc_camera_free_user_formats(icd);
1209

1210 1211
	regulator_bulk_free(icl->num_regulators, icl->regulators);

1212 1213 1214
	return 0;
}

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
static int default_cropcap(struct soc_camera_device *icd,
			   struct v4l2_cropcap *a)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, cropcap, a);
}

static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, g_crop, a);
}

static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_crop, a);
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
static int default_g_parm(struct soc_camera_device *icd,
			  struct v4l2_streamparm *parm)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, g_parm, parm);
}

static int default_s_parm(struct soc_camera_device *icd,
			  struct v4l2_streamparm *parm)
{
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	return v4l2_subdev_call(sd, video, s_parm, parm);
}

1248 1249
static int default_enum_framesizes(struct soc_camera_device *icd,
				   struct v4l2_frmsizeenum *fsize)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
{
	int ret;
	struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
	const struct soc_camera_format_xlate *xlate;
	__u32 pixfmt = fsize->pixel_format;
	struct v4l2_frmsizeenum fsize_mbus = *fsize;

	xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
	if (!xlate)
		return -EINVAL;
	/* map xlate-code to pixel_format, sensor only handle xlate-code*/
	fsize_mbus.pixel_format = xlate->code;

	ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
	if (ret < 0)
		return ret;

	*fsize = fsize_mbus;
	fsize->pixel_format = pixfmt;

	return 0;
}

1273
int soc_camera_host_register(struct soc_camera_host *ici)
1274 1275
{
	struct soc_camera_host *ix;
1276
	int ret;
1277

1278 1279 1280 1281 1282
	if (!ici || !ici->ops ||
	    !ici->ops->try_fmt ||
	    !ici->ops->set_fmt ||
	    !ici->ops->set_bus_param ||
	    !ici->ops->querycap ||
1283 1284 1285
	    ((!ici->ops->init_videobuf ||
	      !ici->ops->reqbufs) &&
	     !ici->ops->init_videobuf2) ||
1286 1287
	    !ici->ops->add ||
	    !ici->ops->remove ||
1288
	    !ici->ops->poll ||
1289
	    !ici->v4l2_dev.dev)
1290 1291
		return -EINVAL;

1292 1293 1294 1295 1296 1297
	if (!ici->ops->set_crop)
		ici->ops->set_crop = default_s_crop;
	if (!ici->ops->get_crop)
		ici->ops->get_crop = default_g_crop;
	if (!ici->ops->cropcap)
		ici->ops->cropcap = default_cropcap;
1298 1299 1300 1301
	if (!ici->ops->set_parm)
		ici->ops->set_parm = default_s_parm;
	if (!ici->ops->get_parm)
		ici->ops->get_parm = default_g_parm;
1302 1303
	if (!ici->ops->enum_framesizes)
		ici->ops->enum_framesizes = default_enum_framesizes;
1304

1305 1306 1307
	mutex_lock(&list_lock);
	list_for_each_entry(ix, &hosts, list) {
		if (ix->nr == ici->nr) {
1308 1309
			ret = -EBUSY;
			goto edevreg;
1310 1311 1312
		}
	}

1313 1314 1315
	ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
	if (ret < 0)
		goto edevreg;
1316

1317 1318 1319
	list_add_tail(&ici->list, &hosts);
	mutex_unlock(&list_lock);

1320
	mutex_init(&ici->host_lock);
1321 1322 1323
	scan_add_host(ici);

	return 0;
1324 1325 1326 1327

edevreg:
	mutex_unlock(&list_lock);
	return ret;
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
}
EXPORT_SYMBOL(soc_camera_host_register);

/* Unregister all clients! */
void soc_camera_host_unregister(struct soc_camera_host *ici)
{
	struct soc_camera_device *icd;

	mutex_lock(&list_lock);

	list_del(&ici->list);
1339 1340 1341
	list_for_each_entry(icd, &devices, list)
		if (icd->iface == ici->nr && to_soc_camera_control(icd))
			soc_camera_remove(icd);
1342 1343 1344

	mutex_unlock(&list_lock);

1345
	v4l2_device_unregister(&ici->v4l2_dev);
1346 1347 1348 1349
}
EXPORT_SYMBOL(soc_camera_host_unregister);

/* Image capture device */
1350
static int soc_camera_device_register(struct soc_camera_device *icd)
1351 1352 1353 1354 1355 1356
{
	struct soc_camera_device *ix;
	int num = -1, i;

	for (i = 0; i < 256 && num < 0; i++) {
		num = i;
1357
		/* Check if this index is available on this interface */
1358 1359 1360 1361 1362 1363 1364 1365 1366
		list_for_each_entry(ix, &devices, list) {
			if (ix->iface == icd->iface && ix->devnum == i) {
				num = -1;
				break;
			}
		}
	}

	if (num < 0)
1367 1368 1369 1370
		/*
		 * ok, we have 256 cameras on this host...
		 * man, stay reasonable...
		 */
1371 1372
		return -ENOMEM;

1373
	icd->devnum		= num;
1374 1375
	icd->use_count		= 0;
	icd->host_priv		= NULL;
1376
	mutex_init(&icd->video_lock);
1377

1378 1379 1380
	list_add_tail(&icd->list, &devices);

	return 0;
1381 1382
}

1383 1384
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
	.vidioc_querycap	 = soc_camera_querycap,
1385
	.vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
1386 1387
	.vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
1388
	.vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1389 1390 1391 1392
	.vidioc_enum_input	 = soc_camera_enum_input,
	.vidioc_g_input		 = soc_camera_g_input,
	.vidioc_s_input		 = soc_camera_s_input,
	.vidioc_s_std		 = soc_camera_s_std,
1393
	.vidioc_g_std		 = soc_camera_g_std,
1394
	.vidioc_enum_framesizes  = soc_camera_enum_framesizes,
1395 1396 1397 1398
	.vidioc_reqbufs		 = soc_camera_reqbufs,
	.vidioc_querybuf	 = soc_camera_querybuf,
	.vidioc_qbuf		 = soc_camera_qbuf,
	.vidioc_dqbuf		 = soc_camera_dqbuf,
1399 1400
	.vidioc_create_bufs	 = soc_camera_create_bufs,
	.vidioc_prepare_buf	 = soc_camera_prepare_buf,
1401 1402 1403 1404 1405
	.vidioc_streamon	 = soc_camera_streamon,
	.vidioc_streamoff	 = soc_camera_streamoff,
	.vidioc_cropcap		 = soc_camera_cropcap,
	.vidioc_g_crop		 = soc_camera_g_crop,
	.vidioc_s_crop		 = soc_camera_s_crop,
1406 1407
	.vidioc_g_parm		 = soc_camera_g_parm,
	.vidioc_s_parm		 = soc_camera_s_parm,
1408 1409 1410 1411 1412 1413 1414
	.vidioc_g_chip_ident     = soc_camera_g_chip_ident,
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register	 = soc_camera_g_register,
	.vidioc_s_register	 = soc_camera_s_register,
#endif
};

1415
static int video_dev_create(struct soc_camera_device *icd)
1416
{
1417
	struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1418
	struct video_device *vdev = video_device_alloc();
1419 1420

	if (!vdev)
1421
		return -ENOMEM;
1422 1423

	strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1424

1425
	vdev->parent		= icd->pdev;
1426 1427
	vdev->current_norm	= V4L2_STD_UNKNOWN;
	vdev->fops		= &soc_camera_fops;
1428
	vdev->ioctl_ops		= &soc_camera_ioctl_ops;
1429
	vdev->release		= video_device_release;
1430
	vdev->tvnorms		= V4L2_STD_UNKNOWN;
1431
	vdev->ctrl_handler	= &icd->ctrl_handler;
1432
	vdev->lock		= &icd->video_lock;
1433 1434 1435 1436
	/* Locking in file operations other than ioctl should be done
	   by the driver, not the V4L2 core.
	   This driver needs auditing so that this flag can be removed. */
	set_bit(V4L2_FL_LOCK_ALL_FOPS, &vdev->flags);
1437 1438 1439 1440

	icd->vdev = vdev;

	return 0;
1441
}
1442

1443
/*
1444
 * Called from soc_camera_probe() above (with .video_lock held???)
1445
 */
1446
static int soc_camera_video_start(struct soc_camera_device *icd)
1447
{
1448
	const struct device_type *type = icd->vdev->dev.type;
1449
	int ret;
1450

1451
	if (!icd->parent)
1452 1453
		return -ENODEV;

1454
	ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1455
	if (ret < 0) {
1456
		dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1457 1458
		return ret;
	}
1459

1460 1461 1462
	/* Restore device type, possibly set by the subdevice driver */
	icd->vdev->dev.type = type;

1463
	return 0;
1464 1465
}

1466
static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1467
{
1468 1469
	struct soc_camera_link *icl = pdev->dev.platform_data;
	struct soc_camera_device *icd;
1470
	int ret;
1471

1472 1473
	if (!icl)
		return -EINVAL;
1474

1475 1476 1477
	icd = kzalloc(sizeof(*icd), GFP_KERNEL);
	if (!icd)
		return -ENOMEM;
1478

1479
	icd->iface = icl->bus_id;
1480
	icd->link = icl;
1481
	icd->pdev = &pdev->dev;
1482
	platform_set_drvdata(pdev, icd);
1483

1484 1485 1486
	ret = soc_camera_device_register(icd);
	if (ret < 0)
		goto escdevreg;
1487

1488 1489 1490
	icd->user_width		= DEFAULT_WIDTH;
	icd->user_height	= DEFAULT_HEIGHT;

1491
	return 0;
1492

1493 1494
escdevreg:
	kfree(icd);
1495

1496
	return ret;
1497
}
1498

1499 1500
/*
 * Only called on rmmod for each platform device, since they are not
1501
 * hot-pluggable. Now we know, that all our users - hosts and devices have
1502 1503
 * been unloaded already
 */
1504
static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1505
{
1506
	struct soc_camera_device *icd = platform_get_drvdata(pdev);
1507

1508
	if (!icd)
1509 1510
		return -EINVAL;

1511
	list_del(&icd->list);
1512

1513
	kfree(icd);
1514

1515 1516 1517 1518
	return 0;
}

static struct platform_driver __refdata soc_camera_pdrv = {
1519 1520 1521 1522
	.remove  = __devexit_p(soc_camera_pdrv_remove),
	.driver  = {
		.name	= "soc-camera-pdrv",
		.owner	= THIS_MODULE,
1523 1524 1525
	},
};

1526 1527
static int __init soc_camera_init(void)
{
1528
	return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1529 1530 1531 1532
}

static void __exit soc_camera_exit(void)
{
1533
	platform_driver_unregister(&soc_camera_pdrv);
1534 1535 1536 1537 1538 1539 1540 1541
}

module_init(soc_camera_init);
module_exit(soc_camera_exit);

MODULE_DESCRIPTION("Image capture bus driver");
MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
MODULE_LICENSE("GPL");
1542
MODULE_ALIAS("platform:soc-camera-pdrv");