title: Handling interrupt in character device
body: <p>I am trying to correctly register interrupt in kernel for user interface.</p>
  
  <p>Surprisingly, I did not find many examples in kernel for that.</p>
  
  <h1>irq handler</h1>
  
  <pre><code>static irqreturn_t irq_handler(int irq, void *dev_id)
  {
     struct el_irq_dev *el_irq = &amp;el_irq_devices[0];
      printk("irq in\n");
  
      spin_lock(&amp;el-&gt;my_lock,flags);
  
      clear_interrupt() 
  
      some_buffer[buf_wr] = ch;
      el_irq-&gt;buf_wr++;
      if (el_irqbuf_wr &gt;= 16)
          el_irqbuf_wr = 0;
  
  
       spin_unlock(&amp;el-&gt;my_lock,flags);
       wake_up_interruptible(&amp;el-&gt;pollw);
  
  
  
  return IRQ_HANDLED;
  </code></pre>
  
  <p>}</p>
  
  <h1>ioctl for waiting on interrupts</h1>
  
  <pre><code>static long el_device_ioctl( struct file *filp, 
           unsigned int ioctl_num, 
           unsigned long ioctl_param)
  {
      struct el_irq_dev *el_irq = &amp;el_irq_devices[0];
  switch (ioctl_num) {
  case IOCTL_WAIT_IRQ:      &lt;&lt;&lt;---- using ioctl (no poll) to wait on interrupt
      wait_event_interruptible(el_irq-&gt;pollw, &amp;el_irq-&gt;buf_wr != &amp;el_irq-&gt;buf_rd) ;
      spin_lock(&amp;el_irq-&gt;my_lock);
      if (el_irq-&gt;buf_wr != &amp;el_irq-&gt;buf_rd)
      {
          my_value=some_buffer[el_irq-&gt;buf_rd];
          el_irq-&gt;buf_rd++;
          if (el_irq-&gt;buf_rd &gt;= 16)
              el_irq-&gt;buf_rd = 0;
      }
  
      spin_unlock(&amp;el_irq-&gt;my_lock);
      copy_to_user(ioctl_param,&amp;my_value,sizeof(my_value));
  
  default:
          break;
      }
      return 0;
  }
  </code></pre>
  
  <p>My question is:</p>
  
  <ol>
  <li>Should we put the clear of interrupts (clear_interrupt() ) in fpga in the interrupt
  before or after the wake_up ? Can we event put the clearing interrupt in the userspace handler (IOCTL_WAIT_IRQ)  instead of  clearing the interrupt in the
  interrupt handler ?</li>
  <li>As you can see in the code, I am using cyclic buffer in order to handle cases where the userspace handler is missing interrupts. Is that really required  or can we assume that there are no misses ?
  In other words, Is it reasnoble to assume that there should never be missed interrupts ? so that the ioctl call should never see more than 1 waiting interrupt ? If yes - maybe I don't need buffer mechanism between the interrupt handler and the ioctl handler.</li>
  </ol>
  
  <p>Thank you,
  Ran</p>