vivi.c 29 KB

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