blob: 66ebe5956a878ef45cb0a591d88cb2496bea3bc2 [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
2 * camera image capture (abstract) bus driver
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This driver provides an interface between platform-specific camera
7 * busses and camera devices. It should be used if the camera is
8 * connected not over a "proper" bus like PCI or USB, but over a
9 * special bus, like, for example, the Quick Capture interface on PXA270
10 * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
11 * It can handle multiple cameras and / or multiple busses, which can
12 * be used, e.g., in stereo-vision applications.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/device.h>
22#include <linux/list.h>
23#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/vmalloc.h>
26
27#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030028#include <media/v4l2-ioctl.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030029#include <media/v4l2-dev.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030030#include <media/videobuf-core.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030031#include <media/soc_camera.h>
32
33static LIST_HEAD(hosts);
34static LIST_HEAD(devices);
35static DEFINE_MUTEX(list_lock);
36static DEFINE_MUTEX(video_lock);
37
38const static struct soc_camera_data_format*
39format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
40{
41 unsigned int i;
42
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -030043 for (i = 0; i < icd->num_formats; i++)
44 if (icd->formats[i].fourcc == fourcc)
45 return icd->formats + i;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030046 return NULL;
47}
48
Hans Verkuil72937892008-06-01 10:35:18 -030049static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030050 struct v4l2_format *f)
51{
52 struct soc_camera_file *icf = file->private_data;
53 struct soc_camera_device *icd = icf->icd;
54 struct soc_camera_host *ici =
55 to_soc_camera_host(icd->dev.parent);
56 enum v4l2_field field;
57 const struct soc_camera_data_format *fmt;
58 int ret;
59
60 WARN_ON(priv != file->private_data);
61
62 fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
63 if (!fmt) {
64 dev_dbg(&icd->dev, "invalid format 0x%08x\n",
65 f->fmt.pix.pixelformat);
66 return -EINVAL;
67 }
68
69 dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc);
70
71 field = f->fmt.pix.field;
72
73 if (field == V4L2_FIELD_ANY) {
74 field = V4L2_FIELD_NONE;
75 } else if (V4L2_FIELD_NONE != field) {
76 dev_err(&icd->dev, "Field type invalid.\n");
77 return -EINVAL;
78 }
79
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030080 /* test physical bus parameters */
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -030081 ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030082 if (ret)
83 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030084
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030085 /* limit format to hardware capabilities */
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -030086 ret = ici->ops->try_fmt_cap(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030087
88 /* calculate missing fields */
89 f->fmt.pix.field = field;
90 f->fmt.pix.bytesperline =
91 (f->fmt.pix.width * fmt->depth) >> 3;
92 f->fmt.pix.sizeimage =
93 f->fmt.pix.height * f->fmt.pix.bytesperline;
94
95 return ret;
96}
97
98static int soc_camera_enum_input(struct file *file, void *priv,
99 struct v4l2_input *inp)
100{
101 if (inp->index != 0)
102 return -EINVAL;
103
104 inp->type = V4L2_INPUT_TYPE_CAMERA;
105 inp->std = V4L2_STD_UNKNOWN;
106 strcpy(inp->name, "Camera");
107
108 return 0;
109}
110
111static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
112{
113 *i = 0;
114
115 return 0;
116}
117
118static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
119{
120 if (i > 0)
121 return -EINVAL;
122
123 return 0;
124}
125
126static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
127{
128 return 0;
129}
130
131static int soc_camera_reqbufs(struct file *file, void *priv,
132 struct v4l2_requestbuffers *p)
133{
134 int ret;
135 struct soc_camera_file *icf = file->private_data;
136 struct soc_camera_device *icd = icf->icd;
137 struct soc_camera_host *ici =
138 to_soc_camera_host(icd->dev.parent);
139
140 WARN_ON(priv != file->private_data);
141
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300142 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300143
144 ret = videobuf_reqbufs(&icf->vb_vidq, p);
145 if (ret < 0)
146 return ret;
147
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300148 return ici->ops->reqbufs(icf, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300149}
150
151static int soc_camera_querybuf(struct file *file, void *priv,
152 struct v4l2_buffer *p)
153{
154 struct soc_camera_file *icf = file->private_data;
155
156 WARN_ON(priv != file->private_data);
157
158 return videobuf_querybuf(&icf->vb_vidq, p);
159}
160
161static int soc_camera_qbuf(struct file *file, void *priv,
162 struct v4l2_buffer *p)
163{
164 struct soc_camera_file *icf = file->private_data;
165
166 WARN_ON(priv != file->private_data);
167
168 return videobuf_qbuf(&icf->vb_vidq, p);
169}
170
171static int soc_camera_dqbuf(struct file *file, void *priv,
172 struct v4l2_buffer *p)
173{
174 struct soc_camera_file *icf = file->private_data;
175
176 WARN_ON(priv != file->private_data);
177
178 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
179}
180
181static int soc_camera_open(struct inode *inode, struct file *file)
182{
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300183 struct video_device *vdev;
184 struct soc_camera_device *icd;
185 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300186 struct soc_camera_file *icf;
187 int ret;
188
189 icf = vmalloc(sizeof(*icf));
190 if (!icf)
191 return -ENOMEM;
192
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300193 /* Protect against icd->remove() until we module_get() both drivers. */
194 mutex_lock(&video_lock);
195
196 vdev = video_devdata(file);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300197 icd = container_of(vdev->parent, struct soc_camera_device, dev);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300198 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300199
200 if (!try_module_get(icd->ops->owner)) {
201 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
202 ret = -EINVAL;
203 goto emgd;
204 }
205
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300206 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300207 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
208 ret = -EINVAL;
209 goto emgi;
210 }
211
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300212 icf->icd = icd;
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300213 icd->use_count++;
214
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300215 /* Now we really have to activate the camera */
216 if (icd->use_count == 1) {
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300217 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300218 if (ret < 0) {
219 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
220 icd->use_count--;
221 goto eiciadd;
222 }
223 }
224
225 mutex_unlock(&video_lock);
226
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300227 file->private_data = icf;
228 dev_dbg(&icd->dev, "camera device open\n");
229
Magnus Damma034d1b2008-07-11 20:59:34 -0300230 ici->ops->init_videobuf(&icf->vb_vidq, icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300231
232 return 0;
233
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300234 /* All errors are entered with the video_lock held */
235eiciadd:
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300236 module_put(ici->ops->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300237emgi:
238 module_put(icd->ops->owner);
239emgd:
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300240 mutex_unlock(&video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300241 vfree(icf);
242 return ret;
243}
244
245static int soc_camera_close(struct inode *inode, struct file *file)
246{
247 struct soc_camera_file *icf = file->private_data;
248 struct soc_camera_device *icd = icf->icd;
249 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
250 struct video_device *vdev = icd->vdev;
251
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300252 mutex_lock(&video_lock);
253 icd->use_count--;
254 if (!icd->use_count)
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300255 ici->ops->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300256 module_put(icd->ops->owner);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300257 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300258 mutex_unlock(&video_lock);
259
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300260 vfree(icf);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300261
Hans Verkuil5e85e732008-07-20 06:31:39 -0300262 dev_dbg(vdev->parent, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300263
264 return 0;
265}
266
Andrew Mortonaba360d2008-04-22 14:45:59 -0300267static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300268 size_t count, loff_t *ppos)
269{
270 struct soc_camera_file *icf = file->private_data;
271 struct soc_camera_device *icd = icf->icd;
272 struct video_device *vdev = icd->vdev;
273 int err = -EINVAL;
274
Hans Verkuil5e85e732008-07-20 06:31:39 -0300275 dev_err(vdev->parent, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300276
277 return err;
278}
279
280static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
281{
282 struct soc_camera_file *icf = file->private_data;
283 struct soc_camera_device *icd = icf->icd;
284 int err;
285
286 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
287
288 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
289
290 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
291 (unsigned long)vma->vm_start,
292 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
293 err);
294
295 return err;
296}
297
298static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
299{
300 struct soc_camera_file *icf = file->private_data;
301 struct soc_camera_device *icd = icf->icd;
302 struct soc_camera_host *ici =
303 to_soc_camera_host(icd->dev.parent);
304
305 if (list_empty(&icf->vb_vidq.stream)) {
306 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
307 return POLLERR;
308 }
309
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300310 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300311}
312
313
314static struct file_operations soc_camera_fops = {
315 .owner = THIS_MODULE,
316 .open = soc_camera_open,
317 .release = soc_camera_close,
318 .ioctl = video_ioctl2,
319 .read = soc_camera_read,
320 .mmap = soc_camera_mmap,
321 .poll = soc_camera_poll,
322 .llseek = no_llseek,
323};
324
325
Hans Verkuil72937892008-06-01 10:35:18 -0300326static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300327 struct v4l2_format *f)
328{
329 struct soc_camera_file *icf = file->private_data;
330 struct soc_camera_device *icd = icf->icd;
331 struct soc_camera_host *ici =
332 to_soc_camera_host(icd->dev.parent);
333 int ret;
334 struct v4l2_rect rect;
335 const static struct soc_camera_data_format *data_fmt;
336
337 WARN_ON(priv != file->private_data);
338
339 data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
340 if (!data_fmt)
341 return -EINVAL;
342
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300343 /* buswidth may be further adjusted by the ici */
344 icd->buswidth = data_fmt->depth;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300345
Hans Verkuil72937892008-06-01 10:35:18 -0300346 ret = soc_camera_try_fmt_vid_cap(file, icf, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300347 if (ret < 0)
348 return ret;
349
350 rect.left = icd->x_current;
351 rect.top = icd->y_current;
352 rect.width = f->fmt.pix.width;
353 rect.height = f->fmt.pix.height;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300354 ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300355 if (ret < 0)
356 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300357
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300358 icd->current_fmt = data_fmt;
359 icd->width = rect.width;
360 icd->height = rect.height;
361 icf->vb_vidq.field = f->fmt.pix.field;
362 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
363 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
364 f->type);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300365
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300366 dev_dbg(&icd->dev, "set width: %d height: %d\n",
367 icd->width, icd->height);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300368
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300369 /* set physical bus parameters */
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300370 return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300371}
372
Hans Verkuil72937892008-06-01 10:35:18 -0300373static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300374 struct v4l2_fmtdesc *f)
375{
376 struct soc_camera_file *icf = file->private_data;
377 struct soc_camera_device *icd = icf->icd;
378 const struct soc_camera_data_format *format;
379
380 WARN_ON(priv != file->private_data);
381
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300382 if (f->index >= icd->num_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300383 return -EINVAL;
384
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300385 format = &icd->formats[f->index];
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300386
387 strlcpy(f->description, format->name, sizeof(f->description));
388 f->pixelformat = format->fourcc;
389 return 0;
390}
391
Hans Verkuil72937892008-06-01 10:35:18 -0300392static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300393 struct v4l2_format *f)
394{
395 struct soc_camera_file *icf = file->private_data;
396 struct soc_camera_device *icd = icf->icd;
397
398 WARN_ON(priv != file->private_data);
399
400 f->fmt.pix.width = icd->width;
401 f->fmt.pix.height = icd->height;
402 f->fmt.pix.field = icf->vb_vidq.field;
403 f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
404 f->fmt.pix.bytesperline =
405 (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
406 f->fmt.pix.sizeimage =
407 f->fmt.pix.height * f->fmt.pix.bytesperline;
408 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
409 icd->current_fmt->fourcc);
410 return 0;
411}
412
413static int soc_camera_querycap(struct file *file, void *priv,
414 struct v4l2_capability *cap)
415{
416 struct soc_camera_file *icf = file->private_data;
417 struct soc_camera_device *icd = icf->icd;
418 struct soc_camera_host *ici =
419 to_soc_camera_host(icd->dev.parent);
420
421 WARN_ON(priv != file->private_data);
422
423 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300424 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300425}
426
427static int soc_camera_streamon(struct file *file, void *priv,
428 enum v4l2_buf_type i)
429{
430 struct soc_camera_file *icf = file->private_data;
431 struct soc_camera_device *icd = icf->icd;
432
433 WARN_ON(priv != file->private_data);
434
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300435 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300436
437 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
438 return -EINVAL;
439
440 icd->ops->start_capture(icd);
441
442 /* This calls buf_queue from host driver's videobuf_queue_ops */
443 return videobuf_streamon(&icf->vb_vidq);
444}
445
446static int soc_camera_streamoff(struct file *file, void *priv,
447 enum v4l2_buf_type i)
448{
449 struct soc_camera_file *icf = file->private_data;
450 struct soc_camera_device *icd = icf->icd;
451
452 WARN_ON(priv != file->private_data);
453
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300454 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300455
456 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
457 return -EINVAL;
458
459 /* This calls buf_release from host driver's videobuf_queue_ops for all
460 * remaining buffers. When the last buffer is freed, stop capture */
461 videobuf_streamoff(&icf->vb_vidq);
462
463 icd->ops->stop_capture(icd);
464
465 return 0;
466}
467
468static int soc_camera_queryctrl(struct file *file, void *priv,
469 struct v4l2_queryctrl *qc)
470{
471 struct soc_camera_file *icf = file->private_data;
472 struct soc_camera_device *icd = icf->icd;
473 int i;
474
475 WARN_ON(priv != file->private_data);
476
477 if (!qc->id)
478 return -EINVAL;
479
480 for (i = 0; i < icd->ops->num_controls; i++)
481 if (qc->id == icd->ops->controls[i].id) {
482 memcpy(qc, &(icd->ops->controls[i]),
483 sizeof(*qc));
484 return 0;
485 }
486
487 return -EINVAL;
488}
489
490static int soc_camera_g_ctrl(struct file *file, void *priv,
491 struct v4l2_control *ctrl)
492{
493 struct soc_camera_file *icf = file->private_data;
494 struct soc_camera_device *icd = icf->icd;
495
496 WARN_ON(priv != file->private_data);
497
498 switch (ctrl->id) {
499 case V4L2_CID_GAIN:
500 if (icd->gain == (unsigned short)~0)
501 return -EINVAL;
502 ctrl->value = icd->gain;
503 return 0;
504 case V4L2_CID_EXPOSURE:
505 if (icd->exposure == (unsigned short)~0)
506 return -EINVAL;
507 ctrl->value = icd->exposure;
508 return 0;
509 }
510
511 if (icd->ops->get_control)
512 return icd->ops->get_control(icd, ctrl);
513 return -EINVAL;
514}
515
516static int soc_camera_s_ctrl(struct file *file, void *priv,
517 struct v4l2_control *ctrl)
518{
519 struct soc_camera_file *icf = file->private_data;
520 struct soc_camera_device *icd = icf->icd;
521
522 WARN_ON(priv != file->private_data);
523
524 if (icd->ops->set_control)
525 return icd->ops->set_control(icd, ctrl);
526 return -EINVAL;
527}
528
529static int soc_camera_cropcap(struct file *file, void *fh,
530 struct v4l2_cropcap *a)
531{
532 struct soc_camera_file *icf = file->private_data;
533 struct soc_camera_device *icd = icf->icd;
534
535 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
536 a->bounds.left = icd->x_min;
537 a->bounds.top = icd->y_min;
538 a->bounds.width = icd->width_max;
539 a->bounds.height = icd->height_max;
540 a->defrect.left = icd->x_min;
541 a->defrect.top = icd->y_min;
542 a->defrect.width = 640;
543 a->defrect.height = 480;
544 a->pixelaspect.numerator = 1;
545 a->pixelaspect.denominator = 1;
546
547 return 0;
548}
549
550static int soc_camera_g_crop(struct file *file, void *fh,
551 struct v4l2_crop *a)
552{
553 struct soc_camera_file *icf = file->private_data;
554 struct soc_camera_device *icd = icf->icd;
555
556 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
557 a->c.left = icd->x_current;
558 a->c.top = icd->y_current;
559 a->c.width = icd->width;
560 a->c.height = icd->height;
561
562 return 0;
563}
564
565static int soc_camera_s_crop(struct file *file, void *fh,
566 struct v4l2_crop *a)
567{
568 struct soc_camera_file *icf = file->private_data;
569 struct soc_camera_device *icd = icf->icd;
570 struct soc_camera_host *ici =
571 to_soc_camera_host(icd->dev.parent);
572 int ret;
573
574 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
575 return -EINVAL;
576
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300577 ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300578 if (!ret) {
579 icd->width = a->c.width;
580 icd->height = a->c.height;
581 icd->x_current = a->c.left;
582 icd->y_current = a->c.top;
583 }
584
585 return ret;
586}
587
588static int soc_camera_g_chip_ident(struct file *file, void *fh,
589 struct v4l2_chip_ident *id)
590{
591 struct soc_camera_file *icf = file->private_data;
592 struct soc_camera_device *icd = icf->icd;
593
594 if (!icd->ops->get_chip_id)
595 return -EINVAL;
596
597 return icd->ops->get_chip_id(icd, id);
598}
599
600#ifdef CONFIG_VIDEO_ADV_DEBUG
601static int soc_camera_g_register(struct file *file, void *fh,
602 struct v4l2_register *reg)
603{
604 struct soc_camera_file *icf = file->private_data;
605 struct soc_camera_device *icd = icf->icd;
606
607 if (!icd->ops->get_register)
608 return -EINVAL;
609
610 return icd->ops->get_register(icd, reg);
611}
612
613static int soc_camera_s_register(struct file *file, void *fh,
614 struct v4l2_register *reg)
615{
616 struct soc_camera_file *icf = file->private_data;
617 struct soc_camera_device *icd = icf->icd;
618
619 if (!icd->ops->set_register)
620 return -EINVAL;
621
622 return icd->ops->set_register(icd, reg);
623}
624#endif
625
626static int device_register_link(struct soc_camera_device *icd)
627{
628 int ret = device_register(&icd->dev);
629
630 if (ret < 0) {
631 /* Prevent calling device_unregister() */
632 icd->dev.parent = NULL;
633 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
634 /* Even if probe() was unsuccessful for all registered drivers,
635 * device_register() returns 0, and we add the link, just to
636 * document this camera's control device */
637 } else if (icd->control)
638 /* Have to sysfs_remove_link() before device_unregister()? */
639 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
640 "control"))
641 dev_warn(&icd->dev,
642 "Failed creating the control symlink\n");
643 return ret;
644}
645
646/* So far this function cannot fail */
647static void scan_add_host(struct soc_camera_host *ici)
648{
649 struct soc_camera_device *icd;
650
651 mutex_lock(&list_lock);
652
653 list_for_each_entry(icd, &devices, list) {
654 if (icd->iface == ici->nr) {
655 icd->dev.parent = &ici->dev;
656 device_register_link(icd);
657 }
658 }
659
660 mutex_unlock(&list_lock);
661}
662
663/* return: 0 if no match found or a match found and
664 * device_register() successful, error code otherwise */
665static int scan_add_device(struct soc_camera_device *icd)
666{
667 struct soc_camera_host *ici;
668 int ret = 0;
669
670 mutex_lock(&list_lock);
671
672 list_add_tail(&icd->list, &devices);
673
674 /* Watch out for class_for_each_device / class_find_device API by
675 * Dave Young <hidave.darkstar@gmail.com> */
676 list_for_each_entry(ici, &hosts, list) {
677 if (icd->iface == ici->nr) {
678 ret = 1;
679 icd->dev.parent = &ici->dev;
680 break;
681 }
682 }
683
684 mutex_unlock(&list_lock);
685
686 if (ret)
687 ret = device_register_link(icd);
688
689 return ret;
690}
691
692static int soc_camera_probe(struct device *dev)
693{
694 struct soc_camera_device *icd = to_soc_camera_dev(dev);
695 struct soc_camera_host *ici =
696 to_soc_camera_host(icd->dev.parent);
697 int ret;
698
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300699 if (!icd->ops->probe)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300700 return -ENODEV;
701
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300702 /* We only call ->add() here to activate and probe the camera.
703 * We shall ->remove() and deactivate it immediately afterwards. */
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300704 ret = ici->ops->add(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300705 if (ret < 0)
706 return ret;
707
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300708 ret = icd->ops->probe(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300709 if (ret >= 0) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300710 const struct v4l2_queryctrl *qctrl;
711
712 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
713 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
714 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
715 icd->exposure = qctrl ? qctrl->default_value :
716 (unsigned short)~0;
717 }
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300718 ici->ops->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300719
720 return ret;
721}
722
723/* This is called on device_unregister, which only means we have to disconnect
724 * from the host, but not remove ourselves from the device list */
725static int soc_camera_remove(struct device *dev)
726{
727 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300728
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300729 if (icd->ops->remove)
730 icd->ops->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300731
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300732 return 0;
733}
734
Robert Jarzmik2e521062008-08-01 20:14:50 -0300735static int soc_camera_suspend(struct device *dev, pm_message_t state)
736{
737 struct soc_camera_device *icd = to_soc_camera_dev(dev);
738 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
739 int ret = 0;
740
741 if (ici->ops->suspend)
742 ret = ici->ops->suspend(icd, state);
743
744 return ret;
745}
746
747static int soc_camera_resume(struct device *dev)
748{
749 struct soc_camera_device *icd = to_soc_camera_dev(dev);
750 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
751 int ret = 0;
752
753 if (ici->ops->resume)
754 ret = ici->ops->resume(icd);
755
756 return ret;
757}
758
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300759static struct bus_type soc_camera_bus_type = {
760 .name = "soc-camera",
761 .probe = soc_camera_probe,
762 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -0300763 .suspend = soc_camera_suspend,
764 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300765};
766
767static struct device_driver ic_drv = {
768 .name = "camera",
769 .bus = &soc_camera_bus_type,
770 .owner = THIS_MODULE,
771};
772
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300773static void dummy_release(struct device *dev)
774{
775}
776
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300777int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300778{
779 int ret;
780 struct soc_camera_host *ix;
781
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300782 if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300783 return -EINVAL;
784
785 /* Number might be equal to the platform device ID */
786 sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300787
788 mutex_lock(&list_lock);
789 list_for_each_entry(ix, &hosts, list) {
790 if (ix->nr == ici->nr) {
791 mutex_unlock(&list_lock);
792 return -EBUSY;
793 }
794 }
795
796 list_add_tail(&ici->list, &hosts);
797 mutex_unlock(&list_lock);
798
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300799 ici->dev.release = dummy_release;
800
801 ret = device_register(&ici->dev);
802
803 if (ret)
804 goto edevr;
805
806 scan_add_host(ici);
807
808 return 0;
809
810edevr:
811 mutex_lock(&list_lock);
812 list_del(&ici->list);
813 mutex_unlock(&list_lock);
814
815 return ret;
816}
817EXPORT_SYMBOL(soc_camera_host_register);
818
819/* Unregister all clients! */
820void soc_camera_host_unregister(struct soc_camera_host *ici)
821{
822 struct soc_camera_device *icd;
823
824 mutex_lock(&list_lock);
825
826 list_del(&ici->list);
827
828 list_for_each_entry(icd, &devices, list) {
829 if (icd->dev.parent == &ici->dev) {
830 device_unregister(&icd->dev);
831 /* Not before device_unregister(), .remove
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300832 * needs parent to call ici->ops->remove() */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300833 icd->dev.parent = NULL;
834 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
835 }
836 }
837
838 mutex_unlock(&list_lock);
839
840 device_unregister(&ici->dev);
841}
842EXPORT_SYMBOL(soc_camera_host_unregister);
843
844/* Image capture device */
845int soc_camera_device_register(struct soc_camera_device *icd)
846{
847 struct soc_camera_device *ix;
848 int num = -1, i;
849
850 if (!icd)
851 return -EINVAL;
852
853 for (i = 0; i < 256 && num < 0; i++) {
854 num = i;
855 list_for_each_entry(ix, &devices, list) {
856 if (ix->iface == icd->iface && ix->devnum == i) {
857 num = -1;
858 break;
859 }
860 }
861 }
862
863 if (num < 0)
864 /* ok, we have 256 cameras on this host...
865 * man, stay reasonable... */
866 return -ENOMEM;
867
868 icd->devnum = num;
869 icd->dev.bus = &soc_camera_bus_type;
870 snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
871 "%u-%u", icd->iface, icd->devnum);
872
873 icd->dev.release = dummy_release;
874
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300875 return scan_add_device(icd);
876}
877EXPORT_SYMBOL(soc_camera_device_register);
878
879void soc_camera_device_unregister(struct soc_camera_device *icd)
880{
881 mutex_lock(&list_lock);
882 list_del(&icd->list);
883
884 /* The bus->remove will be eventually called */
885 if (icd->dev.parent)
886 device_unregister(&icd->dev);
887 mutex_unlock(&list_lock);
888}
889EXPORT_SYMBOL(soc_camera_device_unregister);
890
Hans Verkuila3998102008-07-21 02:57:38 -0300891static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
892 .vidioc_querycap = soc_camera_querycap,
893 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
894 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
895 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
896 .vidioc_enum_input = soc_camera_enum_input,
897 .vidioc_g_input = soc_camera_g_input,
898 .vidioc_s_input = soc_camera_s_input,
899 .vidioc_s_std = soc_camera_s_std,
900 .vidioc_reqbufs = soc_camera_reqbufs,
901 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
902 .vidioc_querybuf = soc_camera_querybuf,
903 .vidioc_qbuf = soc_camera_qbuf,
904 .vidioc_dqbuf = soc_camera_dqbuf,
905 .vidioc_streamon = soc_camera_streamon,
906 .vidioc_streamoff = soc_camera_streamoff,
907 .vidioc_queryctrl = soc_camera_queryctrl,
908 .vidioc_g_ctrl = soc_camera_g_ctrl,
909 .vidioc_s_ctrl = soc_camera_s_ctrl,
910 .vidioc_cropcap = soc_camera_cropcap,
911 .vidioc_g_crop = soc_camera_g_crop,
912 .vidioc_s_crop = soc_camera_s_crop,
913 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
914#ifdef CONFIG_VIDEO_ADV_DEBUG
915 .vidioc_g_register = soc_camera_g_register,
916 .vidioc_s_register = soc_camera_s_register,
917#endif
918};
919
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300920int soc_camera_video_start(struct soc_camera_device *icd)
921{
922 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
923 int err = -ENOMEM;
924 struct video_device *vdev;
925
926 if (!icd->dev.parent)
927 return -ENODEV;
928
929 vdev = video_device_alloc();
930 if (!vdev)
931 goto evidallocd;
932 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
933
934 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
935 /* Maybe better &ici->dev */
Hans Verkuil5e85e732008-07-20 06:31:39 -0300936 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300937 vdev->current_norm = V4L2_STD_UNKNOWN;
938 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -0300939 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300940 vdev->release = video_device_release;
941 vdev->minor = -1;
942 vdev->tvnorms = V4L2_STD_UNKNOWN,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300943
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300944 icd->current_fmt = &icd->formats[0];
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300945
946 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
947 if (err < 0) {
Hans Verkuil5e85e732008-07-20 06:31:39 -0300948 dev_err(vdev->parent, "video_register_device failed\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300949 goto evidregd;
950 }
951 icd->vdev = vdev;
952
953 return 0;
954
955evidregd:
956 video_device_release(vdev);
957evidallocd:
958 return err;
959}
960EXPORT_SYMBOL(soc_camera_video_start);
961
962void soc_camera_video_stop(struct soc_camera_device *icd)
963{
964 struct video_device *vdev = icd->vdev;
965
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300966 dev_dbg(&icd->dev, "%s\n", __func__);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300967
968 if (!icd->dev.parent || !vdev)
969 return;
970
971 mutex_lock(&video_lock);
972 video_unregister_device(vdev);
973 icd->vdev = NULL;
974 mutex_unlock(&video_lock);
975}
976EXPORT_SYMBOL(soc_camera_video_stop);
977
978static int __init soc_camera_init(void)
979{
980 int ret = bus_register(&soc_camera_bus_type);
981 if (ret)
982 return ret;
983 ret = driver_register(&ic_drv);
984 if (ret)
985 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300986
987 return 0;
988
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300989edrvr:
990 bus_unregister(&soc_camera_bus_type);
991 return ret;
992}
993
994static void __exit soc_camera_exit(void)
995{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300996 driver_unregister(&ic_drv);
997 bus_unregister(&soc_camera_bus_type);
998}
999
1000module_init(soc_camera_init);
1001module_exit(soc_camera_exit);
1002
1003MODULE_DESCRIPTION("Image capture bus driver");
1004MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1005MODULE_LICENSE("GPL");