vivi.c 33 KB

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