static int xxxopenstruct inode inode struct file file

  • Slides: 20
Download presentation

/* 打開設備模組 */ static int xxx_open(struct inode *inode, struct file *file) { /*…………*/ }

/* 打開設備模組 */ static int xxx_open(struct inode *inode, struct file *file) { /*…………*/ } /* 讀設備模組 */ static int xxx_read(struct inode *inode, struct file *file) { /*…………*/ } /* 寫設備模組 */ static int xxx_write(struct inode *inode, struct file *file) { /*…………*/ } /* 控制設備模組 */ static int xxx_ioctl(struct inode *inode, struct file *file) { /*…………*/ } /* 中斷處理模組 */ static void xxx_interrupt(int irq, void *dev_id, struct pt_regs *regs) { /*. . . */ }

/* 設備檔操作介面 */ static struct file_operations xxx_fops = { read: xxx_read, /* 讀設備操作*/ write:

/* 設備檔操作介面 */ static struct file_operations xxx_fops = { read: xxx_read, /* 讀設備操作*/ write: xxx_write, /* 寫設備操作*/ ioctl: xxx_ioctl, /* 控制設備操作*/ open: xxx_open, /* 打開設備操作*/ release: xxx_release /* 釋放設備操作*/ /*. . . */ }; static int __init xxx_init_module (void) { /*. . . */ } static void __exit demo_cleanup_module (void) { pci_unregister_driver(&demo_pci_driver); } /* 載入驅動程式模組入口 */ module_init(xxx_init_module); /* 卸載驅動程式模組入口 */ module_exit(xxx_cleanup_module);

struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t

struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long); };

struct file { struct list_head struct dentry struct vfsmount struct file_operations atomic_t unsigned int

struct file { struct list_head struct dentry struct vfsmount struct file_operations atomic_t unsigned int mode_t loff_t unsigned long struct fown_struct unsigned int unsigned long void struct kiobuf long f_list; *f_dentry; *f_vfsmnt; *f_op; f_count; f_flags; f_mode; f_pos; f_reada, f_ramax, f_raend, f_ralen, f_rawin; f_owner; f_uid, f_gid; f_error; f_version; *private_data; *f_iobuf; f_iobuf_lock; /* needed for tty driver, and maybe others */ /* preallocated helper kiobuf to speedup O_DIRECT */ }; 驅動程式中常用的函數 int xxx_open(struct inode *inode, struct file *filp); int xxx_release(struct inode *inode, struc file *filp); ssize_t xxx_read(struct file *filp, char *buff,size_t count, loff_t *offp); ssize_t xxx_write(struct file *filp, const char *buff, size_t count , loff_t *offp);

實驗內容 1、dri_arh 模組載入實驗 • 編寫實驗程式 • #include <linux/string. h> • #include <linux/module. h> •

實驗內容 1、dri_arh 模組載入實驗 • 編寫實驗程式 • #include <linux/string. h> • #include <linux/module. h> • #include <linux/fs. h> • #include <linux/init. h> • #include <linux/types. h> • #include <linux/fs. h> • static int __init dri_arch_init_module(void) • { • printk("This is a simple driver-module!rn"); • return 0; • } • static void __exit dri_arch_cleanup_module(void) • { • printk("Goodbye driver-module!rn"); • } • module_init(dri_arch_init_module); • module_exit(dri_arch_cleanup_module);

編譯 make 使用 Makefile檔 CC =/opt/xscalev 1/bin/arm-linux-gcc INCLUDEDIR = /root/xsbase/Xsbase 270_Linux_F/Kernel/linux-2. 4. 21 -emdoor/include

編譯 make 使用 Makefile檔 CC =/opt/xscalev 1/bin/arm-linux-gcc INCLUDEDIR = /root/xsbase/Xsbase 270_Linux_F/Kernel/linux-2. 4. 21 -emdoor/include CFLAGS = -D__KERNEL__ -DMODULE -Wall -O 2 CFLAGS += -I. . -I$(INCLUDEDIR) DEBUG = TARGET = dri_arch OBJS = $(TARGET). o SRC = dri_arch. c All: dri_arch. o: dri_arch. c $(CC) $(CFLAGS) $(DEBUG) -c -o dri_arch. c clean : rm -rf *. o

END~

END~