videodev.c 44 KB

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