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