亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4131 | 回復(fù): 1
打印 上一主題 下一主題

用一個(gè).c文件來(lái)理解bus,driver,device,sysfs的關(guān)系和driver&device的bind過(guò)程 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-09-21 17:48 |只看該作者 |倒序?yàn)g覽
忙了一個(gè)下午,調(diào)成功了一個(gè)module.寫(xiě)出來(lái)可能對(duì)驅(qū)動(dòng)新手有一定幫助。我本人就是新手,所以從源碼中理解這些概念花了一定的功夫。
以下module可以驗(yàn)證device,driver,bus的關(guān)系及在sysfs的體系,及bus先match,driver再probe的過(guò)程。
同時(shí)追求代碼最小化,可見(jiàn)bus,device,driver等對(duì)象在注冊(cè)時(shí)哪些信息是必須的。

  1. #include <linux/module.h>
  2. #include <linux/kobject.h>
  3. #include <linux/device.h>

  4. /*未實(shí)現(xiàn)設(shè)備文件創(chuàng)建和文件系統(tǒng)驅(qū)動(dòng)*/

  5. static int my_bus_match(struct device* device, struct device_driver* driver);
  6. static int my_driver_probe(struct device* dev);
  7. static void unregister_all(void);
  8. static int register_all(void);

  9. MODULE_LICENSE("GPL");

  10. struct bus_type my_bus_type =
  11. {
  12.   .name = "my_bus_type",
  13.   .match= my_bus_match
  14. };

  15. struct my_device
  16. {
  17.   int id;
  18.   struct device dev;
  19.   int drv_id;
  20. };

  21. struct my_device_driver
  22. {
  23. int id;
  24. struct device_driver drv;
  25. };

  26. struct my_device_driver my_driver_1 =
  27. {
  28.   .drv =
  29.   {
  30.     .name = "my_bus_driver_1",
  31.     .probe = my_driver_probe,
  32.     .bus = &my_bus_type,
  33.   },
  34.   .id = 1,
  35. };
  36. struct my_device_driver my_driver_2 =
  37. {
  38.   .drv =
  39.   {
  40.     .name = "my_bus_driver_2",
  41.     .probe = my_driver_probe,
  42.     .bus = &my_bus_type
  43.   },
  44.   .id = 2
  45. };

  46. struct my_device my_device_1 =
  47. {
  48.   .id=001,
  49.   .drv_id = 1,
  50.   .dev =
  51.   {
  52.     .bus = &my_bus_type,   
  53.     .bus_id = "my_bus_id_1"
  54.   }
  55.   
  56. };
  57. struct my_device my_device_2 =
  58. {
  59.   .id=002,
  60.   .drv_id = 2,
  61.   .dev =
  62.   {
  63.     .bus = &my_bus_type,
  64.     .bus_id = "my_bus_id_2"
  65.   }
  66. };


  67. static int device_match_driver(struct my_device* mydev, struct my_device_driver* mydrv)
  68. {
  69.   printk("one match tested: dev_id=%d, dev.drv_id=%d, drv_id=%d, drv_name=%s\n", mydev->id,
  70.          mydev->drv_id, mydrv->id, mydrv->drv.name );
  71.   if(mydev->drv_id != mydrv->id)
  72.        return 0;
  73.   return 1;
  74. }

  75. static int my_bus_match(struct device* device, struct device_driver* driver)
  76. {
  77.   struct my_device* mydev = container_of(device, struct  my_device, dev);
  78.   struct my_device_driver* mydrv = container_of(driver, struct my_device_driver, drv);
  79.   printk("my_bus_match called\n");
  80.   if(device_match_driver(mydev, mydrv))
  81.   {
  82.         printk("one matched group:dev_id=%d drv_id=%d\n", mydev->id, mydrv->id);
  83.         return 1;
  84.   }
  85.   printk("device %d and driver %d not match\n", mydev->id, mydrv->id);
  86.   return 0;
  87. }

  88. static int my_driver_probe(struct device* dev)
  89. {
  90.   struct my_device_driver* mydrv = container_of(dev->driver, struct my_device_driver, drv);
  91.   struct my_device* mydev = container_of(dev, struct my_device, dev);
  92.   printk("my_driver %s drive probe called\n", mydrv->drv.name);
  93.   if(device_match_driver(mydev, mydrv))
  94.       return 0;
  95.   return 1;
  96. }
  97. const int errExist = -17;
  98. int bus_is_registered = 0;
  99. static int register_all()
  100. {
  101.   int res;
  102.   res = bus_register(&my_bus_type);
  103.   if(res)
  104.   {
  105.      printk("register my_bus_type failed\n");
  106.      return res;
  107.   }

  108.   bus_is_registered = 1;

  109.    /*register driver 1*/
  110.    res = driver_register(&my_driver_1.drv);
  111.    if(res)
  112.    {
  113.       printk("register my_driver_1 failed\n");
  114.       return res;
  115.    }
  116.        
  117.    /*register driver 2*/
  118.    res  = driver_register(&my_driver_2.drv);  
  119.    if(res)
  120.    {
  121.       printk("register my_driver_2 failed\n");
  122.       return res;
  123.    }
  124.    
  125.    /*add device 1*/
  126.    res = device_register(&my_device_1.dev);
  127.    if(res)
  128.    {
  129.      printk("register my_device_1 failed\n");
  130.      return res;
  131.    }
  132.    res = device_register(&my_device_2.dev);
  133.    if(res)
  134.    {
  135.      printk("register my_device_2 failed\n");
  136.      return res;
  137.    }
  138.    return 0;
  139. }

  140. static int my_init(void)
  141. {
  142.   int res;
  143.   printk("my_init called\n");
  144.   if((res = register_all()))
  145.   {
  146.     if(res != errExist && bus_is_registered)
  147.        bus_unregister(&my_bus_type);
  148.   /* dangerous if call unregister_all();*/
  149.         printk("res=%d\n",res);

  150.     return res;
  151.   }
  152.   return 0;
  153. }

  154. static void unregister_all()
  155. {
  156. device_unregister(&my_device_1.dev);
  157. device_unregister(&my_device_2.dev);
  158. driver_unregister(&my_driver_1.drv);
  159. driver_unregister(&my_driver_2.drv);
  160. bus_unregister(&my_bus_type);
  161. }

  162. static void my_exit(void)
  163. {
  164. unregister_all();
  165. }

  166. module_init(my_init);
  167. module_exit(my_exit);
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-09-22 23:41 |只看該作者
:wink:
LDD 里面講得也不錯(cuò)~
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP