{"id":2977,"date":"2011-04-25T08:25:53","date_gmt":"2011-04-25T00:25:53","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2977"},"modified":"2011-05-02T14:08:26","modified_gmt":"2011-05-02T06:08:26","slug":"cdev","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2977","title":{"rendered":"\u52a8\u624b\u5b9e\u8df5\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8"},"content":{"rendered":"<p>\u4eca\u5e74\u5e26\u8f6f\u4ef608\u7ea7\u7ae5\u978bLinuxOS\u8bd5\u9a8c\u7684\u65f6\u5019\uff0c\u5bfc\u5e08\u8ba9\u6211\u5199\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\uff0c\u4ee5\u4fbf\u8ba9\u4ece\u672a\u63a5\u89e6\u8fc7\u7684\u521d\u5b66\u8005\u5feb\u901f\u5165\u95e8\u3002\u4ee3\u7801\u53c2\u8003\u5982\u4e0b\u3002<\/p>\n<h4><strong>\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\uff1a<\/strong><\/h4>\n<pre class=\"brush:c\">#include &lt; linux\/init.h &gt;\r\n#include &lt; linux\/module.h &gt;\r\n#include &lt; linux\/types.h &gt;\r\n#include &lt; linux\/fs.h &gt;\r\n#include &lt; asm\/uaccess.h &gt;\r\n#include &lt; linux\/cdev.h &gt;\r\n\r\nMODULE_AUTHOR(\"Edsionte Wu\");\r\nMODULE_LICENSE(\"GPL\");\r\n\r\n#define MYCDEV_MAJOR 231 \/*the predefined mycdev's major devno*\/\r\n#define MYCDEV_SIZE 100\r\n\r\nstatic int mycdev_open(struct inode *inode, struct file *fp)\r\n{\r\n\treturn 0;\r\n}\r\n\r\nstatic int mycdev_release(struct inode *inode, struct file *fp)\r\n{\r\n\treturn 0;\r\n}\r\n\r\nstatic ssize_t mycdev_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)\r\n{\r\n\tunsigned long p = *pos;\r\n\tunsigned int count = size;\r\n\tint i;\r\n\tchar kernel_buf[MYCDEV_SIZE] = \"This is mycdev!\";\r\n\r\n\tif(p &gt;= MYCDEV_SIZE)\r\n\t\treturn -1;\r\n\tif(count &gt; MYCDEV_SIZE)\r\n\t\tcount = MYCDEV_SIZE - p;\r\n\r\n\tif (copy_to_user(buf, kernel_buf, count) != 0) {\r\n\t\tprintk(\"read error!\\n\");\r\n\t\treturn -1;\r\n\t}\r\n\r\n\t\/*\r\n\tfor (i = 0; i &lt; count; i++) {\r\n\t\t__put_user(i, buf);\/\/write 'i' from kernel space to user space's buf;\r\n\t\tbuf++;\r\n\t}\r\n\t*\/\r\n\r\n\tprintk(\"edsionte's reader: %d bytes was read...\\n\", count);\r\n\treturn count;\r\n\r\n}\r\n\r\nstatic ssize_t mycdev_write(struct file *fp, const char __user *buf, size_t size, loff_t *pos)\r\n{\r\n\treturn size;\r\n}\r\n\r\n\/*filling the mycdev's file operation interface in the struct file_operations*\/\r\nstatic const struct file_operations mycdev_fops =\r\n{\r\n\t.owner = THIS_MODULE,\r\n\t.read = mycdev_read,\r\n\t.write = mycdev_write,\r\n\t.open = mycdev_open,\r\n\t.release = mycdev_release,\r\n};\r\n\r\n\/*module loading function*\/\r\nstatic int __init mycdev_init(void)\r\n{\r\n\tint ret;\r\n\r\n\tprintk(\"mycdev module is staring..\\n\");\r\n\r\n\tret=register_chrdev(MYCDEV_MAJOR,\"edsionte_cdev\",&amp;mycdev_fops);\r\n\tif(ret&lt;0)\r\n\t{\r\n\t\tprintk(\"register failed..\\n\");\r\n\t\treturn 0;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintk(\"register success..\\n\");\r\n\t}\r\n\r\n\treturn 0;\r\n}\r\n\r\n\/*module unloading function*\/\r\nstatic void __exit mycdev_exit(void)\r\n{\r\n\tprintk(\"mycdev module is leaving..\\n\");\r\n\tunregister_chrdev(MYCDEV_MAJOR,\"edsionte_cdev\");\r\n}\r\n\r\nmodule_init(mycdev_init);\r\nmodule_exit(mycdev_exit);<\/pre>\n<h4>Makefile\u6587\u4ef6:<\/h4>\n<pre class=\"brush:c\">obj-m:=mycdev.o\r\nPWD:=$(shell pwd)\r\nCUR_PATH:=$(shell uname -r)\r\nKERNEL_PATH:=\/usr\/src\/linux-headers-$(CUR_PATH)\r\n\r\nall:\r\n\tmake -C $(KERNEL_PATH) M=$(PWD) modules\r\nclean:\r\n\tmake -C $(KERNEL_PATH) M=$(PWD) clean<\/pre>\n<h4>\u7528\u6237\u6001\u6d4b\u8bd5\u7a0b\u5e8f\uff1a<\/h4>\n<pre class=\"brush:c\">#include &lt; stdio.h &gt;\r\n#include &lt; sys\/types.h &gt;\r\n#include &lt; sys\/stat.h &gt;\r\n#include &lt; fcntl.h &gt;\r\n#include &lt; stdlib.h &gt;\r\n\r\nint main()\r\n{\r\n\tint testdev;\r\n\tint i, ret;\r\n\tchar buf[15];\r\n\r\n\ttestdev = open(\"\/dev\/mycdev\", O_RDWR);\r\n\r\n\tif (-1 == testdev) {\r\n\t\tprintf(\"cannot open file.\\n\");\r\n\t\texit(1);\r\n\t}\r\n\r\n\tif (ret = read(testdev, buf, 15) &lt; 15) {\r\n\t\tprintf(\"read error!\\n\");\r\n\t\texit(1);\r\n\t}\r\n\r\n\tprintf(\"%s\\n\", buf);\r\n\r\n\tclose(testdev);\r\n\r\n\treturn 0;\r\n}<\/pre>\n<h4>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/h4>\n<p><strong>1.make\u7f16\u8bd1mycdev.c\u6587\u4ef6\uff0c\u5e76\u63d2\u5165\u5230\u5185\u6838\uff1b<br \/>\n2.\u901a\u8fc7cat \/proc\/devices \u67e5\u770b\u7cfb\u7edf\u4e2d\u672a\u4f7f\u7528\u7684\u5b57\u7b26\u8bbe\u5907\u4e3b\u8bbe\u5907\u53f7\uff0c\u6bd4\u5982\u5f53\u524d231\u672a\u4f7f\u7528\uff1b<br \/>\n3.\u521b\u5efa\u8bbe\u5907\u6587\u4ef6\u7ed3\u70b9\uff1asudo mknod \/dev\/mycdev c 231 0\uff1b\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u901a\u8fc7man mknod\u547d\u4ee4\u67e5\u770b\uff1b<br \/>\n4.\u4fee\u6539\u8bbe\u5907\u6587\u4ef6\u6743\u9650\uff1asudo chmod 777 \/dev\/mycdev\uff1b<br \/>\n5.\u4ee5\u4e0a\u6210\u529f\u5b8c\u6210\u540e\uff0c\u7f16\u8bd1\u672c\u7528\u6237\u6001\u6d4b\u8bd5\u7a0b\u5e8f\uff1b\u8fd0\u884c\u8be5\u7a0b\u5e8f\u67e5\u770b\u7ed3\u679c\uff1b<br \/>\n6.\u901a\u8fc7dmesg\u67e5\u770b\u65e5\u5fd7\u4fe1\u606f\uff1b<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5e74\u5e26\u8f6f\u4ef608\u7ea7\u7ae5\u978bLinuxOS\u8bd5\u9a8c\u7684\u65f6\u5019\uff0c\u5bfc\u5e08\u8ba9\u6211\u5199\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\uff0c\u4ee5\u4fbf\u8ba9\u4ece\u672a\u63a5\u89e6\u8fc7\u7684\u521d\u5b66\u8005\u5feb\u901f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2977","post","type-post","status-publish","format-standard","hentry","category-linuxc"],"views":11120,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2977"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2977\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2977"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}