Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Oct 2019 12:25:09 +0930
From:      "O'Connor, Daniel" <darius@dons.net.au>
To:        freebsd-usb@freebsd.org
Subject:   USB serial ports by serial number
Message-ID:  <CC980A9B-8F41-4C2A-A729-2CC690DFDA7E@dons.net.au>

next in thread | raw e-mail | index | archive | help
Hi,
I have several USB serial ports on a machine and I don't want to rely on =
attach order to get various programs to talk to the correct serial port.

I wrote the following devd script and shell script to create symlinks =
from cu.${sernum}/tty.${sernum} to the real device nodes.

I post it here in the hope other people find it useful.

[maarsytest 2:49] ~> cat /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";
};

detach 100 {
	device-name		".*";
	action "/usr/local/libexec/usbserialsn detach $device-name";
};
[maarsytest 2:49] ~> cat /usr/local/libexec/usbserialsn
#!/bin/sh

if [ $# -lt 2 ]; then
  echo "Bad usage"
  exit 1
fi

mode=3D$1
devname=3D$2
sernum=3D$3
ttyname=3D$4

case "$mode" in
  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=3D$(cat /var/run/usbserialsn.${devname})
    rm -f /dev/cu.${sernum} /dev/tty.${sernum} =
/var/run/usbserialsn.${devname}
    ;;

  *)
    echo "Unknown mode"
    exit 1
    ;;
esac

It would be a lot simpler if devd reported the same things on detach as =
it does on attach but I am not sure how difficult that would be to =
achieve (presumably it would require caching them).

--
Daniel O'Connor
"The nice thing about standards is that there
are so many of them to choose from."
 -- Andrew Tanenbaum





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CC980A9B-8F41-4C2A-A729-2CC690DFDA7E>