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