Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

Ioctl problem

Hi,

I'm writing a device driver and I have problems with the ioctl function. When I run my user application associated with the driver in the terminal, "printks" that are inside my module functions appeared in a wrong order, I use "printk" to know where I am. Besides "printk" that are inside ioctl code of the module appeared without having make a call of ioctl. Maby the computer has reorganized the order of the instructions and ioctl always starts when I run a new application?

I have used in my user code a file_pointer instead of a file_descriptor, althought I have used function fileno to have a file_descriptor because it is one of the paremeters of the function ioctl.

I don't know how to solution these problems.

Thanks

Code_User_Application

FILE *leds;                
    .
    .
    .    
    printf("Fitxer obert Usuari\n");
    descriptor_leds=fileno(leds);       
    fwrite(Dada,sizeof(char),1,leds);
    ioctl(descriptor_leds,LEDS_IOCSETD,&Dada_ioctl);
    printf("IOCTL_Usuari_Superat\n\n");
Module_Code

static int Leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){   
    static int data;
    printk("\nSuper_ioctl\n");
    printk("Valor_cmd:%d\n",cmd);
    switch(cmd){
        case LEDS_IOCSETD:
            if (copy_from_user(&data,(int *)arg, sizeof(int)))
                return -EFAULT;
            printk("Ioct_Valor: %d\n",data);
            return 0;
        default:
            printk("Ioctl_Cap_opcio\n");
            return -ENOTTY;
    }
}
Thanks

10 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    Do you have a doubt for the printing order of 'Super_ioctl....' and 'Fitxer obert Usuari...'?

    If so, please consider that the 'printf' messages are all buffered and wait the timing to send to your console.

    Kazu
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi:

    I suggest that add sleep(1) after printf("Fitxer obert Usuari\n");

    I‘m sure module has higher priority than app。。

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for answers.

    But I was referring to "printks" that are in functions of my module no "printfs". I mean in my user code I make a call of function fwrite before calling function ioctl and in the terminal I saw "printks" of ioctl before printks of fwrite and all "printks" that I have written in the module have the same priority. And also why "printks" of ioctl can appear in terminal if I don't make a call of function "ioctl", I have chosen a unique magic number.

    Thanks in advice
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    Would you please write the order of printk's message that you are expecting?

    Kazu
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    Thanks for help. Well I think that the order of "printks" should be this:

    //Printk function fwrite
    Copy_from_user: 4
    I'm here
    //Printk function Ioctl
    Super_ioctl
    Valor_cmd:1
    Ioctl_Valor1: 5
    These are "printks" that I think that should appear in terminal with this order and only these "printks". I would like to understand where are mistakes.

    Thanks a lot.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    --- Quote Start ---

    //Printk function fwrite
    Copy_from_user: 4
    I'm here
    //Printk function Ioctl
    Super_ioctl
    Valor_cmd:1
    Ioctl_Valor1: 5

    --- Quote End ---

    Where did you put 'printk' inside of 'fwrite'? The 'fwrite' is only a library function, not a kernel function.

    Kazu
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    Maby I'm wrong but I'm not agree with you. It's true that 'fwrite' is a function of <stdio.h> library , but it is connected with my kernel function write.

    One of the parametres of fwrite is a file_pointer, this file_pointer has the same major as my driver, so when I call a fwrite in user space indirectly I call my function write in kernel.

    Things ara litle complex but more or less.This what I think, maby I'm wrong tell me please.

    Thanks
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    AFAIK, you can't use fwrite() (or anything from stdio.h) in Kernel code.

    -Michael
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    We are mixing things. I know that I can't use fwrite in kernel code Michael, I 'm using it in user application as I explained in my first post. But I used this function fwrite in user application to call my function write in the kernel code.

    The problem is the order of printks.

    Thanks for the pacience

    Thanks
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    --- Quote Start ---

    Maby I'm wrong but I'm not agree with you. It's true that 'fwrite' is a function of <stdio.h> library , but it is connected with my kernel function write.

    --- Quote End ---

    Maybe you wrote next codes somewhere in your kernel.

    
    printk("Copy_from_user:%d\n", **);
    printk("I'm here\n");
    
    And where? That is the problem.

    Kazu