v4l2-dev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. if (!vdev->fops->read)
  159. return -EINVAL;
  160. if (!video_is_registered(vdev))
  161. return -EIO;
  162. return vdev->fops->read(filp, buf, sz, off);
  163. }
  164. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  165. size_t sz, loff_t *off)
  166. {
  167. struct video_device *vdev = video_devdata(filp);
  168. if (!vdev->fops->write)
  169. return -EINVAL;
  170. if (!video_is_registered(vdev))
  171. return -EIO;
  172. return vdev->fops->write(filp, buf, sz, off);
  173. }
  174. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  175. {
  176. struct video_device *vdev = video_devdata(filp);
  177. if (!vdev->fops->poll || !video_is_registered(vdev))
  178. return DEFAULT_POLLMASK;
  179. return vdev->fops->poll(filp, poll);
  180. }
  181. static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  182. {
  183. struct video_device *vdev = video_devdata(filp);
  184. int ret;
  185. if (!vdev->fops->ioctl)
  186. return -ENOTTY;
  187. if (vdev->fops->unlocked_ioctl) {
  188. ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
  189. } else if (vdev->fops->ioctl) {
  190. /* TODO: convert all drivers to unlocked_ioctl */
  191. lock_kernel();
  192. ret = vdev->fops->ioctl(filp, cmd, arg);
  193. unlock_kernel();
  194. } else
  195. ret = -ENOTTY;
  196. return ret;
  197. }
  198. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  199. {
  200. struct video_device *vdev = video_devdata(filp);
  201. if (!vdev->fops->mmap || !video_is_registered(vdev))
  202. return -ENODEV;
  203. return vdev->fops->mmap(filp, vm);
  204. }
  205. /* Override for the open function */
  206. static int v4l2_open(struct inode *inode, struct file *filp)
  207. {
  208. struct video_device *vdev;
  209. int ret = 0;
  210. /* Check if the video device is available */
  211. mutex_lock(&videodev_lock);
  212. vdev = video_devdata(filp);
  213. /* return ENODEV if the video device has been removed
  214. already or if it is not registered anymore. */
  215. if (vdev == NULL || !video_is_registered(vdev)) {
  216. mutex_unlock(&videodev_lock);
  217. return -ENODEV;
  218. }
  219. /* and increase the device refcount */
  220. video_get(vdev);
  221. mutex_unlock(&videodev_lock);
  222. if (vdev->fops->open)
  223. ret = vdev->fops->open(filp);
  224. /* decrease the refcount in case of an error */
  225. if (ret)
  226. video_put(vdev);
  227. return ret;
  228. }
  229. /* Override for the release function */
  230. static int v4l2_release(struct inode *inode, struct file *filp)
  231. {
  232. struct video_device *vdev = video_devdata(filp);
  233. int ret = 0;
  234. if (vdev->fops->release)
  235. vdev->fops->release(filp);
  236. /* decrease the refcount unconditionally since the release()
  237. return value is ignored. */
  238. video_put(vdev);
  239. return ret;
  240. }
  241. static const struct file_operations v4l2_fops = {
  242. .owner = THIS_MODULE,
  243. .read = v4l2_read,
  244. .write = v4l2_write,
  245. .open = v4l2_open,
  246. .mmap = v4l2_mmap,
  247. .unlocked_ioctl = v4l2_ioctl,
  248. #ifdef CONFIG_COMPAT
  249. .compat_ioctl = v4l2_compat_ioctl32,
  250. #endif
  251. .release = v4l2_release,
  252. .poll = v4l2_poll,
  253. .llseek = no_llseek,
  254. };
  255. /**
  256. * get_index - assign stream index number based on parent device
  257. * @vdev: video_device to assign index number to, vdev->parent should be assigned
  258. *
  259. * Note that when this is called the new device has not yet been registered
  260. * in the video_device array, but it was able to obtain a minor number.
  261. *
  262. * This means that we can always obtain a free stream index number since
  263. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  264. * use of the video_device array.
  265. *
  266. * Returns a free index number.
  267. */
  268. static int get_index(struct video_device *vdev)
  269. {
  270. /* This can be static since this function is called with the global
  271. videodev_lock held. */
  272. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  273. int i;
  274. /* Some drivers do not set the parent. In that case always return 0. */
  275. if (vdev->parent == NULL)
  276. return 0;
  277. bitmap_zero(used, VIDEO_NUM_DEVICES);
  278. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  279. if (video_device[i] != NULL &&
  280. video_device[i]->parent == vdev->parent) {
  281. set_bit(video_device[i]->index, used);
  282. }
  283. }
  284. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  285. }
  286. /**
  287. * video_register_device - register video4linux devices
  288. * @vdev: video device structure we want to register
  289. * @type: type of device to register
  290. * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  291. * -1 == first free)
  292. * @warn_if_nr_in_use: warn if the desired device node number
  293. * was already in use and another number was chosen instead.
  294. *
  295. * The registration code assigns minor numbers and device node numbers
  296. * based on the requested type and registers the new device node with
  297. * the kernel.
  298. * An error is returned if no free minor or device node number could be
  299. * found, or if the registration of the device node failed.
  300. *
  301. * Zero is returned on success.
  302. *
  303. * Valid types are
  304. *
  305. * %VFL_TYPE_GRABBER - A frame grabber
  306. *
  307. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  308. *
  309. * %VFL_TYPE_RADIO - A radio card
  310. */
  311. static int __video_register_device(struct video_device *vdev, int type, int nr,
  312. int warn_if_nr_in_use)
  313. {
  314. int i = 0;
  315. int ret;
  316. int minor_offset = 0;
  317. int minor_cnt = VIDEO_NUM_DEVICES;
  318. const char *name_base;
  319. void *priv = vdev->dev.p;
  320. /* A minor value of -1 marks this video device as never
  321. having been registered */
  322. vdev->minor = -1;
  323. /* the release callback MUST be present */
  324. WARN_ON(!vdev->release);
  325. if (!vdev->release)
  326. return -EINVAL;
  327. /* v4l2_fh support */
  328. spin_lock_init(&vdev->fh_lock);
  329. INIT_LIST_HEAD(&vdev->fh_list);
  330. /* Part 1: check device type */
  331. switch (type) {
  332. case VFL_TYPE_GRABBER:
  333. name_base = "video";
  334. break;
  335. case VFL_TYPE_VBI:
  336. name_base = "vbi";
  337. break;
  338. case VFL_TYPE_RADIO:
  339. name_base = "radio";
  340. break;
  341. default:
  342. printk(KERN_ERR "%s called with unknown type: %d\n",
  343. __func__, type);
  344. return -EINVAL;
  345. }
  346. vdev->vfl_type = type;
  347. vdev->cdev = NULL;
  348. if (vdev->v4l2_dev) {
  349. if (vdev->v4l2_dev->dev)
  350. vdev->parent = vdev->v4l2_dev->dev;
  351. if (vdev->ctrl_handler == NULL)
  352. vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
  353. }
  354. /* Part 2: find a free minor, device node number and device index. */
  355. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  356. /* Keep the ranges for the first four types for historical
  357. * reasons.
  358. * Newer devices (not yet in place) should use the range
  359. * of 128-191 and just pick the first free minor there
  360. * (new style). */
  361. switch (type) {
  362. case VFL_TYPE_GRABBER:
  363. minor_offset = 0;
  364. minor_cnt = 64;
  365. break;
  366. case VFL_TYPE_RADIO:
  367. minor_offset = 64;
  368. minor_cnt = 64;
  369. break;
  370. case VFL_TYPE_VBI:
  371. minor_offset = 224;
  372. minor_cnt = 32;
  373. break;
  374. default:
  375. minor_offset = 128;
  376. minor_cnt = 64;
  377. break;
  378. }
  379. #endif
  380. /* Pick a device node number */
  381. mutex_lock(&videodev_lock);
  382. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  383. if (nr == minor_cnt)
  384. nr = devnode_find(vdev, 0, minor_cnt);
  385. if (nr == minor_cnt) {
  386. printk(KERN_ERR "could not get a free device node number\n");
  387. mutex_unlock(&videodev_lock);
  388. return -ENFILE;
  389. }
  390. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  391. /* 1-on-1 mapping of device node number to minor number */
  392. i = nr;
  393. #else
  394. /* The device node number and minor numbers are independent, so
  395. we just find the first free minor number. */
  396. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  397. if (video_device[i] == NULL)
  398. break;
  399. if (i == VIDEO_NUM_DEVICES) {
  400. mutex_unlock(&videodev_lock);
  401. printk(KERN_ERR "could not get a free minor\n");
  402. return -ENFILE;
  403. }
  404. #endif
  405. vdev->minor = i + minor_offset;
  406. vdev->num = nr;
  407. devnode_set(vdev);
  408. /* Should not happen since we thought this minor was free */
  409. WARN_ON(video_device[vdev->minor] != NULL);
  410. vdev->index = get_index(vdev);
  411. mutex_unlock(&videodev_lock);
  412. /* Part 3: Initialize the character device */
  413. vdev->cdev = cdev_alloc();
  414. if (vdev->cdev == NULL) {
  415. ret = -ENOMEM;
  416. goto cleanup;
  417. }
  418. vdev->cdev->ops = &v4l2_fops;
  419. vdev->cdev->owner = vdev->fops->owner;
  420. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  421. if (ret < 0) {
  422. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  423. kfree(vdev->cdev);
  424. vdev->cdev = NULL;
  425. goto cleanup;
  426. }
  427. /* Part 4: register the device with sysfs */
  428. memset(&vdev->dev, 0, sizeof(vdev->dev));
  429. /* The memset above cleared the device's device_private, so
  430. put back the copy we made earlier. */
  431. vdev->dev.p = priv;
  432. vdev->dev.class = &video_class;
  433. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  434. if (vdev->parent)
  435. vdev->dev.parent = vdev->parent;
  436. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  437. ret = device_register(&vdev->dev);
  438. if (ret < 0) {
  439. printk(KERN_ERR "%s: device_register failed\n", __func__);
  440. goto cleanup;
  441. }
  442. /* Register the release callback that will be called when the last
  443. reference to the device goes away. */
  444. vdev->dev.release = v4l2_device_release;
  445. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  446. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  447. name_base, nr, video_device_node_name(vdev));
  448. /* Part 5: Activate this minor. The char device can now be used. */
  449. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  450. mutex_lock(&videodev_lock);
  451. video_device[vdev->minor] = vdev;
  452. mutex_unlock(&videodev_lock);
  453. return 0;
  454. cleanup:
  455. mutex_lock(&videodev_lock);
  456. if (vdev->cdev)
  457. cdev_del(vdev->cdev);
  458. devnode_clear(vdev);
  459. mutex_unlock(&videodev_lock);
  460. /* Mark this video device as never having been registered. */
  461. vdev->minor = -1;
  462. return ret;
  463. }
  464. int video_register_device(struct video_device *vdev, int type, int nr)
  465. {
  466. return __video_register_device(vdev, type, nr, 1);
  467. }
  468. EXPORT_SYMBOL(video_register_device);
  469. int video_register_device_no_warn(struct video_device *vdev, int type, int nr)
  470. {
  471. return __video_register_device(vdev, type, nr, 0);
  472. }
  473. EXPORT_SYMBOL(video_register_device_no_warn);
  474. /**
  475. * video_unregister_device - unregister a video4linux device
  476. * @vdev: the device to unregister
  477. *
  478. * This unregisters the passed device. Future open calls will
  479. * be met with errors.
  480. */
  481. void video_unregister_device(struct video_device *vdev)
  482. {
  483. /* Check if vdev was ever registered at all */
  484. if (!vdev || !video_is_registered(vdev))
  485. return;
  486. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  487. device_unregister(&vdev->dev);
  488. }
  489. EXPORT_SYMBOL(video_unregister_device);
  490. /*
  491. * Initialise video for linux
  492. */
  493. static int __init videodev_init(void)
  494. {
  495. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  496. int ret;
  497. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  498. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  499. if (ret < 0) {
  500. printk(KERN_WARNING "videodev: unable to get major %d\n",
  501. VIDEO_MAJOR);
  502. return ret;
  503. }
  504. ret = class_register(&video_class);
  505. if (ret < 0) {
  506. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  507. printk(KERN_WARNING "video_dev: class_register failed\n");
  508. return -EIO;
  509. }
  510. return 0;
  511. }
  512. static void __exit videodev_exit(void)
  513. {
  514. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  515. class_unregister(&video_class);
  516. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  517. }
  518. module_init(videodev_init)
  519. module_exit(videodev_exit)
  520. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  521. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  522. MODULE_LICENSE("GPL");
  523. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
  524. /*
  525. * Local variables:
  526. * c-basic-offset: 8
  527. * End:
  528. */