Replace native calls in serprog with wrapper calls

Read(), write(), usleep() are replaced.

Corresponding to flashrom svn r1663.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Signed-off-by: Idwer Vollering <vidwer@gmail.com>
Acked-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
diff --git a/serial.c b/serial.c
index 8e27aa0..05f853e 100644
--- a/serial.c
+++ b/serial.c
@@ -354,7 +354,7 @@
 			return 1;
 		}
 		if (!tmp)
-			msg_pdbg("Empty read\n");
+			msg_pdbg2("Empty read\n");
 		readcnt -= tmp;
 		buf += tmp;
 	}
diff --git a/serprog.c b/serprog.c
index ae01d29..b68fc49 100644
--- a/serprog.c
+++ b/serprog.c
@@ -133,7 +133,7 @@
 		goto err_out;
 	}
 	/* A second should be enough to get all the answers to the buffer */
-	usleep(1000 * 1000);
+	internal_delay(1000 * 1000);
 	sp_flush_incoming();
 
 	/* Then try up to 8 times to send syncnop and get the correct special *
@@ -206,15 +206,15 @@
 	unsigned char c;
 	if (sp_automatic_cmdcheck(command))
 		return 1;
-	if (write(sp_fd, &command, 1) != 1) {
+	if (serialport_write(&command, 1) != 0) {
 		msg_perr("Error: cannot write op code: %s\n", strerror(errno));
 		return 1;
 	}
-	if (write(sp_fd, params, parmlen) != (parmlen)) {
+	if (serialport_write(params, parmlen) != 0) {
 		msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
 		return 1;
 	}
-	if (read(sp_fd, &c, 1) != 1) {
+	if (serialport_read(&c, 1) != 0) {
 		msg_perr("Error: cannot read from device: %s\n", strerror(errno));
 		return 1;
 	}
@@ -225,17 +225,10 @@
 		return 1;
 	}
 	if (retlen) {
-		int rd_bytes = 0;
-		do {
-			int r;
-			r = read(sp_fd, retparms + rd_bytes,
-				 retlen - rd_bytes);
-			if (r <= 0) {
-				msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
-				return 1;
-			}
-			rd_bytes += r;
-		} while (rd_bytes != retlen);
+		if (serialport_read(retparms, retlen) != 0) {
+			msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
+			return 1;
+		}
 	}
 	return 0;
 }
@@ -245,7 +238,7 @@
 	if (sp_streamed_transmit_ops)
 		do {
 			unsigned char c;
-			if (read(sp_fd, &c, 1) != 1) {
+			if (serialport_read(&c, 1) != 0) {
 				sp_die("Error: cannot read from device (flushing stream)");
 			}
 			if (c == S_NAK) {
@@ -272,7 +265,7 @@
 	memcpy(&(sp[1]), parms, parmlen);
 	if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
 		sp_flush_stream();
-	if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
+	if (serialport_write(sp, 1 + parmlen) != 0)
 		sp_die("Error: cannot write command");
 	free(sp);
 	sp_streamed_transmit_ops += 1;
@@ -685,10 +678,9 @@
 	header[4] = (sp_write_n_addr >> 0) & 0xFF;
 	header[5] = (sp_write_n_addr >> 8) & 0xFF;
 	header[6] = (sp_write_n_addr >> 16) & 0xFF;
-	if (write(sp_fd, header, 7) != 7)
+	if (serialport_write(header, 7) != 0)
 		sp_die("Error: cannot write write-n command\n");
-	if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
-	    sp_write_n_bytes)
+	if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0)
 		sp_die("Error: cannot write write-n data");
 	sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
 	sp_streamed_transmit_ops += 1;
@@ -789,7 +781,7 @@
 	buf[2] = ((addr >> 16) & 0xFF);
 	sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
 	sp_flush_stream();
-	if (read(sp_fd, &c, 1) != 1)
+	if (serialport_read(&c, 1) != 0)
 		sp_die("readb byteread");
 	msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
 	return c;
@@ -798,7 +790,6 @@
 /* Local version that really does the job, doesn't care of max_read_n. */
 static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
 {
-	int rd_bytes = 0;
 	unsigned char sbuf[6];
 	msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
 	/* Stream the read-n -- as above. */
@@ -812,12 +803,8 @@
 	sbuf[5] = ((len >> 16) & 0xFF);
 	sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
 	sp_flush_stream();
-	do {
-		int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
-		if (r <= 0)
-			sp_die("Error: cannot read read-n data");
-		rd_bytes += r;
-	} while (rd_bytes != len);
+	if (serialport_read(buf, len) != 0)
+		sp_die("Error: cannot read read-n data");
 	return;
 }