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