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