linux 混雜裝置模型

來源:互聯網
上載者:User

聲明:本文章摘至開源中國社區liufazhang的部落格

在Linux系統中,存在一類字元裝置,他們共用一個主裝置號(10),但此裝置號不同,我們稱這類裝置為混雜裝置(miscdeivce),查看/proc/device中可以看到一個名為misc的主裝置號為10.所有的混雜裝置形成一個鏈表,對裝置訪問時核心根據次裝置號找到對應的miscdevice裝置。

Linux核心使用struct miscdeivce來描述一個混雜裝置

struct miscdevice  {

    int minor;

    const char *name;

    const struct file_operations *fops;

    struct list_head list;

    struct device *parent;

    struct device *this_device;

    const char *nodename;

    mode_t mode;

};

minor是這個混雜裝置的次裝置號,若由系統自動設定,則可以設定為MISC_DYNANIC_MINOR,name是裝置名稱.使用時只需填寫minor次裝置號,*name裝置名稱,*fops檔案操作函數集即可。

Linux核心使用misc_register函數註冊一個混雜裝置,使用misc_deregister移除一個混雜裝置。註冊成功後,linux核心為自動為該裝置建立裝置節點,在/dev/下會產生相應的節點。

 註冊函數:

int misc_register(struct miscdevice * misc)

輸入參數:struct miscdevice

傳回值:

   表示註冊成功。

負數 表示未成功。

卸載函數:

int  misc_deregister(struct miscdevice *misc)

0 表示成功。

負數 表示失敗。

下面是基於linux 混雜裝置模型的一個簡單樣本:

view sourceprint?01 #include <linux/module.h> 

02 #include <linux/kernel.h> 

03 #include <linux/types.h> 

04 #include <linux/fs.h> 

05 #include <linux/init.h> 

06 #include <linux/delay.h> 

07 #include <asm/uaccess.h> 

08 #include <asm/irq.h> 

09 #include <asm/io.h> 

10 #include <linux/cdev.h> 

11 #include <linux/device.h> 

12 #include <linux/miscdevice.h> 

13 #define DEVICE_NAME "BELL-Control" 

14 volatile unsigned long *gpbcon = NULL ; 

15 volatile unsigned long *gpbdat = NULL ;   

16 volatile unsigned long *gpbup = NULL ; 

17 static int bell_drv_open(struct inode *inode, struct file *file) 

18 { 

19      *gpbcon &= ~(0x3<<(0*2)); 

20      *gpbcon |= (0x1<<(0*2)) ;  

21      *gpbup = 0x0; 

22       printk("first_drv_open\n"); 

23       return 0; 

24 } 

25   

26 static ssize_t bell_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) 

27 { 

28       int val;    

29       copy_from_user(&val, buf, count) ;     

30       if (val == 1) 

31       { 

32        *gpbdat &= ~(1<<0); 

33          printk("first_drv_write 1111111\n"); 

34       } 

35       else

36       { 

37   

38          *gpbdat |= (1<<0); 

39           printk("first_drv_write 00000\n"); 

40       }   

41      return 0; 

42 } 

43   

44 static struct file_operations bell_drv_fops = { 

45      .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模組時自動建立的__this_module變數 */

46      .open   =   bell_drv_open,      

47     .write = bell_drv_write,     

48 }; 

49  //註冊混雜裝置,每一個misc驅動會自動出現在/sys/class/misc/下 

50 static struct miscdevice misc = { 

51       .minor =MISC_DYNAMIC_MINOR, 

52       .name = DEVICE_NAME, 

53       .fops = &bell_drv_fops, 

54 }; 

55   

56 static int bell_drv_init(void) 

57 { 

58      int result; 

59      result = misc_register(&misc);    

60      printk (""DEVICE_NAME" initialized\n");  

61      gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16); 

62      gpbdat = gpbcon + 1; 

63      gpbup = gpbcon + 2; 

64      return 0;  

65 } 

66   

67 static void bell_drv_exit(void) 

68 { 

69       misc_deregister(&misc); 

70       iounmap(gpbcon); 

71 } 

72   

73 module_init(bell_drv_init); 

74 module_exit(bell_drv_exit); 

75 MODULE_LICENSE("GPL"); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.