v4l1-compat.c 28 KB

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