{"id":295308,"date":"2024-12-31T12:15:51","date_gmt":"2024-12-31T04:15:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/bxqdcxdwzznc.html"},"modified":"2024-12-31T12:15:51","modified_gmt":"2024-12-31T04:15:51","slug":"bxqdcxdwzznc","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bxqdcxdwzznc.html","title":{"rendered":"\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u7684\u5b8c\u6574\u6307\u5357\uff1a\u4ece\u57fa\u7840\u77e5\u8bc6\u5230\u5b9e\u9645\u6848\u4f8b\u7684\u8be6\u7ec6\u6b65\u9aa4"},"content":{"rendered":"

\u7f16\u64b0\u9a71\u52a8\u7a0b\u5e8f<\/p>\n

\u5f00\u59cb\u7f16\u64b0\u9a71\u52a8\u7a0b\u5e8f\u662f\u4e00\u4e2a\u65e2\u5177\u6311\u6218\u6027\u53c8\u9971\u542b\u5b66\u4e60\u673a\u4f1a\u7684\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n

\u5b66\u4e60\u57fa\u7840\u77e5\u8bc6\uff1a<\/p>\n

\u9009\u62e9\u5f00\u53d1\u73af\u5883\uff1a<\/p>\n

\u9605\u8bfb\u6587\u6863\u548c\u6559\u7a0b\uff1a<\/p>\n

\u4ece\u5927\u578b\u9879\u76ee\u5f00\u59cb\uff1a<\/p>\n

\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff1a<\/p>\n

\u9075\u5faa\u6cd5\u5f8b\u548c\u9053\u5fb7\u51c6\u5219\uff1a<\/p>\n

\u6301\u7eed\u5b66\u4e60\u548c\u6539\u8fdb\uff1a<\/p>\n

\u793a\u4f8b\uff1a<\/p>\n

\u7f16\u64b0\u4e00\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u7684\u6848\u4f8b\u4e00\u822c\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u8fd9\u79cd\u6b65\u9aa4\u4ece\u4e86\u89e3\u786c\u4ef6\u89c4\u8303\u5f00\u59cb\uff0c\u5230\u6700\u7ec8\u96c6\u6210\u548c\u6d4b\u8bd5\u9a71\u52a8\u7a0b\u5e8f\u3002\u4e0b\u8fb9\u6211\u5c06\u901a\u8fc7\u4e00\u4e2a\u7b80\u5316\u7684\u4e8b\u4f8b\u6765\u6982\u8ff0\u600e\u6837\u7f16\u64b0\u4e00\u4e2a\u57fa\u672c\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\uff0c\u8fd9\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u53ef\u80fd\u7528\u4e8e\u63a7\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u786c\u4ef6\u7ec4\u4ef6\uff0c\u4f8b\u5982\u4e00\u4e2aLED\u706f\u3002\u8fd9\u4e2a\u4e8b\u4f8b\u5c06\u57fa\u4e8eLinux<\/a>\u64cd\u4f5c\u7cfb\u7edflinux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891<\/strong>\uff0c\u7531\u4e8eLinux\u662f\u5f00\u6e90\u7684\uff0c\u4f46\u662f\u5176\u9a71\u52a8\u7a0b\u5e8f\u5f00\u53d1\u6587\u6863\u76f8\u5bf9\u4e30\u5bcc\u3002<\/p>\n

1.\u786c\u4ef6\u89c4\u8303\u4e86\u89e3<\/p>\n

\u9996\u5148\uff0c\u4f60\u987b\u8981\u4e86\u89e3\u4f60\u7684\u786c\u4ef6\u8bbe\u5907\u7684\u5c3a\u5bf8\u4e2d\u56fdlinux\uff0c\u5305\u62ec\u5b83\u600e\u6837\u4e0eCPU\u901a\u8baf\uff08\u4f8b\u5982\u901a\u8fc7I\/O\u7aef\u53e3\u3001\u5185\u5b58\u6620\u5c04I\/O\u3001PCI\/PCIe\u7b49\uff09\uff0c\u4ee5\u53ca\u5b83\u987b\u8981\u7684\u4efb\u4f55\u7279\u5b9a\u547d\u4ee4<\/a>\u6216\u6570\u636e\u683c\u5f0f\u3002<\/p>\n

2.\u786e\u5b9a\u9a71\u52a8\u7a0b\u5e8f\u7684\u7c7b\u578b<\/p>\n

\u5728\u8fd9\u4e2a\u4e8b\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u64b0\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u3002\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u4e00\u822c\u7528\u4e8e\u5904\u7406\u8fd9\u4e9b\u50cf\u4e32\u884c\u7aef\u53e3\u6216\u7ec8\u7aef\u90a3\u6837\u53ef\u4ee5\u6328\u4e2a\u5b57\u7b26\u8fdb\u884c\u8bfb\u5199\u7684\u8bbe\u5907\u3002<\/p>\n

3.\u7f16\u64b0\u9a71\u52a8\u7a0b\u5e8f\u4ee3\u7801<\/p>\n

\u9a71\u52a8\u7a0b\u5e8f\u4ee3\u7801\u4e00\u822c\u5305\u62ec\u51e0\u4e2a\u5173\u952e\u90e8\u4efd\uff1a\u8bbe\u5907\u521d\u59cb\u5316\u3001\u8bbe\u5907\u64cd\u4f5c\u51fd\u6570\uff08\u5982open\u3001read\u3001write\u3001close\u7b49\uff09\u3001\u4e2d\u65ad\u5904\u7406\uff08\u5047\u5982\u9002\u7528\uff09\u548c\u8bbe\u5907\u6ce8\u9500\u3002<\/p>\n

\u793a\u4f8b\u4ee3\u7801\u6846\u67b6<\/p>\n

\n

#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n\/\/ \u8bbe\u5907\u7ed3\u6784\u4f53<\/span>\n

\u89c6\u9891\u9a71\u52a8\u5b89\u88c5_linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891_linuxvideo\u9a71\u52a8<\/p>\nstruct<\/span> my_device<\/span> {<\/span>\n struct<\/span> cdev<\/span> cdev;<\/span> \/\/ \u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a cdev \u7684 struct cdev \u7c7b\u578b\u53d8\u91cf<\/span>\n \/\/ \u53ef\u80fd\u8fd8\u6709\u5176\u4ed6\u6210\u5458\uff0c\u5982\u6307\u5411\u786c\u4ef6\u5bc4\u5b58\u5668\u7684\u6307\u9488\u7b49<\/span>\n}<\/span>;<\/span>\n\/\/ \u8bbe\u5907\u64cd\u4f5c\u51fd\u6570<\/span>\nstatic<\/span> int<\/span> my_device_open<\/span>(<\/span>struct<\/span> inode<\/span> *<\/span>inode,<\/span> struct<\/span> file<\/span> *<\/span>file)<\/span> {<\/span>\n \/\/ \u521d\u59cb\u5316\u8bbe\u5907\u6216\u51c6\u5907\u64cd\u4f5c<\/span>\n return<\/span> 0<\/span>;<\/span>\n}<\/span>\nstatic<\/span> ssize_t<\/span> my_device_write<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span> const<\/span> char<\/span> __user *<\/span>buf,<\/span> size_t<\/span> count,<\/span> loff_t<\/span> *<\/span>ppos)<\/span> {<\/span>\n \/\/ \u5199\u5165\u64cd\u4f5c\uff0c\u6bd4\u5982\u63a7\u5236LED\u4eae<\/span>\n return<\/span> count;<\/span> \/\/ \u5047\u8bbe\u603b\u662f\u6210\u529f\u5199\u5165<\/span>\n}<\/span>\n\/\/ \u5176\u4ed6\u64cd\u4f5c\u51fd\u6570...<\/span>\n\/\/ \u8bbe\u5907\u6587\u4ef6\u64cd\u4f5c\u7ed3\u6784<\/span>\nstatic<\/span> struct<\/span> file_operations<\/span> my_device_fops =<\/span> {<\/span>\n .<\/span>owner =<\/span> THIS_MODULE,<\/span>\n .<\/span>open =<\/span> my_device_open,<\/span>\n .<\/span>write =<\/span> my_device_write,<\/span>\n \/\/ ... \u5176\u4ed6\u64cd\u4f5c<\/span>\n}<\/span>;<\/span>\n\/\/ \u6a21\u5757\u521d\u59cb\u5316\u51fd\u6570<\/span>\nstatic<\/span> int<\/span> __init my_device_init<\/span>(<\/span>void<\/span>)<\/span> {<\/span>\n int<\/span> result;<\/span>\n dev_t<\/span> dev_num;<\/span>\n struct<\/span> my_device<\/span> *<\/span>device;<\/span>\n \/\/ \u5206\u914d\u8bbe\u5907\u53f7<\/span>\n result =<\/span> alloc_chrdev_region<\/span>(<\/span>&<\/span>dev_num,<\/span> 0<\/span>,<\/span> 1<\/span>,<\/span> \"my_device\"<\/span>)<\/span>;<\/span>\n if<\/span> (<\/span>result <<\/span> 0<\/span>)<\/span> {<\/span>\n printk<\/span>(<\/span>KERN_WARNING \"my_device: cannot get major numbern\"<\/span>)<\/span>;<\/span>\n return<\/span> result;<\/span>\n }<\/span>\n \/\/ \u5206\u914d\u5e76\u521d\u59cb\u5316\u8bbe\u5907\u7ed3\u6784\u4f53<\/span>\n device =<\/span> kmalloc<\/span>(<\/span>sizeof<\/span>(<\/span>struct<\/span> my_device<\/span>)<\/span>,<\/span> GFP_KERNEL)<\/span>;<\/span>\n if<\/span> (<\/span>!<\/span>device)<\/span> {<\/span>\n result =<\/span> -<\/span>ENOMEM;<\/span>\n goto<\/span> fail_malloc;<\/span>\n }<\/span>\n \/\/ \u521d\u59cb\u5316cdev<\/span>\n cdev_init<\/span>(<\/span>&<\/span>device-><\/span>cdev,<\/span> &<\/span>my_device_fops)<\/span>;<\/span>\n device-><\/span>cdev.<\/span>owner =<\/span> THIS_MODULE;<\/span>\n result =<\/span> cdev_add<\/span>(<\/span>&<\/span>device-><\/span>cdev,<\/span> dev_num,<\/span> 1<\/span>)<\/span>;<\/span>\n if<\/span> (<\/span>result)<\/span> {<\/span>\n goto<\/span> fail_cdev;<\/span>\n }<\/span>\n

linuxvideo\u9a71\u52a8_linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891_\u89c6\u9891\u9a71\u52a8\u5b89\u88c5<\/p>\n \/\/ \u521b\u5efa\u8bbe\u5907\u8282\u70b9\uff08\u53ef\u9009\uff0c\u901a\u5e38\u901a\u8fc7udev\u6216mdev\u81ea\u52a8\u521b\u5efa\uff09<\/span>\n \/\/ ...<\/span>\n return<\/span> 0<\/span>;<\/span>\nfail_cdev:<\/span>\n kfree<\/span>(<\/span>device)<\/span>;<\/span>\nfail_malloc:<\/span>\n unregister_chrdev_region<\/span>(<\/span>dev_num,<\/span> 1<\/span>)<\/span>;<\/span>\n return<\/span> result;<\/span>\n}<\/span>\n\/\/ \u6a21\u5757\u5378\u8f7d\u51fd\u6570<\/span>\nstatic<\/span> void<\/span> __exit my_device_exit<\/span>(<\/span>void<\/span>)<\/span> {<\/span>\n \/\/ \u91ca\u653e\u8d44\u6e90\uff0c\u5982cdev\u3001\u8bbe\u5907\u53f7\u3001\u8bbe\u5907\u8282\u70b9\u7b49<\/span>\n}<\/span>\nmodule_init<\/span>(<\/span>my_device_init)<\/span>;<\/span>\nmodule_exit<\/span>(<\/span>my_device_exit)<\/span>;<\/span>\nMODULE_LICENSE<\/span>(<\/span>\"GPL\"<\/span>)<\/span>;<\/span>\nMODULE_AUTHOR<\/span>(<\/span>\"Your Name\"<\/span>)<\/span>;<\/span>\nMODULE_DESCRIPTION<\/span>(<\/span>\"A simple example driver for a character device\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

4.\u7f16\u64b0\u5355\u5143\u6d4b\u8bd5<\/p>\n

\u5c3d\u7ba1Linux\u5185\u6838\u6ca1\u6709\u5916\u7f6e\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982KUnit\u8fd9\u6837\u7684\u5de5\u5177\u6765\u4e3a\u4f60\u7684\u9a71\u52a8\u7a0b\u5e8f\u7f16\u64b0\u5355\u5143\u6d4b\u8bd5\u3002\u5355\u5143\u6d4b\u8bd5\u5c06\u786e\u4fdd\u4f60\u7684\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n

5.\u96c6\u6210\u6d4b\u8bd5<\/p>\n

\u96c6\u6210\u6d4b\u8bd5\u6d89\u53ca\u5c06\u4f60\u7684\u9a71\u52a8\u7a0b\u5e8f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u548c\u5176\u4ed6\u786c\u4ef6\u7ec4\u4ef6\u96c6\u6210\u5728\u4e00\u8d77\u8fdb\u884c\u6d4b\u8bd5\u3002\u8fd9\u4e00\u822c\u5305\u62ec\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u3001\u6267\u884c\u5404\u7c7b\u64cd\u4f5c\uff08\u5982\u8bfb\u5199\u3001\u63a7\u5236\u786c\u4ef6\u7b49\uff09\uff0c\u5e76\u89c2\u5bdf\u7cfb\u7edf\u54cd\u5e94\u3002<\/p>\n

6.\u8c03\u8bd5<\/p>\n

\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2dlinux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f \u89c6\u9891<\/strong>\uff0c\u4f60\u53ef\u80fd\u987b\u8981\u8c03\u8bd5\u4f60\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002Linux\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5982dmesg\u3001strace\u3001gdb\uff08\u5bf9\u4e8e\u5185\u6838\u6a21\u5757\uff0c\u4e00\u822c\u4f7f\u7528<\/p>\n

7.\u5e03\u7f72\u548c\u5206\u53d1<\/p>\n

\u4e00\u65e6\u9a71\u52a8\u7a0b\u5e8f\u901a\u8fc7\u6240\u6709\u6d4b\u8bd5\u5e76\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f60\u5c31\u53ef\u4ee5\u5c06\u5176\u5e03\u7f72\u5230\u76ee\u6807\u7cfb\u7edf\u4e0alinux\u4f7f\u7528\u6559\u7a0b\uff0c\u5e76\u6253\u7b97\u5206\u53d1\u7ed9\u6700\u7ec8\u7528\u6237\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u7f16\u5199\u4e00\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u7684\u6848\u4f8b\u901a\u5e38\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u4ece\u4e86\u89e3\u786c\u4ef6\u89c4\u8303\u5f00\u59cb\uff0c\u5230\u6700\u7ec8\u96c6\u6210\u548c\u6d4b\u8bd5\u9a71\u52a8\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u3002\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u4ee3\u7801\u867d\u7136Linux\u5185\u6838\u6ca1\u6709\u5185\u7f6e\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982KUnit\u8fd9\u6837\u7684\u5de5\u5177\u6765\u4e3a\u4f60\u7684\u9a71\u52a8\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":295309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[763,5313,2990,5314,3034],"class_list":["post-295308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-linux","tag-dycs","tag-zfsbqd","tag-yjkz","tag-qdcx"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/295308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=295308"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/295308\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/295309"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=295308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=295308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=295308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

\u5728Linux\u5185\u6838<\/a>\u6a21\u5757\u5f00\u53d1\u4e2d\uff0cmodule_init\u548cmodule_exit\u5b8f\u4ee5\u53caMODULE_LICENSE\u3001MODULE_AUTHOR\u3001MODULE_DESCRIPTION\u7b49\u5b8f\u7528\u4e8e\u7533\u660e\u6a21\u5757\u7684\u521d\u59cb\u5316\u51fd\u6570\u3001\u9000\u51fa\u51fd\u6570\u4ee5\u53ca\u6a21\u5757\u7684\u5143\u6570\u636e\u3002\u4e0b\u8fb9\u662f\u5bf9\u90a3\u4e9b\u5b8f\u548c\u51fd\u6570\u7684\u89e3\u91ca\uff1a<\/p>\n