#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/of_platform.h>
#include <linux/of_device.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/sysfs.h>
/* 描述字符设备的结构体,包括主设备号次设备号,定义操作函数 */
static struct cdev mtdev;
/* 在/sys/class/下创建设备的逻辑类 */
static struct class *mtclass;
/* 设备驱动模型基础结构体 */
static struct device *mtdevice;
/* 设备驱动设备号 */
static dev_t devid;
static int mymotor_open (struct inode *inode, struct file *filep)
{
printk("mymotor_open()\n");
return 0;
}
static ssize_t mymotor_read(struct file *filep, char __user *buf, size_t len, loff_t *off)
{
printk("mymotor_read()\n");
return 0;
}
static ssize_t mymotor_write(struct file *filep, const char __user *buf, size_t len, loff_t *off)
{
printk("mymotor_write()\n");
return 0;
}
static long mymotor_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
printk("mymotor_ioctl()\n");
return 0;
}
static int mymotor_release(struct inode *inode, struct file *filep)
{
printk("mymotor_release()\n");
return 0;
}
static struct file_operations mymotor_ops =
{
.owner = THIS_MODULE,
.open = mymotor_open,
.release = mymotor_release,
.read = mymotor_read,
.write = mymotor_write,
.unlocked_ioctl = mymotor_ioctl,
};
/* 探测 */
static int mymotor_probe(struct platform_device *pdev)
{
/*注册字符串设备(申请设备号),
参数分别为:
申请的设备号、次设备号起始、申请次设备号个数、执行cat /proc/device显示的名称
*/
alloc_chrdev_region(&devid, 0, 1, "mymotor_id");
/* 初始化字符设备,建立cdev 和 file_operations之间的连接 */
cdev_init(&mtdev, &mymotor_ops);
/* 添加一个字符设备到系统,正式通知内核由struct cdev *p代表的字符设备已经可以使用了 */
cdev_add(&mtdev, devid, 1);
/* 在/sys/class/下创建设备的逻辑类,并完成部分字段的初始化,然后将其添加进Linux内核系统中 */
mtclass = class_create(THIS_MODULE, "my_motor");
if(mtclass == NULL)
{
pr_info("class = NULL\n");
}
/* 创建设备节点
参数分别为:
设备所依赖的类、父设备、设备号、保存私有数据、设备名
*/
mtdevice = device_create(mtclass, &pdev->dev, devid, &mtdev, "%s%d", "my_motor", 1);
if(mtdevice == NULL)
{
pr_info("mtdevice = NULL\n");
}
pr_info("mymotor_probe ok");
return 0;
}
/* 移除 */
static int mymotor_remove(struct platform_device *pdev)
{
/* 卸载设备 */
device_destroy(mtclass, devid);
/* 删除设备的逻辑类,即从Linux内核系统中删除设备的逻辑类 */
class_destroy(mtclass);
/* 注销字符设备驱动*/
cdev_del(&mtdev);
/* 释放设备号*/
unregister_chrdev_region(devid, 1);
pr_info("mymotor_remove ok");
return 0;
}
static struct of_device_id mymotor_match[] = {
{ .compatible = "lindenis,ldmotor", },
{}
};
/* 平台驱动注册结构体 */
static struct platform_driver mymotor_drv = {
.probe = mymotor_probe,
.remove = mymotor_remove,
.driver = {
.name = "my_motor",
.of_match_table = of_match_ptr(mymotor_match),
},
};
static int __init cll_drv_init(void)
{
int err;
/* 平台驱动注册 */
err = platform_driver_register(&mymotor_drv);
if(err < 0)
{
pr_info("platform_driver_register error\n");
}
pr_info("call drv init\n");
return 0;
}
static void __exit cll_drv_exit(void)
{
/* 平台驱动注销 */
platform_driver_unregister(&mymotor_drv);
pr_info("call drv exit\n");
}
module_init(cll_drv_init);
module_exit(cll_drv_exit);
MODULE_LICENSE("GPL");
/* 测试程序 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
int fd, ret;
char buf[32] = {0};
fd = open("/dev/my_motor1", O_RDWR);
if(fd < 0)
{
printf("open /dev/my_motor1 ret = %d error\n", fd);
return -1;
}
ret = read(fd, buf, 32);
if(ret < 0)
{
printf("read /dev/my_motor1 error\n");
return -1;
}
ret = ioctl(fd, 0, 0);
if(ret < 0)
{
printf("ioctl error\n");
return -1;
}
ret = write(fd, buf, 32);
if(ret < 0)
{
printf("write /dev/my_motor1 error\n");
return -1;
}
close(fd);
return 0;
}
运行代码结果: