Pinephone Pro GPS,Camera
2023-04-02
I recently posted about getting Gentoo running on my Pinephone Pro with Pinephone Keyboard. In that post I had gotten this device to pretty much act like a laptop. I had pretty much every piece of software I have on my laptop up and running on my pinephone, under sway using wayland without Xwayland.
Since then I've been working to get GPS, and then the Camera to work. And I now have both working at a beta level at least. Time will tell how stable my efforts will be. Note that the GPS setup is also largely a prerequisite for using the cellphone modem, though I'm not interested in paying for service at this time, so I haven't tried to get that working.
GPS
I'm going to start with the GPS. Specifically my goal was to get offline puremaps to work with GPS input. gpsd has existed for ages, but modern Linux phone OSes largely use geoclue as their input. Geoclue can determine your location using a GPS, but also by looking up wifi access points and cellphone towers in a database, you can use either the database owned by Mozilla or the one owned by Google. Both have privacy implications. You can also *just* use GPS if you like.
Both the pinephone and pinephone pro use an eg5 modem for talking to cell towers. This modem also embeds a GPS unit as well, so we need to get communciation with the eg25 modem working to use the GPS. A lot of documentation I found about the eg25 modem claims that if you don't see the eg25 modem on your USB bus, the modem is faulty. This is false. There are in fact other reasons the mdoem might not appear.
You'll also need a SIM card for the modem. It doesn't need to be recent or active, any SIM card should o it. It's actually possible to get the GPS to turn on, get a lock, and spit out GPS data without the SIM using AT commands, but it's not possible to get geoclue to talk to it, because it'll only talk to the modem if the modem is "enabled", which requires the SIM card. So, my advice is jsut drop a SIM card in and move on.
-
Step 1) Switch to systemd
Geoclue as a service is designed to autostart when something talks to it, and stop again when it's done. This is a really useful feature on a phone, but it requires systemd. For this reason I decided to switch over to systemd (despite my preference for OpenRC). I did this by simply switching my system profile, checking my usef flags, doing rebuild of @system with --newuse, and then doing one of @world. Maybe there's a safer/better way, I don't know, but it worked. Obviously if your system crashes part way through this process it probably won't boot again. If that happens remember our qemu setup that lets us chroot, and the ability to mount the phone as an external drive. If you can't do that boot into mobian off an external SSD-card and chroot in.
I found this broke my pipewire setup, which I didn't realize until later. So make sure to check that things work after doing the switchover. Systemd automatically starts your dbus session on login, so you don't need to do that anymore, and I found gentoo-pipewire-launcher disappeared, so I started the 3 services manually via systemctl in my sway config (it's possible to get 2 of these service to start automatically, like geoclue, but I haven't gotten that working yet).
-
Step 2) Make sure your kernel has the right configs
I couldn't get things working on my kernel at first. Instead I got GPS working on the 6.1 mobian kernel first, then came back and built the right options into my kernel. I had trouble finding exactly what I needed, so I'll attach a kernel config. The short short version though is these modules:
- option
- usbserial
- usb_wwan
- mii
At this point if you run `lsusb` you probaby still won't see the the device. `lsusb -v` won't help either. Manually adding the modules might work, I haven't tried that. Anyway, it's fine because we need some other stuff first anyway.
-
Step 3) Install eg25-manager AND ModemManager
eg25-manager should be available in the bingch overlay. install it, and set it to start by default
systemctl enable eg25-manager
Install ModemManager as well, but you don't need to enable it, it'll be started on demand by eg25-manager anyway.
Make the sure the SIM card is in, and start the eg25-manager (or reboot)
systemctl start eg25-manager
A few seconds, maybe a minute, after eg25-manager starts lsusb should start showing the modem.
If you want to use `mmcli` commands while `ModemManager` is running you may need to add `--debug` to ModemManager. You can do this by adding it right after the command in `/lib/systemd/system/ModemManager.service`. Don't forget to `systemctl daemon-reload` to load the change into systemd and then `systemctl restart ModemManager` to update the service with the change.
At this point you can talk to the modem and do all sorts of interesting things. Here's some useful commands:
- `mmcli -L` # list modems
- `mmcli -m any --enable` # enable the modem
- `echo at+qgps=1 | sudo atinout - /dev/ttyUSB2 -` # turn on GNSS (GPS)
- `echo at+qgpsgnmea=\"gsv\" | sudo atinout - /dev/ttyUSB2 -` # show satellites tracked
- `echo at+qgpsgnmea=\"gga\" | sudo atinout - /dev/ttyUSB2 -` # show the location if we have a fix
- `echo at+qgpsend | sudo atinout - /dev/ttyUSB2 -` # turn off GNSS (GPS)
For the AT commands (the ones starting with echo) this list of errorcode may be helpful
- 501 Invalid parameter(s)
- 502 Operation not supported
- 503 GNSS subsystem busy
- 504 Session is ongoing
- 505 Session not active
- 506 Operation timeout
- 507 Function not enabled
- 508 Time information error
- 512 Validity time is out of range
- 513 Internal resource error
- 514 GNSS locked
- 515 End by E911
- 516 Not fixed now
- 517 CMUX port is not opened
- 549 Unknown error
With the above information you can enable the GPS, make sure you're in an open area, and watch as it finds satelites and gets a position lock. You have GPS!
If you have trouble getting these steps to work don't forget to check the logs of both the ModemManager and the eg25-manager. e.g. `journalctl -u eg25-manager`. At one point while debugging I had 3 terminals open following the 2 modem managers and geoclue as I tried things. Logs are your friend.
-
Step 4) Install, configure, and test geoclue
Install geoclue as you'd install any other software. I needed 2.7.0 to be able to disable other sources, the disabling seems to be somewhat broken in previous versions. The logging in 2.7.0 is also way more verbose and useful than 2.6.0, which I also tried.
Edit /etc/geoclue.conf. I disabled everything BUT GPS. If you do this it will still perform the IP lookup if there's no GPS psotion, but at least that's all it does making it easier to see if things are working. Restart geoclue with systemctl.
Make sure the modem is enabled, and GPS is on using the commands from the previous step. Wait for GPS to get a lock. Then run
/usr/libexec/geoclue-2.0/demos/agent & /usr/libexec/geoclue-2.0/demos/where-am-i
Hopefully this will show you your current location. If it's showing you some other location check the geoclue logs and see what's up.
You can add `Environment="G_MESSAGES_DEBUG=Geoclue"` to `/lib/systemd/system/geoclue.service` under the `[service]` stanza to get more information out of logging. Again, don't forget to reload the config, and then restart the service as described for ModemManager.
-
Step 5) Install Puremaps and OSMScoutServer
Puremaps is a bit of a pain to install. I prefer native apps to flatpak, but at this point flatpak is definitely the best way to get puremaps. Finding how to use flatpak is easy, so look it up. You'll want to install Puremaps and OSMScoutServer.
Once those are installed start up OSMScoutServer, and download some maps so you can use maps offline. Next start up PureMaps and tell it to use OSMScoutServer for maps.
This is another reason I switched to systemd. PureMaps is smart enough to start OSMScoutServer for you, but again it's using systemd's service triggering magic, so won't work under OpenRC.
-
Step 6) get a GPS lock, start the agent and run puremaps
Puremaps requires the demo agent to be running, and performs it's communication through that agent. I start the agent as part of sway startup so it's just always there. The agent is /usr/libexec/geoclue-2.0/demos/agent. Then I'm using the following script.
#!/bin/bash set -e # enable the modem - this step is just to convince geoclue to talk to it sudo mmcli -m any --enable echo hi # sometimes the modem gets enumerated weird # enable GNSS echo at+qgps=1 | sudo atinout - /dev/ttyUSB2 - cleanup() { # stop GNSS echo at+qgpsend | sudo atinout - /dev/ttyUSB2 - # stop the modem sudo mmcli -m any --disable } trap cleanup EXIT while x=$(echo at+qgpsgnmea=\"gga\" | sudo atinout - /dev/ttyUSB2 - | grep -o ",,,,," | tail -n1) [[ "${x}" == ",,,,," ]] do echo "Waiting for GPS lock, Satellites:" echo at+qgpsgnmea=\"gsv\" | sudo atinout - /dev/ttyUSB2 - sleep 5 done echo "Got GPS lock, satellites:" echo at+qgpsgnmea=\"gsv\" | sudo atinout - /dev/ttyUSB2 - echo "Press enter to terminate" read
I run this script in a terminal and wait for it to say that it's gotten a lock. Then I start up puremaps and use it. When I'm done I can stop pure-maps and then hit enter in the terminal and it'll shut my GPS back off. This is why I say I have things at a beta level, it works, but it's not exactly 100% clean.
Camera
As of the time of writing of this post megapixels supports the pinephone camera out of the box, but not the pinephone pro camera. This will probably be rectified soon at which point you can use the "megapixels" app from the bingch overlay. For now though, you'll need to build it yourself.
-
Step 1) Make sure you have kernels support
Again, my kernel config will be posted below, but here's the relevent modules
- rockchip_isp1
- ov8858
- imx258
- v4l2_fwnod
-
Step 2) Download the megapixels branch for pinephone pro and hack it
Go to https://github.com/kgmt0/megapixels and git clone the repo to your phone. You'll want to edit the file `pine64,pinephone-pro.ini` with the changes found here: https://forum.pine64.org/showthread.php?tid=17711.
Then read the README.md in your checkout and follow the instructions to install.
Start megapixels from a shell so you can see if it errors. If you get an error about themes, you can build the theme like this:
glib-compile-schemas /usr/local/share/glib-2.0/schemas
-
Step 3) Get jpeg working
If you want your photos stored as jpeg and not raw files you'll also want to install `dcraw` and `imagemagick`. Make sure imagemagic has at least the tiff and jpeg use flags set.
After you open the megapixels app go to settings and enable the postprocess script. The script is pretty simple and comes from the megapixels repo if you need to change it (you shouldn't) I'd change it there and re-install.
Megapixels should now be able to take photos. The green tint is only in the viewfinder, the final images will be properly post-processed and should come out with pretty okay colors.
Kernel config
Here's the kernel config I'm using right now. I'm still building some modules I don't need, but as I get things working it's becoming clearer what I can just remove. pinephonepro-kernel-config-2
One last thing. As I mentioned in my previous post, you need to ensure you're not constantly logging to the internal storage or you'll burn it out. I've set up the systemd journal stuff to only log to memory. I removed virtual/logger and told portage it was installed via /etc/portage/packages.provided, so I have no logger. After trying a few other approaches I made /var/log a small tmpfs. /tmp and /var/tmp/portage are also tmpfses for similar reasons. I tried symlinks and some other tricks for /var/log and none quite worked.
Conclusion
That just about wraps it up for the pinephone pro. At this point we have almost all of the hardware capabilities of any of the main phone linux distributions, but working on Gentoo. The one thing we don't have working is the "cell" part, because right now that's not something I particularly want. But we have the eg25 modem working, so we're a good chunk of the way there.
In addition to the cell stuff there are a couple of things that still don't work.
- The Camera's on the pinephone pro are phone-style cameras, quite different from the cameras on laptops. So far no-one has gotten things like video-chat apps working with this type of camera under linux yet, but people are working on it.
- We also can't watch netflix yet due to the DRM binary blob being for x86_64. A few folks have worked around this, but I haven't talked that yet.
- I haven't gotten anbox working on it to run android apps. Systemd is required for anbox so that's yet another reason to use systemd. Most of the anbox apps I want use GPS, so I probably won't bother with this until folks have GPS working under anbox.
Otherwise though our phone can do everything (within processing power limits) that a laptop can do, AND several things only a phone can do, has a physical keyboard, and fits in a large pants pocket. This is the closest I've ever gotten to a true convergence device. There are plenty of improvements to be made, but for the first time I have a truly portable linux box that's actually *useful*.
Lastly, here's some useful resources
- Mobian location stuff: https://wiki.mobian-project.org/doku.php?id=location#software-stack
- Pinephone thread on GPS: https://forum.manjaro.org/t/pinephone-gps-gnss-working/38478
- Tons of Pinephone AT command info: https://forum.pine64.org/showthread.php?tid=14114
- Disabling Systemd logging: https://raspberrypi.stackexchange.com/questions/109422/how-to-disable-logs