Peak CAN usb module

G’Day,

I’m looking at trying to get support for my peak-can usb device, it has a in tree driver https://github.com/torvalds/linux/tree/v5.2/drivers/net/can/usb/peak_usb but not enabled in BalenaOS

I made a container environment based on the kernel-module-build example, getting the driver to work this way would me more convenient than re spinning BalenaOS at this time.

My problem is writing the Makefile, and perhaps also which of the drivers to include. My service directory atm:

can-peak
├── peak_usb/
│   ├── Makefile
│   ├── pcan_usb.c
│   ├── pcan_usb_core.c
│   ├── pcan_usb_core.h
│   ├── pcan_usb_fd.c
│   ├── pcan_usb_pro.c
│   └── pcan_usb_pro.h
├── build.sh*
├── Dockerfile.template
├── run.sh*
└── workarounds.sh*

My kernel make-fu is weak:

# SPDX-License-Identifier: GPL-2.0-only

# obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
# peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o

# ---

KERNEL_TREE_PATH?=/lib/modules/$(shell uname -r)/build
EXTRA_CFLAGS="-DDEBUG"

obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o

#obj-m+=peak_usb.o

all:
        make -C $(KERNEL_TREE_PATH) M=$(PWD) modules

clean:
        make -C $(KERNEL_TREE_PATH) M=$(PWD) clean

.PHONY: all clean

I get a bunch of link errors during compilation pointing to Makefile issues and perhaps not enough of the driver tree included.

Hi

Can you post the logs here?

OK with the Makefile like so:

KERNEL_TREE_PATH?=/lib/modules/$(shell uname -r)/build
EXTRA_CFLAGS="-DDEBUG"

obj-m += peak_usb.o
peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o

all:
	make -C $(KERNEL_TREE_PATH) M=$(PWD) modules

clean:
	make -C $(KERNEL_TREE_PATH) M=$(PWD) clean

.PHONY: all clean

Dockerfile.template:

FROM balenalib/%%BALENA_MACHINE_NAME%%-debian

RUN apt-get update && apt-get install -y \
  awscli \
  bc \
  bison \
  build-essential \
  curl \
  flex \
  libelf-dev \
  libssl-dev \
  python \
  wget


ENV VERSION '2.47.0+rev1'

COPY . /usr/src/app
WORKDIR /usr/src/app

RUN ./build.sh %%BALENA_MACHINE_NAME%% $VERSION peak_usb

CMD ./run.sh

Doing the build reults in the following:

[Build]   [can-peak]   CC       /tmp/tmp.14Q9jqGKEd/tools/objtool/special.o
[Build]   [can-peak]   CC       /tmp/tmp.14Q9jqGKEd/tools/objtool/objtool.o
[Build]   [can-peak]   CC       /tmp/tmp.14Q9jqGKEd/tools/objtool/libstring.o
[Build]   [can-peak]   CC       /tmp/tmp.14Q9jqGKEd/tools/objtool/str_error_r.o
[Build]   [can-peak]   LD       /tmp/tmp.14Q9jqGKEd/tools/objtool/objtool-in.o
[Build]   [can-peak] diff: ../../arch/x86/lib/insn.c: No such file or directory
[Build]   
[Build]   [can-peak] Warning: synced file at 'tools/objtool/arch/x86/lib/insn.c' differs from latest kernel version at 'arch/x86/lib/insn.c'
[Build]   [can-peak] diff: 
[Build]   [can-peak] ../../arch/x86/lib/inat.c
[Build]   [can-peak] : No such file or directory
[Build]   [can-peak] 
[Build]   
[Build]   [can-peak] Warning: synced file at 'tools/objtool/arch/x86/lib/inat.c' differs from latest kernel version at 'arch/x86/lib/inat.c'
[Build]   [can-peak] diff: 
[Build]   [can-peak] ../../arch/x86/lib/x86-opcode-map.txt
[Build]   [can-peak] : No such file or directory
[Build]   [can-peak] 
[Build]   
[Build]   [can-peak] Warning: synced file at 'tools/objtool/arch/x86/lib/x86-opcode-map.txt' differs from latest kernel version at 'arch/x86/lib/x86-opcode-map.txt'
[Build]   [can-peak] diff: 
[Build]   [can-peak] ../../arch/x86/tools/gen-insn-attr-x86.awk
[Build]   [can-peak] : No such file or directory
[Build]   [can-peak] 
[Build]   
[Build]   [can-peak] Warning: synced file at 'tools/objtool/arch/x86/tools/gen-insn-attr-x86.awk' differs from latest kernel version at 'arch/x86/tools/gen-insn-attr-x86.awk'
[Build]   [can-peak]   LINK     /tmp/tmp.14Q9jqGKEd/tools/objtool/objtool
[Build]   [can-peak] make: Leaving directory '/tmp/tmp.14Q9jqGKEd'
[Build]   [can-peak] make: Entering directory '/tmp/tmp.14Q9jqGKEd'
[Build]   [can-peak]   CC [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/pcan_usb_core.o
[Build]   [can-peak]   CC [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/pcan_usb.o
[Build]   [can-peak]   CC [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/pcan_usb_pro.o
[Build]   [can-peak]   CC [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/pcan_usb_fd.o
[Build]   [can-peak]   LD [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/peak_usb.o
[Build]   [can-peak]   Building modules, stage 2.
[Build]   [can-peak]   MODPOST 1 modules
[Build]   [can-peak]   CC      /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/peak_usb.mod.o
[Build]   [can-peak]   LD [M]  /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/peak_usb.ko
[Build]   [can-peak] make: Leaving directory '/tmp/tmp.14Q9jqGKEd'
[Build]   [can-peak] Removing intermediate container bae5a4b8cfae
[Build]   [can-peak]  ---> baacf040b324
[Build]   [can-peak] Step 7/9 : CMD ./run.sh
[Build]   [can-peak]  ---> Running in 5381204516c8
[Build]   [can-peak] Removing intermediate container 5381204516c8
[Build]   [can-peak]  ---> c09acf8878bd
[Build]   [can-peak] Step 8/9 : LABEL io.resin.local.image=1
[Build]   [can-peak]  ---> Running in 7ee27adb7e42
[Build]   [can-peak] Removing intermediate container 7ee27adb7e42
[Build]   [can-peak]  ---> fabc09d32752
[Build]   [can-peak] Step 9/9 : LABEL io.resin.local.service=can-peak
[Build]   [can-peak]  ---> Running in cc9feb4e77b2
[Build]   [can-peak] Removing intermediate container cc9feb4e77b2
[Build]   [can-peak]  ---> c190cb009532
[Build]   [can-peak] Successfully built c190cb009532
[Build]   [can-peak] Successfully tagged local_image_can-peak:latest

[Live]    Waiting for device state to settle...
[Info]    Streaming device logs...
[Live]    Watching for file changes...
[Logs]    [4/14/2020, 9:54:17 PM] Killing service 'can-peak sha256:cd06dfe98fec8e552bec58d35eb913ef7632859172be2b426ab76c494ab51cde'
[Logs]    [4/14/2020, 9:54:24 PM] [peak] 2020-04-14 13:54:24.256 | Level 10 | paho.mqtt.client:_easy_log:2415 - Sending PINGREQ
[Logs]    [4/14/2020, 9:54:24 PM] [peak] 2020-04-14 13:54:24.299 | Level 10 | paho.mqtt.client:_easy_log:2415 - Received PINGRESP
[Logs]    [4/14/2020, 9:54:28 PM] Service exited 'can-peak sha256:cd06dfe98fec8e552bec58d35eb913ef7632859172be2b426ab76c494ab51cde'
[Logs]    [4/14/2020, 9:54:29 PM] Killed service 'can-peak sha256:cd06dfe98fec8e552bec58d35eb913ef7632859172be2b426ab76c494ab51cde'
[Logs]    [4/14/2020, 9:54:34 PM] Installing service 'can-peak sha256:c190cb009532c31d6209144fa781167b075d7e87655fcdc22fbdf1281612680f'
[Logs]    [4/14/2020, 9:54:34 PM] Installed service 'can-peak sha256:c190cb009532c31d6209144fa781167b075d7e87655fcdc22fbdf1281612680f'
[Logs]    [4/14/2020, 9:54:34 PM] Starting service 'can-peak sha256:c190cb009532c31d6209144fa781167b075d7e87655fcdc22fbdf1281612680f'
[Logs]    [4/14/2020, 9:54:35 PM] Started service 'can-peak sha256:c190cb009532c31d6209144fa781167b075d7e87655fcdc22fbdf1281612680f'
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: FATAL: Module can not found in directory /lib/modules/5.2.10-yocto-standard
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] insmod: ERROR: could not load module peak_usb.ko: No such file or directory
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] Loading module from peak_usb_intel-nuc_2.47.0+rev1.prod_from_src
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] insmod: ERROR: could not load module peak_usb.ko: No such file or directory
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] insmod: ERROR: could not load module peak_usb.ko: No such file or directory
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] OS Version is 2.47.0+rev1
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] Loading module from peak_usb_intel-nuc_2.47.0+rev1.dev
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.2.10-yocto-standard/modules.dep.bin'
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] Loading module from peak_usb_intel-nuc_2.47.0+rev1.prod
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] Loading module from peak_usb_intel-nuc_2.47.0+rev1.dev_from_src
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.2.10-yocto-standard/modules.dep.bin'
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.2.10-yocto-standard/modules.dep.bin'
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.2.10-yocto-standard/modules.dep.bin'
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: FATAL: Module can not found in directory /lib/modules/5.2.10-yocto-standard
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: FATAL: Module can not found in directory /lib/modules/5.2.10-yocto-standard
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] insmod: ERROR: could not load module peak_usb.ko: No such file or directory
[Logs]    [4/14/2020, 9:54:35 PM] [can-peak] modprobe: FATAL: Module can not found in directory /lib/modules/5.2.10-yocto-standard

The .ko gets built (with the Makefile above) and it is available in the can-peak container:

root@a942892:/usr/src/app# ls /usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/peak_usb.ko
/usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.prod_from_src/peak_usb.ko

Trying to load the dependencies

root@a942892:/usr/src/app#   modprobe can can-dev
modprobe: ERROR: ../libkmod/libkmod.c:586 kmod_search_moddep() could not open moddep file '/lib/modules/5.2.10-yocto-standard/modules.dep.bin'
modprobe: FATAL: Module can not found in directory /lib/modules/5.2.10-yocto-standard

Quick question, are you running the container as a standalone deployment? Or are you using a docker-compose.yml file?

docker-compose.yml, it all happens in the can-peak container compose snippet below

  can-peak:
    build:
      context: ./can-peak/
    privileged: true
    network_mode: host

  peak:
    build:
      context: ./rag-peak/
      args:
        RELEASE: 0.0.4

    restart: always
    privileged: true
    network_mode: host
    depends_on: 
      - mqtt
      - can-peak
    volumes:
      - pat-config:/root/.config/heihei/

I’ve not thought about the composition much atm, need to be able to build and load the module first

Thanks, the error you are seeing points towards missing files in /lib/modules directory, for multi-container deployments you need to pass a label to the service to have our engine bind mount the /lib/modules directory from the host into your container.

The label is io.balena.features.kernel-modules, you can read a bit more about it here: https://www.balena.io/docs/learn/develop/multicontainer/#labels

So your can-peak container section should look like:

  can-peak:
    build:
      context: ./can-peak/
    privileged: true
    network_mode: host
    labels:
        io.balena.features.kernel-modules: '1'

Let me know if this helps!

Nice !

root@a942892:/usr/src/app# modprobe can
root@a942892:/usr/src/app# modprobe can-dev
root@a942892:/usr/src/app# insmod peak_usb.ko
root@a942892:/usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.dev_from_src# insmod peak_usb.ko

root@a942892:/usr/src/app/peak_usb_intel-nuc_2.47.0+rev1.dev_from_src# lsmod
Module                  Size  Used by
peak_usb               40960  0
can_dev                20480  1 peak_usb
ip6table_filter        16384  0
can_raw                20480  0
can                    20480  1 can_raw

Thanks for your help mate.

Glad that helped! Cheers!!