{"id":1975,"date":"2016-09-20T15:13:12","date_gmt":"2016-09-20T07:13:12","guid":{"rendered":"http:\/\/www.51cos.com\/?p=1975"},"modified":"2016-09-20T17:15:21","modified_gmt":"2016-09-20T09:15:21","slug":"insmod-%e4%b8%8e-probe","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=1975","title":{"rendered":"insmod \u4e0e probe"},"content":{"rendered":"<p>insmod\u6700\u4e3b\u8981\u5c31\u662f\u6267\u884c*.ko\u91cc\u9762__init\u4fee\u9970\u7684\u51fd\u6570\uff0c<br \/>\n\u4e00\u822c\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u9762\u53bb\u6ce8\u518cdriver\uff0c<br \/>\n\u6ce8\u518cdirver\u7684\u51fd\u6570\u4f1a\u901a\u8fc7name\u5339\u914d\u662f\u5426\u6709device\u5b58\u5728\uff0c<br \/>\n\u5982\u679c\u5b58\u5728\u5219\u6267\u884cprobe\u51fd\u6570\u3002<\/p>\n<p>\u5177\u4f53\u610f\u601d\u662f\uff0c\u4ee5\u5b57\u7b26\u9a71\u52a8\u4e3a\u4f8b\uff0c\u5728.c\u6587\u4ef6\u4e2d\uff0c\u4f1a\u586b\u5145platform_driver\u7ed3\u6784\u4f53\uff0c\u91cc\u9762\u6307\u5b9aname\uff0c<\/p>\n<p>\u7136\u540e\u5728linux\\arch\\mips\\xxx-platform.c\u4e2d\uff0c\u586b\u5145platform_device\u7ed3\u6784\u4f53\uff0c\u6307\u5b9aname\u3002<\/p>\n<p>\u53ef\u4ee5\u9605\u8bfb\u300alinux \u5185\u6838\u9a71\u52a8&#8211;Platform Device\u548cPlatform_driver\u6ce8\u518c\u8fc7\u7a0b\u300b<br \/>\nhttp:\/\/www.cnblogs.com\/haimeng2010\/p\/3582403.html<br \/>\nhttp:\/\/www.cnblogs.com\/leaven\/archive\/2010\/01\/06\/1640177.html<br \/>\nhttp:\/\/blog.sina.com.cn\/s\/blog_720b34850101r2qc.html<\/p>\n<p>platform_device\u548cplatform_driver\u7684\u6ce8\u518c\u8fc7\u7a0b\uff0c\u53caprobe\u51fd\u6570\u4f55\u65f6\u8c03\u7528\u7684\u5206\u6790 <\/p>\n<p>\u901a\u8fc7\u7cfb\u7edf\u521d\u59cb\u5316\u91cc\u8fb9\u8c03\u7528platform_add_devices\uff0c\u628a\u6240\u6709\u653e\u7f6e\u5728\u677f\u7ea7platform_device\u6570\u7ec4\u4e2d\u7684\u6240\u6709platform_device\u9010\u6b21\u8c03\u7528platform_device_register\u6dfb\u52a0\u5230\u7cfb\u7edf\u4e2d\u53bb\uff0c<\/p>\n<p>platform_device_register\u4e2d\u4f1a\u8c03\u7528platform_device_add\uff08\u6ce8\u610f\uff1a\u8fd9\u4e2a\u540cplatform_add_devices\u6709\u672c\u8d28\u533a\u522b\u7684\uff09\uff0c\u5168\u90e8add\u5230\u7cfb\u7edf\u4e4b\u540e\uff0c\u4fbf\u53ef\u4ee5\u901a\u8fc7platform\u7684\u64cd\u4f5c\u63a5\u53e3\u6765\u83b7\u53d6platform_device\u4e2d\u7684\u8d44\u6e90\u3002<\/p>\n<p>\u9a71\u52a8\u6a21\u5757insmod\u5230\u7cfb\u7edf\u65f6\uff0c\u9a71\u52a8\u4ee3\u7801\u91cc\u8fb9\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u51fd\u6570\u6765\u83b7\u53d6\u5bf9\u5e94platform_device\u7684resource\uff0c\u6bd4\u5982\u5728module_init\u4e2d\u6211\u4eec\u4f1a\u8c03\u7528plarform_driver_register\uff0c\u8fd9\u4e2a\u4f1a\u5f15\u7528\u5230platform_driver\u4e2d\u7684probe\u51fd\u6570\uff0c\u800cprobe\u51fd\u6570\u4e2d\u5219\u53ef\u4ee5\u8fdb\u884ccdev\u7684\u521d\u59cb\u5316\u53cacdev_add\u7684\u64cd\u4f5c\uff0c\u5728\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u4e4b\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7get_resource\u6765\u83b7\u53d6\u5bc4\u5b58\u5668\u7269\u7406\u57fa\u5730\u5740\uff0c\u7136\u540eioremap\u5230kernel\u7684\u865a\u62df\u7a7a\u95f4\u6765\uff0c\u8fd9\u6837\u9a71\u52a8\u5c31\u53ef\u4ee5\u6b63\u5f0f\u64cd\u7eb5\u6539\u8bbe\u5907\u7684\u5bc4\u5b58\u5668\u4e86\u3002<\/p>\n<p>\u9a71\u52a8\u6ce8\u518c\u7684\u65f6\u5019 platform_driver_register()->driver_register()->bus_add_driver()->driver_attach()->bus_for_each_dev()&#8211; \u5bf9\u6bcf\u4e2a\u6302\u5728\u865a\u62df\u7684platform bus\u7684\u8bbe\u5907\u4f5c- __driver_attach()->driver_probe_device()->drv->bus->match()==platform_match()-\u6bd4\u8f83strncmp(pdev->name, drv->name, BUS_ID_SIZE)\uff0c\u5982\u679c\u76f8\u7b26\u5c31\u8c03\u7528platform_drv_probe()->driver->probe()\uff0c\u5982\u679cprobe\u6210\u529f\u5219\u7ed1\u5b9a\u8be5\u8bbe\u5907\u5230\u8be5\u9a71\u52a8\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6e90\u4e8elinux3.1.9\u3002<\/p>\n<p>platform_driver_register( ) \u662f\u5185\u6838\u4e2d\u975e\u5e38\u8457\u540d\u7684\u51fd\u6570 \u3002platform_driver_register( )\u8d1f\u8d23\u6ce8\u518c\u5e73\u53f0\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5982\u679c\u5728\u5185\u6838\u4e2d\u627e\u5230\u4e86\u4f7f\u7528\u9a71\u52a8\u7a0b\u5e8f\u7684\u8bbe\u5907\uff0c\u8c03\u7528probe( )\u3002\u5228\u53bb\u53c2\u6570\u68c0\u67e5\u3001\u9519\u8bef\u5904\u7406\uff0cplatform_driver_register\u7684\u4e3b\u8981\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>\u3002\u3002\u3002<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    return platform_driver_register(&#038;bcm2708_i2c_driver);<\/p>\n<p>    \u3002\u3002\u3002<br \/>\n}   <\/p>\n<p>int platform_driver_register(struct platform_driver *drv)<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    return driver_register(&#038;drv->driver);<br \/>\n}<\/p>\n<p>int driver_register(struct device_driver *drv)<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    ret = bus_add_driver(drv);                    \/\/platform\u662f\u4e00\u4e2a\u865a\u62df\u603b\u7ebf<br \/>\n    \u3002\u3002\u3002<br \/>\n}<\/p>\n<p>int bus_add_driver(struct device_driver *drv)<br \/>\n{<br \/>\n    \u3002\u3002\u3002 <\/p>\n<p>    if (drv->bus->p->drivers_autoprobe) {<br \/>\n        error = driver_attach(drv);<br \/>\n        if (error)<br \/>\n            goto out_unregister;<br \/>\n    }<br \/>\n    \u3002\u3002\u3002<br \/>\n    kobject_uevent(&#038;priv->kobj, KOBJ_ADD);                   \/\/\u53d1\u9001uevent\u6d88\u606f<br \/>\n    return 0;<br \/>\n    \u3002\u3002\u3002<br \/>\n}<\/p>\n<p>int driver_attach(struct device_driver *drv)<br \/>\n{<br \/>\n    return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);  \/\/\u76d1\u6d4b\u5230bus\u8bbe\u5907\uff0c\u8c03\u7528__driver_attach( )<br \/>\n}<\/p>\n<p>static int __driver_attach(struct device *dev, void *data)                    \/\/dev \u4e3a\u4f7f\u7528\u9a71\u52a8\u7a0b\u5f0f\u7684\u8bbe\u5907\u7ed3\u6784\u4f53<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    if (!dev->driver)<br \/>\n        driver_probe_device(drv, dev);<br \/>\n    \u3002\u3002\u3002<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>int driver_probe_device(struct device_driver *drv, struct device *dev)<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    ret = really_probe(dev, drv);<br \/>\n    \u3002\u3002\u3002<br \/>\n}<\/p>\n<p>static int really_probe(struct device *dev, struct device_driver *drv)<br \/>\n{<br \/>\n    \u3002\u3002\u3002<br \/>\n    dev->driver = drv;<br \/>\n    if (driver_sysfs_add(dev)) {           \/\/\u5728sysfs\u4e2d\u6dfb\u52a0\u8bbe\u5907\u7684Kobject\u76ee\u5f55<br \/>\n         \u3002\u3002\u3002<br \/>\n    }<br \/>\n    if (dev->bus->probe) {<br \/>\n       \u3002\u3002\u3002<\/p>\n<p>    } else if (drv->probe) {<br \/>\n        ret = drv->probe(dev);               \/\/\u8c03\u7528driver\u7684probe( )\uff0cdev\u4e3a\u8bbe\u5907\u7ed3\u6784\u4f53<br \/>\n        \u3002\u3002\u3002<\/p>\n<p>    }<br \/>\n     \u3002\u3002\u3002<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>insmod\u6700\u4e3b\u8981\u5c31\u662f\u6267\u884c*.ko\u91cc\u9762__init\u4fee\u9970\u7684\u51fd\u6570\uff0c \u4e00\u822c\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u9762\u53bb\u6ce8\u518cdriver\uff0c \u6ce8\u518cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1975","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1975","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=1975"}],"version-history":[{"count":3,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1975\/revisions"}],"predecessor-version":[{"id":1978,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1975\/revisions\/1978"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1975"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}