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