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> |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 25 | #include <linux/module.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> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 31 | #include <media/v4l2-ioctl.h> |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 32 | #include <media/v4l2-dev.h> |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 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); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 41 | static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ |
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 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 173 | ret = videobuf_reqbufs(&icf->vb_vidq, p); |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 177 | return ici->ops->reqbufs(icf, p); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int soc_camera_querybuf(struct file *file, void *priv, |
| 181 | struct v4l2_buffer *p) |
| 182 | { |
| 183 | struct soc_camera_file *icf = file->private_data; |
| 184 | |
| 185 | WARN_ON(priv != file->private_data); |
| 186 | |
| 187 | return videobuf_querybuf(&icf->vb_vidq, p); |
| 188 | } |
| 189 | |
| 190 | static int soc_camera_qbuf(struct file *file, void *priv, |
| 191 | struct v4l2_buffer *p) |
| 192 | { |
| 193 | struct soc_camera_file *icf = file->private_data; |
| 194 | |
| 195 | WARN_ON(priv != file->private_data); |
| 196 | |
| 197 | return videobuf_qbuf(&icf->vb_vidq, p); |
| 198 | } |
| 199 | |
| 200 | static int soc_camera_dqbuf(struct file *file, void *priv, |
| 201 | struct v4l2_buffer *p) |
| 202 | { |
| 203 | struct soc_camera_file *icf = file->private_data; |
| 204 | |
| 205 | WARN_ON(priv != file->private_data); |
| 206 | |
| 207 | return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK); |
| 208 | } |
| 209 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 210 | /* Always entered with .video_lock held */ |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 211 | static int soc_camera_init_user_formats(struct soc_camera_device *icd) |
| 212 | { |
| 213 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 214 | int i, fmts = 0; |
| 215 | |
| 216 | if (!ici->ops->get_formats) |
| 217 | /* |
| 218 | * Fallback mode - the host will have to serve all |
| 219 | * sensor-provided formats one-to-one to the user |
| 220 | */ |
| 221 | fmts = icd->num_formats; |
| 222 | else |
| 223 | /* |
| 224 | * First pass - only count formats this host-sensor |
| 225 | * configuration can provide |
| 226 | */ |
| 227 | for (i = 0; i < icd->num_formats; i++) |
| 228 | fmts += ici->ops->get_formats(icd, i, NULL); |
| 229 | |
| 230 | if (!fmts) |
| 231 | return -ENXIO; |
| 232 | |
| 233 | icd->user_formats = |
| 234 | vmalloc(fmts * sizeof(struct soc_camera_format_xlate)); |
| 235 | if (!icd->user_formats) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | icd->num_user_formats = fmts; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 239 | |
| 240 | dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts); |
| 241 | |
| 242 | /* Second pass - actually fill data formats */ |
Stefan Herbrechtsmeier | 14df2cc | 2009-06-24 10:31:25 -0300 | [diff] [blame] | 243 | fmts = 0; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 244 | for (i = 0; i < icd->num_formats; i++) |
| 245 | if (!ici->ops->get_formats) { |
| 246 | icd->user_formats[i].host_fmt = icd->formats + i; |
| 247 | icd->user_formats[i].cam_fmt = icd->formats + i; |
| 248 | icd->user_formats[i].buswidth = icd->formats[i].depth; |
| 249 | } else { |
| 250 | fmts += ici->ops->get_formats(icd, i, |
| 251 | &icd->user_formats[fmts]); |
| 252 | } |
| 253 | |
| 254 | icd->current_fmt = icd->user_formats[0].host_fmt; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 259 | /* Always entered with .video_lock held */ |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 260 | static void soc_camera_free_user_formats(struct soc_camera_device *icd) |
| 261 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 262 | icd->current_fmt = NULL; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 263 | vfree(icd->user_formats); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 264 | icd->user_formats = NULL; |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 265 | } |
| 266 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 267 | /* Called with .vb_lock held */ |
| 268 | static int soc_camera_set_fmt(struct soc_camera_file *icf, |
| 269 | struct v4l2_format *f) |
| 270 | { |
| 271 | struct soc_camera_device *icd = icf->icd; |
| 272 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 273 | struct v4l2_pix_format *pix = &f->fmt.pix; |
| 274 | int ret; |
| 275 | |
| 276 | /* We always call try_fmt() before set_fmt() or set_crop() */ |
| 277 | ret = ici->ops->try_fmt(icd, f); |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | ret = ici->ops->set_fmt(icd, f); |
| 282 | if (ret < 0) { |
| 283 | return ret; |
| 284 | } else if (!icd->current_fmt || |
| 285 | icd->current_fmt->fourcc != pix->pixelformat) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 286 | dev_err(ici->v4l2_dev.dev, |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 287 | "Host driver hasn't set up current format correctly!\n"); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | |
| 291 | icd->width = pix->width; |
| 292 | icd->height = pix->height; |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 293 | icf->vb_vidq.field = |
| 294 | icd->field = pix->field; |
| 295 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 296 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 297 | dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n", |
| 298 | f->type); |
| 299 | |
| 300 | dev_dbg(&icd->dev, "set width: %d height: %d\n", |
| 301 | icd->width, icd->height); |
| 302 | |
| 303 | /* set physical bus parameters */ |
| 304 | return ici->ops->set_bus_param(icd, pix->pixelformat); |
| 305 | } |
| 306 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 307 | static int soc_camera_open(struct file *file) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 308 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 309 | struct video_device *vdev = video_devdata(file); |
| 310 | struct soc_camera_device *icd = container_of(vdev->parent, struct soc_camera_device, dev); |
| 311 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 312 | struct soc_camera_host *ici; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 313 | struct soc_camera_file *icf; |
| 314 | int ret; |
| 315 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 316 | if (!icd->ops) |
| 317 | /* No device driver attached */ |
| 318 | return -ENODEV; |
| 319 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 320 | ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 321 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 322 | icf = vmalloc(sizeof(*icf)); |
| 323 | if (!icf) |
| 324 | return -ENOMEM; |
| 325 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 326 | if (!try_module_get(ici->ops->owner)) { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 327 | dev_err(&icd->dev, "Couldn't lock capture bus driver.\n"); |
| 328 | ret = -EINVAL; |
| 329 | goto emgi; |
| 330 | } |
| 331 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 332 | /* Protect against icd->ops->remove() until we module_get() both drivers. */ |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 333 | mutex_lock(&icd->video_lock); |
| 334 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 335 | icf->icd = icd; |
Guennadi Liakhovetski | 1a0063a | 2008-04-04 13:46:34 -0300 | [diff] [blame] | 336 | icd->use_count++; |
| 337 | |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 338 | /* Now we really have to activate the camera */ |
| 339 | if (icd->use_count == 1) { |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 340 | /* Restore parameters before the last close() per V4L2 API */ |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 341 | struct v4l2_format f = { |
| 342 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 343 | .fmt.pix = { |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 344 | .width = icd->width, |
| 345 | .height = icd->height, |
| 346 | .field = icd->field, |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 347 | }, |
| 348 | }; |
| 349 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 350 | ret = soc_camera_init_user_formats(icd); |
| 351 | if (ret < 0) |
| 352 | goto eiufmt; |
| 353 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 354 | f.fmt.pix.pixelformat = icd->current_fmt->fourcc; |
| 355 | f.fmt.pix.colorspace = icd->current_fmt->colorspace; |
| 356 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 357 | if (icl->power) { |
| 358 | ret = icl->power(icd->pdev, 1); |
| 359 | if (ret < 0) |
| 360 | goto epower; |
| 361 | } |
| 362 | |
| 363 | /* The camera could have been already on, try to reset */ |
| 364 | if (icl->reset) |
| 365 | icl->reset(icd->pdev); |
| 366 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 367 | ret = ici->ops->add(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 368 | if (ret < 0) { |
| 369 | dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 370 | goto eiciadd; |
| 371 | } |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 372 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 373 | if (icd->ops->init) { |
| 374 | ret = icd->ops->init(icd); |
| 375 | if (ret < 0) |
| 376 | goto einit; |
| 377 | } |
| 378 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 379 | /* Try to configure with default parameters */ |
| 380 | ret = soc_camera_set_fmt(icf, &f); |
| 381 | if (ret < 0) |
| 382 | goto esfmt; |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 383 | } |
| 384 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 385 | file->private_data = icf; |
| 386 | dev_dbg(&icd->dev, "camera device open\n"); |
| 387 | |
Magnus Damm | a034d1b | 2008-07-11 20:59:34 -0300 | [diff] [blame] | 388 | ici->ops->init_videobuf(&icf->vb_vidq, icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 389 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 390 | mutex_unlock(&icd->video_lock); |
| 391 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 392 | return 0; |
| 393 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 394 | /* |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 395 | * First five errors are entered with the .video_lock held |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 396 | * and use_count == 1 |
| 397 | */ |
| 398 | esfmt: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 399 | if (icd->ops->release) |
| 400 | icd->ops->release(icd); |
| 401 | einit: |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 402 | ici->ops->remove(icd); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 403 | eiciadd: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 404 | if (icl->power) |
| 405 | icl->power(icd->pdev, 0); |
| 406 | epower: |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 407 | soc_camera_free_user_formats(icd); |
| 408 | eiufmt: |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 409 | icd->use_count--; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 410 | mutex_unlock(&icd->video_lock); |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 411 | module_put(ici->ops->owner); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 412 | emgi: |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 413 | vfree(icf); |
| 414 | return ret; |
| 415 | } |
| 416 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 417 | static int soc_camera_close(struct file *file) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 418 | { |
| 419 | struct soc_camera_file *icf = file->private_data; |
| 420 | struct soc_camera_device *icd = icf->icd; |
| 421 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 422 | struct video_device *vdev = icd->vdev; |
| 423 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 424 | mutex_lock(&icd->video_lock); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 425 | icd->use_count--; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 426 | if (!icd->use_count) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 427 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 428 | |
| 429 | if (icd->ops->release) |
| 430 | icd->ops->release(icd); |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 431 | ici->ops->remove(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 432 | if (icl->power) |
| 433 | icl->power(icd->pdev, 0); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 434 | soc_camera_free_user_formats(icd); |
| 435 | } |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 436 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 437 | mutex_unlock(&icd->video_lock); |
| 438 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 439 | module_put(ici->ops->owner); |
Guennadi Liakhovetski | 9dc4e48 | 2008-04-22 14:45:32 -0300 | [diff] [blame] | 440 | |
Guennadi Liakhovetski | 1a0063a | 2008-04-04 13:46:34 -0300 | [diff] [blame] | 441 | vfree(icf); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 442 | |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 443 | dev_dbg(vdev->parent, "camera device close\n"); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Andrew Morton | aba360d | 2008-04-22 14:45:59 -0300 | [diff] [blame] | 448 | static ssize_t soc_camera_read(struct file *file, char __user *buf, |
Guennadi Liakhovetski | abe4c47 | 2008-12-01 09:44:56 -0300 | [diff] [blame] | 449 | size_t count, loff_t *ppos) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 450 | { |
| 451 | struct soc_camera_file *icf = file->private_data; |
| 452 | struct soc_camera_device *icd = icf->icd; |
| 453 | struct video_device *vdev = icd->vdev; |
| 454 | int err = -EINVAL; |
| 455 | |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 456 | dev_err(vdev->parent, "camera device read not implemented\n"); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 457 | |
| 458 | return err; |
| 459 | } |
| 460 | |
| 461 | static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma) |
| 462 | { |
| 463 | struct soc_camera_file *icf = file->private_data; |
| 464 | struct soc_camera_device *icd = icf->icd; |
| 465 | int err; |
| 466 | |
| 467 | dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
| 468 | |
| 469 | err = videobuf_mmap_mapper(&icf->vb_vidq, vma); |
| 470 | |
| 471 | dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n", |
| 472 | (unsigned long)vma->vm_start, |
| 473 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
| 474 | err); |
| 475 | |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | static unsigned int soc_camera_poll(struct file *file, poll_table *pt) |
| 480 | { |
| 481 | struct soc_camera_file *icf = file->private_data; |
| 482 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 483 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 484 | |
| 485 | if (list_empty(&icf->vb_vidq.stream)) { |
| 486 | dev_err(&icd->dev, "Trying to poll with no queued buffers!\n"); |
| 487 | return POLLERR; |
| 488 | } |
| 489 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 490 | return ici->ops->poll(file, pt); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 491 | } |
| 492 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 493 | static struct v4l2_file_operations soc_camera_fops = { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 494 | .owner = THIS_MODULE, |
| 495 | .open = soc_camera_open, |
| 496 | .release = soc_camera_close, |
| 497 | .ioctl = video_ioctl2, |
| 498 | .read = soc_camera_read, |
| 499 | .mmap = soc_camera_mmap, |
| 500 | .poll = soc_camera_poll, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 501 | }; |
| 502 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 503 | 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] | 504 | struct v4l2_format *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 505 | { |
| 506 | struct soc_camera_file *icf = file->private_data; |
| 507 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 508 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 509 | |
| 510 | WARN_ON(priv != file->private_data); |
| 511 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 512 | mutex_lock(&icf->vb_vidq.vb_lock); |
| 513 | |
| 514 | if (videobuf_queue_is_busy(&icf->vb_vidq)) { |
| 515 | dev_err(&icd->dev, "S_FMT denied: queue busy\n"); |
| 516 | ret = -EBUSY; |
| 517 | goto unlock; |
| 518 | } |
| 519 | |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 520 | ret = soc_camera_set_fmt(icf, f); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 521 | |
| 522 | unlock: |
| 523 | mutex_unlock(&icf->vb_vidq.vb_lock); |
| 524 | |
| 525 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 526 | } |
| 527 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 528 | 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] | 529 | struct v4l2_fmtdesc *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 530 | { |
| 531 | struct soc_camera_file *icf = file->private_data; |
| 532 | struct soc_camera_device *icd = icf->icd; |
| 533 | const struct soc_camera_data_format *format; |
| 534 | |
| 535 | WARN_ON(priv != file->private_data); |
| 536 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 537 | if (f->index >= icd->num_user_formats) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 538 | return -EINVAL; |
| 539 | |
Guennadi Liakhovetski | c2786ad | 2008-12-01 09:45:27 -0300 | [diff] [blame] | 540 | format = icd->user_formats[f->index].host_fmt; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 541 | |
| 542 | strlcpy(f->description, format->name, sizeof(f->description)); |
| 543 | f->pixelformat = format->fourcc; |
| 544 | return 0; |
| 545 | } |
| 546 | |
Hans Verkuil | 7293789 | 2008-06-01 10:35:18 -0300 | [diff] [blame] | 547 | 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] | 548 | struct v4l2_format *f) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 549 | { |
| 550 | struct soc_camera_file *icf = file->private_data; |
| 551 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 552 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 553 | |
| 554 | WARN_ON(priv != file->private_data); |
| 555 | |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 556 | pix->width = icd->width; |
| 557 | pix->height = icd->height; |
| 558 | pix->field = icf->vb_vidq.field; |
| 559 | pix->pixelformat = icd->current_fmt->fourcc; |
| 560 | pix->bytesperline = pix->width * |
Guennadi Liakhovetski | 25c4d74 | 2008-12-01 09:44:59 -0300 | [diff] [blame] | 561 | DIV_ROUND_UP(icd->current_fmt->depth, 8); |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 562 | pix->sizeimage = pix->height * pix->bytesperline; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 563 | dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n", |
| 564 | icd->current_fmt->fourcc); |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int soc_camera_querycap(struct file *file, void *priv, |
| 569 | struct v4l2_capability *cap) |
| 570 | { |
| 571 | struct soc_camera_file *icf = file->private_data; |
| 572 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 573 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 574 | |
| 575 | WARN_ON(priv != file->private_data); |
| 576 | |
| 577 | strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver)); |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 578 | return ici->ops->querycap(ici, cap); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static int soc_camera_streamon(struct file *file, void *priv, |
| 582 | enum v4l2_buf_type i) |
| 583 | { |
| 584 | struct soc_camera_file *icf = file->private_data; |
| 585 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 586 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 587 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 588 | |
| 589 | WARN_ON(priv != file->private_data); |
| 590 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 591 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 592 | return -EINVAL; |
| 593 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 594 | mutex_lock(&icd->video_lock); |
| 595 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 596 | v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 1); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 597 | |
| 598 | /* This calls buf_queue from host driver's videobuf_queue_ops */ |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 599 | ret = videobuf_streamon(&icf->vb_vidq); |
| 600 | |
| 601 | mutex_unlock(&icd->video_lock); |
| 602 | |
| 603 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int soc_camera_streamoff(struct file *file, void *priv, |
| 607 | enum v4l2_buf_type i) |
| 608 | { |
| 609 | struct soc_camera_file *icf = file->private_data; |
| 610 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 611 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 612 | |
| 613 | WARN_ON(priv != file->private_data); |
| 614 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 615 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 616 | return -EINVAL; |
| 617 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 618 | mutex_lock(&icd->video_lock); |
| 619 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 620 | /* This calls buf_release from host driver's videobuf_queue_ops for all |
| 621 | * remaining buffers. When the last buffer is freed, stop capture */ |
| 622 | videobuf_streamoff(&icf->vb_vidq); |
| 623 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 624 | v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_stream, 0); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 625 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 626 | mutex_unlock(&icd->video_lock); |
| 627 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int soc_camera_queryctrl(struct file *file, void *priv, |
| 632 | struct v4l2_queryctrl *qc) |
| 633 | { |
| 634 | struct soc_camera_file *icf = file->private_data; |
| 635 | struct soc_camera_device *icd = icf->icd; |
| 636 | int i; |
| 637 | |
| 638 | WARN_ON(priv != file->private_data); |
| 639 | |
| 640 | if (!qc->id) |
| 641 | return -EINVAL; |
| 642 | |
| 643 | for (i = 0; i < icd->ops->num_controls; i++) |
| 644 | if (qc->id == icd->ops->controls[i].id) { |
| 645 | memcpy(qc, &(icd->ops->controls[i]), |
| 646 | sizeof(*qc)); |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
| 653 | static int soc_camera_g_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; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 658 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 659 | |
| 660 | WARN_ON(priv != file->private_data); |
| 661 | |
| 662 | switch (ctrl->id) { |
| 663 | case V4L2_CID_GAIN: |
| 664 | if (icd->gain == (unsigned short)~0) |
| 665 | return -EINVAL; |
| 666 | ctrl->value = icd->gain; |
| 667 | return 0; |
| 668 | case V4L2_CID_EXPOSURE: |
| 669 | if (icd->exposure == (unsigned short)~0) |
| 670 | return -EINVAL; |
| 671 | ctrl->value = icd->exposure; |
| 672 | return 0; |
| 673 | } |
| 674 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 675 | return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_ctrl, ctrl); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static int soc_camera_s_ctrl(struct file *file, void *priv, |
| 679 | struct v4l2_control *ctrl) |
| 680 | { |
| 681 | struct soc_camera_file *icf = file->private_data; |
| 682 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 683 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 684 | |
| 685 | WARN_ON(priv != file->private_data); |
| 686 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 687 | return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_ctrl, ctrl); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static int soc_camera_cropcap(struct file *file, void *fh, |
| 691 | struct v4l2_cropcap *a) |
| 692 | { |
| 693 | struct soc_camera_file *icf = file->private_data; |
| 694 | struct soc_camera_device *icd = icf->icd; |
| 695 | |
| 696 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 697 | a->bounds.left = icd->x_min; |
| 698 | a->bounds.top = icd->y_min; |
| 699 | a->bounds.width = icd->width_max; |
| 700 | a->bounds.height = icd->height_max; |
| 701 | a->defrect.left = icd->x_min; |
| 702 | a->defrect.top = icd->y_min; |
Guennadi Liakhovetski | df2ed07 | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 703 | a->defrect.width = DEFAULT_WIDTH; |
| 704 | a->defrect.height = DEFAULT_HEIGHT; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 705 | a->pixelaspect.numerator = 1; |
| 706 | a->pixelaspect.denominator = 1; |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static int soc_camera_g_crop(struct file *file, void *fh, |
| 712 | struct v4l2_crop *a) |
| 713 | { |
| 714 | struct soc_camera_file *icf = file->private_data; |
| 715 | struct soc_camera_device *icd = icf->icd; |
| 716 | |
| 717 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 718 | a->c.left = icd->x_current; |
| 719 | a->c.top = icd->y_current; |
| 720 | a->c.width = icd->width; |
| 721 | a->c.height = icd->height; |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static int soc_camera_s_crop(struct file *file, void *fh, |
| 727 | struct v4l2_crop *a) |
| 728 | { |
| 729 | struct soc_camera_file *icf = file->private_data; |
| 730 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 731 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 732 | int ret; |
| 733 | |
| 734 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 735 | return -EINVAL; |
| 736 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 737 | /* Cropping is allowed during a running capture, guard consistency */ |
| 738 | mutex_lock(&icf->vb_vidq.vb_lock); |
| 739 | |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 740 | ret = ici->ops->set_crop(icd, &a->c); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 741 | if (!ret) { |
| 742 | icd->width = a->c.width; |
| 743 | icd->height = a->c.height; |
| 744 | icd->x_current = a->c.left; |
| 745 | icd->y_current = a->c.top; |
| 746 | } |
| 747 | |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 748 | mutex_unlock(&icf->vb_vidq.vb_lock); |
| 749 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static int soc_camera_g_chip_ident(struct file *file, void *fh, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 754 | struct v4l2_dbg_chip_ident *id) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 755 | { |
| 756 | struct soc_camera_file *icf = file->private_data; |
| 757 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 758 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 759 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 760 | return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_chip_ident, id); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 764 | static int soc_camera_g_register(struct file *file, void *fh, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 765 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 766 | { |
| 767 | struct soc_camera_file *icf = file->private_data; |
| 768 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 769 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 770 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 771 | return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, g_register, reg); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static int soc_camera_s_register(struct file *file, void *fh, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 775 | struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 776 | { |
| 777 | struct soc_camera_file *icf = file->private_data; |
| 778 | struct soc_camera_device *icd = icf->icd; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 779 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 780 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 781 | return v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, core, s_register, reg); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 782 | } |
| 783 | #endif |
| 784 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 785 | /* So far this function cannot fail */ |
| 786 | static void scan_add_host(struct soc_camera_host *ici) |
| 787 | { |
| 788 | struct soc_camera_device *icd; |
| 789 | |
| 790 | mutex_lock(&list_lock); |
| 791 | |
| 792 | list_for_each_entry(icd, &devices, list) { |
| 793 | if (icd->iface == ici->nr) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 794 | int ret; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 795 | icd->dev.parent = ici->v4l2_dev.dev; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 796 | dev_set_name(&icd->dev, "%u-%u", icd->iface, |
| 797 | icd->devnum); |
| 798 | ret = device_register(&icd->dev); |
| 799 | if (ret < 0) { |
| 800 | icd->dev.parent = NULL; |
| 801 | dev_err(&icd->dev, |
| 802 | "Cannot register device: %d\n", ret); |
| 803 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | mutex_unlock(&list_lock); |
| 808 | } |
| 809 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 810 | #ifdef CONFIG_I2C_BOARDINFO |
| 811 | static int soc_camera_init_i2c(struct soc_camera_device *icd, |
| 812 | struct soc_camera_link *icl) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 813 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 814 | struct i2c_client *client; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 815 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 816 | struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 817 | struct v4l2_subdev *subdev; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 818 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 819 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 820 | if (!adap) { |
| 821 | ret = -ENODEV; |
| 822 | dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n", |
| 823 | icl->i2c_adapter_id); |
| 824 | goto ei2cga; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 825 | } |
| 826 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 827 | icl->board_info->platform_data = icd; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 828 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 829 | subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap, |
| 830 | icl->module_name, icl->board_info, NULL); |
| 831 | if (!subdev) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 832 | ret = -ENOMEM; |
| 833 | goto ei2cnd; |
| 834 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 835 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 836 | subdev->grp_id = (__u32)icd; |
| 837 | client = subdev->priv; |
| 838 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 839 | /* Use to_i2c_client(dev) to recover the i2c client */ |
| 840 | dev_set_drvdata(&icd->dev, &client->dev); |
| 841 | |
| 842 | return 0; |
| 843 | ei2cnd: |
| 844 | i2c_put_adapter(adap); |
| 845 | ei2cga: |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 849 | static void soc_camera_free_i2c(struct soc_camera_device *icd) |
| 850 | { |
| 851 | struct i2c_client *client = |
| 852 | to_i2c_client(to_soc_camera_control(icd)); |
| 853 | dev_set_drvdata(&icd->dev, NULL); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 854 | v4l2_device_unregister_subdev(i2c_get_clientdata(client)); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 855 | i2c_unregister_device(client); |
| 856 | i2c_put_adapter(client->adapter); |
| 857 | } |
| 858 | #else |
| 859 | #define soc_camera_init_i2c(icd, icl) (-ENODEV) |
| 860 | #define soc_camera_free_i2c(icd) do {} while (0) |
| 861 | #endif |
| 862 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 863 | static int soc_camera_video_start(struct soc_camera_device *icd); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 864 | static int video_dev_create(struct soc_camera_device *icd); |
| 865 | /* Called during host-driver probe */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 866 | static int soc_camera_probe(struct device *dev) |
| 867 | { |
| 868 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 869 | struct soc_camera_host *ici = to_soc_camera_host(dev->parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 870 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 871 | struct device *control = NULL; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 872 | int ret; |
| 873 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 874 | dev_info(dev, "Probing %s\n", dev_name(dev)); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 875 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 876 | if (icl->power) { |
| 877 | ret = icl->power(icd->pdev, 1); |
| 878 | if (ret < 0) { |
| 879 | dev_err(dev, |
| 880 | "Platform failed to power-on the camera.\n"); |
| 881 | goto epower; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* The camera could have been already on, try to reset */ |
| 886 | if (icl->reset) |
| 887 | icl->reset(icd->pdev); |
| 888 | |
| 889 | ret = ici->ops->add(icd); |
| 890 | if (ret < 0) |
| 891 | goto eadd; |
| 892 | |
| 893 | /* Must have icd->vdev before registering the device */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 894 | ret = video_dev_create(icd); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 895 | if (ret < 0) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 896 | goto evdc; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 897 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 898 | /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ |
| 899 | if (icl->board_info) { |
| 900 | ret = soc_camera_init_i2c(icd, icl); |
| 901 | if (ret < 0) |
| 902 | goto eadddev; |
| 903 | } else if (!icl->add_device || !icl->del_device) { |
| 904 | ret = -EINVAL; |
| 905 | goto eadddev; |
| 906 | } else { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 907 | if (icl->module_name) |
| 908 | ret = request_module(icl->module_name); |
| 909 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 910 | ret = icl->add_device(icl, &icd->dev); |
| 911 | if (ret < 0) |
| 912 | goto eadddev; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 913 | |
| 914 | /* FIXME: this is racy, have to use driver-binding notification */ |
| 915 | control = to_soc_camera_control(icd); |
| 916 | if (!control || !control->driver || |
| 917 | !try_module_get(control->driver->owner)) { |
| 918 | icl->del_device(icl); |
| 919 | goto enodrv; |
| 920 | } |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 921 | } |
| 922 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 923 | /* ..._video_start() will create a device node, so we have to protect */ |
| 924 | mutex_lock(&icd->video_lock); |
| 925 | |
| 926 | ret = soc_camera_video_start(icd); |
| 927 | if (ret < 0) |
| 928 | goto evidstart; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 929 | |
| 930 | /* Do we have to sysfs_remove_link() before device_unregister()? */ |
| 931 | if (to_soc_camera_control(icd) && |
| 932 | sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj, |
| 933 | "control")) |
| 934 | dev_warn(&icd->dev, "Failed creating the control symlink\n"); |
| 935 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 936 | ici->ops->remove(icd); |
| 937 | |
| 938 | if (icl->power) |
| 939 | icl->power(icd->pdev, 0); |
| 940 | |
| 941 | mutex_unlock(&icd->video_lock); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 945 | evidstart: |
| 946 | mutex_unlock(&icd->video_lock); |
| 947 | if (icl->board_info) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 948 | soc_camera_free_i2c(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 949 | } else { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 950 | icl->del_device(icl); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 951 | module_put(control->driver->owner); |
| 952 | } |
| 953 | enodrv: |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 954 | eadddev: |
| 955 | video_device_release(icd->vdev); |
| 956 | evdc: |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 957 | ici->ops->remove(icd); |
| 958 | eadd: |
| 959 | if (icl->power) |
| 960 | icl->power(icd->pdev, 0); |
| 961 | epower: |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | /* This is called on device_unregister, which only means we have to disconnect |
| 966 | * from the host, but not remove ourselves from the device list */ |
| 967 | static int soc_camera_remove(struct device *dev) |
| 968 | { |
| 969 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 970 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
| 971 | struct video_device *vdev = icd->vdev; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 972 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 973 | BUG_ON(!dev->parent); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 974 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 975 | if (vdev) { |
| 976 | mutex_lock(&icd->video_lock); |
| 977 | video_unregister_device(vdev); |
| 978 | icd->vdev = NULL; |
| 979 | mutex_unlock(&icd->video_lock); |
| 980 | } |
| 981 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 982 | if (icl->board_info) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 983 | soc_camera_free_i2c(icd); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 984 | } else { |
| 985 | struct device_driver *drv = to_soc_camera_control(icd) ? |
| 986 | to_soc_camera_control(icd)->driver : NULL; |
| 987 | if (drv) { |
| 988 | icl->del_device(icl); |
| 989 | module_put(drv->owner); |
| 990 | } |
| 991 | } |
Guennadi Liakhovetski | 025c18a | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 992 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | |
Robert Jarzmik | 2e52106 | 2008-08-01 20:14:50 -0300 | [diff] [blame] | 996 | static int soc_camera_suspend(struct device *dev, pm_message_t state) |
| 997 | { |
| 998 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
| 999 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1000 | int ret = 0; |
| 1001 | |
| 1002 | if (ici->ops->suspend) |
| 1003 | ret = ici->ops->suspend(icd, state); |
| 1004 | |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | static int soc_camera_resume(struct device *dev) |
| 1009 | { |
| 1010 | struct soc_camera_device *icd = to_soc_camera_dev(dev); |
| 1011 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
| 1012 | int ret = 0; |
| 1013 | |
| 1014 | if (ici->ops->resume) |
| 1015 | ret = ici->ops->resume(icd); |
| 1016 | |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1020 | static struct bus_type soc_camera_bus_type = { |
| 1021 | .name = "soc-camera", |
| 1022 | .probe = soc_camera_probe, |
| 1023 | .remove = soc_camera_remove, |
Robert Jarzmik | 2e52106 | 2008-08-01 20:14:50 -0300 | [diff] [blame] | 1024 | .suspend = soc_camera_suspend, |
| 1025 | .resume = soc_camera_resume, |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1026 | }; |
| 1027 | |
| 1028 | static struct device_driver ic_drv = { |
| 1029 | .name = "camera", |
| 1030 | .bus = &soc_camera_bus_type, |
| 1031 | .owner = THIS_MODULE, |
| 1032 | }; |
| 1033 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1034 | static void dummy_release(struct device *dev) |
| 1035 | { |
| 1036 | } |
| 1037 | |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 1038 | int soc_camera_host_register(struct soc_camera_host *ici) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1039 | { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1040 | struct soc_camera_host *ix; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1041 | int ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1042 | |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1043 | if (!ici || !ici->ops || |
| 1044 | !ici->ops->try_fmt || |
| 1045 | !ici->ops->set_fmt || |
Guennadi Liakhovetski | 09e231b | 2009-03-13 06:08:20 -0300 | [diff] [blame] | 1046 | !ici->ops->set_crop || |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1047 | !ici->ops->set_bus_param || |
| 1048 | !ici->ops->querycap || |
| 1049 | !ici->ops->init_videobuf || |
| 1050 | !ici->ops->reqbufs || |
| 1051 | !ici->ops->add || |
| 1052 | !ici->ops->remove || |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1053 | !ici->ops->poll || |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1054 | !ici->v4l2_dev.dev) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1055 | return -EINVAL; |
| 1056 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1057 | mutex_lock(&list_lock); |
| 1058 | list_for_each_entry(ix, &hosts, list) { |
| 1059 | if (ix->nr == ici->nr) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1060 | ret = -EBUSY; |
| 1061 | goto edevreg; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1065 | ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev); |
| 1066 | if (ret < 0) |
| 1067 | goto edevreg; |
Guennadi Liakhovetski | eff505f | 2009-04-24 12:55:48 -0300 | [diff] [blame] | 1068 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1069 | list_add_tail(&ici->list, &hosts); |
| 1070 | mutex_unlock(&list_lock); |
| 1071 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1072 | scan_add_host(ici); |
| 1073 | |
| 1074 | return 0; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1075 | |
| 1076 | edevreg: |
| 1077 | mutex_unlock(&list_lock); |
| 1078 | return ret; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1079 | } |
| 1080 | EXPORT_SYMBOL(soc_camera_host_register); |
| 1081 | |
| 1082 | /* Unregister all clients! */ |
| 1083 | void soc_camera_host_unregister(struct soc_camera_host *ici) |
| 1084 | { |
| 1085 | struct soc_camera_device *icd; |
| 1086 | |
| 1087 | mutex_lock(&list_lock); |
| 1088 | |
| 1089 | list_del(&ici->list); |
| 1090 | |
| 1091 | list_for_each_entry(icd, &devices, list) { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1092 | if (icd->iface == ici->nr) { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1093 | /* The bus->remove will be called */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1094 | device_unregister(&icd->dev); |
| 1095 | /* Not before device_unregister(), .remove |
Guennadi Liakhovetski | b8d9904 | 2008-04-04 13:41:25 -0300 | [diff] [blame] | 1096 | * needs parent to call ici->ops->remove() */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1097 | icd->dev.parent = NULL; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1098 | |
| 1099 | /* If the host module is loaded again, device_register() |
| 1100 | * would complain "already initialised" */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1101 | memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj)); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | mutex_unlock(&list_lock); |
| 1106 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1107 | v4l2_device_unregister(&ici->v4l2_dev); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1108 | } |
| 1109 | EXPORT_SYMBOL(soc_camera_host_unregister); |
| 1110 | |
| 1111 | /* Image capture device */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1112 | static int soc_camera_device_register(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1113 | { |
| 1114 | struct soc_camera_device *ix; |
| 1115 | int num = -1, i; |
| 1116 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1117 | for (i = 0; i < 256 && num < 0; i++) { |
| 1118 | num = i; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1119 | /* Check if this index is available on this interface */ |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1120 | list_for_each_entry(ix, &devices, list) { |
| 1121 | if (ix->iface == icd->iface && ix->devnum == i) { |
| 1122 | num = -1; |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (num < 0) |
| 1129 | /* ok, we have 256 cameras on this host... |
| 1130 | * man, stay reasonable... */ |
| 1131 | return -ENOMEM; |
| 1132 | |
| 1133 | icd->devnum = num; |
| 1134 | icd->dev.bus = &soc_camera_bus_type; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1135 | |
Guennadi Liakhovetski | 64f5905 | 2008-12-18 11:51:55 -0300 | [diff] [blame] | 1136 | icd->dev.release = dummy_release; |
| 1137 | icd->use_count = 0; |
| 1138 | icd->host_priv = NULL; |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 1139 | mutex_init(&icd->video_lock); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1140 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1141 | list_add_tail(&icd->list, &devices); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1142 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | static void soc_camera_device_unregister(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1147 | { |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1148 | list_del(&icd->list); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1149 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1150 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1151 | static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = { |
| 1152 | .vidioc_querycap = soc_camera_querycap, |
| 1153 | .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, |
| 1154 | .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, |
| 1155 | .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, |
| 1156 | .vidioc_enum_input = soc_camera_enum_input, |
| 1157 | .vidioc_g_input = soc_camera_g_input, |
| 1158 | .vidioc_s_input = soc_camera_s_input, |
| 1159 | .vidioc_s_std = soc_camera_s_std, |
| 1160 | .vidioc_reqbufs = soc_camera_reqbufs, |
| 1161 | .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap, |
| 1162 | .vidioc_querybuf = soc_camera_querybuf, |
| 1163 | .vidioc_qbuf = soc_camera_qbuf, |
| 1164 | .vidioc_dqbuf = soc_camera_dqbuf, |
| 1165 | .vidioc_streamon = soc_camera_streamon, |
| 1166 | .vidioc_streamoff = soc_camera_streamoff, |
| 1167 | .vidioc_queryctrl = soc_camera_queryctrl, |
| 1168 | .vidioc_g_ctrl = soc_camera_g_ctrl, |
| 1169 | .vidioc_s_ctrl = soc_camera_s_ctrl, |
| 1170 | .vidioc_cropcap = soc_camera_cropcap, |
| 1171 | .vidioc_g_crop = soc_camera_g_crop, |
| 1172 | .vidioc_s_crop = soc_camera_s_crop, |
| 1173 | .vidioc_g_chip_ident = soc_camera_g_chip_ident, |
| 1174 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1175 | .vidioc_g_register = soc_camera_g_register, |
| 1176 | .vidioc_s_register = soc_camera_s_register, |
| 1177 | #endif |
| 1178 | }; |
| 1179 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1180 | static int video_dev_create(struct soc_camera_device *icd) |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1181 | { |
| 1182 | struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1183 | struct video_device *vdev = video_device_alloc(); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1184 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1185 | if (!vdev) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1186 | return -ENOMEM; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1187 | |
| 1188 | strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name)); |
Guennadi Liakhovetski | 1c3bb74 | 2008-12-18 12:28:54 -0300 | [diff] [blame] | 1189 | |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 1190 | vdev->parent = &icd->dev; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1191 | vdev->current_norm = V4L2_STD_UNKNOWN; |
| 1192 | vdev->fops = &soc_camera_fops; |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1193 | vdev->ioctl_ops = &soc_camera_ioctl_ops; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1194 | vdev->release = video_device_release; |
| 1195 | vdev->minor = -1; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1196 | vdev->tvnorms = V4L2_STD_UNKNOWN; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1197 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1198 | icd->vdev = vdev; |
| 1199 | |
| 1200 | return 0; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1201 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1202 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1203 | /* |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1204 | * Called from soc_camera_probe() above (with .video_lock held???) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1205 | */ |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1206 | static int soc_camera_video_start(struct soc_camera_device *icd) |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1207 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1208 | const struct v4l2_queryctrl *qctrl; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1209 | int ret; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1210 | |
| 1211 | if (!icd->dev.parent) |
| 1212 | return -ENODEV; |
| 1213 | |
| 1214 | if (!icd->ops || |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1215 | !icd->ops->query_bus_param || |
| 1216 | !icd->ops->set_bus_param) |
| 1217 | return -EINVAL; |
| 1218 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1219 | ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, |
| 1220 | icd->vdev->minor); |
| 1221 | if (ret < 0) { |
| 1222 | dev_err(&icd->dev, "video_register_device failed: %d\n", ret); |
| 1223 | return ret; |
| 1224 | } |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1225 | |
| 1226 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN); |
| 1227 | icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0; |
| 1228 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); |
| 1229 | icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0; |
| 1230 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1231 | return 0; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1232 | } |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1233 | |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1234 | static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev) |
| 1235 | { |
| 1236 | struct soc_camera_link *icl = pdev->dev.platform_data; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1237 | struct soc_camera_device *icd; |
| 1238 | int ret; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1239 | |
| 1240 | if (!icl) |
| 1241 | return -EINVAL; |
| 1242 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1243 | icd = kzalloc(sizeof(*icd), GFP_KERNEL); |
| 1244 | if (!icd) |
| 1245 | return -ENOMEM; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1246 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1247 | icd->iface = icl->bus_id; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame^] | 1248 | icd->pdev = &pdev->dev; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1249 | platform_set_drvdata(pdev, icd); |
| 1250 | icd->dev.platform_data = icl; |
| 1251 | |
| 1252 | ret = soc_camera_device_register(icd); |
| 1253 | if (ret < 0) |
| 1254 | goto escdevreg; |
| 1255 | |
| 1256 | return 0; |
| 1257 | |
| 1258 | escdevreg: |
| 1259 | kfree(icd); |
| 1260 | |
| 1261 | return ret; |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1262 | } |
| 1263 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1264 | /* Only called on rmmod for each platform device, since they are not |
| 1265 | * hot-pluggable. Now we know, that all our users - hosts and devices have |
| 1266 | * been unloaded already */ |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1267 | static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev) |
| 1268 | { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1269 | struct soc_camera_device *icd = platform_get_drvdata(pdev); |
Guennadi Liakhovetski | c41deba | 2009-08-25 11:06:21 -0300 | [diff] [blame] | 1270 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1271 | if (!icd) |
| 1272 | return -EINVAL; |
| 1273 | |
| 1274 | soc_camera_device_unregister(icd); |
| 1275 | |
| 1276 | kfree(icd); |
| 1277 | |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static struct platform_driver __refdata soc_camera_pdrv = { |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1282 | .remove = __devexit_p(soc_camera_pdrv_remove), |
| 1283 | .driver = { |
| 1284 | .name = "soc-camera-pdrv", |
| 1285 | .owner = THIS_MODULE, |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1286 | }, |
| 1287 | }; |
| 1288 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1289 | static int __init soc_camera_init(void) |
| 1290 | { |
| 1291 | int ret = bus_register(&soc_camera_bus_type); |
| 1292 | if (ret) |
| 1293 | return ret; |
| 1294 | ret = driver_register(&ic_drv); |
| 1295 | if (ret) |
| 1296 | goto edrvr; |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1297 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 1298 | ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe); |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1299 | if (ret) |
| 1300 | goto epdr; |
| 1301 | |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1302 | return 0; |
| 1303 | |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1304 | epdr: |
| 1305 | driver_unregister(&ic_drv); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1306 | edrvr: |
| 1307 | bus_unregister(&soc_camera_bus_type); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | static void __exit soc_camera_exit(void) |
| 1312 | { |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1313 | platform_driver_unregister(&soc_camera_pdrv); |
Guennadi Liakhovetski | e55222e | 2008-04-22 14:42:03 -0300 | [diff] [blame] | 1314 | driver_unregister(&ic_drv); |
| 1315 | bus_unregister(&soc_camera_bus_type); |
| 1316 | } |
| 1317 | |
| 1318 | module_init(soc_camera_init); |
| 1319 | module_exit(soc_camera_exit); |
| 1320 | |
| 1321 | MODULE_DESCRIPTION("Image capture bus driver"); |
| 1322 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 1323 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | 0fd327b | 2009-05-07 13:25:32 -0300 | [diff] [blame] | 1324 | MODULE_ALIAS("platform:soc-camera-pdrv"); |