linux platform driver register

linux platform driver registerPlatformDevicesandDrivers~~~~~~~~~~~~~~~~~~~~~~~~~~~~Seeforthedrivermodelinterfacetotheplatformbus: platform_device,andplatform_driver. Thispseudo-busisusedtoconnectdevice

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
Platform Devices and Drivers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See <linux/platform_device.h> for the driver model interface to the
platform bus:  platform_device, and platform_driver.  This pseudo-bus
is used to connect devices on busses with minimal infrastructure,
like those used to integrate peripherals on many system-on-chip
processors, or some “legacy” PC interconnects; as opposed to large
formally specified ones like PCI or USB.

Platform devices
~~~~~~~~~~~~~~~~
Platform devices are devices that typically appear as autonomous
entities in the system. This includes legacy port-based devices and
host bridges to peripheral buses, and most controllers integrated
into system-on-chip platforms.  What they usually have in common
is direct addressing from a CPU bus.  Rarely, a platform_device will
be connected through a segment of some other kind of bus; but its
registers will still be directly addressable.

Platform devices are given a name, used in driver binding, and a
list of resources such as addresses and IRQs.

struct platform_device {
    const char    *name;
    u32        id;
    struct device    dev;
    u32        num_resources;
    struct resource    *resource;
};

Platform drivers

~~~~~~~~~~~~~~~~

Platform drivers follow the standard driver model convention, where

discovery/enumeration is handled outside the drivers, and drivers

provide probe() and remove() methods.  They support power management

and shutdown notifications using the standard conventions.

struct platform_driver {
    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
    void (*shutdown)(struct platform_device *);
    int (*suspend)(struct platform_device *, pm_message_t state);
    int (*suspend_late)(struct platform_device *, pm_message_t state);
    int (*resume_early)(struct platform_device *);
    int (*resume)(struct platform_device *);
    struct device_driver driver;
};

Note that probe() should general verify that the specified device hardware

actually exists; sometimes platform setup code can’t be sure.  The probing

can use device resources, including clocks, and device platform_data.

Platform drivers register themselves the normal way:

    int platform_driver_register(struct platform_driver *drv);

Or, in common situations where the device is known not to be hot-pluggable,

the probe() routine can live in an init section to reduce the driver’s

runtime memory footprint:

    int platform_driver_probe(struct platform_driver *drv,

              int (*probe)(struct platform_device *))

Device Enumeration

~~~~~~~~~~~~~~~~~~

As a rule, platform specific (and often board-specific) setup code will

register platform devices:

    int platform_device_register(struct platform_device *pdev);

    int platform_add_devices(struct platform_device **pdevs, int ndev);

The general rule is to register only those devices that actually exist,

but in some cases extra devices might be registered.  For example, a kernel

might be configured to work with an external network adapter that might not

be populated on all boards, or likewise to work with an integrated controller

that some boards might not hook up to any peripherals.

In some cases, boot firmware will export tables describing the devices

that are populated on a given board.   Without such tables, often the

only way for system setup code to set up the correct devices is to build

a kernel for a specific target board.  Such board-specific kernels are

common with embedded and custom systems development.

In many cases, the memory and IRQ resources associated with the platform

device are not enough to let the device’s driver work.  Board setup code

will often provide additional information using the device’s platform_data

field to hold additional information.

Embedded systems frequently need one or more clocks for platform devices,

which are normally kept off until they’re actively needed (to save power).

System setup also associates those clocks with the device, so that that

calls to clk_get(&pdev->dev, clock_name) return them as needed.

Legacy Drivers:  Device Probing

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Some drivers are not fully converted to the driver model, because they take

on a non-driver role:  the driver registers its platform device, rather than

leaving that for system infrastructure.  Such drivers can’t be hotplugged

or coldplugged, since those mechanisms require device creation to be in a

different system component than the driver.

The only “good” reason for this is to handle older system designs which, like

original IBM PCs, rely on error-prone “probe-the-hardware” models for hardware

configuration.  Newer systems have largely abandoned that model, in favor of

bus-level support for dynamic configuration (PCI, USB), or device tables

provided by the boot firmware (e.g. PNPACPI on x86).  There are too many

conflicting options about what might be where, and even educated guesses by

an operating system will be wrong often enough to make trouble.

This style of driver is discouraged.  If you’re updating such a driver,

please try to move the device enumeration to a more appropriate location,

outside the driver.  This will usually be cleanup, since such drivers

tend to already have “normal” modes, such as ones using device nodes that

were created by PNP or by platform device setup.

None the less, there are some APIs to support such legacy drivers.  Avoid

using these calls except with such hotplug-deficient drivers.

    struct platform_device *platform_device_alloc(

            const char *name, int id);

You can use platform_device_alloc() to dynamically allocate a device, which

you will then initialize with resources and platform_device_register().

A better solution is usually:

    struct platform_device *platform_device_register_simple(

            const char *name, int id,

            struct resource *res, unsigned int nres);

You can use platform_device_register_simple() as a one-step call to allocate

and register a device.

Device Naming and Driver Binding

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The platform_device.dev.bus_id is the canonical name for the devices.

It’s built from two components:

    * platform_device.name … which is also used to for driver matching.

    * platform_device.id … the device instance number, or else “-1”

      to indicate there’s only one.

These are concatenated, so name/id “serial”/0 indicates bus_id “serial.0”, and

“serial/3” indicates bus_id “serial.3”; both would use the platform_driver

named “serial”.  While “my_rtc”/-1 would be bus_id “my_rtc” (no instance id)

and use the platform_driver called “my_rtc”.

Driver binding is performed automatically by the driver core, invoking

driver probe() after finding a match between device and driver.  If the

probe() succeeds, the driver and device are bound as usual.  There are

three different ways to find such a match:

    – Whenever a device is registered, the drivers for that bus are

      checked for matches.  Platform devices should be registered very

      early during system boot.

    – When a driver is registered using platform_driver_register(), all

      unbound devices on that bus are checked for matches.  Drivers

      usually register later during booting, or by module loading.

    – Registering a driver using platform_driver_probe() works just like

      using platform_driver_register(), except that the driver won’t

      be probed later if another device registers.  (Which is OK, since

      this interface is only for use with non-hotpluggable devices.)

Early Platform Devices and Drivers

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The early platform interfaces provide platform data to platform device

drivers early on during the system boot. The code is built on top of the

early_param() command line parsing and can be executed very early on.

Example: “earlyprintk” class early serial console in 6 steps

1. Registering early platform device data

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The architecture code registers platform device data using the function

early_platform_add_devices(). In the case of early serial console this

should be hardware configuration for the serial port. Devices registered

at this point will later on be matched against early platform drivers.

2. Parsing kernel command line

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The architecture code calls parse_early_param() to parse the kernel

command line. This will execute all matching early_param() callbacks.

User specified early platform devices will be registered at this point.

For the early serial console case the user can specify port on the

kernel command line as “earlyprintk=serial.0” where “earlyprintk” is

the class string, “serial” is the name of the platform driver and

0 is the platform device id. If the id is -1 then the dot and the

id can be omitted.

3. Installing early platform drivers belonging to a certain class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The architecture code may optionally force registration of all early

platform drivers belonging to a certain class using the function

early_platform_driver_register_all(). User specified devices from

step 2 have priority over these. This step is omitted by the serial

driver example since the early serial driver code should be disabled

unless the user has specified port on the kernel command line.

4. Early platform driver registration

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compiled-in platform drivers making use of early_platform_init() are

automatically registered during step 2 or 3. The serial driver example

should use early_platform_init(“earlyprintk”, &platform_driver).

5. Probing of early platform drivers belonging to a certain class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The architecture code calls early_platform_driver_probe() to match

registered early platform devices associated with a certain class with

registered early platform drivers. Matched devices will get probed().

This step can be executed at any point during the early boot. As soon

as possible may be good for the serial port case.

6. Inside the early platform driver probe()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The driver code needs to take special care during early boot, especially

when it comes to memory allocation and interrupt registration. The code

in the probe() function can use is_early_platform_device() to check if

it is called at early platform device or at the regular platform device

time. The early serial driver performs register_console() at this point.

For further information, see <linux/platform_device.h>.

中文版:

http://wenku.baidu.com/view/ff17f319227916888486d7ca.html

platform_driver_register与of_register_platform_driver分析

转载:

http://www.360doc.com/content/13/0509/09/12277762_284056901.shtml

在ARM体系结构中常见的是platform_driver_register
在powerpc中是of_register_platform_driver
下面通过具体的代码来分析其区别
platform_driver_register在driver/base/platform.c中定义
int platform_driver_register(struct platform_driver *drv)
{

    drv->driver.bus = &platform_bus_type;
    if (drv->probe)
        drv->driver.probe = platform_drv_probe;
    if (drv->remove)
        drv->driver.remove = platform_drv_remove;
    if (drv->shutdown)
        drv->driver.shutdown = platform_drv_shutdown;

    return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(platform_driver_register);
——————————————————————————————————
而of_register_platform_driver在of_platform.c中定义
static inline int of_register_platform_driver(struct of_platform_driver *drv)
{

    return of_register_driver(drv, &of_platform_bus_type);
}
int of_register_driver()在driver/of/plateform.c中定义
int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
{

    drv->driver.bus = bus;

    /* register with core */
    return driver_register(&drv->driver);
}
而着最终都是调用的driver_register();
_____________________________________
这是由于二者结构获取硬件信息 的方式不同造成 的,在powerpc体系是通过dts
对比platform_driver和of_platform_driver
在include/linux/platform_device.h
struct platform_driver {

    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
    void (*shutdown)(struct platform_device *);
    int (*suspend)(struct platform_device *, pm_message_t state);
    int (*resume)(struct platform_device *);
    struct device_driver driver;
    const struct platform_device_id *id_table;
};
在include/linux/of_platform_device.h
struct of_platform_driver
{

    int    (*probe)(struct of_device* dev,
             const struct of_device_id *match);
    int    (*remove)(struct of_device* dev);

    int    (*suspend)(struct of_device* dev, pm_message_t state);
    int    (*resume)(struct of_device* dev);
    int    (*shutdown)(struct of_device* dev);

    struct device_driver    driver;
};
#define    to_of_platform_driver(drv) \
    container_of(drv,struct of_platform_driver, driver)
of_device_id 和platform_device_id
可以看出 主要所区别在 of_device 和platform_device
***********************************************************
在arch/powerpc/include/asm/of_device.h定义了
/*
 * The of_device is a kind of “base class” that is a superset of
 * struct device for use by devices attached to an OF node and
 * probed using OF properties.
 */
struct of_device
{

    struct device        dev;        /* Generic device interface */
    struct pdev_archdata    archdata;
};

/*
 * Struct used for matching a device
 */
struct of_device_id
{

    char    name[32];
    char    type[32];
    char    compatible[128];  //dts中看到这个东东了哈。通过它的匹配获取硬件资源
#ifdef __KERNEL__
    void    *data;
#else
    kernel_ulong_t data;
#endif
};
——————————————————————————————————
在在include/linux/of_platform_device.h定义
struct platform_device {

    const char    * name;
    int        id;
    struct device    dev;//这个是通用的
    u32        num_resources;
    struct resource    * resource; //获取硬件资源

    const struct platform_device_id    *id_entry;

    /* arch specific additions */
    struct pdev_archdata    archdata;
};从上述定义的文件也可以发现powerpc 并不是一个通用的,和自己体系相关

struct platform_device_id {

    char name[PLATFORM_NAME_SIZE];
    kernel_ulong_t driver_data
            __attribute__((aligned(sizeof(kernel_ulong_t))));
};

*********************************
返回到我们的
int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
{

    drv->driver.bus = bus;

    /* register with core */
    return driver_register(&drv->driver);
}
bus 是从of_register_platform_driver中传入 of_platform_bus_type,是个全局变量在
arch/powerpc/kernel/of_platform.c中定义
struct bus_type of_platform_bus_type = {

       .uevent    = of_device_uevent,
};

of_device_uevent,在同上目录of_device.c定义
int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
{

    struct of_device *ofdev;
    const char *compat;
    int seen = 0, cplen, sl;

    if (!dev)
        return -ENODEV;

    ofdev = to_of_device(dev);

    if (add_uevent_var(env, “OF_NAME=%s”, ofdev->dev.of_node->name))
        return -ENOMEM;

    if (add_uevent_var(env, “OF_TYPE=%s”, ofdev->dev.of_node->type))
        return -ENOMEM;

        /* Since the compatible field can contain pretty much anything
         * it’s not really legal to split it out with commas. We split it
         * up using a number of environment variables instead. */

    compat = of_get_property(ofdev->dev.of_node, “compatible”, &cplen);//根据compatible获取资源 具体看dts文件,在i2c学习中提过
    while (compat && *compat && cplen > 0) {

        if (add_uevent_var(env, “OF_COMPATIBLE_%d=%s”, seen, compat))
            return -ENOMEM;

        sl = strlen (compat) + 1;
        compat += sl;
        cplen -= sl;
        seen++;
    }

    if (add_uevent_var(env, “OF_COMPATIBLE_N=%d”, seen))
        return -ENOMEM;

    /* modalias is trickier, we add it in 2 steps */
    if (add_uevent_var(env, “MODALIAS=”))
        return -ENOMEM;
    sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
                    sizeof(env->buf) – env->buflen);
    if (sl >= (sizeof(env->buf) – env->buflen))
        return -ENOMEM;
    env->buflen += sl;

    return 0;
}

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158246.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 共享打印机无法连接到打印机0x00000bcb_共享打印机错误为0X0000011b

    共享打印机无法连接到打印机0x00000bcb_共享打印机错误为0X0000011b  有不少用户遇到了网络共享打印机无法连接的问题,尤其是Win10最常遇见,打印机后提示“windows无法连接到打印机0x0000011b”错误。下面系统之家小编给大家带来0x0000011b共享打印机无法连接解决方法。一起来看看吧。  0x0000011b共享打印机无法连接解决方法  卸载补丁  打开设置——>更新和安全—->Windows更新—->“查看更新历史记录—->卸载更新  Win10更新2021年9月补丁后导致的,共享打印机.

    2025年9月7日
    6
  • AMEYA360讲解电子元器件代理怎么做

    AMEYA360讲解电子元器件代理怎么做时代的发展可以带来的优势很多,对很多行业也都有很多方面的支持,可以提供多方面的发展支持,展现出来的优势也是很多的,而提到了电子元器件的使用,也是现在很专业的设备的使用,因此要关注的内容非常多。在国内想要购买到质量高的国外电子元器件的话,是可以选择电子元器件代理购买的,那么要如何选择?具体操作的内容是什么?1、考虑到合法代理不论在什么情况下,想要选择到专业可靠的电子元器件代理的话,那么的肯定都是要首选合法的代理结构才可以的,只有这样才可以顺利的保证各种服务的质量,也不会销售各种残次品,保证了产品的

    2022年6月18日
    32
  • idea构建springboot_jsp项目搭建过程

    idea构建springboot_jsp项目搭建过程SpringBoot项目相对SpringMVC项目有搭建迅速,配置更少的优点。创建springboot项目有很多种方式,本文使用idea创建一个整合mongoDB和mysql数据库的简单的springboot项目。文章末尾附源码地址。搭建步骤:主要是以截图的方式介绍搭建过程。进入新建项目界面,按照下图操作经过以上步骤,基本项目框架就会搭建起来。因为项目中需要用到阿里的数据库连接池和jso

    2025年10月27日
    4
  • 另外一个进程已经为dpkg frontend 加锁_oracle数据库重启步骤

    另外一个进程已经为dpkg frontend 加锁_oracle数据库重启步骤一、问题描述  平时喜欢边听歌边敲代码(有种拯救世界的感觉),windows时一直用网易云,换了linux非常不方便,所以想给我的ubuntu(16.04)装一个。去官网找了一下,还真有linux版的,还特别标明是ubuntu16.04(64位),良心软件啊,接下来就是载下来按部就班安装了。  载下来是.deb格式的,需要用以下命令:dpkg-i&amp;amp;amp;lt;软件名.deb&amp;amp;amp;gt;…

    2022年10月6日
    2
  • tabnine激活码-激活码分享

    (tabnine激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~V…

    2022年3月22日
    86
  • 路由器VLAN对接交换机VLAN_交换机vlan指的是什么

    路由器VLAN对接交换机VLAN_交换机vlan指的是什么**浅谈路由器,交换机,集线器,vlan作用**二层交换机:基于MAC有MAC单播无MAC洪泛路由器的作用:1.路由(为其所承载的数据路由)—选路2.不同步广播域隔离广播域3.不同网络类型通讯交换机的作用:1.取代集线器2.也提供接口连接更多的用户3.基于MAC地址单播通讯4.理论上无限延长传输距离5.对二进制进行存储存储数据集线器(一层设备物理层…

    2022年8月10日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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