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

From: "pelzflorian (Florian Pelz)" <[email-masked]>
Date: Thu, 13 Jun 2019 20:05:52 +0200

On Thu, Jun 13, 2019 at 08:04:52PM +0200, pelzflorian (Florian Pelz) wrote:
> Find attached patches taking into account your suggestions.  (The
> second patch is unchanged.)
> 

Forgot attachment.  Sorry.
>From 27aff97683537407c178ea2b664069def0cf141a Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Thu, 13 Jun 2019 19:17:05 +0200
Subject: [PATCH 1/2] Add usb-modeswitch-service-type.

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

diff --git a/doc/guix.texi b/doc/guix.texi
index 9dc1d2a9ca..c407d6257f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12668,6 +12668,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..73acb3b17f 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,100 @@ 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-sh usb-modeswitch config-file)
+  "Build a copy of usb_modeswitch.sh located in package USB-MODESWITCH,
+modified to pass the CONFIG-FILE in its calls to usb_modeswitch_dispatcher,
+and wrap it to actually find the dispatcher in USB-MODESWITCH.  The script
+will be run by USB_ModeSwitch’s udev rules file when a modeswitchable USB
+device is detected."
+  (computed-file
+   "usb_modeswitch-sh"
+   (with-imported-modules '((guix build utils))
+     #~(begin
+         (use-modules (guix build utils))
+         (let ((cfg-param
+                #$(if config-file
+                      #~(string-append " --config-file=" #$config-file)
+                      "")))
+           (mkdir #$output)
+           (install-file (string-append #$usb-modeswitch:dispatcher
+                                        "/lib/udev/usb_modeswitch")
+                         #$output)
+
+           ;; insert CFG-PARAM into usb_modeswitch_dispatcher command-lines
+           (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")))))))))
+
+(define (usb-modeswitch-configuration->udev-rules config)
+  "Build a rules file for extending udev-service-type from the rules in the
+usb-modeswitch package specified in CONFIG.  The rules file will invoke
+usb_modeswitch.sh from the usb-modeswitch package, modified to pass the right
+config file."
+  (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"))
+                  (script #$(usb-modeswitch-sh usb-modeswitch config-file)))
+              (mkdir-p out)
+              (chdir out)
+              (install-file in out)
+              (substitute* "40-usb_modeswitch.rules"
+                (("PROGRAM=\"usb_modeswitch")
+                 (string-append "PROGRAM=\"" script "/usb_modeswitch"))
+                (("RUN\\+=\"usb_modeswitch")
+                 (string-append "RUN+=\"" script "/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 f4a5103b365f8bc5d2c514dad2e40f3c9981cfc2 Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Wed, 12 Jun 2019 11:16:57 +0200
Subject: [PATCH 2/2] 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 c407d6257f..8b07693445 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12681,6 +12681,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