blob: 1bd88fca05425a9d6ddfb9f52c36d296f832fad2 [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
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
24MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
25MODULE_DESCRIPTION("Input device TTY line discipline");
26MODULE_LICENSE("GPL");
27MODULE_ALIAS_LDISC(N_MOUSE);
28
29#define SERPORT_BUSY 1
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070030#define SERPORT_ACTIVE 2
31#define SERPORT_DEAD 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33struct serport {
34 struct tty_struct *tty;
35 wait_queue_head_t wait;
36 struct serio *serio;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070037 struct serio_device_id id;
38 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 unsigned long flags;
40};
41
42/*
43 * Callback functions from the serio code.
44 */
45
46static int serport_serio_write(struct serio *serio, unsigned char data)
47{
48 struct serport *serport = serio->port_data;
49 return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
50}
51
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070052static 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 Torvalds1da177e2005-04-16 15:20:36 -070065static void serport_serio_close(struct serio *serio)
66{
67 struct serport *serport = serio->port_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070068 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070070 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 Torvalds1da177e2005-04-16 15:20:36 -070075 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
83static int serport_ldisc_open(struct tty_struct *tty)
84{
85 struct serport *serport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (!capable(CAP_SYS_ADMIN))
88 return -EPERM;
89
Pekka Enberga97e1482005-09-06 15:18:33 -070090 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070091 if (!serport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 serport->tty = tty;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070095 spin_lock_init(&serport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 init_waitqueue_head(&serport->wait);
97
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070098 tty->disc_data = serport;
99 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
117 * are ready for us. We forward the characters, one by one to the 'interrupt'
118 * routine.
119 *
120 * FIXME: We should get pt_regs from the tty layer and forward them to
121 * serio_interrupt here.
122 */
123
124static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
125{
126 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700127 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int i;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700129
130 spin_lock_irqsave(&serport->lock, flags);
131
132 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
133 goto out;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 for (i = 0; i < count; i++)
136 serio_interrupt(serport->serio, cp[i], 0, NULL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700137
138out:
139 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/*
143 * serport_ldisc_room() reports how much room we do have for receiving data.
144 * Although we in fact have infinite room, we need to specify some value
145 * here, and 256 seems to be reasonable.
146 */
147
148static int serport_ldisc_room(struct tty_struct *tty)
149{
150 return 256;
151}
152
153/*
154 * serport_ldisc_read() just waits indefinitely if everything goes well.
155 * However, when the serio driver closes the serio port, it finishes,
156 * returning 0 characters.
157 */
158
159static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
160{
161 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700162 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 char name[64];
164
165 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
166 return -EBUSY;
167
Pekka Enberga97e1482005-09-06 15:18:33 -0700168 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700169 if (!serio)
170 return -ENOMEM;
171
172 strlcpy(serio->name, "Serial port", sizeof(serio->name));
173 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
174 serio->id = serport->id;
175 serio->id.type = SERIO_RS232;
176 serio->write = serport_serio_write;
177 serio->open = serport_serio_open;
178 serio->close = serport_serio_close;
179 serio->port_data = serport;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 serio_register_port(serport->serio);
182 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700184 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
185 serio_unregister_port(serport->serio);
186 serport->serio = NULL;
187
188 clear_bit(SERPORT_DEAD, &serport->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 clear_bit(SERPORT_BUSY, &serport->flags);
190
191 return 0;
192}
193
194/*
195 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
196 */
197
198static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
199{
200 struct serport *serport = (struct serport*) tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned long type;
202
203 if (cmd == SPIOCSTYPE) {
204 if (get_user(type, (unsigned long __user *) arg))
205 return -EFAULT;
206
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700207 serport->id.proto = type & 0x000000ff;
208 serport->id.id = (type & 0x0000ff00) >> 8;
209 serport->id.extra = (type & 0x00ff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return 0;
212 }
213
214 return -EINVAL;
215}
216
217static void serport_ldisc_write_wakeup(struct tty_struct * tty)
218{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700219 struct serport *serport = (struct serport *) tty->disc_data;
220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700222 spin_lock_irqsave(&serport->lock, flags);
223 if (test_bit(SERPORT_ACTIVE, &serport->flags))
224 serio_drv_write_wakeup(serport->serio);
225 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/*
229 * The line discipline structure.
230 */
231
232static struct tty_ldisc serport_ldisc = {
233 .owner = THIS_MODULE,
234 .name = "input",
235 .open = serport_ldisc_open,
236 .close = serport_ldisc_close,
237 .read = serport_ldisc_read,
238 .ioctl = serport_ldisc_ioctl,
239 .receive_buf = serport_ldisc_receive,
240 .receive_room = serport_ldisc_room,
241 .write_wakeup = serport_ldisc_write_wakeup
242};
243
244/*
245 * The functions for insering/removing us as a module.
246 */
247
248static int __init serport_init(void)
249{
250 int retval;
251 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
252 if (retval)
253 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
254
255 return retval;
256}
257
258static void __exit serport_exit(void)
259{
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700260 tty_unregister_ldisc(N_MOUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263module_init(serport_init);
264module_exit(serport_exit);