v4l2-dev.c 9.8 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. mutex_lock(&videodev_lock);
  102. vfl = video_device[minor];
  103. if (vfl == NULL) {
  104. mutex_unlock(&videodev_lock);
  105. request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
  106. mutex_lock(&videodev_lock);
  107. vfl = video_device[minor];
  108. if (vfl == NULL) {
  109. mutex_unlock(&videodev_lock);
  110. return -ENODEV;
  111. }
  112. }
  113. old_fops = file->f_op;
  114. file->f_op = fops_get(vfl->fops);
  115. if (file->f_op->open)
  116. err = file->f_op->open(inode, file);
  117. if (err) {
  118. fops_put(file->f_op);
  119. file->f_op = fops_get(old_fops);
  120. }
  121. fops_put(old_fops);
  122. mutex_unlock(&videodev_lock);
  123. return err;
  124. }
  125. /*
  126. * open/release helper functions -- handle exclusive opens
  127. * Should be removed soon
  128. */
  129. int video_exclusive_open(struct inode *inode, struct file *file)
  130. {
  131. struct video_device *vfl = video_devdata(file);
  132. int retval = 0;
  133. mutex_lock(&vfl->lock);
  134. if (vfl->users)
  135. retval = -EBUSY;
  136. else
  137. vfl->users++;
  138. mutex_unlock(&vfl->lock);
  139. return retval;
  140. }
  141. EXPORT_SYMBOL(video_exclusive_open);
  142. int video_exclusive_release(struct inode *inode, struct file *file)
  143. {
  144. struct video_device *vfl = video_devdata(file);
  145. vfl->users--;
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(video_exclusive_release);
  149. /**
  150. * get_index - assign stream number based on parent device
  151. * @vdev: video_device to assign index number to, vdev->dev should be assigned
  152. * @num: -1 if auto assign, requested number otherwise
  153. *
  154. *
  155. * returns -ENFILE if num is already in use, a free index number if
  156. * successful.
  157. */
  158. static int get_index(struct video_device *vdev, int num)
  159. {
  160. u32 used = 0;
  161. const int max_index = sizeof(used) * 8 - 1;
  162. int i;
  163. /* Currently a single v4l driver instance cannot create more than
  164. 32 devices.
  165. Increase to u64 or an array of u32 if more are needed. */
  166. if (num > max_index) {
  167. printk(KERN_ERR "videodev: %s num is too large\n", __func__);
  168. return -EINVAL;
  169. }
  170. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  171. if (video_device[i] != NULL &&
  172. video_device[i] != vdev &&
  173. video_device[i]->parent == vdev->parent) {
  174. used |= 1 << video_device[i]->index;
  175. }
  176. }
  177. if (num >= 0) {
  178. if (used & (1 << num))
  179. return -ENFILE;
  180. return num;
  181. }
  182. i = ffz(used);
  183. return i > max_index ? -ENFILE : i;
  184. }
  185. static const struct file_operations video_fops;
  186. int video_register_device(struct video_device *vfd, int type, int nr)
  187. {
  188. return video_register_device_index(vfd, type, nr, -1);
  189. }
  190. EXPORT_SYMBOL(video_register_device);
  191. /**
  192. * video_register_device_index - register video4linux devices
  193. * @vfd: video device structure we want to register
  194. * @type: type of device to register
  195. * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
  196. * -1 == first free)
  197. * @index: stream number based on parent device;
  198. * -1 if auto assign, requested number otherwise
  199. *
  200. * The registration code assigns minor numbers based on the type
  201. * requested. -ENFILE is returned in all the device slots for this
  202. * category are full. If not then the minor field is set and the
  203. * driver initialize function is called (if non %NULL).
  204. *
  205. * Zero is returned on success.
  206. *
  207. * Valid types are
  208. *
  209. * %VFL_TYPE_GRABBER - A frame grabber
  210. *
  211. * %VFL_TYPE_VTX - A teletext device
  212. *
  213. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  214. *
  215. * %VFL_TYPE_RADIO - A radio card
  216. */
  217. int video_register_device_index(struct video_device *vfd, int type, int nr,
  218. int index)
  219. {
  220. int i = 0;
  221. int base;
  222. int end;
  223. int ret;
  224. char *name_base;
  225. if (vfd == NULL)
  226. return -EINVAL;
  227. if (vfd == NULL)
  228. return -EINVAL;
  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 -EINVAL;
  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->vfl_type = type;
  276. vfd->minor = i;
  277. ret = get_index(vfd, index);
  278. vfd->index = ret;
  279. mutex_unlock(&videodev_lock);
  280. if (ret < 0) {
  281. printk(KERN_ERR "%s: get_index failed\n", __func__);
  282. goto fail_minor;
  283. }
  284. mutex_init(&vfd->lock);
  285. /* sysfs class */
  286. memset(&vfd->dev, 0x00, sizeof(vfd->dev));
  287. vfd->dev.class = &video_class;
  288. vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
  289. if (vfd->parent)
  290. vfd->dev.parent = vfd->parent;
  291. sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
  292. ret = device_register(&vfd->dev);
  293. if (ret < 0) {
  294. printk(KERN_ERR "%s: device_register failed\n", __func__);
  295. goto fail_minor;
  296. }
  297. #if 1
  298. /* needed until all drivers are fixed */
  299. if (!vfd->release)
  300. printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
  301. "Please fix your driver for proper sysfs support, see "
  302. "http://lwn.net/Articles/36850/\n", vfd->name);
  303. #endif
  304. return 0;
  305. fail_minor:
  306. mutex_lock(&videodev_lock);
  307. video_device[vfd->minor] = NULL;
  308. vfd->minor = -1;
  309. mutex_unlock(&videodev_lock);
  310. return ret;
  311. }
  312. EXPORT_SYMBOL(video_register_device_index);
  313. /**
  314. * video_unregister_device - unregister a video4linux device
  315. * @vfd: the device to unregister
  316. *
  317. * This unregisters the passed device and deassigns the minor
  318. * number. Future open calls will be met with errors.
  319. */
  320. void video_unregister_device(struct video_device *vfd)
  321. {
  322. mutex_lock(&videodev_lock);
  323. if (video_device[vfd->minor] != vfd)
  324. panic("videodev: bad unregister");
  325. video_device[vfd->minor] = NULL;
  326. device_unregister(&vfd->dev);
  327. mutex_unlock(&videodev_lock);
  328. }
  329. EXPORT_SYMBOL(video_unregister_device);
  330. /*
  331. * Video fs operations
  332. */
  333. static const struct file_operations video_fops = {
  334. .owner = THIS_MODULE,
  335. .llseek = no_llseek,
  336. .open = video_open,
  337. };
  338. /*
  339. * Initialise video for linux
  340. */
  341. static int __init videodev_init(void)
  342. {
  343. int ret;
  344. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  345. if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
  346. printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
  347. return -EIO;
  348. }
  349. ret = class_register(&video_class);
  350. if (ret < 0) {
  351. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  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. class_unregister(&video_class);
  360. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  361. }
  362. module_init(videodev_init)
  363. module_exit(videodev_exit)
  364. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  365. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  366. MODULE_LICENSE("GPL");
  367. /*
  368. * Local variables:
  369. * c-basic-offset: 8
  370. * End:
  371. */