videodev.c 45 KB

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