# Writing a FreeBSD kernel module that handles arbitrary interrupt and output to device



## Aaci (Jul 3, 2017)

I would like to write a FreeBSD kernel module that could accept some arbitrary interrupts and upon receiving these interrupt, output some data to an arbitrary device. Currently, I'm facing several issues:


How would I acquire interrupts through a specific IRQ? On Linux there is the request_irq() call but it seems there's no similar API for FreeBSD... Say, I want to be able to detect all the keyboard interrupt through my kernel module (the keyboard is on irq1), how would I do that? (On Linux it is possible through calling free_irq(1, NULL) and request_irq(1, ...), correct me if I'm wrong though).


Is it possible at all to write to a device file under /dev through a kernel module? There is a guide on StackOverflow about how to do file I/O in kernel, following that I was able to do read/write on regular files, but not a device file under /dev (the "device" was a pseudo "echo device", the classical one used in char device examples). I was able to open the file though.
I do understand that it is considered as a bad practice to do file I/O's in kernel, but I could not come up with any other way... If anyone has a better solution please tell me. (i.e. write to a device through its device_t node?)

The reason I was doing this in a kernel is that I really need all interrupts to be hit, and running it in the user space has the risk of missing interrupts due to kernel threads preempting user threads (the interrupts could come very frequent, like ~70-100Hz).

I would also appreciate if anyone could provide me with some other ideas on how to implement this program (basically, the idea is a kernel module that could do the job of a microcontroller...). I've never done any kernel programming prior to this one, so I'll be really appreciated for any guidance


----------



## leebrown66 (Jul 6, 2017)

Thread 31610 looks like it may be useful to you.  Partway down the page, the author wrote his own driver for UART and it's in ports, so you have a nice starting point.


----------



## Aaci (Jul 6, 2017)

leebrown66 said:


> Thread 31610 looks like it may be useful to you.  Partway down the page, the author wrote his own driver for UART and it's in ports, so you have a nice starting point.


Hmm, seems in this case, I'd need to modify the ATKBD driver and rebuild that into the kernel... Kind of a big project lol


----------



## leebrown66 (Jul 6, 2017)

Aaci said:


> Hmm, seems in this case, I'd need to modify the ATKBD driver and rebuild that into the kernel... Kind of a big project lol


You have the code there allocating the interrupt.  Otherwise I'd suggest finding the simplest driver in the kernel that does the closest thing to what you and and start reading it's source, copy and go from there.


----------

