mycdev.c:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
module_license("gpl");
#define mycdev_major 231 //cat /proc/devices檢視系統中未使用的字元裝置主裝置號
#define mycdev_size 1024
static int mycdev_open(struct inode *inode, struct file *fp)
static int mycdev_release(struct inode *inode, struct file *fp)
static ssize_t mycdev_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)
printk("reader: %d bytes was read...\n",count);
return count;
}static ssize_t mycdev_write(struct file *fp, const char __user *buf, size_t size, loff_t *pos)
static const struct file_operations mycdev_fops =
;static int __init mycdev_init(void)
else
return 0;
}static void __exit mycdev_exit(void)
module_init(mycdev_init);
module_exit(mycdev_exit);
先編譯mycdev.c模組,並把mycdev.ko插入核心。
然後建立裝置檔案結點sudo mknod /dev/mycdev c 231 0;(231的**看**中的注釋)
修改裝置檔案許可權:sudo chmod 777 /dev/mycdev.
編寫使用者態測試程式:test.c
#include
#include
#include
#include
#include
int main()
if(ret = read(testdev, buf, 10) < 10)
for (i=0; i<10; i++)
printf("\n");
close(testdev);
return 0;
}使用者態測試程式結果:
dmesg結果:
mycdev.ko解除安裝後使用者態測試程式:
簡單字元驅動程式mycdev c
root bogon 桌面 gedit mycdev.c include include include include include include module author me module license gpl define mycdev major 231 給定的主裝置號 defin...
簡單字元裝置驅動程式 以及 相應測試程式
include include include include include include include include include define device name embedsky leds 載入模式後,執行 cat proc devices 命令看到的裝置名稱 define le...
linux 驅動程式 高階字元驅動程式
ioctl方法 驅動程式的原型實現 int ioctl struct inode inode,struct file filp,unsigned int cmd,unsigned long arg ioctl 命令選擇 位段結構 number direction ioc read ioc write...