{"id":425,"date":"2023-06-05T23:06:57","date_gmt":"2023-06-06T07:06:57","guid":{"rendered":"http:\/\/bitas.net.cn\/?page_id=425"},"modified":"2023-06-05T23:24:22","modified_gmt":"2023-06-06T07:24:22","slug":"636-lk-drivermodel-example","status":"publish","type":"page","link":"http:\/\/bitas.net.cn\/index.php\/sourcecode\/636-lk-drivermodel-example\/","title":{"rendered":"636 (LK) \u9a71\u52a8\u6a21\u578b\u5b9e\u4f8b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/* \n * my_platform_device.c\n *\/ \n#include &lt;linux\/module.h> \n#include &lt;linux\/platform_device.h>\n\n#define DEV_NAME    \"my_pltdev\"\n#define DVR_NAME    \"my_pltdev\"\n\nstatic const char *mydata = \"ldm\";\nstatic struct platform_device *mypltdev;\n\nstatic int mypltdev_probe(struct platform_device *pdev)\n{\n    if (pdev->dev.platform_data)\n        pr_info(\"in mypltdev-%d probe, private platform data is %s\\n\",\n                pdev->dev.id, *(const char **)pdev->dev.platform_data);\n    return 0;\n}\n\nstatic struct platform_driver mypltdev_driver = { \n    .driver = { \n        .name   = DVR_NAME,\n        .owner  = THIS_MODULE,\n    },  \n    .probe = mypltdev_probe,\n};\n\nstatic void set_private_data(struct platform_device *pdev)\n{\n    pdev->dev.platform_data = &amp;mydata;\n}\n\nstatic int __init mymodule_init(void) \n{\n    int ret;\n    pr_info(\"mymodule init\\n\"); \n    mypltdev = platform_device_alloc(DEV_NAME, 0); \n    if (mypltdev == NULL) {\n        pr_info(\"Failed to alloc device\\n\");\n        goto err;\n    }   \n    ret = platform_device_add(mypltdev);\n    if (ret != 0) {\n        pr_info(\"Failed to add device\\n\");\n        goto err;\n    }\n    set_private_data(mypltdev);\n    ret = platform_driver_register(&amp;mypltdev_driver);\n    if (ret != 0) {\n        pr_info(\"Failed to register %s\\n\", DVR_NAME);\n        goto err_del_dev;\n    }\n    pr_info(\"mypltdev at %p(%lu), assert to be less than %lu\\n\",\n            (void *)mypltdev, (unsigned long)(void *)mypltdev, (unsigned long)-4095);\n    return 0;\n\nerr_del_dev:\n    platform_device_del(mypltdev);\nerr:\n    pr_info(\"mymodule init failed\\n\");\n    return -1;\n}\n\nstatic void __exit mymodule_exit(void)\n{\n    pr_info(\"mymodule exit\\n\");\n    platform_driver_unregister(&amp;mypltdev_driver);\n    platform_device_del(mypltdev);\n}\n\nmodule_init(mymodule_init);\nmodule_exit(mymodule_exit);\n\nMODULE_LICENSE(\"GPL\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":127,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-425","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":1,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/425\/revisions"}],"predecessor-version":[{"id":426,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/425\/revisions\/426"}],"up":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/127"}],"wp:attachment":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/media?parent=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}