v4l1-compat.c 27 KB

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