/*=========================================================================
工程名称: 1_udev_driver
组成文件: mydriver.c
功能描述: udev模块框架的编写
硬件连接: 无
维护记录: 2010-08-23 v1.1 add by dxh
维护记录: 2011-10-13 v1.2 add by zhangda
=========================================================================*/
#include <linux/module.h> /*module_init()*/
#include <linux/kernel.h> /* printk() */
#include <linux/init.h> /* __init __exit */
#include <linux/fs.h> /* file_operation */
#include <asm/uaccess.h> /* copy_to_user, copy_from_user */
#include <linux/device.h> /*class ,class_create ,device_create 等*/
#include "mydriver.h"
#define DEBUG //open debug message
#ifdef DEBUG
#define PRINTK(fmt, arg...) printk(KERN_WARNING fmt, ##arg)
#else
#define PRINTK(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
#endif
/*
KERN_EMERG 用于紧急事件,一般是系统崩溃前的提示信息
KERN_ALERT 用于需要立即采取动作的场合
KERN_CRIT 临界状态,通常设计验证的硬件或软件操作失败
KERN_ERR 用于报告错误状态.设备驱动程序通常会用它报告来自硬件的问题
KERN_WARNING 就可能出现的问题提出警告.这些问题通常不会对系统造成严重破坏
KERN_NOTICE 有必要提示的正常情况.许多安全相关的情况用这个级别汇报
KERN_INFO 提示性信息.有很多驱动程序在启动时用这个级别打印相关信息
KERN_DEBUG 用于调试的信息
*/
#define DRIVER_NAME "myDriver"
static int MAJOR_NR = 0; /* Driver Major Number */
static int MINOR_NR = 0; /* Driver Major Number */
unsigned char raver[30];
unsigned char saver[30]="form driver ";
struct class *my_class;
/* Driver Operation Functions */
static int myDriver_open(struct inode *inode, struct file *filp)
{
int Minor = MINOR(inode->i_rdev);//获得设备的从设备号,创建设备节点时设置
PRINTK("myDriver open called!, the MINOR is %d,\n",Minor);
return 0;
}
static int myDriver_release(struct inode *inode, struct file *filp)
{
int Minor = MINOR(inode->i_rdev);
PRINTK("myDriver release called!, the MINOR is %d,\n",Minor);
return 0;
}
static ssize_t myDriver_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int ret;
ret = copy_to_user(buf,saver, count);
if(ret != 0)
PRINTK("copy_to_user error\n");
return count;
}
static ssize_t myDriver_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
int ret;
ret = copy_from_user(raver, buf, count);
if(ret != 0)
PRINTK("copy_from_user error\n");
PRINTK("\n%s\n",raver);
return count;
}
static int myDriver_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case MYDRV_IOCTL0:
PRINTK("IOCTRL 0 called(arg=0x%lx)\n", arg);
break;
case MYDRV_IOCTL1:
PRINTK("IOCTRL 1 called(0x%lx)\n", arg);
break;
case MYDRV_IOCTL2:
PRINTK("IOCTRL 2 called(0x%lx)\n", arg);
break;
case MYDRV_IOCTL3:
PRINTK("IOCTRL 3 called(0x%lx)\n", arg);
break;
}
return 0;
}
/* Driver Operation structure */
static struct file_operations myDriver_fops = {
.owner = THIS_MODULE,
.write = myDriver_write,
.read = myDriver_read,
.ioctl = myDriver_ioctl,
.open = myDriver_open,
.release = myDriver_release,
};
static int __init myModule_init(void)
{
/* Module init code */
PRINTK("myModule_init\n");
/* Driver register */
MAJOR_NR = register_chrdev(MAJOR_NR, DRIVER_NAME, &myDriver_fops);
if(MAJOR_NR < 0)
{
PRINTK("register char device fail!\n");
return MAJOR_NR;
}
my_class=class_create(THIS_MODULE,"udev_test");
//class_device_create(my_class,NULL, MKDEV(MAJOR_NR, MINOR_NR), NULL,DRIVER_NAME);//2.6.15之前
device_create(my_class,NULL, MKDEV(MAJOR_NR, MINOR_NR), NULL,DRIVER_NAME);
PRINTK("register myDriver OK! Major = %d\n", MAJOR_NR);
return 0;
}
static void __exit myModule_exit(void)
{
/* Module exit code */
PRINTK("exit in\n");
/* Driver unregister */
if(MAJOR_NR > 0)
{
unregister_chrdev(MAJOR_NR, DRIVER_NAME);
device_destroy(my_class,MKDEV(MAJOR_NR, MINOR_NR));
class_destroy(my_class);
PRINTK("myModule_exit ok\n");
}
return;
}
module_init(myModule_init);
module_exit(myModule_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangda");