vivi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. #define MODULE_NAME "vivi"
  42. /* Wake up at about 30 fps */
  43. #define WAKE_NUMERATOR 30
  44. #define WAKE_DENOMINATOR 1001
  45. #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
  46. #include "font.h"
  47. #define VIVI_MAJOR_VERSION 0
  48. #define VIVI_MINOR_VERSION 5
  49. #define VIVI_RELEASE 0
  50. #define VIVI_VERSION \
  51. 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 int video_nr = -1; /* /dev/videoN, -1 for autodetect */
  55. static int n_devs = 1; /* Number of virtual devices */
  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(dev, level, fmt, arg...) \
  107. do { \
  108. if (dev->vfd->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. };
  134. struct vivi_dmaqueue {
  135. struct list_head active;
  136. /* thread for generating video stream*/
  137. struct task_struct *kthread;
  138. wait_queue_head_t wq;
  139. /* Counters to control fps rate */
  140. int frame;
  141. int ini_jiffies;
  142. };
  143. static LIST_HEAD(vivi_devlist);
  144. struct vivi_dev {
  145. struct list_head vivi_devlist;
  146. spinlock_t slock;
  147. struct mutex mutex;
  148. int users;
  149. /* various device info */
  150. struct video_device *vfd;
  151. struct vivi_dmaqueue vidq;
  152. /* Several counters */
  153. int h, m, s, ms;
  154. unsigned long jiffies;
  155. char timestr[13];
  156. int mv_count; /* Controls bars movement */
  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. BLACK,
  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}, /* black */
  190. };
  191. #define TO_Y(r, g, b) \
  192. (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
  193. /* RGB to V(Cr) Color transform */
  194. #define TO_V(r, g, b) \
  195. (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
  196. /* RGB to U(Cb) Color transform */
  197. #define TO_U(r, g, b) \
  198. (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
  199. #define TSTAMP_MIN_Y 24
  200. #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
  201. #define TSTAMP_MIN_X 64
  202. static void gen_line(char *basep, int inipos, int wmax,
  203. int hmax, int line, int count, char *timestr)
  204. {
  205. int w, i, j, y;
  206. int pos = inipos;
  207. char *p, *s;
  208. u8 chr, r, g, b, color;
  209. /* We will just duplicate the second pixel at the packet */
  210. wmax /= 2;
  211. /* Generate a standard color bar pattern */
  212. for (w = 0; w < wmax; w++) {
  213. int colorpos = ((w + count) * 8/(wmax + 1)) % 8;
  214. r = bars[colorpos][0];
  215. g = bars[colorpos][1];
  216. b = bars[colorpos][2];
  217. for (color = 0; color < 4; color++) {
  218. p = basep + pos;
  219. switch (color) {
  220. case 0:
  221. case 2:
  222. *p = TO_Y(r, g, b); /* Luma */
  223. break;
  224. case 1:
  225. *p = TO_U(r, g, b); /* Cb */
  226. break;
  227. case 3:
  228. *p = TO_V(r, g, b); /* Cr */
  229. break;
  230. }
  231. pos++;
  232. }
  233. }
  234. /* Checks if it is possible to show timestamp */
  235. if (TSTAMP_MAX_Y >= hmax)
  236. goto end;
  237. if (TSTAMP_MIN_X + strlen(timestr) >= wmax)
  238. goto end;
  239. /* Print stream time */
  240. if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
  241. j = TSTAMP_MIN_X;
  242. for (s = timestr; *s; s++) {
  243. chr = rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
  244. for (i = 0; i < 7; i++) {
  245. if (chr & 1 << (7 - i)) {
  246. /* Font color*/
  247. r = 0;
  248. g = 198;
  249. b = 0;
  250. } else {
  251. /* Background color */
  252. r = bars[BLACK][0];
  253. g = bars[BLACK][1];
  254. b = bars[BLACK][2];
  255. }
  256. pos = inipos + j * 2;
  257. for (color = 0; color < 4; color++) {
  258. p = basep + pos;
  259. y = TO_Y(r, g, b);
  260. switch (color) {
  261. case 0:
  262. case 2:
  263. *p = TO_Y(r, g, b); /* Luma */
  264. break;
  265. case 1:
  266. *p = TO_U(r, g, b); /* Cb */
  267. break;
  268. case 3:
  269. *p = TO_V(r, g, b); /* Cr */
  270. break;
  271. }
  272. pos++;
  273. }
  274. j++;
  275. }
  276. }
  277. }
  278. end:
  279. return;
  280. }
  281. static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
  282. {
  283. int h , pos = 0;
  284. int hmax = buf->vb.height;
  285. int wmax = buf->vb.width;
  286. struct timeval ts;
  287. char *tmpbuf;
  288. void *vbuf = videobuf_to_vmalloc(&buf->vb);
  289. if (!vbuf)
  290. return;
  291. tmpbuf = kmalloc(wmax * 2, GFP_ATOMIC);
  292. if (!tmpbuf)
  293. return;
  294. for (h = 0; h < hmax; h++) {
  295. gen_line(tmpbuf, 0, wmax, hmax, h, dev->mv_count,
  296. dev->timestr);
  297. memcpy(vbuf + pos, tmpbuf, wmax * 2);
  298. pos += wmax*2;
  299. }
  300. dev->mv_count++;
  301. kfree(tmpbuf);
  302. /* Updates stream time */
  303. dev->ms += jiffies_to_msecs(jiffies-dev->jiffies);
  304. dev->jiffies = jiffies;
  305. if (dev->ms >= 1000) {
  306. dev->ms -= 1000;
  307. dev->s++;
  308. if (dev->s >= 60) {
  309. dev->s -= 60;
  310. dev->m++;
  311. if (dev->m > 60) {
  312. dev->m -= 60;
  313. dev->h++;
  314. if (dev->h > 24)
  315. dev->h -= 24;
  316. }
  317. }
  318. }
  319. sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
  320. dev->h, dev->m, dev->s, dev->ms);
  321. dprintk(dev, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
  322. dev->timestr, (unsigned long)tmpbuf, pos);
  323. /* Advice that buffer was filled */
  324. buf->vb.field_count++;
  325. do_gettimeofday(&ts);
  326. buf->vb.ts = ts;
  327. buf->vb.state = VIDEOBUF_DONE;
  328. }
  329. static void vivi_thread_tick(struct vivi_fh *fh)
  330. {
  331. struct vivi_buffer *buf;
  332. struct vivi_dev *dev = fh->dev;
  333. struct vivi_dmaqueue *dma_q = &dev->vidq;
  334. unsigned long flags = 0;
  335. dprintk(dev, 1, "Thread tick\n");
  336. spin_lock_irqsave(&dev->slock, flags);
  337. if (list_empty(&dma_q->active)) {
  338. dprintk(dev, 1, "No active queue to serve\n");
  339. goto unlock;
  340. }
  341. buf = list_entry(dma_q->active.next,
  342. struct vivi_buffer, vb.queue);
  343. /* Nobody is waiting on this buffer, return */
  344. if (!waitqueue_active(&buf->vb.done))
  345. goto unlock;
  346. list_del(&buf->vb.queue);
  347. do_gettimeofday(&buf->vb.ts);
  348. /* Fill buffer */
  349. vivi_fillbuff(dev, buf);
  350. dprintk(dev, 1, "filled buffer %p\n", buf);
  351. wake_up(&buf->vb.done);
  352. dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
  353. unlock:
  354. spin_unlock_irqrestore(&dev->slock, flags);
  355. return;
  356. }
  357. #define frames_to_ms(frames) \
  358. ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
  359. static void vivi_sleep(struct vivi_fh *fh)
  360. {
  361. struct vivi_dev *dev = fh->dev;
  362. struct vivi_dmaqueue *dma_q = &dev->vidq;
  363. int timeout;
  364. DECLARE_WAITQUEUE(wait, current);
  365. dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
  366. (unsigned long)dma_q);
  367. add_wait_queue(&dma_q->wq, &wait);
  368. if (kthread_should_stop())
  369. goto stop_task;
  370. /* Calculate time to wake up */
  371. timeout = msecs_to_jiffies(frames_to_ms(1));
  372. vivi_thread_tick(fh);
  373. schedule_timeout_interruptible(timeout);
  374. stop_task:
  375. remove_wait_queue(&dma_q->wq, &wait);
  376. try_to_freeze();
  377. }
  378. static int vivi_thread(void *data)
  379. {
  380. struct vivi_fh *fh = data;
  381. struct vivi_dev *dev = fh->dev;
  382. dprintk(dev, 1, "thread started\n");
  383. set_freezable();
  384. for (;;) {
  385. vivi_sleep(fh);
  386. if (kthread_should_stop())
  387. break;
  388. }
  389. dprintk(dev, 1, "thread: exit\n");
  390. return 0;
  391. }
  392. static int vivi_start_thread(struct vivi_fh *fh)
  393. {
  394. struct vivi_dev *dev = fh->dev;
  395. struct vivi_dmaqueue *dma_q = &dev->vidq;
  396. dma_q->frame = 0;
  397. dma_q->ini_jiffies = jiffies;
  398. dprintk(dev, 1, "%s\n", __func__);
  399. dma_q->kthread = kthread_run(vivi_thread, fh, "vivi");
  400. if (IS_ERR(dma_q->kthread)) {
  401. printk(KERN_ERR "vivi: kernel_thread() failed\n");
  402. return PTR_ERR(dma_q->kthread);
  403. }
  404. /* Wakes thread */
  405. wake_up_interruptible(&dma_q->wq);
  406. dprintk(dev, 1, "returning from %s\n", __func__);
  407. return 0;
  408. }
  409. static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
  410. {
  411. struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
  412. dprintk(dev, 1, "%s\n", __func__);
  413. /* shutdown control thread */
  414. if (dma_q->kthread) {
  415. kthread_stop(dma_q->kthread);
  416. dma_q->kthread = NULL;
  417. }
  418. }
  419. /* ------------------------------------------------------------------
  420. Videobuf operations
  421. ------------------------------------------------------------------*/
  422. static int
  423. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  424. {
  425. struct vivi_fh *fh = vq->priv_data;
  426. struct vivi_dev *dev = fh->dev;
  427. *size = fh->width*fh->height*2;
  428. if (0 == *count)
  429. *count = 32;
  430. while (*size * *count > vid_limit * 1024 * 1024)
  431. (*count)--;
  432. dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
  433. *count, *size);
  434. return 0;
  435. }
  436. static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
  437. {
  438. struct vivi_fh *fh = vq->priv_data;
  439. struct vivi_dev *dev = fh->dev;
  440. dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
  441. if (in_interrupt())
  442. BUG();
  443. videobuf_vmalloc_free(&buf->vb);
  444. dprintk(dev, 1, "free_buffer: freed\n");
  445. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  446. }
  447. #define norm_maxw() 1024
  448. #define norm_maxh() 768
  449. static int
  450. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  451. enum v4l2_field field)
  452. {
  453. struct vivi_fh *fh = vq->priv_data;
  454. struct vivi_dev *dev = fh->dev;
  455. struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
  456. int rc;
  457. dprintk(dev, 1, "%s, field=%d\n", __func__, field);
  458. BUG_ON(NULL == fh->fmt);
  459. if (fh->width < 48 || fh->width > norm_maxw() ||
  460. fh->height < 32 || fh->height > norm_maxh())
  461. return -EINVAL;
  462. buf->vb.size = fh->width*fh->height*2;
  463. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  464. return -EINVAL;
  465. /* These properties only change when queue is idle, see s_fmt */
  466. buf->fmt = fh->fmt;
  467. buf->vb.width = fh->width;
  468. buf->vb.height = fh->height;
  469. buf->vb.field = field;
  470. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  471. rc = videobuf_iolock(vq, &buf->vb, NULL);
  472. if (rc < 0)
  473. goto fail;
  474. }
  475. buf->vb.state = VIDEOBUF_PREPARED;
  476. return 0;
  477. fail:
  478. free_buffer(vq, buf);
  479. return rc;
  480. }
  481. static void
  482. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  483. {
  484. struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
  485. struct vivi_fh *fh = vq->priv_data;
  486. struct vivi_dev *dev = fh->dev;
  487. struct vivi_dmaqueue *vidq = &dev->vidq;
  488. dprintk(dev, 1, "%s\n", __func__);
  489. buf->vb.state = VIDEOBUF_QUEUED;
  490. list_add_tail(&buf->vb.queue, &vidq->active);
  491. }
  492. static void buffer_release(struct videobuf_queue *vq,
  493. struct videobuf_buffer *vb)
  494. {
  495. struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
  496. struct vivi_fh *fh = vq->priv_data;
  497. struct vivi_dev *dev = (struct vivi_dev *)fh->dev;
  498. dprintk(dev, 1, "%s\n", __func__);
  499. free_buffer(vq, buf);
  500. }
  501. static struct videobuf_queue_ops vivi_video_qops = {
  502. .buf_setup = buffer_setup,
  503. .buf_prepare = buffer_prepare,
  504. .buf_queue = buffer_queue,
  505. .buf_release = buffer_release,
  506. };
  507. /* ------------------------------------------------------------------
  508. IOCTL vidioc handling
  509. ------------------------------------------------------------------*/
  510. static int vidioc_querycap(struct file *file, void *priv,
  511. struct v4l2_capability *cap)
  512. {
  513. strcpy(cap->driver, "vivi");
  514. strcpy(cap->card, "vivi");
  515. cap->version = VIVI_VERSION;
  516. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  517. V4L2_CAP_STREAMING |
  518. V4L2_CAP_READWRITE;
  519. return 0;
  520. }
  521. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  522. struct v4l2_fmtdesc *f)
  523. {
  524. if (f->index > 0)
  525. return -EINVAL;
  526. strlcpy(f->description, format.name, sizeof(f->description));
  527. f->pixelformat = format.fourcc;
  528. return 0;
  529. }
  530. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  531. struct v4l2_format *f)
  532. {
  533. struct vivi_fh *fh = priv;
  534. f->fmt.pix.width = fh->width;
  535. f->fmt.pix.height = fh->height;
  536. f->fmt.pix.field = fh->vb_vidq.field;
  537. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  538. f->fmt.pix.bytesperline =
  539. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  540. f->fmt.pix.sizeimage =
  541. f->fmt.pix.height * f->fmt.pix.bytesperline;
  542. return (0);
  543. }
  544. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  545. struct v4l2_format *f)
  546. {
  547. struct vivi_fh *fh = priv;
  548. struct vivi_dev *dev = fh->dev;
  549. struct vivi_fmt *fmt;
  550. enum v4l2_field field;
  551. unsigned int maxw, maxh;
  552. if (format.fourcc != f->fmt.pix.pixelformat) {
  553. dprintk(dev, 1, "Fourcc format (0x%08x) invalid. "
  554. "Driver accepts only 0x%08x\n",
  555. f->fmt.pix.pixelformat, format.fourcc);
  556. return -EINVAL;
  557. }
  558. fmt = &format;
  559. field = f->fmt.pix.field;
  560. if (field == V4L2_FIELD_ANY) {
  561. field = V4L2_FIELD_INTERLACED;
  562. } else if (V4L2_FIELD_INTERLACED != field) {
  563. dprintk(dev, 1, "Field type invalid.\n");
  564. return -EINVAL;
  565. }
  566. maxw = norm_maxw();
  567. maxh = norm_maxh();
  568. f->fmt.pix.field = field;
  569. if (f->fmt.pix.height < 32)
  570. f->fmt.pix.height = 32;
  571. if (f->fmt.pix.height > maxh)
  572. f->fmt.pix.height = maxh;
  573. if (f->fmt.pix.width < 48)
  574. f->fmt.pix.width = 48;
  575. if (f->fmt.pix.width > maxw)
  576. f->fmt.pix.width = maxw;
  577. f->fmt.pix.width &= ~0x03;
  578. f->fmt.pix.bytesperline =
  579. (f->fmt.pix.width * fmt->depth) >> 3;
  580. f->fmt.pix.sizeimage =
  581. f->fmt.pix.height * f->fmt.pix.bytesperline;
  582. return 0;
  583. }
  584. /*FIXME: This seems to be generic enough to be at videodev2 */
  585. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  586. struct v4l2_format *f)
  587. {
  588. struct vivi_fh *fh = priv;
  589. struct videobuf_queue *q = &fh->vb_vidq;
  590. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  591. if (ret < 0)
  592. return (ret);
  593. mutex_lock(&q->vb_lock);
  594. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  595. dprintk(fh->dev, 1, "%s queue busy\n", __func__);
  596. ret = -EBUSY;
  597. goto out;
  598. }
  599. fh->fmt = &format;
  600. fh->width = f->fmt.pix.width;
  601. fh->height = f->fmt.pix.height;
  602. fh->vb_vidq.field = f->fmt.pix.field;
  603. fh->type = f->type;
  604. ret = 0;
  605. out:
  606. mutex_unlock(&q->vb_lock);
  607. return (ret);
  608. }
  609. static int vidioc_reqbufs(struct file *file, void *priv,
  610. struct v4l2_requestbuffers *p)
  611. {
  612. struct vivi_fh *fh = priv;
  613. return (videobuf_reqbufs(&fh->vb_vidq, p));
  614. }
  615. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  616. {
  617. struct vivi_fh *fh = priv;
  618. return (videobuf_querybuf(&fh->vb_vidq, p));
  619. }
  620. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  621. {
  622. struct vivi_fh *fh = priv;
  623. return (videobuf_qbuf(&fh->vb_vidq, p));
  624. }
  625. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  626. {
  627. struct vivi_fh *fh = priv;
  628. return (videobuf_dqbuf(&fh->vb_vidq, p,
  629. file->f_flags & O_NONBLOCK));
  630. }
  631. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  632. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  633. {
  634. struct vivi_fh *fh = priv;
  635. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  636. }
  637. #endif
  638. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  639. {
  640. struct vivi_fh *fh = priv;
  641. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  642. return -EINVAL;
  643. if (i != fh->type)
  644. return -EINVAL;
  645. return videobuf_streamon(&fh->vb_vidq);
  646. }
  647. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  648. {
  649. struct vivi_fh *fh = priv;
  650. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  651. return -EINVAL;
  652. if (i != fh->type)
  653. return -EINVAL;
  654. return videobuf_streamoff(&fh->vb_vidq);
  655. }
  656. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
  657. {
  658. return 0;
  659. }
  660. /* only one input in this sample driver */
  661. static int vidioc_enum_input(struct file *file, void *priv,
  662. struct v4l2_input *inp)
  663. {
  664. if (inp->index != 0)
  665. return -EINVAL;
  666. inp->type = V4L2_INPUT_TYPE_CAMERA;
  667. inp->std = V4L2_STD_525_60;
  668. strcpy(inp->name, "Camera");
  669. return (0);
  670. }
  671. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  672. {
  673. *i = 0;
  674. return (0);
  675. }
  676. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  677. {
  678. if (i > 0)
  679. return -EINVAL;
  680. return (0);
  681. }
  682. /* --- controls ---------------------------------------------- */
  683. static int vidioc_queryctrl(struct file *file, void *priv,
  684. struct v4l2_queryctrl *qc)
  685. {
  686. int i;
  687. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  688. if (qc->id && qc->id == vivi_qctrl[i].id) {
  689. memcpy(qc, &(vivi_qctrl[i]),
  690. sizeof(*qc));
  691. return (0);
  692. }
  693. return -EINVAL;
  694. }
  695. static int vidioc_g_ctrl(struct file *file, void *priv,
  696. struct v4l2_control *ctrl)
  697. {
  698. int i;
  699. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  700. if (ctrl->id == vivi_qctrl[i].id) {
  701. ctrl->value = qctl_regs[i];
  702. return (0);
  703. }
  704. return -EINVAL;
  705. }
  706. static int vidioc_s_ctrl(struct file *file, void *priv,
  707. struct v4l2_control *ctrl)
  708. {
  709. int i;
  710. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  711. if (ctrl->id == vivi_qctrl[i].id) {
  712. if (ctrl->value < vivi_qctrl[i].minimum
  713. || ctrl->value > vivi_qctrl[i].maximum) {
  714. return (-ERANGE);
  715. }
  716. qctl_regs[i] = ctrl->value;
  717. return (0);
  718. }
  719. return -EINVAL;
  720. }
  721. /* ------------------------------------------------------------------
  722. File operations for the device
  723. ------------------------------------------------------------------*/
  724. #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
  725. static int vivi_open(struct inode *inode, struct file *file)
  726. {
  727. int minor = iminor(inode);
  728. struct vivi_dev *dev;
  729. struct vivi_fh *fh = NULL;
  730. int i;
  731. int retval = 0;
  732. printk(KERN_DEBUG "vivi: open called (minor=%d)\n", minor);
  733. list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
  734. if (dev->vfd->minor == minor)
  735. goto found;
  736. return -ENODEV;
  737. found:
  738. mutex_lock(&dev->mutex);
  739. dev->users++;
  740. if (dev->users > 1) {
  741. dev->users--;
  742. retval = -EBUSY;
  743. goto unlock;
  744. }
  745. dprintk(dev, 1, "open minor=%d type=%s users=%d\n", minor,
  746. v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
  747. /* allocate + initialize per filehandle data */
  748. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  749. if (NULL == fh) {
  750. dev->users--;
  751. retval = -ENOMEM;
  752. goto unlock;
  753. }
  754. unlock:
  755. mutex_unlock(&dev->mutex);
  756. if (retval)
  757. return retval;
  758. file->private_data = fh;
  759. fh->dev = dev;
  760. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  761. fh->fmt = &format;
  762. fh->width = 640;
  763. fh->height = 480;
  764. /* Put all controls at a sane state */
  765. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  766. qctl_regs[i] = vivi_qctrl[i].default_value;
  767. /* Resets frame counters */
  768. dev->h = 0;
  769. dev->m = 0;
  770. dev->s = 0;
  771. dev->ms = 0;
  772. dev->mv_count = 0;
  773. dev->jiffies = jiffies;
  774. sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
  775. dev->h, dev->m, dev->s, dev->ms);
  776. videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
  777. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  778. sizeof(struct vivi_buffer), fh);
  779. vivi_start_thread(fh);
  780. return 0;
  781. }
  782. static ssize_t
  783. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  784. {
  785. struct vivi_fh *fh = file->private_data;
  786. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  787. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  788. file->f_flags & O_NONBLOCK);
  789. }
  790. return 0;
  791. }
  792. static unsigned int
  793. vivi_poll(struct file *file, struct poll_table_struct *wait)
  794. {
  795. struct vivi_fh *fh = file->private_data;
  796. struct vivi_dev *dev = fh->dev;
  797. struct videobuf_queue *q = &fh->vb_vidq;
  798. dprintk(dev, 1, "%s\n", __func__);
  799. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  800. return POLLERR;
  801. return videobuf_poll_stream(file, q, wait);
  802. }
  803. static int vivi_close(struct inode *inode, struct file *file)
  804. {
  805. struct vivi_fh *fh = file->private_data;
  806. struct vivi_dev *dev = fh->dev;
  807. struct vivi_dmaqueue *vidq = &dev->vidq;
  808. int minor = iminor(inode);
  809. vivi_stop_thread(vidq);
  810. videobuf_stop(&fh->vb_vidq);
  811. videobuf_mmap_free(&fh->vb_vidq);
  812. kfree(fh);
  813. mutex_lock(&dev->mutex);
  814. dev->users--;
  815. mutex_unlock(&dev->mutex);
  816. dprintk(dev, 1, "close called (minor=%d, users=%d)\n",
  817. minor, dev->users);
  818. return 0;
  819. }
  820. static int vivi_release(void)
  821. {
  822. struct vivi_dev *dev;
  823. struct list_head *list;
  824. while (!list_empty(&vivi_devlist)) {
  825. list = vivi_devlist.next;
  826. list_del(list);
  827. dev = list_entry(list, struct vivi_dev, vivi_devlist);
  828. if (-1 != dev->vfd->minor) {
  829. video_unregister_device(dev->vfd);
  830. printk(KERN_INFO "%s: /dev/video%d unregistered.\n",
  831. MODULE_NAME, dev->vfd->minor);
  832. } else {
  833. video_device_release(dev->vfd);
  834. printk(KERN_INFO "%s: /dev/video%d released.\n",
  835. MODULE_NAME, dev->vfd->minor);
  836. }
  837. kfree(dev);
  838. }
  839. return 0;
  840. }
  841. static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
  842. {
  843. struct vivi_fh *fh = file->private_data;
  844. struct vivi_dev *dev = fh->dev;
  845. int ret;
  846. dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  847. ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  848. dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
  849. (unsigned long)vma->vm_start,
  850. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  851. ret);
  852. return ret;
  853. }
  854. static const struct file_operations vivi_fops = {
  855. .owner = THIS_MODULE,
  856. .open = vivi_open,
  857. .release = vivi_close,
  858. .read = vivi_read,
  859. .poll = vivi_poll,
  860. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  861. .compat_ioctl = v4l_compat_ioctl32,
  862. .mmap = vivi_mmap,
  863. .llseek = no_llseek,
  864. };
  865. static struct video_device vivi_template = {
  866. .name = "vivi",
  867. .type = VID_TYPE_CAPTURE,
  868. .fops = &vivi_fops,
  869. .minor = -1,
  870. .release = video_device_release,
  871. .vidioc_querycap = vidioc_querycap,
  872. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  873. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  874. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  875. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  876. .vidioc_reqbufs = vidioc_reqbufs,
  877. .vidioc_querybuf = vidioc_querybuf,
  878. .vidioc_qbuf = vidioc_qbuf,
  879. .vidioc_dqbuf = vidioc_dqbuf,
  880. .vidioc_s_std = vidioc_s_std,
  881. .vidioc_enum_input = vidioc_enum_input,
  882. .vidioc_g_input = vidioc_g_input,
  883. .vidioc_s_input = vidioc_s_input,
  884. .vidioc_queryctrl = vidioc_queryctrl,
  885. .vidioc_g_ctrl = vidioc_g_ctrl,
  886. .vidioc_s_ctrl = vidioc_s_ctrl,
  887. .vidioc_streamon = vidioc_streamon,
  888. .vidioc_streamoff = vidioc_streamoff,
  889. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  890. .vidiocgmbuf = vidiocgmbuf,
  891. #endif
  892. .tvnorms = V4L2_STD_525_60,
  893. .current_norm = V4L2_STD_NTSC_M,
  894. };
  895. /* -----------------------------------------------------------------
  896. Initialization and module stuff
  897. ------------------------------------------------------------------*/
  898. static int __init vivi_init(void)
  899. {
  900. int ret = -ENOMEM, i;
  901. struct vivi_dev *dev;
  902. struct video_device *vfd;
  903. for (i = 0; i < n_devs; i++) {
  904. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  905. if (NULL == dev)
  906. break;
  907. list_add_tail(&dev->vivi_devlist, &vivi_devlist);
  908. /* init video dma queues */
  909. INIT_LIST_HEAD(&dev->vidq.active);
  910. init_waitqueue_head(&dev->vidq.wq);
  911. /* initialize locks */
  912. spin_lock_init(&dev->slock);
  913. mutex_init(&dev->mutex);
  914. vfd = video_device_alloc();
  915. if (NULL == vfd)
  916. break;
  917. *vfd = vivi_template;
  918. ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
  919. if (ret < 0)
  920. break;
  921. snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
  922. vivi_template.name, vfd->minor);
  923. if (video_nr >= 0)
  924. video_nr++;
  925. dev->vfd = vfd;
  926. printk(KERN_INFO "%s: V4L2 device registered as /dev/video%d\n",
  927. MODULE_NAME, vfd->minor);
  928. }
  929. if (ret < 0) {
  930. vivi_release();
  931. printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
  932. } else
  933. printk(KERN_INFO "Video Technology Magazine Virtual Video "
  934. "Capture Board ver %u.%u.%u successfully loaded.\n",
  935. (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF,
  936. VIVI_VERSION & 0xFF);
  937. return ret;
  938. }
  939. static void __exit vivi_exit(void)
  940. {
  941. vivi_release();
  942. }
  943. module_init(vivi_init);
  944. module_exit(vivi_exit);
  945. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  946. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  947. MODULE_LICENSE("Dual BSD/GPL");
  948. module_param(video_nr, int, 0);
  949. MODULE_PARM_DESC(video_nr, "video iminor start number");
  950. module_param(n_devs, int, 0);
  951. MODULE_PARM_DESC(n_devs, "number of video devices to create");
  952. module_param_named(debug, vivi_template.debug, int, 0444);
  953. MODULE_PARM_DESC(debug, "activates debug info");
  954. module_param(vid_limit, int, 0644);
  955. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");