Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / joystick / serport.c
blob453e674d70326d3eecbd3fb38045c7a22c6cb221
1 /*
2 * $Id: serport.c,v 1.4 2000/05/29 10:54:53 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Sponsored by SuSE
7 */
9 /*
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/malloc.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/serio.h>
40 #include <linux/tty.h>
42 struct serport {
43 struct tty_struct *tty;
44 wait_queue_head_t wait;
45 struct serio serio;
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)
60 return 0;
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;
79 MOD_INC_USE_COUNT;
81 if (!(serport = kmalloc(sizeof(struct serport), GFP_KERNEL))) {
82 MOD_DEC_USE_COUNT;
83 return -ENOMEM;
86 memset(serport, 0, sizeof(struct serport));
88 serport->tty = tty;
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);
99 return 0;
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;
109 kfree(serport);
110 MOD_DEC_USE_COUNT;
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'
116 * routine.
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;
122 int i;
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)
136 return 256;
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);
149 char name[32];
151 sprintf(name, tty->driver.name, MINOR(tty->device) - tty->driver.minor_start);
153 serio_register_port(&serport->serio);
155 printk(KERN_INFO "serio%d: Serial port %s\n", serport->serio.number, name);
157 add_wait_queue(&serport->wait, &wait);
158 current->state = TASK_INTERRUPTIBLE;
160 while(serport->serio.type && !signal_pending(current)) schedule();
162 current->state = TASK_RUNNING;
163 remove_wait_queue(&serport->wait, &wait);
165 serio_unregister_port(&serport->serio);
167 return 0;
171 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
174 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
176 struct serport *serport = (struct serport*) tty->disc_data;
178 switch (cmd) {
179 case SPIOCSTYPE:
180 return get_user(serport->serio.type, (unsigned long *) arg);
183 return -EINVAL;
187 * The line discipline structure.
190 static struct tty_ldisc serport_ldisc = {
191 name: "input",
192 open: serport_ldisc_open,
193 close: serport_ldisc_close,
194 read: serport_ldisc_read,
195 ioctl: serport_ldisc_ioctl,
196 receive_buf: serport_ldisc_receive,
197 receive_room: serport_ldisc_room,
201 * The functions for insering/removing us as a module.
204 int __init serport_init(void)
206 if (tty_register_ldisc(N_MOUSE, &serport_ldisc)) {
207 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
208 return -ENODEV;
211 return 0;
214 void __exit serport_exit(void)
216 tty_register_ldisc(N_MOUSE, NULL);
219 module_init(serport_init);
220 module_exit(serport_exit);