forked from analogdevicesinc/linux_image_ADI-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iiod_usbd.init
162 lines (136 loc) · 4.43 KB
/
iiod_usbd.init
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: iiod
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: IIO Daemon
### END INIT INFO
# Debian init.d script for the IIO Daemon
# Copyright (C) 2016 Analog Devices Inc.
. /lib/lsb/init-functions
# Server-side demuxing by default
IIOD_OPTS=-D
if test -f /etc/default/iiod; then
. /etc/default/iiod
fi
UDC_NAME="$(ls -1 /sys/class/udc | head -n1)"
GADGET=/sys/kernel/config/usb_gadget/ffs
SYSID="$(dmesg | grep axi_sysid | cut -d " " -f 3- | sed -e 's/</[/' -e 's/>/]/')"
# Most ARM systems will fill /sys/firmware; most x86 will fill out Desktop Management Interface
MODEL="/sys/firmware/devicetree/base/model"
DMI="/sys/class/dmi/id/board_vendor"
if [ -f "${MODEL}" ] ; then
BASE=$(cat $MODEL)
elif [ -f "${DMI}" ] ; then
BASE="$(cat ${DMI}) $(cat /sys/class/dmi/id/product_name)"
fi
# If this is an FMC Board, capture the data
for i in $(find /sys/ -name eeprom)
do
if [ `stat -c %s $i` -ne "256" ] ; then
continue;
fi
fru-dump $i > /dev/null
if [ $? -eq "0" ] ; then
BOARD=$(fru-dump $i -b | grep "Part Number" | awk -F: '{print $2}' | sed 's/^[[:space:]]*//')
SERIAL=$(fru-dump $i -b | grep "Serial Number" | awk -F: '{print $2}' | sed 's/^[[:space:]]*//')
NAME=$(fru-dump $i -b | grep "Product Name" | awk -F: '{print $2}' | sed 's/^[[:space:]]*//')
VENDOR=$(fru-dump $i -b | grep "Manufacturer" | awk -F: '{print $2}' | sed 's/^[[:space:]]*//')
break
fi
done
# build up /etc/libiio.ini or add to it if it is not there
replace_or_add() {
if [ ${#1} -eq 0 ] ; then
return
fi
if [ ${#2} -eq 0 ] ; then
return
fi
if [ ! -f /etc/libiio.ini ] ; then
echo "# This file is autogenerated from /etc/init.d/iiod\n[Context Attributes]" > /etc/libiio.ini
fi
grep -q '\[Context Attributes\]' /etc/libiio.ini || echo "[Context Attributes]" >> /etc/libiio.ini
if grep -q $1 /etc/libiio.ini ; then
sed -i -e "/$1/c $1=$2" /etc/libiio.ini
else
echo "$1=$2" >> /etc/libiio.ini
fi
}
case "$1" in
start)
log_daemon_msg "Starting IIO Daemon" "iiod" || true
replace_or_add hdl_system_id "${SYSID}"
replace_or_add hw_model "${BOARD} on ${BASE}"
replace_or_add hw_carrier "${BASE}"
replace_or_add hw_mezzanine "${BOARD}"
replace_or_add hw_name "${NAME}"
replace_or_add hw_vendor "${VENDOR}"
replace_or_add hw_serial "${SERIAL}"
if [ ${#UDC_NAME} -ne 0 ] ; then
if [ ! -d /sys/kernel/config/usb_gadget ] ; then
mount configfs -t configfs /sys/kernel/config
mkdir ${GADGET}
echo 0x0456 > ${GADGET}/idVendor
echo 0xb671 > ${GADGET}/idProduct
mkdir ${GADGET}/strings/0x409
echo "Analog Devices Inc." > ${GADGET}/strings/0x409/manufacturer
echo "Generic USB IIOD" > ${GADGET}/strings/0x409/product
echo 00000000 > ${GADGET}/strings/0x409/serialnumber
mkdir ${GADGET}/functions/ffs.iio_ffs
mkdir ${GADGET}/configs/c.1
mkdir ${GADGET}/configs/c.1/strings/0x409
echo "IIO" > ${GADGET}/configs/c.1/strings/0x409/configuration
mkdir -p ${GADGET}/functions/acm.usb0
ln -s ${GADGET}/functions/ffs.iio_ffs ${GADGET}/configs/c.1/ffs.iio_ffs
ln -s ${GADGET}/functions/acm.usb0 ${GADGET}/configs/c.1
fi
if [ ! -d /dev/iio_ffs ] ; then
mkdir /dev/iio_ffs
mount iio_ffs -t functionfs /dev/iio_ffs
fi
fi
START_UDC=0
grep -q iio_ffs /proc/mounts && IIOD_OPTS="${IIOD_OPTS} -F /dev/iio_ffs" && START_UDC=1
if start-stop-daemon -S -b -q -m -p /var/run/iiod.pid -x /usr/sbin/iiod -- $IIOD_OPTS; then
if [ $START_UDC -eq 1 ] ; then
for _ in $(seq 1 10) ; do
sleep 1
if echo $UDC_NAME > ${GADGET}/UDC ; then
break
fi
done
fi
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
stop)
log_daemon_msg "Stopping IIO Daemon" "iiod" || true
[ -e ${GADGET}/UDC ] && echo "" > ${GADGET}/UDC
if start-stop-daemon -K -q -p /var/run/iiod.pid; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
restart|force-reload)
$0 stop
$0 start
;;
status)
if [ -f /var/run/iiod.pid ] ; then
status_of_proc -p /var/run/iiod.pid /usr/sbin/iiod iiod && exit 0 || exit $?
else
status_of_proc /usr/sbin/iiod iiod && exit 0 || exit $?
fi
;;
*)
log_action_msg "Usage: /etc/init.d/iiod.sh {start|stop|restart|status}" || true
exit 1
esac
exit 0