[LAD] /dev/uinput

Jens M Andreasen jens.andreasen at comhem.se
Thu Sep 27 05:53:35 UTC 2007


I am trying to create a userspace driver with module uinput and I am
getting into trouble when I terminate the program that creates the
device. After a while the machine will crash ... 

The object is to create a joystick device, and I might be missing some
important bits of documentation. So far I am not writing any data to the
interface, but js_demo will find it and open it, showing the expected
number of axis. The name of the interface looks like random though,
which worries me and gets me to suspect that something is wrong

Any idea why the machine is crashing or has a pointer to some example I
could study? As long as I just let the program run, there is no
problem ... Only on termination.

This is what I have gotten so far in the uinput department:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <linux/input.h>
#include <linux/uinput.h>

int create_uinput() {

  int fd = -1;

  fd = open ("/dev/uinput", O_RDWR);
  if (fd <= 0)
      fprintf (stderr,"could not open uinput device\n");
  memset (&uinp, 0, sizeof (struct uinput_user_dev));

  strncpy (uinp.name, "NoJoy!", 7); // FIXME!
  uinp.id.version = 4;
  uinp.id.bustype = BUS_USB;

  ioctl (fd, UI_SET_EVBIT, EV_ABS);
  ioctl (fd, UI_SET_EVBIT, EV_KEY);
  ioctl (fd, UI_SET_ABSBIT, ABS_X);
  ioctl (fd, UI_SET_ABSBIT, ABS_Y);
  ioctl (fd, UI_SET_ABSBIT, ABS_Z);
  ioctl (fd, UI_SET_KEYBIT, BTN_TOP);
  ioctl (fd, UI_SET_KEYBIT, BTN_TOP2);
  ioctl (fd, UI_SET_KEYBIT, BTN_BASE);
  ioctl (fd, UI_SET_KEYBIT, BTN_BASE2);
  ioctl (fd, UI_SET_KEYBIT, BTN_BASE3);
  ioctl (fd, UI_SET_KEYBIT, BTN_BASE4);

  // Create device  
  write (fd, &uinp, sizeof (uinp));
  if (ioctl (fd, UI_DEV_CREATE))
      fprintf (stderr,"could not create uinput device.\n");

  return fd;

void destroy_uinput()
  fprintf(stderr,"NoJoy says: BYE!\n");
  if(uinp_fd > 0)    
      ioctl (uinp_fd, UI_DEV_DESTROY);
      close (uinp_fd);


More information about the Linux-audio-dev mailing list