Rev 494 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
494 | giacomo | 1 | /* |
523 | mauro | 2 | * $Id: serio.c,v 1.2 2004-03-25 10:37:49 mauro Exp $ |
494 | giacomo | 3 | * |
4 | * Copyright (c) 1999-2001 Vojtech Pavlik |
||
5 | */ |
||
6 | |||
7 | /* |
||
8 | * The Serio abstraction module |
||
9 | */ |
||
10 | |||
11 | /* |
||
12 | * This program is free software; you can redistribute it and/or modify |
||
13 | * it under the terms of the GNU General Public License as published by |
||
14 | * the Free Software Foundation; either version 2 of the License, or |
||
15 | * (at your option) any later version. |
||
16 | * |
||
17 | * This program is distributed in the hope that it will be useful, |
||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
20 | * GNU General Public License for more details. |
||
21 | * |
||
22 | * You should have received a copy of the GNU General Public License |
||
23 | * along with this program; if not, write to the Free Software |
||
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
25 | * |
||
26 | * Should you need to contact me, the author, you can do so either by |
||
27 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
||
28 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
||
29 | * |
||
30 | * Changes: |
||
31 | * 20 Jul. 2003 Daniele Bellucci <bellucda@tiscali.it> |
||
32 | * Minor cleanups. |
||
33 | */ |
||
34 | |||
35 | #include <linuxcomp.h> |
||
36 | |||
37 | #include <linux/stddef.h> |
||
38 | #include <linux/module.h> |
||
39 | #include <linux/serio.h> |
||
40 | #include <linux/errno.h> |
||
41 | #include <linux/wait.h> |
||
42 | #include <linux/completion.h> |
||
43 | #include <linux/sched.h> |
||
44 | #include <linux/smp_lock.h> |
||
45 | #include <linux/suspend.h> |
||
46 | #include <linux/slab.h> |
||
47 | |||
48 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
||
49 | MODULE_DESCRIPTION("Serio abstraction core"); |
||
50 | MODULE_LICENSE("GPL"); |
||
51 | |||
52 | EXPORT_SYMBOL(serio_interrupt); |
||
53 | EXPORT_SYMBOL(serio_register_port); |
||
54 | EXPORT_SYMBOL(serio_register_slave_port); |
||
55 | EXPORT_SYMBOL(serio_unregister_port); |
||
56 | EXPORT_SYMBOL(serio_unregister_slave_port); |
||
57 | EXPORT_SYMBOL(serio_register_device); |
||
58 | EXPORT_SYMBOL(serio_unregister_device); |
||
59 | EXPORT_SYMBOL(serio_open); |
||
60 | EXPORT_SYMBOL(serio_close); |
||
61 | EXPORT_SYMBOL(serio_rescan); |
||
62 | |||
63 | struct serio_event { |
||
64 | int type; |
||
65 | struct serio *serio; |
||
66 | struct list_head node; |
||
67 | }; |
||
68 | |||
69 | static DECLARE_MUTEX(serio_sem); |
||
70 | static LIST_HEAD(serio_list); |
||
71 | static LIST_HEAD(serio_dev_list); |
||
72 | static LIST_HEAD(serio_event_list); |
||
73 | static int serio_pid; |
||
74 | |||
75 | static void serio_find_dev(struct serio *serio) |
||
76 | { |
||
77 | struct serio_dev *dev; |
||
78 | |||
79 | list_for_each_entry(dev, &serio_dev_list, node) { |
||
80 | if (serio->dev) |
||
81 | break; |
||
82 | if (dev->connect) |
||
83 | dev->connect(serio, dev); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | #define SERIO_RESCAN 1 |
||
88 | |||
89 | static DECLARE_WAIT_QUEUE_HEAD(serio_wait); |
||
90 | static DECLARE_COMPLETION(serio_exited); |
||
91 | |||
92 | void serio_handle_events(void) |
||
93 | { |
||
94 | struct list_head *node, *next; |
||
95 | struct serio_event *event; |
||
96 | |||
97 | list_for_each_safe(node, next, &serio_event_list) { |
||
98 | event = container_of(node, struct serio_event, node); |
||
99 | |||
100 | switch (event->type) { |
||
101 | case SERIO_RESCAN : |
||
102 | //!!!down(&serio_sem); |
||
103 | if (event->serio->dev && event->serio->dev->disconnect) |
||
104 | event->serio->dev->disconnect(event->serio); |
||
105 | serio_find_dev(event->serio); |
||
106 | //!!!up(&serio_sem); |
||
107 | break; |
||
108 | default: |
||
109 | break; |
||
110 | } |
||
111 | list_del_init(node); |
||
112 | kfree(event); |
||
113 | } |
||
114 | } |
||
115 | |||
523 | mauro | 116 | /* |
494 | giacomo | 117 | static int serio_thread(void *nothing) |
118 | { |
||
523 | mauro | 119 | lock_kernel(); |
494 | giacomo | 120 | daemonize("kseriod"); |
121 | allow_signal(SIGTERM); |
||
122 | |||
123 | do { |
||
124 | serio_handle_events(); |
||
125 | wait_event_interruptible(serio_wait, !list_empty(&serio_event_list)); |
||
126 | if (current->flags & PF_FREEZE) |
||
127 | refrigerator(PF_IOTHREAD); |
||
128 | } while (!signal_pending(current)); |
||
129 | |||
130 | printk(KERN_DEBUG "serio: kseriod exiting\n"); |
||
131 | |||
132 | unlock_kernel(); |
||
523 | mauro | 133 | complete_and_exit(&serio_exited, 0); |
494 | giacomo | 134 | return 1; |
135 | } |
||
523 | mauro | 136 | */ |
494 | giacomo | 137 | |
138 | void serio_rescan(struct serio *serio) |
||
139 | { |
||
140 | struct serio_event *event; |
||
141 | |||
142 | if (!(event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) |
||
143 | return; |
||
144 | |||
145 | event->type = SERIO_RESCAN; |
||
146 | event->serio = serio; |
||
147 | |||
148 | list_add_tail(&event->node, &serio_event_list); |
||
149 | //!!!wake_up(&serio_wait); |
||
150 | } |
||
151 | |||
152 | irqreturn_t serio_interrupt(struct serio *serio, |
||
153 | unsigned char data, unsigned int flags, struct pt_regs *regs) |
||
154 | { |
||
155 | irqreturn_t ret = IRQ_NONE; |
||
156 | |||
157 | if (serio->dev && serio->dev->interrupt) { |
||
158 | ret = serio->dev->interrupt(serio, data, flags, regs); |
||
159 | } else { |
||
160 | if (!flags) { |
||
161 | serio_rescan(serio); |
||
162 | ret = IRQ_HANDLED; |
||
163 | } |
||
164 | } |
||
165 | return ret; |
||
166 | } |
||
167 | |||
168 | void serio_register_port(struct serio *serio) |
||
169 | { |
||
170 | //!!!down(&serio_sem); |
||
171 | list_add_tail(&serio->node, &serio_list); |
||
172 | serio_find_dev(serio); |
||
173 | //!!!up(&serio_sem); |
||
174 | } |
||
175 | |||
176 | /* |
||
177 | * Same as serio_register_port but does not try to acquire serio_sem. |
||
178 | * Should be used when registering a serio from other input device's |
||
179 | * connect() function. |
||
180 | */ |
||
181 | void serio_register_slave_port(struct serio *serio) |
||
182 | { |
||
183 | list_add_tail(&serio->node, &serio_list); |
||
184 | serio_find_dev(serio); |
||
185 | } |
||
186 | |||
187 | void serio_unregister_port(struct serio *serio) |
||
188 | { |
||
189 | //!!!down(&serio_sem); |
||
190 | list_del_init(&serio->node); |
||
191 | if (serio->dev && serio->dev->disconnect) |
||
192 | serio->dev->disconnect(serio); |
||
193 | //!!!up(&serio_sem); |
||
194 | } |
||
195 | |||
196 | /* |
||
197 | * Same as serio_unregister_port but does not try to acquire serio_sem. |
||
198 | * Should be used when unregistering a serio from other input device's |
||
199 | * disconnect() function. |
||
200 | */ |
||
201 | void serio_unregister_slave_port(struct serio *serio) |
||
202 | { |
||
203 | list_del_init(&serio->node); |
||
204 | if (serio->dev && serio->dev->disconnect) |
||
205 | serio->dev->disconnect(serio); |
||
206 | } |
||
207 | |||
208 | void serio_register_device(struct serio_dev *dev) |
||
209 | { |
||
210 | struct serio *serio; |
||
211 | //!!!down(&serio_sem); |
||
212 | list_add_tail(&dev->node, &serio_dev_list); |
||
213 | list_for_each_entry(serio, &serio_list, node) |
||
214 | if (!serio->dev && dev->connect) |
||
215 | dev->connect(serio, dev); |
||
216 | //!!!up(&serio_sem); |
||
217 | } |
||
218 | |||
219 | void serio_unregister_device(struct serio_dev *dev) |
||
220 | { |
||
221 | struct serio *serio; |
||
222 | |||
223 | //!!!down(&serio_sem); |
||
224 | list_del_init(&dev->node); |
||
225 | |||
226 | list_for_each_entry(serio, &serio_list, node) { |
||
227 | if (serio->dev == dev && dev->disconnect) |
||
228 | dev->disconnect(serio); |
||
229 | serio_find_dev(serio); |
||
230 | } |
||
231 | //!!!up(&serio_sem); |
||
232 | } |
||
233 | |||
234 | /* called from serio_dev->connect/disconnect methods under serio_sem */ |
||
235 | int serio_open(struct serio *serio, struct serio_dev *dev) |
||
236 | { |
||
237 | serio->dev = dev; |
||
238 | if (serio->open(serio)) { |
||
239 | serio->dev = NULL; |
||
240 | return -1; |
||
241 | } |
||
242 | return 0; |
||
243 | } |
||
244 | |||
245 | /* called from serio_dev->connect/disconnect methods under serio_sem */ |
||
246 | void serio_close(struct serio *serio) |
||
247 | { |
||
248 | serio->close(serio); |
||
249 | serio->dev = NULL; |
||
250 | } |
||
251 | |||
252 | /*static*/ int __init serio_init(void) |
||
253 | { |
||
254 | int pid = 1; |
||
255 | |||
256 | //!!!pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL); |
||
257 | |||
258 | if (!pid) { |
||
259 | printk(KERN_WARNING "serio: Failed to start kseriod\n"); |
||
260 | return -1; |
||
261 | } |
||
262 | |||
263 | serio_pid = pid; |
||
264 | |||
265 | return 0; |
||
266 | } |
||
267 | |||
268 | /*static*/ void __exit serio_exit(void) |
||
269 | { |
||
270 | //!!!kill_proc(serio_pid, SIGTERM, 1); |
||
271 | wait_for_completion(&serio_exited); |
||
272 | } |
||
273 | |||
274 | module_init(serio_init); |
||
275 | module_exit(serio_exit); |