1 // SPDX-License-Identifier: GPL-2.0-only 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 11 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> 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 28 #define SERPORT_ACTIVE 2 29 #define SERPORT_DEAD 3 30 31 struct serport { 32 struct tty_struct *tty; 33 wait_queue_head_t wait; 34 struct serio *serio; 35 struct serio_device_id id; 36 spinlock_t lock; 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; 47 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1); 48 } 49 50 static 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 63 static void serport_serio_close(struct serio *serio) 64 { 65 struct serport *serport = serio->port_data; 66 unsigned long flags; 67 68 spin_lock_irqsave(&serport->lock, flags); 69 clear_bit(SERPORT_ACTIVE, &serport->flags); 70 spin_unlock_irqrestore(&serport->lock, flags); 71 } 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 78 static int serport_ldisc_open(struct tty_struct *tty) 79 { 80 struct serport *serport; 81 82 if (!capable(CAP_SYS_ADMIN)) 83 return -EPERM; 84 85 serport = kzalloc(sizeof(struct serport), GFP_KERNEL); 86 if (!serport) 87 return -ENOMEM; 88 89 serport->tty = tty; 90 spin_lock_init(&serport->lock); 91 init_waitqueue_head(&serport->wait); 92 93 tty->disc_data = serport; 94 tty->receive_room = 256; 95 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 96 97 return 0; 98 } 99 100 /* 101 * serport_ldisc_close() is the opposite of serport_ldisc_open() 102 */ 103 104 static void serport_ldisc_close(struct tty_struct *tty) 105 { 106 struct serport *serport = tty->disc_data; 107 108 kfree(serport); 109 } 110 111 /* 112 * serport_ldisc_receive() is called by the low level tty driver when characters 113 * are ready for us. We forward the characters and flags, one by one to the 114 * 'interrupt' routine. 115 */ 116 117 static void serport_ldisc_receive(struct tty_struct *tty, 118 const unsigned char *cp, const char *fp, 119 size_t count) 120 { 121 struct serport *serport = tty->disc_data; 122 unsigned long flags; 123 unsigned int ch_flags = 0; 124 int i; 125 126 spin_lock_irqsave(&serport->lock, flags); 127 128 if (!test_bit(SERPORT_ACTIVE, &serport->flags)) 129 goto out; 130 131 for (i = 0; i < count; i++) { 132 if (fp) { 133 switch (fp[i]) { 134 case TTY_FRAME: 135 ch_flags = SERIO_FRAME; 136 break; 137 138 case TTY_PARITY: 139 ch_flags = SERIO_PARITY; 140 break; 141 142 default: 143 ch_flags = 0; 144 break; 145 } 146 } 147 148 serio_interrupt(serport->serio, cp[i], ch_flags); 149 } 150 151 out: 152 spin_unlock_irqrestore(&serport->lock, flags); 153 } 154 155 /* 156 * serport_ldisc_read() just waits indefinitely if everything goes well. 157 * However, when the serio driver closes the serio port, it finishes, 158 * returning 0 characters. 159 */ 160 161 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, 162 unsigned char *kbuf, size_t nr, 163 void **cookie, unsigned long offset) 164 { 165 struct serport *serport = tty->disc_data; 166 struct serio *serio; 167 168 if (test_and_set_bit(SERPORT_BUSY, &serport->flags)) 169 return -EBUSY; 170 171 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL); 172 if (!serio) 173 return -ENOMEM; 174 175 strscpy(serio->name, "Serial port", sizeof(serio->name)); 176 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty)); 177 serio->id = serport->id; 178 serio->id.type = SERIO_RS232; 179 serio->write = serport_serio_write; 180 serio->open = serport_serio_open; 181 serio->close = serport_serio_close; 182 serio->port_data = serport; 183 serio->dev.parent = tty->dev; 184 185 serio_register_port(serport->serio); 186 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty)); 187 188 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags)); 189 serio_unregister_port(serport->serio); 190 serport->serio = NULL; 191 192 clear_bit(SERPORT_DEAD, &serport->flags); 193 clear_bit(SERPORT_BUSY, &serport->flags); 194 195 return 0; 196 } 197 198 static void serport_set_type(struct tty_struct *tty, unsigned long type) 199 { 200 struct serport *serport = tty->disc_data; 201 202 serport->id.proto = type & 0x000000ff; 203 serport->id.id = (type & 0x0000ff00) >> 8; 204 serport->id.extra = (type & 0x00ff0000) >> 16; 205 } 206 207 /* 208 * serport_ldisc_ioctl() allows to set the port protocol, and device ID 209 */ 210 211 static int serport_ldisc_ioctl(struct tty_struct *tty, unsigned int cmd, 212 unsigned long arg) 213 { 214 if (cmd == SPIOCSTYPE) { 215 unsigned long type; 216 217 if (get_user(type, (unsigned long __user *) arg)) 218 return -EFAULT; 219 220 serport_set_type(tty, type); 221 return 0; 222 } 223 224 return -EINVAL; 225 } 226 227 #ifdef CONFIG_COMPAT 228 #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t) 229 static int serport_ldisc_compat_ioctl(struct tty_struct *tty, 230 unsigned int cmd, unsigned long arg) 231 { 232 if (cmd == COMPAT_SPIOCSTYPE) { 233 void __user *uarg = compat_ptr(arg); 234 compat_ulong_t compat_type; 235 236 if (get_user(compat_type, (compat_ulong_t __user *)uarg)) 237 return -EFAULT; 238 239 serport_set_type(tty, compat_type); 240 return 0; 241 } 242 243 return -EINVAL; 244 } 245 #endif 246 247 static void serport_ldisc_hangup(struct tty_struct *tty) 248 { 249 struct serport *serport = tty->disc_data; 250 unsigned long flags; 251 252 spin_lock_irqsave(&serport->lock, flags); 253 set_bit(SERPORT_DEAD, &serport->flags); 254 spin_unlock_irqrestore(&serport->lock, flags); 255 256 wake_up_interruptible(&serport->wait); 257 } 258 259 static void serport_ldisc_write_wakeup(struct tty_struct * tty) 260 { 261 struct serport *serport = tty->disc_data; 262 unsigned long flags; 263 264 spin_lock_irqsave(&serport->lock, flags); 265 if (test_bit(SERPORT_ACTIVE, &serport->flags)) 266 serio_drv_write_wakeup(serport->serio); 267 spin_unlock_irqrestore(&serport->lock, flags); 268 } 269 270 /* 271 * The line discipline structure. 272 */ 273 274 static struct tty_ldisc_ops serport_ldisc = { 275 .owner = THIS_MODULE, 276 .num = N_MOUSE, 277 .name = "input", 278 .open = serport_ldisc_open, 279 .close = serport_ldisc_close, 280 .read = serport_ldisc_read, 281 .ioctl = serport_ldisc_ioctl, 282 #ifdef CONFIG_COMPAT 283 .compat_ioctl = serport_ldisc_compat_ioctl, 284 #endif 285 .receive_buf = serport_ldisc_receive, 286 .hangup = serport_ldisc_hangup, 287 .write_wakeup = serport_ldisc_write_wakeup 288 }; 289 290 /* 291 * The functions for insering/removing us as a module. 292 */ 293 294 static int __init serport_init(void) 295 { 296 int retval; 297 retval = tty_register_ldisc(&serport_ldisc); 298 if (retval) 299 printk(KERN_ERR "serport.c: Error registering line discipline.\n"); 300 301 return retval; 302 } 303 304 static void __exit serport_exit(void) 305 { 306 tty_unregister_ldisc(&serport_ldisc); 307 } 308 309 module_init(serport_init); 310 module_exit(serport_exit); 311