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