您的位置:首页 > 运维架构 > Linux

Linux miscdevice 混杂设备

2017-05-18 17:18 417 查看
miscdevice混杂设备

:主设备号通常是10。

需要引用的头文件

:#include <linux/miscdevice.h>

设备注册成功后会自动生成设备节点

:/dev/xxx 

<linux/miscdevice.h>头文件内容:

#ifndef _LINUX_MISCDEVICE_H

#define _LINUX_MISCDEVICE_H

#include <linux/major.h>

#include <linux/list.h>

#include <linux/types.h>

/*

 * These allocations are managed by device@lanana.org. If you use an

 * entry that is not in assigned your entry may well be moved and

 * reassigned, or set dynamic if a fixed value is not justified.

 */

#define PSMOUSE_MINOR 1

#define MS_BUSMOUSE_MINOR 2

#define ATIXL_BUSMOUSE_MINOR 3

/*#define AMIGAMOUSE_MINOR 4
FIXME OBSOLETE */

#define ATARIMOUSE_MINOR 5

#define SUN_MOUSE_MINOR 6

#define APOLLO_MOUSE_MINOR 7

#define PC110PAD_MINOR 9

/*#define ADB_MOUSE_MINOR 10
FIXME OBSOLETE */

#define WATCHDOG_MINOR 130
/* Watchdog timer     */

#define TEMP_MINOR 131
/* Temperature Sensor */

#define RTC_MINOR 135

#define EFI_RTC_MINOR 136
/* EFI Time services */

#define SUN_OPENPROM_MINOR 139

#define DMAPI_MINOR 140
/* DMAPI */

#define NVRAM_MINOR 144

#define SGI_MMTIMER 153

#define STORE_QUEUE_MINOR 155

#define I2O_MINOR 166

#define MICROCODE_MINOR 184

#define VFIO_MINOR 196

#define TUN_MINOR 200

#define CUSE_MINOR 203

#define MWAVE_MINOR 219
/* ACP/Mwave Modem */

#define MPT_MINOR 220

#define MPT2SAS_MINOR 221

#define MPT3SAS_MINOR 222

#define UINPUT_MINOR 223

#define MISC_MCELOG_MINOR 227

#define HPET_MINOR 228

#define FUSE_MINOR 229

#define KVM_MINOR 232

#define BTRFS_MINOR 234

#define AUTOFS_MINOR 235

#define MAPPER_CTRL_MINOR 236

#define LOOP_CTRL_MINOR 237

#define VHOST_NET_MINOR 238

#define UHID_MINOR 239

#define MISC_DYNAMIC_MINOR 255   //表示动态成生MINOR 次设备号,以上都是指定的次设备号。

struct device;

struct miscdevice  {
int minor;
//次设备号一般用MISC_DYNAMIC_MINOR。
const char *name;   //设备名
const struct file_operations *fops; //操作设备的函数。
struct list_head list;  //连接到下一个miscdevice设备的链表。
struct device *parent;  //指向父设备。
struct device *this_device; //指向当前设备结构体。
const char *nodename; //节点名
umode_t mode;

};

extern int misc_register(struct miscdevice * misc);  //注册混杂设备函数。

extern int misc_deregister(struct miscdevice *misc); //注销混杂设备函数。

#define MODULE_ALIAS_MISCDEV(minor) \
MODULE_ALIAS("char-major-" __stringify(MISC_MAJOR)
\
"-" __stringify(minor))

#endif

miscdevice设备:使用模板

==================================================================================================

static int xxx_open(struct inode *inode, struct file *file)

{
int ret;
ret = nonseekable_open(inode, file); //不允许调用seek
if(ret < 0)
return ret;
file->private_data = xxxx_data_ptr; //如果设置有私有数据指针,可以赋值到file->private_data 
return 0;

}

static int xxx_release(struct inode *inode, struct file *file)

{
return 0;

}

#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,36))

static long xxx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

#else

static int xxx_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)

#endif

{
void __user *argp = (void __user *)arg;
int retval = 0;
char state = 0, restore_state = 0;
char rwbuf[8] = "";
uint32_t delay_ns;
char char3_buffer[3];
int result;
int int3_buffer[3];
struct stk831x_data *stk = file->private_data;

switch (cmd) 
{
case STK_IOCTL_SET_OFFSET:
if(copy_from_user(&char3_buffer, argp, sizeof(char3_buffer)))
return -EFAULT;
break;
case STK_IOCTL_SET_DELAY:
if(copy_from_user(&delay_ns, argp, sizeof(uint32_t)))
return -EFAULT;
break;
case STK_IOCTL_WRITE:
case STK_IOCTL_READ:
if (copy_from_user(&rwbuf, argp, sizeof(rwbuf)))
return -EFAULT;
break;



switch (cmd) 
{
case STK_IOCTL_GET_ACCELERATION:
if(copy_to_user(argp, &int3_buffer, sizeof(int3_buffer)))
return -EFAULT;
break;

case STK_IOCTL_READ:
if(copy_to_user(argp, &rwbuf, sizeof(rwbuf)))
return -EFAULT;
break;

case STK_IOCTL_GET_DELAY:
if(copy_to_user(argp, &delay_ns, sizeof(delay_ns)))
return -EFAULT;
break;

case STK_IOCTL_GET_OFFSET:
if(copy_to_user(argp, &char3_buffer, sizeof(char3_buffer)))
return -EFAULT;
break;

case STK_IOCTL_GET_RANGE:
case STK_IOCTL_GET_ENABLE:
if(copy_to_user(argp, &state, sizeof(char)))
return -EFAULT;
break;
default:
break;
}



static struct file_operations xxx_fops = {
.owner = THIS_MODULE,
.open = xxx_open,
.release = xxx_release,

#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,36)) //Linux内核版本 兼容处理
.unlocked_ioctl = xxx_ioctl,

#else
.ioctl = xxx_ioctl,

#endif

};

static struct miscdevice stk_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "xxxdev",
.fops = &xxx_fops,

};

//init
error = misc_register(&stk_device);
if (error) 
{
printk(KERN_ERR "%s: misc_register failed\n", __func__);
}

//exit
misc_deregister(&stk_device);

===================================================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: