设备驱动基础学习–platform driver简单实现「建议收藏」

设备驱动基础学习–platform driver简单实现「建议收藏」设备驱动基础学习–platformdriver简单实现

大家好,又见面了,我是你们的朋友全栈君。

platform是一条虚拟的总线。设备用platform_device表示,驱动用platform_driver进行注册,Linux platform driver机制和传统的device driver机制(通过driver_register进行注册)相比,一个明显的优势在于platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动中使用这些资源时通过platform device提供的标准结构进行申请并使用。这样提高了驱动和资源的独立性,并且具有较好的可移植性和安全性(这些标准接口是安全的)。

pltform机制本身使用并不复杂,由两部分组成:platform_device和platform_driver。通过platform机制开发底层驱动的大致流程为:定义platform_deive->注册platform_device->定义platform_driver->注册platform_driver。

platform driver的probe函数是平台总线实现匹配以后首先被调用的函数,因此在其中实现字符设备、块设备、网络设备驱动的初始化是有意义的,这样的设备驱动就是基于平台总线的设备驱动,便于维护。如果添加实际的设备到该平台总线设备驱动模型中,则可以在该函数中实现具体的设备驱动函数的初始化操作,包括设备号的申请,设备的初始化,添加。自动设备文件创建函数的添加等操作。或者是混杂字符设备的相关初始化操作。在remove函数中实现具体的设备的释放,包括设备的删除,设备号的注销等操作。

基于misc device实现一个简单的platform driver(仿照driver/char/sonypi.c).

fellowplat.h

#ifndef _FELLOW_MISC_H_
#define _FELLOW_MISC_H_
#include <linux/ioctl.h>
//#define FELLOW_MISC_MAJOR 199
//#define FELLOW_MISC_NR 2
struct miscdata {
int val;
char *str;
unsigned int size;
};
#define FELLOW_MISC_IOC_MAGIC ‘f’
#define FELLOW_MISC_IOC_PRINT _IO(FELLOW_MISC_IOC_MAGIC, 1)
#define FELLOW_MISC_IOC_GET _IOR(FELLOW_MISC_IOC_MAGIC, 2, struct miscdata)
#define FELLOW_MISC_IOC_SET _IOW(FELLOW_MISC_IOC_MAGIC, 3, struct miscdata)
#define FELLOW_MISC_IOC_MAXNR 3
#endif

 

fellowplat.c

#include <linux/module.h>
#include <linux/init.h>
//#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include “fellowplat.h”
struct fellowmisc_dev{
struct miscdevice misc;
struct miscdata data;
};
struct fellowmisc_dev *fellowmisc_devp;
int fellowmisc_open(struct inode *inode, struct file *filep)
{
  filep->private_data = fellowmisc_devp;
  return 0;
}
int fellowmisc_release(struct inode *inode, struct file *filep)
{
  return 0;
}

long fellowmisc_ioctl(struct file *filep,unsigned int cmd,unsigned long arg)

{

  int ret = 0;
  struct fellowmisc_dev *devp = (struct fellowmisc_dev *)(filep->private_data);
  if (_IOC_TYPE(cmd) != FELLOW_MISC_IOC_MAGIC)
    return -EINVAL;
  if (_IOC_NR(cmd) > FELLOW_MISC_IOC_MAXNR)
    return -EINVAL;
  switch(cmd)
  {
    case FELLOW_MISC_IOC_PRINT:
      printk(“FELLOW_MISC_IOC_PRINT\n”);
      printk(“val:%d, size: %d, str: %s\n”, devp->data.val, devp->data.size, devp->data.str);
    break;
    case FELLOW_MISC_IOC_SET:
      printk(“FELLOW_MISC_IOC_SET\n”);
      ret = copy_from_user((unsigned char*)&(devp->data), (unsigned char *)arg, sizeof(struct miscdata));
      printk(“set val:%d, size: %d, str: %s\n”, devp->data.val, devp->data.size, devp->data.str);
    break;
    case FELLOW_MISC_IOC_GET:
      printk(“FELLOW_MISC_IOC_GET\n”);
      ret = copy_to_user((unsigned char*)arg,(unsigned char*)&(devp->data), sizeof(struct miscdata));
    break;
    default:
      return -EINVAL;

  }
  return ret;
}
static const struct file_operations fellowmisc_fops ={
.owner = THIS_MODULE,
.open = fellowmisc_open,
.release = fellowmisc_release,
.unlocked_ioctl = fellowmisc_ioctl,
};
static struct miscdevice fellow_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = “fellowplat”,
.fops = &fellowmisc_fops,
};
static struct platform_device *fellow_platform_device;
static int fellow_plat_drv_probe(struct platform_device *dev)
{
  int error;
  printk(“fellow_plat_drv_probe\n”);
  fellowmisc_devp = kmalloc(sizeof(struct fellowmisc_dev), GFP_KERNEL);
  if (!fellowmisc_devp)
  {
    error = -ENOMEM;
    return error;
  }
  memset(&(fellowmisc_devp->data), 0, sizeof(fellowmisc_devp->data));
  fellowmisc_devp->misc = fellow_misc;
  error = misc_register(&fellow_misc);
  return error;
}
static int fellow_plat_drv_remove(struct platform_device *dev)
{
  int error;
  if (fellowmisc_devp)
    kfree(fellowmisc_devp);
  error = misc_deregister(&fellow_misc);
  return error;
}
static struct platform_driver fellow_platform_driver = {
.driver = {
.name = “fellow”,
},
.probe = fellow_plat_drv_probe,
.remove = fellow_plat_drv_remove,
};

static int fellowplat_init(void)
{
  int error;
  printk(“fellowplat_init\n”);
  printk(“fellow register driver\n”);
  error = platform_driver_register(&fellow_platform_driver);//注册platform driver
  if (error)
    return error;

  fellow_platform_device = platform_device_alloc(“fellow”, -1);//名字与platform driver相同。
  if (!fellow_platform_device) {
    error = -ENOMEM;
    goto err_driver_unregister;
  }

  printk(“fellow register device\n”);
  error = platform_device_add(fellow_platform_device);//添加platform device
  if (error)
    goto err_free_device;

  return 0;

err_free_device:
  platform_device_put(fellow_platform_device);
err_driver_unregister:
  platform_driver_unregister(&fellow_platform_driver);
  return error;
}

static void fellowplat_exit(void)
{
  platform_device_unregister(fellow_platform_device);
  platform_driver_unregister(&fellow_platform_driver);
}

MODULE_AUTHOR(“fellow”);
MODULE_LICENSE(“GPL”);
module_init(fellowplat_init);
module_exit(fellowplat_exit);

 

app.c

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include “fellowplat.h”
int main(void)
{
  int fd = open(“/dev/fellowplat”, O_RDWR);
  if (fd < 0)
  {
    printf(“open fail:%s\n”, strerror(errno));
    return -1;
  }
  int ret = 0;
  struct miscdata data;
  data.val = 18;
  data.str = “fellow platform device”;
  data.size = sizeof(“fellow platform device”);
  if ((ret = ioctl(fd, FELLOW_MISC_IOC_SET, &data)) < 0)
  {
    printf(“ioctl set fail:%s\n”, strerror(errno));
  }
  struct miscdata getdata;
  if ((ret = ioctl(fd, FELLOW_MISC_IOC_GET, &getdata)) < 0)
  {
    printf(“ioctl get fail:%s\n”, strerror(errno));
  }
  printf(“get val:%d, str:%s, size: %d\n”, getdata.val, getdata.str, getdata.size);
  if ((ret = ioctl(fd, FELLOW_MISC_IOC_PRINT, NULL)) < 0)
  {
    printf(“ioctl print fail:%s\n”, strerror(errno));
  }
  close(fd);
  return ret;
}

运行结果如下

设备驱动基础学习--platform driver简单实现「建议收藏」

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/154964.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • GSLB原理介绍

    GSLB原理介绍1.GSLB     GSLB,是GlobalServerLoadBalance的缩写,意思是全局负载均衡。目的是实现在广域网(包括互联网)上不同地域的服务器间的流量调配,保证使用户的请求能被离用户最近或者服务质量最好的服务器来处理,从而确保访问质量。       能通过判断服务器的负载,包括CPU占用、带宽占用等数据,决定服务器的可用性,同时能判断用户(访问者)与服

  • 杭州电信域名解析服务器,浙江电信的DNS是多少?

    杭州电信域名解析服务器,浙江电信的DNS是多少?1、湖州、嘉兴、绍兴、舟山202.101.172.362、金华、衢州、丽水、台州202.101.172.373、温州202.96.104.164、宁波202.96.104.155、杭州202.101.172.35行政区域名行政区域名是按照我国的各个行政区划分而成的,其划分标准依照国家技术监督局发布的国家标准而定,包括“行政区域名”34个,适用于我国的各省、自治区、直辖市,分别为:BJ-北京市;SH…

  • ShFileOperation函数详解

    ShFileOperation函数详解[WinAPI]ShFileOperation函数详解2010-04-1110:24ShFileOperation只有一个参数是LPSHFILEOPSTRUCT型的相当于delphi中的TSHFileOpStruct;  c语言定义为:  typedef struct _SHFILEOPSTRUCT{   HWND         hwnd

  • 一遍过的刷四史脚本教程「建议收藏」

    一遍过的刷四史脚本教程「建议收藏」首先你得有台电脑和一部手机手机进行抓包提前打开四史的界面,切换至后台,然后打开手机中的抓包软件开始抓包!进入带四史答题界面后,切换后台,打开抓包软件,一个一个打开去找抓包内容请求中Bearer后面的字符串电脑运行文件四史脚本加抓包软件传送门->>>…

  • 分布式架构设计之电商平台

    分布式架构设计之电商平台何为软件架构?不同人的答案会有所不同,而我认为一个好的软件架构除了要具备业务功能外,还应该具备一定的高性能、高可用、高伸缩性及可拓展等非功能需求。而软件架构是由业务架构和技术架构两部分组成,因为有了业务结构才会催生出软件架构,进而来满足业务上的需求,所以,在做软件架构设计时,需要分为业务架构设计和技术软件架构设计,二者不可分离哦!那么,接下来就以本人实际工作中的电商平台为例,进行说明电商平台架构设计,因为不同行业产品系统不同业务不同,而催生的系统软件的实现要求及架构设计就不同了!

  • 单模和多模光纤可以混用吗_多模光纤和单模光纤能混用吗

    单模和多模光纤可以混用吗_多模光纤和单模光纤能混用吗我们知道光纤和光模块都有单模和多模两种类型,那么我们可能在使用中会产生疑问,单模/多模光纤和单模/多模光模块如何配套使用?它们可以混用吗?下面飞速光纤将通过问答的方式来为大家解答这个疑惑。  问:单模光纤和多模光纤有什么区别?  答:单模光纤采用固体激光器做光源;多模光纤则采用发光二极管做光源;单模光纤传输频带宽、传输距离长,但因其需要激光源,成本较高;多模光纤传输速度低、距离短,但其成本比较低;单模光纤芯径和色散小,仅允许一种模式传输;多模光纤芯径和色散大,允许上百种模式传输。  问:单模光模块和多模

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号