if (copy_to_user((void __user *)arg, &info, sizeof(info)))
return -EFAULT;
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset) /* loff_t == long long */
{
// Number of bytes actually written to the buffer
int bytes_read = 0;
if (*msg_Ptr == 0)
return 0;
// while (length && *msg_Ptr) {
//
// // kernel ----> user
// put_user(*(msg_Ptr++), buffer++);
//
// length--;
// bytes_read++;
// }
/* copy_to_user() 的用法很简单 */
dbg("msg_Ptr length=%d\n",strlen(msg_Ptr));
if(copy_to_user(buffer,msg_Ptr,min(strlen(msg_Ptr),length)))
{
dbg("copy_to_user() error \n");
return -EFAULT;
}
bytes_read = min(strlen(msg_Ptr),length);
return bytes_read;
}
static ssize_t
device_write(struct file *filp, const char *buffer, size_t len, loff_t * off)
{
int bytes_write = 0;
int max_bytes = sizeof(msg) -1;
memset(msg,'\0',sizeof(msg));
// while( *buffer && bytes_write < len && bytes_write < max_bytes )
// {
// // kernel <---user
// get_user(*(msg_Ptr++),buffer++);//msg_Ptr is same
// bytes_write++;
// }
copy_from_user(msg_Ptr,buffer,min( min(strlen(buffer),len), sizeof(msg)-1));
bytes_write = min( min(strlen(buffer),len), sizeof(msg)-1);
dbg("write %d bytes \n",bytes_write);
return bytes_write;
}