Ubuntu22.04@WSL2でKernel ConfigしてBluetooth動いたよメモ

## 前提
* ***たぶん***Win11 + WSL2じゃないとだめ
* windows側でusbipをインストールしている
* usbipd wsl attach --busid BUS-ID を実行している
* lsusbでデバイスが認識されている
* 僕が試したのはIntel AX200

## カーネルコンパイル

```shell
$ uname --kernel-release
5.15.79.1-microsoft-standard-WSL2+
```

```shell
$ git clone --filter=blob:none https://github.com/microsoft/WSL2-Linux-Kernel
$ cd WSL2-Linux-Kernel
$ git checkout -b tag-5.15.79.1 refs/tags/linux-msft-wsl-5.15.79.1
```




```shell
$ zcat /proc/config.gz | sudo tee .config >/dev/null
$ make menuconfig #下に抜き出し
$ sudo make -j$(nproc)
$ sudo make modules_install
$ sudo make install -j 16
$ ls /lib/modules/
# 5.15.79.1-microsoft-standard-WSL2+ が出来ているはず
# カーネルをWindows側にコピー
cp ./vmlinux /mnt/c/tmp/wsl2-kernel/vmlinux
```

### menuconfig
[KERNEL Bluetooth サポートを有効化する](https://wiki.gentoo.org/wiki/Bluetooth/ja)

一応抜き出したけど↑を参照した方が良いと思います
```
  │ │                        --- Networking support                                                                         │ │
  │ │                              Networking options  --->                                                                 │ │
  │ │                        [ ]   Amateur Radio support  ----                                                              │ │
  │ │                        < >   CAN bus subsystem support  ----                                                          │ │
  │ │                        <M>   Bluetooth subsystem support  --->                                                        │ │  ←
  │ │                        < >   RxRPC session sockets                                                                    │ │
  │ │                        < >   KCM sockets                                                                              │ │
  │ │                        < >   MCTP core protocol support  ----                                                         │ │
  │ │                        [ ]   Wireless  ----                                                                           │ │
  │ │                        <*>   RF switch subsystem support  --->                                                        │ │  ←
  │ │                        <*>   Plan 9 Resource Sharing Support (9P2000)  --->                                           │ │
  │ │                        < >   CAIF support  ----                                                                       │ │
  │ │                        -*-   Ceph core library                                                                        │ │
  │ │                        [ ]     Include file:line in ceph debug output                                                 │ │
  │ │                        [ ]     Use in-kernel support for DNS lookup                                                   │ │
  │ │                        < >   NFC subsystem support  ----                                                              │ │
  │ │                        < >   Packet-sampling netlink channel  ----              

  │ │                        --- Bluetooth subsystem support                                                                │ │
  │ │                        [*]   Bluetooth Classic (BR/EDR) features                                                      │ │  ←
  │ │                        <M>     RFCOMM protocol support                                                                │ │  ←
  │ │                        [ ]       RFCOMM TTY support                                                                   │ │
  │ │                        < >     BNEP protocol support                                                                  │ │
  │ │                        <*>     HIDP protocol support                                                                  │ │  ←
  │ │                        [*]     Bluetooth High Speed (HS) features                                                     │ │  ←
  │ │                        [*]   Bluetooth Low Energy (LE) features                                                       │ │  ←
  │ │                        [ ]   Enable Microsoft extensions                                                              │ │
  │ │                        [ ]   Enable Android Open Source Project extensions                                            │ │
  │ │                        [*]   Export Bluetooth internals in debugfs                                                    │ │
  │ │                        [ ]   Bluetooth self testing support                                                           │ │
  │ │                        [ ]   Enable runtime option for debugging statements                                           │ │
  │ │                              Bluetooth device drivers  --->                                                           │ │  ←

  │ │                        <M> HCI USB driver                                                                             │ │  ←
  │ │                        [ ]   Enable USB autosuspend for Bluetooth USB devices by default                              │ │
  │ │                        [*]   Broadcom protocol support                                                                │ │
  │ │                        [ ]   MediaTek protocol support                                                                │ │
  │ │                        [*]   Realtek protocol support                                                                 │ │
  │ │                        <M> HCI UART driver                                                                            │ │  ←
  │ │                        -*-   UART (H4) protocol support                                                               │ │
  │ │                        [ ]   BCSP protocol support                                                                    │ │
  │ │                        [ ]   Atheros AR300x serial support                                                            │ │
  │ │                        [*] Intel AG6XX protocol support                                                               │ │  ← 念のため


  │ ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │
  │ │                        -*- HID bus support                                                                            │ │
  │ │                        [ ]   Battery level reporting for HID devices                                                  │ │
  │ │                        [ ]   /dev/hidraw raw HID device support                                                       │ │
  │ │                        <*>   User-space I/O driver support for HID subsystem                                          │ │  ←
  │ │                        <*>   Generic HID driver                                                                       │ │
  │ │                              Special HID drivers  --->                                                                │ │
  │ │                            USB HID support  --->                                                                      │ │
  │ │                            I2C HID support  --->                                                                      │ │
  │ │                            Intel ISH HID support  --->                                                                │ │
  │ │                            AMD SFH HID Support  --->                                                                  │ │

```

## 設定
```
$ cat /etc/wsl.conf
[boot]
systemd=true

$ cat /mnt/c/Users/admin/.wslconfig
[wsl2]
kernel=C:\\tmp\\wsl2-kernel\\vmlinux
```

## PowerShellで終了を確認
```powershell
PS C:\Windows\system32> wsl.exe --shutdown
PS C:\Windows\system32> wsl.exe --list -v
  NAME                   STATE           VERSION
* Ubuntu-22.04           Stopped         2
  docker-desktop-data    Stopped         2
  docker-desktop         Stopped         2
  Ubuntu-22.04-cuda      Stopped         2
```

## WSL2を起動して色々試していく
```shell
$ uname --kernel-release
5.15.79.1-microsoft-standard-WSL2+
$ uname --kernel-version # ビルド日時
#2 SMP Fri Jan 6 21:39:44 JST 2023

$ sudo systemctl start bluetooth
$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-01-06 22:22:39 JST; 14min ago
       Docs: man:bluetoothd(8)

$ lsmod
Module                  Size  Used by
btusb                  40960  0
btrtl                  24576  1 btusb
btbcm                  24576  1 btusb
btintel                36864  1 btusb
bluetooth             528384  25 btrtl,btintel,btbcm,btusb
ecdh_generic           16384  2 bluetooth
ecc                    36864  1 ecdh_generic
...

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 8087:0029 Intel Corp. AX200 Bluetooth
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

$ hcitool dev
Devices:
        hci0    XX:XX:XX:XX:XX:XX

$bluetoothctl scan on
Discovery started
[CHG] Controller  XX:XX:XX:XX:XX:XX Discovering: yes
[NEW] Device XX:XX:XX:XX:XX:XX YY-YY-YY-YY-YY-YY
```
というわけでうまくいったぽいのでメモ


## 追記 bluepy使うには権限が必要らしい
https://github.com/IanHarvey/bluepy/issues/218

```
$
pip install bluepy
blescan

sudo setcap cap_net_raw,cap_net_admin+ep .pyenv/versions/3.10.8/lib/python3.10/site-packages/bluepy/blue
py-helper

getcap .pyenv/versions/3.10.8/lib/python3.10/site-packages/bluepy/blue
py-helper
.pyenv/versions/3.10.8/lib/python3.10/site-packages/bluepy/bluepy-helper cap_net_admin,cap_net_raw=ep


$ blescan
Scanning for devices...
    Device (new): XX:XX:XX:XX:XX:XX (public), -75 dBm
        Flags: <06>
        Manufacturer: <省略>
    Device (new): XX:XX:XX:XX:XX:XX (random), -64 dBm
        Flags: <1a>
        Tx Power: <18>
        Manufacturer: <省略>
```



## パーミッションは必要？
```
$ cat /etc/group
$ id USERNAME
$ sudo usermod -G bluetooth -a USERNAME
```