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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#!/usr/bin/env bash
# an auxiliary script to produce a "stub" usbhid-ups subdriver from
# the output of
#
# drivers/usbhid-ups -s ups -DD -u root -x explore -x vendorid=XXXX -x productid=XXXX -x port=auto -d1 > debuginfo 2>&1
#
# Usage: cat debuginfo | gen-usbhid-subdriver.sh
#
# See also: docs/hid-subdrivers.txt
usage() {
echo "Usage: $0 [options] [file] < debuginfo"
echo "with data prepared by a driver walk:"
echo " drivers/usbhid-ups -s ups -DD -u root -x vendorid=XXXX -x productid=XXXX \\"
echo " -x port=auto -x explore -d1 > debuginfo 2>&1"
echo "Options:"
echo " -h, --help -- show this message and quit"
echo " -n name -- driver name (use natural capitalization)"
echo " -v XXXX -- vendor id (learned from debuginfo by default)"
echo " -p XXXX -- product id (learned from debuginfo by default)"
echo " -k -- keep temporary files (for debugging)"
echo " file -- read from file instead of stdin"
}
DRIVER=""
VENDORID=""
PRODUCTID=""
KEEP=""
while [ $# -gt 0 ]; do
if [ $# -gt 1 -a "$1" = "-n" ]; then
DRIVER="$2"
shift 2
elif [ $# -gt 1 -a "$1" = "-v" ]; then
VENDORID="$2"
shift 2
elif [ $# -gt 1 -a "$1" = "-p" ]; then
PRODUCTID="$2"
shift 2
elif [ "$1" = "-k" ]; then
KEEP=yes
shift
elif echo "$1" | grep -qv '^-'; then
FILE="$1"
shift
elif [ "$1" = "--help" -o "$1" = "-h" ]; then
usage
exit 0
else
echo "Illegal option $1. Try --help for more info." >&2
exit 1
fi
done
# delete temporary files: this is called just before exiting.
cleanup () {
rm -f "$DEBUG" "$UTABLE" "$USAGES" "$SUBST" "$SEDFILE" "$NEWUTABLE"
}
if [ -z "$KEEP" ]; then
trap cleanup EXIT
fi
NAME=gen-usbhid-subdriver
TMPDIR="${TEMPDIR:-/tmp}"
DEBUG=`mktemp "$TMPDIR/$NAME-DEBUG.XXXXXX"`
UTABLE=`mktemp "$TMPDIR/$NAME-UTABLE.XXXXXX"`
USAGES=`mktemp "$TMPDIR/$NAME-USAGES.XXXXXX"`
SUBST=`mktemp "$TMPDIR/$NAME-SUBST.XXXXXX"`
SEDFILE=`mktemp "$TMPDIR/$NAME-SEDFILE.XXXXXX"`
NEWUTABLE=`mktemp "$TMPDIR/$NAME-NEWUTABLE.XXXXXX"`
# save standard input to a file
if [ -z "$FILE" ]; then
FILE="$DEBUG"
cat > "$DEBUG"
fi
# prompt use for name of driver
while [ -z "$DRIVER" ]; do
echo "
Please enter a name for this driver. Use only letters and numbers. Use
natural (upper- and lowercase) capitalization, e.g., 'Belkin', 'APC'."
read -p "Name of subdriver: " DRIVER < /dev/tty
if echo $DRIVER | grep -E -q '[^a-zA-Z0-9]'; then
echo "Please use only letters and digits"
DRIVER=""
fi
done
# try to determine product and vendor id, if not specified by user
[ -n "$VENDORID" ] || VENDORID=`cat "$FILE" | sed -n 's/.*- VendorID: \([0-9a-fA-F]*\).*/\1/p' | tail -1`
[ -n "$PRODUCTID" ] || PRODUCTID=`cat "$FILE" | sed -n 's/.*- ProductID: \([0-9a-fA-F]*\).*/\1/p' | tail -1`
# prompt for productid, vendorid if necessary
if [ -z "$VENDORID" ]; then
read -p "Vendor ID: " VENDORID < /dev/tty
fi
if [ -z "$PRODUCTID" ]; then
read -p "Product ID: " PRODUCTID < /dev/tty
fi
LDRIVER=`echo $DRIVER | tr A-Z a-z`
UDRIVER=`echo $DRIVER | tr a-z A-Z`
CFILE="$LDRIVER-hid.c"
HFILE="$LDRIVER-hid.h"
# extract Usage Table
cat "$FILE" | sed -n 's/.*Path: \([^,][^,]*\), Type:.*/\1/p' > "$UTABLE"
# extract Usage codes
cat "$UTABLE" | tr '.' $'\n' | sort -u > "$USAGES"
# make up dummy names for unknown usages
count=0
cat "$USAGES" | grep -E '[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]' |\
while read U; do
count=`expr $count + 1`
echo "$U $UDRIVER$count"
done > "$SUBST"
# create an sed script for substitutions
cat "$SUBST" | sed 's/\(.*\) \(.*\)/s!\1!\2!g;/' > "$SEDFILE"
# create modified usage table
sed -f "$SEDFILE" < "$UTABLE" > "$NEWUTABLE"
# generate header file
echo "Creating $HFILE"
cat > "$HFILE" <<EOF
/* ${HFILE} - subdriver to monitor ${DRIVER} USB/HID devices with NUT
*
* Copyright (C)
* 2003 - 2009 Arnaud Quette <ArnaudQuette@Eaton.com>
* 2005 - 2006 Peter Selinger <selinger@users.sourceforge.net>
* 2008 - 2009 Arjen de Korte <adkorte-guest@alioth.debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef ${UDRIVER}_HID_H
#define ${UDRIVER}_HID_H
#include "usbhid-ups.h"
extern subdriver_t ${LDRIVER}_subdriver;
#endif /* ${UDRIVER}_HID_H */
EOF
# generate source file
echo "Creating $CFILE"
cat > "$CFILE" <<EOF
/* ${CFILE} - subdriver to monitor ${DRIVER} USB/HID devices with NUT
*
* Copyright (C)
* 2003 - 2012 Arnaud Quette <ArnaudQuette@Eaton.com>
* 2005 - 2006 Peter Selinger <selinger@users.sourceforge.net>
* 2008 - 2009 Arjen de Korte <adkorte-guest@alioth.debian.org>
* 2013 Charles Lepple <clepple+nut@gmail.com>
*
* TODO: Add year and name for new subdriver author (contributor)
* Mention in docs/acknowledgements.txt if this is a vendor contribution
*
* Note: this subdriver was initially generated as a "stub" by the
* gen-usbhid-subdriver script. It must be customized.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "usbhid-ups.h"
#include "${HFILE}"
#include "main.h" /* for getval() */
#include "usb-common.h"
#define ${UDRIVER}_HID_VERSION "${DRIVER} HID 0.1"
/* FIXME: experimental flag to be put in upsdrv_info */
/* ${DRIVER} */
#define ${UDRIVER}_VENDORID 0x${VENDORID}
/* USB IDs device table */
static usb_device_id_t ${LDRIVER}_usb_device_table[] = {
/* ${DRIVER} */
{ USB_DEVICE(${UDRIVER}_VENDORID, 0x${PRODUCTID}), NULL },
/* Terminating entry */
{ 0, 0, NULL }
};
/* --------------------------------------------------------------- */
/* Vendor-specific usage table */
/* --------------------------------------------------------------- */
/* ${UDRIVER} usage table */
static usage_lkp_t ${LDRIVER}_usage_lkp[] = {
EOF
cat "$SUBST" | sed 's/\(.*\) \(.*\)/\t{ "\2",\t0x\1 },/' >> "$CFILE"
cat >> "$CFILE" <<EOF
{ NULL, 0 }
};
static usage_tables_t ${LDRIVER}_utab[] = {
${LDRIVER}_usage_lkp,
hid_usage_lkp,
NULL,
};
/* --------------------------------------------------------------- */
/* HID2NUT lookup table */
/* --------------------------------------------------------------- */
static hid_info_t ${LDRIVER}_hid2nut[] = {
/* Please revise values discovered by data walk for mappings to
* docs/nut-names.txt and group the rest under the ifdef below:
*/
#if WITH_UNMAPPED_DATA_POINTS
EOF
cat "$NEWUTABLE" | sort -u | while read U; do
UL=`echo $U | tr A-Z a-z`
cat >> "$CFILE" <<EOF
{ "unmapped.${UL}", 0, 0, "${U}", NULL, "%.0f", 0, NULL },
EOF
done
cat >> "$CFILE" <<EOF
#endif /* if WITH_UNMAPPED_DATA_POINTS */
/* end of structure. */
{ NULL, 0, 0, NULL, NULL, NULL, 0, NULL }
};
static const char *${LDRIVER}_format_model(HIDDevice_t *hd) {
return hd->Product;
}
static const char *${LDRIVER}_format_mfr(HIDDevice_t *hd) {
return hd->Vendor ? hd->Vendor : "${DRIVER}";
}
static const char *${LDRIVER}_format_serial(HIDDevice_t *hd) {
return hd->Serial;
}
/* this function allows the subdriver to "claim" a device: return 1 if
* the device is supported by this subdriver, else 0. */
static int ${LDRIVER}_claim(HIDDevice_t *hd)
{
int status = is_usb_device_supported(${LDRIVER}_usb_device_table, hd);
switch (status)
{
case POSSIBLY_SUPPORTED:
/* by default, reject, unless the productid option is given */
if (getval("productid")) {
return 1;
}
possibly_supported("${DRIVER}", hd);
return 0;
case SUPPORTED:
return 1;
case NOT_SUPPORTED:
default:
return 0;
}
}
subdriver_t ${LDRIVER}_subdriver = {
${UDRIVER}_HID_VERSION,
${LDRIVER}_claim,
${LDRIVER}_utab,
${LDRIVER}_hid2nut,
${LDRIVER}_format_model,
${LDRIVER}_format_mfr,
${LDRIVER}_format_serial,
fix_report_desc, /* may optionally be customized, see cps-hid.c for example */
};
EOF
cat <<EOF
Done.
If you are looking to extend an existing subdriver with data points
not yet handled, now is a good time to compare ${LDRIVER}_hid2nut[]
tables in existing sources vs. content generated from this device walk.
Using a GUI tool like Meld or WinMerge is recommended.
If you are crafting a new subdriver, do not forget to:
* add #include "${HFILE}" to drivers/usbhid-ups.c,
* add &${LDRIVER}_subdriver to drivers/usbhid-ups.c:subdriver_list,
* add ${LDRIVER}-hid.c to USBHID_UPS_SUBDRIVERS in drivers/Makefile.am
* add ${LDRIVER}-hid.h to dist_noinst_HEADERS in drivers/Makefile.am
* "autoreconf" from the top level directory
For new data points in ${LDRIVER}_hid2nut[] tables be sure to not
invent new names, but use standard ones from docs/nut-names.txt file.
If you need to standardize a name for some concept not addressed yet,
please do so via nut-upsdev mailing list discussion.
EOF
|