All of lore.kernel.org
 help / color / mirror / Atom feed
* Writing a console/tty driver -- how to use tty_port?
@ 2010-10-27 21:04 Timur Tabi
  2010-10-27 22:19 ` Alan Cox
  2010-10-28 20:57 ` Arnd Bergmann
  0 siblings, 2 replies; 12+ messages in thread
From: Timur Tabi @ 2010-10-27 21:04 UTC (permalink / raw)
  To: gregkh, lkml

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.

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-27 21:04 Writing a console/tty driver -- how to use tty_port? Timur Tabi
@ 2010-10-27 22:19 ` Alan Cox
  2010-10-28 19:35   ` Timur Tabi
  2010-10-28 20:34   ` Timur Tabi
  2010-10-28 20:57 ` Arnd Bergmann
  1 sibling, 2 replies; 12+ messages in thread
From: Alan Cox @ 2010-10-27 22:19 UTC (permalink / raw)
  To: Timur Tabi; +Cc: gregkh, lkml

On Wed, 27 Oct 2010 16:04:13 -0500
Timur Tabi <timur@freescale.com> 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.
> 
> 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-27 22:19 ` Alan Cox
@ 2010-10-28 19:35   ` Timur Tabi
  2010-10-29 13:55     ` Timur Tabi
  2010-10-28 20:34   ` Timur Tabi
  1 sibling, 1 reply; 12+ messages in thread
From: Timur Tabi @ 2010-10-28 19:35 UTC (permalink / raw)
  To: Alan Cox; +Cc: gregkh, lkml

Alan Cox wrote:
> 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.

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-27 22:19 ` Alan Cox
  2010-10-28 19:35   ` Timur Tabi
@ 2010-10-28 20:34   ` Timur Tabi
  2010-10-28 20:47     ` Timur Tabi
  1 sibling, 1 reply; 12+ messages in thread
From: Timur Tabi @ 2010-10-28 20:34 UTC (permalink / raw)
  To: Alan Cox; +Cc: gregkh, lkml

Alan Cox wrote:
> 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?

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 20:34   ` Timur Tabi
@ 2010-10-28 20:47     ` Timur Tabi
  0 siblings, 0 replies; 12+ messages in thread
From: Timur Tabi @ 2010-10-28 20:47 UTC (permalink / raw)
  To: Alan Cox; +Cc: gregkh, lkml

Timur Tabi wrote:
> 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.

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-27 21:04 Writing a console/tty driver -- how to use tty_port? Timur Tabi
  2010-10-27 22:19 ` Alan Cox
@ 2010-10-28 20:57 ` Arnd Bergmann
  2010-10-28 21:08   ` Timur Tabi
  2010-10-28 22:11   ` Alan Cox
  1 sibling, 2 replies; 12+ messages in thread
From: Arnd Bergmann @ 2010-10-28 20:57 UTC (permalink / raw)
  To: Timur Tabi; +Cc: gregkh, lkml

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 20:57 ` Arnd Bergmann
@ 2010-10-28 21:08   ` Timur Tabi
  2010-10-29 14:21     ` Arnd Bergmann
  2010-10-28 22:11   ` Alan Cox
  1 sibling, 1 reply; 12+ messages in thread
From: Timur Tabi @ 2010-10-28 21:08 UTC (permalink / raw)
  To: Arnd Bergmann; +Cc: gregkh, lkml

Arnd Bergmann wrote:
> 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.

> 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.

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 20:57 ` Arnd Bergmann
  2010-10-28 21:08   ` Timur Tabi
@ 2010-10-28 22:11   ` Alan Cox
  2010-10-29 23:55     ` Arnd Bergmann
  1 sibling, 1 reply; 12+ messages in thread
From: Alan Cox @ 2010-10-28 22:11 UTC (permalink / raw)
  To: Arnd Bergmann; +Cc: Timur Tabi, gregkh, lkml

On Thu, 28 Oct 2010 22:57:33 +0200
Arnd Bergmann <arnd@arndb.de> wrote:

> 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.

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.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 19:35   ` Timur Tabi
@ 2010-10-29 13:55     ` Timur Tabi
  2010-10-29 14:14       ` Timur Tabi
  0 siblings, 1 reply; 12+ messages in thread
From: Timur Tabi @ 2010-10-29 13:55 UTC (permalink / raw)
  To: Alan Cox; +Cc: gregkh, lkml

Timur Tabi wrote:
> 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 /]#

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-29 13:55     ` Timur Tabi
@ 2010-10-29 14:14       ` Timur Tabi
  0 siblings, 0 replies; 12+ messages in thread
From: Timur Tabi @ 2010-10-29 14:14 UTC (permalink / raw)
  To: Alan Cox; +Cc: gregkh, lkml

Timur Tabi wrote:
> 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.

-- 
Timur Tabi
Linux kernel developer at Freescale


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 21:08   ` Timur Tabi
@ 2010-10-29 14:21     ` Arnd Bergmann
  0 siblings, 0 replies; 12+ messages in thread
From: Arnd Bergmann @ 2010-10-29 14:21 UTC (permalink / raw)
  To: Timur Tabi; +Cc: gregkh, lkml

On Thursday 28 October 2010, Timur Tabi wrote:
> Arnd Bergmann wrote:
> > 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.

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.

> > 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Writing a console/tty driver -- how to use tty_port?
  2010-10-28 22:11   ` Alan Cox
@ 2010-10-29 23:55     ` Arnd Bergmann
  0 siblings, 0 replies; 12+ messages in thread
From: Arnd Bergmann @ 2010-10-29 23:55 UTC (permalink / raw)
  To: Alan Cox; +Cc: Timur Tabi, gregkh, lkml

On Friday 29 October 2010, Alan Cox wrote:
> On Thu, 28 Oct 2010 22:57:33 +0200
> Arnd Bergmann <arnd@arndb.de> wrote:
> 
> > 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.
> 
> 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-10-29 23:54 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-27 21:04 Writing a console/tty driver -- how to use tty_port? Timur Tabi
2010-10-27 22:19 ` Alan Cox
2010-10-28 19:35   ` Timur Tabi
2010-10-29 13:55     ` Timur Tabi
2010-10-29 14:14       ` Timur Tabi
2010-10-28 20:34   ` Timur Tabi
2010-10-28 20:47     ` Timur Tabi
2010-10-28 20:57 ` Arnd Bergmann
2010-10-28 21:08   ` Timur Tabi
2010-10-29 14:21     ` Arnd Bergmann
2010-10-28 22:11   ` Alan Cox
2010-10-29 23:55     ` Arnd Bergmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.