1
votes

i am writing a driver code, to read some register values from x86., when i ran my user space application i got the below error.

ioctl:Inappropriate ioctl for device

here is the code sniff..

fd = open_drvfile();
if(ioctl(fd, IOCTL_MSR_CMDS, (long long)msr_start) == -1 ) {
    perror("ioctl:");
    exit (0);
}

and open_drvfile() just open(create and open) the char file as below

fd = open("/dev/" DEV_NAME, O_RDWR|O_CREAT);
if (fd == -1) {
    perror("Failed to open /dev/" DEV_NAME);
}
return fd;

can some one point where i made mistake on this?

1
I don't quite understand what you're trying to do, but since you're referencing a char device in the question title, the O_CREAT flag under the open() isn't making much sense to me. Have you created the char device with mknod() prior to open()? - pah
I am using open(), to create and open the file (since file is not present already). i think this was wrong. after saw your answer, i used mknod() to create file and removed O_CREAT , now it is working as expected. Thanks. - Sakthivel Thandabani
I'll post the answer, so it may be helpful to others :) - pah
@pah accepting the answer is new to me.. i just did that. Thanks - Sakthivel Thandabani

1 Answers

0
votes

A char device implies that it shall be created with mknod(), and not with O_CREAT under open() flags (which will create a regular file, not a char device).

(see question comments).