blob: 804c675f2cb9a19d1f549155bc593a584c487d6a [file] [log] [blame]
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001/*
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
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030019#include <linux/device.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030020#include <linux/err.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030021#include <linux/i2c.h>
22#include <linux/init.h>
23#include <linux/list.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030024#include <linux/mutex.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030025#include <linux/module.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030026#include <linux/platform_device.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030027#include <linux/vmalloc.h>
28
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030029#include <media/soc_camera.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030030#include <media/v4l2-common.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030031#include <media/v4l2-ioctl.h>
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030032#include <media/v4l2-dev.h>
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -030033#include <media/videobuf-core.h>
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030034
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -030035/* Default to VGA resolution */
36#define DEFAULT_WIDTH 640
37#define DEFAULT_HEIGHT 480
38
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030039static LIST_HEAD(hosts);
40static LIST_HEAD(devices);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030041static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030042
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030043const struct soc_camera_data_format *soc_camera_format_by_fourcc(
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -030044 struct soc_camera_device *icd, unsigned int fourcc)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030045{
46 unsigned int i;
47
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -030048 for (i = 0; i < icd->num_formats; i++)
49 if (icd->formats[i].fourcc == fourcc)
50 return icd->formats + i;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030051 return NULL;
52}
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -030053EXPORT_SYMBOL(soc_camera_format_by_fourcc);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -030054
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -030055const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
56 struct soc_camera_device *icd, unsigned int fourcc)
57{
58 unsigned int i;
59
60 for (i = 0; i < icd->num_user_formats; i++)
61 if (icd->user_formats[i].host_fmt->fourcc == fourcc)
62 return icd->user_formats + i;
63 return NULL;
64}
65EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
66
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -030067/**
68 * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
69 * @icl: camera platform parameters
70 * @flags: flags to be inverted according to platform configuration
71 * @return: resulting flags
72 */
73unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
74 unsigned long flags)
75{
76 unsigned long f;
77
78 /* If only one of the two polarities is supported, switch to the opposite */
79 if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
80 f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
81 if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
82 flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
83 }
84
85 if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
86 f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
87 if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
88 flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
89 }
90
91 if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
92 f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
93 if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
94 flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
95 }
96
97 return flags;
98}
99EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
100
Hans Verkuil72937892008-06-01 10:35:18 -0300101static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300102 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300103{
104 struct soc_camera_file *icf = file->private_data;
105 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300106 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300107
108 WARN_ON(priv != file->private_data);
109
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300110 /* limit format to hardware capabilities */
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -0300111 return ici->ops->try_fmt(icd, f);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300112}
113
114static int soc_camera_enum_input(struct file *file, void *priv,
115 struct v4l2_input *inp)
116{
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300117 struct soc_camera_file *icf = file->private_data;
118 struct soc_camera_device *icd = icf->icd;
119 int ret = 0;
120
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300121 if (inp->index != 0)
122 return -EINVAL;
123
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300124 if (icd->ops->enum_input)
125 ret = icd->ops->enum_input(icd, inp);
126 else {
127 /* default is camera */
128 inp->type = V4L2_INPUT_TYPE_CAMERA;
129 inp->std = V4L2_STD_UNKNOWN;
130 strcpy(inp->name, "Camera");
131 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300132
Kuninori Morimoto34d359d2008-12-18 12:47:46 -0300133 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300134}
135
136static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
137{
138 *i = 0;
139
140 return 0;
141}
142
143static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
144{
145 if (i > 0)
146 return -EINVAL;
147
148 return 0;
149}
150
151static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
152{
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300153 struct soc_camera_file *icf = file->private_data;
154 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300155 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Kuninori Morimoto513791a2008-12-18 12:46:45 -0300156
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300157 return v4l2_subdev_call(sd, core, s_std, *a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300158}
159
160static int soc_camera_reqbufs(struct file *file, void *priv,
161 struct v4l2_requestbuffers *p)
162{
163 int ret;
164 struct soc_camera_file *icf = file->private_data;
165 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300166 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300167
168 WARN_ON(priv != file->private_data);
169
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300170 ret = videobuf_reqbufs(&icf->vb_vidq, p);
171 if (ret < 0)
172 return ret;
173
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300174 return ici->ops->reqbufs(icf, p);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300175}
176
177static int soc_camera_querybuf(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_querybuf(&icf->vb_vidq, p);
185}
186
187static int soc_camera_qbuf(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_qbuf(&icf->vb_vidq, p);
195}
196
197static int soc_camera_dqbuf(struct file *file, void *priv,
198 struct v4l2_buffer *p)
199{
200 struct soc_camera_file *icf = file->private_data;
201
202 WARN_ON(priv != file->private_data);
203
204 return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
205}
206
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300207/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300208static int soc_camera_init_user_formats(struct soc_camera_device *icd)
209{
210 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300211 int i, fmts = 0, ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300212
213 if (!ici->ops->get_formats)
214 /*
215 * Fallback mode - the host will have to serve all
216 * sensor-provided formats one-to-one to the user
217 */
218 fmts = icd->num_formats;
219 else
220 /*
221 * First pass - only count formats this host-sensor
222 * configuration can provide
223 */
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300224 for (i = 0; i < icd->num_formats; i++) {
225 ret = ici->ops->get_formats(icd, i, NULL);
226 if (ret < 0)
227 return ret;
228 fmts += ret;
229 }
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300230
231 if (!fmts)
232 return -ENXIO;
233
234 icd->user_formats =
235 vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
236 if (!icd->user_formats)
237 return -ENOMEM;
238
239 icd->num_user_formats = fmts;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300240
241 dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
242
243 /* Second pass - actually fill data formats */
Stefan Herbrechtsmeier14df2cc2009-06-24 10:31:25 -0300244 fmts = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300245 for (i = 0; i < icd->num_formats; i++)
246 if (!ici->ops->get_formats) {
247 icd->user_formats[i].host_fmt = icd->formats + i;
248 icd->user_formats[i].cam_fmt = icd->formats + i;
249 icd->user_formats[i].buswidth = icd->formats[i].depth;
250 } else {
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300251 ret = ici->ops->get_formats(icd, i,
252 &icd->user_formats[fmts]);
253 if (ret < 0)
254 goto egfmt;
255 fmts += ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300256 }
257
258 icd->current_fmt = icd->user_formats[0].host_fmt;
259
260 return 0;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300261
262egfmt:
263 icd->num_user_formats = 0;
264 vfree(icd->user_formats);
265 return ret;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300266}
267
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300268/* Always entered with .video_lock held */
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300269static void soc_camera_free_user_formats(struct soc_camera_device *icd)
270{
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300271 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
272
273 if (ici->ops->put_formats)
274 ici->ops->put_formats(icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300275 icd->current_fmt = NULL;
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300276 icd->num_user_formats = 0;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300277 vfree(icd->user_formats);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300278 icd->user_formats = NULL;
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300279}
280
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300281#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
282 ((x) >> 24) & 0xff
283
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300284/* Called with .vb_lock held */
285static int soc_camera_set_fmt(struct soc_camera_file *icf,
286 struct v4l2_format *f)
287{
288 struct soc_camera_device *icd = icf->icd;
289 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
290 struct v4l2_pix_format *pix = &f->fmt.pix;
291 int ret;
292
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300293 dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
294 pixfmtstr(pix->pixelformat), pix->width, pix->height);
295
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300296 /* We always call try_fmt() before set_fmt() or set_crop() */
297 ret = ici->ops->try_fmt(icd, f);
298 if (ret < 0)
299 return ret;
300
301 ret = ici->ops->set_fmt(icd, f);
302 if (ret < 0) {
303 return ret;
304 } else if (!icd->current_fmt ||
305 icd->current_fmt->fourcc != pix->pixelformat) {
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300306 dev_err(&icd->dev,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300307 "Host driver hasn't set up current format correctly!\n");
308 return -EINVAL;
309 }
310
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300311 icd->user_width = pix->width;
312 icd->user_height = pix->height;
313 icf->vb_vidq.field =
314 icd->field = pix->field;
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300315
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300316 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
317 dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
318 f->type);
319
320 dev_dbg(&icd->dev, "set width: %d height: %d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300321 icd->user_width, icd->user_height);
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300322
323 /* set physical bus parameters */
324 return ici->ops->set_bus_param(icd, pix->pixelformat);
325}
326
Hans Verkuilbec43662008-12-30 06:58:20 -0300327static int soc_camera_open(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300328{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300329 struct video_device *vdev = video_devdata(file);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300330 struct soc_camera_device *icd = container_of(vdev->parent,
331 struct soc_camera_device,
332 dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300333 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300334 struct soc_camera_host *ici;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300335 struct soc_camera_file *icf;
336 int ret;
337
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300338 if (!icd->ops)
339 /* No device driver attached */
340 return -ENODEV;
341
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300342 ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300343
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300344 icf = vmalloc(sizeof(*icf));
345 if (!icf)
346 return -ENOMEM;
347
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300348 if (!try_module_get(ici->ops->owner)) {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300349 dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
350 ret = -EINVAL;
351 goto emgi;
352 }
353
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300354 /*
355 * Protect against icd->ops->remove() until we module_get() both
356 * drivers.
357 */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300358 mutex_lock(&icd->video_lock);
359
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300360 icf->icd = icd;
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300361 icd->use_count++;
362
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300363 /* Now we really have to activate the camera */
364 if (icd->use_count == 1) {
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300365 /* Restore parameters before the last close() per V4L2 API */
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300366 struct v4l2_format f = {
367 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
368 .fmt.pix = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300369 .width = icd->user_width,
370 .height = icd->user_height,
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300371 .field = icd->field,
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300372 .pixelformat = icd->current_fmt->fourcc,
373 .colorspace = icd->current_fmt->colorspace,
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300374 },
375 };
376
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300377 if (icl->power) {
378 ret = icl->power(icd->pdev, 1);
379 if (ret < 0)
380 goto epower;
381 }
382
383 /* The camera could have been already on, try to reset */
384 if (icl->reset)
385 icl->reset(icd->pdev);
386
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300387 ret = ici->ops->add(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300388 if (ret < 0) {
389 dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300390 goto eiciadd;
391 }
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300392
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300393 /* Try to configure with default parameters */
394 ret = soc_camera_set_fmt(icf, &f);
395 if (ret < 0)
396 goto esfmt;
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300397 }
398
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300399 file->private_data = icf;
400 dev_dbg(&icd->dev, "camera device open\n");
401
Magnus Damma034d1b2008-07-11 20:59:34 -0300402 ici->ops->init_videobuf(&icf->vb_vidq, icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300403
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300404 mutex_unlock(&icd->video_lock);
405
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300406 return 0;
407
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300408 /*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300409 * First five errors are entered with the .video_lock held
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300410 * and use_count == 1
411 */
412esfmt:
413 ici->ops->remove(icd);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300414eiciadd:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300415 if (icl->power)
416 icl->power(icd->pdev, 0);
417epower:
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300418 icd->use_count--;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300419 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300420 module_put(ici->ops->owner);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300421emgi:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300422 vfree(icf);
423 return ret;
424}
425
Hans Verkuilbec43662008-12-30 06:58:20 -0300426static int soc_camera_close(struct file *file)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300427{
428 struct soc_camera_file *icf = file->private_data;
429 struct soc_camera_device *icd = icf->icd;
430 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300431
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300432 mutex_lock(&icd->video_lock);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300433 icd->use_count--;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300434 if (!icd->use_count) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300435 struct soc_camera_link *icl = to_soc_camera_link(icd);
436
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300437 ici->ops->remove(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300438 if (icl->power)
439 icl->power(icd->pdev, 0);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300440 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300441
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300442 mutex_unlock(&icd->video_lock);
443
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300444 module_put(ici->ops->owner);
Guennadi Liakhovetski9dc4e482008-04-22 14:45:32 -0300445
Guennadi Liakhovetski1a0063a2008-04-04 13:46:34 -0300446 vfree(icf);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300447
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300448 dev_dbg(&icd->dev, "camera device close\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300449
450 return 0;
451}
452
Andrew Mortonaba360d2008-04-22 14:45:59 -0300453static ssize_t soc_camera_read(struct file *file, char __user *buf,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300454 size_t count, loff_t *ppos)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300455{
456 struct soc_camera_file *icf = file->private_data;
457 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300458 int err = -EINVAL;
459
Guennadi Liakhovetski2aa58db2009-08-25 11:47:00 -0300460 dev_err(&icd->dev, "camera device read not implemented\n");
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300461
462 return err;
463}
464
465static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
466{
467 struct soc_camera_file *icf = file->private_data;
468 struct soc_camera_device *icd = icf->icd;
469 int err;
470
471 dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
472
473 err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
474
475 dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
476 (unsigned long)vma->vm_start,
477 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
478 err);
479
480 return err;
481}
482
483static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
484{
485 struct soc_camera_file *icf = file->private_data;
486 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300487 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300488
489 if (list_empty(&icf->vb_vidq.stream)) {
490 dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
491 return POLLERR;
492 }
493
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300494 return ici->ops->poll(file, pt);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300495}
496
Hans Verkuilbec43662008-12-30 06:58:20 -0300497static struct v4l2_file_operations soc_camera_fops = {
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300498 .owner = THIS_MODULE,
499 .open = soc_camera_open,
500 .release = soc_camera_close,
501 .ioctl = video_ioctl2,
502 .read = soc_camera_read,
503 .mmap = soc_camera_mmap,
504 .poll = soc_camera_poll,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300505};
506
Hans Verkuil72937892008-06-01 10:35:18 -0300507static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300508 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300509{
510 struct soc_camera_file *icf = file->private_data;
511 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300512 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300513
514 WARN_ON(priv != file->private_data);
515
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300516 mutex_lock(&icf->vb_vidq.vb_lock);
517
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300518 if (icf->vb_vidq.bufs[0]) {
519 dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300520 ret = -EBUSY;
521 goto unlock;
522 }
523
Guennadi Liakhovetskidf2ed072009-03-13 06:08:20 -0300524 ret = soc_camera_set_fmt(icf, f);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300525
526unlock:
527 mutex_unlock(&icf->vb_vidq.vb_lock);
528
529 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300530}
531
Hans Verkuil72937892008-06-01 10:35:18 -0300532static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300533 struct v4l2_fmtdesc *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300534{
535 struct soc_camera_file *icf = file->private_data;
536 struct soc_camera_device *icd = icf->icd;
537 const struct soc_camera_data_format *format;
538
539 WARN_ON(priv != file->private_data);
540
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300541 if (f->index >= icd->num_user_formats)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300542 return -EINVAL;
543
Guennadi Liakhovetskic2786ad2008-12-01 09:45:27 -0300544 format = icd->user_formats[f->index].host_fmt;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300545
546 strlcpy(f->description, format->name, sizeof(f->description));
547 f->pixelformat = format->fourcc;
548 return 0;
549}
550
Hans Verkuil72937892008-06-01 10:35:18 -0300551static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
Guennadi Liakhovetskiabe4c472008-12-01 09:44:56 -0300552 struct v4l2_format *f)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300553{
554 struct soc_camera_file *icf = file->private_data;
555 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300556 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300557
558 WARN_ON(priv != file->private_data);
559
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300560 pix->width = icd->user_width;
561 pix->height = icd->user_height;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300562 pix->field = icf->vb_vidq.field;
563 pix->pixelformat = icd->current_fmt->fourcc;
564 pix->bytesperline = pix->width *
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -0300565 DIV_ROUND_UP(icd->current_fmt->depth, 8);
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300566 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300567 dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
568 icd->current_fmt->fourcc);
569 return 0;
570}
571
572static int soc_camera_querycap(struct file *file, void *priv,
573 struct v4l2_capability *cap)
574{
575 struct soc_camera_file *icf = file->private_data;
576 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300577 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300578
579 WARN_ON(priv != file->private_data);
580
581 strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -0300582 return ici->ops->querycap(ici, cap);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300583}
584
585static int soc_camera_streamon(struct file *file, void *priv,
586 enum v4l2_buf_type i)
587{
588 struct soc_camera_file *icf = file->private_data;
589 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300590 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300591 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300592
593 WARN_ON(priv != file->private_data);
594
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300595 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
596 return -EINVAL;
597
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300598 mutex_lock(&icd->video_lock);
599
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300600 v4l2_subdev_call(sd, video, s_stream, 1);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300601
602 /* This calls buf_queue from host driver's videobuf_queue_ops */
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300603 ret = videobuf_streamon(&icf->vb_vidq);
604
605 mutex_unlock(&icd->video_lock);
606
607 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300608}
609
610static int soc_camera_streamoff(struct file *file, void *priv,
611 enum v4l2_buf_type i)
612{
613 struct soc_camera_file *icf = file->private_data;
614 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300615 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300616
617 WARN_ON(priv != file->private_data);
618
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300619 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
620 return -EINVAL;
621
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300622 mutex_lock(&icd->video_lock);
623
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300624 /* This calls buf_release from host driver's videobuf_queue_ops for all
625 * remaining buffers. When the last buffer is freed, stop capture */
626 videobuf_streamoff(&icf->vb_vidq);
627
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300628 v4l2_subdev_call(sd, video, s_stream, 0);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300629
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300630 mutex_unlock(&icd->video_lock);
631
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300632 return 0;
633}
634
635static int soc_camera_queryctrl(struct file *file, void *priv,
636 struct v4l2_queryctrl *qc)
637{
638 struct soc_camera_file *icf = file->private_data;
639 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300640 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300641 int i;
642
643 WARN_ON(priv != file->private_data);
644
645 if (!qc->id)
646 return -EINVAL;
647
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300648 /* First check host controls */
649 for (i = 0; i < ici->ops->num_controls; i++)
650 if (qc->id == ici->ops->controls[i].id) {
651 memcpy(qc, &(ici->ops->controls[i]),
652 sizeof(*qc));
653 return 0;
654 }
655
656 /* Then device controls */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300657 for (i = 0; i < icd->ops->num_controls; i++)
658 if (qc->id == icd->ops->controls[i].id) {
659 memcpy(qc, &(icd->ops->controls[i]),
660 sizeof(*qc));
661 return 0;
662 }
663
664 return -EINVAL;
665}
666
667static int soc_camera_g_ctrl(struct file *file, void *priv,
668 struct v4l2_control *ctrl)
669{
670 struct soc_camera_file *icf = file->private_data;
671 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300672 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300673 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300674 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300675
676 WARN_ON(priv != file->private_data);
677
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300678 if (ici->ops->get_ctrl) {
679 ret = ici->ops->get_ctrl(icd, ctrl);
680 if (ret != -ENOIOCTLCMD)
681 return ret;
682 }
683
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300684 return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300685}
686
687static int soc_camera_s_ctrl(struct file *file, void *priv,
688 struct v4l2_control *ctrl)
689{
690 struct soc_camera_file *icf = file->private_data;
691 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300692 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300693 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300694 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300695
696 WARN_ON(priv != file->private_data);
697
Guennadi Liakhovetski2840d242009-08-25 11:44:15 -0300698 if (ici->ops->set_ctrl) {
699 ret = ici->ops->set_ctrl(icd, ctrl);
700 if (ret != -ENOIOCTLCMD)
701 return ret;
702 }
703
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300704 return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300705}
706
707static int soc_camera_cropcap(struct file *file, void *fh,
708 struct v4l2_cropcap *a)
709{
710 struct soc_camera_file *icf = file->private_data;
711 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300712 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300713
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300714 return ici->ops->cropcap(icd, a);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300715}
716
717static int soc_camera_g_crop(struct file *file, void *fh,
718 struct v4l2_crop *a)
719{
720 struct soc_camera_file *icf = file->private_data;
721 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300722 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
723 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300724
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300725 mutex_lock(&icf->vb_vidq.vb_lock);
726 ret = ici->ops->get_crop(icd, a);
727 mutex_unlock(&icf->vb_vidq.vb_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300728
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300729 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300730}
731
Guennadi Liakhovetski68a54f02009-08-25 11:46:52 -0300732/*
733 * According to the V4L2 API, drivers shall not update the struct v4l2_crop
734 * argument with the actual geometry, instead, the user shall use G_CROP to
735 * retrieve it. However, we expect camera host and client drivers to update
736 * the argument, which we then use internally, but do not return to the user.
737 */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300738static int soc_camera_s_crop(struct file *file, void *fh,
739 struct v4l2_crop *a)
740{
741 struct soc_camera_file *icf = file->private_data;
742 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300743 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300744 struct v4l2_rect *rect = &a->c;
745 struct v4l2_crop current_crop;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300746 int ret;
747
748 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
749 return -EINVAL;
750
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300751 dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
752 rect->width, rect->height, rect->left, rect->top);
753
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300754 /* Cropping is allowed during a running capture, guard consistency */
755 mutex_lock(&icf->vb_vidq.vb_lock);
756
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300757 /* If get_crop fails, we'll let host and / or client drivers decide */
758 ret = ici->ops->get_crop(icd, &current_crop);
759
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300760 /* Prohibit window size change with initialised buffers */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300761 if (icf->vb_vidq.bufs[0] && !ret &&
762 (a->c.width != current_crop.c.width ||
763 a->c.height != current_crop.c.height)) {
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300764 dev_err(&icd->dev,
765 "S_CROP denied: queue initialised and sizes differ\n");
766 ret = -EBUSY;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300767 } else {
768 ret = ici->ops->set_crop(icd, a);
Guennadi Liakhovetskib897a912009-08-25 11:46:53 -0300769 }
770
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300771 mutex_unlock(&icf->vb_vidq.vb_lock);
772
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300773 return ret;
774}
775
776static int soc_camera_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300777 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300778{
779 struct soc_camera_file *icf = file->private_data;
780 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300781 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300782
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300783 return v4l2_subdev_call(sd, core, g_chip_ident, id);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300784}
785
786#ifdef CONFIG_VIDEO_ADV_DEBUG
787static int soc_camera_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300788 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300789{
790 struct soc_camera_file *icf = file->private_data;
791 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300792 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300793
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300794 return v4l2_subdev_call(sd, core, g_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300795}
796
797static int soc_camera_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300798 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300799{
800 struct soc_camera_file *icf = file->private_data;
801 struct soc_camera_device *icd = icf->icd;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300802 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300803
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300804 return v4l2_subdev_call(sd, core, s_register, reg);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300805}
806#endif
807
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300808/* So far this function cannot fail */
809static void scan_add_host(struct soc_camera_host *ici)
810{
811 struct soc_camera_device *icd;
812
813 mutex_lock(&list_lock);
814
815 list_for_each_entry(icd, &devices, list) {
816 if (icd->iface == ici->nr) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300817 int ret;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300818 icd->dev.parent = ici->v4l2_dev.dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300819 dev_set_name(&icd->dev, "%u-%u", icd->iface,
820 icd->devnum);
821 ret = device_register(&icd->dev);
822 if (ret < 0) {
823 icd->dev.parent = NULL;
824 dev_err(&icd->dev,
825 "Cannot register device: %d\n", ret);
826 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300827 }
828 }
829
830 mutex_unlock(&list_lock);
831}
832
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300833#ifdef CONFIG_I2C_BOARDINFO
834static int soc_camera_init_i2c(struct soc_camera_device *icd,
835 struct soc_camera_link *icl)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300836{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300837 struct i2c_client *client;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300838 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300839 struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300840 struct v4l2_subdev *subdev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300841 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300842
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300843 if (!adap) {
844 ret = -ENODEV;
845 dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
846 icl->i2c_adapter_id);
847 goto ei2cga;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300848 }
849
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300850 icl->board_info->platform_data = icd;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300851
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300852 subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
853 icl->module_name, icl->board_info, NULL);
854 if (!subdev) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300855 ret = -ENOMEM;
856 goto ei2cnd;
857 }
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300858
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300859 client = subdev->priv;
860
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300861 /* Use to_i2c_client(dev) to recover the i2c client */
862 dev_set_drvdata(&icd->dev, &client->dev);
863
864 return 0;
865ei2cnd:
866 i2c_put_adapter(adap);
867ei2cga:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300868 return ret;
869}
870
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300871static void soc_camera_free_i2c(struct soc_camera_device *icd)
872{
873 struct i2c_client *client =
874 to_i2c_client(to_soc_camera_control(icd));
875 dev_set_drvdata(&icd->dev, NULL);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300876 v4l2_device_unregister_subdev(i2c_get_clientdata(client));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300877 i2c_unregister_device(client);
878 i2c_put_adapter(client->adapter);
879}
880#else
881#define soc_camera_init_i2c(icd, icl) (-ENODEV)
882#define soc_camera_free_i2c(icd) do {} while (0)
883#endif
884
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300885static int soc_camera_video_start(struct soc_camera_device *icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300886static int video_dev_create(struct soc_camera_device *icd);
887/* Called during host-driver probe */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300888static int soc_camera_probe(struct device *dev)
889{
890 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300891 struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300892 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300893 struct device *control = NULL;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300894 struct v4l2_subdev *sd;
895 struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300896 int ret;
897
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300898 dev_info(dev, "Probing %s\n", dev_name(dev));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300899
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300900 if (icl->power) {
901 ret = icl->power(icd->pdev, 1);
902 if (ret < 0) {
903 dev_err(dev,
904 "Platform failed to power-on the camera.\n");
905 goto epower;
906 }
907 }
908
909 /* The camera could have been already on, try to reset */
910 if (icl->reset)
911 icl->reset(icd->pdev);
912
913 ret = ici->ops->add(icd);
914 if (ret < 0)
915 goto eadd;
916
917 /* Must have icd->vdev before registering the device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300918 ret = video_dev_create(icd);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300919 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300920 goto evdc;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -0300921
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300922 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
923 if (icl->board_info) {
924 ret = soc_camera_init_i2c(icd, icl);
925 if (ret < 0)
926 goto eadddev;
927 } else if (!icl->add_device || !icl->del_device) {
928 ret = -EINVAL;
929 goto eadddev;
930 } else {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300931 if (icl->module_name)
932 ret = request_module(icl->module_name);
933
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300934 ret = icl->add_device(icl, &icd->dev);
935 if (ret < 0)
936 goto eadddev;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300937
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300938 /*
939 * FIXME: this is racy, have to use driver-binding notification,
940 * when it is available
941 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300942 control = to_soc_camera_control(icd);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300943 if (!control || !control->driver || !dev_get_drvdata(control) ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300944 !try_module_get(control->driver->owner)) {
945 icl->del_device(icl);
946 goto enodrv;
947 }
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -0300948 }
949
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300950 /* At this point client .probe() should have run already */
951 ret = soc_camera_init_user_formats(icd);
952 if (ret < 0)
953 goto eiufmt;
954
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300955 icd->field = V4L2_FIELD_ANY;
956
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300957 /* ..._video_start() will create a device node, so we have to protect */
958 mutex_lock(&icd->video_lock);
959
960 ret = soc_camera_video_start(icd);
961 if (ret < 0)
962 goto evidstart;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300963
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300964 /* Try to improve our guess of a reasonable window format */
965 sd = soc_camera_to_subdev(icd);
966 if (!v4l2_subdev_call(sd, video, g_fmt, &f)) {
967 icd->user_width = f.fmt.pix.width;
968 icd->user_height = f.fmt.pix.height;
969 }
970
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300971 /* Do we have to sysfs_remove_link() before device_unregister()? */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300972 if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300973 "control"))
974 dev_warn(&icd->dev, "Failed creating the control symlink\n");
975
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300976 ici->ops->remove(icd);
977
978 if (icl->power)
979 icl->power(icd->pdev, 0);
980
981 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300982
983 return 0;
984
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300985evidstart:
986 mutex_unlock(&icd->video_lock);
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -0300987 soc_camera_free_user_formats(icd);
988eiufmt:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300989 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300990 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300991 } else {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300992 icl->del_device(icl);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300993 module_put(control->driver->owner);
994 }
995enodrv:
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300996eadddev:
997 video_device_release(icd->vdev);
998evdc:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300999 ici->ops->remove(icd);
1000eadd:
1001 if (icl->power)
1002 icl->power(icd->pdev, 0);
1003epower:
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001004 return ret;
1005}
1006
1007/* This is called on device_unregister, which only means we have to disconnect
1008 * from the host, but not remove ourselves from the device list */
1009static int soc_camera_remove(struct device *dev)
1010{
1011 struct soc_camera_device *icd = to_soc_camera_dev(dev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001012 struct soc_camera_link *icl = to_soc_camera_link(icd);
1013 struct video_device *vdev = icd->vdev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001014
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001015 BUG_ON(!dev->parent);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001016
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001017 if (vdev) {
1018 mutex_lock(&icd->video_lock);
1019 video_unregister_device(vdev);
1020 icd->vdev = NULL;
1021 mutex_unlock(&icd->video_lock);
1022 }
1023
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001024 if (icl->board_info) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001025 soc_camera_free_i2c(icd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001026 } else {
1027 struct device_driver *drv = to_soc_camera_control(icd) ?
1028 to_soc_camera_control(icd)->driver : NULL;
1029 if (drv) {
1030 icl->del_device(icl);
1031 module_put(drv->owner);
1032 }
1033 }
Guennadi Liakhovetskifa489842009-08-25 11:46:43 -03001034 soc_camera_free_user_formats(icd);
Guennadi Liakhovetski025c18a2009-03-13 06:08:20 -03001035
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001036 return 0;
1037}
1038
Robert Jarzmik2e521062008-08-01 20:14:50 -03001039static int soc_camera_suspend(struct device *dev, pm_message_t state)
1040{
1041 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1042 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1043 int ret = 0;
1044
1045 if (ici->ops->suspend)
1046 ret = ici->ops->suspend(icd, state);
1047
1048 return ret;
1049}
1050
1051static int soc_camera_resume(struct device *dev)
1052{
1053 struct soc_camera_device *icd = to_soc_camera_dev(dev);
1054 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1055 int ret = 0;
1056
1057 if (ici->ops->resume)
1058 ret = ici->ops->resume(icd);
1059
1060 return ret;
1061}
1062
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001063static struct bus_type soc_camera_bus_type = {
1064 .name = "soc-camera",
1065 .probe = soc_camera_probe,
1066 .remove = soc_camera_remove,
Robert Jarzmik2e521062008-08-01 20:14:50 -03001067 .suspend = soc_camera_suspend,
1068 .resume = soc_camera_resume,
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001069};
1070
1071static struct device_driver ic_drv = {
1072 .name = "camera",
1073 .bus = &soc_camera_bus_type,
1074 .owner = THIS_MODULE,
1075};
1076
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001077static void dummy_release(struct device *dev)
1078{
1079}
1080
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001081static int default_cropcap(struct soc_camera_device *icd,
1082 struct v4l2_cropcap *a)
1083{
1084 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1085 return v4l2_subdev_call(sd, video, cropcap, a);
1086}
1087
1088static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1089{
1090 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1091 return v4l2_subdev_call(sd, video, g_crop, a);
1092}
1093
1094static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
1095{
1096 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1097 return v4l2_subdev_call(sd, video, s_crop, a);
1098}
1099
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001100static void soc_camera_device_init(struct device *dev, void *pdata)
1101{
1102 dev->platform_data = pdata;
1103 dev->bus = &soc_camera_bus_type;
1104 dev->release = dummy_release;
1105}
1106
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001107int soc_camera_host_register(struct soc_camera_host *ici)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001108{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001109 struct soc_camera_host *ix;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001110 int ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001111
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001112 if (!ici || !ici->ops ||
1113 !ici->ops->try_fmt ||
1114 !ici->ops->set_fmt ||
1115 !ici->ops->set_bus_param ||
1116 !ici->ops->querycap ||
1117 !ici->ops->init_videobuf ||
1118 !ici->ops->reqbufs ||
1119 !ici->ops->add ||
1120 !ici->ops->remove ||
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001121 !ici->ops->poll ||
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001122 !ici->v4l2_dev.dev)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001123 return -EINVAL;
1124
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001125 if (!ici->ops->set_crop)
1126 ici->ops->set_crop = default_s_crop;
1127 if (!ici->ops->get_crop)
1128 ici->ops->get_crop = default_g_crop;
1129 if (!ici->ops->cropcap)
1130 ici->ops->cropcap = default_cropcap;
1131
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001132 mutex_lock(&list_lock);
1133 list_for_each_entry(ix, &hosts, list) {
1134 if (ix->nr == ici->nr) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001135 ret = -EBUSY;
1136 goto edevreg;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001137 }
1138 }
1139
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001140 ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
1141 if (ret < 0)
1142 goto edevreg;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001143
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001144 list_add_tail(&ici->list, &hosts);
1145 mutex_unlock(&list_lock);
1146
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001147 scan_add_host(ici);
1148
1149 return 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001150
1151edevreg:
1152 mutex_unlock(&list_lock);
1153 return ret;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001154}
1155EXPORT_SYMBOL(soc_camera_host_register);
1156
1157/* Unregister all clients! */
1158void soc_camera_host_unregister(struct soc_camera_host *ici)
1159{
1160 struct soc_camera_device *icd;
1161
1162 mutex_lock(&list_lock);
1163
1164 list_del(&ici->list);
1165
1166 list_for_each_entry(icd, &devices, list) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001167 if (icd->iface == ici->nr) {
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001168 void *pdata = icd->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001169 /* The bus->remove will be called */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001170 device_unregister(&icd->dev);
Guennadi Liakhovetski76823b72009-10-05 12:54:54 -03001171 /*
1172 * Not before device_unregister(), .remove
1173 * needs parent to call ici->ops->remove().
1174 * If the host module is loaded again, device_register()
1175 * would complain "already initialised," since 2.6.32
1176 * this is also needed to prevent use-after-free of the
1177 * device private data.
1178 */
1179 memset(&icd->dev, 0, sizeof(icd->dev));
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001180 soc_camera_device_init(&icd->dev, pdata);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001181 }
1182 }
1183
1184 mutex_unlock(&list_lock);
1185
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001186 v4l2_device_unregister(&ici->v4l2_dev);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001187}
1188EXPORT_SYMBOL(soc_camera_host_unregister);
1189
1190/* Image capture device */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001191static int soc_camera_device_register(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001192{
1193 struct soc_camera_device *ix;
1194 int num = -1, i;
1195
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001196 for (i = 0; i < 256 && num < 0; i++) {
1197 num = i;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001198 /* Check if this index is available on this interface */
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001199 list_for_each_entry(ix, &devices, list) {
1200 if (ix->iface == icd->iface && ix->devnum == i) {
1201 num = -1;
1202 break;
1203 }
1204 }
1205 }
1206
1207 if (num < 0)
1208 /* ok, we have 256 cameras on this host...
1209 * man, stay reasonable... */
1210 return -ENOMEM;
1211
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001212 icd->devnum = num;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001213 icd->use_count = 0;
1214 icd->host_priv = NULL;
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001215 mutex_init(&icd->video_lock);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001216
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001217 list_add_tail(&icd->list, &devices);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001218
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001219 return 0;
1220}
1221
1222static void soc_camera_device_unregister(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001223{
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001224 list_del(&icd->list);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001225}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001226
Hans Verkuila3998102008-07-21 02:57:38 -03001227static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
1228 .vidioc_querycap = soc_camera_querycap,
1229 .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
1230 .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
1231 .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
1232 .vidioc_enum_input = soc_camera_enum_input,
1233 .vidioc_g_input = soc_camera_g_input,
1234 .vidioc_s_input = soc_camera_s_input,
1235 .vidioc_s_std = soc_camera_s_std,
1236 .vidioc_reqbufs = soc_camera_reqbufs,
1237 .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
1238 .vidioc_querybuf = soc_camera_querybuf,
1239 .vidioc_qbuf = soc_camera_qbuf,
1240 .vidioc_dqbuf = soc_camera_dqbuf,
1241 .vidioc_streamon = soc_camera_streamon,
1242 .vidioc_streamoff = soc_camera_streamoff,
1243 .vidioc_queryctrl = soc_camera_queryctrl,
1244 .vidioc_g_ctrl = soc_camera_g_ctrl,
1245 .vidioc_s_ctrl = soc_camera_s_ctrl,
1246 .vidioc_cropcap = soc_camera_cropcap,
1247 .vidioc_g_crop = soc_camera_g_crop,
1248 .vidioc_s_crop = soc_camera_s_crop,
1249 .vidioc_g_chip_ident = soc_camera_g_chip_ident,
1250#ifdef CONFIG_VIDEO_ADV_DEBUG
1251 .vidioc_g_register = soc_camera_g_register,
1252 .vidioc_s_register = soc_camera_s_register,
1253#endif
1254};
1255
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001256static int video_dev_create(struct soc_camera_device *icd)
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001257{
1258 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001259 struct video_device *vdev = video_device_alloc();
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001260
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001261 if (!vdev)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001262 return -ENOMEM;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001263
1264 strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -03001265
Hans Verkuil5e85e732008-07-20 06:31:39 -03001266 vdev->parent = &icd->dev;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001267 vdev->current_norm = V4L2_STD_UNKNOWN;
1268 vdev->fops = &soc_camera_fops;
Hans Verkuila3998102008-07-21 02:57:38 -03001269 vdev->ioctl_ops = &soc_camera_ioctl_ops;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001270 vdev->release = video_device_release;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001271 vdev->tvnorms = V4L2_STD_UNKNOWN;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001272
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001273 icd->vdev = vdev;
1274
1275 return 0;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001276}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001277
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001278/*
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001279 * Called from soc_camera_probe() above (with .video_lock held???)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001280 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001281static int soc_camera_video_start(struct soc_camera_device *icd)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001282{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001283 int ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001284
1285 if (!icd->dev.parent)
1286 return -ENODEV;
1287
1288 if (!icd->ops ||
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001289 !icd->ops->query_bus_param ||
1290 !icd->ops->set_bus_param)
1291 return -EINVAL;
1292
Laurent Pinchart46b21092009-12-10 11:51:38 -02001293 ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001294 if (ret < 0) {
1295 dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
1296 return ret;
1297 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001298
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001299 return 0;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001300}
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001301
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001302static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
1303{
1304 struct soc_camera_link *icl = pdev->dev.platform_data;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001305 struct soc_camera_device *icd;
1306 int ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001307
1308 if (!icl)
1309 return -EINVAL;
1310
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001311 icd = kzalloc(sizeof(*icd), GFP_KERNEL);
1312 if (!icd)
1313 return -ENOMEM;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001314
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001315 icd->iface = icl->bus_id;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001316 icd->pdev = &pdev->dev;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001317 platform_set_drvdata(pdev, icd);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001318
1319 ret = soc_camera_device_register(icd);
1320 if (ret < 0)
1321 goto escdevreg;
1322
Guennadi Liakhovetski64ff9ba2009-11-09 16:12:37 -03001323 soc_camera_device_init(&icd->dev, icl);
1324
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001325 icd->user_width = DEFAULT_WIDTH;
1326 icd->user_height = DEFAULT_HEIGHT;
1327
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001328 return 0;
1329
1330escdevreg:
1331 kfree(icd);
1332
1333 return ret;
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001334}
1335
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001336/* Only called on rmmod for each platform device, since they are not
1337 * hot-pluggable. Now we know, that all our users - hosts and devices have
1338 * been unloaded already */
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001339static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
1340{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001341 struct soc_camera_device *icd = platform_get_drvdata(pdev);
Guennadi Liakhovetskic41deba2009-08-25 11:06:21 -03001342
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001343 if (!icd)
1344 return -EINVAL;
1345
1346 soc_camera_device_unregister(icd);
1347
1348 kfree(icd);
1349
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001350 return 0;
1351}
1352
1353static struct platform_driver __refdata soc_camera_pdrv = {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001354 .remove = __devexit_p(soc_camera_pdrv_remove),
1355 .driver = {
1356 .name = "soc-camera-pdrv",
1357 .owner = THIS_MODULE,
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001358 },
1359};
1360
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001361static int __init soc_camera_init(void)
1362{
1363 int ret = bus_register(&soc_camera_bus_type);
1364 if (ret)
1365 return ret;
1366 ret = driver_register(&ic_drv);
1367 if (ret)
1368 goto edrvr;
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001369
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001370 ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001371 if (ret)
1372 goto epdr;
1373
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001374 return 0;
1375
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001376epdr:
1377 driver_unregister(&ic_drv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001378edrvr:
1379 bus_unregister(&soc_camera_bus_type);
1380 return ret;
1381}
1382
1383static void __exit soc_camera_exit(void)
1384{
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001385 platform_driver_unregister(&soc_camera_pdrv);
Guennadi Liakhovetskie55222e2008-04-22 14:42:03 -03001386 driver_unregister(&ic_drv);
1387 bus_unregister(&soc_camera_bus_type);
1388}
1389
1390module_init(soc_camera_init);
1391module_exit(soc_camera_exit);
1392
1393MODULE_DESCRIPTION("Image capture bus driver");
1394MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1395MODULE_LICENSE("GPL");
Guennadi Liakhovetski0fd327b2009-05-07 13:25:32 -03001396MODULE_ALIAS("platform:soc-camera-pdrv");