v4l2-dev.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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@lxorguk.ukuu.org.uk> (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. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/kmod.h>
  26. #include <linux/slab.h>
  27. #include <asm/uaccess.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-ioctl.h>
  31. #define VIDEO_NUM_DEVICES 256
  32. #define VIDEO_NAME "video4linux"
  33. /*
  34. * sysfs stuff
  35. */
  36. static ssize_t show_index(struct device *cd,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct video_device *vdev = to_video_device(cd);
  40. return sprintf(buf, "%i\n", vdev->index);
  41. }
  42. static ssize_t show_debug(struct device *cd,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct video_device *vdev = to_video_device(cd);
  46. return sprintf(buf, "%i\n", vdev->debug);
  47. }
  48. static ssize_t set_debug(struct device *cd, struct device_attribute *attr,
  49. const char *buf, size_t len)
  50. {
  51. struct video_device *vdev = to_video_device(cd);
  52. int res = 0;
  53. u16 value;
  54. res = kstrtou16(buf, 0, &value);
  55. if (res)
  56. return res;
  57. vdev->debug = value;
  58. return len;
  59. }
  60. static ssize_t show_name(struct device *cd,
  61. struct device_attribute *attr, char *buf)
  62. {
  63. struct video_device *vdev = to_video_device(cd);
  64. return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
  65. }
  66. static struct device_attribute video_device_attrs[] = {
  67. __ATTR(name, S_IRUGO, show_name, NULL),
  68. __ATTR(debug, 0644, show_debug, set_debug),
  69. __ATTR(index, S_IRUGO, show_index, NULL),
  70. __ATTR_NULL
  71. };
  72. /*
  73. * Active devices
  74. */
  75. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  76. static DEFINE_MUTEX(videodev_lock);
  77. static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
  78. /* Device node utility functions */
  79. /* Note: these utility functions all assume that vfl_type is in the range
  80. [0, VFL_TYPE_MAX-1]. */
  81. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  82. /* Return the bitmap corresponding to vfl_type. */
  83. static inline unsigned long *devnode_bits(int vfl_type)
  84. {
  85. /* Any types not assigned to fixed minor ranges must be mapped to
  86. one single bitmap for the purposes of finding a free node number
  87. since all those unassigned types use the same minor range. */
  88. int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
  89. return devnode_nums[idx];
  90. }
  91. #else
  92. /* Return the bitmap corresponding to vfl_type. */
  93. static inline unsigned long *devnode_bits(int vfl_type)
  94. {
  95. return devnode_nums[vfl_type];
  96. }
  97. #endif
  98. /* Mark device node number vdev->num as used */
  99. static inline void devnode_set(struct video_device *vdev)
  100. {
  101. set_bit(vdev->num, devnode_bits(vdev->vfl_type));
  102. }
  103. /* Mark device node number vdev->num as unused */
  104. static inline void devnode_clear(struct video_device *vdev)
  105. {
  106. clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
  107. }
  108. /* Try to find a free device node number in the range [from, to> */
  109. static inline int devnode_find(struct video_device *vdev, int from, int to)
  110. {
  111. return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
  112. }
  113. struct video_device *video_device_alloc(void)
  114. {
  115. return kzalloc(sizeof(struct video_device), GFP_KERNEL);
  116. }
  117. EXPORT_SYMBOL(video_device_alloc);
  118. void video_device_release(struct video_device *vdev)
  119. {
  120. kfree(vdev);
  121. }
  122. EXPORT_SYMBOL(video_device_release);
  123. void video_device_release_empty(struct video_device *vdev)
  124. {
  125. /* Do nothing */
  126. /* Only valid when the video_device struct is a static. */
  127. }
  128. EXPORT_SYMBOL(video_device_release_empty);
  129. static inline void video_get(struct video_device *vdev)
  130. {
  131. get_device(&vdev->dev);
  132. }
  133. static inline void video_put(struct video_device *vdev)
  134. {
  135. put_device(&vdev->dev);
  136. }
  137. /* Called when the last user of the video device exits. */
  138. static void v4l2_device_release(struct device *cd)
  139. {
  140. struct video_device *vdev = to_video_device(cd);
  141. struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
  142. mutex_lock(&videodev_lock);
  143. if (WARN_ON(video_device[vdev->minor] != vdev)) {
  144. /* should not happen */
  145. mutex_unlock(&videodev_lock);
  146. return;
  147. }
  148. /* Free up this device for reuse */
  149. video_device[vdev->minor] = NULL;
  150. /* Delete the cdev on this minor as well */
  151. cdev_del(vdev->cdev);
  152. /* Just in case some driver tries to access this from
  153. the release() callback. */
  154. vdev->cdev = NULL;
  155. /* Mark device node number as free */
  156. devnode_clear(vdev);
  157. mutex_unlock(&videodev_lock);
  158. #if defined(CONFIG_MEDIA_CONTROLLER)
  159. if (v4l2_dev && v4l2_dev->mdev &&
  160. vdev->vfl_type != VFL_TYPE_SUBDEV)
  161. media_device_unregister_entity(&vdev->entity);
  162. #endif
  163. /* Do not call v4l2_device_put if there is no release callback set.
  164. * Drivers that have no v4l2_device release callback might free the
  165. * v4l2_dev instance in the video_device release callback below, so we
  166. * must perform this check here.
  167. *
  168. * TODO: In the long run all drivers that use v4l2_device should use the
  169. * v4l2_device release callback. This check will then be unnecessary.
  170. */
  171. if (v4l2_dev && v4l2_dev->release == NULL)
  172. v4l2_dev = NULL;
  173. /* Release video_device and perform other
  174. cleanups as needed. */
  175. vdev->release(vdev);
  176. /* Decrease v4l2_device refcount */
  177. if (v4l2_dev)
  178. v4l2_device_put(v4l2_dev);
  179. }
  180. static struct class video_class = {
  181. .name = VIDEO_NAME,
  182. .dev_attrs = video_device_attrs,
  183. };
  184. struct video_device *video_devdata(struct file *file)
  185. {
  186. return video_device[iminor(file_inode(file))];
  187. }
  188. EXPORT_SYMBOL(video_devdata);
  189. /* Priority handling */
  190. static inline bool prio_is_valid(enum v4l2_priority prio)
  191. {
  192. return prio == V4L2_PRIORITY_BACKGROUND ||
  193. prio == V4L2_PRIORITY_INTERACTIVE ||
  194. prio == V4L2_PRIORITY_RECORD;
  195. }
  196. void v4l2_prio_init(struct v4l2_prio_state *global)
  197. {
  198. memset(global, 0, sizeof(*global));
  199. }
  200. EXPORT_SYMBOL(v4l2_prio_init);
  201. int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  202. enum v4l2_priority new)
  203. {
  204. if (!prio_is_valid(new))
  205. return -EINVAL;
  206. if (*local == new)
  207. return 0;
  208. atomic_inc(&global->prios[new]);
  209. if (prio_is_valid(*local))
  210. atomic_dec(&global->prios[*local]);
  211. *local = new;
  212. return 0;
  213. }
  214. EXPORT_SYMBOL(v4l2_prio_change);
  215. void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
  216. {
  217. v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
  218. }
  219. EXPORT_SYMBOL(v4l2_prio_open);
  220. void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
  221. {
  222. if (prio_is_valid(local))
  223. atomic_dec(&global->prios[local]);
  224. }
  225. EXPORT_SYMBOL(v4l2_prio_close);
  226. enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
  227. {
  228. if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
  229. return V4L2_PRIORITY_RECORD;
  230. if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
  231. return V4L2_PRIORITY_INTERACTIVE;
  232. if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
  233. return V4L2_PRIORITY_BACKGROUND;
  234. return V4L2_PRIORITY_UNSET;
  235. }
  236. EXPORT_SYMBOL(v4l2_prio_max);
  237. int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
  238. {
  239. return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
  240. }
  241. EXPORT_SYMBOL(v4l2_prio_check);
  242. static ssize_t v4l2_read(struct file *filp, char __user *buf,
  243. size_t sz, loff_t *off)
  244. {
  245. struct video_device *vdev = video_devdata(filp);
  246. int ret = -ENODEV;
  247. if (!vdev->fops->read)
  248. return -EINVAL;
  249. if (video_is_registered(vdev))
  250. ret = vdev->fops->read(filp, buf, sz, off);
  251. if (vdev->debug)
  252. printk(KERN_DEBUG "%s: read: %zd (%d)\n",
  253. video_device_node_name(vdev), sz, ret);
  254. return ret;
  255. }
  256. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  257. size_t sz, loff_t *off)
  258. {
  259. struct video_device *vdev = video_devdata(filp);
  260. int ret = -ENODEV;
  261. if (!vdev->fops->write)
  262. return -EINVAL;
  263. if (video_is_registered(vdev))
  264. ret = vdev->fops->write(filp, buf, sz, off);
  265. if (vdev->debug)
  266. printk(KERN_DEBUG "%s: write: %zd (%d)\n",
  267. video_device_node_name(vdev), sz, ret);
  268. return ret;
  269. }
  270. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  271. {
  272. struct video_device *vdev = video_devdata(filp);
  273. unsigned int res = POLLERR | POLLHUP;
  274. if (!vdev->fops->poll)
  275. return DEFAULT_POLLMASK;
  276. if (video_is_registered(vdev))
  277. res = vdev->fops->poll(filp, poll);
  278. if (vdev->debug)
  279. printk(KERN_DEBUG "%s: poll: %08x\n",
  280. video_device_node_name(vdev), res);
  281. return res;
  282. }
  283. static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  284. {
  285. struct video_device *vdev = video_devdata(filp);
  286. int ret = -ENODEV;
  287. if (vdev->fops->unlocked_ioctl) {
  288. struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
  289. if (lock && mutex_lock_interruptible(lock))
  290. return -ERESTARTSYS;
  291. if (video_is_registered(vdev))
  292. ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
  293. if (lock)
  294. mutex_unlock(lock);
  295. } else if (vdev->fops->ioctl) {
  296. /* This code path is a replacement for the BKL. It is a major
  297. * hack but it will have to do for those drivers that are not
  298. * yet converted to use unlocked_ioctl.
  299. *
  300. * There are two options: if the driver implements struct
  301. * v4l2_device, then the lock defined there is used to
  302. * serialize the ioctls. Otherwise the v4l2 core lock defined
  303. * below is used. This lock is really bad since it serializes
  304. * completely independent devices.
  305. *
  306. * Both variants suffer from the same problem: if the driver
  307. * sleeps, then it blocks all ioctls since the lock is still
  308. * held. This is very common for VIDIOC_DQBUF since that
  309. * normally waits for a frame to arrive. As a result any other
  310. * ioctl calls will proceed very, very slowly since each call
  311. * will have to wait for the VIDIOC_QBUF to finish. Things that
  312. * should take 0.01s may now take 10-20 seconds.
  313. *
  314. * The workaround is to *not* take the lock for VIDIOC_DQBUF.
  315. * This actually works OK for videobuf-based drivers, since
  316. * videobuf will take its own internal lock.
  317. */
  318. static DEFINE_MUTEX(v4l2_ioctl_mutex);
  319. struct mutex *m = vdev->v4l2_dev ?
  320. &vdev->v4l2_dev->ioctl_lock : &v4l2_ioctl_mutex;
  321. if (cmd != VIDIOC_DQBUF && mutex_lock_interruptible(m))
  322. return -ERESTARTSYS;
  323. if (video_is_registered(vdev))
  324. ret = vdev->fops->ioctl(filp, cmd, arg);
  325. if (cmd != VIDIOC_DQBUF)
  326. mutex_unlock(m);
  327. } else
  328. ret = -ENOTTY;
  329. return ret;
  330. }
  331. #ifdef CONFIG_MMU
  332. #define v4l2_get_unmapped_area NULL
  333. #else
  334. static unsigned long v4l2_get_unmapped_area(struct file *filp,
  335. unsigned long addr, unsigned long len, unsigned long pgoff,
  336. unsigned long flags)
  337. {
  338. struct video_device *vdev = video_devdata(filp);
  339. int ret;
  340. if (!vdev->fops->get_unmapped_area)
  341. return -ENOSYS;
  342. if (!video_is_registered(vdev))
  343. return -ENODEV;
  344. ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
  345. if (vdev->debug)
  346. printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
  347. video_device_node_name(vdev), ret);
  348. return ret;
  349. }
  350. #endif
  351. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  352. {
  353. struct video_device *vdev = video_devdata(filp);
  354. int ret = -ENODEV;
  355. if (!vdev->fops->mmap)
  356. return -ENODEV;
  357. if (video_is_registered(vdev))
  358. ret = vdev->fops->mmap(filp, vm);
  359. if (vdev->debug)
  360. printk(KERN_DEBUG "%s: mmap (%d)\n",
  361. video_device_node_name(vdev), ret);
  362. return ret;
  363. }
  364. /* Override for the open function */
  365. static int v4l2_open(struct inode *inode, struct file *filp)
  366. {
  367. struct video_device *vdev;
  368. int ret = 0;
  369. /* Check if the video device is available */
  370. mutex_lock(&videodev_lock);
  371. vdev = video_devdata(filp);
  372. /* return ENODEV if the video device has already been removed. */
  373. if (vdev == NULL || !video_is_registered(vdev)) {
  374. mutex_unlock(&videodev_lock);
  375. return -ENODEV;
  376. }
  377. /* and increase the device refcount */
  378. video_get(vdev);
  379. mutex_unlock(&videodev_lock);
  380. if (vdev->fops->open) {
  381. if (video_is_registered(vdev))
  382. ret = vdev->fops->open(filp);
  383. else
  384. ret = -ENODEV;
  385. }
  386. if (vdev->debug)
  387. printk(KERN_DEBUG "%s: open (%d)\n",
  388. video_device_node_name(vdev), ret);
  389. /* decrease the refcount in case of an error */
  390. if (ret)
  391. video_put(vdev);
  392. return ret;
  393. }
  394. /* Override for the release function */
  395. static int v4l2_release(struct inode *inode, struct file *filp)
  396. {
  397. struct video_device *vdev = video_devdata(filp);
  398. int ret = 0;
  399. if (vdev->fops->release)
  400. ret = vdev->fops->release(filp);
  401. if (vdev->debug)
  402. printk(KERN_DEBUG "%s: release\n",
  403. video_device_node_name(vdev));
  404. /* decrease the refcount unconditionally since the release()
  405. return value is ignored. */
  406. video_put(vdev);
  407. return ret;
  408. }
  409. static const struct file_operations v4l2_fops = {
  410. .owner = THIS_MODULE,
  411. .read = v4l2_read,
  412. .write = v4l2_write,
  413. .open = v4l2_open,
  414. .get_unmapped_area = v4l2_get_unmapped_area,
  415. .mmap = v4l2_mmap,
  416. .unlocked_ioctl = v4l2_ioctl,
  417. #ifdef CONFIG_COMPAT
  418. .compat_ioctl = v4l2_compat_ioctl32,
  419. #endif
  420. .release = v4l2_release,
  421. .poll = v4l2_poll,
  422. .llseek = no_llseek,
  423. };
  424. /**
  425. * get_index - assign stream index number based on parent device
  426. * @vdev: video_device to assign index number to, vdev->parent should be assigned
  427. *
  428. * Note that when this is called the new device has not yet been registered
  429. * in the video_device array, but it was able to obtain a minor number.
  430. *
  431. * This means that we can always obtain a free stream index number since
  432. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  433. * use of the video_device array.
  434. *
  435. * Returns a free index number.
  436. */
  437. static int get_index(struct video_device *vdev)
  438. {
  439. /* This can be static since this function is called with the global
  440. videodev_lock held. */
  441. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  442. int i;
  443. /* Some drivers do not set the parent. In that case always return 0. */
  444. if (vdev->parent == NULL)
  445. return 0;
  446. bitmap_zero(used, VIDEO_NUM_DEVICES);
  447. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  448. if (video_device[i] != NULL &&
  449. video_device[i]->parent == vdev->parent) {
  450. set_bit(video_device[i]->index, used);
  451. }
  452. }
  453. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  454. }
  455. #define SET_VALID_IOCTL(ops, cmd, op) \
  456. if (ops->op) \
  457. set_bit(_IOC_NR(cmd), valid_ioctls)
  458. /* This determines which ioctls are actually implemented in the driver.
  459. It's a one-time thing which simplifies video_ioctl2 as it can just do
  460. a bit test.
  461. Note that drivers can override this by setting bits to 1 in
  462. vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
  463. called, then that ioctl will actually be marked as unimplemented.
  464. It does that by first setting up the local valid_ioctls bitmap, and
  465. at the end do a:
  466. vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
  467. */
  468. static void determine_valid_ioctls(struct video_device *vdev)
  469. {
  470. DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
  471. const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
  472. bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
  473. bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
  474. bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
  475. bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
  476. bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
  477. bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
  478. /* vfl_type and vfl_dir independent ioctls */
  479. SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
  480. if (ops->vidioc_g_priority ||
  481. test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags))
  482. set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
  483. if (ops->vidioc_s_priority ||
  484. test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags))
  485. set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
  486. SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
  487. SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
  488. SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
  489. SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
  490. SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
  491. SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
  492. SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
  493. /* Note: the control handler can also be passed through the filehandle,
  494. and that can't be tested here. If the bit for these control ioctls
  495. is set, then the ioctl is valid. But if it is 0, then it can still
  496. be valid if the filehandle passed the control handler. */
  497. if (vdev->ctrl_handler || ops->vidioc_queryctrl)
  498. set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
  499. if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
  500. set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
  501. if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
  502. set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
  503. if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
  504. set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
  505. if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
  506. set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
  507. if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
  508. set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
  509. if (vdev->ctrl_handler || ops->vidioc_querymenu)
  510. set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
  511. SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
  512. SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
  513. SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
  514. #ifdef CONFIG_VIDEO_ADV_DEBUG
  515. SET_VALID_IOCTL(ops, VIDIOC_DBG_G_REGISTER, vidioc_g_register);
  516. SET_VALID_IOCTL(ops, VIDIOC_DBG_S_REGISTER, vidioc_s_register);
  517. #endif
  518. SET_VALID_IOCTL(ops, VIDIOC_DBG_G_CHIP_IDENT, vidioc_g_chip_ident);
  519. /* yes, really vidioc_subscribe_event */
  520. SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
  521. SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
  522. SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
  523. SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
  524. SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
  525. if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
  526. set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
  527. if (is_vid) {
  528. /* video specific ioctls */
  529. if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
  530. ops->vidioc_enum_fmt_vid_cap_mplane ||
  531. ops->vidioc_enum_fmt_vid_overlay)) ||
  532. (is_tx && (ops->vidioc_enum_fmt_vid_out ||
  533. ops->vidioc_enum_fmt_vid_out_mplane)))
  534. set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
  535. if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
  536. ops->vidioc_g_fmt_vid_cap_mplane ||
  537. ops->vidioc_g_fmt_vid_overlay)) ||
  538. (is_tx && (ops->vidioc_g_fmt_vid_out ||
  539. ops->vidioc_g_fmt_vid_out_mplane ||
  540. ops->vidioc_g_fmt_vid_out_overlay)))
  541. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  542. if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
  543. ops->vidioc_s_fmt_vid_cap_mplane ||
  544. ops->vidioc_s_fmt_vid_overlay)) ||
  545. (is_tx && (ops->vidioc_s_fmt_vid_out ||
  546. ops->vidioc_s_fmt_vid_out_mplane ||
  547. ops->vidioc_s_fmt_vid_out_overlay)))
  548. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  549. if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
  550. ops->vidioc_try_fmt_vid_cap_mplane ||
  551. ops->vidioc_try_fmt_vid_overlay)) ||
  552. (is_tx && (ops->vidioc_try_fmt_vid_out ||
  553. ops->vidioc_try_fmt_vid_out_mplane ||
  554. ops->vidioc_try_fmt_vid_out_overlay)))
  555. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  556. SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
  557. SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
  558. SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
  559. SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
  560. SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
  561. SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
  562. SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
  563. SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
  564. SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
  565. SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
  566. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
  567. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
  568. } else if (is_vbi) {
  569. /* vbi specific ioctls */
  570. if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
  571. ops->vidioc_g_fmt_sliced_vbi_cap)) ||
  572. (is_tx && (ops->vidioc_g_fmt_vbi_out ||
  573. ops->vidioc_g_fmt_sliced_vbi_out)))
  574. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  575. if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
  576. ops->vidioc_s_fmt_sliced_vbi_cap)) ||
  577. (is_tx && (ops->vidioc_s_fmt_vbi_out ||
  578. ops->vidioc_s_fmt_sliced_vbi_out)))
  579. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  580. if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
  581. ops->vidioc_try_fmt_sliced_vbi_cap)) ||
  582. (is_tx && (ops->vidioc_try_fmt_vbi_out ||
  583. ops->vidioc_try_fmt_sliced_vbi_out)))
  584. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  585. SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
  586. }
  587. if (!is_radio) {
  588. /* ioctls valid for video or vbi */
  589. if (ops->vidioc_s_std)
  590. set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
  591. if (ops->vidioc_g_std || vdev->current_norm)
  592. set_bit(_IOC_NR(VIDIOC_G_STD), valid_ioctls);
  593. SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
  594. if (is_rx) {
  595. SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
  596. SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
  597. SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
  598. SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
  599. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
  600. SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
  601. SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
  602. SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset);
  603. SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
  604. }
  605. if (is_tx) {
  606. SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
  607. SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
  608. SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
  609. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
  610. SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
  611. SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
  612. }
  613. if (ops->vidioc_g_crop || ops->vidioc_g_selection)
  614. set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
  615. if (ops->vidioc_s_crop || ops->vidioc_s_selection)
  616. set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
  617. SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
  618. SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
  619. if (ops->vidioc_cropcap || ops->vidioc_g_selection)
  620. set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
  621. if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
  622. (ops->vidioc_g_std || vdev->current_norm)))
  623. set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
  624. SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
  625. SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets);
  626. SET_VALID_IOCTL(ops, VIDIOC_S_DV_PRESET, vidioc_s_dv_preset);
  627. SET_VALID_IOCTL(ops, VIDIOC_G_DV_PRESET, vidioc_g_dv_preset);
  628. SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
  629. SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
  630. SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
  631. SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
  632. }
  633. if (is_tx) {
  634. /* transmitter only ioctls */
  635. SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
  636. SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
  637. }
  638. if (is_rx) {
  639. /* receiver only ioctls */
  640. SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
  641. SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
  642. SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
  643. }
  644. bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
  645. BASE_VIDIOC_PRIVATE);
  646. }
  647. /**
  648. * __video_register_device - register video4linux devices
  649. * @vdev: video device structure we want to register
  650. * @type: type of device to register
  651. * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  652. * -1 == first free)
  653. * @warn_if_nr_in_use: warn if the desired device node number
  654. * was already in use and another number was chosen instead.
  655. * @owner: module that owns the video device node
  656. *
  657. * The registration code assigns minor numbers and device node numbers
  658. * based on the requested type and registers the new device node with
  659. * the kernel.
  660. *
  661. * This function assumes that struct video_device was zeroed when it
  662. * was allocated and does not contain any stale date.
  663. *
  664. * An error is returned if no free minor or device node number could be
  665. * found, or if the registration of the device node failed.
  666. *
  667. * Zero is returned on success.
  668. *
  669. * Valid types are
  670. *
  671. * %VFL_TYPE_GRABBER - A frame grabber
  672. *
  673. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  674. *
  675. * %VFL_TYPE_RADIO - A radio card
  676. *
  677. * %VFL_TYPE_SUBDEV - A subdevice
  678. */
  679. int __video_register_device(struct video_device *vdev, int type, int nr,
  680. int warn_if_nr_in_use, struct module *owner)
  681. {
  682. int i = 0;
  683. int ret;
  684. int minor_offset = 0;
  685. int minor_cnt = VIDEO_NUM_DEVICES;
  686. const char *name_base;
  687. /* A minor value of -1 marks this video device as never
  688. having been registered */
  689. vdev->minor = -1;
  690. /* the release callback MUST be present */
  691. if (WARN_ON(!vdev->release))
  692. return -EINVAL;
  693. /* v4l2_fh support */
  694. spin_lock_init(&vdev->fh_lock);
  695. INIT_LIST_HEAD(&vdev->fh_list);
  696. /* Part 1: check device type */
  697. switch (type) {
  698. case VFL_TYPE_GRABBER:
  699. name_base = "video";
  700. break;
  701. case VFL_TYPE_VBI:
  702. name_base = "vbi";
  703. break;
  704. case VFL_TYPE_RADIO:
  705. name_base = "radio";
  706. break;
  707. case VFL_TYPE_SUBDEV:
  708. name_base = "v4l-subdev";
  709. break;
  710. default:
  711. printk(KERN_ERR "%s called with unknown type: %d\n",
  712. __func__, type);
  713. return -EINVAL;
  714. }
  715. vdev->vfl_type = type;
  716. vdev->cdev = NULL;
  717. if (vdev->v4l2_dev) {
  718. if (vdev->v4l2_dev->dev)
  719. vdev->parent = vdev->v4l2_dev->dev;
  720. if (vdev->ctrl_handler == NULL)
  721. vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
  722. /* If the prio state pointer is NULL, then use the v4l2_device
  723. prio state. */
  724. if (vdev->prio == NULL)
  725. vdev->prio = &vdev->v4l2_dev->prio;
  726. }
  727. /* Part 2: find a free minor, device node number and device index. */
  728. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  729. /* Keep the ranges for the first four types for historical
  730. * reasons.
  731. * Newer devices (not yet in place) should use the range
  732. * of 128-191 and just pick the first free minor there
  733. * (new style). */
  734. switch (type) {
  735. case VFL_TYPE_GRABBER:
  736. minor_offset = 0;
  737. minor_cnt = 64;
  738. break;
  739. case VFL_TYPE_RADIO:
  740. minor_offset = 64;
  741. minor_cnt = 64;
  742. break;
  743. case VFL_TYPE_VBI:
  744. minor_offset = 224;
  745. minor_cnt = 32;
  746. break;
  747. default:
  748. minor_offset = 128;
  749. minor_cnt = 64;
  750. break;
  751. }
  752. #endif
  753. /* Pick a device node number */
  754. mutex_lock(&videodev_lock);
  755. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  756. if (nr == minor_cnt)
  757. nr = devnode_find(vdev, 0, minor_cnt);
  758. if (nr == minor_cnt) {
  759. printk(KERN_ERR "could not get a free device node number\n");
  760. mutex_unlock(&videodev_lock);
  761. return -ENFILE;
  762. }
  763. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  764. /* 1-on-1 mapping of device node number to minor number */
  765. i = nr;
  766. #else
  767. /* The device node number and minor numbers are independent, so
  768. we just find the first free minor number. */
  769. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  770. if (video_device[i] == NULL)
  771. break;
  772. if (i == VIDEO_NUM_DEVICES) {
  773. mutex_unlock(&videodev_lock);
  774. printk(KERN_ERR "could not get a free minor\n");
  775. return -ENFILE;
  776. }
  777. #endif
  778. vdev->minor = i + minor_offset;
  779. vdev->num = nr;
  780. devnode_set(vdev);
  781. /* Should not happen since we thought this minor was free */
  782. WARN_ON(video_device[vdev->minor] != NULL);
  783. vdev->index = get_index(vdev);
  784. mutex_unlock(&videodev_lock);
  785. if (vdev->ioctl_ops)
  786. determine_valid_ioctls(vdev);
  787. /* Part 3: Initialize the character device */
  788. vdev->cdev = cdev_alloc();
  789. if (vdev->cdev == NULL) {
  790. ret = -ENOMEM;
  791. goto cleanup;
  792. }
  793. vdev->cdev->ops = &v4l2_fops;
  794. vdev->cdev->owner = owner;
  795. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  796. if (ret < 0) {
  797. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  798. kfree(vdev->cdev);
  799. vdev->cdev = NULL;
  800. goto cleanup;
  801. }
  802. /* Part 4: register the device with sysfs */
  803. vdev->dev.class = &video_class;
  804. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  805. if (vdev->parent)
  806. vdev->dev.parent = vdev->parent;
  807. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  808. ret = device_register(&vdev->dev);
  809. if (ret < 0) {
  810. printk(KERN_ERR "%s: device_register failed\n", __func__);
  811. goto cleanup;
  812. }
  813. /* Register the release callback that will be called when the last
  814. reference to the device goes away. */
  815. vdev->dev.release = v4l2_device_release;
  816. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  817. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  818. name_base, nr, video_device_node_name(vdev));
  819. /* Increase v4l2_device refcount */
  820. if (vdev->v4l2_dev)
  821. v4l2_device_get(vdev->v4l2_dev);
  822. #if defined(CONFIG_MEDIA_CONTROLLER)
  823. /* Part 5: Register the entity. */
  824. if (vdev->v4l2_dev && vdev->v4l2_dev->mdev &&
  825. vdev->vfl_type != VFL_TYPE_SUBDEV) {
  826. vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
  827. vdev->entity.name = vdev->name;
  828. vdev->entity.info.v4l.major = VIDEO_MAJOR;
  829. vdev->entity.info.v4l.minor = vdev->minor;
  830. ret = media_device_register_entity(vdev->v4l2_dev->mdev,
  831. &vdev->entity);
  832. if (ret < 0)
  833. printk(KERN_WARNING
  834. "%s: media_device_register_entity failed\n",
  835. __func__);
  836. }
  837. #endif
  838. /* Part 6: Activate this minor. The char device can now be used. */
  839. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  840. mutex_lock(&videodev_lock);
  841. video_device[vdev->minor] = vdev;
  842. mutex_unlock(&videodev_lock);
  843. return 0;
  844. cleanup:
  845. mutex_lock(&videodev_lock);
  846. if (vdev->cdev)
  847. cdev_del(vdev->cdev);
  848. devnode_clear(vdev);
  849. mutex_unlock(&videodev_lock);
  850. /* Mark this video device as never having been registered. */
  851. vdev->minor = -1;
  852. return ret;
  853. }
  854. EXPORT_SYMBOL(__video_register_device);
  855. /**
  856. * video_unregister_device - unregister a video4linux device
  857. * @vdev: the device to unregister
  858. *
  859. * This unregisters the passed device. Future open calls will
  860. * be met with errors.
  861. */
  862. void video_unregister_device(struct video_device *vdev)
  863. {
  864. /* Check if vdev was ever registered at all */
  865. if (!vdev || !video_is_registered(vdev))
  866. return;
  867. mutex_lock(&videodev_lock);
  868. /* This must be in a critical section to prevent a race with v4l2_open.
  869. * Once this bit has been cleared video_get may never be called again.
  870. */
  871. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  872. mutex_unlock(&videodev_lock);
  873. device_unregister(&vdev->dev);
  874. }
  875. EXPORT_SYMBOL(video_unregister_device);
  876. /*
  877. * Initialise video for linux
  878. */
  879. static int __init videodev_init(void)
  880. {
  881. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  882. int ret;
  883. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  884. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  885. if (ret < 0) {
  886. printk(KERN_WARNING "videodev: unable to get major %d\n",
  887. VIDEO_MAJOR);
  888. return ret;
  889. }
  890. ret = class_register(&video_class);
  891. if (ret < 0) {
  892. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  893. printk(KERN_WARNING "video_dev: class_register failed\n");
  894. return -EIO;
  895. }
  896. return 0;
  897. }
  898. static void __exit videodev_exit(void)
  899. {
  900. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  901. class_unregister(&video_class);
  902. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  903. }
  904. subsys_initcall(videodev_init);
  905. module_exit(videodev_exit)
  906. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  907. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  908. MODULE_LICENSE("GPL");
  909. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
  910. /*
  911. * Local variables:
  912. * c-basic-offset: 8
  913. * End:
  914. */