Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Samsung TV Mixer driver |
| 3 | * |
| 4 | * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. |
| 5 | * |
| 6 | * Tomasz Stanislawski, <t.stanislaws@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published |
| 10 | * by the Free Software Foundation. either version 2 of the License, |
| 11 | * or (at your option) any later version |
| 12 | */ |
| 13 | |
Sachin Kamat | f5c9903 | 2012-06-11 06:13:52 -0300 | [diff] [blame] | 14 | #define pr_fmt(fmt) "s5p-tv (mixer): " fmt |
| 15 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 16 | #include "mixer.h" |
| 17 | |
| 18 | #include <media/v4l2-ioctl.h> |
| 19 | #include <linux/videodev2.h> |
| 20 | #include <linux/mm.h> |
Marek Szyprowski | fc8ac77 | 2011-11-21 05:02:38 -0300 | [diff] [blame] | 21 | #include <linux/module.h> |
Sachin Kamat | 3e58ac1 | 2013-02-07 02:55:55 -0300 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 23 | #include <linux/timer.h> |
| 24 | #include <media/videobuf2-dma-contig.h> |
| 25 | |
| 26 | static int find_reg_callback(struct device *dev, void *p) |
| 27 | { |
| 28 | struct v4l2_subdev **sd = p; |
| 29 | |
| 30 | *sd = dev_get_drvdata(dev); |
| 31 | /* non-zero value stops iteration */ |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | static struct v4l2_subdev *find_and_register_subdev( |
| 36 | struct mxr_device *mdev, char *module_name) |
| 37 | { |
| 38 | struct device_driver *drv; |
| 39 | struct v4l2_subdev *sd = NULL; |
| 40 | int ret; |
| 41 | |
| 42 | /* TODO: add waiting until probe is finished */ |
| 43 | drv = driver_find(module_name, &platform_bus_type); |
| 44 | if (!drv) { |
| 45 | mxr_warn(mdev, "module %s is missing\n", module_name); |
| 46 | return NULL; |
| 47 | } |
| 48 | /* driver refcnt is increased, it is safe to iterate over devices */ |
| 49 | ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback); |
| 50 | /* ret == 0 means that find_reg_callback was never executed */ |
| 51 | if (sd == NULL) { |
| 52 | mxr_warn(mdev, "module %s provides no subdev!\n", module_name); |
| 53 | goto done; |
| 54 | } |
| 55 | /* v4l2_device_register_subdev detects if sd is NULL */ |
| 56 | ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd); |
| 57 | if (ret) { |
| 58 | mxr_warn(mdev, "failed to register subdev %s\n", sd->name); |
| 59 | sd = NULL; |
| 60 | } |
| 61 | |
| 62 | done: |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 63 | return sd; |
| 64 | } |
| 65 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 66 | int mxr_acquire_video(struct mxr_device *mdev, |
| 67 | struct mxr_output_conf *output_conf, int output_count) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 68 | { |
| 69 | struct device *dev = mdev->dev; |
| 70 | struct v4l2_device *v4l2_dev = &mdev->v4l2_dev; |
| 71 | int i; |
| 72 | int ret = 0; |
| 73 | struct v4l2_subdev *sd; |
| 74 | |
| 75 | strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name)); |
| 76 | /* prepare context for V4L2 device */ |
| 77 | ret = v4l2_device_register(dev, v4l2_dev); |
| 78 | if (ret) { |
| 79 | mxr_err(mdev, "could not register v4l2 device.\n"); |
| 80 | goto fail; |
| 81 | } |
| 82 | |
| 83 | mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev); |
Wei Yongjun | 6a2f18c | 2013-05-13 01:49:13 -0300 | [diff] [blame] | 84 | if (IS_ERR(mdev->alloc_ctx)) { |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 85 | mxr_err(mdev, "could not acquire vb2 allocator\n"); |
Wei Yongjun | 6a2f18c | 2013-05-13 01:49:13 -0300 | [diff] [blame] | 86 | ret = PTR_ERR(mdev->alloc_ctx); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 87 | goto fail_v4l2_dev; |
| 88 | } |
| 89 | |
| 90 | /* registering outputs */ |
| 91 | mdev->output_cnt = 0; |
| 92 | for (i = 0; i < output_count; ++i) { |
| 93 | struct mxr_output_conf *conf = &output_conf[i]; |
| 94 | struct mxr_output *out; |
| 95 | |
| 96 | sd = find_and_register_subdev(mdev, conf->module_name); |
| 97 | /* trying to register next output */ |
| 98 | if (sd == NULL) |
| 99 | continue; |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 100 | out = kzalloc(sizeof(*out), GFP_KERNEL); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 101 | if (out == NULL) { |
| 102 | mxr_err(mdev, "no memory for '%s'\n", |
| 103 | conf->output_name); |
| 104 | ret = -ENOMEM; |
| 105 | /* registered subdevs are removed in fail_v4l2_dev */ |
| 106 | goto fail_output; |
| 107 | } |
| 108 | strlcpy(out->name, conf->output_name, sizeof(out->name)); |
| 109 | out->sd = sd; |
| 110 | out->cookie = conf->cookie; |
| 111 | mdev->output[mdev->output_cnt++] = out; |
| 112 | mxr_info(mdev, "added output '%s' from module '%s'\n", |
| 113 | conf->output_name, conf->module_name); |
| 114 | /* checking if maximal number of outputs is reached */ |
| 115 | if (mdev->output_cnt >= MXR_MAX_OUTPUTS) |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | if (mdev->output_cnt == 0) { |
| 120 | mxr_err(mdev, "failed to register any output\n"); |
| 121 | ret = -ENODEV; |
| 122 | /* skipping fail_output because there is nothing to free */ |
| 123 | goto fail_vb2_allocator; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | |
| 128 | fail_output: |
| 129 | /* kfree is NULL-safe */ |
| 130 | for (i = 0; i < mdev->output_cnt; ++i) |
| 131 | kfree(mdev->output[i]); |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 132 | memset(mdev->output, 0, sizeof(mdev->output)); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 133 | |
| 134 | fail_vb2_allocator: |
| 135 | /* freeing allocator context */ |
| 136 | vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx); |
| 137 | |
| 138 | fail_v4l2_dev: |
| 139 | /* NOTE: automatically unregister all subdevs */ |
| 140 | v4l2_device_unregister(v4l2_dev); |
| 141 | |
| 142 | fail: |
| 143 | return ret; |
| 144 | } |
| 145 | |
Sachin Kamat | e6364a9 | 2012-03-12 03:13:34 -0300 | [diff] [blame] | 146 | void mxr_release_video(struct mxr_device *mdev) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 147 | { |
| 148 | int i; |
| 149 | |
| 150 | /* kfree is NULL-safe */ |
| 151 | for (i = 0; i < mdev->output_cnt; ++i) |
| 152 | kfree(mdev->output[i]); |
| 153 | |
| 154 | vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx); |
| 155 | v4l2_device_unregister(&mdev->v4l2_dev); |
| 156 | } |
| 157 | |
| 158 | static int mxr_querycap(struct file *file, void *priv, |
| 159 | struct v4l2_capability *cap) |
| 160 | { |
| 161 | struct mxr_layer *layer = video_drvdata(file); |
| 162 | |
| 163 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 164 | |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 165 | strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver)); |
| 166 | strlcpy(cap->card, layer->vfd.name, sizeof(cap->card)); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 167 | sprintf(cap->bus_info, "%d", layer->idx); |
Sylwester Nawrocki | 7208507 | 2012-09-24 06:03:04 -0300 | [diff] [blame] | 168 | cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE; |
| 169 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 174 | static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 175 | { |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 176 | mxr_dbg(mdev, "src.full_size = (%u, %u)\n", |
| 177 | geo->src.full_width, geo->src.full_height); |
| 178 | mxr_dbg(mdev, "src.size = (%u, %u)\n", |
| 179 | geo->src.width, geo->src.height); |
| 180 | mxr_dbg(mdev, "src.offset = (%u, %u)\n", |
| 181 | geo->src.x_offset, geo->src.y_offset); |
| 182 | mxr_dbg(mdev, "dst.full_size = (%u, %u)\n", |
| 183 | geo->dst.full_width, geo->dst.full_height); |
| 184 | mxr_dbg(mdev, "dst.size = (%u, %u)\n", |
| 185 | geo->dst.width, geo->dst.height); |
| 186 | mxr_dbg(mdev, "dst.offset = (%u, %u)\n", |
| 187 | geo->dst.x_offset, geo->dst.y_offset); |
| 188 | mxr_dbg(mdev, "ratio = (%u, %u)\n", |
| 189 | geo->x_ratio, geo->y_ratio); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void mxr_layer_default_geo(struct mxr_layer *layer) |
| 193 | { |
| 194 | struct mxr_device *mdev = layer->mdev; |
| 195 | struct v4l2_mbus_framefmt mbus_fmt; |
| 196 | |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 197 | memset(&layer->geo, 0, sizeof(layer->geo)); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 198 | |
| 199 | mxr_get_mbus_fmt(mdev, &mbus_fmt); |
| 200 | |
| 201 | layer->geo.dst.full_width = mbus_fmt.width; |
| 202 | layer->geo.dst.full_height = mbus_fmt.height; |
| 203 | layer->geo.dst.width = layer->geo.dst.full_width; |
| 204 | layer->geo.dst.height = layer->geo.dst.full_height; |
| 205 | layer->geo.dst.field = mbus_fmt.field; |
| 206 | |
| 207 | layer->geo.src.full_width = mbus_fmt.width; |
| 208 | layer->geo.src.full_height = mbus_fmt.height; |
| 209 | layer->geo.src.width = layer->geo.src.full_width; |
| 210 | layer->geo.src.height = layer->geo.src.full_height; |
| 211 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 212 | mxr_geometry_dump(mdev, &layer->geo); |
| 213 | layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0); |
| 214 | mxr_geometry_dump(mdev, &layer->geo); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 215 | } |
| 216 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 217 | static void mxr_layer_update_output(struct mxr_layer *layer) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 218 | { |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 219 | struct mxr_device *mdev = layer->mdev; |
| 220 | struct v4l2_mbus_framefmt mbus_fmt; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 221 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 222 | mxr_get_mbus_fmt(mdev, &mbus_fmt); |
| 223 | /* checking if update is needed */ |
| 224 | if (layer->geo.dst.full_width == mbus_fmt.width && |
| 225 | layer->geo.dst.full_height == mbus_fmt.width) |
| 226 | return; |
| 227 | |
| 228 | layer->geo.dst.full_width = mbus_fmt.width; |
| 229 | layer->geo.dst.full_height = mbus_fmt.height; |
| 230 | layer->geo.dst.field = mbus_fmt.field; |
| 231 | layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0); |
| 232 | |
| 233 | mxr_geometry_dump(mdev, &layer->geo); |
| 234 | } |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 235 | |
| 236 | static const struct mxr_format *find_format_by_fourcc( |
| 237 | struct mxr_layer *layer, unsigned long fourcc); |
| 238 | static const struct mxr_format *find_format_by_index( |
| 239 | struct mxr_layer *layer, unsigned long index); |
| 240 | |
| 241 | static int mxr_enum_fmt(struct file *file, void *priv, |
| 242 | struct v4l2_fmtdesc *f) |
| 243 | { |
| 244 | struct mxr_layer *layer = video_drvdata(file); |
| 245 | struct mxr_device *mdev = layer->mdev; |
| 246 | const struct mxr_format *fmt; |
| 247 | |
| 248 | mxr_dbg(mdev, "%s\n", __func__); |
| 249 | fmt = find_format_by_index(layer, f->index); |
| 250 | if (fmt == NULL) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 254 | f->pixelformat = fmt->fourcc; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 259 | static unsigned int divup(unsigned int divident, unsigned int divisor) |
| 260 | { |
| 261 | return (divident + divisor - 1) / divisor; |
| 262 | } |
| 263 | |
| 264 | unsigned long mxr_get_plane_size(const struct mxr_block *blk, |
| 265 | unsigned int width, unsigned int height) |
| 266 | { |
| 267 | unsigned int bl_width = divup(width, blk->width); |
| 268 | unsigned int bl_height = divup(height, blk->height); |
| 269 | |
| 270 | return bl_width * bl_height * blk->size; |
| 271 | } |
| 272 | |
| 273 | static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes, |
| 274 | const struct mxr_format *fmt, u32 width, u32 height) |
| 275 | { |
| 276 | int i; |
| 277 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 278 | /* checking if nothing to fill */ |
| 279 | if (!planes) |
| 280 | return; |
| 281 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 282 | memset(planes, 0, sizeof(*planes) * fmt->num_subframes); |
| 283 | for (i = 0; i < fmt->num_planes; ++i) { |
| 284 | struct v4l2_plane_pix_format *plane = planes |
| 285 | + fmt->plane2subframe[i]; |
| 286 | const struct mxr_block *blk = &fmt->plane[i]; |
| 287 | u32 bl_width = divup(width, blk->width); |
| 288 | u32 bl_height = divup(height, blk->height); |
| 289 | u32 sizeimage = bl_width * bl_height * blk->size; |
| 290 | u16 bytesperline = bl_width * blk->size / blk->height; |
| 291 | |
| 292 | plane->sizeimage += sizeimage; |
| 293 | plane->bytesperline = max(plane->bytesperline, bytesperline); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static int mxr_g_fmt(struct file *file, void *priv, |
| 298 | struct v4l2_format *f) |
| 299 | { |
| 300 | struct mxr_layer *layer = video_drvdata(file); |
| 301 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
| 302 | |
| 303 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 304 | |
| 305 | pix->width = layer->geo.src.full_width; |
| 306 | pix->height = layer->geo.src.full_height; |
| 307 | pix->field = V4L2_FIELD_NONE; |
| 308 | pix->pixelformat = layer->fmt->fourcc; |
| 309 | pix->colorspace = layer->fmt->colorspace; |
| 310 | mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 315 | static int mxr_s_fmt(struct file *file, void *priv, |
| 316 | struct v4l2_format *f) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 317 | { |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 318 | struct mxr_layer *layer = video_drvdata(file); |
| 319 | const struct mxr_format *fmt; |
| 320 | struct v4l2_pix_format_mplane *pix; |
| 321 | struct mxr_device *mdev = layer->mdev; |
| 322 | struct mxr_geometry *geo = &layer->geo; |
| 323 | |
| 324 | mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__); |
| 325 | |
| 326 | pix = &f->fmt.pix_mp; |
| 327 | fmt = find_format_by_fourcc(layer, pix->pixelformat); |
| 328 | if (fmt == NULL) { |
| 329 | mxr_warn(mdev, "not recognized fourcc: %08x\n", |
| 330 | pix->pixelformat); |
| 331 | return -EINVAL; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 332 | } |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 333 | layer->fmt = fmt; |
| 334 | /* set source size to highest accepted value */ |
| 335 | geo->src.full_width = max(geo->dst.full_width, pix->width); |
| 336 | geo->src.full_height = max(geo->dst.full_height, pix->height); |
| 337 | layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0); |
| 338 | mxr_geometry_dump(mdev, &layer->geo); |
| 339 | /* set cropping to total visible screen */ |
| 340 | geo->src.width = pix->width; |
| 341 | geo->src.height = pix->height; |
| 342 | geo->src.x_offset = 0; |
| 343 | geo->src.y_offset = 0; |
| 344 | /* assure consistency of geometry */ |
| 345 | layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET); |
| 346 | mxr_geometry_dump(mdev, &layer->geo); |
| 347 | /* set full size to lowest possible value */ |
| 348 | geo->src.full_width = 0; |
| 349 | geo->src.full_height = 0; |
| 350 | layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0); |
| 351 | mxr_geometry_dump(mdev, &layer->geo); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 352 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 353 | /* returning results */ |
| 354 | mxr_g_fmt(file, priv, f); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 355 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 359 | static int mxr_g_selection(struct file *file, void *fh, |
| 360 | struct v4l2_selection *s) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 361 | { |
| 362 | struct mxr_layer *layer = video_drvdata(file); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 363 | struct mxr_geometry *geo = &layer->geo; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 364 | |
| 365 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 366 | |
| 367 | if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && |
| 368 | s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 369 | return -EINVAL; |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 370 | |
| 371 | switch (s->target) { |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 372 | case V4L2_SEL_TGT_CROP: |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 373 | s->r.left = geo->src.x_offset; |
| 374 | s->r.top = geo->src.y_offset; |
| 375 | s->r.width = geo->src.width; |
| 376 | s->r.height = geo->src.height; |
| 377 | break; |
| 378 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 379 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 380 | s->r.left = 0; |
| 381 | s->r.top = 0; |
| 382 | s->r.width = geo->src.full_width; |
| 383 | s->r.height = geo->src.full_height; |
| 384 | break; |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 385 | case V4L2_SEL_TGT_COMPOSE: |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 386 | case V4L2_SEL_TGT_COMPOSE_PADDED: |
| 387 | s->r.left = geo->dst.x_offset; |
| 388 | s->r.top = geo->dst.y_offset; |
| 389 | s->r.width = geo->dst.width; |
| 390 | s->r.height = geo->dst.height; |
| 391 | break; |
| 392 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 393 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 394 | s->r.left = 0; |
| 395 | s->r.top = 0; |
| 396 | s->r.width = geo->dst.full_width; |
| 397 | s->r.height = geo->dst.full_height; |
| 398 | break; |
| 399 | default: |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 406 | /* returns 1 if rectangle 'a' is inside 'b' */ |
| 407 | static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b) |
| 408 | { |
| 409 | if (a->left < b->left) |
| 410 | return 0; |
| 411 | if (a->top < b->top) |
| 412 | return 0; |
| 413 | if (a->left + a->width > b->left + b->width) |
| 414 | return 0; |
| 415 | if (a->top + a->height > b->top + b->height) |
| 416 | return 0; |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | static int mxr_s_selection(struct file *file, void *fh, |
| 421 | struct v4l2_selection *s) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 422 | { |
| 423 | struct mxr_layer *layer = video_drvdata(file); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 424 | struct mxr_geometry *geo = &layer->geo; |
| 425 | struct mxr_crop *target = NULL; |
| 426 | enum mxr_geometry_stage stage; |
| 427 | struct mxr_geometry tmp; |
| 428 | struct v4l2_rect res; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 429 | |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 430 | memset(&res, 0, sizeof(res)); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 431 | |
| 432 | mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__, |
| 433 | s->r.width, s->r.height, s->r.left, s->r.top); |
| 434 | |
| 435 | if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && |
| 436 | s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 437 | return -EINVAL; |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 438 | |
| 439 | switch (s->target) { |
| 440 | /* ignore read-only targets */ |
| 441 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 442 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 443 | res.width = geo->src.full_width; |
| 444 | res.height = geo->src.full_height; |
| 445 | break; |
| 446 | |
| 447 | /* ignore read-only targets */ |
| 448 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 449 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 450 | res.width = geo->dst.full_width; |
| 451 | res.height = geo->dst.full_height; |
| 452 | break; |
| 453 | |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 454 | case V4L2_SEL_TGT_CROP: |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 455 | target = &geo->src; |
| 456 | stage = MXR_GEOMETRY_CROP; |
| 457 | break; |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 458 | case V4L2_SEL_TGT_COMPOSE: |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 459 | case V4L2_SEL_TGT_COMPOSE_PADDED: |
| 460 | target = &geo->dst; |
| 461 | stage = MXR_GEOMETRY_COMPOSE; |
| 462 | break; |
| 463 | default: |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | /* apply change and update geometry if needed */ |
| 467 | if (target) { |
| 468 | /* backup current geometry if setup fails */ |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 469 | memcpy(&tmp, geo, sizeof(tmp)); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 470 | |
| 471 | /* apply requested selection */ |
| 472 | target->x_offset = s->r.left; |
| 473 | target->y_offset = s->r.top; |
| 474 | target->width = s->r.width; |
| 475 | target->height = s->r.height; |
| 476 | |
| 477 | layer->ops.fix_geometry(layer, stage, s->flags); |
| 478 | |
| 479 | /* retrieve update selection rectangle */ |
| 480 | res.left = target->x_offset; |
| 481 | res.top = target->y_offset; |
| 482 | res.width = target->width; |
| 483 | res.height = target->height; |
| 484 | |
| 485 | mxr_geometry_dump(layer->mdev, &layer->geo); |
| 486 | } |
| 487 | |
| 488 | /* checking if the rectangle satisfies constraints */ |
| 489 | if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r)) |
| 490 | goto fail; |
| 491 | if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res)) |
| 492 | goto fail; |
| 493 | |
| 494 | /* return result rectangle */ |
| 495 | s->r = res; |
| 496 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 497 | return 0; |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 498 | fail: |
| 499 | /* restore old geometry, which is not touched if target is NULL */ |
| 500 | if (target) |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 501 | memcpy(geo, &tmp, sizeof(tmp)); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 502 | return -ERANGE; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 503 | } |
| 504 | |
Hans Verkuil | 8e42bf0 | 2013-03-04 09:30:48 -0300 | [diff] [blame] | 505 | static int mxr_enum_dv_timings(struct file *file, void *fh, |
| 506 | struct v4l2_enum_dv_timings *timings) |
| 507 | { |
| 508 | struct mxr_layer *layer = video_drvdata(file); |
| 509 | struct mxr_device *mdev = layer->mdev; |
| 510 | int ret; |
| 511 | |
| 512 | /* lock protects from changing sd_out */ |
| 513 | mutex_lock(&mdev->mutex); |
| 514 | ret = v4l2_subdev_call(to_outsd(mdev), video, enum_dv_timings, timings); |
| 515 | mutex_unlock(&mdev->mutex); |
| 516 | |
| 517 | return ret ? -EINVAL : 0; |
| 518 | } |
| 519 | |
| 520 | static int mxr_s_dv_timings(struct file *file, void *fh, |
| 521 | struct v4l2_dv_timings *timings) |
| 522 | { |
| 523 | struct mxr_layer *layer = video_drvdata(file); |
| 524 | struct mxr_device *mdev = layer->mdev; |
| 525 | int ret; |
| 526 | |
| 527 | /* lock protects from changing sd_out */ |
| 528 | mutex_lock(&mdev->mutex); |
| 529 | |
| 530 | /* timings change cannot be done while there is an entity |
Jonathan McCrohan | 39c1cb2 | 2013-10-20 21:34:01 -0300 | [diff] [blame^] | 531 | * dependent on output configuration |
Hans Verkuil | 8e42bf0 | 2013-03-04 09:30:48 -0300 | [diff] [blame] | 532 | */ |
| 533 | if (mdev->n_output > 0) { |
| 534 | mutex_unlock(&mdev->mutex); |
| 535 | return -EBUSY; |
| 536 | } |
| 537 | |
| 538 | ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings); |
| 539 | |
| 540 | mutex_unlock(&mdev->mutex); |
| 541 | |
| 542 | mxr_layer_update_output(layer); |
| 543 | |
| 544 | /* any failure should return EINVAL according to V4L2 doc */ |
| 545 | return ret ? -EINVAL : 0; |
| 546 | } |
| 547 | |
| 548 | static int mxr_g_dv_timings(struct file *file, void *fh, |
| 549 | struct v4l2_dv_timings *timings) |
| 550 | { |
| 551 | struct mxr_layer *layer = video_drvdata(file); |
| 552 | struct mxr_device *mdev = layer->mdev; |
| 553 | int ret; |
| 554 | |
| 555 | /* lock protects from changing sd_out */ |
| 556 | mutex_lock(&mdev->mutex); |
| 557 | ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings); |
| 558 | mutex_unlock(&mdev->mutex); |
| 559 | |
| 560 | return ret ? -EINVAL : 0; |
| 561 | } |
| 562 | |
| 563 | static int mxr_dv_timings_cap(struct file *file, void *fh, |
| 564 | struct v4l2_dv_timings_cap *cap) |
| 565 | { |
| 566 | struct mxr_layer *layer = video_drvdata(file); |
| 567 | struct mxr_device *mdev = layer->mdev; |
| 568 | int ret; |
| 569 | |
| 570 | /* lock protects from changing sd_out */ |
| 571 | mutex_lock(&mdev->mutex); |
| 572 | ret = v4l2_subdev_call(to_outsd(mdev), video, dv_timings_cap, cap); |
| 573 | mutex_unlock(&mdev->mutex); |
| 574 | |
| 575 | return ret ? -EINVAL : 0; |
| 576 | } |
| 577 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 578 | static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 579 | { |
| 580 | struct mxr_layer *layer = video_drvdata(file); |
| 581 | struct mxr_device *mdev = layer->mdev; |
| 582 | int ret; |
| 583 | |
| 584 | /* lock protects from changing sd_out */ |
| 585 | mutex_lock(&mdev->mutex); |
| 586 | |
| 587 | /* standard change cannot be done while there is an entity |
Jonathan McCrohan | 39c1cb2 | 2013-10-20 21:34:01 -0300 | [diff] [blame^] | 588 | * dependent on output configuration |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 589 | */ |
| 590 | if (mdev->n_output > 0) { |
| 591 | mutex_unlock(&mdev->mutex); |
| 592 | return -EBUSY; |
| 593 | } |
| 594 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 595 | ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 596 | |
| 597 | mutex_unlock(&mdev->mutex); |
| 598 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 599 | mxr_layer_update_output(layer); |
| 600 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 601 | return ret ? -EINVAL : 0; |
| 602 | } |
| 603 | |
| 604 | static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm) |
| 605 | { |
| 606 | struct mxr_layer *layer = video_drvdata(file); |
| 607 | struct mxr_device *mdev = layer->mdev; |
| 608 | int ret; |
| 609 | |
| 610 | /* lock protects from changing sd_out */ |
| 611 | mutex_lock(&mdev->mutex); |
| 612 | ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm); |
| 613 | mutex_unlock(&mdev->mutex); |
| 614 | |
| 615 | return ret ? -EINVAL : 0; |
| 616 | } |
| 617 | |
| 618 | static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a) |
| 619 | { |
| 620 | struct mxr_layer *layer = video_drvdata(file); |
| 621 | struct mxr_device *mdev = layer->mdev; |
| 622 | struct mxr_output *out; |
| 623 | struct v4l2_subdev *sd; |
| 624 | |
| 625 | if (a->index >= mdev->output_cnt) |
| 626 | return -EINVAL; |
| 627 | out = mdev->output[a->index]; |
| 628 | BUG_ON(out == NULL); |
| 629 | sd = out->sd; |
| 630 | strlcpy(a->name, out->name, sizeof(a->name)); |
| 631 | |
| 632 | /* try to obtain supported tv norms */ |
| 633 | v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std); |
| 634 | a->capabilities = 0; |
Hans Verkuil | 8e42bf0 | 2013-03-04 09:30:48 -0300 | [diff] [blame] | 635 | if (sd->ops->video && sd->ops->video->s_dv_timings) |
| 636 | a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 637 | if (sd->ops->video && sd->ops->video->s_std_output) |
| 638 | a->capabilities |= V4L2_OUT_CAP_STD; |
| 639 | a->type = V4L2_OUTPUT_TYPE_ANALOG; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int mxr_s_output(struct file *file, void *fh, unsigned int i) |
| 645 | { |
| 646 | struct video_device *vfd = video_devdata(file); |
| 647 | struct mxr_layer *layer = video_drvdata(file); |
| 648 | struct mxr_device *mdev = layer->mdev; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 649 | |
| 650 | if (i >= mdev->output_cnt || mdev->output[i] == NULL) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | mutex_lock(&mdev->mutex); |
| 654 | if (mdev->n_output > 0) { |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 655 | mutex_unlock(&mdev->mutex); |
| 656 | return -EBUSY; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 657 | } |
| 658 | mdev->current_output = i; |
| 659 | vfd->tvnorms = 0; |
| 660 | v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output, |
| 661 | &vfd->tvnorms); |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 662 | mutex_unlock(&mdev->mutex); |
| 663 | |
| 664 | /* update layers geometry */ |
| 665 | mxr_layer_update_output(layer); |
| 666 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 667 | mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms); |
| 668 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 669 | return 0; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static int mxr_g_output(struct file *file, void *fh, unsigned int *p) |
| 673 | { |
| 674 | struct mxr_layer *layer = video_drvdata(file); |
| 675 | struct mxr_device *mdev = layer->mdev; |
| 676 | |
| 677 | mutex_lock(&mdev->mutex); |
| 678 | *p = mdev->current_output; |
| 679 | mutex_unlock(&mdev->mutex); |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static int mxr_reqbufs(struct file *file, void *priv, |
| 685 | struct v4l2_requestbuffers *p) |
| 686 | { |
| 687 | struct mxr_layer *layer = video_drvdata(file); |
| 688 | |
| 689 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 690 | return vb2_reqbufs(&layer->vb_queue, p); |
| 691 | } |
| 692 | |
| 693 | static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 694 | { |
| 695 | struct mxr_layer *layer = video_drvdata(file); |
| 696 | |
| 697 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 698 | return vb2_querybuf(&layer->vb_queue, p); |
| 699 | } |
| 700 | |
| 701 | static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 702 | { |
| 703 | struct mxr_layer *layer = video_drvdata(file); |
| 704 | |
| 705 | mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index); |
| 706 | return vb2_qbuf(&layer->vb_queue, p); |
| 707 | } |
| 708 | |
| 709 | static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 710 | { |
| 711 | struct mxr_layer *layer = video_drvdata(file); |
| 712 | |
| 713 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 714 | return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK); |
| 715 | } |
| 716 | |
Tomasz Stanislawski | 3c44efd | 2012-06-14 11:32:27 -0300 | [diff] [blame] | 717 | static int mxr_expbuf(struct file *file, void *priv, |
| 718 | struct v4l2_exportbuffer *eb) |
| 719 | { |
| 720 | struct mxr_layer *layer = video_drvdata(file); |
| 721 | |
| 722 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 723 | return vb2_expbuf(&layer->vb_queue, eb); |
| 724 | } |
| 725 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 726 | static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
| 727 | { |
| 728 | struct mxr_layer *layer = video_drvdata(file); |
| 729 | |
| 730 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 731 | return vb2_streamon(&layer->vb_queue, i); |
| 732 | } |
| 733 | |
| 734 | static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 735 | { |
| 736 | struct mxr_layer *layer = video_drvdata(file); |
| 737 | |
| 738 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 739 | return vb2_streamoff(&layer->vb_queue, i); |
| 740 | } |
| 741 | |
| 742 | static const struct v4l2_ioctl_ops mxr_ioctl_ops = { |
| 743 | .vidioc_querycap = mxr_querycap, |
| 744 | /* format handling */ |
Sylwester Nawrocki | 7208507 | 2012-09-24 06:03:04 -0300 | [diff] [blame] | 745 | .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 746 | .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt, |
| 747 | .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt, |
| 748 | /* buffer control */ |
| 749 | .vidioc_reqbufs = mxr_reqbufs, |
| 750 | .vidioc_querybuf = mxr_querybuf, |
| 751 | .vidioc_qbuf = mxr_qbuf, |
| 752 | .vidioc_dqbuf = mxr_dqbuf, |
Tomasz Stanislawski | 3c44efd | 2012-06-14 11:32:27 -0300 | [diff] [blame] | 753 | .vidioc_expbuf = mxr_expbuf, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 754 | /* Streaming control */ |
| 755 | .vidioc_streamon = mxr_streamon, |
| 756 | .vidioc_streamoff = mxr_streamoff, |
Hans Verkuil | 8e42bf0 | 2013-03-04 09:30:48 -0300 | [diff] [blame] | 757 | /* DV Timings functions */ |
| 758 | .vidioc_enum_dv_timings = mxr_enum_dv_timings, |
| 759 | .vidioc_s_dv_timings = mxr_s_dv_timings, |
| 760 | .vidioc_g_dv_timings = mxr_g_dv_timings, |
| 761 | .vidioc_dv_timings_cap = mxr_dv_timings_cap, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 762 | /* analog TV standard functions */ |
| 763 | .vidioc_s_std = mxr_s_std, |
| 764 | .vidioc_g_std = mxr_g_std, |
| 765 | /* Output handling */ |
| 766 | .vidioc_enum_output = mxr_enum_output, |
| 767 | .vidioc_s_output = mxr_s_output, |
| 768 | .vidioc_g_output = mxr_g_output, |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 769 | /* selection ioctls */ |
| 770 | .vidioc_g_selection = mxr_g_selection, |
| 771 | .vidioc_s_selection = mxr_s_selection, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 772 | }; |
| 773 | |
| 774 | static int mxr_video_open(struct file *file) |
| 775 | { |
| 776 | struct mxr_layer *layer = video_drvdata(file); |
| 777 | struct mxr_device *mdev = layer->mdev; |
| 778 | int ret = 0; |
| 779 | |
| 780 | mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 781 | if (mutex_lock_interruptible(&layer->mutex)) |
| 782 | return -ERESTARTSYS; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 783 | /* assure device probe is finished */ |
| 784 | wait_for_device_probe(); |
| 785 | /* creating context for file descriptor */ |
| 786 | ret = v4l2_fh_open(file); |
| 787 | if (ret) { |
| 788 | mxr_err(mdev, "v4l2_fh_open failed\n"); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 789 | goto unlock; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* leaving if layer is already initialized */ |
| 793 | if (!v4l2_fh_is_singular_file(file)) |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 794 | goto unlock; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 795 | |
| 796 | /* FIXME: should power be enabled on open? */ |
| 797 | ret = mxr_power_get(mdev); |
| 798 | if (ret) { |
| 799 | mxr_err(mdev, "power on failed\n"); |
| 800 | goto fail_fh_open; |
| 801 | } |
| 802 | |
| 803 | ret = vb2_queue_init(&layer->vb_queue); |
| 804 | if (ret != 0) { |
| 805 | mxr_err(mdev, "failed to initialize vb2 queue\n"); |
| 806 | goto fail_power; |
| 807 | } |
| 808 | /* set default format, first on the list */ |
| 809 | layer->fmt = layer->fmt_array[0]; |
| 810 | /* setup default geometry */ |
| 811 | mxr_layer_default_geo(layer); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 812 | mutex_unlock(&layer->mutex); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 813 | |
| 814 | return 0; |
| 815 | |
| 816 | fail_power: |
| 817 | mxr_power_put(mdev); |
| 818 | |
| 819 | fail_fh_open: |
| 820 | v4l2_fh_release(file); |
| 821 | |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 822 | unlock: |
| 823 | mutex_unlock(&layer->mutex); |
| 824 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | static unsigned int |
| 829 | mxr_video_poll(struct file *file, struct poll_table_struct *wait) |
| 830 | { |
| 831 | struct mxr_layer *layer = video_drvdata(file); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 832 | unsigned int res; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 833 | |
| 834 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 835 | |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 836 | mutex_lock(&layer->mutex); |
| 837 | res = vb2_poll(&layer->vb_queue, file, wait); |
| 838 | mutex_unlock(&layer->mutex); |
| 839 | return res; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma) |
| 843 | { |
| 844 | struct mxr_layer *layer = video_drvdata(file); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 845 | int ret; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 846 | |
| 847 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
| 848 | |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 849 | if (mutex_lock_interruptible(&layer->mutex)) |
| 850 | return -ERESTARTSYS; |
| 851 | ret = vb2_mmap(&layer->vb_queue, vma); |
| 852 | mutex_unlock(&layer->mutex); |
| 853 | return ret; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int mxr_video_release(struct file *file) |
| 857 | { |
| 858 | struct mxr_layer *layer = video_drvdata(file); |
| 859 | |
| 860 | mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 861 | mutex_lock(&layer->mutex); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 862 | if (v4l2_fh_is_singular_file(file)) { |
| 863 | vb2_queue_release(&layer->vb_queue); |
| 864 | mxr_power_put(layer->mdev); |
| 865 | } |
| 866 | v4l2_fh_release(file); |
Hans Verkuil | 00ccdc3 | 2012-06-24 07:02:01 -0300 | [diff] [blame] | 867 | mutex_unlock(&layer->mutex); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static const struct v4l2_file_operations mxr_fops = { |
| 872 | .owner = THIS_MODULE, |
| 873 | .open = mxr_video_open, |
| 874 | .poll = mxr_video_poll, |
| 875 | .mmap = mxr_video_mmap, |
| 876 | .release = mxr_video_release, |
| 877 | .unlocked_ioctl = video_ioctl2, |
| 878 | }; |
| 879 | |
Guennadi Liakhovetski | fc714e70 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 880 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt, |
| 881 | unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 882 | void *alloc_ctxs[]) |
| 883 | { |
| 884 | struct mxr_layer *layer = vb2_get_drv_priv(vq); |
| 885 | const struct mxr_format *fmt = layer->fmt; |
| 886 | int i; |
| 887 | struct mxr_device *mdev = layer->mdev; |
| 888 | struct v4l2_plane_pix_format planes[3]; |
| 889 | |
| 890 | mxr_dbg(mdev, "%s\n", __func__); |
| 891 | /* checking if format was configured */ |
| 892 | if (fmt == NULL) |
| 893 | return -EINVAL; |
| 894 | mxr_dbg(mdev, "fmt = %s\n", fmt->name); |
| 895 | mxr_mplane_fill(planes, fmt, layer->geo.src.full_width, |
| 896 | layer->geo.src.full_height); |
| 897 | |
| 898 | *nplanes = fmt->num_subframes; |
| 899 | for (i = 0; i < fmt->num_subframes; ++i) { |
| 900 | alloc_ctxs[i] = layer->mdev->alloc_ctx; |
Marek Szyprowski | c1bf9c6 | 2011-10-21 04:56:12 -0300 | [diff] [blame] | 901 | sizes[i] = planes[i].sizeimage; |
Tomasz Stanislawski | c31e3c4 | 2012-03-09 07:07:28 -0300 | [diff] [blame] | 902 | mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | if (*nbuffers == 0) |
| 906 | *nbuffers = 1; |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | static void buf_queue(struct vb2_buffer *vb) |
| 912 | { |
| 913 | struct mxr_buffer *buffer = container_of(vb, struct mxr_buffer, vb); |
| 914 | struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue); |
| 915 | struct mxr_device *mdev = layer->mdev; |
| 916 | unsigned long flags; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 917 | |
| 918 | spin_lock_irqsave(&layer->enq_slock, flags); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 919 | list_add_tail(&buffer->list, &layer->enq_list); |
| 920 | spin_unlock_irqrestore(&layer->enq_slock, flags); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 921 | |
| 922 | mxr_dbg(mdev, "queuing buffer\n"); |
| 923 | } |
| 924 | |
| 925 | static void wait_lock(struct vb2_queue *vq) |
| 926 | { |
| 927 | struct mxr_layer *layer = vb2_get_drv_priv(vq); |
| 928 | |
| 929 | mxr_dbg(layer->mdev, "%s\n", __func__); |
| 930 | mutex_lock(&layer->mutex); |
| 931 | } |
| 932 | |
| 933 | static void wait_unlock(struct vb2_queue *vq) |
| 934 | { |
| 935 | struct mxr_layer *layer = vb2_get_drv_priv(vq); |
| 936 | |
| 937 | mxr_dbg(layer->mdev, "%s\n", __func__); |
| 938 | mutex_unlock(&layer->mutex); |
| 939 | } |
| 940 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 941 | static int start_streaming(struct vb2_queue *vq, unsigned int count) |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 942 | { |
| 943 | struct mxr_layer *layer = vb2_get_drv_priv(vq); |
| 944 | struct mxr_device *mdev = layer->mdev; |
| 945 | unsigned long flags; |
| 946 | |
| 947 | mxr_dbg(mdev, "%s\n", __func__); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 948 | |
| 949 | if (count == 0) { |
| 950 | mxr_dbg(mdev, "no output buffers queued\n"); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 954 | /* block any changes in output configuration */ |
| 955 | mxr_output_get(mdev); |
| 956 | |
Tomasz Stanislawski | 0d066d3 | 2011-08-25 07:14:26 -0300 | [diff] [blame] | 957 | mxr_layer_update_output(layer); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 958 | layer->ops.format_set(layer); |
| 959 | /* enabling layer in hardware */ |
| 960 | spin_lock_irqsave(&layer->enq_slock, flags); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 961 | layer->state = MXR_LAYER_STREAMING; |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 962 | spin_unlock_irqrestore(&layer->enq_slock, flags); |
| 963 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 964 | layer->ops.stream_set(layer, MXR_ENABLE); |
| 965 | mxr_streamer_get(mdev); |
| 966 | |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static void mxr_watchdog(unsigned long arg) |
| 971 | { |
| 972 | struct mxr_layer *layer = (struct mxr_layer *) arg; |
| 973 | struct mxr_device *mdev = layer->mdev; |
| 974 | unsigned long flags; |
| 975 | |
| 976 | mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name); |
| 977 | |
| 978 | spin_lock_irqsave(&layer->enq_slock, flags); |
| 979 | |
| 980 | if (layer->update_buf == layer->shadow_buf) |
| 981 | layer->update_buf = NULL; |
| 982 | if (layer->update_buf) { |
| 983 | vb2_buffer_done(&layer->update_buf->vb, VB2_BUF_STATE_ERROR); |
| 984 | layer->update_buf = NULL; |
| 985 | } |
| 986 | if (layer->shadow_buf) { |
| 987 | vb2_buffer_done(&layer->shadow_buf->vb, VB2_BUF_STATE_ERROR); |
| 988 | layer->shadow_buf = NULL; |
| 989 | } |
| 990 | spin_unlock_irqrestore(&layer->enq_slock, flags); |
| 991 | } |
| 992 | |
| 993 | static int stop_streaming(struct vb2_queue *vq) |
| 994 | { |
| 995 | struct mxr_layer *layer = vb2_get_drv_priv(vq); |
| 996 | struct mxr_device *mdev = layer->mdev; |
| 997 | unsigned long flags; |
| 998 | struct timer_list watchdog; |
| 999 | struct mxr_buffer *buf, *buf_tmp; |
| 1000 | |
| 1001 | mxr_dbg(mdev, "%s\n", __func__); |
| 1002 | |
| 1003 | spin_lock_irqsave(&layer->enq_slock, flags); |
| 1004 | |
| 1005 | /* reset list */ |
| 1006 | layer->state = MXR_LAYER_STREAMING_FINISH; |
| 1007 | |
| 1008 | /* set all buffer to be done */ |
| 1009 | list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) { |
| 1010 | list_del(&buf->list); |
| 1011 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 1012 | } |
| 1013 | |
| 1014 | spin_unlock_irqrestore(&layer->enq_slock, flags); |
| 1015 | |
| 1016 | /* give 1 seconds to complete to complete last buffers */ |
| 1017 | setup_timer_on_stack(&watchdog, mxr_watchdog, |
| 1018 | (unsigned long)layer); |
| 1019 | mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000)); |
| 1020 | |
| 1021 | /* wait until all buffers are goes to done state */ |
| 1022 | vb2_wait_for_all_buffers(vq); |
| 1023 | |
| 1024 | /* stop timer if all synchronization is done */ |
| 1025 | del_timer_sync(&watchdog); |
| 1026 | destroy_timer_on_stack(&watchdog); |
| 1027 | |
| 1028 | /* stopping hardware */ |
| 1029 | spin_lock_irqsave(&layer->enq_slock, flags); |
| 1030 | layer->state = MXR_LAYER_IDLE; |
| 1031 | spin_unlock_irqrestore(&layer->enq_slock, flags); |
| 1032 | |
| 1033 | /* disabling layer in hardware */ |
| 1034 | layer->ops.stream_set(layer, MXR_DISABLE); |
| 1035 | /* remove one streamer */ |
| 1036 | mxr_streamer_put(mdev); |
| 1037 | /* allow changes in output configuration */ |
| 1038 | mxr_output_put(mdev); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static struct vb2_ops mxr_video_qops = { |
| 1043 | .queue_setup = queue_setup, |
| 1044 | .buf_queue = buf_queue, |
| 1045 | .wait_prepare = wait_unlock, |
| 1046 | .wait_finish = wait_lock, |
| 1047 | .start_streaming = start_streaming, |
| 1048 | .stop_streaming = stop_streaming, |
| 1049 | }; |
| 1050 | |
| 1051 | /* FIXME: try to put this functions to mxr_base_layer_create */ |
| 1052 | int mxr_base_layer_register(struct mxr_layer *layer) |
| 1053 | { |
| 1054 | struct mxr_device *mdev = layer->mdev; |
| 1055 | int ret; |
| 1056 | |
| 1057 | ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1); |
| 1058 | if (ret) |
| 1059 | mxr_err(mdev, "failed to register video device\n"); |
| 1060 | else |
| 1061 | mxr_info(mdev, "registered layer %s as /dev/video%d\n", |
| 1062 | layer->vfd.name, layer->vfd.num); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | void mxr_base_layer_unregister(struct mxr_layer *layer) |
| 1067 | { |
| 1068 | video_unregister_device(&layer->vfd); |
| 1069 | } |
| 1070 | |
| 1071 | void mxr_layer_release(struct mxr_layer *layer) |
| 1072 | { |
| 1073 | if (layer->ops.release) |
| 1074 | layer->ops.release(layer); |
| 1075 | } |
| 1076 | |
| 1077 | void mxr_base_layer_release(struct mxr_layer *layer) |
| 1078 | { |
| 1079 | kfree(layer); |
| 1080 | } |
| 1081 | |
| 1082 | static void mxr_vfd_release(struct video_device *vdev) |
| 1083 | { |
Sachin Kamat | f5c9903 | 2012-06-11 06:13:52 -0300 | [diff] [blame] | 1084 | pr_info("video device release\n"); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev, |
| 1088 | int idx, char *name, struct mxr_layer_ops *ops) |
| 1089 | { |
| 1090 | struct mxr_layer *layer; |
| 1091 | |
Sachin Kamat | c0d5120 | 2012-11-26 01:49:01 -0300 | [diff] [blame] | 1092 | layer = kzalloc(sizeof(*layer), GFP_KERNEL); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1093 | if (layer == NULL) { |
| 1094 | mxr_err(mdev, "not enough memory for layer.\n"); |
| 1095 | goto fail; |
| 1096 | } |
| 1097 | |
| 1098 | layer->mdev = mdev; |
| 1099 | layer->idx = idx; |
| 1100 | layer->ops = *ops; |
| 1101 | |
| 1102 | spin_lock_init(&layer->enq_slock); |
| 1103 | INIT_LIST_HEAD(&layer->enq_list); |
| 1104 | mutex_init(&layer->mutex); |
| 1105 | |
| 1106 | layer->vfd = (struct video_device) { |
| 1107 | .minor = -1, |
| 1108 | .release = mxr_vfd_release, |
| 1109 | .fops = &mxr_fops, |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 1110 | .vfl_dir = VFL_DIR_TX, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1111 | .ioctl_ops = &mxr_ioctl_ops, |
| 1112 | }; |
| 1113 | strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name)); |
| 1114 | /* let framework control PRIORITY */ |
| 1115 | set_bit(V4L2_FL_USE_FH_PRIO, &layer->vfd.flags); |
| 1116 | |
| 1117 | video_set_drvdata(&layer->vfd, layer); |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1118 | layer->vfd.lock = &layer->mutex; |
| 1119 | layer->vfd.v4l2_dev = &mdev->v4l2_dev; |
| 1120 | |
| 1121 | layer->vb_queue = (struct vb2_queue) { |
| 1122 | .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, |
Tomasz Stanislawski | fa77521 | 2012-06-14 10:37:48 -0300 | [diff] [blame] | 1123 | .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF, |
Tomasz Stanislawski | fef1c8d | 2011-02-02 05:40:08 -0300 | [diff] [blame] | 1124 | .drv_priv = layer, |
| 1125 | .buf_struct_size = sizeof(struct mxr_buffer), |
| 1126 | .ops = &mxr_video_qops, |
| 1127 | .mem_ops = &vb2_dma_contig_memops, |
| 1128 | }; |
| 1129 | |
| 1130 | return layer; |
| 1131 | |
| 1132 | fail: |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | |
| 1136 | static const struct mxr_format *find_format_by_fourcc( |
| 1137 | struct mxr_layer *layer, unsigned long fourcc) |
| 1138 | { |
| 1139 | int i; |
| 1140 | |
| 1141 | for (i = 0; i < layer->fmt_array_size; ++i) |
| 1142 | if (layer->fmt_array[i]->fourcc == fourcc) |
| 1143 | return layer->fmt_array[i]; |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
| 1147 | static const struct mxr_format *find_format_by_index( |
| 1148 | struct mxr_layer *layer, unsigned long index) |
| 1149 | { |
| 1150 | if (index >= layer->fmt_array_size) |
| 1151 | return NULL; |
| 1152 | return layer->fmt_array[index]; |
| 1153 | } |
| 1154 | |