有时候,就是想窥视一下不知道的东东,因为好奇!
分类: 嵌入式
2011-06-11 14:57:04
#include #include #include #include #include #include MODULE_LICENSE("GPL"); MODULE_AUTHOR("zhanglong"); /* * 板子上的led1,2,4,8分别对应连在GPF4,5,6,7上 * *GPFCON ==> 0x56000050 *GPFDAT ==> 0x56000054 *GPFUP ==> 0x56000058 * */ #define IO_PHYS 0x56000000 #define GPFCON_OFFSET 0x50 #define GPFDAT_OFFSET 0x54 #define GPFUP_OFFSET 0x58 struct led_device_s { struct cdev dev; dev_t no; short *io_gpfcon; //注意数据类型, 前面因为错误声明为char型指针,造成结果与预期的不同,郁闷了很久. char *io_gpfdat; char *io_gpfup; }; struct led_device_s my_led[4]; ssize_t my_write(struct file *fp, const char __user *buf, size_t count, loff_t *off) { struct led_device_s *get = fp->private_data; unsigned int minor; minor = MINOR(get->no); *(get->io_gpfcon) &= ~(3 << ((minor * 2) + 8)); *(get->io_gpfcon) |= (1 << ((minor * 2) + 8)); *(get->io_gpfup) &= ~(1 << (minor + 4)); if((*buf == 0) || (*buf == 48)) { *(get->io_gpfdat) &= ~(1 << (minor + 4)); } else { *(get->io_gpfdat) |= (1 << (minor + 4)); } return 4; } ssize_t my_read(struct file *fp, char __user *buf, size_t count, loff_t *off) { return 4; //不能返回0, 否则读相关设备时会卡住. } int my_open(struct inode *no, struct file *fp) { fp->private_data = container_of(no->i_cdev, struct led_device_s, dev); //printk(" kernel: open.\n"); return 0; } int my_release(struct inode *no, struct file *fp) { //printk(" kernel: release.\n"); return 0; } struct file_operations my_ops = { .open = my_open, .release = my_release, .read = my_read, .write = my_write, }; int test_init(void) { int ret = 0; char *virt; int i; virt = ioremap(IO_PHYS, SZ_4K); for (i = 0; i < 4; i ++) { my_led[i].no = MKDEV(52, i); cdev_init(&my_led[i].dev, &my_ops); my_led[i].io_gpfcon = virt + GPFCON_OFFSET; my_led[i].io_gpfdat = virt + GPFDAT_OFFSET; my_led[i].io_gpfup = virt + GPFUP_OFFSET; } ret = register_chrdev_region(my_led[0].no, 4, "my dev"); if (ret) { printk(" register device number failed.\n"); return ret; } for (i = 0; i < 4; i ++) { cdev_add(&my_led[i].dev, my_led[i].no, 1); } return ret; } void test_exit(void) { int i ; for (i = 0; i < 4; i ++) { cdev_del(&my_led[i].dev); } unregister_chrdev_region(my_led[0].no, 4); } module_init(test_init); module_exit(test_exit); |
#KERNEL = /home/zl/my2440-2.6.13 |