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

From: "pelzflorian (Florian Pelz)" <[email-masked]>
Date: Mon, 13 May 2019 22:57:20 +0200

On Thu, May 09, 2019 at 05:24:39PM +0200, Ludovic Courtès wrote:
> The comment is unnecessary, but otherwise LGTM!
> 

:) I have attached the patch without the command.

Also attached is my current work-in-progress package for
USB_ModeSwitch.  It does *not* work because I cannot figure out how to
use tcl in the package’s inputs field.

#:use-module gives me errors; only (@ (gnu packages tcl) tcl) appears
to work.

> Would it be an option to avoid propagation by instead substituting
> references to these two things in the code of network-manager-applet and
> gnome-control-center?  WDYT?
> 

I will try finding and imitating an example for such substitutions
tomorrow.

Regards,
Florian

>From 827d931eb6b72572f547c87e650780cf50e9e20c Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Mon, 13 May 2019 22:51:28 +0200
Subject: [PATCH] services: Include ModemManager in %desktop-services.

* gnu/services/desktop.scm (%desktop-services): Add modem-manager-service-type.
---
 gnu/services/desktop.scm | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gnu/services/desktop.scm b/gnu/services/desktop.scm
index 5e0bd102a2..ac714ca5e0 100644
--- a/gnu/services/desktop.scm
+++ b/gnu/services/desktop.scm
@@ -1062,6 +1062,7 @@ dispatches events from it.")))
 
          ;; The D-Bus clique.
          (service network-manager-service-type)
+         (service modem-manager-service-type)
          (service wpa-supplicant-service-type)    ;needed by NetworkManager
          (service avahi-service-type)
          (udisks-service)
-- 
2.21.0

>From 8861177166d74b84c0ac18bf102802cc1e6cf5d3 Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Mon, 13 May 2019 22:35:00 +0200
Subject: [PATCH] Add USB_ModeSwitch.

* gnu/packages/libusb.scm (usb-modeswitch-data) Add it.
(usb-modeswitch) Add it.
---
 gnu/packages/libusb.scm | 91 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/gnu/packages/libusb.scm b/gnu/packages/libusb.scm
index 5ef62e47fb..00577bb333 100644
--- a/gnu/packages/libusb.scm
+++ b/gnu/packages/libusb.scm
@@ -8,6 +8,7 @@
 ;;; Copyright © 2017 Jonathan Brielmaier <[email-masked]>
 ;;; Copyright © 2018 Tobias Geerinckx-Rice <[email-masked]>
 ;;; Copyright © 2018 Vagrant Cascadian <[email-masked]>
+;;; Copyright © 2019 Florian Pelz <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -25,6 +26,7 @@
 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
 
 (define-module (gnu packages libusb)
+  #:use-module (system repl debug)
   #:use-module (gnu packages)
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix packages)
@@ -36,6 +38,7 @@
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system glib-or-gtk)
   #:use-module (guix build-system python)
+  #:use-module (guix build-system trivial)
   #:use-module (gnu packages autotools)
   #:use-module (gnu packages gnupg)
   #:use-module (gnu packages gtk)
@@ -45,8 +48,12 @@
   #:use-module (gnu packages pkg-config)
   #:use-module (gnu packages python)
   #:use-module (gnu packages python-xyz)
+  #:use-module ((gnu packages tcl) #:prefix tcl:)
+  #:use-module (system foreign)
+  #:use-module (rnrs bytevectors)
   #:use-module (gnu packages tls)
   #:use-module (gnu packages xiph))
+(terminal-width 1000000)
 
 (define-public libusb
   (package
@@ -601,3 +608,87 @@ HID-Class devices.")
 
 (define-public python2-hidapi
   (package-with-python2 python-hidapi))
+
+(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)
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder
+       (begin
+         (let ((share-dir (string-append %output "/share")))
+           (install-file
+            (string-append (assoc-ref %build-inputs "source")
+                           "/usb_modeswitch.d")
+            share-dir))
+         #t)))
+    (home-page "http://www.draisberghof.de/usb_modeswitch/")
+    (synopsis "Data package for USB_ModeSwitch")
+    (description "Device data collection and UDEV rules file for
+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"))))
+    (native-inputs
+     `(("pkg-config" ,pkg-config)))
+    (inputs `(("libusb" ,libusb)
+              ("tcl" ,(error tcl:tcl))))
+    (propagated-inputs `(("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 _                  ; calling make would use usr as PREFIX
+             (let* ((source (assoc-ref %build-inputs "source"))
+                    (out (assoc-ref %outputs "out"))
+                    (bin (string-append out "/bin"))
+                    (man1 (string-append out "/share/man/man1"))
+                    (dispatcher-out (assoc-ref %outputs "dispatcher"))
+                    (etc (string-append dispatcher-out "/etc"))
+                    (dispatcher-bin (string-append dispatcher-out "/bin")))
+               (begin
+                 (install-file "usb_modeswitch" bin)
+                 (install-file "usb_modeswitch.1" man1)
+                 (install-file "usb_modeswitch.conf" etc)
+                 (rename-file "usb_modeswitch.tcl" "usb_modeswitch_dispatcher")
+                 (substitute* "usb_modeswitch_dispatcher"
+                   (("!/usr/bin/tclsh")
+                     (string-append (assoc-ref %build-inputs "tcl")
+                                    "/bin/tclsh")))
+                 (install-file "usb_modeswitch_dispatcher"
+                               dispatcher-bin))))))))
+    (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 \"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