<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>
    </p>
    <div class="moz-text-html" lang="x-unicode">
      <div class="WordSection1">
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">Hello,<br>
          </span></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">I have multiple exactly same USB
            devices. Each device enumerates multiply ttyACM ports under
            /dev directory. Each port has it unique purpose, one is for
            handling some commands, another is diagnostic, etc. What I
            want is to alias tty's for particular device by creating
            symlinks with the ability to distinguish to which device
            this port belongs.</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">For
            example:</span></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><i><span
              style="color:#242729">    DEVICE_UNIQUENUM_PURPOSE</span></i></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><i><span
              style="color:#242729">    /dev/MODULE_0_DIAG<br>
                  /dev/MODULE_0_CMD<br>
                  /dev/MODULE_1_DIAG<br>
                  /dev/MODULE_1_CMD</span></i></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">To achieve this I started working with
            udev to write rules for my device. I learned that udev is
            not able to enumerate symlinks for the same device in the
            sophisticated way I want it. So I decided to write bash
            script which do all the work. Here are the rules:</span></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><i><span
              style="color:#242729">    ACTION=="add",
              KERNEL=="ttyACM[0-9]*", SUBSYSTEM=="tty",
              SUBSYSTEMS=="usb", ATTRS{idVendor}=="1e2d",
              ATTRS{idProduct}=="0063", PROGRAM="/bin/bash
              /home/user/script.sh %k", SYMLINK+="%c"<br>
                  ACTION=="remove", SUBSYSTEM=="usb", DRIVER=="usb",
              ATTRS{idVendor}=="1e2d", ATTRS{idProduct}=="0063",
              PROGRAM="/bin/bash /home/user/script.sh"</span></i></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">Basically script starts by checking
            what action did occur "add" or "remove" and takes next steps
            based on that. The tricky part is that rule for "add" will
            fire script multiple times at once cause there are multiple
            tty devices handled at the same time. So to synchronize my
            script I implemented simple mutex based on creating
            directory at some place. If directory is present then other
            scripts will wait until they can create one. Moving forward,
            add operation:</span></p>
        <p class="MsoNormal"
          style="margin-left:22.5pt;vertical-align:baseline"><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);"><span
              style="mso-list:Ignore">1.<span style="font-style: normal;
                font-variant: normal; font-weight: normal; font-stretch:
                normal; font-size: 7pt; line-height: normal;
                font-size-adjust: none; font-kerning: auto;
                font-language-override: normal; font-feature-settings:
                normal;">      </span></span></span><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);">Creates
            temporary database if not present<span
              style="mso-list:Ignore"><br>
              2.<span style="font-style: normal; font-variant: normal;
                font-weight: normal; font-stretch: normal; font-size:
                7pt; line-height: normal; font-size-adjust: none;
                font-kerning: auto; font-language-override: normal;
                font-feature-settings: normal;">      </span></span></span><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);">Based on
            information passed by udev it determines what type of device
            is that and what unique number it should give it.<span
              style="mso-list:Ignore"><br>
              3.<span style="font-style: normal; font-variant: normal;
                font-weight: normal; font-stretch: normal; font-size:
                7pt; line-height: normal; font-size-adjust: none;
                font-kerning: auto; font-language-override: normal;
                font-feature-settings: normal;">      </span></span></span><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);">Writes
            record to database for this particular tty</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">Remove
            operation:</span></p>
        <p class="MsoNormal"
          style="margin-left:22.5pt;vertical-align:baseline"><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);"><span
              style="mso-list:Ignore">1.<span style="font-style: normal;
                font-variant: normal; font-weight: normal; font-stretch:
                normal; font-size: 7pt; line-height: normal;
                font-size-adjust: none; font-kerning: auto;
                font-language-override: normal; font-feature-settings:
                normal;">      </span></span></span><span
            style="font-size: 11.5pt; color: rgb(36, 39, 41);">Based on
            serial number passed from udev it should remove all records
            for matching tty devices.</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">Now the
            problem. For some reason action "remove" is not triggering
            my script properly. For action "add" all works flawlessly.
            Upon device plug in records are written to database file
            correctly for each tty. When I plug out device script most
            of the time won't even run. Sometimes it will. Previously I
            had almost same rule for action "remove" as for "add":</span></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><i><span
              style="color:#242729">    ACTION=="remove",
              KERNEL=="ttyACM[0-9]*", SUBSYSTEM=="tty",
              SUBSYSTEMS=="usb", ATTRS{idVendor}=="1e2d",
              ATTRS{idProduct}=="0063", PROGRAM="/bin/bash
              /home/user/script.sh %k"</span></i></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">This one sometimes run script for one
            tty, sometimes for more. Completely nondeterministicly. I
            have no idea why is this behaving like this.</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">When I'm
            using "udevadm test" utility to simulate action "remove" for
            device then it works perfectly every time. But for real plug
            out it is not. I also checked with "udevadm monitor" what
            events are occuring and "remove" actions are present for
            device so everything looks correct.</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">Another
            thing I tried was to change "PROGRAM" command to "RUN"
            command but it did not helped.</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">After
            that I decided to do the simplest test possible. I have
            written this rule:</span></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><i><span
              style="color:#242729">    ACTION=="remove",
              SUBSYSTEM=="usb", DRIVER=="usb", ATTRS{idVendor}=="1244",
              ATTRS{idProduct}=="206d", RUN+="/bin/touch
              /home/user/udev/%k"</span></i></p>
        <p
style="margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white"><span
            style="color:#242729">This one suppose to create file named
            after "KERNEL" param of the device. This is also not
            happening! But for action "add" it works!</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">This
            drives me nuts. Am I missing something? I know udev
            specifies that only short tasks should be runned via
            "PROGRAM/RUN" but if this is not short task then what is?
            Also everything works for action "add". I'm simply out of
            ideas. Maybe this is udev flaw?</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">Thanks,</span></p>
        <p
style="mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;margin-left:0in;background:white;font-variant-ligatures:
          normal;font-variant-caps: normal;font-variant-numeric:
          inherit;font-variant-east-asian: inherit;font-stretch:
          inherit;line-height:inherit;baseline;box-sizing:
          inherit;orphans: 2;widows: 2;-webkit-text-stroke-width:
          0px;text-decoration-style: initial;text-decoration-color:
          initial;word-spacing:0px"><span style="color:#242729">Ziemowit
            Podwysocki<br>
          </span></p>
        <p class="MsoNormal"><span lang="PL"> </span></p>
      </div>
    </div>
  </body>
</html>