staticintskeleton_open(structinode*i,structfile*f){pr_info("skeleton : open operation... major:%d, minor:%d\n",imajor(i),iminor(i));if((f->f_mode&(FMODE_READ|FMODE_WRITE))!=0){pr_info("skeleton : opened for reading & writing...\n");}elseif((f->f_mode&FMODE_READ)!=0){pr_info("skeleton : opened for reading...\n");}elseif((f->f_mode&FMODE_WRITE)!=0){pr_info("skeleton : opened for writing...\n");}return0;}
#define BUFFER_SZ 10000staticchars_buffer[BUFFER_SZ];staticssize_tskeleton_read(structfile*f,char__user*buf,size_tcount,loff_t*off){// compute remaining bytes to copy, update count and pointersssize_tremaining=BUFFER_SZ-(ssize_t)(*off);char*ptr=s_buffer+*off;if(count>remaining)count=remaining;*off+=count;// copy required number of bytesif(copy_to_user(buf,ptr,count)!=0)count=-EFAULT;pr_info("skeleton: read operation... read=%ld\n",count);returncount;}
Exemple: write
staticssize_tskeleton_write(structfile*f,constchar__user*buf,size_tcount,loff_t*off){// compute remaining space in buffer and update pointersssize_tremaining=BUFFER_SZ-(ssize_t)(*off);// check if still remaining space to store additional bytesif(count>=remaining)count=-EIO;// store additional bytes into internal bufferif(count>0){char*ptr=s_buffer+*off;*off+=count;ptr[count]=0;// make sure string is null terminatedif(copy_from_user(ptr,buf,count))count=-EFAULT;}pr_info("skeleton: write operation... written=%ld\n",count);returncount;}