Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Input device TTY line discipline |
| 3 | * |
| 4 | * Copyright (c) 1999-2002 Vojtech Pavlik |
| 5 | * |
| 6 | * This is a module that converts a tty line into a much simpler |
| 7 | * 'serial io port' abstraction that the input device drivers use. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 as published by |
| 13 | * the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <asm/uaccess.h> |
| 17 | #include <linux/kernel.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/serio.h> |
| 23 | #include <linux/tty.h> |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 24 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 27 | MODULE_DESCRIPTION("Input device TTY line discipline"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_ALIAS_LDISC(N_MOUSE); |
| 30 | |
| 31 | #define SERPORT_BUSY 1 |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 32 | #define SERPORT_ACTIVE 2 |
| 33 | #define SERPORT_DEAD 3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | struct serport { |
| 36 | struct tty_struct *tty; |
| 37 | wait_queue_head_t wait; |
| 38 | struct serio *serio; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 39 | struct serio_device_id id; |
| 40 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | unsigned long flags; |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * Callback functions from the serio code. |
| 46 | */ |
| 47 | |
| 48 | static int serport_serio_write(struct serio *serio, unsigned char data) |
| 49 | { |
| 50 | struct serport *serport = serio->port_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 51 | return -(serport->tty->ops->write(serport->tty, &data, 1) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 54 | static int serport_serio_open(struct serio *serio) |
| 55 | { |
| 56 | struct serport *serport = serio->port_data; |
| 57 | unsigned long flags; |
| 58 | |
| 59 | spin_lock_irqsave(&serport->lock, flags); |
| 60 | set_bit(SERPORT_ACTIVE, &serport->flags); |
| 61 | spin_unlock_irqrestore(&serport->lock, flags); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static void serport_serio_close(struct serio *serio) |
| 68 | { |
| 69 | struct serport *serport = serio->port_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 70 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 72 | spin_lock_irqsave(&serport->lock, flags); |
| 73 | clear_bit(SERPORT_ACTIVE, &serport->flags); |
| 74 | set_bit(SERPORT_DEAD, &serport->flags); |
| 75 | spin_unlock_irqrestore(&serport->lock, flags); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | wake_up_interruptible(&serport->wait); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * serport_ldisc_open() is the routine that is called upon setting our line |
| 82 | * discipline on a tty. It prepares the serio struct. |
| 83 | */ |
| 84 | |
| 85 | static int serport_ldisc_open(struct tty_struct *tty) |
| 86 | { |
| 87 | struct serport *serport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | if (!capable(CAP_SYS_ADMIN)) |
| 90 | return -EPERM; |
| 91 | |
Pekka Enberg | a97e148 | 2005-09-06 15:18:33 -0700 | [diff] [blame] | 92 | serport = kzalloc(sizeof(struct serport), GFP_KERNEL); |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 93 | if (!serport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | serport->tty = tty; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 97 | spin_lock_init(&serport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | init_waitqueue_head(&serport->wait); |
| 99 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 100 | tty->disc_data = serport; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 101 | tty->receive_room = 256; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 102 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * serport_ldisc_close() is the opposite of serport_ldisc_open() |
| 109 | */ |
| 110 | |
| 111 | static void serport_ldisc_close(struct tty_struct *tty) |
| 112 | { |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 113 | struct serport *serport = (struct serport *) tty->disc_data; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | kfree(serport); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * serport_ldisc_receive() is called by the low level tty driver when characters |
David Engraf | 48c2701 | 2011-01-20 23:05:17 -0800 | [diff] [blame] | 120 | * are ready for us. We forward the characters and flags, one by one to the |
| 121 | * 'interrupt' routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | */ |
| 123 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 124 | static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct serport *serport = (struct serport*) tty->disc_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 127 | unsigned long flags; |
Peter Hurley | 82f91fe | 2013-12-02 13:56:03 -0500 | [diff] [blame] | 128 | unsigned int ch_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | int i; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 130 | |
| 131 | spin_lock_irqsave(&serport->lock, flags); |
| 132 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 133 | if (!test_bit(SERPORT_ACTIVE, &serport->flags)) |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 134 | goto out; |
| 135 | |
David Engraf | 48c2701 | 2011-01-20 23:05:17 -0800 | [diff] [blame] | 136 | for (i = 0; i < count; i++) { |
Peter Hurley | 82f91fe | 2013-12-02 13:56:03 -0500 | [diff] [blame] | 137 | if (fp) { |
| 138 | switch (fp[i]) { |
| 139 | case TTY_FRAME: |
| 140 | ch_flags = SERIO_FRAME; |
| 141 | break; |
David Engraf | 48c2701 | 2011-01-20 23:05:17 -0800 | [diff] [blame] | 142 | |
Peter Hurley | 82f91fe | 2013-12-02 13:56:03 -0500 | [diff] [blame] | 143 | case TTY_PARITY: |
| 144 | ch_flags = SERIO_PARITY; |
| 145 | break; |
David Engraf | 48c2701 | 2011-01-20 23:05:17 -0800 | [diff] [blame] | 146 | |
Peter Hurley | 82f91fe | 2013-12-02 13:56:03 -0500 | [diff] [blame] | 147 | default: |
| 148 | ch_flags = 0; |
| 149 | break; |
| 150 | } |
David Engraf | 48c2701 | 2011-01-20 23:05:17 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | serio_interrupt(serport->serio, cp[i], ch_flags); |
| 154 | } |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 155 | |
| 156 | out: |
| 157 | spin_unlock_irqrestore(&serport->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | * serport_ldisc_read() just waits indefinitely if everything goes well. |
| 162 | * However, when the serio driver closes the serio port, it finishes, |
| 163 | * returning 0 characters. |
| 164 | */ |
| 165 | |
| 166 | static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr) |
| 167 | { |
| 168 | struct serport *serport = (struct serport*) tty->disc_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 169 | struct serio *serio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | char name[64]; |
| 171 | |
| 172 | if (test_and_set_bit(SERPORT_BUSY, &serport->flags)) |
| 173 | return -EBUSY; |
| 174 | |
Pekka Enberg | a97e148 | 2005-09-06 15:18:33 -0700 | [diff] [blame] | 175 | serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 176 | if (!serio) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | strlcpy(serio->name, "Serial port", sizeof(serio->name)); |
| 180 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name)); |
| 181 | serio->id = serport->id; |
| 182 | serio->id.type = SERIO_RS232; |
| 183 | serio->write = serport_serio_write; |
| 184 | serio->open = serport_serio_open; |
| 185 | serio->close = serport_serio_close; |
| 186 | serio->port_data = serport; |
Dmitry Eremin-Solenikov | de838a9 | 2010-08-09 18:22:50 +0400 | [diff] [blame] | 187 | serio->dev.parent = tty->dev; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | serio_register_port(serport->serio); |
| 190 | printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 192 | wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags)); |
| 193 | serio_unregister_port(serport->serio); |
| 194 | serport->serio = NULL; |
| 195 | |
| 196 | clear_bit(SERPORT_DEAD, &serport->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | clear_bit(SERPORT_BUSY, &serport->flags); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 202 | static void serport_set_type(struct tty_struct *tty, unsigned long type) |
| 203 | { |
| 204 | struct serport *serport = tty->disc_data; |
| 205 | |
| 206 | serport->id.proto = type & 0x000000ff; |
| 207 | serport->id.id = (type & 0x0000ff00) >> 8; |
| 208 | serport->id.extra = (type & 0x00ff0000) >> 16; |
| 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* |
| 212 | * serport_ldisc_ioctl() allows to set the port protocol, and device ID |
| 213 | */ |
| 214 | |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 215 | static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file, |
| 216 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (cmd == SPIOCSTYPE) { |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 219 | unsigned long type; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (get_user(type, (unsigned long __user *) arg)) |
| 222 | return -EFAULT; |
| 223 | |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 224 | serport_set_type(tty, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 231 | #ifdef CONFIG_COMPAT |
| 232 | #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t) |
| 233 | static long serport_ldisc_compat_ioctl(struct tty_struct *tty, |
| 234 | struct file *file, |
| 235 | unsigned int cmd, unsigned long arg) |
| 236 | { |
| 237 | if (cmd == COMPAT_SPIOCSTYPE) { |
| 238 | void __user *uarg = compat_ptr(arg); |
| 239 | compat_ulong_t compat_type; |
| 240 | |
| 241 | if (get_user(compat_type, (compat_ulong_t __user *)uarg)) |
| 242 | return -EFAULT; |
| 243 | |
| 244 | serport_set_type(tty, compat_type); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | #endif |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static void serport_ldisc_write_wakeup(struct tty_struct * tty) |
| 253 | { |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 254 | struct serport *serport = (struct serport *) tty->disc_data; |
| 255 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 257 | spin_lock_irqsave(&serport->lock, flags); |
| 258 | if (test_bit(SERPORT_ACTIVE, &serport->flags)) |
| 259 | serio_drv_write_wakeup(serport->serio); |
| 260 | spin_unlock_irqrestore(&serport->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * The line discipline structure. |
| 265 | */ |
| 266 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 267 | static struct tty_ldisc_ops serport_ldisc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | .owner = THIS_MODULE, |
| 269 | .name = "input", |
| 270 | .open = serport_ldisc_open, |
| 271 | .close = serport_ldisc_close, |
| 272 | .read = serport_ldisc_read, |
| 273 | .ioctl = serport_ldisc_ioctl, |
John Sung | a80d8b0 | 2014-09-09 10:06:51 -0700 | [diff] [blame^] | 274 | #ifdef CONFIG_COMPAT |
| 275 | .compat_ioctl = serport_ldisc_compat_ioctl, |
| 276 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | .receive_buf = serport_ldisc_receive, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | .write_wakeup = serport_ldisc_write_wakeup |
| 279 | }; |
| 280 | |
| 281 | /* |
| 282 | * The functions for insering/removing us as a module. |
| 283 | */ |
| 284 | |
| 285 | static int __init serport_init(void) |
| 286 | { |
| 287 | int retval; |
| 288 | retval = tty_register_ldisc(N_MOUSE, &serport_ldisc); |
| 289 | if (retval) |
| 290 | printk(KERN_ERR "serport.c: Error registering line discipline.\n"); |
| 291 | |
| 292 | return retval; |
| 293 | } |
| 294 | |
| 295 | static void __exit serport_exit(void) |
| 296 | { |
Alexey Dobriyan | 64ccd71 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 297 | tty_unregister_ldisc(N_MOUSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | module_init(serport_init); |
| 301 | module_exit(serport_exit); |