简体   繁体   中英

Interrupt handler code executed concurrently with the user code

I have written a simple UART driver using interrupt.Right now I am only transmitting the data.

write function in the user space copies the data to the buffer in kernel space.

Now during Interrupt the data in kernel space is copied byte wise(100 times) from kernel space to THR buffer register.

But before the Interrupt handler completes it job,the debug statements in close function gets printed.Isn't it the wrong way?ie I want the debug statements of close function to be printed after the statements of handler function

The code below is the handler function and this handler function is enabled just after the copyfromuser function

irqreturn_t uart_handler(int irq, void *dev_id) 
{
    write_to_uart(bytes_written);
    printk(KERN_ALERT "index %d\n", Uindex);

    if (Uindex >= 100)
        iowrite32(0,io_map + 0x4);//IER THR disable

    printk(KERN_ALERT "out from if\n");

    return IRQ_HANDLED;
}

The code below is the write_to_uart

void write_to_uart(int nofbytes)
{
    if (((ioread32(io_map + 0x8)) & 0x2) == 0x2) {
        printk(KERN_ALERT "Interrupt activated\n");
        iowrite32((unsigned)((device_buffer)[Uindex]), io_map);
        Uindex++;
    } else {
        printk(KERN_ALERT "Interrupt not activated\n");
        printk(KERN_ALERT "IIR reg = %d\n", ioread32(io_map + 0x8));
    }
}

The close function does nothing

int uart_release(struct inode *inode, struct file *filp)
{
    printk(KERN_ALERT "DEVICE CLOSED\n");
    return 0;
}

Since there is no code in the interrupt handler that would be able to block the write(2) call, the data will be sent to the UART asynchronously. Assuming that you proceed to close(2) the file descriptor after the data has been written (as perceived from user-space); the behavior that you are describing is exactly what I would expect to see.

If you wan't to make the write be blocking, have a look at completions .

On a side note; is there any reason why you can't use the standard 8250/16550 driver in the kernel?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM