Linux HAL (Hardware Abstraction Layer)的工作原理

Linux HAL (Hardware Abstraction Layer)的工作原理

                      

转载时请注明出处和作者联系方式:http://blog.csdn.net/absurd

作者联系方式:李先静<xianjimli at hotmail dot com>

更新时间:2007-5-3

 

HALHardware Abstraction Layer的首字母缩写。我最早是在Winnt 3.5的帮助中知道这个名词的,对帮助文档中的说法我比较认同,所以一直对它抱有好感。不过Windows下的HALLinux下的HAL两者所指并非相同之物:

 

Windows下的HAL位于操作系统的最底层,直接操作物理硬件,隔离与硬件相关的信息,为上层的操作系统和设备驱动程序提供一个统一的接口,起到对硬件的抽象作用。有了HAL,编写驱动程序就容易多了,因为HAL的接口不但使用简单,而且具有更好的可移植性(没用过)。

 

Linux 下的HAL:至于对硬件的抽象,Linux内核早就有类似机制,只不过没有专门的名称罢了。而LinuxHAL指的并非这个,它不是位于操作系统的最底层,直接操作硬件,相反,它位于操作系统和驱动程序之上,是一个运行在用户空间中服务程序。

 

我们知道,Linux和所有的Unix一样,习惯用文件来抽象设备,任何设备都是一个文件,比如/dev/mouse是鼠标的设备文件。这种方法看起来不错,每个设备都有统一的形式,但使用并不那么容易,设备文件名没有什么规范,从简单的一个文件名,你无法得知它是什么设备,具有有什么特性。

 

结果形成这样的尴尬:有了设备和设备驱动程序,却不知道如何使用它。这些乱七八糟的设备文件,让设备的管理和应用程序的开发都变得很麻烦,所以有必要提供一个硬件抽象层,来为上层应用程序提供一个统一的接口,LinuxHAL就这样应运而生了。

 

HAL并不提供诸如拍照和刻录等之类的功能,相反它只是告诉应用程序,系统中有哪些设备可用,以及这些设备的类型、特性和能力等。主要说来,它提供以下几项功能:

1.         获取指定类型的设备列表。

2.         获取/更改设备的属性值。

3.         获取设备具有的能力描述。

4.         设备插入/拔除时,通知相关应用程序。

5.         设备属性或能力变化时,通知相关应用程序。

 

udev创建dev下的文件结点,加载驱动程序,让设备处于可用状态。而HAL则告诉应用程序,现在有哪些设备可用,这些设备的类型、特性和能力,让应用程序知道如何使用它们。

 

设备的属性管理是HAL最重要任务之一,有的设备属性来源于实际的硬件,有的来源于设备信息文件(/usr/share/hal/fdi/),有的来源其它配置信息(/usr/share/hwdata/)。设备属性的都有标准的定义,这些属性定义是HALSPEC的主要内容之一,可以参考http://people.freedesktop.org/~david/hal-spec/hal-spec.html

 

HAL作为一个后台服务程序运行,它的主体架构基于MVC的模型,在DBUS的帮助下,实现了异步事件通知机制。HAL的分层视图如下:

 

说明:

1.         实线箭头为主动调用,虚线箭头为事件上报。

 

2.         udev通过NetLink注册内核的设备事件,当有设备插入/拔除时,udev就会收到通知,它会从事件中所带参数和sysfs中的信息,加载适当的驱动程序,创建dev下的结点,让设备处于可用的状态。

 

3.         udev只是一个框架,它的行为完全受它的规则所控制,这些规则存放在目录/etc/udev/rules.d/中,其中90-hal.rules是用来让udev把设备插入/拔除的事件通过socket socket:/org/freedesktop/hal/udev_event转发给HAL的。

 

4.         HAL挂在socket:/org/freedesktop/hal/udev_event上等待事件,有事件发生时就调用函数hald_udev_data处理,它先从事件中取出主要参数,创建一个hotplug_event对象,把它放入事件队列中,然后调用hotplug_event_process_queue处理事件。

 

5.         函数hotplug_event_begin负责具体事件的处理,它把全部事件分为四类,并分别处理hotplug_event_begin_sysfs处理普通设备事件,hotplug_event_begin_acpi处理ACPI事件,hotplug_event_begin_apm处理APM事件,hotplug_event_begin_pmu处理PMU事件。要注意的是,后三者的事件源并非源于udev,而是在device_reprobe时触发的(osspec_device_reprobe/hotplug_reprobe_tree/hotplug_reprobe_generate_add_events/acpi_generate_add_hotplug_event)

 

6.         函数hotplug_event_begin_sysfs中,如果是插入设备,则创建一个设备对象,设置设备的属性,调用相关callouts,然后放入设备列表中,并触发signaldbus通知相关应用程序。如果是拔除设备,则调用相关callouts,然后从设备列表中删除,并触发signaldbus通知相关应用程序。

 

7.         应用程序可以主动调用HAL提供的DBUS接口函数,这些函数在libhal.h中有定义。应用程序也可以注册HALsignal,当设备变化时,HAL通过DBUS上报事件给应用程序。

 

8.         calloutHAL一种扩展方式,它在设备插入/拔除时执行。可以在设备信息文件中(/usr/share/hal目录)指定。

 

9.         addon也是HAL一种扩展方式,它与callout的不同之处在于addon往往是事件的触发者,而不是事件的消费者。HAL的事件源主要源于udev,而udev源于kernelhotplug,然而有的设备如电源设备、磁盘设备和特殊按键等,它们并不产生hotplug事件。HAL就得不到通知,怎么办呢,addon就是用于支持新事件源的扩展方式。比如addon-acpi/proc/acpi/event或者/var/run/acpid.socket收到事件,然后转发成HAL事件。addon-storage检测光盘或磁盘的状态,并设置设备的属性。addon-keyboard检测一些特殊按键,并触发相应事件。

 

access-check/ci-tracker/ck-tracker负责权限的检查,里面提到的PolicyKit/ConsoleKit不是太熟悉,有时间再看看。

 

简单的说,HAL就是一个设备数据库,它管理当前系统中所有的设备,你可以以多种灵活的方式去查询这些设备,可以获取指定设备的特性,可以注册设备变化事件。

 

~~end~~

 

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
xv Contents Graphical Debugging Tools 299 DDD 299 Eclipse 302 Kernel Debugging 305 Don’t Panic! 306 Making Sense of an oops 307 Using UML for Debugging 309 An Anecdotal Word 312 A Note about In-Kernel Debuggers 313 Summary 313 Chapter 11: The GNOME Developer Platform 315 GNOME Libraries 316 Glib 316 GObject 316 Cairo 316 GDK 317 Pango 317 GTK+ 317 libglade 318 GConf 318 GStreamer 318 Building a Music Player 319 Requirements 319 Getting Started: The Main Window 319 Building the GUI 321 Summary 340 Chapter 12: The FreeDesktop Project 341 D-BUS: The Desktop Bus 341 What Is D-Bus? 342 Under D-Hood of D-Bus 342 D-Bus Methods 346 Hardware Abstraction Layer 350 Making Hardware Just Work 350 Hal Device Objects 353 The Network Manager 358 Other Freedesktop Projects 360 Summary 360 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xv xvi Contents Chapter 13: Graphics and Audio 361 Linux and Graphics 361 X Windows 362 Open Graphics Library 364 OpenGL Utilities Toolkit 365 Simple Directmedia Layer 365 Writing OpenGL Applications 365 Downloading and Installing 366 Programming Environment 367 Using the GLUT Library 368 Writing SDL Applications 382 Downloading and Installing 382 Programming Environment 383 Using the SDL Library 383 Summary 394 Chapter 14: LAMP 395 What Is LAMP? 395 Apache 396 MySQL 396 PHP 397 The Rebel Platform 397 Evaluating the LAMP Platform 397 Apache 399 Virtual Hosting 400 Installation and Configuration of PHP 5 401 Apache Basic Authentication 402 Apache and SSL 402 Integrating SSL with HTTP Authentication 403 MySQL 404 Installing MySQL 404 Configuring and Starting the Database 404 Changing the Default Password 405 The MySQL Client Interface 405 Relational Databases 405 SQL 406 The Relational Model 409 PHP 411 The PHP Language 411 Error Handling 420 Error-Handling Exceptions 421 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xvi xvii Contents Optimization Techniques 422 Installing Additional PHP Software 427 Logging 427 Parameter Handling 428 Session Handling 429 Unit Testing 430 Databases and PHP 432 PHP Frameworks 432 The DVD Library 433 Version 1: The Developer’s Nightmare 433 Version 2: Basic Application with DB-Specific Data Layer 434 Version 3: Rewriting the Data Layer,Adding Logging and Exceptions 437 Version 4: Applying a Templating Framework 441 Summary 442 Index 443 GNU 47 Acknowledgments ix Introduction xix Chapter 1: Working with Linux 1 A Brief History of Linux 2 The GNU Project 2 The Linux Kernel 3 Linux Distributions 4 Free Software vs. Open Source 4 Beginning Development 5 Choosing a Linux Distribution 6 Installing a Linux Distribution 8 Linux Community 15 Linux User Groups 15 Mailing lists 16 IRC 16 Private Communities 16 Key Differences 16 Linux Is Modular 17 Linux Is Portable 17 Linux Is Generic 17 Summary 18 Chapter 2: Toolchains 19 The Linux Development Process 19 Working with Sources 20 Configuring to the Local Environment 21 Building the Sources 22 Components of the GNU Toolchain 23 The GNU Compiler Collection 23 The GNU binutils 34 GNU Make 39 The GNU Debugger 40 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xi xii Contents The Linux Kernel and the GNU Toolchain 44 Inline Assembly 44 Attribute Tags 45 Custom Linker Scripts 45 Cross-Compilation 46 Building the GNU Toolchain 47 Summary 48 Chapter 3: Portability 49 The Need for Portability 50 The Portability of Linux 51 Layers of Abstraction 51 Linux Distributions 52 Building Packages 57 Portable Source Code 70 Internationalization 81 Hardware Portability 88 64-Bit Cleanliness 89 Endian Neutrality 89 Summary 92 Chapter 4: Software Configuration Management 93 The Need for SCM 94 Centralized vs. Decentralized Development 95 Centralized Tools 95 The Concurrent Version System 96 Subversion 104 Decentralized tools 108 Bazaar-NG 109 Linux kernel SCM (git) 112 Integrated SCM Tools 115 Eclipse 115 Summary 117 Chapter 5: Network Programming 119 Linux Socket Programming 119 Sockets 120 Network Addresses 122 Using Connection-Oriented Sockets 123 Using Connectionless Sockets 130 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xii xiii Contents Moving Data 133 Datagrams vs. Streams 133 Marking Message Boundaries 137 Using Network Programming Libraries 140 The libCurl Library 140 Using the libCurl Library 141 Summary 147 Chapter 6: Databases 149 Persistent Data Storage 149 Using a Standard File 150 Using a Database 150 The Berkeley DB Package 152 Downloading and Installing 153 Building Programs 154 Basic Data Handling 154 The PostgreSQL Database Server 165 Downloading and Installing 165 Building Programs 167 Creating an Application Database 167 Connecting to the Server 169 Executing SQL Commands 173 Using Parameters 181 Summary 184 Chapter 7: Kernel Development 185 Starting Out 185 Kernel Concepts 199 A Word of Warning 200 The Task Abstraction 200 Virtual Memory 205 Don’t Panic! 208 Kernel Hacking 208 Loadable Modules 209 Kernel Development Process 211 Git: the “Stupid Content Tracker” 212 The Linux Kernel Mailing List 213 The “mm” Development Tree 215 The Stable Kernel Team 215 LWN: Linux Weekly News 216 Summary 216 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xiii xiv Contents Chapter 8: Kernel Interfaces 217 What Is an Interface? 217 Undefined Interfaces 218 External Kernel Interfaces 219 System Calls 219 The Device File Abstraction 224 Kernel Events 238 Ignoring Kernel Protections 239 Internal Kernel Interfaces 243 The Kernel API 243 The kernel ABI 244 Summary 245 Chapter 9: Linux Kernel Modules 247 How Modules Work 247 Extending the Kernel Namespace 250 No Guaranteed Module Compatibility 251 Finding Good Documentation 251 Linux Kernel Man Pages 251 Writing Linux Kernel Modules 252 Before You Begin 253 Essential Module Requirements 253 Logging 256 Exported Symbols 257 Allocating Memory 259 Locking considerations 267 Deferring work 275 Further Reading 283 Distributing Linux Kernel Modules 284 Going Upstream 284 Shipping Sources 284 Shipping Prebuilt Modules 284 Summary 285 Chapter 10: Debugging 287 Debugging Overview 287 A Word about Memory Management 288 Essential Debugging Tools 289 The GNU Debugger 289 Valgrind 298 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xiv

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值