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