99-ugen-perms-minipro revision 1.2
1#!/bin/sh -x
2#
3# $NetBSD: 99-ugen-perms-minipro,v 1.2 2025/09/05 23:55:54 thorpej Exp $
4#
5# Look for a "Minipro" (https://gitlab.com/DavidGriffith/minipro) compatible
6# EEPROM programmer and change change the permissions to 0660.
7#
8# Written by Jason R. Thorpe, March 2024.  Public domain.
9#
10
11export LC_ALL=C
12
13event="$1"
14shift
15devices=$@
16
17orig_perms=0600
18new_perms=0660
19
20orig_group=wheel
21new_group=wheel
22
23device_name=minipro
24
25is_target_device()
26{
27	local vendor_string
28	local product_string
29	local vendor_id
30	local product_id
31
32	#
33	# TL866A/TL866CS programmers have:
34	#
35	#	VID = 0x04d8 (1240)	# Microchip
36	#	PID = 0xe11c (57628)	# probably some PIC microcontroller
37	#
38	# XXX It's probably better to match on vendor-string / product-string
39	# in this case because of the use of the generic Microchip VID.
40	#
41	# The XGecu-branded TL866II+ devices have:
42	#
43	#	vendor-string="Xingong Electronicg Co.."
44	#	product-string="Xingong XGecu USB Prog.. Device"
45	#
46	# ...but they also have seemingly unique VID/PID (not the
47	# generic Microchip VID the older TL866A/CS programmers have):
48	#
49	#	VID = 0xa466 (42086)
50	#	PID = 0x0a53 (2643)
51	#
52	# The XGecu T48 and T56 use the same VID/PID as the TL866II+.
53	#
54	# The XGecu T76 uses:
55	#
56	#	VID = 0xa466 (42086)
57	#	PID = 0x1a86 (6790)
58	#
59
60	vendor_string="$(drvctl -p $1 vendor-string)"
61	product_string="$(drvctl -p $1 product-string)"
62	vendor_id="$(drvctl -p $1 vendor-id)"
63	product_id="$(drvctl -p $1 product-id)"
64
65	#
66	# TL866A / TL866CS
67	#
68	if [ x"$vendor_id" = x"1240" -a \
69	     x"$product_id" = x"57628" ]; then
70		echo "yes"
71		return;
72	fi
73
74	#
75	# TL866II+ / T48 / T56
76	#
77	if [ x"$vendor_id" = x"42086" -a \
78	     x"$product_id" = x"2643" ]; then
79		echo "yes"
80		return
81	fi
82
83	#
84	# T76
85	#
86	if [ x"$vendor_id" = x"42086" -a \
87	     x"$product_id" = x"6790" ]; then
88		echo "yes"
89		return
90	fi
91
92	echo "no"
93}
94
95set_permissions()
96{
97	if [ x$(is_target_device $1) = xyes ]; then
98		chgrp $new_group /dev/"${2}".*
99		chmod $new_perms /dev/"${2}".*
100		#
101		# We need to create a symlink here to remember
102		# the ugen device node that was used, since we
103		# can't recover it from the device name that
104		# comes from the kernel later because we get the
105		# event *after* the device is gone, and thus
106		# cannot query any properties.
107		#
108		rm -f /dev/${1}-${device_name}
109		ln -sf ${2} /dev/${1}-${device_name}
110	fi
111}
112
113restore_permissions()
114{
115	if [ -h "/dev/${1}-${device_name}" ]; then
116		devnode=$(readlink "/dev/${1}-${device_name}")
117		if [ x"$devnode" != x ]; then
118			chmod $orig_perms /dev/"${devnode}".*
119			chgrp $orig_group /dev/"${devnode}".*
120		fi
121		rm -f "/dev/${1}-${device_name}"
122	fi
123}
124
125get_ugen_devnode()
126{
127	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
128	# namespace, we have to query an additional property to
129	# determine which one it is.
130	local ugen_unit
131
132	ugen_unit=$(drvctl -p $1 ugen-unit)
133	case "$ugen_unit" in
134	[0-9]*)
135		echo "ugen$ugen_unit"
136		;;
137	esac
138}
139
140for device in $devices; do
141	case $device in
142	ugensa*)
143		# Ignore ugensa(4).
144		;;
145	ugen*)
146		case $event in
147		device-attach)
148			devnode=$(get_ugen_devnode $1)
149			if [ x"$devnode" != x ]; then
150				set_permissions $device $devnode
151			fi
152			;;
153		device-detach)
154			restore_permissions $device
155			;;
156		esac
157	esac
158done
159