0

I am a beginner with linux interrupt function, I try to run the program below in Ubuntu 20, but there is always red wave line with the "#include <linux/interrupt.h>", I search the file and copy it into the directory. But after that, I get other warning messages with headers, such like "linux/bitops.h" , "linux/bits.h", or "asm/xxx.h". I try to copy them one by one into the include folder, but it seems like not the correct way to solve the problem.

I tried to correct the problem, with documents and videos, etc., but it is not easy to find something on this topic. Thanks for you help, appreciate.


#include <linux/interrupt.h>
#include <linux/irqreturn.h>

MODULE_LICENSE("GPL");

#define DPRINT(x) printk("<%s>" #x " = 0x%x\n", __func__, (int)x);

void beep(void)
{
    volatile int i = 0;
    printk("beep on\n");

    for (i = 0; i<800000000; i++);
    
    printk("beep off\n");
}

irqreturn_t myhandler(int irq, void * p)
{
    int tmp;
    //printk("irqhandler called!\n");
    DPRINT(irq);
    DPRINT(&tmp);

    beep();
    
    return IRQ_HANDLED;
}

static __init int interrupt_init(void)
{
    int ri;
    int btn_irq;

    btn_irq = 160;

    ri = request_irq(btn_irq, myhandler, IRQF_TRIGGER_FALLING, "btn k1 interrupt", NULL);
    ri = request_irq(btn_irq+1, myhandler, IRQF_TRIGGER_FALLING, "btn k2 interrupt", NULL);

    DPRINT(ri);

    return 0;
}

static __exit void interrupt_exit(void)
{
    DPRINT(interrupt_exit);

    free_irq(160,NULL);
    free_irq(161,NULL);

    return;
}

module_init(interrupt_init);
module_exit(interrupt_exit);

new edited: Thanks a lot for help, sorry I still don't know what to do with it exactly, I need to download kernel source to support this function? or the source code is already on my computer but the target directory is not correct? Is there a document or a video that something like a tutorial? with "apt-cache search linux-source", I got a list below:

linux-source - Linux kernel source with Ubuntu patches
linux-source-5.4.0 - Linux kernel source for version 5.4.0 with Ubuntu patches
linux-gkeop-source-5.4.0 - Linux kernel source for version 5.4.0 with Ubuntu patches
linux-hwe-5.11-source-5.11.0 - Linux kernel source for version 5.11.0 with Ubuntu patches
linux-hwe-5.13-source-5.13.0 - Linux kernel source for version 5.13.0 with Ubuntu patches
linux-hwe-5.8-source-5.8.0 - Linux kernel source for version 5.8.0 with Ubuntu patches
linux-intel-5.13-source-5.13.0 - Linux kernel source for version 5.13.0 with Ubuntu patches
linux-source-4.10.0 - Linux kernel source for version 4.10.0 with Ubuntu patches
linux-source-4.11.0 - Linux kernel source for version 4.11.0 with Ubuntu patches
linux-source-4.13.0 - Linux kernel source for version 4.13.0 with Ubuntu patches
linux-source-4.15.0 - Linux kernel source for version 4.15.0 with Ubuntu patches
linux-source-4.4.0 - Linux kernel source for version 4.4.0 with Ubuntu patches
linux-source-4.8.0 - Linux kernel source for version 4.8.0 with Ubuntu patches

NEW EDITED: I tried as below, it was just the same like days ago when I copied files to a folder:

gcc irq.c -include /usr/src/linux-hwe-5.13-headers-5.13.0-35/include/linux/interrupt.h /usr/src/linux-hwe-5.13-headers-5.13.0-35/include/linux/irqreturn.h
In file included from <command-line>:32:
/usr/src/linux-hwe-5.13-headers-5.13.0-35/include/linux/interrupt.h:7:10: fatal error: linux/bitops.h: No such file or directory
    7 | #include <linux/bitops.h>
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
In file included from <command-line>:31:
/usr/include/stdc-predef.h:1: fatal error: can’t create precompiled header /usr/src/linux-hwe-5.13-headers-5.13.0-35/include/linux/irqreturn.h.gch: Permission denied
    1 | /* Copyright (C) 1991-2020 Free Software Foundation, Inc.
      | 
compilation terminated.
  • How did you download the kernel sources? – tripleee Mar 19 '22 at 16:56
  • Don't copy files. Your make command should point to the root of the kernel sources. – stark Mar 19 '22 at 16:59
  • As others have said already, don't copy the files, add their (parents') paths to the compilation command for the compiler to find them. The red lines are not compilation errors, though, but added by your language server client. Depending on your setup, you need to tell your language server what the compilation commands are (which include the `-I` flags adding non-standard include dirs). A widely supported way is to generate a `compile_commands.json` file. Both `ccls` and `clangd` support this format. – frippe Mar 19 '22 at 20:26
  • Thanks for your answers, I made some more test and edited my question with the result, could you please take a look and give me some advice? Thanks again, it really took me a lot of time. – Justin Bob Mar 21 '22 at 13:42
  • The command-line options to GCC are supposed to tell it the _directories_ where it will search for the files names in `#include` statements in your code. You don't list every included file once in code and once on the command line. Read the free [manual](https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Directory-Options.html#Directory-Options). – Useless Mar 21 '22 at 17:01
  • Thanks for your answer, I added in the makefile: "-I /usr/src/linux-hwe-5.13-headers-5.13.0-37/include/ -I /usr/src/linux-hwe-5.13-headers-5.13.0-37/include/linux/ -I /usr/src/linux-headers-5.13.0-37-generic/include/ -I /usr/src/linux-headers-5.13.0-37-generic/include/linux/ ", but still, there were other messages, please see the new edited above, thanks again. – Justin Bob Mar 24 '22 at 15:16

0 Answers0