Installing DraftSight 2016 Pre-Release in Gentoo Linux

Last year I posted about installing the 2015 Draftsight Free 2D CAD application in Gentoo Linux. Now a DraftSight 2016 Pre-Release is available, and it works in Gentoo. Dassault Systemes has fixed the annoying cursor lag in the 2015 Linux version. \o/

You can click on a link ‘Download DraftSight 2016 for Ubuntu (beta)’ on their Web page ‘DraftSight® FREE* CAD Software Download‘ and copy the downloaded file draftSight.deb to /usr/portage/distfiles/draftsight-1.7.0_beta.deb. Then rename the ebuild draftsight-bin-1.6.1_beta.ebuild to draftsight-bin-1.7.0_beta.ebuild in your local overlay directory /usr/local/portage/media-gfx/draftsight-bin/, but draftsight-bin-1.7.0_beta.ebuild is listed below anyway:

# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=5

inherit fdo-mime udev unpacker

MY_PN="draftsight"
MY_P="${MY_PN}-${PV}"
DESCRIPTION="Professional 2D CAD application, supporting DWT, DXF and DWG."
HOMEPAGE="http://www.3ds.com/products/draftsight/free-cad-software/"
SRC_URI="${MY_P}.deb"

LICENSE="${MY_PN}"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""
S="${WORKDIR}"

QA_PRESTRIPPED="opt/dassault-systemes/${MY_PN}/bin/DWGConverter
	amd64? ( opt/dassault-systemes/${MY_PN}/lib/libaudio.so.2 )"
QA_TEXTRELS="opt/dassault-systemes/${MY_PN}/lib/libDDKERNEL.so.1"
QA_EXECSTACK="opt/dassault-systemes/${MY_PN}/bin/FxCrashRptApp
opt/dassault-systemes/${MY_PN}/lib/libDDKERNEL.so.1"

RESTRICT="fetch"
DEPEND=""
RDEPEND="amd64? (
		sys-libs/zlib
		net-print/cups
		dev-libs/expat
		dev-libs/glib:2
		media-libs/glu
		media-libs/phonon
		dev-qt/qtcore:4
		dev-qt/qtdbus:4
		dev-qt/qtgui:4
		dev-qt/qtopengl:4
		dev-qt/qtsql:4
		dev-qt/qtwebkit:4
		dev-qt/qtsvg:4
		media-libs/alsa-lib
		media-libs/fontconfig
		media-libs/freetype
		x11-libs/libICE
		x11-libs/libSM
		x11-libs/libX11
		x11-libs/libXext
		x11-libs/libXrender
		x11-libs/libXt
		media-libs/nas
		)"

pkg_nofetch() {
	einfo "Upstream has a mandatory EULA agreement to download this file."
	einfo "Please navigate your browser to:"
	einfo "http://www.3ds.com/products-services/draftsight-cad-software/free-download/"
	einfo "Click \"Download DraftSight 2015 for Ubuntu (beta)\""
	einfo "Download the deb file and move it to ${DISTDIR}/${MY_P}.deb"
}

src_install() {
	cp -R "${WORKDIR}/opt" "${D}"
	exeinto /usr/bin
	doexe "${FILESDIR}/${MY_PN}"

	# prepare for dongle
	udev_dorules "${FILESDIR}"/10-ft-rockey.rules
}

pkg_postinst() {
	elog "To use DraftSight as your default viewer for DWG, DXF, and DWT"
	elog "Please run the following commands respectively as your normal user:"
	elog "xdg-mime default \"dassault-systemes\"_\"draftsight.desktop\" \"application/vnd.dassault-systemes.draftsight-dwg\""
	elog "xdg-mime default \"dassault-systemes\"_\"draftsight.desktop\" \"application/vnd.dassault-systemes.draftsight-dxf\""
	elog "xdg-mime default \"dassault-systemes\"_\"draftsight.desktop\" \"application/vnd.dassault-systemes.draftsight-dwt\""

	fdo-mime_desktop_database_update
	fdo-mime_mime_database_update

	for size in 16 32 48 64 128 ; do
		local XDG_OPTS="--noupdate --novendor --mode system --size ${size}"
		xdg-icon-resource install ${XDG_OPTS} --context apps \
			"${ROOT}/opt/dassault-systemes/DraftSight/Resources/pixmaps/${size}x${size}/program.png" \
			"dassault-systemes.draftsight"
		xdg-icon-resource install ${XDG_OPTS} --context apps --theme gnome \
			"${ROOT}/opt/dassault-systemes/DraftSight/Resources/pixmaps/${size}x${size}/program.png" \
			"dassault-systemes.draftsight"
		for mimetype in dwg dxf dwt ; do
			xdg-icon-resource install ${XDG_OPTS} --context mimetypes \
				"${ROOT}/opt/dassault-systemes/DraftSight/Resources/pixmaps/${size}x${size}/file-${mimetype}.png" \
				"application-vnd.dassault-systemes.draftsight-${mimetype}"
			xdg-icon-resource install ${XDG_OPTS} --context mimetypes --theme gnome \
				"${ROOT}/opt/dassault-systemes/DraftSight/Resources/pixmaps/${size}x${size}/file-${mimetype}.png" \
				"application-vnd.dassault-systemes.draftsight-${mimetype}"
		done
	done
	xdg-icon-resource forceupdate
}

pkg_postrm() {
	fdo-mime_desktop_database_update
	fdo-mime_mime_database_update
	for size in 16 32 48 64 128 ; do
		xdg-icon-resource uninstall --noupdate --context apps --mode system \
			--size ${size} "dassault-systemes.draftsight"
		xdg-icon-resource uninstall --noupdate --context apps --mode system --theme gnome \
			--size ${size} "dassault-systemes.draftsight"
		for mimetype in dwg dxf dwt ; do
			xdg-icon-resource uninstall --noupdate --context mimetypes --mode system \
				--size ${size} "application-vnd.dassault-systemes.draftsight-${mimetype}"
			xdg-icon-resource uninstall --noupdate --context mimetypes --mode system --theme gnome \
				--size ${size} "application-vnd.dassault-systemes.draftsight-${mimetype}"
		done
	done
	xdg-icon-resource forceupdate
}

The files 10-ft-rockey.rules and draftsight in the directory /usr/local/portage/media-gfx/draftsight-bin/files/ can stay the same as for the previous ebuild draftsight-bin-1.6.1_beta.ebuild, but they are listed below anyway:

BUS=="usb", SYSFS{idVendor}=="096e", MODE=="0666"

#! /bin/bash
BASEDIR="/opt/dassault-systemes/DraftSight/Linux"

export LD_LIBRARY_PATH="${BASEDIR}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"

# work around DT_RPATH (.) security issue by chdir'ing into expected $LD_LIBRARY_PATH
cd "${BASEDIR}"

exec "${BASEDIR}/DraftSight" $*

Then generate the manifest as usual and merge the package.

Automatically detecting files placed in my Downloads directory in Gentoo Linux and scanning them for viruses

I have been using Linux for almost a decade and have never been unduly concerned about viruses on my machines running Linux. However, I do receive files from people who use Windows and Mac OS, and some of those files might contain Windows or Mac OS viruses, so, as a matter of courtesy and assistance to others, it would make some sense to scan those files before passing them on. Furthermore, as I use some Windows applications under WINE, it would also make sense to scan received files for Windows viruses if I am going to use those files with a Windows application running under WINE.

External files could get into my Gentoo Linux installations via pen drives, memory cards, optical discs, e-mails, my Dropbox directory and downloads from Web sites. In this post I am going to concentrate on the last of these. All the various e-mail account providers I use already scan e-mails for viruses on their e-mail servers before I even download e-mail into the e-mail client on my laptop (standard practice these days), so e-mail is not a particular worry.

I have had ClamAV and its GUI, ClamTk, installed for a long time. Whilst ClamTk can be used to schedule a daily update of virus signatures and a daily scan of one’s home directory by ClamAV, I normally run ClamTk and ClamAV ad hoc. However, I can see some benefit in launching ClamAV automatically when I download a file from the Internet, so I decided to do the following …

Automatically scan a file downloaded via a Web browser

I use Firefox to browse the Web, and had configured it to download files to the directory /home/fitzcarraldo/Downloads/. I decided to monitor automatically the Downloads directory for the addition of any file. As I use the ext4 file system, the method I opted to use is inotify, specifically the inotifywait command which is available once you install the package sys-fs/inotify-tools.

It is surprisingly easy to create a shell script to detect files downloaded into a directory. The following script, running continuously in a terminal, would detect any files created in my /home/fitzcarraldo/Downloads directory, scan the new files with ClamAV and display a report in the terminal window:

#!/bin/bash

echo
DIR=$HOME/Downloads

inotifywait -q -m -e create --format '%w%f' $DIR | while read FILE
do
     date
     echo "File $FILE has been detected. Scanning it for viruses now ..."
     clamscan $FILE
     echo
done

A usable script would need to be a bit more sophisticated than the one shown above, because an existing file in the directory could be overwritten by one with the same name, or opened and amended. Furthermore, the script above would need a permanently open terminal window. Therefore I created a script to run in the background and use a GUI dialogue tool to pop up a window with the virus scanner’s report when the script detects a new or changed file in the Downloads directory. As this laptop has KDE 4 installed I opted to use KDialog to display the pop-up window, but I could instead have used Zenity. The final script is shown below.

#!/bin/bash

DIR=$HOME/Downloads

# Get rid of old log file
rm $HOME/virus-scan.log 2> /dev/null

inotifywait -q -m -e close_write,moved_to --format '%w%f' $DIR | while read FILE
do
     # Have to check file length is nonzero otherwise commands may be repeated
     if [ -s $FILE ]; then
          date > $HOME/virus-scan.log
          clamscan $FILE >> $HOME/virus-scan.log
          kdialog --title "Virus scan of $FILE" --msgbox "$(cat $HOME/virus-scan.log)"
     fi
done

Now when I download a file in Firefox, a window pops up, displaying a message similar to the following:

Virus scan of /home/fitzcarraldo/Downloads/eicar_com.zip – KDialog

Fri 19 Feb 23:42:02 GMT 2016
/home/fitzcarraldo/Downloads/eicar_com.zip: Eicar-Test-Signature FOUND

———– SCAN SUMMARY ———–
Known viruses: 4259980
Engine version: 0.98.7
Scanned directories: 0
Scanned files: 1
Infected files: 1
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 4.595 sec (0 m 4 s)

Notice in the above message that ClamAV detected a virus in a file eicar_com.zip that I downloaded from the European Expert Group for IT Security Web site (originally ‘European Institute for Computer Antivirus Research’). In fact the executable eicar.com does not contain a real virus; it was designed to contain a known signature that virus scanner creators and users can use in checking anti-virus software. You can find out more about the virus test files on the EICAR Web site.

Of course, if I use applications other than Firefox to download files, I need to make sure they download the files into the applicable directory so that the script can detect and scan the files:

fitzcarraldo@clevow230ss ~ $ cd Downloads/
fitzcarraldo@clevow230ss ~/Downloads $ youtube-dl -o Carnavalito.mp4 -f 18 https://www.youtube.com/watch?v=ZDUL3w7zFD4
ZDUL3w7zFD4: Downloading webpage
ZDUL3w7zFD4: Downloading video info webpage
ZDUL3w7zFD4: Extracting video information
ZDUL3w7zFD4: Downloading MPD manifest
[download] Destination: Carnavalito.mp4
[download] 100% of 16.61MiB in 00:05

So, now I have a shell script that pops up a window informing me whether or not any file I put in $HOME/Downloads/ contains a virus. But I would like the script to be launched automatically when I login to the Desktop Environment. Therefore, as I use KDE 4, I selected ‘System Settings’ > ‘Startup and Shutdown’ and, in the ‘Autostart’ pane, clicked on ‘Add Script…’ and entered the path to my shell script (I left ‘create as symlink’ ticked). Now, every time I use KDE, any file placed (automatically or manually) into $HOME/Downloads/ is scanned for viruses automatically and a window pops up giving the result.

As my laptop is not always connected to the Internet, I prefer to update the ClamAV virus signatures database manually, which I do either using the ClamTk GUI or via the command line using the freshclam command:

fitzcarraldo@clevow230ss ~ $ su
Password:
clevow230ss fitzcarraldo # freshclam
ClamAV update process started at Sat Feb 20 10:51:01 2016
WARNING: Your ClamAV installation is OUTDATED!
WARNING: Local version: 0.98.7 Recommended version: 0.99
DON'T PANIC! Read http://www.clamav.net/support/faq
main.cvd is up to date (version: 55, sigs: 2424225, f-level: 60, builder: neo)
Downloading daily-21375.cdiff [100%]
Downloading daily-21376.cdiff [100%]
Downloading daily-21377.cdiff [100%]
Downloading daily-21378.cdiff [100%]
Downloading daily-21379.cdiff [100%]
Downloading daily-21380.cdiff [100%]
Downloading daily-21381.cdiff [100%]
Downloading daily-21382.cdiff [100%]
Downloading daily-21383.cdiff [100%]
Downloading daily-21384.cdiff [100%]
Downloading daily-21385.cdiff [100%]
Downloading daily-21386.cdiff [100%]
Downloading daily-21387.cdiff [100%]
Downloading daily-21388.cdiff [100%]
Downloading daily-21389.cdiff [100%]
Downloading daily-21390.cdiff [100%]
Downloading daily-21391.cdiff [100%]
daily.cld updated (version: 21391, sigs: 1850214, f-level: 63, builder: neo)
bytecode.cld is up to date (version: 271, sigs: 47, f-level: 63, builder: anvilleg)
Database updated (4274486 signatures) from db.UK.clamav.net (IP: 129.67.1.218)
WARNING: Clamd was NOT notified: Can't connect to clamd through /var/run/clamav/clamd.sock: No such file or directory

Stopping my laptop spontaneously resuming immediately after Suspend to RAM

If I selected ‘Suspend to RAM’ via the Desktop Environment in the Gentoo Linux installation on my Clevo W230SS laptop, the laptop did suspend but then immediately resumed automatically. The same thing happened if I suspended the laptop using either of the following commands from the command line:

root # pm-suspend

user $ qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Suspend

This behaviour was annoying, as it meant I had to shut down the laptop completely when I was not at my desk for a long time, rather than just being able to suspend the laptop.

Problem 1: USB devices

I usually have several USB devices connected to my laptop when I am at home or in the office, and I began to suspect that these USB connections were somehow causing Linux to resume as soon as it had suspended. Searching the Web turned up a Q&A page that seemed to confirm my suspicion: Why does my laptop resume immediately after suspend? I installed the utility acpitool mentioned on that Web page and used it with the ‘-w‘ option to check which wakeup-capable USB devices were currently enabled in my installation:

root # acpitool -w
   Device       S-state   Status   Sysfs node
  ---------------------------------------
  1. RP01         S4    *disabled  pci:0000:00:1c.0
  2. PXSX         S4    *disabled
  3. RP02         S4    *disabled
  4. PXSX         S4    *disabled
  5. RP03         S4    *disabled  pci:0000:00:1c.2
  6. PXSX         S4    *disabled  pci:0000:03:00.0
  7. RP04         S4    *disabled  pci:0000:00:1c.3
  8. PXSX         S4    *disabled  pci:0000:04:00.0
  9.            *disabled  platform:rtsx_pci_sdmmc.0
  10.           *disabled  platform:rtsx_pci_ms.0
  11. RLAN        S4    *disabled
  12. RP05        S4    *disabled
  13. PXSX        S4    *disabled
  14. RP06        S4    *disabled
  15. PXSX        S4    *disabled
  16. RP07        S4    *disabled
  17. PXSX        S4    *disabled
  18. RP08        S4    *disabled
  19. PXSX        S4    *disabled
  20. GLAN        S4    *disabled
  21. EHC1        S3    *enabled   pci:0000:00:1d.0
  22. EHC2        S3    *enabled   pci:0000:00:1a.0
  23. XHC         S3    *disabled  pci:0000:00:14.0
  24. HDEF        S4    *disabled  pci:0000:00:1b.0
  25. PEG0        S4    *disabled  pci:0000:00:01.0
  26. PEGP        S4    *disabled  pci:0000:01:00.0
  27. PEGA        S4    *disabled
  28. PWRB        S3    *enabled   platform:PNP0C0C:0
^C
root #

(I had to use Ctrl-C to get back to the command prompt.)

I then used the command ‘acpitool -W <device number>‘ on each of the three enabled devices (21, 22 and 28 above) in order to find out which of them needed to be disabled in order for my laptop to remain suspended when I suspended it. I found that I only needed to disable devices EHC1 (pci:0000:00:1d.0) and EHC2 (pci:0000:00:1a.0) to be able to suspend the laptop successfully:

root # acpitool -W 21 | grep 21
  Changed status for wakeup device #21 (EHC1)
  21. EHC1        S3    *disabled  pci:0000:00:1d.0
^C
root # acpitool -W 22 | grep 22
  Changed status for wakeup device #22 (EHC2)
  22. EHC2        S3    *disabled  pci:0000:00:1a.0
^C
root # pm-suspend

In this laptop these two devices are two internal USB root hubs:

user $ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
        |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 4, If 0, Class=Mass Storage, Driver=usb-storage, 480M
            |__ Port 3: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 6, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 4: Dev 7, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 4: Dev 7, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 3: Dev 4, If 0, Class=Wireless, Driver=btusb, 12M
        |__ Port 3: Dev 4, If 1, Class=Wireless, Driver=btusb, 12M
        |__ Port 4: Dev 5, If 0, Class=Video, Driver=uvcvideo, 480M
        |__ Port 4: Dev 5, If 1, Class=Video, Driver=uvcvideo, 480M

The next challenge was to find out how to disable and re-enable the two devices automatically when I suspend and resume the installation. Further searching of the Web turned up another Q&A page which pointed me in the right direction: How to run a script when suspending/resuming?. It turns out that you need to put a script of the following form in the directory /etc/pm/sleep.d/:

#!/bin/bash

case "$1" in
    suspend)
        # executed on suspend
        ;;
    resume) 
        # executed on resume
        ;;
    *)
        ;;
esac

If you want the script to run when hibernating and thawing, the tests would be for ‘hibernate‘ and ‘thaw‘ instead of ‘suspend‘ and ‘resume‘.

The thread [SOLVED] Computer immediately resumes after suspend in the KDE Forums almost gave me the solution I needed. I created a file /etc/pm/sleep.d/01-toggle-usb-hubs containing the following:

#!/bin/sh
#
username=fitzcarraldo
userhome=/home/$username
export XAUTHORITY="$userhome/.Xauthority"
export DISPLAY=":0"
#
case "$1" in
    suspend|hibernate)
        # Unbind ehci-pci for the device 0000:00:1a.0
        echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci-pci/unbind
        # Unbind ehci-pci for the device 0000:00:1d.0
        echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci-pci/unbind
    ;;
    resume|thaw)
        # Bind ehci-pci for the device 0000:00:1a.0
        echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci-pci/bind
        # Bind ehci-pci for the device 0000:00:1d.0
        echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci-pci/bind
    ;;
    *)
        exit $NA
    ;;
esac

I obtained the device details from the output of the ‘acpitools -w‘ command listed earlier and by looking in the directory /sys/bus/pci/drivers/ehci-pci:

root # ls /sys/bus/pci/drivers/ehci-pci
0000:00:1a.0  0000:00:1d.0  bind  new_id  remove_id  uevent  unbind

Notice that the script tests for either ‘suspend‘ or ‘hibernate‘ to disable the two devices, and tests for either ‘resume‘ or ‘thaw‘ to enable the two devices.

I made the script executable:

root # chmod +x /etc/pm/sleep.d/01-toggle-usb-hubs


Problem 2: Blank X Windows display due to NVIDIA closed-source driver bug

However, a problem remained: My laptop has an NVIDIA GPU and, when resuming from suspension, the X Windows display (Virtual Terminal 7) was a blank screen with only the mouse pointer visible. Now, it so happens that I also experience this behaviour if I switch from Virtual Terminal 7 to e.g. Virtual Terminal 1 (Ctrl-Alt-F1) and then switch back to Virtual Terminal 7 (Ctrl-Alt-F7). Apparently this is due to a bug in the closed-source NVIDIA driver (I am currently using Gentoo package x11-drivers/nvidia-drivers-358.16-r1). However, if I first disable compositing before switching to another virtual terminal, the X Windows display on Virtual Terminal 7 is still visible when I switch back to Virtual Terminal 7. It turns out there is a known bug in the NVIDIA closed-source driver, as explained in the following KDE bug report and thread in the NVIDIA CUDA ZONE Forums:

KDE Bugtracking System Bug No. 344326 – Black or corrupted screen on resume from suspend

NVIDIA CUDA ZONE Forums – Black screen on resume from suspend with 325.15 and KWin 4.11 with enabled compositing

As the suggested work-around is to disable compositing before suspending to RAM, I created a script /etc/pm/sleep.d/02-toggle-compositing containing the following:

#!/bin/sh
#
username=fitzcarraldo
userhome=/home/$username
export XAUTHORITY="$userhome/.Xauthority"
export DISPLAY=":0"
#
case "$1" in
    suspend|hibernate)
        su $username -c "qdbus org.kde.kwin /KWin toggleCompositing" &
    ;;
    resume|thaw)
        su $username -c "qdbus org.kde.kwin /KWin toggleCompositing" &
    ;;
    *)
        exit $NA
    ;;
esac

As I have KDE 4 on this laptop, I made the script use the command ‘qdbus org.kde.kwin /KWin toggleCompositing‘ to disable/enable compositing, so replace that command with the appropriate command if you are not using KDE 4.

I made the script executable:

root # chmod +x /etc/pm/sleep.d/02-toggle-compositing

Now both the scripts in the directory /etc/pm/sleep.d/ run when I suspend or resume the laptop, and everything works as expected. Mission accomplished!:-)

KDE Connect on a hotel Wi-Fi network

KDE Connect

I am a fan of KDE Connect (see my 2014 post about an earlier version), but had previously been unable to use it with a hotel network. However today I managed to do that, and here is how I did it …

I first connected my laptop and my Samsung Galaxy Note 4 to the hotel’s Wi-Fi network, then used the ifconfig command in Linux on my laptop to find the IP address of my laptop on the hotel’s network. Note that the IP address one sees if one uses a Web site such as WhatIsMyIPAddress will be the laptop’s outward-facing IP address, not the IP address of the laptop on the hotel network. For example, the ifconfig command has just shown me that my current DHCP-allocated IP address is 10.154.245.40 on this hotel’s network for this session whereas the Web site WhatIsMyIPAddress is showing my IP address as 78.100.57.102.

By the way, I can also use the excellent Android utility Fing on my Galaxy Note 4 to find the IP address of my laptop on the hotel’s network. It is quite interesting to use Fing to see what other devices (their hostname and IP address) are currently connected to the hotel’s network.

Anyway, then I launched KDE Connect on the Galaxy Note 4, tapped and ‘Add devices by IP’, and entered the laptop’s IP address (10.154.245.40 in this specific case). I was able to pair with KDE Connect running on my laptop and send files from my phone to the laptop, and vice versa.

NetworkManager: Failed to activate – The name org.freedesktop.NetworkManager was not provided by any .service files

Because I need to connect quickly and easily to numerous wired and wireless networks (DHCP or static IP addressing), I use NetworkManager in my Gentoo Linux amd64 installation running OpenRC and KDE 4. My Clevo W230SS laptop has an Intel Dual Band Wireless-AC 7260 Plus Bluetooth adapter card, and my installation uses the iwlwifi module:

# lspci -knn | grep Net -A2
03:00.0 Network controller [0280]: Intel Corporation Wireless 7260 [8086:08b1] (rev bb)
        Subsystem: Intel Corporation Dual Band Wireless-AC 7260 [8086:4070]
        Kernel driver in use: iwlwifi
# lsmod | grep iwl
iwlmvm                143919  0
iwlwifi                75747  1 iwlmvm

As I am using NetworkManager instead of netifrc, in accordance with the instructions in the Gentoo Wiki article on NetworkManager I do not have any net.* services enabled (not even net.lo):

# rc-update show -v
       NetworkManager |      default                 
                acpid |                              
            alsasound |                              
         avahi-daemon |                              
       avahi-dnsconfd |                              
               binfmt | boot                         
            bluetooth |      default                 
             bootmisc | boot                         
         busybox-ntpd |                              
     busybox-watchdog |                              
                clamd |                              
          consolefont |                              
           consolekit |      default                 
               cronie |      default                 
         cups-browsed |      default                 
                cupsd |      default                 
                 dbus |      default                 
                devfs |                       sysinit
               dhcpcd |                              
                dhcpd |                              
             dhcrelay |                              
            dhcrelay6 |                              
                dmesg |                       sysinit
              dropbox |                              
           fancontrol |                              
                 fsck | boot                         
                 fuse |                              
           git-daemon |                              
                  gpm |                              
              hddtemp |                              
             hostname | boot                         
              hwclock | boot                         
            ip6tables |                              
             iptables |                              
              keymaps | boot                         
            killprocs |              shutdown        
    kmod-static-nodes |                       sysinit
           lm_sensors |                              
                local |      default                 
           localmount | boot                         
             loopback | boot                         
      mit-krb5kadmind |                              
          mit-krb5kdc |                              
       mit-krb5kpropd |                              
              modules | boot                         
             mount-ro |              shutdown        
                 mtab | boot                         
                mysql |                              
                  nas |                              
         net.enp4s0f1 |                              
               net.lo |                              
             netmount |      default                 
           ntp-client |                              
                 ntpd |                              
           nullmailer |                              
              numlock |                              
  nvidia-persistenced |                              
           nvidia-smi |                              
              osclock |                              
              pciparm |                              
               procfs | boot                         
              pwcheck |                              
            pydoc-2.7 |                              
            pydoc-3.4 |                              
               rfcomm |                              
                 root | boot                         
               rsyncd |                              
            s6-svscan |                              
                samba |      default                 
                saned |                              
            saslauthd |                              
            savecache |              shutdown        
                 sntp |                              
                 sshd |      default                 
             svnserve |                              
                 swap | boot                         
            swapfiles | boot                         
              swclock |                              
               sysctl | boot                         
                sysfs |                       sysinit
            syslog-ng |      default                 
        teamviewerd10 |                              
         termencoding | boot                         
             timidity |                              
         tmpfiles.dev |                       sysinit
       tmpfiles.setup | boot                         
               twistd |                              
                 udev |                       sysinit
                  ufw | boot                         
              urandom | boot                         
       wpa_supplicant |                              
                  xdm |      default                 
            xdm-setup |

I have left the netmount service enabled in case I want to use network-attached file shares at home or in one of the various office locations where I work.

Networking works fine on my laptop with the many wired and wireless networks I have used except for one particular public wireless network (it is in an airport, has multiple Access Points, and its Access Points only support 802.11a/b/g, which may or may not be relevant) for which the following message would usually appear in a pop-up window when I tried to connect to the network from the KDE network management GUI after start-up:

Failed to activate
The name org.freedesktop.NetworkManager was not provided by any .service files

Error message displayed by KDE when trying to connect to one specific network

Error message displayed by KDE when trying to connect to one specific network


This occurred with both networkmanager-1.0.2-r1 and networkmanager-1.0.6, the two Stable Branch releases of NetworkManager currently available in Gentoo Linux.

The wireless network is not the only network at that particular location, and the ‘Failed to activate’ message occurred whichever network (wireless or wired) I tried to access at that location. When this problem occurred, it transpired that the NetworkManager service was not running (it had crashed):

$ nmcli d
Error: NetworkManager is not running.
$ rc-status
Runlevel: default
 dbus                   [  started  ]
 NetworkManager         [  crashed  ]
 netmount               [  started  ]
 syslog-ng              [  started  ]
 cupsd                  [  started  ]
 samba                  [  crashed  ]
 consolekit             [  started  ]
 cronie                 [  started  ]
 bluetooth              [  started  ]
 xdm                    [  started  ]
 cups-browsed           [  started  ]
 sshd                   [  started  ]
 local                  [  started  ]
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed
 xdm-setup              [  started  ]
 avahi-daemon           [  started  ]
Dynamic Runlevel: manual

(I am not bothered that Samba crashes in that particular location. It crashes even if a connection is established, because the public wireless network does not provide network file systems. Samba works fine when I connect the laptop to an office network or to my home network.)

Even if the ‘Failed to activate’ message occurred, sometimes (but not always) the laptop could still connect to networks after I restarted the NetworkManager service (albeit sometimes it was necessary to restart it more than once):

# /etc/init.d/NetworkManager restart

When it is possible to connect to networks, the NetworkManager service is of course running:

$ nmcli d
DEVICE    TYPE      STATE        CONNECTION           
sit0      sit       connected    sit0                 
wlp3s0    wifi      connected    Free_Airport_Internet
enp4s0f1  ethernet  unavailable  --                   
lo        loopback  unmanaged    --        
$ rc-status
Runlevel: default
 dbus                   [  started  ]
 NetworkManager         [  started  ]
 netmount               [  started  ]
 syslog-ng              [  started  ]
 cupsd                  [  started  ]
 samba                  [  crashed  ]
 consolekit             [  started  ]
 cronie                 [  started  ]
 bluetooth              [  started  ]
 xdm                    [  started  ]
 cups-browsed           [  started  ]
 sshd                   [  started  ]
 local                  [  started  ]
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed
 xdm-setup              [  started  ]
 avahi-daemon           [  started  ]
Dynamic Runlevel: manual

I searched the Web for the error message and, based on a recommendation on the Web page ‘nm-applet gives errors‘ claiming the problem is due to the iwlwifi driver when used with an Intel 7260 controller, I created a file /etc/modprobe.d/iwlwifi.conf containing the following line, and rebooted:

options iwlwifi power_save=0

However, the error message still occurred. So I changed the iwlwifi module options line to the following, as also recommended on that page, and rebooted:

options iwlwifi 11n_disable=1 power_save=0

However, the error message still occurred.

The default value for OpenRC’s rc_depend_strict variable is YES if rc_depend_strict is not declared in the file /etc/rc.conf, but I do not think that is the cause of the problem:

# Do we allow any started service in the runlevel to satisfy the dependency
# or do we want all of them regardless of state? For example, if net.eth0
# and net.eth1 are in the default runlevel then with rc_depend_strict="NO"
# both will be started, but services that depend on 'net' will work if either
# one comes up. With rc_depend_strict="YES" we would require them both to
# come up.
#rc_depend_strict="YES"

As already mentioned, sometimes just restarting the NetworkManager service once or more did enable the laptop to connect to the network. This made me wonder whether the problem had something to do either with the timing of the launch of the NetworkManager service or with the timing of the service establishing a connection. As netmount is the only other network-related service enabled at start-up, I checked the netmount service’s configuration file /etc/conf.d/netmount to see what it contained (it’s the same in both the latest stable openrc-0.17 and the latest testing openrc-0.18.2):

# You will need to set the dependencies in the netmount script to match
# the network configuration tools you are using. This should be done in
# this file by following the examples below, and not by changing the
# service script itself.
#
# Each of these examples is meant to be used separately. So, for
# example, do not set rc_need to something like "net.eth0 dhcpcd".
#
# If you are using newnet and configuring your interfaces with static
# addresses with the network script, you  should use this setting.
#
#rc_need="network"
#
# If you are using oldnet, you must list the specific net.* services you
# need.
#
# This example assumes all of your netmounts can be reached on
# eth0.
#
#rc_need="net.eth0"
#
# This example assumes some of your netmounts are on eth1 and some
# are on eth2.
#
#rc_need="net.eth1 net.eth2"
#
# If you are using a dynamic network management tool like
# networkmanager, dhcpcd in standalone mode, wicd, badvpn-ncd, etc, to
# manage the network interfaces with the routes to your netmounts, you
# should list that tool.
#
#rc_need="networkmanager"
#rc_need="dhcpcd"
#rc_need="wicd"
#
# The default setting is designed to be backward compatible with our
# current setup, but you are highly discouraged from using this. In
# other words, please change it to be more suited to your system.
#
rc_need="net"

As I am using NetworkManager rather than netifrc, I followed the instructions in the file’s comments and changed the file’s contents from:

rc_need="net"

to:

rc_need="networkmanager"

After making the above change, the console messages at boot-up included a new message:

* ERROR: netmount needs service(s) networkmanager

That message made sense: rc_need had been set to "networkmanager" and, obviously, netmount can only do its job if NetworkManager is running (AND a network connection has been established). However, notice that the name of the NetworkManager service initscript is /etc/init.d/NetworkManager, not /etc/init.d/networkmanager. In other words, the instructions in /etc/conf.d/netmount are wrong: the name of the service is actually ‘NetworkManager‘, not ‘networkmanager‘. So I changed /etc/conf.d/netmount to contain rc_need="NetworkManager" instead of rc_need="networkmanager" and, unsurprisingly, the above-mentioned error message no longer occurs. I have filed Gentoo Bugzilla Bug Report No. 564846 requesting that the comment in the configuration file be changed.

Nevertheless, the ‘Failed to activate’ message still occurred when I tried to connect to any network at that location by using the DE’s network management GUI, and therefore I still needed to restart the NetworkManager service manually in order to be able to connect to any network there. Although I am not yet sure of the root cause and solution, I have found a work-around which avoids me having to manually restart the NetworkManager service, as explained below.

Although OpenRC correctly launches the NetworkManager service, that service remains inactive until it actually establishes a network connection. This is not a bug, it is the way OpenRC and NetworkManager work (see the explanation in the Gentoo Forums thread NetworkManager has started, but is inactive). This is why the following console message appears during boot-up:

* WARNING: NetworkManager has already started, but is inactive

If you did not configure NetworkManager to connect automatically to a network, after logging-in to the DE you will need to use the DE’s network management GUI (plasma-nm in the case if KDE, nm-applet in the case of e.g. Xfce) to tell NetworkManager to connect to the desired network. However, I found that waiting that long before trying to connect is too late to avoid the ‘Failed to activate’ problem, i.e. NetworkManager crashes after a while. I do not know why this happens, but it usually happens only when I am at the location covered by one specific wireless network (which is why I wonder if the problem is a result of that network only supporting 802.11a/b/g). By configuring NetworkManager to connect automatically to the wireless network which seemed to trigger the problem, the NetworkManager service tries to connect earlier. It is possible to configure NetworkManager to do this either by using the DE network GUI and ticking ‘Automatically connect to this network when it is available’ for the relevant network connection, or by directly editing the relevant connection’s file in the directory /etc/NetworkManager/system-connections/.

Of the various wired and wireless connections I had configured on the laptop, I had named the problematic wireless network’s connection ‘Free_Airport_Internet’. So I edited the file /etc/NetworkManager/system-connections/Free_Airport_Internet and deleted the line ‘autoconnect=false‘ in the [connections] section of the file (the default value of the autoconnect variable is TRUE – see man nm-settings). I could instead have done this by using the DE’s network manager GUI and ticking ‘Automatically connect to this network when it is available’ for that network connection. Now, when the laptop boots, NetworkManager tries to connect to that network and the ‘Failed to activate’ problem is avoided. This works with or without the iwlwifi driver options I mentioned above, so, despite the claim on the Web page I referenced above, the root cause of the problem does not appear to be the iwlwifi driver. What I don’t understand is why the problem only seems to occur with one particular network (a public wireless network which happens to only support 802.11a/b/g), i.e. even if none of the NetworkManager connection files in my installation have been configured to try to establish a connection automatically, with all the other wireless networks I have used in other locations (I believe those all support at least 802.11a/b/g/n) I have been able to establish a connection manually by using the DE’s network management GUI.

The bottom line

If your installation uses NetworkManager and you experience the ‘Failed to activate’ message when trying to connect to networks from the DE’s network management GUI, check if the NetworkManager service is running. You can check by using the command ‘nmcli d‘ in a console. If it is not running, try to restart the NetworkManager service from the command line. If the connection is not already configured to start automatically, configure it to start automatically in order to try to make NetworkManager become active at an early stage.

POSTSCRIPT (November 6, 2015)

The two links below are to old bug reports regarding earlier versions of NetworkManager having trouble using wireless networks with multiple Access Points. I wonder if the problem I saw with NetworkManager crashing when not configured to connect automatically to the specific network I mentioned above is somehow related to those problems:

background scanning causes drivers to disassociate – WiFi roaming causes NetworkManager to lose routing

network-manager roams to (none) ((none)) – background scanning

Roaming to BSSID “(none)” certainly happens with this particular network too, as shown by the messages in the laptop’s system log from yesterday when I was using the laptop with that network (the laptop was stationary the whole time):

# cat /var/log/messages | grep "Nov  5 11" | grep NetworkManager | grep \(none\)
Nov  5 11:01:22 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID 04:C5:A4:C3:F9:EE (Free_Airport_Internet) to (none) ((none))
Nov  5 11:01:22 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to B8:BE:BF:69:89:6E (Free_Airport_Internet)
Nov  5 11:13:23 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID B8:BE:BF:69:89:6E (Free_Airport_Internet) to (none) ((none))
Nov  5 11:13:23 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to 04:C5:A4:C3:F9:EE (Free_Airport_Internet)
Nov  5 11:15:23 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID 04:C5:A4:C3:F9:EE (Free_Airport_Internet) to (none) ((none))
Nov  5 11:15:23 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to B8:BE:BF:69:89:6E (Free_Airport_Internet)
Nov  5 11:19:22 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID B8:BE:BF:69:89:6E (Free_Airport_Internet) to (none) ((none))
Nov  5 11:19:23 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to B8:BE:BF:69:89:6E (Free_Airport_Internet)
Nov  5 11:49:50 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID B8:BE:BF:69:89:6E (Free_Airport_Internet) to (none) ((none))
Nov  5 11:49:50 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to 68:BC:0C:A1:3C:DE (Free_Airport_Internet)
Nov  5 11:51:51 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID 68:BC:0C:A1:3C:DE (Free_Airport_Internet) to (none) ((none))
Nov  5 11:51:51 clevow230ss NetworkManager[2459]:   (wlp3s0): roamed from BSSID (none) ((none)) to B8:BE:BF:69:89:6E (Free_Airport_Internet)

Today I’m using a hotel network in my hotel room, and that does not roam to BSSID “(none)”, but I don’t know if my room is within range of more than one Access Point:

# cat /var/log/messages | grep "Nov  6" | grep NetworkManager | grep \(none\)
#

Anyway, with the work-around described in this post I have not had any further trouble accessing the particular network, but it would be interesting to know the root cause.

Using a keyboard shortcut in Linux to add an e-mail signature giving current location and local time

In my previous post I showed how to find the current time at any town or city Worldwide from the command line in Gentoo Linux. My interest in a command to do this is not to use it on the command line per se, but to use the command in a keyboard shortcut to insert a signature at the end of my e-mails.

I have to travel internationally frequently because of my work, but I leave my laptop’s hardware clock set to UTC and the system clock set to the local time of my home town. This means that, irrespective of where I am in the World, the e-mail client (Thunderbird, in my case) uses the local time of my home town in e-mail headers and calenders. It is not practical to reconfigure Linux for each timezone I happen to be in (see my post Configuring the Linux clock), and, in any case, I want the file system’s timestamps to use one timezone only and all the timestamps in my e-mails and the e-mail client’s calender to use one timezone only, so there is less chance of me getting confused. I could have configured the installation to use UTC for the system clock, but I prefer the system clock to use the timezone of my home town. Of course, even though the system clock is always set to the timezone of my home town, on the Panel clock I select the timezone of the location where I happen to be, so that the Panel clock displays the local time in that timezone.

I wanted to be able to insert a signature at the end of each e-mail, stating my current location and the current time at that location, so that the person receiving the e-mail could tell from where in the World I sent the e-mail and the local time it was sent, as that local time could differ from the time shown in the e-mail header. For example, let us assume that Jane, who lives in the UK and whose system clock is configured for the timezone Europe/London, is making a brief visit to Perth, Australia and sends an e-mail to Dave in the UK at 06:36 on 11 October (Perth time). The e-mail below illustrates the type of signature I wanted to achieve.

Subject: Site visit
From: Jane <jane@acompany.com>
To: Dave <dave@acompany.com>
Date: Sat Oct 10 2015 23:36:40 GMT+0100 (BST)

Hello Dave,

This is to let you know that I have just arrived in Perth and will be
visiting site at 09:00 local time to speak to the client. Tomorrow p.m.
I have a meeting scheduled with our local project manager, so I would
appreciate it if you would e-mail the latest documentation to me. I will
not have spare time until I’m in my room at the hotel tonight but will
read the documents tomorrow a.m. in readiness for the meeting with
the local project manager. Thanks in advance.

Regards,
Jane
Current location: Perth (Australia)
Local time now: Sat Oct 11 06:36:31 2015 AWST

As you can see above, because the OS on Jane’s and Dave’s laptops is configured for the timezone Europe/London, the e-mail header shows the current time in the UK when the e-mail was sent, which was October 10, 23:36 British Summer Time (22:36 UTC), and the signature shows the corresponding local time in Perth, Australia, which was October 11, 06:36 Australian Western Standard Time. It becomes even more confusing if the computer of the person receiving the e-mail is configured for a third timezone. For example, let’s say Dave is based in Seattle, USA rather than the UK. His e-mail client would then display the time in that timezone when the e-mail was sent. This is usually my case, i.e. my Linux installation is configured for Timezone1 but I happen to be in Timezone2 when I send an e-mail to someone who is based in Timezone3 and whose OS is configured for that timezone.

I wanted to use a keyboard shortcut to add a signature to the end of my e-mails, as shown above. I therefore created the Bash script listed below, which I named timezone_signature_GeoNames.sh:

#!/bin/bash

location=$(kdialog --title "Current Location" --inputbox "Enter your location:")

localtime=$(perl /home/fitzcarraldo/now1.pl $location)
place=`echo $localtime | cut -d'|' -f1`
place=$place" "`echo $localtime | cut -d'|' -f2`
timezone=`echo $localtime | cut -d'|' -f4`

if [ $location != "" ]; then
  echo -n "Current location: "
  echo $place
  echo -n "Local time now:"
  /usr/sbin/zdump ${timezone} | cut -d' ' -f2-
fi
echo

Notice that the Bash script uses the GUI dialogues utility kdialog to display a pop-up window prompting me to enter the name of a town/city. As I am using KDE I opted to use a dialogues utility developed for use in KDE, but I could have used Zenity instead.

The Perl script now1.pl is a variant of the Perl script now.pl described in my previous post, modified very slightly in order to facilitate formatting of the output by the Bash script, and is listed below.

#!perl

use strict;
use warnings;

use DateTime;
use Geo::GeoNames;
use URI::Escape;
use Encode;

binmode STDOUT, ':encoding(UTF-8)';

my $city = decode("UTF-8", @ARGV ? shift : 'London');
# N.B. Replace London with your home town/city.

my $geo = Geo::GeoNames->new( username => '************' );
# N.B. Replace the asterisks with your GeoNames user name.

my $result = $geo->search(
q       => uri_escape_utf8($city),
maxRows => 1,
style   => 'FULL'
);

defined $result->[0] or die "Unrecognized city '$city'\n";

my $city_name    = $result->[0]->{name};
my $country_name = $result->[0]->{countryName};
my $time_zone    = $result->[0]->{timezone}{content};
my $time_now     = DateTime->now( time_zone => $time_zone );

#print "$city_name ($country_name) $time_now ($time_zone)\n";

print "$city_name|($country_name)|$time_now|$time_zone\n";

exit 0;

The only thing remaining was to configure a keyboard shortcut to launch the Bash script. I opted to use the key combination Ctrl-Alt-z for the shortcut. As I am using KDE I could have used KDE’s ‘System Settings’ > ‘Shortcuts and Gestures’> ‘Custom Shortcuts’ to specify the shortcut and the name of the script it launches. However, as I also use AutoKey for various shortcuts, I opted to use that instead, so I used the AutoKey GUI to create a shortcut named ‘Insert Current Time’ to use the following command:

output = system.exec_command("/home/fitzcarraldo/timezone_signature_GeoNames.sh")
keyboard.send_keys(output)

Use

I compose my e-mails as usual, and, after entering my name at the end of the e-mail, I press Ctrl-Alt-z. A window then pops up prompting me to enter my current location, which I do and then click on ‘OK’. The location and current time at that location are then added to the end of the e-mail, and it just remains for me to click on the ‘Send’ button in the e-mail client’s window. As the Perl script now1.pl uses the Internet to access the GeoNames database, my laptop must be connected to the Internet when I use the shortcut.

KDialog window that pops up when I use the keyboard shortcut

KDialog window that pops up when I use the keyboard shortcut

If the town/city name consists of more than one word (Rio de Janeiro, for example) then replace spaces with hyphens when you enter the location name in the pop-up window (Rio-de-Janeiro, for example) and then the keyboard shortcut will return the correct location and local time:

Current location: Rio de Janeiro (Brazil)
Local time now: Fri Oct 16 09:28:19 2015 BRT

Installing the DraftSight Free 2D CAD application in Gentoo Linux

I occasionally have to view AutoCAD files and had been using a 2009 version of VariCAD Viewer for Linux, installed from an RPM package using the rpm command in Gentoo Linux. It was the only version that I could get to work correctly in Gentoo Linux. Versions from more recent years would either not install at all or would install but not run correctly. And the 2009 version of VariCAD Viewer could not open more-recent AutoCAD files. So I had been looking for an alternative for quite a while.

Recently I found out about DraftSight, which is produced by Dassault Systèmes SolidWorks Corp. and is available for Windows, Mac OS and Linux. Apart from the commercial versions DraftSight Professional and DraftSight Enterprise, there is also a free version, DraftSight Free, which is billed as ‘straightforward 2D CAD software for students, hobbyists and other individuals’. I thought this would suit my purposes, as it can read and write DWG and DXF files (see the features page on the DraftSight Web site).

The Gentoo Linux betagarden overlay has ebuilds for releases of DraftSight Free. The package is a proprietary binary package, the Gentoo ebuild is named media-gfx/draftsight-bin and the current version in the betagarden overlay is 1.6.1_beta.

So I added the betagarden overlay using Layman, downloaded the file draftSight.deb (DraftSight 2015 beta) from the DraftSight Web site and copied it to /usr/portage/distfiles/draftsight-1.6.1_beta.deb as specified in the ebuild, and issued the usual emerge command to merge the package. However the ebuild would not install the package whatever I tried: Portage gave an error message that the .deb file could not be downloaded (despite it already being in the distfiles directory). In the end I copied the ebuild and its files sub-directory to /usr/local/portage/media-gfx/draftsight-bin/ in my local overlay, disconnected from the network and merged the package:

# layman -a betagarden
# rm /usr/portage/distfiles/draftsight*
# cp /home/fitzcarraldo/Downloads/draftSight.deb /usr/portage/distfiles/draftsight-1.6.1_beta.deb
# mkdir -p /usr/local/portage/media-gfx/draftsight-bin/files
# cp /var/lib/layman/betagarden/media-gfx/draftsight-bin/draftsight-bin-1.6.1_beta.ebuild /usr/local/portage/media-gfx/draftsight-bin/
# cp /var/lib/layman/betagarden/media-gfx/draftsight-bin/files/* /usr/local/portage/media-gfx/draftsight-bin/files/
# layman -d betagarden
# cd /usr/local/portage/media-gfx/draftsight-bin/
# ebuild draftsight-bin-1.6.1_beta.ebuild manifest
# emerge --ask draftsight-bin

This worked, and I can now launch DraftSight from the KDE launcher’s menu (the only additional thing I did was to specify an icon myself using the KDE Menu Editor, as the DraftSight entry in the KDE launcher menu was icon-less) or by issuing the command ‘draftsight‘ from the command line.

DraftSight is a big advance on using an old version of VariCAD Viewer, and I have finally found a decent 2D CAD application to use in Linux.

Update (March 13, 2016): A new version has been released; see my latest post Installing DraftSight 2016 Pre-Release in Gentoo Linux.

The problem of scanning using USB multi-function printers in Linux (success at last)

After my investigations described in a couple of earlier posts ([1], [2]), I finally got Gentoo Linux to scan reliably via the USB connection to my Canon MP560 MFP (multi-function peripheral), a single USB device with three interfaces: scanner (Interface 0); printer (Interface 1); mass storage (Interface 2). Well, ninety-nine per cent reliably, if that isn’t a contradiction in terms. The formula for success? I had to do all the following:

  1. Stop the kernel from binding the usb-storage driver to the device.

  2. Create a UDEV rule to modify the ACL (access control list) of the device to give both the ‘lp‘ and ‘scanner‘ groups Read-Write access to the device.

  3. Create a UDEV rule to: a) change the owner of the device from ‘root‘ to my user account (fitzcarraldo); b) change the device’s group to ‘scanner‘ instead of ‘lp‘; c) give all users Read-Write access to the device (see my note about this at the end of this post).

  4. Disable the SANE backend from accessing the MP560 via the network.

Let’s look in more detail at each of these …

Stop the kernel from binding the usb-storage driver to the device

Originally I had built the usb-storage driver into the kernel (CONFIG_USB_STORAGE=y). During the course of my investigations into this problem I rebuilt the kernel with CONFIG_USB_STORAGE=m, i.e. I rebuilt the driver as an external module, although it should not matter either way.

The vendor ID (04a9) and product ID (173e) of the MP560 can be found using the lsusb command:

# lsusb | grep -i canon
Bus 001 Device 007: ID 04a9:173e Canon, Inc. MP560

If the usb-storage driver is built as an external module, the kernel configuration file will include the following:

# grep CONFIG_USB_STORAGE= /usr/src/linux/.config
CONFIG_USB_STORAGE=m

In this case, it is possible to stop the driver binding to the USB storage interface in the MP560 by creating a ‘quirk’ in the file /etc/modprobe.d/usb-storage.conf as shown below:

# cat /etc/modprobe.d/usb-storage.conf
options usb-storage quirks=04a9:173e:i

If the usb-storage driver is built into the kernel, the kernel configuration file will include the following:

# grep CONFIG_USB_STORAGE= /usr/src/linux/.config
CONFIG_USB_STORAGE=y

In this case, it is possible to stop the driver binding to the USB storage interface in the MP560 by adding a ‘quirk’ (usb-storage.quirks=<idVendor>:<idProduct>:i) to the kernel boot line as shown below (ignore the rest of the parameters shown below, as yours are likely to be different):

# grep usb-storage /boot/grub/grub.cfg
linux /vmlinuz-3.18.11-gentoo root=/dev/sda5 ro drm_kms_helper.edid_firmware=edid/1920x1080_Clevo_W230SS.bin i915.modeset=1 rcutree.rcu_idle_gp_delay=1 usb-storage.quirks=04a9:173e:i

If you edit the file /boot/grub/grub.cfg directly, the change will be lost if you rebuild or upgrade in future. The ‘proper’ way to add the quirk would be to add it to the list of kernel boot parameters in GRUB_CMDLINE_LINUX_DEFAULT in the file /etc/default/grub and regenerate grub.cfg using the command ‘grub2-mkconfig -o /boot/grub/grub.cfg‘.

Create a UDEV rules file to modify the device’s ACL

I created a UDEV rules file /etc/udev/rules.d/99-canon-mp560.rules containing the following:

# ACL settings for Canon PIXMA MP560 printer MFP
ATTR{idVendor}=="04a9", ATTR{idProduct}=="173e", GOTO="canon"
GOTO="canon_end"
 
LABEL="canon"
RUN+="/bin/setfacl -m g:scanner:rw -m g:lp:rw $env{DEVNAME}"
TEST=="/var/run/ConsoleKit/database", \
        RUN+="udev-acl --action=$env{ACTION} --device=$env{DEVNAME}"
 
LABEL="canon_end"

Create a UDEV rules file to change the owner, group and permissions of the device

The file 41-libsane.rules in my installation is the only UDEV rules file in the directory /lib/udev/rules.d/ that explicitly mentions the MP560:

# grep 173e /lib/udev/rules.d/*
/lib/udev/rules.d/41-libsane.rules:ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="173e", MODE="0664", GROUP="scanner", ENV{libsane_matched}="yes"

As the USB device was always in the ‘lp‘ group (see the output of the ‘ls -la‘ command [1]), the above UDEV rule was clearly being overridden by another rule, but which one in the long list of rules files created automatically when I installed the OS and various packages (see below)?

# ls -F1 /lib/udev/rules.d/
10-ft-rockey.rules
10-virtualbox.rules
40-gentoo.rules
40-usb-media-players.rules
41-libsane.rules
42-usb-hid-pm.rules
50-udev-default.rules
56-hpmud.rules
60-block.rules
60-cdrom_id.rules
60-drm.rules
60-evdev.rules
60-persistent-alsa.rules
60-persistent-input.rules
60-persistent-storage-tape.rules
60-persistent-storage.rules
60-persistent-v4l.rules
60-serial.rules
61-accelerometer.rules
64-btrfs.rules
69-cd-sensors.rules
70-libgphoto2.rules
70-mouse.rules
70-printers.rules
70-touchpad.rules
70-udev-acl.rules
75-net-description.rules
75-probe_mtd.rules
77-mm-cinterion-port-types.rules
77-mm-ericsson-mbm.rules
77-mm-huawei-net-port-types.rules
77-mm-longcheer-port-types.rules
77-mm-mtk-port-types.rules
77-mm-nokia-port-types.rules
77-mm-pcmcia-device-blacklist.rules
77-mm-platform-serial-whitelist.rules
77-mm-simtech-port-types.rules
77-mm-telit-port-types.rules
77-mm-usb-device-blacklist.rules
77-mm-usb-serial-adapters-greylist.rules
77-mm-x22x-port-types.rules
77-mm-zte-port-types.rules
77-nm-olpc-mesh.rules
78-sound-card.rules
80-drivers.rules
80-mm-candidate.rules
80-net-name-slot.rules
80-udisks2.rules
85-regulatory.rules
90-alsa-restore.rules
90-libgpod.rules
90-network.rules
90-pulseaudio.rules
95-cd-devices.rules
95-upower-battery-recall-dell.rules
95-upower-battery-recall-fujitsu.rules
95-upower-battery-recall-gateway.rules
95-upower-battery-recall-ibm.rules
95-upower-battery-recall-lenovo.rules
95-upower-battery-recall-toshiba.rules
95-upower-csr.rules
95-upower-hid.rules
95-upower-wup.rules
97-hid2hci.rules
99-fuse.rules
99-ntfs3g.rules
99-nvidia.rules

Anyway, I created a UDEV rules file /etc/udev/rules.d/95-libsane.rules to change the owner of the MP560 USB device from ‘root‘ to me (i.e. user name ‘fitzcarraldo‘), to put the device in the ‘scanner‘ group instead of the ‘lp‘ group, and to give all users Read-Write permission for the device:

ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="173e", MODE="0666", OWNER="fitzcarraldo", GROUP="scanner", ENV{libsane_matched}="yes"

So I now have two user-created UDEV rules files:

# ls /etc/udev/rules.d/
95-libsane.rules 99-canon-mp560.rules

Note that user-created UDEV rules files should be put in the directory /etc/udev/rules.d/, not the directory /lib/udev/rules.d/ which is reserved for UDEV rules installed when you install the OS and by packages you install later. Unfortunately, there is no consistency in UDEV rules file names and their contents between Linux distributions, which makes debugging UDEV rules even more difficult. Higher numbered rules files override lower-numbered rules files.

I then switched off the MP560 and switched it on again. The two UDEV rules files I created had worked as intended. The ownership and group of the device had changed, and so had the permissions and ACL:

# ls -la /dev/bus/usb/001
total 0
drwxr-xr-x  2 root         root       200 Jul 29 10:10 .
drwxr-xr-x  4 root         root        80 Jul 29 09:56 ..
crw-rw-r--  1 root         usb     189, 0 Jul 29 11:25 001
crw-rw-r--  1 root         usb     189, 1 Jul 29 09:56 002
crw-rw-r--  1 root         usb     189, 2 Jul 29 11:25 003
crw-rw-r--  1 root         usb     189, 3 Jul 29 09:56 004
crw-rw-r--  1 root         usb     189, 4 Jul 29 11:25 005
crw-rw-r--  1 root         usb     189, 5 Jul 29 11:25 006
crw-rw-r--  1 root         usb     189, 7 Jul 29 11:25 008
crw-rw-rw-+ 1 fitzcarraldo scanner 189, 8 Jul 29 11:27 009

# getfacl /dev/bus/usb/001/009
getfacl: Removing leading '/' from absolute path names
# file: dev/bus/usb/001/009
# owner: fitzcarraldo
# group: scanner
user::rw-
user:fitzcarraldo:rw-
group::rw-
group:lp:rw-
group:scanner:rw-
mask::rw-
other::rw-

Disable the SANE backend from accessing the MP560 via the network

Although scanning via the USB connection sometimes still works if I have also specified a network connection in the file /etc/sane.d/pixma.conf, apparently the two interfaces should not normally be enabled simultaneously. See the post [sane-devel] Pixma 530 series – tested and not working by SANE developer Rolf Bensch:

Please disconnect the scanner from network and try again. The backend
has problems if both network and USB are connected at once.

Therefore, to try using SANE to access the MP560 via USB, I disabled network access to the MP560 by commenting out the URI in the relevant SANE backend configuration file, which in the case of the MP560 is the file /etc/sane.d/pixma.conf. I commented out the line ‘bjnp://192.168.1.78‘ which I had previously inserted to enable me to scan via my home network because the USB connection was unreliable.

Trying to scan

I then checked if the USB-connected scanner is detected by SANE when running as root user and when running under my user account:

# scanimage -L
device `pixma:04A9173E_1653C4' is a CANON Canon PIXMA MP560 multi-function peripheral
# sane-find-scanner -q
found USB scanner (vendor=0x04a9 [Canon], product=0x173e [MP560 series]) at libusb:001:010
# exit
exit
$ scanimage -L
device `pixma:04A9173E_1653C4' is a CANON Canon PIXMA MP560 multi-function peripheral
$ sane-find-scanner -q
found USB scanner (vendor=0x04a9 [Canon], product=0x173e [MP560 series]) at libusb:001:010

The test using the scanimage command worked:

$ scanimage -d pixma:04A9173E_1653C4 -T
scanimage: scanning image of size 638x877 pixels at 24 bits/pixel
scanimage: acquiring RGB frame, 8 bits/sample
scanimage: reading one scanline, 1914 bytes...  PASS
scanimage: reading one byte...          PASS
scanimage: stepped read, 2 bytes...     PASS
scanimage: stepped read, 4 bytes...     PASS
scanimage: stepped read, 8 bytes...     PASS
scanimage: stepped read, 16 bytes...    PASS
scanimage: stepped read, 32 bytes...    PASS
scanimage: stepped read, 64 bytes...    PASS
scanimage: stepped read, 128 bytes...   PASS
scanimage: stepped read, 256 bytes...   PASS
scanimage: stepped read, 512 bytes...   PASS
scanimage: stepped read, 1024 bytes...  PASS
scanimage: stepped read, 2048 bytes...  PASS
scanimage: stepped read, 2047 bytes...  PASS
scanimage: stepped read, 1023 bytes...  PASS
scanimage: stepped read, 511 bytes...   PASS
scanimage: stepped read, 255 bytes...   PASS
scanimage: stepped read, 127 bytes...   PASS
scanimage: stepped read, 63 bytes...    PASS
scanimage: stepped read, 31 bytes...    PASS
scanimage: stepped read, 15 bytes...    PASS
scanimage: stepped read, 7 bytes...     PASS
scanimage: stepped read, 3 bytes...     PASS

So I tried to scan an A4 page from the command line:

$ scanimage -d pixma:04A9173E_1653C4 -l 0 -t 0 -x 215 -y 297 --resolution 150 --mode Color | convert - scanned-page.png

That worked, so I launched XSane, one of the GUI scanner applications:

$ xsane

That also worked.

Sometimes either scanimage or a GUI scanner application (XSane, gscan2pdf, Simple Scan or whatever) hangs, but, more often than not, scanning now works via the USB connection. However, if either scanimage or a GUI scanner application hangs, I kill them, run them again and then they usually work.

Importantly, printing still works, even though I have changed the USB device group from ‘lp‘ to ‘scanner‘.

So I have satisfied my curiosity and managed to get the MP560 to scan when connected to my laptop via USB, even though it already worked fine when using a network connection instead.

A note on the version of SANE

For information, I currently have Version 1.0.25_pre20150725 (daily snapshot from 25 July 2015) of the package sane-backends installed:

$ eix -I sane-backends
[I] media-gfx/sane-backends
     Available versions:  1.0.24-r5 (~)1.0.25_pre20150628 (~)1.0.25_pre20150725[1] {avahi doc gphoto2 ipv6 snmp systemd threads usb v4l xinetd ABI_MIPS="n32 n64 o32" ABI_PPC="32 64" ABI_S390="32 64" ABI_X86="32 64 x32" SANE_BACKENDS="+abaton +agfafocus +apple +artec +artec_eplus48u +as6e +avision +bh +canon +canon630u +canon_dr -canon_pp +cardscan +coolscan +coolscan2 +coolscan3 +dc210 +dc240 +dc25 +dell1600n_net +dmc +epjitsu +epson +epson2 +fujitsu +genesys +gt68xx +hp +hp3500 +hp3900 +hp4200 +hp5400 +hp5590 +hpljm1005 -hpsj5s +hs2p +ibm +kodak +kodakaio +kvs1025 +kvs20xx kvs40xx +leo +lexmark +ma1509 +magicolor +matsushita +microtek +microtek2 +mustek -mustek_pp +mustek_usb mustek_usb2 +nec +net +niash +p5 +pie +pixma +plustek +plustek_pp -pnm +qcam +ricoh +rts8891 +s9036 +sceptre +sharp +sm3600 +sm3840 +snapscan +sp15c +st400 +stv680 +tamarack +teco1 +teco2 +teco3 +test +u12 +umax +umax1220u +umax_pp +xerox_mfp"}
     Installed versions:  1.0.25_pre20150725[1](01:39:24 25/07/15)(avahi gphoto2 ipv6 usb v4l -doc -snmp -systemd -threads -xinetd ABI_MIPS="-n32 -n64 -o32" ABI_PPC="-32 -64" ABI_S390="-32 -64" ABI_X86="32 64 -x32" SANE_BACKENDS="abaton agfafocus apple artec artec_eplus48u as6e avision bh canon canon630u canon_dr cardscan coolscan coolscan2 coolscan3 dc210 dc240 dc25 dell1600n_net dmc epjitsu epson epson2 fujitsu genesys gt68xx hp hp3500 hp3900 hp4200 hp5400 hp5590 hpljm1005 hs2p ibm kodak kodakaio kvs1025 kvs20xx leo lexmark ma1509 magicolor matsushita microtek microtek2 mustek mustek_usb nec net niash p5 pie pixma plustek plustek_pp qcam ricoh rts8891 s9036 sceptre sharp sm3600 sm3840 snapscan sp15c st400 stv680 tamarack teco1 teco2 teco3 test u12 umax umax1220u umax_pp xerox_mfp -canon_pp -hpsj5s -kvs40xx -mustek_pp -mustek_usb2 -pnm")
     Homepage:            http://www.sane-project.org/
     Description:         Scanner Access Now Easy - Backends

[1] "local_overlay" /usr/local/portage

However, the same symptoms occurred with versions 1.0.24-r5, 1.0.25_pre20150628 and 1.0.25_pre20150725 of the sane-backends package, so I believe the problem was not version-specific.

A note about scanning in Linux
An early developer’s Web site for the SANE pixma backend has the following warning:

If you grant a group a full permission to access the scanner (by using udev, hotplug, resmgr etc.), every members in the group will have full access rights not only for the scanner part but also for the other parts of the device i.e. printer, memory-card reader and fax. These users can bypass your system’s security policy and do everything with the device intentionally and unintentionally.

It is all very well to say that, but it is the only way I could get the backend to work reliably (almost). And, judging by the number of people who posted on the Web that they had to do the same with other Linux distributions, other manufacturers and models of MFPs, this is a common requirement in order to get Linux to work with MFP scanners. It’s not impressive when people have to resort to this to get something to work.

Note that the success of the approach described in this post is dependent on the contents and order of all the UDEV rules files. Just because the rules files I have posted above work in my case does not guarantee they will work in your case too. It depends what rules files your installation already has, what are their precise contents and in what order they are. In my opinion this is not a good concept.

I stand by my comments in the Conclusion of my first post regarding this problem. To have to jump through so many hoops to be able to scan using Linux on a computer connected to a MFP via USB is ridiculous in my opinion. The fact that it works reliably via a network connection does not alter that. I have been ‘scratching an itch’ as I enjoy tinkering, but I feel sorry for users who just need something that works without having to mess around for hours or days.

Background

  1. The problem of scanning using USB multi-function printers in Linux
  2. The problem of scanning using USB multi-function printers in Linux (continued)

The problem of scanning using USB multi-function printers in Linux (continued)

My previous post discussed the inability of the Linux installation on my main laptop to use the scanner of my Canon PIXMA MP560 MFP (multi-function peripheral) when they are connected via USB. Linux sees the MP560 as a single USB device with multiple interfaces: Interface 0 (scanner), Interface 1 (printer) and Interface 2 (USB mass storage). The basic problem seems to be that the usb-storage driver — whether built into the kernel or as an external module — claims the USB device and blocks access by userspace’s libusb, which is what the SANE backend (‘pixma‘, in the case of the MP560) uses to access the USB device. However, scanning works fine when using a network connection instead of the USB connection.

Attempts such as unbinding the usb-storage driver from the USB device — either manually or using a UDEV rule — in order to try and allow libusb/SANE to access the scanner, did not solve the problem. Although I can scan by connecting to the MP560 via my home network, the inability of the OS to scan via the USB interface piqued my interest, so I continued my investigations and below I discuss my latest findings.

Disabling kernel auto-binding

I learned from the interesting blog post Controlling USB device access on Linux how to stop the kernel automatically binding a driver to a USB device in the first place, as distinct from unbinding a driver after the kernel has bound it to a device. I thought this approach could be used to stop the usb-storage driver from binding to the MP560 and thus allow libusb/SANE to access Interface 0. The method using sysfs to stop the kernel binding the driver is to disable kernel USB auto-probing before launching a scanner application, by using the following command:

# echo 0 > /sys/bus/usb/drivers_autoprobe

This did indeed stop the kernel from automatically binding USB drivers to USB devices, but unfortunately libusb/SANE would still not access the MP560 scanner via USB. When drivers_autoprobe contains zero, applications such as XSane can use the network-connected MP560 as before, but still hang if the USB-connected MP560 is selected.

Unloading the usb-storage module

According to various blog and forum posts, some people have been able to use MFP scanners via USB after manually unloading the usb-storage module. I decided to try this. As I had built the kernel with the usb-storage driver internally rather than an external module, I first had to rebuild the kernel with CONFIG_USB_STORAGE=m instead of CONFIG_USB_STORAGE=y. Then I unloaded manually the usb_storage module using the command ‘rmmod usb_storage‘ and launched a scanner application, but the application still hung.

Disabling the usb-storage driver using a ‘quirk’

Then I had a thought: Perhaps there is a kernel ‘quirk’ to disable the usb-storage driver from binding to a specific USB device. Indeed there is (see Kernel Parameters or /usr/src/linux-<version>/Documentation/kernel-parameters.txt).

If the usb-storage driver is built into the kernel (CONFIG_USB_STORAGE=y), I would need to add the following kernel boot parameter to the kernel boot line:

usb-storage.quirks=04a9:173e:i

’04a9′ is the Vendor ID and ‘173e’ is the Product ID of the MP560. The ‘i‘ stands for ‘ignore’.

If the usb-storage driver is built as a kernel module (CONFIG_USB_STORAGE=m), I would need to create a file with a name such as /etc/modprobe.d/usb-storage.conf containing the following line:

options usb-storage quirks=04a9:173e:i

So I set up the quirk. Upon rebooting my laptop, the dmesg command reported the following:

usb 1-1.2.2: new high-speed USB device number 8 using ehci-pci
usb 1-1.2.2: New USB device found, idVendor=04a9, idProduct=173e
usb 1-1.2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.2.2: Product: MP560 series
usb 1-1.2.2: Manufacturer: Canon
usb 1-1.2.2: SerialNumber: 1653C4
usb-storage 1-1.2.2:1.2: USB Mass Storage device detected
usb-storage 1-1.2.2:1.2: device ignored

Notice the final line. The quirk does indeed cause the usb-storage driver to ignore the MP560.

The lsusb command still shows the device, which is a good sign:

# lsusb
Bus 002 Device 005: ID 0bc2:3300 Seagate RSS LLC
Bus 002 Device 004: ID 0411:01d9 BUFFALO INC. (formerly MelCo., Inc.)
Bus 002 Device 003: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 002 Device 002: ID 8087:8000 Intel Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 006: ID 5986:055c Acer, Inc
Bus 001 Device 004: ID 8087:07dc Intel Corp.
Bus 001 Device 007: ID 0603:00f2 Novatek Microelectronics Corp. Keyboard (Labtec Ultra Flat Keyboard)
Bus 001 Device 008: ID 04a9:173e Canon, Inc. MP560
Bus 001 Device 005: ID 045e:00d1 Microsoft Corp. Optical Mouse with Tilt Wheel
Bus 001 Device 003: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 002: ID 8087:8008 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

# lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
        |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 4, If 0, Class=Mass Storage, Driver=usb-storage, 480M
            |__ Port 3: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 5, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 2: Dev 8, If 0, Class=Vendor Specific Class, Driver=usbfs, 480M
            |__ Port 2: Dev 8, If 1, Class=Printer, Driver=, 480M
            |__ Port 2: Dev 8, If 2, Class=Mass Storage, Driver=, 480M
            |__ Port 4: Dev 7, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 4: Dev 7, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 3: Dev 4, If 0, Class=Wireless, Driver=btusb, 12M
        |__ Port 3: Dev 4, If 1, Class=Wireless, Driver=btusb, 12M
        |__ Port 4: Dev 6, If 0, Class=Video, Driver=uvcvideo, 480M
        |__ Port 4: Dev 6, If 1, Class=Video, Driver=uvcvideo, 480M

The output of the lsusb -v -d 04a9:173e command applicable to the MP560 is as follows:

Bus 001 Device 008: ID 04a9:173e Canon, Inc. MP560
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x04a9 Canon, Inc.
  idProduct          0x173e MP560
  bcdDevice            0.04
  iManufacturer           1 Canon
  iProduct                2 MP560 series
  iSerial                 3 1653C4
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           85
    bNumInterfaces          3
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xc0
      Self Powered
    MaxPower                2mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      0 
      bInterfaceProtocol    255 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x07  EP 7 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x88  EP 8 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x89  EP 9 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              11
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         7 Printer
      bInterfaceSubClass      1 Printer
      bInterfaceProtocol      2 Bidirectional
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        2
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         8 Mass Storage
      bInterfaceSubClass      6 SCSI
      bInterfaceProtocol     80 Bulk-Only
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x84  EP 4 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x05  EP 5 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
Device Qualifier (for other device speed):
  bLength                10
  bDescriptorType         6
  bcdUSB               2.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  bNumConfigurations      1
can't get debug descriptor: Resource temporarily unavailable
Device Status:     0x0001
  Self Powered

The USB device created at boot was as follows:

# ls -la /dev/bus/usb/001/008
crw-rw-r--+ 1 root lp 189, 7 Jul 24 11:01 /dev/bus/usb/001/008

Whether run by the root user or under my user account, the SANE tools detected the MP560 scanner network and USB connections:

$ scanimage -L
device `pixma:MP560_192.168.1.78' is a CANON Canon PIXMA MP560 multi-function peripheral
device `pixma:04A9173E' is a CANON Canon PIXMA MP560 multi-function peripheral

$ sane-find-scanner

  # sane-find-scanner will now attempt to detect your scanner. If the
  # result is different from what you expected, first make sure your
  # scanner is powered up and properly connected to your computer.

  # No SCSI scanners found. If you expected something different, make sure that
  # you have loaded a kernel SCSI driver for your SCSI adapter.

found USB scanner (vendor=0x04a9 [Canon], product=0x173e [MP560 series]) at libusb:001:008
  # Your USB scanner was (probably) detected. It may or may not be supported by
  # SANE. Try scanimage -L and read the backend's manpage.

  # Not checking for parallel port scanners.

  # Most Scanners connected to the parallel port or other proprietary ports
  # can't be detected by this program.

But scanner applications still hung! This time the message displayed by the dmesg command was:

usb 1-1.2.2: usbfs: interface 0 claimed by usbfs while 'scanimage' sets config #1

I unplugged the USB cable and plugged it in again. The output of the dmesg command was:

usb 1-1.2.2: USB disconnect, device number 8
usb 1-1.2.2: new high-speed USB device number 9 using ehci-pci
usb 1-1.2.2: New USB device found, idVendor=04a9, idProduct=173e
usb 1-1.2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.2.2: Product: MP560 series
usb 1-1.2.2: Manufacturer: Canon
usb 1-1.2.2: SerialNumber: 1653C4
usb-storage 1-1.2.2:1.2: USB Mass Storage device detected
usb-storage 1-1.2.2:1.2: device ignored

I then launched XSane several times and each time I was able to select either the MP560 via my home network (pixma:MP560_192.168.1.78) or via the USB cable (pixma:04A9173E_1653C4) and scan successfully.

By the way, it is possible to scan via the command line instead of using a GUI scanner application. For example, tjhe following command will scan an A4 page via the MP560’s USB connection (see man scanimage for details of the options):

$ scanimage -d pixma:04A9173E_1653C4 -l 0 -t 0 -x 215 -y 297 --resolution 150 --mode Color | convert - scanned-page.png

So the problem is solved, right? Wrong! For whatever reason, scanning via the USB interface is not guaranteed to work every time I boot the laptop. Sometimes scanner applications (and the scanimage command) hang if I select the USB interface; at other times they don’t hang and scanning works fine. If scanning using the USB device is not working, if I unplug and re-insert the USB cable a few times it becomes possible to scan using the USB interface. And once scanning via the USB interface does work, from then onwards it works consistently during that session.

I had been using Version 1.0.24-r5 of the sane-backends package in Gentoo Linux, which is the latest stable version according to the Gentoo package manager. To check if that particular version of the SANE pixma backend might be the cause of the problem I installed the latest version available in the Gentoo package manager (currently 1.0.5_pre20150625). However it made no discernable difference.

To sum up, in my case the most successful approach so far has been to use a kernel ‘quirk’ to force the usb-storage driver to ignore the device and not bind to it. However, this approach does not result in libusb/SANE being able to access the scanner during every boot session, but, if access is successful, it remains successful during the current session. Even if access via the USB interface does not work initially, sometimes I can get it to work by unplugging and re-inserting the USB cable.

UPDATE (July 30, 2015): I have now managed to get scanning to work with almost 100% reliability when connected to the MP560 via USB — see my latest post for details.

The problem of scanning using USB multi-function printers in Linux

Whilst searching the Web I have found many other people experiencing this problem, which I believe happens as a consequence of the way Linux is designed. Basically, if you have a multi-function peripheral that you connect to your computer via USB, the chances are that either scanning or printing will not work. The Web is littered with blog posts, forum threads, mailing list posts and bug reports regarding this phenomenon. There are numerous suggested fixes, usually entailing the addition or modification of udev rules. Sometimes the suggested fixes work; often they don’t. Linux can be a major hassle where peripherals are concerned.

I have a Canon Pixma MP560 MFP (multi-function printer/peripheral), which is a printer, scanner and copier. It also has memory card slots and a USB pen drive socket, to be able to store images directly from the scanner without the need to connect a computer. The MP560 provides two alternative means of connection to a computer: USB and Wi-Fi. I normally connect it via USB to my Clevo W230SS laptop running Gentoo Linux (amd64), but I can also connect it to the laptop via my home Wi-Fi network. Either connection method allows me to print, but I can scan only via the wireless connection.

The dmesg command displays the following messages after I plug a USB cable from the MP560 into my laptop:

usb 1-1.2.2: new high-speed USB device number 8 using ehci-pci
usb 1-1.2.2: New USB device found, idVendor=04a9, idProduct=173e
usb 1-1.2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.2.2: Product: MP560 series
usb 1-1.2.2: Manufacturer: Canon
usb 1-1.2.2: SerialNumber: 1653C4
usb-storage 1-1.2.2:1.2: USB Mass Storage device detected
scsi host6: usb-storage 1-1.2.2:1.2
scsi 6:0:0:0: Direct-Access     Canon    MP560 series     0104 PQ: 0 ANSI: 2
sd 6:0:0:0: Attached scsi generic sg1 type 0
sd 6:0:0:0: [sdb] Attached SCSI removable disk

Notice that the device also has some internal storage.

The lsusb command shows the device:

$ lsusb
Bus 002 Device 002: ID 8087:8000 Intel Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 006: ID 5986:055c Acer, Inc
Bus 001 Device 004: ID 8087:07dc Intel Corp.
Bus 001 Device 007: ID 0603:00f2 Novatek Microelectronics Corp. Keyboard (Labtec Ultra Flat Keyboard)
Bus 001 Device 009: ID 04a9:173e Canon, Inc. MP560
Bus 001 Device 005: ID 045e:00d1 Microsoft Corp. Optical Mouse with Tilt Wheel
Bus 001 Device 003: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 002: ID 8087:8008 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

My Linux user account is in the lp and scanner groups:

$ groups
disk lp wheel floppy uucp cron audio cdrom video games cdrw usb users plugdev crontab wireshark vboxusers scanner fitzcarraldo

The device is in the lp group and I can print without any trouble:

$ ls -la /dev/bus/usb/001/009
crw-rw-r--+ 1 root lp 189, 8 Jun 19 02:55 /dev/bus/usb/001/009

The usblp driver, a common cause of problems with USB printers, does not exist because I purposely did not set CONFIG_USB_PRINTER when I built the kernel, specifically to avoid such problems:

$ grep CONFIG_USB_PRINTER /usr/src/linux/.config
# CONFIG_USB_PRINTER is not set

so, obviously, the usblp driver is neither built into the kernel nor compiled as a module:

$ lsmod | grep usb
btusb                  22292  0
bluetooth             281605  27 bnep,hidp,btusb,rfcomm

I have the latest stable versions of the udev-related packages installed (as you can see below, I use eudev instead of udev):

$ eix -I udev
[I] dev-python/pyudev
     Available versions:  0.16.1-r1 {pygobject pyqt4 pyside test PYTHON_TARGETS="python2_7 python3_3 python3_4"}
     Installed versions:  0.16.1-r1(03:32:17 18/04/15)(pyqt4 -pygobject -pyside -test PYTHON_TARGETS="python2_7 python3_3 -python3_4")
     Homepage:            http://pyudev.readthedocs.org https://github.com/pyudev/pyudev
     Description:         Python binding to libudev

[I] sys-fs/eudev
     Available versions:  *1.3 *1.5.3-r1 1.9-r2 1.10-r2 ~2.1.1 ~3.0 3.1.2 ~3.1.2-r10 **9999 {doc gudev (+)hwdb introspection (+)keymap (+)kmod +modutils +openrc +rule-generator selinux static-libs test ABI_MIPS="n32 n64 o32" ABI_PPC="32 64" ABI_S390="32 64" ABI_X86="32 64 x32"}
     Installed versions:  3.1.2(19:44:12 16/07/15)(gudev hwdb introspection kmod -doc -selinux -static-libs -test ABI_MIPS="-n32 -n64 -o32" ABI_PPC="-32 -64" ABI_S390="-32 -64" ABI_X86="32 64 -x32")
     Homepage:            https://github.com/gentoo/eudev
     Description:         Linux dynamic and persistent device naming support (aka userspace devfs)

[I] sys-fs/udev-init-scripts
     Available versions:  27^t [M]~29^t ~30^t **9999^t
     Installed versions:  27^t(05:29:06 09/04/15)
     Homepage:            http://www.gentoo.org
     Description:         udev startup scripts for openrc

[I] virtual/libgudev
     Available versions:  215-r1(0/0) 215-r2(0/0) 215-r3(0/0) ~230(0/0) {introspection static-libs systemd ABI_MIPS="n32 n64 o32" ABI_PPC="32 64" ABI_S390="32 64" ABI_X86="32 64 x32"}
     Installed versions:  215-r3(10:18:16 11/06/15)(introspection -static-libs -systemd ABI_MIPS="-n32 -n64 -o32" ABI_PPC="-32 -64" ABI_S390="-32 -64" ABI_X86="64 -32 -x32")
     Description:         Virtual for libgudev providers

[I] virtual/libudev
     Available versions:  215-r1(0/1) {static-libs systemd ABI_MIPS="n32 n64 o32" ABI_PPC="32 64" ABI_S390="32 64" ABI_X86="32 64 x32"}
     Installed versions:  215-r1(17:51:42 18/04/15)(-static-libs -systemd ABI_MIPS="-n32 -n64 -o32" ABI_PPC="-32 -64" ABI_S390="-32 -64" ABI_X86="32 64 -x32")
     Description:         Virtual for libudev providers

[I] virtual/udev
     Available versions:  215 ~217 {systemd}
     Installed versions:  215(05:28:55 09/04/15)(-systemd)
     Description:         Virtual to select between different udev daemon providers

Found 6 matches.

I have the latest stable SANE-related packages installed:

$ eix -I sane
[I] dev-perl/Sane
     Available versions:  (~)0.50.0-r1 {test}
     Installed versions:  0.50.0-r1(17:39:10 23/06/15)(-test)
     Homepage:            http://search.cpan.org/dist/Sane/
     Description:         The Sane module allows you to access SANE-compatible scanners in a Perl

[I] kde-apps/ksaneplugin
     Available versions:  (4) 4.14.3(4/4.14)
       {aqua debug}
     Installed versions:  4.14.3(4)(14:13:01 17/04/15)(-aqua -debug)
     Homepage:            http://www.kde.org/
     Description:         SANE Plugin for KDE

[I] kde-apps/libksane
     Available versions:  (4) 4.14.3(4/4.14)
       {aqua debug}
     Installed versions:  4.14.3(4)(13:55:44 17/04/15)(-aqua -debug)
     Homepage:            http://www.kde.org/
     Description:         SANE Library interface for KDE

[I] media-gfx/sane-backends
     Available versions:  1.0.24-r5 ~1.0.25_pre20150628 {avahi doc gphoto2 ipv6 snmp systemd threads usb v4l xinetd ABI_MIPS="n32 n64 o32" ABI_PPC="32 64" ABI_S390="32 64" ABI_X86="32 64 x32" SANE_BACKENDS="+abaton +agfafocus +apple +artec +artec_eplus48u +as6e +avision +bh +canon +canon630u +canon_dr -canon_pp +cardscan +coolscan +coolscan2 +coolscan3 +dc210 +dc240 +dc25 +dell1600n_net +dmc +epjitsu +epson +epson2 +fujitsu +genesys +gt68xx +hp +hp3500 +hp3900 +hp4200 +hp5400 +hp5590 +hpljm1005 -hpsj5s +hs2p +ibm +kodak +kodakaio +kvs1025 +kvs20xx kvs40xx +leo +lexmark +ma1509 +magicolor +matsushita +microtek +microtek2 +mustek -mustek_pp +mustek_usb mustek_usb2 +nec +net +niash +p5 +pie +pixma +plustek +plustek_pp -pnm +qcam +ricoh +rts8891 +s9036 +sceptre +sharp +sm3600 +sm3840 +snapscan +sp15c +st400 +stv680 +tamarack +teco1 +teco2 +teco3 +test +u12 +umax +umax1220u +umax_pp +xerox_mfp"}
     Installed versions:  1.0.24-r5(17:55:11 18/04/15)(avahi gphoto2 ipv6 usb v4l -doc -snmp -systemd -threads -xinetd ABI_MIPS="-n32 -n64 -o32" ABI_PPC="-32 -64" ABI_S390="-32 -64" ABI_X86="32 64 -x32" SANE_BACKENDS="abaton agfafocus apple artec artec_eplus48u as6e avision bh canon canon630u canon_dr cardscan coolscan coolscan2 coolscan3 dc210 dc240 dc25 dell1600n_net dmc epjitsu epson epson2 fujitsu genesys gt68xx hp hp3500 hp3900 hp4200 hp5400 hp5590 hpljm1005 hs2p ibm kodak kodakaio kvs1025 kvs20xx leo lexmark ma1509 magicolor matsushita microtek microtek2 mustek mustek_usb nec net niash p5 pie pixma plustek plustek_pp qcam ricoh rts8891 s9036 sceptre sharp sm3600 sm3840 snapscan sp15c st400 stv680 tamarack teco1 teco2 teco3 test u12 umax umax1220u umax_pp xerox_mfp -canon_pp -hpsj5s -kvs40xx -mustek_pp -mustek_usb2 -pnm")
     Homepage:            http://www.sane-project.org/
     Description:         Scanner Access Now Easy - Backends

[I] media-gfx/sane-frontends
     Available versions:  1.0.14 {gimp}
     Installed versions:  1.0.14(19:47:50 18/06/15)(gimp)
     Homepage:            http://www.sane-project.org
     Description:         Scanner Access Now Easy

[I] media-gfx/xsane
     Available versions:  0.999-r1 {gimp jpeg lcms nls ocr png tiff}
     Installed versions:  0.999-r1(12:03:12 19/04/15)(gimp jpeg lcms nls png tiff -ocr)
     Homepage:            http://www.xsane.org/
     Description:         graphical scanning frontend

Found 6 matches.

The following SANE backends were installed when I installed SANE:

$ ls -F1 /etc/sane.d/ | columns -c 3 -w 20
abaton.conf         agfafocus.conf      apple.conf
artec.conf          artec_eplus48u.conf avision.conf
bh.conf             canon.conf          canon630u.conf
canon_dr.conf       cardscan.conf       coolscan.conf
coolscan2.conf      coolscan3.conf      dc210.conf
dc240.conf          dc25.conf           dell1600n_net.conf
dll.conf            dll.d/              dmc.conf
epjitsu.conf        epson.conf          epson2.conf
fujitsu.conf        genesys.conf        gphoto2.conf
gt68xx.conf         hp.conf             hp3900.conf
hp4200.conf         hp5400.conf         hs2p.conf
ibm.conf            kodak.conf          kodakaio.conf
leo.conf            lexmark.conf        ma1509.conf
magicolor.conf      matsushita.conf     microtek.conf
microtek2.conf      mustek.conf         mustek_usb.conf
nec.conf            net.conf            p5.conf
pie.conf            pixma.conf          plustek.conf
plustek_pp.conf     qcam.conf           ricoh.conf
rts8891.conf        s9036.conf          saned.conf
sceptre.conf        sharp.conf          sm3840.conf
smfp.conf           snapscan.conf       sp15c.conf
st400.conf          stv680.conf         tamarack.conf
teco1.conf          teco2.conf          teco3.conf
test.conf           u12.conf            umax.conf
umax1220u.conf      umax_pp.conf        v4l.conf
xerox_mfp.conf

I checked all the SANE backend configuration files, and none of them mention the MP560 (I was not expecting them to mention it, as, according to man sane-pixma, the pixma backend detects Canon PIXMA USB devices automatically):

$ grep -rnwi /etc/sane.d/ -e "173e"
$

The SANE file dll.conf does list the pixma backend configuration file which is only for declaring networked Canon PIXMA devices, as the pixma backend detects Canon USB devices automatically):

$ grep pixma /etc/sane.d/dll.conf
pixma

$ cat /etc/sane.d/pixma.conf
# pixma.conf configuration for the sane pixma backend
#
# define URI's of scanners (one per line)
# This is only used for network scanners.
# normally scanners will be detected by sending a broadcast
# if this does not work under your OS, or if the scanners
# are on a different subnet, configure your scanners URI here
#
# method must be bjnp
# port number can normally be left out, port 8612 is used as default
# Example:
# bjnp://myscanner.my.domain:8612
# bjnp://printer-1.pheasant.org
#

I don’t think it is even used these days, but the file /etc/hotplug/usb/libsane.usermap already includes the device:

# Canon PIXMA MP560
libusbscanner 0x0003 0x04a9 0x173e 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000

The file /lib64/udev/rules.d/41-libsane.rules already includes the device:

# Canon PIXMA MP560
ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="173e", MODE="0664", GROUP="scanner", ENV{libsane_matched}="yes"

However, scanning from Linux via USB is a troubled affair. Very occasionally the scanner application (XSane, Simple Scan, gscan2pdf, or whatever) will launch and the scanner works, but usually the scanner application just hangs and the dmesg command shows one or more error messages such as the following:

usb 1-1.2.2: usbfs: interface 2 claimed by usb-storage while 'xsane' sets config #1

usb 1-1.2.2: usbfs: interface 2 claimed by usb-storage while 'gscan2pdf' sets config #1

I have USB mass storage support enabled in the kernel so that my laptop can access external USB mass storage devices:

$ grep CONFIG_USB_STORAGE= /usr/src/linux/.config
CONFIG_USB_STORAGE=y

As the three interfaces (printer, scanner and internal storage) are all accessed as a single USB device (/dev/bus/usb/001/009, or whatever), there appears to be a conflict between the kernel claiming the MFP’s internal mass storage and SANE trying to access the scanner. I do not want to disable the kernel’s USB mass storage support (not even by rebuilding the kernel with CONFIG_USB_STORAGE=m and temporarily unloading manually the usb-storage module, as some people suggest), as I would not be able to access my external USB mass storage devices.

The scanimage and sane-find-scanner utilities detect the scanner correctly via USB:

$ scanimage -L
device `v4l:/dev/video0' is a Noname BisonCam, NB Pro virtual device
device `pixma:04A9173E_1653C4' is a CANON Canon PIXMA MP560 multi-function peripheral

(The BisonCam is the Webcam built into my laptop.)

$ sane-find-scanner

  # sane-find-scanner will now attempt to detect your scanner. If the
  # result is different from what you expected, first make sure your
  # scanner is powered up and properly connected to your computer.

  # No SCSI scanners found. If you expected something different, make sure that
  # you have loaded a kernel SCSI driver for your SCSI adapter.

found USB scanner (vendor=0x04a9 [Canon], product=0x173e [MP560 series]) at libusb:001:009
  # Your USB scanner was (probably) detected. It may or may not be supported by
  # SANE. Try scanimage -L and read the backend's manpage.

  # Not checking for parallel port scanners.

  # Most Scanners connected to the parallel port or other proprietary ports
  # can't be detected by this program.

  # You may want to run this program as root to find all devices. Once you
  # found the scanner devices, be sure to adjust access permissions as
  # necessary.

The scanimage test commands do not indicate any problem:

$ scanimage -d test -T
scanimage: scanning image of size 157x196 pixels at 8 bits/pixel
scanimage: acquiring gray frame, 8 bits/sample
scanimage: reading one scanline, 157 bytes...   PASS
scanimage: reading one byte...          PASS
scanimage: stepped read, 2 bytes...     PASS
scanimage: stepped read, 4 bytes...     PASS
scanimage: stepped read, 8 bytes...     PASS
scanimage: stepped read, 16 bytes...    PASS
scanimage: stepped read, 32 bytes...    PASS
scanimage: stepped read, 64 bytes...    PASS
scanimage: stepped read, 128 bytes...   PASS
scanimage: stepped read, 256 bytes...   PASS
scanimage: stepped read, 255 bytes...   PASS
scanimage: stepped read, 127 bytes...   PASS
scanimage: stepped read, 63 bytes...    PASS
scanimage: stepped read, 31 bytes...    PASS
scanimage: stepped read, 15 bytes...    PASS
scanimage: stepped read, 7 bytes...     PASS
scanimage: stepped read, 3 bytes...     PASS

$ scanimage -d pixma -T
scanimage: scanning image of size 638x877 pixels at 24 bits/pixel
scanimage: acquiring RGB frame, 8 bits/sample
scanimage: reading one scanline, 1914 bytes...  PASS
scanimage: reading one byte...          PASS
scanimage: stepped read, 2 bytes...     PASS
scanimage: stepped read, 4 bytes...     PASS
scanimage: stepped read, 8 bytes...     PASS
scanimage: stepped read, 16 bytes...    PASS
scanimage: stepped read, 32 bytes...    PASS
scanimage: stepped read, 64 bytes...    PASS
scanimage: stepped read, 128 bytes...   PASS
scanimage: stepped read, 256 bytes...   PASS
scanimage: stepped read, 512 bytes...   PASS
scanimage: stepped read, 1024 bytes...  PASS
scanimage: stepped read, 2048 bytes...  PASS
scanimage: stepped read, 2047 bytes...  PASS
scanimage: stepped read, 1023 bytes...  PASS
scanimage: stepped read, 511 bytes...   PASS
scanimage: stepped read, 255 bytes...   PASS
scanimage: stepped read, 127 bytes...   PASS
scanimage: stepped read, 63 bytes...    PASS
scanimage: stepped read, 31 bytes...    PASS
scanimage: stepped read, 15 bytes...    PASS
scanimage: stepped read, 7 bytes...     PASS
scanimage: stepped read, 3 bytes...     PASS

As mentioned earlier, the udev/eudev rules file appears to contain a valid rule for the scanner:

# Canon PIXMA MP560
ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="173e", MODE="0664", GROUP="scanner", ENV{libsane_matched}="yes"

Notice the rule assigns the device’s group to be ‘scanner‘, not ‘lp‘, although changing the group assignment to ‘lp‘ in the rule makes no discernable difference to the scanning problem in my case. As the device’s group is actually ‘lp‘, I assume another rule overrides this rule, but I have no idea which rule it is and in which rules file it occurs.

Occasionally I am lucky and scanning does work, but, more often than not, scanner applications cannot access the scanner and just hang.

A work-around if the peripheral also has a network interface

I configured my home network router always to use the IP address 192.168.1.78 for the MP560 and I added the following line to the file /etc/sane.d/pixma.conf in order to be able to scan via Wi-Fi instead of USB (I have the package cups-bjnp installed for printing purposes, but I am not sure if that package is also needed by SANE in this case):

bjnp://192.168.1.78

With the above line added to /etc/sane.d/pixma.conf the scanimage utility detects the scanner correctly via both the Wi-Fi connection and the USB interface:

$ scanimage -L
device `v4l:/dev/video0' is a Noname BisonCam, NB Pro virtual device
device `pixma:MP560_192.168.1.78' is a CANON Canon PIXMA MP560 multi-function peripheral
device `pixma:04A9173E_1653C4' is a CANON Canon PIXMA MP560 multi-function peripheral

So, as I usually cannot get scanning to work via USB, I can scan via Wi-Fi instead. It’s a work-around, not a solution, but at least by doing that I can scan reliably as well as print. Of course you can’t use such a work-around if you have a model of MFP that only provides a USB connection.

Failed experiments

Experiment 1

I also tried unbinding the usb-storage driver from the usb device:

# ls /sys/bus/usb/drivers
btusb hub usb usb-storage usbfs usbhid uvcvideo
# ls /sys/bus/usb/drivers/btusb/
1-1.3:1.0 1-1.3:1.1 bind module new_id remove_id uevent unbind
# ls /sys/bus/usb/drivers/hub
1-0:1.0 1-1.2:1.0 1-1:1.0 2-0:1.0 2-1:1.0 bind module new_id remove_id uevent unbind
# ls /sys/bus/usb/drivers/usb
1-1 1-1.2 1-1.2.1 1-1.2.2 1-1.2.4 1-1.3 1-1.4 2-1 bind uevent unbind usb1 usb2
# ls /sys/bus/usb/drivers/usb-storage/
1-1.2.2:1.2 bind module new_id remove_id uevent unbind
# ls /sys/bus/usb/drivers/usbfs
bind module new_id remove_id uevent unbind
# ls /sys/bus/usb/drivers/usbhid
1-1.2.1:1.0 1-1.2.4:1.0 1-1.2.4:1.1 bind module new_id remove_id uevent unbind
# ls /sys/bus/usb/drivers/uvcvideo
1-1.4:1.0 1-1.4:1.1 bind module new_id remove_id uevent unbind
# tree /sys/bus/usb/drivers/usb-storage/
/sys/bus/usb/drivers/usb-storage/
├── 1-1.2.2:1.2 -> ../../../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2.2/1-1.2.2:1.2
├── bind
├── module -> ../../../../module/usb_storage
├── new_id
├── remove_id
├── uevent
└── unbind

2 directories, 5 files
# echo -n "1-1.2.2:1.2" > /sys/bus/usb/drivers/usb-storage/unbind
# tree /sys/bus/usb/drivers/usb-storage/
/sys/bus/usb/drivers/usb-storage/
├── bind
├── module -> ../../../../module/usb_storage
├── new_id
├── remove_id
├── uevent
└── unbind

1 directory, 5 files
# tree /sys/bus/usb/devices/1-1.2.2:1.2
/sys/bus/usb/devices/1-1.2.2:1.2
├── bAlternateSetting
├── bInterfaceClass
├── bInterfaceNumber
├── bInterfaceProtocol
├── bInterfaceSubClass
├── bNumEndpoints
├── ep_05
│   ├── bEndpointAddress
│   ├── bInterval
│   ├── bLength
│   ├── bmAttributes
│   ├── direction
│   ├── interval
│   ├── power
│   ├── type
│   ├── uevent
│   └── wMaxPacketSize
├── ep_84
│   ├── bEndpointAddress
│   ├── bInterval
│   ├── bLength
│   ├── bmAttributes
│   ├── direction
│   ├── interval
│   ├── power
│   ├── type
│   ├── uevent
│   └── wMaxPacketSize
├── modalias
├── power
├── subsystem -> ../../../../../../../../bus/usb
├── supports_autosuspend
└── uevent

6 directories, 27 files

After unbinding the usb-storage driver from the USB device, no more messages such as ‘usbfs: interface 2 claimed by usb-storage while 'xsane' sets config #1‘ are added to /var/log/messages when I launch a scanner application and select the USB device, but the scanner application still hangs (e.g. the XSane windows do not appear until I switch off the MP560 or unplug the USB cable). This happens irrespective of whether the USB device permissions are:

# ls -la /dev/bus/usb/001/009
crw-rw-r--+ 1 root lp 189, 8 Jul 20 20:35 /dev/bus/usb/001/009

or:

# ls -la /dev/bus/usb/001/009
crw-rw-r--+ 1 fitzcarraldo lp 189, 8 Jul 20 20:35 /dev/bus/usb/001/009

or:

# ls -la /dev/bus/usb/001/009
crw-rw-r--+ 1 fitzcarraldo scanner 189, 8 Jul 20 20:51 /dev/bus/usb/001/009

To reiterate, neither unbinding the usb-storage driver from the USB device first nor changing the owner and group of the USB device solves the problem. So possibly the problem is not caused by udev or by a USB driver, but by the SANE software.

Experiment 2

I found a thread in the SANE developers’ mailing list about the same problem with a different model of MFP: Help needed diagnosing strange failure to scan with Samsung SCX-4500W. The same user posted the same query in the Arch Linux forums: [SOLVED] Samsung SCX4500W MFP can’t get scanner to work. Like my Canon PIXMA MP560, that MFP has more than one interface in a single USB device. Notice in the Arch Linux thread that the user never found a solution and finally adopted the same work-around I use, namely to scan via a network interface rather than via the USB interface.

Theodore Kilgore gives a good explanation in that SANE developers’ mailing list thread of why the problem occurs in Linux, and Thorsten Müller’s post suggests a udev rule that sets the permissions for the MFP so that SANE, CUPS and logged-in users can all access the USB device. All I had to do was replace references to Samsung (such as the vendor ID and product ID) with those for my Canon PIXMA MP560. I left everything else in my installation exactly the way it was (for example I did not touch /lib64/udev/rules.d/41-libsane.rules and /lib64/udev/rules.d/70-printers.rules). The only thing I did was to add a udev rules file /etc/udev/rules.d/99-canon-mp560.rules with the following contents:

# ACL settings for Canon PIXMA MP560 printer MFP
ATTR{idVendor}=="04a9", ATTR{idProduct}=="173e", GOTO="canon"
GOTO="canon_end"

LABEL="canon"
RUN+="/bin/setfacl -m g:scanner:rw -m g:lp:rw $env{DEVNAME}"
TEST=="/var/run/ConsoleKit/database", \
        RUN+="udev-acl --action=$env{ACTION} --device=$env{DEVNAME}"

LABEL="canon_end"

Both /bin/setfacl and /var/run/ConsoleKit/database exist in my installation, and, as shown earlier, my user account is a member of both the lp and scanner groups. After adding the above file, I rebooted and it appears the rule works as intended:

# getfacl /dev/bus/usb/001/009
getfacl: Removing leading '/' from absolute path names
# file: dev/bus/usb/001/009
# owner: root
# group: lp
user::rw-
user:fitzcarraldo:rw-
group::rw-
group:lp:rw-
group:scanner:rw-
mask::rw-
other::r--

However, although I was still able to print I still could not scan. Mind you, the line beginning ‘TEST==‘ in the file /etc/udev/rules.d/99-canon-mp560.rules is also in the file /lib64/udev/rules.d/70-udev-acl.rules, but I’m no expert in udev rules, which are frankly beyond the ability of most users.

Experiment 3

I found a blog post about the same problem but with a Canon MP210 MFP: Canon multifunction printer: getting the printer drivers and scanner to work in debian/ubuntu/mint. This post suggested creating a udev rule file /etc/udev/rules.d/40-scanner-permissions.rules containing the following:

# usb scanner
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE:="0666"
SUBSYSTEM=="usb_device",MODE:="0666"

but, although the rule appears to work as intended, it is still not possible to scan in my case. I then changed the file name to /etc/udev/rules.d/95-scanner-permissions.rules, but that made no difference in my case.

Experiment 4

I also tried changing MODE="0664" to MODE="0666" in the following line in the file /lib64/udev/rules.d/50-udev-default.rules but that made no difference either:

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0664"

Experiment 5

I am able to change the group of the USB device to ‘scanner‘ instead of ‘lp‘ by adding a rules file /lib64/udev/rules.d/99-canon.rules (apparently 41-libsane.rules does not work as intended because it is overruled by a higher-numbered file, but I have no idea which one, as 41-libsane.rules is the only one explicitly mentioning the MP560) containing the following:

ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="173e", MODE="0664", GROUP="scanner", ENV{libsane_matched}="yes"

But scanning still does not work, whether or not I unbind the usb-storage driver from the USB device first and whether or not I change the owner and group of the USB device to root:lp, root:scanner, fitzcarraldo:lp or fitzcarraldo:scanner.

Conclusion

Although my installation can print via USB, scanning via USB does not work, whether or not the usb-storage driver is bound to the USB device and whoever is the owner and group of the USB device. So it could be that the problem is not caused by udev or by the kernel (a USB driver), but by the SANE software itself. However, I think it is more likely to be some sort of contention between kernel and userspace, despite unbinding the usb-storage driver from the device. Although I’m not excluding the possibility of a bug in SANE, irrespective of that it seems to me that the way GNU/Linux handles multiple interfaces of a single USB device is flawed. The udev design results in a morass of confusing and potentially conflicting rules in umpteen files in different directories, and the apparently uncoordinated independent demands on the same USB interface by the kernel and userspace are another potential source of problems. There is no standardisation in the udev rules files and their contents between the different Linux distributions, making investigation and solution of such problems even more complicated. Having to resort to work-arounds such as scanning via a network interface or scanning to a USB pen drive or memory card is a poor way of circumventing the problem, and is of no help anyway if a multi-function peripheral does not have a network interface, pen drive socket or memory card slot.

UPDATE (July 24, 2015): I have tried some other approaches, and had more success using a ‘quirk’ for the usb-storage driver — see my next post for details.

Addendum: How to stop scanning applications detecting a Webcam

By the way, to stop XSane and other scanning applications detecting my Webcam via the Video for Linux SANE backend, I commented out all the device lines in the file /etc/sane.d/v4l.conf like so:

$ cat
# In order to use the v4linux backend you have to give the device
# You can enable multiple lines if
# you really have multible v4l devices.
#
#/dev/bttv0
#/dev/video0
#/dev/video1
#/dev/video2
#/dev/video3

Follow

Get every new post delivered to your Inbox.

Join 68 other followers