v4l1-compat.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. *
  3. * Video for Linux Two
  4. * Backward Compatibility Layer
  5. *
  6. * Support subroutines for providing V4L2 drivers with backward
  7. * compatibility with applications using the old API.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Author: Bill Dirks <bill@thedirks.org>
  15. * et al.
  16. *
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/mm.h>
  25. #include <linux/fs.h>
  26. #include <linux/file.h>
  27. #include <linux/string.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/videodev.h>
  31. #include <media/v4l2-common.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/system.h>
  34. #include <asm/pgtable.h>
  35. #ifdef CONFIG_KMOD
  36. #include <linux/kmod.h>
  37. #endif
  38. static unsigned int debug = 0;
  39. module_param(debug, int, 0644);
  40. MODULE_PARM_DESC(debug,"enable debug messages");
  41. MODULE_AUTHOR("Bill Dirks");
  42. MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
  43. MODULE_LICENSE("GPL");
  44. #define dprintk(fmt, arg...) if (debug) \
  45. printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg)
  46. /*
  47. * I O C T L T R A N S L A T I O N
  48. *
  49. * From here on down is the code for translating the numerous
  50. * ioctl commands from the old API to the new API.
  51. */
  52. static int
  53. get_v4l_control(struct inode *inode,
  54. struct file *file,
  55. int cid,
  56. v4l2_kioctl drv)
  57. {
  58. struct v4l2_queryctrl qctrl2;
  59. struct v4l2_control ctrl2;
  60. int err;
  61. qctrl2.id = cid;
  62. err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
  63. if (err < 0)
  64. dprintk("VIDIOC_QUERYCTRL: %d\n",err);
  65. if (err == 0 &&
  66. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  67. {
  68. ctrl2.id = qctrl2.id;
  69. err = drv(inode, file, VIDIOC_G_CTRL, &ctrl2);
  70. if (err < 0) {
  71. dprintk("VIDIOC_G_CTRL: %d\n",err);
  72. return 0;
  73. }
  74. return ((ctrl2.value - qctrl2.minimum) * 65535
  75. + (qctrl2.maximum - qctrl2.minimum) / 2)
  76. / (qctrl2.maximum - qctrl2.minimum);
  77. }
  78. return 0;
  79. }
  80. static int
  81. set_v4l_control(struct inode *inode,
  82. struct file *file,
  83. int cid,
  84. int value,
  85. v4l2_kioctl drv)
  86. {
  87. struct v4l2_queryctrl qctrl2;
  88. struct v4l2_control ctrl2;
  89. int err;
  90. qctrl2.id = cid;
  91. err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
  92. if (err < 0)
  93. dprintk("VIDIOC_QUERYCTRL: %d\n",err);
  94. if (err == 0 &&
  95. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
  96. !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED))
  97. {
  98. if (value < 0)
  99. value = 0;
  100. if (value > 65535)
  101. value = 65535;
  102. if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
  103. value = 65535;
  104. ctrl2.id = qctrl2.id;
  105. ctrl2.value =
  106. (value * (qctrl2.maximum - qctrl2.minimum)
  107. + 32767)
  108. / 65535;
  109. ctrl2.value += qctrl2.minimum;
  110. err = drv(inode, file, VIDIOC_S_CTRL, &ctrl2);
  111. if (err < 0)
  112. dprintk("VIDIOC_S_CTRL: %d\n",err);
  113. }
  114. return 0;
  115. }
  116. /* ----------------------------------------------------------------- */
  117. const static unsigned int palette2pixelformat[] = {
  118. [VIDEO_PALETTE_GREY] = V4L2_PIX_FMT_GREY,
  119. [VIDEO_PALETTE_RGB555] = V4L2_PIX_FMT_RGB555,
  120. [VIDEO_PALETTE_RGB565] = V4L2_PIX_FMT_RGB565,
  121. [VIDEO_PALETTE_RGB24] = V4L2_PIX_FMT_BGR24,
  122. [VIDEO_PALETTE_RGB32] = V4L2_PIX_FMT_BGR32,
  123. /* yuv packed pixel */
  124. [VIDEO_PALETTE_YUYV] = V4L2_PIX_FMT_YUYV,
  125. [VIDEO_PALETTE_YUV422] = V4L2_PIX_FMT_YUYV,
  126. [VIDEO_PALETTE_UYVY] = V4L2_PIX_FMT_UYVY,
  127. /* yuv planar */
  128. [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
  129. [VIDEO_PALETTE_YUV420] = V4L2_PIX_FMT_YUV420,
  130. [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
  131. [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
  132. [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
  133. };
  134. static unsigned int __attribute_pure__
  135. palette_to_pixelformat(unsigned int palette)
  136. {
  137. if (palette < ARRAY_SIZE(palette2pixelformat))
  138. return palette2pixelformat[palette];
  139. else
  140. return 0;
  141. }
  142. static unsigned int __attribute_const__
  143. pixelformat_to_palette(unsigned int pixelformat)
  144. {
  145. int palette = 0;
  146. switch (pixelformat)
  147. {
  148. case V4L2_PIX_FMT_GREY:
  149. palette = VIDEO_PALETTE_GREY;
  150. break;
  151. case V4L2_PIX_FMT_RGB555:
  152. palette = VIDEO_PALETTE_RGB555;
  153. break;
  154. case V4L2_PIX_FMT_RGB565:
  155. palette = VIDEO_PALETTE_RGB565;
  156. break;
  157. case V4L2_PIX_FMT_BGR24:
  158. palette = VIDEO_PALETTE_RGB24;
  159. break;
  160. case V4L2_PIX_FMT_BGR32:
  161. palette = VIDEO_PALETTE_RGB32;
  162. break;
  163. /* yuv packed pixel */
  164. case V4L2_PIX_FMT_YUYV:
  165. palette = VIDEO_PALETTE_YUYV;
  166. break;
  167. case V4L2_PIX_FMT_UYVY:
  168. palette = VIDEO_PALETTE_UYVY;
  169. break;
  170. /* yuv planar */
  171. case V4L2_PIX_FMT_YUV410:
  172. palette = VIDEO_PALETTE_YUV420;
  173. break;
  174. case V4L2_PIX_FMT_YUV420:
  175. palette = VIDEO_PALETTE_YUV420;
  176. break;
  177. case V4L2_PIX_FMT_YUV411P:
  178. palette = VIDEO_PALETTE_YUV411P;
  179. break;
  180. case V4L2_PIX_FMT_YUV422P:
  181. palette = VIDEO_PALETTE_YUV422P;
  182. break;
  183. }
  184. return palette;
  185. }
  186. /* ----------------------------------------------------------------- */
  187. static int poll_one(struct file *file)
  188. {
  189. int retval = 1;
  190. poll_table *table;
  191. struct poll_wqueues pwq;
  192. poll_initwait(&pwq);
  193. table = &pwq.pt;
  194. for (;;) {
  195. int mask;
  196. set_current_state(TASK_INTERRUPTIBLE);
  197. mask = file->f_op->poll(file, table);
  198. if (mask & POLLIN)
  199. break;
  200. table = NULL;
  201. if (signal_pending(current)) {
  202. retval = -ERESTARTSYS;
  203. break;
  204. }
  205. schedule();
  206. }
  207. set_current_state(TASK_RUNNING);
  208. poll_freewait(&pwq);
  209. return retval;
  210. }
  211. static int count_inputs(struct inode *inode,
  212. struct file *file,
  213. v4l2_kioctl drv)
  214. {
  215. struct v4l2_input input2;
  216. int i;
  217. for (i = 0;; i++) {
  218. memset(&input2,0,sizeof(input2));
  219. input2.index = i;
  220. if (0 != drv(inode,file,VIDIOC_ENUMINPUT, &input2))
  221. break;
  222. }
  223. return i;
  224. }
  225. static int check_size(struct inode *inode,
  226. struct file *file,
  227. v4l2_kioctl drv,
  228. int *maxw, int *maxh)
  229. {
  230. struct v4l2_fmtdesc desc2;
  231. struct v4l2_format fmt2;
  232. memset(&desc2,0,sizeof(desc2));
  233. memset(&fmt2,0,sizeof(fmt2));
  234. desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  235. if (0 != drv(inode,file,VIDIOC_ENUM_FMT, &desc2))
  236. goto done;
  237. fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  238. fmt2.fmt.pix.width = 10000;
  239. fmt2.fmt.pix.height = 10000;
  240. fmt2.fmt.pix.pixelformat = desc2.pixelformat;
  241. if (0 != drv(inode,file,VIDIOC_TRY_FMT, &fmt2))
  242. goto done;
  243. *maxw = fmt2.fmt.pix.width;
  244. *maxh = fmt2.fmt.pix.height;
  245. done:
  246. return 0;
  247. }
  248. /* ----------------------------------------------------------------- */
  249. /*
  250. * This function is exported.
  251. */
  252. int
  253. v4l_compat_translate_ioctl(struct inode *inode,
  254. struct file *file,
  255. int cmd,
  256. void *arg,
  257. v4l2_kioctl drv)
  258. {
  259. struct v4l2_capability *cap2 = NULL;
  260. struct v4l2_format *fmt2 = NULL;
  261. enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  262. struct v4l2_framebuffer fbuf2;
  263. struct v4l2_input input2;
  264. struct v4l2_tuner tun2;
  265. struct v4l2_standard std2;
  266. struct v4l2_frequency freq2;
  267. struct v4l2_audio aud2;
  268. struct v4l2_queryctrl qctrl2;
  269. struct v4l2_buffer buf2;
  270. v4l2_std_id sid;
  271. int i, err = 0;
  272. switch (cmd) {
  273. case VIDIOCGCAP: /* capability */
  274. {
  275. struct video_capability *cap = arg;
  276. cap2 = kzalloc(sizeof(*cap2),GFP_KERNEL);
  277. memset(cap, 0, sizeof(*cap));
  278. memset(&fbuf2, 0, sizeof(fbuf2));
  279. err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
  280. if (err < 0) {
  281. dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n",err);
  282. break;
  283. }
  284. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
  285. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  286. if (err < 0) {
  287. dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n",err);
  288. memset(&fbuf2, 0, sizeof(fbuf2));
  289. }
  290. err = 0;
  291. }
  292. memcpy(cap->name, cap2->card,
  293. min(sizeof(cap->name), sizeof(cap2->card)));
  294. cap->name[sizeof(cap->name) - 1] = 0;
  295. if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
  296. cap->type |= VID_TYPE_CAPTURE;
  297. if (cap2->capabilities & V4L2_CAP_TUNER)
  298. cap->type |= VID_TYPE_TUNER;
  299. if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
  300. cap->type |= VID_TYPE_TELETEXT;
  301. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
  302. cap->type |= VID_TYPE_OVERLAY;
  303. if (fbuf2.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
  304. cap->type |= VID_TYPE_CLIPPING;
  305. cap->channels = count_inputs(inode,file,drv);
  306. check_size(inode,file,drv,
  307. &cap->maxwidth,&cap->maxheight);
  308. cap->audios = 0; /* FIXME */
  309. cap->minwidth = 48; /* FIXME */
  310. cap->minheight = 32; /* FIXME */
  311. break;
  312. }
  313. case VIDIOCGFBUF: /* get frame buffer */
  314. {
  315. struct video_buffer *buffer = arg;
  316. memset(buffer, 0, sizeof(*buffer));
  317. memset(&fbuf2, 0, sizeof(fbuf2));
  318. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  319. if (err < 0) {
  320. dprintk("VIDIOCGFBUF / VIDIOC_G_FBUF: %d\n",err);
  321. break;
  322. }
  323. buffer->base = fbuf2.base;
  324. buffer->height = fbuf2.fmt.height;
  325. buffer->width = fbuf2.fmt.width;
  326. switch (fbuf2.fmt.pixelformat) {
  327. case V4L2_PIX_FMT_RGB332:
  328. buffer->depth = 8;
  329. break;
  330. case V4L2_PIX_FMT_RGB555:
  331. buffer->depth = 15;
  332. break;
  333. case V4L2_PIX_FMT_RGB565:
  334. buffer->depth = 16;
  335. break;
  336. case V4L2_PIX_FMT_BGR24:
  337. buffer->depth = 24;
  338. break;
  339. case V4L2_PIX_FMT_BGR32:
  340. buffer->depth = 32;
  341. break;
  342. default:
  343. buffer->depth = 0;
  344. }
  345. if (fbuf2.fmt.bytesperline) {
  346. buffer->bytesperline = fbuf2.fmt.bytesperline;
  347. if (!buffer->depth && buffer->width)
  348. buffer->depth = ((fbuf2.fmt.bytesperline<<3)
  349. + (buffer->width-1) )
  350. /buffer->width;
  351. } else {
  352. buffer->bytesperline =
  353. (buffer->width * buffer->depth + 7) & 7;
  354. buffer->bytesperline >>= 3;
  355. }
  356. break;
  357. }
  358. case VIDIOCSFBUF: /* set frame buffer */
  359. {
  360. struct video_buffer *buffer = arg;
  361. memset(&fbuf2, 0, sizeof(fbuf2));
  362. fbuf2.base = buffer->base;
  363. fbuf2.fmt.height = buffer->height;
  364. fbuf2.fmt.width = buffer->width;
  365. switch (buffer->depth) {
  366. case 8:
  367. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
  368. break;
  369. case 15:
  370. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
  371. break;
  372. case 16:
  373. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
  374. break;
  375. case 24:
  376. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
  377. break;
  378. case 32:
  379. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
  380. break;
  381. }
  382. fbuf2.fmt.bytesperline = buffer->bytesperline;
  383. err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  384. if (err < 0)
  385. dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %d\n",err);
  386. break;
  387. }
  388. case VIDIOCGWIN: /* get window or capture dimensions */
  389. {
  390. struct video_window *win = arg;
  391. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  392. memset(win,0,sizeof(*win));
  393. fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
  394. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  395. if (err < 0)
  396. dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n",err);
  397. if (err == 0) {
  398. win->x = fmt2->fmt.win.w.left;
  399. win->y = fmt2->fmt.win.w.top;
  400. win->width = fmt2->fmt.win.w.width;
  401. win->height = fmt2->fmt.win.w.height;
  402. win->chromakey = fmt2->fmt.win.chromakey;
  403. win->clips = NULL;
  404. win->clipcount = 0;
  405. break;
  406. }
  407. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  408. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  409. if (err < 0) {
  410. dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n",err);
  411. break;
  412. }
  413. win->x = 0;
  414. win->y = 0;
  415. win->width = fmt2->fmt.pix.width;
  416. win->height = fmt2->fmt.pix.height;
  417. win->chromakey = 0;
  418. win->clips = NULL;
  419. win->clipcount = 0;
  420. break;
  421. }
  422. case VIDIOCSWIN: /* set window and/or capture dimensions */
  423. {
  424. struct video_window *win = arg;
  425. int err1,err2;
  426. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  427. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  428. drv(inode, file, VIDIOC_STREAMOFF, &fmt2->type);
  429. err1 = drv(inode, file, VIDIOC_G_FMT, fmt2);
  430. if (err1 < 0)
  431. dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n",err);
  432. if (err1 == 0) {
  433. fmt2->fmt.pix.width = win->width;
  434. fmt2->fmt.pix.height = win->height;
  435. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  436. fmt2->fmt.pix.bytesperline = 0;
  437. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  438. if (err < 0)
  439. dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
  440. err);
  441. win->width = fmt2->fmt.pix.width;
  442. win->height = fmt2->fmt.pix.height;
  443. }
  444. memset(fmt2,0,sizeof(*fmt2));
  445. fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
  446. fmt2->fmt.win.w.left = win->x;
  447. fmt2->fmt.win.w.top = win->y;
  448. fmt2->fmt.win.w.width = win->width;
  449. fmt2->fmt.win.w.height = win->height;
  450. fmt2->fmt.win.chromakey = win->chromakey;
  451. fmt2->fmt.win.clips = (void __user *)win->clips;
  452. fmt2->fmt.win.clipcount = win->clipcount;
  453. err2 = drv(inode, file, VIDIOC_S_FMT, fmt2);
  454. if (err2 < 0)
  455. dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n",err);
  456. if (err1 != 0 && err2 != 0)
  457. err = err1;
  458. break;
  459. }
  460. case VIDIOCCAPTURE: /* turn on/off preview */
  461. {
  462. int *on = arg;
  463. if (0 == *on) {
  464. /* dirty hack time. But v4l1 has no STREAMOFF
  465. * equivalent in the API, and this one at
  466. * least comes close ... */
  467. drv(inode, file, VIDIOC_STREAMOFF, &captype);
  468. }
  469. err = drv(inode, file, VIDIOC_OVERLAY, arg);
  470. if (err < 0)
  471. dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n",err);
  472. break;
  473. }
  474. case VIDIOCGCHAN: /* get input information */
  475. {
  476. struct video_channel *chan = arg;
  477. memset(&input2,0,sizeof(input2));
  478. input2.index = chan->channel;
  479. err = drv(inode, file, VIDIOC_ENUMINPUT, &input2);
  480. if (err < 0) {
  481. dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
  482. "channel=%d err=%d\n",chan->channel,err);
  483. break;
  484. }
  485. chan->channel = input2.index;
  486. memcpy(chan->name, input2.name,
  487. min(sizeof(chan->name), sizeof(input2.name)));
  488. chan->name[sizeof(chan->name) - 1] = 0;
  489. chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
  490. chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
  491. switch (input2.type) {
  492. case V4L2_INPUT_TYPE_TUNER:
  493. chan->type = VIDEO_TYPE_TV;
  494. break;
  495. default:
  496. case V4L2_INPUT_TYPE_CAMERA:
  497. chan->type = VIDEO_TYPE_CAMERA;
  498. break;
  499. }
  500. chan->norm = 0;
  501. err = drv(inode, file, VIDIOC_G_STD, &sid);
  502. if (err < 0)
  503. dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n",err);
  504. if (err == 0) {
  505. if (sid & V4L2_STD_PAL)
  506. chan->norm = VIDEO_MODE_PAL;
  507. if (sid & V4L2_STD_NTSC)
  508. chan->norm = VIDEO_MODE_NTSC;
  509. if (sid & V4L2_STD_SECAM)
  510. chan->norm = VIDEO_MODE_SECAM;
  511. }
  512. break;
  513. }
  514. case VIDIOCSCHAN: /* set input */
  515. {
  516. struct video_channel *chan = arg;
  517. sid = 0;
  518. err = drv(inode, file, VIDIOC_S_INPUT, &chan->channel);
  519. if (err < 0)
  520. dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n",err);
  521. switch (chan->norm) {
  522. case VIDEO_MODE_PAL:
  523. sid = V4L2_STD_PAL;
  524. break;
  525. case VIDEO_MODE_NTSC:
  526. sid = V4L2_STD_NTSC;
  527. break;
  528. case VIDEO_MODE_SECAM:
  529. sid = V4L2_STD_SECAM;
  530. break;
  531. }
  532. if (0 != sid) {
  533. err = drv(inode, file, VIDIOC_S_STD, &sid);
  534. if (err < 0)
  535. dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n",err);
  536. }
  537. break;
  538. }
  539. case VIDIOCGPICT: /* get tone controls & partial capture format */
  540. {
  541. struct video_picture *pict = arg;
  542. pict->brightness = get_v4l_control(inode, file,
  543. V4L2_CID_BRIGHTNESS,drv);
  544. pict->hue = get_v4l_control(inode, file,
  545. V4L2_CID_HUE, drv);
  546. pict->contrast = get_v4l_control(inode, file,
  547. V4L2_CID_CONTRAST, drv);
  548. pict->colour = get_v4l_control(inode, file,
  549. V4L2_CID_SATURATION, drv);
  550. pict->whiteness = get_v4l_control(inode, file,
  551. V4L2_CID_WHITENESS, drv);
  552. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  553. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  554. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  555. if (err < 0) {
  556. dprintk("VIDIOCGPICT / VIDIOC_G_FMT: %d\n",err);
  557. break;
  558. }
  559. pict->depth = ((fmt2->fmt.pix.bytesperline<<3)
  560. + (fmt2->fmt.pix.width-1) )
  561. /fmt2->fmt.pix.width;
  562. pict->palette = pixelformat_to_palette(
  563. fmt2->fmt.pix.pixelformat);
  564. break;
  565. }
  566. case VIDIOCSPICT: /* set tone controls & partial capture format */
  567. {
  568. struct video_picture *pict = arg;
  569. int mem_err = 0, ovl_err = 0;
  570. memset(&fbuf2, 0, sizeof(fbuf2));
  571. set_v4l_control(inode, file,
  572. V4L2_CID_BRIGHTNESS, pict->brightness, drv);
  573. set_v4l_control(inode, file,
  574. V4L2_CID_HUE, pict->hue, drv);
  575. set_v4l_control(inode, file,
  576. V4L2_CID_CONTRAST, pict->contrast, drv);
  577. set_v4l_control(inode, file,
  578. V4L2_CID_SATURATION, pict->colour, drv);
  579. set_v4l_control(inode, file,
  580. V4L2_CID_WHITENESS, pict->whiteness, drv);
  581. /*
  582. * V4L1 uses this ioctl to set both memory capture and overlay
  583. * pixel format, while V4L2 has two different ioctls for this.
  584. * Some cards may not support one or the other, and may support
  585. * different pixel formats for memory vs overlay.
  586. */
  587. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  588. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  589. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  590. /* If VIDIOC_G_FMT failed, then the driver likely doesn't
  591. support memory capture. Trying to set the memory capture
  592. parameters would be pointless. */
  593. if (err < 0) {
  594. dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
  595. mem_err = -1000; /* didn't even try */
  596. } else if (fmt2->fmt.pix.pixelformat !=
  597. palette_to_pixelformat(pict->palette)) {
  598. fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
  599. pict->palette);
  600. mem_err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  601. if (mem_err < 0)
  602. dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",
  603. mem_err);
  604. }
  605. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  606. /* If VIDIOC_G_FBUF failed, then the driver likely doesn't
  607. support overlay. Trying to set the overlay parameters
  608. would be quite pointless. */
  609. if (err < 0) {
  610. dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
  611. ovl_err = -1000; /* didn't even try */
  612. } else if (fbuf2.fmt.pixelformat !=
  613. palette_to_pixelformat(pict->palette)) {
  614. fbuf2.fmt.pixelformat = palette_to_pixelformat(
  615. pict->palette);
  616. ovl_err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  617. if (ovl_err < 0)
  618. dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",
  619. ovl_err);
  620. }
  621. if (ovl_err < 0 && mem_err < 0)
  622. /* ioctl failed, couldn't set either parameter */
  623. if (mem_err != -1000) {
  624. err = mem_err;
  625. } else if (ovl_err == -EPERM) {
  626. err = 0;
  627. } else {
  628. err = ovl_err;
  629. }
  630. else
  631. err = 0;
  632. break;
  633. }
  634. case VIDIOCGTUNER: /* get tuner information */
  635. {
  636. struct video_tuner *tun = arg;
  637. memset(&tun2,0,sizeof(tun2));
  638. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  639. if (err < 0) {
  640. dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
  641. break;
  642. }
  643. memcpy(tun->name, tun2.name,
  644. min(sizeof(tun->name), sizeof(tun2.name)));
  645. tun->name[sizeof(tun->name) - 1] = 0;
  646. tun->rangelow = tun2.rangelow;
  647. tun->rangehigh = tun2.rangehigh;
  648. tun->flags = 0;
  649. tun->mode = VIDEO_MODE_AUTO;
  650. for (i = 0; i < 64; i++) {
  651. memset(&std2,0,sizeof(std2));
  652. std2.index = i;
  653. if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
  654. break;
  655. if (std2.id & V4L2_STD_PAL)
  656. tun->flags |= VIDEO_TUNER_PAL;
  657. if (std2.id & V4L2_STD_NTSC)
  658. tun->flags |= VIDEO_TUNER_NTSC;
  659. if (std2.id & V4L2_STD_SECAM)
  660. tun->flags |= VIDEO_TUNER_SECAM;
  661. }
  662. err = drv(inode, file, VIDIOC_G_STD, &sid);
  663. if (err < 0)
  664. dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
  665. if (err == 0) {
  666. if (sid & V4L2_STD_PAL)
  667. tun->mode = VIDEO_MODE_PAL;
  668. if (sid & V4L2_STD_NTSC)
  669. tun->mode = VIDEO_MODE_NTSC;
  670. if (sid & V4L2_STD_SECAM)
  671. tun->mode = VIDEO_MODE_SECAM;
  672. }
  673. if (tun2.capability & V4L2_TUNER_CAP_LOW)
  674. tun->flags |= VIDEO_TUNER_LOW;
  675. if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  676. tun->flags |= VIDEO_TUNER_STEREO_ON;
  677. tun->signal = tun2.signal;
  678. break;
  679. }
  680. case VIDIOCSTUNER: /* select a tuner input */
  681. {
  682. struct video_tuner *tun = arg;
  683. struct v4l2_tuner t;
  684. memset(&t,0,sizeof(t));
  685. t.index=tun->tuner;
  686. err = drv(inode, file, VIDIOC_S_INPUT, &t);
  687. if (err < 0)
  688. dprintk("VIDIOCSTUNER / VIDIOC_S_INPUT: %d\n",err);
  689. break;
  690. }
  691. case VIDIOCGFREQ: /* get frequency */
  692. {
  693. unsigned long *freq = arg;
  694. memset(&freq2,0,sizeof(freq2));
  695. freq2.tuner = 0;
  696. err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  697. if (err < 0)
  698. dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
  699. if (0 == err)
  700. *freq = freq2.frequency;
  701. break;
  702. }
  703. case VIDIOCSFREQ: /* set frequency */
  704. {
  705. unsigned long *freq = arg;
  706. memset(&freq2,0,sizeof(freq2));
  707. drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  708. freq2.frequency = *freq;
  709. err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
  710. if (err < 0)
  711. dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
  712. break;
  713. }
  714. case VIDIOCGAUDIO: /* get audio properties/controls */
  715. {
  716. struct video_audio *aud = arg;
  717. memset(&aud2,0,sizeof(aud2));
  718. err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
  719. if (err < 0) {
  720. dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
  721. break;
  722. }
  723. memcpy(aud->name, aud2.name,
  724. min(sizeof(aud->name), sizeof(aud2.name)));
  725. aud->name[sizeof(aud->name) - 1] = 0;
  726. aud->audio = aud2.index;
  727. aud->flags = 0;
  728. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
  729. if (i >= 0) {
  730. aud->volume = i;
  731. aud->flags |= VIDEO_AUDIO_VOLUME;
  732. }
  733. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
  734. if (i >= 0) {
  735. aud->bass = i;
  736. aud->flags |= VIDEO_AUDIO_BASS;
  737. }
  738. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
  739. if (i >= 0) {
  740. aud->treble = i;
  741. aud->flags |= VIDEO_AUDIO_TREBLE;
  742. }
  743. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
  744. if (i >= 0) {
  745. aud->balance = i;
  746. aud->flags |= VIDEO_AUDIO_BALANCE;
  747. }
  748. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
  749. if (i >= 0) {
  750. if (i)
  751. aud->flags |= VIDEO_AUDIO_MUTE;
  752. aud->flags |= VIDEO_AUDIO_MUTABLE;
  753. }
  754. aud->step = 1;
  755. qctrl2.id = V4L2_CID_AUDIO_VOLUME;
  756. if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
  757. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  758. aud->step = qctrl2.step;
  759. aud->mode = 0;
  760. memset(&tun2,0,sizeof(tun2));
  761. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  762. if (err < 0) {
  763. dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
  764. err = 0;
  765. break;
  766. }
  767. if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
  768. aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  769. else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  770. aud->mode = VIDEO_SOUND_STEREO;
  771. else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
  772. aud->mode = VIDEO_SOUND_MONO;
  773. break;
  774. }
  775. case VIDIOCSAUDIO: /* set audio controls */
  776. {
  777. struct video_audio *aud = arg;
  778. memset(&aud2,0,sizeof(aud2));
  779. memset(&tun2,0,sizeof(tun2));
  780. aud2.index = aud->audio;
  781. err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
  782. if (err < 0) {
  783. dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
  784. break;
  785. }
  786. set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
  787. aud->volume, drv);
  788. set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
  789. aud->bass, drv);
  790. set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
  791. aud->treble, drv);
  792. set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
  793. aud->balance, drv);
  794. set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
  795. !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
  796. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  797. if (err < 0)
  798. dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
  799. if (err == 0) {
  800. switch (aud->mode) {
  801. default:
  802. case VIDEO_SOUND_MONO:
  803. case VIDEO_SOUND_LANG1:
  804. tun2.audmode = V4L2_TUNER_MODE_MONO;
  805. break;
  806. case VIDEO_SOUND_STEREO:
  807. tun2.audmode = V4L2_TUNER_MODE_STEREO;
  808. break;
  809. case VIDEO_SOUND_LANG2:
  810. tun2.audmode = V4L2_TUNER_MODE_LANG2;
  811. break;
  812. }
  813. err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
  814. if (err < 0)
  815. dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
  816. }
  817. err = 0;
  818. break;
  819. }
  820. case VIDIOCMCAPTURE: /* capture a frame */
  821. {
  822. struct video_mmap *mm = arg;
  823. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  824. memset(&buf2,0,sizeof(buf2));
  825. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  826. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  827. if (err < 0) {
  828. dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
  829. break;
  830. }
  831. if (mm->width != fmt2->fmt.pix.width ||
  832. mm->height != fmt2->fmt.pix.height ||
  833. palette_to_pixelformat(mm->format) !=
  834. fmt2->fmt.pix.pixelformat)
  835. {/* New capture format... */
  836. fmt2->fmt.pix.width = mm->width;
  837. fmt2->fmt.pix.height = mm->height;
  838. fmt2->fmt.pix.pixelformat =
  839. palette_to_pixelformat(mm->format);
  840. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  841. fmt2->fmt.pix.bytesperline = 0;
  842. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  843. if (err < 0) {
  844. dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
  845. break;
  846. }
  847. }
  848. buf2.index = mm->frame;
  849. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  850. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  851. if (err < 0) {
  852. dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
  853. break;
  854. }
  855. err = drv(inode, file, VIDIOC_QBUF, &buf2);
  856. if (err < 0) {
  857. dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
  858. break;
  859. }
  860. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  861. if (err < 0)
  862. dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
  863. break;
  864. }
  865. case VIDIOCSYNC: /* wait for a frame */
  866. {
  867. int *i = arg;
  868. memset(&buf2,0,sizeof(buf2));
  869. buf2.index = *i;
  870. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  871. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  872. if (err < 0) {
  873. /* No such buffer */
  874. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  875. break;
  876. }
  877. if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
  878. /* Buffer is not mapped */
  879. err = -EINVAL;
  880. break;
  881. }
  882. /* make sure capture actually runs so we don't block forever */
  883. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  884. if (err < 0) {
  885. dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
  886. break;
  887. }
  888. /* Loop as long as the buffer is queued, but not done */
  889. while ((buf2.flags &
  890. (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
  891. == V4L2_BUF_FLAG_QUEUED)
  892. {
  893. err = poll_one(file);
  894. if (err < 0 || /* error or sleep was interrupted */
  895. err == 0) /* timeout? Shouldn't occur. */
  896. break;
  897. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  898. if (err < 0)
  899. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  900. }
  901. if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
  902. break;
  903. do {
  904. err = drv(inode, file, VIDIOC_DQBUF, &buf2);
  905. if (err < 0)
  906. dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
  907. } while (err == 0 && buf2.index != *i);
  908. break;
  909. }
  910. case VIDIOCGVBIFMT: /* query VBI data capture format */
  911. {
  912. struct vbi_format *fmt = arg;
  913. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  914. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  915. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  916. if (err < 0) {
  917. dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
  918. break;
  919. }
  920. if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
  921. err = -EINVAL;
  922. break;
  923. }
  924. memset(fmt, 0, sizeof(*fmt));
  925. fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
  926. fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
  927. fmt->sample_format = VIDEO_PALETTE_RAW;
  928. fmt->start[0] = fmt2->fmt.vbi.start[0];
  929. fmt->count[0] = fmt2->fmt.vbi.count[0];
  930. fmt->start[1] = fmt2->fmt.vbi.start[1];
  931. fmt->count[1] = fmt2->fmt.vbi.count[1];
  932. fmt->flags = fmt2->fmt.vbi.flags & 0x03;
  933. break;
  934. }
  935. case VIDIOCSVBIFMT:
  936. {
  937. struct vbi_format *fmt = arg;
  938. if (VIDEO_PALETTE_RAW != fmt->sample_format) {
  939. err = -EINVAL;
  940. break;
  941. }
  942. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  943. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  944. fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
  945. fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
  946. fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  947. fmt2->fmt.vbi.start[0] = fmt->start[0];
  948. fmt2->fmt.vbi.count[0] = fmt->count[0];
  949. fmt2->fmt.vbi.start[1] = fmt->start[1];
  950. fmt2->fmt.vbi.count[1] = fmt->count[1];
  951. fmt2->fmt.vbi.flags = fmt->flags;
  952. err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
  953. if (err < 0) {
  954. dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
  955. break;
  956. }
  957. if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
  958. fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
  959. fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
  960. fmt2->fmt.vbi.start[0] != fmt->start[0] ||
  961. fmt2->fmt.vbi.count[0] != fmt->count[0] ||
  962. fmt2->fmt.vbi.start[1] != fmt->start[1] ||
  963. fmt2->fmt.vbi.count[1] != fmt->count[1] ||
  964. fmt2->fmt.vbi.flags != fmt->flags) {
  965. err = -EINVAL;
  966. break;
  967. }
  968. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  969. if (err < 0)
  970. dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
  971. break;
  972. }
  973. default:
  974. err = -ENOIOCTLCMD;
  975. break;
  976. }
  977. kfree(cap2);
  978. kfree(fmt2);
  979. return err;
  980. }
  981. EXPORT_SYMBOL(v4l_compat_translate_ioctl);
  982. /*
  983. * Local variables:
  984. * c-basic-offset: 8
  985. * End:
  986. */