Home | History | Annotate | Line # | Download | only in hooks
99-ugen-perms-minipro revision 1.1
      1  1.1  thorpej #!/bin/sh -x
      2  1.1  thorpej #
      3  1.1  thorpej # $NetBSD: 99-ugen-perms-minipro,v 1.1 2024/03/30 16:47:55 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.1  thorpej 	# XXX Add the XGecu T48 programmer info here.
     53  1.1  thorpej 	#
     54  1.1  thorpej 
     55  1.1  thorpej 	vendor_string="$(drvctl -p $1 vendor-string)"
     56  1.1  thorpej 	product_string="$(drvctl -p $1 product-string)"
     57  1.1  thorpej 	vendor_id="$(drvctl -p $1 vendor-id)"
     58  1.1  thorpej 	product_id="$(drvctl -p $1 product-id)"
     59  1.1  thorpej 
     60  1.1  thorpej 	#
     61  1.1  thorpej 	# TL866A / TL866CS
     62  1.1  thorpej 	#
     63  1.1  thorpej 	if [ x"$vendor_id" = x"1240" -a \
     64  1.1  thorpej 	     x"$product_id" = x"57628" ]; then
     65  1.1  thorpej 		echo "yes"
     66  1.1  thorpej 		return;
     67  1.1  thorpej 	fi
     68  1.1  thorpej 
     69  1.1  thorpej 	#
     70  1.1  thorpej 	# TL866II+
     71  1.1  thorpej 	#
     72  1.1  thorpej 	if [ x"$vendor_id" = x"42086" -a \
     73  1.1  thorpej 	     x"$product_id" = x"2643" ]; then
     74  1.1  thorpej 		echo "yes"
     75  1.1  thorpej 		return
     76  1.1  thorpej 	fi
     77  1.1  thorpej 
     78  1.1  thorpej 	echo "no"
     79  1.1  thorpej }
     80  1.1  thorpej 
     81  1.1  thorpej set_permissions()
     82  1.1  thorpej {
     83  1.1  thorpej 	if [ x$(is_target_device $1) = xyes ]; then
     84  1.1  thorpej 		chgrp $new_group /dev/"${2}".*
     85  1.1  thorpej 		chmod $new_perms /dev/"${2}".*
     86  1.1  thorpej 		#
     87  1.1  thorpej 		# We need to create a symlink here to remember
     88  1.1  thorpej 		# the ugen device node that was used, since we
     89  1.1  thorpej 		# can't recover it from the device name that
     90  1.1  thorpej 		# comes from the kernel later because we get the
     91  1.1  thorpej 		# event *after* the device is gone, and thus
     92  1.1  thorpej 		# cannot query any properties.
     93  1.1  thorpej 		#
     94  1.1  thorpej 		rm -f /dev/${1}-${device_name}
     95  1.1  thorpej 		ln -sf ${2} /dev/${1}-${device_name}
     96  1.1  thorpej 	fi
     97  1.1  thorpej }
     98  1.1  thorpej 
     99  1.1  thorpej restore_permissions()
    100  1.1  thorpej {
    101  1.1  thorpej 	if [ -h "/dev/${1}-${device_name}" ]; then
    102  1.1  thorpej 		devnode=$(readlink "/dev/${1}-${device_name}")
    103  1.1  thorpej 		if [ x"$devnode" != x ]; then
    104  1.1  thorpej 			chmod $orig_perms /dev/"${devnode}".*
    105  1.1  thorpej 			chgrp $orig_group /dev/"${devnode}".*
    106  1.1  thorpej 		fi
    107  1.1  thorpej 		rm -f "/dev/${1}-${device_name}"
    108  1.1  thorpej 	fi
    109  1.1  thorpej }
    110  1.1  thorpej 
    111  1.1  thorpej get_ugen_devnode()
    112  1.1  thorpej {
    113  1.1  thorpej 	# Because "ugen" and "ugenif" share the same /dev/ugenN.*
    114  1.1  thorpej 	# namespace, we have to query an additional property to
    115  1.1  thorpej 	# determine which one it is.
    116  1.1  thorpej 	local ugen_unit
    117  1.1  thorpej 
    118  1.1  thorpej 	ugen_unit=$(drvctl -p $1 ugen-unit)
    119  1.1  thorpej 	case "$ugen_unit" in
    120  1.1  thorpej 	[0-9]*)
    121  1.1  thorpej 		echo "ugen$ugen_unit"
    122  1.1  thorpej 		;;
    123  1.1  thorpej 	esac
    124  1.1  thorpej }
    125  1.1  thorpej 
    126  1.1  thorpej for device in $devices; do
    127  1.1  thorpej 	case $device in
    128  1.1  thorpej 	ugensa*)
    129  1.1  thorpej 		# Ignore ugensa(4).
    130  1.1  thorpej 		;;
    131  1.1  thorpej 	ugen*)
    132  1.1  thorpej 		case $event in
    133  1.1  thorpej 		device-attach)
    134  1.1  thorpej 			devnode=$(get_ugen_devnode $1)
    135  1.1  thorpej 			if [ x"$devnode" != x ]; then
    136  1.1  thorpej 				set_permissions $device $devnode
    137  1.1  thorpej 			fi
    138  1.1  thorpej 			;;
    139  1.1  thorpej 		device-detach)
    140  1.1  thorpej 			restore_permissions $device
    141  1.1  thorpej 			;;
    142  1.1  thorpej 		esac
    143  1.1  thorpej 	esac
    144  1.1  thorpej done
    145