v4l1-compat.c 28 KB

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