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> | |
a80d8b02 | 24 | #include <linux/compat.h> |
1da177e4 LT |
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 | |
1ff2c873 DT |
32 | #define SERPORT_ACTIVE 2 |
33 | #define SERPORT_DEAD 3 | |
1da177e4 LT |
34 | |
35 | struct serport { | |
36 | struct tty_struct *tty; | |
37 | wait_queue_head_t wait; | |
38 | struct serio *serio; | |
1ff2c873 DT |
39 | struct serio_device_id id; |
40 | spinlock_t lock; | |
1da177e4 LT |
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; | |
f34d7a5b | 51 | return -(serport->tty->ops->write(serport->tty, &data, 1) != 1); |
1da177e4 LT |
52 | } |
53 | ||
1ff2c873 DT |
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 | ||
1da177e4 LT |
67 | static void serport_serio_close(struct serio *serio) |
68 | { | |
69 | struct serport *serport = serio->port_data; | |
1ff2c873 DT |
70 | unsigned long flags; |
71 | ||
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); | |
1da177e4 | 76 | |
1da177e4 LT |
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; | |
1da177e4 LT |
88 | |
89 | if (!capable(CAP_SYS_ADMIN)) | |
90 | return -EPERM; | |
91 | ||
a97e148a | 92 | serport = kzalloc(sizeof(struct serport), GFP_KERNEL); |
1ff2c873 | 93 | if (!serport) |
1da177e4 | 94 | return -ENOMEM; |
1da177e4 | 95 | |
1da177e4 | 96 | serport->tty = tty; |
1ff2c873 | 97 | spin_lock_init(&serport->lock); |
1da177e4 LT |
98 | init_waitqueue_head(&serport->wait); |
99 | ||
1ff2c873 | 100 | tty->disc_data = serport; |
33f0f88f | 101 | tty->receive_room = 256; |
1ff2c873 DT |
102 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
103 | ||
1da177e4 LT |
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 | { | |
1ff2c873 DT |
113 | struct serport *serport = (struct serport *) tty->disc_data; |
114 | ||
1da177e4 LT |
115 | kfree(serport); |
116 | } | |
117 | ||
118 | /* | |
119 | * serport_ldisc_receive() is called by the low level tty driver when characters | |
48c27016 DE |
120 | * are ready for us. We forward the characters and flags, one by one to the |
121 | * 'interrupt' routine. | |
1da177e4 LT |
122 | */ |
123 | ||
55db4c64 | 124 | static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) |
1da177e4 LT |
125 | { |
126 | struct serport *serport = (struct serport*) tty->disc_data; | |
1ff2c873 | 127 | unsigned long flags; |
82f91fe0 | 128 | unsigned int ch_flags = 0; |
1da177e4 | 129 | int i; |
1ff2c873 DT |
130 | |
131 | spin_lock_irqsave(&serport->lock, flags); | |
132 | ||
55db4c64 | 133 | if (!test_bit(SERPORT_ACTIVE, &serport->flags)) |
1ff2c873 DT |
134 | goto out; |
135 | ||
48c27016 | 136 | for (i = 0; i < count; i++) { |
82f91fe0 PH |
137 | if (fp) { |
138 | switch (fp[i]) { | |
139 | case TTY_FRAME: | |
140 | ch_flags = SERIO_FRAME; | |
141 | break; | |
142 | ||
143 | case TTY_PARITY: | |
144 | ch_flags = SERIO_PARITY; | |
145 | break; | |
146 | ||
147 | default: | |
148 | ch_flags = 0; | |
149 | break; | |
150 | } | |
48c27016 DE |
151 | } |
152 | ||
153 | serio_interrupt(serport->serio, cp[i], ch_flags); | |
154 | } | |
1ff2c873 DT |
155 | |
156 | out: | |
157 | spin_unlock_irqrestore(&serport->lock, flags); | |
1da177e4 LT |
158 | } |
159 | ||
1da177e4 LT |
160 | /* |
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; | |
1ff2c873 | 169 | struct serio *serio; |
1da177e4 LT |
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)); | |
429b4749 | 179 | snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty)); |
1ff2c873 DT |
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 | 188 | serio_register_port(serport->serio); |
429b4749 | 189 | printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty)); |
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 | ||
a80d8b02 JS |
201 | static void serport_set_type(struct tty_struct *tty, unsigned long type) |
202 | { | |
203 | struct serport *serport = tty->disc_data; | |
204 | ||
205 | serport->id.proto = type & 0x000000ff; | |
206 | serport->id.id = (type & 0x0000ff00) >> 8; | |
207 | serport->id.extra = (type & 0x00ff0000) >> 16; | |
208 | } | |
209 | ||
1da177e4 LT |
210 | /* |
211 | * serport_ldisc_ioctl() allows to set the port protocol, and device ID | |
212 | */ | |
213 | ||
a80d8b02 JS |
214 | static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file, |
215 | unsigned int cmd, unsigned long arg) | |
1da177e4 | 216 | { |
1da177e4 | 217 | if (cmd == SPIOCSTYPE) { |
a80d8b02 JS |
218 | unsigned long type; |
219 | ||
1da177e4 LT |
220 | if (get_user(type, (unsigned long __user *) arg)) |
221 | return -EFAULT; | |
222 | ||
a80d8b02 JS |
223 | serport_set_type(tty, type); |
224 | return 0; | |
225 | } | |
226 | ||
227 | return -EINVAL; | |
228 | } | |
229 | ||
230 | #ifdef CONFIG_COMPAT | |
231 | #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t) | |
232 | static long serport_ldisc_compat_ioctl(struct tty_struct *tty, | |
233 | struct file *file, | |
234 | unsigned int cmd, unsigned long arg) | |
235 | { | |
236 | if (cmd == COMPAT_SPIOCSTYPE) { | |
237 | void __user *uarg = compat_ptr(arg); | |
238 | compat_ulong_t compat_type; | |
239 | ||
240 | if (get_user(compat_type, (compat_ulong_t __user *)uarg)) | |
241 | return -EFAULT; | |
1da177e4 | 242 | |
a80d8b02 | 243 | serport_set_type(tty, compat_type); |
1da177e4 LT |
244 | return 0; |
245 | } | |
246 | ||
247 | return -EINVAL; | |
248 | } | |
a80d8b02 | 249 | #endif |
1da177e4 LT |
250 | |
251 | static void serport_ldisc_write_wakeup(struct tty_struct * tty) | |
252 | { | |
1ff2c873 DT |
253 | struct serport *serport = (struct serport *) tty->disc_data; |
254 | unsigned long flags; | |
1da177e4 | 255 | |
1ff2c873 DT |
256 | spin_lock_irqsave(&serport->lock, flags); |
257 | if (test_bit(SERPORT_ACTIVE, &serport->flags)) | |
258 | serio_drv_write_wakeup(serport->serio); | |
259 | spin_unlock_irqrestore(&serport->lock, flags); | |
1da177e4 LT |
260 | } |
261 | ||
262 | /* | |
263 | * The line discipline structure. | |
264 | */ | |
265 | ||
a352def2 | 266 | static struct tty_ldisc_ops serport_ldisc = { |
1da177e4 LT |
267 | .owner = THIS_MODULE, |
268 | .name = "input", | |
269 | .open = serport_ldisc_open, | |
270 | .close = serport_ldisc_close, | |
271 | .read = serport_ldisc_read, | |
272 | .ioctl = serport_ldisc_ioctl, | |
a80d8b02 JS |
273 | #ifdef CONFIG_COMPAT |
274 | .compat_ioctl = serport_ldisc_compat_ioctl, | |
275 | #endif | |
1da177e4 | 276 | .receive_buf = serport_ldisc_receive, |
1da177e4 LT |
277 | .write_wakeup = serport_ldisc_write_wakeup |
278 | }; | |
279 | ||
280 | /* | |
281 | * The functions for insering/removing us as a module. | |
282 | */ | |
283 | ||
284 | static int __init serport_init(void) | |
285 | { | |
286 | int retval; | |
287 | retval = tty_register_ldisc(N_MOUSE, &serport_ldisc); | |
288 | if (retval) | |
289 | printk(KERN_ERR "serport.c: Error registering line discipline.\n"); | |
290 | ||
291 | return retval; | |
292 | } | |
293 | ||
294 | static void __exit serport_exit(void) | |
295 | { | |
64ccd715 | 296 | tty_unregister_ldisc(N_MOUSE); |
1da177e4 LT |
297 | } |
298 | ||
299 | module_init(serport_init); | |
300 | module_exit(serport_exit); |