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