V4L/DVB (9787): soc-camera: let camera host drivers decide upon pixel format
[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
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>
35ea11ff 28#include <media/v4l2-ioctl.h>
e55222ef 29#include <media/v4l2-dev.h>
092d3921 30#include <media/videobuf-core.h>
e55222ef
GL
31#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
25c4d74e 38const struct soc_camera_data_format *soc_camera_format_by_fourcc(
abe4c471 39 struct soc_camera_device *icd, unsigned int fourcc)
e55222ef
GL
40{
41 unsigned int i;
42
26f1b942
GL
43 for (i = 0; i < icd->num_formats; i++)
44 if (icd->formats[i].fourcc == fourcc)
45 return icd->formats + i;
e55222ef
GL
46 return NULL;
47}
25c4d74e 48EXPORT_SYMBOL(soc_camera_format_by_fourcc);
e55222ef 49
72937890 50static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
abe4c471 51 struct v4l2_format *f)
e55222ef
GL
52{
53 struct soc_camera_file *icf = file->private_data;
54 struct soc_camera_device *icd = icf->icd;
55 struct soc_camera_host *ici =
56 to_soc_camera_host(icd->dev.parent);
57 enum v4l2_field field;
e55222ef
GL
58 int ret;
59
60 WARN_ON(priv != file->private_data);
61
25c4d74e
GL
62 /*
63 * TODO: this might also have to migrate to host-drivers, if anyone
64 * wishes to support other fields
65 */
e55222ef
GL
66 field = f->fmt.pix.field;
67
68 if (field == V4L2_FIELD_ANY) {
25c4d74e
GL
69 f->fmt.pix.field = V4L2_FIELD_NONE;
70 } else if (field != V4L2_FIELD_NONE) {
e55222ef
GL
71 dev_err(&icd->dev, "Field type invalid.\n");
72 return -EINVAL;
73 }
74
ad5f2e85 75 /* limit format to hardware capabilities */
b8d9904c 76 ret = ici->ops->try_fmt_cap(icd, f);
e55222ef 77
e55222ef
GL
78 return ret;
79}
80
81static int soc_camera_enum_input(struct file *file, void *priv,
82 struct v4l2_input *inp)
83{
84 if (inp->index != 0)
85 return -EINVAL;
86
87 inp->type = V4L2_INPUT_TYPE_CAMERA;
88 inp->std = V4L2_STD_UNKNOWN;
89 strcpy(inp->name, "Camera");
90
91 return 0;
92}
93
94static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
95{
96 *i = 0;
97
98 return 0;
99}
100
101static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
102{
103 if (i > 0)
104 return -EINVAL;
105
106 return 0;
107}
108
109static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
110{
111 return 0;
112}
113
114static int soc_camera_reqbufs(struct file *file, void *priv,
115 struct v4l2_requestbuffers *p)
116{
117 int ret;
118 struct soc_camera_file *icf = file->private_data;
119 struct soc_camera_device *icd = icf->icd;
120 struct soc_camera_host *ici =
121 to_soc_camera_host(icd->dev.parent);
122
123 WARN_ON(priv != file->private_data);
124
7e28adb2 125 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
e55222ef
GL
126
127 ret = videobuf_reqbufs(&icf->vb_vidq, p);
128 if (ret < 0)
129 return ret;
130
b8d9904c 131 return ici->ops->reqbufs(icf, p);
e55222ef
GL
132}
133
134static int soc_camera_querybuf(struct file *file, void *priv,
135 struct v4l2_buffer *p)
136{
137 struct soc_camera_file *icf = file->private_data;
138
139 WARN_ON(priv != file->private_data);
140
141 return videobuf_querybuf(&icf->vb_vidq, p);
142}
143
144static int soc_camera_qbuf(struct file *file, void *priv,
145 struct v4l2_buffer *p)
146{
147 struct soc_camera_file *icf = file->private_data;
148
149 WARN_ON(priv != file->private_data);
150
151 return videobuf_qbuf(&icf->vb_vidq, p);
152}
153
154static int soc_camera_dqbuf(struct file *file, void *priv,
155 struct v4l2_buffer *p)
156{
157 struct soc_camera_file *icf = file->private_data;
158
159 WARN_ON(priv != file->private_data);
160
161 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
162}
163
164static int soc_camera_open(struct inode *inode, struct file *file)
165{
9dc4e48f
GL
166 struct video_device *vdev;
167 struct soc_camera_device *icd;
168 struct soc_camera_host *ici;
e55222ef
GL
169 struct soc_camera_file *icf;
170 int ret;
171
172 icf = vmalloc(sizeof(*icf));
173 if (!icf)
174 return -ENOMEM;
175
9dc4e48f
GL
176 /* Protect against icd->remove() until we module_get() both drivers. */
177 mutex_lock(&video_lock);
178
179 vdev = video_devdata(file);
5e85e732 180 icd = container_of(vdev->parent, struct soc_camera_device, dev);
9dc4e48f 181 ici = to_soc_camera_host(icd->dev.parent);
e55222ef
GL
182
183 if (!try_module_get(icd->ops->owner)) {
184 dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
185 ret = -EINVAL;
186 goto emgd;
187 }
188
b8d9904c 189 if (!try_module_get(ici->ops->owner)) {
e55222ef
GL
190 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
191 ret = -EINVAL;
192 goto emgi;
193 }
194
9dc4e48f 195 icf->icd = icd;
1a0063a9
GL
196 icd->use_count++;
197
9dc4e48f
GL
198 /* Now we really have to activate the camera */
199 if (icd->use_count == 1) {
b8d9904c 200 ret = ici->ops->add(icd);
9dc4e48f
GL
201 if (ret < 0) {
202 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
203 icd->use_count--;
204 goto eiciadd;
205 }
206 }
207
208 mutex_unlock(&video_lock);
209
e55222ef
GL
210 file->private_data = icf;
211 dev_dbg(&icd->dev, "camera device open\n");
212
a034d1b7 213 ici->ops->init_videobuf(&icf->vb_vidq, icd);
e55222ef
GL
214
215 return 0;
216
9dc4e48f
GL
217 /* All errors are entered with the video_lock held */
218eiciadd:
b8d9904c 219 module_put(ici->ops->owner);
e55222ef
GL
220emgi:
221 module_put(icd->ops->owner);
222emgd:
9dc4e48f 223 mutex_unlock(&video_lock);
e55222ef
GL
224 vfree(icf);
225 return ret;
226}
227
228static int soc_camera_close(struct inode *inode, struct file *file)
229{
230 struct soc_camera_file *icf = file->private_data;
231 struct soc_camera_device *icd = icf->icd;
232 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
233 struct video_device *vdev = icd->vdev;
234
9dc4e48f
GL
235 mutex_lock(&video_lock);
236 icd->use_count--;
237 if (!icd->use_count)
b8d9904c 238 ici->ops->remove(icd);
e55222ef 239 module_put(icd->ops->owner);
b8d9904c 240 module_put(ici->ops->owner);
9dc4e48f
GL
241 mutex_unlock(&video_lock);
242
1a0063a9 243 vfree(icf);
e55222ef 244
5e85e732 245 dev_dbg(vdev->parent, "camera device close\n");
e55222ef
GL
246
247 return 0;
248}
249
aba360d8 250static ssize_t soc_camera_read(struct file *file, char __user *buf,
abe4c471 251 size_t count, loff_t *ppos)
e55222ef
GL
252{
253 struct soc_camera_file *icf = file->private_data;
254 struct soc_camera_device *icd = icf->icd;
255 struct video_device *vdev = icd->vdev;
256 int err = -EINVAL;
257
5e85e732 258 dev_err(vdev->parent, "camera device read not implemented\n");
e55222ef
GL
259
260 return err;
261}
262
263static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
264{
265 struct soc_camera_file *icf = file->private_data;
266 struct soc_camera_device *icd = icf->icd;
267 int err;
268
269 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
270
271 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
272
273 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
274 (unsigned long)vma->vm_start,
275 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
276 err);
277
278 return err;
279}
280
281static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
282{
283 struct soc_camera_file *icf = file->private_data;
284 struct soc_camera_device *icd = icf->icd;
285 struct soc_camera_host *ici =
286 to_soc_camera_host(icd->dev.parent);
287
288 if (list_empty(&icf->vb_vidq.stream)) {
289 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
290 return POLLERR;
291 }
292
b8d9904c 293 return ici->ops->poll(file, pt);
e55222ef
GL
294}
295
e55222ef
GL
296static struct file_operations soc_camera_fops = {
297 .owner = THIS_MODULE,
298 .open = soc_camera_open,
299 .release = soc_camera_close,
300 .ioctl = video_ioctl2,
301 .read = soc_camera_read,
302 .mmap = soc_camera_mmap,
303 .poll = soc_camera_poll,
304 .llseek = no_llseek,
305};
306
72937890 307static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
abe4c471 308 struct v4l2_format *f)
e55222ef
GL
309{
310 struct soc_camera_file *icf = file->private_data;
311 struct soc_camera_device *icd = icf->icd;
312 struct soc_camera_host *ici =
313 to_soc_camera_host(icd->dev.parent);
314 int ret;
315 struct v4l2_rect rect;
e55222ef
GL
316
317 WARN_ON(priv != file->private_data);
318
25c4d74e 319 ret = soc_camera_try_fmt_vid_cap(file, priv, f);
e55222ef
GL
320 if (ret < 0)
321 return ret;
322
323 rect.left = icd->x_current;
324 rect.top = icd->y_current;
325 rect.width = f->fmt.pix.width;
326 rect.height = f->fmt.pix.height;
b8d9904c 327 ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
25c4d74e 328 if (ret < 0) {
ad5f2e85 329 return ret;
25c4d74e
GL
330 } else if (!icd->current_fmt ||
331 icd->current_fmt->fourcc != f->fmt.pix.pixelformat) {
332 dev_err(&ici->dev, "Host driver hasn't set up current "
333 "format correctly!\n");
334 return -EINVAL;
335 }
e55222ef 336
25c4d74e
GL
337 /* buswidth may be further adjusted by the ici */
338 icd->buswidth = icd->current_fmt->depth;
ad5f2e85
GL
339 icd->width = rect.width;
340 icd->height = rect.height;
341 icf->vb_vidq.field = f->fmt.pix.field;
25c4d74e 342 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
ad5f2e85
GL
343 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
344 f->type);
e55222ef 345
ad5f2e85
GL
346 dev_dbg(&icd->dev, "set width: %d height: %d\n",
347 icd->width, icd->height);
348
349 /* set physical bus parameters */
b8d9904c 350 return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
e55222ef
GL
351}
352
72937890 353static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
abe4c471 354 struct v4l2_fmtdesc *f)
e55222ef
GL
355{
356 struct soc_camera_file *icf = file->private_data;
357 struct soc_camera_device *icd = icf->icd;
358 const struct soc_camera_data_format *format;
359
360 WARN_ON(priv != file->private_data);
361
26f1b942 362 if (f->index >= icd->num_formats)
e55222ef
GL
363 return -EINVAL;
364
26f1b942 365 format = &icd->formats[f->index];
e55222ef
GL
366
367 strlcpy(f->description, format->name, sizeof(f->description));
368 f->pixelformat = format->fourcc;
369 return 0;
370}
371
72937890 372static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
abe4c471 373 struct v4l2_format *f)
e55222ef
GL
374{
375 struct soc_camera_file *icf = file->private_data;
376 struct soc_camera_device *icd = icf->icd;
377
378 WARN_ON(priv != file->private_data);
379
380 f->fmt.pix.width = icd->width;
381 f->fmt.pix.height = icd->height;
382 f->fmt.pix.field = icf->vb_vidq.field;
383 f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
25c4d74e
GL
384 f->fmt.pix.bytesperline = f->fmt.pix.width *
385 DIV_ROUND_UP(icd->current_fmt->depth, 8);
386 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
e55222ef
GL
387 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
388 icd->current_fmt->fourcc);
389 return 0;
390}
391
392static int soc_camera_querycap(struct file *file, void *priv,
393 struct v4l2_capability *cap)
394{
395 struct soc_camera_file *icf = file->private_data;
396 struct soc_camera_device *icd = icf->icd;
397 struct soc_camera_host *ici =
398 to_soc_camera_host(icd->dev.parent);
399
400 WARN_ON(priv != file->private_data);
401
402 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
b8d9904c 403 return ici->ops->querycap(ici, cap);
e55222ef
GL
404}
405
406static int soc_camera_streamon(struct file *file, void *priv,
407 enum v4l2_buf_type i)
408{
409 struct soc_camera_file *icf = file->private_data;
410 struct soc_camera_device *icd = icf->icd;
411
412 WARN_ON(priv != file->private_data);
413
7e28adb2 414 dev_dbg(&icd->dev, "%s\n", __func__);
e55222ef
GL
415
416 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
417 return -EINVAL;
418
419 icd->ops->start_capture(icd);
420
421 /* This calls buf_queue from host driver's videobuf_queue_ops */
422 return videobuf_streamon(&icf->vb_vidq);
423}
424
425static int soc_camera_streamoff(struct file *file, void *priv,
426 enum v4l2_buf_type i)
427{
428 struct soc_camera_file *icf = file->private_data;
429 struct soc_camera_device *icd = icf->icd;
430
431 WARN_ON(priv != file->private_data);
432
7e28adb2 433 dev_dbg(&icd->dev, "%s\n", __func__);
e55222ef
GL
434
435 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
436 return -EINVAL;
437
438 /* This calls buf_release from host driver's videobuf_queue_ops for all
439 * remaining buffers. When the last buffer is freed, stop capture */
440 videobuf_streamoff(&icf->vb_vidq);
441
442 icd->ops->stop_capture(icd);
443
444 return 0;
445}
446
447static int soc_camera_queryctrl(struct file *file, void *priv,
448 struct v4l2_queryctrl *qc)
449{
450 struct soc_camera_file *icf = file->private_data;
451 struct soc_camera_device *icd = icf->icd;
452 int i;
453
454 WARN_ON(priv != file->private_data);
455
456 if (!qc->id)
457 return -EINVAL;
458
459 for (i = 0; i < icd->ops->num_controls; i++)
460 if (qc->id == icd->ops->controls[i].id) {
461 memcpy(qc, &(icd->ops->controls[i]),
462 sizeof(*qc));
463 return 0;
464 }
465
466 return -EINVAL;
467}
468
469static int soc_camera_g_ctrl(struct file *file, void *priv,
470 struct v4l2_control *ctrl)
471{
472 struct soc_camera_file *icf = file->private_data;
473 struct soc_camera_device *icd = icf->icd;
474
475 WARN_ON(priv != file->private_data);
476
477 switch (ctrl->id) {
478 case V4L2_CID_GAIN:
479 if (icd->gain == (unsigned short)~0)
480 return -EINVAL;
481 ctrl->value = icd->gain;
482 return 0;
483 case V4L2_CID_EXPOSURE:
484 if (icd->exposure == (unsigned short)~0)
485 return -EINVAL;
486 ctrl->value = icd->exposure;
487 return 0;
488 }
489
490 if (icd->ops->get_control)
491 return icd->ops->get_control(icd, ctrl);
492 return -EINVAL;
493}
494
495static int soc_camera_s_ctrl(struct file *file, void *priv,
496 struct v4l2_control *ctrl)
497{
498 struct soc_camera_file *icf = file->private_data;
499 struct soc_camera_device *icd = icf->icd;
500
501 WARN_ON(priv != file->private_data);
502
503 if (icd->ops->set_control)
504 return icd->ops->set_control(icd, ctrl);
505 return -EINVAL;
506}
507
508static int soc_camera_cropcap(struct file *file, void *fh,
509 struct v4l2_cropcap *a)
510{
511 struct soc_camera_file *icf = file->private_data;
512 struct soc_camera_device *icd = icf->icd;
513
514 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
515 a->bounds.left = icd->x_min;
516 a->bounds.top = icd->y_min;
517 a->bounds.width = icd->width_max;
518 a->bounds.height = icd->height_max;
519 a->defrect.left = icd->x_min;
520 a->defrect.top = icd->y_min;
521 a->defrect.width = 640;
522 a->defrect.height = 480;
523 a->pixelaspect.numerator = 1;
524 a->pixelaspect.denominator = 1;
525
526 return 0;
527}
528
529static int soc_camera_g_crop(struct file *file, void *fh,
530 struct v4l2_crop *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->c.left = icd->x_current;
537 a->c.top = icd->y_current;
538 a->c.width = icd->width;
539 a->c.height = icd->height;
540
541 return 0;
542}
543
544static int soc_camera_s_crop(struct file *file, void *fh,
545 struct v4l2_crop *a)
546{
547 struct soc_camera_file *icf = file->private_data;
548 struct soc_camera_device *icd = icf->icd;
549 struct soc_camera_host *ici =
550 to_soc_camera_host(icd->dev.parent);
551 int ret;
552
553 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
554 return -EINVAL;
555
b8d9904c 556 ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
e55222ef
GL
557 if (!ret) {
558 icd->width = a->c.width;
559 icd->height = a->c.height;
560 icd->x_current = a->c.left;
561 icd->y_current = a->c.top;
562 }
563
564 return ret;
565}
566
567static int soc_camera_g_chip_ident(struct file *file, void *fh,
568 struct v4l2_chip_ident *id)
569{
570 struct soc_camera_file *icf = file->private_data;
571 struct soc_camera_device *icd = icf->icd;
572
573 if (!icd->ops->get_chip_id)
574 return -EINVAL;
575
576 return icd->ops->get_chip_id(icd, id);
577}
578
579#ifdef CONFIG_VIDEO_ADV_DEBUG
580static int soc_camera_g_register(struct file *file, void *fh,
581 struct v4l2_register *reg)
582{
583 struct soc_camera_file *icf = file->private_data;
584 struct soc_camera_device *icd = icf->icd;
585
586 if (!icd->ops->get_register)
587 return -EINVAL;
588
589 return icd->ops->get_register(icd, reg);
590}
591
592static int soc_camera_s_register(struct file *file, void *fh,
593 struct v4l2_register *reg)
594{
595 struct soc_camera_file *icf = file->private_data;
596 struct soc_camera_device *icd = icf->icd;
597
598 if (!icd->ops->set_register)
599 return -EINVAL;
600
601 return icd->ops->set_register(icd, reg);
602}
603#endif
604
605static int device_register_link(struct soc_camera_device *icd)
606{
607 int ret = device_register(&icd->dev);
608
609 if (ret < 0) {
610 /* Prevent calling device_unregister() */
611 icd->dev.parent = NULL;
612 dev_err(&icd->dev, "Cannot register device: %d\n", ret);
613 /* Even if probe() was unsuccessful for all registered drivers,
614 * device_register() returns 0, and we add the link, just to
615 * document this camera's control device */
616 } else if (icd->control)
617 /* Have to sysfs_remove_link() before device_unregister()? */
618 if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
619 "control"))
620 dev_warn(&icd->dev,
621 "Failed creating the control symlink\n");
622 return ret;
623}
624
625/* So far this function cannot fail */
626static void scan_add_host(struct soc_camera_host *ici)
627{
628 struct soc_camera_device *icd;
629
630 mutex_lock(&list_lock);
631
632 list_for_each_entry(icd, &devices, list) {
633 if (icd->iface == ici->nr) {
634 icd->dev.parent = &ici->dev;
635 device_register_link(icd);
636 }
637 }
638
639 mutex_unlock(&list_lock);
640}
641
642/* return: 0 if no match found or a match found and
643 * device_register() successful, error code otherwise */
644static int scan_add_device(struct soc_camera_device *icd)
645{
646 struct soc_camera_host *ici;
647 int ret = 0;
648
649 mutex_lock(&list_lock);
650
651 list_add_tail(&icd->list, &devices);
652
653 /* Watch out for class_for_each_device / class_find_device API by
654 * Dave Young <hidave.darkstar@gmail.com> */
655 list_for_each_entry(ici, &hosts, list) {
656 if (icd->iface == ici->nr) {
657 ret = 1;
658 icd->dev.parent = &ici->dev;
659 break;
660 }
661 }
662
663 mutex_unlock(&list_lock);
664
665 if (ret)
666 ret = device_register_link(icd);
667
668 return ret;
669}
670
671static int soc_camera_probe(struct device *dev)
672{
673 struct soc_camera_device *icd = to_soc_camera_dev(dev);
674 struct soc_camera_host *ici =
675 to_soc_camera_host(icd->dev.parent);
676 int ret;
677
26f1b942 678 if (!icd->ops->probe)
e55222ef
GL
679 return -ENODEV;
680
9dc4e48f
GL
681 /* We only call ->add() here to activate and probe the camera.
682 * We shall ->remove() and deactivate it immediately afterwards. */
b8d9904c 683 ret = ici->ops->add(icd);
e55222ef
GL
684 if (ret < 0)
685 return ret;
686
26f1b942 687 ret = icd->ops->probe(icd);
9dc4e48f 688 if (ret >= 0) {
e55222ef
GL
689 const struct v4l2_queryctrl *qctrl;
690
691 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
692 icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
693 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
694 icd->exposure = qctrl ? qctrl->default_value :
695 (unsigned short)~0;
696 }
b8d9904c 697 ici->ops->remove(icd);
e55222ef
GL
698
699 return ret;
700}
701
702/* This is called on device_unregister, which only means we have to disconnect
703 * from the host, but not remove ourselves from the device list */
704static int soc_camera_remove(struct device *dev)
705{
706 struct soc_camera_device *icd = to_soc_camera_dev(dev);
e55222ef 707
26f1b942
GL
708 if (icd->ops->remove)
709 icd->ops->remove(icd);
e55222ef 710
e55222ef
GL
711 return 0;
712}
713
2e521061
RJ
714static int soc_camera_suspend(struct device *dev, pm_message_t state)
715{
716 struct soc_camera_device *icd = to_soc_camera_dev(dev);
717 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
718 int ret = 0;
719
720 if (ici->ops->suspend)
721 ret = ici->ops->suspend(icd, state);
722
723 return ret;
724}
725
726static int soc_camera_resume(struct device *dev)
727{
728 struct soc_camera_device *icd = to_soc_camera_dev(dev);
729 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
730 int ret = 0;
731
732 if (ici->ops->resume)
733 ret = ici->ops->resume(icd);
734
735 return ret;
736}
737
e55222ef
GL
738static struct bus_type soc_camera_bus_type = {
739 .name = "soc-camera",
740 .probe = soc_camera_probe,
741 .remove = soc_camera_remove,
2e521061
RJ
742 .suspend = soc_camera_suspend,
743 .resume = soc_camera_resume,
e55222ef
GL
744};
745
746static struct device_driver ic_drv = {
747 .name = "camera",
748 .bus = &soc_camera_bus_type,
749 .owner = THIS_MODULE,
750};
751
e55222ef
GL
752static void dummy_release(struct device *dev)
753{
754}
755
b8d9904c 756int soc_camera_host_register(struct soc_camera_host *ici)
e55222ef
GL
757{
758 int ret;
759 struct soc_camera_host *ix;
760
092d3921 761 if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
e55222ef
GL
762 return -EINVAL;
763
764 /* Number might be equal to the platform device ID */
af128a10 765 dev_set_name(&ici->dev, "camera_host%d", ici->nr);
e55222ef
GL
766
767 mutex_lock(&list_lock);
768 list_for_each_entry(ix, &hosts, list) {
769 if (ix->nr == ici->nr) {
770 mutex_unlock(&list_lock);
771 return -EBUSY;
772 }
773 }
774
775 list_add_tail(&ici->list, &hosts);
776 mutex_unlock(&list_lock);
777
e55222ef
GL
778 ici->dev.release = dummy_release;
779
780 ret = device_register(&ici->dev);
781
782 if (ret)
783 goto edevr;
784
785 scan_add_host(ici);
786
787 return 0;
788
789edevr:
790 mutex_lock(&list_lock);
791 list_del(&ici->list);
792 mutex_unlock(&list_lock);
793
794 return ret;
795}
796EXPORT_SYMBOL(soc_camera_host_register);
797
798/* Unregister all clients! */
799void soc_camera_host_unregister(struct soc_camera_host *ici)
800{
801 struct soc_camera_device *icd;
802
803 mutex_lock(&list_lock);
804
805 list_del(&ici->list);
806
807 list_for_each_entry(icd, &devices, list) {
808 if (icd->dev.parent == &ici->dev) {
809 device_unregister(&icd->dev);
810 /* Not before device_unregister(), .remove
b8d9904c 811 * needs parent to call ici->ops->remove() */
e55222ef
GL
812 icd->dev.parent = NULL;
813 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
814 }
815 }
816
817 mutex_unlock(&list_lock);
818
819 device_unregister(&ici->dev);
820}
821EXPORT_SYMBOL(soc_camera_host_unregister);
822
823/* Image capture device */
824int soc_camera_device_register(struct soc_camera_device *icd)
825{
826 struct soc_camera_device *ix;
827 int num = -1, i;
828
829 if (!icd)
830 return -EINVAL;
831
832 for (i = 0; i < 256 && num < 0; i++) {
833 num = i;
834 list_for_each_entry(ix, &devices, list) {
835 if (ix->iface == icd->iface && ix->devnum == i) {
836 num = -1;
837 break;
838 }
839 }
840 }
841
842 if (num < 0)
843 /* ok, we have 256 cameras on this host...
844 * man, stay reasonable... */
845 return -ENOMEM;
846
847 icd->devnum = num;
848 icd->dev.bus = &soc_camera_bus_type;
af128a10 849 dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
e55222ef
GL
850
851 icd->dev.release = dummy_release;
852
e55222ef
GL
853 return scan_add_device(icd);
854}
855EXPORT_SYMBOL(soc_camera_device_register);
856
857void soc_camera_device_unregister(struct soc_camera_device *icd)
858{
859 mutex_lock(&list_lock);
860 list_del(&icd->list);
861
862 /* The bus->remove will be eventually called */
863 if (icd->dev.parent)
864 device_unregister(&icd->dev);
865 mutex_unlock(&list_lock);
866}
867EXPORT_SYMBOL(soc_camera_device_unregister);
868
a399810c
HV
869static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
870 .vidioc_querycap = soc_camera_querycap,
871 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
872 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
873 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
874 .vidioc_enum_input = soc_camera_enum_input,
875 .vidioc_g_input = soc_camera_g_input,
876 .vidioc_s_input = soc_camera_s_input,
877 .vidioc_s_std = soc_camera_s_std,
878 .vidioc_reqbufs = soc_camera_reqbufs,
879 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
880 .vidioc_querybuf = soc_camera_querybuf,
881 .vidioc_qbuf = soc_camera_qbuf,
882 .vidioc_dqbuf = soc_camera_dqbuf,
883 .vidioc_streamon = soc_camera_streamon,
884 .vidioc_streamoff = soc_camera_streamoff,
885 .vidioc_queryctrl = soc_camera_queryctrl,
886 .vidioc_g_ctrl = soc_camera_g_ctrl,
887 .vidioc_s_ctrl = soc_camera_s_ctrl,
888 .vidioc_cropcap = soc_camera_cropcap,
889 .vidioc_g_crop = soc_camera_g_crop,
890 .vidioc_s_crop = soc_camera_s_crop,
891 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
892#ifdef CONFIG_VIDEO_ADV_DEBUG
893 .vidioc_g_register = soc_camera_g_register,
894 .vidioc_s_register = soc_camera_s_register,
895#endif
896};
897
e55222ef
GL
898int soc_camera_video_start(struct soc_camera_device *icd)
899{
900 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
901 int err = -ENOMEM;
902 struct video_device *vdev;
903
904 if (!icd->dev.parent)
905 return -ENODEV;
906
907 vdev = video_device_alloc();
908 if (!vdev)
909 goto evidallocd;
910 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
911
912 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
913 /* Maybe better &ici->dev */
5e85e732 914 vdev->parent = &icd->dev;
e55222ef
GL
915 vdev->current_norm = V4L2_STD_UNKNOWN;
916 vdev->fops = &soc_camera_fops;
a399810c 917 vdev->ioctl_ops = &soc_camera_ioctl_ops;
e55222ef
GL
918 vdev->release = video_device_release;
919 vdev->minor = -1;
920 vdev->tvnorms = V4L2_STD_UNKNOWN,
e55222ef 921
26f1b942 922 icd->current_fmt = &icd->formats[0];
e55222ef
GL
923
924 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
925 if (err < 0) {
5e85e732 926 dev_err(vdev->parent, "video_register_device failed\n");
e55222ef
GL
927 goto evidregd;
928 }
929 icd->vdev = vdev;
930
931 return 0;
932
933evidregd:
934 video_device_release(vdev);
935evidallocd:
936 return err;
937}
938EXPORT_SYMBOL(soc_camera_video_start);
939
940void soc_camera_video_stop(struct soc_camera_device *icd)
941{
942 struct video_device *vdev = icd->vdev;
943
7e28adb2 944 dev_dbg(&icd->dev, "%s\n", __func__);
e55222ef
GL
945
946 if (!icd->dev.parent || !vdev)
947 return;
948
949 mutex_lock(&video_lock);
950 video_unregister_device(vdev);
951 icd->vdev = NULL;
952 mutex_unlock(&video_lock);
953}
954EXPORT_SYMBOL(soc_camera_video_stop);
955
956static int __init soc_camera_init(void)
957{
958 int ret = bus_register(&soc_camera_bus_type);
959 if (ret)
960 return ret;
961 ret = driver_register(&ic_drv);
962 if (ret)
963 goto edrvr;
e55222ef
GL
964
965 return 0;
966
e55222ef
GL
967edrvr:
968 bus_unregister(&soc_camera_bus_type);
969 return ret;
970}
971
972static void __exit soc_camera_exit(void)
973{
e55222ef
GL
974 driver_unregister(&ic_drv);
975 bus_unregister(&soc_camera_bus_type);
976}
977
978module_init(soc_camera_init);
979module_exit(soc_camera_exit);
980
981MODULE_DESCRIPTION("Image capture bus driver");
982MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
983MODULE_LICENSE("GPL");
This page took 0.222384 seconds and 5 git commands to generate.