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