]>
Commit | Line | Data |
---|---|---|
e55222ef GL |
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 | ||
e55222ef | 19 | #include <linux/device.h> |
e55222ef | 20 | #include <linux/err.h> |
0fd327bd GL |
21 | #include <linux/i2c.h> |
22 | #include <linux/init.h> | |
23 | #include <linux/list.h> | |
e55222ef | 24 | #include <linux/mutex.h> |
40e2e092 | 25 | #include <linux/module.h> |
0fd327bd | 26 | #include <linux/platform_device.h> |
e55222ef GL |
27 | #include <linux/vmalloc.h> |
28 | ||
0fd327bd | 29 | #include <media/soc_camera.h> |
e55222ef | 30 | #include <media/v4l2-common.h> |
0fd327bd | 31 | #include <media/v4l2-ioctl.h> |
40e2e092 | 32 | #include <media/v4l2-dev.h> |
092d3921 | 33 | #include <media/videobuf-core.h> |
e55222ef | 34 | |
df2ed070 GL |
35 | /* Default to VGA resolution */ |
36 | #define DEFAULT_WIDTH 640 | |
37 | #define DEFAULT_HEIGHT 480 | |
38 | ||
e55222ef GL |
39 | static LIST_HEAD(hosts); |
40 | static LIST_HEAD(devices); | |
40e2e092 | 41 | static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ |
e55222ef | 42 | |
25c4d74e | 43 | const struct soc_camera_data_format *soc_camera_format_by_fourcc( |
abe4c471 | 44 | struct soc_camera_device *icd, unsigned int fourcc) |
e55222ef GL |
45 | { |
46 | unsigned int i; | |
47 | ||
26f1b942 GL |
48 | for (i = 0; i < icd->num_formats; i++) |
49 | if (icd->formats[i].fourcc == fourcc) | |
50 | return icd->formats + i; | |
e55222ef GL |
51 | return NULL; |
52 | } | |
25c4d74e | 53 | EXPORT_SYMBOL(soc_camera_format_by_fourcc); |
e55222ef | 54 | |
c2786ad2 GL |
55 | const 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 | } | |
65 | EXPORT_SYMBOL(soc_camera_xlate_by_fourcc); | |
66 | ||
bd73b36f GL |
67 | /** |
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 | */ | |
73 | unsigned 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 | } | |
99 | EXPORT_SYMBOL(soc_camera_apply_sensor_flags); | |
100 | ||
72937890 | 101 | static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv, |
abe4c471 | 102 | struct v4l2_format *f) |
e55222ef GL |
103 | { |
104 | struct soc_camera_file *icf = file->private_data; | |
105 | struct soc_camera_device *icd = icf->icd; | |
64f5905e | 106 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef GL |
107 | |
108 | WARN_ON(priv != file->private_data); | |
109 | ||
ad5f2e85 | 110 | /* limit format to hardware capabilities */ |
06daa1af | 111 | return ici->ops->try_fmt(icd, f); |
e55222ef GL |
112 | } |
113 | ||
114 | static int soc_camera_enum_input(struct file *file, void *priv, | |
115 | struct v4l2_input *inp) | |
116 | { | |
34d359db KM |
117 | struct soc_camera_file *icf = file->private_data; |
118 | struct soc_camera_device *icd = icf->icd; | |
119 | int ret = 0; | |
120 | ||
e55222ef GL |
121 | if (inp->index != 0) |
122 | return -EINVAL; | |
123 | ||
34d359db KM |
124 | 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 | } | |
e55222ef | 132 | |
34d359db | 133 | return ret; |
e55222ef GL |
134 | } |
135 | ||
136 | static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) | |
137 | { | |
138 | *i = 0; | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | static 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 | ||
151 | static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) | |
152 | { | |
513791ab KM |
153 | struct soc_camera_file *icf = file->private_data; |
154 | struct soc_camera_device *icd = icf->icd; | |
c9c1f1c0 | 155 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
513791ab | 156 | |
c9c1f1c0 | 157 | return v4l2_subdev_call(sd, core, s_std, *a); |
e55222ef GL |
158 | } |
159 | ||
160 | static 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; | |
64f5905e | 166 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef GL |
167 | |
168 | WARN_ON(priv != file->private_data); | |
169 | ||
e55222ef GL |
170 | ret = videobuf_reqbufs(&icf->vb_vidq, p); |
171 | if (ret < 0) | |
172 | return ret; | |
173 | ||
b8d9904c | 174 | return ici->ops->reqbufs(icf, p); |
e55222ef GL |
175 | } |
176 | ||
177 | static 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 | ||
187 | static 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 | ||
197 | static 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 | ||
40e2e092 | 207 | /* Always entered with .video_lock held */ |
c2786ad2 GL |
208 | static 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); | |
fa48984e | 211 | int i, fmts = 0, ret; |
c2786ad2 GL |
212 | |
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 | */ | |
fa48984e GL |
224 | 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 | } | |
c2786ad2 GL |
230 | |
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; | |
c2786ad2 GL |
240 | |
241 | dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts); | |
242 | ||
243 | /* Second pass - actually fill data formats */ | |
14df2cce | 244 | fmts = 0; |
c2786ad2 GL |
245 | 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 { | |
fa48984e GL |
251 | ret = ici->ops->get_formats(icd, i, |
252 | &icd->user_formats[fmts]); | |
253 | if (ret < 0) | |
254 | goto egfmt; | |
255 | fmts += ret; | |
c2786ad2 GL |
256 | } |
257 | ||
258 | icd->current_fmt = icd->user_formats[0].host_fmt; | |
259 | ||
260 | return 0; | |
fa48984e GL |
261 | |
262 | egfmt: | |
263 | icd->num_user_formats = 0; | |
264 | vfree(icd->user_formats); | |
265 | return ret; | |
c2786ad2 GL |
266 | } |
267 | ||
40e2e092 | 268 | /* Always entered with .video_lock held */ |
c2786ad2 GL |
269 | static void soc_camera_free_user_formats(struct soc_camera_device *icd) |
270 | { | |
fa48984e GL |
271 | 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); | |
40e2e092 | 275 | icd->current_fmt = NULL; |
fa48984e | 276 | icd->num_user_formats = 0; |
c2786ad2 | 277 | vfree(icd->user_formats); |
40e2e092 | 278 | icd->user_formats = NULL; |
c2786ad2 GL |
279 | } |
280 | ||
6a6c8786 GL |
281 | #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \ |
282 | ((x) >> 24) & 0xff | |
283 | ||
df2ed070 GL |
284 | /* Called with .vb_lock held */ |
285 | static 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 | ||
6a6c8786 GL |
293 | dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n", |
294 | pixfmtstr(pix->pixelformat), pix->width, pix->height); | |
295 | ||
df2ed070 GL |
296 | /* 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) { | |
2aa58db4 | 306 | dev_err(&icd->dev, |
df2ed070 GL |
307 | "Host driver hasn't set up current format correctly!\n"); |
308 | return -EINVAL; | |
309 | } | |
310 | ||
6a6c8786 GL |
311 | icd->user_width = pix->width; |
312 | icd->user_height = pix->height; | |
313 | icf->vb_vidq.field = | |
314 | icd->field = pix->field; | |
025c18a1 | 315 | |
df2ed070 GL |
316 | 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", | |
6a6c8786 | 321 | icd->user_width, icd->user_height); |
df2ed070 GL |
322 | |
323 | /* set physical bus parameters */ | |
324 | return ici->ops->set_bus_param(icd, pix->pixelformat); | |
325 | } | |
326 | ||
bec43661 | 327 | static int soc_camera_open(struct file *file) |
e55222ef | 328 | { |
979ea1dd | 329 | struct video_device *vdev = video_devdata(file); |
96c75399 GL |
330 | struct soc_camera_device *icd = container_of(vdev->parent, |
331 | struct soc_camera_device, | |
332 | dev); | |
979ea1dd | 333 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
9dc4e48f | 334 | struct soc_camera_host *ici; |
e55222ef GL |
335 | struct soc_camera_file *icf; |
336 | int ret; | |
337 | ||
40e2e092 GL |
338 | if (!icd->ops) |
339 | /* No device driver attached */ | |
340 | return -ENODEV; | |
341 | ||
9dc4e48f | 342 | ici = to_soc_camera_host(icd->dev.parent); |
e55222ef | 343 | |
40e2e092 GL |
344 | icf = vmalloc(sizeof(*icf)); |
345 | if (!icf) | |
346 | return -ENOMEM; | |
347 | ||
b8d9904c | 348 | if (!try_module_get(ici->ops->owner)) { |
e55222ef GL |
349 | dev_err(&icd->dev, "Couldn't lock capture bus driver.\n"); |
350 | ret = -EINVAL; | |
351 | goto emgi; | |
352 | } | |
353 | ||
96c75399 GL |
354 | /* |
355 | * Protect against icd->ops->remove() until we module_get() both | |
356 | * drivers. | |
357 | */ | |
1c3bb743 GL |
358 | mutex_lock(&icd->video_lock); |
359 | ||
9dc4e48f | 360 | icf->icd = icd; |
1a0063a9 GL |
361 | icd->use_count++; |
362 | ||
9dc4e48f GL |
363 | /* Now we really have to activate the camera */ |
364 | if (icd->use_count == 1) { | |
025c18a1 | 365 | /* Restore parameters before the last close() per V4L2 API */ |
df2ed070 GL |
366 | struct v4l2_format f = { |
367 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
368 | .fmt.pix = { | |
6a6c8786 GL |
369 | .width = icd->user_width, |
370 | .height = icd->user_height, | |
025c18a1 | 371 | .field = icd->field, |
fa48984e GL |
372 | .pixelformat = icd->current_fmt->fourcc, |
373 | .colorspace = icd->current_fmt->colorspace, | |
df2ed070 GL |
374 | }, |
375 | }; | |
376 | ||
979ea1dd GL |
377 | 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 | ||
b8d9904c | 387 | ret = ici->ops->add(icd); |
9dc4e48f GL |
388 | if (ret < 0) { |
389 | dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret); | |
9dc4e48f GL |
390 | goto eiciadd; |
391 | } | |
df2ed070 | 392 | |
df2ed070 GL |
393 | /* Try to configure with default parameters */ |
394 | ret = soc_camera_set_fmt(icf, &f); | |
395 | if (ret < 0) | |
396 | goto esfmt; | |
9dc4e48f GL |
397 | } |
398 | ||
e55222ef GL |
399 | file->private_data = icf; |
400 | dev_dbg(&icd->dev, "camera device open\n"); | |
401 | ||
a034d1b7 | 402 | ici->ops->init_videobuf(&icf->vb_vidq, icd); |
e55222ef | 403 | |
979ea1dd GL |
404 | mutex_unlock(&icd->video_lock); |
405 | ||
e55222ef GL |
406 | return 0; |
407 | ||
df2ed070 | 408 | /* |
979ea1dd | 409 | * First five errors are entered with the .video_lock held |
df2ed070 GL |
410 | * and use_count == 1 |
411 | */ | |
412 | esfmt: | |
413 | ici->ops->remove(icd); | |
9dc4e48f | 414 | eiciadd: |
979ea1dd GL |
415 | if (icl->power) |
416 | icl->power(icd->pdev, 0); | |
417 | epower: | |
c2786ad2 | 418 | icd->use_count--; |
1c3bb743 | 419 | mutex_unlock(&icd->video_lock); |
b8d9904c | 420 | module_put(ici->ops->owner); |
e55222ef | 421 | emgi: |
e55222ef GL |
422 | vfree(icf); |
423 | return ret; | |
424 | } | |
425 | ||
bec43661 | 426 | static int soc_camera_close(struct file *file) |
e55222ef GL |
427 | { |
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); | |
e55222ef | 431 | |
1c3bb743 | 432 | mutex_lock(&icd->video_lock); |
9dc4e48f | 433 | icd->use_count--; |
40e2e092 | 434 | if (!icd->use_count) { |
979ea1dd GL |
435 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
436 | ||
b8d9904c | 437 | ici->ops->remove(icd); |
979ea1dd GL |
438 | if (icl->power) |
439 | icl->power(icd->pdev, 0); | |
40e2e092 | 440 | } |
025c18a1 | 441 | |
1c3bb743 GL |
442 | mutex_unlock(&icd->video_lock); |
443 | ||
b8d9904c | 444 | module_put(ici->ops->owner); |
9dc4e48f | 445 | |
1a0063a9 | 446 | vfree(icf); |
e55222ef | 447 | |
2aa58db4 | 448 | dev_dbg(&icd->dev, "camera device close\n"); |
e55222ef GL |
449 | |
450 | return 0; | |
451 | } | |
452 | ||
aba360d8 | 453 | static ssize_t soc_camera_read(struct file *file, char __user *buf, |
abe4c471 | 454 | size_t count, loff_t *ppos) |
e55222ef GL |
455 | { |
456 | struct soc_camera_file *icf = file->private_data; | |
457 | struct soc_camera_device *icd = icf->icd; | |
e55222ef GL |
458 | int err = -EINVAL; |
459 | ||
2aa58db4 | 460 | dev_err(&icd->dev, "camera device read not implemented\n"); |
e55222ef GL |
461 | |
462 | return err; | |
463 | } | |
464 | ||
465 | static 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 | ||
483 | static 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; | |
64f5905e | 487 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef GL |
488 | |
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 | ||
b8d9904c | 494 | return ici->ops->poll(file, pt); |
e55222ef GL |
495 | } |
496 | ||
bec43661 | 497 | static struct v4l2_file_operations soc_camera_fops = { |
e55222ef GL |
498 | .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, | |
e55222ef GL |
505 | }; |
506 | ||
72937890 | 507 | static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv, |
abe4c471 | 508 | struct v4l2_format *f) |
e55222ef GL |
509 | { |
510 | struct soc_camera_file *icf = file->private_data; | |
511 | struct soc_camera_device *icd = icf->icd; | |
e55222ef | 512 | int ret; |
e55222ef GL |
513 | |
514 | WARN_ON(priv != file->private_data); | |
515 | ||
1c3bb743 GL |
516 | mutex_lock(&icf->vb_vidq.vb_lock); |
517 | ||
b897a91a GL |
518 | if (icf->vb_vidq.bufs[0]) { |
519 | dev_err(&icd->dev, "S_FMT denied: queue initialised\n"); | |
1c3bb743 GL |
520 | ret = -EBUSY; |
521 | goto unlock; | |
522 | } | |
523 | ||
df2ed070 | 524 | ret = soc_camera_set_fmt(icf, f); |
1c3bb743 GL |
525 | |
526 | unlock: | |
527 | mutex_unlock(&icf->vb_vidq.vb_lock); | |
528 | ||
529 | return ret; | |
e55222ef GL |
530 | } |
531 | ||
72937890 | 532 | static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv, |
abe4c471 | 533 | struct v4l2_fmtdesc *f) |
e55222ef GL |
534 | { |
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 | ||
c2786ad2 | 541 | if (f->index >= icd->num_user_formats) |
e55222ef GL |
542 | return -EINVAL; |
543 | ||
c2786ad2 | 544 | format = icd->user_formats[f->index].host_fmt; |
e55222ef GL |
545 | |
546 | strlcpy(f->description, format->name, sizeof(f->description)); | |
547 | f->pixelformat = format->fourcc; | |
548 | return 0; | |
549 | } | |
550 | ||
72937890 | 551 | static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv, |
abe4c471 | 552 | struct v4l2_format *f) |
e55222ef GL |
553 | { |
554 | struct soc_camera_file *icf = file->private_data; | |
555 | struct soc_camera_device *icd = icf->icd; | |
64f5905e | 556 | struct v4l2_pix_format *pix = &f->fmt.pix; |
e55222ef GL |
557 | |
558 | WARN_ON(priv != file->private_data); | |
559 | ||
6a6c8786 GL |
560 | pix->width = icd->user_width; |
561 | pix->height = icd->user_height; | |
64f5905e GL |
562 | pix->field = icf->vb_vidq.field; |
563 | pix->pixelformat = icd->current_fmt->fourcc; | |
564 | pix->bytesperline = pix->width * | |
25c4d74e | 565 | DIV_ROUND_UP(icd->current_fmt->depth, 8); |
64f5905e | 566 | pix->sizeimage = pix->height * pix->bytesperline; |
e55222ef GL |
567 | dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n", |
568 | icd->current_fmt->fourcc); | |
569 | return 0; | |
570 | } | |
571 | ||
572 | static 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; | |
64f5905e | 577 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef GL |
578 | |
579 | WARN_ON(priv != file->private_data); | |
580 | ||
581 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); | |
b8d9904c | 582 | return ici->ops->querycap(ici, cap); |
e55222ef GL |
583 | } |
584 | ||
585 | static 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; | |
c9c1f1c0 | 590 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
1c3bb743 | 591 | int ret; |
e55222ef GL |
592 | |
593 | WARN_ON(priv != file->private_data); | |
594 | ||
e55222ef GL |
595 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
596 | return -EINVAL; | |
597 | ||
1c3bb743 GL |
598 | mutex_lock(&icd->video_lock); |
599 | ||
c9c1f1c0 | 600 | v4l2_subdev_call(sd, video, s_stream, 1); |
e55222ef GL |
601 | |
602 | /* This calls buf_queue from host driver's videobuf_queue_ops */ | |
1c3bb743 GL |
603 | ret = videobuf_streamon(&icf->vb_vidq); |
604 | ||
605 | mutex_unlock(&icd->video_lock); | |
606 | ||
607 | return ret; | |
e55222ef GL |
608 | } |
609 | ||
610 | static 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; | |
c9c1f1c0 | 615 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
e55222ef GL |
616 | |
617 | WARN_ON(priv != file->private_data); | |
618 | ||
e55222ef GL |
619 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
620 | return -EINVAL; | |
621 | ||
1c3bb743 GL |
622 | mutex_lock(&icd->video_lock); |
623 | ||
e55222ef GL |
624 | /* 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 | ||
c9c1f1c0 | 628 | v4l2_subdev_call(sd, video, s_stream, 0); |
e55222ef | 629 | |
1c3bb743 GL |
630 | mutex_unlock(&icd->video_lock); |
631 | ||
e55222ef GL |
632 | return 0; |
633 | } | |
634 | ||
635 | static 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; | |
2840d249 | 640 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef GL |
641 | int i; |
642 | ||
643 | WARN_ON(priv != file->private_data); | |
644 | ||
645 | if (!qc->id) | |
646 | return -EINVAL; | |
647 | ||
2840d249 GL |
648 | /* 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 */ | |
e55222ef GL |
657 | 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 | ||
667 | static 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; | |
979ea1dd | 672 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
c9c1f1c0 | 673 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
2840d249 | 674 | int ret; |
e55222ef GL |
675 | |
676 | WARN_ON(priv != file->private_data); | |
677 | ||
2840d249 GL |
678 | if (ici->ops->get_ctrl) { |
679 | ret = ici->ops->get_ctrl(icd, ctrl); | |
680 | if (ret != -ENOIOCTLCMD) | |
681 | return ret; | |
682 | } | |
683 | ||
c9c1f1c0 | 684 | return v4l2_subdev_call(sd, core, g_ctrl, ctrl); |
e55222ef GL |
685 | } |
686 | ||
687 | static 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; | |
979ea1dd | 692 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
c9c1f1c0 | 693 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
2840d249 | 694 | int ret; |
e55222ef GL |
695 | |
696 | WARN_ON(priv != file->private_data); | |
697 | ||
2840d249 GL |
698 | if (ici->ops->set_ctrl) { |
699 | ret = ici->ops->set_ctrl(icd, ctrl); | |
700 | if (ret != -ENOIOCTLCMD) | |
701 | return ret; | |
702 | } | |
703 | ||
c9c1f1c0 | 704 | return v4l2_subdev_call(sd, core, s_ctrl, ctrl); |
e55222ef GL |
705 | } |
706 | ||
707 | static 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; | |
6a6c8786 | 712 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
e55222ef | 713 | |
6a6c8786 | 714 | return ici->ops->cropcap(icd, a); |
e55222ef GL |
715 | } |
716 | ||
717 | static 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; | |
6a6c8786 GL |
722 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
723 | int ret; | |
e55222ef | 724 | |
6a6c8786 GL |
725 | mutex_lock(&icf->vb_vidq.vb_lock); |
726 | ret = ici->ops->get_crop(icd, a); | |
727 | mutex_unlock(&icf->vb_vidq.vb_lock); | |
e55222ef | 728 | |
6a6c8786 | 729 | return ret; |
e55222ef GL |
730 | } |
731 | ||
68a54f0e GL |
732 | /* |
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 | */ | |
e55222ef GL |
738 | static 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; | |
64f5905e | 743 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
6a6c8786 GL |
744 | struct v4l2_rect *rect = &a->c; |
745 | struct v4l2_crop current_crop; | |
e55222ef GL |
746 | int ret; |
747 | ||
748 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
749 | return -EINVAL; | |
750 | ||
6a6c8786 GL |
751 | dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n", |
752 | rect->width, rect->height, rect->left, rect->top); | |
753 | ||
1c3bb743 GL |
754 | /* Cropping is allowed during a running capture, guard consistency */ |
755 | mutex_lock(&icf->vb_vidq.vb_lock); | |
756 | ||
6a6c8786 GL |
757 | /* If get_crop fails, we'll let host and / or client drivers decide */ |
758 | ret = ici->ops->get_crop(icd, ¤t_crop); | |
759 | ||
b897a91a | 760 | /* Prohibit window size change with initialised buffers */ |
6a6c8786 GL |
761 | if (icf->vb_vidq.bufs[0] && !ret && |
762 | (a->c.width != current_crop.c.width || | |
763 | a->c.height != current_crop.c.height)) { | |
b897a91a GL |
764 | dev_err(&icd->dev, |
765 | "S_CROP denied: queue initialised and sizes differ\n"); | |
766 | ret = -EBUSY; | |
6a6c8786 GL |
767 | } else { |
768 | ret = ici->ops->set_crop(icd, a); | |
b897a91a GL |
769 | } |
770 | ||
1c3bb743 GL |
771 | mutex_unlock(&icf->vb_vidq.vb_lock); |
772 | ||
e55222ef GL |
773 | return ret; |
774 | } | |
775 | ||
776 | static int soc_camera_g_chip_ident(struct file *file, void *fh, | |
aecde8b5 | 777 | struct v4l2_dbg_chip_ident *id) |
e55222ef GL |
778 | { |
779 | struct soc_camera_file *icf = file->private_data; | |
780 | struct soc_camera_device *icd = icf->icd; | |
c9c1f1c0 | 781 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
e55222ef | 782 | |
c9c1f1c0 | 783 | return v4l2_subdev_call(sd, core, g_chip_ident, id); |
e55222ef GL |
784 | } |
785 | ||
786 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
787 | static int soc_camera_g_register(struct file *file, void *fh, | |
aecde8b5 | 788 | struct v4l2_dbg_register *reg) |
e55222ef GL |
789 | { |
790 | struct soc_camera_file *icf = file->private_data; | |
791 | struct soc_camera_device *icd = icf->icd; | |
c9c1f1c0 | 792 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
e55222ef | 793 | |
c9c1f1c0 | 794 | return v4l2_subdev_call(sd, core, g_register, reg); |
e55222ef GL |
795 | } |
796 | ||
797 | static int soc_camera_s_register(struct file *file, void *fh, | |
aecde8b5 | 798 | struct v4l2_dbg_register *reg) |
e55222ef GL |
799 | { |
800 | struct soc_camera_file *icf = file->private_data; | |
801 | struct soc_camera_device *icd = icf->icd; | |
c9c1f1c0 | 802 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
e55222ef | 803 | |
c9c1f1c0 | 804 | return v4l2_subdev_call(sd, core, s_register, reg); |
e55222ef GL |
805 | } |
806 | #endif | |
807 | ||
e55222ef GL |
808 | /* So far this function cannot fail */ |
809 | static 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) { | |
40e2e092 | 817 | int ret; |
979ea1dd | 818 | icd->dev.parent = ici->v4l2_dev.dev; |
40e2e092 GL |
819 | 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 | } | |
e55222ef GL |
827 | } |
828 | } | |
829 | ||
830 | mutex_unlock(&list_lock); | |
831 | } | |
832 | ||
40e2e092 GL |
833 | #ifdef CONFIG_I2C_BOARDINFO |
834 | static int soc_camera_init_i2c(struct soc_camera_device *icd, | |
835 | struct soc_camera_link *icl) | |
e55222ef | 836 | { |
40e2e092 | 837 | struct i2c_client *client; |
979ea1dd | 838 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
40e2e092 | 839 | struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id); |
979ea1dd | 840 | struct v4l2_subdev *subdev; |
40e2e092 | 841 | int ret; |
e55222ef | 842 | |
40e2e092 GL |
843 | 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; | |
848 | } | |
e55222ef | 849 | |
40e2e092 | 850 | icl->board_info->platform_data = icd; |
e55222ef | 851 | |
979ea1dd GL |
852 | subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, |
853 | icl->module_name, icl->board_info, NULL); | |
854 | if (!subdev) { | |
40e2e092 GL |
855 | ret = -ENOMEM; |
856 | goto ei2cnd; | |
e55222ef GL |
857 | } |
858 | ||
979ea1dd GL |
859 | client = subdev->priv; |
860 | ||
40e2e092 GL |
861 | /* Use to_i2c_client(dev) to recover the i2c client */ |
862 | dev_set_drvdata(&icd->dev, &client->dev); | |
e55222ef | 863 | |
40e2e092 GL |
864 | return 0; |
865 | ei2cnd: | |
866 | i2c_put_adapter(adap); | |
867 | ei2cga: | |
e55222ef GL |
868 | return ret; |
869 | } | |
870 | ||
40e2e092 GL |
871 | static 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); | |
979ea1dd | 876 | v4l2_device_unregister_subdev(i2c_get_clientdata(client)); |
40e2e092 GL |
877 | 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 | ||
979ea1dd | 885 | static int soc_camera_video_start(struct soc_camera_device *icd); |
40e2e092 GL |
886 | static int video_dev_create(struct soc_camera_device *icd); |
887 | /* Called during host-driver probe */ | |
e55222ef GL |
888 | static int soc_camera_probe(struct device *dev) |
889 | { | |
890 | struct soc_camera_device *icd = to_soc_camera_dev(dev); | |
979ea1dd | 891 | struct soc_camera_host *ici = to_soc_camera_host(dev->parent); |
40e2e092 | 892 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
979ea1dd | 893 | struct device *control = NULL; |
6a6c8786 GL |
894 | struct v4l2_subdev *sd; |
895 | struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE}; | |
e55222ef GL |
896 | int ret; |
897 | ||
40e2e092 | 898 | dev_info(dev, "Probing %s\n", dev_name(dev)); |
1c3bb743 | 899 | |
979ea1dd GL |
900 | 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 */ | |
40e2e092 GL |
918 | ret = video_dev_create(icd); |
919 | if (ret < 0) | |
920 | goto evdc; | |
1c3bb743 | 921 | |
40e2e092 GL |
922 | /* 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) { | |
1c3bb743 | 928 | ret = -EINVAL; |
40e2e092 GL |
929 | goto eadddev; |
930 | } else { | |
979ea1dd GL |
931 | if (icl->module_name) |
932 | ret = request_module(icl->module_name); | |
933 | ||
40e2e092 GL |
934 | ret = icl->add_device(icl, &icd->dev); |
935 | if (ret < 0) | |
936 | goto eadddev; | |
1c3bb743 | 937 | |
96c75399 GL |
938 | /* |
939 | * FIXME: this is racy, have to use driver-binding notification, | |
940 | * when it is available | |
941 | */ | |
979ea1dd | 942 | control = to_soc_camera_control(icd); |
08590b96 | 943 | if (!control || !control->driver || !dev_get_drvdata(control) || |
979ea1dd GL |
944 | !try_module_get(control->driver->owner)) { |
945 | icl->del_device(icl); | |
946 | goto enodrv; | |
947 | } | |
40e2e092 | 948 | } |
e55222ef | 949 | |
fa48984e GL |
950 | /* 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 | ||
fa48984e GL |
955 | icd->field = V4L2_FIELD_ANY; |
956 | ||
979ea1dd GL |
957 | /* ..._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; | |
963 | ||
6a6c8786 GL |
964 | /* 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 | ||
40e2e092 | 971 | /* Do we have to sysfs_remove_link() before device_unregister()? */ |
6a6c8786 | 972 | if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj, |
40e2e092 GL |
973 | "control")) |
974 | dev_warn(&icd->dev, "Failed creating the control symlink\n"); | |
025c18a1 | 975 | |
979ea1dd GL |
976 | ici->ops->remove(icd); |
977 | ||
978 | if (icl->power) | |
979 | icl->power(icd->pdev, 0); | |
980 | ||
981 | mutex_unlock(&icd->video_lock); | |
025c18a1 | 982 | |
40e2e092 | 983 | return 0; |
e55222ef | 984 | |
979ea1dd GL |
985 | evidstart: |
986 | mutex_unlock(&icd->video_lock); | |
fa48984e GL |
987 | soc_camera_free_user_formats(icd); |
988 | eiufmt: | |
979ea1dd | 989 | if (icl->board_info) { |
40e2e092 | 990 | soc_camera_free_i2c(icd); |
979ea1dd | 991 | } else { |
40e2e092 | 992 | icl->del_device(icl); |
979ea1dd GL |
993 | module_put(control->driver->owner); |
994 | } | |
995 | enodrv: | |
40e2e092 GL |
996 | eadddev: |
997 | video_device_release(icd->vdev); | |
998 | evdc: | |
979ea1dd GL |
999 | ici->ops->remove(icd); |
1000 | eadd: | |
1001 | if (icl->power) | |
1002 | icl->power(icd->pdev, 0); | |
1003 | epower: | |
e55222ef GL |
1004 | 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 */ | |
1009 | static int soc_camera_remove(struct device *dev) | |
1010 | { | |
1011 | struct soc_camera_device *icd = to_soc_camera_dev(dev); | |
40e2e092 GL |
1012 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
1013 | struct video_device *vdev = icd->vdev; | |
e55222ef | 1014 | |
40e2e092 | 1015 | BUG_ON(!dev->parent); |
e55222ef | 1016 | |
40e2e092 GL |
1017 | 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 | ||
979ea1dd | 1024 | if (icl->board_info) { |
40e2e092 | 1025 | soc_camera_free_i2c(icd); |
979ea1dd GL |
1026 | } 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 | } | |
fa48984e | 1034 | soc_camera_free_user_formats(icd); |
025c18a1 | 1035 | |
e55222ef GL |
1036 | return 0; |
1037 | } | |
1038 | ||
2e521061 RJ |
1039 | static 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 | ||
1051 | static 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 | ||
e55222ef GL |
1063 | static struct bus_type soc_camera_bus_type = { |
1064 | .name = "soc-camera", | |
1065 | .probe = soc_camera_probe, | |
1066 | .remove = soc_camera_remove, | |
2e521061 RJ |
1067 | .suspend = soc_camera_suspend, |
1068 | .resume = soc_camera_resume, | |
e55222ef GL |
1069 | }; |
1070 | ||
1071 | static struct device_driver ic_drv = { | |
1072 | .name = "camera", | |
1073 | .bus = &soc_camera_bus_type, | |
1074 | .owner = THIS_MODULE, | |
1075 | }; | |
1076 | ||
e55222ef GL |
1077 | static void dummy_release(struct device *dev) |
1078 | { | |
1079 | } | |
1080 | ||
6a6c8786 GL |
1081 | static 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 | ||
1088 | static 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 | ||
1094 | static 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 | ||
b8d9904c | 1100 | int soc_camera_host_register(struct soc_camera_host *ici) |
e55222ef | 1101 | { |
e55222ef | 1102 | struct soc_camera_host *ix; |
979ea1dd | 1103 | int ret; |
e55222ef | 1104 | |
64f5905e GL |
1105 | if (!ici || !ici->ops || |
1106 | !ici->ops->try_fmt || | |
1107 | !ici->ops->set_fmt || | |
1108 | !ici->ops->set_bus_param || | |
1109 | !ici->ops->querycap || | |
1110 | !ici->ops->init_videobuf || | |
1111 | !ici->ops->reqbufs || | |
1112 | !ici->ops->add || | |
1113 | !ici->ops->remove || | |
eff505fa | 1114 | !ici->ops->poll || |
979ea1dd | 1115 | !ici->v4l2_dev.dev) |
e55222ef GL |
1116 | return -EINVAL; |
1117 | ||
6a6c8786 GL |
1118 | if (!ici->ops->set_crop) |
1119 | ici->ops->set_crop = default_s_crop; | |
1120 | if (!ici->ops->get_crop) | |
1121 | ici->ops->get_crop = default_g_crop; | |
1122 | if (!ici->ops->cropcap) | |
1123 | ici->ops->cropcap = default_cropcap; | |
1124 | ||
e55222ef GL |
1125 | mutex_lock(&list_lock); |
1126 | list_for_each_entry(ix, &hosts, list) { | |
1127 | if (ix->nr == ici->nr) { | |
979ea1dd GL |
1128 | ret = -EBUSY; |
1129 | goto edevreg; | |
e55222ef GL |
1130 | } |
1131 | } | |
1132 | ||
979ea1dd GL |
1133 | ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); |
1134 | if (ret < 0) | |
1135 | goto edevreg; | |
eff505fa | 1136 | |
e55222ef GL |
1137 | list_add_tail(&ici->list, &hosts); |
1138 | mutex_unlock(&list_lock); | |
1139 | ||
e55222ef GL |
1140 | scan_add_host(ici); |
1141 | ||
1142 | return 0; | |
979ea1dd GL |
1143 | |
1144 | edevreg: | |
1145 | mutex_unlock(&list_lock); | |
1146 | return ret; | |
e55222ef GL |
1147 | } |
1148 | EXPORT_SYMBOL(soc_camera_host_register); | |
1149 | ||
1150 | /* Unregister all clients! */ | |
1151 | void soc_camera_host_unregister(struct soc_camera_host *ici) | |
1152 | { | |
1153 | struct soc_camera_device *icd; | |
1154 | ||
1155 | mutex_lock(&list_lock); | |
1156 | ||
1157 | list_del(&ici->list); | |
1158 | ||
1159 | list_for_each_entry(icd, &devices, list) { | |
979ea1dd | 1160 | if (icd->iface == ici->nr) { |
40e2e092 | 1161 | /* The bus->remove will be called */ |
e55222ef | 1162 | device_unregister(&icd->dev); |
76823b79 GL |
1163 | /* |
1164 | * Not before device_unregister(), .remove | |
1165 | * needs parent to call ici->ops->remove(). | |
1166 | * If the host module is loaded again, device_register() | |
1167 | * would complain "already initialised," since 2.6.32 | |
1168 | * this is also needed to prevent use-after-free of the | |
1169 | * device private data. | |
1170 | */ | |
1171 | memset(&icd->dev, 0, sizeof(icd->dev)); | |
e55222ef GL |
1172 | } |
1173 | } | |
1174 | ||
1175 | mutex_unlock(&list_lock); | |
1176 | ||
979ea1dd | 1177 | v4l2_device_unregister(&ici->v4l2_dev); |
e55222ef GL |
1178 | } |
1179 | EXPORT_SYMBOL(soc_camera_host_unregister); | |
1180 | ||
1181 | /* Image capture device */ | |
40e2e092 | 1182 | static int soc_camera_device_register(struct soc_camera_device *icd) |
e55222ef GL |
1183 | { |
1184 | struct soc_camera_device *ix; | |
1185 | int num = -1, i; | |
1186 | ||
e55222ef GL |
1187 | for (i = 0; i < 256 && num < 0; i++) { |
1188 | num = i; | |
40e2e092 | 1189 | /* Check if this index is available on this interface */ |
e55222ef GL |
1190 | list_for_each_entry(ix, &devices, list) { |
1191 | if (ix->iface == icd->iface && ix->devnum == i) { | |
1192 | num = -1; | |
1193 | break; | |
1194 | } | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | if (num < 0) | |
1199 | /* ok, we have 256 cameras on this host... | |
1200 | * man, stay reasonable... */ | |
1201 | return -ENOMEM; | |
1202 | ||
1203 | icd->devnum = num; | |
1204 | icd->dev.bus = &soc_camera_bus_type; | |
e55222ef | 1205 | |
64f5905e GL |
1206 | icd->dev.release = dummy_release; |
1207 | icd->use_count = 0; | |
1208 | icd->host_priv = NULL; | |
1c3bb743 | 1209 | mutex_init(&icd->video_lock); |
e55222ef | 1210 | |
40e2e092 GL |
1211 | list_add_tail(&icd->list, &devices); |
1212 | ||
1213 | return 0; | |
e55222ef | 1214 | } |
e55222ef | 1215 | |
40e2e092 | 1216 | static void soc_camera_device_unregister(struct soc_camera_device *icd) |
e55222ef | 1217 | { |
e55222ef | 1218 | list_del(&icd->list); |
e55222ef | 1219 | } |
e55222ef | 1220 | |
a399810c HV |
1221 | static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { |
1222 | .vidioc_querycap = soc_camera_querycap, | |
1223 | .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, | |
1224 | .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, | |
1225 | .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, | |
1226 | .vidioc_enum_input = soc_camera_enum_input, | |
1227 | .vidioc_g_input = soc_camera_g_input, | |
1228 | .vidioc_s_input = soc_camera_s_input, | |
1229 | .vidioc_s_std = soc_camera_s_std, | |
1230 | .vidioc_reqbufs = soc_camera_reqbufs, | |
1231 | .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, | |
1232 | .vidioc_querybuf = soc_camera_querybuf, | |
1233 | .vidioc_qbuf = soc_camera_qbuf, | |
1234 | .vidioc_dqbuf = soc_camera_dqbuf, | |
1235 | .vidioc_streamon = soc_camera_streamon, | |
1236 | .vidioc_streamoff = soc_camera_streamoff, | |
1237 | .vidioc_queryctrl = soc_camera_queryctrl, | |
1238 | .vidioc_g_ctrl = soc_camera_g_ctrl, | |
1239 | .vidioc_s_ctrl = soc_camera_s_ctrl, | |
1240 | .vidioc_cropcap = soc_camera_cropcap, | |
1241 | .vidioc_g_crop = soc_camera_g_crop, | |
1242 | .vidioc_s_crop = soc_camera_s_crop, | |
1243 | .vidioc_g_chip_ident = soc_camera_g_chip_ident, | |
1244 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
1245 | .vidioc_g_register = soc_camera_g_register, | |
1246 | .vidioc_s_register = soc_camera_s_register, | |
1247 | #endif | |
1248 | }; | |
1249 | ||
40e2e092 | 1250 | static int video_dev_create(struct soc_camera_device *icd) |
e55222ef GL |
1251 | { |
1252 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | |
40e2e092 | 1253 | struct video_device *vdev = video_device_alloc(); |
e55222ef | 1254 | |
e55222ef | 1255 | if (!vdev) |
40e2e092 | 1256 | return -ENOMEM; |
e55222ef GL |
1257 | |
1258 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); | |
1c3bb743 | 1259 | |
5e85e732 | 1260 | vdev->parent = &icd->dev; |
e55222ef GL |
1261 | vdev->current_norm = V4L2_STD_UNKNOWN; |
1262 | vdev->fops = &soc_camera_fops; | |
a399810c | 1263 | vdev->ioctl_ops = &soc_camera_ioctl_ops; |
e55222ef GL |
1264 | vdev->release = video_device_release; |
1265 | vdev->minor = -1; | |
40e2e092 | 1266 | vdev->tvnorms = V4L2_STD_UNKNOWN; |
e55222ef | 1267 | |
e55222ef GL |
1268 | icd->vdev = vdev; |
1269 | ||
1270 | return 0; | |
40e2e092 | 1271 | } |
e55222ef | 1272 | |
40e2e092 | 1273 | /* |
979ea1dd | 1274 | * Called from soc_camera_probe() above (with .video_lock held???) |
40e2e092 | 1275 | */ |
979ea1dd | 1276 | static int soc_camera_video_start(struct soc_camera_device *icd) |
40e2e092 | 1277 | { |
979ea1dd | 1278 | int ret; |
40e2e092 GL |
1279 | |
1280 | if (!icd->dev.parent) | |
1281 | return -ENODEV; | |
1282 | ||
1283 | if (!icd->ops || | |
40e2e092 GL |
1284 | !icd->ops->query_bus_param || |
1285 | !icd->ops->set_bus_param) | |
1286 | return -EINVAL; | |
1287 | ||
979ea1dd GL |
1288 | ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, |
1289 | icd->vdev->minor); | |
1290 | if (ret < 0) { | |
1291 | dev_err(&icd->dev, "video_register_device failed: %d\n", ret); | |
1292 | return ret; | |
1293 | } | |
40e2e092 | 1294 | |
979ea1dd | 1295 | return 0; |
e55222ef | 1296 | } |
e55222ef | 1297 | |
40e2e092 | 1298 | static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev) |
0fd327bd | 1299 | { |
40e2e092 GL |
1300 | struct soc_camera_link *icl = pdev->dev.platform_data; |
1301 | struct soc_camera_device *icd; | |
c41debaf | 1302 | int ret; |
0fd327bd | 1303 | |
40e2e092 GL |
1304 | if (!icl) |
1305 | return -EINVAL; | |
0fd327bd | 1306 | |
40e2e092 GL |
1307 | icd = kzalloc(sizeof(*icd), GFP_KERNEL); |
1308 | if (!icd) | |
1309 | return -ENOMEM; | |
0fd327bd | 1310 | |
40e2e092 | 1311 | icd->iface = icl->bus_id; |
979ea1dd | 1312 | icd->pdev = &pdev->dev; |
40e2e092 GL |
1313 | platform_set_drvdata(pdev, icd); |
1314 | icd->dev.platform_data = icl; | |
0fd327bd | 1315 | |
40e2e092 GL |
1316 | ret = soc_camera_device_register(icd); |
1317 | if (ret < 0) | |
1318 | goto escdevreg; | |
0fd327bd | 1319 | |
6a6c8786 GL |
1320 | icd->user_width = DEFAULT_WIDTH; |
1321 | icd->user_height = DEFAULT_HEIGHT; | |
1322 | ||
40e2e092 | 1323 | return 0; |
0fd327bd | 1324 | |
40e2e092 GL |
1325 | escdevreg: |
1326 | kfree(icd); | |
0fd327bd | 1327 | |
40e2e092 | 1328 | return ret; |
c41debaf | 1329 | } |
0fd327bd | 1330 | |
40e2e092 GL |
1331 | /* Only called on rmmod for each platform device, since they are not |
1332 | * hot-pluggable. Now we know, that all our users - hosts and devices have | |
1333 | * been unloaded already */ | |
1334 | static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev) | |
c41debaf | 1335 | { |
40e2e092 | 1336 | struct soc_camera_device *icd = platform_get_drvdata(pdev); |
c41debaf | 1337 | |
40e2e092 | 1338 | if (!icd) |
c41debaf GL |
1339 | return -EINVAL; |
1340 | ||
40e2e092 | 1341 | soc_camera_device_unregister(icd); |
c41debaf | 1342 | |
40e2e092 | 1343 | kfree(icd); |
c41debaf | 1344 | |
0fd327bd GL |
1345 | return 0; |
1346 | } | |
1347 | ||
1348 | static struct platform_driver __refdata soc_camera_pdrv = { | |
40e2e092 GL |
1349 | .remove = __devexit_p(soc_camera_pdrv_remove), |
1350 | .driver = { | |
1351 | .name = "soc-camera-pdrv", | |
1352 | .owner = THIS_MODULE, | |
0fd327bd GL |
1353 | }, |
1354 | }; | |
1355 | ||
e55222ef GL |
1356 | static int __init soc_camera_init(void) |
1357 | { | |
1358 | int ret = bus_register(&soc_camera_bus_type); | |
1359 | if (ret) | |
1360 | return ret; | |
1361 | ret = driver_register(&ic_drv); | |
1362 | if (ret) | |
1363 | goto edrvr; | |
e55222ef | 1364 | |
40e2e092 | 1365 | ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe); |
0fd327bd GL |
1366 | if (ret) |
1367 | goto epdr; | |
1368 | ||
e55222ef GL |
1369 | return 0; |
1370 | ||
0fd327bd GL |
1371 | epdr: |
1372 | driver_unregister(&ic_drv); | |
e55222ef GL |
1373 | edrvr: |
1374 | bus_unregister(&soc_camera_bus_type); | |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | static void __exit soc_camera_exit(void) | |
1379 | { | |
0fd327bd | 1380 | platform_driver_unregister(&soc_camera_pdrv); |
e55222ef GL |
1381 | driver_unregister(&ic_drv); |
1382 | bus_unregister(&soc_camera_bus_type); | |
1383 | } | |
1384 | ||
1385 | module_init(soc_camera_init); | |
1386 | module_exit(soc_camera_exit); | |
1387 | ||
1388 | MODULE_DESCRIPTION("Image capture bus driver"); | |
1389 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
1390 | MODULE_LICENSE("GPL"); | |
0fd327bd | 1391 | MODULE_ALIAS("platform:soc-camera-pdrv"); |