# README-udev
# File last changed: Feb 23, 2021

Various articles and documentation:

- Writing udev rules
  by Daniel Drake (dsd)
  http://www.reactivated.net/writing_udev_rules.html

- udev - Linux dynamic device management
  by Debian Wiki team
  https://wiki.debian.org/udev

- udev - Dynamic device management
  by freedesktop.org
  https://www.freedesktop.org/software/systemd/man/udev.html

- Persistent names for usb-serial devices
  by Michael Ludvig - HintShop
  http://hintshop.ludvig.co.nz/show/persistent-names-usb-serial-devices/

- udev rule for assigning known symlinks for identical usb serial devices
  https://unix.stackexchange.com/questions/105144/udev-rule-for-assigning-known-symlinks-for-identical-usb-serial-devices
  by Unix & Linux Stack Exchange

- Udev rule: ACTION=="add" is working, but ACTION=="remove"” isn't working!
  https://unix.stackexchange.com/questions/178341/udev-rule-action-add-is-working-but-action-remove-isnt-working
  by Unix & Linux Stack Exchange

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Dealing with the ModemManager by the udev rules.

ENV{ID_MM_DEVICE_IGNORE}="1" in the udev rules accelerates the overall
system startup - disables device port probing by the ModemManager.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

56K USB Modem (Conexant), the CTI Comet USB (FT232RL) and
the Holtek HT9032D based PSTN Caller ID module (pl2303) are already covered with:
/usr/lib/udev/rules.d/70-persistent-modem-usb-name.rules
/usr/lib/udev/rules.d/70-persistent-comet-usb-name.rules
/usr/lib/udev/rules.d/70-persistent-holtek-usb-name.rules

If different devices and types of device connections are used,
such as with 56K Serial modem or the CTI Comet Serial,
create an appropriate udev rules for these particular devices, where and when needed.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

= Example 1: 56K Serial modem to Serial port =

56K Serial modem is connected to the 1st Serial port (onboard) - ttyS0 aka COM1:
# dmesg -t | grep ttyS0
00:04: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A

Check the current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyS0 | grep MM
ID_MM_CANDIDATE=1

Which means that the device is set for probing by the ModemManager.

Create a probe exclusion rule, by a certain attribute of the device:
# udevadm info -a /sys/class/tty/ttyS0 | grep id
    ATTRS{id}=="PNP0501"

The udev rule will be enhanced by defining a custom, user-friendly name for the device,
moreover, the predictable device name by the auxiliary script.

The rules look like this:
# cat /etc/udev/rules.d/70-persistent-modem-serial-name.rules 
ATTRS{id}=="PNP0501", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="tty", ATTRS{id}=="PNP0501", PROGRAM="/usr/share/ncid/sys/udev-name ModemSerial", SYMLINK+="%c"

In the ncidd configuration file  - /etc/ncid/ncidd.conf, add the following line:
set ttyport = /dev/ModemSerial0

Reboot.

After reboot, check again now the new current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyS0 | grep MM
ID_MM_CANDIDATE=1
ID_MM_DEVICE_IGNORE=1

Which means that the device is excluded from probing by the ModemManager.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

= Example 2: CTI Comet Serial to Serial port =

The CTI Comet Serial is connected to the 1st Serial port (onboard) - ttyS0 aka COM1:
# dmesg -t | grep ttyS0
00:04: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A

Check the current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyS0 | grep MM
ID_MM_CANDIDATE=1

Which means that the device is set for probing by the ModemManager.

Create a probe exclusion rule, by a certain attribute of the device:
# udevadm info -a /sys/class/tty/ttyS0 | grep id
    ATTRS{id}=="PNP0501"

The udev rule will be enhanced by defining a custom, user-friendly name for the device,
moreover, the predictable device name by the auxiliary script.

The rules look like this:
# cat /etc/udev/rules.d/70-persistent-comet-serial-name.rules 
ATTRS{id}=="PNP0501", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="tty", ATTRS{id}=="PNP0501", PROGRAM="/usr/share/ncid/sys/udev-name CometSerial", SYMLINK+="%c"

In the xdmf2ncid configuration file - /etc/ncid/xdmf2ncid.conf, add the following line:
usbport = /dev/CometSerial0

Reboot.

After reboot, check again now the new current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyS0 | grep MM
ID_MM_CANDIDATE=1
ID_MM_DEVICE_IGNORE=1

Which means that the device is excluded from probing by the ModemManager.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

If available, one of these ubiquitous Prolific USB to Serial adapters:

# dmesg -t
usb 4-4: new full-speed USB device number 3 using ohci-pci
usb 4-4: New USB device found, idVendor=067b, idProduct=2303
usb 4-4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
usb 4-4: Product: USB-Serial Controller D
usb 4-4: Manufacturer: Prolific Technology Inc. 
usbcore: registered new interface driver pl2303
usbserial: USB Serial support registered for pl2303
pl2303 4-4:1.0: pl2303 converter detected
usb 4-4: pl2303 converter now attached to ttyUSB1

you can try the following examples.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

= Example 3: 56K Serial modem to USB-Serial port =

56K Serial modem is connected to the USB-Serial port - ttyUSB1:

Check the current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep MM
ID_MM_CANDIDATE=1

Which means that the device is set for probing by the ModemManager.

Create a probe exclusion rule, by a certain attribute of the device:
# udevadm info -a /sys/class/tty/ttyUSB1 | grep -m 1 product
    ATTRS{product}=="USB-Serial Controller D"

The udev rule will be enhanced by defining a custom, user-friendly name for the device,
moreover, the predictable device name by the auxiliary script.

The rules look like this:
# cat /etc/udev/rules.d/70-persistent-modem-usbserial-name.rules 
ATTRS{product}=="USB-Serial Controller D", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="tty", ATTRS{product}=="USB-Serial Controller D", PROGRAM="/usr/share/ncid/sys/udev-name ModemUSBSerial", SYMLINK+="%c"

In the ncidd configuration file - /etc/ncid/ncidd.conf, add the following line:
set ttyport = /dev/ModemUSBSerial0

Reboot.

After reboot, check again now the new current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep MM
ID_MM_CANDIDATE=1
ID_MM_DEVICE_IGNORE=1

Which means that the device is excluded from probing by the ModemManager.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

= Example 4: CTI Comet Serial to USB-Serial port =

The CTI Comet Serial is connected to the USB-Serial port - ttyUSB1:

Check the current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep MM
ID_MM_CANDIDATE=1

Which means that the device is set for probing by the ModemManager.

Create a probe exclusion rule, by a certain attribute of the device:
# udevadm info -a /sys/class/tty/ttyUSB1 | grep -m 1 product
    ATTRS{product}=="USB-Serial Controller D"

The udev rule will be enhanced by defining a custom, user-friendly name for the device,
moreover, the predictable device name by the auxiliary script.

The rules look like this:
# cat /etc/udev/rules.d/70-persistent-comet-usbserial-name.rules 
ATTRS{product}=="USB-Serial Controller D", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="tty", ATTRS{product}=="USB-Serial Controller D", PROGRAM="/usr/share/ncid/sys/udev-name CometUSBSerial", SYMLINK+="%c"

In the xdmf2ncid configuration file - /etc/ncid/xdmf2ncid.conf, add the following line:
usbport = /dev/CometUSBSerial0

Reboot.

After reboot, check again now the new current status which relates to device port probing by the ModemManager:
# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep MM
ID_MM_CANDIDATE=1
ID_MM_DEVICE_IGNORE=1

Which means that the device is excluded from probing by the ModemManager.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Note:
If necessary, multiple device attributes can be combined,
for example, with the Prolific USB to Serial adapter:

# udevadm info -a /sys/class/tty/ttyUSB1
    ATTRS{idProduct}=="2303"
    ATTRS{idVendor}=="067b"
    ATTRS{manufacturer}=="Prolific Technology Inc. "
    ATTRS{product}=="USB-Serial Controller D"

the udev rules may contain:
 ATTRS{idProduct}=="2303", ATTRS{idVendor}=="067b", ATTRS{manufacturer}=="Prolific Technology Inc. ", etc.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Dealing with the GPS (Global Positioning System) Daemon.

It appears that the GPS Daemon does not have a mechanism similar to that of the ModemManager,
to filter device probing, so udev rule for gps, for the particular device, should be banned.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Prolific USB to Serial adapter:
# dmesg -t | grep ttyUSB1
usb 4-4: pl2303 converter now attached to ttyUSB1

Check the current status which relates to device port probing by the GPS Daemon:
# file /dev/gps1
/dev/gps1: symbolic link to ttyUSB1

# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep SYSTEMD_WANTS
SYSTEMD_WANTS=gpsdctl@ttyUSB1.service

# journalctl -b -u gpsdctl@ttyUSB1.service
systemd[1]: Dependency failed for Manage ttyUSB1 for GPS daemon.
systemd[1]: gpsdctl@ttyUSB1.service: Job gpsdctl@ttyUSB1.service/start failed with result 'dependency'.

Which means that the device is set for probing by the GPS Daemon.

The rule can be overridden by copying it to the /etc/udev/rules.d/ and then by modifying it:
# cp /usr/lib/udev/rules.d/99-gpsd.rules /etc/udev/rules.d/

Comment the line that is located after the line with:
# Prolific Technology, Inc. PL2303 Serial Port [linux module: pl2303]
so the particular rule for the particular device looks like this:
# cat /etc/udev/rules.d/99-gpsd.rules 
#ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SYMLINK+="gps%n", TAG+="systemd", ENV{SYSTEMD_WANTS}="gpsdctl@%k.service"

Reboot.

Prolific USB to Serial adapter:
# dmesg -t | grep ttyUSB1
usb 4-4: pl2303 converter now attached to ttyUSB1

After reboot, check again now the new current status which relates to device port probing by the GPS Daemon:
# file /dev/gps1
/dev/gps1: cannot open `/dev/gps1' (No such file or directory)

# udevadm info -a -q env /sys/class/tty/ttyUSB1 | grep SYSTEMD_WANTS

# journalctl -b -u gpsdctl@ttyUSB1.service 
-- No entries --

Which means that the device is excluded from probing by the GPS Daemon.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

