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