1. gzyueqian
      13352868059

      Linux驅(qū)動(dòng)入門(mén)

      更新時(shí)間: 2005-11-25 00:00:00來(lái)源: 粵嵌教育瀏覽量:5668

      內(nèi)核版本: 2.4.22
      閱讀此文的目的: 學(xué)會(huì)編寫(xiě)Linux設(shè)備驅(qū)動(dòng)。
      閱讀此文的方法: 閱讀以下2個(gè)文件: hello.c,asdf.c。
      此文假設(shè)讀者:
      已經(jīng)能用C語(yǔ)言編寫(xiě)Linux應(yīng)用程序,
      理解"字符設(shè)備文件, 塊設(shè)備文件, 主設(shè)備號(hào), 次設(shè)備號(hào)",
      會(huì)寫(xiě)簡(jiǎn)單的Shell腳本和Makefile。

      1. "hello.c"
      --------------------------------
      /*
      * 這是我們的個(gè)源文件,
      * 它是一個(gè)可以加載的內(nèi)核模塊,
      * 加載時(shí)顯示"Hello,World!",
      * 卸載時(shí)顯示"Bye!"。

      * 需要說(shuō)明一點(diǎn),寫(xiě)內(nèi)核或內(nèi)核模塊不能用寫(xiě)應(yīng)用程序時(shí)的系統(tǒng)調(diào)用或函數(shù)庫(kù),
      * 因?yàn)槲覀儗?xiě)的就是為應(yīng)用程序提供系統(tǒng)調(diào)用的代碼。

      * 內(nèi)核有專(zhuān)用的函數(shù)庫(kù),如 等,
      * 現(xiàn)在還沒(méi)必要了解得很詳細(xì),
      * 這里用到的printk的功能類(lèi)似于printf。

      * "/usr/src/linux"是你實(shí)際的內(nèi)核源碼目錄的一個(gè)符號(hào)鏈接,
      * 如果沒(méi)有現(xiàn)在就創(chuàng)建一個(gè),因?yàn)橄旅婧鸵院蠖紩?huì)用到。

      * 編譯它用"gcc -c -I/usr/src/linux/include hello.c",
      * 如果正常會(huì)生成文件hello.o,

      * 加載它用"insmod hello.o",
      * 只有在文本終端下才能看到輸出。

      * 卸載它用"rmmod hello"
      */

      /*
      * 小技巧: 在用戶(hù)目錄的.bashrc里加上一行:
      * alias mkmod='gcc -c -I/usr/src/linux/include'
      * 然后重新登陸Shell,
      * 以后就可以用"mkmod hello.c"的方式來(lái)編譯內(nèi)核模塊了。
      */

      /* 開(kāi)始例行公事 */
      #ifndef __KERNEL__
      # define __KERNEL__
      #endif
      #ifndef MODULE
      # define MODULE
      #endif

      #include 
      #include 

      MODULE_LICENSE("GPL");
      #ifdef CONFIG_SMP
      #define __SMP__
      #endif
      /* 結(jié)束例行公事 */

      #include   /* printk()在這個(gè)文件里 */

      static int
      init_module
      (){
      printk("Hello,World! ");
      return 0; /* 如果初始工作失敗,就返回非0 */
      }

      static void
      cleanup_module
      (){
      printk("Bye! ");
      }
      ------------------------------------

      2. "asdf.c"
      ------------------------------------
      /*
      * 這個(gè)文件是一個(gè)內(nèi)核模塊。
      * 內(nèi)核模塊的編譯,加載和卸載在前面已經(jīng)介紹了。

      * 這個(gè)模塊的功能是,創(chuàng)建一個(gè)字符設(shè)備。
      * 這個(gè)設(shè)備是一塊4096字節(jié)的共享內(nèi)存。
      * 內(nèi)核分配的主設(shè)備號(hào)會(huì)在加載模塊時(shí)顯示。
      */

      /* 開(kāi)始例行公事 */
      #ifndef __KERNEL__
      # define __KERNEL__
      #endif
      #ifndef MODULE
      # define MODULE
      #endif

      #include 
      #include 

      #ifdef CONFIG_SMP
      #define __SMP__
      #endif
      MODULE_LICENSE("GPL");
      /* 結(jié)束例行公事 */

      #include  /* copy_to_user(), copy_from_user */
      #include   /* struct file_operations, register_chrdev(), ... */
      #include   /* printk()在這個(gè)文件里 */
      #include   /* 和任務(wù)調(diào)度有關(guān) */
      #include   /* u8, u16, u32 ... */

      /*
      * 關(guān)于內(nèi)核功能庫(kù),可以去網(wǎng)上搜索詳細(xì)資料,
      */

      /* 文件被操作時(shí)的回調(diào)功能 */
      static int asdf_open (struct inode *inode, struct file *filp);
      static int asdf_release (struct inode *inode, struct file *filp);
      static ssize_t asdf_read (struct file *filp, char *buf, size_t count,loff_t *f_pos);
      static ssize_t asdf_write (struct file *filp, const char *buf, size_t count,loff_t *f_pos);
      static loff_t asdf_lseek (struct file * file, loff_t offset, int orig);

      /* 申請(qǐng)主設(shè)備號(hào)時(shí)用的結(jié)構(gòu), 在linux/fs.h里定義 */
      struct file_operations asdf_fops = {
      open: asdf_open,
      release: asdf_release,
      read: asdf_read,
      write: asdf_write,
      llseek: asdf_lseek,
      };

      static int asdf_major; /* 用來(lái)保存申請(qǐng)到的主設(shè)備號(hào) */
      static u8 asdf_body[4096]="asdf_body "; /* 設(shè)備 */

      static int
      init_module
      (){
      printk ("Hi, This' A Simple Device File! ");
      asdf_major = register_chrdev (0, "A Simple Device File", &asdf_fops); /* 申請(qǐng)字符設(shè)備的主設(shè)備號(hào) */
      if (asdf_major < 0) return asdf_major; /* 申請(qǐng)失敗就直接返回錯(cuò)誤編號(hào) */
      printk ("The major is:%d ", asdf_major); /* 顯示申請(qǐng)到的主設(shè)備號(hào) */
      return 0; /* 模塊正常初始化 */
      }

      static void
      cleanup_module
      (){
      unregister_chrdev(asdf_major, "A Simple Device File"); /* 注銷(xiāo)以后,設(shè)備就不存在了 */
      printk("A Simple Device has been removed,Bye! ");
      }

      /*
      * 編譯這個(gè)模塊然后加載它,
      * 如果正常,會(huì)顯示你的設(shè)備的主設(shè)備號(hào)。
      * 現(xiàn)在你的設(shè)備就建立好了,我們可以測(cè)試一下。
      * 假設(shè)你的模塊申請(qǐng)到的主設(shè)備號(hào)是254,
      * 運(yùn)行"mknod abc c 254 0",就建立了我們的設(shè)備文件abc。
      * 可以把它當(dāng)成一個(gè)4096字節(jié)的內(nèi)存塊來(lái)測(cè)試一下,
      * 比如"cat abc", "cp abc image", "cp image abc",
      * 或?qū)憥讉€(gè)應(yīng)用程序用它來(lái)進(jìn)行通訊。

      * 介紹一下兩個(gè)需要注意的事,
      * 一是printk()的顯示只有在非圖形模式的終端下才能看到,
      * 二是加載過(guò)的模塊在不用以后卸載掉。

      * 如果對(duì)Linux環(huán)境的系統(tǒng)調(diào)用很陌生,建議先看APUE這本書(shū)。
      */

      static int
      asdf_open /* open回調(diào) */
      (
      struct inode *inode,
      struct file *filp
      ){
      printk("^_^ : open %s  ",
      current->comm);
      /*
      * 應(yīng)用程序的運(yùn)行環(huán)境由內(nèi)核提供,內(nèi)核的運(yùn)行環(huán)境由硬件提供。
      * 這里的current是一個(gè)指向當(dāng)前進(jìn)程的指針,
      * 現(xiàn)在沒(méi)必要了解current的細(xì)節(jié)。
      * 在這里,當(dāng)前進(jìn)程正打開(kāi)這個(gè)設(shè)備,
      * 返回0表示打開(kāi)成功,內(nèi)核會(huì)給它一個(gè)文件描述符。
      * 這里的comm是當(dāng)前進(jìn)程在Shell下的command字符串。
      */
      return 0;
      }

      static int
      asdf_release /* close回調(diào) */
      (
      struct inode *inode,
      struct file *filp
      ){
      printk("^_^ : close  ");
      return 0;
      }

      static ssize_t
      asdf_read /* read回調(diào) */
      (
      struct file *filp,
      char *buf,
      size_t count,
      loff_t *f_pos
      ){
      loff_t pos;
      pos = *f_pos; /* 文件的讀寫(xiě)位置 */
      if ((pos==4096) || (count>4096)) return 0; /* 判斷是否已經(jīng)到設(shè)備尾,或?qū)懙拈L(zhǎng)度超過(guò)設(shè)備大小 */
      pos += count;
      if (pos > 4096) {
      count -= (pos - 4096);
      pos = 4096;
      }
      if (copy_to_user(buf, asdf_body+*f_pos, count)) return -EFAULT; /* 把數(shù)據(jù)寫(xiě)到應(yīng)用程序空間 */
      *f_pos = pos; /* 改變文件的讀寫(xiě)位置 */
      return count; /* 返回讀到的字節(jié)數(shù) */
      }

      static ssize_t
      asdf_write /* write回調(diào),和read一一對(duì)應(yīng) */
      (
      struct file *filp,
      const char *buf,
      size_t count,
      loff_t *f_pos
      ){
      loff_t pos;
      pos = *f_pos;
      if ((pos==4096) || (count>4096)) return 0;
      pos += count;
      if (pos > 4096) {
      count -= (pos - 4096);
      pos = 4096;
      }
      if (copy_from_user(asdf_body+*f_pos, buf, count)) return -EFAULT;
      *f_pos = pos;
      return count;
      }

      static loff_t
      asdf_lseek /* lseek回調(diào) */
      (
      struct file * file,
      loff_t offset,
      int orig
      ){
      loff_t pos;
      pos = file->f_pos;
      switch (orig) {
      case 0:
      pos = offset;
      break;
      case 1:
      pos += offset;
      break;
      case 2:
      pos = 4096+offset;
      break;
      default:
      return -EINVAL;
      }
      if ((pos>4096) || (pos<0)) {
      printk("^_^ : lseek error %d ",pos);
      return -EINVAL;
      }
      return file->f_pos = pos;
      }

      免費(fèi)預(yù)約試聽(tīng)課

      亚洲另类欧美综合久久图片区_亚洲中文字幕日产无码2020_欧美日本一区二区三区桃色视频_亚洲AⅤ天堂一区二区三区

      
      

      1. 日韩精品久久一区二区三区 | 久久极品免费视频 | 五月天在线精品电影 | 日本亚洲国产一区二区三区 | 一本一道a√无碼中文字幕 亚洲无毒AV在线 | 亚洲人成网最新在线 |