blob: ff39eb2f2d7b19204668e305cc8e8271afe8d0cf [file] [log] [blame]
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001/*
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 Osciake007a322011-01-19 13:02:29 -020010 * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski
11 * Copyright (c) 2010 Samsung Electronics
12 *
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030013 * 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 Chehab1e6dd652006-03-10 12:40:10 -030019#include <linux/errno.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030020#include <linux/kernel.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030021#include <linux/init.h>
22#include <linux/sched.h>
Randy Dunlap6b46c392010-05-07 15:22:26 -030023#include <linux/slab.h>
Hans Verkuil730947b2010-04-10 04:13:53 -030024#include <linux/font.h>
Matthias Kaehlcke51b54022007-07-02 10:19:38 -030025#include <linux/mutex.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030026#include <linux/videodev2.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030027#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Pawel Osciake007a322011-01-19 13:02:29 -020029#include <media/videobuf2-vmalloc.h>
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030030#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
Hans Verkuil7e996af2011-01-23 12:33:16 -020032#include <media/v4l2-ctrls.h>
Hans Verkuil2e4784d2011-03-11 20:01:54 -030033#include <media/v4l2-fh.h>
Hans Verkuilc7a52f82011-06-07 10:20:23 -030034#include <media/v4l2-event.h>
Hans Verkuil730947b2010-04-10 04:13:53 -030035#include <media/v4l2-common.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030036
Mauro Carvalho Chehab584ce482008-06-10 15:21:49 -030037#define VIVI_MODULE_NAME "vivi"
Carl Karsten745271a2008-06-10 00:02:32 -030038
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030039/* Wake up at about 30 fps */
40#define WAKE_NUMERATOR 30
41#define WAKE_DENOMINATOR 1001
42#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
43
Hans Verkuil730947b2010-04-10 04:13:53 -030044#define MAX_WIDTH 1920
45#define MAX_HEIGHT 1200
46
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030047#define VIVI_VERSION "0.8.1"
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030048
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030049MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
50MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
51MODULE_LICENSE("Dual BSD/GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030052MODULE_VERSION(VIVI_VERSION);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030053
54static unsigned video_nr = -1;
55module_param(video_nr, uint, 0644);
56MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
57
58static unsigned n_devs = 1;
59module_param(n_devs, uint, 0644);
60MODULE_PARM_DESC(n_devs, "number of video devices to create");
61
62static unsigned debug;
63module_param(debug, uint, 0644);
64MODULE_PARM_DESC(debug, "activates debug info");
65
66static unsigned int vid_limit = 16;
67module_param(vid_limit, uint, 0644);
68MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
69
Hans Verkuil730947b2010-04-10 04:13:53 -030070/* Global font descriptor */
71static const u8 *font8x16;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030072
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030073#define dprintk(dev, level, fmt, arg...) \
74 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030075
76/* ------------------------------------------------------------------
77 Basic structures
78 ------------------------------------------------------------------*/
79
80struct vivi_fmt {
81 char *name;
82 u32 fourcc; /* v4l2 format id */
83 int depth;
84};
85
Magnus Dammd891f472008-10-14 12:47:09 -030086static struct vivi_fmt formats[] = {
87 {
88 .name = "4:2:2, packed, YUYV",
89 .fourcc = V4L2_PIX_FMT_YUYV,
90 .depth = 16,
91 },
Magnus Dammfca36ba2008-10-14 12:47:25 -030092 {
93 .name = "4:2:2, packed, UYVY",
94 .fourcc = V4L2_PIX_FMT_UYVY,
95 .depth = 16,
96 },
Magnus Dammaeadb5d2008-10-14 12:47:35 -030097 {
98 .name = "RGB565 (LE)",
99 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
100 .depth = 16,
101 },
102 {
103 .name = "RGB565 (BE)",
104 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
105 .depth = 16,
106 },
Magnus Dammdef52392008-10-14 12:47:43 -0300107 {
108 .name = "RGB555 (LE)",
109 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
110 .depth = 16,
111 },
112 {
113 .name = "RGB555 (BE)",
114 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
115 .depth = 16,
116 },
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300117};
118
Magnus Dammd891f472008-10-14 12:47:09 -0300119static struct vivi_fmt *get_format(struct v4l2_format *f)
120{
121 struct vivi_fmt *fmt;
122 unsigned int k;
123
124 for (k = 0; k < ARRAY_SIZE(formats); k++) {
125 fmt = &formats[k];
126 if (fmt->fourcc == f->fmt.pix.pixelformat)
127 break;
128 }
129
130 if (k == ARRAY_SIZE(formats))
131 return NULL;
132
133 return &formats[k];
134}
135
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300136/* buffer for one video frame */
137struct vivi_buffer {
138 /* common v4l buffer stuff -- must be first */
Pawel Osciake007a322011-01-19 13:02:29 -0200139 struct vb2_buffer vb;
140 struct list_head list;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300141 struct vivi_fmt *fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300142};
143
144struct vivi_dmaqueue {
145 struct list_head active;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300146
147 /* thread for generating video stream*/
148 struct task_struct *kthread;
149 wait_queue_head_t wq;
150 /* Counters to control fps rate */
151 int frame;
152 int ini_jiffies;
153};
154
155static LIST_HEAD(vivi_devlist);
156
157struct vivi_dev {
158 struct list_head vivi_devlist;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300159 struct v4l2_device v4l2_dev;
Hans Verkuil7e996af2011-01-23 12:33:16 -0200160 struct v4l2_ctrl_handler ctrl_handler;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300161
Hans Verkuil730947b2010-04-10 04:13:53 -0300162 /* controls */
Hans Verkuil7e996af2011-01-23 12:33:16 -0200163 struct v4l2_ctrl *brightness;
164 struct v4l2_ctrl *contrast;
165 struct v4l2_ctrl *saturation;
166 struct v4l2_ctrl *hue;
Hans Verkuila1c894f2011-06-07 06:34:41 -0300167 struct {
168 /* autogain/gain cluster */
169 struct v4l2_ctrl *autogain;
170 struct v4l2_ctrl *gain;
171 };
Hans Verkuil7e996af2011-01-23 12:33:16 -0200172 struct v4l2_ctrl *volume;
173 struct v4l2_ctrl *button;
174 struct v4l2_ctrl *boolean;
175 struct v4l2_ctrl *int32;
176 struct v4l2_ctrl *int64;
177 struct v4l2_ctrl *menu;
178 struct v4l2_ctrl *string;
Hans Verkuilb6d17a52011-03-29 16:33:11 -0300179 struct v4l2_ctrl *bitmask;
Sakari Ailusc5203312011-08-05 06:38:05 -0300180 struct v4l2_ctrl *int_menu;
Hans Verkuil730947b2010-04-10 04:13:53 -0300181
Mauro Carvalho Chehab55862ac2007-12-13 16:13:37 -0300182 spinlock_t slock;
Brandon Philipsaa9dbac2008-04-02 18:10:59 -0300183 struct mutex mutex;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300184
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300185 /* various device info */
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -0300186 struct video_device *vfd;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300187
188 struct vivi_dmaqueue vidq;
189
190 /* Several counters */
Hans Verkuil730947b2010-04-10 04:13:53 -0300191 unsigned ms;
Mauro Carvalho Chehabdfd8c042008-01-13 19:36:11 -0300192 unsigned long jiffies;
Hans Verkuil7e996af2011-01-23 12:33:16 -0200193 unsigned button_pressed;
Mauro Carvalho Chehab025341d2007-12-10 04:43:38 -0300194
195 int mv_count; /* Controls bars movement */
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300196
197 /* Input Number */
198 int input;
Hans Verkuilc41ee242009-02-14 13:43:44 -0300199
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300200 /* video capture */
201 struct vivi_fmt *fmt;
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300202 unsigned int width, height;
Pawel Osciake007a322011-01-19 13:02:29 -0200203 struct vb2_queue vb_vidq;
204 enum v4l2_field field;
205 unsigned int field_count;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300206
Hans Verkuil730947b2010-04-10 04:13:53 -0300207 u8 bars[9][3];
208 u8 line[MAX_WIDTH * 4];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300209};
210
211/* ------------------------------------------------------------------
212 DMA and thread functions
213 ------------------------------------------------------------------*/
214
215/* Bars and Colors should match positions */
216
217enum colors {
218 WHITE,
Hans Verkuil730947b2010-04-10 04:13:53 -0300219 AMBER,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300220 CYAN,
221 GREEN,
222 MAGENTA,
223 RED,
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300224 BLUE,
225 BLACK,
Hans Verkuil730947b2010-04-10 04:13:53 -0300226 TEXT_BLACK,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300227};
228
Hans Verkuil730947b2010-04-10 04:13:53 -0300229/* R G B */
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300230#define COLOR_WHITE {204, 204, 204}
Hans Verkuil730947b2010-04-10 04:13:53 -0300231#define COLOR_AMBER {208, 208, 0}
232#define COLOR_CYAN { 0, 206, 206}
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300233#define COLOR_GREEN { 0, 239, 0}
234#define COLOR_MAGENTA {239, 0, 239}
235#define COLOR_RED {205, 0, 0}
236#define COLOR_BLUE { 0, 0, 255}
237#define COLOR_BLACK { 0, 0, 0}
238
239struct bar_std {
Hans Verkuil730947b2010-04-10 04:13:53 -0300240 u8 bar[9][3];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300241};
242
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300243/* Maximum number of bars are 10 - otherwise, the input print code
244 should be modified */
245static struct bar_std bars[] = {
246 { /* Standard ITU-R color bar sequence */
Hans Verkuil730947b2010-04-10 04:13:53 -0300247 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
248 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300249 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300250 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
251 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300252 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300253 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
254 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300255 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300256 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
257 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300258 },
259};
260
261#define NUM_INPUTS ARRAY_SIZE(bars)
262
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300263#define TO_Y(r, g, b) \
264 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300265/* RGB to V(Cr) Color transform */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300266#define TO_V(r, g, b) \
267 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300268/* RGB to U(Cb) Color transform */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300269#define TO_U(r, g, b) \
270 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300271
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300272/* precalculate color bar values to speed up rendering */
Hans Verkuil730947b2010-04-10 04:13:53 -0300273static void precalculate_bars(struct vivi_dev *dev)
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300274{
Hans Verkuil730947b2010-04-10 04:13:53 -0300275 u8 r, g, b;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300276 int k, is_yuv;
277
Hans Verkuil730947b2010-04-10 04:13:53 -0300278 for (k = 0; k < 9; k++) {
279 r = bars[dev->input].bar[k][0];
280 g = bars[dev->input].bar[k][1];
281 b = bars[dev->input].bar[k][2];
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300282 is_yuv = 0;
283
Hans Verkuil730947b2010-04-10 04:13:53 -0300284 switch (dev->fmt->fourcc) {
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300285 case V4L2_PIX_FMT_YUYV:
286 case V4L2_PIX_FMT_UYVY:
287 is_yuv = 1;
288 break;
289 case V4L2_PIX_FMT_RGB565:
290 case V4L2_PIX_FMT_RGB565X:
291 r >>= 3;
292 g >>= 2;
293 b >>= 3;
294 break;
295 case V4L2_PIX_FMT_RGB555:
296 case V4L2_PIX_FMT_RGB555X:
297 r >>= 3;
298 g >>= 3;
299 b >>= 3;
300 break;
301 }
302
303 if (is_yuv) {
Hans Verkuil730947b2010-04-10 04:13:53 -0300304 dev->bars[k][0] = TO_Y(r, g, b); /* Luma */
305 dev->bars[k][1] = TO_U(r, g, b); /* Cb */
306 dev->bars[k][2] = TO_V(r, g, b); /* Cr */
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300307 } else {
Hans Verkuil730947b2010-04-10 04:13:53 -0300308 dev->bars[k][0] = r;
309 dev->bars[k][1] = g;
310 dev->bars[k][2] = b;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300311 }
312 }
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300313}
314
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300315#define TSTAMP_MIN_Y 24
316#define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
317#define TSTAMP_INPUT_X 10
318#define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300319
Hans Verkuil730947b2010-04-10 04:13:53 -0300320static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300321{
Hans Verkuil730947b2010-04-10 04:13:53 -0300322 u8 r_y, g_u, b_v;
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300323 int color;
Hans Verkuil730947b2010-04-10 04:13:53 -0300324 u8 *p;
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300325
Hans Verkuil730947b2010-04-10 04:13:53 -0300326 r_y = dev->bars[colorpos][0]; /* R or precalculated Y */
327 g_u = dev->bars[colorpos][1]; /* G or precalculated U */
328 b_v = dev->bars[colorpos][2]; /* B or precalculated V */
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300329
330 for (color = 0; color < 4; color++) {
331 p = buf + color;
332
Hans Verkuil730947b2010-04-10 04:13:53 -0300333 switch (dev->fmt->fourcc) {
Magnus Dammd891f472008-10-14 12:47:09 -0300334 case V4L2_PIX_FMT_YUYV:
335 switch (color) {
336 case 0:
337 case 2:
338 *p = r_y;
339 break;
340 case 1:
341 *p = g_u;
342 break;
343 case 3:
344 *p = b_v;
345 break;
346 }
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300347 break;
Magnus Dammfca36ba2008-10-14 12:47:25 -0300348 case V4L2_PIX_FMT_UYVY:
349 switch (color) {
350 case 1:
351 case 3:
352 *p = r_y;
353 break;
354 case 0:
355 *p = g_u;
356 break;
357 case 2:
358 *p = b_v;
359 break;
360 }
361 break;
Magnus Dammaeadb5d2008-10-14 12:47:35 -0300362 case V4L2_PIX_FMT_RGB565:
363 switch (color) {
364 case 0:
365 case 2:
366 *p = (g_u << 5) | b_v;
367 break;
368 case 1:
369 case 3:
370 *p = (r_y << 3) | (g_u >> 3);
371 break;
372 }
373 break;
374 case V4L2_PIX_FMT_RGB565X:
375 switch (color) {
376 case 0:
377 case 2:
378 *p = (r_y << 3) | (g_u >> 3);
379 break;
380 case 1:
381 case 3:
382 *p = (g_u << 5) | b_v;
383 break;
384 }
385 break;
Magnus Dammdef52392008-10-14 12:47:43 -0300386 case V4L2_PIX_FMT_RGB555:
387 switch (color) {
388 case 0:
389 case 2:
390 *p = (g_u << 5) | b_v;
391 break;
392 case 1:
393 case 3:
394 *p = (r_y << 2) | (g_u >> 3);
395 break;
396 }
397 break;
398 case V4L2_PIX_FMT_RGB555X:
399 switch (color) {
400 case 0:
401 case 2:
402 *p = (r_y << 2) | (g_u >> 3);
403 break;
404 case 1:
405 case 3:
406 *p = (g_u << 5) | b_v;
407 break;
408 }
409 break;
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300410 }
411 }
412}
413
Hans Verkuil730947b2010-04-10 04:13:53 -0300414static void precalculate_line(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300415{
Hans Verkuil730947b2010-04-10 04:13:53 -0300416 int w;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300417
Hans Verkuil730947b2010-04-10 04:13:53 -0300418 for (w = 0; w < dev->width * 2; w += 2) {
419 int colorpos = (w / (dev->width / 8) % 8);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300420
Hans Verkuil730947b2010-04-10 04:13:53 -0300421 gen_twopix(dev, dev->line + w * 2, colorpos);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300422 }
Hans Verkuil730947b2010-04-10 04:13:53 -0300423}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300424
Hans Verkuil730947b2010-04-10 04:13:53 -0300425static void gen_text(struct vivi_dev *dev, char *basep,
426 int y, int x, char *text)
427{
428 int line;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300429
Hans Verkuil730947b2010-04-10 04:13:53 -0300430 /* Checks if it is possible to show string */
431 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
432 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300433
434 /* Print stream time */
Hans Verkuil730947b2010-04-10 04:13:53 -0300435 for (line = y; line < y + 16; line++) {
436 int j = 0;
437 char *pos = basep + line * dev->width * 2 + x * 2;
438 char *s;
439
440 for (s = text; *s; s++) {
441 u8 chr = font8x16[*s * 16 + line - y];
442 int i;
443
444 for (i = 0; i < 7; i++, j++) {
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300445 /* Draw white font on black background */
Hans Verkuil730947b2010-04-10 04:13:53 -0300446 if (chr & (1 << (7 - i)))
447 gen_twopix(dev, pos + j * 2, WHITE);
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300448 else
Hans Verkuil730947b2010-04-10 04:13:53 -0300449 gen_twopix(dev, pos + j * 2, TEXT_BLACK);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300450 }
451 }
452 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300453}
Brandon Philips78718e52008-04-02 18:10:59 -0300454
Hans Verkuil730947b2010-04-10 04:13:53 -0300455static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300456{
Pawel Osciake007a322011-01-19 13:02:29 -0200457 int wmax = dev->width;
458 int hmax = dev->height;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300459 struct timeval ts;
Pawel Osciake007a322011-01-19 13:02:29 -0200460 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
Hans Verkuil730947b2010-04-10 04:13:53 -0300461 unsigned ms;
462 char str[100];
463 int h, line = 1;
Hans Verkuila1c894f2011-06-07 06:34:41 -0300464 s32 gain;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300465
Marcin Slusarz5c554e62008-06-22 09:11:40 -0300466 if (!vbuf)
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300467 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300468
Hans Verkuil730947b2010-04-10 04:13:53 -0300469 for (h = 0; h < hmax; h++)
470 memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300471
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300472 /* Updates stream time */
473
Hans Verkuil730947b2010-04-10 04:13:53 -0300474 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300475 dev->jiffies = jiffies;
Hans Verkuil730947b2010-04-10 04:13:53 -0300476 ms = dev->ms;
477 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
478 (ms / (60 * 60 * 1000)) % 24,
479 (ms / (60 * 1000)) % 60,
480 (ms / 1000) % 60,
481 ms % 1000);
482 gen_text(dev, vbuf, line++ * 16, 16, str);
483 snprintf(str, sizeof(str), " %dx%d, input %d ",
484 dev->width, dev->height, dev->input);
485 gen_text(dev, vbuf, line++ * 16, 16, str);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300486
Hans Verkuila1c894f2011-06-07 06:34:41 -0300487 gain = v4l2_ctrl_g_ctrl(dev->gain);
Hans Verkuil7e996af2011-01-23 12:33:16 -0200488 mutex_lock(&dev->ctrl_handler.lock);
Hans Verkuil730947b2010-04-10 04:13:53 -0300489 snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
Hans Verkuil7e996af2011-01-23 12:33:16 -0200490 dev->brightness->cur.val,
491 dev->contrast->cur.val,
492 dev->saturation->cur.val,
493 dev->hue->cur.val);
Hans Verkuil730947b2010-04-10 04:13:53 -0300494 gen_text(dev, vbuf, line++ * 16, 16, str);
Hans Verkuila1c894f2011-06-07 06:34:41 -0300495 snprintf(str, sizeof(str), " autogain %d, gain %3d, volume %3d ",
496 dev->autogain->cur.val, gain, dev->volume->cur.val);
Hans Verkuil730947b2010-04-10 04:13:53 -0300497 gen_text(dev, vbuf, line++ * 16, 16, str);
Hans Verkuilb6d17a52011-03-29 16:33:11 -0300498 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
Hans Verkuil7e996af2011-01-23 12:33:16 -0200499 dev->int32->cur.val,
Hans Verkuilb6d17a52011-03-29 16:33:11 -0300500 dev->int64->cur.val64,
501 dev->bitmask->cur.val);
Hans Verkuil7e996af2011-01-23 12:33:16 -0200502 gen_text(dev, vbuf, line++ * 16, 16, str);
503 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
504 dev->boolean->cur.val,
505 dev->menu->qmenu[dev->menu->cur.val],
506 dev->string->cur.string);
Sakari Ailusc5203312011-08-05 06:38:05 -0300507 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
508 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
509 dev->int_menu->cur.val);
510 gen_text(dev, vbuf, line++ * 16, 16, str);
Hans Verkuil7e996af2011-01-23 12:33:16 -0200511 mutex_unlock(&dev->ctrl_handler.lock);
512 gen_text(dev, vbuf, line++ * 16, 16, str);
513 if (dev->button_pressed) {
514 dev->button_pressed--;
515 snprintf(str, sizeof(str), " button pressed!");
516 gen_text(dev, vbuf, line++ * 16, 16, str);
517 }
Hans Verkuil730947b2010-04-10 04:13:53 -0300518
519 dev->mv_count += 2;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300520
Pawel Osciake007a322011-01-19 13:02:29 -0200521 buf->vb.v4l2_buf.field = dev->field;
522 dev->field_count++;
523 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300524 do_gettimeofday(&ts);
Pawel Osciake007a322011-01-19 13:02:29 -0200525 buf->vb.v4l2_buf.timestamp = ts;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300526}
527
Hans Verkuil730947b2010-04-10 04:13:53 -0300528static void vivi_thread_tick(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300529{
Brandon Philips78718e52008-04-02 18:10:59 -0300530 struct vivi_dmaqueue *dma_q = &dev->vidq;
Hans Verkuil730947b2010-04-10 04:13:53 -0300531 struct vivi_buffer *buf;
Brandon Philips78718e52008-04-02 18:10:59 -0300532 unsigned long flags = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300533
Brandon Philips78718e52008-04-02 18:10:59 -0300534 dprintk(dev, 1, "Thread tick\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300535
Brandon Philips78718e52008-04-02 18:10:59 -0300536 spin_lock_irqsave(&dev->slock, flags);
537 if (list_empty(&dma_q->active)) {
538 dprintk(dev, 1, "No active queue to serve\n");
Hans Verkuil1de5be52011-07-05 07:19:23 -0300539 spin_unlock_irqrestore(&dev->slock, flags);
540 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300541 }
Brandon Philips78718e52008-04-02 18:10:59 -0300542
Pawel Osciake007a322011-01-19 13:02:29 -0200543 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
544 list_del(&buf->list);
Hans Verkuil1de5be52011-07-05 07:19:23 -0300545 spin_unlock_irqrestore(&dev->slock, flags);
Brandon Philips78718e52008-04-02 18:10:59 -0300546
Pawel Osciake007a322011-01-19 13:02:29 -0200547 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
Brandon Philips78718e52008-04-02 18:10:59 -0300548
549 /* Fill buffer */
Hans Verkuil730947b2010-04-10 04:13:53 -0300550 vivi_fillbuff(dev, buf);
Brandon Philips78718e52008-04-02 18:10:59 -0300551 dprintk(dev, 1, "filled buffer %p\n", buf);
552
Pawel Osciake007a322011-01-19 13:02:29 -0200553 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
554 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300555}
556
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300557#define frames_to_ms(frames) \
558 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
559
Hans Verkuil730947b2010-04-10 04:13:53 -0300560static void vivi_sleep(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300561{
Brandon Philips78718e52008-04-02 18:10:59 -0300562 struct vivi_dmaqueue *dma_q = &dev->vidq;
563 int timeout;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300564 DECLARE_WAITQUEUE(wait, current);
565
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300566 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300567 (unsigned long)dma_q);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300568
569 add_wait_queue(&dma_q->wq, &wait);
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300570 if (kthread_should_stop())
571 goto stop_task;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300572
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300573 /* Calculate time to wake up */
Brandon Philips78718e52008-04-02 18:10:59 -0300574 timeout = msecs_to_jiffies(frames_to_ms(1));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300575
Hans Verkuil730947b2010-04-10 04:13:53 -0300576 vivi_thread_tick(dev);
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300577
578 schedule_timeout_interruptible(timeout);
579
580stop_task:
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300581 remove_wait_queue(&dma_q->wq, &wait);
582 try_to_freeze();
583}
584
Adrian Bunk972c3512006-04-27 21:06:50 -0300585static int vivi_thread(void *data)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300586{
Hans Verkuil730947b2010-04-10 04:13:53 -0300587 struct vivi_dev *dev = data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300588
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300589 dprintk(dev, 1, "thread started\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300590
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700591 set_freezable();
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300592
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300593 for (;;) {
Hans Verkuil730947b2010-04-10 04:13:53 -0300594 vivi_sleep(dev);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300595
596 if (kthread_should_stop())
597 break;
598 }
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300599 dprintk(dev, 1, "thread: exit\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300600 return 0;
601}
602
Pawel Osciake007a322011-01-19 13:02:29 -0200603static int vivi_start_generating(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300604{
Brandon Philips78718e52008-04-02 18:10:59 -0300605 struct vivi_dmaqueue *dma_q = &dev->vidq;
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300606
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300607 dprintk(dev, 1, "%s\n", __func__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300608
Hans Verkuil730947b2010-04-10 04:13:53 -0300609 /* Resets frame counters */
610 dev->ms = 0;
611 dev->mv_count = 0;
612 dev->jiffies = jiffies;
613
614 dma_q->frame = 0;
615 dma_q->ini_jiffies = jiffies;
616 dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300617
Akinobu Mita054afee2006-12-20 10:04:00 -0300618 if (IS_ERR(dma_q->kthread)) {
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300619 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
Pawel Osciake007a322011-01-19 13:02:29 -0200620 return PTR_ERR(dma_q->kthread);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300621 }
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300622 /* Wakes thread */
623 wake_up_interruptible(&dma_q->wq);
624
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300625 dprintk(dev, 1, "returning from %s\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200626 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300627}
628
Pawel Osciake007a322011-01-19 13:02:29 -0200629static void vivi_stop_generating(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300630{
Hans Verkuil730947b2010-04-10 04:13:53 -0300631 struct vivi_dmaqueue *dma_q = &dev->vidq;
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300632
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300633 dprintk(dev, 1, "%s\n", __func__);
Hans Verkuil730947b2010-04-10 04:13:53 -0300634
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300635 /* shutdown control thread */
636 if (dma_q->kthread) {
637 kthread_stop(dma_q->kthread);
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300638 dma_q->kthread = NULL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300639 }
Hans Verkuil730947b2010-04-10 04:13:53 -0300640
Pawel Osciake007a322011-01-19 13:02:29 -0200641 /*
642 * Typical driver might need to wait here until dma engine stops.
643 * In this case we can abort imiedetly, so it's just a noop.
644 */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300645
Pawel Osciake007a322011-01-19 13:02:29 -0200646 /* Release all active buffers */
647 while (!list_empty(&dma_q->active)) {
648 struct vivi_buffer *buf;
649 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
650 list_del(&buf->list);
651 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
652 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
653 }
654}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300655/* ------------------------------------------------------------------
656 Videobuf operations
657 ------------------------------------------------------------------*/
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300658static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
659 unsigned int *nbuffers, unsigned int *nplanes,
660 unsigned int sizes[], void *alloc_ctxs[])
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300661{
Pawel Osciake007a322011-01-19 13:02:29 -0200662 struct vivi_dev *dev = vb2_get_drv_priv(vq);
663 unsigned long size;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300664
Pawel Osciake007a322011-01-19 13:02:29 -0200665 size = dev->width * dev->height * 2;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300666
Pawel Osciake007a322011-01-19 13:02:29 -0200667 if (0 == *nbuffers)
668 *nbuffers = 32;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300669
Pawel Osciake007a322011-01-19 13:02:29 -0200670 while (size * *nbuffers > vid_limit * 1024 * 1024)
671 (*nbuffers)--;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300672
Pawel Osciake007a322011-01-19 13:02:29 -0200673 *nplanes = 1;
674
675 sizes[0] = size;
676
677 /*
678 * videobuf2-vmalloc allocator is context-less so no need to set
679 * alloc_ctxs array.
680 */
681
682 dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
683 *nbuffers, size);
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300684
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300685 return 0;
686}
687
Pawel Osciake007a322011-01-19 13:02:29 -0200688static int buffer_init(struct vb2_buffer *vb)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300689{
Pawel Osciake007a322011-01-19 13:02:29 -0200690 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300691
Hans Verkuil730947b2010-04-10 04:13:53 -0300692 BUG_ON(NULL == dev->fmt);
Brandon Philips78718e52008-04-02 18:10:59 -0300693
Pawel Osciake007a322011-01-19 13:02:29 -0200694 /*
695 * This callback is called once per buffer, after its allocation.
696 *
697 * Vivi does not allow changing format during streaming, but it is
698 * possible to do so when streaming is paused (i.e. in streamoff state).
699 * Buffers however are not freed when going into streamoff and so
700 * buffer size verification has to be done in buffer_prepare, on each
701 * qbuf.
702 * It would be best to move verification code here to buf_init and
703 * s_fmt though.
704 */
705
706 return 0;
707}
708
709static int buffer_prepare(struct vb2_buffer *vb)
710{
711 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
712 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
713 unsigned long size;
714
715 dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
716
717 BUG_ON(NULL == dev->fmt);
718
719 /*
720 * Theses properties only change when queue is idle, see s_fmt.
721 * The below checks should not be performed here, on each
722 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
723 * should thus be moved to buffer_init and s_fmt.
724 */
Hans Verkuil730947b2010-04-10 04:13:53 -0300725 if (dev->width < 48 || dev->width > MAX_WIDTH ||
726 dev->height < 32 || dev->height > MAX_HEIGHT)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300727 return -EINVAL;
Brandon Philips78718e52008-04-02 18:10:59 -0300728
Pawel Osciake007a322011-01-19 13:02:29 -0200729 size = dev->width * dev->height * 2;
730 if (vb2_plane_size(vb, 0) < size) {
731 dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
732 __func__, vb2_plane_size(vb, 0), size);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300733 return -EINVAL;
Pawel Osciake007a322011-01-19 13:02:29 -0200734 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300735
Pawel Osciake007a322011-01-19 13:02:29 -0200736 vb2_set_plane_payload(&buf->vb, 0, size);
737
738 buf->fmt = dev->fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300739
Hans Verkuil730947b2010-04-10 04:13:53 -0300740 precalculate_bars(dev);
741 precalculate_line(dev);
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300742
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300743 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300744}
745
Pawel Osciake007a322011-01-19 13:02:29 -0200746static int buffer_finish(struct vb2_buffer *vb)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300747{
Pawel Osciake007a322011-01-19 13:02:29 -0200748 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
749 dprintk(dev, 1, "%s\n", __func__);
750 return 0;
751}
752
753static void buffer_cleanup(struct vb2_buffer *vb)
754{
755 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
756 dprintk(dev, 1, "%s\n", __func__);
757
758}
759
760static void buffer_queue(struct vb2_buffer *vb)
761{
762 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuil730947b2010-04-10 04:13:53 -0300763 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
Brandon Philips78718e52008-04-02 18:10:59 -0300764 struct vivi_dmaqueue *vidq = &dev->vidq;
Pawel Osciake007a322011-01-19 13:02:29 -0200765 unsigned long flags = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300766
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300767 dprintk(dev, 1, "%s\n", __func__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300768
Pawel Osciake007a322011-01-19 13:02:29 -0200769 spin_lock_irqsave(&dev->slock, flags);
770 list_add_tail(&buf->list, &vidq->active);
771 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300772}
773
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300774static int start_streaming(struct vb2_queue *vq, unsigned int count)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300775{
Pawel Osciake007a322011-01-19 13:02:29 -0200776 struct vivi_dev *dev = vb2_get_drv_priv(vq);
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300777 dprintk(dev, 1, "%s\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200778 return vivi_start_generating(dev);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300779}
780
Pawel Osciake007a322011-01-19 13:02:29 -0200781/* abort streaming and wait for last buffer */
782static int stop_streaming(struct vb2_queue *vq)
783{
784 struct vivi_dev *dev = vb2_get_drv_priv(vq);
785 dprintk(dev, 1, "%s\n", __func__);
786 vivi_stop_generating(dev);
787 return 0;
788}
789
790static void vivi_lock(struct vb2_queue *vq)
791{
792 struct vivi_dev *dev = vb2_get_drv_priv(vq);
793 mutex_lock(&dev->mutex);
794}
795
796static void vivi_unlock(struct vb2_queue *vq)
797{
798 struct vivi_dev *dev = vb2_get_drv_priv(vq);
799 mutex_unlock(&dev->mutex);
800}
801
802
803static struct vb2_ops vivi_video_qops = {
804 .queue_setup = queue_setup,
805 .buf_init = buffer_init,
806 .buf_prepare = buffer_prepare,
807 .buf_finish = buffer_finish,
808 .buf_cleanup = buffer_cleanup,
809 .buf_queue = buffer_queue,
810 .start_streaming = start_streaming,
811 .stop_streaming = stop_streaming,
812 .wait_prepare = vivi_unlock,
813 .wait_finish = vivi_lock,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300814};
815
816/* ------------------------------------------------------------------
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300817 IOCTL vidioc handling
818 ------------------------------------------------------------------*/
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300819static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300820 struct v4l2_capability *cap)
821{
Hans Verkuil730947b2010-04-10 04:13:53 -0300822 struct vivi_dev *dev = video_drvdata(file);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300823
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300824 strcpy(cap->driver, "vivi");
825 strcpy(cap->card, "vivi");
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300826 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
Hans Verkuil23268ae2012-01-24 05:24:36 -0300827 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
828 V4L2_CAP_READWRITE;
829 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300830 return 0;
831}
832
Hans Verkuil78b526a2008-05-28 12:16:41 -0300833static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300834 struct v4l2_fmtdesc *f)
835{
Magnus Dammd891f472008-10-14 12:47:09 -0300836 struct vivi_fmt *fmt;
837
838 if (f->index >= ARRAY_SIZE(formats))
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300839 return -EINVAL;
840
Magnus Dammd891f472008-10-14 12:47:09 -0300841 fmt = &formats[f->index];
842
843 strlcpy(f->description, fmt->name, sizeof(f->description));
844 f->pixelformat = fmt->fourcc;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300845 return 0;
846}
847
Hans Verkuil78b526a2008-05-28 12:16:41 -0300848static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300849 struct v4l2_format *f)
850{
Hans Verkuil730947b2010-04-10 04:13:53 -0300851 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300852
Hans Verkuil730947b2010-04-10 04:13:53 -0300853 f->fmt.pix.width = dev->width;
854 f->fmt.pix.height = dev->height;
Pawel Osciake007a322011-01-19 13:02:29 -0200855 f->fmt.pix.field = dev->field;
Hans Verkuil730947b2010-04-10 04:13:53 -0300856 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300857 f->fmt.pix.bytesperline =
Hans Verkuil730947b2010-04-10 04:13:53 -0300858 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300859 f->fmt.pix.sizeimage =
860 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil8c79eec2011-07-29 07:19:46 -0300861 if (dev->fmt->fourcc == V4L2_PIX_FMT_YUYV ||
862 dev->fmt->fourcc == V4L2_PIX_FMT_UYVY)
863 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
864 else
865 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
Hans Verkuil730947b2010-04-10 04:13:53 -0300866 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300867}
868
Hans Verkuil78b526a2008-05-28 12:16:41 -0300869static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300870 struct v4l2_format *f)
871{
Hans Verkuil730947b2010-04-10 04:13:53 -0300872 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300873 struct vivi_fmt *fmt;
874 enum v4l2_field field;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300875
Magnus Dammd891f472008-10-14 12:47:09 -0300876 fmt = get_format(f);
877 if (!fmt) {
878 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
879 f->fmt.pix.pixelformat);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300880 return -EINVAL;
881 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300882
883 field = f->fmt.pix.field;
884
885 if (field == V4L2_FIELD_ANY) {
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300886 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300887 } else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300888 dprintk(dev, 1, "Field type invalid.\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300889 return -EINVAL;
890 }
891
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300892 f->fmt.pix.field = field;
Hans Verkuil730947b2010-04-10 04:13:53 -0300893 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
894 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300895 f->fmt.pix.bytesperline =
896 (f->fmt.pix.width * fmt->depth) >> 3;
897 f->fmt.pix.sizeimage =
898 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil8c79eec2011-07-29 07:19:46 -0300899 if (fmt->fourcc == V4L2_PIX_FMT_YUYV ||
900 fmt->fourcc == V4L2_PIX_FMT_UYVY)
901 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
902 else
903 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300904 return 0;
905}
906
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300907static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
908 struct v4l2_format *f)
909{
Hans Verkuil730947b2010-04-10 04:13:53 -0300910 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200911 struct vb2_queue *q = &dev->vb_vidq;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300912
Hans Verkuil730947b2010-04-10 04:13:53 -0300913 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300914 if (ret < 0)
915 return ret;
916
Pawel Osciake007a322011-01-19 13:02:29 -0200917 if (vb2_is_streaming(q)) {
Hans Verkuil730947b2010-04-10 04:13:53 -0300918 dprintk(dev, 1, "%s device busy\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200919 return -EBUSY;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300920 }
921
Hans Verkuil730947b2010-04-10 04:13:53 -0300922 dev->fmt = get_format(f);
923 dev->width = f->fmt.pix.width;
924 dev->height = f->fmt.pix.height;
Pawel Osciake007a322011-01-19 13:02:29 -0200925 dev->field = f->fmt.pix.field;
926
927 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300928}
929
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300930static int vidioc_reqbufs(struct file *file, void *priv,
931 struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300932{
Hans Verkuil730947b2010-04-10 04:13:53 -0300933 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200934 return vb2_reqbufs(&dev->vb_vidq, p);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300935}
936
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300937static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300938{
Hans Verkuil730947b2010-04-10 04:13:53 -0300939 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200940 return vb2_querybuf(&dev->vb_vidq, p);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300941}
942
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300943static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300944{
Hans Verkuil730947b2010-04-10 04:13:53 -0300945 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200946 return vb2_qbuf(&dev->vb_vidq, p);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300947}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300948
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300949static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300950{
Hans Verkuil730947b2010-04-10 04:13:53 -0300951 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200952 return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300953}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300954
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300955static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300956{
Hans Verkuil730947b2010-04-10 04:13:53 -0300957 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200958 return vb2_streamon(&dev->vb_vidq, i);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300959}
960
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300961static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300962{
Hans Verkuil730947b2010-04-10 04:13:53 -0300963 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200964 return vb2_streamoff(&dev->vb_vidq, i);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300965}
966
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300967static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300968{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300969 return 0;
970}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300971
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300972/* only one input in this sample driver */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300973static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300974 struct v4l2_input *inp)
975{
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300976 if (inp->index >= NUM_INPUTS)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300977 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300978
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300979 inp->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab784c6682007-12-13 06:35:26 -0300980 inp->std = V4L2_STD_525_60;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300981 sprintf(inp->name, "Camera %u", inp->index);
Hans Verkuil730947b2010-04-10 04:13:53 -0300982 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300983}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300984
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300985static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300986{
Hans Verkuil730947b2010-04-10 04:13:53 -0300987 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300988
989 *i = dev->input;
Hans Verkuil730947b2010-04-10 04:13:53 -0300990 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300991}
Hans Verkuil730947b2010-04-10 04:13:53 -0300992
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300993static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300994{
Hans Verkuil730947b2010-04-10 04:13:53 -0300995 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300996
997 if (i >= NUM_INPUTS)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300998 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300999
Hans Verkuilc7a52f82011-06-07 10:20:23 -03001000 if (i == dev->input)
1001 return 0;
1002
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -03001003 dev->input = i;
Hans Verkuil730947b2010-04-10 04:13:53 -03001004 precalculate_bars(dev);
1005 precalculate_line(dev);
1006 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001007}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001008
Hans Verkuil730947b2010-04-10 04:13:53 -03001009/* --- controls ---------------------------------------------- */
Hans Verkuil7e996af2011-01-23 12:33:16 -02001010
Hans Verkuila1c894f2011-06-07 06:34:41 -03001011static int vivi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1012{
1013 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
1014
1015 if (ctrl == dev->autogain)
1016 dev->gain->val = jiffies & 0xff;
1017 return 0;
1018}
1019
Hans Verkuil7e996af2011-01-23 12:33:16 -02001020static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001021{
Hans Verkuil7e996af2011-01-23 12:33:16 -02001022 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001023
Hans Verkuil7e996af2011-01-23 12:33:16 -02001024 if (ctrl == dev->button)
1025 dev->button_pressed = 30;
1026 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001027}
1028
1029/* ------------------------------------------------------------------
1030 File operations for the device
1031 ------------------------------------------------------------------*/
1032
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001033static ssize_t
1034vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1035{
Hans Verkuil730947b2010-04-10 04:13:53 -03001036 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001037
Pawel Osciake007a322011-01-19 13:02:29 -02001038 dprintk(dev, 1, "read called\n");
1039 return vb2_read(&dev->vb_vidq, data, count, ppos,
1040 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001041}
1042
1043static unsigned int
1044vivi_poll(struct file *file, struct poll_table_struct *wait)
1045{
Hans Verkuil730947b2010-04-10 04:13:53 -03001046 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -02001047 struct vb2_queue *q = &dev->vb_vidq;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001048
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001049 dprintk(dev, 1, "%s\n", __func__);
Hans Verkuil0bf0f712011-07-13 04:28:27 -03001050 return vb2_poll(q, file, wait);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001051}
1052
Hans Verkuilbec43662008-12-30 06:58:20 -03001053static int vivi_close(struct file *file)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001054{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001055 struct video_device *vdev = video_devdata(file);
Hans Verkuil730947b2010-04-10 04:13:53 -03001056 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001057
Pawel Osciake007a322011-01-19 13:02:29 -02001058 dprintk(dev, 1, "close called (dev=%s), file %p\n",
1059 video_device_node_name(vdev), file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001060
Hans Verkuil2e4784d2011-03-11 20:01:54 -03001061 if (v4l2_fh_is_singular_file(file))
Pawel Osciake007a322011-01-19 13:02:29 -02001062 vb2_queue_release(&dev->vb_vidq);
Hans Verkuil2e4784d2011-03-11 20:01:54 -03001063 return v4l2_fh_release(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001064}
1065
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -03001066static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001067{
Hans Verkuil730947b2010-04-10 04:13:53 -03001068 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001069 int ret;
1070
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -03001071 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001072
Pawel Osciake007a322011-01-19 13:02:29 -02001073 ret = vb2_mmap(&dev->vb_vidq, vma);
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -03001074 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001075 (unsigned long)vma->vm_start,
Hans Verkuil730947b2010-04-10 04:13:53 -03001076 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001077 ret);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001078 return ret;
1079}
1080
Hans Verkuil7e996af2011-01-23 12:33:16 -02001081static const struct v4l2_ctrl_ops vivi_ctrl_ops = {
Hans Verkuila1c894f2011-06-07 06:34:41 -03001082 .g_volatile_ctrl = vivi_g_volatile_ctrl,
Hans Verkuil7e996af2011-01-23 12:33:16 -02001083 .s_ctrl = vivi_s_ctrl,
1084};
1085
1086#define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
1087
1088static const struct v4l2_ctrl_config vivi_ctrl_button = {
1089 .ops = &vivi_ctrl_ops,
1090 .id = VIVI_CID_CUSTOM_BASE + 0,
1091 .name = "Button",
1092 .type = V4L2_CTRL_TYPE_BUTTON,
1093};
1094
1095static const struct v4l2_ctrl_config vivi_ctrl_boolean = {
1096 .ops = &vivi_ctrl_ops,
1097 .id = VIVI_CID_CUSTOM_BASE + 1,
1098 .name = "Boolean",
1099 .type = V4L2_CTRL_TYPE_BOOLEAN,
1100 .min = 0,
1101 .max = 1,
1102 .step = 1,
1103 .def = 1,
1104};
1105
1106static const struct v4l2_ctrl_config vivi_ctrl_int32 = {
1107 .ops = &vivi_ctrl_ops,
1108 .id = VIVI_CID_CUSTOM_BASE + 2,
1109 .name = "Integer 32 Bits",
1110 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil5b283022011-01-11 17:32:28 -03001111 .min = 0x80000000,
1112 .max = 0x7fffffff,
Hans Verkuil7e996af2011-01-23 12:33:16 -02001113 .step = 1,
1114};
1115
1116static const struct v4l2_ctrl_config vivi_ctrl_int64 = {
1117 .ops = &vivi_ctrl_ops,
1118 .id = VIVI_CID_CUSTOM_BASE + 3,
1119 .name = "Integer 64 Bits",
1120 .type = V4L2_CTRL_TYPE_INTEGER64,
1121};
1122
1123static const char * const vivi_ctrl_menu_strings[] = {
1124 "Menu Item 0 (Skipped)",
1125 "Menu Item 1",
1126 "Menu Item 2 (Skipped)",
1127 "Menu Item 3",
1128 "Menu Item 4",
1129 "Menu Item 5 (Skipped)",
1130 NULL,
1131};
1132
1133static const struct v4l2_ctrl_config vivi_ctrl_menu = {
1134 .ops = &vivi_ctrl_ops,
1135 .id = VIVI_CID_CUSTOM_BASE + 4,
1136 .name = "Menu",
1137 .type = V4L2_CTRL_TYPE_MENU,
1138 .min = 1,
1139 .max = 4,
1140 .def = 3,
1141 .menu_skip_mask = 0x04,
1142 .qmenu = vivi_ctrl_menu_strings,
1143};
1144
1145static const struct v4l2_ctrl_config vivi_ctrl_string = {
1146 .ops = &vivi_ctrl_ops,
1147 .id = VIVI_CID_CUSTOM_BASE + 5,
1148 .name = "String",
1149 .type = V4L2_CTRL_TYPE_STRING,
1150 .min = 2,
1151 .max = 4,
1152 .step = 1,
1153};
1154
Hans Verkuilb6d17a52011-03-29 16:33:11 -03001155static const struct v4l2_ctrl_config vivi_ctrl_bitmask = {
1156 .ops = &vivi_ctrl_ops,
1157 .id = VIVI_CID_CUSTOM_BASE + 6,
1158 .name = "Bitmask",
1159 .type = V4L2_CTRL_TYPE_BITMASK,
1160 .def = 0x80002000,
1161 .min = 0,
1162 .max = 0x80402010,
1163 .step = 0,
1164};
1165
Sakari Ailusc5203312011-08-05 06:38:05 -03001166static const s64 vivi_ctrl_int_menu_values[] = {
1167 1, 1, 2, 3, 5, 8, 13, 21, 42,
1168};
1169
1170static const struct v4l2_ctrl_config vivi_ctrl_int_menu = {
1171 .ops = &vivi_ctrl_ops,
1172 .id = VIVI_CID_CUSTOM_BASE + 7,
1173 .name = "Integer menu",
1174 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
1175 .min = 1,
1176 .max = 8,
1177 .def = 4,
1178 .menu_skip_mask = 0x02,
1179 .qmenu_int = vivi_ctrl_int_menu_values,
1180};
1181
Hans Verkuilbec43662008-12-30 06:58:20 -03001182static const struct v4l2_file_operations vivi_fops = {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001183 .owner = THIS_MODULE,
Hans Verkuilc7a52f82011-06-07 10:20:23 -03001184 .open = v4l2_fh_open,
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001185 .release = vivi_close,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001186 .read = vivi_read,
1187 .poll = vivi_poll,
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001188 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001189 .mmap = vivi_mmap,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001190};
1191
Hans Verkuila3998102008-07-21 02:57:38 -03001192static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001193 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001194 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1195 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1196 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1197 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001198 .vidioc_reqbufs = vidioc_reqbufs,
1199 .vidioc_querybuf = vidioc_querybuf,
1200 .vidioc_qbuf = vidioc_qbuf,
1201 .vidioc_dqbuf = vidioc_dqbuf,
1202 .vidioc_s_std = vidioc_s_std,
1203 .vidioc_enum_input = vidioc_enum_input,
1204 .vidioc_g_input = vidioc_g_input,
1205 .vidioc_s_input = vidioc_s_input,
Hans Verkuil730947b2010-04-10 04:13:53 -03001206 .vidioc_streamon = vidioc_streamon,
1207 .vidioc_streamoff = vidioc_streamoff,
Hans Verkuile2ecb252012-02-02 08:20:53 -03001208 .vidioc_log_status = v4l2_ctrl_log_status,
Hans Verkuil6d6604f2012-01-27 16:21:10 -03001209 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
Hans Verkuilc7a52f82011-06-07 10:20:23 -03001210 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuila3998102008-07-21 02:57:38 -03001211};
1212
1213static struct video_device vivi_template = {
1214 .name = "vivi",
Hans Verkuila3998102008-07-21 02:57:38 -03001215 .fops = &vivi_fops,
1216 .ioctl_ops = &vivi_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001217 .release = video_device_release,
1218
Mauro Carvalho Chehab784c6682007-12-13 06:35:26 -03001219 .tvnorms = V4L2_STD_525_60,
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001220 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001221};
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001222
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001223/* -----------------------------------------------------------------
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001224 Initialization and module stuff
1225 ------------------------------------------------------------------*/
1226
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001227static int vivi_release(void)
1228{
1229 struct vivi_dev *dev;
1230 struct list_head *list;
1231
1232 while (!list_empty(&vivi_devlist)) {
1233 list = vivi_devlist.next;
1234 list_del(list);
1235 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1236
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001237 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1238 video_device_node_name(dev->vfd));
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001239 video_unregister_device(dev->vfd);
1240 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuil7e996af2011-01-23 12:33:16 -02001241 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001242 kfree(dev);
1243 }
1244
1245 return 0;
1246}
1247
Hans Verkuilc41ee242009-02-14 13:43:44 -03001248static int __init vivi_create_instance(int inst)
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001249{
1250 struct vivi_dev *dev;
1251 struct video_device *vfd;
Hans Verkuil7e996af2011-01-23 12:33:16 -02001252 struct v4l2_ctrl_handler *hdl;
Pawel Osciake007a322011-01-19 13:02:29 -02001253 struct vb2_queue *q;
Hans Verkuil730947b2010-04-10 04:13:53 -03001254 int ret;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001255
1256 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1257 if (!dev)
1258 return -ENOMEM;
1259
1260 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
Hans Verkuilc41ee242009-02-14 13:43:44 -03001261 "%s-%03d", VIVI_MODULE_NAME, inst);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001262 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1263 if (ret)
1264 goto free_dev;
1265
Hans Verkuil730947b2010-04-10 04:13:53 -03001266 dev->fmt = &formats[0];
1267 dev->width = 640;
1268 dev->height = 480;
Hans Verkuil7e996af2011-01-23 12:33:16 -02001269 hdl = &dev->ctrl_handler;
1270 v4l2_ctrl_handler_init(hdl, 11);
1271 dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1272 V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
1273 dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1274 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1275 dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1276 V4L2_CID_CONTRAST, 0, 255, 1, 16);
1277 dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1278 V4L2_CID_SATURATION, 0, 255, 1, 127);
1279 dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1280 V4L2_CID_HUE, -128, 127, 1, 0);
Hans Verkuila1c894f2011-06-07 06:34:41 -03001281 dev->autogain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1282 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1283 dev->gain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1284 V4L2_CID_GAIN, 0, 255, 1, 100);
Hans Verkuil7e996af2011-01-23 12:33:16 -02001285 dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL);
1286 dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL);
1287 dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL);
1288 dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL);
1289 dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL);
1290 dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL);
Hans Verkuilb6d17a52011-03-29 16:33:11 -03001291 dev->bitmask = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_bitmask, NULL);
Sakari Ailusc5203312011-08-05 06:38:05 -03001292 dev->int_menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int_menu, NULL);
Hans Verkuil7e996af2011-01-23 12:33:16 -02001293 if (hdl->error) {
1294 ret = hdl->error;
1295 goto unreg_dev;
1296 }
Hans Verkuila1c894f2011-06-07 06:34:41 -03001297 v4l2_ctrl_auto_cluster(2, &dev->autogain, 0, true);
Hans Verkuil7e996af2011-01-23 12:33:16 -02001298 dev->v4l2_dev.ctrl_handler = hdl;
Hans Verkuil730947b2010-04-10 04:13:53 -03001299
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001300 /* initialize locks */
1301 spin_lock_init(&dev->slock);
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001302
Pawel Osciake007a322011-01-19 13:02:29 -02001303 /* initialize queue */
1304 q = &dev->vb_vidq;
1305 memset(q, 0, sizeof(dev->vb_vidq));
1306 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1307 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1308 q->drv_priv = dev;
1309 q->buf_struct_size = sizeof(struct vivi_buffer);
1310 q->ops = &vivi_video_qops;
1311 q->mem_ops = &vb2_vmalloc_memops;
1312
1313 vb2_queue_init(q);
1314
1315 mutex_init(&dev->mutex);
Hans Verkuil730947b2010-04-10 04:13:53 -03001316
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001317 /* init video dma queues */
1318 INIT_LIST_HEAD(&dev->vidq.active);
1319 init_waitqueue_head(&dev->vidq.wq);
1320
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001321 ret = -ENOMEM;
1322 vfd = video_device_alloc();
1323 if (!vfd)
1324 goto unreg_dev;
1325
1326 *vfd = vivi_template;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -03001327 vfd->debug = debug;
Hans Verkuil730947b2010-04-10 04:13:53 -03001328 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03001329 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Pawel Osciake007a322011-01-19 13:02:29 -02001330
1331 /*
1332 * Provide a mutex to v4l2 core. It will be used to protect
1333 * all fops and v4l2 ioctls.
1334 */
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001335 vfd->lock = &dev->mutex;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001336
1337 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1338 if (ret < 0)
1339 goto rel_vdev;
1340
1341 video_set_drvdata(vfd, dev);
1342
1343 /* Now that everything is fine, let's add it to device list */
1344 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1345
Roel Kluin7de0b872009-12-16 13:06:33 -03001346 if (video_nr != -1)
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001347 video_nr++;
1348
1349 dev->vfd = vfd;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001350 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1351 video_device_node_name(vfd));
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001352 return 0;
1353
1354rel_vdev:
1355 video_device_release(vfd);
1356unreg_dev:
Hans Verkuil7e996af2011-01-23 12:33:16 -02001357 v4l2_ctrl_handler_free(hdl);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001358 v4l2_device_unregister(&dev->v4l2_dev);
1359free_dev:
1360 kfree(dev);
1361 return ret;
1362}
1363
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001364/* This routine allocates from 1 to n_devs virtual drivers.
1365
1366 The real maximum number of virtual drivers will depend on how many drivers
1367 will succeed. This is limited to the maximum number of devices that
Hans Verkuil62cfdac2009-02-14 11:37:17 -03001368 videodev supports, which is equal to VIDEO_NUM_DEVICES.
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001369 */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001370static int __init vivi_init(void)
1371{
Hans Verkuil730947b2010-04-10 04:13:53 -03001372 const struct font_desc *font = find_font("VGA8x16");
Hans Verkuil9185cbf2009-03-06 09:58:12 -03001373 int ret = 0, i;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001374
Hans Verkuil730947b2010-04-10 04:13:53 -03001375 if (font == NULL) {
1376 printk(KERN_ERR "vivi: could not find font\n");
1377 return -ENODEV;
1378 }
1379 font8x16 = font->data;
1380
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001381 if (n_devs <= 0)
1382 n_devs = 1;
1383
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001384 for (i = 0; i < n_devs; i++) {
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001385 ret = vivi_create_instance(i);
1386 if (ret) {
1387 /* If some instantiations succeeded, keep driver */
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001388 if (i)
1389 ret = 0;
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001390 break;
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001391 }
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001392 }
1393
1394 if (ret < 0) {
Hans Verkuil730947b2010-04-10 04:13:53 -03001395 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001396 return ret;
1397 }
1398
1399 printk(KERN_INFO "Video Technology Magazine Virtual Video "
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03001400 "Capture Board ver %s successfully loaded.\n",
1401 VIVI_VERSION);
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001402
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001403 /* n_devs will reflect the actual number of allocated devices */
1404 n_devs = i;
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001405
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001406 return ret;
1407}
1408
1409static void __exit vivi_exit(void)
1410{
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001411 vivi_release();
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001412}
1413
1414module_init(vivi_init);
1415module_exit(vivi_exit);