diff options
Diffstat (limited to 'drivers/media/video/soc_camera.c')
-rw-r--r-- | drivers/media/video/soc_camera.c | 1031 |
1 files changed, 1031 insertions, 0 deletions
diff --git a/drivers/media/video/soc_camera.c b/drivers/media/video/soc_camera.c new file mode 100644 index 000000000000..a1b92446c8b4 --- /dev/null +++ b/drivers/media/video/soc_camera.c | |||
@@ -0,0 +1,1031 @@ | |||
1 | /* | ||
2 | * camera image capture (abstract) bus driver | ||
3 | * | ||
4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> | ||
5 | * | ||
6 | * This driver provides an interface between platform-specific camera | ||
7 | * busses and camera devices. It should be used if the camera is | ||
8 | * connected not over a "proper" bus like PCI or USB, but over a | ||
9 | * special bus, like, for example, the Quick Capture interface on PXA270 | ||
10 | * SoCs. Later it should also be used for i.MX31 SoCs from Freescale. | ||
11 | * It can handle multiple cameras and / or multiple busses, which can | ||
12 | * be used, e.g., in stereo-vision applications. | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or modify | ||
15 | * it under the terms of the GNU General Public License version 2 as | ||
16 | * published by the Free Software Foundation. | ||
17 | */ | ||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/init.h> | ||
21 | #include <linux/device.h> | ||
22 | #include <linux/list.h> | ||
23 | #include <linux/err.h> | ||
24 | #include <linux/mutex.h> | ||
25 | #include <linux/vmalloc.h> | ||
26 | |||
27 | #include <media/v4l2-common.h> | ||
28 | #include <media/v4l2-dev.h> | ||
29 | #include <media/soc_camera.h> | ||
30 | |||
31 | static LIST_HEAD(hosts); | ||
32 | static LIST_HEAD(devices); | ||
33 | static DEFINE_MUTEX(list_lock); | ||
34 | static DEFINE_MUTEX(video_lock); | ||
35 | |||
36 | const static struct soc_camera_data_format* | ||
37 | format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc) | ||
38 | { | ||
39 | unsigned int i; | ||
40 | |||
41 | for (i = 0; i < icd->num_formats; i++) | ||
42 | if (icd->formats[i].fourcc == fourcc) | ||
43 | return icd->formats + i; | ||
44 | return NULL; | ||
45 | } | ||
46 | |||
47 | static int soc_camera_try_fmt_cap(struct file *file, void *priv, | ||
48 | struct v4l2_format *f) | ||
49 | { | ||
50 | struct soc_camera_file *icf = file->private_data; | ||
51 | struct soc_camera_device *icd = icf->icd; | ||
52 | struct soc_camera_host *ici = | ||
53 | to_soc_camera_host(icd->dev.parent); | ||
54 | enum v4l2_field field; | ||
55 | const struct soc_camera_data_format *fmt; | ||
56 | int ret; | ||
57 | |||
58 | WARN_ON(priv != file->private_data); | ||
59 | |||
60 | fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat); | ||
61 | if (!fmt) { | ||
62 | dev_dbg(&icd->dev, "invalid format 0x%08x\n", | ||
63 | f->fmt.pix.pixelformat); | ||
64 | return -EINVAL; | ||
65 | } | ||
66 | |||
67 | dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc); | ||
68 | |||
69 | field = f->fmt.pix.field; | ||
70 | |||
71 | if (field == V4L2_FIELD_ANY) { | ||
72 | field = V4L2_FIELD_NONE; | ||
73 | } else if (V4L2_FIELD_NONE != field) { | ||
74 | dev_err(&icd->dev, "Field type invalid.\n"); | ||
75 | return -EINVAL; | ||
76 | } | ||
77 | |||
78 | /* test physical bus parameters */ | ||
79 | ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat); | ||
80 | if (ret) | ||
81 | return ret; | ||
82 | |||
83 | /* limit format to hardware capabilities */ | ||
84 | ret = ici->ops->try_fmt_cap(icd, f); | ||
85 | |||
86 | /* calculate missing fields */ | ||
87 | f->fmt.pix.field = field; | ||
88 | f->fmt.pix.bytesperline = | ||
89 | (f->fmt.pix.width * fmt->depth) >> 3; | ||
90 | f->fmt.pix.sizeimage = | ||
91 | f->fmt.pix.height * f->fmt.pix.bytesperline; | ||
92 | |||
93 | return ret; | ||
94 | } | ||
95 | |||
96 | static int soc_camera_enum_input(struct file *file, void *priv, | ||
97 | struct v4l2_input *inp) | ||
98 | { | ||
99 | if (inp->index != 0) | ||
100 | return -EINVAL; | ||
101 | |||
102 | inp->type = V4L2_INPUT_TYPE_CAMERA; | ||
103 | inp->std = V4L2_STD_UNKNOWN; | ||
104 | strcpy(inp->name, "Camera"); | ||
105 | |||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i) | ||
110 | { | ||
111 | *i = 0; | ||
112 | |||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | static int soc_camera_s_input(struct file *file, void *priv, unsigned int i) | ||
117 | { | ||
118 | if (i > 0) | ||
119 | return -EINVAL; | ||
120 | |||
121 | return 0; | ||
122 | } | ||
123 | |||
124 | static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a) | ||
125 | { | ||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | static int soc_camera_reqbufs(struct file *file, void *priv, | ||
130 | struct v4l2_requestbuffers *p) | ||
131 | { | ||
132 | int ret; | ||
133 | struct soc_camera_file *icf = file->private_data; | ||
134 | struct soc_camera_device *icd = icf->icd; | ||
135 | struct soc_camera_host *ici = | ||
136 | to_soc_camera_host(icd->dev.parent); | ||
137 | |||
138 | WARN_ON(priv != file->private_data); | ||
139 | |||
140 | dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory); | ||
141 | |||
142 | ret = videobuf_reqbufs(&icf->vb_vidq, p); | ||
143 | if (ret < 0) | ||
144 | return ret; | ||
145 | |||
146 | return ici->ops->reqbufs(icf, p); | ||
147 | } | ||
148 | |||
149 | static int soc_camera_querybuf(struct file *file, void *priv, | ||
150 | struct v4l2_buffer *p) | ||
151 | { | ||
152 | struct soc_camera_file *icf = file->private_data; | ||
153 | |||
154 | WARN_ON(priv != file->private_data); | ||
155 | |||
156 | return videobuf_querybuf(&icf->vb_vidq, p); | ||
157 | } | ||
158 | |||
159 | static int soc_camera_qbuf(struct file *file, void *priv, | ||
160 | struct v4l2_buffer *p) | ||
161 | { | ||
162 | struct soc_camera_file *icf = file->private_data; | ||
163 | |||
164 | WARN_ON(priv != file->private_data); | ||
165 | |||
166 | return videobuf_qbuf(&icf->vb_vidq, p); | ||
167 | } | ||
168 | |||
169 | static int soc_camera_dqbuf(struct file *file, void *priv, | ||
170 | struct v4l2_buffer *p) | ||
171 | { | ||
172 | struct soc_camera_file *icf = file->private_data; | ||
173 | |||
174 | WARN_ON(priv != file->private_data); | ||
175 | |||
176 | return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK); | ||
177 | } | ||
178 | |||
179 | static int soc_camera_open(struct inode *inode, struct file *file) | ||
180 | { | ||
181 | struct video_device *vdev; | ||
182 | struct soc_camera_device *icd; | ||
183 | struct soc_camera_host *ici; | ||
184 | struct soc_camera_file *icf; | ||
185 | spinlock_t *lock; | ||
186 | int ret; | ||
187 | |||
188 | icf = vmalloc(sizeof(*icf)); | ||
189 | if (!icf) | ||
190 | return -ENOMEM; | ||
191 | |||
192 | /* Protect against icd->remove() until we module_get() both drivers. */ | ||
193 | mutex_lock(&video_lock); | ||
194 | |||
195 | vdev = video_devdata(file); | ||
196 | icd = container_of(vdev->dev, struct soc_camera_device, dev); | ||
197 | ici = to_soc_camera_host(icd->dev.parent); | ||
198 | |||
199 | if (!try_module_get(icd->ops->owner)) { | ||
200 | dev_err(&icd->dev, "Couldn't lock sensor driver.\n"); | ||
201 | ret = -EINVAL; | ||
202 | goto emgd; | ||
203 | } | ||
204 | |||
205 | if (!try_module_get(ici->ops->owner)) { | ||
206 | dev_err(&icd->dev, "Couldn't lock capture bus driver.\n"); | ||
207 | ret = -EINVAL; | ||
208 | goto emgi; | ||
209 | } | ||
210 | |||
211 | icf->icd = icd; | ||
212 | |||
213 | icf->lock = ici->ops->spinlock_alloc(icf); | ||
214 | if (!icf->lock) { | ||
215 | ret = -ENOMEM; | ||
216 | goto esla; | ||
217 | } | ||
218 | |||
219 | icd->use_count++; | ||
220 | |||
221 | /* Now we really have to activate the camera */ | ||
222 | if (icd->use_count == 1) { | ||
223 | ret = ici->ops->add(icd); | ||
224 | if (ret < 0) { | ||
225 | dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret); | ||
226 | icd->use_count--; | ||
227 | goto eiciadd; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | mutex_unlock(&video_lock); | ||
232 | |||
233 | file->private_data = icf; | ||
234 | dev_dbg(&icd->dev, "camera device open\n"); | ||
235 | |||
236 | /* We must pass NULL as dev pointer, then all pci_* dma operations | ||
237 | * transform to normal dma_* ones. */ | ||
238 | videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, icf->lock, | ||
239 | V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE, | ||
240 | ici->msize, icd); | ||
241 | |||
242 | return 0; | ||
243 | |||
244 | /* All errors are entered with the video_lock held */ | ||
245 | eiciadd: | ||
246 | lock = icf->lock; | ||
247 | icf->lock = NULL; | ||
248 | if (ici->ops->spinlock_free) | ||
249 | ici->ops->spinlock_free(lock); | ||
250 | esla: | ||
251 | module_put(ici->ops->owner); | ||
252 | emgi: | ||
253 | module_put(icd->ops->owner); | ||
254 | emgd: | ||
255 | mutex_unlock(&video_lock); | ||
256 | vfree(icf); | ||
257 | return ret; | ||
258 | } | ||
259 | |||
260 | static int soc_camera_close(struct inode *inode, struct file *file) | ||
261 | { | ||
262 | struct soc_camera_file *icf = file->private_data; | ||
263 | struct soc_camera_device *icd = icf->icd; | ||
264 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | ||
265 | struct video_device *vdev = icd->vdev; | ||
266 | spinlock_t *lock = icf->lock; | ||
267 | |||
268 | mutex_lock(&video_lock); | ||
269 | icd->use_count--; | ||
270 | if (!icd->use_count) | ||
271 | ici->ops->remove(icd); | ||
272 | icf->lock = NULL; | ||
273 | if (ici->ops->spinlock_free) | ||
274 | ici->ops->spinlock_free(lock); | ||
275 | module_put(icd->ops->owner); | ||
276 | module_put(ici->ops->owner); | ||
277 | mutex_unlock(&video_lock); | ||
278 | |||
279 | vfree(icf); | ||
280 | |||
281 | dev_dbg(vdev->dev, "camera device close\n"); | ||
282 | |||
283 | return 0; | ||
284 | } | ||
285 | |||
286 | static ssize_t soc_camera_read(struct file *file, char __user *buf, | ||
287 | size_t count, loff_t *ppos) | ||
288 | { | ||
289 | struct soc_camera_file *icf = file->private_data; | ||
290 | struct soc_camera_device *icd = icf->icd; | ||
291 | struct video_device *vdev = icd->vdev; | ||
292 | int err = -EINVAL; | ||
293 | |||
294 | dev_err(vdev->dev, "camera device read not implemented\n"); | ||
295 | |||
296 | return err; | ||
297 | } | ||
298 | |||
299 | static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) | ||
300 | { | ||
301 | struct soc_camera_file *icf = file->private_data; | ||
302 | struct soc_camera_device *icd = icf->icd; | ||
303 | int err; | ||
304 | |||
305 | dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); | ||
306 | |||
307 | err = videobuf_mmap_mapper(&icf->vb_vidq, vma); | ||
308 | |||
309 | dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n", | ||
310 | (unsigned long)vma->vm_start, | ||
311 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, | ||
312 | err); | ||
313 | |||
314 | return err; | ||
315 | } | ||
316 | |||
317 | static unsigned int soc_camera_poll(struct file *file, poll_table *pt) | ||
318 | { | ||
319 | struct soc_camera_file *icf = file->private_data; | ||
320 | struct soc_camera_device *icd = icf->icd; | ||
321 | struct soc_camera_host *ici = | ||
322 | to_soc_camera_host(icd->dev.parent); | ||
323 | |||
324 | if (list_empty(&icf->vb_vidq.stream)) { | ||
325 | dev_err(&icd->dev, "Trying to poll with no queued buffers!\n"); | ||
326 | return POLLERR; | ||
327 | } | ||
328 | |||
329 | return ici->ops->poll(file, pt); | ||
330 | } | ||
331 | |||
332 | |||
333 | static struct file_operations soc_camera_fops = { | ||
334 | .owner = THIS_MODULE, | ||
335 | .open = soc_camera_open, | ||
336 | .release = soc_camera_close, | ||
337 | .ioctl = video_ioctl2, | ||
338 | .read = soc_camera_read, | ||
339 | .mmap = soc_camera_mmap, | ||
340 | .poll = soc_camera_poll, | ||
341 | .llseek = no_llseek, | ||
342 | }; | ||
343 | |||
344 | |||
345 | static int soc_camera_s_fmt_cap(struct file *file, void *priv, | ||
346 | struct v4l2_format *f) | ||
347 | { | ||
348 | struct soc_camera_file *icf = file->private_data; | ||
349 | struct soc_camera_device *icd = icf->icd; | ||
350 | struct soc_camera_host *ici = | ||
351 | to_soc_camera_host(icd->dev.parent); | ||
352 | int ret; | ||
353 | struct v4l2_rect rect; | ||
354 | const static struct soc_camera_data_format *data_fmt; | ||
355 | |||
356 | WARN_ON(priv != file->private_data); | ||
357 | |||
358 | data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat); | ||
359 | if (!data_fmt) | ||
360 | return -EINVAL; | ||
361 | |||
362 | /* buswidth may be further adjusted by the ici */ | ||
363 | icd->buswidth = data_fmt->depth; | ||
364 | |||
365 | ret = soc_camera_try_fmt_cap(file, icf, f); | ||
366 | if (ret < 0) | ||
367 | return ret; | ||
368 | |||
369 | rect.left = icd->x_current; | ||
370 | rect.top = icd->y_current; | ||
371 | rect.width = f->fmt.pix.width; | ||
372 | rect.height = f->fmt.pix.height; | ||
373 | ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect); | ||
374 | if (ret < 0) | ||
375 | return ret; | ||
376 | |||
377 | icd->current_fmt = data_fmt; | ||
378 | icd->width = rect.width; | ||
379 | icd->height = rect.height; | ||
380 | icf->vb_vidq.field = f->fmt.pix.field; | ||
381 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type) | ||
382 | dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n", | ||
383 | f->type); | ||
384 | |||
385 | dev_dbg(&icd->dev, "set width: %d height: %d\n", | ||
386 | icd->width, icd->height); | ||
387 | |||
388 | /* set physical bus parameters */ | ||
389 | return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat); | ||
390 | } | ||
391 | |||
392 | static int soc_camera_enum_fmt_cap(struct file *file, void *priv, | ||
393 | struct v4l2_fmtdesc *f) | ||
394 | { | ||
395 | struct soc_camera_file *icf = file->private_data; | ||
396 | struct soc_camera_device *icd = icf->icd; | ||
397 | const struct soc_camera_data_format *format; | ||
398 | |||
399 | WARN_ON(priv != file->private_data); | ||
400 | |||
401 | if (f->index >= icd->num_formats) | ||
402 | return -EINVAL; | ||
403 | |||
404 | format = &icd->formats[f->index]; | ||
405 | |||
406 | strlcpy(f->description, format->name, sizeof(f->description)); | ||
407 | f->pixelformat = format->fourcc; | ||
408 | return 0; | ||
409 | } | ||
410 | |||
411 | static int soc_camera_g_fmt_cap(struct file *file, void *priv, | ||
412 | struct v4l2_format *f) | ||
413 | { | ||
414 | struct soc_camera_file *icf = file->private_data; | ||
415 | struct soc_camera_device *icd = icf->icd; | ||
416 | |||
417 | WARN_ON(priv != file->private_data); | ||
418 | |||
419 | f->fmt.pix.width = icd->width; | ||
420 | f->fmt.pix.height = icd->height; | ||
421 | f->fmt.pix.field = icf->vb_vidq.field; | ||
422 | f->fmt.pix.pixelformat = icd->current_fmt->fourcc; | ||
423 | f->fmt.pix.bytesperline = | ||
424 | (f->fmt.pix.width * icd->current_fmt->depth) >> 3; | ||
425 | f->fmt.pix.sizeimage = | ||
426 | f->fmt.pix.height * f->fmt.pix.bytesperline; | ||
427 | dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n", | ||
428 | icd->current_fmt->fourcc); | ||
429 | return 0; | ||
430 | } | ||
431 | |||
432 | static int soc_camera_querycap(struct file *file, void *priv, | ||
433 | struct v4l2_capability *cap) | ||
434 | { | ||
435 | struct soc_camera_file *icf = file->private_data; | ||
436 | struct soc_camera_device *icd = icf->icd; | ||
437 | struct soc_camera_host *ici = | ||
438 | to_soc_camera_host(icd->dev.parent); | ||
439 | |||
440 | WARN_ON(priv != file->private_data); | ||
441 | |||
442 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); | ||
443 | return ici->ops->querycap(ici, cap); | ||
444 | } | ||
445 | |||
446 | static int soc_camera_streamon(struct file *file, void *priv, | ||
447 | enum v4l2_buf_type i) | ||
448 | { | ||
449 | struct soc_camera_file *icf = file->private_data; | ||
450 | struct soc_camera_device *icd = icf->icd; | ||
451 | |||
452 | WARN_ON(priv != file->private_data); | ||
453 | |||
454 | dev_dbg(&icd->dev, "%s\n", __func__); | ||
455 | |||
456 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
457 | return -EINVAL; | ||
458 | |||
459 | icd->ops->start_capture(icd); | ||
460 | |||
461 | /* This calls buf_queue from host driver's videobuf_queue_ops */ | ||
462 | return videobuf_streamon(&icf->vb_vidq); | ||
463 | } | ||
464 | |||
465 | static int soc_camera_streamoff(struct file *file, void *priv, | ||
466 | enum v4l2_buf_type i) | ||
467 | { | ||
468 | struct soc_camera_file *icf = file->private_data; | ||
469 | struct soc_camera_device *icd = icf->icd; | ||
470 | |||
471 | WARN_ON(priv != file->private_data); | ||
472 | |||
473 | dev_dbg(&icd->dev, "%s\n", __func__); | ||
474 | |||
475 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
476 | return -EINVAL; | ||
477 | |||
478 | /* This calls buf_release from host driver's videobuf_queue_ops for all | ||
479 | * remaining buffers. When the last buffer is freed, stop capture */ | ||
480 | videobuf_streamoff(&icf->vb_vidq); | ||
481 | |||
482 | icd->ops->stop_capture(icd); | ||
483 | |||
484 | return 0; | ||
485 | } | ||
486 | |||
487 | static int soc_camera_queryctrl(struct file *file, void *priv, | ||
488 | struct v4l2_queryctrl *qc) | ||
489 | { | ||
490 | struct soc_camera_file *icf = file->private_data; | ||
491 | struct soc_camera_device *icd = icf->icd; | ||
492 | int i; | ||
493 | |||
494 | WARN_ON(priv != file->private_data); | ||
495 | |||
496 | if (!qc->id) | ||
497 | return -EINVAL; | ||
498 | |||
499 | for (i = 0; i < icd->ops->num_controls; i++) | ||
500 | if (qc->id == icd->ops->controls[i].id) { | ||
501 | memcpy(qc, &(icd->ops->controls[i]), | ||
502 | sizeof(*qc)); | ||
503 | return 0; | ||
504 | } | ||
505 | |||
506 | return -EINVAL; | ||
507 | } | ||
508 | |||
509 | static int soc_camera_g_ctrl(struct file *file, void *priv, | ||
510 | struct v4l2_control *ctrl) | ||
511 | { | ||
512 | struct soc_camera_file *icf = file->private_data; | ||
513 | struct soc_camera_device *icd = icf->icd; | ||
514 | |||
515 | WARN_ON(priv != file->private_data); | ||
516 | |||
517 | switch (ctrl->id) { | ||
518 | case V4L2_CID_GAIN: | ||
519 | if (icd->gain == (unsigned short)~0) | ||
520 | return -EINVAL; | ||
521 | ctrl->value = icd->gain; | ||
522 | return 0; | ||
523 | case V4L2_CID_EXPOSURE: | ||
524 | if (icd->exposure == (unsigned short)~0) | ||
525 | return -EINVAL; | ||
526 | ctrl->value = icd->exposure; | ||
527 | return 0; | ||
528 | } | ||
529 | |||
530 | if (icd->ops->get_control) | ||
531 | return icd->ops->get_control(icd, ctrl); | ||
532 | return -EINVAL; | ||
533 | } | ||
534 | |||
535 | static int soc_camera_s_ctrl(struct file *file, void *priv, | ||
536 | struct v4l2_control *ctrl) | ||
537 | { | ||
538 | struct soc_camera_file *icf = file->private_data; | ||
539 | struct soc_camera_device *icd = icf->icd; | ||
540 | |||
541 | WARN_ON(priv != file->private_data); | ||
542 | |||
543 | if (icd->ops->set_control) | ||
544 | return icd->ops->set_control(icd, ctrl); | ||
545 | return -EINVAL; | ||
546 | } | ||
547 | |||
548 | static int soc_camera_cropcap(struct file *file, void *fh, | ||
549 | struct v4l2_cropcap *a) | ||
550 | { | ||
551 | struct soc_camera_file *icf = file->private_data; | ||
552 | struct soc_camera_device *icd = icf->icd; | ||
553 | |||
554 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
555 | a->bounds.left = icd->x_min; | ||
556 | a->bounds.top = icd->y_min; | ||
557 | a->bounds.width = icd->width_max; | ||
558 | a->bounds.height = icd->height_max; | ||
559 | a->defrect.left = icd->x_min; | ||
560 | a->defrect.top = icd->y_min; | ||
561 | a->defrect.width = 640; | ||
562 | a->defrect.height = 480; | ||
563 | a->pixelaspect.numerator = 1; | ||
564 | a->pixelaspect.denominator = 1; | ||
565 | |||
566 | return 0; | ||
567 | } | ||
568 | |||
569 | static int soc_camera_g_crop(struct file *file, void *fh, | ||
570 | struct v4l2_crop *a) | ||
571 | { | ||
572 | struct soc_camera_file *icf = file->private_data; | ||
573 | struct soc_camera_device *icd = icf->icd; | ||
574 | |||
575 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
576 | a->c.left = icd->x_current; | ||
577 | a->c.top = icd->y_current; | ||
578 | a->c.width = icd->width; | ||
579 | a->c.height = icd->height; | ||
580 | |||
581 | return 0; | ||
582 | } | ||
583 | |||
584 | static int soc_camera_s_crop(struct file *file, void *fh, | ||
585 | struct v4l2_crop *a) | ||
586 | { | ||
587 | struct soc_camera_file *icf = file->private_data; | ||
588 | struct soc_camera_device *icd = icf->icd; | ||
589 | struct soc_camera_host *ici = | ||
590 | to_soc_camera_host(icd->dev.parent); | ||
591 | int ret; | ||
592 | |||
593 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
594 | return -EINVAL; | ||
595 | |||
596 | ret = ici->ops->set_fmt_cap(icd, 0, &a->c); | ||
597 | if (!ret) { | ||
598 | icd->width = a->c.width; | ||
599 | icd->height = a->c.height; | ||
600 | icd->x_current = a->c.left; | ||
601 | icd->y_current = a->c.top; | ||
602 | } | ||
603 | |||
604 | return ret; | ||
605 | } | ||
606 | |||
607 | static int soc_camera_g_chip_ident(struct file *file, void *fh, | ||
608 | struct v4l2_chip_ident *id) | ||
609 | { | ||
610 | struct soc_camera_file *icf = file->private_data; | ||
611 | struct soc_camera_device *icd = icf->icd; | ||
612 | |||
613 | if (!icd->ops->get_chip_id) | ||
614 | return -EINVAL; | ||
615 | |||
616 | return icd->ops->get_chip_id(icd, id); | ||
617 | } | ||
618 | |||
619 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
620 | static int soc_camera_g_register(struct file *file, void *fh, | ||
621 | struct v4l2_register *reg) | ||
622 | { | ||
623 | struct soc_camera_file *icf = file->private_data; | ||
624 | struct soc_camera_device *icd = icf->icd; | ||
625 | |||
626 | if (!icd->ops->get_register) | ||
627 | return -EINVAL; | ||
628 | |||
629 | return icd->ops->get_register(icd, reg); | ||
630 | } | ||
631 | |||
632 | static int soc_camera_s_register(struct file *file, void *fh, | ||
633 | struct v4l2_register *reg) | ||
634 | { | ||
635 | struct soc_camera_file *icf = file->private_data; | ||
636 | struct soc_camera_device *icd = icf->icd; | ||
637 | |||
638 | if (!icd->ops->set_register) | ||
639 | return -EINVAL; | ||
640 | |||
641 | return icd->ops->set_register(icd, reg); | ||
642 | } | ||
643 | #endif | ||
644 | |||
645 | static int device_register_link(struct soc_camera_device *icd) | ||
646 | { | ||
647 | int ret = device_register(&icd->dev); | ||
648 | |||
649 | if (ret < 0) { | ||
650 | /* Prevent calling device_unregister() */ | ||
651 | icd->dev.parent = NULL; | ||
652 | dev_err(&icd->dev, "Cannot register device: %d\n", ret); | ||
653 | /* Even if probe() was unsuccessful for all registered drivers, | ||
654 | * device_register() returns 0, and we add the link, just to | ||
655 | * document this camera's control device */ | ||
656 | } else if (icd->control) | ||
657 | /* Have to sysfs_remove_link() before device_unregister()? */ | ||
658 | if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj, | ||
659 | "control")) | ||
660 | dev_warn(&icd->dev, | ||
661 | "Failed creating the control symlink\n"); | ||
662 | return ret; | ||
663 | } | ||
664 | |||
665 | /* So far this function cannot fail */ | ||
666 | static void scan_add_host(struct soc_camera_host *ici) | ||
667 | { | ||
668 | struct soc_camera_device *icd; | ||
669 | |||
670 | mutex_lock(&list_lock); | ||
671 | |||
672 | list_for_each_entry(icd, &devices, list) { | ||
673 | if (icd->iface == ici->nr) { | ||
674 | icd->dev.parent = &ici->dev; | ||
675 | device_register_link(icd); | ||
676 | } | ||
677 | } | ||
678 | |||
679 | mutex_unlock(&list_lock); | ||
680 | } | ||
681 | |||
682 | /* return: 0 if no match found or a match found and | ||
683 | * device_register() successful, error code otherwise */ | ||
684 | static int scan_add_device(struct soc_camera_device *icd) | ||
685 | { | ||
686 | struct soc_camera_host *ici; | ||
687 | int ret = 0; | ||
688 | |||
689 | mutex_lock(&list_lock); | ||
690 | |||
691 | list_add_tail(&icd->list, &devices); | ||
692 | |||
693 | /* Watch out for class_for_each_device / class_find_device API by | ||
694 | * Dave Young <hidave.darkstar@gmail.com> */ | ||
695 | list_for_each_entry(ici, &hosts, list) { | ||
696 | if (icd->iface == ici->nr) { | ||
697 | ret = 1; | ||
698 | icd->dev.parent = &ici->dev; | ||
699 | break; | ||
700 | } | ||
701 | } | ||
702 | |||
703 | mutex_unlock(&list_lock); | ||
704 | |||
705 | if (ret) | ||
706 | ret = device_register_link(icd); | ||
707 | |||
708 | return ret; | ||
709 | } | ||
710 | |||
711 | static int soc_camera_probe(struct device *dev) | ||
712 | { | ||
713 | struct soc_camera_device *icd = to_soc_camera_dev(dev); | ||
714 | struct soc_camera_host *ici = | ||
715 | to_soc_camera_host(icd->dev.parent); | ||
716 | int ret; | ||
717 | |||
718 | if (!icd->ops->probe) | ||
719 | return -ENODEV; | ||
720 | |||
721 | /* We only call ->add() here to activate and probe the camera. | ||
722 | * We shall ->remove() and deactivate it immediately afterwards. */ | ||
723 | ret = ici->ops->add(icd); | ||
724 | if (ret < 0) | ||
725 | return ret; | ||
726 | |||
727 | ret = icd->ops->probe(icd); | ||
728 | if (ret >= 0) { | ||
729 | const struct v4l2_queryctrl *qctrl; | ||
730 | |||
731 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN); | ||
732 | icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0; | ||
733 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | ||
734 | icd->exposure = qctrl ? qctrl->default_value : | ||
735 | (unsigned short)~0; | ||
736 | } | ||
737 | ici->ops->remove(icd); | ||
738 | |||
739 | return ret; | ||
740 | } | ||
741 | |||
742 | /* This is called on device_unregister, which only means we have to disconnect | ||
743 | * from the host, but not remove ourselves from the device list */ | ||
744 | static int soc_camera_remove(struct device *dev) | ||
745 | { | ||
746 | struct soc_camera_device *icd = to_soc_camera_dev(dev); | ||
747 | |||
748 | if (icd->ops->remove) | ||
749 | icd->ops->remove(icd); | ||
750 | |||
751 | return 0; | ||
752 | } | ||
753 | |||
754 | static struct bus_type soc_camera_bus_type = { | ||
755 | .name = "soc-camera", | ||
756 | .probe = soc_camera_probe, | ||
757 | .remove = soc_camera_remove, | ||
758 | }; | ||
759 | |||
760 | static struct device_driver ic_drv = { | ||
761 | .name = "camera", | ||
762 | .bus = &soc_camera_bus_type, | ||
763 | .owner = THIS_MODULE, | ||
764 | }; | ||
765 | |||
766 | /* | ||
767 | * Image capture host - this is a host device, not a bus device, so, | ||
768 | * no bus reference, no probing. | ||
769 | */ | ||
770 | static struct class soc_camera_host_class = { | ||
771 | .owner = THIS_MODULE, | ||
772 | .name = "camera_host", | ||
773 | }; | ||
774 | |||
775 | static void dummy_release(struct device *dev) | ||
776 | { | ||
777 | } | ||
778 | |||
779 | static spinlock_t *spinlock_alloc(struct soc_camera_file *icf) | ||
780 | { | ||
781 | spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL); | ||
782 | |||
783 | if (lock) | ||
784 | spin_lock_init(lock); | ||
785 | |||
786 | return lock; | ||
787 | } | ||
788 | |||
789 | static void spinlock_free(spinlock_t *lock) | ||
790 | { | ||
791 | kfree(lock); | ||
792 | } | ||
793 | |||
794 | int soc_camera_host_register(struct soc_camera_host *ici) | ||
795 | { | ||
796 | int ret; | ||
797 | struct soc_camera_host *ix; | ||
798 | |||
799 | if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove) | ||
800 | return -EINVAL; | ||
801 | |||
802 | /* Number might be equal to the platform device ID */ | ||
803 | sprintf(ici->dev.bus_id, "camera_host%d", ici->nr); | ||
804 | ici->dev.class = &soc_camera_host_class; | ||
805 | |||
806 | mutex_lock(&list_lock); | ||
807 | list_for_each_entry(ix, &hosts, list) { | ||
808 | if (ix->nr == ici->nr) { | ||
809 | mutex_unlock(&list_lock); | ||
810 | return -EBUSY; | ||
811 | } | ||
812 | } | ||
813 | |||
814 | list_add_tail(&ici->list, &hosts); | ||
815 | mutex_unlock(&list_lock); | ||
816 | |||
817 | ici->dev.release = dummy_release; | ||
818 | |||
819 | ret = device_register(&ici->dev); | ||
820 | |||
821 | if (ret) | ||
822 | goto edevr; | ||
823 | |||
824 | if (!ici->ops->spinlock_alloc) { | ||
825 | ici->ops->spinlock_alloc = spinlock_alloc; | ||
826 | ici->ops->spinlock_free = spinlock_free; | ||
827 | } | ||
828 | |||
829 | scan_add_host(ici); | ||
830 | |||
831 | return 0; | ||
832 | |||
833 | edevr: | ||
834 | mutex_lock(&list_lock); | ||
835 | list_del(&ici->list); | ||
836 | mutex_unlock(&list_lock); | ||
837 | |||
838 | return ret; | ||
839 | } | ||
840 | EXPORT_SYMBOL(soc_camera_host_register); | ||
841 | |||
842 | /* Unregister all clients! */ | ||
843 | void soc_camera_host_unregister(struct soc_camera_host *ici) | ||
844 | { | ||
845 | struct soc_camera_device *icd; | ||
846 | |||
847 | mutex_lock(&list_lock); | ||
848 | |||
849 | list_del(&ici->list); | ||
850 | |||
851 | list_for_each_entry(icd, &devices, list) { | ||
852 | if (icd->dev.parent == &ici->dev) { | ||
853 | device_unregister(&icd->dev); | ||
854 | /* Not before device_unregister(), .remove | ||
855 | * needs parent to call ici->ops->remove() */ | ||
856 | icd->dev.parent = NULL; | ||
857 | memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj)); | ||
858 | } | ||
859 | } | ||
860 | |||
861 | mutex_unlock(&list_lock); | ||
862 | |||
863 | device_unregister(&ici->dev); | ||
864 | } | ||
865 | EXPORT_SYMBOL(soc_camera_host_unregister); | ||
866 | |||
867 | /* Image capture device */ | ||
868 | int soc_camera_device_register(struct soc_camera_device *icd) | ||
869 | { | ||
870 | struct soc_camera_device *ix; | ||
871 | int num = -1, i; | ||
872 | |||
873 | if (!icd) | ||
874 | return -EINVAL; | ||
875 | |||
876 | for (i = 0; i < 256 && num < 0; i++) { | ||
877 | num = i; | ||
878 | list_for_each_entry(ix, &devices, list) { | ||
879 | if (ix->iface == icd->iface && ix->devnum == i) { | ||
880 | num = -1; | ||
881 | break; | ||
882 | } | ||
883 | } | ||
884 | } | ||
885 | |||
886 | if (num < 0) | ||
887 | /* ok, we have 256 cameras on this host... | ||
888 | * man, stay reasonable... */ | ||
889 | return -ENOMEM; | ||
890 | |||
891 | icd->devnum = num; | ||
892 | icd->dev.bus = &soc_camera_bus_type; | ||
893 | snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id), | ||
894 | "%u-%u", icd->iface, icd->devnum); | ||
895 | |||
896 | icd->dev.release = dummy_release; | ||
897 | |||
898 | return scan_add_device(icd); | ||
899 | } | ||
900 | EXPORT_SYMBOL(soc_camera_device_register); | ||
901 | |||
902 | void soc_camera_device_unregister(struct soc_camera_device *icd) | ||
903 | { | ||
904 | mutex_lock(&list_lock); | ||
905 | list_del(&icd->list); | ||
906 | |||
907 | /* The bus->remove will be eventually called */ | ||
908 | if (icd->dev.parent) | ||
909 | device_unregister(&icd->dev); | ||
910 | mutex_unlock(&list_lock); | ||
911 | } | ||
912 | EXPORT_SYMBOL(soc_camera_device_unregister); | ||
913 | |||
914 | int soc_camera_video_start(struct soc_camera_device *icd) | ||
915 | { | ||
916 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); | ||
917 | int err = -ENOMEM; | ||
918 | struct video_device *vdev; | ||
919 | |||
920 | if (!icd->dev.parent) | ||
921 | return -ENODEV; | ||
922 | |||
923 | vdev = video_device_alloc(); | ||
924 | if (!vdev) | ||
925 | goto evidallocd; | ||
926 | dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev); | ||
927 | |||
928 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); | ||
929 | /* Maybe better &ici->dev */ | ||
930 | vdev->dev = &icd->dev; | ||
931 | vdev->type = VID_TYPE_CAPTURE; | ||
932 | vdev->current_norm = V4L2_STD_UNKNOWN; | ||
933 | vdev->fops = &soc_camera_fops; | ||
934 | vdev->release = video_device_release; | ||
935 | vdev->minor = -1; | ||
936 | vdev->tvnorms = V4L2_STD_UNKNOWN, | ||
937 | vdev->vidioc_querycap = soc_camera_querycap; | ||
938 | vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap; | ||
939 | vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap; | ||
940 | vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap; | ||
941 | vdev->vidioc_enum_input = soc_camera_enum_input; | ||
942 | vdev->vidioc_g_input = soc_camera_g_input; | ||
943 | vdev->vidioc_s_input = soc_camera_s_input; | ||
944 | vdev->vidioc_s_std = soc_camera_s_std; | ||
945 | vdev->vidioc_reqbufs = soc_camera_reqbufs; | ||
946 | vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap; | ||
947 | vdev->vidioc_querybuf = soc_camera_querybuf; | ||
948 | vdev->vidioc_qbuf = soc_camera_qbuf; | ||
949 | vdev->vidioc_dqbuf = soc_camera_dqbuf; | ||
950 | vdev->vidioc_streamon = soc_camera_streamon; | ||
951 | vdev->vidioc_streamoff = soc_camera_streamoff; | ||
952 | vdev->vidioc_queryctrl = soc_camera_queryctrl; | ||
953 | vdev->vidioc_g_ctrl = soc_camera_g_ctrl; | ||
954 | vdev->vidioc_s_ctrl = soc_camera_s_ctrl; | ||
955 | vdev->vidioc_cropcap = soc_camera_cropcap; | ||
956 | vdev->vidioc_g_crop = soc_camera_g_crop; | ||
957 | vdev->vidioc_s_crop = soc_camera_s_crop; | ||
958 | vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident; | ||
959 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
960 | vdev->vidioc_g_register = soc_camera_g_register; | ||
961 | vdev->vidioc_s_register = soc_camera_s_register; | ||
962 | #endif | ||
963 | |||
964 | icd->current_fmt = &icd->formats[0]; | ||
965 | |||
966 | err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor); | ||
967 | if (err < 0) { | ||
968 | dev_err(vdev->dev, "video_register_device failed\n"); | ||
969 | goto evidregd; | ||
970 | } | ||
971 | icd->vdev = vdev; | ||
972 | |||
973 | return 0; | ||
974 | |||
975 | evidregd: | ||
976 | video_device_release(vdev); | ||
977 | evidallocd: | ||
978 | return err; | ||
979 | } | ||
980 | EXPORT_SYMBOL(soc_camera_video_start); | ||
981 | |||
982 | void soc_camera_video_stop(struct soc_camera_device *icd) | ||
983 | { | ||
984 | struct video_device *vdev = icd->vdev; | ||
985 | |||
986 | dev_dbg(&icd->dev, "%s\n", __func__); | ||
987 | |||
988 | if (!icd->dev.parent || !vdev) | ||
989 | return; | ||
990 | |||
991 | mutex_lock(&video_lock); | ||
992 | video_unregister_device(vdev); | ||
993 | icd->vdev = NULL; | ||
994 | mutex_unlock(&video_lock); | ||
995 | } | ||
996 | EXPORT_SYMBOL(soc_camera_video_stop); | ||
997 | |||
998 | static int __init soc_camera_init(void) | ||
999 | { | ||
1000 | int ret = bus_register(&soc_camera_bus_type); | ||
1001 | if (ret) | ||
1002 | return ret; | ||
1003 | ret = driver_register(&ic_drv); | ||
1004 | if (ret) | ||
1005 | goto edrvr; | ||
1006 | ret = class_register(&soc_camera_host_class); | ||
1007 | if (ret) | ||
1008 | goto eclr; | ||
1009 | |||
1010 | return 0; | ||
1011 | |||
1012 | eclr: | ||
1013 | driver_unregister(&ic_drv); | ||
1014 | edrvr: | ||
1015 | bus_unregister(&soc_camera_bus_type); | ||
1016 | return ret; | ||
1017 | } | ||
1018 | |||
1019 | static void __exit soc_camera_exit(void) | ||
1020 | { | ||
1021 | class_unregister(&soc_camera_host_class); | ||
1022 | driver_unregister(&ic_drv); | ||
1023 | bus_unregister(&soc_camera_bus_type); | ||
1024 | } | ||
1025 | |||
1026 | module_init(soc_camera_init); | ||
1027 | module_exit(soc_camera_exit); | ||
1028 | |||
1029 | MODULE_DESCRIPTION("Image capture bus driver"); | ||
1030 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | ||
1031 | MODULE_LICENSE("GPL"); | ||