blob: 8ac970a423de6e6b0434ef6c645ee88e24eff4f2 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Input device TTY line discipline
4 *
5 * Copyright (c) 1999-2002 Vojtech Pavlik
6 *
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.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/serio.h>
19#include <linux/tty.h>
John Sunga80d8b02014-09-09 10:06:51 -070020#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23MODULE_DESCRIPTION("Input device TTY line discipline");
24MODULE_LICENSE("GPL");
25MODULE_ALIAS_LDISC(N_MOUSE);
26
27#define SERPORT_BUSY 1
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070028#define SERPORT_ACTIVE 2
29#define SERPORT_DEAD 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct serport {
32 struct tty_struct *tty;
33 wait_queue_head_t wait;
34 struct serio *serio;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070035 struct serio_device_id id;
36 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned long flags;
38};
39
40/*
41 * Callback functions from the serio code.
42 */
43
44static int serport_serio_write(struct serio *serio, unsigned char data)
45{
46 struct serport *serport = serio->port_data;
Alan Coxf34d7a52008-04-30 00:54:13 -070047 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070050static int serport_serio_open(struct serio *serio)
51{
52 struct serport *serport = serio->port_data;
53 unsigned long flags;
54
55 spin_lock_irqsave(&serport->lock, flags);
56 set_bit(SERPORT_ACTIVE, &serport->flags);
57 spin_unlock_irqrestore(&serport->lock, flags);
58
59 return 0;
60}
61
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void serport_serio_close(struct serio *serio)
64{
65 struct serport *serport = serio->port_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070066 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070068 spin_lock_irqsave(&serport->lock, flags);
69 clear_bit(SERPORT_ACTIVE, &serport->flags);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070070 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/*
74 * serport_ldisc_open() is the routine that is called upon setting our line
75 * discipline on a tty. It prepares the serio struct.
76 */
77
78static int serport_ldisc_open(struct tty_struct *tty)
79{
80 struct serport *serport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (!capable(CAP_SYS_ADMIN))
83 return -EPERM;
84
Pekka Enberga97e1482005-09-06 15:18:33 -070085 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070086 if (!serport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 serport->tty = tty;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070090 spin_lock_init(&serport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 init_waitqueue_head(&serport->wait);
92
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070093 tty->disc_data = serport;
Alan Cox33f0f882006-01-09 20:54:13 -080094 tty->receive_room = 256;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070095 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98}
99
100/*
101 * serport_ldisc_close() is the opposite of serport_ldisc_open()
102 */
103
104static void serport_ldisc_close(struct tty_struct *tty)
105{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700106 struct serport *serport = (struct serport *) tty->disc_data;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kfree(serport);
109}
110
111/*
112 * serport_ldisc_receive() is called by the low level tty driver when characters
David Engraf48c27012011-01-20 23:05:17 -0800113 * are ready for us. We forward the characters and flags, one by one to the
114 * 'interrupt' routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
Linus Torvalds55db4c62011-06-04 06:33:24 +0900117static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700120 unsigned long flags;
Peter Hurley82f91fe2013-12-02 13:56:03 -0500121 unsigned int ch_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int i;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700123
124 spin_lock_irqsave(&serport->lock, flags);
125
Linus Torvalds55db4c62011-06-04 06:33:24 +0900126 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700127 goto out;
128
David Engraf48c27012011-01-20 23:05:17 -0800129 for (i = 0; i < count; i++) {
Peter Hurley82f91fe2013-12-02 13:56:03 -0500130 if (fp) {
131 switch (fp[i]) {
132 case TTY_FRAME:
133 ch_flags = SERIO_FRAME;
134 break;
David Engraf48c27012011-01-20 23:05:17 -0800135
Peter Hurley82f91fe2013-12-02 13:56:03 -0500136 case TTY_PARITY:
137 ch_flags = SERIO_PARITY;
138 break;
David Engraf48c27012011-01-20 23:05:17 -0800139
Peter Hurley82f91fe2013-12-02 13:56:03 -0500140 default:
141 ch_flags = 0;
142 break;
143 }
David Engraf48c27012011-01-20 23:05:17 -0800144 }
145
146 serio_interrupt(serport->serio, cp[i], ch_flags);
147 }
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700148
149out:
150 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * serport_ldisc_read() just waits indefinitely if everything goes well.
155 * However, when the serio driver closes the serio port, it finishes,
156 * returning 0 characters.
157 */
158
159static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
160{
161 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700162 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
165 return -EBUSY;
166
Pekka Enberga97e1482005-09-06 15:18:33 -0700167 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700168 if (!serio)
169 return -ENOMEM;
170
171 strlcpy(serio->name, "Serial port", sizeof(serio->name));
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200172 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700173 serio->id = serport->id;
174 serio->id.type = SERIO_RS232;
175 serio->write = serport_serio_write;
176 serio->open = serport_serio_open;
177 serio->close = serport_serio_close;
178 serio->port_data = serport;
Dmitry Eremin-Solenikovde838a92010-08-09 18:22:50 +0400179 serio->dev.parent = tty->dev;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 serio_register_port(serport->serio);
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200182 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700184 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
185 serio_unregister_port(serport->serio);
186 serport->serio = NULL;
187
188 clear_bit(SERPORT_DEAD, &serport->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 clear_bit(SERPORT_BUSY, &serport->flags);
190
191 return 0;
192}
193
John Sunga80d8b02014-09-09 10:06:51 -0700194static void serport_set_type(struct tty_struct *tty, unsigned long type)
195{
196 struct serport *serport = tty->disc_data;
197
198 serport->id.proto = type & 0x000000ff;
199 serport->id.id = (type & 0x0000ff00) >> 8;
200 serport->id.extra = (type & 0x00ff0000) >> 16;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
204 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
205 */
206
John Sunga80d8b02014-09-09 10:06:51 -0700207static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
208 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (cmd == SPIOCSTYPE) {
John Sunga80d8b02014-09-09 10:06:51 -0700211 unsigned long type;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (get_user(type, (unsigned long __user *) arg))
214 return -EFAULT;
215
John Sunga80d8b02014-09-09 10:06:51 -0700216 serport_set_type(tty, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 }
219
220 return -EINVAL;
221}
222
John Sunga80d8b02014-09-09 10:06:51 -0700223#ifdef CONFIG_COMPAT
224#define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
Al Virof0193d32018-09-13 22:12:15 -0400225static int serport_ldisc_compat_ioctl(struct tty_struct *tty,
John Sunga80d8b02014-09-09 10:06:51 -0700226 struct file *file,
227 unsigned int cmd, unsigned long arg)
228{
229 if (cmd == COMPAT_SPIOCSTYPE) {
230 void __user *uarg = compat_ptr(arg);
231 compat_ulong_t compat_type;
232
233 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
234 return -EFAULT;
235
236 serport_set_type(tty, compat_type);
237 return 0;
238 }
239
240 return -EINVAL;
241}
242#endif
243
Hans Verkuil51db0132016-08-04 11:16:23 -0700244static int serport_ldisc_hangup(struct tty_struct *tty)
245{
246 struct serport *serport = (struct serport *) tty->disc_data;
247 unsigned long flags;
248
249 spin_lock_irqsave(&serport->lock, flags);
250 set_bit(SERPORT_DEAD, &serport->flags);
251 spin_unlock_irqrestore(&serport->lock, flags);
252
253 wake_up_interruptible(&serport->wait);
254 return 0;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static void serport_ldisc_write_wakeup(struct tty_struct * tty)
258{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700259 struct serport *serport = (struct serport *) tty->disc_data;
260 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700262 spin_lock_irqsave(&serport->lock, flags);
263 if (test_bit(SERPORT_ACTIVE, &serport->flags))
264 serio_drv_write_wakeup(serport->serio);
265 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * The line discipline structure.
270 */
271
Alan Coxa352def2008-07-16 21:53:12 +0100272static struct tty_ldisc_ops serport_ldisc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .owner = THIS_MODULE,
274 .name = "input",
275 .open = serport_ldisc_open,
276 .close = serport_ldisc_close,
277 .read = serport_ldisc_read,
278 .ioctl = serport_ldisc_ioctl,
John Sunga80d8b02014-09-09 10:06:51 -0700279#ifdef CONFIG_COMPAT
280 .compat_ioctl = serport_ldisc_compat_ioctl,
281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .receive_buf = serport_ldisc_receive,
Hans Verkuil51db0132016-08-04 11:16:23 -0700283 .hangup = serport_ldisc_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .write_wakeup = serport_ldisc_write_wakeup
285};
286
287/*
288 * The functions for insering/removing us as a module.
289 */
290
291static int __init serport_init(void)
292{
293 int retval;
294 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
295 if (retval)
296 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
297
298 return retval;
299}
300
301static void __exit serport_exit(void)
302{
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700303 tty_unregister_ldisc(N_MOUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306module_init(serport_init);
307module_exit(serport_exit);