v4l2-dev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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@redhat.com> (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. #define VIDEO_NUM_DEVICES 256
  32. #define VIDEO_NAME "video4linux"
  33. /*
  34. * sysfs stuff
  35. */
  36. static ssize_t show_index(struct device *cd,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct video_device *vfd = container_of(cd, struct video_device, dev);
  40. return sprintf(buf, "%i\n", vfd->index);
  41. }
  42. static ssize_t show_name(struct device *cd,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct video_device *vfd = container_of(cd, struct video_device, dev);
  46. return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
  47. }
  48. static struct device_attribute video_device_attrs[] = {
  49. __ATTR(name, S_IRUGO, show_name, NULL),
  50. __ATTR(index, S_IRUGO, show_index, NULL),
  51. __ATTR_NULL
  52. };
  53. /*
  54. * Active devices
  55. */
  56. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  57. static DEFINE_MUTEX(videodev_lock);
  58. static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
  59. struct video_device *video_device_alloc(void)
  60. {
  61. return kzalloc(sizeof(struct video_device), GFP_KERNEL);
  62. }
  63. EXPORT_SYMBOL(video_device_alloc);
  64. void video_device_release(struct video_device *vfd)
  65. {
  66. kfree(vfd);
  67. }
  68. EXPORT_SYMBOL(video_device_release);
  69. void video_device_release_empty(struct video_device *vfd)
  70. {
  71. /* Do nothing */
  72. /* Only valid when the video_device struct is a static. */
  73. }
  74. EXPORT_SYMBOL(video_device_release_empty);
  75. /* Called when the last user of the character device is gone. */
  76. static void v4l2_chardev_release(struct kobject *kobj)
  77. {
  78. struct video_device *vfd = container_of(kobj, struct video_device, cdev.kobj);
  79. mutex_lock(&videodev_lock);
  80. if (video_device[vfd->minor] != vfd) {
  81. mutex_unlock(&videodev_lock);
  82. BUG();
  83. return;
  84. }
  85. /* Free up this device for reuse */
  86. video_device[vfd->minor] = NULL;
  87. clear_bit(vfd->num, video_nums[vfd->vfl_type]);
  88. mutex_unlock(&videodev_lock);
  89. /* Release the character device */
  90. vfd->cdev_release(kobj);
  91. /* Release video_device and perform other
  92. cleanups as needed. */
  93. if (vfd->release)
  94. vfd->release(vfd);
  95. }
  96. /* The new kobj_type for the character device */
  97. static struct kobj_type v4l2_ktype_cdev_default = {
  98. .release = v4l2_chardev_release,
  99. };
  100. static void video_release(struct device *cd)
  101. {
  102. struct video_device *vfd = container_of(cd, struct video_device, dev);
  103. /* It's now safe to delete the char device.
  104. This will either trigger the v4l2_chardev_release immediately (if
  105. the refcount goes to 0) or later when the last user of the
  106. character device closes it. */
  107. cdev_del(&vfd->cdev);
  108. }
  109. static struct class video_class = {
  110. .name = VIDEO_NAME,
  111. .dev_attrs = video_device_attrs,
  112. .dev_release = video_release,
  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. /**
  120. * get_index - assign stream number based on parent device
  121. * @vdev: video_device to assign index number to, vdev->dev should be assigned
  122. * @num: -1 if auto assign, requested number otherwise
  123. *
  124. *
  125. * returns -ENFILE if num is already in use, a free index number if
  126. * successful.
  127. */
  128. static int get_index(struct video_device *vdev, int num)
  129. {
  130. u32 used = 0;
  131. const int max_index = sizeof(used) * 8 - 1;
  132. int i;
  133. /* Currently a single v4l driver instance cannot create more than
  134. 32 devices.
  135. Increase to u64 or an array of u32 if more are needed. */
  136. if (num > max_index) {
  137. printk(KERN_ERR "videodev: %s num is too large\n", __func__);
  138. return -EINVAL;
  139. }
  140. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  141. if (video_device[i] != NULL &&
  142. video_device[i] != vdev &&
  143. video_device[i]->parent == vdev->parent) {
  144. used |= 1 << video_device[i]->index;
  145. }
  146. }
  147. if (num >= 0) {
  148. if (used & (1 << num))
  149. return -ENFILE;
  150. return num;
  151. }
  152. i = ffz(used);
  153. return i > max_index ? -ENFILE : i;
  154. }
  155. static const struct file_operations video_fops;
  156. int video_register_device(struct video_device *vfd, int type, int nr)
  157. {
  158. return video_register_device_index(vfd, type, nr, -1);
  159. }
  160. EXPORT_SYMBOL(video_register_device);
  161. /**
  162. * video_register_device_index - register video4linux devices
  163. * @vfd: video device structure we want to register
  164. * @type: type of device to register
  165. * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
  166. * -1 == first free)
  167. * @index: stream number based on parent device;
  168. * -1 if auto assign, requested number otherwise
  169. *
  170. * The registration code assigns minor numbers based on the type
  171. * requested. -ENFILE is returned in all the device slots for this
  172. * category are full. If not then the minor field is set and the
  173. * driver initialize function is called (if non %NULL).
  174. *
  175. * Zero is returned on success.
  176. *
  177. * Valid types are
  178. *
  179. * %VFL_TYPE_GRABBER - A frame grabber
  180. *
  181. * %VFL_TYPE_VTX - A teletext device
  182. *
  183. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  184. *
  185. * %VFL_TYPE_RADIO - A radio card
  186. */
  187. int video_register_device_index(struct video_device *vfd, int type, int nr,
  188. int index)
  189. {
  190. int i = 0;
  191. int ret;
  192. int minor_offset = 0;
  193. int minor_cnt = VIDEO_NUM_DEVICES;
  194. const char *name_base;
  195. void *priv = video_get_drvdata(vfd);
  196. /* the release callback MUST be present */
  197. BUG_ON(!vfd->release);
  198. if (vfd == NULL)
  199. return -EINVAL;
  200. switch (type) {
  201. case VFL_TYPE_GRABBER:
  202. name_base = "video";
  203. break;
  204. case VFL_TYPE_VTX:
  205. name_base = "vtx";
  206. break;
  207. case VFL_TYPE_VBI:
  208. name_base = "vbi";
  209. break;
  210. case VFL_TYPE_RADIO:
  211. name_base = "radio";
  212. break;
  213. default:
  214. printk(KERN_ERR "%s called with unknown type: %d\n",
  215. __func__, type);
  216. return -EINVAL;
  217. }
  218. vfd->vfl_type = type;
  219. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  220. /* Keep the ranges for the first four types for historical
  221. * reasons.
  222. * Newer devices (not yet in place) should use the range
  223. * of 128-191 and just pick the first free minor there
  224. * (new style). */
  225. switch (type) {
  226. case VFL_TYPE_GRABBER:
  227. minor_offset = 0;
  228. minor_cnt = 64;
  229. break;
  230. case VFL_TYPE_RADIO:
  231. minor_offset = 64;
  232. minor_cnt = 64;
  233. break;
  234. case VFL_TYPE_VTX:
  235. minor_offset = 192;
  236. minor_cnt = 32;
  237. break;
  238. case VFL_TYPE_VBI:
  239. minor_offset = 224;
  240. minor_cnt = 32;
  241. break;
  242. default:
  243. minor_offset = 128;
  244. minor_cnt = 64;
  245. break;
  246. }
  247. #endif
  248. /* Initialize the character device */
  249. cdev_init(&vfd->cdev, vfd->fops);
  250. vfd->cdev.owner = vfd->fops->owner;
  251. /* pick a minor number */
  252. mutex_lock(&videodev_lock);
  253. nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
  254. if (nr == minor_cnt)
  255. nr = find_first_zero_bit(video_nums[type], minor_cnt);
  256. if (nr == minor_cnt) {
  257. printk(KERN_ERR "could not get a free kernel number\n");
  258. mutex_unlock(&videodev_lock);
  259. return -ENFILE;
  260. }
  261. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  262. /* 1-on-1 mapping of kernel number to minor number */
  263. i = nr;
  264. #else
  265. /* The kernel number and minor numbers are independent */
  266. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  267. if (video_device[i] == NULL)
  268. break;
  269. if (i == VIDEO_NUM_DEVICES) {
  270. mutex_unlock(&videodev_lock);
  271. printk(KERN_ERR "could not get a free minor\n");
  272. return -ENFILE;
  273. }
  274. #endif
  275. vfd->minor = i + minor_offset;
  276. vfd->num = nr;
  277. set_bit(nr, video_nums[type]);
  278. BUG_ON(video_device[vfd->minor]);
  279. video_device[vfd->minor] = vfd;
  280. ret = get_index(vfd, index);
  281. vfd->index = ret;
  282. mutex_unlock(&videodev_lock);
  283. if (ret < 0) {
  284. printk(KERN_ERR "%s: get_index failed\n", __func__);
  285. goto fail_minor;
  286. }
  287. ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1);
  288. if (ret < 0) {
  289. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  290. goto fail_minor;
  291. }
  292. /* sysfs class */
  293. memset(&vfd->dev, 0, sizeof(vfd->dev));
  294. /* The memset above cleared the device's drvdata, so
  295. put back the copy we made earlier. */
  296. video_set_drvdata(vfd, priv);
  297. vfd->dev.class = &video_class;
  298. vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
  299. if (vfd->parent)
  300. vfd->dev.parent = vfd->parent;
  301. sprintf(vfd->dev.bus_id, "%s%d", name_base, nr);
  302. ret = device_register(&vfd->dev);
  303. if (ret < 0) {
  304. printk(KERN_ERR "%s: device_register failed\n", __func__);
  305. goto del_cdev;
  306. }
  307. /* Remember the cdev's release function */
  308. vfd->cdev_release = vfd->cdev.kobj.ktype->release;
  309. /* Install our own */
  310. vfd->cdev.kobj.ktype = &v4l2_ktype_cdev_default;
  311. return 0;
  312. del_cdev:
  313. cdev_del(&vfd->cdev);
  314. fail_minor:
  315. mutex_lock(&videodev_lock);
  316. video_device[vfd->minor] = NULL;
  317. clear_bit(vfd->num, video_nums[type]);
  318. mutex_unlock(&videodev_lock);
  319. vfd->minor = -1;
  320. return ret;
  321. }
  322. EXPORT_SYMBOL(video_register_device_index);
  323. /**
  324. * video_unregister_device - unregister a video4linux device
  325. * @vfd: the device to unregister
  326. *
  327. * This unregisters the passed device and deassigns the minor
  328. * number. Future open calls will be met with errors.
  329. */
  330. void video_unregister_device(struct video_device *vfd)
  331. {
  332. device_unregister(&vfd->dev);
  333. }
  334. EXPORT_SYMBOL(video_unregister_device);
  335. /*
  336. * Initialise video for linux
  337. */
  338. static int __init videodev_init(void)
  339. {
  340. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  341. int ret;
  342. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  343. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  344. if (ret < 0) {
  345. printk(KERN_WARNING "videodev: unable to get major %d\n",
  346. VIDEO_MAJOR);
  347. return ret;
  348. }
  349. ret = class_register(&video_class);
  350. if (ret < 0) {
  351. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  352. printk(KERN_WARNING "video_dev: class_register failed\n");
  353. return -EIO;
  354. }
  355. return 0;
  356. }
  357. static void __exit videodev_exit(void)
  358. {
  359. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  360. class_unregister(&video_class);
  361. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  362. }
  363. module_init(videodev_init)
  364. module_exit(videodev_exit)
  365. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  366. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  367. MODULE_LICENSE("GPL");
  368. /*
  369. * Local variables:
  370. * c-basic-offset: 8
  371. * End:
  372. */