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