vivi.c 33 KB

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