How can I detect a USB port being used for charging in Linux? - linux

I'm using a USB port on the Raspberry Pi 3 Model B to charge an accessory via a USB magnetic charging cable. I want to detect when the cable is plugged in and removed from the Pi.
I've been plugging in and removing this cable and looking for any traces of this being detected on Linux. So far, I've tried:
monitoring udev events by udevadm monitor.
tailing the system logs in /var/log etc.
running an inotify watch on the /dev directory for new devices
looking around in /sys/bus/usb/devices for any clues
Is it possible to detect my simple charging cable being inserted/removed? By intuition I would think no (since the cable is purely for charging, and doesn't have drivers), however I observe that the RPI flashes its builtin LED red when I insert the cable.

Without significant effort (hardware or hacks), No.
The red LED flash you observe is the power monitor for the Pi indicating a sudden voltage drop as current begins to flow to your accessory. This LED indicates voltage (electrical energy) has briefly dropped below stable levels, and system instability may result.
As the connection is purely charging (no USB data exchanged with the Pi) the operating system has no idea the device is connected. You could build an external current-measuring circuit to detect the peripheral drawing power, but such a question is better off in the electronics stack exchange.

I think that you cand do it with the lsusb tools:
lsusb -v
Or Filtering with:
lsusb -v |grep 'Bus\|MaxPower'
With this command I can see how much power each USB is usign and which device is connected.

Related

Linux USB Disable USB Power Only

I've have been looking for a while and though a lot of Exchange Q&A's have helped, I haven't exactly received the answer I'm looking for.
Going off the question asked here: How can I detect a USB port being used for charging in Linux?
I felt as if it needed it's own question. I have a USB Powered Hub for charging smart phones and I wish to control the power (On/Off only) to each of the ports individually.
Using a USB charging cable, Linux cannot detect that a device is plugged in, so using a USB data cable might be more useful to this scenario.
Essentially, I want Linux to be able to detect when a device is plugged in, however I do not want any data transmission to occur between the device and Linux. Simply only power. By detecting the device being plugged in, I want to be able to control whether power will flow to the device or not, but no data transmission.
I'm currently testing with uhubctl, however when enabling/disabling the port, unfortunately it controls both power/data where I wish to cut off data entirely and only allow power to the device.
Is it possible to using a USB Data Cable detect a device plugged in, then essentially disable the phone talking to the computer entirely, but still allow power to flow to the device?
If not, the other option I was thinking is if there is a USB Power Hub out there that can tell Linux if a new device has been plugged in or not and I can tell it whether to charge or not?
Any input would be greatly appreciated.

Reading signals on ethernet port directly with RaspberryPi

I have a device which connects to its remote using rj45 port and 4 wire cable (a desk with adjustable height). I'm pretty sure it does not actually use an Ethernet protocol and uses sends some simple digital signals.
I want to Raspberry Pi to be able to control the device, so I was wondering if it is possible to read and dump and then send signals using Ethernet port just like I can do with any other pins? Probably the actual question here is about a way to bypass Ethernet driver in Linux OS.
This won't work. The Ethernet NIC on the RPi - just like any other NIC - can only receive Ethernet frames. Anything else needs to be connected over GPIO.
Edit: As NO-OP has reasonably pointed out, the signals may need to be made compatible with the GPIO pins - levels adjusted, maybe an opto-isolator here and there. Nothing expensive though usually. For controlling powered devices you likely need a driver or relais board - there are plenty around for the RPi.

Use a Linux Computer as a USB Coupler

I am on debian and:
I have a USB controller hooked up to a USB port on my PC (Device 1).
I have a male to male USB cord hooked up to another port on the PC that connects to Device 2. (it is a "bridging" usb cord, and has the chip for it)
I want to make them connect to each other as if they were one cord, so neither device knows that there is a computer in the middle.
This would be called a 'Coupler', except that I am using a PC as a coupler.
Here is a (really bad) diagram I made:
What I have done:
I have been able to connect the two devices independently of each other and sniff the results for when they fail to connect. The devices don't send a large volume of data back and forth.
Maybe there is some kind of command tool that I could use, for example (psudocode):
$ couple-usb-ports PORT1 PORT2
You're trying to reinvent the wheel here.
You might consider looking at this link instead.
http://dan3lmi.blogspot.com/2012/10/sniffing-usb-traffic-different.html
Specifically this.
Windows: You cannot directly capture raw USB traffic on Windows with Wireshark/WinPcap, but it is possible to capture and debug USB traffic on a virtual Windows machine under Oracle Virtual Box.
You cannot use a simple PC as transparent USB sniffer without extra (expensive) hardware. An USB bus has always one host (and one or more devices), and the PC can only be the host. This is a hardware limitation.
But you can capture USB data in a Windows machine using Wireshark and USBPcap, eliminating the need for the middle box in most cases.
As this post is tagged Linux, I suppose the controller PC is a Linux machine. Instead of connecting USB ports with a male-male connector, which is all kinds of bad (you are connecting the 5V lines of both machine with each other!), just run Wireshark in the controller PC.
There might be a little work to be done previously, as you have to enable Wireshark for USB monitoring (Particularly in Debian, this is disabled by default), and you might have to install a small driver to enable the monitoring. Have a look at this page for more information.
Once you get it working, Wireshark is an excellent tool for this!

Linux writing raw bytes on USB

I've got usb cable plugged to my computer, which D+ and D- pins are connected to multimeter. I want to send some raw bytes to get some voltage.. is it possible at all?
I'm 99% sure that usb port I've plugged cable in is something like /dev/bus/usb/002
I know that there was possibility to do the same with LPT or RS232 ports.
RS232 and LPT are not bus ! USB devices need to be addressed in order to become reachable.
Maybe unloading and reloading usb driver that drive your usb host... or trying to make a reset on usb hub host...
For doing this kind of operation on usb port, you have to break usb kernel driver and whipe all addressing operation to address directly the chipset...
At all, due to USB concept, I'm not sure you may successfully hold some power state on outlet.
For playing with that kind of physical IO, two solution:
Install a low-cost RS-232 <-> USB adapter
or better
Buy an Arduino micro-controller for prototyping and development.
I'm nearly 100% sure that you can't send anything down your USB lead unless you actually have a device at the other end. If you still want to play with this, get a cheap memory stick, break the casing off it [not too roughly], and measure whilst doing a large file-transfer to the memory stick, or some such.
But I'm not sure your multimeter will show much, as they tend to be a bit slow, compared to USB rates.
USB uses pull-up / pull-down resistors on the data lines to detect whether or not a port is connected (1.5k pull-up to 3.3v on the device side, 15k pull-down on the host side IIRC). The exact connection depends on the device speed.
So if you connect an appropriate resistor, the host should attempt to start signalling. Because of the data-rate, you might not be able to see that on a multimeter; an oscilloscope would be more appropriate.
If you want to by-pass the normal USB protocol and just blindly send data, I think you'll need to get your hands dirty and write code to bypass the usual device drivers and access the USB hardware directly. Even then I'm not sure what's possible - the USB hardware is a lot smarter than good ol' LPT and RS232 ports, which might get in the way of doing this sort of low level stuff.

How can I figure out which tty file points to which USB-to-Serial device?

I have two legacy machines connected to a Linux box with USB using the ftdi_sio driver, to /dev/ttyUSB0 and /dev/ttyUSB1. The Linux box is relaying and analyzing the traffic between the machines. When the Linux box boots up, the machines are connected to the files pretty much randomly. The problem is to know which one is which.
I could just ask the devices, of course, but I'd like to avoid the risk of malfunction due to sending wrong data to the wrong device. Is there a way to figure out, for example, the id of the device connected to a tty file?
Check this Using Linux USB page.
/proc/bus/usb/devices lists information about the devices currently attached to the USB bus. This is very useful when trying to figure out if the device is correctly enumerated.
Maybe you can use the output from lsusb -v and look at iProduct + iSerial to determine the order the devices are attached.

Resources