v4l2-dev.c 9.1 KB

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