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