/* Made by Kjetil S. Matheussen 17.12.99.

   Updated: 12.4.2k. Changes: Used as a part of the CAMD
   TPP/QP driver.

Copyright 1999-2000 Kjetil S. Matheussen

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <dos/dos.h>
#include <stdio.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <devices/timer.h>
#include <exec/types.h>
#include <hardware/custom.h>
#include <hardware/intbits.h>

__far extern struct Custom custom;

#define causeTBE()  if(custom.serdatr&1L<<13) custom.intreq=0x8001;
#define checkTSRE() (custom.serdatr & (1<<12))

struct timerequest *TimerIO;
struct MsgPort  *TimerMP;         /* Message port pointer */
struct Message *TimerMSG;

LONG portchangewaitvalue;
BOOL doio_questionmark;
extern BOOL waitsending;

struct Task *waitforporttask;

ULONG waitforportsig=31,mysignal_2,usersig_2;

__saveds void portchangewaithandle(void)
{
	LONG error;

	waitforportsig=AllocSignal(waitforportsig);

	usersig_2=SIGBREAKF_CTRL_C;

	if(TimerMP=CreatePort(0,0)){
		if(TimerIO=(struct timerequest *)CreateExtIO(TimerMP,sizeof(struct timerequest))){
			if(!(error=OpenDevice(TIMERNAME,UNIT_ECLOCK,(struct IORequest *)TimerIO,0L))){
			}else{
/*				printf("Error : Could not open timer device, error # %d\n",error);
				printf("Eh, this shouldn't have happened, this is not good...\n");
*/				goto exit;
			}
		}else{
/*			printf("Error: Could not create I/O structure for the timing device\n");
			printf("Eh, this shouldn't have happened, this is not good...\n");
*/			goto exit;
		}
	}else{
/*		printf("Error: Could not create port\n");
		printf("Eh, this shouldn't have happened, this is not good...\n");
		printf("Perhaps you are low on memory?\n");
*/		goto exit;
	}

	for(;;){
		mysignal_2=Wait(1L<<waitforportsig | usersig_2);

		if(mysignal_2 & 1L<<waitforportsig){

			while(!(checkTSRE())){
				TimerIO->tr_node.io_Command=TR_ADDREQUEST;
				TimerIO->tr_time.tv_secs=0;
				TimerIO->tr_time.tv_micro=1;
				DoIO((struct IORequest *)TimerIO);			/*Midibyte is not finished sent yet.
																		  Has to wait..*/
			}
			waitsending=FALSE;
			causeTBE();
		}

		if(mysignal_2 & usersig_2)
			break;
	}

exit:
	FreeSignal(1L<<waitforportsig);
	CloseDevice((struct IORequest *)TimerIO);
	DeleteExtIO((struct IORequest *)TimerIO);
	DeletePort(TimerMP);
}


__saveds void startportwaithandletask(void)
{
	waitforporttask = CreateTask("portchangewaittask",50L,portchangewaithandle,6000L);
}

__saveds void removeportwaithandletask(void)
{
	struct Task *waittask=(struct Task *)50L;

	Signal(waitforporttask,SIGBREAKF_CTRL_C);
/*
	while(waittask!=0){
		Delay(10);
		Forbid();
			waittask=FindTask("portchangewaittask");
		Permit();
	}
*/
}



