vivi.c 33 KB

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