#!/bin/sh # # Script for creating aliases for USB serial devices. It will create # either /dev/{tty,cu}. or /dev/{tty,cu}.usb. if # there is no serial number for the device. A path starts w/ the # USB bus number, and then continues w/ each hub's port number. For # example: 2.1.4.1.4 means USB bus number 2, port 1 on root hub, port 4 # on next hub, port 1 on next hub, and port 4 on the last hub. # # You can use `usbserialsn discover ` to get the path that a # device is at. It will echo the device name followed by the path. # # # Add the following to /usr/local/etc/devd/usbserialsn.conf: # attach 100 { # device-name ".*"; # match "ttyname" ".+"; # match "ugen" ".+"; # match "sernum" ".+"; # action "/usr/local/libexec/usbserialsn attach $device-name $sernum $ttyname"; # }; # # attach 100 { # device-name ".*"; # match "ttyname" ".+"; # match "ugen" ".+"; # match "sernum" "^$"; # action "/usr/local/libexec/usbserialsn attach $device-name auto $ttyname"; # }; # # detach 100 { # device-name ".*"; # action "/usr/local/libexec/usbserialsn detach $device-name"; # }; # if [ $# -lt 2 ]; then echo "Usage: $0 discover " echo " $0 attach |auto " echo " $0 detach " exit 1 fi mode=$1 devname=$2 sernum=$3 ttyname=$4 getdevpath() { echo $(devinfo -v | awk ' BEGIN { #print "start" } $0 ~ /bus=/ && $0 ~ /vendor=/ { # XXX - does not handle quoted strings #print mode = "dev" for (i = 3; i <= NF; i++) { if ($i == "at") { mode = "bus" continue } cnt = split($i, info, "=") if (cnt != 2) print "error: " cnt " " $i pnpinfo[$1, mode, info[1]] = info[2] } # create index of devices by: bus, devaddr. addrs[pnpinfo[$1, "bus", "bus"], pnpinfo[$1, "bus", "devaddr"]] = $1 devices[$1] = 1 } function devpath(ndev, hubaddr, portinfo) { hubaddr = 0 portinfo = "" bus = pnpinfo[ndev, "bus", "bus"] while (hubaddr != 1) { hubaddr = pnpinfo[ndev, "bus", "hubaddr"] portinfo = "." pnpinfo[ndev, "bus", "port"] portinfo ndev = addrs[bus, hubaddr] } return bus portinfo } END { if (!devices[DEV]) { print "bogus" exit 0 } print devpath(DEV) } ' "DEV=$1") } if [ x"$sernum" = x"auto" ]; then sernum=usb.$(getdevpath "$devname") fi case "$mode" in discover) echo "$devname" $(getdevpath "$devname") ;; attach) if [ $# -ne 4 ]; then echo "Bad usage" exit 1 fi ln -sf cua${ttyname} /dev/cu.${sernum} ln -sf tty${ttyname} /dev/tty.${sernum} echo ${sernum} >/var/run/usbserialsn.${devname} ;; detach) if [ $# -ne 2 ]; then echo "Bad usage" exit 1 fi if [ ! -e /var/run/usbserialsn.${devname} ]; then exit 0 fi sernum=$(cat /var/run/usbserialsn.${devname}) rm -f /dev/cu.${sernum} /dev/tty.${sernum} /var/run/usbserialsn.${devname} ;; *) echo "Unknown mode" exit 1 ;; esac