vivi.c 31 KB

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