v4l2-dev.c 15 KB

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