vivi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. unsigned long flags;
  234. spinlock_t spinlock;
  235. spin_lock_init(&spinlock);
  236. /* Get first addr pointed to pixel position */
  237. oldpg=get_addr_pos(pos,pages,to_addr);
  238. pg=pfn_to_page(sg_dma_address(to_addr[oldpg].sg) >> PAGE_SHIFT);
  239. spin_lock_irqsave(&spinlock,flags);
  240. basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
  241. /* We will just duplicate the second pixel at the packet */
  242. wmax/=2;
  243. /* Generate a standard color bar pattern */
  244. for (w=0;w<wmax;w++) {
  245. r=bars[w*7/wmax][0];
  246. g=bars[w*7/wmax][1];
  247. b=bars[w*7/wmax][2];
  248. for (color=0;color<4;color++) {
  249. pgpos=get_addr_pos(pos,pages,to_addr);
  250. if (pgpos!=oldpg) {
  251. pg=pfn_to_page(sg_dma_address(to_addr[pgpos].sg) >> PAGE_SHIFT);
  252. kunmap_atomic(basep, KM_BOUNCE_READ);
  253. basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
  254. oldpg=pgpos;
  255. }
  256. p=basep+pos-to_addr[pgpos].pos;
  257. switch (color) {
  258. case 0:
  259. case 2:
  260. *p=TO_Y(r,g,b); /* Luminance */
  261. break;
  262. case 1:
  263. *p=TO_U(r,g,b); /* Cb */
  264. break;
  265. case 3:
  266. *p=TO_V(r,g,b); /* Cr */
  267. break;
  268. }
  269. pos++;
  270. }
  271. }
  272. /* Checks if it is possible to show timestamp */
  273. if (TSTAMP_MAX_Y>=hmax)
  274. goto end;
  275. if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
  276. goto end;
  277. /* Print stream time */
  278. if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
  279. j=TSTAMP_MIN_X;
  280. for (s=timestr;*s;s++) {
  281. chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
  282. for (i=0;i<7;i++) {
  283. if (chr&1<<(7-i)) { /* Font color*/
  284. r=bars[BLUE][0];
  285. g=bars[BLUE][1];
  286. b=bars[BLUE][2];
  287. r=g=b=0;
  288. g=198;
  289. } else { /* Background color */
  290. r=bars[WHITE][0];
  291. g=bars[WHITE][1];
  292. b=bars[WHITE][2];
  293. r=g=b=0;
  294. }
  295. pos=inipos+j*2;
  296. for (color=0;color<4;color++) {
  297. pgpos=get_addr_pos(pos,pages,to_addr);
  298. if (pgpos!=oldpg) {
  299. pg=pfn_to_page(sg_dma_address(
  300. to_addr[pgpos].sg)
  301. >> PAGE_SHIFT);
  302. kunmap_atomic(basep,
  303. KM_BOUNCE_READ);
  304. basep= kmap_atomic(pg,
  305. KM_BOUNCE_READ)+
  306. to_addr[pgpos].sg->offset;
  307. oldpg=pgpos;
  308. }
  309. p=basep+pos-to_addr[pgpos].pos;
  310. y=TO_Y(r,g,b);
  311. switch (color) {
  312. case 0:
  313. case 2:
  314. *p=TO_Y(r,g,b); /* Luminance */
  315. break;
  316. case 1:
  317. *p=TO_U(r,g,b); /* Cb */
  318. break;
  319. case 3:
  320. *p=TO_V(r,g,b); /* Cr */
  321. break;
  322. }
  323. pos++;
  324. }
  325. j++;
  326. }
  327. }
  328. }
  329. end:
  330. kunmap_atomic(basep, KM_BOUNCE_READ);
  331. spin_unlock_irqrestore(&spinlock,flags);
  332. }
  333. static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
  334. {
  335. int h,pos=0;
  336. int hmax = buf->vb.height;
  337. int wmax = buf->vb.width;
  338. struct videobuf_buffer *vb=&buf->vb;
  339. struct sg_to_addr *to_addr=buf->to_addr;
  340. struct timeval ts;
  341. /* Test if DMA mapping is ready */
  342. if (!sg_dma_address(&vb->dma.sglist[0]))
  343. return;
  344. prep_to_addr(to_addr,vb);
  345. /* Check if there is enough memory */
  346. BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
  347. for (h=0;h<hmax;h++) {
  348. gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
  349. pos += wmax*2;
  350. }
  351. /* Updates stream time */
  352. dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
  353. dev->jiffies=jiffies;
  354. if (dev->us>=1000000) {
  355. dev->us-=1000000;
  356. dev->s++;
  357. if (dev->s>=60) {
  358. dev->s-=60;
  359. dev->m++;
  360. if (dev->m>60) {
  361. dev->m-=60;
  362. dev->h++;
  363. if (dev->h>24)
  364. dev->h-=24;
  365. }
  366. }
  367. }
  368. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  369. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  370. dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
  371. (unsigned long)buf->vb.dma.vmalloc,pos);
  372. /* Advice that buffer was filled */
  373. buf->vb.state = STATE_DONE;
  374. buf->vb.field_count++;
  375. do_gettimeofday(&ts);
  376. buf->vb.ts = ts;
  377. list_del(&buf->vb.queue);
  378. wake_up(&buf->vb.done);
  379. }
  380. static int restart_video_queue(struct vivi_dmaqueue *dma_q);
  381. static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
  382. {
  383. struct vivi_buffer *buf;
  384. struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
  385. int bc;
  386. /* Announces videobuf that all went ok */
  387. for (bc = 0;; bc++) {
  388. if (list_empty(&dma_q->active)) {
  389. dprintk(1,"No active queue to serve\n");
  390. break;
  391. }
  392. buf = list_entry(dma_q->active.next,
  393. struct vivi_buffer, vb.queue);
  394. /* Nobody is waiting something to be done, just return */
  395. if (!waitqueue_active(&buf->vb.done)) {
  396. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  397. return;
  398. }
  399. do_gettimeofday(&buf->vb.ts);
  400. dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
  401. /* Fill buffer */
  402. vivi_fillbuff(dev,buf);
  403. }
  404. if (list_empty(&dma_q->active)) {
  405. del_timer(&dma_q->timeout);
  406. } else {
  407. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  408. }
  409. if (bc != 1)
  410. dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
  411. }
  412. static void vivi_sleep(struct vivi_dmaqueue *dma_q)
  413. {
  414. int timeout;
  415. DECLARE_WAITQUEUE(wait, current);
  416. dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
  417. add_wait_queue(&dma_q->wq, &wait);
  418. if (!kthread_should_stop()) {
  419. dma_q->frame++;
  420. /* Calculate time to wake up */
  421. timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
  422. if (timeout <= 0) {
  423. int old=dma_q->frame;
  424. dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
  425. timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
  426. dprintk(1,"underrun, losed %d frames. "
  427. "Now, frame is %d. Waking on %d jiffies\n",
  428. dma_q->frame-old,dma_q->frame,timeout);
  429. } else
  430. dprintk(1,"will sleep for %i jiffies\n",timeout);
  431. vivi_thread_tick(dma_q);
  432. schedule_timeout_interruptible (timeout);
  433. }
  434. remove_wait_queue(&dma_q->wq, &wait);
  435. try_to_freeze();
  436. }
  437. static int vivi_thread(void *data)
  438. {
  439. struct vivi_dmaqueue *dma_q=data;
  440. dprintk(1,"thread started\n");
  441. for (;;) {
  442. vivi_sleep(dma_q);
  443. if (kthread_should_stop())
  444. break;
  445. }
  446. dprintk(1, "thread: exit\n");
  447. return 0;
  448. }
  449. static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
  450. {
  451. dma_q->frame=0;
  452. dma_q->ini_jiffies=jiffies;
  453. dprintk(1,"%s\n",__FUNCTION__);
  454. init_waitqueue_head(&dma_q->wq);
  455. dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
  456. if (IS_ERR(dma_q->kthread)) {
  457. printk(KERN_ERR "vivi: kernel_thread() failed\n");
  458. return PTR_ERR(dma_q->kthread);
  459. }
  460. dprintk(1,"returning from %s\n",__FUNCTION__);
  461. return 0;
  462. }
  463. static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
  464. {
  465. dprintk(1,"%s\n",__FUNCTION__);
  466. /* shutdown control thread */
  467. if (dma_q->kthread) {
  468. kthread_stop(dma_q->kthread);
  469. dma_q->kthread=NULL;
  470. }
  471. }
  472. static int restart_video_queue(struct vivi_dmaqueue *dma_q)
  473. {
  474. struct vivi_buffer *buf, *prev;
  475. struct list_head *item;
  476. dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
  477. if (!list_empty(&dma_q->active)) {
  478. buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
  479. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  480. buf, buf->vb.i);
  481. dprintk(1,"Restarting video dma\n");
  482. vivi_stop_thread(dma_q);
  483. // vivi_start_thread(dma_q);
  484. /* cancel all outstanding capture / vbi requests */
  485. list_for_each(item,&dma_q->active) {
  486. buf = list_entry(item, struct vivi_buffer, vb.queue);
  487. list_del(&buf->vb.queue);
  488. buf->vb.state = STATE_ERROR;
  489. wake_up(&buf->vb.done);
  490. }
  491. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  492. return 0;
  493. }
  494. prev = NULL;
  495. for (;;) {
  496. if (list_empty(&dma_q->queued))
  497. return 0;
  498. buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
  499. if (NULL == prev) {
  500. list_del(&buf->vb.queue);
  501. list_add_tail(&buf->vb.queue,&dma_q->active);
  502. dprintk(1,"Restarting video dma\n");
  503. vivi_stop_thread(dma_q);
  504. vivi_start_thread(dma_q);
  505. buf->vb.state = STATE_ACTIVE;
  506. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  507. dprintk(2,"[%p/%d] restart_queue - first active\n",
  508. buf,buf->vb.i);
  509. } else if (prev->vb.width == buf->vb.width &&
  510. prev->vb.height == buf->vb.height &&
  511. prev->fmt == buf->fmt) {
  512. list_del(&buf->vb.queue);
  513. list_add_tail(&buf->vb.queue,&dma_q->active);
  514. buf->vb.state = STATE_ACTIVE;
  515. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  516. buf,buf->vb.i);
  517. } else {
  518. return 0;
  519. }
  520. prev = buf;
  521. }
  522. }
  523. static void vivi_vid_timeout(unsigned long data)
  524. {
  525. struct vivi_dev *dev = (struct vivi_dev*)data;
  526. struct vivi_dmaqueue *vidq = &dev->vidq;
  527. struct vivi_buffer *buf;
  528. while (!list_empty(&vidq->active)) {
  529. buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
  530. list_del(&buf->vb.queue);
  531. buf->vb.state = STATE_ERROR;
  532. wake_up(&buf->vb.done);
  533. printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
  534. }
  535. restart_video_queue(vidq);
  536. }
  537. /* ------------------------------------------------------------------
  538. Videobuf operations
  539. ------------------------------------------------------------------*/
  540. static int
  541. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  542. {
  543. struct vivi_fh *fh = vq->priv_data;
  544. *size = fh->width*fh->height*2;
  545. if (0 == *count)
  546. *count = 32;
  547. while (*size * *count > vid_limit * 1024 * 1024)
  548. (*count)--;
  549. return 0;
  550. }
  551. static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
  552. {
  553. dprintk(1,"%s\n",__FUNCTION__);
  554. if (in_interrupt())
  555. BUG();
  556. /*FIXME: Maybe a spinlock is required here */
  557. kfree(buf->to_addr);
  558. buf->to_addr=NULL;
  559. videobuf_waiton(&buf->vb,0,0);
  560. videobuf_dma_unmap(vq, &buf->vb.dma);
  561. videobuf_dma_free(&buf->vb.dma);
  562. buf->vb.state = STATE_NEEDS_INIT;
  563. }
  564. #define norm_maxw() 1024
  565. #define norm_maxh() 768
  566. static int
  567. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  568. enum v4l2_field field)
  569. {
  570. struct vivi_fh *fh = vq->priv_data;
  571. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  572. int rc, init_buffer = 0;
  573. // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
  574. BUG_ON(NULL == fh->fmt);
  575. if (fh->width < 48 || fh->width > norm_maxw() ||
  576. fh->height < 32 || fh->height > norm_maxh())
  577. return -EINVAL;
  578. buf->vb.size = fh->width*fh->height*2;
  579. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  580. return -EINVAL;
  581. if (buf->fmt != fh->fmt ||
  582. buf->vb.width != fh->width ||
  583. buf->vb.height != fh->height ||
  584. buf->vb.field != field) {
  585. buf->fmt = fh->fmt;
  586. buf->vb.width = fh->width;
  587. buf->vb.height = fh->height;
  588. buf->vb.field = field;
  589. init_buffer = 1;
  590. }
  591. if (STATE_NEEDS_INIT == buf->vb.state) {
  592. if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
  593. goto fail;
  594. }
  595. buf->vb.state = STATE_PREPARED;
  596. if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
  597. rc=-ENOMEM;
  598. goto fail;
  599. }
  600. return 0;
  601. fail:
  602. free_buffer(vq,buf);
  603. return rc;
  604. }
  605. static void
  606. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  607. {
  608. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  609. struct vivi_fh *fh = vq->priv_data;
  610. struct vivi_dev *dev = fh->dev;
  611. struct vivi_dmaqueue *vidq = &dev->vidq;
  612. struct vivi_buffer *prev;
  613. if (!list_empty(&vidq->queued)) {
  614. dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
  615. list_add_tail(&buf->vb.queue,&vidq->queued);
  616. buf->vb.state = STATE_QUEUED;
  617. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  618. buf, buf->vb.i);
  619. } else if (list_empty(&vidq->active)) {
  620. list_add_tail(&buf->vb.queue,&vidq->active);
  621. buf->vb.state = STATE_ACTIVE;
  622. mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
  623. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  624. buf, buf->vb.i);
  625. vivi_start_thread(vidq);
  626. } else {
  627. prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
  628. if (prev->vb.width == buf->vb.width &&
  629. prev->vb.height == buf->vb.height &&
  630. prev->fmt == buf->fmt) {
  631. list_add_tail(&buf->vb.queue,&vidq->active);
  632. buf->vb.state = STATE_ACTIVE;
  633. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  634. buf, buf->vb.i);
  635. } else {
  636. list_add_tail(&buf->vb.queue,&vidq->queued);
  637. buf->vb.state = STATE_QUEUED;
  638. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  639. buf, buf->vb.i);
  640. }
  641. }
  642. }
  643. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  644. {
  645. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  646. struct vivi_fh *fh = vq->priv_data;
  647. struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
  648. struct vivi_dmaqueue *vidq = &dev->vidq;
  649. dprintk(1,"%s\n",__FUNCTION__);
  650. vivi_stop_thread(vidq);
  651. free_buffer(vq,buf);
  652. }
  653. static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
  654. int direction)
  655. {
  656. int i;
  657. dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
  658. BUG_ON(direction == DMA_NONE);
  659. for (i = 0; i < nents; i++ ) {
  660. BUG_ON(!sg[i].page);
  661. sg_dma_address(&sg[i]) = page_to_phys(sg[i].page) + sg[i].offset;
  662. }
  663. return nents;
  664. }
  665. static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
  666. int direction)
  667. {
  668. dprintk(1,"%s\n",__FUNCTION__);
  669. return 0;
  670. }
  671. static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
  672. int direction)
  673. {
  674. // dprintk(1,"%s\n",__FUNCTION__);
  675. // flush_write_buffers();
  676. return 0;
  677. }
  678. static struct videobuf_queue_ops vivi_video_qops = {
  679. .buf_setup = buffer_setup,
  680. .buf_prepare = buffer_prepare,
  681. .buf_queue = buffer_queue,
  682. .buf_release = buffer_release,
  683. /* Non-pci handling routines */
  684. .vb_map_sg = vivi_map_sg,
  685. .vb_dma_sync_sg = vivi_dma_sync_sg,
  686. .vb_unmap_sg = vivi_unmap_sg,
  687. };
  688. /* ------------------------------------------------------------------
  689. IOCTL handling
  690. ------------------------------------------------------------------*/
  691. static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
  692. {
  693. /* is it free? */
  694. down(&dev->lock);
  695. if (dev->resources) {
  696. /* no, someone else uses it */
  697. up(&dev->lock);
  698. return 0;
  699. }
  700. /* it's free, grab it */
  701. dev->resources =1;
  702. dprintk(1,"res: get\n");
  703. up(&dev->lock);
  704. return 1;
  705. }
  706. static int res_locked(struct vivi_dev *dev)
  707. {
  708. return (dev->resources);
  709. }
  710. static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
  711. {
  712. down(&dev->lock);
  713. dev->resources = 0;
  714. dprintk(1,"res: put\n");
  715. up(&dev->lock);
  716. }
  717. /* ------------------------------------------------------------------
  718. IOCTL vidioc handling
  719. ------------------------------------------------------------------*/
  720. static int vidioc_querycap (struct file *file, void *priv,
  721. struct v4l2_capability *cap)
  722. {
  723. strcpy(cap->driver, "vivi");
  724. strcpy(cap->card, "vivi");
  725. cap->version = VIVI_VERSION;
  726. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  727. V4L2_CAP_STREAMING |
  728. V4L2_CAP_READWRITE;
  729. return 0;
  730. }
  731. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  732. struct v4l2_fmtdesc *f)
  733. {
  734. if (f->index > 0)
  735. return -EINVAL;
  736. strlcpy(f->description,format.name,sizeof(f->description));
  737. f->pixelformat = format.fourcc;
  738. return 0;
  739. }
  740. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  741. struct v4l2_format *f)
  742. {
  743. struct vivi_fh *fh=priv;
  744. f->fmt.pix.width = fh->width;
  745. f->fmt.pix.height = fh->height;
  746. f->fmt.pix.field = fh->vb_vidq.field;
  747. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  748. f->fmt.pix.bytesperline =
  749. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  750. f->fmt.pix.sizeimage =
  751. f->fmt.pix.height * f->fmt.pix.bytesperline;
  752. return (0);
  753. }
  754. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  755. struct v4l2_format *f)
  756. {
  757. struct vivi_fmt *fmt;
  758. enum v4l2_field field;
  759. unsigned int maxw, maxh;
  760. if (format.fourcc != f->fmt.pix.pixelformat) {
  761. dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
  762. "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
  763. return -EINVAL;
  764. }
  765. fmt=&format;
  766. field = f->fmt.pix.field;
  767. if (field == V4L2_FIELD_ANY) {
  768. // field=V4L2_FIELD_INTERLACED;
  769. field=V4L2_FIELD_SEQ_TB;
  770. } else if (V4L2_FIELD_INTERLACED != field) {
  771. dprintk(1,"Field type invalid.\n");
  772. return -EINVAL;
  773. }
  774. maxw = norm_maxw();
  775. maxh = norm_maxh();
  776. f->fmt.pix.field = field;
  777. if (f->fmt.pix.height < 32)
  778. f->fmt.pix.height = 32;
  779. if (f->fmt.pix.height > maxh)
  780. f->fmt.pix.height = maxh;
  781. if (f->fmt.pix.width < 48)
  782. f->fmt.pix.width = 48;
  783. if (f->fmt.pix.width > maxw)
  784. f->fmt.pix.width = maxw;
  785. f->fmt.pix.width &= ~0x03;
  786. f->fmt.pix.bytesperline =
  787. (f->fmt.pix.width * fmt->depth) >> 3;
  788. f->fmt.pix.sizeimage =
  789. f->fmt.pix.height * f->fmt.pix.bytesperline;
  790. return 0;
  791. }
  792. /*FIXME: This seems to be generic enough to be at videodev2 */
  793. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  794. struct v4l2_format *f)
  795. {
  796. struct vivi_fh *fh=priv;
  797. int ret = vidioc_try_fmt_cap(file,fh,f);
  798. if (ret < 0)
  799. return (ret);
  800. fh->fmt = &format;
  801. fh->width = f->fmt.pix.width;
  802. fh->height = f->fmt.pix.height;
  803. fh->vb_vidq.field = f->fmt.pix.field;
  804. fh->type = f->type;
  805. return (0);
  806. }
  807. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  808. {
  809. struct vivi_fh *fh=priv;
  810. return (videobuf_reqbufs(&fh->vb_vidq, p));
  811. }
  812. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  813. {
  814. struct vivi_fh *fh=priv;
  815. return (videobuf_querybuf(&fh->vb_vidq, p));
  816. }
  817. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  818. {
  819. struct vivi_fh *fh=priv;
  820. return (videobuf_qbuf(&fh->vb_vidq, p));
  821. }
  822. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  823. {
  824. struct vivi_fh *fh=priv;
  825. return (videobuf_dqbuf(&fh->vb_vidq, p,
  826. file->f_flags & O_NONBLOCK));
  827. }
  828. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  829. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  830. {
  831. struct vivi_fh *fh=priv;
  832. struct videobuf_queue *q=&fh->vb_vidq;
  833. struct v4l2_requestbuffers req;
  834. unsigned int i;
  835. int ret;
  836. req.type = q->type;
  837. req.count = 8;
  838. req.memory = V4L2_MEMORY_MMAP;
  839. ret = videobuf_reqbufs(q,&req);
  840. if (ret < 0)
  841. return (ret);
  842. mbuf->frames = req.count;
  843. mbuf->size = 0;
  844. for (i = 0; i < mbuf->frames; i++) {
  845. mbuf->offsets[i] = q->bufs[i]->boff;
  846. mbuf->size += q->bufs[i]->bsize;
  847. }
  848. return (0);
  849. }
  850. #endif
  851. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  852. {
  853. struct vivi_fh *fh=priv;
  854. struct vivi_dev *dev = fh->dev;
  855. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  856. return -EINVAL;
  857. if (i != fh->type)
  858. return -EINVAL;
  859. if (!res_get(dev,fh))
  860. return -EBUSY;
  861. return (videobuf_streamon(&fh->vb_vidq));
  862. }
  863. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  864. {
  865. struct vivi_fh *fh=priv;
  866. struct vivi_dev *dev = fh->dev;
  867. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  868. return -EINVAL;
  869. if (i != fh->type)
  870. return -EINVAL;
  871. videobuf_streamoff(&fh->vb_vidq);
  872. res_free(dev,fh);
  873. return (0);
  874. }
  875. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
  876. {
  877. return 0;
  878. }
  879. /* only one input in this sample driver */
  880. static int vidioc_enum_input (struct file *file, void *priv,
  881. struct v4l2_input *inp)
  882. {
  883. if (inp->index != 0)
  884. return -EINVAL;
  885. inp->type = V4L2_INPUT_TYPE_CAMERA;
  886. inp->std = V4L2_STD_NTSC_M;
  887. strcpy(inp->name,"Camera");
  888. return (0);
  889. }
  890. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  891. {
  892. *i = 0;
  893. return (0);
  894. }
  895. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  896. {
  897. if (i > 0)
  898. return -EINVAL;
  899. return (0);
  900. }
  901. /* --- controls ---------------------------------------------- */
  902. static int vidioc_queryctrl (struct file *file, void *priv,
  903. struct v4l2_queryctrl *qc)
  904. {
  905. int i;
  906. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  907. if (qc->id && qc->id == vivi_qctrl[i].id) {
  908. memcpy(qc, &(vivi_qctrl[i]),
  909. sizeof(*qc));
  910. return (0);
  911. }
  912. return -EINVAL;
  913. }
  914. static int vidioc_g_ctrl (struct file *file, void *priv,
  915. struct v4l2_control *ctrl)
  916. {
  917. int i;
  918. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  919. if (ctrl->id == vivi_qctrl[i].id) {
  920. ctrl->value=qctl_regs[i];
  921. return (0);
  922. }
  923. return -EINVAL;
  924. }
  925. static int vidioc_s_ctrl (struct file *file, void *priv,
  926. struct v4l2_control *ctrl)
  927. {
  928. int i;
  929. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  930. if (ctrl->id == vivi_qctrl[i].id) {
  931. if (ctrl->value <
  932. vivi_qctrl[i].minimum
  933. || ctrl->value >
  934. vivi_qctrl[i].maximum) {
  935. return (-ERANGE);
  936. }
  937. qctl_regs[i]=ctrl->value;
  938. return (0);
  939. }
  940. return -EINVAL;
  941. }
  942. /* ------------------------------------------------------------------
  943. File operations for the device
  944. ------------------------------------------------------------------*/
  945. #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
  946. static int vivi_open(struct inode *inode, struct file *file)
  947. {
  948. int minor = iminor(inode);
  949. struct vivi_dev *h,*dev = NULL;
  950. struct vivi_fh *fh;
  951. struct list_head *list;
  952. enum v4l2_buf_type type = 0;
  953. int i;
  954. printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
  955. list_for_each(list,&vivi_devlist) {
  956. h = list_entry(list, struct vivi_dev, vivi_devlist);
  957. if (h->vfd.minor == minor) {
  958. dev = h;
  959. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  960. }
  961. }
  962. if (NULL == dev)
  963. return -ENODEV;
  964. /* If more than one user, mutex should be added */
  965. dev->users++;
  966. dprintk(1,"open minor=%d type=%s users=%d\n",
  967. minor,v4l2_type_names[type],dev->users);
  968. /* allocate + initialize per filehandle data */
  969. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  970. if (NULL == fh) {
  971. dev->users--;
  972. return -ENOMEM;
  973. }
  974. file->private_data = fh;
  975. fh->dev = dev;
  976. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  977. fh->fmt = &format;
  978. fh->width = 640;
  979. fh->height = 480;
  980. /* Put all controls at a sane state */
  981. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  982. qctl_regs[i] =vivi_qctrl[i].default_value;
  983. dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
  984. (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
  985. dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
  986. dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
  987. /* Resets frame counters */
  988. dev->h=0;
  989. dev->m=0;
  990. dev->s=0;
  991. dev->us=0;
  992. dev->jiffies=jiffies;
  993. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  994. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  995. videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
  996. NULL, NULL,
  997. fh->type,
  998. V4L2_FIELD_INTERLACED,
  999. sizeof(struct vivi_buffer),fh);
  1000. return 0;
  1001. }
  1002. static ssize_t
  1003. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1004. {
  1005. struct vivi_fh *fh = file->private_data;
  1006. if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1007. if (res_locked(fh->dev))
  1008. return -EBUSY;
  1009. return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
  1010. file->f_flags & O_NONBLOCK);
  1011. }
  1012. return 0;
  1013. }
  1014. static unsigned int
  1015. vivi_poll(struct file *file, struct poll_table_struct *wait)
  1016. {
  1017. struct vivi_fh *fh = file->private_data;
  1018. struct vivi_buffer *buf;
  1019. dprintk(1,"%s\n",__FUNCTION__);
  1020. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1021. return POLLERR;
  1022. if (res_get(fh->dev,fh)) {
  1023. dprintk(1,"poll: mmap interface\n");
  1024. /* streaming capture */
  1025. if (list_empty(&fh->vb_vidq.stream))
  1026. return POLLERR;
  1027. buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
  1028. } else {
  1029. dprintk(1,"poll: read() interface\n");
  1030. /* read() capture */
  1031. buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
  1032. if (NULL == buf)
  1033. return POLLERR;
  1034. }
  1035. poll_wait(file, &buf->vb.done, wait);
  1036. if (buf->vb.state == STATE_DONE ||
  1037. buf->vb.state == STATE_ERROR)
  1038. return POLLIN|POLLRDNORM;
  1039. return 0;
  1040. }
  1041. static int vivi_release(struct inode *inode, struct file *file)
  1042. {
  1043. struct vivi_fh *fh = file->private_data;
  1044. struct vivi_dev *dev = fh->dev;
  1045. struct vivi_dmaqueue *vidq = &dev->vidq;
  1046. int minor = iminor(inode);
  1047. vivi_stop_thread(vidq);
  1048. videobuf_mmap_free(&fh->vb_vidq);
  1049. kfree (fh);
  1050. dev->users--;
  1051. printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
  1052. return 0;
  1053. }
  1054. static int
  1055. vivi_mmap(struct file *file, struct vm_area_struct * vma)
  1056. {
  1057. struct vivi_fh *fh = file->private_data;
  1058. int ret;
  1059. dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
  1060. ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1061. dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
  1062. (unsigned long)vma->vm_start,
  1063. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1064. ret);
  1065. return ret;
  1066. }
  1067. static const struct file_operations vivi_fops = {
  1068. .owner = THIS_MODULE,
  1069. .open = vivi_open,
  1070. .release = vivi_release,
  1071. .read = vivi_read,
  1072. .poll = vivi_poll,
  1073. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1074. .mmap = vivi_mmap,
  1075. .llseek = no_llseek,
  1076. };
  1077. static struct video_device vivi = {
  1078. .name = "vivi",
  1079. .type = VID_TYPE_CAPTURE,
  1080. .hardware = 0,
  1081. .fops = &vivi_fops,
  1082. .minor = -1,
  1083. // .release = video_device_release,
  1084. .vidioc_querycap = vidioc_querycap,
  1085. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1086. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1087. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1088. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1089. .vidioc_reqbufs = vidioc_reqbufs,
  1090. .vidioc_querybuf = vidioc_querybuf,
  1091. .vidioc_qbuf = vidioc_qbuf,
  1092. .vidioc_dqbuf = vidioc_dqbuf,
  1093. .vidioc_s_std = vidioc_s_std,
  1094. .vidioc_enum_input = vidioc_enum_input,
  1095. .vidioc_g_input = vidioc_g_input,
  1096. .vidioc_s_input = vidioc_s_input,
  1097. .vidioc_queryctrl = vidioc_queryctrl,
  1098. .vidioc_g_ctrl = vidioc_g_ctrl,
  1099. .vidioc_s_ctrl = vidioc_s_ctrl,
  1100. .vidioc_streamon = vidioc_streamon,
  1101. .vidioc_streamoff = vidioc_streamoff,
  1102. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1103. .vidiocgmbuf = vidiocgmbuf,
  1104. #endif
  1105. .tvnorms = V4L2_STD_NTSC_M,
  1106. .current_norm = V4L2_STD_NTSC_M,
  1107. };
  1108. /* -----------------------------------------------------------------
  1109. Initialization and module stuff
  1110. ------------------------------------------------------------------*/
  1111. static int __init vivi_init(void)
  1112. {
  1113. int ret;
  1114. struct vivi_dev *dev;
  1115. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1116. if (NULL == dev)
  1117. return -ENOMEM;
  1118. list_add_tail(&dev->vivi_devlist,&vivi_devlist);
  1119. /* init video dma queues */
  1120. INIT_LIST_HEAD(&dev->vidq.active);
  1121. INIT_LIST_HEAD(&dev->vidq.queued);
  1122. /* initialize locks */
  1123. init_MUTEX(&dev->lock);
  1124. dev->vidq.timeout.function = vivi_vid_timeout;
  1125. dev->vidq.timeout.data = (unsigned long)dev;
  1126. init_timer(&dev->vidq.timeout);
  1127. ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
  1128. printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
  1129. return ret;
  1130. }
  1131. static void __exit vivi_exit(void)
  1132. {
  1133. struct vivi_dev *h;
  1134. struct list_head *list;
  1135. while (!list_empty(&vivi_devlist)) {
  1136. list = vivi_devlist.next;
  1137. list_del(list);
  1138. h = list_entry(list, struct vivi_dev, vivi_devlist);
  1139. kfree (h);
  1140. }
  1141. video_unregister_device(&vivi);
  1142. }
  1143. module_init(vivi_init);
  1144. module_exit(vivi_exit);
  1145. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  1146. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  1147. MODULE_LICENSE("Dual BSD/GPL");
  1148. module_param(video_nr, int, 0);
  1149. module_param_named(debug,vivi.debug, int, 0644);
  1150. MODULE_PARM_DESC(debug,"activates debug info");
  1151. module_param(vid_limit,int,0644);
  1152. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");