2 * $Id: serport.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
10 * This is a module that converts a tty line into a much simpler
11 * 'serial io port' abstraction that the input device drivers use.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Should you need to contact me, the author, you can do so either by
30 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
34 #include <asm/uaccess.h>
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/serio.h>
40 #include <linux/tty.h>
43 struct tty_struct *tty;
44 wait_queue_head_t wait;
49 * Callback functions from the serio code.
52 static int serport_serio_write(struct serio *serio, unsigned char data)
54 struct serport *serport = serio->driver;
55 return -(serport->tty->driver.write(serport->tty, 0, &data, 1) != 1);
58 static int serport_serio_open(struct serio *serio)
63 static void serport_serio_close(struct serio *serio)
65 struct serport *serport = serio->driver;
66 wake_up_interruptible(&serport->wait);
70 * serport_ldisc_open() is the routine that is called upon setting our line
71 * discipline on a tty. It looks for the Mag, and if found, registers
72 * it as a joystick device.
75 static int serport_ldisc_open(struct tty_struct *tty)
77 struct serport *serport;
81 if (!(serport = kmalloc(sizeof(struct serport), GFP_KERNEL))) {
86 memset(serport, 0, sizeof(struct serport));
89 tty->disc_data = serport;
91 serport->serio.type = SERIO_RS232;
92 serport->serio.write = serport_serio_write;
93 serport->serio.open = serport_serio_open;
94 serport->serio.close = serport_serio_close;
95 serport->serio.driver = serport;
97 init_waitqueue_head(&serport->wait);
103 * serport_ldisc_close() is the opposite of serport_ldisc_open()
106 static void serport_ldisc_close(struct tty_struct *tty)
108 struct serport *serport = (struct serport*) tty->disc_data;
114 * serport_ldisc_receive() is called by the low level tty driver when characters
115 * are ready for us. We forward the characters, one by one to the 'interrupt'
119 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
121 struct serport *serport = (struct serport*) tty->disc_data;
123 for (i = 0; i < count; i++)
124 if (serport->serio.dev)
125 serport->serio.dev->interrupt(&serport->serio, cp[i], 0);
129 * serport_ldisc_room() reports how much room we do have for receiving data.
130 * Although we in fact have infinite room, we need to specify some value
131 * here, and 256 seems to be reasonable.
134 static int serport_ldisc_room(struct tty_struct *tty)
140 * serport_ldisc_read() just waits indefinitely if everything goes well.
141 * However, when the serio driver closes the serio port, it finishes,
142 * returning 0 characters.
145 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char * buf, size_t nr)
147 struct serport *serport = (struct serport*) tty->disc_data;
148 DECLARE_WAITQUEUE(wait, current);
151 #ifdef CONFIG_DEVFS_FS
152 sprintf(name, tty->driver.name, MINOR(tty->device) - tty->driver.minor_start);
154 sprintf(name, "%s%d", tty->driver.name, MINOR(tty->device) - tty->driver.minor_start);
157 serio_register_port(&serport->serio);
159 printk(KERN_INFO "serio%d: Serial port %s\n", serport->serio.number, name);
161 add_wait_queue(&serport->wait, &wait);
162 current->state = TASK_INTERRUPTIBLE;
164 while(serport->serio.type && !signal_pending(current)) schedule();
166 current->state = TASK_RUNNING;
167 remove_wait_queue(&serport->wait, &wait);
169 serio_unregister_port(&serport->serio);
175 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
178 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
180 struct serport *serport = (struct serport*) tty->disc_data;
184 return get_user(serport->serio.type, (unsigned long *) arg);
191 * The line discipline structure.
194 static struct tty_ldisc serport_ldisc = {
196 open: serport_ldisc_open,
197 close: serport_ldisc_close,
198 read: serport_ldisc_read,
199 ioctl: serport_ldisc_ioctl,
200 receive_buf: serport_ldisc_receive,
201 receive_room: serport_ldisc_room,
205 * The functions for insering/removing us as a module.
208 int __init serport_init(void)
210 if (tty_register_ldisc(N_MOUSE, &serport_ldisc)) {
211 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
218 void __exit serport_exit(void)
220 tty_register_ldisc(N_MOUSE, NULL);
223 module_init(serport_init);
224 module_exit(serport_exit);
226 MODULE_LICENSE("GPL");