2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Input device TTY line discipline
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2002 Vojtech Pavlik
|
|
|
|
*
|
|
|
|
* This is a module that converts a tty line into a much simpler
|
|
|
|
* 'serial io port' abstraction that the input device drivers use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
* the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/kernel.h>
|
2009-10-07 21:09:06 +08:00
|
|
|
#include <linux/sched.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/serio.h>
|
|
|
|
#include <linux/tty.h>
|
2014-09-10 01:06:51 +08:00
|
|
|
#include <linux/compat.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
|
|
|
|
MODULE_DESCRIPTION("Input device TTY line discipline");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_LDISC(N_MOUSE);
|
|
|
|
|
|
|
|
#define SERPORT_BUSY 1
|
2005-05-17 12:53:07 +08:00
|
|
|
#define SERPORT_ACTIVE 2
|
|
|
|
#define SERPORT_DEAD 3
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct serport {
|
|
|
|
struct tty_struct *tty;
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
struct serio *serio;
|
2005-05-17 12:53:07 +08:00
|
|
|
struct serio_device_id id;
|
|
|
|
spinlock_t lock;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback functions from the serio code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int serport_serio_write(struct serio *serio, unsigned char data)
|
|
|
|
{
|
|
|
|
struct serport *serport = serio->port_data;
|
2008-04-30 15:54:13 +08:00
|
|
|
return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-05-17 12:53:07 +08:00
|
|
|
static int serport_serio_open(struct serio *serio)
|
|
|
|
{
|
|
|
|
struct serport *serport = serio->port_data;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&serport->lock, flags);
|
|
|
|
set_bit(SERPORT_ACTIVE, &serport->flags);
|
|
|
|
spin_unlock_irqrestore(&serport->lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void serport_serio_close(struct serio *serio)
|
|
|
|
{
|
|
|
|
struct serport *serport = serio->port_data;
|
2005-05-17 12:53:07 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&serport->lock, flags);
|
|
|
|
clear_bit(SERPORT_ACTIVE, &serport->flags);
|
|
|
|
spin_unlock_irqrestore(&serport->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serport_ldisc_open() is the routine that is called upon setting our line
|
|
|
|
* discipline on a tty. It prepares the serio struct.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int serport_ldisc_open(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct serport *serport;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2005-09-07 06:18:33 +08:00
|
|
|
serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
|
2005-05-17 12:53:07 +08:00
|
|
|
if (!serport)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
serport->tty = tty;
|
2005-05-17 12:53:07 +08:00
|
|
|
spin_lock_init(&serport->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
init_waitqueue_head(&serport->wait);
|
|
|
|
|
2005-05-17 12:53:07 +08:00
|
|
|
tty->disc_data = serport;
|
[PATCH] TTY layer buffering revamp
The API and code have been through various bits of initial review by
serial driver people but they definitely need to live somewhere for a
while so the unconverted drivers can get knocked into shape, existing
drivers that have been updated can be better tuned and bugs whacked out.
This replaces the tty flip buffers with kmalloc objects in rings. In the
normal situation for an IRQ driven serial port at typical speeds the
behaviour is pretty much the same, two buffers end up allocated and the
kernel cycles between them as before.
When there are delays or at high speed we now behave far better as the
buffer pool can grow a bit rather than lose characters. This also means
that we can operate at higher speeds reliably.
For drivers that receive characters in blocks (DMA based, USB and
especially virtualisation) the layer allows a lot of driver specific
code that works around the tty layer with private secondary queues to be
removed. The IBM folks need this sort of layer, the smart serial port
people do, the virtualisers do (because a virtualised tty typically
operates at infinite speed rather than emulating 9600 baud).
Finally many drivers had invalid and unsafe attempts to avoid buffer
overflows by directly invoking tty methods extracted out of the innards
of work queue structs. These are no longer needed and all go away. That
fixes various random hangs with serial ports on overflow.
The other change in here is to optimise the receive_room path that is
used by some callers. It turns out that only one ldisc uses receive room
except asa constant and it updates it far far less than the value is
read. We thus make it a variable not a function call.
I expect the code to contain bugs due to the size alone but I'll be
watching and squashing them and feeding out new patches as it goes.
Because the buffers now dynamically expand you should only run out of
buffering when the kernel runs out of memory for real. That means a lot of
the horrible hacks high performance drivers used to do just aren't needed any
more.
Description:
tty_insert_flip_char is an old API and continues to work as before, as does
tty_flip_buffer_push() [this is why many drivers dont need modification]. It
does now also return the number of chars inserted
There are also
tty_buffer_request_room(tty, len)
which asks for a buffer block of the length requested and returns the space
found. This improves efficiency with hardware that knows how much to
transfer.
and tty_insert_flip_string_flags(tty, str, flags, len)
to insert a string of characters and flags
For a smart interface the usual code is
len = tty_request_buffer_room(tty, amount_hardware_says);
tty_insert_flip_string(tty, buffer_from_card, len);
More description!
At the moment tty buffers are attached directly to the tty. This is causing a
lot of the problems related to tty layer locking, also problems at high speed
and also with bursty data (such as occurs in virtualised environments)
I'm working on ripping out the flip buffers and replacing them with a pool of
dynamically allocated buffers. This allows both for old style "byte I/O"
devices and also helps virtualisation and smart devices where large blocks of
data suddenely materialise and need storing.
So far so good. Lots of drivers reference tty->flip.*. Several of them also
call directly and unsafely into function pointers it provides. This will all
break. Most drivers can use tty_insert_flip_char which can be kept as an API
but others need more.
At the moment I've added the following interfaces, if people think more will
be needed now is a good time to say
int tty_buffer_request_room(tty, size)
Try and ensure at least size bytes are available, returns actual room (may be
zero). At the moment it just uses the flipbuf space but that will change.
Repeated calls without characters being added are not cumulative. (ie if you
call it with 1, 1, 1, and then 4 you'll have four characters of space. The
other functions will also try and grow buffers in future but this will be a
more efficient way when you know block sizes.
int tty_insert_flip_char(tty, ch, flag)
As before insert a character if there is room. Now returns 1 for success, 0
for failure.
int tty_insert_flip_string(tty, str, len)
Insert a block of non error characters. Returns the number inserted.
int tty_prepare_flip_string(tty, strptr, len)
Adjust the buffer to allow len characters to be added. Returns a buffer
pointer in strptr and the length available. This allows for hardware that
needs to use functions like insl or mencpy_fromio.
Signed-off-by: Alan Cox <alan@redhat.com>
Cc: Paul Fulghum <paulkf@microgate.com>
Signed-off-by: Hirokazu Takata <takata@linux-m32r.org>
Signed-off-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: John Hawkes <hawkes@sgi.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Adrian Bunk <bunk@stusta.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-10 12:54:13 +08:00
|
|
|
tty->receive_room = 256;
|
2005-05-17 12:53:07 +08:00
|
|
|
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serport_ldisc_close() is the opposite of serport_ldisc_open()
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void serport_ldisc_close(struct tty_struct *tty)
|
|
|
|
{
|
2005-05-17 12:53:07 +08:00
|
|
|
struct serport *serport = (struct serport *) tty->disc_data;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(serport);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serport_ldisc_receive() is called by the low level tty driver when characters
|
2011-01-21 15:05:17 +08:00
|
|
|
* are ready for us. We forward the characters and flags, one by one to the
|
|
|
|
* 'interrupt' routine.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2011-06-04 05:33:24 +08:00
|
|
|
static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct serport *serport = (struct serport*) tty->disc_data;
|
2005-05-17 12:53:07 +08:00
|
|
|
unsigned long flags;
|
2013-12-03 02:56:03 +08:00
|
|
|
unsigned int ch_flags = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
2005-05-17 12:53:07 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&serport->lock, flags);
|
|
|
|
|
2011-06-04 05:33:24 +08:00
|
|
|
if (!test_bit(SERPORT_ACTIVE, &serport->flags))
|
2005-05-17 12:53:07 +08:00
|
|
|
goto out;
|
|
|
|
|
2011-01-21 15:05:17 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-12-03 02:56:03 +08:00
|
|
|
if (fp) {
|
|
|
|
switch (fp[i]) {
|
|
|
|
case TTY_FRAME:
|
|
|
|
ch_flags = SERIO_FRAME;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TTY_PARITY:
|
|
|
|
ch_flags = SERIO_PARITY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ch_flags = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-21 15:05:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
serio_interrupt(serport->serio, cp[i], ch_flags);
|
|
|
|
}
|
2005-05-17 12:53:07 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&serport->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serport_ldisc_read() just waits indefinitely if everything goes well.
|
|
|
|
* However, when the serio driver closes the serio port, it finishes,
|
|
|
|
* returning 0 characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
|
|
|
|
{
|
|
|
|
struct serport *serport = (struct serport*) tty->disc_data;
|
2005-05-17 12:53:07 +08:00
|
|
|
struct serio *serio;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2005-09-07 06:18:33 +08:00
|
|
|
serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
|
2005-05-17 12:53:07 +08:00
|
|
|
if (!serio)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
strlcpy(serio->name, "Serial port", sizeof(serio->name));
|
2015-03-31 21:55:59 +08:00
|
|
|
snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
|
2005-05-17 12:53:07 +08:00
|
|
|
serio->id = serport->id;
|
|
|
|
serio->id.type = SERIO_RS232;
|
|
|
|
serio->write = serport_serio_write;
|
|
|
|
serio->open = serport_serio_open;
|
|
|
|
serio->close = serport_serio_close;
|
|
|
|
serio->port_data = serport;
|
2010-08-09 22:22:50 +08:00
|
|
|
serio->dev.parent = tty->dev;
|
2005-05-17 12:53:07 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
serio_register_port(serport->serio);
|
2015-03-31 21:55:59 +08:00
|
|
|
printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
|
2005-05-17 12:53:07 +08:00
|
|
|
|
|
|
|
wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
|
2005-04-17 06:20:36 +08:00
|
|
|
serio_unregister_port(serport->serio);
|
2005-05-17 12:53:07 +08:00
|
|
|
serport->serio = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-17 12:53:07 +08:00
|
|
|
clear_bit(SERPORT_DEAD, &serport->flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
clear_bit(SERPORT_BUSY, &serport->flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-10 01:06:51 +08:00
|
|
|
static void serport_set_type(struct tty_struct *tty, unsigned long type)
|
|
|
|
{
|
|
|
|
struct serport *serport = tty->disc_data;
|
|
|
|
|
|
|
|
serport->id.proto = type & 0x000000ff;
|
|
|
|
serport->id.id = (type & 0x0000ff00) >> 8;
|
|
|
|
serport->id.extra = (type & 0x00ff0000) >> 16;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* serport_ldisc_ioctl() allows to set the port protocol, and device ID
|
|
|
|
*/
|
|
|
|
|
2014-09-10 01:06:51 +08:00
|
|
|
static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (cmd == SPIOCSTYPE) {
|
2014-09-10 01:06:51 +08:00
|
|
|
unsigned long type;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (get_user(type, (unsigned long __user *) arg))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2014-09-10 01:06:51 +08:00
|
|
|
serport_set_type(tty, type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
#define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
|
2018-09-14 10:12:15 +08:00
|
|
|
static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
|
2014-09-10 01:06:51 +08:00
|
|
|
struct file *file,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
if (cmd == COMPAT_SPIOCSTYPE) {
|
|
|
|
void __user *uarg = compat_ptr(arg);
|
|
|
|
compat_ulong_t compat_type;
|
|
|
|
|
|
|
|
if (get_user(compat_type, (compat_ulong_t __user *)uarg))
|
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-09-10 01:06:51 +08:00
|
|
|
serport_set_type(tty, compat_type);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-09-10 01:06:51 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-08-05 02:16:23 +08:00
|
|
|
static int serport_ldisc_hangup(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct serport *serport = (struct serport *) tty->disc_data;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&serport->lock, flags);
|
|
|
|
set_bit(SERPORT_DEAD, &serport->flags);
|
|
|
|
spin_unlock_irqrestore(&serport->lock, flags);
|
|
|
|
|
|
|
|
wake_up_interruptible(&serport->wait);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void serport_ldisc_write_wakeup(struct tty_struct * tty)
|
|
|
|
{
|
2005-05-17 12:53:07 +08:00
|
|
|
struct serport *serport = (struct serport *) tty->disc_data;
|
|
|
|
unsigned long flags;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-17 12:53:07 +08:00
|
|
|
spin_lock_irqsave(&serport->lock, flags);
|
|
|
|
if (test_bit(SERPORT_ACTIVE, &serport->flags))
|
|
|
|
serio_drv_write_wakeup(serport->serio);
|
|
|
|
spin_unlock_irqrestore(&serport->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The line discipline structure.
|
|
|
|
*/
|
|
|
|
|
2008-07-17 04:53:12 +08:00
|
|
|
static struct tty_ldisc_ops serport_ldisc = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "input",
|
|
|
|
.open = serport_ldisc_open,
|
|
|
|
.close = serport_ldisc_close,
|
|
|
|
.read = serport_ldisc_read,
|
|
|
|
.ioctl = serport_ldisc_ioctl,
|
2014-09-10 01:06:51 +08:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = serport_ldisc_compat_ioctl,
|
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
.receive_buf = serport_ldisc_receive,
|
2016-08-05 02:16:23 +08:00
|
|
|
.hangup = serport_ldisc_hangup,
|
2005-04-17 06:20:36 +08:00
|
|
|
.write_wakeup = serport_ldisc_write_wakeup
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The functions for insering/removing us as a module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int __init serport_init(void)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
|
|
|
|
if (retval)
|
|
|
|
printk(KERN_ERR "serport.c: Error registering line discipline.\n");
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit serport_exit(void)
|
|
|
|
{
|
2005-06-23 15:10:33 +08:00
|
|
|
tty_unregister_ldisc(N_MOUSE);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(serport_init);
|
|
|
|
module_exit(serport_exit);
|