Adding a device from a kernel module the udev way (pi, a device that prints out the value of pi)
来源:互联网 发布:linux网站管理系统限速 编辑:程序博客网 时间:2024/05/29 17:28
It seems like the interface for creating modules has changed a bit, andudev is the way to go. Here is a short, quick and dirty module thatcreates /dev/pi with anonymous major and minor using udev.
The module creates the device (not the file) with register_chrdev, which returns the value of the major number that was assigned. We then use class_create to create a udev class to contain the device, finally you call device_create to actually make the device file.
To remove the device, you call device_destroy to remove the device from the class, then class_destroy to delete the class and finally unregister_chrdev to remove the device from the kernel (the device not the file).
The relevant init and exit sections are:
Hope that helps someone out there.source files
static int pi_init(void)
{
struct device *err_dev;
msg = kmalloc(sizeof(char)*DIGITS, GFP_ATOMIC);
// register pi as a device
pi_Major = register_chrdev(0, DEVICE_NAME, &fops);
if (pi_Major < 0) {
printk ("Failed to register device %s with error %d/n", DEVICE_NAME, pi_Major);
return pi_Major;
}
/* create /dev/pi
* we use udev to make the file
*/
pi_class = class_create(THIS_MODULE,DEVICE_NAME);
err_dev = device_create(pi_class, NULL, MKDEV(pi_Major,0),NULL,DEVICE_NAME);
return 0;
}
/* remove the module
*/
static void pi_exit(void)
{
/* free our memory, then destroy the device
* then unregister the class and destroy it (unregister before destroy....important
*/
kfree(msg);
device_destroy(pi_class,MKDEV(pi_Major,0));
class_destroy(pi_class);
unregister_chrdev(pi_Major, DEVICE_NAME);
}
- Adding a device from a kernel module the udev way (pi, a device that prints out the value of pi)
- Write a program that prints the numbers from 1 to 100,but for multiples of three print “Fizz” inste
- 7.3 Pinpointing the Location of a Device
- Adding a simple io device
- Enable/disable a device from the command line
- Xcode9 Could not receive a message from the device
- Find a way out of the ClassLoader maze (1)
- Find a way out of the ClassLoader maze (2)
- Find a way out of the ClassLoader maze
- Find a way out of the ClassLoader maze
- Setup Kubernetes on a Raspberry Pi Cluster easily the official way!
- how to change the default mode of a linux device
- How to Accept/Reject the Transfer of a Device
- THE VALUE OF A SMILE
- A Tutorial on the Device Tree
- sp_addsegment the specified device is not a database device
- Make sure the device specification refers to a valid device
- Use the Raspberry Pi as a DIY Surveillance camera
- 写代码之余
- sockaddr和sockaddr_in
- repllog rapisrv eventrst rra_stm systemcpl udp2tcp wceload unload
- 判断目录、创建目录、保存文件、取得目录下文件、删除文件
- 梦的终焉II
- Adding a device from a kernel module the udev way (pi, a device that prints out the value of pi)
- GoDaddy上注册域名,还需要备案吗?
- 再看ODBC数据访问
- php 日历
- 穷人们的生活
- Windows多媒体API
- Hibernate 疑难异常及处理
- plug.xml文件初步分析
- java web service 开发 具体实例