基于platform_driver 实现的misc 杂项设备

Posted Wu_Being

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于platform_driver 实现的misc 杂项设备相关的知识,希望对你有一定的参考价值。

misc 杂项设备

有些设备不像Input/rtc/framebuffer/tty等具体设备有归属类型可以套用框架,这些设备都叫杂项misc 设备。
misc_init()确定了miscdevice 的主设备号固定是10,次设备号在注册misc 可自定义,也可以随意分配。

wu-being@ubuntu:~/code/linux_driver_study/song06--Character-devices$ cat /proc/devices 
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
 13 input
 21 sg
 29 fb
108 ppp
128 ptm
136 pts
180 usb
189 usb_device
251 hidraw
252 bsg
253 watchdog
254 rtc
...

本章节两个例子中,分别insmod 各自ko文件(无先后顺序),分别会注册platfrom device 和platform driver。
如果platfrom device的name 和platform driver的driver.name 通过platfrom_match 匹配一致,则会运行driver 的probe 函数,根据实现的probe代码创建/dev/xxx 相关misc设备。

如下用各自的名字是无法匹配

misc-platform_device

ls /sys/devices/platform/ -l
...
drwxr-xr-x 3 root root    0 May 20 14:58 misc_platform_device-name

ls /sys/bus/platform/devices/ -l
...
lrwxrwxrwx 1 root root 0 May 20 15:03 misc_platform_device-name -> ../../../devices/platform/misc_platform_device-name

ls /sys/devices/platform/misc_platform_device-name/ -l
...
lrwxrwxrwx 1 root root    0 May 20 15:02 subsystem -> ../../../bus/platform

misc-platform_driver

ls /sys/bus/platform/drivers/ -l
...
drwxr-xr-x 2 root root    0 May 20 16:47 misc_platform_driver-name/

ls /sys/bus/platform/drivers/misc_platform_driver-name/ -l
...
lrwxrwxrwx 1 root root    0 May 20 16:47 module -> ../../../../module/misc_platform_driver

如下用相同的名字是可以匹配

misc-platform_driver

ls /sys/bus/platform/drivers/ -l
...
drwxr-xr-x 2 root root    0 May 20 17:09 misc_platform_device+driver-name/   

ls /sys/bus/platform/drivers/misc_platform_device+driver-name/ -l
...
lrwxrwxrwx 1 root root    0 May 20 17:09 misc_platform_device+driver-name -> ../../../../devices/platform/misc_platform_device+driver-name
lrwxrwxrwx 1 root root    0 May 20 17:08 module -> ../../../../module/misc_platform_driver

misc-platform_device

ls /sys/devices/platform/misc_platform_device+driver-name/ -l
...
lrwxrwxrwx 1 root root    0 May 20 17:08 driver -> ../../../bus/platform/drivers/misc_platform_device+driver-name
lrwxrwxrwx 1 root root    0 May 20 17:18 subsystem -> ../../../bus/platform

ls /dev/ |grep misc
misc_platform_driver_miscdeviceNAME

相关代码

完整代码:https://github.com/1040003585/linux_driver_study/tree/main/song12–Softwave-Architecture/2.Layered-thinking/6.misc-device-driver

misc-platform_device.c

#include <linux/module.h>
//#include <linux/init.h>
//#include <linux/kernel.h>
//#include <linux/fs.h>
//#include <linux/miscdevice.h>
//#include <asm/uaccess.h>
#include <linux/platform_device.h>


#define TAG "[misc_platform_device] "

static struct platform_device *pdev;

static int misc_platform_device_init(void)

	int ret;
	
	//pdev = platform_device_alloc("misc_platform_device-name", -1);
	pdev = platform_device_alloc("misc_platform_device+driver-name", -1);
	if (!pdev)
		return -ENOMEM;

	ret = platform_device_add(pdev);
	if(ret) 
		platform_device_put(pdev);
		return ret;
	

	printk(TAG "misc_platform_device_init sucess\\n");
	return ret;


static void misc_platform_device_exit(void)

	platform_device_unregister(pdev);

	printk(TAG "misc_platform_device_exit sucess\\n");


module_init(misc_platform_device_init);
module_exit(misc_platform_device_exit);

MODULE_LICENSE("GPL");

misc-platform_driver.c

#include <linux/module.h>
//#include <linux/init.h>
//#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
//#include <asm/uaccess.h>
#include <linux/platform_device.h>

#define DEVICE_NAME "misc_platform_driver_miscdeviceNAME"
#define TAG "[misc_platform_driver] "


static struct file_operations  dev_fops =
 .owner = THIS_MODULE ;

static struct miscdevice misc =
 .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops ;

struct globalfifo_dev 
	// spinlock_t lock;
	// ...
	struct miscdevice miscdev;
;

static int misc_platform_driver_probe(struct platform_device *pdev)

	int ret;
	struct globalfifo_dev *gl;

	gl = devm_kzalloc(&pdev->dev, sizeof(*gl), GFP_KERNEL);
	if (!gl)
		return -ENOMEM;

	gl->miscdev = misc;
	platform_set_drvdata(pdev, gl);
	
	ret = misc_register(&gl->miscdev);
	if (ret < 0)
		goto err;

	printk(TAG "misc_platform_driver_probe sucess\\n");
	return 0;
err:
	printk(TAG "misc_platform_driver_init err...\\n");
	return 0;



static int misc_platform_driver_remove(struct platform_device *pdev)

	struct globalfifo_dev *gl = platform_get_drvdata(pdev);
	misc_deregister(&gl->miscdev);

	printk(TAG "misc_platform_driver_remove sucess\\n");

	return 0;


static struct platform_driver misc_platform_driver = 
	.driver = 
		//.name  = "misc_platform_driver-name",
		.name  = "misc_platform_device+driver-name",
		.owner = THIS_MODULE,
	,
	.probe = misc_platform_driver_probe,
	.remove = misc_platform_driver_remove,
;

/*
#define module_driver(__driver, __register, __unregister, ...) \\
static int __init __driver##_init(void) \\
 \\
	return __register(&(__driver) , ##__VA_ARGS__); \\
 \\
module_init(__driver##_init); \\
static void __exit __driver##_exit(void) \\
 \\
	__unregister(&(__driver) , ##__VA_ARGS__); \\
 \\
module_exit(__driver##_exit);


#define module_platform_driver(__platform_driver) \\
	module_driver(__platform_driver, platform_driver_register, \\
			platform_driver_unregister)
*/

module_platform_driver(misc_platform_driver);

MODULE_LICENSE("GPL");

以上是关于基于platform_driver 实现的misc 杂项设备的主要内容,如果未能解决你的问题,请参考以下文章

基于platform_driver 实现的misc 杂项设备

基于platform_driver 实现的misc 杂项设备

手把手教你从零实现Linux misc设备驱动一(基于友善之臂4412开发板)

linux platform device/driver--Platform Device和Platform_driver注册过程之代码对比

linux platform device/driver--Platform Device和Platform_driver注册过程

adreno源码系列注册platform_driver