blob: 9ed3831b34bd2e890ecd89c82e1c4c0e6745415f [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>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030025#include <linux/version.h>
Matthias Kaehlcke51b54022007-07-02 10:19:38 -030026#include <linux/mutex.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030027#include <linux/videodev2.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030028#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080029#include <linux/freezer.h>
Pawel Osciake007a322011-01-19 13:02:29 -020030#include <media/videobuf2-vmalloc.h>
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030031#include <media/v4l2-device.h>
32#include <media/v4l2-ioctl.h>
Hans Verkuil730947b2010-04-10 04:13:53 -030033#include <media/v4l2-common.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030034
Mauro Carvalho Chehab584ce482008-06-10 15:21:49 -030035#define VIVI_MODULE_NAME "vivi"
Carl Karsten745271a2008-06-10 00:02:32 -030036
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030037/* 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 Verkuil730947b2010-04-10 04:13:53 -030042#define MAX_WIDTH 1920
43#define MAX_HEIGHT 1200
44
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030045#define VIVI_MAJOR_VERSION 0
Pawel Osciake007a322011-01-19 13:02:29 -020046#define VIVI_MINOR_VERSION 8
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030047#define VIVI_RELEASE 0
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -030048#define VIVI_VERSION \
49 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030050
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030051MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
52MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
53MODULE_LICENSE("Dual BSD/GPL");
54
55static unsigned video_nr = -1;
56module_param(video_nr, uint, 0644);
57MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
58
59static unsigned n_devs = 1;
60module_param(n_devs, uint, 0644);
61MODULE_PARM_DESC(n_devs, "number of video devices to create");
62
63static unsigned debug;
64module_param(debug, uint, 0644);
65MODULE_PARM_DESC(debug, "activates debug info");
66
67static unsigned int vid_limit = 16;
68module_param(vid_limit, uint, 0644);
69MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
70
Hans Verkuil730947b2010-04-10 04:13:53 -030071/* Global font descriptor */
72static const u8 *font8x16;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030073
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -030074#define dprintk(dev, level, fmt, arg...) \
75 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030076
77/* ------------------------------------------------------------------
78 Basic structures
79 ------------------------------------------------------------------*/
80
81struct vivi_fmt {
82 char *name;
83 u32 fourcc; /* v4l2 format id */
84 int depth;
85};
86
Magnus Dammd891f472008-10-14 12:47:09 -030087static struct vivi_fmt formats[] = {
88 {
89 .name = "4:2:2, packed, YUYV",
90 .fourcc = V4L2_PIX_FMT_YUYV,
91 .depth = 16,
92 },
Magnus Dammfca36ba2008-10-14 12:47:25 -030093 {
94 .name = "4:2:2, packed, UYVY",
95 .fourcc = V4L2_PIX_FMT_UYVY,
96 .depth = 16,
97 },
Magnus Dammaeadb5d2008-10-14 12:47:35 -030098 {
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 Dammdef52392008-10-14 12:47:43 -0300108 {
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 Chehab1e6dd652006-03-10 12:40:10 -0300118};
119
Magnus Dammd891f472008-10-14 12:47:09 -0300120static 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 Chehab1e6dd652006-03-10 12:40:10 -0300137/* buffer for one video frame */
138struct vivi_buffer {
139 /* common v4l buffer stuff -- must be first */
Pawel Osciake007a322011-01-19 13:02:29 -0200140 struct vb2_buffer vb;
141 struct list_head list;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300142 struct vivi_fmt *fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300143};
144
145struct vivi_dmaqueue {
146 struct list_head active;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300147
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
156static LIST_HEAD(vivi_devlist);
157
158struct vivi_dev {
159 struct list_head vivi_devlist;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300160 struct v4l2_device v4l2_dev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300161
Hans Verkuil730947b2010-04-10 04:13:53 -0300162 /* controls */
163 int brightness;
164 int contrast;
165 int saturation;
166 int hue;
167 int volume;
168
Mauro Carvalho Chehab55862ac2007-12-13 16:13:37 -0300169 spinlock_t slock;
Brandon Philipsaa9dbac2008-04-02 18:10:59 -0300170 struct mutex mutex;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300171
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300172 /* various device info */
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -0300173 struct video_device *vfd;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300174
175 struct vivi_dmaqueue vidq;
176
177 /* Several counters */
Hans Verkuil730947b2010-04-10 04:13:53 -0300178 unsigned ms;
Mauro Carvalho Chehabdfd8c042008-01-13 19:36:11 -0300179 unsigned long jiffies;
Mauro Carvalho Chehab025341d2007-12-10 04:43:38 -0300180
181 int mv_count; /* Controls bars movement */
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300182
183 /* Input Number */
184 int input;
Hans Verkuilc41ee242009-02-14 13:43:44 -0300185
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300186 /* video capture */
187 struct vivi_fmt *fmt;
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300188 unsigned int width, height;
Pawel Osciake007a322011-01-19 13:02:29 -0200189 struct vb2_queue vb_vidq;
190 enum v4l2_field field;
191 unsigned int field_count;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300192
Pawel Osciake007a322011-01-19 13:02:29 -0200193 unsigned int open_count;
Hans Verkuil730947b2010-04-10 04:13:53 -0300194 u8 bars[9][3];
195 u8 line[MAX_WIDTH * 4];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300196};
197
198/* ------------------------------------------------------------------
199 DMA and thread functions
200 ------------------------------------------------------------------*/
201
202/* Bars and Colors should match positions */
203
204enum colors {
205 WHITE,
Hans Verkuil730947b2010-04-10 04:13:53 -0300206 AMBER,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300207 CYAN,
208 GREEN,
209 MAGENTA,
210 RED,
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300211 BLUE,
212 BLACK,
Hans Verkuil730947b2010-04-10 04:13:53 -0300213 TEXT_BLACK,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300214};
215
Hans Verkuil730947b2010-04-10 04:13:53 -0300216/* R G B */
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300217#define COLOR_WHITE {204, 204, 204}
Hans Verkuil730947b2010-04-10 04:13:53 -0300218#define COLOR_AMBER {208, 208, 0}
219#define COLOR_CYAN { 0, 206, 206}
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300220#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
226struct bar_std {
Hans Verkuil730947b2010-04-10 04:13:53 -0300227 u8 bar[9][3];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300228};
229
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300230/* Maximum number of bars are 10 - otherwise, the input print code
231 should be modified */
232static struct bar_std bars[] = {
233 { /* Standard ITU-R color bar sequence */
Hans Verkuil730947b2010-04-10 04:13:53 -0300234 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
235 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300236 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300237 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
238 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300239 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300240 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
241 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300242 }, {
Hans Verkuil730947b2010-04-10 04:13:53 -0300243 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
244 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300245 },
246};
247
248#define NUM_INPUTS ARRAY_SIZE(bars)
249
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300250#define TO_Y(r, g, b) \
251 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300252/* RGB to V(Cr) Color transform */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300253#define TO_V(r, g, b) \
254 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300255/* RGB to U(Cb) Color transform */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300256#define TO_U(r, g, b) \
257 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300258
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300259/* precalculate color bar values to speed up rendering */
Hans Verkuil730947b2010-04-10 04:13:53 -0300260static void precalculate_bars(struct vivi_dev *dev)
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300261{
Hans Verkuil730947b2010-04-10 04:13:53 -0300262 u8 r, g, b;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300263 int k, is_yuv;
264
Hans Verkuil730947b2010-04-10 04:13:53 -0300265 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 Chehabc285add2009-06-25 16:28:23 -0300269 is_yuv = 0;
270
Hans Verkuil730947b2010-04-10 04:13:53 -0300271 switch (dev->fmt->fourcc) {
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300272 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 Verkuil730947b2010-04-10 04:13:53 -0300291 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 Chehabc285add2009-06-25 16:28:23 -0300294 } else {
Hans Verkuil730947b2010-04-10 04:13:53 -0300295 dev->bars[k][0] = r;
296 dev->bars[k][1] = g;
297 dev->bars[k][2] = b;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300298 }
299 }
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300300}
301
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300302#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 Chehab1e6dd652006-03-10 12:40:10 -0300306
Hans Verkuil730947b2010-04-10 04:13:53 -0300307static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300308{
Hans Verkuil730947b2010-04-10 04:13:53 -0300309 u8 r_y, g_u, b_v;
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300310 int color;
Hans Verkuil730947b2010-04-10 04:13:53 -0300311 u8 *p;
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300312
Hans Verkuil730947b2010-04-10 04:13:53 -0300313 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 Damm74d7c5a2008-10-14 12:46:59 -0300316
317 for (color = 0; color < 4; color++) {
318 p = buf + color;
319
Hans Verkuil730947b2010-04-10 04:13:53 -0300320 switch (dev->fmt->fourcc) {
Magnus Dammd891f472008-10-14 12:47:09 -0300321 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 Damm74d7c5a2008-10-14 12:46:59 -0300334 break;
Magnus Dammfca36ba2008-10-14 12:47:25 -0300335 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 Dammaeadb5d2008-10-14 12:47:35 -0300349 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 Dammdef52392008-10-14 12:47:43 -0300373 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 Damm74d7c5a2008-10-14 12:46:59 -0300397 }
398 }
399}
400
Hans Verkuil730947b2010-04-10 04:13:53 -0300401static void precalculate_line(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300402{
Hans Verkuil730947b2010-04-10 04:13:53 -0300403 int w;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300404
Hans Verkuil730947b2010-04-10 04:13:53 -0300405 for (w = 0; w < dev->width * 2; w += 2) {
406 int colorpos = (w / (dev->width / 8) % 8);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300407
Hans Verkuil730947b2010-04-10 04:13:53 -0300408 gen_twopix(dev, dev->line + w * 2, colorpos);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300409 }
Hans Verkuil730947b2010-04-10 04:13:53 -0300410}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300411
Hans Verkuil730947b2010-04-10 04:13:53 -0300412static void gen_text(struct vivi_dev *dev, char *basep,
413 int y, int x, char *text)
414{
415 int line;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300416
Hans Verkuil730947b2010-04-10 04:13:53 -0300417 /* Checks if it is possible to show string */
418 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
419 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300420
421 /* Print stream time */
Hans Verkuil730947b2010-04-10 04:13:53 -0300422 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 Damm74d7c5a2008-10-14 12:46:59 -0300432 /* Draw white font on black background */
Hans Verkuil730947b2010-04-10 04:13:53 -0300433 if (chr & (1 << (7 - i)))
434 gen_twopix(dev, pos + j * 2, WHITE);
Magnus Damm74d7c5a2008-10-14 12:46:59 -0300435 else
Hans Verkuil730947b2010-04-10 04:13:53 -0300436 gen_twopix(dev, pos + j * 2, TEXT_BLACK);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300437 }
438 }
439 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300440}
Brandon Philips78718e52008-04-02 18:10:59 -0300441
Hans Verkuil730947b2010-04-10 04:13:53 -0300442static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300443{
Pawel Osciake007a322011-01-19 13:02:29 -0200444 int wmax = dev->width;
445 int hmax = dev->height;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300446 struct timeval ts;
Pawel Osciake007a322011-01-19 13:02:29 -0200447 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
Hans Verkuil730947b2010-04-10 04:13:53 -0300448 unsigned ms;
449 char str[100];
450 int h, line = 1;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300451
Marcin Slusarz5c554e62008-06-22 09:11:40 -0300452 if (!vbuf)
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300453 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300454
Hans Verkuil730947b2010-04-10 04:13:53 -0300455 for (h = 0; h < hmax; h++)
456 memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300457
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300458 /* Updates stream time */
459
Hans Verkuil730947b2010-04-10 04:13:53 -0300460 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300461 dev->jiffies = jiffies;
Hans Verkuil730947b2010-04-10 04:13:53 -0300462 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 Chehab1e6dd652006-03-10 12:40:10 -0300472
Hans Verkuil730947b2010-04-10 04:13:53 -0300473 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 Chehab1e6dd652006-03-10 12:40:10 -0300483
Pawel Osciake007a322011-01-19 13:02:29 -0200484 buf->vb.v4l2_buf.field = dev->field;
485 dev->field_count++;
486 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300487 do_gettimeofday(&ts);
Pawel Osciake007a322011-01-19 13:02:29 -0200488 buf->vb.v4l2_buf.timestamp = ts;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300489}
490
Hans Verkuil730947b2010-04-10 04:13:53 -0300491static void vivi_thread_tick(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300492{
Brandon Philips78718e52008-04-02 18:10:59 -0300493 struct vivi_dmaqueue *dma_q = &dev->vidq;
Hans Verkuil730947b2010-04-10 04:13:53 -0300494 struct vivi_buffer *buf;
Brandon Philips78718e52008-04-02 18:10:59 -0300495 unsigned long flags = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300496
Brandon Philips78718e52008-04-02 18:10:59 -0300497 dprintk(dev, 1, "Thread tick\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300498
Brandon Philips78718e52008-04-02 18:10:59 -0300499 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 Chehab1e6dd652006-03-10 12:40:10 -0300503 }
Brandon Philips78718e52008-04-02 18:10:59 -0300504
Pawel Osciake007a322011-01-19 13:02:29 -0200505 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
506 list_del(&buf->list);
Brandon Philips78718e52008-04-02 18:10:59 -0300507
Pawel Osciake007a322011-01-19 13:02:29 -0200508 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
Brandon Philips78718e52008-04-02 18:10:59 -0300509
510 /* Fill buffer */
Hans Verkuil730947b2010-04-10 04:13:53 -0300511 vivi_fillbuff(dev, buf);
Brandon Philips78718e52008-04-02 18:10:59 -0300512 dprintk(dev, 1, "filled buffer %p\n", buf);
513
Pawel Osciake007a322011-01-19 13:02:29 -0200514 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
515 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
Brandon Philips78718e52008-04-02 18:10:59 -0300516unlock:
517 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300518}
519
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300520#define frames_to_ms(frames) \
521 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
522
Hans Verkuil730947b2010-04-10 04:13:53 -0300523static void vivi_sleep(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300524{
Brandon Philips78718e52008-04-02 18:10:59 -0300525 struct vivi_dmaqueue *dma_q = &dev->vidq;
526 int timeout;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300527 DECLARE_WAITQUEUE(wait, current);
528
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300529 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300530 (unsigned long)dma_q);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300531
532 add_wait_queue(&dma_q->wq, &wait);
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300533 if (kthread_should_stop())
534 goto stop_task;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300535
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300536 /* Calculate time to wake up */
Brandon Philips78718e52008-04-02 18:10:59 -0300537 timeout = msecs_to_jiffies(frames_to_ms(1));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300538
Hans Verkuil730947b2010-04-10 04:13:53 -0300539 vivi_thread_tick(dev);
Mauro Carvalho Chehab6594ad82007-12-13 16:15:41 -0300540
541 schedule_timeout_interruptible(timeout);
542
543stop_task:
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300544 remove_wait_queue(&dma_q->wq, &wait);
545 try_to_freeze();
546}
547
Adrian Bunk972c3512006-04-27 21:06:50 -0300548static int vivi_thread(void *data)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300549{
Hans Verkuil730947b2010-04-10 04:13:53 -0300550 struct vivi_dev *dev = data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300551
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300552 dprintk(dev, 1, "thread started\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300553
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700554 set_freezable();
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300555
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300556 for (;;) {
Hans Verkuil730947b2010-04-10 04:13:53 -0300557 vivi_sleep(dev);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300558
559 if (kthread_should_stop())
560 break;
561 }
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300562 dprintk(dev, 1, "thread: exit\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300563 return 0;
564}
565
Pawel Osciake007a322011-01-19 13:02:29 -0200566static int vivi_start_generating(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300567{
Brandon Philips78718e52008-04-02 18:10:59 -0300568 struct vivi_dmaqueue *dma_q = &dev->vidq;
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300569
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300570 dprintk(dev, 1, "%s\n", __func__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300571
Hans Verkuil730947b2010-04-10 04:13:53 -0300572 /* 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 Chehab1e6dd652006-03-10 12:40:10 -0300580
Akinobu Mita054afee2006-12-20 10:04:00 -0300581 if (IS_ERR(dma_q->kthread)) {
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300582 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
Pawel Osciake007a322011-01-19 13:02:29 -0200583 return PTR_ERR(dma_q->kthread);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300584 }
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300585 /* Wakes thread */
586 wake_up_interruptible(&dma_q->wq);
587
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300588 dprintk(dev, 1, "returning from %s\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200589 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300590}
591
Pawel Osciake007a322011-01-19 13:02:29 -0200592static void vivi_stop_generating(struct vivi_dev *dev)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300593{
Hans Verkuil730947b2010-04-10 04:13:53 -0300594 struct vivi_dmaqueue *dma_q = &dev->vidq;
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300595
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300596 dprintk(dev, 1, "%s\n", __func__);
Hans Verkuil730947b2010-04-10 04:13:53 -0300597
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300598 /* shutdown control thread */
599 if (dma_q->kthread) {
600 kthread_stop(dma_q->kthread);
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300601 dma_q->kthread = NULL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300602 }
Hans Verkuil730947b2010-04-10 04:13:53 -0300603
Pawel Osciake007a322011-01-19 13:02:29 -0200604 /*
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 Chehab1e6dd652006-03-10 12:40:10 -0300608
Pawel Osciake007a322011-01-19 13:02:29 -0200609 /* 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 Chehab1e6dd652006-03-10 12:40:10 -0300618/* ------------------------------------------------------------------
619 Videobuf operations
620 ------------------------------------------------------------------*/
Pawel Osciake007a322011-01-19 13:02:29 -0200621static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
622 unsigned int *nplanes, unsigned long sizes[],
623 void *alloc_ctxs[])
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300624{
Pawel Osciake007a322011-01-19 13:02:29 -0200625 struct vivi_dev *dev = vb2_get_drv_priv(vq);
626 unsigned long size;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300627
Pawel Osciake007a322011-01-19 13:02:29 -0200628 size = dev->width * dev->height * 2;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300629
Pawel Osciake007a322011-01-19 13:02:29 -0200630 if (0 == *nbuffers)
631 *nbuffers = 32;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300632
Pawel Osciake007a322011-01-19 13:02:29 -0200633 while (size * *nbuffers > vid_limit * 1024 * 1024)
634 (*nbuffers)--;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300635
Pawel Osciake007a322011-01-19 13:02:29 -0200636 *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 Chehab6bb27902007-08-23 16:41:14 -0300647
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300648 return 0;
649}
650
Pawel Osciake007a322011-01-19 13:02:29 -0200651static int buffer_init(struct vb2_buffer *vb)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300652{
Pawel Osciake007a322011-01-19 13:02:29 -0200653 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300654
Hans Verkuil730947b2010-04-10 04:13:53 -0300655 BUG_ON(NULL == dev->fmt);
Brandon Philips78718e52008-04-02 18:10:59 -0300656
Pawel Osciake007a322011-01-19 13:02:29 -0200657 /*
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
672static 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 Verkuil730947b2010-04-10 04:13:53 -0300688 if (dev->width < 48 || dev->width > MAX_WIDTH ||
689 dev->height < 32 || dev->height > MAX_HEIGHT)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300690 return -EINVAL;
Brandon Philips78718e52008-04-02 18:10:59 -0300691
Pawel Osciake007a322011-01-19 13:02:29 -0200692 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 Chehab1e6dd652006-03-10 12:40:10 -0300696 return -EINVAL;
Pawel Osciake007a322011-01-19 13:02:29 -0200697 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300698
Pawel Osciake007a322011-01-19 13:02:29 -0200699 vb2_set_plane_payload(&buf->vb, 0, size);
700
701 buf->fmt = dev->fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300702
Hans Verkuil730947b2010-04-10 04:13:53 -0300703 precalculate_bars(dev);
704 precalculate_line(dev);
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -0300705
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300706 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300707}
708
Pawel Osciake007a322011-01-19 13:02:29 -0200709static int buffer_finish(struct vb2_buffer *vb)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300710{
Pawel Osciake007a322011-01-19 13:02:29 -0200711 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
712 dprintk(dev, 1, "%s\n", __func__);
713 return 0;
714}
715
716static 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
723static void buffer_queue(struct vb2_buffer *vb)
724{
725 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuil730947b2010-04-10 04:13:53 -0300726 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
Brandon Philips78718e52008-04-02 18:10:59 -0300727 struct vivi_dmaqueue *vidq = &dev->vidq;
Pawel Osciake007a322011-01-19 13:02:29 -0200728 unsigned long flags = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300729
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300730 dprintk(dev, 1, "%s\n", __func__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300731
Pawel Osciake007a322011-01-19 13:02:29 -0200732 spin_lock_irqsave(&dev->slock, flags);
733 list_add_tail(&buf->list, &vidq->active);
734 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300735}
736
Pawel Osciake007a322011-01-19 13:02:29 -0200737static int start_streaming(struct vb2_queue *vq)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300738{
Pawel Osciake007a322011-01-19 13:02:29 -0200739 struct vivi_dev *dev = vb2_get_drv_priv(vq);
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300740 dprintk(dev, 1, "%s\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200741 return vivi_start_generating(dev);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300742}
743
Pawel Osciake007a322011-01-19 13:02:29 -0200744/* abort streaming and wait for last buffer */
745static 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
753static 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
759static 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
766static 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 Chehab1e6dd652006-03-10 12:40:10 -0300777};
778
779/* ------------------------------------------------------------------
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300780 IOCTL vidioc handling
781 ------------------------------------------------------------------*/
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300782static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300783 struct v4l2_capability *cap)
784{
Hans Verkuil730947b2010-04-10 04:13:53 -0300785 struct vivi_dev *dev = video_drvdata(file);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300786
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300787 strcpy(cap->driver, "vivi");
788 strcpy(cap->card, "vivi");
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -0300789 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300790 cap->version = VIVI_VERSION;
Hans Verkuil730947b2010-04-10 04:13:53 -0300791 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \
792 V4L2_CAP_READWRITE;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300793 return 0;
794}
795
Hans Verkuil78b526a2008-05-28 12:16:41 -0300796static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300797 struct v4l2_fmtdesc *f)
798{
Magnus Dammd891f472008-10-14 12:47:09 -0300799 struct vivi_fmt *fmt;
800
801 if (f->index >= ARRAY_SIZE(formats))
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300802 return -EINVAL;
803
Magnus Dammd891f472008-10-14 12:47:09 -0300804 fmt = &formats[f->index];
805
806 strlcpy(f->description, fmt->name, sizeof(f->description));
807 f->pixelformat = fmt->fourcc;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300808 return 0;
809}
810
Hans Verkuil78b526a2008-05-28 12:16:41 -0300811static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300812 struct v4l2_format *f)
813{
Hans Verkuil730947b2010-04-10 04:13:53 -0300814 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300815
Hans Verkuil730947b2010-04-10 04:13:53 -0300816 f->fmt.pix.width = dev->width;
817 f->fmt.pix.height = dev->height;
Pawel Osciake007a322011-01-19 13:02:29 -0200818 f->fmt.pix.field = dev->field;
Hans Verkuil730947b2010-04-10 04:13:53 -0300819 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300820 f->fmt.pix.bytesperline =
Hans Verkuil730947b2010-04-10 04:13:53 -0300821 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300822 f->fmt.pix.sizeimage =
823 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil730947b2010-04-10 04:13:53 -0300824 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300825}
826
Hans Verkuil78b526a2008-05-28 12:16:41 -0300827static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300828 struct v4l2_format *f)
829{
Hans Verkuil730947b2010-04-10 04:13:53 -0300830 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300831 struct vivi_fmt *fmt;
832 enum v4l2_field field;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300833
Magnus Dammd891f472008-10-14 12:47:09 -0300834 fmt = get_format(f);
835 if (!fmt) {
836 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
837 f->fmt.pix.pixelformat);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300838 return -EINVAL;
839 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300840
841 field = f->fmt.pix.field;
842
843 if (field == V4L2_FIELD_ANY) {
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300844 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300845 } else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -0300846 dprintk(dev, 1, "Field type invalid.\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300847 return -EINVAL;
848 }
849
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300850 f->fmt.pix.field = field;
Hans Verkuil730947b2010-04-10 04:13:53 -0300851 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
852 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300853 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 Chehab1e6dd652006-03-10 12:40:10 -0300857 return 0;
858}
859
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300860static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
861 struct v4l2_format *f)
862{
Hans Verkuil730947b2010-04-10 04:13:53 -0300863 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200864 struct vb2_queue *q = &dev->vb_vidq;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300865
Hans Verkuil730947b2010-04-10 04:13:53 -0300866 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300867 if (ret < 0)
868 return ret;
869
Pawel Osciake007a322011-01-19 13:02:29 -0200870 if (vb2_is_streaming(q)) {
Hans Verkuil730947b2010-04-10 04:13:53 -0300871 dprintk(dev, 1, "%s device busy\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -0200872 return -EBUSY;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300873 }
874
Hans Verkuil730947b2010-04-10 04:13:53 -0300875 dev->fmt = get_format(f);
876 dev->width = f->fmt.pix.width;
877 dev->height = f->fmt.pix.height;
Pawel Osciake007a322011-01-19 13:02:29 -0200878 dev->field = f->fmt.pix.field;
879
880 return 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300881}
882
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300883static int vidioc_reqbufs(struct file *file, void *priv,
884 struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300885{
Hans Verkuil730947b2010-04-10 04:13:53 -0300886 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200887 return vb2_reqbufs(&dev->vb_vidq, p);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300888}
889
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300890static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300891{
Hans Verkuil730947b2010-04-10 04:13:53 -0300892 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200893 return vb2_querybuf(&dev->vb_vidq, p);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300894}
895
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300896static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300897{
Hans Verkuil730947b2010-04-10 04:13:53 -0300898 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200899 return vb2_qbuf(&dev->vb_vidq, p);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300900}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300901
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300902static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300903{
Hans Verkuil730947b2010-04-10 04:13:53 -0300904 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200905 return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300906}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300907
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300908static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300909{
Hans Verkuil730947b2010-04-10 04:13:53 -0300910 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200911 return vb2_streamon(&dev->vb_vidq, i);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300912}
913
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300914static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300915{
Hans Verkuil730947b2010-04-10 04:13:53 -0300916 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -0200917 return vb2_streamoff(&dev->vb_vidq, i);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300918}
919
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300920static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300921{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300922 return 0;
923}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300924
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300925/* only one input in this sample driver */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300926static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300927 struct v4l2_input *inp)
928{
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300929 if (inp->index >= NUM_INPUTS)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300930 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300931
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300932 inp->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab784c6682007-12-13 06:35:26 -0300933 inp->std = V4L2_STD_525_60;
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300934 sprintf(inp->name, "Camera %u", inp->index);
Hans Verkuil730947b2010-04-10 04:13:53 -0300935 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300936}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300937
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300938static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300939{
Hans Verkuil730947b2010-04-10 04:13:53 -0300940 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300941
942 *i = dev->input;
Hans Verkuil730947b2010-04-10 04:13:53 -0300943 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300944}
Hans Verkuil730947b2010-04-10 04:13:53 -0300945
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300946static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300947{
Hans Verkuil730947b2010-04-10 04:13:53 -0300948 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300949
950 if (i >= NUM_INPUTS)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300951 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300952
Mauro Carvalho Chehabe164b582009-01-11 10:29:43 -0300953 dev->input = i;
Hans Verkuil730947b2010-04-10 04:13:53 -0300954 precalculate_bars(dev);
955 precalculate_line(dev);
956 return 0;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300957}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300958
Hans Verkuil730947b2010-04-10 04:13:53 -0300959/* --- controls ---------------------------------------------- */
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300960static int vidioc_queryctrl(struct file *file, void *priv,
961 struct v4l2_queryctrl *qc)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300962{
Hans Verkuil730947b2010-04-10 04:13:53 -0300963 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 Chehabc820cc42006-06-04 10:34:12 -0300975 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300976}
977
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -0300978static int vidioc_g_ctrl(struct file *file, void *priv,
979 struct v4l2_control *ctrl)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300980{
Hans Verkuil730947b2010-04-10 04:13:53 -0300981 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300982
Hans Verkuil730947b2010-04-10 04:13:53 -0300983 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 Chehabc820cc42006-06-04 10:34:12 -03001000 return -EINVAL;
1001}
Hans Verkuil730947b2010-04-10 04:13:53 -03001002
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -03001003static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001004 struct v4l2_control *ctrl)
1005{
Hans Verkuil730947b2010-04-10 04:13:53 -03001006 struct vivi_dev *dev = video_drvdata(file);
1007 struct v4l2_queryctrl qc;
1008 int err;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001009
Hans Verkuil730947b2010-04-10 04:13:53 -03001010 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 Chehabc820cc42006-06-04 10:34:12 -03001033 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001034}
1035
1036/* ------------------------------------------------------------------
1037 File operations for the device
1038 ------------------------------------------------------------------*/
1039
Pawel Osciake007a322011-01-19 13:02:29 -02001040static 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 Chehab1e6dd652006-03-10 12:40:10 -03001049static ssize_t
1050vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1051{
Hans Verkuil730947b2010-04-10 04:13:53 -03001052 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001053
Pawel Osciake007a322011-01-19 13:02:29 -02001054 dprintk(dev, 1, "read called\n");
1055 return vb2_read(&dev->vb_vidq, data, count, ppos,
1056 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001057}
1058
1059static unsigned int
1060vivi_poll(struct file *file, struct poll_table_struct *wait)
1061{
Hans Verkuil730947b2010-04-10 04:13:53 -03001062 struct vivi_dev *dev = video_drvdata(file);
Pawel Osciake007a322011-01-19 13:02:29 -02001063 struct vb2_queue *q = &dev->vb_vidq;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001064
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001065 dprintk(dev, 1, "%s\n", __func__);
Pawel Osciake007a322011-01-19 13:02:29 -02001066 return vb2_poll(q, file, wait);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001067}
1068
Hans Verkuilbec43662008-12-30 06:58:20 -03001069static int vivi_close(struct file *file)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001070{
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001071 struct video_device *vdev = video_devdata(file);
Hans Verkuil730947b2010-04-10 04:13:53 -03001072 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001073
Pawel Osciake007a322011-01-19 13:02:29 -02001074 dprintk(dev, 1, "close called (dev=%s), file %p\n",
1075 video_device_node_name(vdev), file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001076
Pawel Osciake007a322011-01-19 13:02:29 -02001077 if (--dev->open_count == 0)
1078 vb2_queue_release(&dev->vb_vidq);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001079 return 0;
1080}
1081
Mauro Carvalho Chehab543323b2007-12-10 09:33:52 -03001082static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001083{
Hans Verkuil730947b2010-04-10 04:13:53 -03001084 struct vivi_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001085 int ret;
1086
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -03001087 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001088
Pawel Osciake007a322011-01-19 13:02:29 -02001089 ret = vb2_mmap(&dev->vb_vidq, vma);
Mauro Carvalho Chehab6c2f9902007-12-13 13:30:14 -03001090 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001091 (unsigned long)vma->vm_start,
Hans Verkuil730947b2010-04-10 04:13:53 -03001092 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001093 ret);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001094 return ret;
1095}
1096
Hans Verkuilbec43662008-12-30 06:58:20 -03001097static const struct v4l2_file_operations vivi_fops = {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001098 .owner = THIS_MODULE,
Pawel Osciake007a322011-01-19 13:02:29 -02001099 .open = vivi_open,
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001100 .release = vivi_close,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001101 .read = vivi_read,
1102 .poll = vivi_poll,
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001103 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001104 .mmap = vivi_mmap,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001105};
1106
Hans Verkuila3998102008-07-21 02:57:38 -03001107static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001108 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001109 .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 Chehabc820cc42006-06-04 10:34:12 -03001113 .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 Verkuil730947b2010-04-10 04:13:53 -03001121 .vidioc_streamon = vidioc_streamon,
1122 .vidioc_streamoff = vidioc_streamoff,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001123 .vidioc_queryctrl = vidioc_queryctrl,
1124 .vidioc_g_ctrl = vidioc_g_ctrl,
1125 .vidioc_s_ctrl = vidioc_s_ctrl,
Hans Verkuila3998102008-07-21 02:57:38 -03001126};
1127
1128static struct video_device vivi_template = {
1129 .name = "vivi",
Hans Verkuila3998102008-07-21 02:57:38 -03001130 .fops = &vivi_fops,
1131 .ioctl_ops = &vivi_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001132 .release = video_device_release,
1133
Mauro Carvalho Chehab784c6682007-12-13 06:35:26 -03001134 .tvnorms = V4L2_STD_525_60,
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001135 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001136};
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001137
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001138/* -----------------------------------------------------------------
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001139 Initialization and module stuff
1140 ------------------------------------------------------------------*/
1141
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001142static 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 Pinchart38c7c032009-11-27 13:57:15 -03001152 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1153 video_device_node_name(dev->vfd));
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001154 video_unregister_device(dev->vfd);
1155 v4l2_device_unregister(&dev->v4l2_dev);
1156 kfree(dev);
1157 }
1158
1159 return 0;
1160}
1161
Hans Verkuilc41ee242009-02-14 13:43:44 -03001162static int __init vivi_create_instance(int inst)
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001163{
1164 struct vivi_dev *dev;
1165 struct video_device *vfd;
Pawel Osciake007a322011-01-19 13:02:29 -02001166 struct vb2_queue *q;
Hans Verkuil730947b2010-04-10 04:13:53 -03001167 int ret;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001168
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 Verkuilc41ee242009-02-14 13:43:44 -03001174 "%s-%03d", VIVI_MODULE_NAME, inst);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001175 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1176 if (ret)
1177 goto free_dev;
1178
Hans Verkuil730947b2010-04-10 04:13:53 -03001179 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 Verkuilfedc6c82010-09-20 18:25:55 -03001188 /* initialize locks */
1189 spin_lock_init(&dev->slock);
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001190
Pawel Osciake007a322011-01-19 13:02:29 -02001191 /* 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 Verkuil730947b2010-04-10 04:13:53 -03001204
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001205 /* init video dma queues */
1206 INIT_LIST_HEAD(&dev->vidq.active);
1207 init_waitqueue_head(&dev->vidq.wq);
1208
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001209 ret = -ENOMEM;
1210 vfd = video_device_alloc();
1211 if (!vfd)
1212 goto unreg_dev;
1213
1214 *vfd = vivi_template;
Mauro Carvalho Chehabc285add2009-06-25 16:28:23 -03001215 vfd->debug = debug;
Hans Verkuil730947b2010-04-10 04:13:53 -03001216 vfd->v4l2_dev = &dev->v4l2_dev;
Pawel Osciake007a322011-01-19 13:02:29 -02001217
1218 /*
1219 * Provide a mutex to v4l2 core. It will be used to protect
1220 * all fops and v4l2 ioctls.
1221 */
Hans Verkuilfedc6c82010-09-20 18:25:55 -03001222 vfd->lock = &dev->mutex;
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001223
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 Kluin7de0b872009-12-16 13:06:33 -03001233 if (video_nr != -1)
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001234 video_nr++;
1235
1236 dev->vfd = vfd;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001237 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1238 video_device_node_name(vfd));
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001239 return 0;
1240
1241rel_vdev:
1242 video_device_release(vfd);
1243unreg_dev:
1244 v4l2_device_unregister(&dev->v4l2_dev);
1245free_dev:
1246 kfree(dev);
1247 return ret;
1248}
1249
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001250/* 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 Verkuil62cfdac2009-02-14 11:37:17 -03001254 videodev supports, which is equal to VIDEO_NUM_DEVICES.
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001255 */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001256static int __init vivi_init(void)
1257{
Hans Verkuil730947b2010-04-10 04:13:53 -03001258 const struct font_desc *font = find_font("VGA8x16");
Hans Verkuil9185cbf2009-03-06 09:58:12 -03001259 int ret = 0, i;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001260
Hans Verkuil730947b2010-04-10 04:13:53 -03001261 if (font == NULL) {
1262 printk(KERN_ERR "vivi: could not find font\n");
1263 return -ENODEV;
1264 }
1265 font8x16 = font->data;
1266
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001267 if (n_devs <= 0)
1268 n_devs = 1;
1269
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001270 for (i = 0; i < n_devs; i++) {
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001271 ret = vivi_create_instance(i);
1272 if (ret) {
1273 /* If some instantiations succeeded, keep driver */
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001274 if (i)
1275 ret = 0;
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001276 break;
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001277 }
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001278 }
1279
1280 if (ret < 0) {
Hans Verkuil730947b2010-04-10 04:13:53 -03001281 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001282 return ret;
1283 }
1284
1285 printk(KERN_INFO "Video Technology Magazine Virtual Video "
Carl Karsten745271a2008-06-10 00:02:32 -03001286 "Capture Board ver %u.%u.%u successfully loaded.\n",
1287 (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF,
1288 VIVI_VERSION & 0xFF);
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001289
Hans Verkuil5ab6c9a2009-02-14 13:23:12 -03001290 /* n_devs will reflect the actual number of allocated devices */
1291 n_devs = i;
Mauro Carvalho Chehab980d4f12008-09-03 17:11:53 -03001292
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001293 return ret;
1294}
1295
1296static void __exit vivi_exit(void)
1297{
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001298 vivi_release();
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001299}
1300
1301module_init(vivi_init);
1302module_exit(vivi_exit);