blob: 91e1ab36fe5d110c44a18080bbdc31437e889e1e [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
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
41 for (i = 0; i < icd->ops->num_formats; i++)
42 if (icd->ops->formats[i].fourcc == fourcc)
43 return icd->ops->formats + i;
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
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030078 /* test physical bus parameters */
79 ret = ici->try_bus_param(icd, f->fmt.pix.pixelformat);
80 if (ret)
81 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030082
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -030083 /* limit format to hardware capabilities */
84 ret = ici->try_fmt_cap(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030085
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
146 return ici->reqbufs(icf, p);
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{
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300183 struct video_device *vdev;
184 struct soc_camera_device *icd;
185 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300186 struct soc_camera_file *icf;
187 int ret;
188
189 icf = vmalloc(sizeof(*icf));
190 if (!icf)
191 return -ENOMEM;
192
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300193 /* 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);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300199
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->owner)) {
207 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
208 ret = -EINVAL;
209 goto emgi;
210 }
211
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300212 icd->use_count++;
213
214 icf->icd = icd;
215
216 /* Now we really have to activate the camera */
217 if (icd->use_count == 1) {
218 ret = ici->add(icd);
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
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300228 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? */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300233 videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, NULL,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300234 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
235 ici->msize, icd);
236
237 return 0;
238
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300239 /* All errors are entered with the video_lock held */
240eiciadd:
241 module_put(ici->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300242emgi:
243 module_put(icd->ops->owner);
244emgd:
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300245 mutex_unlock(&video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300246 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
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300257 mutex_lock(&video_lock);
258 icd->use_count--;
259 if (!icd->use_count)
260 ici->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300261 module_put(icd->ops->owner);
262 module_put(ici->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300263 mutex_unlock(&video_lock);
264
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300265 vfree(file->private_data);
266
267 dev_dbg(vdev->dev, "camera device close\n");
268
269 return 0;
270}
271
Andrew Mortonaba360d2008-04-22 14:45:59 -0300272static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300273 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
315 return ici->poll(file, pt);
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
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300348 /* buswidth may be further adjusted by the ici */
349 icd->buswidth = data_fmt->depth;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300350
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;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300359 ret = ici->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
360 if (ret < 0)
361 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300362
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300363 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);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300370
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300371 dev_dbg(&icd->dev, "set width: %d height: %d\n",
372 icd->width, icd->height);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300373
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300374 /* set physical bus parameters */
375 return ici->set_bus_param(icd, f->fmt.pix.pixelformat);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300376}
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
387 if (f->index >= icd->ops->num_formats)
388 return -EINVAL;
389
390 format = &icd->ops->formats[f->index];
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));
429 return ici->querycap(ici, cap);
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
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300582 ret = ici->set_fmt_cap(icd, 0, &a->c);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300583 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
704 if (!icd->probe)
705 return -ENODEV;
706
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300707 /* We only call ->add() here to activate and probe the camera.
708 * We shall ->remove() and deactivate it immediately afterwards. */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300709 ret = ici->add(icd);
710 if (ret < 0)
711 return ret;
712
713 ret = icd->probe(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300714 if (ret >= 0) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300715 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 }
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300723 ici->remove(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300724
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);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300733
734 if (icd->remove)
735 icd->remove(icd);
736
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300737 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
765int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner)
766{
767 int ret;
768 struct soc_camera_host *ix;
769
770 if (!ici->vbq_ops || !ici->add || !ici->remove || !owner)
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
788 ici->owner = owner;
789 ici->dev.release = dummy_release;
790
791 ret = device_register(&ici->dev);
792
793 if (ret)
794 goto edevr;
795
796 scan_add_host(ici);
797
798 return 0;
799
800edevr:
801 mutex_lock(&list_lock);
802 list_del(&ici->list);
803 mutex_unlock(&list_lock);
804
805 return ret;
806}
807EXPORT_SYMBOL(soc_camera_host_register);
808
809/* Unregister all clients! */
810void soc_camera_host_unregister(struct soc_camera_host *ici)
811{
812 struct soc_camera_device *icd;
813
814 mutex_lock(&list_lock);
815
816 list_del(&ici->list);
817
818 list_for_each_entry(icd, &devices, list) {
819 if (icd->dev.parent == &ici->dev) {
820 device_unregister(&icd->dev);
821 /* Not before device_unregister(), .remove
822 * needs parent to call ici->remove() */
823 icd->dev.parent = NULL;
824 memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
825 }
826 }
827
828 mutex_unlock(&list_lock);
829
830 device_unregister(&ici->dev);
831}
832EXPORT_SYMBOL(soc_camera_host_unregister);
833
834/* Image capture device */
835int soc_camera_device_register(struct soc_camera_device *icd)
836{
837 struct soc_camera_device *ix;
838 int num = -1, i;
839
840 if (!icd)
841 return -EINVAL;
842
843 for (i = 0; i < 256 && num < 0; i++) {
844 num = i;
845 list_for_each_entry(ix, &devices, list) {
846 if (ix->iface == icd->iface && ix->devnum == i) {
847 num = -1;
848 break;
849 }
850 }
851 }
852
853 if (num < 0)
854 /* ok, we have 256 cameras on this host...
855 * man, stay reasonable... */
856 return -ENOMEM;
857
858 icd->devnum = num;
859 icd->dev.bus = &soc_camera_bus_type;
860 snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
861 "%u-%u", icd->iface, icd->devnum);
862
863 icd->dev.release = dummy_release;
864
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300865 return scan_add_device(icd);
866}
867EXPORT_SYMBOL(soc_camera_device_register);
868
869void soc_camera_device_unregister(struct soc_camera_device *icd)
870{
871 mutex_lock(&list_lock);
872 list_del(&icd->list);
873
874 /* The bus->remove will be eventually called */
875 if (icd->dev.parent)
876 device_unregister(&icd->dev);
877 mutex_unlock(&list_lock);
878}
879EXPORT_SYMBOL(soc_camera_device_unregister);
880
881int soc_camera_video_start(struct soc_camera_device *icd)
882{
883 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
884 int err = -ENOMEM;
885 struct video_device *vdev;
886
887 if (!icd->dev.parent)
888 return -ENODEV;
889
890 vdev = video_device_alloc();
891 if (!vdev)
892 goto evidallocd;
893 dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
894
895 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
896 /* Maybe better &ici->dev */
897 vdev->dev = &icd->dev;
898 vdev->type = VID_TYPE_CAPTURE;
899 vdev->current_norm = V4L2_STD_UNKNOWN;
900 vdev->fops = &soc_camera_fops;
901 vdev->release = video_device_release;
902 vdev->minor = -1;
903 vdev->tvnorms = V4L2_STD_UNKNOWN,
904 vdev->vidioc_querycap = soc_camera_querycap;
905 vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap;
906 vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap;
907 vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap;
908 vdev->vidioc_enum_input = soc_camera_enum_input;
909 vdev->vidioc_g_input = soc_camera_g_input;
910 vdev->vidioc_s_input = soc_camera_s_input;
911 vdev->vidioc_s_std = soc_camera_s_std;
912 vdev->vidioc_reqbufs = soc_camera_reqbufs;
913 vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap;
914 vdev->vidioc_querybuf = soc_camera_querybuf;
915 vdev->vidioc_qbuf = soc_camera_qbuf;
916 vdev->vidioc_dqbuf = soc_camera_dqbuf;
917 vdev->vidioc_streamon = soc_camera_streamon;
918 vdev->vidioc_streamoff = soc_camera_streamoff;
919 vdev->vidioc_queryctrl = soc_camera_queryctrl;
920 vdev->vidioc_g_ctrl = soc_camera_g_ctrl;
921 vdev->vidioc_s_ctrl = soc_camera_s_ctrl;
922 vdev->vidioc_cropcap = soc_camera_cropcap;
923 vdev->vidioc_g_crop = soc_camera_g_crop;
924 vdev->vidioc_s_crop = soc_camera_s_crop;
925 vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident;
926#ifdef CONFIG_VIDEO_ADV_DEBUG
927 vdev->vidioc_g_register = soc_camera_g_register;
928 vdev->vidioc_s_register = soc_camera_s_register;
929#endif
930
931 icd->current_fmt = &icd->ops->formats[0];
932
933 err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
934 if (err < 0) {
935 dev_err(vdev->dev, "video_register_device failed\n");
936 goto evidregd;
937 }
938 icd->vdev = vdev;
939
940 return 0;
941
942evidregd:
943 video_device_release(vdev);
944evidallocd:
945 return err;
946}
947EXPORT_SYMBOL(soc_camera_video_start);
948
949void soc_camera_video_stop(struct soc_camera_device *icd)
950{
951 struct video_device *vdev = icd->vdev;
952
953 dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
954
955 if (!icd->dev.parent || !vdev)
956 return;
957
958 mutex_lock(&video_lock);
959 video_unregister_device(vdev);
960 icd->vdev = NULL;
961 mutex_unlock(&video_lock);
962}
963EXPORT_SYMBOL(soc_camera_video_stop);
964
965static int __init soc_camera_init(void)
966{
967 int ret = bus_register(&soc_camera_bus_type);
968 if (ret)
969 return ret;
970 ret = driver_register(&ic_drv);
971 if (ret)
972 goto edrvr;
973 ret = class_register(&soc_camera_host_class);
974 if (ret)
975 goto eclr;
976
977 return 0;
978
979eclr:
980 driver_unregister(&ic_drv);
981edrvr:
982 bus_unregister(&soc_camera_bus_type);
983 return ret;
984}
985
986static void __exit soc_camera_exit(void)
987{
988 class_unregister(&soc_camera_host_class);
989 driver_unregister(&ic_drv);
990 bus_unregister(&soc_camera_bus_type);
991}
992
993module_init(soc_camera_init);
994module_exit(soc_camera_exit);
995
996MODULE_DESCRIPTION("Image capture bus driver");
997MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
998MODULE_LICENSE("GPL");