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