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