videodev.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /*
  2. * Video capture interface for Linux version 2
  3. *
  4. * A generic video device interface for the LINUX operating system
  5. * using a set of device structures/vectors for low level operations.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Authors: Alan Cox, <alan@redhat.com> (version 1)
  13. * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  14. *
  15. * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
  16. * - Added procfs support
  17. */
  18. #define dbgarg(cmd, fmt, arg...) \
  19. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
  20. printk (KERN_DEBUG "%s: ", vfd->name); \
  21. v4l_printk_ioctl(cmd); \
  22. printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \
  23. }
  24. #define dbgarg2(fmt, arg...) \
  25. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
  26. printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/mm.h>
  31. #include <linux/string.h>
  32. #include <linux/errno.h>
  33. #include <linux/init.h>
  34. #include <linux/kmod.h>
  35. #include <linux/slab.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #define __OLD_VIDIOC_ /* To allow fixing old calls*/
  39. #include <linux/videodev2.h>
  40. #ifdef CONFIG_VIDEO_V4L1
  41. #include <linux/videodev.h>
  42. #endif
  43. #include <media/v4l2-common.h>
  44. #define VIDEO_NUM_DEVICES 256
  45. #define VIDEO_NAME "video4linux"
  46. /*
  47. * sysfs stuff
  48. */
  49. static ssize_t show_name(struct device *cd,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct video_device *vfd = container_of(cd, struct video_device,
  53. class_dev);
  54. return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
  55. }
  56. struct video_device *video_device_alloc(void)
  57. {
  58. struct video_device *vfd;
  59. vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
  60. return vfd;
  61. }
  62. void video_device_release(struct video_device *vfd)
  63. {
  64. kfree(vfd);
  65. }
  66. static void video_release(struct device *cd)
  67. {
  68. struct video_device *vfd = container_of(cd, struct video_device,
  69. class_dev);
  70. #if 1
  71. /* needed until all drivers are fixed */
  72. if (!vfd->release)
  73. return;
  74. #endif
  75. vfd->release(vfd);
  76. }
  77. static struct device_attribute video_device_attrs[] = {
  78. __ATTR(name, S_IRUGO, show_name, NULL),
  79. __ATTR_NULL
  80. };
  81. static struct class video_class = {
  82. .name = VIDEO_NAME,
  83. .dev_attrs = video_device_attrs,
  84. .dev_release = video_release,
  85. };
  86. /*
  87. * Active devices
  88. */
  89. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  90. static DEFINE_MUTEX(videodev_lock);
  91. struct video_device* video_devdata(struct file *file)
  92. {
  93. return video_device[iminor(file->f_path.dentry->d_inode)];
  94. }
  95. /*
  96. * Open a video device - FIXME: Obsoleted
  97. */
  98. static int video_open(struct inode *inode, struct file *file)
  99. {
  100. unsigned int minor = iminor(inode);
  101. int err = 0;
  102. struct video_device *vfl;
  103. const struct file_operations *old_fops;
  104. if(minor>=VIDEO_NUM_DEVICES)
  105. return -ENODEV;
  106. mutex_lock(&videodev_lock);
  107. vfl=video_device[minor];
  108. if(vfl==NULL) {
  109. mutex_unlock(&videodev_lock);
  110. request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
  111. mutex_lock(&videodev_lock);
  112. vfl=video_device[minor];
  113. if (vfl==NULL) {
  114. mutex_unlock(&videodev_lock);
  115. return -ENODEV;
  116. }
  117. }
  118. old_fops = file->f_op;
  119. file->f_op = fops_get(vfl->fops);
  120. if(file->f_op->open)
  121. err = file->f_op->open(inode,file);
  122. if (err) {
  123. fops_put(file->f_op);
  124. file->f_op = fops_get(old_fops);
  125. }
  126. fops_put(old_fops);
  127. mutex_unlock(&videodev_lock);
  128. return err;
  129. }
  130. /*
  131. * helper function -- handles userspace copying for ioctl arguments
  132. */
  133. #ifdef __OLD_VIDIOC_
  134. static unsigned int
  135. video_fix_command(unsigned int cmd)
  136. {
  137. switch (cmd) {
  138. case VIDIOC_OVERLAY_OLD:
  139. cmd = VIDIOC_OVERLAY;
  140. break;
  141. case VIDIOC_S_PARM_OLD:
  142. cmd = VIDIOC_S_PARM;
  143. break;
  144. case VIDIOC_S_CTRL_OLD:
  145. cmd = VIDIOC_S_CTRL;
  146. break;
  147. case VIDIOC_G_AUDIO_OLD:
  148. cmd = VIDIOC_G_AUDIO;
  149. break;
  150. case VIDIOC_G_AUDOUT_OLD:
  151. cmd = VIDIOC_G_AUDOUT;
  152. break;
  153. case VIDIOC_CROPCAP_OLD:
  154. cmd = VIDIOC_CROPCAP;
  155. break;
  156. }
  157. return cmd;
  158. }
  159. #endif
  160. /*
  161. * Obsolete usercopy function - Should be removed soon
  162. */
  163. int
  164. video_usercopy(struct inode *inode, struct file *file,
  165. unsigned int cmd, unsigned long arg,
  166. int (*func)(struct inode *inode, struct file *file,
  167. unsigned int cmd, void *arg))
  168. {
  169. char sbuf[128];
  170. void *mbuf = NULL;
  171. void *parg = NULL;
  172. int err = -EINVAL;
  173. int is_ext_ctrl;
  174. size_t ctrls_size = 0;
  175. void __user *user_ptr = NULL;
  176. #ifdef __OLD_VIDIOC_
  177. cmd = video_fix_command(cmd);
  178. #endif
  179. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  180. cmd == VIDIOC_TRY_EXT_CTRLS);
  181. /* Copy arguments into temp kernel buffer */
  182. switch (_IOC_DIR(cmd)) {
  183. case _IOC_NONE:
  184. parg = NULL;
  185. break;
  186. case _IOC_READ:
  187. case _IOC_WRITE:
  188. case (_IOC_WRITE | _IOC_READ):
  189. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  190. parg = sbuf;
  191. } else {
  192. /* too big to allocate from stack */
  193. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  194. if (NULL == mbuf)
  195. return -ENOMEM;
  196. parg = mbuf;
  197. }
  198. err = -EFAULT;
  199. if (_IOC_DIR(cmd) & _IOC_WRITE)
  200. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  201. goto out;
  202. break;
  203. }
  204. if (is_ext_ctrl) {
  205. struct v4l2_ext_controls *p = parg;
  206. /* In case of an error, tell the caller that it wasn't
  207. a specific control that caused it. */
  208. p->error_idx = p->count;
  209. user_ptr = (void __user *)p->controls;
  210. if (p->count) {
  211. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  212. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  213. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  214. err = -ENOMEM;
  215. if (NULL == mbuf)
  216. goto out_ext_ctrl;
  217. err = -EFAULT;
  218. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  219. goto out_ext_ctrl;
  220. p->controls = mbuf;
  221. }
  222. }
  223. /* call driver */
  224. err = func(inode, file, cmd, parg);
  225. if (err == -ENOIOCTLCMD)
  226. err = -EINVAL;
  227. if (is_ext_ctrl) {
  228. struct v4l2_ext_controls *p = parg;
  229. p->controls = (void *)user_ptr;
  230. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  231. err = -EFAULT;
  232. goto out_ext_ctrl;
  233. }
  234. if (err < 0)
  235. goto out;
  236. out_ext_ctrl:
  237. /* Copy results into user buffer */
  238. switch (_IOC_DIR(cmd))
  239. {
  240. case _IOC_READ:
  241. case (_IOC_WRITE | _IOC_READ):
  242. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  243. err = -EFAULT;
  244. break;
  245. }
  246. out:
  247. kfree(mbuf);
  248. return err;
  249. }
  250. /*
  251. * open/release helper functions -- handle exclusive opens
  252. * Should be removed soon
  253. */
  254. int video_exclusive_open(struct inode *inode, struct file *file)
  255. {
  256. struct video_device *vfl = video_devdata(file);
  257. int retval = 0;
  258. mutex_lock(&vfl->lock);
  259. if (vfl->users) {
  260. retval = -EBUSY;
  261. } else {
  262. vfl->users++;
  263. }
  264. mutex_unlock(&vfl->lock);
  265. return retval;
  266. }
  267. int video_exclusive_release(struct inode *inode, struct file *file)
  268. {
  269. struct video_device *vfl = video_devdata(file);
  270. vfl->users--;
  271. return 0;
  272. }
  273. static char *v4l2_memory_names[] = {
  274. [V4L2_MEMORY_MMAP] = "mmap",
  275. [V4L2_MEMORY_USERPTR] = "userptr",
  276. [V4L2_MEMORY_OVERLAY] = "overlay",
  277. };
  278. /* FIXME: Those stuff are replicated also on v4l2-common.c */
  279. static char *v4l2_type_names_FIXME[] = {
  280. [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
  281. [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
  282. [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
  283. [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
  284. [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
  285. [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
  286. [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
  287. [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
  288. [V4L2_BUF_TYPE_PRIVATE] = "private",
  289. };
  290. static char *v4l2_field_names_FIXME[] = {
  291. [V4L2_FIELD_ANY] = "any",
  292. [V4L2_FIELD_NONE] = "none",
  293. [V4L2_FIELD_TOP] = "top",
  294. [V4L2_FIELD_BOTTOM] = "bottom",
  295. [V4L2_FIELD_INTERLACED] = "interlaced",
  296. [V4L2_FIELD_SEQ_TB] = "seq-tb",
  297. [V4L2_FIELD_SEQ_BT] = "seq-bt",
  298. [V4L2_FIELD_ALTERNATE] = "alternate",
  299. [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
  300. [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
  301. };
  302. #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
  303. static void dbgbuf(unsigned int cmd, struct video_device *vfd,
  304. struct v4l2_buffer *p)
  305. {
  306. struct v4l2_timecode *tc=&p->timecode;
  307. dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
  308. "bytesused=%d, flags=0x%08d, "
  309. "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
  310. (p->timestamp.tv_sec/3600),
  311. (int)(p->timestamp.tv_sec/60)%60,
  312. (int)(p->timestamp.tv_sec%60),
  313. p->timestamp.tv_usec,
  314. p->index,
  315. prt_names(p->type,v4l2_type_names_FIXME),
  316. p->bytesused,p->flags,
  317. p->field,p->sequence,
  318. prt_names(p->memory,v4l2_memory_names),
  319. p->m.userptr, p->length);
  320. dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
  321. "flags=0x%08d, frames=%d, userbits=0x%08x\n",
  322. tc->hours,tc->minutes,tc->seconds,
  323. tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
  324. }
  325. static inline void dbgrect(struct video_device *vfd, char *s,
  326. struct v4l2_rect *r)
  327. {
  328. dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
  329. r->width, r->height);
  330. };
  331. static inline void v4l_print_pix_fmt (struct video_device *vfd,
  332. struct v4l2_pix_format *fmt)
  333. {
  334. dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
  335. "bytesperline=%d sizeimage=%d, colorspace=%d\n",
  336. fmt->width,fmt->height,
  337. (fmt->pixelformat & 0xff),
  338. (fmt->pixelformat >> 8) & 0xff,
  339. (fmt->pixelformat >> 16) & 0xff,
  340. (fmt->pixelformat >> 24) & 0xff,
  341. prt_names(fmt->field,v4l2_field_names_FIXME),
  342. fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
  343. };
  344. static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
  345. {
  346. switch (type) {
  347. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  348. if (vfd->vidioc_try_fmt_cap)
  349. return (0);
  350. break;
  351. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  352. if (vfd->vidioc_try_fmt_overlay)
  353. return (0);
  354. break;
  355. case V4L2_BUF_TYPE_VBI_CAPTURE:
  356. if (vfd->vidioc_try_fmt_vbi)
  357. return (0);
  358. break;
  359. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  360. if (vfd->vidioc_try_fmt_vbi_output)
  361. return (0);
  362. break;
  363. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  364. if (vfd->vidioc_try_fmt_vbi_capture)
  365. return (0);
  366. break;
  367. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  368. if (vfd->vidioc_try_fmt_video_output)
  369. return (0);
  370. break;
  371. case V4L2_BUF_TYPE_VBI_OUTPUT:
  372. if (vfd->vidioc_try_fmt_vbi_output)
  373. return (0);
  374. break;
  375. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  376. if (vfd->vidioc_try_fmt_output_overlay)
  377. return (0);
  378. break;
  379. case V4L2_BUF_TYPE_PRIVATE:
  380. if (vfd->vidioc_try_fmt_type_private)
  381. return (0);
  382. break;
  383. }
  384. return (-EINVAL);
  385. }
  386. static int __video_do_ioctl(struct inode *inode, struct file *file,
  387. unsigned int cmd, void *arg)
  388. {
  389. struct video_device *vfd = video_devdata(file);
  390. void *fh = file->private_data;
  391. int ret = -EINVAL;
  392. if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
  393. !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
  394. v4l_print_ioctl(vfd->name, cmd);
  395. }
  396. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  397. /***********************************************************
  398. Handles calls to the obsoleted V4L1 API
  399. Due to the nature of VIDIOCGMBUF, each driver that supports
  400. V4L1 should implement its own handler for this ioctl.
  401. ***********************************************************/
  402. /* --- streaming capture ------------------------------------- */
  403. if (cmd == VIDIOCGMBUF) {
  404. struct video_mbuf *p=arg;
  405. memset(p, 0, sizeof(*p));
  406. if (!vfd->vidiocgmbuf)
  407. return ret;
  408. ret=vfd->vidiocgmbuf(file, fh, p);
  409. if (!ret)
  410. dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
  411. p->size, p->frames,
  412. (unsigned long)p->offsets);
  413. return ret;
  414. }
  415. /********************************************************
  416. All other V4L1 calls are handled by v4l1_compat module.
  417. Those calls will be translated into V4L2 calls, and
  418. __video_do_ioctl will be called again, with one or more
  419. V4L2 ioctls.
  420. ********************************************************/
  421. if (_IOC_TYPE(cmd)=='v')
  422. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  423. __video_do_ioctl);
  424. #endif
  425. switch(cmd) {
  426. /* --- capabilities ------------------------------------------ */
  427. case VIDIOC_QUERYCAP:
  428. {
  429. struct v4l2_capability *cap = (struct v4l2_capability*)arg;
  430. memset(cap, 0, sizeof(*cap));
  431. if (!vfd->vidioc_querycap)
  432. break;
  433. ret=vfd->vidioc_querycap(file, fh, cap);
  434. if (!ret)
  435. dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
  436. "version=0x%08x, "
  437. "capabilities=0x%08x\n",
  438. cap->driver,cap->card,cap->bus_info,
  439. cap->version,
  440. cap->capabilities);
  441. break;
  442. }
  443. /* --- priority ------------------------------------------ */
  444. case VIDIOC_G_PRIORITY:
  445. {
  446. enum v4l2_priority *p=arg;
  447. if (!vfd->vidioc_g_priority)
  448. break;
  449. ret=vfd->vidioc_g_priority(file, fh, p);
  450. if (!ret)
  451. dbgarg(cmd, "priority is %d\n", *p);
  452. break;
  453. }
  454. case VIDIOC_S_PRIORITY:
  455. {
  456. enum v4l2_priority *p=arg;
  457. if (!vfd->vidioc_s_priority)
  458. break;
  459. dbgarg(cmd, "setting priority to %d\n", *p);
  460. ret=vfd->vidioc_s_priority(file, fh, *p);
  461. break;
  462. }
  463. /* --- capture ioctls ---------------------------------------- */
  464. case VIDIOC_ENUM_FMT:
  465. {
  466. struct v4l2_fmtdesc *f = arg;
  467. enum v4l2_buf_type type;
  468. unsigned int index;
  469. index = f->index;
  470. type = f->type;
  471. memset(f,0,sizeof(*f));
  472. f->index = index;
  473. f->type = type;
  474. switch (type) {
  475. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  476. if (vfd->vidioc_enum_fmt_cap)
  477. ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
  478. break;
  479. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  480. if (vfd->vidioc_enum_fmt_overlay)
  481. ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
  482. break;
  483. case V4L2_BUF_TYPE_VBI_CAPTURE:
  484. if (vfd->vidioc_enum_fmt_vbi)
  485. ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
  486. break;
  487. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  488. if (vfd->vidioc_enum_fmt_vbi_output)
  489. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  490. fh, f);
  491. break;
  492. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  493. if (vfd->vidioc_enum_fmt_vbi_capture)
  494. ret=vfd->vidioc_enum_fmt_vbi_capture(file,
  495. fh, f);
  496. break;
  497. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  498. if (vfd->vidioc_enum_fmt_video_output)
  499. ret=vfd->vidioc_enum_fmt_video_output(file,
  500. fh, f);
  501. break;
  502. case V4L2_BUF_TYPE_VBI_OUTPUT:
  503. if (vfd->vidioc_enum_fmt_vbi_output)
  504. ret=vfd->vidioc_enum_fmt_vbi_output(file,
  505. fh, f);
  506. break;
  507. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  508. if (vfd->vidioc_enum_fmt_output_overlay)
  509. ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
  510. break;
  511. case V4L2_BUF_TYPE_PRIVATE:
  512. if (vfd->vidioc_enum_fmt_type_private)
  513. ret=vfd->vidioc_enum_fmt_type_private(file,
  514. fh, f);
  515. break;
  516. }
  517. if (!ret)
  518. dbgarg (cmd, "index=%d, type=%d, flags=%d, "
  519. "pixelformat=%c%c%c%c, description='%s'\n",
  520. f->index, f->type, f->flags,
  521. (f->pixelformat & 0xff),
  522. (f->pixelformat >> 8) & 0xff,
  523. (f->pixelformat >> 16) & 0xff,
  524. (f->pixelformat >> 24) & 0xff,
  525. f->description);
  526. break;
  527. }
  528. case VIDIOC_G_FMT:
  529. {
  530. struct v4l2_format *f = (struct v4l2_format *)arg;
  531. enum v4l2_buf_type type=f->type;
  532. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  533. f->type=type;
  534. /* FIXME: Should be one dump per type */
  535. dbgarg (cmd, "type=%s\n", prt_names(type,
  536. v4l2_type_names_FIXME));
  537. switch (type) {
  538. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  539. if (vfd->vidioc_g_fmt_cap)
  540. ret=vfd->vidioc_g_fmt_cap(file, fh, f);
  541. if (!ret)
  542. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  543. break;
  544. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  545. if (vfd->vidioc_g_fmt_overlay)
  546. ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
  547. break;
  548. case V4L2_BUF_TYPE_VBI_CAPTURE:
  549. if (vfd->vidioc_g_fmt_vbi)
  550. ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
  551. break;
  552. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  553. if (vfd->vidioc_g_fmt_vbi_output)
  554. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  555. break;
  556. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  557. if (vfd->vidioc_g_fmt_vbi_capture)
  558. ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
  559. break;
  560. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  561. if (vfd->vidioc_g_fmt_video_output)
  562. ret=vfd->vidioc_g_fmt_video_output(file,
  563. fh, f);
  564. break;
  565. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  566. if (vfd->vidioc_g_fmt_output_overlay)
  567. ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
  568. break;
  569. case V4L2_BUF_TYPE_VBI_OUTPUT:
  570. if (vfd->vidioc_g_fmt_vbi_output)
  571. ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
  572. break;
  573. case V4L2_BUF_TYPE_PRIVATE:
  574. if (vfd->vidioc_g_fmt_type_private)
  575. ret=vfd->vidioc_g_fmt_type_private(file,
  576. fh, f);
  577. break;
  578. }
  579. break;
  580. }
  581. case VIDIOC_S_FMT:
  582. {
  583. struct v4l2_format *f = (struct v4l2_format *)arg;
  584. /* FIXME: Should be one dump per type */
  585. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  586. v4l2_type_names_FIXME));
  587. switch (f->type) {
  588. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  589. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  590. if (vfd->vidioc_s_fmt_cap)
  591. ret=vfd->vidioc_s_fmt_cap(file, fh, f);
  592. break;
  593. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  594. if (vfd->vidioc_s_fmt_overlay)
  595. ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
  596. break;
  597. case V4L2_BUF_TYPE_VBI_CAPTURE:
  598. if (vfd->vidioc_s_fmt_vbi)
  599. ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
  600. break;
  601. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  602. if (vfd->vidioc_s_fmt_vbi_output)
  603. ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
  604. break;
  605. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  606. if (vfd->vidioc_s_fmt_vbi_capture)
  607. ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
  608. break;
  609. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  610. if (vfd->vidioc_s_fmt_video_output)
  611. ret=vfd->vidioc_s_fmt_video_output(file,
  612. fh, f);
  613. break;
  614. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  615. if (vfd->vidioc_s_fmt_output_overlay)
  616. ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
  617. break;
  618. case V4L2_BUF_TYPE_VBI_OUTPUT:
  619. if (vfd->vidioc_s_fmt_vbi_output)
  620. ret=vfd->vidioc_s_fmt_vbi_output(file,
  621. fh, f);
  622. break;
  623. case V4L2_BUF_TYPE_PRIVATE:
  624. if (vfd->vidioc_s_fmt_type_private)
  625. ret=vfd->vidioc_s_fmt_type_private(file,
  626. fh, f);
  627. break;
  628. }
  629. break;
  630. }
  631. case VIDIOC_TRY_FMT:
  632. {
  633. struct v4l2_format *f = (struct v4l2_format *)arg;
  634. /* FIXME: Should be one dump per type */
  635. dbgarg (cmd, "type=%s\n", prt_names(f->type,
  636. v4l2_type_names_FIXME));
  637. switch (f->type) {
  638. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  639. if (vfd->vidioc_try_fmt_cap)
  640. ret=vfd->vidioc_try_fmt_cap(file, fh, f);
  641. if (!ret)
  642. v4l_print_pix_fmt(vfd,&f->fmt.pix);
  643. break;
  644. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  645. if (vfd->vidioc_try_fmt_overlay)
  646. ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
  647. break;
  648. case V4L2_BUF_TYPE_VBI_CAPTURE:
  649. if (vfd->vidioc_try_fmt_vbi)
  650. ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
  651. break;
  652. case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  653. if (vfd->vidioc_try_fmt_vbi_output)
  654. ret=vfd->vidioc_try_fmt_vbi_output(file,
  655. fh, f);
  656. break;
  657. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  658. if (vfd->vidioc_try_fmt_vbi_capture)
  659. ret=vfd->vidioc_try_fmt_vbi_capture(file,
  660. fh, f);
  661. break;
  662. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  663. if (vfd->vidioc_try_fmt_video_output)
  664. ret=vfd->vidioc_try_fmt_video_output(file,
  665. fh, f);
  666. break;
  667. case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  668. if (vfd->vidioc_try_fmt_output_overlay)
  669. ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
  670. break;
  671. case V4L2_BUF_TYPE_VBI_OUTPUT:
  672. if (vfd->vidioc_try_fmt_vbi_output)
  673. ret=vfd->vidioc_try_fmt_vbi_output(file,
  674. fh, f);
  675. break;
  676. case V4L2_BUF_TYPE_PRIVATE:
  677. if (vfd->vidioc_try_fmt_type_private)
  678. ret=vfd->vidioc_try_fmt_type_private(file,
  679. fh, f);
  680. break;
  681. }
  682. break;
  683. }
  684. /* FIXME: Those buf reqs could be handled here,
  685. with some changes on videobuf to allow its header to be included at
  686. videodev2.h or being merged at videodev2.
  687. */
  688. case VIDIOC_REQBUFS:
  689. {
  690. struct v4l2_requestbuffers *p=arg;
  691. if (!vfd->vidioc_reqbufs)
  692. break;
  693. ret = check_fmt (vfd, p->type);
  694. if (ret)
  695. break;
  696. ret=vfd->vidioc_reqbufs(file, fh, p);
  697. dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
  698. p->count,
  699. prt_names(p->type,v4l2_type_names_FIXME),
  700. prt_names(p->memory,v4l2_memory_names));
  701. break;
  702. }
  703. case VIDIOC_QUERYBUF:
  704. {
  705. struct v4l2_buffer *p=arg;
  706. if (!vfd->vidioc_querybuf)
  707. break;
  708. ret = check_fmt (vfd, p->type);
  709. if (ret)
  710. break;
  711. ret=vfd->vidioc_querybuf(file, fh, p);
  712. if (!ret)
  713. dbgbuf(cmd,vfd,p);
  714. break;
  715. }
  716. case VIDIOC_QBUF:
  717. {
  718. struct v4l2_buffer *p=arg;
  719. if (!vfd->vidioc_qbuf)
  720. break;
  721. ret = check_fmt (vfd, p->type);
  722. if (ret)
  723. break;
  724. ret=vfd->vidioc_qbuf(file, fh, p);
  725. if (!ret)
  726. dbgbuf(cmd,vfd,p);
  727. break;
  728. }
  729. case VIDIOC_DQBUF:
  730. {
  731. struct v4l2_buffer *p=arg;
  732. if (!vfd->vidioc_dqbuf)
  733. break;
  734. ret = check_fmt (vfd, p->type);
  735. if (ret)
  736. break;
  737. ret=vfd->vidioc_dqbuf(file, fh, p);
  738. if (!ret)
  739. dbgbuf(cmd,vfd,p);
  740. break;
  741. }
  742. case VIDIOC_OVERLAY:
  743. {
  744. int *i = arg;
  745. if (!vfd->vidioc_overlay)
  746. break;
  747. dbgarg (cmd, "value=%d\n",*i);
  748. ret=vfd->vidioc_overlay(file, fh, *i);
  749. break;
  750. }
  751. case VIDIOC_G_FBUF:
  752. {
  753. struct v4l2_framebuffer *p=arg;
  754. if (!vfd->vidioc_g_fbuf)
  755. break;
  756. ret=vfd->vidioc_g_fbuf(file, fh, arg);
  757. if (!ret) {
  758. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  759. p->capability,p->flags,
  760. (unsigned long)p->base);
  761. v4l_print_pix_fmt (vfd, &p->fmt);
  762. }
  763. break;
  764. }
  765. case VIDIOC_S_FBUF:
  766. {
  767. struct v4l2_framebuffer *p=arg;
  768. if (!vfd->vidioc_s_fbuf)
  769. break;
  770. dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
  771. p->capability,p->flags,(unsigned long)p->base);
  772. v4l_print_pix_fmt (vfd, &p->fmt);
  773. ret=vfd->vidioc_s_fbuf(file, fh, arg);
  774. break;
  775. }
  776. case VIDIOC_STREAMON:
  777. {
  778. enum v4l2_buf_type i = *(int *)arg;
  779. if (!vfd->vidioc_streamon)
  780. break;
  781. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  782. ret=vfd->vidioc_streamon(file, fh,i);
  783. break;
  784. }
  785. case VIDIOC_STREAMOFF:
  786. {
  787. enum v4l2_buf_type i = *(int *)arg;
  788. if (!vfd->vidioc_streamoff)
  789. break;
  790. dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
  791. ret=vfd->vidioc_streamoff(file, fh, i);
  792. break;
  793. }
  794. /* ---------- tv norms ---------- */
  795. case VIDIOC_ENUMSTD:
  796. {
  797. struct v4l2_standard *p = arg;
  798. v4l2_std_id id = vfd->tvnorms,curr_id=0;
  799. unsigned int index = p->index,i;
  800. if (index<0) {
  801. ret=-EINVAL;
  802. break;
  803. }
  804. /* Return norm array on a canonical way */
  805. for (i=0;i<= index && id; i++) {
  806. if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
  807. curr_id = V4L2_STD_PAL;
  808. } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
  809. curr_id = V4L2_STD_PAL_BG;
  810. } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
  811. curr_id = V4L2_STD_PAL_DK;
  812. } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
  813. curr_id = V4L2_STD_PAL_B;
  814. } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
  815. curr_id = V4L2_STD_PAL_B1;
  816. } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
  817. curr_id = V4L2_STD_PAL_G;
  818. } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
  819. curr_id = V4L2_STD_PAL_H;
  820. } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
  821. curr_id = V4L2_STD_PAL_I;
  822. } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
  823. curr_id = V4L2_STD_PAL_D;
  824. } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
  825. curr_id = V4L2_STD_PAL_D1;
  826. } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
  827. curr_id = V4L2_STD_PAL_K;
  828. } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
  829. curr_id = V4L2_STD_PAL_M;
  830. } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
  831. curr_id = V4L2_STD_PAL_N;
  832. } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
  833. curr_id = V4L2_STD_PAL_Nc;
  834. } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
  835. curr_id = V4L2_STD_PAL_60;
  836. } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
  837. curr_id = V4L2_STD_NTSC;
  838. } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
  839. curr_id = V4L2_STD_NTSC_M;
  840. } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
  841. curr_id = V4L2_STD_NTSC_M_JP;
  842. } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
  843. curr_id = V4L2_STD_NTSC_443;
  844. } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
  845. curr_id = V4L2_STD_NTSC_M_KR;
  846. } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  847. curr_id = V4L2_STD_SECAM;
  848. } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
  849. curr_id = V4L2_STD_SECAM_DK;
  850. } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
  851. curr_id = V4L2_STD_SECAM_B;
  852. } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
  853. curr_id = V4L2_STD_SECAM_D;
  854. } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
  855. curr_id = V4L2_STD_SECAM_G;
  856. } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
  857. curr_id = V4L2_STD_SECAM_H;
  858. } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
  859. curr_id = V4L2_STD_SECAM_K;
  860. } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
  861. curr_id = V4L2_STD_SECAM_K1;
  862. } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
  863. curr_id = V4L2_STD_SECAM_L;
  864. } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
  865. curr_id = V4L2_STD_SECAM_LC;
  866. } else {
  867. break;
  868. }
  869. id &= ~curr_id;
  870. }
  871. if (i<=index)
  872. return -EINVAL;
  873. v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
  874. p->index = index;
  875. dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
  876. "framelines=%d\n", p->index,
  877. (unsigned long long)p->id, p->name,
  878. p->frameperiod.numerator,
  879. p->frameperiod.denominator,
  880. p->framelines);
  881. ret=0;
  882. break;
  883. }
  884. case VIDIOC_G_STD:
  885. {
  886. v4l2_std_id *id = arg;
  887. *id = vfd->current_norm;
  888. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  889. ret=0;
  890. break;
  891. }
  892. case VIDIOC_S_STD:
  893. {
  894. v4l2_std_id *id = arg,norm;
  895. dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
  896. norm = (*id) & vfd->tvnorms;
  897. if ( vfd->tvnorms && !norm) /* Check if std is supported */
  898. break;
  899. /* Calls the specific handler */
  900. if (vfd->vidioc_s_std)
  901. ret=vfd->vidioc_s_std(file, fh, &norm);
  902. else
  903. ret=-EINVAL;
  904. /* Updates standard information */
  905. if (ret>=0)
  906. vfd->current_norm=norm;
  907. break;
  908. }
  909. case VIDIOC_QUERYSTD:
  910. {
  911. v4l2_std_id *p=arg;
  912. if (!vfd->vidioc_querystd)
  913. break;
  914. ret=vfd->vidioc_querystd(file, fh, arg);
  915. if (!ret)
  916. dbgarg (cmd, "detected std=%Lu\n",
  917. (unsigned long long)*p);
  918. break;
  919. }
  920. /* ------ input switching ---------- */
  921. /* FIXME: Inputs can be handled inside videodev2 */
  922. case VIDIOC_ENUMINPUT:
  923. {
  924. struct v4l2_input *p=arg;
  925. int i=p->index;
  926. if (!vfd->vidioc_enum_input)
  927. break;
  928. memset(p, 0, sizeof(*p));
  929. p->index=i;
  930. ret=vfd->vidioc_enum_input(file, fh, p);
  931. if (!ret)
  932. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  933. "audioset=%d, "
  934. "tuner=%d, std=%Ld, status=%d\n",
  935. p->index,p->name,p->type,p->audioset,
  936. p->tuner,
  937. (unsigned long long)p->std,
  938. p->status);
  939. break;
  940. }
  941. case VIDIOC_G_INPUT:
  942. {
  943. unsigned int *i = arg;
  944. if (!vfd->vidioc_g_input)
  945. break;
  946. ret=vfd->vidioc_g_input(file, fh, i);
  947. if (!ret)
  948. dbgarg (cmd, "value=%d\n",*i);
  949. break;
  950. }
  951. case VIDIOC_S_INPUT:
  952. {
  953. unsigned int *i = arg;
  954. if (!vfd->vidioc_s_input)
  955. break;
  956. dbgarg (cmd, "value=%d\n",*i);
  957. ret=vfd->vidioc_s_input(file, fh, *i);
  958. break;
  959. }
  960. /* ------ output switching ---------- */
  961. case VIDIOC_G_OUTPUT:
  962. {
  963. unsigned int *i = arg;
  964. if (!vfd->vidioc_g_output)
  965. break;
  966. ret=vfd->vidioc_g_output(file, fh, i);
  967. if (!ret)
  968. dbgarg (cmd, "value=%d\n",*i);
  969. break;
  970. }
  971. case VIDIOC_S_OUTPUT:
  972. {
  973. unsigned int *i = arg;
  974. if (!vfd->vidioc_s_output)
  975. break;
  976. dbgarg (cmd, "value=%d\n",*i);
  977. ret=vfd->vidioc_s_output(file, fh, *i);
  978. break;
  979. }
  980. /* --- controls ---------------------------------------------- */
  981. case VIDIOC_QUERYCTRL:
  982. {
  983. struct v4l2_queryctrl *p=arg;
  984. if (!vfd->vidioc_queryctrl)
  985. break;
  986. ret=vfd->vidioc_queryctrl(file, fh, p);
  987. if (!ret)
  988. dbgarg (cmd, "id=%d, type=%d, name=%s, "
  989. "min/max=%d/%d,"
  990. " step=%d, default=%d, flags=0x%08x\n",
  991. p->id,p->type,p->name,p->minimum,
  992. p->maximum,p->step,p->default_value,
  993. p->flags);
  994. break;
  995. }
  996. case VIDIOC_G_CTRL:
  997. {
  998. struct v4l2_control *p = arg;
  999. if (!vfd->vidioc_g_ctrl)
  1000. break;
  1001. dbgarg(cmd, "Enum for index=%d\n", p->id);
  1002. ret=vfd->vidioc_g_ctrl(file, fh, p);
  1003. if (!ret)
  1004. dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
  1005. break;
  1006. }
  1007. case VIDIOC_S_CTRL:
  1008. {
  1009. struct v4l2_control *p = arg;
  1010. if (!vfd->vidioc_s_ctrl)
  1011. break;
  1012. dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
  1013. ret=vfd->vidioc_s_ctrl(file, fh, p);
  1014. break;
  1015. }
  1016. case VIDIOC_G_EXT_CTRLS:
  1017. {
  1018. struct v4l2_ext_controls *p = arg;
  1019. if (vfd->vidioc_g_ext_ctrls) {
  1020. dbgarg(cmd, "count=%d\n", p->count);
  1021. ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
  1022. }
  1023. break;
  1024. }
  1025. case VIDIOC_S_EXT_CTRLS:
  1026. {
  1027. struct v4l2_ext_controls *p = arg;
  1028. if (vfd->vidioc_s_ext_ctrls) {
  1029. dbgarg(cmd, "count=%d\n", p->count);
  1030. ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
  1031. }
  1032. break;
  1033. }
  1034. case VIDIOC_TRY_EXT_CTRLS:
  1035. {
  1036. struct v4l2_ext_controls *p = arg;
  1037. if (vfd->vidioc_try_ext_ctrls) {
  1038. dbgarg(cmd, "count=%d\n", p->count);
  1039. ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
  1040. }
  1041. break;
  1042. }
  1043. case VIDIOC_QUERYMENU:
  1044. {
  1045. struct v4l2_querymenu *p=arg;
  1046. if (!vfd->vidioc_querymenu)
  1047. break;
  1048. ret=vfd->vidioc_querymenu(file, fh, p);
  1049. if (!ret)
  1050. dbgarg (cmd, "id=%d, index=%d, name=%s\n",
  1051. p->id,p->index,p->name);
  1052. break;
  1053. }
  1054. /* --- audio ---------------------------------------------- */
  1055. case VIDIOC_ENUMAUDIO:
  1056. {
  1057. struct v4l2_audio *p=arg;
  1058. if (!vfd->vidioc_enumaudio)
  1059. break;
  1060. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1061. ret=vfd->vidioc_enumaudio(file, fh, p);
  1062. if (!ret)
  1063. dbgarg2("index=%d, name=%s, capability=%d, "
  1064. "mode=%d\n",p->index,p->name,
  1065. p->capability, p->mode);
  1066. break;
  1067. }
  1068. case VIDIOC_G_AUDIO:
  1069. {
  1070. struct v4l2_audio *p=arg;
  1071. __u32 index=p->index;
  1072. if (!vfd->vidioc_g_audio)
  1073. break;
  1074. memset(p,0,sizeof(*p));
  1075. p->index=index;
  1076. dbgarg(cmd, "Get for index=%d\n", p->index);
  1077. ret=vfd->vidioc_g_audio(file, fh, p);
  1078. if (!ret)
  1079. dbgarg2("index=%d, name=%s, capability=%d, "
  1080. "mode=%d\n",p->index,
  1081. p->name,p->capability, p->mode);
  1082. break;
  1083. }
  1084. case VIDIOC_S_AUDIO:
  1085. {
  1086. struct v4l2_audio *p=arg;
  1087. if (!vfd->vidioc_s_audio)
  1088. break;
  1089. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1090. "mode=%d\n", p->index, p->name,
  1091. p->capability, p->mode);
  1092. ret=vfd->vidioc_s_audio(file, fh, p);
  1093. break;
  1094. }
  1095. case VIDIOC_ENUMAUDOUT:
  1096. {
  1097. struct v4l2_audioout *p=arg;
  1098. if (!vfd->vidioc_enumaudout)
  1099. break;
  1100. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1101. ret=vfd->vidioc_enumaudout(file, fh, p);
  1102. if (!ret)
  1103. dbgarg2("index=%d, name=%s, capability=%d, "
  1104. "mode=%d\n", p->index, p->name,
  1105. p->capability,p->mode);
  1106. break;
  1107. }
  1108. case VIDIOC_G_AUDOUT:
  1109. {
  1110. struct v4l2_audioout *p=arg;
  1111. if (!vfd->vidioc_g_audout)
  1112. break;
  1113. dbgarg(cmd, "Enum for index=%d\n", p->index);
  1114. ret=vfd->vidioc_g_audout(file, fh, p);
  1115. if (!ret)
  1116. dbgarg2("index=%d, name=%s, capability=%d, "
  1117. "mode=%d\n", p->index, p->name,
  1118. p->capability,p->mode);
  1119. break;
  1120. }
  1121. case VIDIOC_S_AUDOUT:
  1122. {
  1123. struct v4l2_audioout *p=arg;
  1124. if (!vfd->vidioc_s_audout)
  1125. break;
  1126. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1127. "mode=%d\n", p->index, p->name,
  1128. p->capability,p->mode);
  1129. ret=vfd->vidioc_s_audout(file, fh, p);
  1130. break;
  1131. }
  1132. case VIDIOC_G_MODULATOR:
  1133. {
  1134. struct v4l2_modulator *p=arg;
  1135. if (!vfd->vidioc_g_modulator)
  1136. break;
  1137. ret=vfd->vidioc_g_modulator(file, fh, p);
  1138. if (!ret)
  1139. dbgarg(cmd, "index=%d, name=%s, "
  1140. "capability=%d, rangelow=%d,"
  1141. " rangehigh=%d, txsubchans=%d\n",
  1142. p->index, p->name,p->capability,
  1143. p->rangelow, p->rangehigh,
  1144. p->txsubchans);
  1145. break;
  1146. }
  1147. case VIDIOC_S_MODULATOR:
  1148. {
  1149. struct v4l2_modulator *p=arg;
  1150. if (!vfd->vidioc_s_modulator)
  1151. break;
  1152. dbgarg(cmd, "index=%d, name=%s, capability=%d, "
  1153. "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
  1154. p->index, p->name,p->capability,p->rangelow,
  1155. p->rangehigh,p->txsubchans);
  1156. ret=vfd->vidioc_s_modulator(file, fh, p);
  1157. break;
  1158. }
  1159. case VIDIOC_G_CROP:
  1160. {
  1161. struct v4l2_crop *p=arg;
  1162. if (!vfd->vidioc_g_crop)
  1163. break;
  1164. ret=vfd->vidioc_g_crop(file, fh, p);
  1165. if (!ret) {
  1166. dbgarg(cmd, "type=%d\n", p->type);
  1167. dbgrect(vfd, "", &p->c);
  1168. }
  1169. break;
  1170. }
  1171. case VIDIOC_S_CROP:
  1172. {
  1173. struct v4l2_crop *p=arg;
  1174. if (!vfd->vidioc_s_crop)
  1175. break;
  1176. dbgarg(cmd, "type=%d\n", p->type);
  1177. dbgrect(vfd, "", &p->c);
  1178. ret=vfd->vidioc_s_crop(file, fh, p);
  1179. break;
  1180. }
  1181. case VIDIOC_CROPCAP:
  1182. {
  1183. struct v4l2_cropcap *p=arg;
  1184. /*FIXME: Should also show v4l2_fract pixelaspect */
  1185. if (!vfd->vidioc_cropcap)
  1186. break;
  1187. dbgarg(cmd, "type=%d\n", p->type);
  1188. dbgrect(vfd, "bounds ", &p->bounds);
  1189. dbgrect(vfd, "defrect ", &p->defrect);
  1190. ret=vfd->vidioc_cropcap(file, fh, p);
  1191. break;
  1192. }
  1193. case VIDIOC_G_MPEGCOMP:
  1194. {
  1195. struct v4l2_mpeg_compression *p=arg;
  1196. /*FIXME: Several fields not shown */
  1197. if (!vfd->vidioc_g_mpegcomp)
  1198. break;
  1199. ret=vfd->vidioc_g_mpegcomp(file, fh, p);
  1200. if (!ret)
  1201. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
  1202. " ts_pid_video=%d, ts_pid_pcr=%d, "
  1203. "ps_size=%d, au_sample_rate=%d, "
  1204. "au_pesid=%c, vi_frame_rate=%d, "
  1205. "vi_frames_per_gop=%d, "
  1206. "vi_bframes_count=%d, vi_pesid=%c\n",
  1207. p->ts_pid_pmt,p->ts_pid_audio,
  1208. p->ts_pid_video,p->ts_pid_pcr,
  1209. p->ps_size, p->au_sample_rate,
  1210. p->au_pesid, p->vi_frame_rate,
  1211. p->vi_frames_per_gop,
  1212. p->vi_bframes_count, p->vi_pesid);
  1213. break;
  1214. }
  1215. case VIDIOC_S_MPEGCOMP:
  1216. {
  1217. struct v4l2_mpeg_compression *p=arg;
  1218. /*FIXME: Several fields not shown */
  1219. if (!vfd->vidioc_s_mpegcomp)
  1220. break;
  1221. dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
  1222. "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
  1223. "au_sample_rate=%d, au_pesid=%c, "
  1224. "vi_frame_rate=%d, vi_frames_per_gop=%d, "
  1225. "vi_bframes_count=%d, vi_pesid=%c\n",
  1226. p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
  1227. p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
  1228. p->au_pesid, p->vi_frame_rate,
  1229. p->vi_frames_per_gop, p->vi_bframes_count,
  1230. p->vi_pesid);
  1231. ret=vfd->vidioc_s_mpegcomp(file, fh, p);
  1232. break;
  1233. }
  1234. case VIDIOC_G_JPEGCOMP:
  1235. {
  1236. struct v4l2_jpegcompression *p=arg;
  1237. if (!vfd->vidioc_g_jpegcomp)
  1238. break;
  1239. ret=vfd->vidioc_g_jpegcomp(file, fh, p);
  1240. if (!ret)
  1241. dbgarg (cmd, "quality=%d, APPn=%d, "
  1242. "APP_len=%d, COM_len=%d, "
  1243. "jpeg_markers=%d\n",
  1244. p->quality,p->APPn,p->APP_len,
  1245. p->COM_len,p->jpeg_markers);
  1246. break;
  1247. }
  1248. case VIDIOC_S_JPEGCOMP:
  1249. {
  1250. struct v4l2_jpegcompression *p=arg;
  1251. if (!vfd->vidioc_g_jpegcomp)
  1252. break;
  1253. dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
  1254. "COM_len=%d, jpeg_markers=%d\n",
  1255. p->quality,p->APPn,p->APP_len,
  1256. p->COM_len,p->jpeg_markers);
  1257. ret=vfd->vidioc_s_jpegcomp(file, fh, p);
  1258. break;
  1259. }
  1260. case VIDIOC_G_ENC_INDEX:
  1261. {
  1262. struct v4l2_enc_idx *p=arg;
  1263. if (!vfd->vidioc_g_enc_index)
  1264. break;
  1265. ret=vfd->vidioc_g_enc_index(file, fh, p);
  1266. if (!ret)
  1267. dbgarg (cmd, "entries=%d, entries_cap=%d\n",
  1268. p->entries,p->entries_cap);
  1269. break;
  1270. }
  1271. case VIDIOC_ENCODER_CMD:
  1272. {
  1273. struct v4l2_encoder_cmd *p=arg;
  1274. if (!vfd->vidioc_encoder_cmd)
  1275. break;
  1276. ret=vfd->vidioc_encoder_cmd(file, fh, p);
  1277. if (!ret)
  1278. dbgarg (cmd, "cmd=%d, flags=%d\n",
  1279. p->cmd,p->flags);
  1280. break;
  1281. }
  1282. case VIDIOC_TRY_ENCODER_CMD:
  1283. {
  1284. struct v4l2_encoder_cmd *p=arg;
  1285. if (!vfd->vidioc_try_encoder_cmd)
  1286. break;
  1287. ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
  1288. if (!ret)
  1289. dbgarg (cmd, "cmd=%d, flags=%d\n",
  1290. p->cmd,p->flags);
  1291. break;
  1292. }
  1293. case VIDIOC_G_PARM:
  1294. {
  1295. struct v4l2_streamparm *p=arg;
  1296. __u32 type=p->type;
  1297. memset(p,0,sizeof(*p));
  1298. p->type=type;
  1299. if (vfd->vidioc_g_parm) {
  1300. ret=vfd->vidioc_g_parm(file, fh, p);
  1301. } else {
  1302. struct v4l2_standard s;
  1303. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1304. return -EINVAL;
  1305. v4l2_video_std_construct(&s, vfd->current_norm,
  1306. v4l2_norm_to_name(vfd->current_norm));
  1307. p->parm.capture.timeperframe = s.frameperiod;
  1308. ret=0;
  1309. }
  1310. dbgarg (cmd, "type=%d\n", p->type);
  1311. break;
  1312. }
  1313. case VIDIOC_S_PARM:
  1314. {
  1315. struct v4l2_streamparm *p=arg;
  1316. if (!vfd->vidioc_s_parm)
  1317. break;
  1318. dbgarg (cmd, "type=%d\n", p->type);
  1319. ret=vfd->vidioc_s_parm(file, fh, p);
  1320. break;
  1321. }
  1322. case VIDIOC_G_TUNER:
  1323. {
  1324. struct v4l2_tuner *p=arg;
  1325. __u32 index=p->index;
  1326. if (!vfd->vidioc_g_tuner)
  1327. break;
  1328. memset(p,0,sizeof(*p));
  1329. p->index=index;
  1330. ret=vfd->vidioc_g_tuner(file, fh, p);
  1331. if (!ret)
  1332. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1333. "capability=%d, rangelow=%d, "
  1334. "rangehigh=%d, signal=%d, afc=%d, "
  1335. "rxsubchans=%d, audmode=%d\n",
  1336. p->index, p->name, p->type,
  1337. p->capability, p->rangelow,
  1338. p->rangehigh, p->rxsubchans,
  1339. p->audmode, p->signal, p->afc);
  1340. break;
  1341. }
  1342. case VIDIOC_S_TUNER:
  1343. {
  1344. struct v4l2_tuner *p=arg;
  1345. if (!vfd->vidioc_s_tuner)
  1346. break;
  1347. dbgarg (cmd, "index=%d, name=%s, type=%d, "
  1348. "capability=%d, rangelow=%d, rangehigh=%d, "
  1349. "signal=%d, afc=%d, rxsubchans=%d, "
  1350. "audmode=%d\n",p->index, p->name, p->type,
  1351. p->capability, p->rangelow,p->rangehigh,
  1352. p->rxsubchans, p->audmode, p->signal,
  1353. p->afc);
  1354. ret=vfd->vidioc_s_tuner(file, fh, p);
  1355. break;
  1356. }
  1357. case VIDIOC_G_FREQUENCY:
  1358. {
  1359. struct v4l2_frequency *p=arg;
  1360. if (!vfd->vidioc_g_frequency)
  1361. break;
  1362. memset(p,0,sizeof(*p));
  1363. ret=vfd->vidioc_g_frequency(file, fh, p);
  1364. if (!ret)
  1365. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1366. p->tuner,p->type,p->frequency);
  1367. break;
  1368. }
  1369. case VIDIOC_S_FREQUENCY:
  1370. {
  1371. struct v4l2_frequency *p=arg;
  1372. if (!vfd->vidioc_s_frequency)
  1373. break;
  1374. dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
  1375. p->tuner,p->type,p->frequency);
  1376. ret=vfd->vidioc_s_frequency(file, fh, p);
  1377. break;
  1378. }
  1379. case VIDIOC_G_SLICED_VBI_CAP:
  1380. {
  1381. struct v4l2_sliced_vbi_cap *p=arg;
  1382. if (!vfd->vidioc_g_sliced_vbi_cap)
  1383. break;
  1384. ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
  1385. if (!ret)
  1386. dbgarg (cmd, "service_set=%d\n", p->service_set);
  1387. break;
  1388. }
  1389. case VIDIOC_LOG_STATUS:
  1390. {
  1391. if (!vfd->vidioc_log_status)
  1392. break;
  1393. ret=vfd->vidioc_log_status(file, fh);
  1394. break;
  1395. }
  1396. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1397. case VIDIOC_DBG_G_REGISTER:
  1398. {
  1399. struct v4l2_register *p=arg;
  1400. if (!capable(CAP_SYS_ADMIN))
  1401. ret=-EPERM;
  1402. else if (vfd->vidioc_g_register)
  1403. ret=vfd->vidioc_g_register(file, fh, p);
  1404. break;
  1405. }
  1406. case VIDIOC_DBG_S_REGISTER:
  1407. {
  1408. struct v4l2_register *p=arg;
  1409. if (!capable(CAP_SYS_ADMIN))
  1410. ret=-EPERM;
  1411. else if (vfd->vidioc_s_register)
  1412. ret=vfd->vidioc_s_register(file, fh, p);
  1413. break;
  1414. }
  1415. #endif
  1416. case VIDIOC_G_CHIP_IDENT:
  1417. {
  1418. struct v4l2_chip_ident *p=arg;
  1419. if (!vfd->vidioc_g_chip_ident)
  1420. break;
  1421. ret=vfd->vidioc_g_chip_ident(file, fh, p);
  1422. if (!ret)
  1423. dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
  1424. break;
  1425. }
  1426. } /* switch */
  1427. if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
  1428. if (ret<0) {
  1429. printk ("%s: err:\n", vfd->name);
  1430. v4l_print_ioctl(vfd->name, cmd);
  1431. }
  1432. }
  1433. return ret;
  1434. }
  1435. int video_ioctl2 (struct inode *inode, struct file *file,
  1436. unsigned int cmd, unsigned long arg)
  1437. {
  1438. char sbuf[128];
  1439. void *mbuf = NULL;
  1440. void *parg = NULL;
  1441. int err = -EINVAL;
  1442. int is_ext_ctrl;
  1443. size_t ctrls_size = 0;
  1444. void __user *user_ptr = NULL;
  1445. #ifdef __OLD_VIDIOC_
  1446. cmd = video_fix_command(cmd);
  1447. #endif
  1448. is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
  1449. cmd == VIDIOC_TRY_EXT_CTRLS);
  1450. /* Copy arguments into temp kernel buffer */
  1451. switch (_IOC_DIR(cmd)) {
  1452. case _IOC_NONE:
  1453. parg = NULL;
  1454. break;
  1455. case _IOC_READ:
  1456. case _IOC_WRITE:
  1457. case (_IOC_WRITE | _IOC_READ):
  1458. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  1459. parg = sbuf;
  1460. } else {
  1461. /* too big to allocate from stack */
  1462. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  1463. if (NULL == mbuf)
  1464. return -ENOMEM;
  1465. parg = mbuf;
  1466. }
  1467. err = -EFAULT;
  1468. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1469. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  1470. goto out;
  1471. break;
  1472. }
  1473. if (is_ext_ctrl) {
  1474. struct v4l2_ext_controls *p = parg;
  1475. /* In case of an error, tell the caller that it wasn't
  1476. a specific control that caused it. */
  1477. p->error_idx = p->count;
  1478. user_ptr = (void __user *)p->controls;
  1479. if (p->count) {
  1480. ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
  1481. /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
  1482. mbuf = kmalloc(ctrls_size, GFP_KERNEL);
  1483. err = -ENOMEM;
  1484. if (NULL == mbuf)
  1485. goto out_ext_ctrl;
  1486. err = -EFAULT;
  1487. if (copy_from_user(mbuf, user_ptr, ctrls_size))
  1488. goto out_ext_ctrl;
  1489. p->controls = mbuf;
  1490. }
  1491. }
  1492. /* Handles IOCTL */
  1493. err = __video_do_ioctl(inode, file, cmd, parg);
  1494. if (err == -ENOIOCTLCMD)
  1495. err = -EINVAL;
  1496. if (is_ext_ctrl) {
  1497. struct v4l2_ext_controls *p = parg;
  1498. p->controls = (void *)user_ptr;
  1499. if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
  1500. err = -EFAULT;
  1501. goto out_ext_ctrl;
  1502. }
  1503. if (err < 0)
  1504. goto out;
  1505. out_ext_ctrl:
  1506. /* Copy results into user buffer */
  1507. switch (_IOC_DIR(cmd))
  1508. {
  1509. case _IOC_READ:
  1510. case (_IOC_WRITE | _IOC_READ):
  1511. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  1512. err = -EFAULT;
  1513. break;
  1514. }
  1515. out:
  1516. kfree(mbuf);
  1517. return err;
  1518. }
  1519. static const struct file_operations video_fops;
  1520. /**
  1521. * video_register_device - register video4linux devices
  1522. * @vfd: video device structure we want to register
  1523. * @type: type of device to register
  1524. * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
  1525. * -1 == first free)
  1526. *
  1527. * The registration code assigns minor numbers based on the type
  1528. * requested. -ENFILE is returned in all the device slots for this
  1529. * category are full. If not then the minor field is set and the
  1530. * driver initialize function is called (if non %NULL).
  1531. *
  1532. * Zero is returned on success.
  1533. *
  1534. * Valid types are
  1535. *
  1536. * %VFL_TYPE_GRABBER - A frame grabber
  1537. *
  1538. * %VFL_TYPE_VTX - A teletext device
  1539. *
  1540. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  1541. *
  1542. * %VFL_TYPE_RADIO - A radio card
  1543. */
  1544. int video_register_device(struct video_device *vfd, int type, int nr)
  1545. {
  1546. int i=0;
  1547. int base;
  1548. int end;
  1549. int ret;
  1550. char *name_base;
  1551. switch(type)
  1552. {
  1553. case VFL_TYPE_GRABBER:
  1554. base=MINOR_VFL_TYPE_GRABBER_MIN;
  1555. end=MINOR_VFL_TYPE_GRABBER_MAX+1;
  1556. name_base = "video";
  1557. break;
  1558. case VFL_TYPE_VTX:
  1559. base=MINOR_VFL_TYPE_VTX_MIN;
  1560. end=MINOR_VFL_TYPE_VTX_MAX+1;
  1561. name_base = "vtx";
  1562. break;
  1563. case VFL_TYPE_VBI:
  1564. base=MINOR_VFL_TYPE_VBI_MIN;
  1565. end=MINOR_VFL_TYPE_VBI_MAX+1;
  1566. name_base = "vbi";
  1567. break;
  1568. case VFL_TYPE_RADIO:
  1569. base=MINOR_VFL_TYPE_RADIO_MIN;
  1570. end=MINOR_VFL_TYPE_RADIO_MAX+1;
  1571. name_base = "radio";
  1572. break;
  1573. default:
  1574. printk(KERN_ERR "%s called with unknown type: %d\n",
  1575. __FUNCTION__, type);
  1576. return -1;
  1577. }
  1578. /* pick a minor number */
  1579. mutex_lock(&videodev_lock);
  1580. if (nr >= 0 && nr < end-base) {
  1581. /* use the one the driver asked for */
  1582. i = base+nr;
  1583. if (NULL != video_device[i]) {
  1584. mutex_unlock(&videodev_lock);
  1585. return -ENFILE;
  1586. }
  1587. } else {
  1588. /* use first free */
  1589. for(i=base;i<end;i++)
  1590. if (NULL == video_device[i])
  1591. break;
  1592. if (i == end) {
  1593. mutex_unlock(&videodev_lock);
  1594. return -ENFILE;
  1595. }
  1596. }
  1597. video_device[i]=vfd;
  1598. vfd->minor=i;
  1599. mutex_unlock(&videodev_lock);
  1600. mutex_init(&vfd->lock);
  1601. /* sysfs class */
  1602. memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
  1603. if (vfd->dev)
  1604. vfd->class_dev.parent = vfd->dev;
  1605. vfd->class_dev.class = &video_class;
  1606. vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
  1607. sprintf(vfd->class_dev.bus_id, "%s%d", name_base, i - base);
  1608. ret = device_register(&vfd->class_dev);
  1609. if (ret < 0) {
  1610. printk(KERN_ERR "%s: device_register failed\n",
  1611. __FUNCTION__);
  1612. goto fail_minor;
  1613. }
  1614. #if 1
  1615. /* needed until all drivers are fixed */
  1616. if (!vfd->release)
  1617. printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
  1618. "Please fix your driver for proper sysfs support, see "
  1619. "http://lwn.net/Articles/36850/\n", vfd->name);
  1620. #endif
  1621. return 0;
  1622. fail_minor:
  1623. mutex_lock(&videodev_lock);
  1624. video_device[vfd->minor] = NULL;
  1625. vfd->minor = -1;
  1626. mutex_unlock(&videodev_lock);
  1627. return ret;
  1628. }
  1629. /**
  1630. * video_unregister_device - unregister a video4linux device
  1631. * @vfd: the device to unregister
  1632. *
  1633. * This unregisters the passed device and deassigns the minor
  1634. * number. Future open calls will be met with errors.
  1635. */
  1636. void video_unregister_device(struct video_device *vfd)
  1637. {
  1638. mutex_lock(&videodev_lock);
  1639. if(video_device[vfd->minor]!=vfd)
  1640. panic("videodev: bad unregister");
  1641. video_device[vfd->minor]=NULL;
  1642. device_unregister(&vfd->class_dev);
  1643. mutex_unlock(&videodev_lock);
  1644. }
  1645. /*
  1646. * Video fs operations
  1647. */
  1648. static const struct file_operations video_fops=
  1649. {
  1650. .owner = THIS_MODULE,
  1651. .llseek = no_llseek,
  1652. .open = video_open,
  1653. };
  1654. /*
  1655. * Initialise video for linux
  1656. */
  1657. static int __init videodev_init(void)
  1658. {
  1659. int ret;
  1660. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  1661. if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
  1662. printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
  1663. return -EIO;
  1664. }
  1665. ret = class_register(&video_class);
  1666. if (ret < 0) {
  1667. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  1668. printk(KERN_WARNING "video_dev: class_register failed\n");
  1669. return -EIO;
  1670. }
  1671. return 0;
  1672. }
  1673. static void __exit videodev_exit(void)
  1674. {
  1675. class_unregister(&video_class);
  1676. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  1677. }
  1678. module_init(videodev_init)
  1679. module_exit(videodev_exit)
  1680. EXPORT_SYMBOL(video_register_device);
  1681. EXPORT_SYMBOL(video_unregister_device);
  1682. EXPORT_SYMBOL(video_devdata);
  1683. EXPORT_SYMBOL(video_usercopy);
  1684. EXPORT_SYMBOL(video_exclusive_open);
  1685. EXPORT_SYMBOL(video_exclusive_release);
  1686. EXPORT_SYMBOL(video_ioctl2);
  1687. EXPORT_SYMBOL(video_device_alloc);
  1688. EXPORT_SYMBOL(video_device_release);
  1689. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  1690. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  1691. MODULE_LICENSE("GPL");
  1692. /*
  1693. * Local variables:
  1694. * c-basic-offset: 8
  1695. * End:
  1696. */