与 Linux 中的设备对话。写功能

发布于 2025-01-07 03:13:18 字数 611 浏览 4 评论 0原文

我正在尝试编写一个简单的设备驱动程序,并使用我已经定义的 Dev_Read()Dev_Write() 函数。 我的驱动程序注册了一个与 以这种方式给定主编号

# mknod /dev/mydev c 250 0

然后,

int fd;
if ((fd = open("/dev/mydev", O_RDWR)) < 0)
{
  perror("open /dev/mydev");
  exit(EXIT_FAILURE);
}

在调用 Read() 函数

int read_bytes = read (fd, buffer, 1);

并成功获取内核空间信息后,从我的用户程序中我以这种方式打开设备,即我的 Dev_Read功能有效。 我的问题是我不明白如何实现我的 Dev_Write 函数。 如何向我的文件写入一些内容,以查看 Dev_Write 函数是否有效? 谢谢您的帮助。

I'm trying to write a simple device driver, and use the Dev_Read() and Dev_Write() functions, which I have already defined.
My driver registers a character device tied to a
given major number this way

# mknod /dev/mydev c 250 0

And then, from my user program I open the device this way

int fd;
if ((fd = open("/dev/mydev", O_RDWR)) < 0)
{
  perror("open /dev/mydev");
  exit(EXIT_FAILURE);
}

after I invoke the Read() function

int read_bytes = read (fd, buffer, 1);

and successfully get kernel space information, that my Dev_Read function works.
My problem is that I don't understand how to implement my Dev_Write function.
How can I write someting to my file, to see that Dev_Write function works ?
THANK YOU for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2025-01-14 03:13:18

了解您尝试过哪些方法无效会有所帮助。

需要注意的一件事(不一定直观)是驱动程序的写入函数必须将写入缓冲区从调用者空间复制到内核空间。可以在 http://www.freesoftwaremagazine.com/articles/drivers_linux< 的教程中查看示例。 /a> -

ssize_t memory_write( struct file *filp, char *buf,
                      size_t count, loff_t *f_pos) {

  char *tmp;

  tmp=buf+count-1;
  copy_from_user(memory_buffer,tmp,1);
  return 1;
}

其中 memory_buffer 是您在驱动程序中分配的空间。

It would help to know what you've tried which didn't work.

One thing to be aware of, and not necessarily intuitive, is that your driver's write function must copy the write buffer from the caller's space into kernel space. An example of that can be seen in the tutorial at http://www.freesoftwaremagazine.com/articles/drivers_linux -

ssize_t memory_write( struct file *filp, char *buf,
                      size_t count, loff_t *f_pos) {

  char *tmp;

  tmp=buf+count-1;
  copy_from_user(memory_buffer,tmp,1);
  return 1;
}

where memory_buffer is space you've allocated within your driver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文