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