V4L/DVB (3788): Fix compilation with V4L1_COMPAT
[linux-2.6] / drivers / media / video / vivi.c
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>
28 #include <linux/videodev2.h>
29 #ifdef CONFIG_VIDEO_V4L1_COMPAT
30 /* Include V4L1 specific functions. Should be removed soon */
31 #include <linux/videodev.h>
32 #endif
33 #include <linux/interrupt.h>
34 #include <media/video-buf.h>
35 #include <media/v4l2-common.h>
36 #include <linux/kthread.h>
37 #include <linux/highmem.h>
38
39 /* Wake up at about 30 fps */
40 #define WAKE_NUMERATOR 30
41 #define WAKE_DENOMINATOR 1001
42 #define BUFFER_TIMEOUT     msecs_to_jiffies(500)  /* 0.5 seconds */
43
44 /* These timers are for 1 fps - used only for testing */
45 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
46 //#define BUFFER_TIMEOUT     msecs_to_jiffies(5000)  /* 5 seconds */
47
48 #include "font.h"
49
50 #ifndef kzalloc
51 #define kzalloc(size, flags)                            \
52 ({                                                      \
53         void *__ret = kmalloc(size, flags);             \
54         if (__ret)                                      \
55                 memset(__ret, 0, size);                 \
56         __ret;                                          \
57 })
58 #endif
59
60 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
61 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
62 MODULE_LICENSE("Dual BSD/GPL");
63
64 #define VIVI_MAJOR_VERSION 0
65 #define VIVI_MINOR_VERSION 4
66 #define VIVI_RELEASE 0
67 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
68
69 static int video_nr = -1;        /* /dev/videoN, -1 for autodetect */
70 module_param(video_nr, int, 0);
71
72 static int debug = 0;
73 module_param(debug, int, 0);
74
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
78
79 /* supported controls */
80 static struct v4l2_queryctrl vivi_qctrl[] = {
81         {
82                 .id            = V4L2_CID_AUDIO_VOLUME,
83                 .name          = "Volume",
84                 .minimum       = 0,
85                 .maximum       = 65535,
86                 .step          = 65535/100,
87                 .default_value = 65535,
88                 .flags         = 0,
89                 .type          = V4L2_CTRL_TYPE_INTEGER,
90         },{
91                 .id            = V4L2_CID_BRIGHTNESS,
92                 .type          = V4L2_CTRL_TYPE_INTEGER,
93                 .name          = "Brightness",
94                 .minimum       = 0,
95                 .maximum       = 255,
96                 .step          = 1,
97                 .default_value = 127,
98                 .flags         = 0,
99         }, {
100                 .id            = V4L2_CID_CONTRAST,
101                 .type          = V4L2_CTRL_TYPE_INTEGER,
102                 .name          = "Contrast",
103                 .minimum       = 0,
104                 .maximum       = 255,
105                 .step          = 0x1,
106                 .default_value = 0x10,
107                 .flags         = 0,
108         }, {
109                 .id            = V4L2_CID_SATURATION,
110                 .type          = V4L2_CTRL_TYPE_INTEGER,
111                 .name          = "Saturation",
112                 .minimum       = 0,
113                 .maximum       = 255,
114                 .step          = 0x1,
115                 .default_value = 127,
116                 .flags         = 0,
117         }, {
118                 .id            = V4L2_CID_HUE,
119                 .type          = V4L2_CTRL_TYPE_INTEGER,
120                 .name          = "Hue",
121                 .minimum       = -128,
122                 .maximum       = 127,
123                 .step          = 0x1,
124                 .default_value = 0,
125                 .flags         = 0,
126         }
127 };
128
129 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
130
131 #define dprintk(level,fmt, arg...)                           \
132         do {                                                 \
133                 if (debug >= (level))                        \
134                         printk(KERN_DEBUG "vivi: " fmt , ## arg);    \
135         } while (0)
136
137 /* ------------------------------------------------------------------
138         Basic structures
139    ------------------------------------------------------------------*/
140
141 struct vivi_fmt {
142         char  *name;
143         u32   fourcc;          /* v4l2 format id */
144         int   depth;
145 };
146
147 static struct vivi_fmt format = {
148         .name     = "4:2:2, packed, YUYV",
149         .fourcc   = V4L2_PIX_FMT_YUYV,
150         .depth    = 16,
151 };
152
153 struct sg_to_addr {
154         int pos;
155         struct scatterlist *sg;
156 };
157
158 /* buffer for one video frame */
159 struct vivi_buffer {
160         /* common v4l buffer stuff -- must be first */
161         struct videobuf_buffer vb;
162
163         struct vivi_fmt        *fmt;
164
165         struct sg_to_addr      *to_addr;
166 };
167
168 struct vivi_dmaqueue {
169         struct list_head       active;
170         struct list_head       queued;
171         struct timer_list      timeout;
172
173         /* thread for generating video stream*/
174         struct task_struct         *kthread;
175         wait_queue_head_t          wq;
176         /* Counters to control fps rate */
177         int                        frame;
178         int                        ini_jiffies;
179 };
180
181 static LIST_HEAD(vivi_devlist);
182
183 struct vivi_dev {
184         struct list_head           vivi_devlist;
185
186         struct semaphore           lock;
187
188         int                        users;
189
190         /* various device info */
191         unsigned int               resources;
192         struct video_device        video_dev;
193
194         struct vivi_dmaqueue       vidq;
195
196         /* Several counters */
197         int                        h,m,s,us,jiffies;
198         char                       timestr[13];
199 };
200
201 struct vivi_fh {
202         struct vivi_dev            *dev;
203
204         /* video capture */
205         struct vivi_fmt            *fmt;
206         unsigned int               width,height;
207         struct videobuf_queue      vb_vidq;
208
209         enum v4l2_buf_type         type;
210 };
211
212 /* ------------------------------------------------------------------
213         DMA and thread functions
214    ------------------------------------------------------------------*/
215
216 /* Bars and Colors should match positions */
217
218 enum colors {
219         WHITE,
220         AMBAR,
221         CYAN,
222         GREEN,
223         MAGENTA,
224         RED,
225         BLUE
226 };
227
228 static u8 bars[8][3] = {
229         /* R   G   B */
230         {204,204,204},  /* white */
231         {208,208,  0},  /* ambar */
232         {  0,206,206},  /* cyan */
233         {  0,239,  0},  /* green */
234         {239,  0,239},  /* magenta */
235         {205,  0,  0},  /* red */
236         {  0,  0,255},  /* blue */
237         {  0,  0,  0}
238 };
239
240 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b  + 32768)>>16)+16)
241 /* RGB to  V(Cr) Color transform */
242 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b  + 32768)>>16)+128)
243 /* RGB to  U(Cb) Color transform */
244 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
245
246 #define TSTAMP_MIN_Y 24
247 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
248 #define TSTAMP_MIN_X 64
249
250 void prep_to_addr(struct sg_to_addr to_addr[],struct videobuf_buffer *vb)
251 {
252         int i, pos=0;
253
254         for (i=0;i<vb->dma.nr_pages;i++) {
255                 to_addr[i].sg=&vb->dma.sglist[i];
256                 to_addr[i].pos=pos;
257                 pos += vb->dma.sglist[i].length;
258         }
259 }
260
261 inline int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
262 {
263         int p1=0,p2=pages-1,p3=pages/2;
264
265         /* Sanity test */
266         BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
267
268         while (p1+1<p2) {
269                 if (pos < to_addr[p3].pos) {
270                         p2=p3;
271                 } else {
272                         p1=p3;
273                 }
274                 p3=(p1+p2)/2;
275         }
276         if (pos >= to_addr[p2].pos)
277                 p1=p2;
278
279         return (p1);
280 }
281
282 void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
283                                         int hmax, int line, char *timestr)
284 {
285         int  w,i,j,pos=inipos,pgpos,oldpg,y;
286         char *p,*s,*basep;
287         struct page *pg;
288         u8   chr,r,g,b,color;
289
290         /* Get first addr pointed to pixel position */
291         oldpg=get_addr_pos(pos,pages,to_addr);
292         pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
293         basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
294
295         /* We will just duplicate the second pixel at the packet */
296         wmax/=2;
297
298         /* Generate a standard color bar pattern */
299         for (w=0;w<wmax;w++) {
300                 r=bars[w*7/wmax][0];
301                 g=bars[w*7/wmax][1];
302                 b=bars[w*7/wmax][2];
303
304                 for (color=0;color<4;color++) {
305                         pgpos=get_addr_pos(pos,pages,to_addr);
306                         if (pgpos!=oldpg) {
307                                 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
308                                 kunmap_atomic(basep, KM_BOUNCE_READ);
309                                 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
310                                 oldpg=pgpos;
311                         }
312                         p=basep+pos-to_addr[pgpos].pos;
313
314                         switch (color) {
315                                 case 0:
316                                 case 2:
317                                         *p=TO_Y(r,g,b);         /* Luminance */
318                                         break;
319                                 case 1:
320                                         *p=TO_U(r,g,b);         /* Cb */
321                                         break;
322                                 case 3:
323                                         *p=TO_V(r,g,b);         /* Cr */
324                                         break;
325                         }
326                         pos++;
327                 }
328         }
329
330         /* Checks if it is possible to show timestamp */
331         if (TSTAMP_MAX_Y>=hmax)
332                 goto end;
333         if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
334                 goto end;
335
336         /* Print stream time */
337         if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
338                 j=TSTAMP_MIN_X;
339                 for (s=timestr;*s;s++) {
340                         chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
341                         for (i=0;i<7;i++) {
342                                 if (chr&1<<(7-i)) { /* Font color*/
343                                         r=bars[BLUE][0];
344                                         g=bars[BLUE][1];
345                                         b=bars[BLUE][2];
346                                         r=g=b=0;
347                                         g=198;
348                                 } else { /* Background color */
349                                         r=bars[WHITE][0];
350                                         g=bars[WHITE][1];
351                                         b=bars[WHITE][2];
352                                         r=g=b=0;
353                                 }
354
355                                 pos=inipos+j*2;
356                                 for (color=0;color<4;color++) {
357                                         pgpos=get_addr_pos(pos,pages,to_addr);
358                                         if (pgpos!=oldpg) {
359                                                 pg=pfn_to_page(to_addr[pgpos].
360                                                                 sg->dma_address
361                                                                 >> PAGE_SHIFT);
362                                                 kunmap_atomic(basep,
363                                                                 KM_BOUNCE_READ);
364                                                 basep= kmap_atomic(pg,
365                                                         KM_BOUNCE_READ)+
366                                                         to_addr[pgpos].sg->offset;
367                                                 oldpg=pgpos;
368                                         }
369                                         p=basep+pos-to_addr[pgpos].pos;
370
371                                         y=TO_Y(r,g,b);
372
373                                         switch (color) {
374                                                 case 0:
375                                                 case 2:
376                                                         *p=TO_Y(r,g,b);         /* Luminance */
377                                                         break;
378                                                 case 1:
379                                                         *p=TO_U(r,g,b);         /* Cb */
380                                                         break;
381                                                 case 3:
382                                                         *p=TO_V(r,g,b);         /* Cr */
383                                                         break;
384                                         }
385                                         pos++;
386                                 }
387                                 j++;
388                         }
389                 }
390         }
391
392
393 end:
394         kunmap_atomic(basep, KM_BOUNCE_READ);
395 }
396 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
397 {
398         int h,pos=0;
399         int hmax  = buf->vb.height;
400         int wmax  = buf->vb.width;
401         struct videobuf_buffer *vb=&buf->vb;
402         struct sg_to_addr *to_addr=buf->to_addr;
403         struct timeval ts;
404
405         /* Test if DMA mapping is ready */
406         if (!vb->dma.sglist[0].dma_address)
407                 return;
408
409         prep_to_addr(to_addr,vb);
410
411         /* Check if there is enough memory */
412         BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
413
414         for (h=0;h<hmax;h++) {
415                 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
416                 pos += wmax*2;
417         }
418
419         /* Updates stream time */
420
421         dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
422         dev->jiffies=jiffies;
423         if (dev->us>=1000000) {
424                 dev->us-=1000000;
425                 dev->s++;
426                 if (dev->s>=60) {
427                         dev->s-=60;
428                         dev->m++;
429                         if (dev->m>60) {
430                                 dev->m-=60;
431                                 dev->h++;
432                                 if (dev->h>24)
433                                         dev->h-=24;
434                         }
435                 }
436         }
437         sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
438                         dev->h,dev->m,dev->s,(dev->us+500)/1000);
439
440         dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
441                         (unsigned long)buf->vb.dma.vmalloc,pos);
442
443         /* Advice that buffer was filled */
444         buf->vb.state = STATE_DONE;
445         buf->vb.field_count++;
446         do_gettimeofday(&ts);
447         buf->vb.ts = ts;
448
449         list_del(&buf->vb.queue);
450         wake_up(&buf->vb.done);
451 }
452
453 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
454
455 static void vivi_thread_tick(struct vivi_dmaqueue  *dma_q)
456 {
457         struct vivi_buffer    *buf;
458         struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
459
460         int bc;
461
462         /* Announces videobuf that all went ok */
463         for (bc = 0;; bc++) {
464                 if (list_empty(&dma_q->active)) {
465                         dprintk(1,"No active queue to serve\n");
466                         break;
467                 }
468
469                 buf = list_entry(dma_q->active.next,
470                                  struct vivi_buffer, vb.queue);
471
472                 /* Nobody is waiting something to be done, just return */
473                 if (!waitqueue_active(&buf->vb.done)) {
474                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
475                         return;
476                 }
477
478                 do_gettimeofday(&buf->vb.ts);
479                 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
480
481                 /* Fill buffer */
482                 vivi_fillbuff(dev,buf);
483         }
484         if (list_empty(&dma_q->active)) {
485                 del_timer(&dma_q->timeout);
486         } else {
487                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
488         }
489         if (bc != 1)
490                 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
491 }
492
493 void vivi_sleep(struct vivi_dmaqueue  *dma_q)
494 {
495         int timeout;
496         DECLARE_WAITQUEUE(wait, current);
497
498         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
499
500         add_wait_queue(&dma_q->wq, &wait);
501         if (!kthread_should_stop()) {
502                 dma_q->frame++;
503
504                 /* Calculate time to wake up */
505                 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
506
507                 if (timeout <= 0) {
508                         int old=dma_q->frame;
509                         dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
510
511                         timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
512
513                         dprintk(1,"underrun, losed %d frames. "
514                                   "Now, frame is %d. Waking on %d jiffies\n",
515                                         dma_q->frame-old,dma_q->frame,timeout);
516                 } else
517                         dprintk(1,"will sleep for %i jiffies\n",timeout);
518
519                 vivi_thread_tick(dma_q);
520
521                 schedule_timeout_interruptible (timeout);
522         }
523
524         remove_wait_queue(&dma_q->wq, &wait);
525         try_to_freeze();
526 }
527
528 int vivi_thread(void *data)
529 {
530         struct vivi_dmaqueue  *dma_q=data;
531
532         dprintk(1,"thread started\n");
533
534         for (;;) {
535                 vivi_sleep(dma_q);
536
537                 if (kthread_should_stop())
538                         break;
539         }
540         dprintk(1, "thread: exit\n");
541         return 0;
542 }
543
544 int vivi_start_thread(struct vivi_dmaqueue  *dma_q)
545 {
546         dma_q->frame=0;
547         dma_q->ini_jiffies=jiffies;
548
549         dprintk(1,"%s\n",__FUNCTION__);
550         init_waitqueue_head(&dma_q->wq);
551
552         dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
553
554         if (dma_q->kthread == NULL) {
555                 printk(KERN_ERR "vivi: kernel_thread() failed\n");
556                 return -EINVAL;
557         }
558         dprintk(1,"returning from %s\n",__FUNCTION__);
559         return 0;
560 }
561
562 void vivi_stop_thread(struct vivi_dmaqueue  *dma_q)
563 {
564         dprintk(1,"%s\n",__FUNCTION__);
565         /* shutdown control thread */
566         if (dma_q->kthread) {
567                 kthread_stop(dma_q->kthread);
568                 dma_q->kthread=NULL;
569         }
570 }
571
572 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
573 {
574         struct vivi_buffer *buf, *prev;
575         struct list_head *item;
576
577         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
578
579         if (!list_empty(&dma_q->active)) {
580                 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
581                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
582                         buf, buf->vb.i);
583
584                 dprintk(1,"Restarting video dma\n");
585                 vivi_stop_thread(dma_q);
586 //              vivi_start_thread(dma_q);
587
588                 /* cancel all outstanding capture / vbi requests */
589                 list_for_each(item,&dma_q->active) {
590                         buf = list_entry(item, struct vivi_buffer, vb.queue);
591
592                         list_del(&buf->vb.queue);
593                         buf->vb.state = STATE_ERROR;
594                         wake_up(&buf->vb.done);
595                 }
596                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
597
598                 return 0;
599         }
600
601         prev = NULL;
602         for (;;) {
603                 if (list_empty(&dma_q->queued))
604                         return 0;
605                 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
606                 if (NULL == prev) {
607                         list_del(&buf->vb.queue);
608                         list_add_tail(&buf->vb.queue,&dma_q->active);
609
610                         dprintk(1,"Restarting video dma\n");
611                         vivi_stop_thread(dma_q);
612                         vivi_start_thread(dma_q);
613
614                         buf->vb.state = STATE_ACTIVE;
615                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
616                         dprintk(2,"[%p/%d] restart_queue - first active\n",
617                                 buf,buf->vb.i);
618
619                 } else if (prev->vb.width  == buf->vb.width  &&
620                            prev->vb.height == buf->vb.height &&
621                            prev->fmt       == buf->fmt) {
622                         list_del(&buf->vb.queue);
623                         list_add_tail(&buf->vb.queue,&dma_q->active);
624                         buf->vb.state = STATE_ACTIVE;
625                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
626                                 buf,buf->vb.i);
627                 } else {
628                         return 0;
629                 }
630                 prev = buf;
631         }
632 }
633
634 static void vivi_vid_timeout(unsigned long data)
635 {
636         struct vivi_dev      *dev  = (struct vivi_dev*)data;
637         struct vivi_dmaqueue *vidq = &dev->vidq;
638         struct vivi_buffer   *buf;
639
640         while (!list_empty(&vidq->active)) {
641                 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
642                 list_del(&buf->vb.queue);
643                 buf->vb.state = STATE_ERROR;
644                 wake_up(&buf->vb.done);
645                 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
646         }
647
648         restart_video_queue(vidq);
649 }
650
651 /* ------------------------------------------------------------------
652         Videobuf operations
653    ------------------------------------------------------------------*/
654 static int
655 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
656 {
657         struct vivi_fh *fh = vq->priv_data;
658
659         *size = fh->width*fh->height*2;
660
661         if (0 == *count)
662                 *count = 32;
663         while (*size * *count > vid_limit * 1024 * 1024)
664                 (*count)--;
665         return 0;
666 }
667
668 void
669 free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
670 {
671         dprintk(1,"%s\n",__FUNCTION__);
672
673         if (in_interrupt())
674                 BUG();
675
676         /*FIXME: Maybe a spinlock is required here */
677         kfree(buf->to_addr);
678         buf->to_addr=NULL;
679
680         videobuf_waiton(&buf->vb,0,0);
681         videobuf_dma_unmap(vq, &buf->vb.dma);
682         videobuf_dma_free(&buf->vb.dma);
683         buf->vb.state = STATE_NEEDS_INIT;
684 }
685
686 #define norm_maxw() 1024
687 #define norm_maxh() 768
688 static int
689 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
690                                                 enum v4l2_field field)
691 {
692         struct vivi_fh     *fh  = vq->priv_data;
693         struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
694         int rc, init_buffer = 0;
695
696 //      dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
697
698         BUG_ON(NULL == fh->fmt);
699         if (fh->width  < 48 || fh->width  > norm_maxw() ||
700             fh->height < 32 || fh->height > norm_maxh())
701                 return -EINVAL;
702         buf->vb.size = fh->width*fh->height*2;
703         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
704                 return -EINVAL;
705
706         if (buf->fmt       != fh->fmt    ||
707             buf->vb.width  != fh->width  ||
708             buf->vb.height != fh->height ||
709         buf->vb.field  != field) {
710                 buf->fmt       = fh->fmt;
711                 buf->vb.width  = fh->width;
712                 buf->vb.height = fh->height;
713                 buf->vb.field  = field;
714                 init_buffer = 1;
715         }
716
717         if (STATE_NEEDS_INIT == buf->vb.state) {
718                 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
719                         goto fail;
720         }
721
722         buf->vb.state = STATE_PREPARED;
723
724         if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
725                 rc=-ENOMEM;
726                 goto fail;
727         }
728
729         return 0;
730
731 fail:
732         free_buffer(vq,buf);
733         return rc;
734 }
735
736 static void
737 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
738 {
739         struct vivi_buffer    *buf     = container_of(vb,struct vivi_buffer,vb);
740         struct vivi_fh        *fh      = vq->priv_data;
741         struct vivi_dev       *dev     = fh->dev;
742         struct vivi_dmaqueue  *vidq    = &dev->vidq;
743         struct vivi_buffer    *prev;
744
745         if (!list_empty(&vidq->queued)) {
746                 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
747                 list_add_tail(&buf->vb.queue,&vidq->queued);
748                 buf->vb.state = STATE_QUEUED;
749                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
750                         buf, buf->vb.i);
751         } else if (list_empty(&vidq->active)) {
752                 list_add_tail(&buf->vb.queue,&vidq->active);
753
754                 buf->vb.state = STATE_ACTIVE;
755                 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
756                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
757                         buf, buf->vb.i);
758
759                 vivi_start_thread(vidq);
760         } else {
761                 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
762                 if (prev->vb.width  == buf->vb.width  &&
763                     prev->vb.height == buf->vb.height &&
764                     prev->fmt       == buf->fmt) {
765                         list_add_tail(&buf->vb.queue,&vidq->active);
766                         buf->vb.state = STATE_ACTIVE;
767                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
768                                 buf, buf->vb.i);
769
770                 } else {
771                         list_add_tail(&buf->vb.queue,&vidq->queued);
772                         buf->vb.state = STATE_QUEUED;
773                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
774                                 buf, buf->vb.i);
775                 }
776         }
777 }
778
779 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
780 {
781         struct vivi_buffer   *buf  = container_of(vb,struct vivi_buffer,vb);
782         struct vivi_fh       *fh   = vq->priv_data;
783         struct vivi_dev      *dev  = (struct vivi_dev*)fh->dev;
784         struct vivi_dmaqueue *vidq = &dev->vidq;
785
786         dprintk(1,"%s\n",__FUNCTION__);
787
788         vivi_stop_thread(vidq);
789
790         free_buffer(vq,buf);
791 }
792
793 int vivi_map_sg (void *dev, struct scatterlist *sg, int nents,
794            int direction)
795 {
796         int i;
797
798         dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
799         BUG_ON(direction == DMA_NONE);
800
801         for (i = 0; i < nents; i++ ) {
802                 BUG_ON(!sg[i].page);
803
804                 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
805         }
806
807         return nents;
808 }
809
810 int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
811                                         int direction)
812 {
813         dprintk(1,"%s\n",__FUNCTION__);
814         return 0;
815 }
816
817 int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist,int nr_pages,
818                                         int direction)
819 {
820 //      dprintk(1,"%s\n",__FUNCTION__);
821
822 //      flush_write_buffers();
823         return 0;
824 }
825
826 static struct videobuf_queue_ops vivi_video_qops = {
827         .buf_setup      = buffer_setup,
828         .buf_prepare    = buffer_prepare,
829         .buf_queue      = buffer_queue,
830         .buf_release    = buffer_release,
831
832         /* Non-pci handling routines */
833         .vb_map_sg      = vivi_map_sg,
834         .vb_dma_sync_sg = vivi_dma_sync_sg,
835         .vb_unmap_sg    = vivi_unmap_sg,
836 };
837
838 /* ------------------------------------------------------------------
839         IOCTL handling
840    ------------------------------------------------------------------*/
841
842 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
843                         struct v4l2_format *f)
844 {
845         struct vivi_fmt *fmt;
846         enum v4l2_field field;
847         unsigned int maxw, maxh;
848
849         if (format.fourcc != f->fmt.pix.pixelformat) {
850                 dprintk(1,"Fourcc format invalid.\n");
851                 return -EINVAL;
852         }
853         fmt=&format;
854
855         field = f->fmt.pix.field;
856
857         if (field == V4L2_FIELD_ANY) {
858 //              field=V4L2_FIELD_INTERLACED;
859                 field=V4L2_FIELD_SEQ_TB;
860         } else if (V4L2_FIELD_INTERLACED != field) {
861                 dprintk(1,"Field type invalid.\n");
862                 return -EINVAL;
863         }
864
865         maxw  = norm_maxw();
866         maxh  = norm_maxh();
867
868         f->fmt.pix.field = field;
869         if (f->fmt.pix.height < 32)
870                 f->fmt.pix.height = 32;
871         if (f->fmt.pix.height > maxh)
872                 f->fmt.pix.height = maxh;
873         if (f->fmt.pix.width < 48)
874                 f->fmt.pix.width = 48;
875         if (f->fmt.pix.width > maxw)
876                 f->fmt.pix.width = maxw;
877         f->fmt.pix.width &= ~0x03;
878         f->fmt.pix.bytesperline =
879                 (f->fmt.pix.width * fmt->depth) >> 3;
880         f->fmt.pix.sizeimage =
881                 f->fmt.pix.height * f->fmt.pix.bytesperline;
882
883         return 0;
884 }
885
886 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
887 {
888         /* is it free? */
889         down(&dev->lock);
890         if (dev->resources) {
891                 /* no, someone else uses it */
892                 up(&dev->lock);
893                 return 0;
894         }
895         /* it's free, grab it */
896         dev->resources =1;
897         dprintk(1,"res: get\n");
898         up(&dev->lock);
899         return 1;
900 }
901
902 static inline int res_locked(struct vivi_dev *dev)
903 {
904         return (dev->resources);
905 }
906
907 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
908 {
909         down(&dev->lock);
910         dev->resources = 0;
911         dprintk(1,"res: put\n");
912         up(&dev->lock);
913 }
914
915 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
916 {
917         struct vivi_fh  *fh     = file->private_data;
918         struct vivi_dev *dev    = fh->dev;
919         int ret=0;
920
921         if (debug) {
922                 if (_IOC_DIR(cmd) & _IOC_WRITE)
923                         v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
924                 else if (!_IOC_DIR(cmd) & _IOC_READ) {
925                         v4l_print_ioctl("vivi", cmd);
926                 }
927         }
928
929         switch(cmd) {
930         /* --- capabilities ------------------------------------------ */
931         case VIDIOC_QUERYCAP:
932         {
933                 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
934
935                 memset(cap, 0, sizeof(*cap));
936
937                 strcpy(cap->driver, "vivi");
938                 strcpy(cap->card, "vivi");
939                 cap->version = VIVI_VERSION;
940                 cap->capabilities =
941                                         V4L2_CAP_VIDEO_CAPTURE |
942                                         V4L2_CAP_STREAMING     |
943                                         V4L2_CAP_READWRITE;
944                 break;
945         }
946         /* --- capture ioctls ---------------------------------------- */
947         case VIDIOC_ENUM_FMT:
948         {
949                 struct v4l2_fmtdesc *f = arg;
950                 enum v4l2_buf_type type;
951                 unsigned int index;
952
953                 index = f->index;
954                 type  = f->type;
955
956                 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
957                         ret=-EINVAL;
958                         break;
959                 }
960
961                 switch (type) {
962                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
963                         if (index > 0){
964                                 ret=-EINVAL;
965                                 break;
966                         }
967                         memset(f,0,sizeof(*f));
968
969                         f->index = index;
970                         f->type  = type;
971                         strlcpy(f->description,format.name,sizeof(f->description));
972                         f->pixelformat = format.fourcc;
973                         break;
974                 default:
975                         ret=-EINVAL;
976                 }
977                 break;
978         }
979         case VIDIOC_G_FMT:
980         {
981                 struct v4l2_format *f = (struct v4l2_format *)arg;
982
983                 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
984                         ret=-EINVAL;
985                         break;
986                 }
987
988                 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
989                 f->fmt.pix.width        = fh->width;
990                 f->fmt.pix.height       = fh->height;
991                 f->fmt.pix.field        = fh->vb_vidq.field;
992                 f->fmt.pix.pixelformat  = fh->fmt->fourcc;
993                 f->fmt.pix.bytesperline =
994                         (f->fmt.pix.width * fh->fmt->depth) >> 3;
995                 f->fmt.pix.sizeimage =
996                         f->fmt.pix.height * f->fmt.pix.bytesperline;
997                 break;
998         }
999         case VIDIOC_S_FMT:
1000         {
1001                 struct v4l2_format *f = arg;
1002
1003                 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1004                         dprintk(1,"Only capture supported.\n");
1005                         ret=-EINVAL;
1006                         break;
1007                 }
1008
1009                 ret = vivi_try_fmt(dev,fh,f);
1010                 if (ret < 0)
1011                         break;
1012
1013                 fh->fmt           = &format;
1014                 fh->width         = f->fmt.pix.width;
1015                 fh->height        = f->fmt.pix.height;
1016                 fh->vb_vidq.field = f->fmt.pix.field;
1017                 fh->type          = f->type;
1018
1019                 break;
1020         }
1021         case VIDIOC_TRY_FMT:
1022         {
1023                 struct v4l2_format *f = arg;
1024                 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1025                         ret=-EINVAL;
1026                         break;
1027                 }
1028
1029                 ret=vivi_try_fmt(dev,fh,f);
1030                 break;
1031         }
1032         case VIDIOC_REQBUFS:
1033                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1034                         ret=-EINVAL;
1035                         break;
1036                 }
1037                 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1038                 break;
1039         case VIDIOC_QUERYBUF:
1040                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1041                         ret=-EINVAL;
1042                         break;
1043                 }
1044                 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1045                 break;
1046         case VIDIOC_QBUF:
1047                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1048                         ret=-EINVAL;
1049                         break;
1050                 }
1051                 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1052                 break;
1053         case VIDIOC_DQBUF:
1054                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1055                         ret=-EINVAL;
1056                         break;
1057                 }
1058                 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1059                                         file->f_flags & O_NONBLOCK);
1060                 break;
1061 #ifdef HAVE_V4L1
1062         /* --- streaming capture ------------------------------------- */
1063         case VIDIOCGMBUF:
1064         {
1065                 struct video_mbuf *mbuf = arg;
1066                 struct videobuf_queue *q=&fh->vb_vidq;
1067                 struct v4l2_requestbuffers req;
1068                 unsigned int i;
1069
1070                 memset(&req,0,sizeof(req));
1071                 req.type   = q->type;
1072                 req.count  = 8;
1073                 req.memory = V4L2_MEMORY_MMAP;
1074                 ret = videobuf_reqbufs(q,&req);
1075                 if (ret < 0)
1076                         break;
1077                 memset(mbuf,0,sizeof(*mbuf));
1078                 mbuf->frames = req.count;
1079                 mbuf->size   = 0;
1080                 for (i = 0; i < mbuf->frames; i++) {
1081                         mbuf->offsets[i]  = q->bufs[i]->boff;
1082                         mbuf->size       += q->bufs[i]->bsize;
1083                 }
1084                 break;
1085         }
1086 #endif
1087         case VIDIOC_STREAMON:
1088         {
1089                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1090                         return -EINVAL;
1091                 if (!res_get(dev,fh))
1092                         return -EBUSY;
1093                 ret=videobuf_streamon(&fh->vb_vidq);
1094                 break;
1095         }
1096         case VIDIOC_STREAMOFF:
1097         {
1098                 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1099                         ret=-EINVAL;
1100                         break;
1101                 }
1102                 ret = videobuf_streamoff(&fh->vb_vidq);
1103                 if (ret < 0)
1104                         break;
1105                 res_free(dev,fh);
1106                 break;
1107         }
1108         /* ---------- tv norms ---------- */
1109         case VIDIOC_ENUMSTD:
1110         {
1111                 struct v4l2_standard *e = arg;
1112
1113                 if (e->index>0) {
1114                         ret=-EINVAL;
1115                         break;
1116                 }
1117                 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1118
1119                 /* Allows vivi to use different fps from video std */
1120                 e->frameperiod.numerator = WAKE_NUMERATOR;
1121                 e->frameperiod.denominator = WAKE_DENOMINATOR;
1122
1123                 break;
1124         }
1125         case VIDIOC_G_STD:
1126         {
1127                 v4l2_std_id *id = arg;
1128
1129                 *id = V4L2_STD_NTSC_M;
1130                 break;
1131         }
1132         case VIDIOC_S_STD:
1133         {
1134                 break;
1135         }
1136         /* ------ input switching ---------- */
1137         case VIDIOC_ENUMINPUT:
1138         { /* only one input in this sample driver */
1139                 struct v4l2_input *inp = arg;
1140
1141                 if (inp->index != 0) {
1142                         ret=-EINVAL;
1143                         break;
1144                 }
1145                 memset(inp, 0, sizeof(*inp));
1146
1147                 inp->index = 0;
1148                 inp->type = V4L2_INPUT_TYPE_CAMERA;
1149                 inp->std = V4L2_STD_NTSC_M;
1150                 strcpy(inp->name,"Camera");
1151                 break;
1152         }
1153         case VIDIOC_G_INPUT:
1154         {
1155                 unsigned int *i = arg;
1156
1157                 *i = 0;
1158                 break;
1159         }
1160         case VIDIOC_S_INPUT:
1161         {
1162                 unsigned int *i = arg;
1163
1164                 if (*i > 0)
1165                         ret=-EINVAL;
1166                 break;
1167         }
1168
1169         /* --- controls ---------------------------------------------- */
1170         case VIDIOC_QUERYCTRL:
1171         {
1172                 struct v4l2_queryctrl *qc = arg;
1173                 int i;
1174
1175                 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1176                         if (qc->id && qc->id == vivi_qctrl[i].id) {
1177                                 memcpy(qc, &(vivi_qctrl[i]),
1178                                         sizeof(*qc));
1179                                 break;
1180                         }
1181
1182                 ret=-EINVAL;
1183                 break;
1184         }
1185         case VIDIOC_G_CTRL:
1186         {
1187                 struct v4l2_control *ctrl = arg;
1188                 int i;
1189
1190                 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1191                         if (ctrl->id == vivi_qctrl[i].id) {
1192                                 ctrl->value=qctl_regs[i];
1193                                 break;
1194                         }
1195
1196                 ret=-EINVAL;
1197                 break;
1198         }
1199         case VIDIOC_S_CTRL:
1200         {
1201                 struct v4l2_control *ctrl = arg;
1202                 int i;
1203                 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1204                         if (ctrl->id == vivi_qctrl[i].id) {
1205                                 if (ctrl->value <
1206                                         vivi_qctrl[i].minimum
1207                                         || ctrl->value >
1208                                         vivi_qctrl[i].maximum) {
1209                                                 ret=-ERANGE;
1210                                                 break;
1211                                         }
1212                                 qctl_regs[i]=ctrl->value;
1213                                 break;
1214                         }
1215                 ret=-EINVAL;
1216                 break;
1217         }
1218         default:
1219                 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1220         }
1221
1222         if (debug) {
1223                 if (ret<0) {
1224                         v4l_print_ioctl("vivi(err)", cmd);
1225                         dprintk(1,"errcode=%d\n",ret);
1226                 } else if (_IOC_DIR(cmd) & _IOC_READ)
1227                         v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1228         }
1229
1230         return ret;
1231 }
1232
1233 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1234 {
1235         return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1236 }
1237
1238 /* ------------------------------------------------------------------
1239         File operations for the device
1240    ------------------------------------------------------------------*/
1241
1242 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1243
1244 static int vivi_open(struct inode *inode, struct file *file)
1245 {
1246         int minor = iminor(inode);
1247         struct vivi_dev *h,*dev = NULL;
1248         struct vivi_fh *fh;
1249         struct list_head *list;
1250         enum v4l2_buf_type type = 0;
1251         int i;
1252
1253         printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1254
1255         list_for_each(list,&vivi_devlist) {
1256                 h = list_entry(list, struct vivi_dev, vivi_devlist);
1257                 if (h->video_dev.minor == minor) {
1258                         dev  = h;
1259                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1260                 }
1261         }
1262         if (NULL == dev)
1263                 return -ENODEV;
1264
1265
1266         /* If more than one user, mutex should be added */
1267         dev->users++;
1268
1269         dprintk(1,"open minor=%d type=%s users=%d\n",
1270                                 minor,v4l2_type_names[type],dev->users);
1271
1272         /* allocate + initialize per filehandle data */
1273         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1274         if (NULL == fh) {
1275                 dev->users--;
1276                 return -ENOMEM;
1277         }
1278
1279         file->private_data = fh;
1280         fh->dev      = dev;
1281         fh->type     = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1282         fh->fmt      = &format;
1283         fh->width    = 640;
1284         fh->height   = 480;
1285
1286         /* Put all controls at a sane state */
1287         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1288                 qctl_regs[i] =vivi_qctrl[i].default_value;
1289
1290         dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1291                 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1292         dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1293         dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1294
1295         /* Resets frame counters */
1296         dev->h=0;
1297         dev->m=0;
1298         dev->s=0;
1299         dev->us=0;
1300         dev->jiffies=jiffies;
1301         sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1302                         dev->h,dev->m,dev->s,(dev->us+500)/1000);
1303
1304         videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1305                         NULL, NULL,
1306                         fh->type,
1307                         V4L2_FIELD_INTERLACED,
1308                         sizeof(struct vivi_buffer),fh);
1309
1310         return 0;
1311 }
1312
1313 static ssize_t
1314 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1315 {
1316         struct vivi_fh *fh = file->private_data;
1317
1318         if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1319                 if (res_locked(fh->dev))
1320                         return -EBUSY;
1321                 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1322                                         file->f_flags & O_NONBLOCK);
1323         }
1324         return 0;
1325 }
1326
1327 static unsigned int
1328 vivi_poll(struct file *file, struct poll_table_struct *wait)
1329 {
1330         struct vivi_fh *fh = file->private_data;
1331         struct vivi_buffer *buf;
1332
1333         dprintk(1,"%s\n",__FUNCTION__);
1334
1335         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1336                 return POLLERR;
1337
1338         if (res_get(fh->dev,fh)) {
1339                 dprintk(1,"poll: mmap interface\n");
1340                 /* streaming capture */
1341                 if (list_empty(&fh->vb_vidq.stream))
1342                         return POLLERR;
1343                 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1344         } else {
1345                 dprintk(1,"poll: read() interface\n");
1346                 /* read() capture */
1347                 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1348                 if (NULL == buf)
1349                         return POLLERR;
1350         }
1351         poll_wait(file, &buf->vb.done, wait);
1352         if (buf->vb.state == STATE_DONE ||
1353             buf->vb.state == STATE_ERROR)
1354                 return POLLIN|POLLRDNORM;
1355         return 0;
1356 }
1357
1358 static int vivi_release(struct inode *inode, struct file *file)
1359 {
1360         struct vivi_fh  *fh     = file->private_data;
1361         struct vivi_dev *dev    = fh->dev;
1362         struct vivi_dmaqueue *vidq = &dev->vidq;
1363
1364         int minor = iminor(inode);
1365
1366         vivi_stop_thread(vidq);
1367         videobuf_mmap_free(&fh->vb_vidq);
1368
1369         kfree (fh);
1370
1371         dev->users--;
1372
1373         printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1374
1375         return 0;
1376 }
1377
1378 static int
1379 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1380 {
1381         struct vivi_fh *fh = file->private_data;
1382         int ret;
1383
1384         dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1385
1386         ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1387
1388         dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1389                 (unsigned long)vma->vm_start,
1390                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1391                 ret);
1392
1393         return ret;
1394 }
1395
1396 static struct file_operations vivi_fops = {
1397         .owner          = THIS_MODULE,
1398         .open           = vivi_open,
1399         .release        = vivi_release,
1400         .read           = vivi_read,
1401         .poll           = vivi_poll,
1402         .ioctl          = vivi_ioctl,
1403         .mmap           = vivi_mmap,
1404         .llseek         = no_llseek,
1405 };
1406
1407 static struct video_device vivi = {
1408         .name           = "VTM Virtual Video Capture Board",
1409         .type           = VID_TYPE_CAPTURE,
1410         .hardware       = 0,
1411         .fops           = &vivi_fops,
1412         .minor          = -1,
1413 //      .release        = video_device_release,
1414 };
1415 /* ------------------------------------------------------------------
1416         Initialization and module stuff
1417    ------------------------------------------------------------------*/
1418
1419 static int __init vivi_init(void)
1420 {
1421         int ret;
1422         struct vivi_dev *dev;
1423
1424         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1425         if (NULL == dev)
1426                 return -ENOMEM;
1427         list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1428
1429         /* init video dma queues */
1430         INIT_LIST_HEAD(&dev->vidq.active);
1431         INIT_LIST_HEAD(&dev->vidq.queued);
1432
1433         /* initialize locks */
1434         init_MUTEX(&dev->lock);
1435
1436         dev->vidq.timeout.function = vivi_vid_timeout;
1437         dev->vidq.timeout.data     = (unsigned long)dev;
1438         init_timer(&dev->vidq.timeout);
1439
1440         ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1441         printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1442         return ret;
1443 }
1444
1445 static void __exit vivi_exit(void)
1446 {
1447         struct vivi_dev *h;
1448         struct list_head *list;
1449
1450         list_for_each(list,&vivi_devlist) {
1451                 h = list_entry(list, struct vivi_dev, vivi_devlist);
1452                 kfree (h);
1453         }
1454         video_unregister_device(&vivi);
1455 }
1456
1457 module_init(vivi_init);
1458 module_exit(vivi_exit);