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