v4l2-dev.c 9.6 KB

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