1. mycdev.c

 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/cdev.h>
 #include <linux/fs.h>

 #define MAJOR_NUM 250

 struct mycdev
 {
     struct cdev cdev;
 };

 int mycdev_open(struct inode *inode, struct file *file)
 {
     printk("Call mycdev_open()\n");
     ;
 }

 struct file_operations fops = {
     .owner = THIS_MODULE,
     .open = mycdev_open,
 };

 struct mycdev mycdev;

 int mycdev_init(void)
 {
     ;
     dev_t dev;

     dev = MKDEV(MAJOR_NUM, );
     ret = register_chrdev_region(dev, , "mycdev");
     )
     {
         printk("register_chrdev_region() error\n");
         goto err_register_chrdev_region;
     }

     cdev_init(&(mycdev.cdev), &fops);

     ret = cdev_add(&(mycdev.cdev), dev, );
     )
     {
         printk("cdev_add() error\n");
         goto err_cdev_add;
     }

     printk("mycdev_init()\n");
     ;

 err_cdev_add:
     unregister_chrdev_region(dev, );

 err_register_chrdev_region:
     return ret;
 }

 void mycdev_exit(void)
 {
     dev_t dev = mycdev.cdev.dev;

     cdev_del(&(mycdev.cdev));
     unregister_chrdev_region(dev, );

     printk("mycdev_exit()\n");
     return;
 }

 module_init(mycdev_init);
 module_exit(mycdev_exit);

 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("GNB");

2. Makefile

 KERNEL_DIR = /lib/modules/$(shell uname -r)/build
 PWD = $(shell pwd)

 ifneq ($(KERNELRELEASE),)

 obj-m := mycdev.o

 else

 .PHONY:default clean

 default:
     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules

 clean:
     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean

 endif

3. 应用层app

 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>

 int main(int argc, const char *argv[])
 {
     int fd;

     fd = open("/dev/mycdev",O_RDONLY);
     printf("fd = %d\n",fd);
     );

     ;
 }

4. 加载驱动

sudo insmod mycdev.ko

5. 创建设备节点,并指定权限

sudo mknod /dev/mycdev c 250 0

sudo chmod 666 /dev/mycdev

6. 测试字符设备

cat /dev/mycdev

echo "hello world" > /dev/mycdev

05 Linux字符驱动---静态注册的更多相关文章

  1. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl 0. 导语 在嵌入式的道路上寻寻觅觅很久,进入嵌入式这个行业也有几年的时间了,从2011年后 ...

  2. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read.write 0. 导语 在上一篇博客里面,基于OMAPL138的字符驱动_GPIO驱动AD9833(一)之 ...

  3. linux 字符驱动框架(用户态的read,write,poll是怎么操作驱动的)

    前言 这篇文章是通过对一个简单字符设备驱动的操作来解释,用户态的读写操作是怎么映射到具体设备的. 因为针对不同版本的linux内核,驱动的接口函数一直有变化,这贴出我测试的系统信息: root@ubu ...

  4. linux 字符驱动

    1 结构体说明:     struct cdev {         struct kobject kobj;          // 每一个 cdev 都是一个 kobject         st ...

  5. linux字符驱动之poll机制按键驱动

    在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动.虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情 ...

  6. [S5PV210 Linux字符驱动之PWM蜂鸣器驱动

    在SMDK210.C中添加如下beeper_device 结构体 static struct platform_device beeper_device = { .name = "pwm_b ...

  7. 基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ

    基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ 0. 导语 学习进入到了下一个阶段,还是以AD9833为例,这次学习是向设备申请中断,实现触发,在未来很多场景,比如做用 ...

  8. 旧接口注册LED字符驱动设备(静态映射)

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...

  9. 驱动开发学习笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇

    驱动开发读书笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇 下面这段摘自 linux源码里面的文档 : 内核版本2.6.22Doc ...

随机推荐

  1. Myeclipse或Eclipse中搭建Easyui环境

    1.下载Easyui.网址:http://www.jeasyui.com/download/index.php 2.下载后解压,里面的demo文件夹可以不用添加到工程中. 3.如图所示在工程datag ...

  2. UITextField和UIViewConteoller

    UITextField控件 UITextFiled常用属性和方法   UITextField是常用的文本输入控件,比如我们用的QQ的登录界面,词典输入要查询的单词都使用了文本框控件,如下图所示.之前介 ...

  3. [算法] dijkstra单源无负权最小路径算法

    #include <stdio.h>#include <stdlib.h>#include <string.h> #define INF 1000000#defin ...

  4. Linux的iptables常用配置范例(2)

    iptables -F   #清除所有规则 iptables -X  #清除所有自定义规则 iptables -Z   #各项计数归零 iptables -P INPUT DROP  #将input链 ...

  5. BigDecimal 高精度计算 熟悉扩展,java除法保留小数问题

    java保留两位小数问题: 方式一: 四舍五入  double   f   =   111231.5585;  BigDecimal   b   =   new   BigDecimal(f);  d ...

  6. angular change the url , prevent reloading

    http://stackoverflow.com/questions/14974271/can-you-change-a-path-without-reloading-the-controller-i ...

  7. 配置 Gitblit 进行 Git 代码管理

    配置 Gitblit 进行 Git 代码管理 环境 CentOS 7 x64 IP: 10.6.0.2 首先需要安装jdk  安装步骤 就略过了 下载最新版本  gitblit wget http:/ ...

  8. 浅谈web应用的负载均衡、集群、高可用(HA)解决方案(转)

    1.熟悉几个组件 1.1.apache     —— 它是Apache软件基金会的一个开放源代码的跨平台的网页服务器,属于老牌的web服务器了,支持基于Ip或者域名的虚拟主机,支持代理服务器,支持安 ...

  9. BufferedReader 输出与BufferedWriter 输入的用法

    public static void main(String[] args) {  String content[] = {"好久不见了","最近好吗",&qu ...

  10. CodeForces 625B War of the Corporations

    暴力匹配+一点判断 #include <stdio.h> #include <algorithm> #include <string.h> #include < ...