* 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__
}
}
- if (i == NUM_STD_BAUDRATES)
+ if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+ config->baudrate = (int)data->term.c_ispeed;
+#else
config->baudrate = -1;
+#endif
+ }
switch (data->term.c_cflag & CSIZE) {
case CS8:
const struct sp_port_config *config)
{
unsigned int i;
+#ifdef __APPLE__
+ BAUD_TYPE baud_nonstd;
+
+ baud_nonstd = B0;
+#endif
#ifdef _WIN32
if (config->baudrate >= 0) {
if (!SetCommState(port->hdl, &data->dcb))
return SP_ERR_FAIL;
-#else // !_WIN32
+#else /* !_WIN32 */
if (config->baudrate >= 0) {
for (i = 0; i < NUM_STD_BAUDRATES; i++) {
}
}
- if (i == NUM_STD_BAUDRATES)
+ /* 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;
+#else
return SP_ERR_ARG;
+#endif
+ }
}
if (config->bits >= 0) {
if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
return SP_ERR_FAIL;
-#endif
+
+#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;
+ }
+#endif /* __APPLE__ */
+
+#endif /* !_WIN32 */
return SP_OK;
}