serial_msdos.c 9.66 KB
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2018 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.com/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "log.h"
#include "async_wait.h"
#include "timing.h"
#include "io_misc.h"
#include "ports.h"

#include "serial_msdos.h"
#include "serial_internal.h"

#include <dos.h>
#include <dpmi.h>
#include <bios.h>
#include <go32.h>
#include <sys/farptr.h>

#define SERIAL_DIVISOR_BASE 115200
#define SERIAL_DIVISOR(baud) (SERIAL_DIVISOR_BASE / (baud))
#define SERIAL_SPEED(baud) {.divisor=SERIAL_DIVISOR(baud), .bps=SERIAL_BIOS_BAUD_##baud}
#define SERIAL_BAUD_ENTRY(baud) {baud, .speed=SERIAL_SPEED(baud)}

BEGIN_SERIAL_BAUD_TABLE
  SERIAL_BAUD_ENTRY(110),
  SERIAL_BAUD_ENTRY(150),
  SERIAL_BAUD_ENTRY(300),
  SERIAL_BAUD_ENTRY(600),
  SERIAL_BAUD_ENTRY(1200),
  SERIAL_BAUD_ENTRY(2400),
  SERIAL_BAUD_ENTRY(4800),
  SERIAL_BAUD_ENTRY(9600),
  SERIAL_BAUD_ENTRY(19200),
  SERIAL_BAUD_ENTRY(38400),
  SERIAL_BAUD_ENTRY(57600),
  SERIAL_BAUD_ENTRY(115200),
END_SERIAL_BAUD_TABLE

static inline int
serialGetPort (SerialDevice *serial) {
  return serial->package.deviceIndex;
}

static unsigned short
serialPortBase (SerialDevice *serial) {
  return _farpeekw(_dos_ds, (0X0400 + (2 * serialGetPort(serial))));
}

static unsigned char
serialReadPort (SerialDevice *serial, unsigned char port) {
  return readPort1(serialPortBase(serial)+port);
}

static void
serialWritePort (SerialDevice *serial, unsigned char port, unsigned char value) {
  writePort1(serialPortBase(serial)+port, value);
}

static unsigned int
serialBiosCommand (SerialDevice *serial, int command, unsigned char data) {
  return _bios_serialcom(command, serialGetPort(serial), data);
}

static int
serialTestInput (SerialDevice *serial) {
  return !!(serialBiosCommand(serial, _COM_STATUS, 0) & SERIAL_BIOS_STATUS_DATA_READY);
}

void
serialPutInitialAttributes (SerialAttributes *attributes) {
  attributes->speed = serialGetBaudEntry(9600)->speed;
  attributes->bios.fields.bps = attributes->speed.bps;
  attributes->bios.fields.dataBits = SERIAL_BIOS_DATA_8;
  attributes->bios.fields.stopBits = SERIAL_BIOS_STOP_1;
  attributes->bios.fields.parity = SERIAL_BIOS_PARITY_NONE;
}

int
serialPutSpeed (SerialAttributes *attributes, SerialSpeed speed) {
  logMessage(LOG_CATEGORY(SERIAL_IO), "put speed: bps=%u divisor=%u",
             speed.bps, speed.divisor);

  attributes->speed = speed;
  attributes->bios.fields.bps = attributes->speed.bps;
  return 1;
}

int
serialPutDataBits (SerialAttributes *attributes, unsigned int bits) {
  if (bits == 8) {
    attributes->bios.fields.dataBits = SERIAL_BIOS_DATA_8;
  } else if (bits == 7) {
    attributes->bios.fields.dataBits = SERIAL_BIOS_DATA_7;
  } else {
    return 0;
  }

  return 1;
}

int
serialPutStopBits (SerialAttributes *attributes, SerialStopBits bits) {
  if (bits == SERIAL_STOP_1) {
    attributes->bios.fields.stopBits = SERIAL_BIOS_STOP_1;
  } else if (bits == SERIAL_STOP_2) {
    attributes->bios.fields.stopBits = SERIAL_BIOS_STOP_2;
  } else {
    return 0;
  }

  return 1;
}

int
serialPutParity (SerialAttributes *attributes, SerialParity parity) {
  switch (parity) {
    case SERIAL_PARITY_NONE:
      attributes->bios.fields.parity = SERIAL_BIOS_PARITY_NONE;
      break;

    case SERIAL_PARITY_ODD:
      attributes->bios.fields.parity = SERIAL_BIOS_PARITY_ODD;
      break;

    case SERIAL_PARITY_EVEN:
      attributes->bios.fields.parity = SERIAL_BIOS_PARITY_EVEN;
      break;

    default:
      return 0;
  }

  return 1;
}

SerialFlowControl
serialPutFlowControl (SerialAttributes *attributes, SerialFlowControl flow) {
  return flow;
}

int
serialPutModemState (SerialAttributes *attributes, int enabled) {
  return !enabled;
}

unsigned int
serialGetDataBits (const SerialAttributes *attributes) {
  switch (attributes->bios.fields.dataBits) {
    default:
    case SERIAL_BIOS_DATA_8: return 8;
    case SERIAL_BIOS_DATA_7: return 7;
  }
}

unsigned int
serialGetStopBits (const SerialAttributes *attributes) {
  switch (attributes->bios.fields.stopBits) {
    default:
    case SERIAL_BIOS_STOP_1: return 1;
    case SERIAL_BIOS_STOP_2: return 2;
  }
}

unsigned int
serialGetParityBits (const SerialAttributes *attributes) {
  return (attributes->bios.fields.parity == SERIAL_BIOS_PARITY_NONE)? 0: 1;
}

int
serialGetAttributes (SerialDevice *serial, SerialAttributes *attributes) {
  unsigned char lcr;
  int divisor;

  {
    int wasEnabled = disable();

    lcr = serialReadPort(serial, UART_PORT_LCR);
    serialWritePort(serial, UART_PORT_LCR, (lcr | UART_FLAG_LCR_DLAB));

    divisor = (serialReadPort(serial, UART_PORT_DLH) << 8) |
               serialReadPort(serial, UART_PORT_DLL);
    serialWritePort(serial, UART_PORT_LCR, lcr);

    if (wasEnabled) enable();
  }

  attributes->bios.byte = lcr;

  {
    const SerialBaudEntry *baud = serialGetBaudEntry(SERIAL_DIVISOR_BASE/divisor);

    if (baud) {
      attributes->speed = baud->speed;
    } else {
      logMessage(LOG_WARNING, "unsupported serial divisor: %d", divisor);
      memset(&attributes->speed, 0, sizeof(attributes->speed));
    }
  }

  attributes->bios.fields.bps = attributes->speed.bps;
  return 1;
}

int
serialPutAttributes (SerialDevice *serial, const SerialAttributes *attributes) {
  if (attributes->speed.bps < (0X1 << 3)) {
    unsigned char byte = attributes->bios.byte;

    logMessage(LOG_CATEGORY(SERIAL_IO), "put attributes: port=%d byte=0X%02X",
               serialGetPort(serial), byte);
    serialBiosCommand(serial, _COM_INIT, byte);
  } else {
    SerialBiosConfiguration lcr = attributes->bios;

    lcr.fields.bps = 0;
    logMessage(LOG_CATEGORY(SERIAL_IO), "put attributes: port=%d lcr=0X%02X divisor=%u",
               serialGetPort(serial), lcr.byte, attributes->speed.divisor);

    {
      int wasEnabled = disable();

      serialWritePort(serial, UART_PORT_LCR, (lcr.byte | UART_FLAG_LCR_DLAB));
      serialWritePort(serial, UART_PORT_DLL, (attributes->speed.divisor & 0XFF));
      serialWritePort(serial, UART_PORT_DLH, (attributes->speed.divisor >> 8));
      serialWritePort(serial, UART_PORT_LCR, lcr.byte);

      if (wasEnabled) enable();
    }
  }

  return 1;
}

int
serialCancelInput (SerialDevice *serial) {
  return 1;
}

int
serialCancelOutput (SerialDevice *serial) {
  return 1;
}

int
serialMonitorInput (SerialDevice *serial, AsyncMonitorCallback *callback, void *data) {
  return 0;
}

int
serialPollInput (SerialDevice *serial, int timeout) {
  TimePeriod period;

  if (timeout) startTimePeriod(&period, timeout);

  while (1) {
    if (serialTestInput(serial)) return 1;
    if (!timeout) break;
    if (afterTimePeriod(&period, NULL)) break;
    asyncWait(1);
  }

  errno = EAGAIN;
  return 0;
}

int
serialDrainOutput (SerialDevice *serial) {
  return 1;
}

ssize_t
serialGetData (
  SerialDevice *serial,
  void *buffer, size_t size,
  int initialTimeout, int subsequentTimeout
) {
  unsigned char *const start = buffer;
  unsigned char *const end = start + size;
  unsigned char *byte = start;
  int timeout = initialTimeout;

  while (byte < end) {
    if (!serialPollInput(serial, timeout)) break;
    timeout = subsequentTimeout;

    {
      int status = serialBiosCommand(serial, _COM_RECEIVE, 0);

      *byte++ = status & 0XFF;
    }
  }

  return byte - start;
}

ssize_t
serialPutData (
  SerialDevice *serial,
  const void *data, size_t size
) {
  return writeFile(serial->fileDescriptor, data, size);
}

int
serialGetLines (SerialDevice *serial) {
  serial->linesState = serialReadPort(serial, UART_PORT_MSR) & 0XF0;
  return 1;
}

int
serialPutLines (SerialDevice *serial, SerialLines high, SerialLines low) {
  int wasEnabled = disable();
  unsigned char oldMCR = serialReadPort(serial, UART_PORT_MCR);

  serialWritePort(serial, UART_PORT_MCR,
                  (oldMCR | high) & ~low);
  if (wasEnabled) enable();
  return 1;
}

int
serialRegisterWaitLines (SerialDevice *serial, SerialLines lines) {
  return 1;
}

int
serialMonitorWaitLines (SerialDevice *serial) {
  return 0;
}

int
serialConnectDevice (SerialDevice *serial, const char *device) {
  if ((serial->fileDescriptor = open(device, O_RDWR|O_NOCTTY|O_NONBLOCK)) != -1) {
    serial->package.deviceIndex = -1;

    {
      char *truePath;

      if ((truePath = _truename(device, NULL))) {
        char *com;

        {
          char *c = truePath;

          while (*c) {
            *c = toupper(*c);
            c += 1;
          }
        }

        if ((com = strstr(truePath, "COM"))) {
          serial->package.deviceIndex = atoi(com+3) - 1;
        }

        free(truePath);
      }
    }

    if (serial->package.deviceIndex >= 0) {
      if (serialPrepareDevice(serial)) {
        logMessage(LOG_CATEGORY(SERIAL_IO), "device opened: %s: fd=%d",
                   device, serial->fileDescriptor);
        return 1;
      }
    } else {
      logMessage(LOG_ERR, "could not determine serial device number: %s", device);
    }

    close(serial->fileDescriptor);
  } else {
    logMessage(LOG_ERR, "cannot open serial device: %s: %s", device, strerror(errno));
  }

  return 0;
}

void
serialDisconnectDevice (SerialDevice *serial) {
}

int
serialEnsureFileDescriptor (SerialDevice *serial) {
  return 1;
}

void
serialClearError (SerialDevice *serial) {
}