v4l2-dev.c 16 KB

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