99-ugen-perms-minipro revision 1.1
11.1Sthorpej#!/bin/sh -x
21.1Sthorpej#
31.1Sthorpej# $NetBSD: 99-ugen-perms-minipro,v 1.1 2024/03/30 16:47:55 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.1Sthorpej	# XXX Add the XGecu T48 programmer info here.
531.1Sthorpej	#
541.1Sthorpej
551.1Sthorpej	vendor_string="$(drvctl -p $1 vendor-string)"
561.1Sthorpej	product_string="$(drvctl -p $1 product-string)"
571.1Sthorpej	vendor_id="$(drvctl -p $1 vendor-id)"
581.1Sthorpej	product_id="$(drvctl -p $1 product-id)"
591.1Sthorpej
601.1Sthorpej	#
611.1Sthorpej	# TL866A / TL866CS
621.1Sthorpej	#
631.1Sthorpej	if [ x"$vendor_id" = x"1240" -a \
641.1Sthorpej	     x"$product_id" = x"57628" ]; then
651.1Sthorpej		echo "yes"
661.1Sthorpej		return;
671.1Sthorpej	fi
681.1Sthorpej
691.1Sthorpej	#
701.1Sthorpej	# TL866II+
711.1Sthorpej	#
721.1Sthorpej	if [ x"$vendor_id" = x"42086" -a \
731.1Sthorpej	     x"$product_id" = x"2643" ]; then
741.1Sthorpej		echo "yes"
751.1Sthorpej		return
761.1Sthorpej	fi
771.1Sthorpej
781.1Sthorpej	echo "no"
791.1Sthorpej}
801.1Sthorpej
811.1Sthorpejset_permissions()
821.1Sthorpej{
831.1Sthorpej	if [ x$(is_target_device $1) = xyes ]; then
841.1Sthorpej		chgrp $new_group /dev/"${2}".*
851.1Sthorpej		chmod $new_perms /dev/"${2}".*
861.1Sthorpej		#
871.1Sthorpej		# We need to create a symlink here to remember
881.1Sthorpej		# the ugen device node that was used, since we
891.1Sthorpej		# can't recover it from the device name that
901.1Sthorpej		# comes from the kernel later because we get the
911.1Sthorpej		# event *after* the device is gone, and thus
921.1Sthorpej		# cannot query any properties.
931.1Sthorpej		#
941.1Sthorpej		rm -f /dev/${1}-${device_name}
951.1Sthorpej		ln -sf ${2} /dev/${1}-${device_name}
961.1Sthorpej	fi
971.1Sthorpej}
981.1Sthorpej
991.1Sthorpejrestore_permissions()
1001.1Sthorpej{
1011.1Sthorpej	if [ -h "/dev/${1}-${device_name}" ]; then
1021.1Sthorpej		devnode=$(readlink "/dev/${1}-${device_name}")
1031.1Sthorpej		if [ x"$devnode" != x ]; then
1041.1Sthorpej			chmod $orig_perms /dev/"${devnode}".*
1051.1Sthorpej			chgrp $orig_group /dev/"${devnode}".*
1061.1Sthorpej		fi
1071.1Sthorpej		rm -f "/dev/${1}-${device_name}"
1081.1Sthorpej	fi
1091.1Sthorpej}
1101.1Sthorpej
1111.1Sthorpejget_ugen_devnode()
1121.1Sthorpej{
1131.1Sthorpej	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
1141.1Sthorpej	# namespace, we have to query an additional property to
1151.1Sthorpej	# determine which one it is.
1161.1Sthorpej	local ugen_unit
1171.1Sthorpej
1181.1Sthorpej	ugen_unit=$(drvctl -p $1 ugen-unit)
1191.1Sthorpej	case "$ugen_unit" in
1201.1Sthorpej	[0-9]*)
1211.1Sthorpej		echo "ugen$ugen_unit"
1221.1Sthorpej		;;
1231.1Sthorpej	esac
1241.1Sthorpej}
1251.1Sthorpej
1261.1Sthorpejfor device in $devices; do
1271.1Sthorpej	case $device in
1281.1Sthorpej	ugensa*)
1291.1Sthorpej		# Ignore ugensa(4).
1301.1Sthorpej		;;
1311.1Sthorpej	ugen*)
1321.1Sthorpej		case $event in
1331.1Sthorpej		device-attach)
1341.1Sthorpej			devnode=$(get_ugen_devnode $1)
1351.1Sthorpej			if [ x"$devnode" != x ]; then
1361.1Sthorpej				set_permissions $device $devnode
1371.1Sthorpej			fi
1381.1Sthorpej			;;
1391.1Sthorpej		device-detach)
1401.1Sthorpej			restore_permissions $device
1411.1Sthorpej			;;
1421.1Sthorpej		esac
1431.1Sthorpej	esac
1441.1Sthorpejdone
145