{"id":552,"date":"2019-09-23T14:42:44","date_gmt":"2019-09-23T06:42:44","guid":{"rendered":"http:\/\/iqotom.com\/?p=552"},"modified":"2019-10-17T14:29:30","modified_gmt":"2019-10-17T06:29:30","slug":"liunx%e9%a9%b1%e5%8a%a8-%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aamisc%e8%ae%be%e5%a4%87%e9%a9%b1%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/iqotom.com\/?p=552","title":{"rendered":"Liunx\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2amisc\u8bbe\u5907\u9a71\u52a8"},"content":{"rendered":"\n<p>\n\n================================================================<br>\u300a<a href=\"http:\/\/iqotom.com\/?p=526\">Linux\u9a71\u52a8 \u7f16\u8bd1\u4e00\u4e2ahello\u6a21\u5757<\/a>\u300b<br>\u300a<a href=\"http:\/\/iqotom.com\/?p=539\">Linux\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8<\/a>\u300b<br>\u300a<a href=\"http:\/\/iqotom.com\/?p=552\">Liunx\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2amisc\u8bbe\u5907\u9a71\u52a8<\/a>\u300b<br>================================================================\n\n<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60misc\u8bbe\u5907\u7684\u7f16\u7a0b<\/h3>\n\n\n\n<p> \u56e0\u4e3a\uff0c\u5982\u679c\u6211\u4eec \u6bcf\u4e2a\u9a71\u52a8\u8bbe\u5907\u90fd\u8981\u50cf\u6700\u521d\u90a3\u6837\u5b50\u53bb\u5199\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u4e00\u6837\uff0c\u8981\u5206\u914d\u4e3b\u8bbe\u5907\u53f7\uff0c\u6b21\u8bbe\u5907\u53f7\uff0c\u5b9e\u73b0\u5bf9\u5e94\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u7b49\u7b49\u7684\u6b65\u9aa4\uff0c\u672a\u514d\u5c31\u6709\u70b9\u591a\u4e86\uff0c\u800c\u4e14\u4e5f\u4e0d\u597d\u8bb0\u4f4f\u5b83\uff0c\u4e3a\u6b64\uff0cLinux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5077\u61d2\u7684\u6280\u5de7\uff0c\u90a3\u5c31\u662f\u5b9e\u73b0\u4e86misc\u8bbe\u5907\uff0c\u5176\u5b9emisc\u8bbe\u5907\uff0c\u4e5f\u7b97\u662f\u5b57\u7b26\u8bbe\u5907\uff0c\u53ea\u4e0d\u8fc7\u5bf9\u5b57\u7b26\u8bbe\u5907\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4ecb\u7ecd\u5c31\u77e5\u9053\u4e86 <\/p>\n\n\n\n<p> \u6253\u5f00\u5185\u6838<code>\/include\/linux\/miscdevice.h<\/code> \u627e\u5230misc\u8bbe\u5907\u7684\u7ed3\u6784\u4f53: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct miscdevice  {\n\t\/\/\u6b21\u8bbe\u5907\u53f7\u4e00\u822c\u8d4b\u503c\u4e3aMISC_DYNAMIC_MINOR---->\u7531\u5185\u6838\u81ea\u52a8\u53bb\u5206\u914d\u6b21\u8bbe\u5907\u53f7\n\tint minor;\n\t\/\/misc\u8bbe\u5907\u7684\u540d\u79f0\n\tconst char *name;\n\t\/\/\u6587\u4ef6\u64cd\u4f5c\u7ed3\u6784\u4f53\n\tconst struct file_operations *fops;\n\tstruct list_head list;\n\tstruct device *parent;\n\tstruct device *this_device;\n\tconst struct attribute_group **groups;\n\tconst char *nodename;\n\tumode_t mode;\n};<\/code><\/pre>\n\n\n\n<p> \u8fd9\u91cc\u6211\u53ea\u5b9e\u73b0\u6700\u7b80\u5355\u7684misc\u8bbe\u5907\uff0c\u53ea\u9700\u8981\u5173\u6ce8minor(\u6b21\u8bbe\u5907\u53f7),name(\u8bbe\u5907\u540d\u79f0),fops(\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570)\u3002<\/p>\n\n\n\n<p> misc\u8bbe\u5907\u662f\u5bf9\u5b57\u7b26\u8bbe\u5907\u505a\u4e86\u4e00\u4e2a\u518d\u6b21\u7684\u5c01\u88c5\uff0c\u800c\u4e14\uff0c\u5728misc\u8bbe\u5907\u4e2d\uff0c\u4e3b\u8bbe\u5907\u53f7\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f10\uff0c\u53ea\u6709\u6b21\u8bbe\u5907\u53f7\u4e0d\u540c\uff0c\u5f53\u6211\u4eec\u4e0d\u77e5\u9053\u5185\u6838\u4e2d\u5e94\u8be5\u53bb\u5206\u914d\u90a3\u4e2a\u6b64\u8bbe\u5907\u53f7\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u7ed9minor\u8d4b\u503c\u4e3aMISC_DYNAMIC_MINOR\u8fd9\u4e2a\u5b8f\uff0c\u610f\u601d\u5c31\u662f\u7531\u5185\u6838\u6765\u5e2e\u6211\u4eec\u5206\u914d\u6b21\u8bbe\u5907\u53f7\u3002<\/p>\n\n\n\n<p> name\u5c31\u4e0d\u7528\u8bf4\u4e86\uff0c\u5982\u679c\u8bbe\u5907\u6ce8\u518c\u6210\u529f\uff0c\u5728\u6839\u6587\u4ef6\u7cfb\u7edf\/dev\/\u4e0b\u5c31\u4f1a\u6709\u6ce8\u518c\u8bbe\u5907\u540e\u7684name\u3002 <\/p>\n\n\n\n<p> fops\u5c31\u662f\u4e00\u7cfb\u5217\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u5566\uff0c\u4ec0\u4e48open , read ,write , ioctl\u7b49\u7b49\uff0c\u5f88\u591a,\u8ddf\u5199\u5b57\u7b26\u8bbe\u5907\u662f\u4e00\u6837\u7684 \u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9a71\u52a8\u5b9e\u73b0\u6e90\u7801\u4ee3\u7801\u5982\u4e0b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;linux\/kernel.h>\n#include&lt;linux\/module.h>\n#include&lt;linux\/miscdevice.h>\n#include&lt;linux\/fs.h>\n\n\n#define DEVICE_NAME \"my_misc_dev\"\n\n\/\/\u5b9e\u73b0open\u51fd\u6570\nint my_misc_dev_open(struct inode *inode, struct file *filp)\n{\n\tprintk(\"minipc misc dev open!\\n\");\n\treturn 0 ;\n}\n\n\/\/\u5b9e\u73b0close\u51fd\u6570\nint my_misc_dev_close(struct inode *inode, struct file *filp)\n{\n\tprintk(\"minipc misc dev close!\\n\");\n\treturn 0 ;\n}\n\n\/\/\u521d\u59cb\u5316\u6587\u4ef6\u64cd\u4f5c\u7ed3\u6784\u4f53\nstruct file_operations file_ops = {\n\t.owner\t = THIS_MODULE,\n\t.open\t = my_misc_dev_open,\n\t.release = my_misc_dev_close,\n};\n\n\/\/\u521d\u59cb\u5316misc\u8bbe\u5907\u7ed3\u6784\u4f53\nstruct miscdevice my_misc_dev = {\n\t\/\/\u7531\u5185\u6838\u81ea\u52a8\u5206\u914d\u6b21\u8bbe\u5907\u53f7\n\t.minor = MISC_DYNAMIC_MINOR ,\n\t\/\/\u521d\u59cb\u5316\u8bbe\u5907\u540d\u79f0\n\t.name = DEVICE_NAME ,\n\t\/\/\u521d\u59cb\u5316\u6587\u4ef6\u64cd\u4f5c\u7ed3\u6784\u4f53\n\t.fops = &amp;file_ops,\t\n};\n\n\nstatic int __init my_misc_dev_init(void) \n{\n\tint ret_error ;\n\t\/\/\u6ce8\u518cmisc\u8bbe\u5907\n\tint ret = misc_register(&amp;my_misc_dev);\n\tif(ret != 0){\n\t   ret_error = ret ;\n\t   printk(\"misc register fair!\\n\");\n\t   goto fair ;\n\t}\n\tprintk(\"misc init success!\\n\");\t\n\treturn ret ;\nfair:\n\treturn ret_error ;\n}\n \nstatic void __exit my_misc_dev_exit(void) \n{\n\t\/\/\u6ce8\u9500misc\u8bbe\u5907\n\tmisc_deregister(&amp;my_misc_dev);\n}\n\nmodule_init(my_misc_dev_init);\nmodule_exit(my_misc_dev_exit);\nMODULE_LICENSE(\"GPL\");\nMODULE_AUTHOR(\"LAOHU add misc driver\");<\/code><\/pre>\n\n\n\n<p><strong> Makefile: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TARGET\t\t?= $(shell uname -r)\nKERNEL_MODULES\t?= \/lib\/modules\/$(TARGET)\nKERNEL_BUILD\t?= $(KERNEL_MODULES)\/build\nSYSTEM_MAP\t?= $(KERNEL_BUILD)\/System.map\n\nDRIVER := my_misc\n\nobj-m\t:= $(patsubst %,%.o,$(DRIVER))\nobj-ko  := $(patsubst %,%.ko,$(DRIVER))\n\nMAKEFLAGS += --no-print-directory\n\n.PHONY: all modules clean\n\nall: modules\n\n# Targets for running make directly in the external module directory:\n\nmodules clean:\n\t@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) $@<\/code><\/pre>\n\n\n\n<p> \u7f16\u8bd1\u7a0b\u5e8f\uff0c\u52a0\u8f7d\u9a71\u52a8\uff0c\u53ef\u4ee5\u770b\u5230\u8bbe\u5907\u6ce8\u518c\u6210\u529f\uff0c\u4e3b\u8bbe\u5907\u53f7\uff1a10\uff0c\u6b21\u8bbe\u5907\u53f755 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"294\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/linux-misc-drive-01.png\" alt=\"\" class=\"wp-image-553\" srcset=\"https:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/linux-misc-drive-01.png 948w, https:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/linux-misc-drive-01-300x93.png 300w, https:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/linux-misc-drive-01-768x238.png 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><figcaption>misc device<\/figcaption><\/figure><\/div>\n\n\n\n<p>linux misc\u8bbe\u5907\u9a71\u52a8\u5b8c\u6210<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Liunx\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2amisc\u8bbe\u5907\u9a71\u52a8\uff1b<br \/>\n\u5f00\u53d1\u73af\u5883\uff1aUbuntu 18.04 LTS<\/p>\n","protected":false},"author":1,"featured_media":554,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,5,49],"tags":[51,50],"_links":{"self":[{"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/552"}],"collection":[{"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":2,"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":903,"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions\/903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/media\/554"}],"wp:attachment":[{"href":"https:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}