v4l2-dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 <asm/system.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-ioctl.h>
  32. #define VIDEO_NUM_DEVICES 256
  33. #define VIDEO_NAME "video4linux"
  34. /*
  35. * sysfs stuff
  36. */
  37. static ssize_t show_index(struct device *cd,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct video_device *vdev = to_video_device(cd);
  41. return sprintf(buf, "%i\n", vdev->index);
  42. }
  43. static ssize_t show_name(struct device *cd,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct video_device *vdev = to_video_device(cd);
  47. return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
  48. }
  49. static struct device_attribute video_device_attrs[] = {
  50. __ATTR(name, S_IRUGO, show_name, NULL),
  51. __ATTR(index, S_IRUGO, show_index, NULL),
  52. __ATTR_NULL
  53. };
  54. /*
  55. * Active devices
  56. */
  57. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  58. static DEFINE_MUTEX(videodev_lock);
  59. static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
  60. /* Device node utility functions */
  61. /* Note: these utility functions all assume that vfl_type is in the range
  62. [0, VFL_TYPE_MAX-1]. */
  63. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  64. /* Return the bitmap corresponding to vfl_type. */
  65. static inline unsigned long *devnode_bits(int vfl_type)
  66. {
  67. /* Any types not assigned to fixed minor ranges must be mapped to
  68. one single bitmap for the purposes of finding a free node number
  69. since all those unassigned types use the same minor range. */
  70. int idx = (vfl_type > VFL_TYPE_VTX) ? VFL_TYPE_MAX - 1 : vfl_type;
  71. return devnode_nums[idx];
  72. }
  73. #else
  74. /* Return the bitmap corresponding to vfl_type. */
  75. static inline unsigned long *devnode_bits(int vfl_type)
  76. {
  77. return devnode_nums[vfl_type];
  78. }
  79. #endif
  80. /* Mark device node number vdev->num as used */
  81. static inline void devnode_set(struct video_device *vdev)
  82. {
  83. set_bit(vdev->num, devnode_bits(vdev->vfl_type));
  84. }
  85. /* Mark device node number vdev->num as unused */
  86. static inline void devnode_clear(struct video_device *vdev)
  87. {
  88. clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
  89. }
  90. /* Try to find a free device node number in the range [from, to> */
  91. static inline int devnode_find(struct video_device *vdev, int from, int to)
  92. {
  93. return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
  94. }
  95. struct video_device *video_device_alloc(void)
  96. {
  97. return kzalloc(sizeof(struct video_device), GFP_KERNEL);
  98. }
  99. EXPORT_SYMBOL(video_device_alloc);
  100. void video_device_release(struct video_device *vdev)
  101. {
  102. kfree(vdev);
  103. }
  104. EXPORT_SYMBOL(video_device_release);
  105. void video_device_release_empty(struct video_device *vdev)
  106. {
  107. /* Do nothing */
  108. /* Only valid when the video_device struct is a static. */
  109. }
  110. EXPORT_SYMBOL(video_device_release_empty);
  111. static inline void video_get(struct video_device *vdev)
  112. {
  113. get_device(&vdev->dev);
  114. }
  115. static inline void video_put(struct video_device *vdev)
  116. {
  117. put_device(&vdev->dev);
  118. }
  119. /* Called when the last user of the video device exits. */
  120. static void v4l2_device_release(struct device *cd)
  121. {
  122. struct video_device *vdev = to_video_device(cd);
  123. mutex_lock(&videodev_lock);
  124. if (video_device[vdev->minor] != vdev) {
  125. mutex_unlock(&videodev_lock);
  126. /* should not happen */
  127. WARN_ON(1);
  128. return;
  129. }
  130. /* Free up this device for reuse */
  131. video_device[vdev->minor] = NULL;
  132. /* Delete the cdev on this minor as well */
  133. cdev_del(vdev->cdev);
  134. /* Just in case some driver tries to access this from
  135. the release() callback. */
  136. vdev->cdev = NULL;
  137. /* Mark device node number as free */
  138. devnode_clear(vdev);
  139. mutex_unlock(&videodev_lock);
  140. /* Release video_device and perform other
  141. cleanups as needed. */
  142. vdev->release(vdev);
  143. }
  144. static struct class video_class = {
  145. .name = VIDEO_NAME,
  146. .dev_attrs = video_device_attrs,
  147. };
  148. struct video_device *video_devdata(struct file *file)
  149. {
  150. return video_device[iminor(file->f_path.dentry->d_inode)];
  151. }
  152. EXPORT_SYMBOL(video_devdata);
  153. static ssize_t v4l2_read(struct file *filp, char __user *buf,
  154. size_t sz, loff_t *off)
  155. {
  156. struct video_device *vdev = video_devdata(filp);
  157. if (!vdev->fops->read)
  158. return -EINVAL;
  159. if (!video_is_registered(vdev))
  160. return -EIO;
  161. return vdev->fops->read(filp, buf, sz, off);
  162. }
  163. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  164. size_t sz, loff_t *off)
  165. {
  166. struct video_device *vdev = video_devdata(filp);
  167. if (!vdev->fops->write)
  168. return -EINVAL;
  169. if (!video_is_registered(vdev))
  170. return -EIO;
  171. return vdev->fops->write(filp, buf, sz, off);
  172. }
  173. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  174. {
  175. struct video_device *vdev = video_devdata(filp);
  176. if (!vdev->fops->poll || !video_is_registered(vdev))
  177. return DEFAULT_POLLMASK;
  178. return vdev->fops->poll(filp, poll);
  179. }
  180. static int v4l2_ioctl(struct inode *inode, struct file *filp,
  181. unsigned int cmd, unsigned long arg)
  182. {
  183. struct video_device *vdev = video_devdata(filp);
  184. if (!vdev->fops->ioctl)
  185. return -ENOTTY;
  186. /* Allow ioctl to continue even if the device was unregistered.
  187. Things like dequeueing buffers might still be useful. */
  188. return vdev->fops->ioctl(filp, cmd, arg);
  189. }
  190. static long v4l2_unlocked_ioctl(struct file *filp,
  191. unsigned int cmd, unsigned long arg)
  192. {
  193. struct video_device *vdev = video_devdata(filp);
  194. if (!vdev->fops->unlocked_ioctl)
  195. return -ENOTTY;
  196. /* Allow ioctl to continue even if the device was unregistered.
  197. Things like dequeueing buffers might still be useful. */
  198. return vdev->fops->unlocked_ioctl(filp, cmd, arg);
  199. }
  200. #ifdef CONFIG_MMU
  201. #define v4l2_get_unmapped_area NULL
  202. #else
  203. static unsigned long v4l2_get_unmapped_area(struct file *filp,
  204. unsigned long addr, unsigned long len, unsigned long pgoff,
  205. unsigned long flags)
  206. {
  207. struct video_device *vdev = video_devdata(filp);
  208. if (!vdev->fops->get_unmapped_area)
  209. return -ENOSYS;
  210. if (!video_is_registered(vdev))
  211. return -ENODEV;
  212. return vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
  213. }
  214. #endif
  215. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  216. {
  217. struct video_device *vdev = video_devdata(filp);
  218. if (!vdev->fops->mmap || !video_is_registered(vdev))
  219. return -ENODEV;
  220. return vdev->fops->mmap(filp, vm);
  221. }
  222. /* Override for the open function */
  223. static int v4l2_open(struct inode *inode, struct file *filp)
  224. {
  225. struct video_device *vdev;
  226. int ret = 0;
  227. /* Check if the video device is available */
  228. mutex_lock(&videodev_lock);
  229. vdev = video_devdata(filp);
  230. /* return ENODEV if the video device has been removed
  231. already or if it is not registered anymore. */
  232. if (vdev == NULL || !video_is_registered(vdev)) {
  233. mutex_unlock(&videodev_lock);
  234. return -ENODEV;
  235. }
  236. /* and increase the device refcount */
  237. video_get(vdev);
  238. mutex_unlock(&videodev_lock);
  239. if (vdev->fops->open)
  240. ret = vdev->fops->open(filp);
  241. /* decrease the refcount in case of an error */
  242. if (ret)
  243. video_put(vdev);
  244. return ret;
  245. }
  246. /* Override for the release function */
  247. static int v4l2_release(struct inode *inode, struct file *filp)
  248. {
  249. struct video_device *vdev = video_devdata(filp);
  250. int ret = 0;
  251. if (vdev->fops->release)
  252. vdev->fops->release(filp);
  253. /* decrease the refcount unconditionally since the release()
  254. return value is ignored. */
  255. video_put(vdev);
  256. return ret;
  257. }
  258. static const struct file_operations v4l2_unlocked_fops = {
  259. .owner = THIS_MODULE,
  260. .read = v4l2_read,
  261. .write = v4l2_write,
  262. .open = v4l2_open,
  263. .get_unmapped_area = v4l2_get_unmapped_area,
  264. .mmap = v4l2_mmap,
  265. .unlocked_ioctl = v4l2_unlocked_ioctl,
  266. #ifdef CONFIG_COMPAT
  267. .compat_ioctl = v4l2_compat_ioctl32,
  268. #endif
  269. .release = v4l2_release,
  270. .poll = v4l2_poll,
  271. .llseek = no_llseek,
  272. };
  273. static const struct file_operations v4l2_fops = {
  274. .owner = THIS_MODULE,
  275. .read = v4l2_read,
  276. .write = v4l2_write,
  277. .open = v4l2_open,
  278. .get_unmapped_area = v4l2_get_unmapped_area,
  279. .mmap = v4l2_mmap,
  280. .ioctl = v4l2_ioctl,
  281. #ifdef CONFIG_COMPAT
  282. .compat_ioctl = v4l2_compat_ioctl32,
  283. #endif
  284. .release = v4l2_release,
  285. .poll = v4l2_poll,
  286. .llseek = no_llseek,
  287. };
  288. /**
  289. * get_index - assign stream index number based on parent device
  290. * @vdev: video_device to assign index number to, vdev->parent should be assigned
  291. *
  292. * Note that when this is called the new device has not yet been registered
  293. * in the video_device array, but it was able to obtain a minor number.
  294. *
  295. * This means that we can always obtain a free stream index number since
  296. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  297. * use of the video_device array.
  298. *
  299. * Returns a free index number.
  300. */
  301. static int get_index(struct video_device *vdev)
  302. {
  303. /* This can be static since this function is called with the global
  304. videodev_lock held. */
  305. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  306. int i;
  307. /* Some drivers do not set the parent. In that case always return 0. */
  308. if (vdev->parent == NULL)
  309. return 0;
  310. bitmap_zero(used, VIDEO_NUM_DEVICES);
  311. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  312. if (video_device[i] != NULL &&
  313. video_device[i]->parent == vdev->parent) {
  314. set_bit(video_device[i]->index, used);
  315. }
  316. }
  317. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  318. }
  319. /**
  320. * video_register_device - register video4linux devices
  321. * @vdev: video device structure we want to register
  322. * @type: type of device to register
  323. * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  324. * -1 == first free)
  325. * @warn_if_nr_in_use: warn if the desired device node number
  326. * was already in use and another number was chosen instead.
  327. *
  328. * The registration code assigns minor numbers and device node numbers
  329. * based on the requested type and registers the new device node with
  330. * the kernel.
  331. * An error is returned if no free minor or device node number could be
  332. * found, or if the registration of the device node failed.
  333. *
  334. * Zero is returned on success.
  335. *
  336. * Valid types are
  337. *
  338. * %VFL_TYPE_GRABBER - A frame grabber
  339. *
  340. * %VFL_TYPE_VTX - A teletext device
  341. *
  342. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  343. *
  344. * %VFL_TYPE_RADIO - A radio card
  345. */
  346. static int __video_register_device(struct video_device *vdev, int type, int nr,
  347. int warn_if_nr_in_use)
  348. {
  349. int i = 0;
  350. int ret;
  351. int minor_offset = 0;
  352. int minor_cnt = VIDEO_NUM_DEVICES;
  353. const char *name_base;
  354. void *priv = vdev->dev.p;
  355. /* A minor value of -1 marks this video device as never
  356. having been registered */
  357. vdev->minor = -1;
  358. /* the release callback MUST be present */
  359. WARN_ON(!vdev->release);
  360. if (!vdev->release)
  361. return -EINVAL;
  362. /* v4l2_fh support */
  363. spin_lock_init(&vdev->fh_lock);
  364. INIT_LIST_HEAD(&vdev->fh_list);
  365. /* Part 1: check device type */
  366. switch (type) {
  367. case VFL_TYPE_GRABBER:
  368. name_base = "video";
  369. break;
  370. case VFL_TYPE_VTX:
  371. name_base = "vtx";
  372. break;
  373. case VFL_TYPE_VBI:
  374. name_base = "vbi";
  375. break;
  376. case VFL_TYPE_RADIO:
  377. name_base = "radio";
  378. break;
  379. default:
  380. printk(KERN_ERR "%s called with unknown type: %d\n",
  381. __func__, type);
  382. return -EINVAL;
  383. }
  384. vdev->vfl_type = type;
  385. vdev->cdev = NULL;
  386. if (vdev->v4l2_dev && vdev->v4l2_dev->dev)
  387. vdev->parent = vdev->v4l2_dev->dev;
  388. /* Part 2: find a free minor, device node number and device index. */
  389. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  390. /* Keep the ranges for the first four types for historical
  391. * reasons.
  392. * Newer devices (not yet in place) should use the range
  393. * of 128-191 and just pick the first free minor there
  394. * (new style). */
  395. switch (type) {
  396. case VFL_TYPE_GRABBER:
  397. minor_offset = 0;
  398. minor_cnt = 64;
  399. break;
  400. case VFL_TYPE_RADIO:
  401. minor_offset = 64;
  402. minor_cnt = 64;
  403. break;
  404. case VFL_TYPE_VTX:
  405. minor_offset = 192;
  406. minor_cnt = 32;
  407. break;
  408. case VFL_TYPE_VBI:
  409. minor_offset = 224;
  410. minor_cnt = 32;
  411. break;
  412. default:
  413. minor_offset = 128;
  414. minor_cnt = 64;
  415. break;
  416. }
  417. #endif
  418. /* Pick a device node number */
  419. mutex_lock(&videodev_lock);
  420. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  421. if (nr == minor_cnt)
  422. nr = devnode_find(vdev, 0, minor_cnt);
  423. if (nr == minor_cnt) {
  424. printk(KERN_ERR "could not get a free device node number\n");
  425. mutex_unlock(&videodev_lock);
  426. return -ENFILE;
  427. }
  428. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  429. /* 1-on-1 mapping of device node number to minor number */
  430. i = nr;
  431. #else
  432. /* The device node number and minor numbers are independent, so
  433. we just find the first free minor number. */
  434. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  435. if (video_device[i] == NULL)
  436. break;
  437. if (i == VIDEO_NUM_DEVICES) {
  438. mutex_unlock(&videodev_lock);
  439. printk(KERN_ERR "could not get a free minor\n");
  440. return -ENFILE;
  441. }
  442. #endif
  443. vdev->minor = i + minor_offset;
  444. vdev->num = nr;
  445. devnode_set(vdev);
  446. /* Should not happen since we thought this minor was free */
  447. WARN_ON(video_device[vdev->minor] != NULL);
  448. vdev->index = get_index(vdev);
  449. mutex_unlock(&videodev_lock);
  450. /* Part 3: Initialize the character device */
  451. vdev->cdev = cdev_alloc();
  452. if (vdev->cdev == NULL) {
  453. ret = -ENOMEM;
  454. goto cleanup;
  455. }
  456. if (vdev->fops->unlocked_ioctl)
  457. vdev->cdev->ops = &v4l2_unlocked_fops;
  458. else
  459. vdev->cdev->ops = &v4l2_fops;
  460. vdev->cdev->owner = vdev->fops->owner;
  461. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  462. if (ret < 0) {
  463. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  464. kfree(vdev->cdev);
  465. vdev->cdev = NULL;
  466. goto cleanup;
  467. }
  468. /* Part 4: register the device with sysfs */
  469. memset(&vdev->dev, 0, sizeof(vdev->dev));
  470. /* The memset above cleared the device's device_private, so
  471. put back the copy we made earlier. */
  472. vdev->dev.p = priv;
  473. vdev->dev.class = &video_class;
  474. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  475. if (vdev->parent)
  476. vdev->dev.parent = vdev->parent;
  477. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  478. ret = device_register(&vdev->dev);
  479. if (ret < 0) {
  480. printk(KERN_ERR "%s: device_register failed\n", __func__);
  481. goto cleanup;
  482. }
  483. /* Register the release callback that will be called when the last
  484. reference to the device goes away. */
  485. vdev->dev.release = v4l2_device_release;
  486. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  487. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  488. name_base, nr, video_device_node_name(vdev));
  489. /* Part 5: Activate this minor. The char device can now be used. */
  490. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  491. mutex_lock(&videodev_lock);
  492. video_device[vdev->minor] = vdev;
  493. mutex_unlock(&videodev_lock);
  494. return 0;
  495. cleanup:
  496. mutex_lock(&videodev_lock);
  497. if (vdev->cdev)
  498. cdev_del(vdev->cdev);
  499. devnode_clear(vdev);
  500. mutex_unlock(&videodev_lock);
  501. /* Mark this video device as never having been registered. */
  502. vdev->minor = -1;
  503. return ret;
  504. }
  505. int video_register_device(struct video_device *vdev, int type, int nr)
  506. {
  507. return __video_register_device(vdev, type, nr, 1);
  508. }
  509. EXPORT_SYMBOL(video_register_device);
  510. int video_register_device_no_warn(struct video_device *vdev, int type, int nr)
  511. {
  512. return __video_register_device(vdev, type, nr, 0);
  513. }
  514. EXPORT_SYMBOL(video_register_device_no_warn);
  515. /**
  516. * video_unregister_device - unregister a video4linux device
  517. * @vdev: the device to unregister
  518. *
  519. * This unregisters the passed device. Future open calls will
  520. * be met with errors.
  521. */
  522. void video_unregister_device(struct video_device *vdev)
  523. {
  524. /* Check if vdev was ever registered at all */
  525. if (!vdev || !video_is_registered(vdev))
  526. return;
  527. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  528. device_unregister(&vdev->dev);
  529. }
  530. EXPORT_SYMBOL(video_unregister_device);
  531. /*
  532. * Initialise video for linux
  533. */
  534. static int __init videodev_init(void)
  535. {
  536. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  537. int ret;
  538. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  539. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  540. if (ret < 0) {
  541. printk(KERN_WARNING "videodev: unable to get major %d\n",
  542. VIDEO_MAJOR);
  543. return ret;
  544. }
  545. ret = class_register(&video_class);
  546. if (ret < 0) {
  547. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  548. printk(KERN_WARNING "video_dev: class_register failed\n");
  549. return -EIO;
  550. }
  551. return 0;
  552. }
  553. static void __exit videodev_exit(void)
  554. {
  555. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  556. class_unregister(&video_class);
  557. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  558. }
  559. module_init(videodev_init)
  560. module_exit(videodev_exit)
  561. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  562. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  563. MODULE_LICENSE("GPL");
  564. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
  565. /*
  566. * Local variables:
  567. * c-basic-offset: 8
  568. * End:
  569. */