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