title: Call a userspace function from within a Linux kernel module
body: <p>I'm programming a simple Linux character device driver to output data to a piece of hardware via I/O ports. I have a function which performs floating point operations to calculate the correct output for the hardware; unfortunately this means I need to keep this function in userspace since the Linux kernel doesn't handle floating point operations very nicely.</p>
  <p>Here's a pseudo representation of the setup (note that this code doesn't do anything specific, it just shows the relative layout of my code):</p>
  <p><strong>Userspace function</strong>:</p>
  <pre><code>char calculate_output(char x){
      double y = 2.5*x;
      double z = sqrt(y);
      
      char output = 0xA3;
  
      if(z &gt; 35.67){
          output = 0xC0;
      }
      
      return output;
  }
  </code></pre>
  <p><strong>Kernelspace code</strong>:</p>
  <pre><code>unsigned i;
  for(i = 0; i &lt; 300; i++){
      if(inb(INPUT_PORT) &amp; NEED_DATA){
          char seed = inb(SEED_PORT);
          char output = calculate_output(seed);
          outb(output, OUTPUT_PORT);
      }
  
      /* do some random stuff here */
  }
  </code></pre>
  <p>I thought about using <code>ioctl</code> to pass in the data from the userspace function, but I'm not sure how to handle the fact that the function call is in a loop and more code executes before the next call to <code>calculate_output</code> occurs.</p>
  <p>The way I envision this working is:</p>
  <ol>
  <li>main userspace program will start the kernelspace code (perhaps via <code>ioctl</code>)</li>
  <li>userspace program <strong>blocks</strong> and waits for kernelspace code
  <ul>
  <li>kernelspace program <em>asks</em> userspace program for output data, and <strong>blocks</strong> to wait</li>
  <li>userspace program <strong>unblocks</strong>, calculates and sends data (<code>ioctl</code>?), then <strong>blocks</strong> again</li>
  <li>kernelspace program <strong>unblocks</strong> and continues</li>
  </ul>
  </li>
  <li>kernelspace program finishes and <em>notifies</em> userspace</li>
  <li>userspace <strong>unblocks</strong> and continues to next task</li>
  </ol>
  <p>So how do I have the communication between kernelspace and userspace, and also have blocking so that I don't have the userspace continually polling a device file to see if it needs to send data?</p>
  <hr />
  <p>A caveat: while fixed point arithmetic would work quite well in my example code, it is not an option in the real code; I require the large range that floating point provides and -- even if not -- I'm afraid rewriting the code to use fixed point arithmetic would obfuscate the algorithm for future maintainers.</p>