1

My embedded system runs Linux OS. The system has two joysticks connected to it through USB. My program should read any of these two devices when they are ready and pass the joystick key information to upper layer.
My program does the following.

  1. Open both the joystick files /dev/js0 and /dev/js1 if they are not already opened.
    i.e. if fd0 or fd1 == -1 (default value is -1) open it.
  2. select() // maxfd+1 and readset is passed to select
  3. which ever device is ready, program uses read() to read the data.
  4. The above all three steps are done periodically in a timer.

The above code works fine. No issues.
As part of negative test case, I removed one of the joystick from USB after it has been opened and read successfully. I started seeing the following perror -> "read() failed: no such file or device.

I want to handle this error such that if the joystick is removed and inserted back the code should accordingly re-open it without any issues.

As soon as I encounter the above perror, I tried to close the device and set my fd to -1 so that as part of step 1 it will be reopend. But this is not working. I am currently debugging it. I think close() is not working after encountering no such file or device error.

If anybody has encountered this type of problem or know how to handle this type of scenario (USB device removed and inserted after opening it), kindly provide your inputs.

thanks much. Regards, Emerson

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • 1
    Going by the possibly similar behavior of USB-serial adapters, I wonder if the problem might not be with the close() but rather than the replacement device will get a new "identity" if the existing one is still "open". Specifically if I re-plug my adapter while a terminal program has it open, the minor number increments and udev gives the new device node a new name, wheras if the device is not open when replugged, it goes back at the old minor number and name. Perhaps you have a similar issue? – Chris Stratton Apr 03 '14 at 20:27

0 Answers0