$NetBSD: patch-ae,v 1.2 2003/01/05 03:31:25 dsainty Exp $

Without the first hunk of this patch, ROM transfers tend to not get very far
due to consistently lost characters.

The remainder corrects an assumption in the code that 0 is an invalid file
handle, and registers NetBSD-style tty device names.

--- SrcUnix/EmTransportSerialUnix.cpp.orig	Sat Mar 30 01:11:19 2002
+++ SrcUnix/EmTransportSerialUnix.cpp	Sun Nov 24 05:42:39 2002
@@ -220,7 +220,7 @@
 	// Programming Mini-Howto") says to turn off these for "raw" (as opposed to
 	// "canonical") mode.
 
-	io.c_lflag &= ~(ICANON | ECHO | ISIG);
+	io.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
 
 	// The UNIX Programming FAQ (<www://www.faqs.org/faqs/unix-faq/programmer/faq/>)
 	// recommends just setting all the c_iflags and c_oflags to zero.
@@ -541,6 +541,20 @@
 #ifdef __QNXNTO__
 	results.push_back ("/dev/ser1");
 	results.push_back ("/dev/ser2");
+#elif __FreeBSD__
+	results.push_back ("/dev/pilot");
+	results.push_back ("/dev/cuaa0");
+	results.push_back ("/dev/cuaa1");
+	results.push_back ("/dev/cuaa2");
+	results.push_back ("/dev/cuaa3");
+#elif __NetBSD__
+	results.push_back ("/dev/pilot");
+	results.push_back ("/dev/tty00");
+	results.push_back ("/dev/tty01");
+	results.push_back ("/dev/tty02");
+	results.push_back ("/dev/tty03");
+	results.push_back ("/dev/ttyU0");
+	results.push_back ("/dev/ttyU1");
 #else
 	results.push_back ("/dev/ttyS0");
 	results.push_back ("/dev/ttyS1");
@@ -596,9 +610,9 @@
 EmHostTransportSerial::EmHostTransportSerial (void) :
 	fReadThread (NULL),
 	fWriteThread (NULL),
-	fCommHandle (0),
-	fCommSignalPipeA (0),
-	fCommSignalPipeB (0),
+	fCommHandle (-1),
+	fCommSignalPipeA (-1),
+	fCommSignalPipeB (-1),
 	fTimeToQuit (false),
 	fDataMutex (),
 	fDataCondition (&fDataMutex),
@@ -626,9 +640,9 @@
 {
 	EmAssert (fReadThread == NULL);
 	EmAssert (fWriteThread == NULL);
-	EmAssert (fCommHandle == 0);
-	EmAssert (fCommSignalPipeA == 0);
-	EmAssert (fCommSignalPipeB == 0);
+	EmAssert (fCommHandle == -1);
+	EmAssert (fCommSignalPipeA == -1);
+	EmAssert (fCommSignalPipeB == -1);
 }
 
 
@@ -662,9 +676,9 @@
 
 		fCommHandle = open(portName.c_str (), O_RDWR | O_NOCTTY | O_NDELAY);
 
-		if (fCommHandle <= 0)
+		if (fCommHandle < 0)
 		{
-			fCommHandle = 0;
+			fCommHandle = -1;
 
 			return errno;
 		}
@@ -694,7 +708,7 @@
 
 ErrCode EmHostTransportSerial::CreateCommThreads (const EmTransportSerial::ConfigSerial& /*config*/)
 {
-	if (fCommHandle)
+	if (fCommHandle != -1)
 	{
 		PRINTF ("EmTransportSerial::HostOpen: Creating serial port handler threads...");
 
@@ -734,40 +748,41 @@
 {
 	// If never created, nothing to destroy.
 
-	if (!fCommSignalPipeA)
+	if (fCommSignalPipeA == -1)
 		return errNone;
 
 	// Signal the threads to quit.
 
-	fDataMutex.lock ();
-
-	fTimeToQuit = true;
-
-	int dummy = 0;
-	write (fCommSignalPipeB, &dummy, sizeof (dummy));		// Signals CommRead.
-
-	fDataCondition.broadcast ();	// Signals CommWrite.
-	fDataMutex.unlock ();
+	if((fCommSignalPipeA != -1) && (fCommSignalPipeB != -1))
+	{
+		fDataMutex.lock();
 
-	// Wait for the threads to quit.
+		fTimeToQuit = true;
 
-	if (fReadThread)
-	{
-		fReadThread->join (NULL);
-		fWriteThread->join (NULL);
-	}
+		int dummy = 0;
+		write(fCommSignalPipeB, &dummy, sizeof (dummy)); // Signals CommRead.
 
-	// Thread objects delete themselves, so set our references to NULL.
+		fDataCondition.broadcast();  // Signals CommWrite.
+		fDataMutex.unlock();
 
-	fReadThread = NULL;
-	fWriteThread = NULL;
+		// Wait for the threads to quit.
 
-	// Close the signal pipe.
+		if (fReadThread)
+		{
+			fReadThread->join (NULL);
+			fWriteThread->join (NULL);
+		}
 
-	close (fCommSignalPipeA);
-	close (fCommSignalPipeB);
+		// Thread objects delete themselves, so set our references to NULL.
+		fReadThread = NULL;
+		fWriteThread = NULL;
+
+		// Close the signal pipe.
+		close (fCommSignalPipeA);
+		close (fCommSignalPipeB);
 
-	fCommSignalPipeA = fCommSignalPipeB = 0;
+		fCommSignalPipeA = fCommSignalPipeB = -1;
+	}
 
 	return errNone;
 }
@@ -787,9 +802,11 @@
 
 ErrCode EmHostTransportSerial::CloseCommPort (void)
 {
-	(void) close (fCommHandle);
-
-	fCommHandle = 0;
+	if(fCommHandle != -1)
+	{
+		int result = close(fCommHandle);
+		fCommHandle = -1;
+	}
 
 	return errNone;
 }
