#!/bin/sh # Copyright (c) 2021 Rozhuk Ivan # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # Exit on error. set -e # Settings. FW_DIR='/etc/fw' LOG_TAG='prnt-helper' # Handle only usb/lp*. [ -n "${DEVNAME##usb/lp*}" ] && exit 0 # Remove broken links. if [ "${ACTION}" = "remove" ]; then find -L /dev -type l -maxdepth 1 -exec rm {} ';' exit 0 fi # Handle only ADD action. [ "${ACTION}" != 'add' ] && exit 0 # Get VID, PID and serial. DEVNAME_PATH="/dev/${DEVNAME}" DEV_PID=`cat /sys/${DEVPATH}/../../../idProduct | tr -d '\n'` DEV_VID=`cat /sys/${DEVPATH}/../../../idVendor | tr -d '\n'` DEV_SERIAL=`cat /sys/${DEVPATH}/../../../serial | tr -d '\n'` DEV_IEEE1284_ID=`cat /sys/${DEVPATH}/../../ieee1284_id | tr -d '\n'` DEV_FWVER=`echo "${DEV_IEEE1284_ID}" | grep 'FWVER' | sed -E 's|.*FWVER:(.*);|\1|'` DEV_FW_FILE="${FW_DIR}/${DEV_VID}-${DEV_PID}.dl" # Generate unique simlink. if [ -n "${DEV_SERIAL}" ]; then ln -sf "${DEVNAME_PATH}" "/dev/${DEV_SERIAL}" logger -t "${LOG_TAG}" "Device ${DEVNAME_PATH} linked to /dev/${DEV_SERIAL}" fi # Is FW required to load? (usb_printerid replacement). # This check will fail if script will run before device replug. if [ -n "${DEV_FWVER}" ]; then logger -t "${LOG_TAG}" "${DEVNAME_PATH} already has ${DEV_FWVER}, ${DEV_FW_FILE} will not be loaded" exit 0 fi # Is FW file exist? if [ ! -r "${DEV_FW_FILE}" ]; then logger -t "${LOG_TAG}" "File ${DEV_FW_FILE} not found, can not handle ${DEVNAME_PATH}" exit 0 fi # Try to upload FW. logger -t "${LOG_TAG}" "Attempt to load ${DEV_FW_FILE} to ${DEVNAME_PATH}" for i in $(seq 10); do if [ -c "${DEVNAME_PATH}" ]; then cat "${DEV_FW_FILE}" > "${DEVNAME_PATH}" logger -t "${LOG_TAG}" "${DEV_FW_FILE} loaded to ${DEVNAME_PATH}" exit 0 fi sleep 1 done logger -t "${LOG_TAG}" "${DEV_FW_FILE} load to ${DEVNAME_PATH} fail!" exit 0