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