首页 > 代码库 > 字符类设备

字符类设备

字符设备(顺序读写,不带缓冲)

块设备(读写顺序不固定,带读写缓冲)常用sync命令(强行写入硬件) 同步数据使数据真正写进磁盘不然可能还在缓冲区

网络设备:这家伙破坏了linux一切皆文件规则。

字符设备框架:

硬件上有个字符设备,内核中就有个cdev结构与之对应

struct cdev {

struct kobject kobj;

struct module *owner;

const struct file_operations *ops;

struct list_head list;

dev_t dev;//设备号

unsigned int count;

};

每个cdev都有个设备号

设备号(32bits)=主设备号(高12bits)+次设备号(20bits)

主设备号:代表一个类型的设备

次设备号:用于区分设备的不同个体

主设备号的选取

->静态分配

看现在内核中那些主设备号没有被使用,选择一个

查看 cat/proc/devoces已用的设备号

看内核源码目录下即Documentation\device.txt看看使用那些设备号

register_chedev_region(dev_t from,unsigned count,const char name)可以连续注册多个字符设备号

from要注册的起始设备号

count 连续注册的设备个数

name名字


dev_t  __u32  ->  unsigned int  16位和32位移植兼容性才这么设计的


#definrMINORBITS 20

MKDEV(ma(主设备号),mi(次设备号)) 他是个宏 其实源码也就做了ma<<MINORBITS|mi)

unregister_chrdev_region(dev,nsigned count)卸载字符设备

 


->动态分配  但不是随机分配

alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char name)

注释:

/**

 * alloc_chrdev_region() - register a range of char device numbers

 * @dev: output parameter for first assigned number

 * @baseminor: first of the requested range of minor numbers

 * @count: the number of minor numbers required

 * @name: the name of the associated device or driver

 *

 * Allocates a range of char device numbers.  The major number will be

 * chosen dynamically, and returned (along with the first minor number)

 * in @dev.  Returns zero or a negative error code.

 */


 获取主设备号:

 MAJOR(dev)   dev>>MINORBITS




cdev的操作函数

cdev_init()//初始化cdev

void cdev_init(struct cdev *cdev, const struct file_operations *fops)

{

memset(cdev, 0, sizeof *cdev);

INIT_LIST_HEAD(&cdev->list);

kobject_init(&cdev->kobj, &ktype_cdev_default);

cdev->ops = fops;

}


cdev_add()//向内核注册cdev


int cdev_add(struct cdev *p, dev_t dev, unsigned count)

{

p->dev = dev;

p->count = count;

return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);

}




struct file_operations {

struct module *owner;

loff_t (*llseek) (struct file *, loff_t, int);

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

int (*readdir) (struct file *, void *, filldir_t);

unsigned int (*poll) (struct file *, struct poll_table_struct *);

.....

};



_exit 要逆序消除 _init产生的影响

cdev_del()

unregister_chrdev_region(dev,nsigned count)

手工创建设备节点文件

mknod /dev/xxx c 248 0 

               主  次











arm-none-linux-gnueabi-gcc -o










简单小示例:

#include <linux/init.h>

#include <linux/module.h>

#include <linux/fs.h>

#include <linux/cdev.h>



MODULE_LICENSE("Dua BSD/GPL");

MODULE_AUTHOR("Songmao");

MODULE_DESCRIPTION("cdev tast");


#define TCDEV_MAJOR 0

#define TCDEV_MINOR 0

#define TCDEV_NUM 1

#define TCDEV_NAME "mytcdev"

int tcdev_major =TCDEV_MAJOR,tcdev_minor=TCDEV_MINOR;

module_param(tcdev_major,int,S_IRWXU);

module_param(tcdev_minor,int,S_IRWXU);


static int tcdev_open(struct inode * node,struct file * filep)

{

      printk(KERN_INFO "tcdev open\n");

 return 0;

}

static ssize_t tcdev_read(struct file *file, char __user *buf, size_t count, loff_t *fops)

{

      printk(KERN_INFO "tcdev read\n");

 return 0;

}

static ssize_t tcdev_write (struct file *file, char __user *buf, size_t count, loff_t *fops)

{

      printk(KERN_INFO "tcdev write\n");

 return 0;

}

static int tcdev_release(struct inode * node,struct file * filep)

{

      printk(KERN_INFO "tcdev write\n");

 return 0;

}


static struct file_operations tcdev_fops = {

    .owner=THIS_MODULE,

.open=tcdev_open,

.read=tcdev_read,

.write=tcdev_write,

.release=tcdev_release,

};

static struct cdev tcdev;




static int __init tcdev_init(void)

{

     int ret =-EFAULT;

dev_t tcdev_t;

tcdev_t=MKDEV(tcdev_major,tcdev_minor);

     printk(KERN_INFO "tcdev start ...\n");

if(TCDEV_MAJOR>0){

         ret= alloc_chrdev_region(&tcdev_t,tcdev_minor,TCDEV_NUM,TCDEV_NAME);

if(ret<0){

printk(KERN_ERR"tcdev cdev num apply is fail\n");

goto tcdev_cdev_fail;

             

}

}

else{

  ret=register_chrdev_region(tcdev_t,TCDEV_NUM,TCDEV_NAME);

  if(ret<0){

  printk(KERN_ERR "tcdev cdev num apply is fail\n");

  goto tcdev_cdev_fail;

  }

  

}


     printk(KERN_INFO "tcdev cdev num apply is success,dev_t is %d\n",tcdev_t);

memset(&tcdev,0,sizeof(struct cdev));

cdev_init(&tcdev,&tcdev_fops);

ret=cdev_add(&tcdev,tcdev_t,TCDEV_NUM);

if(ret<0){

         printk(KERN_ERR"tcdev cdev cdev_add is fail\n");

goto tcdev_cdev_add_fail;

}

printk(KERN_INFO"tcdev cdev_add apply is success\n");

tcdev_cdev_add_fail:

unregister_chrdev_region(&tcdev,TCDEV_NUM);

 

tcdev_cdev_fail:

return 0;

}


static void __exit tcdev_exit (void)

{

printk(KERN_INFO "tcdev stop ...\n");

cdev_del(&tcdev);

printk(KERN_INFO"tcdev del success\n");

 unregister_chrdev_region(&tcdev,TCDEV_NUM);

printk(KERN_INFO "tcdev dev_num del success\n");

return 0;

}

 

 


module_init(tcdev_init);

module_exit(tcdev_exit);


Makefile


#!/bin/bash


obj-m += testcdev.o



KDIR := /home/topeet/android4.0/iTop4412_Kernel_3.0


#当前目录变量

PWD ?= $(shell pwd)


#make命名默认寻找第一个目标

#make -C就是指调用执行的路径

#$(KDIR)Linux源码目录,作者这里指的是/home/topeet/android4.0/iTop4412_Kernel_3.0

#$(PWD)当前目录变量

#modules要执行的操作

all:

        make -C $(KDIR) M=$(PWD) modules

                

#make clean执行的操作是删除后缀为o的文件

clean:

        rm -rf *.mod.c *.o *.order *.ko *.mod.o *.symvers



#include <stdio.h>


#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/ioctl.h>

char buf[20];

/*argv[1] is cmd , argv[2] is io_arg*/

int main(int argc , char **argv){

        int fd;

        char *lednode = "/dev/tcdev";

        if((fd = open(lednode,O_RDWR|O_NDELAY))<0){

                printf("APP open %s failed!\n",lednode);

        }

        else{

                printf("APP open %s success!\n",lednode);

                read(fd,buf,20);

             return 0;          

        }


字符类设备