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

Support new GPIO character device interface #157

Open
pdp7 opened this issue Aug 23, 2017 · 14 comments
Open

Support new GPIO character device interface #157

pdp7 opened this issue Aug 23, 2017 · 14 comments

Comments

@pdp7
Copy link
Collaborator

pdp7 commented Aug 23, 2017

Linux kernel gpio subsystem maintainer Linus Walleij presented GPIO descriptors in this presentation:

Linus W. details kernel-external API changes:

  • /sys/class/gpio sysfs interface is marked as deprecated
  • new character device /sys/bus/gpiochipN or /dev/gpiochipN

Linus W. emphasized these points:

The Rules of Linux Userspace GPIO

  1. You do not access GPIOs from userspace
  2. YOU DO NOT ACCESS GPIOS FROM USERSPACE
  3. Read Documentation/gpio/drivers-on-gpio.txt
  4. Use the character device

Features the Character Device Has

  • Discovery mechanism (not magic numbers)
  • Cleanup of resources on closing or crashing
  • Open Drain / Open Source
  • Get and set multiple lines at once
  • Good examples: tools/gpio/*Features the Character Device Has
  • Discovery mechanism (not magic numbers)
  • Cleanup of resources on closing or crashing
  • Open Drain / Open Source
  • Get and set multiple lines at once
  • Good examples: tools/gpio/*
@pdp7 pdp7 self-assigned this Aug 23, 2017
@pdp7 pdp7 changed the title Support new GPIO character device interfce Support new GPIO character device interface Aug 23, 2017
@pdp7
Copy link
Collaborator Author

pdp7 commented Sep 11, 2017

Information on when these gpiolib features were merged into the kernel:

gpio: add a userspace chardev ABI for GPIOs

  • commit `3c702e9987e26
  • [GIT PULL] GPIO bulk changes for kernel v4.6 (Mar 17, 2016)
  • ioctl
    • GPIO_GET_CHIPINFO_IOCTL

gpio: add userspace ABI for GPIO line information

  • commit 521a2ad6f862a
  • [GIT PULL] GPIO bulk changes for kernel v4.6 (Mar 17, 2016)
  • ioctl
    • GPIO_GET_LINEINFO_IOCTL

gpio: userspace ABI for reading/writing GPIO lines

  • commit: d7c51b47ac11e
  • [GIT PULL] bulk GPIO changes for v4.8 (Jul 25, 2016)
  • ioctl
    • GPIOHANDLE_GET_LINE_VALUES_IOCTL
    • GPIOHANDLE_SET_LINE_VALUES_IOCTL
    • GPIO_GET_LINEHANDLE_IOCTL

gpio: userspace ABI for reading GPIO line events

  • commit 61f922db72216
  • [GIT PULL] bulk GPIO changes for v4.8 (Jul 25, 2016)
  • ioctl
    • GPIOHANDLE_GET_LINE_VALUES_IOCTL
    • GPIO_GET_LINEEVENT_IOCTL

@pdp7
Copy link
Collaborator Author

pdp7 commented Dec 2, 2017

lsgpio utlitiy uses these new ioctl()'s to list all lines for each gpiochip:

debian@beaglebone:~/gpio$ sudo ./lsgpio 
Linus W GPIO chip: gpiochip3, "gpio", 32 GPIO lines
	line  0: unnamed unused
	line  1: unnamed unused
<snip>
	line 30: unnamed "P9_11" [kernel]
	line 31: unnamed "P9_13" [kernel]

system calls that lsgpio uses:

debian@beaglebone:~/gpio$ sudo strace -e open,stat,read,write ./lsgpio >/dev/null
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
open("/dev/gpiochip3", O_RDONLY)        = 4
open("/dev/gpiochip2", O_RDONLY)        = 4
open("/dev/gpiochip1", O_RDONLY)        = 4
open("/dev/gpiochip0", O_RDONLY)        = 4
debian@beaglebone:~/gpio$ sudo strace -e open,ioctl ./lsgpio >/dev/null 
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
open("/dev/gpiochip3", O_RDONLY)        = 4
ioctl(4, GPIO_GET_CHIPINFO_IOCTL, 0xbeff6618) = 0
ioctl(4, GPIO_GET_LINEINFO_IOCTL, 0xbeff65cc) = 0
<snip>

@pdp7
Copy link
Collaborator Author

pdp7 commented Feb 8, 2018

Bartosz Golaszewski created libgpiod to make it easier to use the new kernel GPIO API:
New GPIO interface for user space

The repo for libgpiod is hosted here:
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/

Examples of the libgpiod API:

bindings: python #5
@brgl writes:

Seems like ctypes is the way to go and it should be quite easy to implement.

@pdp7
Copy link
Collaborator Author

pdp7 commented Feb 8, 2018

Update on Python bindings for libgpiod by @brgl:

Yes, I was recently contacted by @sgjava who showed me his automatically generated ctypes bindings.

You can find them here.

However I plan to write object oriented python 3 bindings (using C extensions) right after I finish the C++ bindings.

There were some issues raised on the linux-gpio mailing list about the safety of ctypes for public structures with different memory layout on 32 and 64 bit architectures.

Please note that I just released v1.0 of libgpiod with an improved API which shall remain stable from now on.

libgpiod-extra by @sgjava

Automated build, Python and Java bindings for libgpiod

Python bindings
ctypesgen is used to create a Python bindings from libgpiod's gpiod.h C header file. I've added an insert file to add the missing ctypes Structures.

@pdp7
Copy link
Collaborator Author

pdp7 commented Feb 21, 2018

From brgl/libgpiod#5 (comment)
@sgjava has created userspaceio with Python 3 bindings using CFFI:

Python and Java bindings for GPIO, SPI, I2C and Serial interfaces in Linux user space

@pdp7
Copy link
Collaborator Author

pdp7 commented Mar 20, 2018

bookwar/python-gpiodev

Library to operate GPIO pins via character device

GPIOHandle for character device

New GPIO interface has been introduced recently.

It exposes GPIO interface as /dev/gpiochip0 character device and provides several ioctl
syscalls
for bulk operations on sets of GPIO pins.

In gpiodev/src/gpioctl.c we wrap the ioctl calls into C-functions suitable for later use.

In gpiodev/gpio.py the ctypes bindings created and then used to define the main GPIOHandle class.

Check examples for usage.

@silver2row
Copy link

Hello Sir,

I am a novice w/ all this info. but if you need support, I will take time out to look over things and test stuff.

Seth

P.S. If you want to direct me as to what is important right now versus what can wait, I can make some notes and get back to you.

@silver2row
Copy link

@pdp7 ,

Hello Sir...I have been reading and trying to catch up to what you may already know about these instances listed above.

...

https://github.com/torvalds/linux/blob/master/Documentation/driver-api/gpio/drivers-on-gpio.rst.

This is what I found recently in Linux. I have been watching the YouTube.com video on gpio and how we are not supposed to call it from UserSpace. I am almost done w/ the video.

Seth

P.S. I will continue my search on these subjects. Are you still wanting assistance for any particular subject matter?

@silver2row
Copy link

silver2row commented Sep 3, 2018

Hello,

I still have the /sys/class/gpio (legacy) system and the new character device system for gpio at /dev.

Seth

P.S. I will work on it. Be back later in the year or hopefully, sooner.

@silver2row
Copy link

Okay and Hello,

I found the userspaceio GitHub.com page. This is good motion in action. I found the fellow that did a presentation on Linux Embedded Conference, too. He has a GitHub.com page for libgpiod.

Seth

P.S. I was able to get my LED lit and turned off by the character device system.

@pdp7
Copy link
Collaborator Author

pdp7 commented Sep 6, 2018

@silver2row check out libgpiod repo and bindings: python #5.

There are comments in there by @sgjava

@silver2row
Copy link

@pdp7

Hello Again Sir...Look here: https://www.hackster.io/silver2row/the-new-character-device-idea-0968d6. I made a "funny" set up of making a LED blink on and after the return button has been pressed, the LED turns off when the pin is high w/ gpioset --mode=wait gpiochip1 28=0 for P9_12.

Seth

P.S. I thought this was fascinating. Now, I need to read more as usual before I can get more into things. So, let me know if you need support. I am actually building a CNC for fun, just for posting, but I will have time to read more. Just show me the way and I can start on ideas.

@pdp7
Copy link
Collaborator Author

pdp7 commented Sep 10, 2018

@silver2row thanks, that is very interesting

@silver2row
Copy link

@pdp7 ,

No issue, sir. I thought you would have liked that idea.

Seth

P.S. I know you may not know everything on this subject but neither do I. If I can ever catch up to what you have read already, I may be able to help. I will keep searching online for ideas and issues pertaining to character device support. So, just let me know if you have some nice docs. on this subject.

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

2 participants