Merge branch 'linus' into x86/core
[deliverable/linux.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 static struct soc_camera_data_format*
39 format_by_fourcc(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
49 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
50 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
80 /* test physical bus parameters */
81 ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
82 if (ret)
83 return ret;
84
85 /* limit format to hardware capabilities */
86 ret = ici->ops->try_fmt_cap(icd, f);
87
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
98 static 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
111 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
112 {
113 *i = 0;
114
115 return 0;
116 }
117
118 static 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
126 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
127 {
128 return 0;
129 }
130
131 static 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
142 dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
143
144 ret = videobuf_reqbufs(&icf->vb_vidq, p);
145 if (ret < 0)
146 return ret;
147
148 return ici->ops->reqbufs(icf, p);
149 }
150
151 static 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
161 static 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
171 static 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
181 static int soc_camera_open(struct inode *inode, struct file *file)
182 {
183 struct video_device *vdev;
184 struct soc_camera_device *icd;
185 struct soc_camera_host *ici;
186 struct soc_camera_file *icf;
187 int ret;
188
189 icf = vmalloc(sizeof(*icf));
190 if (!icf)
191 return -ENOMEM;
192
193 /* Protect against icd->remove() until we module_get() both drivers. */
194 mutex_lock(&video_lock);
195
196 vdev = video_devdata(file);
197 icd = container_of(vdev->parent, struct soc_camera_device, dev);
198 ici = to_soc_camera_host(icd->dev.parent);
199
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
206 if (!try_module_get(ici->ops->owner)) {
207 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
208 ret = -EINVAL;
209 goto emgi;
210 }
211
212 icf->icd = icd;
213 icd->use_count++;
214
215 /* Now we really have to activate the camera */
216 if (icd->use_count == 1) {
217 ret = ici->ops->add(icd);
218 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
227 file->private_data = icf;
228 dev_dbg(&icd->dev, "camera device open\n");
229
230 ici->ops->init_videobuf(&icf->vb_vidq, icd);
231
232 return 0;
233
234 /* All errors are entered with the video_lock held */
235 eiciadd:
236 module_put(ici->ops->owner);
237 emgi:
238 module_put(icd->ops->owner);
239 emgd:
240 mutex_unlock(&video_lock);
241 vfree(icf);
242 return ret;
243 }
244
245 static 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
252 mutex_lock(&video_lock);
253 icd->use_count--;
254 if (!icd->use_count)
255 ici->ops->remove(icd);
256 module_put(icd->ops->owner);
257 module_put(ici->ops->owner);
258 mutex_unlock(&video_lock);
259
260 vfree(icf);
261
262 dev_dbg(vdev->parent, "camera device close\n");
263
264 return 0;
265 }
266
267 static ssize_t soc_camera_read(struct file *file, char __user *buf,
268 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
275 dev_err(vdev->parent, "camera device read not implemented\n");
276
277 return err;
278 }
279
280 static 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
298 static 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
310 return ici->ops->poll(file, pt);
311 }
312
313
314 static 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
326 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
327 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
343 /* buswidth may be further adjusted by the ici */
344 icd->buswidth = data_fmt->depth;
345
346 ret = soc_camera_try_fmt_vid_cap(file, icf, f);
347 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;
354 ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
355 if (ret < 0)
356 return ret;
357
358 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);
365
366 dev_dbg(&icd->dev, "set width: %d height: %d\n",
367 icd->width, icd->height);
368
369 /* set physical bus parameters */
370 return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
371 }
372
373 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
374 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
382 if (f->index >= icd->num_formats)
383 return -EINVAL;
384
385 format = &icd->formats[f->index];
386
387 strlcpy(f->description, format->name, sizeof(f->description));
388 f->pixelformat = format->fourcc;
389 return 0;
390 }
391
392 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
393 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
413 static 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));
424 return ici->ops->querycap(ici, cap);
425 }
426
427 static 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
435 dev_dbg(&icd->dev, "%s\n", __func__);
436
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
446 static 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
454 dev_dbg(&icd->dev, "%s\n", __func__);
455
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
468 static 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
490 static 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
516 static 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
529 static 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
550 static 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
565 static 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
577 ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
578 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
588 static 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
601 static 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
613 static 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
626 static 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 */
647 static 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 */
665 static 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
692 static 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
699 if (!icd->ops->probe)
700 return -ENODEV;
701
702 /* We only call ->add() here to activate and probe the camera.
703 * We shall ->remove() and deactivate it immediately afterwards. */
704 ret = ici->ops->add(icd);
705 if (ret < 0)
706 return ret;
707
708 ret = icd->ops->probe(icd);
709 if (ret >= 0) {
710 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 }
718 ici->ops->remove(icd);
719
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 */
725 static int soc_camera_remove(struct device *dev)
726 {
727 struct soc_camera_device *icd = to_soc_camera_dev(dev);
728
729 if (icd->ops->remove)
730 icd->ops->remove(icd);
731
732 return 0;
733 }
734
735 static 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
747 static 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
759 static struct bus_type soc_camera_bus_type = {
760 .name = "soc-camera",
761 .probe = soc_camera_probe,
762 .remove = soc_camera_remove,
763 .suspend = soc_camera_suspend,
764 .resume = soc_camera_resume,
765 };
766
767 static struct device_driver ic_drv = {
768 .name = "camera",
769 .bus = &soc_camera_bus_type,
770 .owner = THIS_MODULE,
771 };
772
773 static void dummy_release(struct device *dev)
774 {
775 }
776
777 int soc_camera_host_register(struct soc_camera_host *ici)
778 {
779 int ret;
780 struct soc_camera_host *ix;
781
782 if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
783 return -EINVAL;
784
785 /* Number might be equal to the platform device ID */
786 sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
787
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
799 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
810 edevr:
811 mutex_lock(&list_lock);
812 list_del(&ici->list);
813 mutex_unlock(&list_lock);
814
815 return ret;
816 }
817 EXPORT_SYMBOL(soc_camera_host_register);
818
819 /* Unregister all clients! */
820 void 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
832 * needs parent to call ici->ops->remove() */
833 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 }
842 EXPORT_SYMBOL(soc_camera_host_unregister);
843
844 /* Image capture device */
845 int 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
875 return scan_add_device(icd);
876 }
877 EXPORT_SYMBOL(soc_camera_device_register);
878
879 void 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 }
889 EXPORT_SYMBOL(soc_camera_device_unregister);
890
891 static 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
920 int 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 */
936 vdev->parent = &icd->dev;
937 vdev->current_norm = V4L2_STD_UNKNOWN;
938 vdev->fops = &soc_camera_fops;
939 vdev->ioctl_ops = &soc_camera_ioctl_ops;
940 vdev->release = video_device_release;
941 vdev->minor = -1;
942 vdev->tvnorms = V4L2_STD_UNKNOWN,
943
944 icd->current_fmt = &icd->formats[0];
945
946 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
947 if (err < 0) {
948 dev_err(vdev->parent, "video_register_device failed\n");
949 goto evidregd;
950 }
951 icd->vdev = vdev;
952
953 return 0;
954
955 evidregd:
956 video_device_release(vdev);
957 evidallocd:
958 return err;
959 }
960 EXPORT_SYMBOL(soc_camera_video_start);
961
962 void soc_camera_video_stop(struct soc_camera_device *icd)
963 {
964 struct video_device *vdev = icd->vdev;
965
966 dev_dbg(&icd->dev, "%s\n", __func__);
967
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 }
976 EXPORT_SYMBOL(soc_camera_video_stop);
977
978 static 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;
986
987 return 0;
988
989 edrvr:
990 bus_unregister(&soc_camera_bus_type);
991 return ret;
992 }
993
994 static void __exit soc_camera_exit(void)
995 {
996 driver_unregister(&ic_drv);
997 bus_unregister(&soc_camera_bus_type);
998 }
999
1000 module_init(soc_camera_init);
1001 module_exit(soc_camera_exit);
1002
1003 MODULE_DESCRIPTION("Image capture bus driver");
1004 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1005 MODULE_LICENSE("GPL");
This page took 0.051451 seconds and 6 git commands to generate.