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