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