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