vivi.c 27 KB

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