]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Use new SP_ERR_SUPP error to indicate unsupported operations.
[libserialport.git] / serialport.c
index 065a224603b7669de622ac35d28eec962e0fc1d2..007de4372c4134edf4d821a9a033ab430781a85a 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
  * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
  * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
+ * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as
 #ifdef __APPLE__
 #include <IOKit/IOKitLib.h>
 #include <IOKit/serial/IOSerialKeys.h>
+#include <IOKit/serial/ioss.h>
 #include <sys/syslimits.h>
 #endif
 #ifdef __linux__
 #include "libudev.h"
 #include "linux/serial.h"
+#include "linux_termios.h"
+#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
+#define USE_TERMIOX
+#endif
 #endif
 
 #include "libserialport.h"
@@ -52,6 +58,8 @@ struct port_data {
 #else
        struct termios term;
        int controlbits;
+       int termiox_supported;
+       int flow;
 #endif
 };
 
@@ -77,13 +85,14 @@ const struct std_baudrate std_baudrates[] = {
         */
        BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
        BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
-       BAUD(115200), BAUD(128000), BAUD(256000)
+       BAUD(115200), BAUD(128000), BAUD(256000),
 #else
-       BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200), BAUD(300),
-       BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800), BAUD(9600),
-       BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200), BAUD(230400),
+       BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
+       BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
+       BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
+       BAUD(230400),
 #if !defined(__APPLE__) && !defined(__OpenBSD__)
-       BAUD(460800)
+       BAUD(460800),
 #endif
 #endif
 };
@@ -91,13 +100,17 @@ const struct std_baudrate std_baudrates[] = {
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
 
+#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
+
 /* Helper functions. */
-static int validate_port(struct sp_port *port);
+static enum sp_return validate_port(struct sp_port *port);
 static struct sp_port **list_append(struct sp_port **list, const char *portname);
-static int get_config(struct sp_port *port, struct port_data *data, struct sp_port_config *config);
-static int set_config(struct sp_port *port, struct port_data *data, struct sp_port_config *config);
+static enum sp_return get_config(struct sp_port *port, struct port_data *data,
+       struct sp_port_config *config);
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
+       const struct sp_port_config *config);
 
-int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
+enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
 {
        struct sp_port *port;
        int len;
@@ -115,8 +128,7 @@ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
 
        len = strlen(portname) + 1;
 
-       if (!(port->name = malloc(len)))
-       {
+       if (!(port->name = malloc(len))) {
                free(port);
                return SP_ERR_MEM;
        }
@@ -134,7 +146,7 @@ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
        return SP_OK;
 }
 
-int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
+enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
 {
        if (!copy_ptr)
                return SP_ERR_ARG;
@@ -177,7 +189,7 @@ fail:
        return NULL;
 }
 
-int sp_list_ports(struct sp_port ***list_ptr)
+enum sp_return sp_list_ports(struct sp_port ***list_ptr)
 {
        struct sp_port **list;
        int ret = SP_OK;
@@ -197,25 +209,21 @@ int sp_list_ports(struct sp_port ***list_ptr)
        int name_len;
 
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
-                       0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
-       {
+                       0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
                ret = SP_ERR_FAIL;
                goto out_done;
        }
        if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-                               &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS)
-       {
+                               &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
                ret = SP_ERR_FAIL;
                goto out_close;
        }
        max_data_len = max_data_size / sizeof(TCHAR);
-       if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR))))
-       {
+       if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
                ret = SP_ERR_MEM;
                goto out_close;
        }
-       if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR))))
-       {
+       if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
                ret = SP_ERR_MEM;
                goto out_free_value;
        }
@@ -232,8 +240,7 @@ int sp_list_ports(struct sp_port ***list_ptr)
 #else
                name_len = data_len + 1;
 #endif
-               if (!(name = malloc(name_len)))
-               {
+               if (!(name = malloc(name_len))) {
                        ret = SP_ERR_MEM;
                        goto out;
                }
@@ -242,8 +249,7 @@ int sp_list_ports(struct sp_port ***list_ptr)
 #else
                strcpy(name, data);
 #endif
-               if (type == REG_SZ && !(list = list_append(list, name)))
-               {
+               if (type == REG_SZ && !(list = list_append(list, name))) {
                        ret = SP_ERR_MEM;
                        goto out;
                }
@@ -266,14 +272,12 @@ out_done:
        CFTypeRef cf_path;
        Boolean result;
 
-       if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS)
-       {
+       if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
                ret = SP_ERR_FAIL;
                goto out_done;
        }
 
-       if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue)))
-       {
+       if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
                ret = SP_ERR_FAIL;
                goto out_done;
        }
@@ -281,14 +285,12 @@ out_done:
        CFDictionarySetValue(classes,
                        CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
 
-       if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS)
-       {
+       if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
                ret = SP_ERR_FAIL;
                goto out_done;
        }
 
-       if (!(path = malloc(PATH_MAX)))
-       {
+       if (!(path = malloc(PATH_MAX))) {
                ret = SP_ERR_MEM;
                goto out_release;
        }
@@ -300,8 +302,7 @@ out_done:
                        result = CFStringGetCString(cf_path,
                                        path, PATH_MAX, kCFStringEncodingASCII);
                        CFRelease(cf_path);
-                       if (result && !(list = list_append(list, path)))
-                       {
+                       if (result && !(list = list_append(list, path))) {
                                ret = SP_ERR_MEM;
                                IOObjectRelease(port);
                                goto out;
@@ -332,14 +333,12 @@ out_done:
        udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
        udev_enumerate_scan_devices(ud_enumerate);
        ud_list = udev_enumerate_get_list_entry(ud_enumerate);
-       udev_list_entry_foreach(ud_entry, ud_list)
-       {
+       udev_list_entry_foreach(ud_entry, ud_list) {
                path = udev_list_entry_get_name(ud_entry);
                ud_dev = udev_device_new_from_syspath(ud, path);
                /* If there is no parent device, this is a virtual tty. */
                ud_parent = udev_device_get_parent(ud_dev);
-               if (ud_parent == NULL)
-               {
+               if (ud_parent == NULL) {
                        udev_device_unref(ud_dev);
                        continue;
                }
@@ -348,8 +347,7 @@ out_done:
                 * The only way to tell which actually exist on a given system
                 * is to try to open them and make an ioctl call. */
                driver = udev_device_get_driver(ud_parent);
-               if (driver && !strcmp(driver, "serial8250"))
-               {
+               if (driver && !strcmp(driver, "serial8250")) {
                        if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
                                goto skip;
                        ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
@@ -362,8 +360,7 @@ out_done:
                list = list_append(list, name);
 skip:
                udev_device_unref(ud_dev);
-               if (!list)
-               {
+               if (!list) {
                        ret = SP_ERR_MEM;
                        goto out;
                }
@@ -373,15 +370,11 @@ out:
        udev_unref(ud);
 #endif
 
-       if (ret == SP_OK)
-       {
+       if (ret == SP_OK) {
                *list_ptr = list;
-       }
-       else
-       {
+       } else {
                if (list)
                        sp_free_port_list(list);
-
                *list_ptr = NULL;
        }
 
@@ -397,7 +390,7 @@ void sp_free_port_list(struct sp_port **list)
        free(list);
 }
 
-static int validate_port(struct sp_port *port)
+static enum sp_return validate_port(struct sp_port *port)
 {
        if (port == NULL)
                return 0;
@@ -413,7 +406,7 @@ static int validate_port(struct sp_port *port)
 
 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
 
-int sp_open(struct sp_port *port, int flags)
+enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
 {
        if (!port)
                return SP_ERR_ARG;
@@ -428,9 +421,10 @@ int sp_open(struct sp_port *port, int flags)
        sprintf(escaped_port_name, "\\\\.\\%s", port->name);
 
        /* Map 'flags' to the OS-specific settings. */
-       desired_access |= GENERIC_READ;
        flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
-       if (flags & SP_MODE_RDWR)
+       if (flags & SP_MODE_READ)
+               desired_access |= GENERIC_READ;
+       if (flags & SP_MODE_WRITE)
                desired_access |= GENERIC_WRITE;
        if (flags & SP_MODE_NONBLOCK)
                flags_and_attributes |= FILE_FLAG_OVERLAPPED;
@@ -449,10 +443,12 @@ int sp_open(struct sp_port *port, int flags)
        int ret;
 
        /* Map 'flags' to the OS-specific settings. */
-       if (flags & SP_MODE_RDWR)
+       if (flags & (SP_MODE_READ | SP_MODE_WRITE))
                flags_local |= O_RDWR;
-       if (flags & SP_MODE_RDONLY)
+       else if (flags & SP_MODE_READ)
                flags_local |= O_RDONLY;
+       else if (flags & SP_MODE_WRITE)
+               flags_local |= O_WRONLY;
        if (flags & SP_MODE_NONBLOCK)
                flags_local |= O_NONBLOCK;
 
@@ -461,8 +457,7 @@ int sp_open(struct sp_port *port, int flags)
 
        ret = get_config(port, &data, &config);
 
-       if (ret < 0)
-       {
+       if (ret < 0) {
                sp_close(port);
                return ret;
        }
@@ -481,8 +476,7 @@ int sp_open(struct sp_port *port, int flags)
 
        ret = set_config(port, &data, &config);
 
-       if (ret < 0)
-       {
+       if (ret < 0) {
                sp_close(port);
                return ret;
        }
@@ -491,7 +485,7 @@ int sp_open(struct sp_port *port, int flags)
        return SP_OK;
 }
 
-int sp_close(struct sp_port *port)
+enum sp_return sp_close(struct sp_port *port)
 {
        CHECK_PORT();
 
@@ -510,23 +504,54 @@ int sp_close(struct sp_port *port)
        return SP_OK;
 }
 
-int sp_flush(struct sp_port *port)
+enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
 {
        CHECK_PORT();
 
 #ifdef _WIN32
+       DWORD flags = 0;
+       if (buffers & SP_BUF_INPUT)
+               flags |= PURGE_RXCLEAR;
+       if (buffers & SP_BUF_OUTPUT)
+               flags |= PURGE_TXCLEAR;
+
        /* Returns non-zero upon success, 0 upon failure. */
-       if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
+       if (PurgeComm(port->hdl, flags) == 0)
                return SP_ERR_FAIL;
 #else
+       int flags = 0;
+       if (buffers & SP_BUF_BOTH)
+               flags = TCIOFLUSH;
+       else if (buffers & SP_BUF_INPUT)
+               flags = TCIFLUSH;
+       else if (buffers & SP_BUF_OUTPUT)
+               flags = TCOFLUSH;
+
        /* Returns 0 upon success, -1 upon failure. */
-       if (tcflush(port->fd, TCIOFLUSH) < 0)
+       if (tcflush(port->fd, flags) < 0)
                return SP_ERR_FAIL;
 #endif
        return SP_OK;
 }
 
-int sp_write(struct sp_port *port, const void *buf, size_t count)
+enum sp_return sp_drain(struct sp_port *port)
+{
+       CHECK_PORT();
+
+#ifdef _WIN32
+       /* Returns non-zero upon success, 0 upon failure. */
+       if (FlushFileBuffers(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       /* Returns 0 upon success, -1 upon failure. */
+       if (tcdrain(port->fd) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
+enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
 {
        CHECK_PORT();
 
@@ -551,7 +576,7 @@ int sp_write(struct sp_port *port, const void *buf, size_t count)
 #endif
 }
 
-int sp_read(struct sp_port *port, void *buf, size_t count)
+enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
 {
        CHECK_PORT();
 
@@ -575,7 +600,98 @@ int sp_read(struct sp_port *port, void *buf, size_t count)
 #endif
 }
 
-static int get_config(struct sp_port *port, struct port_data *data, struct sp_port_config *config)
+#ifdef __linux__
+static enum sp_return get_baudrate(int fd, int *baudrate)
+{
+       void *data;
+
+       if (!(data = malloc(get_termios_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       *baudrate = get_termios_speed(data);
+
+       free(data);
+
+       return SP_OK;
+}
+
+static enum sp_return set_baudrate(int fd, int baudrate)
+{
+       void *data;
+
+       if (!(data = malloc(get_termios_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       set_termios_speed(data, baudrate);
+
+       if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       free(data);
+
+       return SP_OK;
+}
+
+#ifdef USE_TERMIOX
+static enum sp_return get_flow(int fd, int *flow)
+{
+       void *data;
+
+       if (!(data = malloc(get_termiox_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, TCGETX, data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       *flow = get_termiox_flow(data);
+
+       free(data);
+
+       return SP_OK;
+}
+
+static enum sp_return set_flow(int fd, int flow)
+{
+       void *data;
+
+       if (!(data = malloc(get_termiox_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, TCGETX, data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       set_termiox_flow(data, flow);
+
+       if (ioctl(fd, TCSETX, data) < 0) {
+               free(data);
+               return SP_ERR_FAIL;
+       }
+
+       free(data);
+
+       return SP_OK;
+}
+#endif /* USE_TERMIOX */
+#endif /* __linux__ */
+
+static enum sp_return get_config(struct sp_port *port, struct port_data *data,
+       struct sp_port_config *config)
 {
        unsigned int i;
 
@@ -625,33 +741,33 @@ static int get_config(struct sp_port *port, struct port_data *data, struct sp_po
        }
 
        switch (data->dcb.fRtsControl) {
-               case RTS_CONTROL_DISABLE:
-                       config->rts = SP_RTS_OFF;
-                       break;
-               case RTS_CONTROL_ENABLE:
-                       config->rts = SP_RTS_ON;
-                       break;
-               case RTS_CONTROL_HANDSHAKE:
-                       config->rts = SP_RTS_FLOW_CONTROL;
-                       break;
-               default:
-                       config->rts = -1;
+       case RTS_CONTROL_DISABLE:
+               config->rts = SP_RTS_OFF;
+               break;
+       case RTS_CONTROL_ENABLE:
+               config->rts = SP_RTS_ON;
+               break;
+       case RTS_CONTROL_HANDSHAKE:
+               config->rts = SP_RTS_FLOW_CONTROL;
+               break;
+       default:
+               config->rts = -1;
        }
 
        config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
 
        switch (data->dcb.fDtrControl) {
-               case DTR_CONTROL_DISABLE:
-                       config->dtr = SP_DTR_OFF;
-                       break;
-               case DTR_CONTROL_ENABLE:
-                       config->dtr = SP_DTR_ON;
-                       break;
-               case DTR_CONTROL_HANDSHAKE:
-                       config->dtr = SP_DTR_FLOW_CONTROL;
-                       break;
-               default:
-                       config->dtr = -1;
+       case DTR_CONTROL_DISABLE:
+               config->dtr = SP_DTR_OFF;
+               break;
+       case DTR_CONTROL_ENABLE:
+               config->dtr = SP_DTR_ON;
+               break;
+       case DTR_CONTROL_HANDSHAKE:
+               config->dtr = SP_DTR_FLOW_CONTROL;
+               break;
+       default:
+               config->dtr = -1;
        }
 
        config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
@@ -675,6 +791,20 @@ static int get_config(struct sp_port *port, struct port_data *data, struct sp_po
 
        if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
                return SP_ERR_FAIL;
+
+#ifdef USE_TERMIOX
+       int ret = get_flow(port->fd, &data->flow);
+
+       if (ret == SP_ERR_FAIL && errno == EINVAL)
+               data->termiox_supported = 0;
+       else if (ret < 0)
+               return ret;
+       else
+               data->termiox_supported = 1;
+#else
+       data->termiox_supported = 0;
+#endif
+
        for (i = 0; i < NUM_STD_BAUDRATES; i++) {
                if (cfgetispeed(&data->term) == std_baudrates[i].index) {
                        config->baudrate = std_baudrates[i].value;
@@ -682,8 +812,15 @@ static int get_config(struct sp_port *port, struct port_data *data, struct sp_po
                }
        }
 
-       if (i == NUM_STD_BAUDRATES)
+       if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+               config->baudrate = (int)data->term.c_ispeed;
+#elif defined(__linux__)
+               TRY(get_baudrate(port->fd, &config->baudrate));
+#else
                config->baudrate = -1;
+#endif
+       }
 
        switch (data->term.c_cflag & CSIZE) {
        case CS8:
@@ -715,24 +852,54 @@ static int get_config(struct sp_port *port, struct port_data *data, struct sp_po
                config->rts = SP_RTS_FLOW_CONTROL;
                config->cts = SP_CTS_FLOW_CONTROL;
        } else {
-               config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
-               config->cts = SP_CTS_IGNORE;
+               if (data->termiox_supported && data->flow & RTS_FLOW)
+                       config->rts = SP_RTS_FLOW_CONTROL;
+               else
+                       config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
+
+               config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
+                       SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
        }
 
-       config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
-       config->dsr = SP_DSR_IGNORE;
+       if (data->termiox_supported && data->flow & DTR_FLOW)
+               config->dtr = SP_DTR_FLOW_CONTROL;
+       else
+               config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
+
+       config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
+               SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
+
+       if (data->term.c_iflag & IXOFF) {
+               if (data->term.c_iflag & IXON)
+                       config->xon_xoff = SP_XONXOFF_INOUT;
+               else
+                       config->xon_xoff = SP_XONXOFF_IN;
+       } else {
+               if (data->term.c_iflag & IXON)
+                       config->xon_xoff = SP_XONXOFF_OUT;
+               else
+                       config->xon_xoff = SP_XONXOFF_DISABLED;
+       }
 #endif
 
        return SP_OK;
 }
 
-static int set_config(struct sp_port *port, struct port_data *data, struct sp_port_config *config)
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
+       const struct sp_port_config *config)
 {
        unsigned int i;
+#ifdef __APPLE__
+       BAUD_TYPE baud_nonstd;
+
+       baud_nonstd = B0;
+#endif
+#ifdef __linux__
+       int baud_nonstd = 0;
+#endif
 
 #ifdef _WIN32
-       if (config->baudrate >= 0)
-       {
+       if (config->baudrate >= 0) {
                for (i = 0; i < NUM_STD_BAUDRATES; i++) {
                        if (config->baudrate == std_baudrates[i].value) {
                                data->dcb.BaudRate = std_baudrates[i].index;
@@ -862,10 +1029,11 @@ static int set_config(struct sp_port *port, struct port_data *data, struct sp_po
        if (!SetCommState(port->hdl, &data->dcb))
                return SP_ERR_FAIL;
 
-#else // !_WIN32
+#else /* !_WIN32 */
 
-       if (config->baudrate >= 0)
-       {
+       int controlbits;
+
+       if (config->baudrate >= 0) {
                for (i = 0; i < NUM_STD_BAUDRATES; i++) {
                        if (config->baudrate == std_baudrates[i].value) {
                                if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
@@ -877,8 +1045,19 @@ static int set_config(struct sp_port *port, struct port_data *data, struct sp_po
                        }
                }
 
-               if (i == NUM_STD_BAUDRATES)
-                       return SP_ERR_ARG;
+               /* Non-standard baud rate */
+               if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+                       /* Set "dummy" baud rate. */
+                       if (cfsetspeed(&data->term, B9600) < 0)
+                               return SP_ERR_FAIL;
+                       baud_nonstd = config->baudrate;
+#elif defined(__linux__)
+                       baud_nonstd = 1;
+#else
+                       return SP_ERR_SUPP;
+#endif
+               }
        }
 
        if (config->bits >= 0) {
@@ -893,6 +1072,9 @@ static int set_config(struct sp_port *port, struct port_data *data, struct sp_po
                case 6:
                        data->term.c_cflag |= CS6;
                        break;
+               case 5:
+                       data->term.c_cflag |= CS5;
+                       break;
                default:
                        return SP_ERR_ARG;
                }
@@ -930,50 +1112,90 @@ static int set_config(struct sp_port *port, struct port_data *data, struct sp_po
                }
        }
 
-       if (config->rts >= 0 || config->cts >= 0)
-       {
-               /* Asymmetric use of RTS/CTS not supported yet. */
-
-               if (data->term.c_iflag & CRTSCTS) {
-                       /* Flow control can only be disabled for both RTS & CTS together. */
-                       if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
-                               if (config->cts != SP_CTS_IGNORE)
-                                       return SP_ERR_ARG;
-                       }
-                       if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
-                               if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
-                                       return SP_ERR_ARG;
+       if (config->rts >= 0 || config->cts >= 0) {
+               if (data->termiox_supported) {
+                       data->flow &= ~(RTS_FLOW | CTS_FLOW);
+                       switch (config->rts) {
+                       case SP_RTS_OFF:
+                       case SP_RTS_ON:
+                               controlbits = TIOCM_RTS;
+                               if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
+                                       return SP_ERR_FAIL;
+                               break;
+                       case SP_RTS_FLOW_CONTROL:
+                               data->flow |= RTS_FLOW;
+                               break;
+                       default:
+                               break;
                        }
-               } else {
-                       /* Flow control can only be enabled for both RTS & CTS together. */
-                       if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
-                               ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
-                               return SP_ERR_ARG;
-               }
+                       if (config->cts == SP_CTS_FLOW_CONTROL)
+                               data->flow |= CTS_FLOW;
 
-               if (config->rts >= 0) {
-                       if (config->rts == SP_RTS_FLOW_CONTROL) {
+                       if (data->flow & (RTS_FLOW | CTS_FLOW))
                                data->term.c_iflag |= CRTSCTS;
+                       else
+                               data->term.c_iflag &= ~CRTSCTS;
+               } else {
+                       /* Asymmetric use of RTS/CTS not supported. */
+                       if (data->term.c_iflag & CRTSCTS) {
+                               /* Flow control can only be disabled for both RTS & CTS together. */
+                               if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
+                                       if (config->cts != SP_CTS_IGNORE)
+                                               return SP_ERR_SUPP;
+                               }
+                               if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
+                                       if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
+                                               return SP_ERR_SUPP;
+                               }
                        } else {
-                               int controlbits = TIOCM_RTS;
-                               if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
-                                               &controlbits) < 0)
-                                       return SP_ERR_FAIL;
+                               /* Flow control can only be enabled for both RTS & CTS together. */
+                               if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
+                                       ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
+                                       return SP_ERR_SUPP;
+                       }
+
+                       if (config->rts >= 0) {
+                               if (config->rts == SP_RTS_FLOW_CONTROL) {
+                                       data->term.c_iflag |= CRTSCTS;
+                               } else {
+                                       controlbits = TIOCM_RTS;
+                                       if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
+                                                       &controlbits) < 0)
+                                               return SP_ERR_FAIL;
+                               }
                        }
                }
        }
 
-       if (config->dtr >= 0 || config->dsr >= 0)
-       {
-               /* DTR/DSR flow control not supported yet. */
-               if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
-                       return SP_ERR_ARG;
+       if (config->dtr >= 0 || config->dsr >= 0) {
+               if (data->termiox_supported) {
+                       data->flow &= ~(DTR_FLOW | DSR_FLOW);
+                       switch (config->dtr) {
+                       case SP_DTR_OFF:
+                       case SP_DTR_ON:
+                               controlbits = TIOCM_DTR;
+                               if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
+                                       return SP_ERR_FAIL;
+                               break;
+                       case SP_DTR_FLOW_CONTROL:
+                               data->flow |= DTR_FLOW;
+                               break;
+                       default:
+                               break;
+                       }
+                       if (config->dsr == SP_DSR_FLOW_CONTROL)
+                               data->flow |= DSR_FLOW;
+               } else {
+                       /* DTR/DSR flow control not supported. */
+                       if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
+                               return SP_ERR_SUPP;
 
-               if (config->dtr >= 0) {
-                       int controlbits = TIOCM_DTR;
-                       if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
-                                       &controlbits) < 0)
-                               return SP_ERR_FAIL;
+                       if (config->dtr >= 0) {
+                               controlbits = TIOCM_DTR;
+                               if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
+                                               &controlbits) < 0)
+                                       return SP_ERR_FAIL;
+                       }
                }
        }
 
@@ -998,14 +1220,31 @@ static int set_config(struct sp_port *port, struct port_data *data, struct sp_po
 
        if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
                return SP_ERR_FAIL;
+
+#ifdef __APPLE__
+       if (baud_nonstd != B0) {
+               if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
+                       return SP_ERR_FAIL;
+               /* Set baud rates in data->term to correct, but incompatible
+                * with tcsetattr() value, same as delivered by tcgetattr(). */
+               if (cfsetspeed(&data->term, baud_nonstd) < 0)
+                       return SP_ERR_FAIL;
+       }
+#elif defined(__linux__)
+       if (baud_nonstd)
+               TRY(set_baudrate(port->fd, config->baudrate));
+#ifdef USE_TERMIOX
+       if (data->termiox_supported)
+               TRY(set_flow(port->fd, data->flow));
+#endif
 #endif
 
+#endif /* !_WIN32 */
+
        return SP_OK;
 }
 
-#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
-
-int sp_set_config(struct sp_port *port, struct sp_port_config *config)
+enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
 {
        struct port_data data;
        struct sp_port_config prev_config;
@@ -1021,7 +1260,7 @@ int sp_set_config(struct sp_port *port, struct sp_port_config *config)
        return SP_OK;
 }
 
-#define CREATE_SETTER(x) int sp_set_##x(struct sp_port *port, int x) { \
+#define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
        struct port_data data; \
        struct sp_port_config config; \
        CHECK_PORT(); \
@@ -1031,17 +1270,17 @@ int sp_set_config(struct sp_port *port, struct sp_port_config *config)
        return SP_OK; \
 }
 
-CREATE_SETTER(baudrate)
-CREATE_SETTER(bits)
-CREATE_SETTER(parity)
-CREATE_SETTER(stopbits)
-CREATE_SETTER(rts)
-CREATE_SETTER(cts)
-CREATE_SETTER(dtr)
-CREATE_SETTER(dsr)
-CREATE_SETTER(xon_xoff)
-
-int sp_set_flowcontrol(struct sp_port *port, int flowcontrol)
+CREATE_SETTER(baudrate, int)
+CREATE_SETTER(bits, int)
+CREATE_SETTER(parity, enum sp_parity)
+CREATE_SETTER(stopbits, int)
+CREATE_SETTER(rts, enum sp_rts)
+CREATE_SETTER(cts, enum sp_cts)
+CREATE_SETTER(dtr, enum sp_dtr)
+CREATE_SETTER(dsr, enum sp_dsr)
+CREATE_SETTER(xon_xoff, enum sp_xonxoff)
+
+enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
 {
        struct port_data data;
        struct sp_port_config config;
@@ -1078,6 +1317,70 @@ int sp_set_flowcontrol(struct sp_port *port, int flowcontrol)
        return SP_OK;
 }
 
+enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
+{
+       CHECK_PORT();
+
+       if (!signals)
+               return SP_ERR_ARG;
+
+       *signals = 0;
+#ifdef _WIN32
+       DWORD bits;
+       if (GetCommModemStatus(port->hdl, &bits) == 0)
+               return SP_ERR_FAIL;
+       if (bits & MS_CTS_ON)
+               *signals |= SP_SIG_CTS;
+       if (bits & MS_DSR_ON)
+               *signals |= SP_SIG_DSR;
+       if (bits & MS_RING_ON)
+               *signals |= SP_SIG_DCD;
+       if (bits & MS_RLSD_ON)
+               *signals |= SP_SIG_RI;
+#else
+       int bits;
+       if (ioctl(port->fd, TIOCMGET, &bits) < 0)
+               return SP_ERR_FAIL;
+       if (bits & TIOCM_CTS)
+               *signals |= SP_SIG_CTS;
+       if (bits & TIOCM_DSR)
+               *signals |= SP_SIG_DSR;
+       if (bits & TIOCM_CAR)
+               *signals |= SP_SIG_DCD;
+       if (bits & TIOCM_RNG)
+               *signals |= SP_SIG_RI;
+#endif
+       return SP_OK;
+}
+
+enum sp_return sp_start_break(struct sp_port *port)
+{
+       CHECK_PORT();
+#ifdef _WIN32
+       if (SetCommBreak(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       if (ioctl(port->fd, TIOCSBRK, 1) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
+enum sp_return sp_end_break(struct sp_port *port)
+{
+       CHECK_PORT();
+#ifdef _WIN32
+       if (ClearCommBreak(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       if (ioctl(port->fd, TIOCCBRK, 1) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
 int sp_last_error_code(void)
 {
 #ifdef _WIN32