- UID
- 1029342
- 性别
- 男
|
highValue <<= 4;
highValue |= ((lowValue&0xf0)>>4) ;
/*拷贝内核数据到用户空间*/
copy_to_user(buffer, &highValue, sizeof(highValue));
return 0;
}
/*写命令,在此置空*/
static int ds18b20_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
return 0;
} static int ds18b20_release(struct inode *inode,struct file *filp)
{
printk (KERN_INFO "device closed\n");
return 0;
}
static int ds18b20_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
return 0;
}
struct file_operations ds18b20_fops ={
.owner = THIS_MODULE,
.open = ds18b20_open,
.read = ds18b20_read,
.write = ds18b20_write,
.ioctl = ds18b20_ioctl,
.release = ds18b20_release,
};
static void ds18b20_setup_cdev(void)
{
int error,devno = MKDEV(ds18b20_major,0);
cdev_init(&cdev,&ds18b20_fops);
cdev.owner = THIS_MODULE;
cdev.ops = &ds18b20_fops;
error = cdev_add(&cdev,devno,1);
if( error )
printk(KERN_INFO"Error %d adding ds18b20 %d\n",error,0);
my_class = class_create(THIS_MODULE,"my_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return;
}
device_create(my_class,NULL,devno,NULL,"ds18b20");
}
/*注册设备*/
static int ds18b20_init(void)
{
int result;
dev = MKDEV(ds18b20_major,0);
if(ds18b20_major)
result = register_chrdev_region(dev,1,"ds18b20");
else{
result = alloc_chrdev_region(&dev,0,1,"ds18b20");
ds18b20_major=MAJOR(dev);
}
if( result < 0 ){
printk(KERN_WARNING"ds18b20:unable to get major %d\n",ds18b20_major);
return result;
}
if(ds18b20_major == 0 )
ds18b20_major = result;
ds18b20_setup_cdev();
printk("ds18b20 initialized.\n");
return 0;
}
static void __exit ds18b20_exit(void)
{
dev_t devno = MKDEV (ds18b20_major, 0);
device_destroy(my_class,devno);
class_destroy(my_class);
cdev_del (&cdev);
unregister_chrdev_region (devno, number_of_devices);
printk("ds18b20_major=%d\n",ds18b20_major);
printk("ds18b20 device uninstalled\n");
}
module_init(ds18b20_init);
module_exit(ds18b20_exit); |
|