LINUX-Writing the kernel console driver







  Writing the kernel console driver





1.寫一個核心控制台(console)驅動程式


  首先,核心需要一個非常簡單的序列驅動程式,它只允許輸出字元.為了盡可能的簡化它應該沒有中斷跟DMA.當核心使用printk()時他應該可以顯示任何訊息


  設一顆晶片叫做fic81xx建立 drivers/serial/fic81xx.c .在這個檔案需先引入下列檔案


  #include <linux/console.h>


  #include <linux/tty.h>


  #include <linux/tty_flip.h>


  #include <linux/serial.h>


  #include <linux/serial_core.h>






2.你必須寫兩個函式


  - static void foo_console_write(struct console *co, const char *s, unsigned count)


  可以寫入一些字串到序列埠,這裡建議使用輪詢代替中斷


  {


  1.除能中斷並儲存中斷設定值


  2.傳送字串(使用輪詢判斷是否字元已傳完並須判斷是否為行尾\n)


  3.等待傳完後回存中斷設定值


  }





- static __init int foo_console_setup(struct console *co, char *options),


  根據 options 給的設定來初始序列埠硬體.






3.現在寫一些console所需的結構





為了宣告新的console裝置 ,你的核心程式必須先引入<linux/console.h>.這裡定義了結構struct console 跟一些需要的旗標





struct console


  {


  char name[8]; //控制台裝置的名稱


  void (*write)(struct console *, const char *, unsigned); //列印核心訊息


  int (*read)(struct console *, const char *, unsigned); //


  kdev_t (*device)(struct console *);


  void (*unblank)(void); //the function, if defined, is used to unblank the screen


  int (*setup)(struct console *, char *); //


  short flags;


  short index; //the number of the device acting as a console in an array of devices


  int cflag;


  struct console *next;


  };






控制台(console)旗標


  The flags field is a bitmask, and linux-2.4 defines three flags:


  CON_ENABLED: the flag is used to tell whether or not this console device is enabled by default.


  As shown in listing 1, only enabled console devices receive kernel messages. The flag is set automatically by the system if one of the kernel command line options selects this console and setup and is successful. Moreover, the flag is set for the first console device that registers itself. That's why neither the serial nor the parallel consoles don't set the flag by default in struct console (see listing 2 and 2a): no serial or parallel port acts as a console by default unless the user asks for a serial or parallel console on the kernel command line or there exists no virtual-terminal device that can act as a default console.


  CON_PRINTBUFFER: the flags requests buffered messages to be dumped to this console device. The serial and parallel consoles, for example, display all kernel boot messages because of this flag: when a serial or parallel console is registered the flag is set, and the kernel immediately dumps all kernel messages that were printed earlier.


  CON_CONSDEV: the console asks to be the preferred console device. The preferred console device is placed first in the console list.






如下範例設定





static struct console sercons = {


  name: "ttyS",


  write: serial_console_write,


  device: serial_console_device,


  wait_key: serial_console_wait_key,


  setup: serial_console_setup,


  flags: CON_PRINTBUFFER,


  index: -1,


  };





ex;


  static struct console fic8120_console = {


  .name = "ttyS",


  .write = fic8120_console_write,


  // .read =


  .device = fic8120_console_device,


  // .unblank =


  .setup = fic8120_console_setup,


  .flags = CON_PRINTBUFFER,


  .index = -1,


  // .cflag =


  // .next =


  };







初始註冊一個console serial driver


  EX:


  void __init fic8120_console_init(void)


  {


  printk(KERN_INFO "FIC8120: serial console init\n");


  register_console(&fic8120_console);


  }



 


  參考文件





Serial">http://www.linux-mips.org/wiki/Serial_Driver_and_Console#Writing_the_kernel_console_driver">Serial Driver and Console





Serial">http://www.linux.it/%7Erubini/docs/sercons/sercons.html">Serial Consoles and Consoles in General


arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()