Reverse Engineering a Samsung Scanner Button, page 2
Pages: 1 2 3 4 5 6 7 8
In this way we can have all actions and parameters regarding the scanning, processing, and storing of the resultant file orchestrated by a script that we can make changes to.
We will not be making a fully fledged Linux scanner driver, however we are going to use the LibUSB library to create C code that can communicate with the device. You could very well use this technique to do just that... write a full driver by reverse engineering it's behavior while connected to a Windows host.
Our method of finding the sequence of commands that the host sends to the device to query the scanner button will be to run a Windows XP Virtual Machine on the CentOS host that is connected to the scanner device. Through built-in functionality of the Linux kernel, we are going to 'sniff' the USB traffic while pressing the 'scan' button, and dump this traffic to a file.
This functionality will be provided by "usbmon" (USB monitor) and the "debugfs" (Debugging file system) features of the Linux kernel. I will go into what "usbmon" is actually doing a little bit later.
We will echo the contents of a usbmon object (pointing to the scanner device physically connected to the Linux host machine) into a file, while pressing the "Scan" button on the scanner (which is virtually connected to the Windows VM).
Then we will stop the capture and examine the data that was recorded in the file. This capture file will contain everything that we need to know in order to mock this behavior using the LibUSB library, and initiate a scan.
What we will use to actually perform the scan is SANE which is a Linux based scanner API, with the binary "scanimage" that can be called to scan a document and store it's data in a file. SANE packages are available for all major distros.
Contrary to what you might think, SANE only handles the ability to use it's driver to initiate a scan from a given piece of scanner hardware via a software interface (the UI of SANE). What this means is that, if you wish to initiate a scan via the onboard controls (buttons) of a scanner, you must use an additional piece of software, which is a "scanner button daemon" from this source: scanbd.
However, the addition of newer hardware to this piece software is rather slow. You have to understand that 'scanbd' is after all, an open source project. Most of these projetcs are lead by a small group of individuals, doing the work themselves when they have the time to add new code for newer hardware. This fact is why my article even exists... at the time that I did all of this, my scanner hardware was not in the code base of "scanbd". I was on my own, so thus I reverse engineered the actions that occur when you press the 'scan' button on the unit that I purchased.
This functionality to perform a scan via the hardware buttons of scanner unit does not exists in SANE and probably never will. It is difficult enough to have the ability to cover such a large hardware base of scanner units and be able to initiate a scan from a user interface, and it is nother story altogether to cover the functionality of the buttons on hundreds of hardware units.
We could write a complete driver if we wanted to, which would detect the "Scan" button being pressed, then initiate the scan, send a buffer to the scanner, have the scanner fill the buffer with the raw data produced by the scan, then save the contents of the buffer to a file.
However, this would add a lot of complexity to this article and I want to stick to fundamentals so that this writing is effective in demonstrating the general process of detecting when a button is pressed on a USB device. In this case, it just happens to be a scanner.
It is also beyond the scope of this article to go through the steps of setting up a CentOS Linux server, as well as the process of setting up VMWare or VirtualBox to host a Windows XP VM. Going forward, this article assumes that you already have a running Linux server as well as a running Windows XP VM on that server. You should also have gone through the steps to make it possible to enable USB devices to connect to your XP VM. There will be some details regarding this, however they will pertain to using a CentOS host and VirtualBox to run the XP VM.
Some other details regarding the process of querying the scanner button will of course pertain to the Samsung SCX-3405w. It is my intent to show this process in such a way that the reader can apply the logic to code adapted to their own USB scanner. If you happen to actually have the Samsung unit, then you are in luck since you can use most of the C code without modification.
One last note before we begin is that the reader does not have to run Windows XP on their VM. You can use Windows 7 as long as the scanner you are using has a Windows 7 driver and the native software from the manufacturer that makes it possible to push the hardware 'scan' button to produce a scan, and store the resulting image in a file, in a specifed location on disk.