Subject: Re: should cngetc block?
To: Kin Cho <kin@neoscale.com>
From: Bill Studenmund <wrstuden@netbsd.org>
List: tech-kern
Date: 02/07/2002 11:34:14
On 7 Feb 2002, Kin Cho wrote:

> Bill Studenmund <wrstuden@netbsd.org> writes:
>
> > I think it would really help us if you explained more of what you are
> > doing. Why do you want to poll for characters? i.e. what else are you
> > doing while you're wanting input? More importantly, are interrupts
> > enabled?
>
> Well, one of the commands of our "debugger/loader" (basically we
> put a command loop where the netbsd "init" would normally be) is
> to test our custom hardware.  So while this command is running,
> we'd like to be able to pause or stop it -- so the need to poll
> for keyboard input.

True, but you don't need to use the console stuff. You can use the tty
system! You don't need to change a thing about the interface, just change
which part of the kernel you talk to.

> We don't disable interrupts.  While the test command is
> running, serial interrupts are processed by comintr as
> usual.  Given the need to pause/stop the test command, I
> need a way to get at the characters just stored in
> com_softc.  Here's what I mean:
>
> 	/* our "init" */
> 	for (;;) {
> 		/* call cngetsn to get a string */
> 		/* parse and execute command_x */
> 	}

How about:

> void command_x(void)
> {
> 	struct com_softc *sc = (struct com_softc *)finddevice("com0");
> 	com_iflush(sc);

	int i;

	cnopen(<console dev for your port>, FREAD | FWRITE | <other opts>
		0, curproc);

	i = FREAD | FWRITE;
	cnioctl(<console dev>, TIOCFLUSH, &i, o, curproc);	/* flush */

> 	for (;;) {
> 		if ((c = com_iavail(sc)) >= 0) {

		if (cnpoll(<dev>, POLLIN | POLLRDNORM, curproc) != 0) {

> 			com_iflush(sc);

flush was shown above

> 			printf("\npress 'y' to stop...");

/*
 * Set up a uio to do a read, then call cnread()
 */

> 			if ((c = cngetc()) == 'y')
> 				break;
> 		}
> 		if (tickle_hardware())
> 			break;
> 	}
> }
>
> Within the existing com driver framework and our limited
> kernel, what would com_iavail() look like?

The abouve should be about it. And you can do that w/o any changes to the
cn interface - you're using the tty interface which can do all of the
things you wanted. :-)

Take care,

Bill