linux driver 寫入 - Linux
By Kelly
at 2015-03-18T00:47
at 2015-03-18T00:47
Table of Contents
不知道在這裡問這個對不對,最近在研究撰寫linux driver,找到一些網路上簡單的範例
但那些範例一次只能寫入一筆資料,我想一次能寫入多筆資料然後讀出來
可是怎麼改都不對...
以下是一些程式碼:
ssize_t dev_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct dev_data *p = filp->private_data;
unsigned char val[5];
int retval = 0, i;
printk("%s: count %d pos %lld\n", __func__, count, *f_pos);
for(i=0;i<count;i++){
if (copy_from_user(&val[i], &buf[i], 1)) {
retval = -EFAULT;
goto out;
}
}
write_lock(&p->lock);
for(i=0;i<5;i++)
p->val[i] = val[i];
write_unlock(&p->lock);
retval = count;
out:
return (retval);
}
ssize_t dev_read(struct file *filp, char __user *buf, size_t count, loff_t
*f_pos)
{
struct dev_data *p = filp->private_data;
unsigned char val[5];
int retval, i;
read_lock(&p->lock);
for(i=0;i<5;i++)
val[i] = p->val[i];
read_unlock(&p->lock);
printk("%s: count %d pos %lld\n", __func__, count, *f_pos);
for (i = 0 ; i < count ; i++) {
if (copy_to_user(&buf[i], &val[i], 1)) {
retval = -EFAULT;
goto out;
}
}
retval = count;
out:
return (retval);
}
TEST:
write_file(fd, "01234");
read_file(fd);
void write_file(int fd, unsigned char *val)
{
ssize_t ret;
ret = write(fd, &val, 5);
if (ret <= 0) {
perror("write");
}
}
void read_file(int fd)
{
unsigned char buf[5], *p;
ssize_t ret;
ret = read(fd, buf, sizeof(buf));
if (ret > 0) {
p = buf;
while (ret--)
printf("%02x ", *p++);
} else {
perror("read");
}
printf("\n");
}
我想要出現30 31 32 33 34但是卻不是,到底哪裡有問題?
--
但那些範例一次只能寫入一筆資料,我想一次能寫入多筆資料然後讀出來
可是怎麼改都不對...
以下是一些程式碼:
ssize_t dev_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct dev_data *p = filp->private_data;
unsigned char val[5];
int retval = 0, i;
printk("%s: count %d pos %lld\n", __func__, count, *f_pos);
for(i=0;i<count;i++){
if (copy_from_user(&val[i], &buf[i], 1)) {
retval = -EFAULT;
goto out;
}
}
write_lock(&p->lock);
for(i=0;i<5;i++)
p->val[i] = val[i];
write_unlock(&p->lock);
retval = count;
out:
return (retval);
}
ssize_t dev_read(struct file *filp, char __user *buf, size_t count, loff_t
*f_pos)
{
struct dev_data *p = filp->private_data;
unsigned char val[5];
int retval, i;
read_lock(&p->lock);
for(i=0;i<5;i++)
val[i] = p->val[i];
read_unlock(&p->lock);
printk("%s: count %d pos %lld\n", __func__, count, *f_pos);
for (i = 0 ; i < count ; i++) {
if (copy_to_user(&buf[i], &val[i], 1)) {
retval = -EFAULT;
goto out;
}
}
retval = count;
out:
return (retval);
}
TEST:
write_file(fd, "01234");
read_file(fd);
void write_file(int fd, unsigned char *val)
{
ssize_t ret;
ret = write(fd, &val, 5);
if (ret <= 0) {
perror("write");
}
}
void read_file(int fd)
{
unsigned char buf[5], *p;
ssize_t ret;
ret = read(fd, buf, sizeof(buf));
if (ret > 0) {
p = buf;
while (ret--)
printf("%02x ", *p++);
} else {
perror("read");
}
printf("\n");
}
我想要出現30 31 32 33 34但是卻不是,到底哪裡有問題?
--
Tags:
Linux
All Comments
By Ida
at 2015-03-22T21:01
at 2015-03-22T21:01
By Lydia
at 2015-03-26T02:24
at 2015-03-26T02:24
By Rae
at 2015-03-27T11:10
at 2015-03-27T11:10
Related Posts
安裝centos6.6失敗
By Emily
at 2015-03-16T22:53
at 2015-03-16T22:53
排序第一行以後再排序第二行 sort
By Kelly
at 2015-03-16T08:34
at 2015-03-16T08:34
CentOS 6.4截圖畫面全黑
By Sandy
at 2015-03-15T23:41
at 2015-03-15T23:41
作Raid 1拆任何一顆硬碟都會沒辦法開機
By Quanna
at 2015-03-15T21:14
at 2015-03-15T21:14
CUPS安裝驅動的問題
By Edward Lewis
at 2015-03-15T02:32
at 2015-03-15T02:32