Close

USB and software

A project log for Remote computer wakeup device

A wired remote control device that can wake up a computer so the computer can wake from sleep by the time you walk to it.

karmanyaah-malhotraKarmanyaah Malhotra 07/06/2020 at 01:590 Comments

USB setup:

Disclaimer:

  1. I'm using Arch Linux but this should probably be portable to most mainstream Linux distros (Debian, Ubuntu, Fedora, etc).
  2. I am using an external USB hub because it stays docked on my desk and I don't trust my soldering enough to plug the Digispark directly into my computer.

What I tried:

I thought it would be as simple as asking the Digispark keyboard library to type something when the button press was detected.
That didn't end up working out because the Digispark keyboard library doesn't have ( as I discovered later after a lot of wasted time and effort) a wake-up flag which means the Linux kernel never considers it to be a device that can wake up the computer.

After that, I tried the DigiCDC library, which can emulate (from what I understand) more low-level USB functionality. I realized that I could just start a serial connection with the SerialUSB.begin() command, and it would (from what I understand) basically act as if a device was just plugged in. This seemed perfect because a device being plugged in seemed exactly like the sort of thing that the kernel would consider waking up the computer for.

After figuring out what wake-up flags were (the capibality of a device to wake up a computer or not) I realized that my USB hub that enabled and it issued a wake-up request when a USB device was plugged in. Perfect! So any time a button was pressed, I issued these commands

SerialUSB.begin();
SerialUSB.delay(6000);
SerialUSB.end();

Looking at dmesg with `dmesg -w`, it seemed like the device was being unplugged each time this was happening. After that,

I sent

echo enabled > /sys/bus/usb/devices/1-3/power/wakeup

I had previously figured out that 1-3 was my usb hub by looking at lsusb and seeing

Bus 004 Device 004: ID 05e3:0749 Genesys Logic, Inc. 
Bus 004 Device 003: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Bus 004 Device 002: ID 05e3:0626 Genesys Logic, Inc. USB3.1 Hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 007: ID 8087:0a2b Intel Corp. 
Bus 001 Device 006: ID 06cb:0081 Synaptics, Inc. 
Bus 001 Device 004: ID 04f2:b61e Chicony Electronics Co., Ltd Integrated Camera
Bus 001 Device 032: ID 046d:c534 Logitech, Inc. Unifying Receiver
Bus 001 Device 038: ID 16d0:087e MCS USB2.1 Hub
Bus 001 Device 030: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I figured that MCS USB was my Digispark because that showed up every time the Digispark's SerialUSB was activated. Also, the Logitech reciever was the only other thing plugged into that particular hub so it made sense that they would be devices 30 and 3[1-9]. Then, I had to investigate a bit to find out how to relate the numbers in /sys to these ID's but it worked out by `grep . /sys/bus/usb/devices/*/idProduct`.

I set up a systemd action to do this on each boot,

And it works!!!!!!

.

.

.

Not so easily, I realized after a few hours and my USB hub being unplugged once for unrelated reasons, each time the USB hub is unplugged, these settings reset themselves. Thankfully though, on my adventures across the internet search for what would end up being the wake-up flag, I had learned about udev. Basically, udev is a utility that does stuff when any peripheral is plugged in (read the Arch wiki page for more info). So, I set up a udev command that whenever the usb device '05e3:0610' is plugged in,

ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", ATTRS{idVendor}=="05e3", ATTRS{idProduct}=="0610",  

udev sets its wake-up attribute to enabled.

  ATTR{power/wakeup}="enabled"

And it works!!!! (As of writing this I haven't rebooted my computer after making this change so I don't know if it will persist yet. In principle this should be pretty robust.)

Discussions