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