Linux: stop using /sys/class/gpio! - Page 1
Forum section: electronics / general

author: DiTBho
author_rank: Super Contributor
timestamp: May 01, 2023, 12:49:55 pm
content: so, as you can readhere/sys/class/gpio is deprecated and has been removed from the kernelfor example, today I am working on these two kernelskernel-5.4.128 has itkernel-5.15.107 doesn't have itit's time to change && update(or it's time to forward-port the old sys/class/gpio from k5.4 to k5.15and I will update myself and userspace apps another day in the future, probably with 6.2 kernelsmumble)

author: Monkeh
author_rank: Super Contributor
timestamp: May 01, 2023, 12:54:16 pm
content: And you only had seven years of warning! Despicable.

author: DiTBho
author_rank: Super Contributor
timestamp: May 01, 2023, 01:13:31 pm
content: Quote from: Monkeh on May 01, 2023, 12:54:16 pmlazy lazy

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 01, 2023, 07:50:36 pm
content: Being on version 5 is already good, Android 12 is still on a 4.14 kernel.

author: Monkeh
author_rank: Super Contributor
timestamp: May 01, 2023, 07:52:40 pm
content: Quote from: SiliconWizard on May 01, 2023, 07:50:36 pmThat's quite new compared to many embedded devices. Pretty sure some vendors are still shipping SDKs and binary drivers for 2.6.

author: DiTBho
author_rank: Super Contributor
timestamp: May 01, 2023, 08:15:01 pm
content: 2.4

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 01, 2023, 10:48:06 pm
content: That's what we get when we don't design the interface well enough from the get go.I am personally of two minds. On one hand, I don't like the kernel support discontinuity at all, and not all GPIO implementations have moved yet.On the other hand, the sysfs approach with a global namespace is just silly. There are several cases when plain GPIO pins are available on chips or modules that may or may not be available. Using a global numbering scheme, where the probe order determines the pin numbering, is just utterly silly.Compare to theLinux Input subsystemand itsevent interfaceandevent codes(and optionallyhiddev); specificallystruct input_eventevent interfaces/dev/input/eventNprovide.Basically, the event/evdev interface is based on a character driver, from which you read (one or more complete)struct input_eventstructures (that have not changed since 2001, since Vojtech Pavlik and others spent some time designing it). There are a few ioctl()s you can use to control e.g. whether the events are consumed (grabbed, see EVIOCGRAB) or whether one only reads the events with others seeing the events too (default).It's just one of the reasons why I keep harping about designing interfaces properly, with security in mind, from the get go. (The evdev character device owner and access mode is set by udev; I personally set rules to generate useful stable symlinks to special devices.)

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 02, 2023, 12:26:42 am
content: Agreed.

author: DiTBho
author_rank: Super Contributor
timestamp: May 02, 2023, 12:48:11 am
content: so, I prepared a special sysfs kernel module that- reads the gpio-phy(1) nodes from the OF (device tree) description- for special types(1), reads the gpio pin from OF and prepares /sys/gpio-phy-en/$item/{ value }- which also exports it to the sysfs-gpio-chardevthis way I don't need the old interface, it's compatible with the new interface and it's also accessible as sysfs, which is enough for my needgpio used to enable/disable the phymarked as "compatible,gpio-phy-en"

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 02, 2023, 01:47:56 am
content: Is it written in my-c?

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 02, 2023, 02:12:20 am
content: Quote from: SiliconWizard on May 02, 2023, 01:47:56 amNo, you can currently only use C in the Linux kernel, although Rust support is being introduced.The hardware involved is MikrotikRBM33G(OpenWRT support); a MIPS (mipsel_24kc architecture) routerboard using Mediatek MT7621A SoC. I have one myself, and it's a pretty interesting cheap little board for firewalls or full-featured 4G/LTE modems and/or wireless access points. You can even use two separate LTE modems, and handle connection failover.Problem is, the hardware is not so widely used, so changes like the aforementioned GPIO –– one of the GPIO pins controlling the USB 3.0 port PHY; i.e. the port is only turned on if you set the GPIO pin high –– do not get the changes applied when subsystems are being deprecated and moved.A tiny custom kernel driver is the perfect solution here, especially if one needs the USB 3 early in the boot, say for connection to the root filesystem.(I have a Kioxia NVMe on mine, but I've also used cheap (<10€) Jmicron JMS578 etc. -based USB-SATA bridges on my other USB 3 -capable SBCs without any issues.)

author: DiTBho
author_rank: Super Contributor
timestamp: May 02, 2023, 11:59:44 am
content: Quote from: SiliconWizard on May 02, 2023, 01:47:56 ammipsel-unknown-linux-gnu:{binutils,gcc}={2.35.1,9.3.0}.

author: DiTBho
author_rank: Super Contributor
timestamp: May 02, 2023, 12:02:21 pm
content: Quote from: Nominal Animal on May 02, 2023, 02:12:20 amumm, I am not sure about what super-gurus did on their Apple M1 Linux port.Someone said there is a Rust kernel driver, but I haven't yet investigated

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 02, 2023, 10:42:07 pm
content: Quote from: Nominal Animal on May 02, 2023, 02:12:20 amHa, if it's forbidden, then...(Kidding aside, I'm not sure DiTBho was really considering getting his contributions accepted in the Linux kernel, although, who knows... Otherwise, you can well do as you please. At least I hope so.)

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 03, 2023, 03:15:38 am
content: Quote from: SiliconWizard on May 02, 2023, 10:42:07 pmI've suggested submitting the kernel patches in PMs. It costs nothing except a bit of time and a self-esteem/ego hit to post them on LKML (as it is likely nobody responds to the patch), and especially when one describes the use case, at least it will be recorded forever in the mailing list archives.There are a couple I should send too. One (to kernel-janitors) is aboutseq_file_path()not escaping a backslash character, so filenames "foo"+newline+"bar" and "foo"+backslash+"n"+"bar" will be shown as the same string (in certain kernel interfaces). Onlymm/swapfile.c:swap_show()does it right currently.It's just that to send patches correctly to LKML, one should follow theSubmitting Patchesguidelines, including sending a Developers Certificate of Origin, using ones real name ("using a known identity (sorry, no anonymous contributions.)") –– well, one does not need to send it, just mention they've read and signed it in the first (couple of) patch submissions –– which is still a bit of an issue for myself. I do have a permanentiki.fiemail address under my real name, I just kinda-sorta still need the pseudonym to use as a crutch in social interactions.I'm a special sensitive snowflake...

author: DiTBho
author_rank: Super Contributor
timestamp: May 03, 2023, 10:22:41 am
content: Quote from: Nominal Animal on May 03, 2023, 03:15:38 amI asked a developer what he thinks and he said the "hacks" don't deserve attention since in my case a better thing to do would be to fix thevoltage regulator&& the device tree part && the gpio-specific part of the Mediatek SoC.Code:reg_usb_vbus: reg_usb_vbus{compatible = "regulator-fixed";regulator-name = "usb_vbus";regulator-min-microvolt = <5000000>;regulator-max-microvolt = <5000000>;//gpio = <&gpio 12 GPIO_ACTIVE_HIGH>; <---- commented, handled by phy-enenable-active-high;regulator-boot-on;regulator-always-on;};(this part of device-tree is correct but the kernel driver doesn't correctly handle it)So, okey ... mine is not a "patch" but rather a "hack"

author: DiTBho
author_rank: Super Contributor
timestamp: May 03, 2023, 10:24:46 am
content: oh, back to reason why I opened this topic: need to learnchardev-gpio? good examples in the link

author: 5U4GB
author_rank: Super Contributor
timestamp: May 03, 2023, 10:42:43 am
content: Quote from: Monkeh on May 01, 2023, 07:52:40 pm2.6.2x is the forever kernel. There are essentially two versions of Linux that you need to deal with, $latest_release and 2.6.2x.

author: DiTBho
author_rank: Super Contributor
timestamp: May 03, 2023, 11:12:24 am
content: Quote from: 5U4GB on May 03, 2023, 10:42:43 amyup, when you talk withdevs, they are usually to say "kernel_next", which means $latest_releasegit clonehttps://git.kernel.org/pub/... somethingbut, sometimes you still find it useful to deal with the simplicity of kernel v2.4kernel v2.4 have simpler PCI code for example, and the kernel tree embeds most of the initializations that kernels >2.6 expect to be performed by the firmware

author: Monkeh
author_rank: Super Contributor
timestamp: May 03, 2023, 12:33:53 pm
content: Quote from: 5U4GB on May 03, 2023, 10:42:43 amThat's what they'd like, but no, I actively refuse to have anything to do with such.

author: metertech58761
author_rank: Frequent Contributor
timestamp: May 11, 2023, 11:34:38 pm
content: ...is this why there are as many versions of Linux as grains of sand in a bucket?

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 12, 2023, 08:12:28 am
content: Quote from: metertech58761 on May 11, 2023, 11:34:38 pmNo, that is unrelated. This is a bad design issue.The reason there are so many versions of Linux is simply because of its configurability, adaptability, and evolution. Limiting to a smaller set of versions would necessarily limit those, too, so it is undesirable from the Linux users' perspective. Because the developers are also the users, that will not happen.Usually, the userspace interface is stable, but in rare cases like this one it just has to be modified because the original interface just cannot be maintained.DiTBho's solution is, in my experience, the correct one here: a tiny backported/forward-ported kernel driver to handle the early boot (before any filesystems are mounted) requirements.