summaryrefslogtreecommitdiff
path: root/openwrt/rhctl/files/rhctl-usbserial.hotplug
blob: 8d2f7bc5692a66882b71dec9b63ee5b5dfcb40cb (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
#!/bin/sh
#
# Persistent naming for usb serial devices.
#
# Copyright 2013-2014 Christian Pointner <equinox@helsinki.at>
#

logger="logger -p daemon.info -t rhctl-usbserial"

idVendor=`echo $PRODUCT | awk -F '/' '{ print($1) }'`
idProduct=`echo $PRODUCT | awk -F '/' '{ print($2) }'`
bcdDevice=`echo $PRODUCT | awk -F '/' '{ print($3) }'`

if [ "$idVendor" != "403" ] || [ "$idProduct" != "6011" ]; then
  exit 0
fi

DEVPATH="/sys$DEVPATH"
if [ $ACTION == "add" ]; then
  if [ ! -e "$DEVPATH/bInterfaceNumber" ]; then
    exit 0
  fi

  PARENT=`dirname "$DEVPATH`
  SERIAL=`cat $PARENT/serial`

  if [ "$SERIAL" != $(uci -q get rhctl.@global[0].ftdi_serial) ]; then
    exit 0
  fi
fi

hotplug=/dev/.hotplug
token=$hotplug/`echo $PRODUCT$DEVPATH | sed 's/\//=/g'`

case "$ACTION" in
  add)
    ttydir=`echo "$DEVPATH/ttyUSB*`
    tty=`basename $ttydir`
    interface=`cat "$DEVPATH/bInterfaceNumber"`

    device="$interface"

    case "$interface" in
      00)     device="ttyMasterCtl"
              ;;
      01)     device="ttyMasterHB"
              ;;
      02)     device="ttyStandbyCtl"
              ;;
      03)     device="ttyStandbyHB"
              ;;
      *)      exit 0
              ;;
    esac

    mkdir -p "$hotplug"
    rm -f "/dev/$device" "$token"
    ln -s "/dev/$device" "$token"
    ln -s "/dev/$tty" "/dev/$device"
    $logger "added: tty=$tty device=$device"
    ;;
  remove)
    if [ -h "$token" ]; then
      device=$(ls -l $token | sed 's/.*-> //;/ /s/.*//')
      case "$device" in
        "") # no symlink
          $logger "failed: token=$token device=$device"
          ;;
        *)
          rm -f $token $device
          $logger "removed: $device"
          ;;
      esac
    fi
    ;;
esac

exit 0