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