videodev.c 9.5 KB

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