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