Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
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 | ||
1da177e4 | 11 | |
7c0f6ba6 | 12 | #include <linux/uaccess.h> |
1da177e4 | 13 | #include <linux/kernel.h> |
d43c36dc | 14 | #include <linux/sched.h> |
1da177e4 LT |
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> | |
a80d8b02 | 20 | #include <linux/compat.h> |
1da177e4 LT |
21 | |
22 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
23 | MODULE_DESCRIPTION("Input device TTY line discipline"); | |
24 | MODULE_LICENSE("GPL"); | |
25 | MODULE_ALIAS_LDISC(N_MOUSE); | |
26 | ||
27 | #define SERPORT_BUSY 1 | |
1ff2c873 DT |
28 | #define SERPORT_ACTIVE 2 |
29 | #define SERPORT_DEAD 3 | |
1da177e4 LT |
30 | |
31 | struct serport { | |
32 | struct tty_struct *tty; | |
33 | wait_queue_head_t wait; | |
34 | struct serio *serio; | |
1ff2c873 DT |
35 | struct serio_device_id id; |
36 | spinlock_t lock; | |
1da177e4 LT |
37 | unsigned long flags; |
38 | }; | |
39 | ||
40 | /* | |
41 | * Callback functions from the serio code. | |
42 | */ | |
43 | ||
44 | static int serport_serio_write(struct serio *serio, unsigned char data) | |
45 | { | |
46 | struct serport *serport = serio->port_data; | |
f34d7a5b | 47 | return -(serport->tty->ops->write(serport->tty, &data, 1) != 1); |
1da177e4 LT |
48 | } |
49 | ||
1ff2c873 DT |
50 | static int serport_serio_open(struct serio *serio) |
51 | { | |
52 | struct serport *serport = serio->port_data; | |
1ff2c873 | 53 | |
f7d15dcc | 54 | guard(spinlock_irqsave)(&serport->lock); |
1ff2c873 | 55 | set_bit(SERPORT_ACTIVE, &serport->flags); |
1ff2c873 DT |
56 | |
57 | return 0; | |
58 | } | |
59 | ||
60 | ||
1da177e4 LT |
61 | static void serport_serio_close(struct serio *serio) |
62 | { | |
63 | struct serport *serport = serio->port_data; | |
1ff2c873 | 64 | |
f7d15dcc | 65 | guard(spinlock_irqsave)(&serport->lock); |
1ff2c873 | 66 | clear_bit(SERPORT_ACTIVE, &serport->flags); |
1da177e4 LT |
67 | } |
68 | ||
69 | /* | |
70 | * serport_ldisc_open() is the routine that is called upon setting our line | |
71 | * discipline on a tty. It prepares the serio struct. | |
72 | */ | |
73 | ||
74 | static int serport_ldisc_open(struct tty_struct *tty) | |
75 | { | |
76 | struct serport *serport; | |
1da177e4 LT |
77 | |
78 | if (!capable(CAP_SYS_ADMIN)) | |
79 | return -EPERM; | |
80 | ||
06b449d7 | 81 | serport = kzalloc(sizeof(*serport), GFP_KERNEL); |
1ff2c873 | 82 | if (!serport) |
1da177e4 | 83 | return -ENOMEM; |
1da177e4 | 84 | |
1da177e4 | 85 | serport->tty = tty; |
1ff2c873 | 86 | spin_lock_init(&serport->lock); |
1da177e4 LT |
87 | init_waitqueue_head(&serport->wait); |
88 | ||
1ff2c873 | 89 | tty->disc_data = serport; |
33f0f88f | 90 | tty->receive_room = 256; |
1ff2c873 DT |
91 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
92 | ||
1da177e4 LT |
93 | return 0; |
94 | } | |
95 | ||
96 | /* | |
97 | * serport_ldisc_close() is the opposite of serport_ldisc_open() | |
98 | */ | |
99 | ||
100 | static void serport_ldisc_close(struct tty_struct *tty) | |
101 | { | |
ca1a8d2f | 102 | struct serport *serport = tty->disc_data; |
1ff2c873 | 103 | |
1da177e4 LT |
104 | kfree(serport); |
105 | } | |
106 | ||
107 | /* | |
108 | * serport_ldisc_receive() is called by the low level tty driver when characters | |
48c27016 DE |
109 | * are ready for us. We forward the characters and flags, one by one to the |
110 | * 'interrupt' routine. | |
1da177e4 LT |
111 | */ |
112 | ||
a8d9cd23 | 113 | static void serport_ldisc_receive(struct tty_struct *tty, const u8 *cp, |
892bc209 | 114 | const u8 *fp, size_t count) |
1da177e4 | 115 | { |
ca1a8d2f | 116 | struct serport *serport = tty->disc_data; |
82f91fe0 | 117 | unsigned int ch_flags = 0; |
1da177e4 | 118 | int i; |
1ff2c873 | 119 | |
f7d15dcc | 120 | guard(spinlock_irqsave)(&serport->lock); |
1ff2c873 | 121 | |
55db4c64 | 122 | if (!test_bit(SERPORT_ACTIVE, &serport->flags)) |
f7d15dcc | 123 | return; |
1ff2c873 | 124 | |
48c27016 | 125 | for (i = 0; i < count; i++) { |
82f91fe0 PH |
126 | if (fp) { |
127 | switch (fp[i]) { | |
128 | case TTY_FRAME: | |
129 | ch_flags = SERIO_FRAME; | |
130 | break; | |
131 | ||
132 | case TTY_PARITY: | |
133 | ch_flags = SERIO_PARITY; | |
134 | break; | |
135 | ||
136 | default: | |
137 | ch_flags = 0; | |
138 | break; | |
139 | } | |
48c27016 DE |
140 | } |
141 | ||
142 | serio_interrupt(serport->serio, cp[i], ch_flags); | |
143 | } | |
1da177e4 LT |
144 | } |
145 | ||
1da177e4 LT |
146 | /* |
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. | |
150 | */ | |
151 | ||
3b830a9c | 152 | static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, |
49b8220c JSS |
153 | u8 *kbuf, size_t nr, void **cookie, |
154 | unsigned long offset) | |
1da177e4 | 155 | { |
ca1a8d2f | 156 | struct serport *serport = tty->disc_data; |
1ff2c873 | 157 | struct serio *serio; |
1da177e4 LT |
158 | |
159 | if (test_and_set_bit(SERPORT_BUSY, &serport->flags)) | |
160 | return -EBUSY; | |
161 | ||
06b449d7 | 162 | serport->serio = serio = kzalloc(sizeof(*serio), GFP_KERNEL); |
1ff2c873 DT |
163 | if (!serio) |
164 | return -ENOMEM; | |
165 | ||
a9f08ad7 | 166 | strscpy(serio->name, "Serial port", sizeof(serio->name)); |
429b4749 | 167 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty)); |
1ff2c873 DT |
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; | |
de838a93 | 174 | serio->dev.parent = tty->dev; |
1ff2c873 | 175 | |
1da177e4 | 176 | serio_register_port(serport->serio); |
429b4749 | 177 | printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty)); |
1ff2c873 DT |
178 | |
179 | wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags)); | |
1da177e4 | 180 | serio_unregister_port(serport->serio); |
1ff2c873 | 181 | serport->serio = NULL; |
1da177e4 | 182 | |
1ff2c873 | 183 | clear_bit(SERPORT_DEAD, &serport->flags); |
1da177e4 LT |
184 | clear_bit(SERPORT_BUSY, &serport->flags); |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
a80d8b02 JS |
189 | static void serport_set_type(struct tty_struct *tty, unsigned long type) |
190 | { | |
191 | struct serport *serport = tty->disc_data; | |
192 | ||
193 | serport->id.proto = type & 0x000000ff; | |
194 | serport->id.id = (type & 0x0000ff00) >> 8; | |
195 | serport->id.extra = (type & 0x00ff0000) >> 16; | |
196 | } | |
197 | ||
1da177e4 LT |
198 | /* |
199 | * serport_ldisc_ioctl() allows to set the port protocol, and device ID | |
200 | */ | |
201 | ||
d78328bc JS |
202 | static int serport_ldisc_ioctl(struct tty_struct *tty, unsigned int cmd, |
203 | unsigned long arg) | |
1da177e4 | 204 | { |
1da177e4 | 205 | if (cmd == SPIOCSTYPE) { |
a80d8b02 JS |
206 | unsigned long type; |
207 | ||
1da177e4 LT |
208 | if (get_user(type, (unsigned long __user *) arg)) |
209 | return -EFAULT; | |
210 | ||
a80d8b02 JS |
211 | serport_set_type(tty, type); |
212 | return 0; | |
213 | } | |
214 | ||
215 | return -EINVAL; | |
216 | } | |
217 | ||
218 | #ifdef CONFIG_COMPAT | |
219 | #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t) | |
f0193d3e | 220 | static int serport_ldisc_compat_ioctl(struct tty_struct *tty, |
a80d8b02 JS |
221 | unsigned int cmd, unsigned long arg) |
222 | { | |
223 | if (cmd == COMPAT_SPIOCSTYPE) { | |
224 | void __user *uarg = compat_ptr(arg); | |
225 | compat_ulong_t compat_type; | |
226 | ||
227 | if (get_user(compat_type, (compat_ulong_t __user *)uarg)) | |
228 | return -EFAULT; | |
1da177e4 | 229 | |
a80d8b02 | 230 | serport_set_type(tty, compat_type); |
1da177e4 LT |
231 | return 0; |
232 | } | |
233 | ||
234 | return -EINVAL; | |
235 | } | |
a80d8b02 | 236 | #endif |
1da177e4 | 237 | |
28f194da | 238 | static void serport_ldisc_hangup(struct tty_struct *tty) |
51db0137 | 239 | { |
ca1a8d2f | 240 | struct serport *serport = tty->disc_data; |
51db0137 | 241 | |
f7d15dcc DT |
242 | scoped_guard(spinlock_irqsave, &serport->lock) |
243 | set_bit(SERPORT_DEAD, &serport->flags); | |
51db0137 HV |
244 | |
245 | wake_up_interruptible(&serport->wait); | |
51db0137 HV |
246 | } |
247 | ||
1da177e4 LT |
248 | static void serport_ldisc_write_wakeup(struct tty_struct * tty) |
249 | { | |
ca1a8d2f | 250 | struct serport *serport = tty->disc_data; |
1da177e4 | 251 | |
f7d15dcc DT |
252 | guard(spinlock_irqsave)(&serport->lock); |
253 | ||
1ff2c873 DT |
254 | if (test_bit(SERPORT_ACTIVE, &serport->flags)) |
255 | serio_drv_write_wakeup(serport->serio); | |
1da177e4 LT |
256 | } |
257 | ||
258 | /* | |
259 | * The line discipline structure. | |
260 | */ | |
261 | ||
a352def2 | 262 | static struct tty_ldisc_ops serport_ldisc = { |
1da177e4 | 263 | .owner = THIS_MODULE, |
fbadf70a | 264 | .num = N_MOUSE, |
1da177e4 LT |
265 | .name = "input", |
266 | .open = serport_ldisc_open, | |
267 | .close = serport_ldisc_close, | |
268 | .read = serport_ldisc_read, | |
269 | .ioctl = serport_ldisc_ioctl, | |
a80d8b02 JS |
270 | #ifdef CONFIG_COMPAT |
271 | .compat_ioctl = serport_ldisc_compat_ioctl, | |
272 | #endif | |
1da177e4 | 273 | .receive_buf = serport_ldisc_receive, |
51db0137 | 274 | .hangup = serport_ldisc_hangup, |
1da177e4 LT |
275 | .write_wakeup = serport_ldisc_write_wakeup |
276 | }; | |
277 | ||
278 | /* | |
279 | * The functions for insering/removing us as a module. | |
280 | */ | |
281 | ||
282 | static int __init serport_init(void) | |
283 | { | |
284 | int retval; | |
fbadf70a | 285 | retval = tty_register_ldisc(&serport_ldisc); |
1da177e4 LT |
286 | if (retval) |
287 | printk(KERN_ERR "serport.c: Error registering line discipline.\n"); | |
288 | ||
289 | return retval; | |
290 | } | |
291 | ||
292 | static void __exit serport_exit(void) | |
293 | { | |
f81ee8b8 | 294 | tty_unregister_ldisc(&serport_ldisc); |
1da177e4 LT |
295 | } |
296 | ||
297 | module_init(serport_init); | |
298 | module_exit(serport_exit); |