videodev.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/config.h>
  18. #include <linux/version.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/mm.h>
  25. #include <linux/string.h>
  26. #include <linux/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/kmod.h>
  29. #include <linux/slab.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/system.h>
  32. #include <asm/semaphore.h>
  33. #include <linux/videodev.h>
  34. #define VIDEO_NUM_DEVICES 256 
  35. /*
  36.  * Active devices 
  37.  */
  38.  
  39. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  40. static DECLARE_MUTEX(videodev_lock);
  41. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  42. #include <linux/proc_fs.h>
  43. struct videodev_proc_data {
  44. struct list_head proc_list;
  45. char name[16];
  46. struct video_device *vdev;
  47. struct proc_dir_entry *proc_entry;
  48. };
  49. static struct proc_dir_entry *video_dev_proc_entry = NULL;
  50. struct proc_dir_entry *video_proc_entry = NULL;
  51. EXPORT_SYMBOL(video_proc_entry);
  52. LIST_HEAD(videodev_proc_list);
  53. #endif /* CONFIG_PROC_FS && CONFIG_VIDEO_PROC_FS */
  54. /*
  55.  * Read will do some smarts later on. Buffer pin etc.
  56.  */
  57.  
  58. static ssize_t video_read(struct file *file,
  59. char *buf, size_t count, loff_t *ppos)
  60. {
  61. struct video_device *vfl = video_devdata(file);
  62. if(vfl->read)
  63. return vfl->read(vfl, buf, count, file->f_flags&O_NONBLOCK);
  64. else
  65. return -EINVAL;
  66. }
  67. /*
  68.  * Write for now does nothing. No reason it shouldnt do overlay setting
  69.  * for some boards I guess..
  70.  */
  71. static ssize_t video_write(struct file *file, const char *buf, 
  72. size_t count, loff_t *ppos)
  73. {
  74. struct video_device *vfl = video_devdata(file);
  75. if(vfl->write)
  76. return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK);
  77. else
  78. return 0;
  79. }
  80. struct video_device* video_devdata(struct file *file)
  81. {
  82. return video_device[minor(file->f_dentry->d_inode->i_rdev)];
  83. }
  84. /*
  85.  * Poll to see if we're readable, can probably be used for timing on incoming
  86.  * frames, etc..
  87.  */
  88. static unsigned int video_poll(struct file *file, poll_table * wait)
  89. {
  90. struct video_device *vfl = video_devdata(file);
  91. if(vfl->poll)
  92. return vfl->poll(vfl, file, wait);
  93. else
  94. return 0;
  95. }
  96. /*
  97.  * Open a video device.
  98.  */
  99. static int video_open(struct inode *inode, struct file *file)
  100. {
  101. unsigned int minor = minor(inode->i_rdev);
  102. int err = 0;
  103. struct video_device *vfl;
  104. if(minor>=VIDEO_NUM_DEVICES)
  105. return -ENODEV;
  106. down(&videodev_lock);
  107. vfl=video_device[minor];
  108. if(vfl==NULL) {
  109. char modname[20];
  110. up(&videodev_lock);
  111. sprintf (modname, "char-major-%d-%d", VIDEO_MAJOR, minor);
  112. request_module(modname);
  113. down(&videodev_lock);
  114. vfl=video_device[minor];
  115. if (vfl==NULL) {
  116. err = -ENODEV;
  117. goto unlock_out;
  118. }
  119. }
  120. if (vfl->fops) {
  121. struct file_operations *old_fops;
  122. old_fops = file->f_op;
  123.                 file->f_op = fops_get(vfl->fops);
  124.                 if(file->f_op->open)
  125.                         err = file->f_op->open(inode,file);
  126.                 if (err) {
  127.                         fops_put(file->f_op);
  128.                         file->f_op = fops_get(old_fops);
  129.                 }
  130.                 fops_put(old_fops);
  131. goto unlock_out;
  132. }
  133. if(vfl->users) {
  134. err = -EBUSY;
  135. goto unlock_out;
  136. }
  137. vfl->users++; /* In case vfl->open sleeps */
  138. if(vfl->owner)
  139. __MOD_INC_USE_COUNT(vfl->owner);
  140. if (vfl->open) {
  141. err=vfl->open(vfl,0); /* Tell the device it is open */
  142. if (err) {
  143. vfl->users--;
  144. if(vfl->owner)
  145. __MOD_DEC_USE_COUNT(vfl->owner);
  146. goto unlock_out;
  147. }
  148. }
  149. err = 0;
  150. unlock_out:
  151. up(&videodev_lock);
  152. return err;
  153. }
  154. /*
  155.  * Last close of a video for Linux device
  156.  */
  157. static int video_release(struct inode *inode, struct file *file)
  158. {
  159. struct video_device *vfl;
  160. down(&videodev_lock);
  161. vfl = video_devdata(file);
  162. if(vfl->close)
  163. vfl->close(vfl);
  164. vfl->users--;
  165. if(vfl->owner)
  166. __MOD_DEC_USE_COUNT(vfl->owner);
  167. up(&videodev_lock);
  168. return 0;
  169. }
  170. static int video_ioctl(struct inode *inode, struct file *file,
  171. unsigned int cmd, unsigned long arg)
  172. {
  173. struct video_device *vfl = video_devdata(file);
  174. int err=vfl->ioctl(vfl, cmd, (void *)arg);
  175. if(err!=-ENOIOCTLCMD)
  176. return err;
  177. switch(cmd)
  178. {
  179. default:
  180. return -EINVAL;
  181. }
  182. }
  183. /*
  184.  * We need to do MMAP support
  185.  */
  186.   
  187. int video_mmap(struct file *file, struct vm_area_struct *vma)
  188. {
  189. int ret = -EINVAL;
  190. struct video_device *vfl = video_devdata(file);
  191. if(vfl->mmap) {
  192. lock_kernel();
  193. ret = vfl->mmap(vfl, (char *)vma->vm_start, 
  194. (unsigned long)(vma->vm_end-vma->vm_start));
  195. unlock_kernel();
  196. }
  197. return ret;
  198. }
  199. /*
  200.  * helper function -- handles userspace copying for ioctl arguments
  201.  */
  202. int
  203. video_usercopy(struct inode *inode, struct file *file,
  204.        unsigned int cmd, unsigned long arg,
  205.        int (*func)(struct inode *inode, struct file *file,
  206.    unsigned int cmd, void *arg))
  207. {
  208. char sbuf[128];
  209. void    *mbuf = NULL;
  210. void *parg = NULL;
  211. int err  = -EINVAL;
  212. /*  Copy arguments into temp kernel buffer  */
  213. switch (_IOC_DIR(cmd)) {
  214. case _IOC_NONE:
  215. parg = (void *)arg;
  216. break;
  217. case _IOC_READ: /* some v4l ioctls are marked wrong ... */
  218. case _IOC_WRITE:
  219. case (_IOC_WRITE | _IOC_READ):
  220. if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  221. parg = sbuf;
  222. } else {
  223. /* too big to allocate from stack */
  224. mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
  225. if (NULL == mbuf)
  226. return -ENOMEM;
  227. parg = mbuf;
  228. }
  229. err = -EFAULT;
  230. if (copy_from_user(parg, (void *)arg, _IOC_SIZE(cmd)))
  231. goto out;
  232. break;
  233. }
  234. /* call driver */
  235. err = func(inode, file, cmd, parg);
  236. if (err == -ENOIOCTLCMD)
  237. err = -EINVAL;
  238. if (err < 0)
  239. goto out;
  240. /*  Copy results into user buffer  */
  241. switch (_IOC_DIR(cmd))
  242. {
  243. case _IOC_READ:
  244. case (_IOC_WRITE | _IOC_READ):
  245. if (copy_to_user((void *)arg, parg, _IOC_SIZE(cmd)))
  246. err = -EFAULT;
  247. break;
  248. }
  249. out:
  250. if (mbuf)
  251. kfree(mbuf);
  252. return err;
  253. }
  254. /*
  255.  * open/release helper functions -- handle exclusive opens
  256.  */
  257. extern int video_exclusive_open(struct inode *inode, struct file *file)
  258. {
  259. struct  video_device *vfl = video_devdata(file);
  260. int retval = 0;
  261. down(&vfl->lock);
  262. if (vfl->users) {
  263. retval = -EBUSY;
  264. } else {
  265. vfl->users++;
  266. }
  267. up(&vfl->lock);
  268. return retval;
  269. }
  270. extern int video_exclusive_release(struct inode *inode, struct file *file)
  271. {
  272. struct  video_device *vfl = video_devdata(file);
  273. vfl->users--;
  274. return 0;
  275. }
  276. /*
  277.  * /proc support
  278.  */
  279. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  280. /* Hmm... i'd like to see video_capability information here, but
  281.  * how can I access it (without changing the other drivers? -claudio
  282.  */
  283. static int videodev_proc_read(char *page, char **start, off_t off,
  284.        int count, int *eof, void *data)
  285. {
  286. char *out = page;
  287. struct video_device *vfd = data;
  288. struct videodev_proc_data *d;
  289. struct list_head *tmp;
  290. int len;
  291. char c = ' ';
  292. list_for_each (tmp, &videodev_proc_list) {
  293. d = list_entry(tmp, struct videodev_proc_data, proc_list);
  294. if (vfd == d->vdev)
  295. break;
  296. }
  297. /* Sanity check */
  298. if (tmp == &videodev_proc_list)
  299. goto skip;
  300. #define PRINT_VID_TYPE(x) do { if (vfd->type & x) 
  301. out += sprintf (out, "%c%s", c, #x); c='|';} while (0)
  302. out += sprintf (out, "name            : %sn", vfd->name);
  303. out += sprintf (out, "type            :");
  304. PRINT_VID_TYPE(VID_TYPE_CAPTURE);
  305. PRINT_VID_TYPE(VID_TYPE_TUNER);
  306. PRINT_VID_TYPE(VID_TYPE_TELETEXT);
  307. PRINT_VID_TYPE(VID_TYPE_OVERLAY);
  308. PRINT_VID_TYPE(VID_TYPE_CHROMAKEY);
  309. PRINT_VID_TYPE(VID_TYPE_CLIPPING);
  310. PRINT_VID_TYPE(VID_TYPE_FRAMERAM);
  311. PRINT_VID_TYPE(VID_TYPE_SCALES);
  312. PRINT_VID_TYPE(VID_TYPE_MONOCHROME);
  313. PRINT_VID_TYPE(VID_TYPE_SUBCAPTURE);
  314. PRINT_VID_TYPE(VID_TYPE_MPEG_DECODER);
  315. PRINT_VID_TYPE(VID_TYPE_MPEG_ENCODER);
  316. PRINT_VID_TYPE(VID_TYPE_MJPEG_DECODER);
  317. PRINT_VID_TYPE(VID_TYPE_MJPEG_ENCODER);
  318. out += sprintf (out, "n");
  319. out += sprintf (out, "hardware        : 0x%xn", vfd->hardware);
  320. #if 0
  321. out += sprintf (out, "channels        : %dn", d->vcap.channels);
  322. out += sprintf (out, "audios          : %dn", d->vcap.audios);
  323. out += sprintf (out, "maxwidth        : %dn", d->vcap.maxwidth);
  324. out += sprintf (out, "maxheight       : %dn", d->vcap.maxheight);
  325. out += sprintf (out, "minwidth        : %dn", d->vcap.minwidth);
  326. out += sprintf (out, "minheight       : %dn", d->vcap.minheight);
  327. #endif
  328. skip:
  329. len = out - page;
  330. len -= off;
  331. if (len < count) {
  332. *eof = 1;
  333. if (len <= 0)
  334. return 0;
  335. } else
  336. len = count;
  337. *start = page + off;
  338. return len;
  339. }
  340. static void videodev_proc_create(void)
  341. {
  342. video_proc_entry = create_proc_entry("video", S_IFDIR, &proc_root);
  343. if (video_proc_entry == NULL) {
  344. printk("video_dev: unable to initialise /proc/videon");
  345. return;
  346. }
  347. video_proc_entry->owner = THIS_MODULE;
  348. video_dev_proc_entry = create_proc_entry("dev", S_IFDIR, video_proc_entry);
  349. if (video_dev_proc_entry == NULL) {
  350. printk("video_dev: unable to initialise /proc/video/devn");
  351. return;
  352. }
  353. video_dev_proc_entry->owner = THIS_MODULE;
  354. }
  355. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  356. static void videodev_proc_destroy(void)
  357. {
  358. if (video_dev_proc_entry != NULL)
  359. remove_proc_entry("dev", video_proc_entry);
  360. if (video_proc_entry != NULL)
  361. remove_proc_entry("video", &proc_root);
  362. }
  363. #endif
  364. static void videodev_proc_create_dev (struct video_device *vfd, char *name)
  365. {
  366. struct videodev_proc_data *d;
  367. struct proc_dir_entry *p;
  368. if (video_dev_proc_entry == NULL)
  369. return;
  370. d = kmalloc (sizeof (struct videodev_proc_data), GFP_KERNEL);
  371. if (!d)
  372. return;
  373. p = create_proc_entry(name, S_IFREG|S_IRUGO|S_IWUSR, video_dev_proc_entry);
  374. if (!p)
  375. return;
  376. p->data = vfd;
  377. p->read_proc = videodev_proc_read;
  378. d->proc_entry = p;
  379. d->vdev = vfd;
  380. strcpy (d->name, name);
  381. /* How can I get capability information ? */
  382. list_add (&d->proc_list, &videodev_proc_list);
  383. }
  384. static void videodev_proc_destroy_dev (struct video_device *vfd)
  385. {
  386. struct list_head *tmp;
  387. struct videodev_proc_data *d;
  388. list_for_each (tmp, &videodev_proc_list) {
  389. d = list_entry(tmp, struct videodev_proc_data, proc_list);
  390. if (vfd == d->vdev) {
  391. remove_proc_entry(d->name, video_dev_proc_entry);
  392. list_del (&d->proc_list);
  393. kfree(d);
  394. break;
  395. }
  396. }
  397. }
  398. #endif /* CONFIG_VIDEO_PROC_FS */
  399. extern struct file_operations video_fops;
  400. /**
  401.  * video_register_device - register video4linux devices
  402.  * @vfd:  video device structure we want to register
  403.  * @type: type of device to register
  404.  * @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
  405.  *             -1 == first free)
  406.  *
  407.  * The registration code assigns minor numbers based on the type
  408.  * requested. -ENFILE is returned in all the device slots for this
  409.  * category are full. If not then the minor field is set and the
  410.  * driver initialize function is called (if non %NULL).
  411.  *
  412.  * Zero is returned on success.
  413.  *
  414.  * Valid types are
  415.  *
  416.  * %VFL_TYPE_GRABBER - A frame grabber
  417.  *
  418.  * %VFL_TYPE_VTX - A teletext device
  419.  *
  420.  * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  421.  *
  422.  * %VFL_TYPE_RADIO - A radio card
  423.  */
  424. int video_register_device(struct video_device *vfd, int type, int nr)
  425. {
  426. int i=0;
  427. int base;
  428. int err;
  429. int end;
  430. char *name_base;
  431. char name[16];
  432. switch(type)
  433. {
  434. case VFL_TYPE_GRABBER:
  435. base=0;
  436. end=64;
  437. name_base = "video";
  438. break;
  439. case VFL_TYPE_VTX:
  440. base=192;
  441. end=224;
  442. name_base = "vtx";
  443. break;
  444. case VFL_TYPE_VBI:
  445. base=224;
  446. end=240;
  447. name_base = "vbi";
  448. break;
  449. case VFL_TYPE_RADIO:
  450. base=64;
  451. end=128;
  452. name_base = "radio";
  453. break;
  454. default:
  455. return -1;
  456. }
  457. /* pick a minor number */
  458. down(&videodev_lock);
  459. if (-1 == nr) {
  460. /* use first free */
  461. for(i=base;i<end;i++)
  462. if (NULL == video_device[i])
  463. break;
  464. if (i == end) {
  465. up(&videodev_lock);
  466. return -ENFILE;
  467. }
  468. } else {
  469. /* use the one the driver asked for */
  470. i = base+nr;
  471. if (NULL != video_device[i]) {
  472. up(&videodev_lock);
  473. return -ENFILE;
  474. }
  475. }
  476. video_device[i]=vfd;
  477. vfd->minor=i;
  478. up(&videodev_lock);
  479. /* The init call may sleep so we book the slot out
  480.    then call */
  481. MOD_INC_USE_COUNT;
  482. if(vfd->initialize) {
  483. err=vfd->initialize(vfd);
  484. if(err<0) {
  485. video_device[i]=NULL;
  486. MOD_DEC_USE_COUNT;
  487. return err;
  488. }
  489. }
  490. sprintf (name, "v4l/%s%d", name_base, i - base);
  491. /*
  492.  * Start the device root only. Anything else
  493.  * has serious privacy issues.
  494.  */
  495. vfd->devfs_handle =
  496. devfs_register (NULL, name, DEVFS_FL_DEFAULT,
  497. VIDEO_MAJOR, vfd->minor,
  498. S_IFCHR | S_IRUSR | S_IWUSR,
  499. &video_fops,
  500. NULL);
  501. init_MUTEX(&vfd->lock);
  502. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  503. sprintf (name, "%s%d", name_base, i - base);
  504. videodev_proc_create_dev (vfd, name);
  505. #endif
  506. return 0;
  507. }
  508. /**
  509.  * video_unregister_device - unregister a video4linux device
  510.  * @vfd: the device to unregister
  511.  *
  512.  * This unregisters the passed device and deassigns the minor
  513.  * number. Future open calls will be met with errors.
  514.  */
  515.  
  516. void video_unregister_device(struct video_device *vfd)
  517. {
  518. down(&videodev_lock);
  519. if(video_device[vfd->minor]!=vfd)
  520. panic("videodev: bad unregister");
  521. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  522. videodev_proc_destroy_dev (vfd);
  523. #endif
  524. devfs_unregister (vfd->devfs_handle);
  525. video_device[vfd->minor]=NULL;
  526. MOD_DEC_USE_COUNT;
  527. up(&videodev_lock);
  528. }
  529. static struct file_operations video_fops=
  530. {
  531. owner: THIS_MODULE,
  532. llseek: no_llseek,
  533. read: video_read,
  534. write: video_write,
  535. ioctl: video_ioctl,
  536. mmap: video_mmap,
  537. open: video_open,
  538. release: video_release,
  539. poll: video_poll,
  540. };
  541. /*
  542.  * Initialise video for linux
  543.  */
  544.  
  545. static int __init videodev_init(void)
  546. {
  547. printk(KERN_INFO "Linux video capture interface: v1.00n");
  548. if(devfs_register_chrdev(VIDEO_MAJOR,"video_capture", &video_fops))
  549. {
  550. printk("video_dev: unable to get major %dn", VIDEO_MAJOR);
  551. return -EIO;
  552. }
  553. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  554. videodev_proc_create ();
  555. #endif
  556. return 0;
  557. }
  558. static void __exit videodev_exit(void)
  559. {
  560. #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
  561. videodev_proc_destroy ();
  562. #endif
  563. devfs_unregister_chrdev(VIDEO_MAJOR, "video_capture");
  564. }
  565. module_init(videodev_init)
  566. module_exit(videodev_exit)
  567. EXPORT_SYMBOL(video_register_device);
  568. EXPORT_SYMBOL(video_unregister_device);
  569. EXPORT_SYMBOL(video_devdata);
  570. EXPORT_SYMBOL(video_usercopy);
  571. EXPORT_SYMBOL(video_exclusive_open);
  572. EXPORT_SYMBOL(video_exclusive_release);
  573. MODULE_AUTHOR("Alan Cox");
  574. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
  575. MODULE_LICENSE("GPL");
  576. /*
  577.  * Local variables:
  578.  * c-basic-offset: 8
  579.  * End:
  580.  */