#include #include #include #include #include MODULE_LICENSE("GPL"); #include #define DEVICE_NAME "HJ" #define HJ_LEN 32 #define HJ_MAGICNUM 254 #define HJ_READ _IOR(HJ_MAGICNUM,0,char*) #define HJ_WRITE _IOW(HJ_MAGICNUM,1,char*) static int HJ_Major=0; static char HJ_block[HJ_LEN]="\0"; static int HJ_open(struct inode *inode, struct file *filp); static int HJ_release(struct inode *inode, struct file *filp); static int HJ_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg); struct file_operations HJ_fops={ open: HJ_open, release: HJ_release, ioctl: HJ_ioctl, }; int init_module(void) { printk("Loading HJ Device Driver... \n"); if((HJ_Major=register_chrdev(0,DEVICE_NAME,&HJ_fops))<0){ printk(DEVICE_NAME" : Device registration failed (%d)\n", HJ_Major); return HJ_Major; } printk(DEVICE_NAME" : Device Driver registeration OK with Major Number = %d\n\n", HJ_Major); return 0; } void cleanup_module(void) { int nRetCode; printk("Unloading HJ Device Driver.. \n"); if((nRetCode=unregister_chrdev(HJ_Major,DEVICE_NAME))<0) printk(DEVICE_NAME" : Device unregisteration failed (%d)\n",nRetCode); else printk(DEVICE_NAME" : Device unregistration OK\n"); } int HJ_open(struct inode *inode, struct file *flip) { MOD_INC_USE_COUNT; printk(DEVICE_NAME" : Device open OK (%d, %d)\n\n",MAJOR(inode->i_rdev),MINOR(inode->i_rdev)); return 0; } int HJ_release(struct inode *inode, struct file *flip) { MOD_DEC_USE_COUNT; printk(DEVICE_NAME" : Device close OK(%d, %d)\n\n",MAJOR(inode->i_rdev),MINOR(inode->i_rdev)); return 0; } int HJ_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg) { int count; int length; char *buffer = (char *)arg; count=0; switch(cmd){ case HJ_READ: length = strlen(HJ_block); printk("READ : length= %d \n ",count); while(count=HJ_LEN) break; } printk("READ : length= %d \n ",count); break; case HJ_WRITE: length = strlen(buffer); while(count=HJ_LEN) break; } printk("WRITE : length= %d \n",count); break; } return 0; }