blob: 6e362de3f412fe32a5755bdd49aed8bd891fecb5 [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>
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>
24
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26MODULE_DESCRIPTION("Input device TTY line discipline");
27MODULE_LICENSE("GPL");
28MODULE_ALIAS_LDISC(N_MOUSE);
29
30#define SERPORT_BUSY 1
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070031#define SERPORT_ACTIVE 2
32#define SERPORT_DEAD 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct serport {
35 struct tty_struct *tty;
36 wait_queue_head_t wait;
37 struct serio *serio;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070038 struct serio_device_id id;
39 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unsigned long flags;
41};
42
43/*
44 * Callback functions from the serio code.
45 */
46
47static int serport_serio_write(struct serio *serio, unsigned char data)
48{
49 struct serport *serport = serio->port_data;
Alan Coxf34d7a52008-04-30 00:54:13 -070050 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070053static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void serport_serio_close(struct serio *serio)
67{
68 struct serport *serport = serio->port_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070069 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070071 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);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 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
84static int serport_ldisc_open(struct tty_struct *tty)
85{
86 struct serport *serport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (!capable(CAP_SYS_ADMIN))
89 return -EPERM;
90
Pekka Enberga97e1482005-09-06 15:18:33 -070091 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070092 if (!serport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 serport->tty = tty;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070096 spin_lock_init(&serport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 init_waitqueue_head(&serport->wait);
98
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -070099 tty->disc_data = serport;
Alan Cox33f0f882006-01-09 20:54:13 -0800100 tty->receive_room = 256;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700101 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
106/*
107 * serport_ldisc_close() is the opposite of serport_ldisc_open()
108 */
109
110static void serport_ldisc_close(struct tty_struct *tty)
111{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700112 struct serport *serport = (struct serport *) tty->disc_data;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 kfree(serport);
115}
116
117/*
118 * serport_ldisc_receive() is called by the low level tty driver when characters
119 * are ready for us. We forward the characters, one by one to the 'interrupt'
120 * routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
123static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
124{
125 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700126 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int i;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700128
129 spin_lock_irqsave(&serport->lock, flags);
130
131 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
132 goto out;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 for (i = 0; i < count; i++)
David Howells7d12e782006-10-05 14:55:46 +0100135 serio_interrupt(serport->serio, cp[i], 0);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700136
137out:
138 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * serport_ldisc_read() just waits indefinitely if everything goes well.
143 * However, when the serio driver closes the serio port, it finishes,
144 * returning 0 characters.
145 */
146
147static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
148{
149 struct serport *serport = (struct serport*) tty->disc_data;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700150 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 char name[64];
152
153 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
154 return -EBUSY;
155
Pekka Enberga97e1482005-09-06 15:18:33 -0700156 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700157 if (!serio)
158 return -ENOMEM;
159
160 strlcpy(serio->name, "Serial port", sizeof(serio->name));
161 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
162 serio->id = serport->id;
163 serio->id.type = SERIO_RS232;
164 serio->write = serport_serio_write;
165 serio->open = serport_serio_open;
166 serio->close = serport_serio_close;
167 serio->port_data = serport;
Dmitry Eremin-Solenikovde838a92010-08-09 18:22:50 +0400168 serio->dev.parent = tty->dev;
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 serio_register_port(serport->serio);
171 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700173 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
174 serio_unregister_port(serport->serio);
175 serport->serio = NULL;
176
177 clear_bit(SERPORT_DEAD, &serport->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 clear_bit(SERPORT_BUSY, &serport->flags);
179
180 return 0;
181}
182
183/*
184 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
185 */
186
187static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
188{
189 struct serport *serport = (struct serport*) tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned long type;
191
192 if (cmd == SPIOCSTYPE) {
193 if (get_user(type, (unsigned long __user *) arg))
194 return -EFAULT;
195
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700196 serport->id.proto = type & 0x000000ff;
197 serport->id.id = (type & 0x0000ff00) >> 8;
198 serport->id.extra = (type & 0x00ff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return 0;
201 }
202
203 return -EINVAL;
204}
205
206static void serport_ldisc_write_wakeup(struct tty_struct * tty)
207{
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700208 struct serport *serport = (struct serport *) tty->disc_data;
209 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dmitry Torokhov1ff2c872005-05-16 21:53:07 -0700211 spin_lock_irqsave(&serport->lock, flags);
212 if (test_bit(SERPORT_ACTIVE, &serport->flags))
213 serio_drv_write_wakeup(serport->serio);
214 spin_unlock_irqrestore(&serport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * The line discipline structure.
219 */
220
Alan Coxa352def2008-07-16 21:53:12 +0100221static struct tty_ldisc_ops serport_ldisc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .owner = THIS_MODULE,
223 .name = "input",
224 .open = serport_ldisc_open,
225 .close = serport_ldisc_close,
226 .read = serport_ldisc_read,
227 .ioctl = serport_ldisc_ioctl,
228 .receive_buf = serport_ldisc_receive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .write_wakeup = serport_ldisc_write_wakeup
230};
231
232/*
233 * The functions for insering/removing us as a module.
234 */
235
236static int __init serport_init(void)
237{
238 int retval;
239 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
240 if (retval)
241 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
242
243 return retval;
244}
245
246static void __exit serport_exit(void)
247{
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700248 tty_unregister_ldisc(N_MOUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251module_init(serport_init);
252module_exit(serport_exit);