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