void exampledev_init(void) { if (register_chrdev(MAJOR_NUM, ' exampledev ', &exampledev_fops)) TRACE_TXT('Device exampledev driver registered error'); else TRACE_TXT('Device exampledev driver registered successfully'); …//设备初始化 } |
struct file_operations { int (*lseek)(); int (*read)(); int (*write)(); int (*readdir)(); int (*select)(); int (*ioctl)(); int (*mmap)(); int (*open)(); void(*release)(); int (*fsync)(); int (*fasync)(); int (*check_media_change)(); void(*revalidate)(); }; |
int open(struct inode * inode ,struct file * file); |
void release (struct inode * inode ,struct file * file) ; |
void read(struct inode * inode ,struct file * file ,char * buf ,int count) ; |
void put_user_byte (char data_byte ,char * u_addr) ; void put_user_word (short data_word ,short * u_addr) ; void put_user_long(long data_long ,long * u_addr) ; void memcpy_tofs (void * u_addr ,void * k_addr ,unsigned long cnt) ; |
void write (struct inode * inode ,struct file * file ,char * buf ,int count) ; |
unsigned char_get_user_byte (char * u_addr) ; unsigned char_get_user_word (short * u_addr) ; unsigned char_get_user_long(long * u_addr) ; unsigned memcpy_fromfs(void * k_addr ,void * u_addr ,unsigned long cnt) ; |
int ioctl (struct inode * inode ,struct file * file ,unsigned int cmd ,unsigned long arg); |
struct file_operations exampledev_fops { NULL , exampledev_read , exampledev_write , NULL , NULL , exampledev_ioctl , NULL , exampledev_open , exampledev_release , NULL , NULL , NULL , NULL } ; |
void kmalloc (size_t size ,int priority); |
# define kfree (n) kfree_s( (n) ,0) |
void kfree_s (void * ptr ,int size); |
int request_irq (unsigned int irq ,void( * handler) int ,unsigned long type ,char * name); |
#include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/miscdevice.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/poll.h> #include <linux/spinlock.h> #include <linux/irq.h> #include <linux/delay.h> #include <asm/hardware.h> #define DEVICE_NAME 'leds' /*定义led 设备的名字*/ #define LED_MAJOR 231 /*定义led 设备的主设备号*/ static unsigned long led_table[] = { /*I/O 方式led 设备对应的硬件资源*/ GPIO_B10, GPIO_B8, GPIO_B5, GPIO_B6, }; /*使用ioctl 控制led*/ static int leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case 0: case 1: if (arg > 4) { return -EINVAL; } write_gpio_bit(led_table[arg], !cmd); default: return -EINVAL; } } static struct file_operations leds_fops = { owner: THIS_MODULE, ioctl: leds_ioctl, }; static devfs_handle_t devfs_handle; static int __init leds_init(void) { int ret; int i; /*在内核中注册设备*/ ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops); if (ret < 0) { printk(DEVICE_NAME ' can't register major number\n'); return ret; } devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, LED_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR, &leds_fops, NULL); /*使用宏进行端口初始化,set_gpio_ctrl 和write_gpio_bit 均为宏定义*/ for (i = 0; i < 8; i++) { set_gpio_ctrl(led_table[i] | GPIO_PULLUP_EN | GPIO_MODE_OUT); write_gpio_bit(led_table[i], 1); } printk(DEVICE_NAME ' initialized\n'); return 0; } static void __exit leds_exit(void) { devfs_unregister(devfs_handle); unregister_chrdev(LED_MAJOR, DEVICE_NAME); } module_init(leds_init); module_exit(leds_exit); |
#arm-linux-gcc -D__KERNEL__ -I/arm/kernel/include -DKBUILD_BASENAME=leds -DMODULE -c -o leds.o leds.c |
#insmod /lib/ leds.o |
#rmmod leds |
联系客服