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

From: "pelzflorian (Florian Pelz)" <[email-masked]>
Date: Wed, 22 May 2019 18:29:09 +0200

Thank you for your thorough review.  Find attached a new version of
the patch.  I can now modeswitch my USB modem by doing

./pre-inst-env guix install usb-modeswitch:dispatcher
sudo usb_modeswitch -c ~/.guix-profile/share/usb_modeswitch.d/12d1\:14fe -v 0x12d1 -p 0x14fe

Tring to put the packages in gnu/packages/libusb.scm and not in their
own file gnu/packages/usb-modeswitch.scm again resulted in

[ 11%] LOAD     guix/scripts/search.scm
[ 11%] LOAD     guix/scripts/gc.scm
[ 11%] LOAD     guix/scripts/hash.scm
[ 11%] LOAD     guix/scripts/pack.scm
Backtrace:
In ice-9/boot-9.scm:
   222:29 19 (map1 (((guix utils)) ((guix store)) ((guix #) # (#)) …))
   222:29 18 (map1 (((guix store)) ((guix status) #:select (#)) (#) …))
   222:29 17 (map1 (((guix status) #:select (#)) ((guix grafts)) # …))
   222:29 16 (map1 (((guix grafts)) ((guix monads)) ((guix #)) (#) …))
   222:29 15 (map1 (((guix monads)) ((guix modules)) ((guix #)) (#) …))
   222:29 14 (map1 (((guix modules)) ((guix packages)) ((guix #)) # …))
   222:29 13 (map1 (((guix packages)) ((guix profiles)) ((guix #)) …))
   222:29 12 (map1 (((guix profiles)) ((guix describe)) ((guix #)) …))
   222:29 11 (map1 (((guix describe)) ((guix derivations)) ((# #)) …))
   222:29 10 (map1 (((guix derivations)) ((guix search-paths)) (#) …))
   222:29  9 (map1 (((guix search-paths)) ((guix build-system #)) # …))
   222:29  8 (map1 (((guix build-system gnu)) ((guix scripts #)) # …))
   222:29  7 (map1 (((guix scripts build)) ((guix self) #:select …) …))
   222:29  6 (map1 (((guix self) #:select (make-config.scm)) ((…)) …))
   222:29  5 (map1 (((gnu packages)) ((gnu packages bootstrap)) (…) …))
   222:29  4 (map1 (((gnu packages bootstrap)) ((gnu packages …) …) …))
   222:17  3 (map1 (((gnu packages compression) #:hide (zip)) ((…)) …))
  2830:10  2 (resolve-interface (gnu packages compression) #:select _ …)
   260:13  1 (for-each #<procedure 52470e0 at ice-9/boot-9.scm:2830…> …)
In unknown file:
           0 (scm-error misc-error #f "~A" ("no binding `zip' to h…") …)

ERROR: In procedure scm-error:
no binding `zip' to hide in module (gnu packages compression)

when running make.  Then I rebuilt and commented and uncommented and
moved some code and putting the code in gnu/packages/libusb.scm worked
again in ./pre-inst-env.  Later it failed again.  Danny speculated the
reason was a use-module cycle.  I would like confirmation that this is
what a use-module cycle looks like and that I should stop trying to
put usb-modeswitch back in gnu/packages/libusb.scm.

As for the packaging, I hope it is correct to still refer to %outputs
in the trivial-build-system for usb-modeswitch-data.

Should I also make the usb-modeswitch-data package install the license
file of usb-modeswitch-data?

Or should I use gnu-build-system instead of trivial-build-system?
trivial works now though.

The file dispatcher.c of usb-modeswitch is licensed bsd-2 and not
gpl2+.  Should I list both licenses or just gpl2+?

I have checked the license of the files of usb-modeswitch-data and
usb-modeswitch by going through each of them with the `less` program
and reading its header while assuming that no license header means the
file has the license described in the README file, by diff’ing the
license files with a license downloaded from gnu.org, and that nothing
looked like a program blob (there only are very small hex strings for
the messages USB_ModeSwitch sends like

MessageContent="55534243123456782400000080000612000024000000000000000000000000"

that seem harmless).  Is this the usual procedure?

Currently I also install the UDEV rule shipped with
usb-modeswitch-data; I hope this is correct.

Currently I install the usb_modeswitch_dispatcher that is needed by
the udev rule in a separate output called “dispatcher”.  I am unsure
if this usb_modeswitch_dispatcher would need the usb_modeswitch
program from the “out” output.  Is it OK when one output only works
when the “out” output is installed too?

What is still missing is adding a service type for usb-modeswitch that
extends the udev service type by the rules file and that calls the
usb-modeswitch dispatcher once every reboot.  I can try making one,
but I am not sure if this is the right approach.  I am also unsure if
this service should be part of %desktop-services; many modems need it
though and modem-manager-service-type.

Regards,
Florian
>From af86ed879ecfc6ef5970ceff638a9303af497b14 Mon Sep 17 00:00:00 2001
From: Florian Pelz <[email-masked]>
Date: Wed, 22 May 2019 18:16:26 +0200
Subject: [PATCH] gnu: Add USB_ModeSwitch.

* gnu/packages/usb-modeswitch.scm: New file.
---
 gnu/local.mk                    |   1 +
 gnu/packages/usb-modeswitch.scm | 133 ++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 gnu/packages/usb-modeswitch.scm

diff --git a/gnu/local.mk b/gnu/local.mk
index cfc16c5ac8..7f834c374f 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -466,6 +466,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		\
diff --git a/gnu/packages/usb-modeswitch.scm b/gnu/packages/usb-modeswitch.scm
new file mode 100644
index 0000000000..c43f7c46bd
--- /dev/null
+++ b/gnu/packages/usb-modeswitch.scm
@@ -0,0 +1,133 @@
+;;; 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 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"))
+               (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)))))
+    (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))))
+    (native-inputs `(("pkg-config" ,pkg-config)))
+    (inputs `(("libusb" ,libusb)
+              ("jimtcl" ,jimtcl)))
+    (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            ; calling make would use usr as PREFIX
+           (lambda* (#:key source outputs inputs #:allow-other-keys)
+             (let* ((source (assoc-ref inputs "source"))
+                    (jimtcl (assoc-ref inputs "jimtcl"))
+                    (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"))
+                    (dispatcher-man1 (string-append dispatcher-out
+                                                    "/share/man/man1")))
+               (begin
+                 (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)
+                 (rename-file "usb_modeswitch.tcl" "usb_modeswitch_dispatcher")
+                 (substitute* "usb_modeswitch_dispatcher"
+                   (("!/usr/bin/tclsh")
+                    (string-append "!" jimtcl "/bin/jimsh")))
+                 (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