videodev.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Video capture interface for Linux
  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. * Author: Alan Cox, <alan@redhat.com>
  13. *
  14. * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
  15. * - Added procfs support
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/kmod.h>
  27. #include <linux/slab.h>
  28. #include <linux/devfs_fs_kernel.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/system.h>
  31. #include <asm/semaphore.h>
  32. #include <linux/videodev.h>
  33. #define VIDEO_NUM_DEVICES 256
  34. #define VIDEO_NAME "video4linux"
  35. /*
  36. * sysfs stuff
  37. */
  38. static ssize_t show_name(struct class_device *cd, char *buf)
  39. {
  40. struct video_device *vfd = container_of(cd, struct video_device, class_dev);
  41. return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
  42. }
  43. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  44. struct video_device *video_device_alloc(void)
  45. {
  46. struct video_device *vfd;
  47. vfd = kmalloc(sizeof(*vfd),GFP_KERNEL);
  48. if (NULL == vfd)
  49. return NULL;
  50. memset(vfd,0,sizeof(*vfd));
  51. return vfd;
  52. }
  53. void video_device_release(struct video_device *vfd)
  54. {
  55. kfree(vfd);
  56. }
  57. static void video_release(struct class_device *cd)
  58. {
  59. struct video_device *vfd = container_of(cd, struct video_device, class_dev);
  60. #if 1 /* needed until all drivers are fixed */
  61. if (!vfd->release)
  62. return;
  63. #endif
  64. vfd->release(vfd);
  65. }
  66. static struct class video_class = {
  67. .name = VIDEO_NAME,
  68. .release = video_release,
  69. };
  70. /*
  71. * Active devices
  72. */
  73. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  74. static DECLARE_MUTEX(videodev_lock);
  75. struct video_device* video_devdata(struct file *file)
  76. {
  77. return video_device[iminor(file->f_dentry->d_inode)];
  78. }
  79. /*
  80. * Open a video device.
  81. */
  82. static int video_open(struct inode *inode, struct file *file)
  83. {
  84. unsigned int minor = iminor(inode);
  85. int err = 0;
  86. struct video_device *vfl;
  87. struct file_operations *old_fops;
  88. if(minor>=VIDEO_NUM_DEVICES)
  89. return -ENODEV;
  90. down(&videodev_lock);
  91. vfl=video_device[minor];
  92. if(vfl==NULL) {
  93. up(&videodev_lock);
  94. request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
  95. down(&videodev_lock);
  96. vfl=video_device[minor];
  97. if (vfl==NULL) {
  98. up(&videodev_lock);
  99. return -ENODEV;
  100. }
  101. }
  102. old_fops = file->f_op;
  103. file->f_op = fops_get(vfl->fops);
  104. if(file->f_op->open)
  105. err = file->f_op->open(inode,file);
  106. if (err) {
  107. fops_put(file->f_op);
  108. file->f_op = fops_get(old_fops);
  109. }
  110. fops_put(old_fops);
  111. up(&videodev_lock);
  112. return err;
  113. }
  114. /*
  115. * helper function -- handles userspace copying for ioctl arguments
  116. */
  117. static unsigned int
  118. video_fix_command(unsigned int cmd)
  119. {
  120. switch (cmd) {
  121. case VIDIOC_OVERLAY_OLD:
  122. cmd = VIDIOC_OVERLAY;
  123. break;
  124. case VIDIOC_S_PARM_OLD:
  125. cmd = VIDIOC_S_PARM;
  126. break;
  127. case VIDIOC_S_CTRL_OLD:
  128. cmd = VIDIOC_S_CTRL;
  129. break;
  130. case VIDIOC_G_AUDIO_OLD:
  131. cmd = VIDIOC_G_AUDIO;
  132. break;
  133. case VIDIOC_G_AUDOUT_OLD:
  134. cmd = VIDIOC_G_AUDOUT;
  135. break;
  136. case VIDIOC_CROPCAP_OLD:
  137. cmd = VIDIOC_CROPCAP;
  138. break;
  139. }
  140. return cmd;
  141. }
  142. int
  143. video_usercopy(struct inode *inode, struct file *file,
  144. unsigned int cmd, unsigned long arg,
  145. int (*func)(struct inode *inode, struct file *file,
  146. unsigned int cmd, void *arg))
  147. {
  148. char sbuf[128];
  149. void *mbuf = NULL;
  150. void *parg = NULL;
  151. int err = -EINVAL;
  152. cmd = video_fix_command(cmd);
  153. /* Copy arguments into temp kernel buffer */
  154. switch (_IOC_DIR(cmd)) {
  155. case _IOC_NONE:
  156. parg = NULL;
  157. break;
  158. case _IOC_READ:
  159. case _IOC_WRITE:
  160. case (_IOC_WRITE | _IOC_READ):
  161. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  162. parg = sbuf;
  163. } else {
  164. /* too big to allocate from stack */
  165. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  166. if (NULL == mbuf)
  167. return -ENOMEM;
  168. parg = mbuf;
  169. }
  170. err = -EFAULT;
  171. if (_IOC_DIR(cmd) & _IOC_WRITE)
  172. if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
  173. goto out;
  174. break;
  175. }
  176. /* call driver */
  177. err = func(inode, file, cmd, parg);
  178. if (err == -ENOIOCTLCMD)
  179. err = -EINVAL;
  180. if (err < 0)
  181. goto out;
  182. /* Copy results into user buffer */
  183. switch (_IOC_DIR(cmd))
  184. {
  185. case _IOC_READ:
  186. case (_IOC_WRITE | _IOC_READ):
  187. if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  188. err = -EFAULT;
  189. break;
  190. }
  191. out:
  192. if (mbuf)
  193. kfree(mbuf);
  194. return err;
  195. }
  196. /*
  197. * open/release helper functions -- handle exclusive opens
  198. */
  199. int video_exclusive_open(struct inode *inode, struct file *file)
  200. {
  201. struct video_device *vfl = video_devdata(file);
  202. int retval = 0;
  203. down(&vfl->lock);
  204. if (vfl->users) {
  205. retval = -EBUSY;
  206. } else {
  207. vfl->users++;
  208. }
  209. up(&vfl->lock);
  210. return retval;
  211. }
  212. int video_exclusive_release(struct inode *inode, struct file *file)
  213. {
  214. struct video_device *vfl = video_devdata(file);
  215. vfl->users--;
  216. return 0;
  217. }
  218. static struct file_operations video_fops;
  219. /**
  220. * video_register_device - register video4linux devices
  221. * @vfd: video device structure we want to register
  222. * @type: type of device to register
  223. * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
  224. * -1 == first free)
  225. *
  226. * The registration code assigns minor numbers based on the type
  227. * requested. -ENFILE is returned in all the device slots for this
  228. * category are full. If not then the minor field is set and the
  229. * driver initialize function is called (if non %NULL).
  230. *
  231. * Zero is returned on success.
  232. *
  233. * Valid types are
  234. *
  235. * %VFL_TYPE_GRABBER - A frame grabber
  236. *
  237. * %VFL_TYPE_VTX - A teletext device
  238. *
  239. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  240. *
  241. * %VFL_TYPE_RADIO - A radio card
  242. */
  243. int video_register_device(struct video_device *vfd, int type, int nr)
  244. {
  245. int i=0;
  246. int base;
  247. int end;
  248. char *name_base;
  249. switch(type)
  250. {
  251. case VFL_TYPE_GRABBER:
  252. base=0;
  253. end=64;
  254. name_base = "video";
  255. break;
  256. case VFL_TYPE_VTX:
  257. base=192;
  258. end=224;
  259. name_base = "vtx";
  260. break;
  261. case VFL_TYPE_VBI:
  262. base=224;
  263. end=240;
  264. name_base = "vbi";
  265. break;
  266. case VFL_TYPE_RADIO:
  267. base=64;
  268. end=128;
  269. name_base = "radio";
  270. break;
  271. default:
  272. return -1;
  273. }
  274. /* pick a minor number */
  275. down(&videodev_lock);
  276. if (nr >= 0 && nr < end-base) {
  277. /* use the one the driver asked for */
  278. i = base+nr;
  279. if (NULL != video_device[i]) {
  280. up(&videodev_lock);
  281. return -ENFILE;
  282. }
  283. } else {
  284. /* use first free */
  285. for(i=base;i<end;i++)
  286. if (NULL == video_device[i])
  287. break;
  288. if (i == end) {
  289. up(&videodev_lock);
  290. return -ENFILE;
  291. }
  292. }
  293. video_device[i]=vfd;
  294. vfd->minor=i;
  295. up(&videodev_lock);
  296. sprintf(vfd->devfs_name, "v4l/%s%d", name_base, i - base);
  297. devfs_mk_cdev(MKDEV(VIDEO_MAJOR, vfd->minor),
  298. S_IFCHR | S_IRUSR | S_IWUSR, vfd->devfs_name);
  299. init_MUTEX(&vfd->lock);
  300. /* sysfs class */
  301. memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
  302. if (vfd->dev)
  303. vfd->class_dev.dev = vfd->dev;
  304. vfd->class_dev.class = &video_class;
  305. vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
  306. strlcpy(vfd->class_dev.class_id, vfd->devfs_name + 4, BUS_ID_SIZE);
  307. class_device_register(&vfd->class_dev);
  308. class_device_create_file(&vfd->class_dev,
  309. &class_device_attr_name);
  310. #if 1 /* needed until all drivers are fixed */
  311. if (!vfd->release)
  312. printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
  313. "Please fix your driver for proper sysfs support, see "
  314. "http://lwn.net/Articles/36850/\n", vfd->name);
  315. #endif
  316. return 0;
  317. }
  318. /**
  319. * video_unregister_device - unregister a video4linux device
  320. * @vfd: the device to unregister
  321. *
  322. * This unregisters the passed device and deassigns the minor
  323. * number. Future open calls will be met with errors.
  324. */
  325. void video_unregister_device(struct video_device *vfd)
  326. {
  327. down(&videodev_lock);
  328. if(video_device[vfd->minor]!=vfd)
  329. panic("videodev: bad unregister");
  330. devfs_remove(vfd->devfs_name);
  331. video_device[vfd->minor]=NULL;
  332. class_device_unregister(&vfd->class_dev);
  333. up(&videodev_lock);
  334. }
  335. static struct file_operations video_fops=
  336. {
  337. .owner = THIS_MODULE,
  338. .llseek = no_llseek,
  339. .open = video_open,
  340. };
  341. /*
  342. * Initialise video for linux
  343. */
  344. static int __init videodev_init(void)
  345. {
  346. int ret;
  347. printk(KERN_INFO "Linux video capture interface: v1.00\n");
  348. if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
  349. printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
  350. return -EIO;
  351. }
  352. ret = class_register(&video_class);
  353. if (ret < 0) {
  354. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  355. printk(KERN_WARNING "video_dev: class_register failed\n");
  356. return -EIO;
  357. }
  358. return 0;
  359. }
  360. static void __exit videodev_exit(void)
  361. {
  362. class_unregister(&video_class);
  363. unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
  364. }
  365. module_init(videodev_init)
  366. module_exit(videodev_exit)
  367. EXPORT_SYMBOL(video_register_device);
  368. EXPORT_SYMBOL(video_unregister_device);
  369. EXPORT_SYMBOL(video_devdata);
  370. EXPORT_SYMBOL(video_usercopy);
  371. EXPORT_SYMBOL(video_exclusive_open);
  372. EXPORT_SYMBOL(video_exclusive_release);
  373. EXPORT_SYMBOL(video_device_alloc);
  374. EXPORT_SYMBOL(video_device_release);
  375. MODULE_AUTHOR("Alan Cox");
  376. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
  377. MODULE_LICENSE("GPL");
  378. /*
  379. * Local variables:
  380. * c-basic-offset: 8
  381. * End:
  382. */