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