title: Disabling specific USB devices
body: <p>I need to write a program in linux (debian, to be exact) that disables a USB device if it doesn't pass specific filters. For example, the program might be set to disallow webcams, usb sticks and keyboards, enable mice and printer (through usb). The filters may change in runtime. For example, the program might receive a message to enable usb sticks and it should respond without rebooting the system. The program is written in python but embedding c code (or others) is also acceptable.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>I have tried many methods but some of them aren't about programming.</p>
  
  <p>First, I tried to mess with udev. I can monitor the activities when a device is plugged and write filters. There used to be an option "ignore_device" that ignored the filtered devices. For example, to ignore all the devices that are a member of usb subsystem, I would write this as a udev rule:</p>
  
  <pre><code>ACTION=="add", SUBSYSTEM=="usb", OPTIONS+="ignore_device"
  </code></pre>
  
  <p>But this option is released in <a href="http://lwn.net/Articles/364728/" rel="nofollow" title="this">this</a> release of udev. What I get so far is that udev can currently be used mainly for monitoring. Sure, I can write additional rules for the rule above that runs a script, but I have to do the disabling elsewhere.</p>
  
  <p>Second, I tried ioctl to send a DISCONNECT signal to device handle. I'm testing this with a USB mouse. This is the python code for that: (I have also tried this in C, nothing changed)</p>
  
  <pre><code>import fcntl
  import sys
  USBDEVFS_RESET = ord('U') &lt;&lt; (4*2) | 20
  USBDEVFS_DISCONNECT = ord('U') &lt;&lt; (4*2) | 22
  raw_name = "/dev/bus/usb/{:03d}/{:03d}"
  filename = raw_name.format(1,2)
  fd = open(filename, "wb")
  fcntl.ioctl(fd, USBDEVFS_DISCONNECT, 0)
  </code></pre>
  
  <p>Here, if I would send USBDEVFS_RESET, it works, the mouse input is ignored for a second or two. But disconnect signal raises an error:</p>
  
  <pre><code>IOError: [Errno 25] Inappropriate ioctl for device
  </code></pre>
  
  <p>What I get from here is, I cannot send disconnect signal to a mouse. Maybe a usb stick or printer or some other devices would work, I haven't tried. I want to develop the program as generic as possible so as to prevent writing additional device-specific code, so this approach seems useless for me. And another point here is that when I manually disconnect/connect my mouse, I see events in udev monitor. But when I send reset signal, no event is sent.</p>
  
  <p>The udev monitor says that the mouse was mounted to this path: <code>/sys/bus/usb/devices/1-3</code> (which is a symlink for <code>/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3</code>). Some documents told me that this folder contains the device's settings and setting /power/level to "off" or "suspended" would turn the device power off. But I cannot manipulate any files in /power. Come to think about it, it might not be a good idea after all.</p>
  
  <p><strong>The Question</strong></p>
  
  <p>So, the question is, what is the best way to achieve such task? I have an idea but I'm not sure whether it will work and even if it does, it might be overkill. My idea is writing a "wrapper driver" that identifies itself to linux kernel as driver for all usb devices. The "wrapper driver" reads the device information and if the device is good to go, it acts as a wrapper for real drivers in the kernel, calling their functions. If not, the "wrapper driver" just ignores the device.</p>
  
  <p>I'm not sure it can even be done, I'm not experienced in kernel or driver programming.</p>
  
  <p>Another way is, -somehow- getting the handle of the device programatically and telling it to power off (or making it busy forever, whatever works). I have also done some little research but couldn't find a proper-easy way. They say all devices are considered as "files" but I cannot reach those files at all.</p>
  
  <p>Note:
  The question is mostly about linux internals but it also involves kernel programming. I read a lot about usb manipulation/monitoring programs, I read manual pages of udevadm. But these approaches do not help me at all. I think I need to alter either kernel or device internals programatically. </p>
  
  <p>I have also tried manipulating <code>authorized</code> file that resides in <code>/sys/devices/pci0000:00/0000:00:14.0/usb1/1-4</code> (for a keyboard). It's default value is 1. Changing it to 0 successfully disabled the bus (NOT device, but the physical usb port. The same device can still be used when plugged in another port). But making it 0 also stopped udev events from this usb port. So, I can disable the port if the user plugs in a forbidden device but I cannot decide when to enable it since I cannot listen to <code>remove</code> events in udev. Would it make sense if I delve deeper to lower levels of code (possibly kernel) and listen to usb events in some other way?</p>