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