#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

ACTION=="remove", GOTO="seat_end"

TAG=="uaccess", SUBSYSTEM!="sound", TAG+="seat"
SUBSYSTEM=="sound", KERNEL=="card*", TAG+="seat"
SUBSYSTEM=="input", KERNEL=="input*", TAG+="seat"
SUBSYSTEM=="graphics", KERNEL=="fb[0-9]*", TAG+="seat"

# Assign keyboard and LCD backlights to the seat
SUBSYSTEM=="leds", TAG+="seat"
SUBSYSTEM=="backlight", TAG+="seat"

# Allow efifb / uvesafb to be a master if KMS is disabled
SUBSYSTEM=="graphics", KERNEL=="fb[0-9]", IMPORT{cmdline}="nomodeset", TAG+="master-of-seat"

# Allow any PCI graphics device to be a master and synthesize a seat if KMS
# is disabled and the kernel doesn't have a driver that would work with this device.
SUBSYSTEM=="pci", ENV{ID_PCI_CLASS_FROM_DATABASE}=="Display controller", \
                  ENV{DRIVER}=="", IMPORT{cmdline}="nomodeset", TAG+="seat", TAG+="master-of-seat"

# Synthesize a seat for graphic devices without DRM and that fall back to fb
# device instead. Such HWs are listed in hwdb.
SUBSYSTEM=="graphics", KERNEL=="fb[0-9]*", ATTRS{modalias}=="?*", IMPORT{builtin}="hwdb fb:$attr{modalias}"
ENV{ID_TAG_MASTER_OF_SEAT}=="1", TAG+="master-of-seat"

SUBSYSTEM=="drm", KERNEL=="card[0-9]*", TAG+="seat", TAG+="master-of-seat"

# Allow individual USB ports to be assigned to a seat
SUBSYSTEM=="usb", ATTR{bDeviceClass}=="00", TAG+="seat"

# Allow USB hubs (and all downstream ports) to be assigned to a seat
SUBSYSTEM=="usb", ATTR{bDeviceClass}=="09", TAG+="seat"

# 'Plugable' USB hub, sound, network, graphics adapter
SUBSYSTEM=="usb", ATTR{idVendor}=="2230", ATTR{idProduct}=="000[13]", ENV{ID_AUTOSEAT}="1"

# qemu (version 2.4+) has a PCI-PCI bridge (-device pci-bridge-seat) to group
# devices belonging to one seat. See:
#     http://git.qemu.org/?p=qemu.git;a=blob;f=docs/multiseat.txt
SUBSYSTEM=="pci", ATTR{vendor}=="0x1b36", ATTR{device}=="0x000a", TAG+="seat", ENV{ID_AUTOSEAT}="1"

# Mimo 720, with integrated USB hub, displaylink graphics, and e2i
# touchscreen. This device carries no proper VID/PID in the USB hub,
# but it does carry good ID data in the graphics component, hence we
# check it from the parent. There's a bit of a race here however,
# given that the child devices might not exist yet at the time this
# rule is executed. To work around this we'll trigger the parent from
# the child if we notice that the parent wasn't recognized yet.

# Match parent
{% raw -%}
SUBSYSTEM=="usb", ATTR{idVendor}=="058f", ATTR{idProduct}=="6254", \
                  ATTR{%k.2/idVendor}=="17e9", ATTR{%k.2/idProduct}=="401a", ATTR{%k.2/product}=="mimo inc", \
                  ENV{ID_AUTOSEAT}="1", ENV{ID_AVOID_LOOP}="1"
{% endraw %}

# Match child, look for parent's ID_AVOID_LOOP
SUBSYSTEM=="usb", ATTR{idVendor}=="17e9", ATTR{idProduct}=="401a", ATTR{product}=="mimo inc", \
                  ATTR{../idVendor}=="058f", ATTR{../idProduct}=="6254", \
                  IMPORT{parent}="ID_AVOID_LOOP"

# Match child, retrigger parent
SUBSYSTEM=="usb", ATTR{idVendor}=="17e9", ATTR{idProduct}=="401a", ATTR{product}=="mimo inc", \
                  ATTR{../idVendor}=="058f", ATTR{../idProduct}=="6254", \
                  ENV{ID_AVOID_LOOP}=="", \
                  RUN+="{{BINDIR}}/udevadm trigger --parent-match=%p/.."

TAG=="seat", ENV{ID_PATH}=="", IMPORT{builtin}="path_id"
TAG=="seat", ENV{ID_FOR_SEAT}=="", ENV{ID_PATH_TAG}!="", ENV{ID_FOR_SEAT}="$env{SUBSYSTEM}-$env{ID_PATH_TAG}"

SUBSYSTEM=="input", ATTR{name}=="Wiebetech LLC Wiebetech", RUN+="{{BINDIR}}/loginctl lock-sessions"

LABEL="seat_end"
