v4l1-compat.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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 = kmalloc(sizeof(*cap2),GFP_KERNEL);
  278. memset(cap, 0, sizeof(*cap));
  279. memset(cap2, 0, sizeof(*cap2));
  280. memset(&fbuf2, 0, sizeof(fbuf2));
  281. err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
  282. if (err < 0) {
  283. dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n",err);
  284. break;
  285. }
  286. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
  287. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  288. if (err < 0) {
  289. dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n",err);
  290. memset(&fbuf2, 0, sizeof(fbuf2));
  291. }
  292. err = 0;
  293. }
  294. memcpy(cap->name, cap2->card,
  295. min(sizeof(cap->name), sizeof(cap2->card)));
  296. cap->name[sizeof(cap->name) - 1] = 0;
  297. if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
  298. cap->type |= VID_TYPE_CAPTURE;
  299. if (cap2->capabilities & V4L2_CAP_TUNER)
  300. cap->type |= VID_TYPE_TUNER;
  301. if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
  302. cap->type |= VID_TYPE_TELETEXT;
  303. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
  304. cap->type |= VID_TYPE_OVERLAY;
  305. if (fbuf2.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
  306. cap->type |= VID_TYPE_CLIPPING;
  307. cap->channels = count_inputs(inode,file,drv);
  308. check_size(inode,file,drv,
  309. &cap->maxwidth,&cap->maxheight);
  310. cap->audios = 0; /* FIXME */
  311. cap->minwidth = 48; /* FIXME */
  312. cap->minheight = 32; /* FIXME */
  313. break;
  314. }
  315. case VIDIOCGFBUF: /* get frame buffer */
  316. {
  317. struct video_buffer *buffer = arg;
  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 (0 != fbuf2.fmt.bytesperline)
  346. buffer->bytesperline = fbuf2.fmt.bytesperline;
  347. else {
  348. buffer->bytesperline =
  349. (buffer->width * buffer->depth + 7) & 7;
  350. buffer->bytesperline >>= 3;
  351. }
  352. break;
  353. }
  354. case VIDIOCSFBUF: /* set frame buffer */
  355. {
  356. struct video_buffer *buffer = arg;
  357. memset(&fbuf2, 0, sizeof(fbuf2));
  358. fbuf2.base = buffer->base;
  359. fbuf2.fmt.height = buffer->height;
  360. fbuf2.fmt.width = buffer->width;
  361. switch (buffer->depth) {
  362. case 8:
  363. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
  364. break;
  365. case 15:
  366. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
  367. break;
  368. case 16:
  369. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
  370. break;
  371. case 24:
  372. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
  373. break;
  374. case 32:
  375. fbuf2.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
  376. break;
  377. }
  378. fbuf2.fmt.bytesperline = buffer->bytesperline;
  379. err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  380. if (err < 0)
  381. dprintk("VIDIOCSFBUF / VIDIOC_S_FBUF: %d\n",err);
  382. break;
  383. }
  384. case VIDIOCGWIN: /* get window or capture dimensions */
  385. {
  386. struct video_window *win = arg;
  387. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  388. memset(win,0,sizeof(*win));
  389. memset(fmt2,0,sizeof(*fmt2));
  390. fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
  391. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  392. if (err < 0)
  393. dprintk("VIDIOCGWIN / VIDIOC_G_WIN: %d\n",err);
  394. if (err == 0) {
  395. win->x = fmt2->fmt.win.w.left;
  396. win->y = fmt2->fmt.win.w.top;
  397. win->width = fmt2->fmt.win.w.width;
  398. win->height = fmt2->fmt.win.w.height;
  399. win->chromakey = fmt2->fmt.win.chromakey;
  400. win->clips = NULL;
  401. win->clipcount = 0;
  402. break;
  403. }
  404. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  405. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  406. if (err < 0) {
  407. dprintk("VIDIOCGWIN / VIDIOC_G_FMT: %d\n",err);
  408. break;
  409. }
  410. win->x = 0;
  411. win->y = 0;
  412. win->width = fmt2->fmt.pix.width;
  413. win->height = fmt2->fmt.pix.height;
  414. win->chromakey = 0;
  415. win->clips = NULL;
  416. win->clipcount = 0;
  417. break;
  418. }
  419. case VIDIOCSWIN: /* set window and/or capture dimensions */
  420. {
  421. struct video_window *win = arg;
  422. int err1,err2;
  423. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  424. memset(fmt2,0,sizeof(*fmt2));
  425. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  426. drv(inode, file, VIDIOC_STREAMOFF, &fmt2->type);
  427. err1 = drv(inode, file, VIDIOC_G_FMT, fmt2);
  428. if (err1 < 0)
  429. dprintk("VIDIOCSWIN / VIDIOC_G_FMT: %d\n",err);
  430. if (err1 == 0) {
  431. fmt2->fmt.pix.width = win->width;
  432. fmt2->fmt.pix.height = win->height;
  433. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  434. fmt2->fmt.pix.bytesperline = 0;
  435. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  436. if (err < 0)
  437. dprintk("VIDIOCSWIN / VIDIOC_S_FMT #1: %d\n",
  438. err);
  439. win->width = fmt2->fmt.pix.width;
  440. win->height = fmt2->fmt.pix.height;
  441. }
  442. memset(fmt2,0,sizeof(*fmt2));
  443. fmt2->type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
  444. fmt2->fmt.win.w.left = win->x;
  445. fmt2->fmt.win.w.top = win->y;
  446. fmt2->fmt.win.w.width = win->width;
  447. fmt2->fmt.win.w.height = win->height;
  448. fmt2->fmt.win.chromakey = win->chromakey;
  449. fmt2->fmt.win.clips = (void __user *)win->clips;
  450. fmt2->fmt.win.clipcount = win->clipcount;
  451. err2 = drv(inode, file, VIDIOC_S_FMT, fmt2);
  452. if (err2 < 0)
  453. dprintk("VIDIOCSWIN / VIDIOC_S_FMT #2: %d\n",err);
  454. if (err1 != 0 && err2 != 0)
  455. err = err1;
  456. break;
  457. }
  458. case VIDIOCCAPTURE: /* turn on/off preview */
  459. {
  460. int *on = arg;
  461. if (0 == *on) {
  462. /* dirty hack time. But v4l1 has no STREAMOFF
  463. * equivalent in the API, and this one at
  464. * least comes close ... */
  465. drv(inode, file, VIDIOC_STREAMOFF, &captype);
  466. }
  467. err = drv(inode, file, VIDIOC_OVERLAY, arg);
  468. if (err < 0)
  469. dprintk("VIDIOCCAPTURE / VIDIOC_PREVIEW: %d\n",err);
  470. break;
  471. }
  472. case VIDIOCGCHAN: /* get input information */
  473. {
  474. struct video_channel *chan = arg;
  475. memset(&input2,0,sizeof(input2));
  476. input2.index = chan->channel;
  477. err = drv(inode, file, VIDIOC_ENUMINPUT, &input2);
  478. if (err < 0) {
  479. dprintk("VIDIOCGCHAN / VIDIOC_ENUMINPUT: "
  480. "channel=%d err=%d\n",chan->channel,err);
  481. break;
  482. }
  483. chan->channel = input2.index;
  484. memcpy(chan->name, input2.name,
  485. min(sizeof(chan->name), sizeof(input2.name)));
  486. chan->name[sizeof(chan->name) - 1] = 0;
  487. chan->tuners = (input2.type == V4L2_INPUT_TYPE_TUNER) ? 1 : 0;
  488. chan->flags = (chan->tuners) ? VIDEO_VC_TUNER : 0;
  489. switch (input2.type) {
  490. case V4L2_INPUT_TYPE_TUNER:
  491. chan->type = VIDEO_TYPE_TV;
  492. break;
  493. default:
  494. case V4L2_INPUT_TYPE_CAMERA:
  495. chan->type = VIDEO_TYPE_CAMERA;
  496. break;
  497. }
  498. chan->norm = 0;
  499. err = drv(inode, file, VIDIOC_G_STD, &sid);
  500. if (err < 0)
  501. dprintk("VIDIOCGCHAN / VIDIOC_G_STD: %d\n",err);
  502. if (err == 0) {
  503. if (sid & V4L2_STD_PAL)
  504. chan->norm = VIDEO_MODE_PAL;
  505. if (sid & V4L2_STD_NTSC)
  506. chan->norm = VIDEO_MODE_NTSC;
  507. if (sid & V4L2_STD_SECAM)
  508. chan->norm = VIDEO_MODE_SECAM;
  509. }
  510. break;
  511. }
  512. case VIDIOCSCHAN: /* set input */
  513. {
  514. struct video_channel *chan = arg;
  515. sid = 0;
  516. err = drv(inode, file, VIDIOC_S_INPUT, &chan->channel);
  517. if (err < 0)
  518. dprintk("VIDIOCSCHAN / VIDIOC_S_INPUT: %d\n",err);
  519. switch (chan->norm) {
  520. case VIDEO_MODE_PAL:
  521. sid = V4L2_STD_PAL;
  522. break;
  523. case VIDEO_MODE_NTSC:
  524. sid = V4L2_STD_NTSC;
  525. break;
  526. case VIDEO_MODE_SECAM:
  527. sid = V4L2_STD_SECAM;
  528. break;
  529. }
  530. if (0 != sid) {
  531. err = drv(inode, file, VIDIOC_S_STD, &sid);
  532. if (err < 0)
  533. dprintk("VIDIOCSCHAN / VIDIOC_S_STD: %d\n",err);
  534. }
  535. break;
  536. }
  537. case VIDIOCGPICT: /* get tone controls & partial capture format */
  538. {
  539. struct video_picture *pict = arg;
  540. pict->brightness = get_v4l_control(inode, file,
  541. V4L2_CID_BRIGHTNESS,drv);
  542. pict->hue = get_v4l_control(inode, file,
  543. V4L2_CID_HUE, drv);
  544. pict->contrast = get_v4l_control(inode, file,
  545. V4L2_CID_CONTRAST, drv);
  546. pict->colour = get_v4l_control(inode, file,
  547. V4L2_CID_SATURATION, drv);
  548. pict->whiteness = get_v4l_control(inode, file,
  549. V4L2_CID_WHITENESS, drv);
  550. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  551. memset(fmt2,0,sizeof(*fmt2));
  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->palette = pixelformat_to_palette(
  559. fmt2->fmt.pix.pixelformat);
  560. break;
  561. }
  562. case VIDIOCSPICT: /* set tone controls & partial capture format */
  563. {
  564. struct video_picture *pict = arg;
  565. set_v4l_control(inode, file,
  566. V4L2_CID_BRIGHTNESS, pict->brightness, drv);
  567. set_v4l_control(inode, file,
  568. V4L2_CID_HUE, pict->hue, drv);
  569. set_v4l_control(inode, file,
  570. V4L2_CID_CONTRAST, pict->contrast, drv);
  571. set_v4l_control(inode, file,
  572. V4L2_CID_SATURATION, pict->colour, drv);
  573. set_v4l_control(inode, file,
  574. V4L2_CID_WHITENESS, pict->whiteness, drv);
  575. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  576. memset(fmt2,0,sizeof(*fmt2));
  577. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  578. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  579. if (err < 0)
  580. dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
  581. if (fmt2->fmt.pix.pixelformat !=
  582. palette_to_pixelformat(pict->palette)) {
  583. fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
  584. pict->palette);
  585. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  586. if (err < 0)
  587. dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
  588. }
  589. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  590. if (err < 0)
  591. dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
  592. if (fbuf2.fmt.pixelformat !=
  593. palette_to_pixelformat(pict->palette)) {
  594. fbuf2.fmt.pixelformat = palette_to_pixelformat(
  595. pict->palette);
  596. err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  597. if (err < 0)
  598. dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
  599. err = 0; /* likely fails for non-root */
  600. }
  601. break;
  602. }
  603. case VIDIOCGTUNER: /* get tuner information */
  604. {
  605. struct video_tuner *tun = arg;
  606. memset(&tun2,0,sizeof(tun2));
  607. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  608. if (err < 0) {
  609. dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
  610. break;
  611. }
  612. memcpy(tun->name, tun2.name,
  613. min(sizeof(tun->name), sizeof(tun2.name)));
  614. tun->name[sizeof(tun->name) - 1] = 0;
  615. tun->rangelow = tun2.rangelow;
  616. tun->rangehigh = tun2.rangehigh;
  617. tun->flags = 0;
  618. tun->mode = VIDEO_MODE_AUTO;
  619. for (i = 0; i < 64; i++) {
  620. memset(&std2,0,sizeof(std2));
  621. std2.index = i;
  622. if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
  623. break;
  624. if (std2.id & V4L2_STD_PAL)
  625. tun->flags |= VIDEO_TUNER_PAL;
  626. if (std2.id & V4L2_STD_NTSC)
  627. tun->flags |= VIDEO_TUNER_NTSC;
  628. if (std2.id & V4L2_STD_SECAM)
  629. tun->flags |= VIDEO_TUNER_SECAM;
  630. }
  631. err = drv(inode, file, VIDIOC_G_STD, &sid);
  632. if (err < 0)
  633. dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
  634. if (err == 0) {
  635. if (sid & V4L2_STD_PAL)
  636. tun->mode = VIDEO_MODE_PAL;
  637. if (sid & V4L2_STD_NTSC)
  638. tun->mode = VIDEO_MODE_NTSC;
  639. if (sid & V4L2_STD_SECAM)
  640. tun->mode = VIDEO_MODE_SECAM;
  641. }
  642. if (tun2.capability & V4L2_TUNER_CAP_LOW)
  643. tun->flags |= VIDEO_TUNER_LOW;
  644. if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  645. tun->flags |= VIDEO_TUNER_STEREO_ON;
  646. tun->signal = tun2.signal;
  647. break;
  648. }
  649. case VIDIOCSTUNER: /* select a tuner input */
  650. {
  651. err = 0;
  652. break;
  653. }
  654. case VIDIOCGFREQ: /* get frequency */
  655. {
  656. int *freq = arg;
  657. freq2.tuner = 0;
  658. err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  659. if (err < 0)
  660. dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
  661. if (0 == err)
  662. *freq = freq2.frequency;
  663. break;
  664. }
  665. case VIDIOCSFREQ: /* set frequency */
  666. {
  667. int *freq = arg;
  668. freq2.tuner = 0;
  669. drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  670. freq2.frequency = *freq;
  671. err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
  672. if (err < 0)
  673. dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
  674. break;
  675. }
  676. case VIDIOCGAUDIO: /* get audio properties/controls */
  677. {
  678. struct video_audio *aud = arg;
  679. err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
  680. if (err < 0) {
  681. dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
  682. break;
  683. }
  684. memcpy(aud->name, aud2.name,
  685. min(sizeof(aud->name), sizeof(aud2.name)));
  686. aud->name[sizeof(aud->name) - 1] = 0;
  687. aud->audio = aud2.index;
  688. aud->flags = 0;
  689. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
  690. if (i >= 0) {
  691. aud->volume = i;
  692. aud->flags |= VIDEO_AUDIO_VOLUME;
  693. }
  694. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
  695. if (i >= 0) {
  696. aud->bass = i;
  697. aud->flags |= VIDEO_AUDIO_BASS;
  698. }
  699. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
  700. if (i >= 0) {
  701. aud->treble = i;
  702. aud->flags |= VIDEO_AUDIO_TREBLE;
  703. }
  704. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
  705. if (i >= 0) {
  706. aud->balance = i;
  707. aud->flags |= VIDEO_AUDIO_BALANCE;
  708. }
  709. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
  710. if (i >= 0) {
  711. if (i)
  712. aud->flags |= VIDEO_AUDIO_MUTE;
  713. aud->flags |= VIDEO_AUDIO_MUTABLE;
  714. }
  715. aud->step = 1;
  716. qctrl2.id = V4L2_CID_AUDIO_VOLUME;
  717. if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
  718. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  719. aud->step = qctrl2.step;
  720. aud->mode = 0;
  721. memset(&tun2,0,sizeof(tun2));
  722. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  723. if (err < 0) {
  724. dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
  725. err = 0;
  726. break;
  727. }
  728. if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
  729. aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  730. else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  731. aud->mode = VIDEO_SOUND_STEREO;
  732. else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
  733. aud->mode = VIDEO_SOUND_MONO;
  734. break;
  735. }
  736. case VIDIOCSAUDIO: /* set audio controls */
  737. {
  738. struct video_audio *aud = arg;
  739. memset(&aud2,0,sizeof(aud2));
  740. memset(&tun2,0,sizeof(tun2));
  741. aud2.index = aud->audio;
  742. err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
  743. if (err < 0) {
  744. dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
  745. break;
  746. }
  747. set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
  748. aud->volume, drv);
  749. set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
  750. aud->bass, drv);
  751. set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
  752. aud->treble, drv);
  753. set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
  754. aud->balance, drv);
  755. set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
  756. !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
  757. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  758. if (err < 0)
  759. dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
  760. if (err == 0) {
  761. switch (aud->mode) {
  762. default:
  763. case VIDEO_SOUND_MONO:
  764. case VIDEO_SOUND_LANG1:
  765. tun2.audmode = V4L2_TUNER_MODE_MONO;
  766. break;
  767. case VIDEO_SOUND_STEREO:
  768. tun2.audmode = V4L2_TUNER_MODE_STEREO;
  769. break;
  770. case VIDEO_SOUND_LANG2:
  771. tun2.audmode = V4L2_TUNER_MODE_LANG2;
  772. break;
  773. }
  774. err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
  775. if (err < 0)
  776. dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
  777. }
  778. err = 0;
  779. break;
  780. }
  781. case VIDIOCMCAPTURE: /* capture a frame */
  782. {
  783. struct video_mmap *mm = arg;
  784. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  785. memset(&buf2,0,sizeof(buf2));
  786. memset(fmt2,0,sizeof(*fmt2));
  787. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  788. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  789. if (err < 0) {
  790. dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
  791. break;
  792. }
  793. if (mm->width != fmt2->fmt.pix.width ||
  794. mm->height != fmt2->fmt.pix.height ||
  795. palette_to_pixelformat(mm->format) !=
  796. fmt2->fmt.pix.pixelformat)
  797. {/* New capture format... */
  798. fmt2->fmt.pix.width = mm->width;
  799. fmt2->fmt.pix.height = mm->height;
  800. fmt2->fmt.pix.pixelformat =
  801. palette_to_pixelformat(mm->format);
  802. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  803. fmt2->fmt.pix.bytesperline = 0;
  804. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  805. if (err < 0) {
  806. dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
  807. break;
  808. }
  809. }
  810. buf2.index = mm->frame;
  811. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  812. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  813. if (err < 0) {
  814. dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
  815. break;
  816. }
  817. err = drv(inode, file, VIDIOC_QBUF, &buf2);
  818. if (err < 0) {
  819. dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
  820. break;
  821. }
  822. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  823. if (err < 0)
  824. dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
  825. break;
  826. }
  827. case VIDIOCSYNC: /* wait for a frame */
  828. {
  829. int *i = arg;
  830. buf2.index = *i;
  831. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  832. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  833. if (err < 0) {
  834. /* No such buffer */
  835. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  836. break;
  837. }
  838. if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
  839. /* Buffer is not mapped */
  840. err = -EINVAL;
  841. break;
  842. }
  843. /* make sure capture actually runs so we don't block forever */
  844. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  845. if (err < 0) {
  846. dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
  847. break;
  848. }
  849. /* Loop as long as the buffer is queued, but not done */
  850. while ((buf2.flags &
  851. (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
  852. == V4L2_BUF_FLAG_QUEUED)
  853. {
  854. err = poll_one(file);
  855. if (err < 0 || /* error or sleep was interrupted */
  856. err == 0) /* timeout? Shouldn't occur. */
  857. break;
  858. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  859. if (err < 0)
  860. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  861. }
  862. if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
  863. break;
  864. do {
  865. err = drv(inode, file, VIDIOC_DQBUF, &buf2);
  866. if (err < 0)
  867. dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
  868. } while (err == 0 && buf2.index != *i);
  869. break;
  870. }
  871. case VIDIOCGVBIFMT: /* query VBI data capture format */
  872. {
  873. struct vbi_format *fmt = arg;
  874. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  875. memset(fmt2, 0, sizeof(*fmt2));
  876. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  877. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  878. if (err < 0) {
  879. dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
  880. break;
  881. }
  882. memset(fmt, 0, sizeof(*fmt));
  883. fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
  884. fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
  885. fmt->sample_format = VIDEO_PALETTE_RAW;
  886. fmt->start[0] = fmt2->fmt.vbi.start[0];
  887. fmt->count[0] = fmt2->fmt.vbi.count[0];
  888. fmt->start[1] = fmt2->fmt.vbi.start[1];
  889. fmt->count[1] = fmt2->fmt.vbi.count[1];
  890. fmt->flags = fmt2->fmt.vbi.flags & 0x03;
  891. break;
  892. }
  893. case VIDIOCSVBIFMT:
  894. {
  895. struct vbi_format *fmt = arg;
  896. fmt2 = kmalloc(sizeof(*fmt2),GFP_KERNEL);
  897. memset(fmt2, 0, sizeof(*fmt2));
  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. VIDEO_PALETTE_RAW != fmt->sample_format ||
  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. if (cap2)
  933. kfree(cap2);
  934. if (fmt2)
  935. kfree(fmt2);
  936. return err;
  937. }
  938. EXPORT_SYMBOL(v4l_compat_translate_ioctl);
  939. /*
  940. * Local variables:
  941. * c-basic-offset: 8
  942. * End:
  943. */