v4l1-compat.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. 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. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  581. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  582. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  583. if (err < 0)
  584. dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
  585. if (fmt2->fmt.pix.pixelformat !=
  586. palette_to_pixelformat(pict->palette)) {
  587. fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
  588. pict->palette);
  589. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  590. if (err < 0)
  591. dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
  592. }
  593. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  594. if (err < 0)
  595. dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
  596. if (fbuf2.fmt.pixelformat !=
  597. palette_to_pixelformat(pict->palette)) {
  598. fbuf2.fmt.pixelformat = palette_to_pixelformat(
  599. pict->palette);
  600. err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  601. if (err < 0)
  602. dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
  603. err = 0; /* likely fails for non-root */
  604. }
  605. break;
  606. }
  607. case VIDIOCGTUNER: /* get tuner information */
  608. {
  609. struct video_tuner *tun = arg;
  610. memset(&tun2,0,sizeof(tun2));
  611. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  612. if (err < 0) {
  613. dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
  614. break;
  615. }
  616. memcpy(tun->name, tun2.name,
  617. min(sizeof(tun->name), sizeof(tun2.name)));
  618. tun->name[sizeof(tun->name) - 1] = 0;
  619. tun->rangelow = tun2.rangelow;
  620. tun->rangehigh = tun2.rangehigh;
  621. tun->flags = 0;
  622. tun->mode = VIDEO_MODE_AUTO;
  623. for (i = 0; i < 64; i++) {
  624. memset(&std2,0,sizeof(std2));
  625. std2.index = i;
  626. if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
  627. break;
  628. if (std2.id & V4L2_STD_PAL)
  629. tun->flags |= VIDEO_TUNER_PAL;
  630. if (std2.id & V4L2_STD_NTSC)
  631. tun->flags |= VIDEO_TUNER_NTSC;
  632. if (std2.id & V4L2_STD_SECAM)
  633. tun->flags |= VIDEO_TUNER_SECAM;
  634. }
  635. err = drv(inode, file, VIDIOC_G_STD, &sid);
  636. if (err < 0)
  637. dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
  638. if (err == 0) {
  639. if (sid & V4L2_STD_PAL)
  640. tun->mode = VIDEO_MODE_PAL;
  641. if (sid & V4L2_STD_NTSC)
  642. tun->mode = VIDEO_MODE_NTSC;
  643. if (sid & V4L2_STD_SECAM)
  644. tun->mode = VIDEO_MODE_SECAM;
  645. }
  646. if (tun2.capability & V4L2_TUNER_CAP_LOW)
  647. tun->flags |= VIDEO_TUNER_LOW;
  648. if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  649. tun->flags |= VIDEO_TUNER_STEREO_ON;
  650. tun->signal = tun2.signal;
  651. break;
  652. }
  653. case VIDIOCSTUNER: /* select a tuner input */
  654. {
  655. struct video_tuner *tun = arg;
  656. struct v4l2_tuner t;
  657. memset(&t,0,sizeof(t));
  658. t.index=tun->tuner;
  659. err = drv(inode, file, VIDIOC_S_INPUT, &t);
  660. if (err < 0)
  661. dprintk("VIDIOCSTUNER / VIDIOC_S_INPUT: %d\n",err);
  662. break;
  663. }
  664. case VIDIOCGFREQ: /* get frequency */
  665. {
  666. unsigned long *freq = arg;
  667. memset(&freq2,0,sizeof(freq2));
  668. freq2.tuner = 0;
  669. err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  670. if (err < 0)
  671. dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
  672. if (0 == err)
  673. *freq = freq2.frequency;
  674. break;
  675. }
  676. case VIDIOCSFREQ: /* set frequency */
  677. {
  678. unsigned long *freq = arg;
  679. memset(&freq2,0,sizeof(freq2));
  680. drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  681. freq2.frequency = *freq;
  682. err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
  683. if (err < 0)
  684. dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
  685. break;
  686. }
  687. case VIDIOCGAUDIO: /* get audio properties/controls */
  688. {
  689. struct video_audio *aud = arg;
  690. memset(&aud2,0,sizeof(aud2));
  691. err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
  692. if (err < 0) {
  693. dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
  694. break;
  695. }
  696. memcpy(aud->name, aud2.name,
  697. min(sizeof(aud->name), sizeof(aud2.name)));
  698. aud->name[sizeof(aud->name) - 1] = 0;
  699. aud->audio = aud2.index;
  700. aud->flags = 0;
  701. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
  702. if (i >= 0) {
  703. aud->volume = i;
  704. aud->flags |= VIDEO_AUDIO_VOLUME;
  705. }
  706. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
  707. if (i >= 0) {
  708. aud->bass = i;
  709. aud->flags |= VIDEO_AUDIO_BASS;
  710. }
  711. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
  712. if (i >= 0) {
  713. aud->treble = i;
  714. aud->flags |= VIDEO_AUDIO_TREBLE;
  715. }
  716. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
  717. if (i >= 0) {
  718. aud->balance = i;
  719. aud->flags |= VIDEO_AUDIO_BALANCE;
  720. }
  721. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
  722. if (i >= 0) {
  723. if (i)
  724. aud->flags |= VIDEO_AUDIO_MUTE;
  725. aud->flags |= VIDEO_AUDIO_MUTABLE;
  726. }
  727. aud->step = 1;
  728. qctrl2.id = V4L2_CID_AUDIO_VOLUME;
  729. if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
  730. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  731. aud->step = qctrl2.step;
  732. aud->mode = 0;
  733. memset(&tun2,0,sizeof(tun2));
  734. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  735. if (err < 0) {
  736. dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
  737. err = 0;
  738. break;
  739. }
  740. if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
  741. aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  742. else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  743. aud->mode = VIDEO_SOUND_STEREO;
  744. else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
  745. aud->mode = VIDEO_SOUND_MONO;
  746. break;
  747. }
  748. case VIDIOCSAUDIO: /* set audio controls */
  749. {
  750. struct video_audio *aud = arg;
  751. memset(&aud2,0,sizeof(aud2));
  752. memset(&tun2,0,sizeof(tun2));
  753. aud2.index = aud->audio;
  754. err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
  755. if (err < 0) {
  756. dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
  757. break;
  758. }
  759. set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
  760. aud->volume, drv);
  761. set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
  762. aud->bass, drv);
  763. set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
  764. aud->treble, drv);
  765. set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
  766. aud->balance, drv);
  767. set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
  768. !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
  769. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  770. if (err < 0)
  771. dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
  772. if (err == 0) {
  773. switch (aud->mode) {
  774. default:
  775. case VIDEO_SOUND_MONO:
  776. case VIDEO_SOUND_LANG1:
  777. tun2.audmode = V4L2_TUNER_MODE_MONO;
  778. break;
  779. case VIDEO_SOUND_STEREO:
  780. tun2.audmode = V4L2_TUNER_MODE_STEREO;
  781. break;
  782. case VIDEO_SOUND_LANG2:
  783. tun2.audmode = V4L2_TUNER_MODE_LANG2;
  784. break;
  785. }
  786. err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
  787. if (err < 0)
  788. dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
  789. }
  790. err = 0;
  791. break;
  792. }
  793. case VIDIOCMCAPTURE: /* capture a frame */
  794. {
  795. struct video_mmap *mm = arg;
  796. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  797. memset(&buf2,0,sizeof(buf2));
  798. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  799. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  800. if (err < 0) {
  801. dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
  802. break;
  803. }
  804. if (mm->width != fmt2->fmt.pix.width ||
  805. mm->height != fmt2->fmt.pix.height ||
  806. palette_to_pixelformat(mm->format) !=
  807. fmt2->fmt.pix.pixelformat)
  808. {/* New capture format... */
  809. fmt2->fmt.pix.width = mm->width;
  810. fmt2->fmt.pix.height = mm->height;
  811. fmt2->fmt.pix.pixelformat =
  812. palette_to_pixelformat(mm->format);
  813. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  814. fmt2->fmt.pix.bytesperline = 0;
  815. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  816. if (err < 0) {
  817. dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
  818. break;
  819. }
  820. }
  821. buf2.index = mm->frame;
  822. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  823. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  824. if (err < 0) {
  825. dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
  826. break;
  827. }
  828. err = drv(inode, file, VIDIOC_QBUF, &buf2);
  829. if (err < 0) {
  830. dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
  831. break;
  832. }
  833. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  834. if (err < 0)
  835. dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
  836. break;
  837. }
  838. case VIDIOCSYNC: /* wait for a frame */
  839. {
  840. int *i = arg;
  841. memset(&buf2,0,sizeof(buf2));
  842. buf2.index = *i;
  843. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  844. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  845. if (err < 0) {
  846. /* No such buffer */
  847. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  848. break;
  849. }
  850. if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
  851. /* Buffer is not mapped */
  852. err = -EINVAL;
  853. break;
  854. }
  855. /* make sure capture actually runs so we don't block forever */
  856. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  857. if (err < 0) {
  858. dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
  859. break;
  860. }
  861. /* Loop as long as the buffer is queued, but not done */
  862. while ((buf2.flags &
  863. (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
  864. == V4L2_BUF_FLAG_QUEUED)
  865. {
  866. err = poll_one(file);
  867. if (err < 0 || /* error or sleep was interrupted */
  868. err == 0) /* timeout? Shouldn't occur. */
  869. break;
  870. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  871. if (err < 0)
  872. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  873. }
  874. if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
  875. break;
  876. do {
  877. err = drv(inode, file, VIDIOC_DQBUF, &buf2);
  878. if (err < 0)
  879. dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
  880. } while (err == 0 && buf2.index != *i);
  881. break;
  882. }
  883. case VIDIOCGVBIFMT: /* query VBI data capture format */
  884. {
  885. struct vbi_format *fmt = arg;
  886. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  887. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  888. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  889. if (err < 0) {
  890. dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
  891. break;
  892. }
  893. if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
  894. err = -EINVAL;
  895. break;
  896. }
  897. memset(fmt, 0, sizeof(*fmt));
  898. fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
  899. fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
  900. fmt->sample_format = VIDEO_PALETTE_RAW;
  901. fmt->start[0] = fmt2->fmt.vbi.start[0];
  902. fmt->count[0] = fmt2->fmt.vbi.count[0];
  903. fmt->start[1] = fmt2->fmt.vbi.start[1];
  904. fmt->count[1] = fmt2->fmt.vbi.count[1];
  905. fmt->flags = fmt2->fmt.vbi.flags & 0x03;
  906. break;
  907. }
  908. case VIDIOCSVBIFMT:
  909. {
  910. struct vbi_format *fmt = arg;
  911. if (VIDEO_PALETTE_RAW != fmt->sample_format) {
  912. err = -EINVAL;
  913. break;
  914. }
  915. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  916. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  917. fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
  918. fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
  919. fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  920. fmt2->fmt.vbi.start[0] = fmt->start[0];
  921. fmt2->fmt.vbi.count[0] = fmt->count[0];
  922. fmt2->fmt.vbi.start[1] = fmt->start[1];
  923. fmt2->fmt.vbi.count[1] = fmt->count[1];
  924. fmt2->fmt.vbi.flags = fmt->flags;
  925. err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
  926. if (err < 0) {
  927. dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
  928. break;
  929. }
  930. if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
  931. fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
  932. fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
  933. fmt2->fmt.vbi.start[0] != fmt->start[0] ||
  934. fmt2->fmt.vbi.count[0] != fmt->count[0] ||
  935. fmt2->fmt.vbi.start[1] != fmt->start[1] ||
  936. fmt2->fmt.vbi.count[1] != fmt->count[1] ||
  937. fmt2->fmt.vbi.flags != fmt->flags) {
  938. err = -EINVAL;
  939. break;
  940. }
  941. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  942. if (err < 0)
  943. dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
  944. break;
  945. }
  946. default:
  947. err = -ENOIOCTLCMD;
  948. break;
  949. }
  950. kfree(cap2);
  951. kfree(fmt2);
  952. return err;
  953. }
  954. EXPORT_SYMBOL(v4l_compat_translate_ioctl);
  955. /*
  956. * Local variables:
  957. * c-basic-offset: 8
  958. * End:
  959. */