入门文章:教你学会编写Linux设备驱动之结束篇
文章作者 100test 发表时间 2007:03:14 16:20:30
来源 100Test.Com百考试题网
return 0.
}
static int
asdf_release /* close回调 */
(
struct inode *inode,
struct file *filp
){
printk("^_^ : close\n ").
return 0.
}
static ssize_t
asdf_read /* read回调 */
(
struct file *filp,
char *buf,
size_t count,
loff_t *f_pos
){
loff_t pos.
pos = *f_pos. /* 文件的读写位置 */
if ((pos==4096) || (count>4096)) return 0. /* 判断是否已经到设备尾,或写的长度超过设备大小 */
pos = count.
if (pos > 4096) {
count -= (pos - 4096).
pos = 4096.
}
if (copy_to_user(buf, asdf_body *f_pos, count)) return -EFAULT. /* 把数据写到应用程序空间 */
*f_pos = pos. /* 改变文件的读写位置 */
return count. /* 返回读到的字节数 */
}
static ssize_t
asdf_write /* write回调,和read一一对应 */
(
struct file *filp,
const char *buf,
size_t count,
loff_t *f_pos
){
loff_t pos.
pos = *f_pos.
if ((pos==4096) || (count>4096)) return 0.
pos = count.
if (pos > 4096) {
count -= (pos - 4096).
pos = 4096.
}
if (copy_from_user(asdf_body *f_pos, buf, count)) return -EFAULT.
*f_pos = pos.
return count.
}
static loff_t
asdf_lseek /* lseek回调 */
(
struct file * file,
loff_t offset,
int orig
){
loff_t pos.
pos = file->f_pos.
switch (orig) {
case 0:
pos = offset.
break.
case 1:
pos = offset.
break.
case 2:
pos = 4096 offset.
break.
default:
return -EINVAL.
}
if ((pos>4096) || (pos<0)) {
printk("^_^ : lseek error %d\n",pos).
return -EINVAL.
}
return file->f_pos = pos.
}