Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Virtual Video driver - This code emulates a real video device with v4l2 api |
| 3 | * |
| 4 | * Copyright (c) 2006 by: |
| 5 | * Mauro Carvalho Chehab <mchehab--a.t--infradead.org> |
| 6 | * Ted Walther <ted--a.t--enumera.com> |
| 7 | * John Sokol <sokol--a.t--videotechnology.com> |
| 8 | * http://v4l.videotechnology.com/ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the BSD Licence, GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version |
| 14 | */ |
| 15 | #include <linux/module.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 16 | #include <linux/errno.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/sched.h> |
Randy Dunlap | 6b46c39 | 2010-05-07 15:22:26 -0300 | [diff] [blame] | 20 | #include <linux/slab.h> |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 21 | #include <linux/font.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 22 | #include <linux/version.h> |
Matthias Kaehlcke | 51b5402 | 2007-07-02 10:19:38 -0300 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 24 | #include <linux/videodev2.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 26 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20) |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 27 | #include <linux/freezer.h> |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 28 | #endif |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 29 | #include <media/videobuf-vmalloc.h> |
| 30 | #include <media/v4l2-device.h> |
| 31 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 32 | #include <media/v4l2-common.h> |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 33 | |
Mauro Carvalho Chehab | 584ce48 | 2008-06-10 15:21:49 -0300 | [diff] [blame] | 34 | #define VIVI_MODULE_NAME "vivi" |
Carl Karsten | 745271a | 2008-06-10 00:02:32 -0300 | [diff] [blame] | 35 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 36 | /* Wake up at about 30 fps */ |
| 37 | #define WAKE_NUMERATOR 30 |
| 38 | #define WAKE_DENOMINATOR 1001 |
| 39 | #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */ |
| 40 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 41 | #define MAX_WIDTH 1920 |
| 42 | #define MAX_HEIGHT 1200 |
| 43 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 44 | #define VIVI_MAJOR_VERSION 0 |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 45 | #define VIVI_MINOR_VERSION 7 |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 46 | #define VIVI_RELEASE 0 |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 47 | #define VIVI_VERSION \ |
| 48 | KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 49 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 50 | MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board"); |
| 51 | MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol"); |
| 52 | MODULE_LICENSE("Dual BSD/GPL"); |
| 53 | |
| 54 | static unsigned video_nr = -1; |
| 55 | module_param(video_nr, uint, 0644); |
| 56 | MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect"); |
| 57 | |
| 58 | static unsigned n_devs = 1; |
| 59 | module_param(n_devs, uint, 0644); |
| 60 | MODULE_PARM_DESC(n_devs, "number of video devices to create"); |
| 61 | |
| 62 | static unsigned debug; |
| 63 | module_param(debug, uint, 0644); |
| 64 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 65 | |
| 66 | static unsigned int vid_limit = 16; |
| 67 | module_param(vid_limit, uint, 0644); |
| 68 | MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); |
| 69 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 70 | /* Global font descriptor */ |
| 71 | static const u8 *font8x16; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 72 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 73 | #define dprintk(dev, level, fmt, arg...) \ |
| 74 | v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 75 | |
| 76 | /* ------------------------------------------------------------------ |
| 77 | Basic structures |
| 78 | ------------------------------------------------------------------*/ |
| 79 | |
| 80 | struct vivi_fmt { |
| 81 | char *name; |
| 82 | u32 fourcc; /* v4l2 format id */ |
| 83 | int depth; |
| 84 | }; |
| 85 | |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 86 | static struct vivi_fmt formats[] = { |
| 87 | { |
| 88 | .name = "4:2:2, packed, YUYV", |
| 89 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 90 | .depth = 16, |
| 91 | }, |
Magnus Damm | fca36ba | 2008-10-14 12:47:25 -0300 | [diff] [blame] | 92 | { |
| 93 | .name = "4:2:2, packed, UYVY", |
| 94 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 95 | .depth = 16, |
| 96 | }, |
Magnus Damm | aeadb5d | 2008-10-14 12:47:35 -0300 | [diff] [blame] | 97 | { |
| 98 | .name = "RGB565 (LE)", |
| 99 | .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */ |
| 100 | .depth = 16, |
| 101 | }, |
| 102 | { |
| 103 | .name = "RGB565 (BE)", |
| 104 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ |
| 105 | .depth = 16, |
| 106 | }, |
Magnus Damm | def5239 | 2008-10-14 12:47:43 -0300 | [diff] [blame] | 107 | { |
| 108 | .name = "RGB555 (LE)", |
| 109 | .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */ |
| 110 | .depth = 16, |
| 111 | }, |
| 112 | { |
| 113 | .name = "RGB555 (BE)", |
| 114 | .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */ |
| 115 | .depth = 16, |
| 116 | }, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 117 | }; |
| 118 | |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 119 | static struct vivi_fmt *get_format(struct v4l2_format *f) |
| 120 | { |
| 121 | struct vivi_fmt *fmt; |
| 122 | unsigned int k; |
| 123 | |
| 124 | for (k = 0; k < ARRAY_SIZE(formats); k++) { |
| 125 | fmt = &formats[k]; |
| 126 | if (fmt->fourcc == f->fmt.pix.pixelformat) |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | if (k == ARRAY_SIZE(formats)) |
| 131 | return NULL; |
| 132 | |
| 133 | return &formats[k]; |
| 134 | } |
| 135 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 136 | struct sg_to_addr { |
| 137 | int pos; |
| 138 | struct scatterlist *sg; |
| 139 | }; |
| 140 | |
| 141 | /* buffer for one video frame */ |
| 142 | struct vivi_buffer { |
| 143 | /* common v4l buffer stuff -- must be first */ |
| 144 | struct videobuf_buffer vb; |
| 145 | |
| 146 | struct vivi_fmt *fmt; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | struct vivi_dmaqueue { |
| 150 | struct list_head active; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 151 | |
| 152 | /* thread for generating video stream*/ |
| 153 | struct task_struct *kthread; |
| 154 | wait_queue_head_t wq; |
| 155 | /* Counters to control fps rate */ |
| 156 | int frame; |
| 157 | int ini_jiffies; |
| 158 | }; |
| 159 | |
| 160 | static LIST_HEAD(vivi_devlist); |
| 161 | |
| 162 | struct vivi_dev { |
| 163 | struct list_head vivi_devlist; |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 164 | struct v4l2_device v4l2_dev; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 165 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 166 | /* controls */ |
| 167 | int brightness; |
| 168 | int contrast; |
| 169 | int saturation; |
| 170 | int hue; |
| 171 | int volume; |
| 172 | |
Mauro Carvalho Chehab | 55862ac | 2007-12-13 16:13:37 -0300 | [diff] [blame] | 173 | spinlock_t slock; |
Brandon Philips | aa9dbac | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 174 | struct mutex mutex; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 175 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 176 | /* various device info */ |
Mauro Carvalho Chehab | f905c44 | 2007-12-10 04:07:03 -0300 | [diff] [blame] | 177 | struct video_device *vfd; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 178 | |
| 179 | struct vivi_dmaqueue vidq; |
| 180 | |
| 181 | /* Several counters */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 182 | unsigned ms; |
Mauro Carvalho Chehab | dfd8c04 | 2008-01-13 19:36:11 -0300 | [diff] [blame] | 183 | unsigned long jiffies; |
Mauro Carvalho Chehab | 025341d | 2007-12-10 04:43:38 -0300 | [diff] [blame] | 184 | |
| 185 | int mv_count; /* Controls bars movement */ |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 186 | |
| 187 | /* Input Number */ |
| 188 | int input; |
Hans Verkuil | c41ee24 | 2009-02-14 13:43:44 -0300 | [diff] [blame] | 189 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 190 | /* video capture */ |
| 191 | struct vivi_fmt *fmt; |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 192 | unsigned int width, height; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 193 | struct videobuf_queue vb_vidq; |
| 194 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 195 | unsigned long generating; |
| 196 | u8 bars[9][3]; |
| 197 | u8 line[MAX_WIDTH * 4]; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | /* ------------------------------------------------------------------ |
| 201 | DMA and thread functions |
| 202 | ------------------------------------------------------------------*/ |
| 203 | |
| 204 | /* Bars and Colors should match positions */ |
| 205 | |
| 206 | enum colors { |
| 207 | WHITE, |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 208 | AMBER, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 209 | CYAN, |
| 210 | GREEN, |
| 211 | MAGENTA, |
| 212 | RED, |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 213 | BLUE, |
| 214 | BLACK, |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 215 | TEXT_BLACK, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 216 | }; |
| 217 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 218 | /* R G B */ |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 219 | #define COLOR_WHITE {204, 204, 204} |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 220 | #define COLOR_AMBER {208, 208, 0} |
| 221 | #define COLOR_CYAN { 0, 206, 206} |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 222 | #define COLOR_GREEN { 0, 239, 0} |
| 223 | #define COLOR_MAGENTA {239, 0, 239} |
| 224 | #define COLOR_RED {205, 0, 0} |
| 225 | #define COLOR_BLUE { 0, 0, 255} |
| 226 | #define COLOR_BLACK { 0, 0, 0} |
| 227 | |
| 228 | struct bar_std { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 229 | u8 bar[9][3]; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 230 | }; |
| 231 | |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 232 | /* Maximum number of bars are 10 - otherwise, the input print code |
| 233 | should be modified */ |
| 234 | static struct bar_std bars[] = { |
| 235 | { /* Standard ITU-R color bar sequence */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 236 | { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN, |
| 237 | COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK } |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 238 | }, { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 239 | { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, |
| 240 | COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK } |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 241 | }, { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 242 | { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, |
| 243 | COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK } |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 244 | }, { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 245 | { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, |
| 246 | COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK } |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 247 | }, |
| 248 | }; |
| 249 | |
| 250 | #define NUM_INPUTS ARRAY_SIZE(bars) |
| 251 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 252 | #define TO_Y(r, g, b) \ |
| 253 | (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 254 | /* RGB to V(Cr) Color transform */ |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 255 | #define TO_V(r, g, b) \ |
| 256 | (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 257 | /* RGB to U(Cb) Color transform */ |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 258 | #define TO_U(r, g, b) \ |
| 259 | (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 260 | |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 261 | /* precalculate color bar values to speed up rendering */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 262 | static void precalculate_bars(struct vivi_dev *dev) |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 263 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 264 | u8 r, g, b; |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 265 | int k, is_yuv; |
| 266 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 267 | for (k = 0; k < 9; k++) { |
| 268 | r = bars[dev->input].bar[k][0]; |
| 269 | g = bars[dev->input].bar[k][1]; |
| 270 | b = bars[dev->input].bar[k][2]; |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 271 | is_yuv = 0; |
| 272 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 273 | switch (dev->fmt->fourcc) { |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 274 | case V4L2_PIX_FMT_YUYV: |
| 275 | case V4L2_PIX_FMT_UYVY: |
| 276 | is_yuv = 1; |
| 277 | break; |
| 278 | case V4L2_PIX_FMT_RGB565: |
| 279 | case V4L2_PIX_FMT_RGB565X: |
| 280 | r >>= 3; |
| 281 | g >>= 2; |
| 282 | b >>= 3; |
| 283 | break; |
| 284 | case V4L2_PIX_FMT_RGB555: |
| 285 | case V4L2_PIX_FMT_RGB555X: |
| 286 | r >>= 3; |
| 287 | g >>= 3; |
| 288 | b >>= 3; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | if (is_yuv) { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 293 | dev->bars[k][0] = TO_Y(r, g, b); /* Luma */ |
| 294 | dev->bars[k][1] = TO_U(r, g, b); /* Cb */ |
| 295 | dev->bars[k][2] = TO_V(r, g, b); /* Cr */ |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 296 | } else { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 297 | dev->bars[k][0] = r; |
| 298 | dev->bars[k][1] = g; |
| 299 | dev->bars[k][2] = b; |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 300 | } |
| 301 | } |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 302 | } |
| 303 | |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 304 | #define TSTAMP_MIN_Y 24 |
| 305 | #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15) |
| 306 | #define TSTAMP_INPUT_X 10 |
| 307 | #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 308 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 309 | static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos) |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 310 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 311 | u8 r_y, g_u, b_v; |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 312 | int color; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 313 | u8 *p; |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 314 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 315 | r_y = dev->bars[colorpos][0]; /* R or precalculated Y */ |
| 316 | g_u = dev->bars[colorpos][1]; /* G or precalculated U */ |
| 317 | b_v = dev->bars[colorpos][2]; /* B or precalculated V */ |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 318 | |
| 319 | for (color = 0; color < 4; color++) { |
| 320 | p = buf + color; |
| 321 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 322 | switch (dev->fmt->fourcc) { |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 323 | case V4L2_PIX_FMT_YUYV: |
| 324 | switch (color) { |
| 325 | case 0: |
| 326 | case 2: |
| 327 | *p = r_y; |
| 328 | break; |
| 329 | case 1: |
| 330 | *p = g_u; |
| 331 | break; |
| 332 | case 3: |
| 333 | *p = b_v; |
| 334 | break; |
| 335 | } |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 336 | break; |
Magnus Damm | fca36ba | 2008-10-14 12:47:25 -0300 | [diff] [blame] | 337 | case V4L2_PIX_FMT_UYVY: |
| 338 | switch (color) { |
| 339 | case 1: |
| 340 | case 3: |
| 341 | *p = r_y; |
| 342 | break; |
| 343 | case 0: |
| 344 | *p = g_u; |
| 345 | break; |
| 346 | case 2: |
| 347 | *p = b_v; |
| 348 | break; |
| 349 | } |
| 350 | break; |
Magnus Damm | aeadb5d | 2008-10-14 12:47:35 -0300 | [diff] [blame] | 351 | case V4L2_PIX_FMT_RGB565: |
| 352 | switch (color) { |
| 353 | case 0: |
| 354 | case 2: |
| 355 | *p = (g_u << 5) | b_v; |
| 356 | break; |
| 357 | case 1: |
| 358 | case 3: |
| 359 | *p = (r_y << 3) | (g_u >> 3); |
| 360 | break; |
| 361 | } |
| 362 | break; |
| 363 | case V4L2_PIX_FMT_RGB565X: |
| 364 | switch (color) { |
| 365 | case 0: |
| 366 | case 2: |
| 367 | *p = (r_y << 3) | (g_u >> 3); |
| 368 | break; |
| 369 | case 1: |
| 370 | case 3: |
| 371 | *p = (g_u << 5) | b_v; |
| 372 | break; |
| 373 | } |
| 374 | break; |
Magnus Damm | def5239 | 2008-10-14 12:47:43 -0300 | [diff] [blame] | 375 | case V4L2_PIX_FMT_RGB555: |
| 376 | switch (color) { |
| 377 | case 0: |
| 378 | case 2: |
| 379 | *p = (g_u << 5) | b_v; |
| 380 | break; |
| 381 | case 1: |
| 382 | case 3: |
| 383 | *p = (r_y << 2) | (g_u >> 3); |
| 384 | break; |
| 385 | } |
| 386 | break; |
| 387 | case V4L2_PIX_FMT_RGB555X: |
| 388 | switch (color) { |
| 389 | case 0: |
| 390 | case 2: |
| 391 | *p = (r_y << 2) | (g_u >> 3); |
| 392 | break; |
| 393 | case 1: |
| 394 | case 3: |
| 395 | *p = (g_u << 5) | b_v; |
| 396 | break; |
| 397 | } |
| 398 | break; |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 403 | static void precalculate_line(struct vivi_dev *dev) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 404 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 405 | int w; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 406 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 407 | for (w = 0; w < dev->width * 2; w += 2) { |
| 408 | int colorpos = (w / (dev->width / 8) % 8); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 409 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 410 | gen_twopix(dev, dev->line + w * 2, colorpos); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 411 | } |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 412 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 413 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 414 | static void gen_text(struct vivi_dev *dev, char *basep, |
| 415 | int y, int x, char *text) |
| 416 | { |
| 417 | int line; |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 418 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 419 | /* Checks if it is possible to show string */ |
| 420 | if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width) |
| 421 | return; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 422 | |
| 423 | /* Print stream time */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 424 | for (line = y; line < y + 16; line++) { |
| 425 | int j = 0; |
| 426 | char *pos = basep + line * dev->width * 2 + x * 2; |
| 427 | char *s; |
| 428 | |
| 429 | for (s = text; *s; s++) { |
| 430 | u8 chr = font8x16[*s * 16 + line - y]; |
| 431 | int i; |
| 432 | |
| 433 | for (i = 0; i < 7; i++, j++) { |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 434 | /* Draw white font on black background */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 435 | if (chr & (1 << (7 - i))) |
| 436 | gen_twopix(dev, pos + j * 2, WHITE); |
Magnus Damm | 74d7c5a | 2008-10-14 12:46:59 -0300 | [diff] [blame] | 437 | else |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 438 | gen_twopix(dev, pos + j * 2, TEXT_BLACK); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 442 | } |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 443 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 444 | static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 445 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 446 | int hmax = buf->vb.height; |
| 447 | int wmax = buf->vb.width; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 448 | struct timeval ts; |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 449 | void *vbuf = videobuf_to_vmalloc(&buf->vb); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 450 | unsigned ms; |
| 451 | char str[100]; |
| 452 | int h, line = 1; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 453 | |
Marcin Slusarz | 5c554e6 | 2008-06-22 09:11:40 -0300 | [diff] [blame] | 454 | if (!vbuf) |
Mauro Carvalho Chehab | 5a03770 | 2007-08-02 23:31:54 -0300 | [diff] [blame] | 455 | return; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 456 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 457 | for (h = 0; h < hmax; h++) |
| 458 | memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2); |
Mauro Carvalho Chehab | 5a03770 | 2007-08-02 23:31:54 -0300 | [diff] [blame] | 459 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 460 | /* Updates stream time */ |
| 461 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 462 | dev->ms += jiffies_to_msecs(jiffies - dev->jiffies); |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 463 | dev->jiffies = jiffies; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 464 | ms = dev->ms; |
| 465 | snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ", |
| 466 | (ms / (60 * 60 * 1000)) % 24, |
| 467 | (ms / (60 * 1000)) % 60, |
| 468 | (ms / 1000) % 60, |
| 469 | ms % 1000); |
| 470 | gen_text(dev, vbuf, line++ * 16, 16, str); |
| 471 | snprintf(str, sizeof(str), " %dx%d, input %d ", |
| 472 | dev->width, dev->height, dev->input); |
| 473 | gen_text(dev, vbuf, line++ * 16, 16, str); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 474 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 475 | snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ", |
| 476 | dev->brightness, |
| 477 | dev->contrast, |
| 478 | dev->saturation, |
| 479 | dev->hue); |
| 480 | gen_text(dev, vbuf, line++ * 16, 16, str); |
| 481 | snprintf(str, sizeof(str), " volume %3d ", dev->volume); |
| 482 | gen_text(dev, vbuf, line++ * 16, 16, str); |
| 483 | |
| 484 | dev->mv_count += 2; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 485 | |
| 486 | /* Advice that buffer was filled */ |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 487 | buf->vb.field_count++; |
| 488 | do_gettimeofday(&ts); |
| 489 | buf->vb.ts = ts; |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 490 | buf->vb.state = VIDEOBUF_DONE; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 491 | } |
| 492 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 493 | static void vivi_thread_tick(struct vivi_dev *dev) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 494 | { |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 495 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 496 | struct vivi_buffer *buf; |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 497 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 498 | |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 499 | dprintk(dev, 1, "Thread tick\n"); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 500 | |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 501 | spin_lock_irqsave(&dev->slock, flags); |
| 502 | if (list_empty(&dma_q->active)) { |
| 503 | dprintk(dev, 1, "No active queue to serve\n"); |
| 504 | goto unlock; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 505 | } |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 506 | |
| 507 | buf = list_entry(dma_q->active.next, |
| 508 | struct vivi_buffer, vb.queue); |
| 509 | |
| 510 | /* Nobody is waiting on this buffer, return */ |
| 511 | if (!waitqueue_active(&buf->vb.done)) |
| 512 | goto unlock; |
| 513 | |
| 514 | list_del(&buf->vb.queue); |
| 515 | |
| 516 | do_gettimeofday(&buf->vb.ts); |
| 517 | |
| 518 | /* Fill buffer */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 519 | vivi_fillbuff(dev, buf); |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 520 | dprintk(dev, 1, "filled buffer %p\n", buf); |
| 521 | |
| 522 | wake_up(&buf->vb.done); |
| 523 | dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i); |
| 524 | unlock: |
| 525 | spin_unlock_irqrestore(&dev->slock, flags); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 526 | } |
| 527 | |
Mauro Carvalho Chehab | 6594ad8 | 2007-12-13 16:15:41 -0300 | [diff] [blame] | 528 | #define frames_to_ms(frames) \ |
| 529 | ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR) |
| 530 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 531 | static void vivi_sleep(struct vivi_dev *dev) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 532 | { |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 533 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
| 534 | int timeout; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 535 | DECLARE_WAITQUEUE(wait, current); |
| 536 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 537 | dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__, |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 538 | (unsigned long)dma_q); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 539 | |
| 540 | add_wait_queue(&dma_q->wq, &wait); |
Mauro Carvalho Chehab | 6594ad8 | 2007-12-13 16:15:41 -0300 | [diff] [blame] | 541 | if (kthread_should_stop()) |
| 542 | goto stop_task; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 543 | |
Mauro Carvalho Chehab | 6594ad8 | 2007-12-13 16:15:41 -0300 | [diff] [blame] | 544 | /* Calculate time to wake up */ |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 545 | timeout = msecs_to_jiffies(frames_to_ms(1)); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 546 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 547 | vivi_thread_tick(dev); |
Mauro Carvalho Chehab | 6594ad8 | 2007-12-13 16:15:41 -0300 | [diff] [blame] | 548 | |
| 549 | schedule_timeout_interruptible(timeout); |
| 550 | |
| 551 | stop_task: |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 552 | remove_wait_queue(&dma_q->wq, &wait); |
| 553 | try_to_freeze(); |
| 554 | } |
| 555 | |
Adrian Bunk | 972c351 | 2006-04-27 21:06:50 -0300 | [diff] [blame] | 556 | static int vivi_thread(void *data) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 557 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 558 | struct vivi_dev *dev = data; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 559 | |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 560 | dprintk(dev, 1, "thread started\n"); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 561 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 562 | set_freezable(); |
Mauro Carvalho Chehab | 0b60051 | 2007-01-14 08:33:24 -0300 | [diff] [blame] | 563 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 564 | for (;;) { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 565 | vivi_sleep(dev); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 566 | |
| 567 | if (kthread_should_stop()) |
| 568 | break; |
| 569 | } |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 570 | dprintk(dev, 1, "thread: exit\n"); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 574 | static void vivi_start_generating(struct file *file) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 575 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 576 | struct vivi_dev *dev = video_drvdata(file); |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 577 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 578 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 579 | dprintk(dev, 1, "%s\n", __func__); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 580 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 581 | if (test_and_set_bit(0, &dev->generating)) |
| 582 | return; |
| 583 | file->private_data = dev; |
| 584 | |
| 585 | /* Resets frame counters */ |
| 586 | dev->ms = 0; |
| 587 | dev->mv_count = 0; |
| 588 | dev->jiffies = jiffies; |
| 589 | |
| 590 | dma_q->frame = 0; |
| 591 | dma_q->ini_jiffies = jiffies; |
| 592 | dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 593 | |
Akinobu Mita | 054afee | 2006-12-20 10:04:00 -0300 | [diff] [blame] | 594 | if (IS_ERR(dma_q->kthread)) { |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 595 | v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 596 | clear_bit(0, &dev->generating); |
| 597 | return; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 598 | } |
Mauro Carvalho Chehab | 0b60051 | 2007-01-14 08:33:24 -0300 | [diff] [blame] | 599 | /* Wakes thread */ |
| 600 | wake_up_interruptible(&dma_q->wq); |
| 601 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 602 | dprintk(dev, 1, "returning from %s\n", __func__); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 603 | } |
| 604 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 605 | static void vivi_stop_generating(struct file *file) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 606 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 607 | struct vivi_dev *dev = video_drvdata(file); |
| 608 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 609 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 610 | dprintk(dev, 1, "%s\n", __func__); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 611 | |
| 612 | if (!file->private_data) |
| 613 | return; |
| 614 | if (!test_and_clear_bit(0, &dev->generating)) |
| 615 | return; |
| 616 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 617 | /* shutdown control thread */ |
| 618 | if (dma_q->kthread) { |
| 619 | kthread_stop(dma_q->kthread); |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 620 | dma_q->kthread = NULL; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 621 | } |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 622 | videobuf_stop(&dev->vb_vidq); |
| 623 | videobuf_mmap_free(&dev->vb_vidq); |
| 624 | } |
| 625 | |
| 626 | static int vivi_is_generating(struct vivi_dev *dev) |
| 627 | { |
| 628 | return test_bit(0, &dev->generating); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 629 | } |
| 630 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 631 | /* ------------------------------------------------------------------ |
| 632 | Videobuf operations |
| 633 | ------------------------------------------------------------------*/ |
| 634 | static int |
| 635 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) |
| 636 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 637 | struct vivi_dev *dev = vq->priv_data; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 638 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 639 | *size = dev->width * dev->height * 2; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 640 | |
| 641 | if (0 == *count) |
| 642 | *count = 32; |
Mauro Carvalho Chehab | 6bb2790 | 2007-08-23 16:41:14 -0300 | [diff] [blame] | 643 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 644 | while (*size * *count > vid_limit * 1024 * 1024) |
| 645 | (*count)--; |
Mauro Carvalho Chehab | 6bb2790 | 2007-08-23 16:41:14 -0300 | [diff] [blame] | 646 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 647 | dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__, |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 648 | *count, *size); |
Mauro Carvalho Chehab | 6bb2790 | 2007-08-23 16:41:14 -0300 | [diff] [blame] | 649 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Adrian Bunk | 972c351 | 2006-04-27 21:06:50 -0300 | [diff] [blame] | 653 | static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 654 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 655 | struct vivi_dev *dev = vq->priv_data; |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 656 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 657 | dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 658 | |
Mauro Carvalho Chehab | 5a03770 | 2007-08-02 23:31:54 -0300 | [diff] [blame] | 659 | videobuf_vmalloc_free(&buf->vb); |
Mauro Carvalho Chehab | fbde31d | 2008-04-13 14:57:44 -0300 | [diff] [blame] | 660 | dprintk(dev, 1, "free_buffer: freed\n"); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 661 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 662 | } |
| 663 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 664 | static int |
| 665 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, |
| 666 | enum v4l2_field field) |
| 667 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 668 | struct vivi_dev *dev = vq->priv_data; |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 669 | struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 670 | int rc; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 671 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 672 | dprintk(dev, 1, "%s, field=%d\n", __func__, field); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 673 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 674 | BUG_ON(NULL == dev->fmt); |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 675 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 676 | if (dev->width < 48 || dev->width > MAX_WIDTH || |
| 677 | dev->height < 32 || dev->height > MAX_HEIGHT) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 678 | return -EINVAL; |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 679 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 680 | buf->vb.size = dev->width * dev->height * 2; |
| 681 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 684 | /* These properties only change when queue is idle, see s_fmt */ |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 685 | buf->fmt = dev->fmt; |
| 686 | buf->vb.width = dev->width; |
| 687 | buf->vb.height = dev->height; |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 688 | buf->vb.field = field; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 689 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 690 | precalculate_bars(dev); |
| 691 | precalculate_line(dev); |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 692 | |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 693 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 694 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
| 695 | if (rc < 0) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 696 | goto fail; |
| 697 | } |
| 698 | |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 699 | buf->vb.state = VIDEOBUF_PREPARED; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 700 | return 0; |
| 701 | |
| 702 | fail: |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 703 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 704 | return rc; |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 709 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 710 | struct vivi_dev *dev = vq->priv_data; |
| 711 | struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 712 | struct vivi_dmaqueue *vidq = &dev->vidq; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 713 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 714 | dprintk(dev, 1, "%s\n", __func__); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 715 | |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 716 | buf->vb.state = VIDEOBUF_QUEUED; |
| 717 | list_add_tail(&buf->vb.queue, &vidq->active); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 718 | } |
| 719 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 720 | static void buffer_release(struct videobuf_queue *vq, |
| 721 | struct videobuf_buffer *vb) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 722 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 723 | struct vivi_dev *dev = vq->priv_data; |
| 724 | struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 725 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 726 | dprintk(dev, 1, "%s\n", __func__); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 727 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 728 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 729 | } |
| 730 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 731 | static struct videobuf_queue_ops vivi_video_qops = { |
| 732 | .buf_setup = buffer_setup, |
| 733 | .buf_prepare = buffer_prepare, |
| 734 | .buf_queue = buffer_queue, |
| 735 | .buf_release = buffer_release, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | /* ------------------------------------------------------------------ |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 739 | IOCTL vidioc handling |
| 740 | ------------------------------------------------------------------*/ |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 741 | static int vidioc_querycap(struct file *file, void *priv, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 742 | struct v4l2_capability *cap) |
| 743 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 744 | struct vivi_dev *dev = video_drvdata(file); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 745 | |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 746 | strcpy(cap->driver, "vivi"); |
| 747 | strcpy(cap->card, "vivi"); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 748 | strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info)); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 749 | cap->version = VIVI_VERSION; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 750 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \ |
| 751 | V4L2_CAP_READWRITE; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 755 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 756 | struct v4l2_fmtdesc *f) |
| 757 | { |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 758 | struct vivi_fmt *fmt; |
| 759 | |
| 760 | if (f->index >= ARRAY_SIZE(formats)) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 761 | return -EINVAL; |
| 762 | |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 763 | fmt = &formats[f->index]; |
| 764 | |
| 765 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 766 | f->pixelformat = fmt->fourcc; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 770 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 771 | struct v4l2_format *f) |
| 772 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 773 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 774 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 775 | f->fmt.pix.width = dev->width; |
| 776 | f->fmt.pix.height = dev->height; |
| 777 | f->fmt.pix.field = dev->vb_vidq.field; |
| 778 | f->fmt.pix.pixelformat = dev->fmt->fourcc; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 779 | f->fmt.pix.bytesperline = |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 780 | (f->fmt.pix.width * dev->fmt->depth) >> 3; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 781 | f->fmt.pix.sizeimage = |
| 782 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 783 | return 0; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 784 | } |
| 785 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 786 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 787 | struct v4l2_format *f) |
| 788 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 789 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 790 | struct vivi_fmt *fmt; |
| 791 | enum v4l2_field field; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 792 | |
Magnus Damm | d891f47 | 2008-10-14 12:47:09 -0300 | [diff] [blame] | 793 | fmt = get_format(f); |
| 794 | if (!fmt) { |
| 795 | dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n", |
| 796 | f->fmt.pix.pixelformat); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 797 | return -EINVAL; |
| 798 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 799 | |
| 800 | field = f->fmt.pix.field; |
| 801 | |
| 802 | if (field == V4L2_FIELD_ANY) { |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 803 | field = V4L2_FIELD_INTERLACED; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 804 | } else if (V4L2_FIELD_INTERLACED != field) { |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 805 | dprintk(dev, 1, "Field type invalid.\n"); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 806 | return -EINVAL; |
| 807 | } |
| 808 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 809 | f->fmt.pix.field = field; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 810 | v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2, |
| 811 | &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 812 | f->fmt.pix.bytesperline = |
| 813 | (f->fmt.pix.width * fmt->depth) >> 3; |
| 814 | f->fmt.pix.sizeimage = |
| 815 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 819 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 820 | struct v4l2_format *f) |
| 821 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 822 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 823 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 824 | int ret = vidioc_try_fmt_vid_cap(file, priv, f); |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 825 | if (ret < 0) |
| 826 | return ret; |
| 827 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 828 | if (vivi_is_generating(dev)) { |
| 829 | dprintk(dev, 1, "%s device busy\n", __func__); |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 830 | ret = -EBUSY; |
| 831 | goto out; |
| 832 | } |
| 833 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 834 | dev->fmt = get_format(f); |
| 835 | dev->width = f->fmt.pix.width; |
| 836 | dev->height = f->fmt.pix.height; |
| 837 | dev->vb_vidq.field = f->fmt.pix.field; |
Brandon Philips | 78718e5 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 838 | ret = 0; |
| 839 | out: |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 840 | return ret; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 841 | } |
| 842 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 843 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 844 | struct v4l2_requestbuffers *p) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 845 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 846 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 847 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 848 | return videobuf_reqbufs(&dev->vb_vidq, p); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 849 | } |
| 850 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 851 | static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 852 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 853 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 854 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 855 | return videobuf_querybuf(&dev->vb_vidq, p); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 856 | } |
| 857 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 858 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 859 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 860 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 861 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 862 | return videobuf_qbuf(&dev->vb_vidq, p); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 863 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 864 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 865 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 866 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 867 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 868 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 869 | return videobuf_dqbuf(&dev->vb_vidq, p, |
| 870 | file->f_flags & O_NONBLOCK); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 871 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 872 | |
Mauro Carvalho Chehab | 0dfa9ab | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 873 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 874 | static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 875 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 876 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 877 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 878 | return videobuf_cgmbuf(&dev->vb_vidq, mbuf, 8); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 879 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 880 | #endif |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 881 | |
Adrian Bunk | dc46ace | 2006-06-23 06:42:44 -0300 | [diff] [blame] | 882 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 883 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 884 | struct vivi_dev *dev = video_drvdata(file); |
| 885 | int ret; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 886 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 887 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 888 | return -EINVAL; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 889 | ret = videobuf_streamon(&dev->vb_vidq); |
| 890 | if (ret) |
| 891 | return ret; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 892 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 893 | vivi_start_generating(file); |
| 894 | return 0; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 895 | } |
| 896 | |
Adrian Bunk | dc46ace | 2006-06-23 06:42:44 -0300 | [diff] [blame] | 897 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 898 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 899 | struct vivi_dev *dev = video_drvdata(file); |
| 900 | int ret; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 901 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 902 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 903 | return -EINVAL; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 904 | ret = videobuf_streamoff(&dev->vb_vidq); |
| 905 | if (!ret) |
| 906 | vivi_stop_generating(file); |
| 907 | return ret; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 908 | } |
| 909 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 910 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 911 | { |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 912 | return 0; |
| 913 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 914 | |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 915 | /* only one input in this sample driver */ |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 916 | static int vidioc_enum_input(struct file *file, void *priv, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 917 | struct v4l2_input *inp) |
| 918 | { |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 919 | if (inp->index >= NUM_INPUTS) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 920 | return -EINVAL; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 921 | |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 922 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
Mauro Carvalho Chehab | 784c668 | 2007-12-13 06:35:26 -0300 | [diff] [blame] | 923 | inp->std = V4L2_STD_525_60; |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 924 | sprintf(inp->name, "Camera %u", inp->index); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 925 | return 0; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 926 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 927 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 928 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 929 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 930 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 931 | |
| 932 | *i = dev->input; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 933 | return 0; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 934 | } |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 935 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 936 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 937 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 938 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 939 | |
| 940 | if (i >= NUM_INPUTS) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 941 | return -EINVAL; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 942 | |
Mauro Carvalho Chehab | e164b58 | 2009-01-11 10:29:43 -0300 | [diff] [blame] | 943 | dev->input = i; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 944 | precalculate_bars(dev); |
| 945 | precalculate_line(dev); |
| 946 | return 0; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 947 | } |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 948 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 949 | /* --- controls ---------------------------------------------- */ |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 950 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 951 | struct v4l2_queryctrl *qc) |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 952 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 953 | switch (qc->id) { |
| 954 | case V4L2_CID_AUDIO_VOLUME: |
| 955 | return v4l2_ctrl_query_fill(qc, 0, 255, 1, 200); |
| 956 | case V4L2_CID_BRIGHTNESS: |
| 957 | return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127); |
| 958 | case V4L2_CID_CONTRAST: |
| 959 | return v4l2_ctrl_query_fill(qc, 0, 255, 1, 16); |
| 960 | case V4L2_CID_SATURATION: |
| 961 | return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127); |
| 962 | case V4L2_CID_HUE: |
| 963 | return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0); |
| 964 | } |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 965 | return -EINVAL; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 966 | } |
| 967 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 968 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 969 | struct v4l2_control *ctrl) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 970 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 971 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 972 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 973 | switch (ctrl->id) { |
| 974 | case V4L2_CID_AUDIO_VOLUME: |
| 975 | ctrl->value = dev->volume; |
| 976 | return 0; |
| 977 | case V4L2_CID_BRIGHTNESS: |
| 978 | ctrl->value = dev->brightness; |
| 979 | return 0; |
| 980 | case V4L2_CID_CONTRAST: |
| 981 | ctrl->value = dev->contrast; |
| 982 | return 0; |
| 983 | case V4L2_CID_SATURATION: |
| 984 | ctrl->value = dev->saturation; |
| 985 | return 0; |
| 986 | case V4L2_CID_HUE: |
| 987 | ctrl->value = dev->hue; |
| 988 | return 0; |
| 989 | } |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 990 | return -EINVAL; |
| 991 | } |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 992 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 993 | static int vidioc_s_ctrl(struct file *file, void *priv, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 994 | struct v4l2_control *ctrl) |
| 995 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 996 | struct vivi_dev *dev = video_drvdata(file); |
| 997 | struct v4l2_queryctrl qc; |
| 998 | int err; |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 999 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1000 | qc.id = ctrl->id; |
| 1001 | err = vidioc_queryctrl(file, priv, &qc); |
| 1002 | if (err < 0) |
| 1003 | return err; |
| 1004 | if (ctrl->value < qc.minimum || ctrl->value > qc.maximum) |
| 1005 | return -ERANGE; |
| 1006 | switch (ctrl->id) { |
| 1007 | case V4L2_CID_AUDIO_VOLUME: |
| 1008 | dev->volume = ctrl->value; |
| 1009 | return 0; |
| 1010 | case V4L2_CID_BRIGHTNESS: |
| 1011 | dev->brightness = ctrl->value; |
| 1012 | return 0; |
| 1013 | case V4L2_CID_CONTRAST: |
| 1014 | dev->contrast = ctrl->value; |
| 1015 | return 0; |
| 1016 | case V4L2_CID_SATURATION: |
| 1017 | dev->saturation = ctrl->value; |
| 1018 | return 0; |
| 1019 | case V4L2_CID_HUE: |
| 1020 | dev->hue = ctrl->value; |
| 1021 | return 0; |
| 1022 | } |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1023 | return -EINVAL; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* ------------------------------------------------------------------ |
| 1027 | File operations for the device |
| 1028 | ------------------------------------------------------------------*/ |
| 1029 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1030 | static ssize_t |
| 1031 | vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
| 1032 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1033 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1034 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1035 | vivi_start_generating(file); |
| 1036 | return videobuf_read_stream(&dev->vb_vidq, data, count, ppos, 0, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1037 | file->f_flags & O_NONBLOCK); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static unsigned int |
| 1041 | vivi_poll(struct file *file, struct poll_table_struct *wait) |
| 1042 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1043 | struct vivi_dev *dev = video_drvdata(file); |
| 1044 | struct videobuf_queue *q = &dev->vb_vidq; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1045 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1046 | dprintk(dev, 1, "%s\n", __func__); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1047 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1048 | vivi_start_generating(file); |
Brandon Philips | 85c7c70bc | 2007-09-27 20:55:02 -0300 | [diff] [blame] | 1049 | return videobuf_poll_stream(file, q, wait); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1050 | } |
| 1051 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1052 | static int vivi_close(struct file *file) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1053 | { |
Laurent Pinchart | 50462eb | 2009-12-10 11:47:13 -0200 | [diff] [blame] | 1054 | struct video_device *vdev = video_devdata(file); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1055 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1056 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1057 | vivi_stop_generating(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1058 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1059 | dprintk(dev, 1, "close called (dev=%s)\n", |
| 1060 | video_device_node_name(vdev)); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
Mauro Carvalho Chehab | 543323b | 2007-12-10 09:33:52 -0300 | [diff] [blame] | 1064 | static int vivi_mmap(struct file *file, struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1065 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1066 | struct vivi_dev *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1067 | int ret; |
| 1068 | |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 1069 | dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1070 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1071 | ret = videobuf_mmap_mapper(&dev->vb_vidq, vma); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1072 | |
Mauro Carvalho Chehab | 6c2f990 | 2007-12-13 13:30:14 -0300 | [diff] [blame] | 1073 | dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n", |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1074 | (unsigned long)vma->vm_start, |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1075 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1076 | ret); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1077 | return ret; |
| 1078 | } |
| 1079 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1080 | static const struct v4l2_file_operations vivi_fops = { |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1081 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | f905c44 | 2007-12-10 04:07:03 -0300 | [diff] [blame] | 1082 | .release = vivi_close, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1083 | .read = vivi_read, |
| 1084 | .poll = vivi_poll, |
Hans Verkuil | fedc6c8 | 2010-09-20 18:25:55 -0300 | [diff] [blame] | 1085 | .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */ |
Mauro Carvalho Chehab | 5a03770 | 2007-08-02 23:31:54 -0300 | [diff] [blame] | 1086 | .mmap = vivi_mmap, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1087 | }; |
| 1088 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1089 | static const struct v4l2_ioctl_ops vivi_ioctl_ops = { |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1090 | .vidioc_querycap = vidioc_querycap, |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1091 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1092 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1093 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1094 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1095 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1096 | .vidioc_querybuf = vidioc_querybuf, |
| 1097 | .vidioc_qbuf = vidioc_qbuf, |
| 1098 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1099 | .vidioc_s_std = vidioc_s_std, |
| 1100 | .vidioc_enum_input = vidioc_enum_input, |
| 1101 | .vidioc_g_input = vidioc_g_input, |
| 1102 | .vidioc_s_input = vidioc_s_input, |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1103 | .vidioc_streamon = vidioc_streamon, |
| 1104 | .vidioc_streamoff = vidioc_streamoff, |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1105 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1106 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1107 | .vidioc_s_ctrl = vidioc_s_ctrl, |
Mauro Carvalho Chehab | 0dfa9ab | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 1108 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1109 | .vidiocgmbuf = vidiocgmbuf, |
| 1110 | #endif |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1111 | }; |
| 1112 | |
| 1113 | static struct video_device vivi_template = { |
| 1114 | .name = "vivi", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1115 | .fops = &vivi_fops, |
| 1116 | .ioctl_ops = &vivi_ioctl_ops, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1117 | .release = video_device_release, |
| 1118 | |
Mauro Carvalho Chehab | 784c668 | 2007-12-13 06:35:26 -0300 | [diff] [blame] | 1119 | .tvnorms = V4L2_STD_525_60, |
Mauro Carvalho Chehab | e75f9ce | 2006-11-20 13:19:20 -0300 | [diff] [blame] | 1120 | .current_norm = V4L2_STD_NTSC_M, |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1121 | }; |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1122 | |
Mauro Carvalho Chehab | c820cc4 | 2006-06-04 10:34:12 -0300 | [diff] [blame] | 1123 | /* ----------------------------------------------------------------- |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1124 | Initialization and module stuff |
| 1125 | ------------------------------------------------------------------*/ |
| 1126 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1127 | static int vivi_release(void) |
| 1128 | { |
| 1129 | struct vivi_dev *dev; |
| 1130 | struct list_head *list; |
| 1131 | |
| 1132 | while (!list_empty(&vivi_devlist)) { |
| 1133 | list = vivi_devlist.next; |
| 1134 | list_del(list); |
| 1135 | dev = list_entry(list, struct vivi_dev, vivi_devlist); |
| 1136 | |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 1137 | v4l2_info(&dev->v4l2_dev, "unregistering %s\n", |
| 1138 | video_device_node_name(dev->vfd)); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1139 | video_unregister_device(dev->vfd); |
| 1140 | v4l2_device_unregister(&dev->v4l2_dev); |
| 1141 | kfree(dev); |
| 1142 | } |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Hans Verkuil | c41ee24 | 2009-02-14 13:43:44 -0300 | [diff] [blame] | 1147 | static int __init vivi_create_instance(int inst) |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1148 | { |
| 1149 | struct vivi_dev *dev; |
| 1150 | struct video_device *vfd; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1151 | int ret; |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1152 | |
| 1153 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1154 | if (!dev) |
| 1155 | return -ENOMEM; |
| 1156 | |
| 1157 | snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), |
Hans Verkuil | c41ee24 | 2009-02-14 13:43:44 -0300 | [diff] [blame] | 1158 | "%s-%03d", VIVI_MODULE_NAME, inst); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1159 | ret = v4l2_device_register(NULL, &dev->v4l2_dev); |
| 1160 | if (ret) |
| 1161 | goto free_dev; |
| 1162 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1163 | dev->fmt = &formats[0]; |
| 1164 | dev->width = 640; |
| 1165 | dev->height = 480; |
| 1166 | dev->volume = 200; |
| 1167 | dev->brightness = 127; |
| 1168 | dev->contrast = 16; |
| 1169 | dev->saturation = 127; |
| 1170 | dev->hue = 0; |
| 1171 | |
Hans Verkuil | fedc6c8 | 2010-09-20 18:25:55 -0300 | [diff] [blame] | 1172 | /* initialize locks */ |
| 1173 | spin_lock_init(&dev->slock); |
| 1174 | mutex_init(&dev->mutex); |
| 1175 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1176 | videobuf_queue_vmalloc_init(&dev->vb_vidq, &vivi_video_qops, |
| 1177 | NULL, &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 1178 | V4L2_FIELD_INTERLACED, |
Hans Verkuil | fedc6c8 | 2010-09-20 18:25:55 -0300 | [diff] [blame] | 1179 | sizeof(struct vivi_buffer), dev, &dev->mutex); |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1180 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1181 | /* init video dma queues */ |
| 1182 | INIT_LIST_HEAD(&dev->vidq.active); |
| 1183 | init_waitqueue_head(&dev->vidq.wq); |
| 1184 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1185 | ret = -ENOMEM; |
| 1186 | vfd = video_device_alloc(); |
| 1187 | if (!vfd) |
| 1188 | goto unreg_dev; |
| 1189 | |
| 1190 | *vfd = vivi_template; |
Mauro Carvalho Chehab | c285add | 2009-06-25 16:28:23 -0300 | [diff] [blame] | 1191 | vfd->debug = debug; |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1192 | vfd->v4l2_dev = &dev->v4l2_dev; |
Hans Verkuil | fedc6c8 | 2010-09-20 18:25:55 -0300 | [diff] [blame] | 1193 | vfd->lock = &dev->mutex; |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1194 | |
| 1195 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr); |
| 1196 | if (ret < 0) |
| 1197 | goto rel_vdev; |
| 1198 | |
| 1199 | video_set_drvdata(vfd, dev); |
| 1200 | |
| 1201 | /* Now that everything is fine, let's add it to device list */ |
| 1202 | list_add_tail(&dev->vivi_devlist, &vivi_devlist); |
| 1203 | |
Roel Kluin | 7de0b87 | 2009-12-16 13:06:33 -0300 | [diff] [blame] | 1204 | if (video_nr != -1) |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1205 | video_nr++; |
| 1206 | |
| 1207 | dev->vfd = vfd; |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 1208 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", |
| 1209 | video_device_node_name(vfd)); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1210 | return 0; |
| 1211 | |
| 1212 | rel_vdev: |
| 1213 | video_device_release(vfd); |
| 1214 | unreg_dev: |
| 1215 | v4l2_device_unregister(&dev->v4l2_dev); |
| 1216 | free_dev: |
| 1217 | kfree(dev); |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1221 | /* This routine allocates from 1 to n_devs virtual drivers. |
| 1222 | |
| 1223 | The real maximum number of virtual drivers will depend on how many drivers |
| 1224 | will succeed. This is limited to the maximum number of devices that |
Hans Verkuil | 62cfdac | 2009-02-14 11:37:17 -0300 | [diff] [blame] | 1225 | videodev supports, which is equal to VIDEO_NUM_DEVICES. |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1226 | */ |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1227 | static int __init vivi_init(void) |
| 1228 | { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1229 | const struct font_desc *font = find_font("VGA8x16"); |
Hans Verkuil | 9185cbf | 2009-03-06 09:58:12 -0300 | [diff] [blame] | 1230 | int ret = 0, i; |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1231 | |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1232 | if (font == NULL) { |
| 1233 | printk(KERN_ERR "vivi: could not find font\n"); |
| 1234 | return -ENODEV; |
| 1235 | } |
| 1236 | font8x16 = font->data; |
| 1237 | |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1238 | if (n_devs <= 0) |
| 1239 | n_devs = 1; |
| 1240 | |
Mauro Carvalho Chehab | 55712ff | 2007-12-10 04:38:11 -0300 | [diff] [blame] | 1241 | for (i = 0; i < n_devs; i++) { |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1242 | ret = vivi_create_instance(i); |
| 1243 | if (ret) { |
| 1244 | /* If some instantiations succeeded, keep driver */ |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1245 | if (i) |
| 1246 | ret = 0; |
Mauro Carvalho Chehab | 55712ff | 2007-12-10 04:38:11 -0300 | [diff] [blame] | 1247 | break; |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1248 | } |
Mauro Carvalho Chehab | 55712ff | 2007-12-10 04:38:11 -0300 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | if (ret < 0) { |
Hans Verkuil | 730947b | 2010-04-10 04:13:53 -0300 | [diff] [blame] | 1252 | printk(KERN_ERR "vivi: error %d while loading driver\n", ret); |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | printk(KERN_INFO "Video Technology Magazine Virtual Video " |
Carl Karsten | 745271a | 2008-06-10 00:02:32 -0300 | [diff] [blame] | 1257 | "Capture Board ver %u.%u.%u successfully loaded.\n", |
| 1258 | (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF, |
| 1259 | VIVI_VERSION & 0xFF); |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1260 | |
Hans Verkuil | 5ab6c9a | 2009-02-14 13:23:12 -0300 | [diff] [blame] | 1261 | /* n_devs will reflect the actual number of allocated devices */ |
| 1262 | n_devs = i; |
Mauro Carvalho Chehab | 980d4f1 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 1263 | |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | static void __exit vivi_exit(void) |
| 1268 | { |
Mauro Carvalho Chehab | 55712ff | 2007-12-10 04:38:11 -0300 | [diff] [blame] | 1269 | vivi_release(); |
Mauro Carvalho Chehab | 1e6dd65 | 2006-03-10 12:40:10 -0300 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | module_init(vivi_init); |
| 1273 | module_exit(vivi_exit); |