11.1Sthorpej#!/bin/sh -
21.1Sthorpej#
31.1Sthorpej# $NetBSD: 99-ugen-perms-tigard,v 1.1 2024/03/30 06:42:10 thorpej Exp $
41.1Sthorpej#
51.1Sthorpej# Look for a Tigard (https://github.com/tigard-tools/tigard) debug
61.1Sthorpej# board and change the permissions to 0660.
71.1Sthorpej#
81.1Sthorpej# Written by Jason R. Thorpe, March 2024.  Public domain.
91.1Sthorpej#
101.1Sthorpej
111.1Sthorpejexport LC_ALL=C
121.1Sthorpej
131.1Sthorpejevent="$1"
141.1Sthorpejshift
151.1Sthorpejdevices=$@
161.1Sthorpej
171.1Sthorpejorig_perms=0600
181.1Sthorpejnew_perms=0660
191.1Sthorpej
201.1Sthorpejorig_group=wheel
211.1Sthorpejnew_group=wheel
221.1Sthorpej
231.1Sthorpejdevice_name=tigard
241.1Sthorpej
251.1Sthorpejis_target_device()
261.1Sthorpej{
271.1Sthorpej	local vendor_string
281.1Sthorpej	local product_string
291.1Sthorpej
301.1Sthorpej	vendor_string="$(drvctl -p $1 vendor-string)"
311.1Sthorpej	product_string="$(drvctl -p $1 product-string)"
321.1Sthorpej
331.1Sthorpej	if [ x"$vendor_string" = x"SecuringHardware.com" -a \
341.1Sthorpej	     x"$product_string" = x"Tigard V1.1" ]; then
351.1Sthorpej		echo "yes"
361.1Sthorpej		return
371.1Sthorpej	fi
381.1Sthorpej
391.1Sthorpej	echo "no"
401.1Sthorpej}
411.1Sthorpej
421.1Sthorpejset_permissions()
431.1Sthorpej{
441.1Sthorpej	if [ x$(is_target_device $1) = xyes ]; then
451.1Sthorpej		chgrp $new_group /dev/"${2}".*
461.1Sthorpej		chmod $new_perms /dev/"${2}".*
471.1Sthorpej		#
481.1Sthorpej		# We need to create a symlink here to remember
491.1Sthorpej		# the ugen device node that was used, since we
501.1Sthorpej		# can't recover it from the device name that
511.1Sthorpej		# comes from the kernel later because we get the
521.1Sthorpej		# event *after* the device is gone, and thus
531.1Sthorpej		# cannot query any properties.
541.1Sthorpej		#
551.1Sthorpej		rm -f /dev/${1}-${device_name}
561.1Sthorpej		ln -sf ${2} /dev/${1}-${device_name}
571.1Sthorpej	fi
581.1Sthorpej}
591.1Sthorpej
601.1Sthorpejrestore_permissions()
611.1Sthorpej{
621.1Sthorpej	if [ -h "/dev/${1}-${device_name}" ]; then
631.1Sthorpej		devnode=$(readlink "/dev/${1}-${device_name}")
641.1Sthorpej		if [ x"$devnode" != x ]; then
651.1Sthorpej			chmod $orig_perms /dev/"${devnode}".*
661.1Sthorpej			chgrp $orig_group /dev/"${devnode}".*
671.1Sthorpej		fi
681.1Sthorpej		rm -f "/dev/${1}-${device_name}"
691.1Sthorpej	fi
701.1Sthorpej}
711.1Sthorpej
721.1Sthorpejget_ugen_devnode()
731.1Sthorpej{
741.1Sthorpej	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
751.1Sthorpej	# namespace, we have to query an additional property to
761.1Sthorpej	# determine which one it is.
771.1Sthorpej	local ugen_unit
781.1Sthorpej
791.1Sthorpej	ugen_unit=$(drvctl -p $1 ugen-unit)
801.1Sthorpej	case "$ugen_unit" in
811.1Sthorpej	[0-9]*)
821.1Sthorpej		echo "ugen$ugen_unit"
831.1Sthorpej		;;
841.1Sthorpej	esac
851.1Sthorpej}
861.1Sthorpej
871.1Sthorpejfor device in $devices; do
881.1Sthorpej	case $device in
891.1Sthorpej	ugensa*)
901.1Sthorpej		# Ignore ugensa(4).
911.1Sthorpej		;;
921.1Sthorpej	ugen*)
931.1Sthorpej		case $event in
941.1Sthorpej		device-attach)
951.1Sthorpej			devnode=$(get_ugen_devnode $1)
961.1Sthorpej			if [ x"$devnode" != x ]; then
971.1Sthorpej				set_permissions $device $devnode
981.1Sthorpej			fi
991.1Sthorpej			;;
1001.1Sthorpej		device-detach)
1011.1Sthorpej			restore_permissions $device
1021.1Sthorpej			;;
1031.1Sthorpej		esac
1041.1Sthorpej	esac
1051.1Sthorpejdone
106