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