1 // SPDX-License-Identifier: GPL-2.0-only
3 * Input device TTY line discipline
5 * Copyright (c) 1999-2002 Vojtech Pavlik
7 * This is a module that converts a tty line into a much simpler
8 * 'serial io port' abstraction that the input device drivers use.
12 #include <linux/uaccess.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/serio.h>
19 #include <linux/tty.h>
20 #include <linux/compat.h>
23 MODULE_DESCRIPTION("Input device TTY line discipline");
24 MODULE_LICENSE("GPL");
25 MODULE_ALIAS_LDISC(N_MOUSE);
27 #define SERPORT_BUSY 1
28 #define SERPORT_ACTIVE 2
29 #define SERPORT_DEAD 3
32 struct tty_struct *tty;
33 wait_queue_head_t wait;
35 struct serio_device_id id;
41 * Callback functions from the serio code.
44 static int serport_serio_write(struct serio *serio, unsigned char data)
46 struct serport *serport = serio->port_data;
47 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
50 static int serport_serio_open(struct serio *serio)
52 struct serport *serport = serio->port_data;
54 guard(spinlock_irqsave)(&serport->lock);
55 set_bit(SERPORT_ACTIVE, &serport->flags);
61 static void serport_serio_close(struct serio *serio)
63 struct serport *serport = serio->port_data;
65 guard(spinlock_irqsave)(&serport->lock);
66 clear_bit(SERPORT_ACTIVE, &serport->flags);
70 * serport_ldisc_open() is the routine that is called upon setting our line
71 * discipline on a tty. It prepares the serio struct.
74 static int serport_ldisc_open(struct tty_struct *tty)
76 struct serport *serport;
78 if (!capable(CAP_SYS_ADMIN))
81 serport = kzalloc(sizeof(*serport), GFP_KERNEL);
86 spin_lock_init(&serport->lock);
87 init_waitqueue_head(&serport->wait);
89 tty->disc_data = serport;
90 tty->receive_room = 256;
91 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
97 * serport_ldisc_close() is the opposite of serport_ldisc_open()
100 static void serport_ldisc_close(struct tty_struct *tty)
102 struct serport *serport = tty->disc_data;
108 * serport_ldisc_receive() is called by the low level tty driver when characters
109 * are ready for us. We forward the characters and flags, one by one to the
110 * 'interrupt' routine.
113 static void serport_ldisc_receive(struct tty_struct *tty, const u8 *cp,
114 const u8 *fp, size_t count)
116 struct serport *serport = tty->disc_data;
117 unsigned int ch_flags = 0;
120 guard(spinlock_irqsave)(&serport->lock);
122 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
125 for (i = 0; i < count; i++) {
129 ch_flags = SERIO_FRAME;
133 ch_flags = SERIO_PARITY;
142 serio_interrupt(serport->serio, cp[i], ch_flags);
147 * serport_ldisc_read() just waits indefinitely if everything goes well.
148 * However, when the serio driver closes the serio port, it finishes,
149 * returning 0 characters.
152 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file,
153 u8 *kbuf, size_t nr, void **cookie,
154 unsigned long offset)
156 struct serport *serport = tty->disc_data;
159 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
162 serport->serio = serio = kzalloc(sizeof(*serio), GFP_KERNEL);
166 strscpy(serio->name, "Serial port", sizeof(serio->name));
167 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
168 serio->id = serport->id;
169 serio->id.type = SERIO_RS232;
170 serio->write = serport_serio_write;
171 serio->open = serport_serio_open;
172 serio->close = serport_serio_close;
173 serio->port_data = serport;
174 serio->dev.parent = tty->dev;
176 serio_register_port(serport->serio);
177 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
179 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
180 serio_unregister_port(serport->serio);
181 serport->serio = NULL;
183 clear_bit(SERPORT_DEAD, &serport->flags);
184 clear_bit(SERPORT_BUSY, &serport->flags);
189 static void serport_set_type(struct tty_struct *tty, unsigned long type)
191 struct serport *serport = tty->disc_data;
193 serport->id.proto = type & 0x000000ff;
194 serport->id.id = (type & 0x0000ff00) >> 8;
195 serport->id.extra = (type & 0x00ff0000) >> 16;
199 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
202 static int serport_ldisc_ioctl(struct tty_struct *tty, unsigned int cmd,
205 if (cmd == SPIOCSTYPE) {
208 if (get_user(type, (unsigned long __user *) arg))
211 serport_set_type(tty, type);
219 #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
220 static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
221 unsigned int cmd, unsigned long arg)
223 if (cmd == COMPAT_SPIOCSTYPE) {
224 void __user *uarg = compat_ptr(arg);
225 compat_ulong_t compat_type;
227 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
230 serport_set_type(tty, compat_type);
238 static void serport_ldisc_hangup(struct tty_struct *tty)
240 struct serport *serport = tty->disc_data;
242 scoped_guard(spinlock_irqsave, &serport->lock)
243 set_bit(SERPORT_DEAD, &serport->flags);
245 wake_up_interruptible(&serport->wait);
248 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
250 struct serport *serport = tty->disc_data;
252 guard(spinlock_irqsave)(&serport->lock);
254 if (test_bit(SERPORT_ACTIVE, &serport->flags))
255 serio_drv_write_wakeup(serport->serio);
259 * The line discipline structure.
262 static struct tty_ldisc_ops serport_ldisc = {
263 .owner = THIS_MODULE,
266 .open = serport_ldisc_open,
267 .close = serport_ldisc_close,
268 .read = serport_ldisc_read,
269 .ioctl = serport_ldisc_ioctl,
271 .compat_ioctl = serport_ldisc_compat_ioctl,
273 .receive_buf = serport_ldisc_receive,
274 .hangup = serport_ldisc_hangup,
275 .write_wakeup = serport_ldisc_write_wakeup
279 * The functions for insering/removing us as a module.
282 static int __init serport_init(void)
285 retval = tty_register_ldisc(&serport_ldisc);
287 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
292 static void __exit serport_exit(void)
294 tty_unregister_ldisc(&serport_ldisc);
297 module_init(serport_init);
298 module_exit(serport_exit);