Raspberry Pi 3でUSBRH

手元にあったので、Raspberry PiでUSBRHを使おうとしてハマったけれど動いたのでメモ

#環境
- Raspberry Pi 3 (not model B)
    - RASPBIAN STRECH WITH DESKTOP (Version: June 2013, Release date: 2018-06-27, Kernel version: 4.14)
    - BOOBS (Version:2.8.2, Release date: 2018-06-27)でインストール
- Strawberry Linux [USB温度・湿度計モジュール(完成品)](https://strawberry-linux.com/catalog/items?code=52002) + ケース ...合計\6,940だったらしい
- [USBRH diver for Linux](http://green-rabbit.sakura.ne.jp/usbrh/) ([github](https://github.com/kimata/usbrh)) 2018-02-05

#設定 (試行錯誤)
## 1. Raspberry Piを普通にセットアップ

```
pi@raspberrypi:/proc/usbrh/0 $ uname -a
Linux raspberrypi 4.14.52-v7+ #1123 SMP Wed Jun 27 17:35:49 BST 2018 armv7l GNU/Linux
```

## 2. usbrhをgithubからダウンロード

```
pi@raspberrypi:~ $ git clone https://github.com/kimata/usbrh.git
Cloning into 'usbrh'...
remote: Counting objects: 136, done.
remote: Total 136 (delta 0), reused 0 (delta 0), pack-reused 136
Receiving objects: 100% (136/136), 174.83 KiB | 138.00 KiB/s, done.
Resolving deltas: 100% (60/60), done.
```

## 3. make

### とりあえずmake

そのままmakeしようとすると、エラーになる...

```
pi@raspberrypi:~/usbrh $ make
make -C src all
make[1]: Entering directory '/home/pi/usbrh/src'
make -C /lib/modules/4.14.52-v7+/build M=/home/pi/usbrh/src modules
make[2]: Entering directory '/home/pi/usbrh/src'
make[2]: *** /lib/modules/4.14.52-v7+/build: No such file or directory.  Stop.
make[2]: Leaving directory '/home/pi/usbrh/src'
Makefile:7: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/home/pi/usbrh/src'
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 2
```

### rpi-sourceを入れる

[rpi-source](https://github.com/notro/rpi-source/wiki)を入れる必要があるらしい (参考: [USBRH Linux用ドライバをラズパイにインストールする](http://maaash.jp/2016/07/installing-usbrh-on-raspi/) )

```
sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source && sudo chmod +x /usr/bin/rpi-source && /usr/bin/rpi-source -q --tag-update
--2018-07-21 18:46:21--  https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.88.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.88.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12667 (12K) [text/plain]
Saving to: ‘/usr/bin/rpi-source’

/usr/bin/rpi-source           100%[=================================================>]  12.37K  --.-KB/s    in 0.001s

2018-07-21 18:46:21 (8.96 MB/s) - ‘/usr/bin/rpi-source’ saved [12667/12667]
```

とりあえず実行してみるが、gccのバージョンが違うといわれる
確認すると確かに違う ( ```4.14.52-v7+``` と ```gcc (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516``` )

```
pi@raspberrypi:~ $ which rpi-source
/usr/bin/rpi-source
pi@raspberrypi:~ $ rpi-source
ERROR:
gcc version check: mismatch between gcc (6) and /proc/version (4.9.3)
Skip this check with --skip-gcc

Help: https://github.com/notro/rpi-source/wiki
pi@raspberrypi:~ $ cat /proc/version
Linux version 4.14.52-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1123 SMP Wed Jun 27 17:35:49 BST 2018
pi@raspberrypi:~ $ gcc --version
gcc (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

\--skip-gccというオプションがあるらしいので、これをつけて実行
さらに、bcが無いというのでこれも追加

```
pi@raspberrypi:~ $ rpi-source --skip-gcc
ERROR:
bc is NOT installed. Needed by 'make modules_prepare'. On Raspbian, run 'sudo apt-get install bc' to install it.

Help: https://github.com/notro/rpi-source/wiki
pi@raspberrypi:~ $ sudo apt-get install bc
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  bc
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 96.3 kB of archives.
After this operation, 195 kB of additional disk space will be used.
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf bc armhf 1.06.95-9 [96.3 kB]
Fetched 96.3 kB in 2s (39.4 kB/s)
Selecting previously unselected package bc.
(Reading database ... 115460 files and directories currently installed.)
Preparing to unpack .../bc_1.06.95-9_armhf.deb ...
Unpacking bc (1.06.95-9) ...
Processing triggers for install-info (6.3.0.dfsg.1-1+b1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up bc (1.06.95-9) ...
```

再実行、さらに libncurses5-devも追加

```
pi@raspberrypi:~ $ rpi-source --skip-gcc

 *** Using: /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz

 *** Latest firmware revision: 21949dc1f06e9f893cc86d4ddacd549d24bd7db1

 *** Linux source commit: 11dc869d874d04293849d830637130269fd0ada9

 *** Download kernel source
--2018-07-21 19:12:56--  https://github.com/raspberrypi/linux/archive/11dc869d874d04293849d830637130269fd0ada9.tar.gz
Resolving github.com (github.com)... 192.30.255.112, 192.30.255.113
Connecting to github.com (github.com)|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/raspberrypi/linux/tar.gz/11dc869d874d04293849d830637130269fd0ada9 [following]
--2018-07-21 19:12:57--  https://codeload.github.com/raspberrypi/linux/tar.gz/11dc869d874d04293849d830637130269fd0ada9
Resolving codeload.github.com (codeload.github.com)... 192.30.255.121, 192.30.255.120
Connecting to codeload.github.com (codeload.github.com)|192.30.255.121|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9.tar.gz’

/home/pi/linux-11dc869d874d04     [                         <=>                      ] 152.39M  2.43MB/s    in 93s

2018-07-21 19:14:31 (1.65 MB/s) - ‘/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9.tar.gz’ saved [159789407]


 *** Unpack kernel source
...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
 *** Add '+' to kernel release string

 *** Create symlink: /home/pi/linux

 *** Create /lib/modules/<ver>/{build,source} symlinks

 *** .config

 *** Module.symvers

 *** make modules_prepare
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf  --silentoldconfig Kconfig
  SYSHDR  arch/arm/include/generated/uapi/asm/unistd-common.h
  SYSHDR  arch/arm/include/generated/uapi/asm/unistd-oabi.h
  SYSHDR  arch/arm/include/generated/uapi/asm/unistd-eabi.h
  CHK     include/config/kernel.release
  UPD     include/config/kernel.release
  WRAP    arch/arm/include/generated/uapi/asm/bitsperlong.h
  WRAP    arch/arm/include/generated/uapi/asm/errno.h
  WRAP    arch/arm/include/generated/uapi/asm/ioctl.h
  WRAP    arch/arm/include/generated/uapi/asm/ipcbuf.h
  WRAP    arch/arm/include/generated/uapi/asm/msgbuf.h
  WRAP    arch/arm/include/generated/uapi/asm/param.h
  WRAP    arch/arm/include/generated/uapi/asm/poll.h
  WRAP    arch/arm/include/generated/uapi/asm/resource.h
  WRAP    arch/arm/include/generated/uapi/asm/sembuf.h
  WRAP    arch/arm/include/generated/uapi/asm/shmbuf.h
  WRAP    arch/arm/include/generated/uapi/asm/siginfo.h
  WRAP    arch/arm/include/generated/uapi/asm/socket.h
  WRAP    arch/arm/include/generated/uapi/asm/sockios.h
  WRAP    arch/arm/include/generated/uapi/asm/termbits.h
  WRAP    arch/arm/include/generated/uapi/asm/termios.h
  WRAP    arch/arm/include/generated/asm/clkdev.h
  WRAP    arch/arm/include/generated/asm/current.h
  WRAP    arch/arm/include/generated/asm/early_ioremap.h
  WRAP    arch/arm/include/generated/asm/emergency-restart.h
  WRAP    arch/arm/include/generated/asm/exec.h
  WRAP    arch/arm/include/generated/asm/extable.h
  WRAP    arch/arm/include/generated/asm/irq_regs.h
  WRAP    arch/arm/include/generated/asm/kdebug.h
  WRAP    arch/arm/include/generated/asm/local.h
  WRAP    arch/arm/include/generated/asm/local64.h
  WRAP    arch/arm/include/generated/asm/mm-arch-hooks.h
  WRAP    arch/arm/include/generated/asm/msi.h
  WRAP    arch/arm/include/generated/asm/parport.h
  WRAP    arch/arm/include/generated/asm/preempt.h
  WRAP    arch/arm/include/generated/asm/rwsem.h
  WRAP    arch/arm/include/generated/asm/seccomp.h
  WRAP    arch/arm/include/generated/asm/segment.h
  WRAP    arch/arm/include/generated/asm/serial.h
  WRAP    arch/arm/include/generated/asm/simd.h
  WRAP    arch/arm/include/generated/asm/sizes.h
  WRAP    arch/arm/include/generated/asm/timex.h
  WRAP    arch/arm/include/generated/asm/trace_clock.h
  CHK     include/generated/uapi/linux/version.h
  UPD     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  UPD     include/generated/utsrelease.h
  HOSTCC  scripts/basic/bin2c
  SYSNR   arch/arm/include/generated/asm/unistd-nr.h
  GEN     arch/arm/include/generated/asm/mach-types.h
  SYSTBL  arch/arm/include/generated/calls-oabi.S
  SYSTBL  arch/arm/include/generated/calls-eabi.S
  CC      kernel/bounds.s
  CHK     include/generated/bounds.h
  UPD     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  UPD     include/generated/timeconst.h
  CC      arch/arm/kernel/asm-offsets.s
  CHK     include/generated/asm-offsets.h
  UPD     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  HOSTCC  scripts/dtc/dtc.o
  HOSTCC  scripts/dtc/flattree.o
  HOSTCC  scripts/dtc/fstree.o
  HOSTCC  scripts/dtc/data.o
  HOSTCC  scripts/dtc/livetree.o
  HOSTCC  scripts/dtc/treesource.o
  HOSTCC  scripts/dtc/srcpos.o
  HOSTCC  scripts/dtc/checks.o
  HOSTCC  scripts/dtc/util.o
  SHIPPED scripts/dtc/dtc-lexer.lex.c
  SHIPPED scripts/dtc/dtc-parser.tab.h
  HOSTCC  scripts/dtc/dtc-lexer.lex.o
  SHIPPED scripts/dtc/dtc-parser.tab.c
  HOSTCC  scripts/dtc/dtc-parser.tab.o
  HOSTLD  scripts/dtc/dtc
  HOSTCC  scripts/genksyms/genksyms.o
  SHIPPED scripts/genksyms/parse.tab.c
  HOSTCC  scripts/genksyms/parse.tab.o
  SHIPPED scripts/genksyms/lex.lex.c
  SHIPPED scripts/genksyms/parse.tab.h
  HOSTCC  scripts/genksyms/lex.lex.o
  HOSTLD  scripts/genksyms/genksyms
  CC      scripts/mod/empty.o
  HOSTCC  scripts/mod/mk_elfconfig
  MKELF   scripts/mod/elfconfig.h
  HOSTCC  scripts/mod/modpost.o
  CC      scripts/mod/devicetable-offsets.s
  CHK     scripts/mod/devicetable-offsets.h
  UPD     scripts/mod/devicetable-offsets.h
  HOSTCC  scripts/mod/file2alias.o
  HOSTCC  scripts/mod/sumversion.o
  HOSTLD  scripts/mod/modpost
  HOSTCC  scripts/kallsyms
  HOSTCC  scripts/pnmtologo
  HOSTCC  scripts/conmakehash
  HOSTCC  scripts/recordmcount
  HOSTCC  scripts/sortextable

 *** ncurses-devel is NOT installed. Needed by 'make menuconfig'. On Raspbian sudo apt-get install libncurses5-dev

 *** Help: https://github.com/notro/rpi-source/wiki
pi@raspberrypi:~ $ sudo apt-get install libncurses5-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libtinfo-dev
Suggested packages:
  ncurses-doc
The following NEW packages will be installed:
  libncurses5-dev libtinfo-dev
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 311 kB of archives.
After this operation, 1,469 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libtinfo-dev armhf 6.0+20161126-1+deb9u2 [65.6 kB]
Get:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libncurses5-dev armhf 6.0+20161126-1+deb9u2 [245 kB]
Fetched 311 kB in 1s (216 kB/s)
Selecting previously unselected package libtinfo-dev:armhf.
(Reading database ... 115478 files and directories currently installed.)
Preparing to unpack .../libtinfo-dev_6.0+20161126-1+deb9u2_armhf.deb ...
Unpacking libtinfo-dev:armhf (6.0+20161126-1+deb9u2) ...
Selecting previously unselected package libncurses5-dev:armhf.
Preparing to unpack .../libncurses5-dev_6.0+20161126-1+deb9u2_armhf.deb ...
Unpacking libncurses5-dev:armhf (6.0+20161126-1+deb9u2) ...
Setting up libtinfo-dev:armhf (6.0+20161126-1+deb9u2) ...
Setting up libncurses5-dev:armhf (6.0+20161126-1+deb9u2) ...
Processing triggers for man-db (2.7.6.1-2) ...
```

buildが出来ていた

```
pi@raspberrypi:/lib/modules/4.14.52-v7+/build $ ls -l
total 2280
drwxr-xr-x  32 pi pi   4096 Jun 27 08:06 arch
drwxr-xr-x   3 pi pi   4096 Jun 27 08:06 block
drwxr-xr-x   2 pi pi   4096 Jun 27 08:06 certs
-rw-r--r--   1 pi pi  18693 Jun 27 08:06 COPYING
-rw-r--r--   1 pi pi  98556 Jun 27 08:06 CREDITS
drwxr-xr-x   4 pi pi   4096 Jun 27 08:06 crypto
drwxr-xr-x 121 pi pi  12288 Jun 27 08:06 Documentation
drwxr-xr-x 131 pi pi   4096 Jun 27 08:06 drivers
drwxr-xr-x   2 pi pi   4096 Jun 27 08:06 firmware
drwxr-xr-x  74 pi pi   4096 Jun 27 08:06 fs
drwxr-xr-x  29 pi pi   4096 Jul 21 19:16 include
drwxr-xr-x   2 pi pi   4096 Jun 27 08:06 init
drwxr-xr-x   2 pi pi   4096 Jun 27 08:06 ipc
-rw-r--r--   1 pi pi   2293 Jun 27 08:06 Kbuild
-rw-r--r--   1 pi pi    287 Jun 27 08:06 Kconfig
drwxr-xr-x  17 pi pi   4096 Jul 21 19:16 kernel
drwxr-xr-x  13 pi pi  12288 Jun 27 08:06 lib
-rw-r--r--   1 pi pi 430280 Jun 27 08:06 MAINTAINERS
-rw-r--r--   1 pi pi  60468 Jun 27 08:06 Makefile
drwxr-xr-x   3 pi pi   4096 Jun 27 08:06 mm
-rw-r--r--   1 pi pi 794936 Jul 21 19:15 Module.symvers
-rw-r--r--   1 pi pi 794936 Jul 21 19:15 Module.symvers.backup
drwxr-xr-x  69 pi pi   4096 Jun 27 08:06 net
-rw-r--r--   1 pi pi    722 Jun 27 08:06 README
drwxr-xr-x  28 pi pi   4096 Jun 27 08:06 samples
drwxr-xr-x  14 pi pi   4096 Jul 21 19:16 scripts
drwxr-xr-x  10 pi pi   4096 Jun 27 08:06 security
drwxr-xr-x  24 pi pi   4096 Jun 27 08:06 sound
drwxr-xr-x  30 pi pi   4096 Jun 27 08:06 tools
drwxr-xr-x   2 pi pi   4096 Jun 27 08:06 usr
drwxr-xr-x   4 pi pi   4096 Jun 27 08:06 virt
```

### 再度make

```
pi@raspberrypi:~/usbrh $ make
make -C src all
make[1]: Entering directory '/home/pi/usbrh/src'
make -C /lib/modules/4.14.52-v7+/build M=/home/pi/usbrh/src modules
make[2]: Entering directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
  CC [M]  /home/pi/usbrh/src/usbrh.o
/home/pi/usbrh/src/usbrh.c: In function ‘usbrh_read’:
/home/pi/usbrh/src/usbrh.c:589:9: error: implicit declaration of function ‘copy_to_user’ [-Werror=implicit-function-declaration]
     if (copy_to_user(buffer, &value, count)) {
         ^~~~~~~~~~~~
cc1: some warnings being treated as errors
scripts/Makefile.build:334: recipe for target '/home/pi/usbrh/src/usbrh.o' failed
make[3]: *** [/home/pi/usbrh/src/usbrh.o] Error 1
Makefile:1522: recipe for target '_module_/home/pi/usbrh/src' failed
make[2]: *** [_module_/home/pi/usbrh/src] Error 2
make[2]: Leaving directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
Makefile:7: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/home/pi/usbrh/src'
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 2
```

``` implicit declaration of function ‘copy_to_user’ ``` のエラーで止まるが、 [implicit declaration of function copy_from_user]
(https://acassis.wordpress.com/2011/09/21/implicit-declaration-of-function-copy_from_user/)に従って、 ```#include <linux/uaccess.h>```をusbrh.cに追加

### 再々度make

```
pi@raspberrypi:~/usbrh $ make
make -C src all
make[1]: Entering directory '/home/pi/usbrh/src'
make -C /lib/modules/4.14.52-v7+/build M=/home/pi/usbrh/src modules
make[2]: Entering directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
  Building modules, stage 2.
  MODPOST 1 modules
make[2]: Leaving directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
make[1]: Leaving directory '/home/pi/usbrh/src'
pi@raspberrypi:~/usbrh $ sudo make install
make -C src install
make[1]: Entering directory '/home/pi/usbrh/src'
make -C /lib/modules/4.14.52-v7+/build M=/home/pi/usbrh/src modules_install
make[2]: Entering directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
  INSTALL /home/pi/usbrh/src/usbrh.ko
  DEPMOD  4.14.52-v7+
make[2]: Leaving directory '/home/pi/linux-11dc869d874d04293849d830637130269fd0ada9'
depmod
make[1]: Leaving directory '/home/pi/usbrh/src'
cp -f util/10-usbrh.rules /etc/udev/rules.d/
cp -f util/usbrh.sh /lib/udev/
```

インストール出来たっぽい

## 4. 動作確認

```
pi@raspberrypi:/proc/usbrh/0 $ dmesg
[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 4.14.52-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1123 SMP Wed Jun 27 17:35:49 BST 2018
[    0.000000] CPU: ARMv7 Processor [410fd034] revision 4 (ARMv7), cr=10c5383d
[    0.000000] CPU: div instructions available: patching division code
  :  :  :
[36008.683683] usb 1-1.4: new low-speed USB device number 6 using dwc_otg
[36008.829278] usb 1-1.4: New USB device found, idVendor=1774, idProduct=1001
[36008.829292] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[36008.829301] usb 1-1.4: Product: Hygrometer/Thermometer
[36008.829309] usb 1-1.4: Manufacturer: Strawberry Linux Co.,Ltd.
[36008.840039] hid-generic 0003:1774:1001.0004: hiddev97,hidraw2: USB HID v1.00 Device [Strawberry Linux Co.,Ltd. Hygrometer/Thermometer] on usb-3f980000.usb-1.4/input0
[36008.914324] usb 1-1.4: USBRH device now attached to /dev/usbrh0
```

```
pi@raspberrypi:/proc/usbrh/0 $ lsmod
Module                  Size  Used by
binfmt_misc            20480  1
usbrh                  16384  0
configs                49152  0
```

このドライバは、/proc/usbrh/(何個目のデバイスか) に出来ているファイルを読み出す形なので、見てみる

```
pi@raspberrypi:~ $ cd /proc/usbrh/0/
pi@raspberrypi:/proc/usbrh/0 $ ls
heater  humidity  led  status  temperature
pi@raspberrypi:/proc/usbrh/0 $ cat status
t:29.95 h:51.16
```

OKっぽい

## 5. 追加

heater, ledはrootのみwritableだったのを、誰でも出来るように変更
(参考 [USBRH+Cactiで室温モニタリング(1)](http://stereocat.hatenablog.com/entry/20100712/1278937930) )

```
pi@raspberrypi:~/usbrh/src $ diff usbrh.c usbrh.c.org
479c479
<             mode |= S_IWUGO;
---
>             mode |= S_IWUSR;
```

```
pi@raspberrypi:/proc/usbrh/0 $ ls -l
total 0
-rw-rw-rw- 1 root root 0 Jul 22 02:55 heater
-r--r--r-- 1 root root 0 Jul 22 02:55 humidity
-rw-rw-rw- 1 root root 0 Jul 22 02:55 led
-r--r--r-- 1 root root 0 Jul 22 02:55 status
-r--r--r-- 1 root root 0 Jul 22 02:55 temperature
```