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