📄 char_dev_example.c
字号:
#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <asm/uaccess.h>#include <asm/arch-s3c2410/regs-serial.h>#include <asm/io.h>#include <linux/slab.h>#define SYSCFG S3C24XX_VA_UART1 #define UART_ULCON (*((volatile unsigned *)(SYSCFG + 0x00)) )#define UART_UCON (*((volatile unsigned *)(SYSCFG + 0x04)) )#define UART_UTRSTAT (*((volatile unsigned *)(SYSCFG + 0x10)) )#define UART_UTXH (*((volatile unsigned *)(SYSCFG + 0x20)) )#define UART_URXH (*((volatile unsigned *)(SYSCFG + 0x24) ))#define UART_UBRDIV (*((volatile unsigned *)(SYSCFG + 0x28) ))#define GET_CHAR() (*((volatile unsigned *)(SYSCFG + 0x24)))#define PUT_CHAR(c) (*((volatile unsigned *)(SYSCFG + 0x20)) = (unsigned)(c))#define AKAE_MAJOR 240MODULE_AUTHOR("Huyongfu");MODULE_DESCRIPTION("module example");MODULE_LICENSE("GPL");ssize_t akae_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos){ int i = 0; char *head = (char *)kmalloc(count,GFP_KERNEL); char *p = head;; while (i < count) { if ((UART_UTRSTAT & 0x01)== 0x01) { p[i++] = GET_CHAR(); } else { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(10); } } p[i] = 0; copy_to_user(buf,head,count); kfree(p); return count;}ssize_t akae_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_ops){ int i = 0; char *head = (char *)kmalloc(count,GFP_KERNEL); char *p = head;; copy_from_user(head,buf,count); while (i < count) { if ((UART_UTRSTAT & 0x02) == 0x02) { PUT_CHAR(p[i++]); } else { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1 * HZ); } } kfree(p); return i;}int akae_release(struct inode *inode,struct file *filp){ return 0;}struct file_operations akae_fops = { .owner = THIS_MODULE, .read = akae_read, .write = akae_write, .release = akae_release,}; int __initakae_init(void){ int rc; printk("Test char dev\n"); UART_UCON |= 0x05; UART_ULCON |= 0x03; UART_UBRDIV = 26; /* (int)(50000000 / (115200 * 16)) -1 = 26 */ rc = register_chrdev(AKAE_MAJOR,"akae",&akae_fops); if (rc < 0) { printk("register %s char dev error\n","akae"); return -1; } printk("ok\n"); return 0;} void __exitakae_exit(void){ unregister_chrdev(AKAE_MAJOR,"akae"); printk("module exit\n"); return ;}module_init(akae_init);module_exit(akae_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -