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