summaryrefslogtreecommitdiff
path: root/drivers/dahdi/xpp/xpp_debug
blob: a3b70471afa1fae9dae69c5d769dbeb5ead797b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /bin/sh
#
# xpp_debug: Turn on/off debugging flags via /sys/module/*/parameters/debug
#

modules="xpp xpp_usb xpd_fxs xpd_fxo xpd_bri xpd_pri"
dbg_names="DEFAULT PCM LEDS SYNC SIGNAL PROC REGS DEVICES COMMANDS"

usage() {
	echo 1>&2 "Usage: $0 [module_name] [[-]flags...]"
	echo 1>&2 "	module_name => $modules"
	echo 1>&2 "	flags       => NONE $dbg_names ANY"
	echo 1>&2 ""
	echo 1>&2 "	Example: $0 xpp ANY -PCM -LEDS"
	echo 1>&2 ""
}

sysfs_name() {
	f=''
	if [ -f "/sys/module/$1/parameters/debug" ]; then
		f="/sys/module/$1/parameters/debug"
	elif [ -f "/sys/module/$1/debug" ]; then
		f="/sys/module/$1/debug"
	fi
	echo "$f"
}

sysfs_value() {
	f=`sysfs_name "$1"`
	if [ "$f" != "" ]; then
		cat "$f"
	fi
}

show_debug() {
	usage
	for i in $modules
	do
		f=`sysfs_name "$i"`
		if [ -f "$f" ]; then
			val=`cat $f`
			j=0
			list=''
			for n in $dbg_names
			do
				if (( val & (1 << j) ))
				then
					list="$list $n"
				fi
				let j++
			done
			if [ "$list" = "" ]; then
				list=' NONE'
			fi
			echo "$i	$list"
		fi
	done
}

calc_debug() {
	val="$1"
	shift
	for wanted in $*
	do
		j=0
		found=0
		for n in $dbg_names
		do
			if [ "$wanted" = "$n" ]; then
				(( val |= (1 << j) ))
				found=1
			elif [ "$wanted" = -"$n" ]; then
				(( val &= ~(1 << j) ))
				found=1
			elif [ "$wanted" = "ANY" ]; then
				(( val = ~0 ))
				found=1
			elif [ "$wanted" = -"ANY" -o "$wanted" = "NONE" ]; then
				(( val = 0 ))
				found=1
			fi
			let j++
		done
		if [ "$found" -eq 0 ]; then
			echo >&2 "$0: Unkown debug flag '$wanted'"
			exit 1
		fi
	done
	echo $val
}

if [ "$#" = 0 ]; then
	show_debug
	exit 0
fi

module="$1"
shift

if ! echo "$modules" | grep -w "$module" > /dev/null; then
	echo >&2 "$0: Unkown module $module"
	exit 1
fi

oldval=`sysfs_value "$module"`
val=`calc_debug "$oldval" $*`
file=`sysfs_name $module`

echo "$val" > "$file"
show_debug