{"id":52036,"date":"2024-12-03T11:21:53","date_gmt":"2024-12-03T03:21:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52036\/"},"modified":"2024-12-03T11:21:53","modified_gmt":"2024-12-03T03:21:53","slug":"linux%e4%b8%8b%e6%9c%89%e5%93%aa%e4%ba%9b%e5%ad%97%e7%ac%a6%e8%ae%be%e5%a4%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52036\/","title":{"rendered":"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300alinux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<blockquote>\n<p>linux\u5b57\u7b26\u8bbe\u5907\u6709\uff1a1\u3001\u9f20\u6807\uff0c\u662f\u8ba1\u7b97\u673a\u7684\u4e00\u79cd\u5916\u63a5\u8f93\u5165\u8bbe\u5907\uff0c\u4e5f\u662f\u8ba1\u7b97\u673a\u663e\u793a\u7cfb\u7edf\u7eb5\u6a2a\u5750\u6807\u5b9a\u4f4d\u7684\u6307\u793a\u5668\uff1b2\u3001\u952e\u76d8\uff0c\u662f\u7528\u4e8e\u64cd\u4f5c\u8ba1\u7b97\u673a\u8bbe\u5907\u8fd0\u884c\u7684\u4e00\u79cd\u6307\u4ee4\u548c\u6570\u636e\u8f93\u5165\u88c5\u7f6e\uff1b3\u3001\u4e32\u884c\u7aef\u53e3\u7ec8\u7aef\uff0c\u4f7f\u7528\u8ba1\u7b97\u673a\u4e32\u884c\u7aef\u53e3\u8fde\u63a5\u7684\u7ec8\u7aef\u8bbe\u5907\uff1b4\u3001\u63a7\u5236\u7ec8\u7aef\uff1b5\u3001\u63a7\u5236\u53f0\u7b49\u3002<\/p>\n<\/blockquote>\n<h3><strong>linux\u5b57\u7b26\u8bbe\u5907<\/strong><\/h3>\n<p>\u5b57\u7b26\u8bbe\u5907\u662fLinux\u4e09\u5927\u8bbe\u5907\u4e4b\u4e00\uff08\u53e6\u5916\u4e24\u79cd\u662f\u5757\u8bbe\u5907\uff0c\u7f51\u7edc\u8bbe\u5907\uff09\u3002\u5b83\u4eec\u5747\u4ee5\u4e00\u4e2a\u6587\u4ef6\u8282\u70b9\u5f62\u5f0f\u663e\u793a\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\/dev\u76ee\u5f55\u4e0b(crw&#8211;w&#8212;- 1 root tty 4\uff0c 0 7\u6708 11 09:11 tty0 \u5176\u4e2dc\u4ee3\u8868\u5b57\u7b26\u8bbe\u5907\u7c7b\u578b)\u3002<\/p>\n<p>\u5b57\u7b26\u8bbe\u5907\u662f\u6307\u8bbe\u5907\u65e0\u9700\u7f13\u51b2\u5373\u53ef\u76f4\u63a5\u8fdb\u884c\u8bfb\u5199\u7684\u8bbe\u5907\uff0c \u5982\u9f20\u6807\uff0c\u952e\u76d8\uff0c\u4e32\u53e3\u8bbe\u5907\u3001\u8c03\u5236\u89e3\u8c03\u5668\u7b49\uff0c \u5b83\u4e0e\u5757\u8bbe\u5907\u7684\u533a\u522b\u5728\u4e8e\u662f\u5b57\u7b26\u64cd\u4f5c\u7684\u57fa\u672c\u5355\u4f4d\u662f\u5b57\u8282\u3002<\/p>\n<p><strong>\u5b57\u7b26\u8bbe\u5907\u7684\u5206\u7c7b<\/strong><\/p>\n<p>\u5b57\u7b26\u8bbe\u5907\u4e3b\u8981\u5305\u62ec\u63a7\u5236\u7ec8\u7aef\u8bbe\u5907\u548c\u4e32\u884c\u7ec8\u7aef\u8bbe\u5907\uff0c \u4f8b\u5982\u63a7\u5236\u53f0\u548c\u952e\u76d8\u3002\u4f9d\u636e\u529f\u80fd\u548c\u786c\u4ef6\u4e0a\u7684\u5dee\u522b\uff0c \u5b57\u7b26\u7ec8\u7aef\u8bbe\u5907\u6709\u5982\u4e0b\u5206\u7c7b:<\/p>\n<ul>\n<li>\n<p>\u4e32\u884c\u7aef\u53e3\u7ec8\u7aef(\/dev\/ttSn)\uff1a\u4f7f\u7528\u8ba1\u7b97\u673a\u4e32\u884c\u7aef\u53e3\u8fde\u63a5\u7684\u7ec8\u7aef\u8bbe\u5907\uff0c \u4e32\u884c\u8bbe\u5907\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u4e3a\u540c\u4e00\u5b57\u7b268\u4e2abit\u5355\u7ebf\u4f20\u8f93\uff0c \u5728\u547d\u4ee4\u884c\u8f93\u5165 echo &#8216;hello world&#8217; &gt; \/dev\/ttyS0\u53ef\u5c06\u8f93\u5165\u5199\u5165\u5230\u5bf9\u5e94\u8bbe\u5907\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f2a\u7ec8\u7aef(\/dev\/ttyp\uff0c\/dev\/ptyp): \u5bf9\u5e94\u5e95\u5c42\u4e0d\u5b58\u5728\u771f\u5b9e\u7684\u786c\u4ef6\u8bbe\u5907\uff0c \u7528\u4e8e\u4e3a\u5176\u4ed6\u7a0b\u5e8f\u63d0\u4f9b\u7ec8\u7aef\u5f0f\u6837\u7684\u63a5\u53e3\uff0c\u5982\u7f51\u7edc\u767b\u9646\u4e3b\u673a\u65f6\u7f51\u7edc\u670d\u52a1\u5668\u548cshell\u7a0b\u5e8f\u4e4b\u95f4\u7684\u7ec8\u7aef\u63a5\u53e3\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a7\u5236\u7ec8\u7aef(\/dev\/tty)\uff1a\u4e3b\u8bbe\u5907\u53f7\u4e3a5\uff0c \u8fdb\u7a0b\u63a7\u5236\u7ec8\u7aef\uff0c\u4e0e\u8fdb\u7a0b\u76f8\u5173\u8054\uff0c\u5982\u767b\u9646shell\u8fdb\u7a0b\u4f7f\u7528\u7684\u5c31\u662f\u7ec8\u7aef\/dev\/tty\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a7\u5236\u53f0(\/dev\/ttyn\uff0c\/dev\/consol): \u8ba1\u7b97\u673a\u8f93\u5165\u8f93\u51fa\u7684\u663e\u793a\u5668\uff0c\u5f53\u63a7\u5236\u53f0\u767b\u9646\u65f6\uff0c \u4f7f\u7528\u7684\u5c31\u662ftty1\uff0c \u800cubuntu \u56fe\u5f62\u754c\u9762\u4f7f\u7528\u7684tty7\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u7c7b\u578b\uff1a\u73b0\u884c\u7684linux\u9488\u5bf9\u8bb8\u591a\u4e0d\u540c\u7684\u8bbe\u5907\u5efa\u6709\u8bb8\u591a\u5176\u4ed6\u79cd\u7c7b\u7684\u8bbe\u5907\u7279\u6b8a\u6587\u4ef6\uff0c\u5982ISIDIN\u8bbe\u5907\u7684\/dev\/ttyIn\u8bbe\u5907\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u5b57\u7b26\u8bbe\u5907\u7684\u6027\u8d28\u53ca\u7279\u70b9<\/strong><\/p>\n<ul>\n<li>\n<p>\u5b57\u7b26\u8bbe\u5907\u5c5e\u4e8e\u8bbe\u5907\u6587\u4ef6\u7cfb\u7edf\u7684\u4e00\u79cd\uff0c \u76f8\u5f53\u4e8e\u5e95\u5c42\u786c\u4ef6\u5411\u4e0a\u5c42\u63d0\u4f9b\u7684\u903b\u8f91\u8bbe\u5907\u6587\u4ef6\uff0c \u5b9b\u5982\u5c06\u4e00\u4e2a\u6570\u636e\u7aef\u53e3(\u6570\u636e\u5bc4\u5b58\u5668)\u4e0e\u4e00\u4e2a\u6587\u4ef6\u5bf9\u63a5\u8d77\u6765\uff0c\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u76f4\u63a5\u5bf9\u6587\u4ef6\u64cd\u4f5c\uff0c \u4e8e\u662f\u4fbf\u76f4\u63a5\u5bf9\u7aef\u53e3\u8fdb\u884c\u4e86\u8bfb\u5199\u64cd\u4f5c\u3002 \u540c\u6837\u4f5c\u4e3a\u6587\u4ef6\uff0c \u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u4e5f\u5fc5\u987b\u5b9e\u73b0\u6587\u4ef6\u7684\u57fa\u672c\u7684\u64cd\u4f5copen()\uff0cclose()\uff0cwrite()\uff0cread()\u7b49\uff0c\u5f53\u7136\u7ec8\u7aef\u91cd\u5b9a\u5411\u64cd\u4f5c\u4e5f\u662f\u652f\u6301\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b57\u7b26\u8bbe\u5907\u6587\u4ef6\u6587\u4ef6\u7684\u8bfb\u5199\u662f\u4ee5\u5355\u4e2a\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\uff0c \u4e0d\u9700\u8981\u8bbe\u7acb\u786c\u4ef6\u7f13\u51b2\u533a\u3002 \u8bbe\u5907\u50cf\u8bbf\u95ee\u5b57\u8282\u6d41\u4e00\u6837\u88ab\u64cd\u4f5c\u7cfb\u7edf\u8bbf\u95ee\u3002 \u5b57\u8282\u6d41\u5c31\u50cf\u5728\u786c\u4ef6\u7aef\u53e3\u548c\u6587\u4ef6\u7cfb\u7edf\u642d\u5efa\u8d77\u4e86\u4e00\u4e2a\u4f20\u9001\u7ba1\u9053\uff0c \u5b57\u8282\u9010\u4e2a\u901a\u8fc7\u7ba1\u9053\u4f20\u8f93\u5e76\u5448\u73b0\u7ed9\u8bfb\u5199\u53cc\u65b9\u3002 \u8fd9\u4e2a\u6d41\u7279\u6027\u5728\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u662f\u4ee5\u7f13\u51b2\u961f\u5217\u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5982: \u63a7\u5236\u53f0\u7684\u7ed3\u6784\u4f53\u4e2d\u7684\u8bfb\u5199\u7f13\u51b2\u961f\u5217<\/p>\n<\/li>\n<\/ul>\n<pre>struct&nbsp;tty_struct&nbsp;{\nstruct&nbsp;termios&nbsp;termios;\nint&nbsp;pgrp;\nint&nbsp;stopped;\nvoid&nbsp;(*write)(struct&nbsp;tty_struct&nbsp;*&nbsp;tty);\nstruct&nbsp;tty_queue&nbsp;read_q;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u8bfb\u961f\u5217\nstruct&nbsp;tty_queue&nbsp;write_q;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u5199\u961f\u5217\nstruct&nbsp;tty_queue&nbsp;secondary;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/tty\u8f85\u52a9\u961f\u5217(\u5b58\u653e\u89c4\u683c\u5316\u540e\u7684\u5b57\u7b26)\n};<\/pre>\n<ul>\n<li>\n<p>\u5b57\u7b26\u8bbe\u5907\u7531\u5b57\u7b26\u8bbe\u5907\u53f7\u6807\u8bc6\u3002\u5b57\u7b26\u8bbe\u5907\u53f7\u7531\u4e3b\u8bbe\u5907\u53f7\u548c\u6b21\u8bbe\u5907\u53f7\u6784\u6210\uff0c \u4f8b\u5982\/dev\/ttyS0\u7684\u8bbe\u5907\u53f7\u4e3a(4\uff0c64); \u4e3b\u8bbe\u5907\u53f7\u6807\u8bc6\u8bbe\u5907\u5bf9\u5e94\u9a71\u52a8\u7a0b\u5e8f\uff0c \u5185\u6838\u901a\u8fc7\u4e3b\u8bbe\u5907\u53f7\u5c06\u8bbe\u5907\u548c\u9a71\u52a8\u7a0b\u5e8f\u4e00\u4e00\u5bf9\u5e94\u8d77\u6765\uff0c \u6b21\u8bbe\u5907\u53f7\u7531\u9a71\u52a8\u7a0b\u5e8f\u4f7f\u7528\uff0c \u7528\u4e8e\u9a71\u52a8\u7a0b\u5e8f\u5185\u90e8\u533a\u5206\u8bbe\u5907\u7ec6\u8282\u5dee\u522b\u4f7f\u7528\u7684\u4ee3\u7801\uff0c\u5185\u6838\u5176\u4ed6\u90e8\u5206\u4e0d\u4f7f\u7528\u5b83\u3002<\/p>\n<\/li>\n<\/ul>\n<h3><strong>\u5b57\u7b26\u8bbe\u5907\u5728\u5e94\u7528\u5c42\u5982\u4f55\u4f53\u73b0<\/strong><\/h3>\n<p><code>cat \/proc\/devices<\/code> \u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u5b57\u7b26\u8bbe\u5907\u548c\u5757\u8bbe\u5907\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/17124563126612027838618.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/1712456312661202783d149.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe1\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe1\" \/><\/p>\n<p>\u5728Linux \u4e2d\u4e00\u5207\u63a5\u6587\u4ef6\uff0c\u8bbe\u5907\u4e5f\u88ab\u62bd\u8c61\u6210\u6587\u4ef6\uff0c\u5728\/dev\/ \u76ee\u5f55\u4e0b\u53ef\u4ee5\u67e5\u770b\u5230\u5b57\u7b26\u8bbe\u5907\u548c\u5757\u8bbe\u5907\u7684\u5bf9\u5e94\u7684\u6587\u4ef6\u3002<br \/> \u4f8b\u5982\u8fd9\u662f\u4e24\u4e2a\u4e32\u53e3\u8bbe\u5907\u6587\u4ef6\uff0c\u4f7f\u7528ls -l \u67e5\u770b\u5b83\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<br \/> \u4e0e\u666e\u901a\u6587\u4ef6\u4e0d\u540c\u7684\u662f\u8bbe\u5907\u6587\u4ef6\u6ca1\u6709\u5927\u5c0f\uff0c\u800c\u662f\u88ab\u66ff\u6362\u6210\u4e86\u8bbe\u5907\u53f7\uff08\u4e3b\u8bbe\u5907\u53f7\u548c\u6b21\u8bbe\u5907\u53f7\uff09\uff0c\u8bbe\u5907\u53f7\u53ef\u4ee5\u4e0e\/proc\/devices \u4e2d\u7684\u4fe1\u606f\u76f8\u5bf9\u5e94\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/171245631266120278406c5.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe2\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe2\" \/><\/p>\n<p>\u5982\u4f55\u8bbf\u95ee\u4e00\u4e2a\u8bbe\u5907\uff1f<\/p>\n<p>\u65e2\u7136\u5b83\u88ab\u62bd\u8c61\u6210\u4e86\u4e00\u4e2a\u6587\u4ef6\uff0c\u90a3\u4e48\u81ea\u7136\u5c31\u7528\u6587\u4ef6IO (open\u3001read\u3001write \u7b49\u7b49) \u6765\u8bbf\u95ee\u3002<\/p>\n<h3><strong>\u8bbe\u5907\u53f7<\/strong><\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/17124563126612027843e3e.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe3\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe3\" \/><\/p>\n<p><code>dev_t dev = MKDEV(major,minor)<\/code><br \/><code>major = MAJOR(dev)<\/code><br \/><code>minor = MINOR(dev)<\/code><\/p>\n<p>\u8bbe\u5907\u53f7\u7531major\u548cminor \u7ec4\u6210\uff0c\u603b\u517132\u4f4d\uff0c\u9ad812\u4f4d\u4e3amajor\uff0c\u4f4e20\u4f4d\u4e3aminor\u3002<strong>\u6bcf\u4e00\u4e2a\u8bbe\u5907\u53f7\u90fd\u552f\u4e00\u5bf9\u5e94\u7740\u4e00\u4e2acdev\u7ed3\u6784\u4f53\u3002<\/strong><\/p>\n<p>\u6ce8\u518c\u5b57\u7b26\u8bbe\u5907\u9996\u5148\u8981\u7533\u8bf7\u8bbe\u5907\u53f7\uff0c\u53ef\u4ee5\u4e00\u6b21\u6279\u91cf\u7684\u7533\u8bf7\u591a\u4e2a\u8bbe\u5907\u53f7\uff08\u76f8\u540c\u7684major\uff0c\u4f9d\u6b21\u9012\u589e\u7684minor\uff09\u3002<br \/> \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u4e3b\u8bbe\u5907\u53f7\u7684\u8bdd\u5c31\u4f7f\u7528\u5982\u4e0b\u51fd\u6570\u6765\u7533\u8bf7\u8bbe\u5907\u53f7\uff1a<br \/><code>int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)<\/code><br \/> baseminor\uff1a\u8d77\u59cb\u7684minor\u503c\u3002<br \/> count\uff1a\u4e00\u5171\u7533\u8bf7\u51e0\u4e2a\u8bbe\u5907\u53f7\u3002\u7533\u8bf7\u5230\u7684\u8bbe\u5907\u53f7\u5728(MKDEV(major,baseminor) ~ MKDEV(major,baseminor+count)) \u8303\u56f4\u5185\u3002<br \/> \uff08\u81ea\u52a8\u7533\u8bf7\u8bbe\u5907\u53f7\u7684\u539f\u7406\uff0c\u5176\u5b9e\u662f\u4f20\u9012\u4e00\u4e2amajor = 0\uff0c\u7136\u540e\u7531\u5185\u6838\u5206\u914d\u4e00\u4e2a\u7a7a\u95f2\u7684\u8bbe\u5907\u53f7\u8fd4\u56de\uff09<\/p>\n<p>\u5982\u679c\u7ed9\u5b9a\u4e86\u8bbe\u5907\u7684\u4e3b\u8bbe\u5907\u53f7\u548c\u6b21\u8bbe\u5907\u53f7\u5c31\u4f7f\u7528\u5982\u4e0b\u6240\u793a\u51fd\u6570\u6765\u6ce8\u518c\u8bbe\u5907\u53f7\u5373\u53ef\uff1a<br \/><code>int register_chrdev_region(dev_t from, unsigned count, const char *name)<\/code><\/p>\n<p>\u91ca\u653e\u8bbe\u5907\u53f7\uff1a<br \/><code>void unregister_chrdev_region(dev_t from, unsigned count)<\/code><\/p>\n<h3><strong>\u5b57\u7b26\u8bbe\u5907\u7ed3\u6784\u4f53 cdev<\/strong><\/h3>\n<pre>\/\/include\/linux\/cdev.h\nstruct&nbsp;cdev&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;kobject&nbsp;kobj;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;module&nbsp;*owner;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;file_operations&nbsp;*ops;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;list_head&nbsp;list;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dev_t&nbsp;dev;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;count;\n};<\/pre>\n<p><strong>\u5e38\u7528<\/strong><\/p>\n<p>\u7533\u8bf7\u4e00\u4e2acdev \u5185\u5b58\uff1a<br \/><code>struct cdev *cdev_alloc(void);<\/code><br \/> \u521d\u59cb\u5316cdev-&gt;ops\uff0c\u5373cdev-&gt;ops = &amp;xxx_file_operation; \uff1a<br \/><code>void cdev_init(struct cdev *, const struct file_operations *);<\/code><br \/> \u5c06\u586b\u5145\u597d\u7684cdev \u5b9e\u4f8b\uff0c\u6dfb\u52a0\u5230cdev \u94fe\u8868\u3002\u610f\u5473\u7740\u5411\u5185\u6838\u6ce8\u518c\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\uff1a<br \/><code>int cdev_add(struct cdev *, dev_t, unsigned);<\/code> \/\/dev_t\uff1a\u6dfb\u52a0cdev\u65f6\u5fc5\u987b\u8981\uff0c\u4f20\u9012\u4e00\u4e2adev_t\uff0c\u5e76\u4e14\u5b83\u4e0ecdev\u662f\u552f\u4e00\u5bf9\u5e94\u7684\u3002<br \/> cdev_add \u6dfb\u52a0\u8fc7\u7a0b\u4e2d\u4f1a\u7ed1\u5b9acdev \u4e0edev_t\u3002<\/p>\n<p>\u4ece\u5185\u6838\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\uff1a<br \/><code>void cdev_del(struct cdev *);<\/code><\/p>\n<p><strong>\u4e0d\u5e38\u7528<\/strong><br \/> \u589e\u52a0cdev \u8c03\u7528\u8ba1\u6570\uff1a<br \/><code>void cdev_put(struct cdev *p);<\/code><\/p>\n<p><strong>\u603b\u7ed3\uff1a\u6ce8\u518c\u5b57\u7b26\u8bbe\u5907\u7684\u4e3b\u8981\u6d41\u7a0b\u5c31\u662f\uff0c\u7533\u8bf7\u8bbe\u5907\u53f7dev_t\uff0c\u521b\u5efa\u4e00\u4e2acdev\u3001\u521d\u59cb\u5316cdev (cdev-&gt;ops)\u3001\u5411\u5185\u6838\u6dfb\u52a0 cdev\uff08\u540c\u65f6\u4f1a\u7ed1\u5b9acdev \u4e0edev_t\uff09\u3002<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u5acc\u8fd9\u4e9b\u6b65\u9aa4\u592a\u9ebb\u70e6\u7684\u8bdd\uff0c\u5185\u6838\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u4e00\u6b65\u5230\u4f4d\u7684\u6ce8\u518c\u5b57\u7b26\u8bbe\u5907\u2014\u2014<code>__register_chrdev<\/code><br \/> \u5b83\u4f1a\u7533\u8bf7\u591a\u4e2a\u8bbe\u5907\u53f7\uff0c\u521b\u5efacdev\u5e76\u521d\u59cb\u5316\u5b83\uff0c\u7136\u540e\u7528\u8fd9\u591a\u4e2a\u8bbe\u5907\u53f7\u7ed1\u5b9a\u540c\u4e00\u4e2acdev \u6ce8\u518c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/171245631266120278462ef.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe4\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe4\" \/><\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a<code>register_chrdev<\/code>\uff0c\u5b83\u662f\u5bf9__register_chrdev \u7684\u5c01\u88c5\uff0c\u6b21\u8bbe\u5907\u53f7\u4ece\u57fa\u503c0\u5f00\u59cb\uff0c\u76f4\u63a5\u7533\u8bf7\u4e86256 \u4e2a\u6b21\u8bbe\u5907\u53f7\u3002<br \/><code>static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops) { return __register_chrdev(major, 0, 256, name, fops); }<\/code><\/p>\n<h4><strong>struct file_operations<\/strong><\/h4>\n<p>\u5b57\u7b26\u8bbe\u5907\u5728\/dev\/ \u76ee\u5f55\u4e0b\u521b\u5efa\u8bbe\u5907\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7struct file_operations \u5411\u5e94\u7528\u5c42\u63d0\u4f9b\u63a7\u5236\u63a5\u53e3\u3002\u5e94\u7528\u5c42\u5bf9\u5e94\u7684open\u3001read \u7b49\u51fd\u6570\u4f1a\u8c03\u7528\u5230file_operations \u5bf9\u5e94\u7684\u51fd\u6570\u3002<\/p>\n<pre>\/\/include\/linux\/fs.h\nstruct&nbsp;file_operations&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;module&nbsp;*owner;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loff_t&nbsp;(*llseek)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;loff_t,&nbsp;int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*read)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;char&nbsp;__user&nbsp;*,&nbsp;size_t,&nbsp;loff_t&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*write)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;const&nbsp;char&nbsp;__user&nbsp;*,&nbsp;size_t,&nbsp;loff_t&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*read_iter)&nbsp;(struct&nbsp;kiocb&nbsp;*,&nbsp;struct&nbsp;iov_iter&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*write_iter)&nbsp;(struct&nbsp;kiocb&nbsp;*,&nbsp;struct&nbsp;iov_iter&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*iterate)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;struct&nbsp;dir_context&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;(*poll)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;struct&nbsp;poll_table_struct&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;(*unlocked_ioctl)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;unsigned&nbsp;int,&nbsp;unsigned&nbsp;long);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;(*compat_ioctl)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;unsigned&nbsp;int,&nbsp;unsigned&nbsp;long);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*mmap)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;struct&nbsp;vm_area_struct&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*mremap)(struct&nbsp;file&nbsp;*,&nbsp;struct&nbsp;vm_area_struct&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*open)&nbsp;(struct&nbsp;inode&nbsp;*,&nbsp;struct&nbsp;file&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*flush)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;fl_owner_t&nbsp;id);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*release)&nbsp;(struct&nbsp;inode&nbsp;*,&nbsp;struct&nbsp;file&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*fsync)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;loff_t,&nbsp;loff_t,&nbsp;int&nbsp;datasync);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*aio_fsync)&nbsp;(struct&nbsp;kiocb&nbsp;*,&nbsp;int&nbsp;datasync);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*fasync)&nbsp;(int,&nbsp;struct&nbsp;file&nbsp;*,&nbsp;int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*lock)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;int,&nbsp;struct&nbsp;file_lock&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*sendpage)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;struct&nbsp;page&nbsp;*,&nbsp;int,&nbsp;size_t,&nbsp;loff_t&nbsp;*,&nbsp;int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;long&nbsp;(*get_unmapped_area)(struct&nbsp;file&nbsp;*,&nbsp;unsigned&nbsp;long,&nbsp;unsigned&nbsp;long,&nbsp;unsigned&nbsp;long,&nbsp;unsigned&nbsp;long);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*check_flags)(int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*flock)&nbsp;(struct&nbsp;file&nbsp;*,&nbsp;int,&nbsp;struct&nbsp;file_lock&nbsp;*);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*splice_write)(struct&nbsp;pipe_inode_info&nbsp;*,&nbsp;struct&nbsp;file&nbsp;*,&nbsp;loff_t&nbsp;*,&nbsp;size_t,&nbsp;unsigned&nbsp;int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;(*splice_read)(struct&nbsp;file&nbsp;*,&nbsp;loff_t&nbsp;*,&nbsp;struct&nbsp;pipe_inode_info&nbsp;*,&nbsp;size_t,&nbsp;unsigned&nbsp;int);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*setlease)(struct&nbsp;file&nbsp;*,&nbsp;long,&nbsp;struct&nbsp;file_lock&nbsp;**,&nbsp;void&nbsp;**);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;(*fallocate)(struct&nbsp;file&nbsp;*file,&nbsp;int&nbsp;mode,&nbsp;loff_t&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loff_t&nbsp;len);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;(*show_fdinfo)(struct&nbsp;seq_file&nbsp;*m,&nbsp;struct&nbsp;file&nbsp;*f);\n#ifndef&nbsp;CONFIG_MMU\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;(*mmap_capabilities)(struct&nbsp;file&nbsp;*);\n#endif\n};<\/pre>\n<p><strong>copy_to_user() \u4e0e copy_from_user()<\/strong><\/p>\n<p>\u4e3a\u4e86\u5b89\u5168\u8003\u8651\uff0c\u5e94\u7528\u8fdb\u7a0b\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5185\u6838\u6570\u636e\uff0c\u9700\u8981\u501f\u52a9\u8fd9\u4e24\u4e2a\u51fd\u6570\u62f7\u8d1d\uff1a<br \/><code>static inline int copy_to_user(void __user volatile *to, const void *from, unsigned long n)<\/code><br \/><code>static inline int copy_from_user(void *to, const void __user volatile *from, unsigned long n)<\/code><br \/> \u8fd4\u56de\u975e0 \u8868\u793a\u9519\u8bef\u3002<\/p>\n<h3><strong>\u81ea\u52a8\u521b\u5efa\u8bbe\u5907\u6587\u4ef6<\/strong><\/h3>\n<p>\u81ea\u52a8\u521b\u5efa\u8bbe\u5907\u8282\u70b9\u7684\u5de5\u4f5c\u662f\u5728\u9a71\u52a8\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\u4e2d\u5b8c\u6210\u7684\uff0c\u4e00\u822c\u5728 cdev_add \u51fd\u6570\u540e\u9762\u6dfb\u52a0\u81ea\u52a8\u521b\u5efa\u8bbe\u5907\u8282\u70b9\u76f8\u5173\u4ee3\u7801\u3002\u9996\u5148\u8981\u521b\u5efa\u4e00\u4e2a class \u7c7b\uff0c class \u662f\u4e2a\u7ed3\u6784\u4f53\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6include\/linux\/device.h \u91cc\u9762\u3002<\/p>\n<p>\u4f7f\u7528 class_create \u521b\u5efa\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<pre>extern&nbsp;struct&nbsp;class&nbsp;*&nbsp;__must_check&nbsp;__class_create(struct&nbsp;module&nbsp;*owner,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;char&nbsp;*name,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;lock_class_key&nbsp;*key);\n\n#define&nbsp;class_create(owner,&nbsp;name)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\\\n({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;struct&nbsp;lock_class_key&nbsp;__key;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\\\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__class_create(owner,&nbsp;name,&nbsp;&amp;__key);&nbsp;&nbsp;&nbsp;&nbsp;\\\n})<\/pre>\n<p>\u4f7f\u7528class_destroy \u6467\u6bc1\u4e00\u4e2a\u7c7b\uff1a<br \/><code>extern void class_destroy(struct class *cls);<\/code><\/p>\n<pre>struct&nbsp;class&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*name;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*owner;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;class_attribute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*class_attrs;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;attribute_group&nbsp;&nbsp;&nbsp;&nbsp;**dev_groups;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;kobject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*dev_kobj;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*dev_uevent)(struct&nbsp;device&nbsp;*dev,&nbsp;struct&nbsp;kobj_uevent_env&nbsp;*env);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*(*devnode)(struct&nbsp;device&nbsp;*dev,&nbsp;umode_t&nbsp;*mode);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;(*class_release)(struct&nbsp;class&nbsp;*class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;(*dev_release)(struct&nbsp;device&nbsp;*dev);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*suspend)(struct&nbsp;device&nbsp;*dev,&nbsp;pm_message_t&nbsp;state);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;(*resume)(struct&nbsp;device&nbsp;*dev);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;kobj_ns_type_operations&nbsp;*ns_type;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;void&nbsp;*(*namespace)(struct&nbsp;device&nbsp;*dev);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;dev_pm_ops&nbsp;*pm;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;subsys_private&nbsp;*p;\n};<\/pre>\n<p>\u5728\u521b\u5efa\u5b8c\u7c7b\u540e\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u8bbe\u5907\uff0c\u4f7f\u7528 device_create\u521b\u5efa\u4e00\u4e2a\u8bbe\u5907\uff1a<br \/><code>struct device *device_create(struct class *cls, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...);<\/code><\/p>\n<p>\u6467\u6bc1\u4e00\u4e2a\u8bbe\u5907\uff1a<br \/><code>extern void device_destroy(struct class *cls, dev_t devt);<\/code><\/p>\n<p>\u521b\u5efa\u7c7b\u4f1a\u5728\/sys\/class\/ \u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u5c5e\u4e8e\u6b64\u7c7b\u7684\u8bbe\u5907\u6587\u4ef6\u5939\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/1712456312661202784771d.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe5\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe5\" \/><\/p>\n<h3><strong>IS_ERR \u548c PTR_ERR<\/strong><\/h3>\n<p>IS_ERR \u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff0cPTR_ERR \u5c06\u6307\u9488\u8f6c\u5316\u4e3a\u6570\u503c\u8fd4\u56de\u3002<\/p>\n<pre>static&nbsp;inline&nbsp;long&nbsp;__must_check&nbsp;PTR_ERR(const&nbsp;void&nbsp;*ptr)\n{\n\treturn&nbsp;(long)&nbsp;ptr;\n}\n\nstatic&nbsp;inline&nbsp;long&nbsp;__must_check&nbsp;IS_ERR(const&nbsp;void&nbsp;*ptr)\n{\n\treturn&nbsp;IS_ERR_VALUE((unsigned&nbsp;long)ptr);\n}<\/pre>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>#include&nbsp;&lt;linux\/fs.h&gt;\t\t&nbsp;\/\/file_operations\u58f0\u660e\n#include&nbsp;&lt;linux\/module.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;\/\/module_init&nbsp;&nbsp;module_exit\u58f0\u660e\n#include&nbsp;&lt;linux\/init.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/__init&nbsp;&nbsp;__exit&nbsp;\u5b8f\u5b9a\u4e49\u58f0\u660e\n#include&nbsp;&lt;linux\/device.h&gt;\t&nbsp;\/\/class&nbsp;&nbsp;devise\u58f0\u660e\n#include&nbsp;&lt;linux\/uaccess.h&gt;&nbsp;&nbsp;&nbsp;\/\/copy_from_user&nbsp;\u7684\u5934\u6587\u4ef6\n#include&nbsp;&lt;linux\/types.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u8bbe\u5907\u53f7&nbsp;&nbsp;dev_t&nbsp;\u7c7b\u578b\u58f0\u660e\n#include&nbsp;&lt;asm\/io.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ioremap&nbsp;iounmap\u7684\u5934\u6587\u4ef6\n\n#define&nbsp;DEVICE_CNT&nbsp;0\t\/\/\u8bbe\u5907\u53f7\u4e2a\u6570\n\nstruct&nbsp;led_device{\n\tdev_t&nbsp;devid;\t\/\/\u8bbe\u5907\u53f7\n\tint&nbsp;major;\t\/\/\u4e3b\u8bbe\u5907\u53f7\n\tint&nbsp;minor;\t\/\/\u6b21\u8bbe\u5907\u53f7\n\tchar*&nbsp;name&nbsp;=&nbsp;\"led\";\t\/\/\u9a71\u52a8\u540d\n\tstruct&nbsp;cdev&nbsp;led_dev;\t\/\/cdev&nbsp;\u7ed3\u6784\u4f53\n\tstruct&nbsp;class&nbsp;*class;\t\/*&nbsp;\u7c7b&nbsp;\t*\/\n\tstruct&nbsp;device*&nbsp;device;\t\/\/\u8bbe\u5907\n};\n\nstruct&nbsp;led_device&nbsp;led;\n\n\nstatic&nbsp;int&nbsp;led_open(struct&nbsp;inode&nbsp;*inode,&nbsp;struct&nbsp;file&nbsp;*filp)\n{\n\treturn&nbsp;0;\n}\n\nstatic&nbsp;ssize_t&nbsp;led_write(struct&nbsp;file&nbsp;*filp,&nbsp;const&nbsp;char&nbsp;__user&nbsp;*buf,&nbsp;size_t&nbsp;cnt,&nbsp;loff_t&nbsp;*offt)\n{\n\treturn&nbsp;0;\n}\n\n\nstatic&nbsp;ssize_t&nbsp;led_read(struct&nbsp;file&nbsp;*filp,&nbsp;char&nbsp;__user&nbsp;*buf,&nbsp;size_t&nbsp;cnt,&nbsp;loff_t&nbsp;*offt)\n{\n\treturn&nbsp;0;\n}\n\n\n\n\/*&nbsp;\u8bbe\u5907\u64cd\u4f5c\u51fd\u6570&nbsp;*\/\nstatic&nbsp;struct&nbsp;file_operations&nbsp;led_fo&nbsp;=&nbsp;{\n\t.owner&nbsp;=&nbsp;THIS_MODULE,\n\t.open&nbsp;=&nbsp;led_open,\n\t.read&nbsp;=&nbsp;led_read,\n\t.write&nbsp;=&nbsp;led_write,\n};\n\nstatic&nbsp;int&nbsp;_init&nbsp;led_init()\n{\n\t\/*\u6ce8\u518c\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8*\/\n\t\n\t\/*1.\u6ce8\u518c\u8bbe\u5907\u53f7*\/\n\tled.major&nbsp;=&nbsp;0;\t\/\/\u7531\u5185\u6838\u81ea\u52a8\u5206\u914d\u4e3b\u8bbe\u5907\u53f7\n\tif(led.major)\t\/\/\u5982\u679c\u5206\u914d\u4e86\u7684\u8bdd\u5c31\u6ce8\u518c\n\t{\n\t\tled.devid&nbsp;=&nbsp;MKDEV(led.major,0);\t\n\t\tregister_chrdev_region(led.devid,DEVICE_CNT,led.name);\t\/\/\u5c06\u9a71\u52a8\u6ce8\u518c\u5230\u5185\u6838\u4e2d\n\t}\n\telse{\t\t\/\/\u5982\u679c\u6ca1\u6709\u5206\u914d\u7684\u8bdd\n\t\t\t\t\t\/\/\u4ece0\u53f7\uff08\u6b21\u8bbe\u5907\u53f7\uff09\u5f00\u59cb\u7533\u8bf7\n\t\talloc_chrdev_region(&amp;led.devid,0,DEVICE_CNT,led.name);\t\t\/\/\u7533\u8bf7\u8bbe\u5907\u53f7\n\t\tled.major&nbsp;=&nbsp;MAJOR(led.devid);\t\/\/\u83b7\u53d6\u4e3b\u8bbe\u5907\u53f7\n\t\tled.minor&nbsp;=&nbsp;MANOR(led.devid);\t\/\/\u83b7\u53d6\u6b21\u8bbe\u5907\u53f7\n\t}\n\tprintk(\"newcheled&nbsp;major=%d,minor=%d\\r\\n\",newchrled.major,&nbsp;newchrled.minor);\t\n\n\t\/*2.\u521d\u59cb\u5316&nbsp;cdev&nbsp;\u7ed3\u6784\u4f53*\/\n\tled.led_dev.woner&nbsp;=&nbsp;THIS_MODULE;\n\tcdev_init(&amp;led.led_dev,&amp;led_fo);\t\/\/\u5c06\u64cd\u4f5c\u51fd\u6570\u521d\u59cb\u5316\u5230cdev\u7ed3\u6784\u4f53\n\n\t\/*3.\u5e94\u8be5\u662f\u5411\u94fe\u8868\u4e2d\u6dfbcdev*\/\n\tcdev_add(&amp;led.led_dev,led.devid,DEVICE_CNT);\t\n\n\t\/*4.\u521b\u5efa\u8282\u70b9*\/\n\tled.class&nbsp;=&nbsp;class_create(THIS_MODULE,led.name);\t\t\/\/\u5148\u521b\u5efa\u4e00\u4e2a\u7c7b\n\tled.device&nbsp;=&nbsp;device_create(led.class,NULL,led.devid,NULL);\t\/\/\u521b\u5efa\u8bbe\u5907\n\n\treturn&nbsp;0;\n\t\t\n}\n\nstatic&nbsp;void&nbsp;_exit&nbsp;led_exit()\n{\n\t\/*&nbsp;\u6ce8\u9500\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8&nbsp;*\/\n\tcdev_del(&amp;newchrled.cdev);\/*&nbsp;&nbsp;\u5220\u9664cdev&nbsp;*\/\n\tunregister_chrdev_region(newchrled.devid,&nbsp;NEWCHRLED_CNT);&nbsp;\/*&nbsp;\u6ce8\u9500\u8bbe\u5907\u53f7&nbsp;*\/\n\n\tdevice_destroy(newchrled.class,&nbsp;newchrled.devid);\n\tclass_destroy(newchrled.class);\n}\n\n\n\n\/*\u6ce8\u518c\u5b57\u7b26\u8bbe\u5907\u5165\u53e3\u4e0e\u5378\u8f7d\u5165\u53e3*\/\nmodule_init(led_init);\nmodule_exit(led_exit);\nMODULE_LICENSE(\"GPL&nbsp;v2\");\nMODULE_AUTHOR(\"zhoujianghong\");<\/pre>\n<p>\u5e94\u7528open\u5230file_operations-&gt;open \u7684\u8c03\u7528\u539f\u7406<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240407\/1712456312661202784c033.jpg\" class=\"aligncenter\" title=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe6\" alt=\"linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u63d2\u56fe6\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907 \u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300alinux\u4e0b\u6709\u54ea\u4e9b\u5b57\u7b26\u8bbe\u5907\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01 linux\u5b57\u7b26\u8bbe\u5907\u6709\uff1a1\u3001\u9f20\u6807\uff0c\u662f\u8ba1\u7b97\u673a\u7684\u4e00\u79cd\u5916\u63a5\u8f93\u5165\u8bbe\u5907\uff0c\u4e5f\u662f\u8ba1\u7b97\u673a\u663e\u793a\u7cfb\u7edf\u7eb5\u6a2a\u5750\u6807\u5b9a\u4f4d\u7684\u6307\u793a\u5668\uff1b2\u3001\u952e\u76d8\uff0c\u662f\u7528\u4e8e\u64cd\u4f5c\u8ba1\u7b97\u673a\u8bbe\u5907\u8fd0\u884c\u7684\u4e00\u79cd\u6307\u4ee4\u548c\u6570\u636e\u8f93\u5165\u88c5\u7f6e\uff1b3\u3001\u4e32\u884c\u7aef\u53e3\u7ec8\u7aef\uff0c\u4f7f\u7528\u8ba1\u7b97\u673a\u4e32\u884c\u7aef\u53e3\u8fde\u63a5\u7684\u7ec8\u7aef\u8bbe\u5907\uff1b4\u3001\u63a7\u5236\u7ec8\u7aef\uff1b5\u3001\u63a7\u5236\u53f0\u7b49\u3002 linux\u5b57\u7b26\u8bbe\u5907 \u5b57\u7b26\u8bbe\u5907\u662fLinux\u4e09\u5927\u8bbe\u5907\u4e4b\u4e00\uff08\u53e6\u5916\u4e24\u79cd\u662f\u5757\u8bbe\u5907\uff0c\u7f51\u7edc\u8bbe\u5907\uff09\u3002\u5b83\u4eec\u5747\u4ee5\u4e00\u4e2a\u6587\u4ef6\u8282\u70b9\u5f62\u5f0f\u663e\u793a\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\/dev\u76ee\u5f55\u4e0b(crw&#8211;w&#8212;- 1 root tty 4\uff0c 0 7\u6708 11 09:11 tty0 \u5176\u4e2dc\u4ee3\u8868\u5b57\u7b26\u8bbe\u5907\u7c7b\u578b)\u3002 \u5b57\u7b26\u8bbe\u5907\u662f\u6307\u8bbe\u5907\u65e0\u9700\u7f13\u51b2\u5373\u53ef\u76f4\u63a5\u8fdb\u884c\u8bfb\u5199\u7684\u8bbe\u5907\uff0c \u5982\u9f20\u6807\uff0c\u952e\u76d8\uff0c\u4e32\u53e3\u8bbe\u5907\u3001\u8c03\u5236\u89e3\u8c03\u5668\u7b49\uff0c \u5b83\u4e0e\u5757\u8bbe\u5907\u7684\u533a\u522b\u5728\u4e8e\u662f\u5b57\u7b26\u64cd\u4f5c\u7684\u57fa\u672c\u5355\u4f4d\u662f\u5b57\u8282\u3002 \u5b57\u7b26\u8bbe\u5907\u7684\u5206\u7c7b \u5b57\u7b26\u8bbe\u5907\u4e3b\u8981\u5305\u62ec\u63a7\u5236\u7ec8\u7aef\u8bbe\u5907\u548c\u4e32\u884c\u7ec8\u7aef\u8bbe\u5907\uff0c \u4f8b\u5982\u63a7\u5236\u53f0\u548c\u952e\u76d8\u3002\u4f9d\u636e\u529f\u80fd\u548c\u786c\u4ef6\u4e0a\u7684\u5dee\u522b\uff0c \u5b57\u7b26\u7ec8\u7aef\u8bbe\u5907\u6709\u5982\u4e0b\u5206\u7c7b: \u4e32\u884c\u7aef\u53e3\u7ec8\u7aef(\/dev\/ttSn)\uff1a\u4f7f\u7528\u8ba1\u7b97\u673a\u4e32\u884c\u7aef\u53e3\u8fde\u63a5\u7684\u7ec8\u7aef\u8bbe\u5907\uff0c \u4e32\u884c\u8bbe\u5907\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u4e3a\u540c\u4e00\u5b57\u7b268\u4e2abit\u5355\u7ebf\u4f20\u8f93\uff0c \u5728\u547d\u4ee4\u884c\u8f93\u5165 echo &#8216;hello world&#8217; &gt; \/dev\/ttyS0\u53ef\u5c06\u8f93\u5165\u5199\u5165\u5230\u5bf9\u5e94\u8bbe\u5907\u3002 \u4f2a\u7ec8\u7aef(\/dev\/ttyp\uff0c\/dev\/ptyp): \u5bf9\u5e94\u5e95\u5c42\u4e0d\u5b58\u5728\u771f\u5b9e\u7684\u786c\u4ef6\u8bbe\u5907\uff0c \u7528\u4e8e\u4e3a\u5176\u4ed6\u7a0b\u5e8f\u63d0\u4f9b\u7ec8\u7aef\u5f0f\u6837\u7684\u63a5\u53e3\uff0c\u5982\u7f51\u7edc\u767b\u9646\u4e3b\u673a\u65f6\u7f51\u7edc\u670d\u52a1\u5668\u548cshell\u7a0b\u5e8f\u4e4b\u95f4\u7684\u7ec8\u7aef\u63a5\u53e3\u3002 \u63a7\u5236\u7ec8\u7aef(\/dev\/tty)\uff1a\u4e3b\u8bbe\u5907\u53f7\u4e3a5\uff0c \u8fdb\u7a0b\u63a7\u5236\u7ec8\u7aef\uff0c\u4e0e\u8fdb\u7a0b\u76f8\u5173\u8054\uff0c\u5982\u767b\u9646shell\u8fdb\u7a0b\u4f7f\u7528\u7684\u5c31\u662f\u7ec8\u7aef\/dev\/tty\u3002 \u63a7\u5236\u53f0(\/dev\/ttyn\uff0c\/dev\/consol): \u8ba1\u7b97\u673a\u8f93\u5165\u8f93\u51fa\u7684\u663e\u793a\u5668\uff0c\u5f53\u63a7\u5236\u53f0\u767b\u9646\u65f6\uff0c \u4f7f\u7528\u7684\u5c31\u662ftty1\uff0c \u800cubuntu \u56fe\u5f62\u754c\u9762\u4f7f\u7528\u7684tty7\u3002 \u5176\u4ed6\u7c7b\u578b\uff1a\u73b0\u884c\u7684linux\u9488\u5bf9\u8bb8\u591a\u4e0d\u540c\u7684\u8bbe\u5907\u5efa\u6709\u8bb8\u591a\u5176\u4ed6\u79cd\u7c7b\u7684\u8bbe\u5907\u7279\u6b8a\u6587\u4ef6\uff0c\u5982ISIDIN\u8bbe\u5907\u7684\/dev\/ttyIn\u8bbe\u5907\u3002 \u5b57\u7b26\u8bbe\u5907\u7684\u6027\u8d28\u53ca\u7279\u70b9 \u5b57\u7b26\u8bbe\u5907\u5c5e\u4e8e\u8bbe\u5907\u6587\u4ef6\u7cfb\u7edf\u7684\u4e00\u79cd\uff0c \u76f8\u5f53\u4e8e\u5e95\u5c42\u786c\u4ef6\u5411\u4e0a\u5c42\u63d0\u4f9b\u7684\u903b\u8f91\u8bbe\u5907\u6587\u4ef6\uff0c \u5b9b\u5982\u5c06\u4e00\u4e2a\u6570\u636e\u7aef\u53e3(\u6570\u636e\u5bc4\u5b58\u5668)\u4e0e\u4e00\u4e2a\u6587\u4ef6\u5bf9\u63a5\u8d77\u6765\uff0c\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u76f4\u63a5\u5bf9\u6587\u4ef6\u64cd\u4f5c\uff0c \u4e8e\u662f\u4fbf\u76f4\u63a5\u5bf9\u7aef\u53e3\u8fdb\u884c\u4e86\u8bfb\u5199\u64cd\u4f5c\u3002 \u540c\u6837\u4f5c\u4e3a\u6587\u4ef6\uff0c \u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u4e5f\u5fc5\u987b\u5b9e\u73b0\u6587\u4ef6\u7684\u57fa\u672c\u7684\u64cd\u4f5copen()\uff0cclose()\uff0cwrite()\uff0cread()\u7b49\uff0c\u5f53\u7136\u7ec8\u7aef\u91cd\u5b9a\u5411\u64cd\u4f5c\u4e5f\u662f\u652f\u6301\u7684\u3002 \u5b57\u7b26\u8bbe\u5907\u6587\u4ef6\u6587\u4ef6\u7684\u8bfb\u5199\u662f\u4ee5\u5355\u4e2a\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\uff0c \u4e0d\u9700\u8981\u8bbe\u7acb\u786c\u4ef6\u7f13\u51b2\u533a\u3002 \u8bbe\u5907\u50cf\u8bbf\u95ee\u5b57\u8282\u6d41\u4e00\u6837\u88ab\u64cd\u4f5c\u7cfb\u7edf\u8bbf\u95ee\u3002 \u5b57\u8282\u6d41\u5c31\u50cf\u5728\u786c\u4ef6\u7aef\u53e3\u548c\u6587\u4ef6\u7cfb\u7edf\u642d\u5efa\u8d77\u4e86\u4e00\u4e2a\u4f20\u9001\u7ba1\u9053\uff0c \u5b57\u8282\u9010\u4e2a\u901a\u8fc7\u7ba1\u9053\u4f20\u8f93\u5e76\u5448\u73b0\u7ed9\u8bfb\u5199\u53cc\u65b9\u3002 \u8fd9\u4e2a\u6d41\u7279\u6027\u5728\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u662f\u4ee5\u7f13\u51b2\u961f\u5217\u6765\u5b9e\u73b0\u7684\u3002\u4f8b\u5982: \u63a7\u5236\u53f0\u7684\u7ed3\u6784\u4f53\u4e2d\u7684\u8bfb\u5199\u7f13\u51b2\u961f\u5217 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-52036","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52036"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52036\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}