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