{"id":2109,"date":"2017-02-16T11:10:44","date_gmt":"2017-02-16T03:10:44","guid":{"rendered":"http:\/\/www.51cos.com\/?p=2109"},"modified":"2017-02-16T11:10:44","modified_gmt":"2017-02-16T03:10:44","slug":"bus%ef%bc%88%e6%80%bb%e7%ba%bf%ef%bc%89%e3%80%81driver%ef%bc%88%e9%a9%b1%e5%8a%a8%ef%bc%89%e3%80%81device%ef%bc%88%e8%ae%be%e5%a4%87%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=2109","title":{"rendered":"bus\uff08\u603b\u7ebf\uff09\u3001driver\uff08\u9a71\u52a8\uff09\u3001device\uff08\u8bbe\u5907\uff09"},"content":{"rendered":"<p>\u4e00\u6761bus\u4e0a\u6302\u7740\u8bb8\u591adevice\uff0c\u800cdevice\u9700\u8981driver\u624d\u80fd\u5de5\u4f5c\u3002\u5185\u6838\u6709\u6ce8\u518cdevice\u548c\u6ce8\u518cdriver\u7684\u6982\u5ff5\u3002\u6ce8\u518cdevice\u53ea\u662f\u5c06deivce\u6dfb\u52a0\u5230\u5185\u6838\u4e2d\uff0c\u8be5\u8bbe\u5907\u8fd8\u4e0d\u80fd\u5de5\u4f5c\u3002\u800c\u6ce8\u518cdriver\uff0c\u5c31\u662f\u5728\u5bf9\u5e94\u7684bus\u4e0a\u627e\u5230device\uff0c\u4ece\u800c\u8c03\u7528driver\u7684probe\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u540e\u7ee7\u7eed\u5176\u5b83\u7684\u4e8b\u60c5\u3002<\/p>\n<p>device\u548cdriver\u901a\u8fc7\u67d0\u4e9b\u65b9\u5f0f\u5339\u914d\uff0c\u5c31\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u6bd4\u5982platform\u8bbe\u5907\u6839\u636ename\u6765\u5339\u914d\u7684\uff0cPCI\u8bbe\u5907\u6839\u636eID\u5339\u914d\u3002\u6240\u4ee5\u770b\u5230\u5f88\u591a\u5185\u6838\u4ee3\u7801\u5728\u5b9a\u4e49platform_device\u65f6\u6307\u5b9aname\uff0c\u800c\u5728\u5b9a\u4e49platform_driver\u65f6\u6307\u5b9adriver\u6210\u5458\u7684name\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u4e24\u8005\u624d\u80fd\u5339\u914d\u3002\u4e00\u822c\u5730\uff0c\u53d1\u884c\u7248\u672c\u7684Linux\u7cfb\u7edf\u90fd\u5e26\u6709\u5f88\u591a\u9a71\u52a8\u3002\u5982\u524d\u6bb5\u65f6\u95f4\u7814\u7a76WIFI\uff0c\u5c06Intel\u65e0\u7ebf\u7f51\u5361\u548catheros\u65e0\u7ebf\u7f51\u5361\u63d2\u5230\u5b89\u88c5\u6709\u53d1\u884c\u7248\u672c\u7684\u7cfb\u7edf\u7684\u8bbe\u5907\u4e0a\uff0c\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u5728\/sys\/bus\/\u76ee\u5f55\u4e0b\u6709\u5f53\u524d\u7cfb\u7edf\u5404\u79cdbus\u3002\u6bd4\u5982i2c\u603b\u7ebf\u3001PCI\u603b\u7ebf\u3001SPI\u603b\u7ebf\u3001platform\u603b\u7ebf\u3002\u6bcf\u79cdbus\u76ee\u5f55\u5747\u6709device\u548cdriver\uff0c\u5728\u5176\u4e2d\u5217\u51fa\u8be5\u603b\u7ebf\u4e0a\u6240\u6709\u7684\u8bbe\u5907\u548c\u5bf9\u5e94\u7684\u9a71\u52a8\u3002\u6bd4\u5982platform\u8bbe\u5907\u3001\u9a71\u52a8\u5206\u522b\u5728\/sys\/bus\/platform\/devices\u548c\/sys\/bus\/platform\/drivers\u8fd9\u4e24\u4e2a\u76ee\u5f55\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8bbe\u5907\u6302\u8f7d\u5230\u603b\u7ebf\u4e0a\uff0c\u5f53\u52a0\u8f7d\u9a71\u52a8\u65f6\uff0c\u9a71\u52a8\u5c31\u4ece\u603b\u7ebf\u4e0a\u627e\u5230\u81ea\u5df1\u5bf9\u5e94\u7684\u8bbe\u5907\u3002<\/strong><\/p>\n<p><strong>\u6216\u8005\u5148\u628a\u9a71\u52a8\u52a0\u8f7d\u4e0a\uff0c\u6765\u4e86\u4e00\u4e2a\u8bbe\u5907\u5c31\u53bb\u603b\u7ebf\u627e\u9a71\u52a8\u3002<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<br \/>\n\u521b\u5efa\u8bbe\u5907\u5b9e\u4f8b\uff1a\u5c06\u8bbe\u5907device.c\uff0c\u7f16\u8bd1\u6210\u6a21\u5757\uff0c\u4ee5\u52a8\u6001\u52a0\u8f7d\u7684\u65b9\u5f0f\u52a0\u8f7d\u5230\u5185\u6838\u3002\u4f1a\u53d1\u73b0\u5728sys\/bus\/my_bus\/devices\/\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2amy_dev\u8bbe\u5907\uff0c\u67e5\u770b\u5c5e\u6027\uff0c\u5b83\u662f\u6302\u5728\/sys\/devices\/my_bus0\/my_dev\u76ee\u5f55\u4e0b\uff0c\u81f3\u6b64\u6dfb\u52a0\u8bbe\u5907\u6210\u529f\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\n<p>#include <linux\/module.h>\n#include <linux\/kernel.h>\n#include <linux\/init.h>\n#include <linux\/device.h>\n#include <linux\/string.h>\nextern struct device my_bus; \/\/\u8fd9\u91cc\u7528\u5230\u4e86\u603b\u7ebf\u8bbe\u5907\u4e2d\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53<\/p>\n<p>extern struct bus_type my_bus_type;<br \/>\nstatic int my_device_release() {<br \/>\n    return 0;<br \/>\n}<br \/>\nstruct device my_dev={ \/\/\u521b\u5efa\u8bbe\u5907\u5c5e\u6027<br \/>\n    .bus = &my_bus_type,\/\/\u5b9a\u4e49\u603b\u7ebf\u7c7b\u578b<br \/>\n    .parent = &my_bus,\/\/\u5b9a\u4e49my_dev\u7684\u7236\u8bbe\u5907\u3002<br \/>\n    .release = my_device_release,<br \/>\n};<br \/>\nstatic ssize_t mydev_show(struct device *dev, char *buf) {<br \/>\n    return sprintf(buf, \"%s\\n\", \"This is my device!\");<br \/>\n}<br \/>\nstatic DEVICE_ATTR(dev, S_IRUGO, mydev_show, NULL);<br \/>\nstatic int __init my_device_init(void){<br \/>\n    int ret;<br \/>\n    strncpy(my_dev.bus_id, \"my_dev\", BUS_ID_SIZE); \/\/\u521d\u59cb\u5316\u8bbe\u5907<\/p>\n<p>    ret = device_register(&my_dev); \/\/\u6ce8\u518c\u8bbe\u5907<\/p>\n<p>    if (ret)<br \/>\n        printk(\"device register!\\n\");<br \/>\n    device_create_file(&my_dev, &dev_attr_dev); \/\/\u521b\u5efa\u8bbe\u5907\u6587\u4ef6<br \/>\n    return ret;<br \/>\n}<br \/>\nstatic void __exit my_device_exit(void) {<br \/>\n        device_unregister(&my_dev);\/\/\u5378\u8f7d\u8bbe\u5907<\/p>\n<p>}<br \/>\nmodule_init(my_device_init);<br \/>\nmodule_exit(my_device_exit);<br \/>\nMODULE_AUTHOR(\"Fany\");<br \/>\nMODULE_LICENSE(\"GPL\");<\/p>\n<p>\u9a71\u52a8\u5b9e\u4f8b\uff1a\u5f53\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u65f6\uff0c\u7ec8\u7aef\u754c\u9762\u4e0a\u6253\u5370Driver found device!<br \/>\n\u8bf4\u660e\u9a71\u52a8\u627e\u5230\u4e86\u5339\u914d\u7684\u8bbe\u5907\uff0c\u770b\u5230\u6253\u5370\u8fd9\u4e2a\u65f6\uff0c\u60f3\u5230\u5728windows\u4e0b\u63d2U\u76d8\uff0c\u7acb\u9a6c\u5f39\u51fa\u201c\u53d1\u73b0\u53ef\u79fb\u52a8\u8bbe\u5907\u201d\uff0c\u6709\u70b9\u76f8\u50cf\uff01<br \/>\n\u518d\u770b\u770b\u76f8\u5e94\u7684\u76ee\u5f55\uff1a\/sys\/bus\/my_bus\/drivers\/\uff0c\u591a\u4e86\u4e00\u4e2amy_dev\u3002<\/p>\n<pre class=\"prettyprint linenums\" >\n#include <linux\/module.h>\n#include <linux\/kernel.h>\n#include <linux\/init.h>\n#include <linux\/device.h>\n#include <linux\/string.h>\nextern struct bus_type my_bus_type;<br \/>\nstatic int my_probe(struct device *dev) {<br \/>\n    printk(\"Driver found device!\\n\");<br \/>\n    return 0;<br \/>\n};<br \/>\nstatic int my_remove(struct device *dev) {<br \/>\n    printk(\"Driver unpluged!\\n\");<br \/>\n    return 0;<br \/>\n};<br \/>\nstruct device_driver my_driver = {<br \/>\n    .name = \"my_dev\",<br \/>\n    .bus = &my_bus_type,<br \/>\n    .probe = my_probe,<br \/>\n    .remove = my_remove,<br \/>\n};<br \/>\n\/\/\u5b9a\u4e49\u8bbe\u5907\u9a71\u52a8\u5c5e\u6027<\/p>\n<p>static ssize_t my_driver_show(struct device_driver *driver, char *buf) {<br \/>\n    return sprintf(buf, \"%s\\n\", \"This is my driver!\");<br \/>\n};<br \/>\nstatic DRIVER_ATTR(drv, S_IRUGO, my_driver_show, NULL);<br \/>\nstatic int __init my_driver_init(void){<br \/>\n    int ret;<br \/>\n    \/\/\u6ce8\u518c\u8bbe\u5907\u9a71\u52a8<\/p>\n<p>    ret = driver_register(&my_driver);<br \/>\n    if(ret)<br \/>\n        printk(\"driver_register failed!\\n\");<br \/>\n    \/\/\u521b\u5efa\u8bbe\u5907\u9a71\u52a8\u5c5e\u6027<\/p>\n<p>    ret = driver_create_file(&my_driver, &driver_attr_drv);<br \/>\n    if(ret)<br \/>\n        printk(\"create_driver_file failed!\\n\");<\/p>\n<p>    return ret;<br \/>\n}<br \/>\nstatic void __exit my_driver_exit(void){<br \/>\n    driver_unregister(&my_driver);\/\/\u6ce8\u9500\u8bbe\u5907\u9a71\u52a8<\/p>\n<p>}<br \/>\nmodule_init(my_driver_init);<br \/>\nmodule_exit(my_driver_exit);<br \/>\nMODULE_AUTHOR(\"Fany\");<br \/>\nMODULE_LICENSE(\"GPL\");<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u6761bus\u4e0a\u6302\u7740\u8bb8\u591adevice\uff0c\u800cdevice\u9700\u8981driver\u624d\u80fd\u5de5\u4f5c\u3002\u5185\u6838\u6709\u6ce8\u518cdevice\u548c\u6ce8\u518cdriv [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2109","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2109"}],"version-history":[{"count":1,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions"}],"predecessor-version":[{"id":2110,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2109\/revisions\/2110"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}