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