summaryrefslogtreecommitdiffstats
path: root/drivers/char/joystick/serio.c
blob: 9595f0a6cac2420eb21f59499944df8761c8740d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * $Id: serio.c,v 1.5 2000/06/04 17:44:59 vojtech Exp $
 *
 *  Copyright (c) 1999-2000 Vojtech Pavlik
 *
 *  Sponsored by SuSE
 */

/*
 *  The Serio abstraction module
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
 */

#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/serio.h>

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");

EXPORT_SYMBOL(serio_register_port);
EXPORT_SYMBOL(serio_unregister_port);
EXPORT_SYMBOL(serio_register_device);
EXPORT_SYMBOL(serio_unregister_device);
EXPORT_SYMBOL(serio_open);
EXPORT_SYMBOL(serio_close);
EXPORT_SYMBOL(serio_rescan);

static struct serio *serio_list = NULL;
static struct serio_dev *serio_dev = NULL;
static int serio_number = 0;

static void serio_find_dev(struct serio *serio)
{
        struct serio_dev *dev = serio_dev;

        while (dev && !serio->dev) {
		if (dev->connect)
                	dev->connect(serio, dev);
                dev = dev->next;
        }
}

void serio_rescan(struct serio *serio)
{
	if (serio->dev && serio->dev->disconnect)
		serio->dev->disconnect(serio);
	serio_find_dev(serio);
}

void serio_register_port(struct serio *serio)
{
	serio->number = serio_number++;
	serio->next = serio_list;	
	serio_list = serio;
	serio_find_dev(serio);
}

void serio_unregister_port(struct serio *serio)
{
        struct serio **serioptr = &serio_list;

        while (*serioptr && (*serioptr != serio)) serioptr = &((*serioptr)->next);
        *serioptr = (*serioptr)->next;

	if (serio->dev && serio->dev->disconnect)
		serio->dev->disconnect(serio);

	serio_number--;
}

void serio_register_device(struct serio_dev *dev)
{
	struct serio *serio = serio_list;

	dev->next = serio_dev;	
	serio_dev = dev;

	while (serio) {
		if (!serio->dev && dev->connect)
			dev->connect(serio, dev);
		serio = serio->next;
	}
}

void serio_unregister_device(struct serio_dev *dev)
{
        struct serio_dev **devptr = &serio_dev;
	struct serio *serio = serio_list;

        while (*devptr && (*devptr != dev)) devptr = &((*devptr)->next);
        *devptr = (*devptr)->next;

	while (serio) {
		if (serio->dev == dev && dev->disconnect)
			dev->disconnect(serio);
		serio_find_dev(serio);
		serio = serio->next;
	}
}

int serio_open(struct serio *serio, struct serio_dev *dev)
{
	if (serio->open(serio))
		return -1;
	serio->dev = dev;
	return 0;
}

void serio_close(struct serio *serio)
{
	serio->close(serio);
	serio->dev = NULL;
}