title: ioctl complains a tty device is not a tty device
body: <p>I am writing a program to open, setup, and write to a tty for rs485. I have played a bit with the tty device, and now I can't seem to open it anymore.</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>int rs485_enable(const char *dev_name, const speed_t speed)
  {
      int fd = open(dev_name, O_RDWR);
      if (fd &lt; 0) {
          perror("open");
          return -1;
      }
  
      if (rs485_speed_set(fd, speed) &lt; 0) {
          perror("rs485_speed_set");
          return -1;
      }
  
      struct serial_rs485 rs485conf = {
          .flags = SER_RS485_ENABLED,
          .delay_rts_before_send = 0x00000004
      };
  
      if (ioctl(fd, TIOCSRS485, &amp;rs485conf) &lt; 0) {
          perror("ioctl");
          return -1;
      }
  
      return fd;
  }
  
  int rs485_speed_set(const int fd, const speed_t speed)
  {
      struct termios tty;
      if (tcgetattr(fd, &amp;tty) &lt; 0) {
          perror("tcgetattr");
          return -1;
      }
  
      cfsetispeed(&amp;tty, speed);
      cfsetospeed(&amp;tty, speed);
  
      if (tcsetattr(fd, TCSADRAIN, &amp;tty) &lt; 0) {
          perror("tcsetattr");
          return -1;
      }
      return 0;
  }
  </code></pre>
  
  <p>, which is called from a test program as such:</p>
  
  <pre><code>int rs485_fd = rs485_enable("/dev/ttyUSBserial", B9600);
  </code></pre>
  
  <p>The <code>ioctl</code> in the <code>rs485_enable</code> function complains with <code>NOTTY</code>. <code>strace</code> says:</p>
  
  <pre><code>open("/dev/ttyUSBserial", O_RDWR)       = 3
  ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
  ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
  ioctl(3, SNDCTL_TMR_STOP or SNDRV_TIMER_IOCTL_GINFO or TCSETSW, {B9600 opost isig icanon echo ...}) = 0
  ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
  ioctl(3, TIOCSRS485, 0x7fff8046f0c0)    = -1 ENOTTY (Inappropriate ioctl for device)
  </code></pre>
  
  <p>and stty says:</p>
  
  <pre><code>$ stty -F /dev/ttyUSBserial -a
  speed 9600 baud; rows 0; columns 0; line = 0;
  intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = &lt;undef&gt;; eol2 = &lt;undef&gt;; swtch = &lt;undef&gt;; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
  werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
  -parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
  -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
  opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
  isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
  </code></pre>
  
  <p>It feels like I have broken the <code>/dev/ttyUSBserial</code> while playing with it, but I don't know how, or how to fix it. Since it is a USB device, I could easily unplug and replug it and hope it resets to some kind of defaults, but I'd rather understand what is going on.</p>