1#!/bin/sh23# Takes care of /dev/bus/usb devices.4#5# The mdev.conf gives us limited options on how to handle ownership, so a fix has come as script.6#7# Features:8# - Support smartcard devices. Adjust device group to one that let pcscd open them.9# - If VirtuaBox is present, support it's usb subsystem by duplicating devices in /dev/vboxusb.10# - If Android device is plugged in, make it possible for users in plugdev group to open them (mtp, fastboot, adb etc)11# - Old mdev+kernel used to keep USB devices in root of /dev, relocate them into a place where they are expected..1213umask 0221415sys_path="/sys/${DEVPATH}"16vbox_group='vboxusers'17vbox_device_root='/dev/vboxusb'18vbox_device_dir="${vbox_device_root}/${BUSNUM}"19vbox_device_path="${vbox_device_dir}/${DEVNUM}"20plugdev_group="plugdev"21# smartcard_group="pcscd"22smartcard_group="gnupg"232425case "${ACTION}" in26 'add')27 [ "${BUSNUM}" ] || return 128 [ "${DEVNUM}" ] || return 129 [ "${MAJOR}" ] || return 130 [ "${MINOR}" ] || return 131 mkdir -p "/dev/bus/usb/${BUSNUM}"3233 # $MDEV will be exported when running `mdev -s`34 # $DEVNAME and $DEVPATH will be when we poke uevent files with add in init script.35 if [ "/dev/${MDEV}" != "/dev/bus/usb/${BUSNUM}/${DEVNUM}" ]; then36 mv "/dev/${MDEV}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}"37 fi3839 if ! [ "${DEVPATH}" ]; then40 return 141 fi4243 if [ -f "${sys_path}/product" ]; then44 idVendor="$(cat ${sys_path}/idVendor)"45 case "${idVendor}" in46 # Smartcards (yubikey etc).47 '1050' | '20a0')48 # The tricky part here is that we may run into race conditon with pcscd not picking usb devices if the ownership is wrong.49 # the `pcscd --hotplug` does not poke USB devices sadly. We better be fast!50 chgrp "${smartcard_group}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}" 2>/dev/null && chmod 660 "/dev/bus/usb/${BUSNUM}/${DEVNUM}"51 ;;52 # Android devices.53 '0bb4'|'18d1'|'22b8'|'0fce'|'19d2'|'04e8'|'2717'|'05c6')54 chgrp "${plugdev_group}" "/dev/bus/usb/${BUSNUM}/${DEVNUM}" 2>/dev/null && chmod 660 "/dev/bus/usb/${BUSNUM}/${DEVNUM}"55 ;;56 esac57 fi5859 if grep -q "${vbox_group}" /etc/group; then60 install -d "${vbox_device_root}" -g "${vbox_group}" -o 'root' -m 075061 install -d "${vbox_device_dir}" -g "${vbox_group}" -o 'root' -m 075062 mknod "${vbox_device_path}" c "${MAJOR}" "${MINOR}" -m 0660 2>/dev/null63 chown "root:${vbox_group}" "${vbox_device_path}"64 fi65 ;;66 'remove')67 [ "${BUSNUM}" ] || return 168 [ "${DEVNUM}" ] || return 169 [ "${MAJOR}" ] || return 170 [ "${MINOR}" ] || return 171 rm "/dev/bus/usb/${BUSNUM}/${DEVNUM}"72 rmdir "/dev/bus/usb/${BUSNUM}" 2>/dev/null73 rmdir "/dev/bus/usb" 2>/dev/null7475 if grep -q "${vbox_group}" /etc/group; then76 rm "${vbox_device_path}"77 rmdir "${vbox_device_dir}" 2>/dev/null78 fi79 ;;80esac