v4l1-compat.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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/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/smp_lock.h>
  25. #include <linux/mm.h>
  26. #include <linux/fs.h>
  27. #include <linux/file.h>
  28. #include <linux/string.h>
  29. #include <linux/errno.h>
  30. #include <linux/slab.h>
  31. #include <linux/videodev.h>
  32. #include <media/v4l2-common.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/system.h>
  35. #include <asm/pgtable.h>
  36. #ifdef CONFIG_KMOD
  37. #include <linux/kmod.h>
  38. #endif
  39. static unsigned int debug = 0;
  40. module_param(debug, int, 0644);
  41. MODULE_PARM_DESC(debug,"enable debug messages");
  42. MODULE_AUTHOR("Bill Dirks");
  43. MODULE_DESCRIPTION("v4l(1) compatibility layer for v4l2 drivers.");
  44. MODULE_LICENSE("GPL");
  45. #define dprintk(fmt, arg...) if (debug) \
  46. printk(KERN_DEBUG "v4l1-compat: " fmt , ## arg)
  47. /*
  48. * I O C T L T R A N S L A T I O N
  49. *
  50. * From here on down is the code for translating the numerous
  51. * ioctl commands from the old API to the new API.
  52. */
  53. static int
  54. get_v4l_control(struct inode *inode,
  55. struct file *file,
  56. int cid,
  57. v4l2_kioctl drv)
  58. {
  59. struct v4l2_queryctrl qctrl2;
  60. struct v4l2_control ctrl2;
  61. int err;
  62. qctrl2.id = cid;
  63. err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
  64. if (err < 0)
  65. dprintk("VIDIOC_QUERYCTRL: %d\n",err);
  66. if (err == 0 &&
  67. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  68. {
  69. ctrl2.id = qctrl2.id;
  70. err = drv(inode, file, VIDIOC_G_CTRL, &ctrl2);
  71. if (err < 0) {
  72. dprintk("VIDIOC_G_CTRL: %d\n",err);
  73. return 0;
  74. }
  75. return ((ctrl2.value - qctrl2.minimum) * 65535
  76. + (qctrl2.maximum - qctrl2.minimum) / 2)
  77. / (qctrl2.maximum - qctrl2.minimum);
  78. }
  79. return 0;
  80. }
  81. static int
  82. set_v4l_control(struct inode *inode,
  83. struct file *file,
  84. int cid,
  85. int value,
  86. v4l2_kioctl drv)
  87. {
  88. struct v4l2_queryctrl qctrl2;
  89. struct v4l2_control ctrl2;
  90. int err;
  91. qctrl2.id = cid;
  92. err = drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2);
  93. if (err < 0)
  94. dprintk("VIDIOC_QUERYCTRL: %d\n",err);
  95. if (err == 0 &&
  96. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED) &&
  97. !(qctrl2.flags & V4L2_CTRL_FLAG_GRABBED))
  98. {
  99. if (value < 0)
  100. value = 0;
  101. if (value > 65535)
  102. value = 65535;
  103. if (value && qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
  104. value = 65535;
  105. ctrl2.id = qctrl2.id;
  106. ctrl2.value =
  107. (value * (qctrl2.maximum - qctrl2.minimum)
  108. + 32767)
  109. / 65535;
  110. ctrl2.value += qctrl2.minimum;
  111. err = drv(inode, file, VIDIOC_S_CTRL, &ctrl2);
  112. if (err < 0)
  113. dprintk("VIDIOC_S_CTRL: %d\n",err);
  114. }
  115. return 0;
  116. }
  117. /* ----------------------------------------------------------------- */
  118. static int palette2pixelformat[] = {
  119. [VIDEO_PALETTE_GREY] = V4L2_PIX_FMT_GREY,
  120. [VIDEO_PALETTE_RGB555] = V4L2_PIX_FMT_RGB555,
  121. [VIDEO_PALETTE_RGB565] = V4L2_PIX_FMT_RGB565,
  122. [VIDEO_PALETTE_RGB24] = V4L2_PIX_FMT_BGR24,
  123. [VIDEO_PALETTE_RGB32] = V4L2_PIX_FMT_BGR32,
  124. /* yuv packed pixel */
  125. [VIDEO_PALETTE_YUYV] = V4L2_PIX_FMT_YUYV,
  126. [VIDEO_PALETTE_YUV422] = V4L2_PIX_FMT_YUYV,
  127. [VIDEO_PALETTE_UYVY] = V4L2_PIX_FMT_UYVY,
  128. /* yuv planar */
  129. [VIDEO_PALETTE_YUV410P] = V4L2_PIX_FMT_YUV410,
  130. [VIDEO_PALETTE_YUV420] = V4L2_PIX_FMT_YUV420,
  131. [VIDEO_PALETTE_YUV420P] = V4L2_PIX_FMT_YUV420,
  132. [VIDEO_PALETTE_YUV411P] = V4L2_PIX_FMT_YUV411P,
  133. [VIDEO_PALETTE_YUV422P] = V4L2_PIX_FMT_YUV422P,
  134. };
  135. static unsigned int
  136. palette_to_pixelformat(unsigned int palette)
  137. {
  138. if (palette < ARRAY_SIZE(palette2pixelformat))
  139. return palette2pixelformat[palette];
  140. else
  141. return 0;
  142. }
  143. static unsigned int
  144. pixelformat_to_palette(int pixelformat)
  145. {
  146. int palette = 0;
  147. switch (pixelformat)
  148. {
  149. case V4L2_PIX_FMT_GREY:
  150. palette = VIDEO_PALETTE_GREY;
  151. break;
  152. case V4L2_PIX_FMT_RGB555:
  153. palette = VIDEO_PALETTE_RGB555;
  154. break;
  155. case V4L2_PIX_FMT_RGB565:
  156. palette = VIDEO_PALETTE_RGB565;
  157. break;
  158. case V4L2_PIX_FMT_BGR24:
  159. palette = VIDEO_PALETTE_RGB24;
  160. break;
  161. case V4L2_PIX_FMT_BGR32:
  162. palette = VIDEO_PALETTE_RGB32;
  163. break;
  164. /* yuv packed pixel */
  165. case V4L2_PIX_FMT_YUYV:
  166. palette = VIDEO_PALETTE_YUYV;
  167. break;
  168. case V4L2_PIX_FMT_UYVY:
  169. palette = VIDEO_PALETTE_UYVY;
  170. break;
  171. /* yuv planar */
  172. case V4L2_PIX_FMT_YUV410:
  173. palette = VIDEO_PALETTE_YUV420;
  174. break;
  175. case V4L2_PIX_FMT_YUV420:
  176. palette = VIDEO_PALETTE_YUV420;
  177. break;
  178. case V4L2_PIX_FMT_YUV411P:
  179. palette = VIDEO_PALETTE_YUV411P;
  180. break;
  181. case V4L2_PIX_FMT_YUV422P:
  182. palette = VIDEO_PALETTE_YUV422P;
  183. break;
  184. }
  185. return palette;
  186. }
  187. /* ----------------------------------------------------------------- */
  188. static int poll_one(struct file *file)
  189. {
  190. int retval = 1;
  191. poll_table *table;
  192. struct poll_wqueues pwq;
  193. poll_initwait(&pwq);
  194. table = &pwq.pt;
  195. for (;;) {
  196. int mask;
  197. set_current_state(TASK_INTERRUPTIBLE);
  198. mask = file->f_op->poll(file, table);
  199. if (mask & POLLIN)
  200. break;
  201. table = NULL;
  202. if (signal_pending(current)) {
  203. retval = -ERESTARTSYS;
  204. break;
  205. }
  206. schedule();
  207. }
  208. set_current_state(TASK_RUNNING);
  209. poll_freewait(&pwq);
  210. return retval;
  211. }
  212. static int count_inputs(struct inode *inode,
  213. struct file *file,
  214. v4l2_kioctl drv)
  215. {
  216. struct v4l2_input input2;
  217. int i;
  218. for (i = 0;; i++) {
  219. memset(&input2,0,sizeof(input2));
  220. input2.index = i;
  221. if (0 != drv(inode,file,VIDIOC_ENUMINPUT, &input2))
  222. break;
  223. }
  224. return i;
  225. }
  226. static int check_size(struct inode *inode,
  227. struct file *file,
  228. v4l2_kioctl drv,
  229. int *maxw, int *maxh)
  230. {
  231. struct v4l2_fmtdesc desc2;
  232. struct v4l2_format fmt2;
  233. memset(&desc2,0,sizeof(desc2));
  234. memset(&fmt2,0,sizeof(fmt2));
  235. desc2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  236. if (0 != drv(inode,file,VIDIOC_ENUM_FMT, &desc2))
  237. goto done;
  238. fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  239. fmt2.fmt.pix.width = 10000;
  240. fmt2.fmt.pix.height = 10000;
  241. fmt2.fmt.pix.pixelformat = desc2.pixelformat;
  242. if (0 != drv(inode,file,VIDIOC_TRY_FMT, &fmt2))
  243. goto done;
  244. *maxw = fmt2.fmt.pix.width;
  245. *maxh = fmt2.fmt.pix.height;
  246. done:
  247. return 0;
  248. }
  249. /* ----------------------------------------------------------------- */
  250. /*
  251. * This function is exported.
  252. */
  253. int
  254. v4l_compat_translate_ioctl(struct inode *inode,
  255. struct file *file,
  256. int cmd,
  257. void *arg,
  258. v4l2_kioctl drv)
  259. {
  260. struct v4l2_capability *cap2 = NULL;
  261. struct v4l2_format *fmt2 = NULL;
  262. enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  263. struct v4l2_framebuffer fbuf2;
  264. struct v4l2_input input2;
  265. struct v4l2_tuner tun2;
  266. struct v4l2_standard std2;
  267. struct v4l2_frequency freq2;
  268. struct v4l2_audio aud2;
  269. struct v4l2_queryctrl qctrl2;
  270. struct v4l2_buffer buf2;
  271. v4l2_std_id sid;
  272. int i, err = 0;
  273. switch (cmd) {
  274. case VIDIOCGCAP: /* capability */
  275. {
  276. struct video_capability *cap = arg;
  277. cap2 = kzalloc(sizeof(*cap2),GFP_KERNEL);
  278. memset(cap, 0, sizeof(*cap));
  279. memset(&fbuf2, 0, sizeof(fbuf2));
  280. err = drv(inode, file, VIDIOC_QUERYCAP, cap2);
  281. if (err < 0) {
  282. dprintk("VIDIOCGCAP / VIDIOC_QUERYCAP: %d\n",err);
  283. break;
  284. }
  285. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY) {
  286. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  287. if (err < 0) {
  288. dprintk("VIDIOCGCAP / VIDIOC_G_FBUF: %d\n",err);
  289. memset(&fbuf2, 0, sizeof(fbuf2));
  290. }
  291. err = 0;
  292. }
  293. memcpy(cap->name, cap2->card,
  294. min(sizeof(cap->name), sizeof(cap2->card)));
  295. cap->name[sizeof(cap->name) - 1] = 0;
  296. if (cap2->capabilities & V4L2_CAP_VIDEO_CAPTURE)
  297. cap->type |= VID_TYPE_CAPTURE;
  298. if (cap2->capabilities & V4L2_CAP_TUNER)
  299. cap->type |= VID_TYPE_TUNER;
  300. if (cap2->capabilities & V4L2_CAP_VBI_CAPTURE)
  301. cap->type |= VID_TYPE_TELETEXT;
  302. if (cap2->capabilities & V4L2_CAP_VIDEO_OVERLAY)
  303. cap->type |= VID_TYPE_OVERLAY;
  304. if (fbuf2.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
  305. cap->type |= VID_TYPE_CLIPPING;
  306. cap->channels = count_inputs(inode,file,drv);
  307. check_size(inode,file,drv,
  308. &cap->maxwidth,&cap->maxheight);
  309. cap->audios = 0; /* FIXME */
  310. cap->minwidth = 48; /* FIXME */
  311. cap->minheight = 32; /* FIXME */
  312. break;
  313. }
  314. case VIDIOCGFBUF: /* get frame buffer */
  315. {
  316. struct video_buffer *buffer = arg;
  317. memset(buffer, 0, sizeof(*buffer));
  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. set_v4l_control(inode, file,
  570. V4L2_CID_BRIGHTNESS, pict->brightness, drv);
  571. set_v4l_control(inode, file,
  572. V4L2_CID_HUE, pict->hue, drv);
  573. set_v4l_control(inode, file,
  574. V4L2_CID_CONTRAST, pict->contrast, drv);
  575. set_v4l_control(inode, file,
  576. V4L2_CID_SATURATION, pict->colour, drv);
  577. set_v4l_control(inode, file,
  578. V4L2_CID_WHITENESS, pict->whiteness, drv);
  579. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  580. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  581. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  582. if (err < 0)
  583. dprintk("VIDIOCSPICT / VIDIOC_G_FMT: %d\n",err);
  584. if (fmt2->fmt.pix.pixelformat !=
  585. palette_to_pixelformat(pict->palette)) {
  586. fmt2->fmt.pix.pixelformat = palette_to_pixelformat(
  587. pict->palette);
  588. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  589. if (err < 0)
  590. dprintk("VIDIOCSPICT / VIDIOC_S_FMT: %d\n",err);
  591. }
  592. err = drv(inode, file, VIDIOC_G_FBUF, &fbuf2);
  593. if (err < 0)
  594. dprintk("VIDIOCSPICT / VIDIOC_G_FBUF: %d\n",err);
  595. if (fbuf2.fmt.pixelformat !=
  596. palette_to_pixelformat(pict->palette)) {
  597. fbuf2.fmt.pixelformat = palette_to_pixelformat(
  598. pict->palette);
  599. err = drv(inode, file, VIDIOC_S_FBUF, &fbuf2);
  600. if (err < 0)
  601. dprintk("VIDIOCSPICT / VIDIOC_S_FBUF: %d\n",err);
  602. err = 0; /* likely fails for non-root */
  603. }
  604. break;
  605. }
  606. case VIDIOCGTUNER: /* get tuner information */
  607. {
  608. struct video_tuner *tun = arg;
  609. memset(&tun2,0,sizeof(tun2));
  610. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  611. if (err < 0) {
  612. dprintk("VIDIOCGTUNER / VIDIOC_G_TUNER: %d\n",err);
  613. break;
  614. }
  615. memcpy(tun->name, tun2.name,
  616. min(sizeof(tun->name), sizeof(tun2.name)));
  617. tun->name[sizeof(tun->name) - 1] = 0;
  618. tun->rangelow = tun2.rangelow;
  619. tun->rangehigh = tun2.rangehigh;
  620. tun->flags = 0;
  621. tun->mode = VIDEO_MODE_AUTO;
  622. for (i = 0; i < 64; i++) {
  623. memset(&std2,0,sizeof(std2));
  624. std2.index = i;
  625. if (0 != drv(inode, file, VIDIOC_ENUMSTD, &std2))
  626. break;
  627. if (std2.id & V4L2_STD_PAL)
  628. tun->flags |= VIDEO_TUNER_PAL;
  629. if (std2.id & V4L2_STD_NTSC)
  630. tun->flags |= VIDEO_TUNER_NTSC;
  631. if (std2.id & V4L2_STD_SECAM)
  632. tun->flags |= VIDEO_TUNER_SECAM;
  633. }
  634. err = drv(inode, file, VIDIOC_G_STD, &sid);
  635. if (err < 0)
  636. dprintk("VIDIOCGTUNER / VIDIOC_G_STD: %d\n",err);
  637. if (err == 0) {
  638. if (sid & V4L2_STD_PAL)
  639. tun->mode = VIDEO_MODE_PAL;
  640. if (sid & V4L2_STD_NTSC)
  641. tun->mode = VIDEO_MODE_NTSC;
  642. if (sid & V4L2_STD_SECAM)
  643. tun->mode = VIDEO_MODE_SECAM;
  644. }
  645. if (tun2.capability & V4L2_TUNER_CAP_LOW)
  646. tun->flags |= VIDEO_TUNER_LOW;
  647. if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  648. tun->flags |= VIDEO_TUNER_STEREO_ON;
  649. tun->signal = tun2.signal;
  650. break;
  651. }
  652. case VIDIOCSTUNER: /* select a tuner input */
  653. {
  654. err = 0;
  655. break;
  656. }
  657. case VIDIOCGFREQ: /* get frequency */
  658. {
  659. unsigned long *freq = arg;
  660. freq2.tuner = 0;
  661. err = drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  662. if (err < 0)
  663. dprintk("VIDIOCGFREQ / VIDIOC_G_FREQUENCY: %d\n",err);
  664. if (0 == err)
  665. *freq = freq2.frequency;
  666. break;
  667. }
  668. case VIDIOCSFREQ: /* set frequency */
  669. {
  670. unsigned long *freq = arg;
  671. freq2.tuner = 0;
  672. drv(inode, file, VIDIOC_G_FREQUENCY, &freq2);
  673. freq2.frequency = *freq;
  674. err = drv(inode, file, VIDIOC_S_FREQUENCY, &freq2);
  675. if (err < 0)
  676. dprintk("VIDIOCSFREQ / VIDIOC_S_FREQUENCY: %d\n",err);
  677. break;
  678. }
  679. case VIDIOCGAUDIO: /* get audio properties/controls */
  680. {
  681. struct video_audio *aud = arg;
  682. err = drv(inode, file, VIDIOC_G_AUDIO, &aud2);
  683. if (err < 0) {
  684. dprintk("VIDIOCGAUDIO / VIDIOC_G_AUDIO: %d\n",err);
  685. break;
  686. }
  687. memcpy(aud->name, aud2.name,
  688. min(sizeof(aud->name), sizeof(aud2.name)));
  689. aud->name[sizeof(aud->name) - 1] = 0;
  690. aud->audio = aud2.index;
  691. aud->flags = 0;
  692. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME, drv);
  693. if (i >= 0) {
  694. aud->volume = i;
  695. aud->flags |= VIDEO_AUDIO_VOLUME;
  696. }
  697. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BASS, drv);
  698. if (i >= 0) {
  699. aud->bass = i;
  700. aud->flags |= VIDEO_AUDIO_BASS;
  701. }
  702. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE, drv);
  703. if (i >= 0) {
  704. aud->treble = i;
  705. aud->flags |= VIDEO_AUDIO_TREBLE;
  706. }
  707. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE, drv);
  708. if (i >= 0) {
  709. aud->balance = i;
  710. aud->flags |= VIDEO_AUDIO_BALANCE;
  711. }
  712. i = get_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE, drv);
  713. if (i >= 0) {
  714. if (i)
  715. aud->flags |= VIDEO_AUDIO_MUTE;
  716. aud->flags |= VIDEO_AUDIO_MUTABLE;
  717. }
  718. aud->step = 1;
  719. qctrl2.id = V4L2_CID_AUDIO_VOLUME;
  720. if (drv(inode, file, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
  721. !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
  722. aud->step = qctrl2.step;
  723. aud->mode = 0;
  724. memset(&tun2,0,sizeof(tun2));
  725. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  726. if (err < 0) {
  727. dprintk("VIDIOCGAUDIO / VIDIOC_G_TUNER: %d\n",err);
  728. err = 0;
  729. break;
  730. }
  731. if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
  732. aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  733. else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
  734. aud->mode = VIDEO_SOUND_STEREO;
  735. else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
  736. aud->mode = VIDEO_SOUND_MONO;
  737. break;
  738. }
  739. case VIDIOCSAUDIO: /* set audio controls */
  740. {
  741. struct video_audio *aud = arg;
  742. memset(&aud2,0,sizeof(aud2));
  743. memset(&tun2,0,sizeof(tun2));
  744. aud2.index = aud->audio;
  745. err = drv(inode, file, VIDIOC_S_AUDIO, &aud2);
  746. if (err < 0) {
  747. dprintk("VIDIOCSAUDIO / VIDIOC_S_AUDIO: %d\n",err);
  748. break;
  749. }
  750. set_v4l_control(inode, file, V4L2_CID_AUDIO_VOLUME,
  751. aud->volume, drv);
  752. set_v4l_control(inode, file, V4L2_CID_AUDIO_BASS,
  753. aud->bass, drv);
  754. set_v4l_control(inode, file, V4L2_CID_AUDIO_TREBLE,
  755. aud->treble, drv);
  756. set_v4l_control(inode, file, V4L2_CID_AUDIO_BALANCE,
  757. aud->balance, drv);
  758. set_v4l_control(inode, file, V4L2_CID_AUDIO_MUTE,
  759. !!(aud->flags & VIDEO_AUDIO_MUTE), drv);
  760. err = drv(inode, file, VIDIOC_G_TUNER, &tun2);
  761. if (err < 0)
  762. dprintk("VIDIOCSAUDIO / VIDIOC_G_TUNER: %d\n",err);
  763. if (err == 0) {
  764. switch (aud->mode) {
  765. default:
  766. case VIDEO_SOUND_MONO:
  767. case VIDEO_SOUND_LANG1:
  768. tun2.audmode = V4L2_TUNER_MODE_MONO;
  769. break;
  770. case VIDEO_SOUND_STEREO:
  771. tun2.audmode = V4L2_TUNER_MODE_STEREO;
  772. break;
  773. case VIDEO_SOUND_LANG2:
  774. tun2.audmode = V4L2_TUNER_MODE_LANG2;
  775. break;
  776. }
  777. err = drv(inode, file, VIDIOC_S_TUNER, &tun2);
  778. if (err < 0)
  779. dprintk("VIDIOCSAUDIO / VIDIOC_S_TUNER: %d\n",err);
  780. }
  781. err = 0;
  782. break;
  783. }
  784. case VIDIOCMCAPTURE: /* capture a frame */
  785. {
  786. struct video_mmap *mm = arg;
  787. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  788. memset(&buf2,0,sizeof(buf2));
  789. fmt2->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  790. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  791. if (err < 0) {
  792. dprintk("VIDIOCMCAPTURE / VIDIOC_G_FMT: %d\n",err);
  793. break;
  794. }
  795. if (mm->width != fmt2->fmt.pix.width ||
  796. mm->height != fmt2->fmt.pix.height ||
  797. palette_to_pixelformat(mm->format) !=
  798. fmt2->fmt.pix.pixelformat)
  799. {/* New capture format... */
  800. fmt2->fmt.pix.width = mm->width;
  801. fmt2->fmt.pix.height = mm->height;
  802. fmt2->fmt.pix.pixelformat =
  803. palette_to_pixelformat(mm->format);
  804. fmt2->fmt.pix.field = V4L2_FIELD_ANY;
  805. fmt2->fmt.pix.bytesperline = 0;
  806. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  807. if (err < 0) {
  808. dprintk("VIDIOCMCAPTURE / VIDIOC_S_FMT: %d\n",err);
  809. break;
  810. }
  811. }
  812. buf2.index = mm->frame;
  813. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  814. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  815. if (err < 0) {
  816. dprintk("VIDIOCMCAPTURE / VIDIOC_QUERYBUF: %d\n",err);
  817. break;
  818. }
  819. err = drv(inode, file, VIDIOC_QBUF, &buf2);
  820. if (err < 0) {
  821. dprintk("VIDIOCMCAPTURE / VIDIOC_QBUF: %d\n",err);
  822. break;
  823. }
  824. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  825. if (err < 0)
  826. dprintk("VIDIOCMCAPTURE / VIDIOC_STREAMON: %d\n",err);
  827. break;
  828. }
  829. case VIDIOCSYNC: /* wait for a frame */
  830. {
  831. int *i = arg;
  832. buf2.index = *i;
  833. buf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  834. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  835. if (err < 0) {
  836. /* No such buffer */
  837. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  838. break;
  839. }
  840. if (!(buf2.flags & V4L2_BUF_FLAG_MAPPED)) {
  841. /* Buffer is not mapped */
  842. err = -EINVAL;
  843. break;
  844. }
  845. /* make sure capture actually runs so we don't block forever */
  846. err = drv(inode, file, VIDIOC_STREAMON, &captype);
  847. if (err < 0) {
  848. dprintk("VIDIOCSYNC / VIDIOC_STREAMON: %d\n",err);
  849. break;
  850. }
  851. /* Loop as long as the buffer is queued, but not done */
  852. while ((buf2.flags &
  853. (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
  854. == V4L2_BUF_FLAG_QUEUED)
  855. {
  856. err = poll_one(file);
  857. if (err < 0 || /* error or sleep was interrupted */
  858. err == 0) /* timeout? Shouldn't occur. */
  859. break;
  860. err = drv(inode, file, VIDIOC_QUERYBUF, &buf2);
  861. if (err < 0)
  862. dprintk("VIDIOCSYNC / VIDIOC_QUERYBUF: %d\n",err);
  863. }
  864. if (!(buf2.flags & V4L2_BUF_FLAG_DONE)) /* not done */
  865. break;
  866. do {
  867. err = drv(inode, file, VIDIOC_DQBUF, &buf2);
  868. if (err < 0)
  869. dprintk("VIDIOCSYNC / VIDIOC_DQBUF: %d\n",err);
  870. } while (err == 0 && buf2.index != *i);
  871. break;
  872. }
  873. case VIDIOCGVBIFMT: /* query VBI data capture format */
  874. {
  875. struct vbi_format *fmt = arg;
  876. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  877. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  878. err = drv(inode, file, VIDIOC_G_FMT, fmt2);
  879. if (err < 0) {
  880. dprintk("VIDIOCGVBIFMT / VIDIOC_G_FMT: %d\n", err);
  881. break;
  882. }
  883. if (fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
  884. err = -EINVAL;
  885. break;
  886. }
  887. memset(fmt, 0, sizeof(*fmt));
  888. fmt->samples_per_line = fmt2->fmt.vbi.samples_per_line;
  889. fmt->sampling_rate = fmt2->fmt.vbi.sampling_rate;
  890. fmt->sample_format = VIDEO_PALETTE_RAW;
  891. fmt->start[0] = fmt2->fmt.vbi.start[0];
  892. fmt->count[0] = fmt2->fmt.vbi.count[0];
  893. fmt->start[1] = fmt2->fmt.vbi.start[1];
  894. fmt->count[1] = fmt2->fmt.vbi.count[1];
  895. fmt->flags = fmt2->fmt.vbi.flags & 0x03;
  896. break;
  897. }
  898. case VIDIOCSVBIFMT:
  899. {
  900. struct vbi_format *fmt = arg;
  901. if (VIDEO_PALETTE_RAW != fmt->sample_format) {
  902. err = -EINVAL;
  903. break;
  904. }
  905. fmt2 = kzalloc(sizeof(*fmt2),GFP_KERNEL);
  906. fmt2->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  907. fmt2->fmt.vbi.samples_per_line = fmt->samples_per_line;
  908. fmt2->fmt.vbi.sampling_rate = fmt->sampling_rate;
  909. fmt2->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  910. fmt2->fmt.vbi.start[0] = fmt->start[0];
  911. fmt2->fmt.vbi.count[0] = fmt->count[0];
  912. fmt2->fmt.vbi.start[1] = fmt->start[1];
  913. fmt2->fmt.vbi.count[1] = fmt->count[1];
  914. fmt2->fmt.vbi.flags = fmt->flags;
  915. err = drv(inode, file, VIDIOC_TRY_FMT, fmt2);
  916. if (err < 0) {
  917. dprintk("VIDIOCSVBIFMT / VIDIOC_TRY_FMT: %d\n", err);
  918. break;
  919. }
  920. if (fmt2->fmt.vbi.samples_per_line != fmt->samples_per_line ||
  921. fmt2->fmt.vbi.sampling_rate != fmt->sampling_rate ||
  922. fmt2->fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
  923. fmt2->fmt.vbi.start[0] != fmt->start[0] ||
  924. fmt2->fmt.vbi.count[0] != fmt->count[0] ||
  925. fmt2->fmt.vbi.start[1] != fmt->start[1] ||
  926. fmt2->fmt.vbi.count[1] != fmt->count[1] ||
  927. fmt2->fmt.vbi.flags != fmt->flags) {
  928. err = -EINVAL;
  929. break;
  930. }
  931. err = drv(inode, file, VIDIOC_S_FMT, fmt2);
  932. if (err < 0)
  933. dprintk("VIDIOCSVBIFMT / VIDIOC_S_FMT: %d\n", err);
  934. break;
  935. }
  936. default:
  937. err = -ENOIOCTLCMD;
  938. break;
  939. }
  940. kfree(cap2);
  941. kfree(fmt2);
  942. return err;
  943. }
  944. EXPORT_SYMBOL(v4l_compat_translate_ioctl);
  945. /*
  946. * Local variables:
  947. * c-basic-offset: 8
  948. * End:
  949. */