vivi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski
  11. * Copyright (c) 2010 Samsung Electronics
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the BSD Licence, GNU General Public License
  15. * as published by the Free Software Foundation; either version 2 of the
  16. * License, or (at your option) any later version
  17. */
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/font.h>
  25. #include <linux/mutex.h>
  26. #include <linux/videodev2.h>
  27. #include <linux/kthread.h>
  28. #include <linux/freezer.h>
  29. #include <media/videobuf2-vmalloc.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-ctrls.h>
  33. #include <media/v4l2-fh.h>
  34. #include <media/v4l2-event.h>
  35. #include <media/v4l2-common.h>
  36. #define VIVI_MODULE_NAME "vivi"
  37. /* Wake up at about 30 fps */
  38. #define WAKE_NUMERATOR 30
  39. #define WAKE_DENOMINATOR 1001
  40. #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
  41. #define MAX_WIDTH 1920
  42. #define MAX_HEIGHT 1200
  43. #define VIVI_VERSION "0.8.1"
  44. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  45. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. MODULE_VERSION(VIVI_VERSION);
  48. static unsigned video_nr = -1;
  49. module_param(video_nr, uint, 0644);
  50. MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
  51. static unsigned n_devs = 1;
  52. module_param(n_devs, uint, 0644);
  53. MODULE_PARM_DESC(n_devs, "number of video devices to create");
  54. static unsigned debug;
  55. module_param(debug, uint, 0644);
  56. MODULE_PARM_DESC(debug, "activates debug info");
  57. static unsigned int vid_limit = 16;
  58. module_param(vid_limit, uint, 0644);
  59. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  60. /* Global font descriptor */
  61. static const u8 *font8x16;
  62. #define dprintk(dev, level, fmt, arg...) \
  63. v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
  64. /* ------------------------------------------------------------------
  65. Basic structures
  66. ------------------------------------------------------------------*/
  67. struct vivi_fmt {
  68. char *name;
  69. u32 fourcc; /* v4l2 format id */
  70. int depth;
  71. };
  72. static struct vivi_fmt formats[] = {
  73. {
  74. .name = "4:2:2, packed, YUYV",
  75. .fourcc = V4L2_PIX_FMT_YUYV,
  76. .depth = 16,
  77. },
  78. {
  79. .name = "4:2:2, packed, UYVY",
  80. .fourcc = V4L2_PIX_FMT_UYVY,
  81. .depth = 16,
  82. },
  83. {
  84. .name = "RGB565 (LE)",
  85. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  86. .depth = 16,
  87. },
  88. {
  89. .name = "RGB565 (BE)",
  90. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  91. .depth = 16,
  92. },
  93. {
  94. .name = "RGB555 (LE)",
  95. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
  96. .depth = 16,
  97. },
  98. {
  99. .name = "RGB555 (BE)",
  100. .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
  101. .depth = 16,
  102. },
  103. };
  104. static struct vivi_fmt *get_format(struct v4l2_format *f)
  105. {
  106. struct vivi_fmt *fmt;
  107. unsigned int k;
  108. for (k = 0; k < ARRAY_SIZE(formats); k++) {
  109. fmt = &formats[k];
  110. if (fmt->fourcc == f->fmt.pix.pixelformat)
  111. break;
  112. }
  113. if (k == ARRAY_SIZE(formats))
  114. return NULL;
  115. return &formats[k];
  116. }
  117. /* buffer for one video frame */
  118. struct vivi_buffer {
  119. /* common v4l buffer stuff -- must be first */
  120. struct vb2_buffer vb;
  121. struct list_head list;
  122. struct vivi_fmt *fmt;
  123. };
  124. struct vivi_dmaqueue {
  125. struct list_head active;
  126. /* thread for generating video stream*/
  127. struct task_struct *kthread;
  128. wait_queue_head_t wq;
  129. /* Counters to control fps rate */
  130. int frame;
  131. int ini_jiffies;
  132. };
  133. static LIST_HEAD(vivi_devlist);
  134. struct vivi_dev {
  135. struct list_head vivi_devlist;
  136. struct v4l2_device v4l2_dev;
  137. struct v4l2_ctrl_handler ctrl_handler;
  138. /* controls */
  139. struct v4l2_ctrl *brightness;
  140. struct v4l2_ctrl *contrast;
  141. struct v4l2_ctrl *saturation;
  142. struct v4l2_ctrl *hue;
  143. struct {
  144. /* autogain/gain cluster */
  145. struct v4l2_ctrl *autogain;
  146. struct v4l2_ctrl *gain;
  147. };
  148. struct v4l2_ctrl *volume;
  149. struct v4l2_ctrl *button;
  150. struct v4l2_ctrl *boolean;
  151. struct v4l2_ctrl *int32;
  152. struct v4l2_ctrl *int64;
  153. struct v4l2_ctrl *menu;
  154. struct v4l2_ctrl *string;
  155. struct v4l2_ctrl *bitmask;
  156. spinlock_t slock;
  157. struct mutex mutex;
  158. /* various device info */
  159. struct video_device *vfd;
  160. struct vivi_dmaqueue vidq;
  161. /* Several counters */
  162. unsigned ms;
  163. unsigned long jiffies;
  164. unsigned button_pressed;
  165. int mv_count; /* Controls bars movement */
  166. /* Input Number */
  167. int input;
  168. /* video capture */
  169. struct vivi_fmt *fmt;
  170. unsigned int width, height;
  171. struct vb2_queue vb_vidq;
  172. enum v4l2_field field;
  173. unsigned int field_count;
  174. u8 bars[9][3];
  175. u8 line[MAX_WIDTH * 4];
  176. };
  177. /* ------------------------------------------------------------------
  178. DMA and thread functions
  179. ------------------------------------------------------------------*/
  180. /* Bars and Colors should match positions */
  181. enum colors {
  182. WHITE,
  183. AMBER,
  184. CYAN,
  185. GREEN,
  186. MAGENTA,
  187. RED,
  188. BLUE,
  189. BLACK,
  190. TEXT_BLACK,
  191. };
  192. /* R G B */
  193. #define COLOR_WHITE {204, 204, 204}
  194. #define COLOR_AMBER {208, 208, 0}
  195. #define COLOR_CYAN { 0, 206, 206}
  196. #define COLOR_GREEN { 0, 239, 0}
  197. #define COLOR_MAGENTA {239, 0, 239}
  198. #define COLOR_RED {205, 0, 0}
  199. #define COLOR_BLUE { 0, 0, 255}
  200. #define COLOR_BLACK { 0, 0, 0}
  201. struct bar_std {
  202. u8 bar[9][3];
  203. };
  204. /* Maximum number of bars are 10 - otherwise, the input print code
  205. should be modified */
  206. static struct bar_std bars[] = {
  207. { /* Standard ITU-R color bar sequence */
  208. { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
  209. COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
  210. }, {
  211. { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
  212. COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
  213. }, {
  214. { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
  215. COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
  216. }, {
  217. { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
  218. COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
  219. },
  220. };
  221. #define NUM_INPUTS ARRAY_SIZE(bars)
  222. #define TO_Y(r, g, b) \
  223. (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
  224. /* RGB to V(Cr) Color transform */
  225. #define TO_V(r, g, b) \
  226. (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
  227. /* RGB to U(Cb) Color transform */
  228. #define TO_U(r, g, b) \
  229. (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
  230. /* precalculate color bar values to speed up rendering */
  231. static void precalculate_bars(struct vivi_dev *dev)
  232. {
  233. u8 r, g, b;
  234. int k, is_yuv;
  235. for (k = 0; k < 9; k++) {
  236. r = bars[dev->input].bar[k][0];
  237. g = bars[dev->input].bar[k][1];
  238. b = bars[dev->input].bar[k][2];
  239. is_yuv = 0;
  240. switch (dev->fmt->fourcc) {
  241. case V4L2_PIX_FMT_YUYV:
  242. case V4L2_PIX_FMT_UYVY:
  243. is_yuv = 1;
  244. break;
  245. case V4L2_PIX_FMT_RGB565:
  246. case V4L2_PIX_FMT_RGB565X:
  247. r >>= 3;
  248. g >>= 2;
  249. b >>= 3;
  250. break;
  251. case V4L2_PIX_FMT_RGB555:
  252. case V4L2_PIX_FMT_RGB555X:
  253. r >>= 3;
  254. g >>= 3;
  255. b >>= 3;
  256. break;
  257. }
  258. if (is_yuv) {
  259. dev->bars[k][0] = TO_Y(r, g, b); /* Luma */
  260. dev->bars[k][1] = TO_U(r, g, b); /* Cb */
  261. dev->bars[k][2] = TO_V(r, g, b); /* Cr */
  262. } else {
  263. dev->bars[k][0] = r;
  264. dev->bars[k][1] = g;
  265. dev->bars[k][2] = b;
  266. }
  267. }
  268. }
  269. #define TSTAMP_MIN_Y 24
  270. #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
  271. #define TSTAMP_INPUT_X 10
  272. #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
  273. static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
  274. {
  275. u8 r_y, g_u, b_v;
  276. int color;
  277. u8 *p;
  278. r_y = dev->bars[colorpos][0]; /* R or precalculated Y */
  279. g_u = dev->bars[colorpos][1]; /* G or precalculated U */
  280. b_v = dev->bars[colorpos][2]; /* B or precalculated V */
  281. for (color = 0; color < 4; color++) {
  282. p = buf + color;
  283. switch (dev->fmt->fourcc) {
  284. case V4L2_PIX_FMT_YUYV:
  285. switch (color) {
  286. case 0:
  287. case 2:
  288. *p = r_y;
  289. break;
  290. case 1:
  291. *p = g_u;
  292. break;
  293. case 3:
  294. *p = b_v;
  295. break;
  296. }
  297. break;
  298. case V4L2_PIX_FMT_UYVY:
  299. switch (color) {
  300. case 1:
  301. case 3:
  302. *p = r_y;
  303. break;
  304. case 0:
  305. *p = g_u;
  306. break;
  307. case 2:
  308. *p = b_v;
  309. break;
  310. }
  311. break;
  312. case V4L2_PIX_FMT_RGB565:
  313. switch (color) {
  314. case 0:
  315. case 2:
  316. *p = (g_u << 5) | b_v;
  317. break;
  318. case 1:
  319. case 3:
  320. *p = (r_y << 3) | (g_u >> 3);
  321. break;
  322. }
  323. break;
  324. case V4L2_PIX_FMT_RGB565X:
  325. switch (color) {
  326. case 0:
  327. case 2:
  328. *p = (r_y << 3) | (g_u >> 3);
  329. break;
  330. case 1:
  331. case 3:
  332. *p = (g_u << 5) | b_v;
  333. break;
  334. }
  335. break;
  336. case V4L2_PIX_FMT_RGB555:
  337. switch (color) {
  338. case 0:
  339. case 2:
  340. *p = (g_u << 5) | b_v;
  341. break;
  342. case 1:
  343. case 3:
  344. *p = (r_y << 2) | (g_u >> 3);
  345. break;
  346. }
  347. break;
  348. case V4L2_PIX_FMT_RGB555X:
  349. switch (color) {
  350. case 0:
  351. case 2:
  352. *p = (r_y << 2) | (g_u >> 3);
  353. break;
  354. case 1:
  355. case 3:
  356. *p = (g_u << 5) | b_v;
  357. break;
  358. }
  359. break;
  360. }
  361. }
  362. }
  363. static void precalculate_line(struct vivi_dev *dev)
  364. {
  365. int w;
  366. for (w = 0; w < dev->width * 2; w += 2) {
  367. int colorpos = (w / (dev->width / 8) % 8);
  368. gen_twopix(dev, dev->line + w * 2, colorpos);
  369. }
  370. }
  371. static void gen_text(struct vivi_dev *dev, char *basep,
  372. int y, int x, char *text)
  373. {
  374. int line;
  375. /* Checks if it is possible to show string */
  376. if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
  377. return;
  378. /* Print stream time */
  379. for (line = y; line < y + 16; line++) {
  380. int j = 0;
  381. char *pos = basep + line * dev->width * 2 + x * 2;
  382. char *s;
  383. for (s = text; *s; s++) {
  384. u8 chr = font8x16[*s * 16 + line - y];
  385. int i;
  386. for (i = 0; i < 7; i++, j++) {
  387. /* Draw white font on black background */
  388. if (chr & (1 << (7 - i)))
  389. gen_twopix(dev, pos + j * 2, WHITE);
  390. else
  391. gen_twopix(dev, pos + j * 2, TEXT_BLACK);
  392. }
  393. }
  394. }
  395. }
  396. static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
  397. {
  398. int wmax = dev->width;
  399. int hmax = dev->height;
  400. struct timeval ts;
  401. void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
  402. unsigned ms;
  403. char str[100];
  404. int h, line = 1;
  405. s32 gain;
  406. if (!vbuf)
  407. return;
  408. for (h = 0; h < hmax; h++)
  409. memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
  410. /* Updates stream time */
  411. dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
  412. dev->jiffies = jiffies;
  413. ms = dev->ms;
  414. snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
  415. (ms / (60 * 60 * 1000)) % 24,
  416. (ms / (60 * 1000)) % 60,
  417. (ms / 1000) % 60,
  418. ms % 1000);
  419. gen_text(dev, vbuf, line++ * 16, 16, str);
  420. snprintf(str, sizeof(str), " %dx%d, input %d ",
  421. dev->width, dev->height, dev->input);
  422. gen_text(dev, vbuf, line++ * 16, 16, str);
  423. gain = v4l2_ctrl_g_ctrl(dev->gain);
  424. mutex_lock(&dev->ctrl_handler.lock);
  425. snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
  426. dev->brightness->cur.val,
  427. dev->contrast->cur.val,
  428. dev->saturation->cur.val,
  429. dev->hue->cur.val);
  430. gen_text(dev, vbuf, line++ * 16, 16, str);
  431. snprintf(str, sizeof(str), " autogain %d, gain %3d, volume %3d ",
  432. dev->autogain->cur.val, gain, dev->volume->cur.val);
  433. gen_text(dev, vbuf, line++ * 16, 16, str);
  434. snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
  435. dev->int32->cur.val,
  436. dev->int64->cur.val64,
  437. dev->bitmask->cur.val);
  438. gen_text(dev, vbuf, line++ * 16, 16, str);
  439. snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
  440. dev->boolean->cur.val,
  441. dev->menu->qmenu[dev->menu->cur.val],
  442. dev->string->cur.string);
  443. mutex_unlock(&dev->ctrl_handler.lock);
  444. gen_text(dev, vbuf, line++ * 16, 16, str);
  445. if (dev->button_pressed) {
  446. dev->button_pressed--;
  447. snprintf(str, sizeof(str), " button pressed!");
  448. gen_text(dev, vbuf, line++ * 16, 16, str);
  449. }
  450. dev->mv_count += 2;
  451. buf->vb.v4l2_buf.field = dev->field;
  452. dev->field_count++;
  453. buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
  454. do_gettimeofday(&ts);
  455. buf->vb.v4l2_buf.timestamp = ts;
  456. }
  457. static void vivi_thread_tick(struct vivi_dev *dev)
  458. {
  459. struct vivi_dmaqueue *dma_q = &dev->vidq;
  460. struct vivi_buffer *buf;
  461. unsigned long flags = 0;
  462. dprintk(dev, 1, "Thread tick\n");
  463. spin_lock_irqsave(&dev->slock, flags);
  464. if (list_empty(&dma_q->active)) {
  465. dprintk(dev, 1, "No active queue to serve\n");
  466. spin_unlock_irqrestore(&dev->slock, flags);
  467. return;
  468. }
  469. buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
  470. list_del(&buf->list);
  471. spin_unlock_irqrestore(&dev->slock, flags);
  472. do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
  473. /* Fill buffer */
  474. vivi_fillbuff(dev, buf);
  475. dprintk(dev, 1, "filled buffer %p\n", buf);
  476. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  477. dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
  478. }
  479. #define frames_to_ms(frames) \
  480. ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
  481. static void vivi_sleep(struct vivi_dev *dev)
  482. {
  483. struct vivi_dmaqueue *dma_q = &dev->vidq;
  484. int timeout;
  485. DECLARE_WAITQUEUE(wait, current);
  486. dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
  487. (unsigned long)dma_q);
  488. add_wait_queue(&dma_q->wq, &wait);
  489. if (kthread_should_stop())
  490. goto stop_task;
  491. /* Calculate time to wake up */
  492. timeout = msecs_to_jiffies(frames_to_ms(1));
  493. vivi_thread_tick(dev);
  494. schedule_timeout_interruptible(timeout);
  495. stop_task:
  496. remove_wait_queue(&dma_q->wq, &wait);
  497. try_to_freeze();
  498. }
  499. static int vivi_thread(void *data)
  500. {
  501. struct vivi_dev *dev = data;
  502. dprintk(dev, 1, "thread started\n");
  503. set_freezable();
  504. for (;;) {
  505. vivi_sleep(dev);
  506. if (kthread_should_stop())
  507. break;
  508. }
  509. dprintk(dev, 1, "thread: exit\n");
  510. return 0;
  511. }
  512. static int vivi_start_generating(struct vivi_dev *dev)
  513. {
  514. struct vivi_dmaqueue *dma_q = &dev->vidq;
  515. dprintk(dev, 1, "%s\n", __func__);
  516. /* Resets frame counters */
  517. dev->ms = 0;
  518. dev->mv_count = 0;
  519. dev->jiffies = jiffies;
  520. dma_q->frame = 0;
  521. dma_q->ini_jiffies = jiffies;
  522. dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name);
  523. if (IS_ERR(dma_q->kthread)) {
  524. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  525. return PTR_ERR(dma_q->kthread);
  526. }
  527. /* Wakes thread */
  528. wake_up_interruptible(&dma_q->wq);
  529. dprintk(dev, 1, "returning from %s\n", __func__);
  530. return 0;
  531. }
  532. static void vivi_stop_generating(struct vivi_dev *dev)
  533. {
  534. struct vivi_dmaqueue *dma_q = &dev->vidq;
  535. dprintk(dev, 1, "%s\n", __func__);
  536. /* shutdown control thread */
  537. if (dma_q->kthread) {
  538. kthread_stop(dma_q->kthread);
  539. dma_q->kthread = NULL;
  540. }
  541. /*
  542. * Typical driver might need to wait here until dma engine stops.
  543. * In this case we can abort imiedetly, so it's just a noop.
  544. */
  545. /* Release all active buffers */
  546. while (!list_empty(&dma_q->active)) {
  547. struct vivi_buffer *buf;
  548. buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
  549. list_del(&buf->list);
  550. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  551. dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
  552. }
  553. }
  554. /* ------------------------------------------------------------------
  555. Videobuf operations
  556. ------------------------------------------------------------------*/
  557. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  558. unsigned int *nbuffers, unsigned int *nplanes,
  559. unsigned int sizes[], void *alloc_ctxs[])
  560. {
  561. struct vivi_dev *dev = vb2_get_drv_priv(vq);
  562. unsigned long size;
  563. size = dev->width * dev->height * 2;
  564. if (0 == *nbuffers)
  565. *nbuffers = 32;
  566. while (size * *nbuffers > vid_limit * 1024 * 1024)
  567. (*nbuffers)--;
  568. *nplanes = 1;
  569. sizes[0] = size;
  570. /*
  571. * videobuf2-vmalloc allocator is context-less so no need to set
  572. * alloc_ctxs array.
  573. */
  574. dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
  575. *nbuffers, size);
  576. return 0;
  577. }
  578. static int buffer_init(struct vb2_buffer *vb)
  579. {
  580. struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  581. BUG_ON(NULL == dev->fmt);
  582. /*
  583. * This callback is called once per buffer, after its allocation.
  584. *
  585. * Vivi does not allow changing format during streaming, but it is
  586. * possible to do so when streaming is paused (i.e. in streamoff state).
  587. * Buffers however are not freed when going into streamoff and so
  588. * buffer size verification has to be done in buffer_prepare, on each
  589. * qbuf.
  590. * It would be best to move verification code here to buf_init and
  591. * s_fmt though.
  592. */
  593. return 0;
  594. }
  595. static int buffer_prepare(struct vb2_buffer *vb)
  596. {
  597. struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  598. struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
  599. unsigned long size;
  600. dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
  601. BUG_ON(NULL == dev->fmt);
  602. /*
  603. * Theses properties only change when queue is idle, see s_fmt.
  604. * The below checks should not be performed here, on each
  605. * buffer_prepare (i.e. on each qbuf). Most of the code in this function
  606. * should thus be moved to buffer_init and s_fmt.
  607. */
  608. if (dev->width < 48 || dev->width > MAX_WIDTH ||
  609. dev->height < 32 || dev->height > MAX_HEIGHT)
  610. return -EINVAL;
  611. size = dev->width * dev->height * 2;
  612. if (vb2_plane_size(vb, 0) < size) {
  613. dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
  614. __func__, vb2_plane_size(vb, 0), size);
  615. return -EINVAL;
  616. }
  617. vb2_set_plane_payload(&buf->vb, 0, size);
  618. buf->fmt = dev->fmt;
  619. precalculate_bars(dev);
  620. precalculate_line(dev);
  621. return 0;
  622. }
  623. static int buffer_finish(struct vb2_buffer *vb)
  624. {
  625. struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  626. dprintk(dev, 1, "%s\n", __func__);
  627. return 0;
  628. }
  629. static void buffer_cleanup(struct vb2_buffer *vb)
  630. {
  631. struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  632. dprintk(dev, 1, "%s\n", __func__);
  633. }
  634. static void buffer_queue(struct vb2_buffer *vb)
  635. {
  636. struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  637. struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
  638. struct vivi_dmaqueue *vidq = &dev->vidq;
  639. unsigned long flags = 0;
  640. dprintk(dev, 1, "%s\n", __func__);
  641. spin_lock_irqsave(&dev->slock, flags);
  642. list_add_tail(&buf->list, &vidq->active);
  643. spin_unlock_irqrestore(&dev->slock, flags);
  644. }
  645. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  646. {
  647. struct vivi_dev *dev = vb2_get_drv_priv(vq);
  648. dprintk(dev, 1, "%s\n", __func__);
  649. return vivi_start_generating(dev);
  650. }
  651. /* abort streaming and wait for last buffer */
  652. static int stop_streaming(struct vb2_queue *vq)
  653. {
  654. struct vivi_dev *dev = vb2_get_drv_priv(vq);
  655. dprintk(dev, 1, "%s\n", __func__);
  656. vivi_stop_generating(dev);
  657. return 0;
  658. }
  659. static void vivi_lock(struct vb2_queue *vq)
  660. {
  661. struct vivi_dev *dev = vb2_get_drv_priv(vq);
  662. mutex_lock(&dev->mutex);
  663. }
  664. static void vivi_unlock(struct vb2_queue *vq)
  665. {
  666. struct vivi_dev *dev = vb2_get_drv_priv(vq);
  667. mutex_unlock(&dev->mutex);
  668. }
  669. static struct vb2_ops vivi_video_qops = {
  670. .queue_setup = queue_setup,
  671. .buf_init = buffer_init,
  672. .buf_prepare = buffer_prepare,
  673. .buf_finish = buffer_finish,
  674. .buf_cleanup = buffer_cleanup,
  675. .buf_queue = buffer_queue,
  676. .start_streaming = start_streaming,
  677. .stop_streaming = stop_streaming,
  678. .wait_prepare = vivi_unlock,
  679. .wait_finish = vivi_lock,
  680. };
  681. /* ------------------------------------------------------------------
  682. IOCTL vidioc handling
  683. ------------------------------------------------------------------*/
  684. static int vidioc_querycap(struct file *file, void *priv,
  685. struct v4l2_capability *cap)
  686. {
  687. struct vivi_dev *dev = video_drvdata(file);
  688. strcpy(cap->driver, "vivi");
  689. strcpy(cap->card, "vivi");
  690. strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
  691. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  692. V4L2_CAP_READWRITE;
  693. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  694. return 0;
  695. }
  696. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  697. struct v4l2_fmtdesc *f)
  698. {
  699. struct vivi_fmt *fmt;
  700. if (f->index >= ARRAY_SIZE(formats))
  701. return -EINVAL;
  702. fmt = &formats[f->index];
  703. strlcpy(f->description, fmt->name, sizeof(f->description));
  704. f->pixelformat = fmt->fourcc;
  705. return 0;
  706. }
  707. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  708. struct v4l2_format *f)
  709. {
  710. struct vivi_dev *dev = video_drvdata(file);
  711. f->fmt.pix.width = dev->width;
  712. f->fmt.pix.height = dev->height;
  713. f->fmt.pix.field = dev->field;
  714. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  715. f->fmt.pix.bytesperline =
  716. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  717. f->fmt.pix.sizeimage =
  718. f->fmt.pix.height * f->fmt.pix.bytesperline;
  719. if (dev->fmt->fourcc == V4L2_PIX_FMT_YUYV ||
  720. dev->fmt->fourcc == V4L2_PIX_FMT_UYVY)
  721. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  722. else
  723. f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  724. return 0;
  725. }
  726. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  727. struct v4l2_format *f)
  728. {
  729. struct vivi_dev *dev = video_drvdata(file);
  730. struct vivi_fmt *fmt;
  731. enum v4l2_field field;
  732. fmt = get_format(f);
  733. if (!fmt) {
  734. dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
  735. f->fmt.pix.pixelformat);
  736. return -EINVAL;
  737. }
  738. field = f->fmt.pix.field;
  739. if (field == V4L2_FIELD_ANY) {
  740. field = V4L2_FIELD_INTERLACED;
  741. } else if (V4L2_FIELD_INTERLACED != field) {
  742. dprintk(dev, 1, "Field type invalid.\n");
  743. return -EINVAL;
  744. }
  745. f->fmt.pix.field = field;
  746. v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
  747. &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
  748. f->fmt.pix.bytesperline =
  749. (f->fmt.pix.width * fmt->depth) >> 3;
  750. f->fmt.pix.sizeimage =
  751. f->fmt.pix.height * f->fmt.pix.bytesperline;
  752. if (fmt->fourcc == V4L2_PIX_FMT_YUYV ||
  753. fmt->fourcc == V4L2_PIX_FMT_UYVY)
  754. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  755. else
  756. f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  757. return 0;
  758. }
  759. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  760. struct v4l2_format *f)
  761. {
  762. struct vivi_dev *dev = video_drvdata(file);
  763. struct vb2_queue *q = &dev->vb_vidq;
  764. int ret = vidioc_try_fmt_vid_cap(file, priv, f);
  765. if (ret < 0)
  766. return ret;
  767. if (vb2_is_streaming(q)) {
  768. dprintk(dev, 1, "%s device busy\n", __func__);
  769. return -EBUSY;
  770. }
  771. dev->fmt = get_format(f);
  772. dev->width = f->fmt.pix.width;
  773. dev->height = f->fmt.pix.height;
  774. dev->field = f->fmt.pix.field;
  775. return 0;
  776. }
  777. static int vidioc_reqbufs(struct file *file, void *priv,
  778. struct v4l2_requestbuffers *p)
  779. {
  780. struct vivi_dev *dev = video_drvdata(file);
  781. return vb2_reqbufs(&dev->vb_vidq, p);
  782. }
  783. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  784. {
  785. struct vivi_dev *dev = video_drvdata(file);
  786. return vb2_querybuf(&dev->vb_vidq, p);
  787. }
  788. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  789. {
  790. struct vivi_dev *dev = video_drvdata(file);
  791. return vb2_qbuf(&dev->vb_vidq, p);
  792. }
  793. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  794. {
  795. struct vivi_dev *dev = video_drvdata(file);
  796. return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
  797. }
  798. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  799. {
  800. struct vivi_dev *dev = video_drvdata(file);
  801. return vb2_streamon(&dev->vb_vidq, i);
  802. }
  803. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  804. {
  805. struct vivi_dev *dev = video_drvdata(file);
  806. return vb2_streamoff(&dev->vb_vidq, i);
  807. }
  808. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
  809. {
  810. return 0;
  811. }
  812. /* only one input in this sample driver */
  813. static int vidioc_enum_input(struct file *file, void *priv,
  814. struct v4l2_input *inp)
  815. {
  816. if (inp->index >= NUM_INPUTS)
  817. return -EINVAL;
  818. inp->type = V4L2_INPUT_TYPE_CAMERA;
  819. inp->std = V4L2_STD_525_60;
  820. sprintf(inp->name, "Camera %u", inp->index);
  821. return 0;
  822. }
  823. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  824. {
  825. struct vivi_dev *dev = video_drvdata(file);
  826. *i = dev->input;
  827. return 0;
  828. }
  829. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  830. {
  831. struct vivi_dev *dev = video_drvdata(file);
  832. if (i >= NUM_INPUTS)
  833. return -EINVAL;
  834. if (i == dev->input)
  835. return 0;
  836. dev->input = i;
  837. precalculate_bars(dev);
  838. precalculate_line(dev);
  839. return 0;
  840. }
  841. /* --- controls ---------------------------------------------- */
  842. static int vivi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  843. {
  844. struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
  845. if (ctrl == dev->autogain)
  846. dev->gain->val = jiffies & 0xff;
  847. return 0;
  848. }
  849. static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
  850. {
  851. struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
  852. if (ctrl == dev->button)
  853. dev->button_pressed = 30;
  854. return 0;
  855. }
  856. /* ------------------------------------------------------------------
  857. File operations for the device
  858. ------------------------------------------------------------------*/
  859. static ssize_t
  860. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  861. {
  862. struct vivi_dev *dev = video_drvdata(file);
  863. dprintk(dev, 1, "read called\n");
  864. return vb2_read(&dev->vb_vidq, data, count, ppos,
  865. file->f_flags & O_NONBLOCK);
  866. }
  867. static unsigned int
  868. vivi_poll(struct file *file, struct poll_table_struct *wait)
  869. {
  870. struct vivi_dev *dev = video_drvdata(file);
  871. struct v4l2_fh *fh = file->private_data;
  872. struct vb2_queue *q = &dev->vb_vidq;
  873. unsigned int res;
  874. dprintk(dev, 1, "%s\n", __func__);
  875. res = vb2_poll(q, file, wait);
  876. if (v4l2_event_pending(fh))
  877. res |= POLLPRI;
  878. else
  879. poll_wait(file, &fh->wait, wait);
  880. return res;
  881. }
  882. static int vivi_close(struct file *file)
  883. {
  884. struct video_device *vdev = video_devdata(file);
  885. struct vivi_dev *dev = video_drvdata(file);
  886. dprintk(dev, 1, "close called (dev=%s), file %p\n",
  887. video_device_node_name(vdev), file);
  888. if (v4l2_fh_is_singular_file(file))
  889. vb2_queue_release(&dev->vb_vidq);
  890. return v4l2_fh_release(file);
  891. }
  892. static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
  893. {
  894. struct vivi_dev *dev = video_drvdata(file);
  895. int ret;
  896. dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  897. ret = vb2_mmap(&dev->vb_vidq, vma);
  898. dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
  899. (unsigned long)vma->vm_start,
  900. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  901. ret);
  902. return ret;
  903. }
  904. static const struct v4l2_ctrl_ops vivi_ctrl_ops = {
  905. .g_volatile_ctrl = vivi_g_volatile_ctrl,
  906. .s_ctrl = vivi_s_ctrl,
  907. };
  908. #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
  909. static const struct v4l2_ctrl_config vivi_ctrl_button = {
  910. .ops = &vivi_ctrl_ops,
  911. .id = VIVI_CID_CUSTOM_BASE + 0,
  912. .name = "Button",
  913. .type = V4L2_CTRL_TYPE_BUTTON,
  914. };
  915. static const struct v4l2_ctrl_config vivi_ctrl_boolean = {
  916. .ops = &vivi_ctrl_ops,
  917. .id = VIVI_CID_CUSTOM_BASE + 1,
  918. .name = "Boolean",
  919. .type = V4L2_CTRL_TYPE_BOOLEAN,
  920. .min = 0,
  921. .max = 1,
  922. .step = 1,
  923. .def = 1,
  924. };
  925. static const struct v4l2_ctrl_config vivi_ctrl_int32 = {
  926. .ops = &vivi_ctrl_ops,
  927. .id = VIVI_CID_CUSTOM_BASE + 2,
  928. .name = "Integer 32 Bits",
  929. .type = V4L2_CTRL_TYPE_INTEGER,
  930. .min = 0x80000000,
  931. .max = 0x7fffffff,
  932. .step = 1,
  933. };
  934. static const struct v4l2_ctrl_config vivi_ctrl_int64 = {
  935. .ops = &vivi_ctrl_ops,
  936. .id = VIVI_CID_CUSTOM_BASE + 3,
  937. .name = "Integer 64 Bits",
  938. .type = V4L2_CTRL_TYPE_INTEGER64,
  939. };
  940. static const char * const vivi_ctrl_menu_strings[] = {
  941. "Menu Item 0 (Skipped)",
  942. "Menu Item 1",
  943. "Menu Item 2 (Skipped)",
  944. "Menu Item 3",
  945. "Menu Item 4",
  946. "Menu Item 5 (Skipped)",
  947. NULL,
  948. };
  949. static const struct v4l2_ctrl_config vivi_ctrl_menu = {
  950. .ops = &vivi_ctrl_ops,
  951. .id = VIVI_CID_CUSTOM_BASE + 4,
  952. .name = "Menu",
  953. .type = V4L2_CTRL_TYPE_MENU,
  954. .min = 1,
  955. .max = 4,
  956. .def = 3,
  957. .menu_skip_mask = 0x04,
  958. .qmenu = vivi_ctrl_menu_strings,
  959. };
  960. static const struct v4l2_ctrl_config vivi_ctrl_string = {
  961. .ops = &vivi_ctrl_ops,
  962. .id = VIVI_CID_CUSTOM_BASE + 5,
  963. .name = "String",
  964. .type = V4L2_CTRL_TYPE_STRING,
  965. .min = 2,
  966. .max = 4,
  967. .step = 1,
  968. };
  969. static const struct v4l2_ctrl_config vivi_ctrl_bitmask = {
  970. .ops = &vivi_ctrl_ops,
  971. .id = VIVI_CID_CUSTOM_BASE + 6,
  972. .name = "Bitmask",
  973. .type = V4L2_CTRL_TYPE_BITMASK,
  974. .def = 0x80002000,
  975. .min = 0,
  976. .max = 0x80402010,
  977. .step = 0,
  978. };
  979. static const struct v4l2_file_operations vivi_fops = {
  980. .owner = THIS_MODULE,
  981. .open = v4l2_fh_open,
  982. .release = vivi_close,
  983. .read = vivi_read,
  984. .poll = vivi_poll,
  985. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  986. .mmap = vivi_mmap,
  987. };
  988. static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
  989. .vidioc_querycap = vidioc_querycap,
  990. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  991. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  992. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  993. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  994. .vidioc_reqbufs = vidioc_reqbufs,
  995. .vidioc_querybuf = vidioc_querybuf,
  996. .vidioc_qbuf = vidioc_qbuf,
  997. .vidioc_dqbuf = vidioc_dqbuf,
  998. .vidioc_s_std = vidioc_s_std,
  999. .vidioc_enum_input = vidioc_enum_input,
  1000. .vidioc_g_input = vidioc_g_input,
  1001. .vidioc_s_input = vidioc_s_input,
  1002. .vidioc_streamon = vidioc_streamon,
  1003. .vidioc_streamoff = vidioc_streamoff,
  1004. .vidioc_log_status = v4l2_ctrl_log_status,
  1005. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1006. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1007. };
  1008. static struct video_device vivi_template = {
  1009. .name = "vivi",
  1010. .fops = &vivi_fops,
  1011. .ioctl_ops = &vivi_ioctl_ops,
  1012. .release = video_device_release,
  1013. .tvnorms = V4L2_STD_525_60,
  1014. .current_norm = V4L2_STD_NTSC_M,
  1015. };
  1016. /* -----------------------------------------------------------------
  1017. Initialization and module stuff
  1018. ------------------------------------------------------------------*/
  1019. static int vivi_release(void)
  1020. {
  1021. struct vivi_dev *dev;
  1022. struct list_head *list;
  1023. while (!list_empty(&vivi_devlist)) {
  1024. list = vivi_devlist.next;
  1025. list_del(list);
  1026. dev = list_entry(list, struct vivi_dev, vivi_devlist);
  1027. v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
  1028. video_device_node_name(dev->vfd));
  1029. video_unregister_device(dev->vfd);
  1030. v4l2_device_unregister(&dev->v4l2_dev);
  1031. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1032. kfree(dev);
  1033. }
  1034. return 0;
  1035. }
  1036. static int __init vivi_create_instance(int inst)
  1037. {
  1038. struct vivi_dev *dev;
  1039. struct video_device *vfd;
  1040. struct v4l2_ctrl_handler *hdl;
  1041. struct vb2_queue *q;
  1042. int ret;
  1043. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1044. if (!dev)
  1045. return -ENOMEM;
  1046. snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  1047. "%s-%03d", VIVI_MODULE_NAME, inst);
  1048. ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  1049. if (ret)
  1050. goto free_dev;
  1051. dev->fmt = &formats[0];
  1052. dev->width = 640;
  1053. dev->height = 480;
  1054. hdl = &dev->ctrl_handler;
  1055. v4l2_ctrl_handler_init(hdl, 11);
  1056. dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1057. V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
  1058. dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1059. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  1060. dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1061. V4L2_CID_CONTRAST, 0, 255, 1, 16);
  1062. dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1063. V4L2_CID_SATURATION, 0, 255, 1, 127);
  1064. dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1065. V4L2_CID_HUE, -128, 127, 1, 0);
  1066. dev->autogain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1067. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1068. dev->gain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
  1069. V4L2_CID_GAIN, 0, 255, 1, 100);
  1070. dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL);
  1071. dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL);
  1072. dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL);
  1073. dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL);
  1074. dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL);
  1075. dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL);
  1076. dev->bitmask = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_bitmask, NULL);
  1077. if (hdl->error) {
  1078. ret = hdl->error;
  1079. goto unreg_dev;
  1080. }
  1081. v4l2_ctrl_auto_cluster(2, &dev->autogain, 0, true);
  1082. dev->v4l2_dev.ctrl_handler = hdl;
  1083. /* initialize locks */
  1084. spin_lock_init(&dev->slock);
  1085. /* initialize queue */
  1086. q = &dev->vb_vidq;
  1087. memset(q, 0, sizeof(dev->vb_vidq));
  1088. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1089. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  1090. q->drv_priv = dev;
  1091. q->buf_struct_size = sizeof(struct vivi_buffer);
  1092. q->ops = &vivi_video_qops;
  1093. q->mem_ops = &vb2_vmalloc_memops;
  1094. vb2_queue_init(q);
  1095. mutex_init(&dev->mutex);
  1096. /* init video dma queues */
  1097. INIT_LIST_HEAD(&dev->vidq.active);
  1098. init_waitqueue_head(&dev->vidq.wq);
  1099. ret = -ENOMEM;
  1100. vfd = video_device_alloc();
  1101. if (!vfd)
  1102. goto unreg_dev;
  1103. *vfd = vivi_template;
  1104. vfd->debug = debug;
  1105. vfd->v4l2_dev = &dev->v4l2_dev;
  1106. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1107. /*
  1108. * Provide a mutex to v4l2 core. It will be used to protect
  1109. * all fops and v4l2 ioctls.
  1110. */
  1111. vfd->lock = &dev->mutex;
  1112. ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
  1113. if (ret < 0)
  1114. goto rel_vdev;
  1115. video_set_drvdata(vfd, dev);
  1116. /* Now that everything is fine, let's add it to device list */
  1117. list_add_tail(&dev->vivi_devlist, &vivi_devlist);
  1118. if (video_nr != -1)
  1119. video_nr++;
  1120. dev->vfd = vfd;
  1121. v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
  1122. video_device_node_name(vfd));
  1123. return 0;
  1124. rel_vdev:
  1125. video_device_release(vfd);
  1126. unreg_dev:
  1127. v4l2_ctrl_handler_free(hdl);
  1128. v4l2_device_unregister(&dev->v4l2_dev);
  1129. free_dev:
  1130. kfree(dev);
  1131. return ret;
  1132. }
  1133. /* This routine allocates from 1 to n_devs virtual drivers.
  1134. The real maximum number of virtual drivers will depend on how many drivers
  1135. will succeed. This is limited to the maximum number of devices that
  1136. videodev supports, which is equal to VIDEO_NUM_DEVICES.
  1137. */
  1138. static int __init vivi_init(void)
  1139. {
  1140. const struct font_desc *font = find_font("VGA8x16");
  1141. int ret = 0, i;
  1142. if (font == NULL) {
  1143. printk(KERN_ERR "vivi: could not find font\n");
  1144. return -ENODEV;
  1145. }
  1146. font8x16 = font->data;
  1147. if (n_devs <= 0)
  1148. n_devs = 1;
  1149. for (i = 0; i < n_devs; i++) {
  1150. ret = vivi_create_instance(i);
  1151. if (ret) {
  1152. /* If some instantiations succeeded, keep driver */
  1153. if (i)
  1154. ret = 0;
  1155. break;
  1156. }
  1157. }
  1158. if (ret < 0) {
  1159. printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
  1160. return ret;
  1161. }
  1162. printk(KERN_INFO "Video Technology Magazine Virtual Video "
  1163. "Capture Board ver %s successfully loaded.\n",
  1164. VIVI_VERSION);
  1165. /* n_devs will reflect the actual number of allocated devices */
  1166. n_devs = i;
  1167. return ret;
  1168. }
  1169. static void __exit vivi_exit(void)
  1170. {
  1171. vivi_release();
  1172. }
  1173. module_init(vivi_init);
  1174. module_exit(vivi_exit);