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