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