1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include <linux/init.h> #include <linux/platform_device.h> #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/ioport.h> #include <linux/of.h> #include <linux/fs.h> #include <linux/delay.h> #include <asm/io.h> #include <linux/time.h> #include <asm/uaccess.h>
#define DEVICE_NAME "ccd_pir"
static void __iomem *gpio_add_minor_pir;
#define GPIO_BASE_PIR 0x41210000
unsigned int GPIO_TRI_Offset = 0x4; unsigned int GPIO_DATA_Offset = 0x0;
static volatile long pir[1] = {0}; void PIR(void); int label_pir, state_pir;
static int pir_open(struct inode *inode,struct file *filp) { gpio_add_minor_pir = (unsigned int)ioremap(GPIO_BASE_PIR, 32); return 0; }
static ssize_t pir_write(struct file *file_p, const char __user *buf, size_t len, loff_t *loff_t_p) { return 0; }
static int pir_release(struct inode *inode_p, struct file *file_p) { iounmap((unsigned int *)gpio_add_minor_pir); return 0; }
static int pir_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
return 0; }
static int pir_read(struct file *filp, char __user *buff, size_t count, loff_t *offp) { pir[0] = 0; PIR(); pir[0] = state_pir;
printk(KERN_DEBUG"state_pir: %d\n", state_pir);
unsigned long err; err = copy_to_user(buff, (const void *)pir, min(sizeof(pir), count)); return err ? -EFAULT : min(sizeof(pir), count); }
struct file_operations pir_fops= { .owner = THIS_MODULE, .open = pir_open, .unlocked_ioctl = pir_ioctl, .read = pir_read, .write = pir_write, .release = pir_release, };
static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &pir_fops, };
static int __init pir_init(void) { int ret; ret = misc_register(&misc); printk (DEVICE_NAME"\tinitialized\n"); return ret; }
static void __exit pir_exit(void) { misc_deregister(&misc); }
void PIR(void) { state_pir = 0; if(ioread32(gpio_add_minor_pir)) { mdelay(2); if(ioread32(gpio_add_minor_pir)) state_pir = 1; } }
module_init(pir_init); module_exit(pir_exit);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("ccd");
|