2010年4月24日 星期六

Linux Driver Hello World

Linux Driver 4 case study Driver 簡單的說就是Software Hardware control. Software 端 Handle application Hardware 端 Handle Device 假設現在要用到USB 來存取 ( "Read", "Write"...) Data, 就必須要有USB 的Driver 來驅動 USB 的硬體裝置,把Data寫入記憶體某個區塊的位置. Ex: 簡單的說 Driver 就是把 Software 轉成 Hardware 看的懂的control Software part: USB->Write(unsigned int address,unsigned int data ); Hardware part: bin 0x00ed001c-o0oocdf1; 00ed001 : address; o0oocdf1 : data 而每個Driver會對 Kernel註冊,藉由Kernel來Handle Program 2 Driver / Driver 2 Program. Kernel 指的是一個提供硬體抽象層, 磁碟及檔案系統控制, 多工等功能的系統軟體,包含了驅動主機各項硬體的偵測程式與驅動模組.

The main features of a driver are:

  • It performs input/output (I/O) management.

  • It provides transparent device management, avoiding low-level programming (ports).

  • It increases I/O speed, because usually it has been optimized.

  • It includes software and hardware error management.

  • It allows concurrent access to the hardware by several processes

Linux Driver Ref: http://www.linuxjournal.com/article/2476?page=0,0 Kernel Ref: http://linux.vbird.org/linux_basic/0540kernel.php#intro_whatiskernel 底下用最簡單的 Hello World 當例子 init : initial 驅動初始化 module : 為我們要對Kernel 註冊的模組,定義value, structure. kernel : 為Linux 的核心中的模型和定義 module_init : 為我們的進入點,有點像 main function. module_exit: 為出場點 Path : \include\linux\Init.h hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int hello_init(void) {
  printk("<1> Hello world!\n");
  return 0;
}

static void hello_exit(void) {
  printk("<1> Bye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
Makefile
obj-m += hello.o

all:
 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
command line %make %modinfo hello*.ko result filename: hello.ko srcversion: C5BB518CD1B8222581499DF depends: vermagic: 2.6.31-20-generic SMP mod_unload modversions 586 Ref: http://www.tldp.org/LDP/lkmpg/2.6/html/

3 則留言:

  1. useful Linux command line
    http://www.pixelbeat.org/cmdline.html

    回覆刪除
  2. vim ref

    http://man.chinaunix.net/newsoft/vi/doc/quickref.html#quickref

    回覆刪除
  3. Linux driver 詳述

    http://blogimg.chinaunix.net/blog/upfile2/080624202201.pdf

    回覆刪除