11.1Sthorpej#!/bin/sh -x
21.1Sthorpej#
31.2Sthorpej# $NetBSD: 99-ugen-perms-minipro,v 1.2 2025/09/05 23:55:54 thorpej Exp $
41.1Sthorpej#
51.1Sthorpej# Look for a "Minipro" (https://gitlab.com/DavidGriffith/minipro) compatible
61.1Sthorpej# EEPROM programmer and change 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=minipro
241.1Sthorpej
251.1Sthorpejis_target_device()
261.1Sthorpej{
271.1Sthorpej	local vendor_string
281.1Sthorpej	local product_string
291.1Sthorpej	local vendor_id
301.1Sthorpej	local product_id
311.1Sthorpej
321.1Sthorpej	#
331.1Sthorpej	# TL866A/TL866CS programmers have:
341.1Sthorpej	#
351.1Sthorpej	#	VID = 0x04d8 (1240)	# Microchip
361.1Sthorpej	#	PID = 0xe11c (57628)	# probably some PIC microcontroller
371.1Sthorpej	#
381.1Sthorpej	# XXX It's probably better to match on vendor-string / product-string
391.1Sthorpej	# in this case because of the use of the generic Microchip VID.
401.1Sthorpej	#
411.1Sthorpej	# The XGecu-branded TL866II+ devices have:
421.1Sthorpej	#
431.1Sthorpej	#	vendor-string="Xingong Electronicg Co.."
441.1Sthorpej	#	product-string="Xingong XGecu USB Prog.. Device"
451.1Sthorpej	#
461.1Sthorpej	# ...but they also have seemingly unique VID/PID (not the
471.1Sthorpej	# generic Microchip VID the older TL866A/CS programmers have):
481.1Sthorpej	#
491.1Sthorpej	#	VID = 0xa466 (42086)
501.1Sthorpej	#	PID = 0x0a53 (2643)
511.1Sthorpej	#
521.2Sthorpej	# The XGecu T48 and T56 use the same VID/PID as the TL866II+.
531.2Sthorpej	#
541.2Sthorpej	# The XGecu T76 uses:
551.2Sthorpej	#
561.2Sthorpej	#	VID = 0xa466 (42086)
571.2Sthorpej	#	PID = 0x1a86 (6790)
581.1Sthorpej	#
591.1Sthorpej
601.1Sthorpej	vendor_string="$(drvctl -p $1 vendor-string)"
611.1Sthorpej	product_string="$(drvctl -p $1 product-string)"
621.1Sthorpej	vendor_id="$(drvctl -p $1 vendor-id)"
631.1Sthorpej	product_id="$(drvctl -p $1 product-id)"
641.1Sthorpej
651.1Sthorpej	#
661.1Sthorpej	# TL866A / TL866CS
671.1Sthorpej	#
681.1Sthorpej	if [ x"$vendor_id" = x"1240" -a \
691.1Sthorpej	     x"$product_id" = x"57628" ]; then
701.1Sthorpej		echo "yes"
711.1Sthorpej		return;
721.1Sthorpej	fi
731.1Sthorpej
741.1Sthorpej	#
751.2Sthorpej	# TL866II+ / T48 / T56
761.1Sthorpej	#
771.1Sthorpej	if [ x"$vendor_id" = x"42086" -a \
781.1Sthorpej	     x"$product_id" = x"2643" ]; then
791.1Sthorpej		echo "yes"
801.1Sthorpej		return
811.1Sthorpej	fi
821.1Sthorpej
831.2Sthorpej	#
841.2Sthorpej	# T76
851.2Sthorpej	#
861.2Sthorpej	if [ x"$vendor_id" = x"42086" -a \
871.2Sthorpej	     x"$product_id" = x"6790" ]; then
881.2Sthorpej		echo "yes"
891.2Sthorpej		return
901.2Sthorpej	fi
911.2Sthorpej
921.1Sthorpej	echo "no"
931.1Sthorpej}
941.1Sthorpej
951.1Sthorpejset_permissions()
961.1Sthorpej{
971.1Sthorpej	if [ x$(is_target_device $1) = xyes ]; then
981.1Sthorpej		chgrp $new_group /dev/"${2}".*
991.1Sthorpej		chmod $new_perms /dev/"${2}".*
1001.1Sthorpej		#
1011.1Sthorpej		# We need to create a symlink here to remember
1021.1Sthorpej		# the ugen device node that was used, since we
1031.1Sthorpej		# can't recover it from the device name that
1041.1Sthorpej		# comes from the kernel later because we get the
1051.1Sthorpej		# event *after* the device is gone, and thus
1061.1Sthorpej		# cannot query any properties.
1071.1Sthorpej		#
1081.1Sthorpej		rm -f /dev/${1}-${device_name}
1091.1Sthorpej		ln -sf ${2} /dev/${1}-${device_name}
1101.1Sthorpej	fi
1111.1Sthorpej}
1121.1Sthorpej
1131.1Sthorpejrestore_permissions()
1141.1Sthorpej{
1151.1Sthorpej	if [ -h "/dev/${1}-${device_name}" ]; then
1161.1Sthorpej		devnode=$(readlink "/dev/${1}-${device_name}")
1171.1Sthorpej		if [ x"$devnode" != x ]; then
1181.1Sthorpej			chmod $orig_perms /dev/"${devnode}".*
1191.1Sthorpej			chgrp $orig_group /dev/"${devnode}".*
1201.1Sthorpej		fi
1211.1Sthorpej		rm -f "/dev/${1}-${device_name}"
1221.1Sthorpej	fi
1231.1Sthorpej}
1241.1Sthorpej
1251.1Sthorpejget_ugen_devnode()
1261.1Sthorpej{
1271.1Sthorpej	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
1281.1Sthorpej	# namespace, we have to query an additional property to
1291.1Sthorpej	# determine which one it is.
1301.1Sthorpej	local ugen_unit
1311.1Sthorpej
1321.1Sthorpej	ugen_unit=$(drvctl -p $1 ugen-unit)
1331.1Sthorpej	case "$ugen_unit" in
1341.1Sthorpej	[0-9]*)
1351.1Sthorpej		echo "ugen$ugen_unit"
1361.1Sthorpej		;;
1371.1Sthorpej	esac
1381.1Sthorpej}
1391.1Sthorpej
1401.1Sthorpejfor device in $devices; do
1411.1Sthorpej	case $device in
1421.1Sthorpej	ugensa*)
1431.1Sthorpej		# Ignore ugensa(4).
1441.1Sthorpej		;;
1451.1Sthorpej	ugen*)
1461.1Sthorpej		case $event in
1471.1Sthorpej		device-attach)
1481.1Sthorpej			devnode=$(get_ugen_devnode $1)
1491.1Sthorpej			if [ x"$devnode" != x ]; then
1501.1Sthorpej				set_permissions $device $devnode
1511.1Sthorpej			fi
1521.1Sthorpej			;;
1531.1Sthorpej		device-detach)
1541.1Sthorpej			restore_permissions $device
1551.1Sthorpej			;;
1561.1Sthorpej		esac
1571.1Sthorpej	esac
1581.1Sthorpejdone
159