blob: f8ead9f9c77eaa7737c1cf94cc1a5dc23051a858 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 Sunga80d8b02014-09-09 10:06:51 -070024#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
27MODULE_DESCRIPTION("Input device TTY line discipline");
28MODULE_LICENSE("GPL");
29MODULE_ALIAS_LDISC(N_MOUSE);
30
31#define SERPORT_BUSY 1
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070032#define SERPORT_ACTIVE 2
33#define SERPORT_DEAD 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35struct serport {
36 struct tty_struct *tty;
37 wait_queue_head_t wait;
38 struct serio *serio;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070039 struct serio_device_id id;
40 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 unsigned long flags;
42};
43
44/*
45 * Callback functions from the serio code.
46 */
47
48static int serport_serio_write(struct serio *serio, unsigned char data)
49{
50 struct serport *serport = serio->port_data;
Alan Coxf34d7a52008-04-30 00:54:13 -070051 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070054static 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 Torvalds1da177e2005-04-16 15:20:36 -070067static void serport_serio_close(struct serio *serio)
68{
69 struct serport *serport = serio->port_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070070 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070072 spin_lock_irqsave(&serport->lock, flags);
73 clear_bit(SERPORT_ACTIVE, &serport->flags);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070074 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77/*
78 * serport_ldisc_open() is the routine that is called upon setting our line
79 * discipline on a tty. It prepares the serio struct.
80 */
81
82static int serport_ldisc_open(struct tty_struct *tty)
83{
84 struct serport *serport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (!capable(CAP_SYS_ADMIN))
87 return -EPERM;
88
Pekka Enberga97e1482005-09-06 15:18:33 -070089 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070090 if (!serport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 serport->tty = tty;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070094 spin_lock_init(&serport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 init_waitqueue_head(&serport->wait);
96
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070097 tty->disc_data = serport;
Alan Cox33f0f882006-01-09 20:54:13 -080098 tty->receive_room = 256;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070099 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
102}
103
104/*
105 * serport_ldisc_close() is the opposite of serport_ldisc_open()
106 */
107
108static void serport_ldisc_close(struct tty_struct *tty)
109{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700110 struct serport *serport = (struct serport *) tty->disc_data;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 kfree(serport);
113}
114
115/*
116 * serport_ldisc_receive() is called by the low level tty driver when characters
David Engraf48c27012011-01-20 23:05:17 -0800117 * are ready for us. We forward the characters and flags, one by one to the
118 * 'interrupt' routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
120
Linus Torvalds55db4c62011-06-04 06:33:24 +0900121static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700124 unsigned long flags;
Peter Hurley82f91fe2013-12-02 13:56:03 -0500125 unsigned int ch_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int i;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700127
128 spin_lock_irqsave(&serport->lock, flags);
129
Linus Torvalds55db4c62011-06-04 06:33:24 +0900130 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700131 goto out;
132
David Engraf48c27012011-01-20 23:05:17 -0800133 for (i = 0; i < count; i++) {
Peter Hurley82f91fe2013-12-02 13:56:03 -0500134 if (fp) {
135 switch (fp[i]) {
136 case TTY_FRAME:
137 ch_flags = SERIO_FRAME;
138 break;
David Engraf48c27012011-01-20 23:05:17 -0800139
Peter Hurley82f91fe2013-12-02 13:56:03 -0500140 case TTY_PARITY:
141 ch_flags = SERIO_PARITY;
142 break;
David Engraf48c27012011-01-20 23:05:17 -0800143
Peter Hurley82f91fe2013-12-02 13:56:03 -0500144 default:
145 ch_flags = 0;
146 break;
147 }
David Engraf48c27012011-01-20 23:05:17 -0800148 }
149
150 serio_interrupt(serport->serio, cp[i], ch_flags);
151 }
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700152
153out:
154 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * serport_ldisc_read() just waits indefinitely if everything goes well.
159 * However, when the serio driver closes the serio port, it finishes,
160 * returning 0 characters.
161 */
162
163static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
164{
165 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700166 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
169 return -EBUSY;
170
Pekka Enberga97e1482005-09-06 15:18:33 -0700171 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700172 if (!serio)
173 return -ENOMEM;
174
175 strlcpy(serio->name, "Serial port", sizeof(serio->name));
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200176 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700177 serio->id = serport->id;
178 serio->id.type = SERIO_RS232;
179 serio->write = serport_serio_write;
180 serio->open = serport_serio_open;
181 serio->close = serport_serio_close;
182 serio->port_data = serport;
Dmitry Eremin-Solenikovde838a92010-08-09 18:22:50 +0400183 serio->dev.parent = tty->dev;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 serio_register_port(serport->serio);
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200186 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700188 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
189 serio_unregister_port(serport->serio);
190 serport->serio = NULL;
191
192 clear_bit(SERPORT_DEAD, &serport->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 clear_bit(SERPORT_BUSY, &serport->flags);
194
195 return 0;
196}
197
John Sunga80d8b02014-09-09 10:06:51 -0700198static void serport_set_type(struct tty_struct *tty, unsigned long type)
199{
200 struct serport *serport = tty->disc_data;
201
202 serport->id.proto = type & 0x000000ff;
203 serport->id.id = (type & 0x0000ff00) >> 8;
204 serport->id.extra = (type & 0x00ff0000) >> 16;
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
209 */
210
John Sunga80d8b02014-09-09 10:06:51 -0700211static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
212 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (cmd == SPIOCSTYPE) {
John Sunga80d8b02014-09-09 10:06:51 -0700215 unsigned long type;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (get_user(type, (unsigned long __user *) arg))
218 return -EFAULT;
219
John Sunga80d8b02014-09-09 10:06:51 -0700220 serport_set_type(tty, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222 }
223
224 return -EINVAL;
225}
226
John Sunga80d8b02014-09-09 10:06:51 -0700227#ifdef CONFIG_COMPAT
228#define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
229static long serport_ldisc_compat_ioctl(struct tty_struct *tty,
230 struct file *file,
231 unsigned int cmd, unsigned long arg)
232{
233 if (cmd == COMPAT_SPIOCSTYPE) {
234 void __user *uarg = compat_ptr(arg);
235 compat_ulong_t compat_type;
236
237 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
238 return -EFAULT;
239
240 serport_set_type(tty, compat_type);
241 return 0;
242 }
243
244 return -EINVAL;
245}
246#endif
247
Hans Verkuil51db0132016-08-04 11:16:23 -0700248static int serport_ldisc_hangup(struct tty_struct *tty)
249{
250 struct serport *serport = (struct serport *) tty->disc_data;
251 unsigned long flags;
252
253 spin_lock_irqsave(&serport->lock, flags);
254 set_bit(SERPORT_DEAD, &serport->flags);
255 spin_unlock_irqrestore(&serport->lock, flags);
256
257 wake_up_interruptible(&serport->wait);
258 return 0;
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static void serport_ldisc_write_wakeup(struct tty_struct * tty)
262{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700263 struct serport *serport = (struct serport *) tty->disc_data;
264 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700266 spin_lock_irqsave(&serport->lock, flags);
267 if (test_bit(SERPORT_ACTIVE, &serport->flags))
268 serio_drv_write_wakeup(serport->serio);
269 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * The line discipline structure.
274 */
275
Alan Coxa352def2008-07-16 21:53:12 +0100276static struct tty_ldisc_ops serport_ldisc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .owner = THIS_MODULE,
278 .name = "input",
279 .open = serport_ldisc_open,
280 .close = serport_ldisc_close,
281 .read = serport_ldisc_read,
282 .ioctl = serport_ldisc_ioctl,
John Sunga80d8b02014-09-09 10:06:51 -0700283#ifdef CONFIG_COMPAT
284 .compat_ioctl = serport_ldisc_compat_ioctl,
285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .receive_buf = serport_ldisc_receive,
Hans Verkuil51db0132016-08-04 11:16:23 -0700287 .hangup = serport_ldisc_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .write_wakeup = serport_ldisc_write_wakeup
289};
290
291/*
292 * The functions for insering/removing us as a module.
293 */
294
295static int __init serport_init(void)
296{
297 int retval;
298 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
299 if (retval)
300 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
301
302 return retval;
303}
304
305static void __exit serport_exit(void)
306{
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700307 tty_unregister_ldisc(N_MOUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310module_init(serport_init);
311module_exit(serport_exit);