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