Page 1 of 1

Problems with USB printer and hotplug script

Posted: Sun Aug 25, 2013 12:41 pm
by leoforti
I'm trying to run an HP LaserJet 1020 printer connected via USB to a TP-Link WDR-4300 v1.3 with Gargoyle 1.5.10 installed.
The router works perfectly in all aspects but I can't print via USB.
When I connect the printer to the usb port is detected by Gargoyle, and indeed in the web interface displays the message "HP LaserJet 1020 is connected via USB - You can connect to your printer on IP 192.168.1.1 via HP JetDirect Protocol". So far so good. Then I set the printer in Windows by following the tutorial and send a job to print... nothing happens. Reading here I find that this printer need to download their firmware every time is powered-on or else it wont work. So, following the blog instructions and other sources like this, this and this, I get to write the following script:
#!/bin/sh

DEVICE=/dev/usb/lp0
FIRMWARE=/usr/lib/sihp1020.dl
PROD_ID=3f0/2b17/100

if [ "$ACTION" == "add" ] && [ "$DEVTYPE" == "usb_interface" ] && [ "$PRODUCT" == "$PROD_ID" ]; then
        for i in $(seq 10); do
                if [ -c $DEVICE ]; then
                        if [ $(usb_printerid $DEVICE | grep -c FWVER) == 0 ]; then
                                cat $FIRMWARE > $DEVICE
                                exit
                        fi
                else
                        sleep 3
                fi
        done
fi
and save it as: /etc/hotplug.d/30-hplj1020 (in that directory I see also the files 10-usb, 20-modeswitch and 80-usb-printer).
For the script to run, I downloaded the firmware (sihp1020.dl and put it in /usr/lib), then I installed usbutils package through opkg, and from here I donwloaded the usb_interface binary and then copied in /user/bin (I used it to check if the firmware is already in the printer or not).

Also, I added this to /etc/config/firewall
# Allow network attached printer
config 'rule'
        option 'src' 'lan'
        option 'proto' 'tcp'
        option 'dest_port' '9100 '
        option 'target' 'ACCEPT'
to allow connections on port 9100.

I rebooted the router, powered on the printer, send a print job and... nothing happends. It appears in the webinterface, but when I print it not work. The job is sent (in the router's log appears a connection and job received in port 9100) but nothing comes from the printer.

After much testing and error, I discovered that the script fails on the line 9 when is trying to check the existence of /dev/usb/lp0. If I use the sentence

if [-c $ DEVICE]

as the test condition it fails (wich is rare, because in theory lp0 is a character-device). I tried changing it by

if [ $(ls / dev/usb/lp0 | grep-c lp0) == 1 ]

but also fails (for unkwnon reasons). Then I directly eliminated the "if" and replaced directly by:

sleep 3

followed by

cat $FIRMWARE > $DEVICE

but also not work! Nothing happens, the firmware is not sent.
All the rest of the script is tested and it works.
The strangest thing of all is that if I write directly from the console:

cat $FIRMWARE > $DEVICE

and then invoke

sh /usr/lib/gargoyle/configure_printer.sh

(to set the webinterface and the p910d print server) the printer starts (you hear the sound that starts) and then prints normally!!
So.. what can be happening with the hotplug script? Can anyone give me some help?

Thanks in advance and sorry for my bad english (I'm from Argentina).

Re: Problems with USB printer and hotplug script

Posted: Sun Sep 15, 2013 7:18 am
by kormikez
Hey,

Initially I had somehow similar issue. The printer was only working when I restarted the router, but if I turned the printer off and than back on (or unplugged for a while) - it did not work until I did driver cat to lp0, as if the hotplug.d script was not being executed at all. I believe I solved it by doing chmod +x /etc/hotplug.d/usb/* since I realized that some scripts had the execute permission, some not. You may try.

What else comes to my mind - having described your issue in details, you have not mentioned about setting the printer ID. Is 3f0/2b17/100 is valid for your printer?

Regards,
kormikez

Re: Problems with USB printer and hotplug script

Posted: Sat Sep 21, 2013 12:00 am
by leoforti
Thanks for the response.
I checked the permissions but all are OK. All the scripts in the USB folder have the correct execute permission. Already check that :(

The printer ID is valid for my printer (HP 1020). I checked that also with lsusb (from usbutils package) and the string matches. Also, if I write little debug messages in the code I see that the ID-check is working.
Even more, with the help of debug messages (logging) I see it stops right in the test condition while checking the existance of lp0. The crazy thing is that lp0 is there! But none of the sentence I wrote works. Why?!
Funny thing is, from console (putty) the sentences (both) works and the cat too.
Maybe is a permission issue but... where?

Thanks again.

Re: Problems with USB printer and hotplug script

Posted: Wed Sep 25, 2013 11:19 am
by kormikez
On some forum I saw a script code with such device check:

Code: Select all

if [ "$PRODUCT" = "3f0/2b17/100" -a "$ACTION" = "add" ]; then
        for i in $(seq 30); do
                if [ -c $DEVICE ]; then
                        cat $FIRMWARE > $DEVICE
                        exit
                fi
                sleep 1
        done
fi
I was wondering, what is the actual reason for using that loop - but seing your issue with nonexistant lp0, I think I'm starting to understand.

Please try this and let me know if it worked for you.

Re: Problems with USB printer and hotplug script

Posted: Mon Oct 07, 2013 6:13 pm
by leoforti
Already check that. Doesn't work.
All the scripts I tried fail whn checking the existance of lp0. And if I ignore if it exist or not and tried cat firmware anyway, it doesnt work. The firmware never reach the printer.
But again, when executing manually the commands from the console, it works fine.
Its driving me crazy.

Any clues?

Re: Problems with USB printer and hotplug script

Posted: Sun Nov 24, 2013 9:41 am
by kormikez
I made a workaround script for you:

Code: Select all

#!/bin/sh

DRIVER_FILE=/usr/lib/sihp1018.dl

if [ ! -f /tmp/printer_status ]; then
        echo 1 > /tmp/printer_status
fi

old_status=$(cat /tmp/printer_status);

grep LaserJet /proc/bus/usb/devices >/dev/null;
echo $? > /tmp/printer_status
new_status=$(cat /tmp/printer_status);

if [ $new_status -lt $old_status ];
then
        cat $DRIVER_FILE > /dev/usb/lp0
fi
Save it i.e. as /root/detect_printer.sh and add a crontab entry:

Code: Select all

* * * * * /root/detect_printer.sh
Not a perfect solution (in worst case you might need to wait 1 minute after you turn on the printer), but it's better than nothing.

Sorry for the late response, but there have been lots of changes on my life lately and, well, the priorities changed ;)

Re: Problems with USB printer and hotplug script

Posted: Mon Aug 18, 2014 5:59 pm
by leoforti
Thanks kormikez!

It works! After adding the script and the cron entry I rebooted the router, turned on the printer and a few moments later I heard the sound of the printer initializing (the firmware was sent to the device). I tried to print a test page ... success! :D :D :D

Now, I have a different question ... Is it possible to make work the bidirectional communication with the printer?
I verified that it was enabled in the printer driver options, but errors (no paper) or two-sided printing (duplex) does not work.

Any ideas?

Thanks again!

Re: Problems with USB printer and hotplug script

Posted: Sat Sep 27, 2014 8:05 am
by Winthrop
leoforti wrote:Thanks kormikez!

It works! After adding NooCube to my daily routine and the script and the cron entry I rebooted the router, turned on the printer and a few moments later I heard the sound of the printer initializing (the firmware was sent to the device). I tried to print a test page ... success! :D :D :D

Now, I have a different question ... Is it possible to make work the bidirectional communication with the printer?
I verified that it was enabled in the printer driver options, but errors (no paper) or two-sided printing (duplex) does not work.

Any ideas?

Thanks again!
Hi, Kormikez. I'm having the same issue and I tried to use your solution. It worked for a while but now it's not working again.