Subject: Re: bug#35640: Make USB modems just work on Guix

From: "pelzflorian (Florian Pelz)" <[email-masked]>
Date: Wed, 12 Jun 2019 12:36:47 +0200

Find attached patches that make USB modems just work, fixing this bug.

I have confirmed that at least on x86_64-linux

(service usb-modeswitch-service-type)

works with the default config and that

(service usb-modeswitch-service-type
         (usb-modeswitch-configuration
                              (config-file #f)))

works, passing no config file, leading to different defaults.  Manual
use as described in a comment of the usb-modeswitch package works as
well.

I hope using putenv before wrap-program is OK.  I tried using
computed-file #:options instead, but could not get it to work.

I am uncertain if having paragraphs like this in
modem-manager-service-type and usb-modeswitch-service-type is a good
idea:

     This service is part of ‘%desktop-services’ (*note Desktop
     Services::).

On Sat, May 25, 2019 at 09:42:15PM +0200, pelzflorian (Florian Pelz) wrote:
> Frankly, I am not actually sure if usb_modeswitch_dispatcher is really
> needed for the USB_ModeSwitch service type.

I am sure, it is needed.

> In much earlier (2010)
> versions of USB_ModeSwitch
> (<https://web.archive.org/web/20100921062014/http://draisberghof.de/usb_modeswitch/usb-modeswitch-data-20100826.tar.bz2>)
> the UDEV rules file referred to usb_modeswitch_dispatcher.  It does
> not do so anymore, instead it apparently uses usb_modeswitch directly.
>

There are two different executable files called usb_modeswitch.  The
one used by the UDEV rule runs the dispatcher.

> I will ask in the USB_ModeSwitch forums tomorrow if the dispatcher is
> needed.  I will also ask about an option for specifying the config
> file.  usb_modeswitch has one, usb_modeswitch_dispatcher does not have
> the option.
>

I have not asked.  I will propose the patch after this is commited at
Guix.

Regards,
Florian
>From 7af598d980a43d426c4341b1c581add06fd6ae72 Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Wed, 12 Jun 2019 10:24:05 +0200
Subject: [PATCH 1/3] gnu: Add USB_ModeSwitch.

* gnu/packages/usb-modeswitch.scm: New file.
* gnu/packages/patches/usb-modeswitch-accept-config-arg.patch: New file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add package.
  (dist_patch_DATA): Add patch.
---
 gnu/local.mk                                  |   2 +
 .../usb-modeswitch-accept-config-arg.patch    |  42 +++++
 gnu/packages/usb-modeswitch.scm               | 166 ++++++++++++++++++
 3 files changed, 210 insertions(+)
 create mode 100644 gnu/packages/patches/usb-modeswitch-accept-config-arg.patch
 create mode 100644 gnu/packages/usb-modeswitch.scm

diff --git a/gnu/local.mk b/gnu/local.mk
index 907641ff3e..7a90b279f6 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -468,6 +468,7 @@ GNU_SYSTEM_MODULES =				\
   %D%/packages/uml.scm				\
   %D%/packages/unrtf.scm			\
   %D%/packages/upnp.scm				\
+  %D%/packages/usb-modeswitch.scm		\
   %D%/packages/uucp.scm				\
   %D%/packages/valgrind.scm			\
   %D%/packages/version-control.scm		\
@@ -1332,6 +1333,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/util-linux-tests.patch			\
   %D%/packages/patches/upower-builddir.patch			\
   %D%/packages/patches/upx-fix-CVE-2017-15056.patch		\
+  %D%/packages/patches/usb-modeswitch-accept-config-arg.patch	\
   %D%/packages/patches/valgrind-enable-arm.patch		\
   %D%/packages/patches/vboot-utils-fix-format-load-address.patch	\
   %D%/packages/patches/vboot-utils-fix-tests-show-contents.patch	\
diff --git a/gnu/packages/patches/usb-modeswitch-accept-config-arg.patch b/gnu/packages/patches/usb-modeswitch-accept-config-arg.patch
new file mode 100644
index 0000000000..9c050f7ee6
--- /dev/null
+++ b/gnu/packages/patches/usb-modeswitch-accept-config-arg.patch
@@ -0,0 +1,42 @@
+--- old/usb_modeswitch.tcl	1970-01-01 01:00:00.000000000 +0100
++++ usb_modeswitch.tcl	2019-06-12 08:39:42.140000000 +0200
+@@ -41,7 +41,7 @@
+ global scsi usb config match device flags setup devdir loginit
+ 
+ set flags(config) ""
+-Log "[ParseGlobalConfig]"
++Log "[ParseGlobalConfig $argv]"
+ 
+ if {$flags(stordelay) > 0} {
+ 	SetStorageDelay $flags(stordelay)
+@@ -496,9 +496,21 @@
+ # end of proc {MatchDevice}
+ 
+ 
+-proc {ParseGlobalConfig} {} {
++proc {ParseGlobalConfig} {argv} {
+ 
+ global flags
++
++set configFileParam ""
++for {set i 0} {$i < [llength $argv]} {incr i} {
++	switch -glob -- [set v [lindex $argv $i]] {
++		--config-file=* {
++			set configFileParam $v
++		}
++	}
++}
++if {$configFileParam != ""} {
++	set configFile [string range $configFileParam [string length "--config-file="] end]
++} else {
+ set configFile ""
+ set places [list /etc/usb_modeswitch.conf /etc/sysconfig/usb_modeswitch /etc/default/usb_modeswitch]
+ foreach cfg $places {
+@@ -507,6 +519,7 @@
+ 		break
+ 	}
+ }
++}
+ if {$configFile == ""} {return}
+ 
+ set rc [open $configFile r]
diff --git a/gnu/packages/usb-modeswitch.scm b/gnu/packages/usb-modeswitch.scm
new file mode 100644
index 0000000000..533b6988ed
--- /dev/null
+++ b/gnu/packages/usb-modeswitch.scm
@@ -0,0 +1,166 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright ? 2019 Florian Pelz <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu packages usb-modeswitch)
+  #:use-module ((guix licenses) #:prefix license:)
+  #:use-module (guix packages)
+  #:use-module (guix download)
+  #:use-module (guix build-system gnu)
+  #:use-module (guix build-system trivial)
+  #:use-module (gnu packages)
+  #:use-module (gnu packages base)
+  #:use-module (gnu packages compression)
+  #:use-module (gnu packages embedded)
+  #:use-module (gnu packages libusb)
+  #:use-module (gnu packages pkg-config))
+
+(define-public usb-modeswitch-data
+  (package
+    (name "usb-modeswitch-data")
+    (version "20170806")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append
+                    "http://www.draisberghof.de/usb_modeswitch/"
+                    "usb-modeswitch-data-" version ".tar.bz2"))
+              (sha256
+               (base32
+                "0b1wari3aza6qjggqd0hk2zsh93k1q8scgmwh6f8wr0flpr3whff"))))
+    (build-system trivial-build-system)
+    (native-inputs `(("tar" ,tar)
+                     ("bzip2" ,bzip2)))
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder
+       (begin
+         (use-modules (guix build utils))
+         (let* ((source (assoc-ref %build-inputs "source"))
+                (tar (assoc-ref %build-inputs "tar"))
+                (bzip2 (assoc-ref %build-inputs "bzip2"))
+                (files (string-append "usb-modeswitch-data-"
+                                      ,(package-version this-package)))
+                (share-dir (string-append %output "/share"))
+                (doc-dir (string-append share-dir "/doc/"))
+                (license-dir (string-append doc-dir
+                                            (strip-store-file-name %output)))
+                (udev-dir (string-append %output "/udev")))
+           (copy-file source "data.tar.bz2")
+           (invoke (string-append bzip2 "/bin/bzip2") "-d" "data.tar.bz2")
+           (invoke (string-append tar "/bin/tar") "xvf" "data.tar")
+           (copy-recursively (string-append files "/usb_modeswitch.d")
+                             (string-append share-dir "/usb_modeswitch.d"))
+           (install-file (string-append files "/40-usb_modeswitch.rules")
+                         udev-dir)
+           (install-file (string-append files "/COPYING") license-dir)))))
+    (home-page "http://www.draisberghof.de/usb_modeswitch/")
+    (synopsis "Data package for USB_ModeSwitch")
+    (description "This packages contains data about devices and a UDEV rules
+file for use with USB_ModeSwitch.")
+    (license license:gpl2+)))
+
+(define-public usb-modeswitch
+  (package
+    (name "usb-modeswitch")
+    (version "2.5.2")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append
+                    "http://www.draisberghof.de/usb_modeswitch/"
+                    "usb-modeswitch-" version ".tar.bz2"))
+              (sha256
+               (base32
+                "19ifi80g9ns5dmspchjvfj4ykxssq9yrci8m227dgb3yr04srzxb"))
+              (modules '((guix build utils)))
+              (snippet
+               ;; Remove bundled jimtcl.
+               '(begin
+                  (delete-file-recursively "jim")
+                  #t))
+              (patches
+               (search-patches "usb-modeswitch-accept-config-arg.patch"))))
+    (native-inputs `(("pkg-config" ,pkg-config)))
+    (inputs `(("libusb" ,libusb)
+              ("jimtcl" ,jimtcl)
+              ("usb-modeswitch-data" ,usb-modeswitch-data)))
+    (outputs '("out" "dispatcher"))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:tests? #f                    ; does not support `make check`
+       #:make-flags (list "CC=gcc")
+       #:phases
+       (modify-phases %standard-phases
+         (delete 'configure)          ; no configure script
+         (replace 'install
+           (lambda* (#:key source outputs inputs #:allow-other-keys)
+             (let* ((source (assoc-ref inputs "source"))
+                    (jimtcl (assoc-ref inputs "jimtcl"))
+                    (data (assoc-ref inputs "usb-modeswitch-data"))
+                    (out (assoc-ref outputs "out"))
+                    (bin (string-append out "/bin"))
+                    (man1 (string-append out "/share/man/man1"))
+                    (dispatcher-out (assoc-ref outputs "dispatcher"))
+                    (udev (string-append dispatcher-out "/lib/udev"))
+                    (etc (string-append dispatcher-out "/etc"))
+                    (dispatcher-bin (string-append dispatcher-out "/bin"))
+                    (dispatcher-man1 (string-append dispatcher-out
+                                                    "/share/man/man1")))
+               (begin
+                 ;; Users can install the default output and
+                 ;; usb-modeswitch-data and then modeswitch their USB device
+                 ;; by running e.g.:
+                 ;;
+                 ;;   sudo usb_modeswitch -c \
+                 ;;   ~/.guix-profile/share/usb_modeswitch.d/12d1\:14fe \
+                 ;;   -v 0x12d1 -p 0x14fe
+                 ;;
+                 ;; But it is simpler to use the usb-modeswitch-service-type
+                 ;; that installs a UDEV rules file which invokes a shell
+                 ;; script in lib/udev (also called `usb_modeswitch' like the
+                 ;; main binary) which, in turn, invokes the program
+                 ;; `usb_modeswitch_dispatcher'.  Normal users should not
+                 ;; invoke this dispatcher directly, so it is a separate output.
+                 (install-file "usb_modeswitch" bin)
+                 (install-file "usb_modeswitch.conf" etc)
+                 (install-file "usb_modeswitch.1" man1)
+                 (install-file "usb_modeswitch_dispatcher.1" dispatcher-man1)
+
+                 (substitute* "usb_modeswitch.sh"
+                   (("PATH=") "PATH=$PATH:") ; we do not want hardcoded FHS path
+                   (("init_path=") "init_path=/does/not/exist")) ; no /sbin/init
+                 (rename-file "usb_modeswitch.sh" "usb_modeswitch")
+                 (install-file "usb_modeswitch" udev)
+
+                 (rename-file "usb_modeswitch.tcl" "usb_modeswitch_dispatcher")
+                 (substitute* "usb_modeswitch_dispatcher"
+                   (("!/usr/bin/tclsh")
+                    (string-append "!" jimtcl "/bin/jimsh"))
+                   (("/usr/sbin") bin)
+                   (("/usr/share/usb_modeswitch")
+                    (string-append data "/share/usb_modeswitch.d")))
+                 (install-file "usb_modeswitch_dispatcher"
+                               dispatcher-bin)
+                 #t)))))))
+    (home-page "http://www.draisberghof.de/usb_modeswitch/")
+    (synopsis "Mode switching tool for controlling `multi-mode' USB devices")
+    (description "USB_ModeSwitch is a mode switching tool for controlling USB
+devices with multiple @dfn{modes}.  When plugged in for the first time many
+USB devices (primarily high-speed WAN modems) act like a flash storage
+containing installers for Windows drivers.  USB_ModeSwitch replays the
+sequence the Windows drivers would send to switch their mode from storage to
+modem (or whatever the thing is supposed to do).")
+    (license license:gpl2+)))
-- 
2.21.0

>From db6e3a937f89811017abf06c7dffc494b71b9127 Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Tue, 11 Jun 2019 16:56:46 +0200
Subject: [PATCH 2/3] Add usb-modeswitch-service-type.

* gnu/services/networking.scm (usb-modeswitch-service-type): New variable.
(usb-modeswitch-configuration): New variable.
(usb-modeswitch-configuration->udev-rules): New procedure.
* doc/guix.texi (Networking Services): Document it.
---
 doc/guix.texi               | 35 +++++++++++++
 gnu/services/networking.scm | 99 +++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)

diff --git a/doc/guix.texi b/doc/guix.texi
index bd0f3e8fd5..28e752a335 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12655,6 +12655,41 @@ The ModemManager package to use.
 @end table
 @end deftp
 
+@cindex USB_ModeSwitch
+@cindex Modeswitching
+
+@defvr {Scheme Variable} usb-modeswitch-service-type
+This is the service type for the
+@uref{http://www.draisberghof.de/usb_modeswitch/, USB_ModeSwitch} service. The
+value for this service type is a @code{usb-modeswitch-configuration} record.
+
+When plugged in, some USB modems (and other USB devices) initially present
+themselves as a read-only storage medium and not as a modem.  They need to be
+@dfn{modeswitched} before they are usable.  The USB_ModeSwitch service type
+installs UDEV rules to automatically modeswitch these devices when they are
+plugged in.
+@end defvr
+
+@deftp {Data Type} usb-modeswitch-configuration
+Data type representing the configuration of USB_ModeSwitch.
+
+@table @asis
+@item @code{usb-modeswitch} (default: @code{usb-modeswitch})
+The USB_ModeSwitch package providing the binaries for modeswitching.
+
+@item @code{usb-modeswitch-data} (default: @code{usb-modeswitch-data})
+The package providing the device data and UDEV rules file used by
+USB_ModeSwitch.
+
+@item @code{config-file} (default: @code{#~(string-append #$usb-modeswitch:dispatcher "/etc/usb_modeswitch.conf")})
+Which config file to use for the USB_ModeSwitch dispatcher.  By default the
+config file shipped with USB_ModeSwitch is used which disables logging to
+@file{/var/log} among other default settings.  If set to @code{#f}, no config
+file is used.
+
+@end table
+@end deftp
+
 @cindex NetworkManager
 
 @defvr {Scheme Variable} network-manager-service-type
diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm
index 082a85f63d..fda589ab28 100644
--- a/gnu/services/networking.scm
+++ b/gnu/services/networking.scm
@@ -9,6 +9,7 @@
 ;;; Copyright © 2018 Tobias Geerinckx-Rice <[email-masked]>
 ;;; Copyright © 2018 Chris Marusich <[email-masked]>
 ;;; Copyright © 2018 Arun Isaac <[email-masked]>
+;;; Copyright © 2019 Florian Pelz <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -33,10 +34,13 @@
   #:use-module (gnu system shadow)
   #:use-module (gnu system pam)
   #:use-module (gnu packages admin)
+  #:use-module (gnu packages base)
+  #:use-module (gnu packages bash)
   #:use-module (gnu packages connman)
   #:use-module (gnu packages freedesktop)
   #:use-module (gnu packages linux)
   #:use-module (gnu packages tor)
+  #:use-module (gnu packages usb-modeswitch)
   #:use-module (gnu packages messaging)
   #:use-module (gnu packages networking)
   #:use-module (gnu packages ntp)
@@ -104,6 +108,12 @@
             modem-manager-configuration?
             modem-manager-service-type
 
+            usb-modeswitch-configuration
+            usb-modeswitch-configuration?
+            usb-modeswitch-configuration-usb-modeswitch
+            usb-modeswitch-configuration-usb-modeswitch-data
+            usb-modeswitch-service-type
+
             <wpa-supplicant-configuration>
             wpa-supplicant-configuration
             wpa-supplicant-configuration?
@@ -1042,6 +1052,95 @@ ModemManager}, a modem management daemon that aims to simplify dialup
 networking."))))
 
 
+;;;
+;;; USB_ModeSwitch
+;;;
+
+(define-record-type* <usb-modeswitch-configuration>
+  usb-modeswitch-configuration make-usb-modeswitch-configuration
+  usb-modeswitch-configuration?
+  (usb-modeswitch      usb-modeswitch-configuration-usb-modeswitch
+                       (default usb-modeswitch))
+  (usb-modeswitch-data usb-modeswitch-configuration-usb-modeswitch-data
+                       (default usb-modeswitch-data))
+  (config-file         usb-modeswitch-configuration-config-file
+                       (default #~(string-append #$usb-modeswitch:dispatcher
+                                                 "/etc/usb_modeswitch.conf"))))
+
+(define (usb-modeswitch-configuration->udev-rules config)
+  "Build a rules file for extending udev-service-type from the rules in the
+usb-modeswitch package.  The rules file will invoke usb_modeswitch.sh from the
+usb-modeswitch package, modified to pass the right config file."
+  (define usb-modeswitch-sh
+    (match-lambda
+      (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
+       (let ((orig #~(string-append #$usb-modeswitch:dispatcher
+                                    "/lib/udev/usb_modeswitch")))
+         (computed-file
+          "usb_modeswitch-sh"
+          (with-imported-modules '((guix build utils))
+            #~(begin
+                (use-modules (guix build utils))
+                (let ((cfg-param
+                       (case #$config-file
+                         ((#f) "")
+                         (else
+                          (string-append " --config-file=" #$config-file)))))
+                  (mkdir #$output)
+                  (install-file #$orig #$output)
+                  (substitute* (string-append #$output "/usb_modeswitch")
+                    (("(exec usb_modeswitch_dispatcher .*)( 2>>)" _ left right)
+                     (string-append left cfg-param right))
+                    (("(exec usb_modeswitch_dispatcher .*)( &)" _ left right)
+                     (string-append left cfg-param right)))
+
+                  ;; wrap-program needs bash in PATH:
+                  (putenv (string-append "PATH=" #$bash "/bin"))
+                  (wrap-program (string-append #$output "/usb_modeswitch")
+                    `("PATH" ":" = (,(string-append #$coreutils "/bin")
+                                    ,(string-append
+                                      #$usb-modeswitch:dispatcher
+                                      "/bin"))))))))))))
+  (match config
+    (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
+     (computed-file
+      "usb_modeswitch.rules"
+      (with-imported-modules '((guix build utils))
+        #~(begin
+            (use-modules (guix build utils))
+            (let ((in (string-append #$data "/udev/40-usb_modeswitch.rules"))
+                  (out (string-append #$output "/lib/udev/rules.d")))
+              (mkdir-p out)
+              (chdir out)
+              (install-file in out)
+              (substitute* "40-usb_modeswitch.rules"
+                (("PROGRAM=\"usb_modeswitch")
+                 (string-append "PROGRAM=\"" #$(usb-modeswitch-sh config)
+                                "/usb_modeswitch"))
+                (("RUN\\+=\"usb_modeswitch")
+                 (string-append "RUN+=\"" #$(usb-modeswitch-sh config)
+                                "/usb_modeswitch"))))))))))
+
+(define usb-modeswitch-service-type
+  (service-type
+   (name 'usb-modeswitch)
+   (extensions
+    (list
+     (service-extension
+      udev-service-type
+      (lambda (config)
+        (let ((rules (usb-modeswitch-configuration->udev-rules config)))
+          (list rules))))))
+   (default-value (usb-modeswitch-configuration))
+   (description "Run @uref{http://www.draisberghof.de/usb_modeswitch/,
+USB_ModeSwitch}, a mode switching tool for controlling USB devices with
+multiple @dfn{modes}.  When plugged in for the first time many USB
+devices (primarily high-speed WAN modems) act like a flash storage containing
+installers for Windows drivers.  USB_ModeSwitch replays the sequence the
+Windows drivers would send to switch their mode from storage to modem (or
+whatever the thing is supposed to do).")))
+
+
 ;;;
 ;;; WPA supplicant
 ;;;
-- 
2.21.0

>From dc50f979f77d9298e8c81e6943076c612e5f459a Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Wed, 12 Jun 2019 11:16:57 +0200
Subject: [PATCH 3/3] services: Include USB_ModeSwitch in %desktop-services.

Fixes <https://bugs.gnu.org/35640>.

* gnu/services/desktop.scm (%desktop-services): Add usb-modeswitch-service-type.
* doc/guix.texi (Networking Services): Document it.
---
 doc/guix.texi            | 3 +++
 gnu/services/desktop.scm | 1 +
 2 files changed, 4 insertions(+)

diff --git a/doc/guix.texi b/doc/guix.texi
index 28e752a335..5c36abeb71 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12668,6 +12668,9 @@ themselves as a read-only storage medium and not as a modem.  They need to be
 @dfn{modeswitched} before they are usable.  The USB_ModeSwitch service type
 installs UDEV rules to automatically modeswitch these devices when they are
 plugged in.
+
+This service is part of @code{%desktop-services} (@pxref{Desktop
+Services}).
 @end defvr
 
 @deftp {Data Type} usb-modeswitch-configuration
diff --git a/gnu/services/desktop.scm b/gnu/services/desktop.scm
index 652f7b1b02..343d507c14 100644
--- a/gnu/services/desktop.scm
+++ b/gnu/services/desktop.scm
@@ -1067,6 +1067,7 @@ dispatches events from it.")))
                          profile-service-type
                          (list network-manager-applet))
          (service modem-manager-service-type)
+         (service usb-modeswitch-service-type)
 
          ;; The D-Bus clique.
          (service avahi-service-type)
-- 
2.21.0