v4l1-compat.c 27 KB

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