?? usirq.c
字號:
/* * usirq * use interrupts from userspace. * * (c) copyright 1999 Eric Lammerts <eric@scintilla.utwente.nl> * $Id: usirq.c,v 1.4 1999/08/15 10:54:12 eric Exp $ * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * It is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#define __KERNEL__#define MODULE#include <linux/version.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/interrupt.h>#include <linux/fcntl.h>#include <linux/errno.h>#include <asm/irq.h>MODULE_AUTHOR("Eric Lammerts <eric@scintilla.utwente.nl");MODULE_DESCRIPTION("Userspace interrupt driver");#define MAX_IRQS 16MODULE_PARM(debug,"i");static int debug;static struct { int open:1; int enabled:1; struct wait_queue *waitq;} usirq[MAX_IRQS];static void usirq_interrupt(int irq, void *dev_id, struct pt_regs *regs){ //disable_irq_nosync(irq); disable_irq(irq); if(debug) printk("usirq_interrupt: disable_irq_nosync(%d)\n", irq); usirq[irq].enabled = 0; if(usirq[irq].waitq) wake_up_interruptible(&usirq[irq].waitq);}static int usirq_open(struct inode * inode, struct file * file){ unsigned int retval; int minor = MINOR(inode->i_rdev); if (usirq[minor].open) return -EBUSY; usirq[minor].enabled = 1; retval = request_irq(minor, usirq_interrupt, 0, "usirq", NULL); if (retval < 0) return retval; cli(); if(usirq[minor].enabled) disable_irq(minor); sti(); usirq[minor].open = 1; MOD_INC_USE_COUNT; return 0;}static int usirq_close(struct inode * inode, struct file * file){ int minor = MINOR(inode->i_rdev); usirq[minor].open = 0; free_irq(minor, NULL); MOD_DEC_USE_COUNT; return 0;}static ssize_t usirq_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos){ int minor = MINOR(file->f_dentry->d_inode->i_rdev); usirq[minor].enabled = 1; if(debug) printk("usirq_read: enable_irq(%d)\n", minor); enable_irq(minor); if(!usirq[minor].enabled) /* apparently the irq happened right away */ return 0; if(!(file->f_flags & O_NONBLOCK)) interruptible_sleep_on(&usirq[minor].waitq); cli(); if(usirq[minor].enabled) { disable_irq(minor); if(debug) printk("usirq_read: disable_irq(%d)\n", minor); } sti(); if(!(file->f_flags & O_NONBLOCK)) { current->state = TASK_RUNNING; usirq[minor].waitq = NULL; if(signal_pending(current)) return -ERESTARTSYS; } if(!usirq[minor].enabled) /* interrupt occurred */ return 0; if(file->f_flags & O_NONBLOCK) return -EAGAIN; printk(KERN_ERR "usirq_read: no signal and no irq?!?\n"); return 0;}static struct file_operations usirq_fops = { NULL, /* lseek */ usirq_read, NULL, /* write */ NULL, /* readdir */ NULL, /* poll */ NULL, /* ioctl */ NULL, /* mmap */ usirq_open, NULL, /* flush */ usirq_close};int init_module(void){ if (register_chrdev(USIRQ_MAJOR, "usirq", &usirq_fops) < 0) { printk(KERN_ERR "usirq: register_chrdev failed.\n"); return -EIO; } return 0;}void cleanup_module(void){ unregister_chrdev(USIRQ_MAJOR, "usirq");}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -