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> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/serio.h> |
| 22 | #include <linux/tty.h> |
| 23 | |
| 24 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 25 | MODULE_DESCRIPTION("Input device TTY line discipline"); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_ALIAS_LDISC(N_MOUSE); |
| 28 | |
| 29 | #define SERPORT_BUSY 1 |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 30 | #define SERPORT_ACTIVE 2 |
| 31 | #define SERPORT_DEAD 3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | struct serport { |
| 34 | struct tty_struct *tty; |
| 35 | wait_queue_head_t wait; |
| 36 | struct serio *serio; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 37 | struct serio_device_id id; |
| 38 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | unsigned long flags; |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Callback functions from the serio code. |
| 44 | */ |
| 45 | |
| 46 | static int serport_serio_write(struct serio *serio, unsigned char data) |
| 47 | { |
| 48 | struct serport *serport = serio->port_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 49 | return -(serport->tty->ops->write(serport->tty, &data, 1) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 52 | static int serport_serio_open(struct serio *serio) |
| 53 | { |
| 54 | struct serport *serport = serio->port_data; |
| 55 | unsigned long flags; |
| 56 | |
| 57 | spin_lock_irqsave(&serport->lock, flags); |
| 58 | set_bit(SERPORT_ACTIVE, &serport->flags); |
| 59 | spin_unlock_irqrestore(&serport->lock, flags); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static void serport_serio_close(struct serio *serio) |
| 66 | { |
| 67 | struct serport *serport = serio->port_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 68 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 70 | spin_lock_irqsave(&serport->lock, flags); |
| 71 | clear_bit(SERPORT_ACTIVE, &serport->flags); |
| 72 | set_bit(SERPORT_DEAD, &serport->flags); |
| 73 | spin_unlock_irqrestore(&serport->lock, flags); |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | wake_up_interruptible(&serport->wait); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * serport_ldisc_open() is the routine that is called upon setting our line |
| 80 | * discipline on a tty. It prepares the serio struct. |
| 81 | */ |
| 82 | |
| 83 | static int serport_ldisc_open(struct tty_struct *tty) |
| 84 | { |
| 85 | struct serport *serport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | if (!capable(CAP_SYS_ADMIN)) |
| 88 | return -EPERM; |
| 89 | |
Pekka Enberg | a97e148 | 2005-09-06 15:18:33 -0700 | [diff] [blame] | 90 | serport = kzalloc(sizeof(struct serport), GFP_KERNEL); |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 91 | if (!serport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | serport->tty = tty; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 95 | spin_lock_init(&serport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | init_waitqueue_head(&serport->wait); |
| 97 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 98 | tty->disc_data = serport; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 99 | tty->receive_room = 256; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 100 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * serport_ldisc_close() is the opposite of serport_ldisc_open() |
| 107 | */ |
| 108 | |
| 109 | static void serport_ldisc_close(struct tty_struct *tty) |
| 110 | { |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 111 | struct serport *serport = (struct serport *) tty->disc_data; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | kfree(serport); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * serport_ldisc_receive() is called by the low level tty driver when characters |
| 118 | * are ready for us. We forward the characters, one by one to the 'interrupt' |
| 119 | * routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | */ |
| 121 | |
| 122 | static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) |
| 123 | { |
| 124 | struct serport *serport = (struct serport*) tty->disc_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 125 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | int i; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 127 | |
| 128 | spin_lock_irqsave(&serport->lock, flags); |
| 129 | |
| 130 | if (!test_bit(SERPORT_ACTIVE, &serport->flags)) |
| 131 | goto out; |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | for (i = 0; i < count; i++) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 134 | serio_interrupt(serport->serio, cp[i], 0); |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 135 | |
| 136 | out: |
| 137 | spin_unlock_irqrestore(&serport->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * serport_ldisc_read() just waits indefinitely if everything goes well. |
| 142 | * However, when the serio driver closes the serio port, it finishes, |
| 143 | * returning 0 characters. |
| 144 | */ |
| 145 | |
| 146 | static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr) |
| 147 | { |
| 148 | struct serport *serport = (struct serport*) tty->disc_data; |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 149 | struct serio *serio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | char name[64]; |
| 151 | |
| 152 | if (test_and_set_bit(SERPORT_BUSY, &serport->flags)) |
| 153 | return -EBUSY; |
| 154 | |
Pekka Enberg | a97e148 | 2005-09-06 15:18:33 -0700 | [diff] [blame] | 155 | serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 156 | if (!serio) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | strlcpy(serio->name, "Serial port", sizeof(serio->name)); |
| 160 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name)); |
| 161 | serio->id = serport->id; |
| 162 | serio->id.type = SERIO_RS232; |
| 163 | serio->write = serport_serio_write; |
| 164 | serio->open = serport_serio_open; |
| 165 | serio->close = serport_serio_close; |
| 166 | serio->port_data = serport; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | serio_register_port(serport->serio); |
| 169 | printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 171 | wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags)); |
| 172 | serio_unregister_port(serport->serio); |
| 173 | serport->serio = NULL; |
| 174 | |
| 175 | clear_bit(SERPORT_DEAD, &serport->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | clear_bit(SERPORT_BUSY, &serport->flags); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * serport_ldisc_ioctl() allows to set the port protocol, and device ID |
| 183 | */ |
| 184 | |
| 185 | static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg) |
| 186 | { |
| 187 | struct serport *serport = (struct serport*) tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned long type; |
| 189 | |
| 190 | if (cmd == SPIOCSTYPE) { |
| 191 | if (get_user(type, (unsigned long __user *) arg)) |
| 192 | return -EFAULT; |
| 193 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 194 | serport->id.proto = type & 0x000000ff; |
| 195 | serport->id.id = (type & 0x0000ff00) >> 8; |
| 196 | serport->id.extra = (type & 0x00ff0000) >> 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | static void serport_ldisc_write_wakeup(struct tty_struct * tty) |
| 205 | { |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 206 | struct serport *serport = (struct serport *) tty->disc_data; |
| 207 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Dmitry Torokhov | 1ff2c87 | 2005-05-16 21:53:07 -0700 | [diff] [blame] | 209 | spin_lock_irqsave(&serport->lock, flags); |
| 210 | if (test_bit(SERPORT_ACTIVE, &serport->flags)) |
| 211 | serio_drv_write_wakeup(serport->serio); |
| 212 | spin_unlock_irqrestore(&serport->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * The line discipline structure. |
| 217 | */ |
| 218 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame^] | 219 | static struct tty_ldisc_ops serport_ldisc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | .owner = THIS_MODULE, |
| 221 | .name = "input", |
| 222 | .open = serport_ldisc_open, |
| 223 | .close = serport_ldisc_close, |
| 224 | .read = serport_ldisc_read, |
| 225 | .ioctl = serport_ldisc_ioctl, |
| 226 | .receive_buf = serport_ldisc_receive, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | .write_wakeup = serport_ldisc_write_wakeup |
| 228 | }; |
| 229 | |
| 230 | /* |
| 231 | * The functions for insering/removing us as a module. |
| 232 | */ |
| 233 | |
| 234 | static int __init serport_init(void) |
| 235 | { |
| 236 | int retval; |
| 237 | retval = tty_register_ldisc(N_MOUSE, &serport_ldisc); |
| 238 | if (retval) |
| 239 | printk(KERN_ERR "serport.c: Error registering line discipline.\n"); |
| 240 | |
| 241 | return retval; |
| 242 | } |
| 243 | |
| 244 | static void __exit serport_exit(void) |
| 245 | { |
Alexey Dobriyan | 64ccd71 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 246 | tty_unregister_ldisc(N_MOUSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | module_init(serport_init); |
| 250 | module_exit(serport_exit); |