Skip to contentSkip to conversation

Writing a console/tty driver -- how to use tty_port?

DISCUSSION Timur Tabi 3 participants 12 messages Oct 27, 2010 – Oct 29, 2010

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

Greg,

Do you have an updated version of Tiny TTY that uses tty_port structures? I'm trying to write a new TTY driver for a device that is not a UART, and I'm having a hard time finding a good example. I suspect I need to understand the tty_port structure, but I can't find any documentation for it.

One specific problem I'm having is that I can't figure out how to get a tty_struct pointer in my interrupt handler. The ISR gets called whenever there is new input, and it needs to call tty_insert_flip_string(). But it appears that it's possible to have my driver opened multiple times, each time creating a new TTY. But since I can only have one interrupt handler, I don't know how to determine which of the tty_structs to use. I figure tty_port is a way to resolve this problem, but I don't know how to use it exactly.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#1 lore raw
Quoted 7 lines · On Wed, 27 Oct 2010 16:04:13 -0500 Timur Tabi <[email protected]> wrote:

Greg,

Do you have an updated version of Tiny TTY that uses tty_port structures? I'm trying to write a new TTY driver for a device that is not a UART, and I'm having a hard time finding a good example. I suspect I need to understand the tty_port structure, but I can't find any documentation for it.

Quoted 7 lines

One specific problem I'm having is that I can't figure out how to get a tty_struct pointer in my interrupt handler. The ISR gets called whenever there is new input, and it needs to call tty_insert_flip_string(). But it appears that it's possible to have my driver opened multiple times, each time creating a new TTY. But since I can only have one interrupt handler, I don't know how to determine which of the tty_structs to use. I figure tty_port is a way to resolve this problem, but I don't know how to use it exactly.

You only have one tty struct for the multiple opens.

Basically

tty_port - lifetime of hardware being loaded
tty_struct - lifetime open/close (and a bit
beyond for refs)

So you can use tty_port_tty_get() which also knows about all the ghastly internal locking rules.

Eventually the rx buffer should be moved to the tty_port which will make the world vastly saner.

drivers/mmc/card/sdio_uart.c

is in some ways the best example, it handles all the horrible cases including unloading of hardware v open races, its way more than you'd need but does illustrate it all.

I've also got a draft tty_kfifo layer which is designed to be tty_port with kfifo queues as a helper. Remind me next week if it would be useful and I'll dig it back out.

Alan

author
#2 lore raw
Quoted 5 lines · Alan Cox

drivers/mmc/card/sdio_uart.c

is in some ways the best example, it handles all the horrible cases including unloading of hardware v open races, its way more than you'd need but does illustrate it all.

Thanks Alan. As always, you're a great help.

I've made some progress adding support for tty_port. I'm registering the ISR in the tty_port .activate function.

I see something weird, though. I lose the console after I log in:

p4080 login: root
Password:
ehv_bc_tty_open:442 ttys=dce50000 stdout_irq=68
ehv_bc_tty_close:450 tty=dce50000
ls

logout
ehv_bc_tty_close:450 tty=dce50000
ehv_bc_console_device:390
ehv_bc_tty_open:442 ttys=dce50000 stdout_irq=68

p4080 login:

The "ehv_bc_..." lines are my debug printks.

When I type "ls", I don't get a listing. The "logout" occurs after I press ^D.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#5 lore raw
In reply to Alan Cox
Quoted 5 lines · Alan Cox

drivers/mmc/card/sdio_uart.c

is in some ways the best example, it handles all the horrible cases including unloading of hardware v open races, its way more than you'd need but does illustrate it all.

I see something weird in sdio_uart_install():

	int ret = tty_init_termios(tty);

	if (ret == 0) {
		tty_driver_kref_get(driver);
		tty->count++;
		/* This is the ref sdio_uart_port get provided */
		tty->driver_data = port;
		driver->ttys[idx] = tty;

This function is called by tty_driver_install_tty(), which also does this:

	if (tty_init_termios(tty) == 0) {
		lock_kernel();
		tty_driver_kref_get(driver);
		tty->count++;
		driver->ttys[idx] = tty;
		unlock_kernel();
		return 0;
	}

Assuming that both calls to tty_init_termios(tty) return 0, these three lines will be executed twice:

		tty_driver_kref_get(driver);
		tty->count++;
		driver->ttys[idx] = tty;

Is that right?

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#6 lore raw
Quoted 2 lines · Timur Tabi

Assuming that both calls to tty_init_termios(tty) return 0, these three lines will be executed twice:

Ugh, never mind. I didn't notice the "return ret;" after it unlocks the kernel.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#7 lore raw
In reply to Timur Tabi
Quoted 4 lines · On Wednesday 27 October 2010 23:04:13 Timur Tabi wrote:

Do you have an updated version of Tiny TTY that uses tty_port structures? I'm trying to write a new TTY driver for a device that is not a UART, and I'm having a hard time finding a good example. I suspect I need to understand the tty_port structure, but I can't find any documentation for it.

If the device is not a UART, the best option may be to make the driver a backend to the hvc driver, like e.g. drivers/char/hvc_tile.c.

This works for all devices with or without interrupts that don't need to set up the communication parameters but simply provide a read/write character interface.

Arnd

author
#8 lore raw
Quoted 2 lines · Arnd Bergmann

If the device is not a UART, the best option may be to make the driver a backend to the hvc driver, like e.g. drivers/char/hvc_tile.c.

The current version *is* a backend to hvc, but I have to abandon that because of this thread:

http://lists.ozlabs.org/pipermail/linuxppc-dev/2010-September/085664.html

I posted a patch that causes hvc to spin if the backend driver returns -EAGAIN, which is my driver does when the output buffer is full.

In other words, hvc does not support backend drivers that can detect full output buffers.

Quoted 3 lines

This works for all devices with or without interrupts that don't need to set up the communication parameters but simply provide a read/write character interface.

If my patch were accepted, I wouldn't need to do the rewrite. Dropping characters during a printk() is not acceptable for us.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#3 lore raw
In reply to Timur Tabi
Quoted 7 lines · Timur Tabi

I see something weird, though. I lose the console after I log in:

p4080 login: root
Password:
ehv_bc_tty_open:442 ttys=dce50000 stdout_irq=68
ehv_bc_tty_close:450 tty=dce50000
ls

I've done a little more debugging, and it's even stranger than I thought. The console is only half missing.

What's missing is my shell prompt, and a few other things. I can type in commands, though. The prompt returns, however, if I 'cd' to the root direcetory.

In addition, the 'set' command only displays some of the environment variables.
The others produce blank lines. Here's a sample output.

Do you have any idea what's going on? This is just so bizarre.

p4080 login: root
Password:
ehv_bc_tty_open:446 ttys=dcf0e400 stdout_irq=66 ehv_bc_tty_open:449 ttys->driver_data=(null) &ttyport=c0558f34 ehv_bc_tty_close:455 tty=dcf0e400
ls
test_setkey

echo $PATH

echo $PS1
[\u@\h \W]\$
set
BASH=/bin/sh

COLUMNS=80
DIRSTACK=()
EUID=0
GROUPS=()

HISTFILESIZE=500
HISTSIZE=500
HOME=/root
HOSTNAME=p4080
HOSTTYPE=powerpc
IFS='
'
LINES=24
LOGNAME=root

MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu

PPID=1

PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/sh

SHLVL=1
TERM=screen
UID=0
USER=root
_='\W]\$'

cd /
[root@p4080 /]# pwd
/
[root@p4080 /]# set
BASH=/bin/sh

COLUMNS=80
DIRSTACK=()
EUID=0
GROUPS=()

HISTFILESIZE=500
HISTSIZE=500
HOME=/root
HOSTNAME=p4080
HOSTTYPE=powerpc
IFS='
'
LINES=24
LOGNAME=root

MAILCHECK=60
OLDPWD=/root
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu

PPID=1

PS2='> '
PS4='+ '
PWD=/
SHELL=/bin/sh

SHLVL=1
TERM=screen
UID=0
USER=root
_=pwd
[root@p4080 /]#
[root@p4080 /]# echo $HOME
/root
[root@p4080 /]# ls -l /root

[root@p4080 /]# cd /root
ls -l

cd /
[root@p4080 /]# ls -l

[root@p4080 /]#

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#4 lore raw
Quoted 5 lines · Timur Tabi

I've done a little more debugging, and it's even stranger than I thought. The console is only half missing.

What's missing is my shell prompt, and a few other things. I can type in commands, though. The prompt returns, however, if I 'cd' to the root direcetory.

Never mind, I figure it out. My console device was silently rejecting any writes larger than 16 bytes.

Sorry for the noise. I know your inboxes must be always full.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#10 lore raw
In reply to Arnd Bergmann
Quoted 6 lines · On Thu, 28 Oct 2010 22:57:33 +0200 Arnd Bergmann <[email protected]> wrote:
Quoted 4 lines · On Wednesday 27 October 2010 23:04:13 Timur Tabi wrote:

Do you have an updated version of Tiny TTY that uses tty_port structures? I'm trying to write a new TTY driver for a device that is not a UART, and I'm having a hard time finding a good example. I suspect I need to understand the tty_port structure, but I can't find any documentation for it.

Quoted 6 lines

If the device is not a UART, the best option may be to make the driver a backend to the hvc driver, like e.g. drivers/char/hvc_tile.c.

This works for all devices with or without interrupts that don't need to set up the communication parameters but simply provide a read/write character interface.

I really don't understand the love of hvc when the hvc drivers seem to be bigger than native tty_port code and haul a whole blob of extra midlayer glue into the system.

#9 lore raw
In reply to Timur Tabi
Quoted 7 lines · Timur Tabi
Quoted 2 lines · Arnd Bergmann

If the device is not a UART, the best option may be to make the driver a backend to the hvc driver, like e.g. drivers/char/hvc_tile.c.

The current version *is* a backend to hvc, but I have to abandon that because of this thread:

Quoted 7 lines

http://lists.ozlabs.org/pipermail/linuxppc-dev/2010-September/085664.html

I posted a patch that causes hvc to spin if the backend driver returns -EAGAIN, which is my driver does when the output buffer is full.

In other words, hvc does not support backend drivers that can detect full output buffers.

To be more specific, hvc does support retransmitting characters on the tty but not on the console!

The backend drivers return 0 to notify the tty driver that output is busy and should be retried, which will set the timer.

Quoted 6 lines
Quoted 3 lines

This works for all devices with or without interrupts that don't need to set up the communication parameters but simply provide a read/write character interface.

If my patch were accepted, I wouldn't need to do the rewrite. Dropping characters during a printk() is not acceptable for us.

Why do you care about printk output overflowing more than others do? Most of the hvc drivers block internally and never return 0, but those that do probably all need the same solution.

Your original one-line patch from last year didn't seem too wrong, though you might want to check with the iseries people, since that driver might return zero in error cases.

The idea of adding a special return code from ->put_chars to distinguish buffer full from other errors also seemed reasonable, as did the approach to flag the hv_ops to tell the base layer wether you want retransmits or drops.

Arnd

#11 lore raw
In reply to Alan Cox
Quoted 12 lines · Alan Cox
Quoted 9 lines · On Thu, 28 Oct 2010 22:57:33 +0200 Arnd Bergmann <[email protected]> wrote:
Quoted 4 lines · On Wednesday 27 October 2010 23:04:13 Timur Tabi wrote:

Do you have an updated version of Tiny TTY that uses tty_port structures? I'm trying to write a new TTY driver for a device that is not a UART, and I'm having a hard time finding a good example. I suspect I need to understand the tty_port structure, but I can't find any documentation for it.

If the device is not a UART, the best option may be to make the driver a backend to the hvc driver, like e.g. drivers/char/hvc_tile.c.

Quoted 8 lines
Quoted 3 lines

This works for all devices with or without interrupts that don't need to set up the communication parameters but simply provide a read/write character interface.

I really don't understand the love of hvc when the hvc drivers seem to be bigger than native tty_port code and haul a whole blob of extra midlayer glue into the system.

Two reasons for me:

* It does all the timers for the tty device to poll reads and retry writes. If someone writes similar code from scratch, they most likely get it wrong.

* It's about the simplest interface you can imagine for a new backend driver, only register a data structure with two function pointers!

Arnd