Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phandle-array doesn't allow array of just phandles #28709

Closed
joelspadin opened this issue Sep 26, 2020 · 8 comments · Fixed by #28927
Closed

phandle-array doesn't allow array of just phandles #28709

joelspadin opened this issue Sep 26, 2020 · 8 comments · Fixed by #28927
Assignees
Labels
area: Devicetree Tooling PR modifies or adds a Device Tree tooling area: Devicetree bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@joelspadin
Copy link
Contributor

Describe the bug

# - 'type: phandle-array' is for properties that take a list of phandles and
# (possibly) 32-bit numbers, like
#
# pwms = <&ctrl-1 1 2 &ctrl-2 3 4>;
indicates that in a value of type phandle-array, numbers are optional, so <&foo &bar> should be valid.

However,

zephyr/scripts/dts/edtlib.py

Lines 1225 to 1230 in 0a0cb52

if prop_type == "phandle-array":
# This type is a bit high-level for dtlib as it involves
# information from bindings and *-names properties, so there's no
# to_phandle_array() in dtlib. Do the type check ourselves.
if prop.type not in (TYPE_PHANDLE, TYPE_PHANDLES_AND_NUMS):
_err("expected property '{0}' in {1} in {2} to be assigned "
appears to only allow TYPE_PHANDLE (a single phandle) and TYPE_PHANDLES_AND_NUMS. Is that supposed to be TYPE_PHANDLES instead of TYPE_PHANDLE?

To Reproduce
Steps to reproduce the behavior:

  1. Set up the ZMK keyboard firmware project.
  2. Edit app/boards/shields/romac/romac.keymap and change one of the bindings arrays to contain only values such as &reset and &trans so there are no numbers in the array.
  3. Build with
cd app
west build -b proton_c -- -DSHIELD=romac

This will fail with an error like

devicetree error: expected property 'bindings' in /keymap/nav_layer in proton_c.dts.pre.tmp to be assigned with 'bindings = < &foo 1 2 ... &bar 3 4 ... >' (a mix of phandles and numbers), not 'bindings = < &reset &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans >;'

Expected behavior
As far as I can tell, an array of just phandles should be valid for a phandle-array.

Impact
Annoyance. Can be worked around by adding something that uses numbers somewhere in the array.

Environment (please complete the following information):

@joelspadin joelspadin added the bug The issue is a bug, or the PR is fixing a bug label Sep 26, 2020
@joelspadin joelspadin changed the title phandle-array does allow array of just phandles phandle-array doesn't allow array of just phandles Sep 27, 2020
@henrikbrixandersen
Copy link
Member

How about <&foo>, <&bar>?

@joelspadin
Copy link
Contributor Author

Same error (tested in a different keymap with a slightly different array):

devicetree error: expected property 'bindings' in /keymap/daw_layer in nice_nano.dts.pre.tmp to be assigned with 'bindings = < &foo 1 2 ... &bar 3 4 ... >' (a mix of phandles and numbers), not 'bindings = < &none >, < &none >, < &none >, < &none >, < &none >, < &none >, < &none >, < &none >, < &trans >, < &none >, < &none >, < &none >, < &none >, < &none >, < &none >, < &none >;'

@b0661
Copy link
Collaborator

b0661 commented Sep 28, 2020

# - 'type: phandle-array' is for properties that take a list of phandles and
# (possibly) 32-bit numbers, like
#
# pwms = <&ctrl-1 1 2 &ctrl-2 3 4>;
the ´(possibly)´ relates to ´32-bit´ (and not to numbers). phandle-array is always a combination of phandle and associated number(s). The numbers may be up to 32-bit size. In case you only have phandles just use ´phandles´ in the binding.

´(possibly)´ should possibly replaced or removed.

@joelspadin
Copy link
Contributor Author

joelspadin commented Sep 28, 2020

phandle-array is always a combination of phandle and associated number(s).

I'll admit I am still very unfamiliar with DT, but I'm confused as to why edtlib.py allows phandle-array to be either TYPE_PHANDLE or TYPE_PHANDLES_AND_NUMS then. Assuming TYPE_PHANDLE is not a typo intended to be TYPE_PHANDLES, then what would it mean for a value of type phandle-array to be set to a single phandle?

Having phandles with no associated numbers seems to work as long as there is at least one number somewhere in the array. If phandle-array allows any element to be a phandle with no associated number, why is it not valid for the entire array to be made of such elements? If that is not valid, then should phandle-array require every phandle to have at least one associated number?

I don't know if DT was intended to be used like this, but one of the ways it is used in ZMK is to define a mapping between keyboard keys and the behaviors to use when each key is pressed. Some behaviors such as &kp KEY and &lt LAYER KEY have one or more associated numbers. Other behaviors such as &reset and &none don't have any associated numbers. ZMK allows you to mix both types of behaviors in a keymap.

@b0661
Copy link
Collaborator

b0661 commented Sep 29, 2020

Having phandles with no associated numbers seems to work as long as there is at least one number somewhere in the array. If phandle-array allows any element to be a phandle with no associated number, why is it not valid for the entire array to be made of such elements?

You are right, the error check seems too strict for the degenerated case that you describe.

if prop.type not in (TYPE_PHANDLE, TYPE_PHANDLES_AND_NUMS):

I could not find behaviors.dtsi which defines the behavior nodes. The reset and none nodes should have properties like xxx-cells = 0; that define the count of values to be 0. With this given the phandles list as a degenerated phandles-array is a valid construct.

In my view TYPE_PHANDLES should be added to the allowed dtslib types for phandle-array.

@MaureenHelm MaureenHelm added the priority: low Low impact/importance bug label Sep 29, 2020
@joelspadin
Copy link
Contributor Author

I could not find behaviors.dtsi which defines the behavior nodes. The reset and none nodes should have properties like xxx-cells = 0; that define the count of values to be 0.

Yep. See https://github.com/zmkfirmware/zmk/blob/main/app/dts/behaviors/none.dtsi for example.

Should TYPE_PHANDLE be kept and TYPE_PHANDLES added, or should that be replaced with TYPE_PHANDLES?

@b0661
Copy link
Collaborator

b0661 commented Oct 1, 2020

Should TYPE_PHANDLE be kept and TYPE_PHANDLES added

Yes. Looking at

zephyr/scripts/dts/dtlib.py

Lines 1353 to 1357 in 1342d12

foo = <&l>; | dtlib.TYPE_PHANDLE
foo = <&l1 &l2 &l3>; | dtlib.TYPE_PHANDLES
foo = <&l1 &l2>, <&l3>; | dtlib.TYPE_PHANDLES
foo = <&l1 1 2 &l2 3 4>; | dtlib.TYPE_PHANDLES_AND_NUMS
foo = <&l1 1 2>, <&l2 3 4>; | dtlib.TYPE_PHANDLES_AND_NUMS
suggests that TYPE_PHANDLE and TYPE_PHANDLES are valid types.

@mbolivar-nordic mbolivar-nordic added the area: Devicetree Tooling PR modifies or adds a Device Tree tooling label Oct 1, 2020
@mbolivar-nordic
Copy link
Contributor

TYPE_PHANDLE and TYPE_PHANDLES are valid types.

Agreed; I'll send a PR

mbolivar-nordic added a commit to mbolivar-nordic/zephyr that referenced this issue Oct 6, 2020
Each controller node in a phandle-array may set the number of cells in
a specifier as any nonnegative integer. Currently, we don't allow
this in edtlib in the case where there are multiple controllers in a
phandle-array property all of which have 0 cells in the relevant
specifier, which is not correct. Fix this, add a regression test, and
improve the error message while we are here.

Fixes: zephyrproject-rtos#28709
Signed-off-by: Martí Bolívar <[email protected]>
galak pushed a commit that referenced this issue Oct 6, 2020
Each controller node in a phandle-array may set the number of cells in
a specifier as any nonnegative integer. Currently, we don't allow
this in edtlib in the case where there are multiple controllers in a
phandle-array property all of which have 0 cells in the relevant
specifier, which is not correct. Fix this, add a regression test, and
improve the error message while we are here.

Fixes: #28709
Signed-off-by: Martí Bolívar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Tooling PR modifies or adds a Device Tree tooling area: Devicetree bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants