v4l2-dev.c 9.4 KB

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