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