Ai
1 Star 0 Fork 0

flameboyence/linux_driver_example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
semaphore.c 5.35 KB
一键复制 编辑 原始数据 按行查看 历史
zkw 提交于 2020-11-01 19:05 +08:00 . 初始化
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <asm/atomic.h>
#define GPIOLED_CNT 1
#define GPIOLED_NAME "gpioled"
#define LEDOFF 0
#define LEDON 1
struct gpioled_dev
{
dev_t devid; /* 设备号 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct cdev cdev; /* 字符设备 */
struct class *class; /* 类 */
struct device_node *nd; /* 设备节点 */
struct device *device; /* 设备 */
int led_gpio;
struct semaphore sem; /* 信号量 */
};
struct gpioled_dev gpioled;
static ssize_t gpioled_write(struct file *pfile,
const char __user *buf, size_t cnt, loff_t *ppos){
int ret = 0;
unsigned char databuf[1];
struct gpioled_dev *dev = pfile->private_data;
ret = copy_from_user(databuf, buf, cnt);
if(ret < 0) {
printk("copy_from_user failed!\r\n");
return -EINVAL;
}
if (databuf[0] == LEDON){
gpio_set_value(dev->led_gpio, 0);
}else if(databuf[0] == LEDOFF)
{
gpio_set_value(dev->led_gpio, 1);
}else
{
printk("command err! please input 0 or 1!\r\n");
return -EINVAL;
}
return ret;
}
static int gpioled_open(struct inode *inode, struct file *pfile){
down(&gpioled.sem);
pfile->private_data = &gpioled;
return 0;
}
int gpioled_release(struct inode *pinode, struct file *pfile){
up(&gpioled.sem);
return 0;
}
static const struct file_operations gpioled_fops= {
.owner = THIS_MODULE,
.write = gpioled_write,
.open = gpioled_open,
.release = gpioled_release,
};
static int __init gpioled_init(void){
int ret = 0;
sema_init(&gpioled.sem, 1);
/* 1,申请设备号 */
gpioled.major = 0; /* 设备号由内核分配 */
if(gpioled.major) { /* 定义了设备号 */
gpioled.devid = MKDEV(gpioled.major, 0);
ret = register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
} else { /* 没有给定设备号 */
ret = alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
if(ret < 0) {
goto fail_devid;
}
printk("gpioled_devid = %d\r\n", gpioled.devid);
printk("gpioled_major = %d\r\n", gpioled.major);
printk("gpioled_minor = %d\r\n", gpioled.minor);
/* 2,添加字符设备 */
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
if(ret < 0)
goto fail_cdev;
printk("cdev_add successful!\r\n");
/* 3,自动创建设备节点 */
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class)) {
ret = PTR_ERR(gpioled.class);
goto fail_class;
}
printk("class_create successful!\r\n");
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.device)) {
ret = PTR_ERR(gpioled.device);
goto fail_device;
}
printk("device_create successful!\r\n");
/* 获取设备节点 */
gpioled.nd = of_find_node_by_path("/gpioled");
if (gpioled.nd == NULL) { /* 失败 */
ret = -EINVAL;
goto fail_findnode;
}
printk("find dts node successful!\r\n");
/* 2, 获取LED所对应的GPIO */
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
if(gpioled.led_gpio < 0) {
printk("can't find led gpio\r\n");
ret = -EINVAL;
goto fail_findnode;
}
printk("led gpio num = %d\r\n", gpioled.led_gpio);
/* 3,申请IO */
ret = gpio_request(gpioled.led_gpio, "led-gpio");
if (ret) {
printk("Failed to request the led gpio\r\n");
ret = -EINVAL;
goto fail_findnode;
}
/* 4,使用IO,设置为输出 */
ret = gpio_direction_output(gpioled.led_gpio, 1);
if (ret) {
goto fail_setoutput;
}
/* 5操作gpio*/
gpio_set_value(gpioled.led_gpio, 0);
return 0;
fail_setoutput:
gpio_free(gpioled.led_gpio);
fail_findnode:
device_destroy(gpioled.class, gpioled.devid);
fail_device:
class_destroy(gpioled.class);
fail_class:
cdev_del(&gpioled.cdev);
fail_cdev:
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
fail_devid:
return ret;
}
static void __exit gpioled_exit(void){
/* 关灯 */
gpio_set_value(gpioled.led_gpio, 1);
/* 释放IO */
gpio_free(gpioled.led_gpio);
/* 删除字符设备 */
cdev_del(&gpioled.cdev);
printk("cdev_del successful!\r\n");
/* 释放设备号 */
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
printk("unregister chrdev successful!\r\n");
/* 摧毁设备*/
device_destroy(gpioled.class, gpioled.devid);
printk("device_destroy successful!\r\n");
/* 摧毁类 */
class_destroy(gpioled.class);
printk("class_destroy successful!\r\n");
}
module_init(gpioled_init);
module_exit(gpioled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/flameboyence/linux_driver_example.git
git@gitee.com:flameboyence/linux_driver_example.git
flameboyence
linux_driver_example
linux_driver_example
master

搜索帮助