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

Debian 10: wiringPiSetup: mmap (GPIO) failed: Operation not permitted #108

Closed
liffiton opened this issue Apr 11, 2021 · 5 comments
Closed

Comments

@liffiton
Copy link
Member

liffiton commented Apr 11, 2021

[To workaround issue #107] I hardcoded the return of piBoardId() to PI_MODEL_4B,0,0,0 seeing that the rest of the values don't seem to matter and finally got past the error:

 gpio -v
gpio version: 2.60
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 00, Memory: 256MB, Maker: Sony
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * Root or sudo required for GPIO access.

But I still can't get it working on plain Debian 10, getting this:

wiringPiSetup: mmap (GPIO) failed: Operation not permitted

Tried to add iomem=relaxed to /boot/firmware/cmdline.txt as suggested in the Debian Wiki and running as root but it didn't make any difference.

Originally posted by @albertmonzo in #107 (comment)

@liffiton
Copy link
Member Author

liffiton commented Apr 11, 2021

One possible cause may be that this line setting piGpioBase to 0 was triggered. It's not clear that the logic there is correct, because it then proceeds to use piGpioBase=0 in the mmap calls, which doesn't seem correct.

To be clear, though, that's just a quick guess.

@albertmonzo
Copy link

That did it! Here's the patch if anyone else needs it:

diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c
index d45f83f..6f3543d 100644
--- a/wiringPi/wiringPi.c
+++ b/wiringPi/wiringPi.c
@@ -750,6 +750,8 @@ int piGpioLayout (void)
   char line [120] ;
   char *c ;
   static int  gpioLayout = -1 ;
+
+  return 2;

   if (gpioLayout != -1)	// No point checking twice
     return gpioLayout ;
@@ -964,6 +966,9 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
 //	Will deal with the properly later on - for now, lets just get it going...
 //  unsigned int modelNum ;

+  *model = PI_MODEL_4B ; *rev = PI_VERSION_1_2 ; *mem = 2 ; *maker = PI_MAKER_EGOMAN;
+  return
+
   (void)piGpioLayout () ;	// Call this first to make sure all's OK. Don't care about the result.

   if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
@@ -2335,7 +2340,6 @@ int wiringPiSetup (void)
   {
     if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)	// We're using gpiomem
     {
-      piGpioBase   = 0 ;
       usingGpioMem = TRUE ;
     }
     else

@liffiton
Copy link
Member Author

Great! There may be other pitfalls lurking, so feel free to open other issues if any arise, of course.

And while it's possible that removing the piGpioBase = 0 line there is the right call in general, I'm not confident enough to commit that myself. So I'll leave this open to help others find a potential solution, at least.

@liffiton
Copy link
Member Author

As I read the code further, I see how piGpioBase = 0 could make sense. If that is set, the code will also be using the file descriptor fd that opened the /dev/gpiomem file when it calls mmap later. It is reasonable that a base address of 0 within that file would be the right thing to do. It depends on how /dev/gpiomem works...

The discussion and code linked here suggests the offset doesn't matter and is in fact ignored. But then why did changing the offset make a difference for you? My guess is that it may be another kernel difference between Debian and Raspberry Pi OS.

That discussion and the kernel code linked, combined with your result, suggests more strongly that leaving piGpioBase unchanged when using /dev/gpiomem is generally the right thing to do. I'm still not certain, though. 😄

@Xrockyman
Copy link

Tried to add iomem=relaxed to /boot/firmware/cmdline.txt as suggested in the Debian Wiki and running as root but it didn't make any difference.

In my case (Debian GNU/Linux 12 (bookworm)) I had the same problem and adding iomem=relaxed to /boot/firmware/cmdline.txt solved the problem. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants