serial: flush() after open() in the serial core.

Quite some drivers flush the serial port after opening it. And quite
some don't although they should. Factor this out, so serial_open() will
always flush the port. The removal in the drivers was done with this
small coccinelle script:

@@
struct sr_serial_dev_inst *serial;
@@

        serial_open(serial, ...)
        ... when != serial
-       serial_flush(serial);

and then the results and the unmatched findings of serial_flush() were
audited.

Signed-off-by: Wolfram Sang <wsa@kernel.org>
This commit is contained in:
Wolfram Sang 2020-05-03 22:57:50 +02:00 committed by Gerhard Sittig
parent 3b13990f7d
commit cb828f1b3e
22 changed files with 9 additions and 43 deletions

View File

@ -131,7 +131,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) {
sr_err("Unable to send identification string.");
return NULL;

View File

@ -79,8 +79,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sr_info("Probing serial port %s.", conn);
serial_flush(serial);
/* Let's get a bit of data and see if we can find a packet. */
if (serial_stream_detect(serial, buf, &len, 25,
appa_55ii_packet_valid, 500) != SR_OK)

View File

@ -99,8 +99,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
/*
* First stop potentially running monitoring and wait for 50ms before
* next command can be sent.

View File

@ -113,8 +113,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options, int modelid)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
/* This is how the vendor software scans for hardware. */
memset(packet, 0, PACKET_SIZE);
packet[0] = 0xaa;

View File

@ -67,8 +67,6 @@ static GSList *center_scan(const char *conn, const char *serialcomm, int idx)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
sr_info("Found device on port %s.", conn);
sdi = g_malloc0(sizeof(struct sr_dev_inst));

View File

@ -75,7 +75,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
serial_close(serial);
sr_spew("Conrad DIGI 35 CPU assumed at %s.", conn);

View File

@ -102,6 +102,7 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
* Try to find message consisting of device code and several
* (at least 4) data bytes.
*/
serial_flush(serial);
for (bytecnt = 0; bytecnt < 100; bytecnt++) {
byte = read_byte(serial, timeout_us);
if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
@ -175,8 +176,6 @@ static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
return NULL;
}
serial_flush(serial);
model = scan_model_sm(serial);
/*
@ -187,10 +186,8 @@ static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
serialcomm = SERIALCOMM_1X_RS232;
g_free(serial->serialcomm);
serial->serialcomm = g_strdup(serialcomm);
if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
serial_flush(serial);
if (serial_set_paramstr(serial, serialcomm) == SR_OK)
model = scan_model_sm(serial);
}
}
if (model != METRAHIT_NONE) {

View File

@ -107,7 +107,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
gpd_send_cmd(serial, "*IDN?\n");
if (gpd_receive_reply(serial, reply, sizeof(reply)) != SR_OK) {
sr_err("Device did not reply.");

View File

@ -79,7 +79,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sr_info("Probing serial port %s.", conn);
devices = NULL;
serial_flush(serial);
sr_spew("Set O1 mode (continuous values, stable and unstable ones).");
if (serial_write_blocking(serial, "O1\r\n", 4, 0) < 0)

View File

@ -128,8 +128,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
/* Get the device model. */
len = 0;
for (i = 0; models[i].id; i++) {

View File

@ -118,8 +118,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
sr_info("Probing serial port %s.", conn);
/* Get the device model. */

View File

@ -81,8 +81,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sr_info("Probing serial port %s.", conn);
serial_flush(serial);
/* Let's get a bit of data and see if we can find a packet. */
if (serial_stream_detect(serial, buf, &len, (2 * MASTECH_MS6514_FRAME_SIZE),
mastech_ms6514_packet_valid, 500) != SR_OK)

View File

@ -67,8 +67,6 @@ static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
/* TODO: Query device type. */
// ret = mic_cmd_get_device_info(serial);

View File

@ -378,7 +378,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
goto exit_err;
/* Query and verify model string. */
serial_flush(serial);
if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
return NULL;

View File

@ -99,8 +99,6 @@ static GSList *scan(struct sr_dev_driver *drv, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serial_flush(serial);
buf = g_malloc(BUF_MAX);
snprintf(req, sizeof(req), "%s\r\n",

View File

@ -85,7 +85,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sr_info("Probing serial port %s.", conn);
devices = NULL;
serial_flush(serial);
/* Request a packet if the DMM requires this. */
if (dmm->packet_request) {

View File

@ -105,7 +105,6 @@ static int scan_lcr_port(const struct lcr_info *lcr,
* send data periodically. So we check if the packets match the
* probed device's expected format.
*/
serial_flush(serial);
if (lcr->packet_request) {
ret = lcr->packet_request(serial);
if (ret < 0) {

View File

@ -75,8 +75,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sr_info("Probing serial port %s.", conn);
serial_flush(serial);
/* Let's get a bit of data and see if we can find a packet. */
if (serial_stream_detect(serial, buf, &len, len,
teleinfo_packet_valid, 3000) != SR_OK)

View File

@ -90,7 +90,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
devices = NULL;
serial = sr_serial_dev_inst_new(conn, serialcomm);
rc = serial_open(serial, SERIAL_RDWR);
serial_flush(serial);
/* Cannot query/identify the device. Successful open shall suffice. */
serial_close(serial);
if (rc != SR_OK) {

View File

@ -57,9 +57,6 @@ static int modbus_serial_rtu_open(void *priv)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
if (serial_flush(serial) != SR_OK)
return SR_ERR;
return SR_OK;
}

View File

@ -114,9 +114,6 @@ static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
if (serial_flush(serial) != SR_OK)
return SR_ERR;
sscpi->got_newline = FALSE;
return SR_OK;

View File

@ -122,10 +122,13 @@ SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
if (ret != SR_OK)
return ret;
if (serial->serialcomm)
return serial_set_paramstr(serial, serial->serialcomm);
else
return SR_OK;
if (serial->serialcomm) {
ret = serial_set_paramstr(serial, serial->serialcomm);
if (ret != SR_OK)
return ret;
}
return serial_flush(serial);
}
/**