vivi.c 34 KB

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