avrdude icon indicating copy to clipboard operation
avrdude copied to clipboard

Fix permission denied error in linuxgpio programmer

Open asmirnou opened this issue 2 years ago • 6 comments

On Raspberry Pi using linuxgpio programmer without sudo fails with

Can't open gpioX/direction: Permission denied

The next attempt fails, because gpio pin remains exported and not removed.

Open issue: #386

Solution

After exporting gpio pin wait, until GPIO directory appears. Retry in case of EACCESS error while delayed udev permission rule applies. Call unexport, if export failed for whatever reason.

Verified that it fixes the problem.

asmirnou avatar Apr 03 '22 19:04 asmirnou

Thanks for the PR! I'm a pretty recent contributor, so @dl8dtl would have to have a look at this one.

When searching for linuxgpio specific Avrdude issues it looks like there are related issues we well. You mentioned #386, but it looks to me like #472 and perhaps #372 reports the same issues too.

MCUdude avatar Apr 04 '22 06:04 MCUdude

This PR seems to help to avoid the issue.

pi@raspberrypi:~/Desktop/build/avr/avrdude_pr917/build_linux/src $ ./avrdude -C ./avrdude.conf -c linuxgpio 
-P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "./blinky_m328p.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 7.75s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against ./blinky_m328p.hex:

Reading | ################################################## | 100% 6.49s

avrdude: 32768 bytes of flash verified

avrdude done.  Thank you.


mcuee avatar Jul 10 '22 12:07 mcuee

But if the issue already happened (say using master without the fix), then it will not help.

pi@raspberrypi:~/Desktop/build/avr/avrdude/build_linux/src $ ./avrdude -C ./avrdude.conf -c linuxgpio 
-P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't open gpioX/direction: Permission denied
avrdude: opening programmer "linuxgpio" on port "GPIO" failed

avrdude done.  Thank you.

pi@raspberrypi:~/Desktop/build/avr/avrdude/build_linux/src $ sudo ./avrdude -C ./avrdude.conf 
-c linuxgpio -P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't export GPIO 25, already exported/busy?: Device or resource busy
avrdude: opening programmer "linuxgpio" on port "GPIO" failed

avrdude done.  Thank you.

In this case, PR #917 does not seem to be able to help any more.

pi@raspberrypi:~/Desktop/build/avr/avrdude_pr917/build_linux/src $ sudo ./avrdude -C ./avrdude.conf 
-c linuxgpio -P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't export GPIO 25, already exported/busy?: Device or resource busy 
(Note: BTW, there is a missing return here.)
avrdude: opening programmer "linuxgpio" on port "GPIO" failed

avrdude done.  Thank you.

mcuee avatar Jul 10 '22 12:07 mcuee

All in all, I think this PR is good to be merged. It at least helps to avoid the issue in the first place.

Moving to libgpiod may be the ultimate solution.

  • https://github.com/avrdudes/avrdude/issues/831

mcuee avatar Jul 10 '22 12:07 mcuee

Moving to libgpiod may be the ultimate solution. https://github.com/avrdudes/avrdude/issues/831

Absolutely! Now that libgpiod is a part of the linux kernel, it looks like libgpiod is available under /linux/gpio.h, which is how linuxspi interacts with the GPIO it uses as reset pin.

But I have no idea how to port the existing linuxgpio code over to libgpiod. Maybe the person who write linuxgpio in the first place would be interested to look into it?

MCUdude avatar Jul 10 '22 17:07 MCUdude

@stefanrueger Maybe you can take a look at this one as well to see if this can be merged as it is. It seems to improve the situation of quite a few outstanding issues.

mcuee avatar Jul 13 '22 01:07 mcuee

Not so sure who can review this and get this merged (or modified if necessary). My test results show it improves the situation.

mcuee avatar Aug 24 '22 01:08 mcuee

I would be happy to merge the PR as @mcuee found it a useful improvement. The one request I have is to replace the printf() with an appropriate avrdude_message().

stefanrueger avatar Sep 21 '22 16:09 stefanrueger