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

Fix permission denied error in linuxgpio programmer #917

Merged
merged 2 commits into from
Sep 24, 2022
Merged

Fix permission denied error in linuxgpio programmer #917

merged 2 commits into from
Sep 24, 2022

Conversation

asmirnou
Copy link
Contributor

@asmirnou asmirnou commented Apr 3, 2022

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.

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 4, 2022

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.

@mcuee
Copy link
Collaborator

mcuee commented Jul 10, 2022

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
Copy link
Collaborator

mcuee commented Jul 10, 2022

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
Copy link
Collaborator

mcuee commented Jul 10, 2022

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.

@MCUdude
Copy link
Collaborator

MCUdude commented Jul 10, 2022

Moving to libgpiod may be the ultimate solution. #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?

@mcuee
Copy link
Collaborator

mcuee commented Jul 13, 2022

@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
Copy link
Collaborator

mcuee commented Aug 24, 2022

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

Copy link
Collaborator

@stefanrueger stefanrueger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is competently coded, well done.

Small comments:

         if (ret == 0) {
            break;
         } else if(...)

Whilst the code is not wrong, the else is not warranted, as break changes the flow of control. Below is arguably better readable:

  if(ret==0)
    break;

  if(...)

Using printf() a no-no. It is better to use the avrdude_message() system as it neatly considers the wanted verbosity and it prints to stderr, where all error and debug messages belong. I would change

if (retry_count > 1) {
  printf("Retrying...\n");
}

to

  if(retry_count > 1)
    avrdude_message(MSG_NOTICE2, "%s: retrying linuxgpio_dir_%s()\n", progname, i == PIN_AVR_MISO? "in": "out");

or something to that effect (untested).

@stefanrueger
Copy link
Collaborator

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 stefanrueger merged commit fe991b8 into avrdudes:main Sep 24, 2022
@asmirnou asmirnou deleted the iss45256-sysfs-gpio branch September 24, 2022 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants