v4l2-dev.c 9.8 KB

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