Index: flash.h
===================================================================
--- flash.h	(Revision 830)
+++ flash.h	(Arbeitskopie)
@@ -598,7 +598,6 @@
 int serialport_shutdown(void);
 int serialport_write(unsigned char *buf, unsigned int writecnt);
 int serialport_read(unsigned char *buf, unsigned int readcnt);
-int serialport_discard_read(void);
 
 #include "chipdrivers.h"
 
Index: buspirate_spi.c
===================================================================
--- buspirate_spi.c	(Revision 830)
+++ buspirate_spi.c	(Arbeitskopie)
@@ -40,7 +40,6 @@
 #define serialport_shutdown(...) 0
 #define serialport_write(...) 0
 #define serialport_read(...) 0
-#define serialport_discard_read(...) 0
 #endif
 
 int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
@@ -144,9 +143,7 @@
 		if (ret)
 			return ret;
 		/* Read any response and discard it. */
-		ret = serialport_discard_read();
-		if (ret)
-			return ret;
+		sp_flush_incoming();
 	}
 	/* Enter raw bitbang mode */
 	buf[0] = 0x00;
Index: serial.c
===================================================================
--- serial.c	(Revision 830)
+++ serial.c	(Arbeitskopie)
@@ -129,16 +129,7 @@
 
 void sp_flush_incoming(void)
 {
-	int i;
-	for (i=0;i<100;i++) { /* In case the device doesnt do EAGAIN, just read 0 */
-		unsigned char flush[16];
-		ssize_t rv;
-		rv = read(sp_fd, flush, sizeof(flush));
-		if ((rv == -1) && (errno == EAGAIN))
-			break;
-		if (rv == -1)
-			sp_die("flush read");
-	}
+	tcflush(sp_fd, TCIFLUSH);
 	return;
 }
 
@@ -158,6 +149,7 @@
 			return 1;
 		if (!tmp)
 			printf_debug("Empty write\n");
+		writecnt -= tmp;
 	}
 
 	return 0;
@@ -173,22 +165,8 @@
 			return 1;
 		if (!tmp)
 			printf_debug("Empty read\n");
+		readcnt -= tmp;
 	}
 
 	return 0;
 }
-
-int serialport_discard_read(void)
-{
-	int flags;
-
-	printf_debug("%s\n", __func__);
-	flags = fcntl(sp_fd, F_GETFL);
-	flags |= O_NONBLOCK;
-	fcntl(sp_fd, F_SETFL, flags);
-	sp_flush_incoming();
-	flags &= ~O_NONBLOCK;
-	fcntl(sp_fd, F_SETFL, flags);
-
-	return 0;
-}
