几个简单的Linux驱动程序
Table of Contents
hello.c
hellop.c带参数输入
complete.c
hello.c
/* * $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $ */
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void)
{printk(KERN_ALERT "Hello, world\n");return 0;
}static void hello_exit(void)
{printk(KERN_ALERT "Goodbye, cruel world\n");
}module_init(hello_init);
module_exit(hello_exit);
hellop.c带参数输入
/* * $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $ */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>MODULE_LICENSE("Dual BSD/GPL");/* * These lines, although not shown in the book, * are needed to make hello.c run properly even when * your kernel has version support enabled */ /** A couple of parameters that can be passed in: how many times we say* hello, and to whom.*/
static char *whom = "world";
static int howmany = 1;
module_param(howmany, int, S_IRUGO);
module_param(whom, charp, S_IRUGO);static int hello_init(void)
{int i;for (i = 0; i < howmany; i++)printk(KERN_ALERT "(%d) Hello, %s\n", i, whom);return 0;
}static void hello_exit(void)
{printk(KERN_ALERT "Goodbye, cruel world\n");
}module_init(hello_init);
module_exit(hello_exit);
complete.c
/** complete.c -- the writers awake the readers** Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet* Copyright (C) 2003 O'Reilly & Associates** The source code in this file can be freely used, adapted,* and redistributed in source or binary form, so long as an* acknowledgment appears in derived source files. The citation* should list that the code comes from the book "Linux Device* Drivers" by Alessandro Rubini and Jonathan Corbet, published* by O'Reilly & Associates. No warranty is attached;* we cannot take responsibility for errors or fitness for use.** $Id: complete.c,v 1.2 2004/09/26 07:02:43 gregkh Exp $*/#include <linux/module.h>
#include <linux/init.h>#include <linux/sched.h> /* current and everything */
#include <linux/kernel.h> /* printk() */
#include <linux/fs.h> /* everything... */
#include <linux/types.h> /* size_t */
#include <linux/completion.h>MODULE_LICENSE("Dual BSD/GPL");static int complete_major = 0;DECLARE_COMPLETION(comp);ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{printk(KERN_DEBUG "process %i (%s) going to sleep\n",current->pid, current->comm);wait_for_completion(&comp);printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);return 0; /* EOF */
}ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,loff_t *pos)
{printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",current->pid, current->comm);complete(&comp);return count; /* succeed, to avoid retrial */
}struct file_operations complete_fops = {.owner = THIS_MODULE,.read = complete_read,.write = complete_write,
};int complete_init(void)
{int result;/** Register your major, and accept a dynamic number*/result = register_chrdev(complete_major, "complete", &complete_fops);if (result < 0)return result;if (complete_major == 0)complete_major = result; /* dynamic */return 0;
}void complete_cleanup(void)
{unregister_chrdev(complete_major, "complete");
}module_init(complete_init);
module_exit(complete_cleanup);
几个简单的Linux驱动程序相关推荐
- Linux驱动程序教程:如何编写简单的Linux设备驱动程序
翻译来自: https://www.apriorit.com/dev-blog/195-simple-driver-for-linux-os 代码下载 此Linux设备驱动程序教程将为您提供有关如何为 ...
- Linux驱动程序编写
工作需要写了我们公司一块网卡的Linux驱动程序.经历一个从无到有的过程,深感技术交流的重要.Linux作为挑战微 软垄断的强有力武器,日益受到大家的喜爱.真希望她能在中国迅速成长.把程序文档贴出来, ...
- Linux驱动程序学习步骤
了解linux驱动程序技巧学习的方法很重要,学习linux操作系统时,你可能会遇到关于驱动方面的问题, 这里将介绍学习linux驱动程序的方法,在这里拿出来和大家分享一下. 1.学会写简单的make ...
- linux卡片电脑源码,x4412开发板ibox卡片电脑项目实战9-搭建最简单的linux文件系统...
Linux文件系统不仅包含着文件中的数据而且还有文件系统的结构,所有Linux用户和程序看到的文件.目录.软连接及文件保护信息等都存储在其中.有了文件系统,用户就可以很方便的和Linux设备进行数据交 ...
- 基于块的linux驱动程序,基于块的Linux驱动程序 块设备驱动 centos内核编译过程 操作系统课程设计...
操作系统的课程设计,本人也是一头雾水地做完了课程设计,在这里贴下操作过程,放下当时参考的一篇CSDN文章链接:https://blog.csdn.net/cxy_chen/article/detail ...
- 第一个linux驱动程序
本章将进行实例的学习,第一个linux驱动程序:统计单词个数.本例子的目的不是讲解如何统计单词个数,而是该算法的实现技术:Linux驱动.Linux系统将每一个驱动都映射成一个文件,这些文件称为设备文 ...
- 基于DM6467的TVP7002 Linux驱动程序开发
在Linux中,使用V4L2框架管理所有的视频编解码设备.针对我们开发板的V4L2框架结构已经在之前的TVP5150驱动程序编写和OV5642驱动程序编写的说明文档中进行了详细的分析,所以这里不再对整 ...
- c 调用 linux驱动程序,Linux下的C编程实战(五)――驱动程序设计
Linux下的C编程实战(五) ――驱动程序设计 1.引言 设备驱动程序是操作系统内核和机器硬件之间的接口,它为应用程序屏蔽硬件的细节,一般来说,Linux的设备驱动程序需要完成如下功能: (1)初始 ...
- dma-buf 由浅入深(一) —— 最简单的 dma-buf 驱动程序
dma-buf 由浅入深(一) -- 最简单的 dma-buf 驱动程序 dma-buf 由浅入深(二) -- kmap / vmap dma-buf 由浅入深(三) -- map attachmen ...
最新文章
- JS深入--词法作用域、执行上下文与闭包
- (转载)jdbc事务处理
- Window server 补丁修复及补丁回滚
- ASP.NET中如何搭建三层架构
- CAS机制中的ABA问题
- 日期类的加减及java中所以日期类的操作算法大全
- g4e基础篇#1 为什么要使用版本控制系统
- 前端学习(2986):一文理解数据劫持4
- Git检出指定的目录-稀疏检出
- pc计算机怎么设置域名管理,如何设置域名的DNS服务器 -电脑资料
- 手機短信阻擊中國化工項目
- MFC CListCtrl 将一个列表的选中项添加到另一个列表
- 设置双击打开.ipynb文件
- 【iOS】—— 多线程编程八重曲之(二)- Pthread
- 炉石传说的代码是Java吗,hearthstone: 炉石传说,JAVA模拟器(HearthStone Simulator for Java)...
- 影响国债收益率的因素
- 前端八股文,https、跨域、闭包、原型链,布局、防抖节流等
- 网络——数据交换方式
- Buffer 的基本用法
- 业务中台构建--业务驱动为核心的云原生体系建设思考