v4l2-dev.c 15 KB

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