vivi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  48. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. #define VIVI_MAJOR_VERSION 0
  51. #define VIVI_MINOR_VERSION 4
  52. #define VIVI_RELEASE 0
  53. #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
  54. static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
  55. module_param(video_nr, int, 0);
  56. static int debug = 0;
  57. module_param(debug, int, 0);
  58. static unsigned int vid_limit = 16;
  59. module_param(vid_limit,int,0644);
  60. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
  61. /* supported controls */
  62. static struct v4l2_queryctrl vivi_qctrl[] = {
  63. {
  64. .id = V4L2_CID_AUDIO_VOLUME,
  65. .name = "Volume",
  66. .minimum = 0,
  67. .maximum = 65535,
  68. .step = 65535/100,
  69. .default_value = 65535,
  70. .flags = 0,
  71. .type = V4L2_CTRL_TYPE_INTEGER,
  72. },{
  73. .id = V4L2_CID_BRIGHTNESS,
  74. .type = V4L2_CTRL_TYPE_INTEGER,
  75. .name = "Brightness",
  76. .minimum = 0,
  77. .maximum = 255,
  78. .step = 1,
  79. .default_value = 127,
  80. .flags = 0,
  81. }, {
  82. .id = V4L2_CID_CONTRAST,
  83. .type = V4L2_CTRL_TYPE_INTEGER,
  84. .name = "Contrast",
  85. .minimum = 0,
  86. .maximum = 255,
  87. .step = 0x1,
  88. .default_value = 0x10,
  89. .flags = 0,
  90. }, {
  91. .id = V4L2_CID_SATURATION,
  92. .type = V4L2_CTRL_TYPE_INTEGER,
  93. .name = "Saturation",
  94. .minimum = 0,
  95. .maximum = 255,
  96. .step = 0x1,
  97. .default_value = 127,
  98. .flags = 0,
  99. }, {
  100. .id = V4L2_CID_HUE,
  101. .type = V4L2_CTRL_TYPE_INTEGER,
  102. .name = "Hue",
  103. .minimum = -128,
  104. .maximum = 127,
  105. .step = 0x1,
  106. .default_value = 0,
  107. .flags = 0,
  108. }
  109. };
  110. static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
  111. #define dprintk(level,fmt, arg...) \
  112. do { \
  113. if (debug >= (level)) \
  114. printk(KERN_DEBUG "vivi: " fmt , ## arg); \
  115. } while (0)
  116. /* ------------------------------------------------------------------
  117. Basic structures
  118. ------------------------------------------------------------------*/
  119. struct vivi_fmt {
  120. char *name;
  121. u32 fourcc; /* v4l2 format id */
  122. int depth;
  123. };
  124. static struct vivi_fmt format = {
  125. .name = "4:2:2, packed, YUYV",
  126. .fourcc = V4L2_PIX_FMT_YUYV,
  127. .depth = 16,
  128. };
  129. struct sg_to_addr {
  130. int pos;
  131. struct scatterlist *sg;
  132. };
  133. /* buffer for one video frame */
  134. struct vivi_buffer {
  135. /* common v4l buffer stuff -- must be first */
  136. struct videobuf_buffer vb;
  137. struct vivi_fmt *fmt;
  138. struct sg_to_addr *to_addr;
  139. };
  140. struct vivi_dmaqueue {
  141. struct list_head active;
  142. struct list_head queued;
  143. struct timer_list timeout;
  144. /* thread for generating video stream*/
  145. struct task_struct *kthread;
  146. wait_queue_head_t wq;
  147. /* Counters to control fps rate */
  148. int frame;
  149. int ini_jiffies;
  150. };
  151. static LIST_HEAD(vivi_devlist);
  152. struct vivi_dev {
  153. struct list_head vivi_devlist;
  154. struct semaphore lock;
  155. int users;
  156. /* various device info */
  157. unsigned int resources;
  158. struct video_device video_dev;
  159. struct vivi_dmaqueue vidq;
  160. /* Several counters */
  161. int h,m,s,us,jiffies;
  162. char timestr[13];
  163. };
  164. struct vivi_fh {
  165. struct vivi_dev *dev;
  166. /* video capture */
  167. struct vivi_fmt *fmt;
  168. unsigned int width,height;
  169. struct videobuf_queue vb_vidq;
  170. enum v4l2_buf_type type;
  171. };
  172. /* ------------------------------------------------------------------
  173. DMA and thread functions
  174. ------------------------------------------------------------------*/
  175. /* Bars and Colors should match positions */
  176. enum colors {
  177. WHITE,
  178. AMBAR,
  179. CYAN,
  180. GREEN,
  181. MAGENTA,
  182. RED,
  183. BLUE
  184. };
  185. static u8 bars[8][3] = {
  186. /* R G B */
  187. {204,204,204}, /* white */
  188. {208,208, 0}, /* ambar */
  189. { 0,206,206}, /* cyan */
  190. { 0,239, 0}, /* green */
  191. {239, 0,239}, /* magenta */
  192. {205, 0, 0}, /* red */
  193. { 0, 0,255}, /* blue */
  194. { 0, 0, 0}
  195. };
  196. #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
  197. /* RGB to V(Cr) Color transform */
  198. #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
  199. /* RGB to U(Cb) Color transform */
  200. #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
  201. #define TSTAMP_MIN_Y 24
  202. #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
  203. #define TSTAMP_MIN_X 64
  204. static void prep_to_addr(struct sg_to_addr to_addr[],
  205. struct videobuf_buffer *vb)
  206. {
  207. int i, pos=0;
  208. for (i=0;i<vb->dma.nr_pages;i++) {
  209. to_addr[i].sg=&vb->dma.sglist[i];
  210. to_addr[i].pos=pos;
  211. pos += vb->dma.sglist[i].length;
  212. }
  213. }
  214. static int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
  215. {
  216. int p1=0,p2=pages-1,p3=pages/2;
  217. /* Sanity test */
  218. BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
  219. while (p1+1<p2) {
  220. if (pos < to_addr[p3].pos) {
  221. p2=p3;
  222. } else {
  223. p1=p3;
  224. }
  225. p3=(p1+p2)/2;
  226. }
  227. if (pos >= to_addr[p2].pos)
  228. p1=p2;
  229. return (p1);
  230. }
  231. static void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
  232. int hmax, int line, char *timestr)
  233. {
  234. int w,i,j,pos=inipos,pgpos,oldpg,y;
  235. char *p,*s,*basep;
  236. struct page *pg;
  237. u8 chr,r,g,b,color;
  238. /* Get first addr pointed to pixel position */
  239. oldpg=get_addr_pos(pos,pages,to_addr);
  240. pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
  241. basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
  242. /* We will just duplicate the second pixel at the packet */
  243. wmax/=2;
  244. /* Generate a standard color bar pattern */
  245. for (w=0;w<wmax;w++) {
  246. r=bars[w*7/wmax][0];
  247. g=bars[w*7/wmax][1];
  248. b=bars[w*7/wmax][2];
  249. for (color=0;color<4;color++) {
  250. pgpos=get_addr_pos(pos,pages,to_addr);
  251. if (pgpos!=oldpg) {
  252. pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
  253. kunmap_atomic(basep, KM_BOUNCE_READ);
  254. basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
  255. oldpg=pgpos;
  256. }
  257. p=basep+pos-to_addr[pgpos].pos;
  258. switch (color) {
  259. case 0:
  260. case 2:
  261. *p=TO_Y(r,g,b); /* Luminance */
  262. break;
  263. case 1:
  264. *p=TO_U(r,g,b); /* Cb */
  265. break;
  266. case 3:
  267. *p=TO_V(r,g,b); /* Cr */
  268. break;
  269. }
  270. pos++;
  271. }
  272. }
  273. /* Checks if it is possible to show timestamp */
  274. if (TSTAMP_MAX_Y>=hmax)
  275. goto end;
  276. if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
  277. goto end;
  278. /* Print stream time */
  279. if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
  280. j=TSTAMP_MIN_X;
  281. for (s=timestr;*s;s++) {
  282. chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
  283. for (i=0;i<7;i++) {
  284. if (chr&1<<(7-i)) { /* Font color*/
  285. r=bars[BLUE][0];
  286. g=bars[BLUE][1];
  287. b=bars[BLUE][2];
  288. r=g=b=0;
  289. g=198;
  290. } else { /* Background color */
  291. r=bars[WHITE][0];
  292. g=bars[WHITE][1];
  293. b=bars[WHITE][2];
  294. r=g=b=0;
  295. }
  296. pos=inipos+j*2;
  297. for (color=0;color<4;color++) {
  298. pgpos=get_addr_pos(pos,pages,to_addr);
  299. if (pgpos!=oldpg) {
  300. pg=pfn_to_page(to_addr[pgpos].
  301. sg->dma_address
  302. >> PAGE_SHIFT);
  303. kunmap_atomic(basep,
  304. KM_BOUNCE_READ);
  305. basep= kmap_atomic(pg,
  306. KM_BOUNCE_READ)+
  307. to_addr[pgpos].sg->offset;
  308. oldpg=pgpos;
  309. }
  310. p=basep+pos-to_addr[pgpos].pos;
  311. y=TO_Y(r,g,b);
  312. switch (color) {
  313. case 0:
  314. case 2:
  315. *p=TO_Y(r,g,b); /* Luminance */
  316. break;
  317. case 1:
  318. *p=TO_U(r,g,b); /* Cb */
  319. break;
  320. case 3:
  321. *p=TO_V(r,g,b); /* Cr */
  322. break;
  323. }
  324. pos++;
  325. }
  326. j++;
  327. }
  328. }
  329. }
  330. end:
  331. kunmap_atomic(basep, KM_BOUNCE_READ);
  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 (!vb->dma.sglist[0].dma_address)
  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 (dma_q->kthread == NULL) {
  457. printk(KERN_ERR "vivi: kernel_thread() failed\n");
  458. return -EINVAL;
  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[i].dma_address = 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 vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
  692. struct v4l2_format *f)
  693. {
  694. struct vivi_fmt *fmt;
  695. enum v4l2_field field;
  696. unsigned int maxw, maxh;
  697. if (format.fourcc != f->fmt.pix.pixelformat) {
  698. dprintk(1,"Fourcc format invalid.\n");
  699. return -EINVAL;
  700. }
  701. fmt=&format;
  702. field = f->fmt.pix.field;
  703. if (field == V4L2_FIELD_ANY) {
  704. // field=V4L2_FIELD_INTERLACED;
  705. field=V4L2_FIELD_SEQ_TB;
  706. } else if (V4L2_FIELD_INTERLACED != field) {
  707. dprintk(1,"Field type invalid.\n");
  708. return -EINVAL;
  709. }
  710. maxw = norm_maxw();
  711. maxh = norm_maxh();
  712. f->fmt.pix.field = field;
  713. if (f->fmt.pix.height < 32)
  714. f->fmt.pix.height = 32;
  715. if (f->fmt.pix.height > maxh)
  716. f->fmt.pix.height = maxh;
  717. if (f->fmt.pix.width < 48)
  718. f->fmt.pix.width = 48;
  719. if (f->fmt.pix.width > maxw)
  720. f->fmt.pix.width = maxw;
  721. f->fmt.pix.width &= ~0x03;
  722. f->fmt.pix.bytesperline =
  723. (f->fmt.pix.width * fmt->depth) >> 3;
  724. f->fmt.pix.sizeimage =
  725. f->fmt.pix.height * f->fmt.pix.bytesperline;
  726. return 0;
  727. }
  728. static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
  729. {
  730. /* is it free? */
  731. down(&dev->lock);
  732. if (dev->resources) {
  733. /* no, someone else uses it */
  734. up(&dev->lock);
  735. return 0;
  736. }
  737. /* it's free, grab it */
  738. dev->resources =1;
  739. dprintk(1,"res: get\n");
  740. up(&dev->lock);
  741. return 1;
  742. }
  743. static int res_locked(struct vivi_dev *dev)
  744. {
  745. return (dev->resources);
  746. }
  747. static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
  748. {
  749. down(&dev->lock);
  750. dev->resources = 0;
  751. dprintk(1,"res: put\n");
  752. up(&dev->lock);
  753. }
  754. static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
  755. {
  756. struct vivi_fh *fh = file->private_data;
  757. struct vivi_dev *dev = fh->dev;
  758. int ret=0;
  759. if (debug) {
  760. if (_IOC_DIR(cmd) & _IOC_WRITE)
  761. v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
  762. else if (!_IOC_DIR(cmd) & _IOC_READ) {
  763. v4l_print_ioctl("vivi", cmd);
  764. }
  765. }
  766. switch(cmd) {
  767. /* --- capabilities ------------------------------------------ */
  768. case VIDIOC_QUERYCAP:
  769. {
  770. struct v4l2_capability *cap = (struct v4l2_capability*)arg;
  771. memset(cap, 0, sizeof(*cap));
  772. strcpy(cap->driver, "vivi");
  773. strcpy(cap->card, "vivi");
  774. cap->version = VIVI_VERSION;
  775. cap->capabilities =
  776. V4L2_CAP_VIDEO_CAPTURE |
  777. V4L2_CAP_STREAMING |
  778. V4L2_CAP_READWRITE;
  779. break;
  780. }
  781. /* --- capture ioctls ---------------------------------------- */
  782. case VIDIOC_ENUM_FMT:
  783. {
  784. struct v4l2_fmtdesc *f = arg;
  785. enum v4l2_buf_type type;
  786. unsigned int index;
  787. index = f->index;
  788. type = f->type;
  789. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  790. ret=-EINVAL;
  791. break;
  792. }
  793. switch (type) {
  794. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  795. if (index > 0){
  796. ret=-EINVAL;
  797. break;
  798. }
  799. memset(f,0,sizeof(*f));
  800. f->index = index;
  801. f->type = type;
  802. strlcpy(f->description,format.name,sizeof(f->description));
  803. f->pixelformat = format.fourcc;
  804. break;
  805. default:
  806. ret=-EINVAL;
  807. }
  808. break;
  809. }
  810. case VIDIOC_G_FMT:
  811. {
  812. struct v4l2_format *f = (struct v4l2_format *)arg;
  813. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  814. ret=-EINVAL;
  815. break;
  816. }
  817. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  818. f->fmt.pix.width = fh->width;
  819. f->fmt.pix.height = fh->height;
  820. f->fmt.pix.field = fh->vb_vidq.field;
  821. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  822. f->fmt.pix.bytesperline =
  823. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  824. f->fmt.pix.sizeimage =
  825. f->fmt.pix.height * f->fmt.pix.bytesperline;
  826. break;
  827. }
  828. case VIDIOC_S_FMT:
  829. {
  830. struct v4l2_format *f = arg;
  831. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  832. dprintk(1,"Only capture supported.\n");
  833. ret=-EINVAL;
  834. break;
  835. }
  836. ret = vivi_try_fmt(dev,fh,f);
  837. if (ret < 0)
  838. break;
  839. fh->fmt = &format;
  840. fh->width = f->fmt.pix.width;
  841. fh->height = f->fmt.pix.height;
  842. fh->vb_vidq.field = f->fmt.pix.field;
  843. fh->type = f->type;
  844. break;
  845. }
  846. case VIDIOC_TRY_FMT:
  847. {
  848. struct v4l2_format *f = arg;
  849. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  850. ret=-EINVAL;
  851. break;
  852. }
  853. ret=vivi_try_fmt(dev,fh,f);
  854. break;
  855. }
  856. case VIDIOC_REQBUFS:
  857. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  858. ret=-EINVAL;
  859. break;
  860. }
  861. ret=videobuf_reqbufs(&fh->vb_vidq, arg);
  862. break;
  863. case VIDIOC_QUERYBUF:
  864. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  865. ret=-EINVAL;
  866. break;
  867. }
  868. ret=videobuf_querybuf(&fh->vb_vidq, arg);
  869. break;
  870. case VIDIOC_QBUF:
  871. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  872. ret=-EINVAL;
  873. break;
  874. }
  875. ret=videobuf_qbuf(&fh->vb_vidq, arg);
  876. break;
  877. case VIDIOC_DQBUF:
  878. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  879. ret=-EINVAL;
  880. break;
  881. }
  882. ret=videobuf_dqbuf(&fh->vb_vidq, arg,
  883. file->f_flags & O_NONBLOCK);
  884. break;
  885. #ifdef HAVE_V4L1
  886. /* --- streaming capture ------------------------------------- */
  887. case VIDIOCGMBUF:
  888. {
  889. struct video_mbuf *mbuf = arg;
  890. struct videobuf_queue *q=&fh->vb_vidq;
  891. struct v4l2_requestbuffers req;
  892. unsigned int i;
  893. memset(&req,0,sizeof(req));
  894. req.type = q->type;
  895. req.count = 8;
  896. req.memory = V4L2_MEMORY_MMAP;
  897. ret = videobuf_reqbufs(q,&req);
  898. if (ret < 0)
  899. break;
  900. memset(mbuf,0,sizeof(*mbuf));
  901. mbuf->frames = req.count;
  902. mbuf->size = 0;
  903. for (i = 0; i < mbuf->frames; i++) {
  904. mbuf->offsets[i] = q->bufs[i]->boff;
  905. mbuf->size += q->bufs[i]->bsize;
  906. }
  907. break;
  908. }
  909. #endif
  910. case VIDIOC_STREAMON:
  911. {
  912. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  913. return -EINVAL;
  914. if (!res_get(dev,fh))
  915. return -EBUSY;
  916. ret=videobuf_streamon(&fh->vb_vidq);
  917. break;
  918. }
  919. case VIDIOC_STREAMOFF:
  920. {
  921. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  922. ret=-EINVAL;
  923. break;
  924. }
  925. ret = videobuf_streamoff(&fh->vb_vidq);
  926. if (ret < 0)
  927. break;
  928. res_free(dev,fh);
  929. break;
  930. }
  931. /* ---------- tv norms ---------- */
  932. case VIDIOC_ENUMSTD:
  933. {
  934. struct v4l2_standard *e = arg;
  935. if (e->index>0) {
  936. ret=-EINVAL;
  937. break;
  938. }
  939. ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
  940. /* Allows vivi to use different fps from video std */
  941. e->frameperiod.numerator = WAKE_NUMERATOR;
  942. e->frameperiod.denominator = WAKE_DENOMINATOR;
  943. break;
  944. }
  945. case VIDIOC_G_STD:
  946. {
  947. v4l2_std_id *id = arg;
  948. *id = V4L2_STD_NTSC_M;
  949. break;
  950. }
  951. case VIDIOC_S_STD:
  952. {
  953. break;
  954. }
  955. /* ------ input switching ---------- */
  956. case VIDIOC_ENUMINPUT:
  957. { /* only one input in this sample driver */
  958. struct v4l2_input *inp = arg;
  959. if (inp->index != 0) {
  960. ret=-EINVAL;
  961. break;
  962. }
  963. memset(inp, 0, sizeof(*inp));
  964. inp->index = 0;
  965. inp->type = V4L2_INPUT_TYPE_CAMERA;
  966. inp->std = V4L2_STD_NTSC_M;
  967. strcpy(inp->name,"Camera");
  968. break;
  969. }
  970. case VIDIOC_G_INPUT:
  971. {
  972. unsigned int *i = arg;
  973. *i = 0;
  974. break;
  975. }
  976. case VIDIOC_S_INPUT:
  977. {
  978. unsigned int *i = arg;
  979. if (*i > 0)
  980. ret=-EINVAL;
  981. break;
  982. }
  983. /* --- controls ---------------------------------------------- */
  984. case VIDIOC_QUERYCTRL:
  985. {
  986. struct v4l2_queryctrl *qc = arg;
  987. int i;
  988. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  989. if (qc->id && qc->id == vivi_qctrl[i].id) {
  990. memcpy(qc, &(vivi_qctrl[i]),
  991. sizeof(*qc));
  992. break;
  993. }
  994. ret=-EINVAL;
  995. break;
  996. }
  997. case VIDIOC_G_CTRL:
  998. {
  999. struct v4l2_control *ctrl = arg;
  1000. int i;
  1001. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  1002. if (ctrl->id == vivi_qctrl[i].id) {
  1003. ctrl->value=qctl_regs[i];
  1004. break;
  1005. }
  1006. ret=-EINVAL;
  1007. break;
  1008. }
  1009. case VIDIOC_S_CTRL:
  1010. {
  1011. struct v4l2_control *ctrl = arg;
  1012. int i;
  1013. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  1014. if (ctrl->id == vivi_qctrl[i].id) {
  1015. if (ctrl->value <
  1016. vivi_qctrl[i].minimum
  1017. || ctrl->value >
  1018. vivi_qctrl[i].maximum) {
  1019. ret=-ERANGE;
  1020. break;
  1021. }
  1022. qctl_regs[i]=ctrl->value;
  1023. break;
  1024. }
  1025. ret=-EINVAL;
  1026. break;
  1027. }
  1028. default:
  1029. ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
  1030. }
  1031. if (debug) {
  1032. if (ret<0) {
  1033. v4l_print_ioctl("vivi(err)", cmd);
  1034. dprintk(1,"errcode=%d\n",ret);
  1035. } else if (_IOC_DIR(cmd) & _IOC_READ)
  1036. v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
  1037. }
  1038. return ret;
  1039. }
  1040. static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1041. {
  1042. return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
  1043. }
  1044. /* ------------------------------------------------------------------
  1045. File operations for the device
  1046. ------------------------------------------------------------------*/
  1047. #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
  1048. static int vivi_open(struct inode *inode, struct file *file)
  1049. {
  1050. int minor = iminor(inode);
  1051. struct vivi_dev *h,*dev = NULL;
  1052. struct vivi_fh *fh;
  1053. struct list_head *list;
  1054. enum v4l2_buf_type type = 0;
  1055. int i;
  1056. printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
  1057. list_for_each(list,&vivi_devlist) {
  1058. h = list_entry(list, struct vivi_dev, vivi_devlist);
  1059. if (h->video_dev.minor == minor) {
  1060. dev = h;
  1061. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1062. }
  1063. }
  1064. if (NULL == dev)
  1065. return -ENODEV;
  1066. /* If more than one user, mutex should be added */
  1067. dev->users++;
  1068. dprintk(1,"open minor=%d type=%s users=%d\n",
  1069. minor,v4l2_type_names[type],dev->users);
  1070. /* allocate + initialize per filehandle data */
  1071. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  1072. if (NULL == fh) {
  1073. dev->users--;
  1074. return -ENOMEM;
  1075. }
  1076. file->private_data = fh;
  1077. fh->dev = dev;
  1078. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1079. fh->fmt = &format;
  1080. fh->width = 640;
  1081. fh->height = 480;
  1082. /* Put all controls at a sane state */
  1083. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  1084. qctl_regs[i] =vivi_qctrl[i].default_value;
  1085. dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
  1086. (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
  1087. dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
  1088. dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
  1089. /* Resets frame counters */
  1090. dev->h=0;
  1091. dev->m=0;
  1092. dev->s=0;
  1093. dev->us=0;
  1094. dev->jiffies=jiffies;
  1095. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  1096. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  1097. videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
  1098. NULL, NULL,
  1099. fh->type,
  1100. V4L2_FIELD_INTERLACED,
  1101. sizeof(struct vivi_buffer),fh);
  1102. return 0;
  1103. }
  1104. static ssize_t
  1105. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1106. {
  1107. struct vivi_fh *fh = file->private_data;
  1108. if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1109. if (res_locked(fh->dev))
  1110. return -EBUSY;
  1111. return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
  1112. file->f_flags & O_NONBLOCK);
  1113. }
  1114. return 0;
  1115. }
  1116. static unsigned int
  1117. vivi_poll(struct file *file, struct poll_table_struct *wait)
  1118. {
  1119. struct vivi_fh *fh = file->private_data;
  1120. struct vivi_buffer *buf;
  1121. dprintk(1,"%s\n",__FUNCTION__);
  1122. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1123. return POLLERR;
  1124. if (res_get(fh->dev,fh)) {
  1125. dprintk(1,"poll: mmap interface\n");
  1126. /* streaming capture */
  1127. if (list_empty(&fh->vb_vidq.stream))
  1128. return POLLERR;
  1129. buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
  1130. } else {
  1131. dprintk(1,"poll: read() interface\n");
  1132. /* read() capture */
  1133. buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
  1134. if (NULL == buf)
  1135. return POLLERR;
  1136. }
  1137. poll_wait(file, &buf->vb.done, wait);
  1138. if (buf->vb.state == STATE_DONE ||
  1139. buf->vb.state == STATE_ERROR)
  1140. return POLLIN|POLLRDNORM;
  1141. return 0;
  1142. }
  1143. static int vivi_release(struct inode *inode, struct file *file)
  1144. {
  1145. struct vivi_fh *fh = file->private_data;
  1146. struct vivi_dev *dev = fh->dev;
  1147. struct vivi_dmaqueue *vidq = &dev->vidq;
  1148. int minor = iminor(inode);
  1149. vivi_stop_thread(vidq);
  1150. videobuf_mmap_free(&fh->vb_vidq);
  1151. kfree (fh);
  1152. dev->users--;
  1153. printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
  1154. return 0;
  1155. }
  1156. static int
  1157. vivi_mmap(struct file *file, struct vm_area_struct * vma)
  1158. {
  1159. struct vivi_fh *fh = file->private_data;
  1160. int ret;
  1161. dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
  1162. ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1163. dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
  1164. (unsigned long)vma->vm_start,
  1165. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1166. ret);
  1167. return ret;
  1168. }
  1169. static struct file_operations vivi_fops = {
  1170. .owner = THIS_MODULE,
  1171. .open = vivi_open,
  1172. .release = vivi_release,
  1173. .read = vivi_read,
  1174. .poll = vivi_poll,
  1175. .ioctl = vivi_ioctl,
  1176. .mmap = vivi_mmap,
  1177. .llseek = no_llseek,
  1178. };
  1179. static struct video_device vivi = {
  1180. .name = "VTM Virtual Video Capture Board",
  1181. .type = VID_TYPE_CAPTURE,
  1182. .hardware = 0,
  1183. .fops = &vivi_fops,
  1184. .minor = -1,
  1185. // .release = video_device_release,
  1186. };
  1187. /* ------------------------------------------------------------------
  1188. Initialization and module stuff
  1189. ------------------------------------------------------------------*/
  1190. static int __init vivi_init(void)
  1191. {
  1192. int ret;
  1193. struct vivi_dev *dev;
  1194. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1195. if (NULL == dev)
  1196. return -ENOMEM;
  1197. list_add_tail(&dev->vivi_devlist,&vivi_devlist);
  1198. /* init video dma queues */
  1199. INIT_LIST_HEAD(&dev->vidq.active);
  1200. INIT_LIST_HEAD(&dev->vidq.queued);
  1201. /* initialize locks */
  1202. init_MUTEX(&dev->lock);
  1203. dev->vidq.timeout.function = vivi_vid_timeout;
  1204. dev->vidq.timeout.data = (unsigned long)dev;
  1205. init_timer(&dev->vidq.timeout);
  1206. ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
  1207. printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
  1208. return ret;
  1209. }
  1210. static void __exit vivi_exit(void)
  1211. {
  1212. struct vivi_dev *h;
  1213. struct list_head *list;
  1214. list_for_each(list,&vivi_devlist) {
  1215. h = list_entry(list, struct vivi_dev, vivi_devlist);
  1216. kfree (h);
  1217. }
  1218. video_unregister_device(&vivi);
  1219. }
  1220. module_init(vivi_init);
  1221. module_exit(vivi_exit);