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