[media] V4L: soc-camera: add 2 new ioctl() handlers
[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/device.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vmalloc.h>
31
32 #include <media/soc_camera.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-dev.h>
36 #include <media/videobuf-core.h>
37 #include <media/videobuf2-core.h>
38 #include <media/soc_mediabus.h>
39
40 /* Default to VGA resolution */
41 #define DEFAULT_WIDTH 640
42 #define DEFAULT_HEIGHT 480
43
44 #define is_streaming(ici, icd) \
45 (((ici)->ops->init_videobuf) ? \
46 (icd)->vb_vidq.streaming : \
47 vb2_is_streaming(&(icd)->vb2_vidq))
48
49 static LIST_HEAD(hosts);
50 static LIST_HEAD(devices);
51 static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
52
53 static int soc_camera_power_set(struct soc_camera_device *icd,
54 struct soc_camera_link *icl,
55 int power_on)
56 {
57 int ret;
58
59 if (power_on) {
60 ret = regulator_bulk_enable(icl->num_regulators,
61 icl->regulators);
62 if (ret < 0) {
63 dev_err(icd->pdev, "Cannot enable regulators\n");
64 return ret;
65 }
66
67 if (icl->power)
68 ret = icl->power(icd->pdev, power_on);
69 if (ret < 0) {
70 dev_err(icd->pdev,
71 "Platform failed to power-on the camera.\n");
72
73 regulator_bulk_disable(icl->num_regulators,
74 icl->regulators);
75 return ret;
76 }
77 } else {
78 ret = 0;
79 if (icl->power)
80 ret = icl->power(icd->pdev, 0);
81 if (ret < 0) {
82 dev_err(icd->pdev,
83 "Platform failed to power-off the camera.\n");
84 return ret;
85 }
86
87 ret = regulator_bulk_disable(icl->num_regulators,
88 icl->regulators);
89 if (ret < 0) {
90 dev_err(icd->pdev, "Cannot disable regulators\n");
91 return ret;
92 }
93 }
94
95 return 0;
96 }
97
98 const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
99 struct soc_camera_device *icd, unsigned int fourcc)
100 {
101 unsigned int i;
102
103 for (i = 0; i < icd->num_user_formats; i++)
104 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
105 return icd->user_formats + i;
106 return NULL;
107 }
108 EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
109
110 /**
111 * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
112 * @icl: camera platform parameters
113 * @cfg: media bus configuration
114 * @return: resulting flags
115 */
116 unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl,
117 const struct v4l2_mbus_config *cfg)
118 {
119 unsigned long f, flags = cfg->flags;
120
121 /* If only one of the two polarities is supported, switch to the opposite */
122 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
123 f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
124 if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
125 flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
126 }
127
128 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
129 f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
130 if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
131 flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
132 }
133
134 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
135 f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
136 if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
137 flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
138 }
139
140 return flags;
141 }
142 EXPORT_SYMBOL(soc_camera_apply_board_flags);
143
144 #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
145 ((x) >> 24) & 0xff
146
147 static int soc_camera_try_fmt(struct soc_camera_device *icd,
148 struct v4l2_format *f)
149 {
150 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
151 struct v4l2_pix_format *pix = &f->fmt.pix;
152 int ret;
153
154 dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
155 pixfmtstr(pix->pixelformat), pix->width, pix->height);
156
157 pix->bytesperline = 0;
158 pix->sizeimage = 0;
159
160 ret = ici->ops->try_fmt(icd, f);
161 if (ret < 0)
162 return ret;
163
164 if (!pix->sizeimage) {
165 if (!pix->bytesperline) {
166 const struct soc_camera_format_xlate *xlate;
167
168 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
169 if (!xlate)
170 return -EINVAL;
171
172 ret = soc_mbus_bytes_per_line(pix->width,
173 xlate->host_fmt);
174 if (ret > 0)
175 pix->bytesperline = ret;
176 }
177 if (pix->bytesperline)
178 pix->sizeimage = pix->bytesperline * pix->height;
179 }
180
181 return 0;
182 }
183
184 static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
185 struct v4l2_format *f)
186 {
187 struct soc_camera_device *icd = file->private_data;
188
189 WARN_ON(priv != file->private_data);
190
191 /* Only single-plane capture is supported so far */
192 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
193 return -EINVAL;
194
195 /* limit format to hardware capabilities */
196 return soc_camera_try_fmt(icd, f);
197 }
198
199 static int soc_camera_enum_input(struct file *file, void *priv,
200 struct v4l2_input *inp)
201 {
202 if (inp->index != 0)
203 return -EINVAL;
204
205 /* default is camera */
206 inp->type = V4L2_INPUT_TYPE_CAMERA;
207 inp->std = V4L2_STD_UNKNOWN;
208 strcpy(inp->name, "Camera");
209
210 return 0;
211 }
212
213 static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
214 {
215 *i = 0;
216
217 return 0;
218 }
219
220 static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
221 {
222 if (i > 0)
223 return -EINVAL;
224
225 return 0;
226 }
227
228 static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
229 {
230 struct soc_camera_device *icd = file->private_data;
231 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
232
233 return v4l2_subdev_call(sd, core, s_std, *a);
234 }
235
236 static int soc_camera_enum_fsizes(struct file *file, void *fh,
237 struct v4l2_frmsizeenum *fsize)
238 {
239 struct soc_camera_device *icd = file->private_data;
240 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
241
242 return ici->ops->enum_fsizes(icd, fsize);
243 }
244
245 static int soc_camera_reqbufs(struct file *file, void *priv,
246 struct v4l2_requestbuffers *p)
247 {
248 int ret;
249 struct soc_camera_device *icd = file->private_data;
250 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
251
252 WARN_ON(priv != file->private_data);
253
254 if (icd->streamer && icd->streamer != file)
255 return -EBUSY;
256
257 if (ici->ops->init_videobuf) {
258 ret = videobuf_reqbufs(&icd->vb_vidq, p);
259 if (ret < 0)
260 return ret;
261
262 ret = ici->ops->reqbufs(icd, p);
263 } else {
264 ret = vb2_reqbufs(&icd->vb2_vidq, p);
265 }
266
267 if (!ret && !icd->streamer)
268 icd->streamer = file;
269
270 return ret;
271 }
272
273 static int soc_camera_querybuf(struct file *file, void *priv,
274 struct v4l2_buffer *p)
275 {
276 struct soc_camera_device *icd = file->private_data;
277 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
278
279 WARN_ON(priv != file->private_data);
280
281 if (ici->ops->init_videobuf)
282 return videobuf_querybuf(&icd->vb_vidq, p);
283 else
284 return vb2_querybuf(&icd->vb2_vidq, p);
285 }
286
287 static int soc_camera_qbuf(struct file *file, void *priv,
288 struct v4l2_buffer *p)
289 {
290 struct soc_camera_device *icd = file->private_data;
291 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
292
293 WARN_ON(priv != file->private_data);
294
295 if (icd->streamer != file)
296 return -EBUSY;
297
298 if (ici->ops->init_videobuf)
299 return videobuf_qbuf(&icd->vb_vidq, p);
300 else
301 return vb2_qbuf(&icd->vb2_vidq, p);
302 }
303
304 static int soc_camera_dqbuf(struct file *file, void *priv,
305 struct v4l2_buffer *p)
306 {
307 struct soc_camera_device *icd = file->private_data;
308 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
309
310 WARN_ON(priv != file->private_data);
311
312 if (icd->streamer != file)
313 return -EBUSY;
314
315 if (ici->ops->init_videobuf)
316 return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
317 else
318 return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
319 }
320
321 static int soc_camera_create_bufs(struct file *file, void *priv,
322 struct v4l2_create_buffers *create)
323 {
324 struct soc_camera_device *icd = file->private_data;
325 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
326
327 /* videobuf2 only */
328 if (ici->ops->init_videobuf)
329 return -EINVAL;
330 else
331 return vb2_create_bufs(&icd->vb2_vidq, create);
332 }
333
334 static int soc_camera_prepare_buf(struct file *file, void *priv,
335 struct v4l2_buffer *b)
336 {
337 struct soc_camera_device *icd = file->private_data;
338 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
339
340 /* videobuf2 only */
341 if (ici->ops->init_videobuf)
342 return -EINVAL;
343 else
344 return vb2_prepare_buf(&icd->vb2_vidq, b);
345 }
346
347 /* Always entered with .video_lock held */
348 static int soc_camera_init_user_formats(struct soc_camera_device *icd)
349 {
350 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
351 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
352 unsigned int i, fmts = 0, raw_fmts = 0;
353 int ret;
354 enum v4l2_mbus_pixelcode code;
355
356 while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
357 raw_fmts++;
358
359 if (!ici->ops->get_formats)
360 /*
361 * Fallback mode - the host will have to serve all
362 * sensor-provided formats one-to-one to the user
363 */
364 fmts = raw_fmts;
365 else
366 /*
367 * First pass - only count formats this host-sensor
368 * configuration can provide
369 */
370 for (i = 0; i < raw_fmts; i++) {
371 ret = ici->ops->get_formats(icd, i, NULL);
372 if (ret < 0)
373 return ret;
374 fmts += ret;
375 }
376
377 if (!fmts)
378 return -ENXIO;
379
380 icd->user_formats =
381 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
382 if (!icd->user_formats)
383 return -ENOMEM;
384
385 dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
386
387 /* Second pass - actually fill data formats */
388 fmts = 0;
389 for (i = 0; i < raw_fmts; i++)
390 if (!ici->ops->get_formats) {
391 v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
392 icd->user_formats[fmts].host_fmt =
393 soc_mbus_get_fmtdesc(code);
394 if (icd->user_formats[fmts].host_fmt)
395 icd->user_formats[fmts++].code = code;
396 } else {
397 ret = ici->ops->get_formats(icd, i,
398 &icd->user_formats[fmts]);
399 if (ret < 0)
400 goto egfmt;
401 fmts += ret;
402 }
403
404 icd->num_user_formats = fmts;
405 icd->current_fmt = &icd->user_formats[0];
406
407 return 0;
408
409 egfmt:
410 vfree(icd->user_formats);
411 return ret;
412 }
413
414 /* Always entered with .video_lock held */
415 static void soc_camera_free_user_formats(struct soc_camera_device *icd)
416 {
417 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
418
419 if (ici->ops->put_formats)
420 ici->ops->put_formats(icd);
421 icd->current_fmt = NULL;
422 icd->num_user_formats = 0;
423 vfree(icd->user_formats);
424 icd->user_formats = NULL;
425 }
426
427 /* Called with .vb_lock held, or from the first open(2), see comment there */
428 static int soc_camera_set_fmt(struct soc_camera_device *icd,
429 struct v4l2_format *f)
430 {
431 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
432 struct v4l2_pix_format *pix = &f->fmt.pix;
433 int ret;
434
435 dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
436 pixfmtstr(pix->pixelformat), pix->width, pix->height);
437
438 /* We always call try_fmt() before set_fmt() or set_crop() */
439 ret = soc_camera_try_fmt(icd, f);
440 if (ret < 0)
441 return ret;
442
443 ret = ici->ops->set_fmt(icd, f);
444 if (ret < 0) {
445 return ret;
446 } else if (!icd->current_fmt ||
447 icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
448 dev_err(icd->pdev,
449 "Host driver hasn't set up current format correctly!\n");
450 return -EINVAL;
451 }
452
453 icd->user_width = pix->width;
454 icd->user_height = pix->height;
455 icd->bytesperline = pix->bytesperline;
456 icd->sizeimage = pix->sizeimage;
457 icd->colorspace = pix->colorspace;
458 icd->field = pix->field;
459 if (ici->ops->init_videobuf)
460 icd->vb_vidq.field = pix->field;
461
462 dev_dbg(icd->pdev, "set width: %d height: %d\n",
463 icd->user_width, icd->user_height);
464
465 /* set physical bus parameters */
466 return ici->ops->set_bus_param(icd, pix->pixelformat);
467 }
468
469 static int soc_camera_open(struct file *file)
470 {
471 struct video_device *vdev = video_devdata(file);
472 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
473 struct soc_camera_link *icl = to_soc_camera_link(icd);
474 struct soc_camera_host *ici;
475 int ret;
476
477 if (!to_soc_camera_control(icd))
478 /* No device driver attached */
479 return -ENODEV;
480
481 ici = to_soc_camera_host(icd->parent);
482
483 if (!try_module_get(ici->ops->owner)) {
484 dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
485 return -EINVAL;
486 }
487
488 icd->use_count++;
489
490 /* Now we really have to activate the camera */
491 if (icd->use_count == 1) {
492 /* Restore parameters before the last close() per V4L2 API */
493 struct v4l2_format f = {
494 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
495 .fmt.pix = {
496 .width = icd->user_width,
497 .height = icd->user_height,
498 .field = icd->field,
499 .colorspace = icd->colorspace,
500 .pixelformat =
501 icd->current_fmt->host_fmt->fourcc,
502 },
503 };
504
505 ret = soc_camera_power_set(icd, icl, 1);
506 if (ret < 0)
507 goto epower;
508
509 /* The camera could have been already on, try to reset */
510 if (icl->reset)
511 icl->reset(icd->pdev);
512
513 ret = ici->ops->add(icd);
514 if (ret < 0) {
515 dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
516 goto eiciadd;
517 }
518
519 pm_runtime_enable(&icd->vdev->dev);
520 ret = pm_runtime_resume(&icd->vdev->dev);
521 if (ret < 0 && ret != -ENOSYS)
522 goto eresume;
523
524 /*
525 * Try to configure with default parameters. Notice: this is the
526 * very first open, so, we cannot race against other calls,
527 * apart from someone else calling open() simultaneously, but
528 * .video_lock is protecting us against it.
529 */
530 ret = soc_camera_set_fmt(icd, &f);
531 if (ret < 0)
532 goto esfmt;
533
534 if (ici->ops->init_videobuf) {
535 ici->ops->init_videobuf(&icd->vb_vidq, icd);
536 } else {
537 ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
538 if (ret < 0)
539 goto einitvb;
540 }
541 v4l2_ctrl_handler_setup(&icd->ctrl_handler);
542 }
543
544 file->private_data = icd;
545 dev_dbg(icd->pdev, "camera device open\n");
546
547 return 0;
548
549 /*
550 * First four errors are entered with the .video_lock held
551 * and use_count == 1
552 */
553 einitvb:
554 esfmt:
555 pm_runtime_disable(&icd->vdev->dev);
556 eresume:
557 ici->ops->remove(icd);
558 eiciadd:
559 soc_camera_power_set(icd, icl, 0);
560 epower:
561 icd->use_count--;
562 module_put(ici->ops->owner);
563
564 return ret;
565 }
566
567 static int soc_camera_close(struct file *file)
568 {
569 struct soc_camera_device *icd = file->private_data;
570 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
571
572 icd->use_count--;
573 if (!icd->use_count) {
574 struct soc_camera_link *icl = to_soc_camera_link(icd);
575
576 pm_runtime_suspend(&icd->vdev->dev);
577 pm_runtime_disable(&icd->vdev->dev);
578
579 ici->ops->remove(icd);
580 if (ici->ops->init_videobuf2)
581 vb2_queue_release(&icd->vb2_vidq);
582
583 soc_camera_power_set(icd, icl, 0);
584 }
585
586 if (icd->streamer == file)
587 icd->streamer = NULL;
588
589 module_put(ici->ops->owner);
590
591 dev_dbg(icd->pdev, "camera device close\n");
592
593 return 0;
594 }
595
596 static ssize_t soc_camera_read(struct file *file, char __user *buf,
597 size_t count, loff_t *ppos)
598 {
599 struct soc_camera_device *icd = file->private_data;
600 int err = -EINVAL;
601
602 dev_err(icd->pdev, "camera device read not implemented\n");
603
604 return err;
605 }
606
607 static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
608 {
609 struct soc_camera_device *icd = file->private_data;
610 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
611 int err;
612
613 dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
614
615 if (icd->streamer != file)
616 return -EBUSY;
617
618 if (ici->ops->init_videobuf)
619 err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
620 else
621 err = vb2_mmap(&icd->vb2_vidq, vma);
622
623 dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
624 (unsigned long)vma->vm_start,
625 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
626 err);
627
628 return err;
629 }
630
631 static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
632 {
633 struct soc_camera_device *icd = file->private_data;
634 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
635
636 if (icd->streamer != file)
637 return -EBUSY;
638
639 if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
640 dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
641 return POLLERR;
642 }
643
644 return ici->ops->poll(file, pt);
645 }
646
647 void soc_camera_lock(struct vb2_queue *vq)
648 {
649 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
650 mutex_lock(&icd->video_lock);
651 }
652 EXPORT_SYMBOL(soc_camera_lock);
653
654 void soc_camera_unlock(struct vb2_queue *vq)
655 {
656 struct soc_camera_device *icd = vb2_get_drv_priv(vq);
657 mutex_unlock(&icd->video_lock);
658 }
659 EXPORT_SYMBOL(soc_camera_unlock);
660
661 static struct v4l2_file_operations soc_camera_fops = {
662 .owner = THIS_MODULE,
663 .open = soc_camera_open,
664 .release = soc_camera_close,
665 .unlocked_ioctl = video_ioctl2,
666 .read = soc_camera_read,
667 .mmap = soc_camera_mmap,
668 .poll = soc_camera_poll,
669 };
670
671 static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
672 struct v4l2_format *f)
673 {
674 struct soc_camera_device *icd = file->private_data;
675 int ret;
676
677 WARN_ON(priv != file->private_data);
678
679 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
680 dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
681 return -EINVAL;
682 }
683
684 if (icd->streamer && icd->streamer != file)
685 return -EBUSY;
686
687 if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
688 dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
689 return -EBUSY;
690 }
691
692 ret = soc_camera_set_fmt(icd, f);
693
694 if (!ret && !icd->streamer)
695 icd->streamer = file;
696
697 return ret;
698 }
699
700 static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
701 struct v4l2_fmtdesc *f)
702 {
703 struct soc_camera_device *icd = file->private_data;
704 const struct soc_mbus_pixelfmt *format;
705
706 WARN_ON(priv != file->private_data);
707
708 if (f->index >= icd->num_user_formats)
709 return -EINVAL;
710
711 format = icd->user_formats[f->index].host_fmt;
712
713 if (format->name)
714 strlcpy(f->description, format->name, sizeof(f->description));
715 f->pixelformat = format->fourcc;
716 return 0;
717 }
718
719 static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
720 struct v4l2_format *f)
721 {
722 struct soc_camera_device *icd = file->private_data;
723 struct v4l2_pix_format *pix = &f->fmt.pix;
724
725 WARN_ON(priv != file->private_data);
726
727 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
728 return -EINVAL;
729
730 pix->width = icd->user_width;
731 pix->height = icd->user_height;
732 pix->bytesperline = icd->bytesperline;
733 pix->sizeimage = icd->sizeimage;
734 pix->field = icd->field;
735 pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
736 pix->colorspace = icd->colorspace;
737 dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
738 icd->current_fmt->host_fmt->fourcc);
739 return 0;
740 }
741
742 static int soc_camera_querycap(struct file *file, void *priv,
743 struct v4l2_capability *cap)
744 {
745 struct soc_camera_device *icd = file->private_data;
746 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
747
748 WARN_ON(priv != file->private_data);
749
750 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
751 return ici->ops->querycap(ici, cap);
752 }
753
754 static int soc_camera_streamon(struct file *file, void *priv,
755 enum v4l2_buf_type i)
756 {
757 struct soc_camera_device *icd = file->private_data;
758 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
759 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
760 int ret;
761
762 WARN_ON(priv != file->private_data);
763
764 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
765 return -EINVAL;
766
767 if (icd->streamer != file)
768 return -EBUSY;
769
770 /* This calls buf_queue from host driver's videobuf_queue_ops */
771 if (ici->ops->init_videobuf)
772 ret = videobuf_streamon(&icd->vb_vidq);
773 else
774 ret = vb2_streamon(&icd->vb2_vidq, i);
775
776 if (!ret)
777 v4l2_subdev_call(sd, video, s_stream, 1);
778
779 return ret;
780 }
781
782 static int soc_camera_streamoff(struct file *file, void *priv,
783 enum v4l2_buf_type i)
784 {
785 struct soc_camera_device *icd = file->private_data;
786 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
787 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
788
789 WARN_ON(priv != file->private_data);
790
791 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
792 return -EINVAL;
793
794 if (icd->streamer != file)
795 return -EBUSY;
796
797 /*
798 * This calls buf_release from host driver's videobuf_queue_ops for all
799 * remaining buffers. When the last buffer is freed, stop capture
800 */
801 if (ici->ops->init_videobuf)
802 videobuf_streamoff(&icd->vb_vidq);
803 else
804 vb2_streamoff(&icd->vb2_vidq, i);
805
806 v4l2_subdev_call(sd, video, s_stream, 0);
807
808 return 0;
809 }
810
811 static int soc_camera_cropcap(struct file *file, void *fh,
812 struct v4l2_cropcap *a)
813 {
814 struct soc_camera_device *icd = file->private_data;
815 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
816
817 return ici->ops->cropcap(icd, a);
818 }
819
820 static int soc_camera_g_crop(struct file *file, void *fh,
821 struct v4l2_crop *a)
822 {
823 struct soc_camera_device *icd = file->private_data;
824 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
825 int ret;
826
827 ret = ici->ops->get_crop(icd, a);
828
829 return ret;
830 }
831
832 /*
833 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
834 * argument with the actual geometry, instead, the user shall use G_CROP to
835 * retrieve it.
836 */
837 static int soc_camera_s_crop(struct file *file, void *fh,
838 struct v4l2_crop *a)
839 {
840 struct soc_camera_device *icd = file->private_data;
841 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
842 struct v4l2_rect *rect = &a->c;
843 struct v4l2_crop current_crop;
844 int ret;
845
846 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
847 return -EINVAL;
848
849 dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
850 rect->width, rect->height, rect->left, rect->top);
851
852 /* If get_crop fails, we'll let host and / or client drivers decide */
853 ret = ici->ops->get_crop(icd, &current_crop);
854
855 /* Prohibit window size change with initialised buffers */
856 if (ret < 0) {
857 dev_err(icd->pdev,
858 "S_CROP denied: getting current crop failed\n");
859 } else if ((a->c.width == current_crop.c.width &&
860 a->c.height == current_crop.c.height) ||
861 !is_streaming(ici, icd)) {
862 /* same size or not streaming - use .set_crop() */
863 ret = ici->ops->set_crop(icd, a);
864 } else if (ici->ops->set_livecrop) {
865 ret = ici->ops->set_livecrop(icd, a);
866 } else {
867 dev_err(icd->pdev,
868 "S_CROP denied: queue initialised and sizes differ\n");
869 ret = -EBUSY;
870 }
871
872 return ret;
873 }
874
875 static int soc_camera_g_parm(struct file *file, void *fh,
876 struct v4l2_streamparm *a)
877 {
878 struct soc_camera_device *icd = file->private_data;
879 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
880
881 if (ici->ops->get_parm)
882 return ici->ops->get_parm(icd, a);
883
884 return -ENOIOCTLCMD;
885 }
886
887 static int soc_camera_s_parm(struct file *file, void *fh,
888 struct v4l2_streamparm *a)
889 {
890 struct soc_camera_device *icd = file->private_data;
891 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
892
893 if (ici->ops->set_parm)
894 return ici->ops->set_parm(icd, a);
895
896 return -ENOIOCTLCMD;
897 }
898
899 static int soc_camera_g_chip_ident(struct file *file, void *fh,
900 struct v4l2_dbg_chip_ident *id)
901 {
902 struct soc_camera_device *icd = file->private_data;
903 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904
905 return v4l2_subdev_call(sd, core, g_chip_ident, id);
906 }
907
908 #ifdef CONFIG_VIDEO_ADV_DEBUG
909 static int soc_camera_g_register(struct file *file, void *fh,
910 struct v4l2_dbg_register *reg)
911 {
912 struct soc_camera_device *icd = file->private_data;
913 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
914
915 return v4l2_subdev_call(sd, core, g_register, reg);
916 }
917
918 static int soc_camera_s_register(struct file *file, void *fh,
919 struct v4l2_dbg_register *reg)
920 {
921 struct soc_camera_device *icd = file->private_data;
922 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
923
924 return v4l2_subdev_call(sd, core, s_register, reg);
925 }
926 #endif
927
928 static int soc_camera_probe(struct soc_camera_device *icd);
929
930 /* So far this function cannot fail */
931 static void scan_add_host(struct soc_camera_host *ici)
932 {
933 struct soc_camera_device *icd;
934
935 mutex_lock(&list_lock);
936
937 list_for_each_entry(icd, &devices, list) {
938 if (icd->iface == ici->nr) {
939 int ret;
940
941 icd->parent = ici->v4l2_dev.dev;
942 ret = soc_camera_probe(icd);
943 }
944 }
945
946 mutex_unlock(&list_lock);
947 }
948
949 #ifdef CONFIG_I2C_BOARDINFO
950 static int soc_camera_init_i2c(struct soc_camera_device *icd,
951 struct soc_camera_link *icl)
952 {
953 struct i2c_client *client;
954 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
955 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
956 struct v4l2_subdev *subdev;
957
958 if (!adap) {
959 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
960 icl->i2c_adapter_id);
961 goto ei2cga;
962 }
963
964 icl->board_info->platform_data = icd;
965
966 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
967 icl->board_info, NULL);
968 if (!subdev)
969 goto ei2cnd;
970
971 client = v4l2_get_subdevdata(subdev);
972
973 /* Use to_i2c_client(dev) to recover the i2c client */
974 icd->control = &client->dev;
975
976 return 0;
977 ei2cnd:
978 i2c_put_adapter(adap);
979 ei2cga:
980 return -ENODEV;
981 }
982
983 static void soc_camera_free_i2c(struct soc_camera_device *icd)
984 {
985 struct i2c_client *client =
986 to_i2c_client(to_soc_camera_control(icd));
987 struct i2c_adapter *adap = client->adapter;
988
989 icd->control = NULL;
990 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
991 i2c_unregister_device(client);
992 i2c_put_adapter(adap);
993 }
994 #else
995 #define soc_camera_init_i2c(icd, icl) (-ENODEV)
996 #define soc_camera_free_i2c(icd) do {} while (0)
997 #endif
998
999 static int soc_camera_video_start(struct soc_camera_device *icd);
1000 static int video_dev_create(struct soc_camera_device *icd);
1001 /* Called during host-driver probe */
1002 static int soc_camera_probe(struct soc_camera_device *icd)
1003 {
1004 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1005 struct soc_camera_link *icl = to_soc_camera_link(icd);
1006 struct device *control = NULL;
1007 struct v4l2_subdev *sd;
1008 struct v4l2_mbus_framefmt mf;
1009 int ret;
1010
1011 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
1012
1013 /*
1014 * Currently the subdev with the largest number of controls (13) is
1015 * ov6550. So let's pick 16 as a hint for the control handler. Note
1016 * that this is a hint only: too large and you waste some memory, too
1017 * small and there is a (very) small performance hit when looking up
1018 * controls in the internal hash.
1019 */
1020 ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
1021 if (ret < 0)
1022 return ret;
1023
1024 ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
1025 icl->regulators);
1026 if (ret < 0)
1027 goto ereg;
1028
1029 ret = soc_camera_power_set(icd, icl, 1);
1030 if (ret < 0)
1031 goto epower;
1032
1033 /* The camera could have been already on, try to reset */
1034 if (icl->reset)
1035 icl->reset(icd->pdev);
1036
1037 ret = ici->ops->add(icd);
1038 if (ret < 0)
1039 goto eadd;
1040
1041 /* Must have icd->vdev before registering the device */
1042 ret = video_dev_create(icd);
1043 if (ret < 0)
1044 goto evdc;
1045
1046 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
1047 if (icl->board_info) {
1048 ret = soc_camera_init_i2c(icd, icl);
1049 if (ret < 0)
1050 goto eadddev;
1051 } else if (!icl->add_device || !icl->del_device) {
1052 ret = -EINVAL;
1053 goto eadddev;
1054 } else {
1055 if (icl->module_name)
1056 ret = request_module(icl->module_name);
1057
1058 ret = icl->add_device(icd);
1059 if (ret < 0)
1060 goto eadddev;
1061
1062 /*
1063 * FIXME: this is racy, have to use driver-binding notification,
1064 * when it is available
1065 */
1066 control = to_soc_camera_control(icd);
1067 if (!control || !control->driver || !dev_get_drvdata(control) ||
1068 !try_module_get(control->driver->owner)) {
1069 icl->del_device(icd);
1070 ret = -ENODEV;
1071 goto enodrv;
1072 }
1073 }
1074
1075 sd = soc_camera_to_subdev(icd);
1076 sd->grp_id = (long)icd;
1077
1078 if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler))
1079 goto ectrl;
1080
1081 /* At this point client .probe() should have run already */
1082 ret = soc_camera_init_user_formats(icd);
1083 if (ret < 0)
1084 goto eiufmt;
1085
1086 icd->field = V4L2_FIELD_ANY;
1087
1088 /*
1089 * ..._video_start() will create a device node, video_register_device()
1090 * itself is protected against concurrent open() calls, but we also have
1091 * to protect our data.
1092 */
1093 mutex_lock(&icd->video_lock);
1094
1095 ret = soc_camera_video_start(icd);
1096 if (ret < 0)
1097 goto evidstart;
1098
1099 /* Try to improve our guess of a reasonable window format */
1100 if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
1101 icd->user_width = mf.width;
1102 icd->user_height = mf.height;
1103 icd->colorspace = mf.colorspace;
1104 icd->field = mf.field;
1105 }
1106
1107 ici->ops->remove(icd);
1108
1109 soc_camera_power_set(icd, icl, 0);
1110
1111 mutex_unlock(&icd->video_lock);
1112
1113 return 0;
1114
1115 evidstart:
1116 mutex_unlock(&icd->video_lock);
1117 soc_camera_free_user_formats(icd);
1118 eiufmt:
1119 ectrl:
1120 if (icl->board_info) {
1121 soc_camera_free_i2c(icd);
1122 } else {
1123 icl->del_device(icd);
1124 module_put(control->driver->owner);
1125 }
1126 enodrv:
1127 eadddev:
1128 video_device_release(icd->vdev);
1129 evdc:
1130 ici->ops->remove(icd);
1131 eadd:
1132 soc_camera_power_set(icd, icl, 0);
1133 epower:
1134 regulator_bulk_free(icl->num_regulators, icl->regulators);
1135 ereg:
1136 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1137 return ret;
1138 }
1139
1140 /*
1141 * This is called on device_unregister, which only means we have to disconnect
1142 * from the host, but not remove ourselves from the device list
1143 */
1144 static int soc_camera_remove(struct soc_camera_device *icd)
1145 {
1146 struct soc_camera_link *icl = to_soc_camera_link(icd);
1147 struct video_device *vdev = icd->vdev;
1148
1149 BUG_ON(!icd->parent);
1150
1151 v4l2_ctrl_handler_free(&icd->ctrl_handler);
1152 if (vdev) {
1153 video_unregister_device(vdev);
1154 icd->vdev = NULL;
1155 }
1156
1157 if (icl->board_info) {
1158 soc_camera_free_i2c(icd);
1159 } else {
1160 struct device_driver *drv = to_soc_camera_control(icd)->driver;
1161 if (drv) {
1162 icl->del_device(icd);
1163 module_put(drv->owner);
1164 }
1165 }
1166 soc_camera_free_user_formats(icd);
1167
1168 regulator_bulk_free(icl->num_regulators, icl->regulators);
1169
1170 return 0;
1171 }
1172
1173 static int default_cropcap(struct soc_camera_device *icd,
1174 struct v4l2_cropcap *a)
1175 {
1176 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1177 return v4l2_subdev_call(sd, video, cropcap, a);
1178 }
1179
1180 static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1181 {
1182 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1183 return v4l2_subdev_call(sd, video, g_crop, a);
1184 }
1185
1186 static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1187 {
1188 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1189 return v4l2_subdev_call(sd, video, s_crop, a);
1190 }
1191
1192 static int default_g_parm(struct soc_camera_device *icd,
1193 struct v4l2_streamparm *parm)
1194 {
1195 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1196 return v4l2_subdev_call(sd, video, g_parm, parm);
1197 }
1198
1199 static int default_s_parm(struct soc_camera_device *icd,
1200 struct v4l2_streamparm *parm)
1201 {
1202 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1203 return v4l2_subdev_call(sd, video, s_parm, parm);
1204 }
1205
1206 static int default_enum_fsizes(struct soc_camera_device *icd,
1207 struct v4l2_frmsizeenum *fsize)
1208 {
1209 int ret;
1210 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1211 const struct soc_camera_format_xlate *xlate;
1212 __u32 pixfmt = fsize->pixel_format;
1213 struct v4l2_frmsizeenum fsize_mbus = *fsize;
1214
1215 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1216 if (!xlate)
1217 return -EINVAL;
1218 /* map xlate-code to pixel_format, sensor only handle xlate-code*/
1219 fsize_mbus.pixel_format = xlate->code;
1220
1221 ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
1222 if (ret < 0)
1223 return ret;
1224
1225 *fsize = fsize_mbus;
1226 fsize->pixel_format = pixfmt;
1227
1228 return 0;
1229 }
1230
1231 int soc_camera_host_register(struct soc_camera_host *ici)
1232 {
1233 struct soc_camera_host *ix;
1234 int ret;
1235
1236 if (!ici || !ici->ops ||
1237 !ici->ops->try_fmt ||
1238 !ici->ops->set_fmt ||
1239 !ici->ops->set_bus_param ||
1240 !ici->ops->querycap ||
1241 ((!ici->ops->init_videobuf ||
1242 !ici->ops->reqbufs) &&
1243 !ici->ops->init_videobuf2) ||
1244 !ici->ops->add ||
1245 !ici->ops->remove ||
1246 !ici->ops->poll ||
1247 !ici->v4l2_dev.dev)
1248 return -EINVAL;
1249
1250 if (!ici->ops->set_crop)
1251 ici->ops->set_crop = default_s_crop;
1252 if (!ici->ops->get_crop)
1253 ici->ops->get_crop = default_g_crop;
1254 if (!ici->ops->cropcap)
1255 ici->ops->cropcap = default_cropcap;
1256 if (!ici->ops->set_parm)
1257 ici->ops->set_parm = default_s_parm;
1258 if (!ici->ops->get_parm)
1259 ici->ops->get_parm = default_g_parm;
1260 if (!ici->ops->enum_fsizes)
1261 ici->ops->enum_fsizes = default_enum_fsizes;
1262
1263 mutex_lock(&list_lock);
1264 list_for_each_entry(ix, &hosts, list) {
1265 if (ix->nr == ici->nr) {
1266 ret = -EBUSY;
1267 goto edevreg;
1268 }
1269 }
1270
1271 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1272 if (ret < 0)
1273 goto edevreg;
1274
1275 list_add_tail(&ici->list, &hosts);
1276 mutex_unlock(&list_lock);
1277
1278 scan_add_host(ici);
1279
1280 return 0;
1281
1282 edevreg:
1283 mutex_unlock(&list_lock);
1284 return ret;
1285 }
1286 EXPORT_SYMBOL(soc_camera_host_register);
1287
1288 /* Unregister all clients! */
1289 void soc_camera_host_unregister(struct soc_camera_host *ici)
1290 {
1291 struct soc_camera_device *icd;
1292
1293 mutex_lock(&list_lock);
1294
1295 list_del(&ici->list);
1296 list_for_each_entry(icd, &devices, list)
1297 if (icd->iface == ici->nr && to_soc_camera_control(icd))
1298 soc_camera_remove(icd);
1299
1300 mutex_unlock(&list_lock);
1301
1302 v4l2_device_unregister(&ici->v4l2_dev);
1303 }
1304 EXPORT_SYMBOL(soc_camera_host_unregister);
1305
1306 /* Image capture device */
1307 static int soc_camera_device_register(struct soc_camera_device *icd)
1308 {
1309 struct soc_camera_device *ix;
1310 int num = -1, i;
1311
1312 for (i = 0; i < 256 && num < 0; i++) {
1313 num = i;
1314 /* Check if this index is available on this interface */
1315 list_for_each_entry(ix, &devices, list) {
1316 if (ix->iface == icd->iface && ix->devnum == i) {
1317 num = -1;
1318 break;
1319 }
1320 }
1321 }
1322
1323 if (num < 0)
1324 /*
1325 * ok, we have 256 cameras on this host...
1326 * man, stay reasonable...
1327 */
1328 return -ENOMEM;
1329
1330 icd->devnum = num;
1331 icd->use_count = 0;
1332 icd->host_priv = NULL;
1333 mutex_init(&icd->video_lock);
1334
1335 list_add_tail(&icd->list, &devices);
1336
1337 return 0;
1338 }
1339
1340 static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1341 .vidioc_querycap = soc_camera_querycap,
1342 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1343 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1344 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1345 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1346 .vidioc_enum_input = soc_camera_enum_input,
1347 .vidioc_g_input = soc_camera_g_input,
1348 .vidioc_s_input = soc_camera_s_input,
1349 .vidioc_s_std = soc_camera_s_std,
1350 .vidioc_enum_framesizes = soc_camera_enum_fsizes,
1351 .vidioc_reqbufs = soc_camera_reqbufs,
1352 .vidioc_querybuf = soc_camera_querybuf,
1353 .vidioc_qbuf = soc_camera_qbuf,
1354 .vidioc_dqbuf = soc_camera_dqbuf,
1355 .vidioc_create_bufs = soc_camera_create_bufs,
1356 .vidioc_prepare_buf = soc_camera_prepare_buf,
1357 .vidioc_streamon = soc_camera_streamon,
1358 .vidioc_streamoff = soc_camera_streamoff,
1359 .vidioc_cropcap = soc_camera_cropcap,
1360 .vidioc_g_crop = soc_camera_g_crop,
1361 .vidioc_s_crop = soc_camera_s_crop,
1362 .vidioc_g_parm = soc_camera_g_parm,
1363 .vidioc_s_parm = soc_camera_s_parm,
1364 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1365 #ifdef CONFIG_VIDEO_ADV_DEBUG
1366 .vidioc_g_register = soc_camera_g_register,
1367 .vidioc_s_register = soc_camera_s_register,
1368 #endif
1369 };
1370
1371 static int video_dev_create(struct soc_camera_device *icd)
1372 {
1373 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1374 struct video_device *vdev = video_device_alloc();
1375
1376 if (!vdev)
1377 return -ENOMEM;
1378
1379 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
1380
1381 vdev->parent = icd->pdev;
1382 vdev->current_norm = V4L2_STD_UNKNOWN;
1383 vdev->fops = &soc_camera_fops;
1384 vdev->ioctl_ops = &soc_camera_ioctl_ops;
1385 vdev->release = video_device_release;
1386 vdev->tvnorms = V4L2_STD_UNKNOWN;
1387 vdev->ctrl_handler = &icd->ctrl_handler;
1388 vdev->lock = &icd->video_lock;
1389
1390 icd->vdev = vdev;
1391
1392 return 0;
1393 }
1394
1395 /*
1396 * Called from soc_camera_probe() above (with .video_lock held???)
1397 */
1398 static int soc_camera_video_start(struct soc_camera_device *icd)
1399 {
1400 const struct device_type *type = icd->vdev->dev.type;
1401 int ret;
1402
1403 if (!icd->parent)
1404 return -ENODEV;
1405
1406 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
1407 if (ret < 0) {
1408 dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
1409 return ret;
1410 }
1411
1412 /* Restore device type, possibly set by the subdevice driver */
1413 icd->vdev->dev.type = type;
1414
1415 return 0;
1416 }
1417
1418 static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1419 {
1420 struct soc_camera_link *icl = pdev->dev.platform_data;
1421 struct soc_camera_device *icd;
1422 int ret;
1423
1424 if (!icl)
1425 return -EINVAL;
1426
1427 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1428 if (!icd)
1429 return -ENOMEM;
1430
1431 icd->iface = icl->bus_id;
1432 icd->link = icl;
1433 icd->pdev = &pdev->dev;
1434 platform_set_drvdata(pdev, icd);
1435
1436 ret = soc_camera_device_register(icd);
1437 if (ret < 0)
1438 goto escdevreg;
1439
1440 icd->user_width = DEFAULT_WIDTH;
1441 icd->user_height = DEFAULT_HEIGHT;
1442
1443 return 0;
1444
1445 escdevreg:
1446 kfree(icd);
1447
1448 return ret;
1449 }
1450
1451 /*
1452 * Only called on rmmod for each platform device, since they are not
1453 * hot-pluggable. Now we know, that all our users - hosts and devices have
1454 * been unloaded already
1455 */
1456 static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1457 {
1458 struct soc_camera_device *icd = platform_get_drvdata(pdev);
1459
1460 if (!icd)
1461 return -EINVAL;
1462
1463 list_del(&icd->list);
1464
1465 kfree(icd);
1466
1467 return 0;
1468 }
1469
1470 static struct platform_driver __refdata soc_camera_pdrv = {
1471 .remove = __devexit_p(soc_camera_pdrv_remove),
1472 .driver = {
1473 .name = "soc-camera-pdrv",
1474 .owner = THIS_MODULE,
1475 },
1476 };
1477
1478 static int __init soc_camera_init(void)
1479 {
1480 return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
1481 }
1482
1483 static void __exit soc_camera_exit(void)
1484 {
1485 platform_driver_unregister(&soc_camera_pdrv);
1486 }
1487
1488 module_init(soc_camera_init);
1489 module_exit(soc_camera_exit);
1490
1491 MODULE_DESCRIPTION("Image capture bus driver");
1492 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1493 MODULE_LICENSE("GPL");
1494 MODULE_ALIAS("platform:soc-camera-pdrv");
This page took 0.11137 seconds and 5 git commands to generate.