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