lilysulia 发表于 2011-01-21 15:55

读不到内核数据,请各位看看啥原因

小弟刚接触驱动,写了个程序结果内核数据却无法读出请给位看一下问题出在那谢谢!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define DEVICE_NAME "/dev/key_test_by_atmel"

int main()
{
        int fd;
      int i;
        int buf;

        fd = open(DEVICE_NAME,O_RDWR);
        printf("fd == %d\n",fd);

        if(fd == -1)
        {
                printf("open failed!\n");
        }
        else
        {
                while(1)
                {
                        i = read(fd,buf,1);
                                      
                                printf("i == %d\n",i);
                       
                                printf("keyvalue == %d\n",buf);
                }
        }
        return 0;
}

这是我的read函数读的时候总是返回-1
static int keypad_read(struct file *filp ,int *buf,size_t count,loff_t *f_pos)
{
        intdate;
      
        memset(date,0,5);
       date = keyvalue;
        __copy_to_user(buf, date, count);

      
                return 0;

}

EZWORD 发表于 2011-01-21 16:37

return 0;
?

lilysulia 发表于 2011-01-21 16:42

就是回复一个返回值,函数是INT 型的,这个地方有错了?

0vk0 发表于 2011-01-21 18:04

请把你的代码贴全一点,我说的是驱动代码

amarant 发表于 2011-01-22 08:30

不能返回0,否则会一直读

amarant 发表于 2011-01-22 08:31

你试试改成return 1; 看看~

lilysulia 发表于 2011-01-24 08:56

本帖最后由 lilysulia 于 2011-01-24 08:57 编辑

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <mach/board.h>
#include <mach/gpio.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include "gpio.h"



static struct task_struct *test_task;
extern void msleep(unsigned int msecs);
int keyvalue;

int get_key7_stat(void)
{
        at91_set_gpio_input(AT91_PIN_PB7,1);
        return at91_get_gpio_value(AT91_PIN_PB7);
}
int get_key6_stat(void)
{
      at91_set_gpio_input(AT91_PIN_PB6,1);
      return at91_get_gpio_value(AT91_PIN_PB6);
}





void led_init(void)
{
    at91_set_GPIO_periph(AT91_PIN_PB7,1);
    at91_set_GPIO_periph(AT91_PIN_PB6,1);
   
    at91_set_A_periph(AT91_PIN_PB7,1);
    at91_set_A_periph(AT91_PIN_PB6,1);

}

static int example_kernel_thread(void *k)
{
        unsigned int i = 0;
        unsigned int j = 0;

        set_current_state(TASK_UNINTERRUPTIBLE);
      while(1){
              if(kthread_should_stop()) break;
                      //printk(KERN_WARNING "sss\n");
                     
                       
                        if(get_key7_stat() == 0)
                        {       
                              msleep(10);
                                i++;
                                if(i == 4)
                                {
                                      keyvalue = 1;
                                      printk("kernel1111 keyvalue == %d\n",keyvalue);

                                }
                                if(i == 12)
                                {
                                        i = 0;
                                        keyvalue = 0;
                                }
                                //msleep(100);

                        }
                        else if(get_key6_stat() == 0)
                        {
                       
                                
                                msleep(10);
                              j++;
                              if(j == 4)
                              {
                                        keyvalue = 2;
                                        printk("kernel2222 keyvalue == %d\n",keyvalue);
                              }
                              if(j == 12)
                                 {      
                                       j = 0;
                                       keyvalue = 0;
                                 }

                      }
        }
      return 0;
}




ssize_t keypad_open(struct inode *inode ,struct file *file)
{
        return 0;
}

ssize_t keypad_read(struct file *filp ,char *buf,size_t count,loff_t *f_pos)
{
        chardate;
      
                printk("kernel keyvalue == %d\n",keyvalue);
        if(keyvalue)
        {
                memset(date,0,5);
                memset(buf,0,5);
                     date = keyvalue;
                __copy_to_user(buf, date, count);
      }
      return 1;

}

static void hello_exit(void)
{
          if(test_task){
               kthread_stop(test_task);
                test_task = NULL;
          }
         printk(KERN_WARNING "exit gpio\n");
}

static struct file_operations keypad_fops =
{
            open:    keypad_open,
            read:    keypad_read,
};






static int hello_init(void)
{

      int ret;
      int err;
      err = register_chrdev(0, "key_test_by_zkw", &keypad_fops);
      printk(" register device NO. is: %x\n",err);

      if (err < 0)
                {
               printk("%s (%s,%d) : Error registering s3c2410-keypad.\n",
                        __FUNCTION__, __FILE__, __LINE__);
                   return err;
                }
      else
            {
               printk("%s (%s,%d) : s3c2410telekeypad registered.\n",
                         __FUNCTION__, __FILE__, __LINE__);
                }
      
             test_task = kthread_create(example_kernel_thread, NULL, "gpio");
      
                if(IS_ERR(test_task)){
                      printk(KERN_ALERT "kernel_thread gpio create failed \n");
                      ret = PTR_ERR(test_task);
                      test_task = NULL;
                      return ret;
            }else{
                    printk(KERN_WARNING "kernel_thread gpio create success \n");
                    wake_up_process(test_task);
                    led_init();
            }
                     
        return 0;
}



module_init(hello_init);
module_exit(hello_exit);

MODULE_DESCRIPTION("gpio test");
MODULE_LICENSE("GPL");

lilysulia 发表于 2011-01-24 08:59

我发现现在的问题是Main函数根本调不到驱动中的read函数,打印语句根本出不来,请各位看看什么原因谢谢!

EZWORD 发表于 2011-01-24 09:40

能再把步骤详细的写出来看看吗?所有打印信息也贴出来

lilysulia 发表于 2011-01-24 09:58

# insmod gpio.ko                                                         
register device NO. is: fb                                                   
hello_init (/home/sulia/qudong/keyctl/gpio.c,180) : s3c2410telekeypad registere.
kernel_thread gpio create success
证明驱动注册成功

# ./main                                                                  
fd == 3   
文件读取成功,

下面按键

kernel2222 keyvalue == 2                                                      
kernel2222 keyvalue == 2                                                      
kernel2222 keyvalue == 2                                                      
kernel2222 keyvalue == 2                                                      
kernel1111 keyvalue == 1                                                      
kernel1111 keyvalue == 1                                                      
kernel2222 keyvalue == 2                                                      
kernel2222 keyvalue == 2                                                      
kernel1111 keyvalue == 1
页: [1] 2
查看完整版本: 读不到内核数据,请各位看看啥原因