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 |
| 30 | |
| 31 | struct serport { |
| 32 | struct tty_struct *tty; |
| 33 | wait_queue_head_t wait; |
| 34 | struct serio *serio; |
| 35 | unsigned long flags; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * Callback functions from the serio code. |
| 40 | */ |
| 41 | |
| 42 | static int serport_serio_write(struct serio *serio, unsigned char data) |
| 43 | { |
| 44 | struct serport *serport = serio->port_data; |
| 45 | return -(serport->tty->driver->write(serport->tty, &data, 1) != 1); |
| 46 | } |
| 47 | |
| 48 | static void serport_serio_close(struct serio *serio) |
| 49 | { |
| 50 | struct serport *serport = serio->port_data; |
| 51 | |
| 52 | serport->serio->id.type = 0; |
| 53 | wake_up_interruptible(&serport->wait); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * serport_ldisc_open() is the routine that is called upon setting our line |
| 58 | * discipline on a tty. It prepares the serio struct. |
| 59 | */ |
| 60 | |
| 61 | static int serport_ldisc_open(struct tty_struct *tty) |
| 62 | { |
| 63 | struct serport *serport; |
| 64 | struct serio *serio; |
| 65 | char name[64]; |
| 66 | |
| 67 | if (!capable(CAP_SYS_ADMIN)) |
| 68 | return -EPERM; |
| 69 | |
| 70 | serport = kmalloc(sizeof(struct serport), GFP_KERNEL); |
| 71 | serio = kmalloc(sizeof(struct serio), GFP_KERNEL); |
| 72 | if (unlikely(!serport || !serio)) { |
| 73 | kfree(serport); |
| 74 | kfree(serio); |
| 75 | return -ENOMEM; |
| 76 | } |
| 77 | |
| 78 | memset(serport, 0, sizeof(struct serport)); |
| 79 | serport->serio = serio; |
| 80 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 81 | serport->tty = tty; |
| 82 | tty->disc_data = serport; |
| 83 | |
| 84 | memset(serio, 0, sizeof(struct serio)); |
| 85 | strlcpy(serio->name, "Serial port", sizeof(serio->name)); |
| 86 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name)); |
| 87 | serio->id.type = SERIO_RS232; |
| 88 | serio->write = serport_serio_write; |
| 89 | serio->close = serport_serio_close; |
| 90 | serio->port_data = serport; |
| 91 | |
| 92 | init_waitqueue_head(&serport->wait); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * serport_ldisc_close() is the opposite of serport_ldisc_open() |
| 99 | */ |
| 100 | |
| 101 | static void serport_ldisc_close(struct tty_struct *tty) |
| 102 | { |
| 103 | struct serport *serport = (struct serport*) tty->disc_data; |
| 104 | kfree(serport); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * serport_ldisc_receive() is called by the low level tty driver when characters |
| 109 | * are ready for us. We forward the characters, one by one to the 'interrupt' |
| 110 | * routine. |
| 111 | * |
| 112 | * FIXME: We should get pt_regs from the tty layer and forward them to |
| 113 | * serio_interrupt here. |
| 114 | */ |
| 115 | |
| 116 | static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) |
| 117 | { |
| 118 | struct serport *serport = (struct serport*) tty->disc_data; |
| 119 | int i; |
| 120 | for (i = 0; i < count; i++) |
| 121 | serio_interrupt(serport->serio, cp[i], 0, NULL); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * serport_ldisc_room() reports how much room we do have for receiving data. |
| 126 | * Although we in fact have infinite room, we need to specify some value |
| 127 | * here, and 256 seems to be reasonable. |
| 128 | */ |
| 129 | |
| 130 | static int serport_ldisc_room(struct tty_struct *tty) |
| 131 | { |
| 132 | return 256; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * serport_ldisc_read() just waits indefinitely if everything goes well. |
| 137 | * However, when the serio driver closes the serio port, it finishes, |
| 138 | * returning 0 characters. |
| 139 | */ |
| 140 | |
| 141 | static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr) |
| 142 | { |
| 143 | struct serport *serport = (struct serport*) tty->disc_data; |
| 144 | char name[64]; |
| 145 | |
| 146 | if (test_and_set_bit(SERPORT_BUSY, &serport->flags)) |
| 147 | return -EBUSY; |
| 148 | |
| 149 | serio_register_port(serport->serio); |
| 150 | printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name)); |
| 151 | wait_event_interruptible(serport->wait, !serport->serio->id.type); |
| 152 | serio_unregister_port(serport->serio); |
| 153 | |
| 154 | clear_bit(SERPORT_BUSY, &serport->flags); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * serport_ldisc_ioctl() allows to set the port protocol, and device ID |
| 161 | */ |
| 162 | |
| 163 | static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg) |
| 164 | { |
| 165 | struct serport *serport = (struct serport*) tty->disc_data; |
| 166 | struct serio *serio = serport->serio; |
| 167 | unsigned long type; |
| 168 | |
| 169 | if (cmd == SPIOCSTYPE) { |
| 170 | if (get_user(type, (unsigned long __user *) arg)) |
| 171 | return -EFAULT; |
| 172 | |
| 173 | serio->id.proto = type & 0x000000ff; |
| 174 | serio->id.id = (type & 0x0000ff00) >> 8; |
| 175 | serio->id.extra = (type & 0x00ff0000) >> 16; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | static void serport_ldisc_write_wakeup(struct tty_struct * tty) |
| 184 | { |
| 185 | struct serport *sp = (struct serport *) tty->disc_data; |
| 186 | |
| 187 | serio_drv_write_wakeup(sp->serio); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * The line discipline structure. |
| 192 | */ |
| 193 | |
| 194 | static struct tty_ldisc serport_ldisc = { |
| 195 | .owner = THIS_MODULE, |
| 196 | .name = "input", |
| 197 | .open = serport_ldisc_open, |
| 198 | .close = serport_ldisc_close, |
| 199 | .read = serport_ldisc_read, |
| 200 | .ioctl = serport_ldisc_ioctl, |
| 201 | .receive_buf = serport_ldisc_receive, |
| 202 | .receive_room = serport_ldisc_room, |
| 203 | .write_wakeup = serport_ldisc_write_wakeup |
| 204 | }; |
| 205 | |
| 206 | /* |
| 207 | * The functions for insering/removing us as a module. |
| 208 | */ |
| 209 | |
| 210 | static int __init serport_init(void) |
| 211 | { |
| 212 | int retval; |
| 213 | retval = tty_register_ldisc(N_MOUSE, &serport_ldisc); |
| 214 | if (retval) |
| 215 | printk(KERN_ERR "serport.c: Error registering line discipline.\n"); |
| 216 | |
| 217 | return retval; |
| 218 | } |
| 219 | |
| 220 | static void __exit serport_exit(void) |
| 221 | { |
| 222 | tty_register_ldisc(N_MOUSE, NULL); |
| 223 | } |
| 224 | |
| 225 | module_init(serport_init); |
| 226 | module_exit(serport_exit); |