宋宝华 《Linux设备驱动开发详解》示例代码之second设备
来源:互联网 发布:高一优化方案英语答案 编辑:程序博客网 时间:2024/06/06 11:02
second.c代码
#include <linux/module.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/cdev.h>#include <linux/init.h>#include <linux/timer.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#defineSECOND_MAJOR252staticintsecond_major = SECOND_MAJOR;structsecond_dev{structcdevcdev;atomic_tcounter;structtimer_lists_timer;};structsecond_dev*second_devp;staticvoidsecond_timer_handle(unsigned long arg){mod_timer(&second_devp->s_timer, jiffies + HZ);atomic_inc(&second_devp->counter);printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);}intsecond_open(struct inode *inode, struct file *filp){init_timer(&second_devp->s_timer);second_devp->s_timer.function = &second_timer_handle;second_devp->s_timer.expires = jiffies + HZ;add_timer(&second_devp->s_timer);atomic_set(&second_devp->counter, 0);return 0;}intsecond_release(struct inode *inode, struct file *filp){del_timer(&second_devp->s_timer);return 0;}staticssize_tsecond_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos){int counter;counter = atomic_read(&second_devp->counter);if(put_user(counter, (int *)buf))return -EFAULT;elsereturn sizeof(unsigned int);return 0;}staticconststructfile_operations second_fops = {.owner=THIS_MODULE,.open=second_open,.release=second_release,.read=second_read,};static voidsecond_setup_cdev(struct second_dev *dev,int index ){int err;int devno = MKDEV(second_major, index);cdev_init(&dev->cdev, &second_fops);dev->cdev.owner = THIS_MODULE;dev->cdev.ops = &second_fops;err=cdev_add(&dev->cdev, devno, 1);if(err)printk(KERN_ALERT "Error setup the second device");}intsecond_init(void){intret;dev_tdevno = MKDEV(second_major, 0);if(second_major)ret = register_chrdev_region(devno, 1, "second");else{ret = alloc_chrdev_region(&devno, 0, 1, "second" );second_major = MAJOR(devno);}if( ret < 0)return ret;second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);if(!second_devp){ret = -ENOMEM;goto fail_malloc;}memset(second_devp, 0 ,sizeof(struct second_dev));second_setup_cdev(second_devp, 0);return 0;fail_malloc:unregister_chrdev_region(devno, 1);return ret;}voidsecond_exit(void){cdev_del(&second_devp->cdev);kfree(second_devp);unregister_chrdev_region(MKDEV(second_major, 0), 1);}MODULE_AUTHOR("BG2BKK");MODULE_LICENSE("Dual BSD/GPL");module_init(second_init);module_exit(second_exit);
makefile
KERNEL_DIR := /lib/modules/$(shell uname -r)/buildPWD:= $(shell pwd)obj-m := second.odefault:$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modulestest: test.cgcc $< -o $@.o -gclean:rm -rf *.o *.ko *~ *.order *.symvers *.markers *.mod.c
测试代码
/*====================================================================== A test program to access /dev/second This example is to help understand kernel timer The initial developer of the original code is Baohua Song <author@linuxdriver.cn>. All Rights Reserved.======================================================================*/#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <sys/time.h>main(){ int fd; int counter = 0; int old_counter = 0; /*打开/dev/second设备文件*/ fd = open("/dev/second", O_RDONLY); if (fd != - 1) { while (1) { read(fd,&counter, sizeof(unsigned int));//读目前经历的秒数 if(counter!=old_counter) { printf("seconds after open /dev/second :%d\n",counter); old_counter = counter; } } } else { printf("Device open failure\n"); }}
测试脚本
1 load_scull.sh
#!/bin/sh/sbin/insmod second.komknod /dev/second c 252 0
2 unload_scull.sh
#!/bin/sh/sbin/rmmod second.korm /dev/second -f
3 test.sh
#!/bin/shmake cleanmake make testsudo ./unload_scullsudo ./load_scullsudo ./test.o
- 宋宝华 《Linux设备驱动开发详解》示例代码之second设备
- 宋宝华 《Linux设备驱动开发详解》示例代码之基本字符设备驱动
- 宋宝华 《Linux设备驱动开发详解》示例代码之fifo字符设备驱动
- 《Linux设备驱动开发详解》源码——second
- Linux设备驱动开发示例
- 宋宝华 《Linux设备驱动开发详解》之基本字符设备驱动misc版本
- Linux设备驱动开发详解
- LINUX设备驱动开发详解
- linux设备驱动开发详解
- 《linux设备驱动开发详解》中globalmem代码笔记
- Linux设备模型之input子系统详解(second)
- Linux设备驱动开发详解--笔记6--字符设备驱动
- Linux设备驱动开发详解总结(一)之字符设备驱动结构
- Linux 设备驱动开发详解之20章usb主机与设备驱动
- 《linux设备驱动开发详解》学习之globalmem
- Linux设备驱动之usb设备驱动详解
- Linux设备驱动之usb设备驱动详解
- linux设备驱动之PCIE驱动开发
- 弹出对话框 用户输入值(FM)
- 空间信息行业忧思录
- android Fragments详解二:创建Fragment
- iPhone开发过程中检测多次Release问题
- android Fragments详解三:实现Fragment的界面
- 宋宝华 《Linux设备驱动开发详解》示例代码之second设备
- 数组的初始化
- 外观模式--牛市股票还会亏钱
- android Fragments详解四:管理fragment
- IO端口和IO内存
- 2013年互联网江湖格局观
- iOS中使用block传值
- 宽带连不上网原因及解决办法
- 《商务智能 管理视角》——(三)数据仓库(2)