2 * Input device TTY line discipline
4 * Copyright (c) 1999-2002 Vojtech Pavlik
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.
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.
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>
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
);
30 #define SERPORT_BUSY 1
31 #define SERPORT_ACTIVE 2
32 #define SERPORT_DEAD 3
35 struct tty_struct
*tty
;
36 wait_queue_head_t wait
;
38 struct serio_device_id id
;
44 * Callback functions from the serio code.
47 static int serport_serio_write(struct serio
*serio
, unsigned char data
)
49 struct serport
*serport
= serio
->port_data
;
50 return -(serport
->tty
->ops
->write(serport
->tty
, &data
, 1) != 1);
53 static int serport_serio_open(struct serio
*serio
)
55 struct serport
*serport
= serio
->port_data
;
58 spin_lock_irqsave(&serport
->lock
, flags
);
59 set_bit(SERPORT_ACTIVE
, &serport
->flags
);
60 spin_unlock_irqrestore(&serport
->lock
, flags
);
66 static void serport_serio_close(struct serio
*serio
)
68 struct serport
*serport
= serio
->port_data
;
71 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
);
76 wake_up_interruptible(&serport
->wait
);
80 * serport_ldisc_open() is the routine that is called upon setting our line
81 * discipline on a tty. It prepares the serio struct.
84 static int serport_ldisc_open(struct tty_struct
*tty
)
86 struct serport
*serport
;
88 if (!capable(CAP_SYS_ADMIN
))
91 serport
= kzalloc(sizeof(struct serport
), GFP_KERNEL
);
96 spin_lock_init(&serport
->lock
);
97 init_waitqueue_head(&serport
->wait
);
99 tty
->disc_data
= serport
;
100 tty
->receive_room
= 256;
101 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
107 * serport_ldisc_close() is the opposite of serport_ldisc_open()
110 static void serport_ldisc_close(struct tty_struct
*tty
)
112 struct serport
*serport
= (struct serport
*) tty
->disc_data
;
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'
123 static void serport_ldisc_receive(struct tty_struct
*tty
, const unsigned char *cp
, char *fp
, int count
)
125 struct serport
*serport
= (struct serport
*) tty
->disc_data
;
129 spin_lock_irqsave(&serport
->lock
, flags
);
131 if (!test_bit(SERPORT_ACTIVE
, &serport
->flags
))
134 for (i
= 0; i
< count
; i
++)
135 serio_interrupt(serport
->serio
, cp
[i
], 0);
138 spin_unlock_irqrestore(&serport
->lock
, flags
);
142 * 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.
147 static ssize_t
serport_ldisc_read(struct tty_struct
* tty
, struct file
* file
, unsigned char __user
* buf
, size_t nr
)
149 struct serport
*serport
= (struct serport
*) tty
->disc_data
;
153 if (test_and_set_bit(SERPORT_BUSY
, &serport
->flags
))
156 serport
->serio
= serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
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
;
168 serio
->dev
.parent
= tty
->dev
;
170 serio_register_port(serport
->serio
);
171 printk(KERN_INFO
"serio: Serial port %s\n", tty_name(tty
, name
));
173 wait_event_interruptible(serport
->wait
, test_bit(SERPORT_DEAD
, &serport
->flags
));
174 serio_unregister_port(serport
->serio
);
175 serport
->serio
= NULL
;
177 clear_bit(SERPORT_DEAD
, &serport
->flags
);
178 clear_bit(SERPORT_BUSY
, &serport
->flags
);
184 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
187 static int serport_ldisc_ioctl(struct tty_struct
* tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
189 struct serport
*serport
= (struct serport
*) tty
->disc_data
;
192 if (cmd
== SPIOCSTYPE
) {
193 if (get_user(type
, (unsigned long __user
*) arg
))
196 serport
->id
.proto
= type
& 0x000000ff;
197 serport
->id
.id
= (type
& 0x0000ff00) >> 8;
198 serport
->id
.extra
= (type
& 0x00ff0000) >> 16;
206 static void serport_ldisc_write_wakeup(struct tty_struct
* tty
)
208 struct serport
*serport
= (struct serport
*) tty
->disc_data
;
211 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
);
218 * The line discipline structure.
221 static struct tty_ldisc_ops serport_ldisc
= {
222 .owner
= THIS_MODULE
,
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
,
229 .write_wakeup
= serport_ldisc_write_wakeup
233 * The functions for insering/removing us as a module.
236 static int __init
serport_init(void)
239 retval
= tty_register_ldisc(N_MOUSE
, &serport_ldisc
);
241 printk(KERN_ERR
"serport.c: Error registering line discipline.\n");
246 static void __exit
serport_exit(void)
248 tty_unregister_ldisc(N_MOUSE
);
251 module_init(serport_init
);
252 module_exit(serport_exit
);