blob: 9b54ff9d2e36210209d2335489fb5fb1fd901ad6 [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 *
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>
Matthias Kaehlcke51b54022007-07-02 10:19:38 -030028#include <linux/mutex.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030029#include <linux/videodev2.h>
Andrew Morton10951362006-04-27 10:10:58 -030030#include <linux/dma-mapping.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030031#ifdef CONFIG_VIDEO_V4L1_COMPAT
32/* Include V4L1 specific functions. Should be removed soon */
33#include <linux/videodev.h>
34#endif
Michael Krufkyf13df912006-03-23 22:01:44 -030035#include <linux/interrupt.h>
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -030036#include <media/videobuf-vmalloc.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030037#include <media/v4l2-common.h>
38#include <linux/kthread.h>
39#include <linux/highmem.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040#include <linux/freezer.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030041
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
47/* These timers are for 1 fps - used only for testing */
48//#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
49//#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
50
51#include "font.h"
52
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030053#define VIVI_MAJOR_VERSION 0
54#define VIVI_MINOR_VERSION 4
55#define VIVI_RELEASE 0
56#define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -030058/* Declare static vars that will be used as parameters */
59static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
60static struct video_device vivi; /* Video device */
61static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030062
63/* supported controls */
64static struct v4l2_queryctrl vivi_qctrl[] = {
65 {
66 .id = V4L2_CID_AUDIO_VOLUME,
67 .name = "Volume",
68 .minimum = 0,
69 .maximum = 65535,
70 .step = 65535/100,
71 .default_value = 65535,
72 .flags = 0,
73 .type = V4L2_CTRL_TYPE_INTEGER,
74 },{
75 .id = V4L2_CID_BRIGHTNESS,
76 .type = V4L2_CTRL_TYPE_INTEGER,
77 .name = "Brightness",
78 .minimum = 0,
79 .maximum = 255,
80 .step = 1,
81 .default_value = 127,
82 .flags = 0,
83 }, {
84 .id = V4L2_CID_CONTRAST,
85 .type = V4L2_CTRL_TYPE_INTEGER,
86 .name = "Contrast",
87 .minimum = 0,
88 .maximum = 255,
89 .step = 0x1,
90 .default_value = 0x10,
91 .flags = 0,
92 }, {
93 .id = V4L2_CID_SATURATION,
94 .type = V4L2_CTRL_TYPE_INTEGER,
95 .name = "Saturation",
96 .minimum = 0,
97 .maximum = 255,
98 .step = 0x1,
99 .default_value = 127,
100 .flags = 0,
101 }, {
102 .id = V4L2_CID_HUE,
103 .type = V4L2_CTRL_TYPE_INTEGER,
104 .name = "Hue",
105 .minimum = -128,
106 .maximum = 127,
107 .step = 0x1,
108 .default_value = 0,
109 .flags = 0,
110 }
111};
112
113static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
114
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300115#define dprintk(level,fmt, arg...) \
116 do { \
117 if (vivi.debug >= (level)) \
118 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300119 } while (0)
120
121/* ------------------------------------------------------------------
122 Basic structures
123 ------------------------------------------------------------------*/
124
125struct vivi_fmt {
126 char *name;
127 u32 fourcc; /* v4l2 format id */
128 int depth;
129};
130
131static struct vivi_fmt format = {
132 .name = "4:2:2, packed, YUYV",
133 .fourcc = V4L2_PIX_FMT_YUYV,
134 .depth = 16,
135};
136
137struct sg_to_addr {
138 int pos;
139 struct scatterlist *sg;
140};
141
142/* buffer for one video frame */
143struct vivi_buffer {
144 /* common v4l buffer stuff -- must be first */
145 struct videobuf_buffer vb;
146
147 struct vivi_fmt *fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300148};
149
150struct vivi_dmaqueue {
151 struct list_head active;
152 struct list_head queued;
153 struct timer_list timeout;
154
155 /* thread for generating video stream*/
156 struct task_struct *kthread;
157 wait_queue_head_t wq;
158 /* Counters to control fps rate */
159 int frame;
160 int ini_jiffies;
161};
162
163static LIST_HEAD(vivi_devlist);
164
165struct vivi_dev {
166 struct list_head vivi_devlist;
167
Matthias Kaehlcke51b54022007-07-02 10:19:38 -0300168 struct mutex lock;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300169
170 int users;
171
172 /* various device info */
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300173 struct video_device vfd;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300174
175 struct vivi_dmaqueue vidq;
176
177 /* Several counters */
178 int h,m,s,us,jiffies;
179 char timestr[13];
180};
181
182struct vivi_fh {
183 struct vivi_dev *dev;
184
185 /* video capture */
186 struct vivi_fmt *fmt;
187 unsigned int width,height;
188 struct videobuf_queue vb_vidq;
189
190 enum v4l2_buf_type type;
191};
192
193/* ------------------------------------------------------------------
194 DMA and thread functions
195 ------------------------------------------------------------------*/
196
197/* Bars and Colors should match positions */
198
199enum colors {
200 WHITE,
201 AMBAR,
202 CYAN,
203 GREEN,
204 MAGENTA,
205 RED,
206 BLUE
207};
208
209static u8 bars[8][3] = {
210 /* R G B */
211 {204,204,204}, /* white */
212 {208,208, 0}, /* ambar */
213 { 0,206,206}, /* cyan */
214 { 0,239, 0}, /* green */
215 {239, 0,239}, /* magenta */
216 {205, 0, 0}, /* red */
217 { 0, 0,255}, /* blue */
218 { 0, 0, 0}
219};
220
221#define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
222/* RGB to V(Cr) Color transform */
223#define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
224/* RGB to U(Cb) Color transform */
225#define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
226
227#define TSTAMP_MIN_Y 24
228#define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
229#define TSTAMP_MIN_X 64
230
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300231static void gen_line(char *basep,int inipos,int wmax,
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300232 int hmax, int line, int count, char *timestr)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300233{
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300234 int w,i,j,pos=inipos,y;
235 char *p,*s;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300236 u8 chr,r,g,b,color;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300237
238 /* We will just duplicate the second pixel at the packet */
239 wmax/=2;
240
241 /* Generate a standard color bar pattern */
242 for (w=0;w<wmax;w++) {
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300243 int colorpos=((w+count)*8/(wmax+1)) % 8;
244 r=bars[colorpos][0];
245 g=bars[colorpos][1];
246 b=bars[colorpos][2];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300247
248 for (color=0;color<4;color++) {
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300249 p=basep+pos;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300250
251 switch (color) {
252 case 0:
253 case 2:
254 *p=TO_Y(r,g,b); /* Luminance */
255 break;
256 case 1:
257 *p=TO_U(r,g,b); /* Cb */
258 break;
259 case 3:
260 *p=TO_V(r,g,b); /* Cr */
261 break;
262 }
263 pos++;
264 }
265 }
266
267 /* Checks if it is possible to show timestamp */
268 if (TSTAMP_MAX_Y>=hmax)
269 goto end;
270 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
271 goto end;
272
273 /* Print stream time */
274 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
275 j=TSTAMP_MIN_X;
276 for (s=timestr;*s;s++) {
277 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
278 for (i=0;i<7;i++) {
279 if (chr&1<<(7-i)) { /* Font color*/
280 r=bars[BLUE][0];
281 g=bars[BLUE][1];
282 b=bars[BLUE][2];
283 r=g=b=0;
284 g=198;
285 } else { /* Background color */
286 r=bars[WHITE][0];
287 g=bars[WHITE][1];
288 b=bars[WHITE][2];
289 r=g=b=0;
290 }
291
292 pos=inipos+j*2;
293 for (color=0;color<4;color++) {
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300294 p=basep+pos;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300295
296 y=TO_Y(r,g,b);
297
298 switch (color) {
299 case 0:
300 case 2:
301 *p=TO_Y(r,g,b); /* Luminance */
302 break;
303 case 1:
304 *p=TO_U(r,g,b); /* Cb */
305 break;
306 case 3:
307 *p=TO_V(r,g,b); /* Cr */
308 break;
309 }
310 pos++;
311 }
312 j++;
313 }
314 }
315 }
316
317
318end:
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300319 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300320}
321static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
322{
323 int h,pos=0;
324 int hmax = buf->vb.height;
325 int wmax = buf->vb.width;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300326 struct timeval ts;
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300327 char *tmpbuf = kmalloc(wmax*2,GFP_KERNEL);
328 void *vbuf=videobuf_to_vmalloc (&buf->vb);
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300329 /* FIXME: move to dev struct */
330 static int mv_count=0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300331
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300332 if (!tmpbuf)
333 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300334
335 for (h=0;h<hmax;h++) {
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300336 gen_line(tmpbuf,0,wmax,hmax,h,mv_count,
337 dev->timestr);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300338 /* FIXME: replacing to __copy_to_user */
339 if (copy_to_user(vbuf+pos,tmpbuf,wmax*2)!=0)
340 dprintk(2,"vivifill copy_to_user failed.\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300341 pos += wmax*2;
342 }
343
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300344 mv_count++;
345
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300346 kfree(tmpbuf);
347
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300348 /* Updates stream time */
349
350 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
351 dev->jiffies=jiffies;
352 if (dev->us>=1000000) {
353 dev->us-=1000000;
354 dev->s++;
355 if (dev->s>=60) {
356 dev->s-=60;
357 dev->m++;
358 if (dev->m>60) {
359 dev->m-=60;
360 dev->h++;
361 if (dev->h>24)
362 dev->h-=24;
363 }
364 }
365 }
366 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
367 dev->h,dev->m,dev->s,(dev->us+500)/1000);
368
369 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300370 (unsigned long)tmpbuf,pos);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300371
372 /* Advice that buffer was filled */
373 buf->vb.state = STATE_DONE;
374 buf->vb.field_count++;
375 do_gettimeofday(&ts);
376 buf->vb.ts = ts;
377
378 list_del(&buf->vb.queue);
379 wake_up(&buf->vb.done);
380}
381
382static int restart_video_queue(struct vivi_dmaqueue *dma_q);
383
384static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
385{
386 struct vivi_buffer *buf;
387 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
388
389 int bc;
390
391 /* Announces videobuf that all went ok */
392 for (bc = 0;; bc++) {
393 if (list_empty(&dma_q->active)) {
394 dprintk(1,"No active queue to serve\n");
395 break;
396 }
397
398 buf = list_entry(dma_q->active.next,
399 struct vivi_buffer, vb.queue);
400
401 /* Nobody is waiting something to be done, just return */
402 if (!waitqueue_active(&buf->vb.done)) {
403 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
404 return;
405 }
406
407 do_gettimeofday(&buf->vb.ts);
408 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
409
410 /* Fill buffer */
411 vivi_fillbuff(dev,buf);
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300412
413 if (list_empty(&dma_q->active)) {
414 del_timer(&dma_q->timeout);
415 } else {
416 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
417 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300418 }
419 if (bc != 1)
420 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
421}
422
Adrian Bunk972c3512006-04-27 21:06:50 -0300423static void vivi_sleep(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300424{
425 int timeout;
426 DECLARE_WAITQUEUE(wait, current);
427
428 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
429
430 add_wait_queue(&dma_q->wq, &wait);
431 if (!kthread_should_stop()) {
432 dma_q->frame++;
433
434 /* Calculate time to wake up */
435 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
436
437 if (timeout <= 0) {
438 int old=dma_q->frame;
439 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
440
441 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
442
443 dprintk(1,"underrun, losed %d frames. "
444 "Now, frame is %d. Waking on %d jiffies\n",
445 dma_q->frame-old,dma_q->frame,timeout);
446 } else
447 dprintk(1,"will sleep for %i jiffies\n",timeout);
448
449 vivi_thread_tick(dma_q);
450
451 schedule_timeout_interruptible (timeout);
452 }
453
454 remove_wait_queue(&dma_q->wq, &wait);
455 try_to_freeze();
456}
457
Adrian Bunk972c3512006-04-27 21:06:50 -0300458static int vivi_thread(void *data)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300459{
460 struct vivi_dmaqueue *dma_q=data;
461
462 dprintk(1,"thread started\n");
463
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300464 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700465 set_freezable();
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300466
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300467 for (;;) {
468 vivi_sleep(dma_q);
469
470 if (kthread_should_stop())
471 break;
472 }
473 dprintk(1, "thread: exit\n");
474 return 0;
475}
476
Adrian Bunk972c3512006-04-27 21:06:50 -0300477static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300478{
479 dma_q->frame=0;
480 dma_q->ini_jiffies=jiffies;
481
482 dprintk(1,"%s\n",__FUNCTION__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300483
484 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
485
Akinobu Mita054afee2006-12-20 10:04:00 -0300486 if (IS_ERR(dma_q->kthread)) {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300487 printk(KERN_ERR "vivi: kernel_thread() failed\n");
Akinobu Mita054afee2006-12-20 10:04:00 -0300488 return PTR_ERR(dma_q->kthread);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300489 }
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300490 /* Wakes thread */
491 wake_up_interruptible(&dma_q->wq);
492
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300493 dprintk(1,"returning from %s\n",__FUNCTION__);
494 return 0;
495}
496
Adrian Bunk972c3512006-04-27 21:06:50 -0300497static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300498{
499 dprintk(1,"%s\n",__FUNCTION__);
500 /* shutdown control thread */
501 if (dma_q->kthread) {
502 kthread_stop(dma_q->kthread);
503 dma_q->kthread=NULL;
504 }
505}
506
507static int restart_video_queue(struct vivi_dmaqueue *dma_q)
508{
509 struct vivi_buffer *buf, *prev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300510
511 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
512
513 if (!list_empty(&dma_q->active)) {
514 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
515 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
516 buf, buf->vb.i);
517
518 dprintk(1,"Restarting video dma\n");
519 vivi_stop_thread(dma_q);
520// vivi_start_thread(dma_q);
521
522 /* cancel all outstanding capture / vbi requests */
Trent Piephoa991f442007-10-10 05:37:43 -0300523 list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300524 list_del(&buf->vb.queue);
525 buf->vb.state = STATE_ERROR;
526 wake_up(&buf->vb.done);
527 }
528 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
529
530 return 0;
531 }
532
533 prev = NULL;
534 for (;;) {
535 if (list_empty(&dma_q->queued))
536 return 0;
537 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
538 if (NULL == prev) {
539 list_del(&buf->vb.queue);
540 list_add_tail(&buf->vb.queue,&dma_q->active);
541
542 dprintk(1,"Restarting video dma\n");
543 vivi_stop_thread(dma_q);
544 vivi_start_thread(dma_q);
545
546 buf->vb.state = STATE_ACTIVE;
547 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
548 dprintk(2,"[%p/%d] restart_queue - first active\n",
549 buf,buf->vb.i);
550
551 } else if (prev->vb.width == buf->vb.width &&
552 prev->vb.height == buf->vb.height &&
553 prev->fmt == buf->fmt) {
554 list_del(&buf->vb.queue);
555 list_add_tail(&buf->vb.queue,&dma_q->active);
556 buf->vb.state = STATE_ACTIVE;
557 dprintk(2,"[%p/%d] restart_queue - move to active\n",
558 buf,buf->vb.i);
559 } else {
560 return 0;
561 }
562 prev = buf;
563 }
564}
565
566static void vivi_vid_timeout(unsigned long data)
567{
568 struct vivi_dev *dev = (struct vivi_dev*)data;
569 struct vivi_dmaqueue *vidq = &dev->vidq;
570 struct vivi_buffer *buf;
571
572 while (!list_empty(&vidq->active)) {
573 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
574 list_del(&buf->vb.queue);
575 buf->vb.state = STATE_ERROR;
576 wake_up(&buf->vb.done);
577 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
578 }
579
580 restart_video_queue(vidq);
581}
582
583/* ------------------------------------------------------------------
584 Videobuf operations
585 ------------------------------------------------------------------*/
586static int
587buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
588{
589 struct vivi_fh *fh = vq->priv_data;
590
591 *size = fh->width*fh->height*2;
592
593 if (0 == *count)
594 *count = 32;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300595
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300596 while (*size * *count > vid_limit * 1024 * 1024)
597 (*count)--;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300598
599 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
600
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300601 return 0;
602}
603
Adrian Bunk972c3512006-04-27 21:06:50 -0300604static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300605{
606 dprintk(1,"%s\n",__FUNCTION__);
607
608 if (in_interrupt())
609 BUG();
610
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300611 videobuf_waiton(&buf->vb,0,0);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300612 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300613 buf->vb.state = STATE_NEEDS_INIT;
614}
615
616#define norm_maxw() 1024
617#define norm_maxh() 768
618static int
619buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
620 enum v4l2_field field)
621{
622 struct vivi_fh *fh = vq->priv_data;
623 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
624 int rc, init_buffer = 0;
625
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300626 dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300627
628 BUG_ON(NULL == fh->fmt);
629 if (fh->width < 48 || fh->width > norm_maxw() ||
630 fh->height < 32 || fh->height > norm_maxh())
631 return -EINVAL;
632 buf->vb.size = fh->width*fh->height*2;
633 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
634 return -EINVAL;
635
636 if (buf->fmt != fh->fmt ||
637 buf->vb.width != fh->width ||
638 buf->vb.height != fh->height ||
639 buf->vb.field != field) {
640 buf->fmt = fh->fmt;
641 buf->vb.width = fh->width;
642 buf->vb.height = fh->height;
643 buf->vb.field = field;
644 init_buffer = 1;
645 }
646
647 if (STATE_NEEDS_INIT == buf->vb.state) {
648 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
649 goto fail;
650 }
651
652 buf->vb.state = STATE_PREPARED;
653
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300654 return 0;
655
656fail:
657 free_buffer(vq,buf);
658 return rc;
659}
660
661static void
662buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
663{
664 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
665 struct vivi_fh *fh = vq->priv_data;
666 struct vivi_dev *dev = fh->dev;
667 struct vivi_dmaqueue *vidq = &dev->vidq;
668 struct vivi_buffer *prev;
669
670 if (!list_empty(&vidq->queued)) {
671 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
672 list_add_tail(&buf->vb.queue,&vidq->queued);
673 buf->vb.state = STATE_QUEUED;
674 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
675 buf, buf->vb.i);
676 } else if (list_empty(&vidq->active)) {
677 list_add_tail(&buf->vb.queue,&vidq->active);
678
679 buf->vb.state = STATE_ACTIVE;
680 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
681 dprintk(2,"[%p/%d] buffer_queue - first active\n",
682 buf, buf->vb.i);
683
684 vivi_start_thread(vidq);
685 } else {
686 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
687 if (prev->vb.width == buf->vb.width &&
688 prev->vb.height == buf->vb.height &&
689 prev->fmt == buf->fmt) {
690 list_add_tail(&buf->vb.queue,&vidq->active);
691 buf->vb.state = STATE_ACTIVE;
692 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
693 buf, buf->vb.i);
694
695 } else {
696 list_add_tail(&buf->vb.queue,&vidq->queued);
697 buf->vb.state = STATE_QUEUED;
698 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
699 buf, buf->vb.i);
700 }
701 }
702}
703
704static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
705{
706 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
707 struct vivi_fh *fh = vq->priv_data;
708 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
709 struct vivi_dmaqueue *vidq = &dev->vidq;
710
711 dprintk(1,"%s\n",__FUNCTION__);
712
713 vivi_stop_thread(vidq);
714
715 free_buffer(vq,buf);
716}
717
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300718static struct videobuf_queue_ops vivi_video_qops = {
719 .buf_setup = buffer_setup,
720 .buf_prepare = buffer_prepare,
721 .buf_queue = buffer_queue,
722 .buf_release = buffer_release,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300723};
724
725/* ------------------------------------------------------------------
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300726 IOCTL vidioc handling
727 ------------------------------------------------------------------*/
728static int vidioc_querycap (struct file *file, void *priv,
729 struct v4l2_capability *cap)
730{
731 strcpy(cap->driver, "vivi");
732 strcpy(cap->card, "vivi");
733 cap->version = VIVI_VERSION;
734 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
735 V4L2_CAP_STREAMING |
736 V4L2_CAP_READWRITE;
737 return 0;
738}
739
740static int vidioc_enum_fmt_cap (struct file *file, void *priv,
741 struct v4l2_fmtdesc *f)
742{
743 if (f->index > 0)
744 return -EINVAL;
745
746 strlcpy(f->description,format.name,sizeof(f->description));
747 f->pixelformat = format.fourcc;
748 return 0;
749}
750
751static int vidioc_g_fmt_cap (struct file *file, void *priv,
752 struct v4l2_format *f)
753{
754 struct vivi_fh *fh=priv;
755
756 f->fmt.pix.width = fh->width;
757 f->fmt.pix.height = fh->height;
758 f->fmt.pix.field = fh->vb_vidq.field;
759 f->fmt.pix.pixelformat = fh->fmt->fourcc;
760 f->fmt.pix.bytesperline =
761 (f->fmt.pix.width * fh->fmt->depth) >> 3;
762 f->fmt.pix.sizeimage =
763 f->fmt.pix.height * f->fmt.pix.bytesperline;
764
765 return (0);
766}
767
768static int vidioc_try_fmt_cap (struct file *file, void *priv,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300769 struct v4l2_format *f)
770{
771 struct vivi_fmt *fmt;
772 enum v4l2_field field;
773 unsigned int maxw, maxh;
774
775 if (format.fourcc != f->fmt.pix.pixelformat) {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300776 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
777 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300778 return -EINVAL;
779 }
780 fmt=&format;
781
782 field = f->fmt.pix.field;
783
784 if (field == V4L2_FIELD_ANY) {
Brandon Philipsa326ae12007-09-27 20:54:52 -0300785 field=V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300786 } else if (V4L2_FIELD_INTERLACED != field) {
787 dprintk(1,"Field type invalid.\n");
788 return -EINVAL;
789 }
790
791 maxw = norm_maxw();
792 maxh = norm_maxh();
793
794 f->fmt.pix.field = field;
795 if (f->fmt.pix.height < 32)
796 f->fmt.pix.height = 32;
797 if (f->fmt.pix.height > maxh)
798 f->fmt.pix.height = maxh;
799 if (f->fmt.pix.width < 48)
800 f->fmt.pix.width = 48;
801 if (f->fmt.pix.width > maxw)
802 f->fmt.pix.width = maxw;
803 f->fmt.pix.width &= ~0x03;
804 f->fmt.pix.bytesperline =
805 (f->fmt.pix.width * fmt->depth) >> 3;
806 f->fmt.pix.sizeimage =
807 f->fmt.pix.height * f->fmt.pix.bytesperline;
808
809 return 0;
810}
811
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300812/*FIXME: This seems to be generic enough to be at videodev2 */
813static int vidioc_s_fmt_cap (struct file *file, void *priv,
814 struct v4l2_format *f)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300815{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300816 struct vivi_fh *fh=priv;
817 int ret = vidioc_try_fmt_cap(file,fh,f);
818 if (ret < 0)
819 return (ret);
820
821 fh->fmt = &format;
822 fh->width = f->fmt.pix.width;
823 fh->height = f->fmt.pix.height;
824 fh->vb_vidq.field = f->fmt.pix.field;
825 fh->type = f->type;
826
827 return (0);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300828}
829
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300830static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300831{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300832 struct vivi_fh *fh=priv;
833
834 return (videobuf_reqbufs(&fh->vb_vidq, p));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300835}
836
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300837static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300838{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300839 struct vivi_fh *fh=priv;
840
841 return (videobuf_querybuf(&fh->vb_vidq, p));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300842}
843
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300844static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300845{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300846 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300847
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300848 return (videobuf_qbuf(&fh->vb_vidq, p));
849}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300850
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300851static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
852{
853 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300854
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300855 return (videobuf_dqbuf(&fh->vb_vidq, p,
856 file->f_flags & O_NONBLOCK));
857}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300858
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -0300859#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300860static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
861{
862 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300863
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300864 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300865}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300866#endif
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300867
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300868static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300869{
870 struct vivi_fh *fh=priv;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300871
872 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 return -EINVAL;
874 if (i != fh->type)
875 return -EINVAL;
876
Brandon Philipsba32bd92007-09-27 20:55:17 -0300877 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300878}
879
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300880static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300881{
882 struct vivi_fh *fh=priv;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300883
884 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
885 return -EINVAL;
886 if (i != fh->type)
887 return -EINVAL;
888
Brandon Philipsba32bd92007-09-27 20:55:17 -0300889 return videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300890}
891
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300892static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300893{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300894 return 0;
895}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300896
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300897/* only one input in this sample driver */
898static int vidioc_enum_input (struct file *file, void *priv,
899 struct v4l2_input *inp)
900{
901 if (inp->index != 0)
902 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300903
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300904 inp->type = V4L2_INPUT_TYPE_CAMERA;
905 inp->std = V4L2_STD_NTSC_M;
906 strcpy(inp->name,"Camera");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300907
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300908 return (0);
909}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300910
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300911static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
912{
913 *i = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300914
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300915 return (0);
916}
917static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
918{
919 if (i > 0)
920 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300921
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300922 return (0);
923}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300924
925 /* --- controls ---------------------------------------------- */
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300926static int vidioc_queryctrl (struct file *file, void *priv,
927 struct v4l2_queryctrl *qc)
928{
929 int i;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300930
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300931 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
932 if (qc->id && qc->id == vivi_qctrl[i].id) {
933 memcpy(qc, &(vivi_qctrl[i]),
934 sizeof(*qc));
935 return (0);
936 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300937
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300938 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300939}
940
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300941static int vidioc_g_ctrl (struct file *file, void *priv,
942 struct v4l2_control *ctrl)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300943{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300944 int i;
945
946 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
947 if (ctrl->id == vivi_qctrl[i].id) {
948 ctrl->value=qctl_regs[i];
949 return (0);
950 }
951
952 return -EINVAL;
953}
954static int vidioc_s_ctrl (struct file *file, void *priv,
955 struct v4l2_control *ctrl)
956{
957 int i;
958
959 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
960 if (ctrl->id == vivi_qctrl[i].id) {
961 if (ctrl->value <
962 vivi_qctrl[i].minimum
963 || ctrl->value >
964 vivi_qctrl[i].maximum) {
965 return (-ERANGE);
966 }
967 qctl_regs[i]=ctrl->value;
968 return (0);
969 }
970 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300971}
972
973/* ------------------------------------------------------------------
974 File operations for the device
975 ------------------------------------------------------------------*/
976
977#define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
978
979static int vivi_open(struct inode *inode, struct file *file)
980{
981 int minor = iminor(inode);
Trent Piephoa991f442007-10-10 05:37:43 -0300982 struct vivi_dev *dev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300983 struct vivi_fh *fh;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300984 int i;
985
986 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
987
Trent Piephoa991f442007-10-10 05:37:43 -0300988 list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
989 if (dev->vfd.minor == minor)
990 goto found;
991 return -ENODEV;
992found:
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300993
994
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300995
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300996 /* If more than one user, mutex should be added */
997 dev->users++;
998
Trent Piephoa991f442007-10-10 05:37:43 -0300999 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1000 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001001
1002 /* allocate + initialize per filehandle data */
1003 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1004 if (NULL == fh) {
1005 dev->users--;
1006 return -ENOMEM;
1007 }
1008
1009 file->private_data = fh;
1010 fh->dev = dev;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001011
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001012 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1013 fh->fmt = &format;
1014 fh->width = 640;
1015 fh->height = 480;
1016
1017 /* Put all controls at a sane state */
1018 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1019 qctl_regs[i] =vivi_qctrl[i].default_value;
1020
1021 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1022 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1023 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1024 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1025
1026 /* Resets frame counters */
1027 dev->h=0;
1028 dev->m=0;
1029 dev->s=0;
1030 dev->us=0;
1031 dev->jiffies=jiffies;
1032 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1033 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1034
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001035 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001036 NULL, NULL,
1037 fh->type,
1038 V4L2_FIELD_INTERLACED,
1039 sizeof(struct vivi_buffer),fh);
1040
1041 return 0;
1042}
1043
1044static ssize_t
1045vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1046{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001047 struct vivi_fh *fh = file->private_data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001048
1049 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehabacb09af2007-07-29 22:56:11 -03001050 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001051 file->f_flags & O_NONBLOCK);
1052 }
1053 return 0;
1054}
1055
1056static unsigned int
1057vivi_poll(struct file *file, struct poll_table_struct *wait)
1058{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001059 struct vivi_fh *fh = file->private_data;
Brandon Philips85c7c70bc2007-09-27 20:55:02 -03001060 struct videobuf_queue *q = &fh->vb_vidq;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001061
1062 dprintk(1,"%s\n",__FUNCTION__);
1063
1064 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1065 return POLLERR;
1066
Brandon Philips85c7c70bc2007-09-27 20:55:02 -03001067 return videobuf_poll_stream(file, q, wait);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001068}
1069
1070static int vivi_release(struct inode *inode, struct file *file)
1071{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001072 struct vivi_fh *fh = file->private_data;
1073 struct vivi_dev *dev = fh->dev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001074 struct vivi_dmaqueue *vidq = &dev->vidq;
1075
1076 int minor = iminor(inode);
1077
1078 vivi_stop_thread(vidq);
Brandon Philips053fcb62007-11-13 20:11:26 -03001079 videobuf_stop(&fh->vb_vidq);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001080 videobuf_mmap_free(&fh->vb_vidq);
1081
1082 kfree (fh);
1083
1084 dev->users--;
1085
1086 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1087
1088 return 0;
1089}
1090
1091static int
1092vivi_mmap(struct file *file, struct vm_area_struct * vma)
1093{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001094 struct vivi_fh *fh = file->private_data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001095 int ret;
1096
1097 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1098
1099 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1100
1101 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1102 (unsigned long)vma->vm_start,
1103 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1104 ret);
1105
1106 return ret;
1107}
1108
Arjan van de Venfa027c22007-02-12 00:55:33 -08001109static const struct file_operations vivi_fops = {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001110 .owner = THIS_MODULE,
1111 .open = vivi_open,
1112 .release = vivi_release,
1113 .read = vivi_read,
1114 .poll = vivi_poll,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001115 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001116 .mmap = vivi_mmap,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001117 .llseek = no_llseek,
1118};
1119
1120static struct video_device vivi = {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001121 .name = "vivi",
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001122 .type = VID_TYPE_CAPTURE,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001123 .fops = &vivi_fops,
1124 .minor = -1,
1125// .release = video_device_release,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001126
1127 .vidioc_querycap = vidioc_querycap,
1128 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1129 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1130 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1131 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1132 .vidioc_reqbufs = vidioc_reqbufs,
1133 .vidioc_querybuf = vidioc_querybuf,
1134 .vidioc_qbuf = vidioc_qbuf,
1135 .vidioc_dqbuf = vidioc_dqbuf,
1136 .vidioc_s_std = vidioc_s_std,
1137 .vidioc_enum_input = vidioc_enum_input,
1138 .vidioc_g_input = vidioc_g_input,
1139 .vidioc_s_input = vidioc_s_input,
1140 .vidioc_queryctrl = vidioc_queryctrl,
1141 .vidioc_g_ctrl = vidioc_g_ctrl,
1142 .vidioc_s_ctrl = vidioc_s_ctrl,
1143 .vidioc_streamon = vidioc_streamon,
1144 .vidioc_streamoff = vidioc_streamoff,
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001145#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001146 .vidiocgmbuf = vidiocgmbuf,
1147#endif
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001148 .tvnorms = V4L2_STD_NTSC_M,
1149 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001150};
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001151/* -----------------------------------------------------------------
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001152 Initialization and module stuff
1153 ------------------------------------------------------------------*/
1154
1155static int __init vivi_init(void)
1156{
1157 int ret;
1158 struct vivi_dev *dev;
1159
1160 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1161 if (NULL == dev)
1162 return -ENOMEM;
1163 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1164
1165 /* init video dma queues */
1166 INIT_LIST_HEAD(&dev->vidq.active);
1167 INIT_LIST_HEAD(&dev->vidq.queued);
Mauro Carvalho Chehabdf3a7102007-01-13 09:25:16 -03001168 init_waitqueue_head(&dev->vidq.wq);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001169
1170 /* initialize locks */
Matthias Kaehlcke51b54022007-07-02 10:19:38 -03001171 mutex_init(&dev->lock);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001172
1173 dev->vidq.timeout.function = vivi_vid_timeout;
1174 dev->vidq.timeout.data = (unsigned long)dev;
1175 init_timer(&dev->vidq.timeout);
1176
1177 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1178 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1179 return ret;
1180}
1181
1182static void __exit vivi_exit(void)
1183{
1184 struct vivi_dev *h;
1185 struct list_head *list;
1186
Akinobu Mita72f678c2006-12-20 10:03:53 -03001187 while (!list_empty(&vivi_devlist)) {
1188 list = vivi_devlist.next;
1189 list_del(list);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001190 h = list_entry(list, struct vivi_dev, vivi_devlist);
1191 kfree (h);
1192 }
1193 video_unregister_device(&vivi);
1194}
1195
1196module_init(vivi_init);
1197module_exit(vivi_exit);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001198
1199MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1200MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1201MODULE_LICENSE("Dual BSD/GPL");
1202
1203module_param(video_nr, int, 0);
1204
1205module_param_named(debug,vivi.debug, int, 0644);
1206MODULE_PARM_DESC(debug,"activates debug info");
1207
1208module_param(vid_limit,int,0644);
1209MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1210