Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * The Serio abstraction module | |
3 | * | |
4 | * Copyright (c) 1999-2004 Vojtech Pavlik | |
5 | * Copyright (c) 2004 Dmitry Torokhov | |
6 | * Copyright (c) 2003 Daniele Bellucci | |
7 | */ | |
8 | ||
9 | /* | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | * | |
24 | * Should you need to contact me, the author, you can do so either by | |
25 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: | |
26 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic | |
27 | */ | |
28 | ||
29 | #include <linux/stddef.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/serio.h> | |
32 | #include <linux/errno.h> | |
33 | #include <linux/wait.h> | |
34 | #include <linux/completion.h> | |
35 | #include <linux/sched.h> | |
36 | #include <linux/smp_lock.h> | |
37 | #include <linux/slab.h> | |
38 | ||
39 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
40 | MODULE_DESCRIPTION("Serio abstraction core"); | |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
43 | EXPORT_SYMBOL(serio_interrupt); | |
44 | EXPORT_SYMBOL(__serio_register_port); | |
45 | EXPORT_SYMBOL(serio_unregister_port); | |
46 | EXPORT_SYMBOL(__serio_unregister_port_delayed); | |
47 | EXPORT_SYMBOL(__serio_register_driver); | |
48 | EXPORT_SYMBOL(serio_unregister_driver); | |
49 | EXPORT_SYMBOL(serio_open); | |
50 | EXPORT_SYMBOL(serio_close); | |
51 | EXPORT_SYMBOL(serio_rescan); | |
52 | EXPORT_SYMBOL(serio_reconnect); | |
53 | ||
54 | /* | |
55 | * serio_sem protects entire serio subsystem and is taken every time | |
56 | * serio port or driver registrered or unregistered. | |
57 | */ | |
58 | static DECLARE_MUTEX(serio_sem); | |
59 | ||
60 | static LIST_HEAD(serio_list); | |
61 | ||
62 | static struct bus_type serio_bus = { | |
63 | .name = "serio", | |
64 | }; | |
65 | ||
66 | static void serio_add_port(struct serio *serio); | |
67 | static void serio_destroy_port(struct serio *serio); | |
68 | static void serio_reconnect_port(struct serio *serio); | |
69 | static void serio_disconnect_port(struct serio *serio); | |
70 | ||
71 | static int serio_match_port(const struct serio_device_id *ids, struct serio *serio) | |
72 | { | |
73 | while (ids->type || ids->proto) { | |
74 | if ((ids->type == SERIO_ANY || ids->type == serio->id.type) && | |
75 | (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) && | |
76 | (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) && | |
77 | (ids->id == SERIO_ANY || ids->id == serio->id.id)) | |
78 | return 1; | |
79 | ids++; | |
80 | } | |
81 | return 0; | |
82 | } | |
83 | ||
84 | /* | |
85 | * Basic serio -> driver core mappings | |
86 | */ | |
87 | ||
88 | static void serio_bind_driver(struct serio *serio, struct serio_driver *drv) | |
89 | { | |
90 | down_write(&serio_bus.subsys.rwsem); | |
91 | ||
92 | if (serio_match_port(drv->id_table, serio)) { | |
93 | serio->dev.driver = &drv->driver; | |
94 | if (drv->connect(serio, drv)) { | |
95 | serio->dev.driver = NULL; | |
96 | goto out; | |
97 | } | |
98 | device_bind_driver(&serio->dev); | |
99 | } | |
100 | out: | |
101 | up_write(&serio_bus.subsys.rwsem); | |
102 | } | |
103 | ||
104 | static void serio_release_driver(struct serio *serio) | |
105 | { | |
106 | down_write(&serio_bus.subsys.rwsem); | |
107 | device_release_driver(&serio->dev); | |
108 | up_write(&serio_bus.subsys.rwsem); | |
109 | } | |
110 | ||
111 | static void serio_find_driver(struct serio *serio) | |
112 | { | |
113 | down_write(&serio_bus.subsys.rwsem); | |
114 | device_attach(&serio->dev); | |
115 | up_write(&serio_bus.subsys.rwsem); | |
116 | } | |
117 | ||
118 | ||
119 | /* | |
120 | * Serio event processing. | |
121 | */ | |
122 | ||
123 | enum serio_event_type { | |
124 | SERIO_RESCAN, | |
125 | SERIO_RECONNECT, | |
126 | SERIO_REGISTER_PORT, | |
127 | SERIO_UNREGISTER_PORT, | |
128 | SERIO_REGISTER_DRIVER, | |
129 | }; | |
130 | ||
131 | struct serio_event { | |
132 | enum serio_event_type type; | |
133 | void *object; | |
134 | struct module *owner; | |
135 | struct list_head node; | |
136 | }; | |
137 | ||
138 | static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */ | |
139 | static LIST_HEAD(serio_event_list); | |
140 | static DECLARE_WAIT_QUEUE_HEAD(serio_wait); | |
141 | static DECLARE_COMPLETION(serio_exited); | |
142 | static int serio_pid; | |
143 | ||
144 | static void serio_queue_event(void *object, struct module *owner, | |
145 | enum serio_event_type event_type) | |
146 | { | |
147 | unsigned long flags; | |
148 | struct serio_event *event; | |
149 | ||
150 | spin_lock_irqsave(&serio_event_lock, flags); | |
151 | ||
152 | /* | |
153 | * Scan event list for the other events for the same serio port, | |
154 | * starting with the most recent one. If event is the same we | |
155 | * do not need add new one. If event is of different type we | |
156 | * need to add this event and should not look further because | |
157 | * we need to preseve sequence of distinct events. | |
158 | */ | |
159 | list_for_each_entry_reverse(event, &serio_event_list, node) { | |
160 | if (event->object == object) { | |
161 | if (event->type == event_type) | |
162 | goto out; | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) { | |
168 | if (!try_module_get(owner)) { | |
169 | printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type); | |
170 | goto out; | |
171 | } | |
172 | ||
173 | event->type = event_type; | |
174 | event->object = object; | |
175 | event->owner = owner; | |
176 | ||
177 | list_add_tail(&event->node, &serio_event_list); | |
178 | wake_up(&serio_wait); | |
179 | } else { | |
180 | printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type); | |
181 | } | |
182 | out: | |
183 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
184 | } | |
185 | ||
186 | static void serio_free_event(struct serio_event *event) | |
187 | { | |
188 | module_put(event->owner); | |
189 | kfree(event); | |
190 | } | |
191 | ||
192 | static void serio_remove_duplicate_events(struct serio_event *event) | |
193 | { | |
194 | struct list_head *node, *next; | |
195 | struct serio_event *e; | |
196 | unsigned long flags; | |
197 | ||
198 | spin_lock_irqsave(&serio_event_lock, flags); | |
199 | ||
200 | list_for_each_safe(node, next, &serio_event_list) { | |
201 | e = list_entry(node, struct serio_event, node); | |
202 | if (event->object == e->object) { | |
203 | /* | |
204 | * If this event is of different type we should not | |
205 | * look further - we only suppress duplicate events | |
206 | * that were sent back-to-back. | |
207 | */ | |
208 | if (event->type != e->type) | |
209 | break; | |
210 | ||
211 | list_del_init(node); | |
212 | serio_free_event(e); | |
213 | } | |
214 | } | |
215 | ||
216 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
217 | } | |
218 | ||
219 | ||
220 | static struct serio_event *serio_get_event(void) | |
221 | { | |
222 | struct serio_event *event; | |
223 | struct list_head *node; | |
224 | unsigned long flags; | |
225 | ||
226 | spin_lock_irqsave(&serio_event_lock, flags); | |
227 | ||
228 | if (list_empty(&serio_event_list)) { | |
229 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
230 | return NULL; | |
231 | } | |
232 | ||
233 | node = serio_event_list.next; | |
234 | event = list_entry(node, struct serio_event, node); | |
235 | list_del_init(node); | |
236 | ||
237 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
238 | ||
239 | return event; | |
240 | } | |
241 | ||
242 | static void serio_handle_events(void) | |
243 | { | |
244 | struct serio_event *event; | |
245 | struct serio_driver *serio_drv; | |
246 | ||
247 | down(&serio_sem); | |
248 | ||
249 | while ((event = serio_get_event())) { | |
250 | ||
251 | switch (event->type) { | |
252 | case SERIO_REGISTER_PORT: | |
253 | serio_add_port(event->object); | |
254 | break; | |
255 | ||
256 | case SERIO_UNREGISTER_PORT: | |
257 | serio_disconnect_port(event->object); | |
258 | serio_destroy_port(event->object); | |
259 | break; | |
260 | ||
261 | case SERIO_RECONNECT: | |
262 | serio_reconnect_port(event->object); | |
263 | break; | |
264 | ||
265 | case SERIO_RESCAN: | |
266 | serio_disconnect_port(event->object); | |
267 | serio_find_driver(event->object); | |
268 | break; | |
269 | ||
270 | case SERIO_REGISTER_DRIVER: | |
271 | serio_drv = event->object; | |
272 | driver_register(&serio_drv->driver); | |
273 | break; | |
274 | ||
275 | default: | |
276 | break; | |
277 | } | |
278 | ||
279 | serio_remove_duplicate_events(event); | |
280 | serio_free_event(event); | |
281 | } | |
282 | ||
283 | up(&serio_sem); | |
284 | } | |
285 | ||
286 | /* | |
287 | * Remove all events that have been submitted for a given serio port. | |
288 | */ | |
289 | static void serio_remove_pending_events(struct serio *serio) | |
290 | { | |
291 | struct list_head *node, *next; | |
292 | struct serio_event *event; | |
293 | unsigned long flags; | |
294 | ||
295 | spin_lock_irqsave(&serio_event_lock, flags); | |
296 | ||
297 | list_for_each_safe(node, next, &serio_event_list) { | |
298 | event = list_entry(node, struct serio_event, node); | |
299 | if (event->object == serio) { | |
300 | list_del_init(node); | |
301 | serio_free_event(event); | |
302 | } | |
303 | } | |
304 | ||
305 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
306 | } | |
307 | ||
308 | /* | |
309 | * Destroy child serio port (if any) that has not been fully registered yet. | |
310 | * | |
311 | * Note that we rely on the fact that port can have only one child and therefore | |
312 | * only one child registration request can be pending. Additionally, children | |
313 | * are registered by driver's connect() handler so there can't be a grandchild | |
314 | * pending registration together with a child. | |
315 | */ | |
316 | static struct serio *serio_get_pending_child(struct serio *parent) | |
317 | { | |
318 | struct serio_event *event; | |
319 | struct serio *serio, *child = NULL; | |
320 | unsigned long flags; | |
321 | ||
322 | spin_lock_irqsave(&serio_event_lock, flags); | |
323 | ||
324 | list_for_each_entry(event, &serio_event_list, node) { | |
325 | if (event->type == SERIO_REGISTER_PORT) { | |
326 | serio = event->object; | |
327 | if (serio->parent == parent) { | |
328 | child = serio; | |
329 | break; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
335 | return child; | |
336 | } | |
337 | ||
338 | static int serio_thread(void *nothing) | |
339 | { | |
340 | lock_kernel(); | |
341 | daemonize("kseriod"); | |
342 | allow_signal(SIGTERM); | |
343 | ||
344 | do { | |
345 | serio_handle_events(); | |
346 | wait_event_interruptible(serio_wait, !list_empty(&serio_event_list)); | |
347 | try_to_freeze(PF_FREEZE); | |
348 | } while (!signal_pending(current)); | |
349 | ||
350 | printk(KERN_DEBUG "serio: kseriod exiting\n"); | |
351 | ||
352 | unlock_kernel(); | |
353 | complete_and_exit(&serio_exited, 0); | |
354 | } | |
355 | ||
356 | ||
357 | /* | |
358 | * Serio port operations | |
359 | */ | |
360 | ||
e404e274 | 361 | static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
362 | { |
363 | struct serio *serio = to_serio_port(dev); | |
364 | return sprintf(buf, "%s\n", serio->name); | |
365 | } | |
366 | ||
e404e274 | 367 | static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
368 | { |
369 | struct serio *serio = to_serio_port(dev); | |
370 | return sprintf(buf, "%02x\n", serio->id.type); | |
371 | } | |
372 | ||
e404e274 | 373 | static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
374 | { |
375 | struct serio *serio = to_serio_port(dev); | |
376 | return sprintf(buf, "%02x\n", serio->id.proto); | |
377 | } | |
378 | ||
e404e274 | 379 | static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
380 | { |
381 | struct serio *serio = to_serio_port(dev); | |
382 | return sprintf(buf, "%02x\n", serio->id.id); | |
383 | } | |
384 | ||
e404e274 | 385 | static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
386 | { |
387 | struct serio *serio = to_serio_port(dev); | |
388 | return sprintf(buf, "%02x\n", serio->id.extra); | |
389 | } | |
390 | ||
baae9561 DT |
391 | static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL); |
392 | static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL); | |
393 | static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL); | |
394 | static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL); | |
395 | ||
396 | static struct attribute *serio_device_id_attrs[] = { | |
397 | &dev_attr_type.attr, | |
398 | &dev_attr_proto.attr, | |
399 | &dev_attr_id.attr, | |
400 | &dev_attr_extra.attr, | |
401 | NULL | |
402 | }; | |
403 | ||
404 | static struct attribute_group serio_id_attr_group = { | |
405 | .name = "id", | |
406 | .attrs = serio_device_id_attrs, | |
407 | }; | |
408 | ||
e404e274 | 409 | static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
410 | { |
411 | struct serio *serio = to_serio_port(dev); | |
412 | struct device_driver *drv; | |
413 | int retval; | |
414 | ||
415 | retval = down_interruptible(&serio_sem); | |
416 | if (retval) | |
417 | return retval; | |
418 | ||
419 | retval = count; | |
420 | if (!strncmp(buf, "none", count)) { | |
421 | serio_disconnect_port(serio); | |
422 | } else if (!strncmp(buf, "reconnect", count)) { | |
423 | serio_reconnect_port(serio); | |
424 | } else if (!strncmp(buf, "rescan", count)) { | |
425 | serio_disconnect_port(serio); | |
426 | serio_find_driver(serio); | |
427 | } else if ((drv = driver_find(buf, &serio_bus)) != NULL) { | |
428 | serio_disconnect_port(serio); | |
429 | serio_bind_driver(serio, to_serio_driver(drv)); | |
430 | put_driver(drv); | |
431 | } else { | |
432 | retval = -EINVAL; | |
433 | } | |
434 | ||
435 | up(&serio_sem); | |
436 | ||
437 | return retval; | |
438 | } | |
439 | ||
e404e274 | 440 | static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
441 | { |
442 | struct serio *serio = to_serio_port(dev); | |
443 | return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto"); | |
444 | } | |
445 | ||
e404e274 | 446 | static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
447 | { |
448 | struct serio *serio = to_serio_port(dev); | |
449 | int retval; | |
450 | ||
451 | retval = count; | |
452 | if (!strncmp(buf, "manual", count)) { | |
453 | serio->manual_bind = 1; | |
454 | } else if (!strncmp(buf, "auto", count)) { | |
455 | serio->manual_bind = 0; | |
456 | } else { | |
457 | retval = -EINVAL; | |
458 | } | |
459 | ||
460 | return retval; | |
461 | } | |
462 | ||
463 | static struct device_attribute serio_device_attrs[] = { | |
464 | __ATTR(description, S_IRUGO, serio_show_description, NULL), | |
1da177e4 LT |
465 | __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver), |
466 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode), | |
467 | __ATTR_NULL | |
468 | }; | |
469 | ||
470 | ||
471 | static void serio_release_port(struct device *dev) | |
472 | { | |
473 | struct serio *serio = to_serio_port(dev); | |
474 | ||
475 | kfree(serio); | |
476 | module_put(THIS_MODULE); | |
477 | } | |
478 | ||
479 | /* | |
480 | * Prepare serio port for registration. | |
481 | */ | |
482 | static void serio_init_port(struct serio *serio) | |
483 | { | |
484 | static atomic_t serio_no = ATOMIC_INIT(0); | |
485 | ||
486 | __module_get(THIS_MODULE); | |
487 | ||
488 | spin_lock_init(&serio->lock); | |
489 | init_MUTEX(&serio->drv_sem); | |
490 | device_initialize(&serio->dev); | |
491 | snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), | |
492 | "serio%ld", (long)atomic_inc_return(&serio_no) - 1); | |
493 | serio->dev.bus = &serio_bus; | |
494 | serio->dev.release = serio_release_port; | |
495 | if (serio->parent) | |
496 | serio->dev.parent = &serio->parent->dev; | |
497 | } | |
498 | ||
499 | /* | |
500 | * Complete serio port registration. | |
501 | * Driver core will attempt to find appropriate driver for the port. | |
502 | */ | |
503 | static void serio_add_port(struct serio *serio) | |
504 | { | |
505 | if (serio->parent) { | |
506 | serio_pause_rx(serio->parent); | |
507 | serio->parent->child = serio; | |
508 | serio_continue_rx(serio->parent); | |
509 | } | |
510 | ||
511 | list_add_tail(&serio->node, &serio_list); | |
512 | if (serio->start) | |
513 | serio->start(serio); | |
514 | device_add(&serio->dev); | |
baae9561 | 515 | sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group); |
1da177e4 LT |
516 | serio->registered = 1; |
517 | } | |
518 | ||
519 | /* | |
520 | * serio_destroy_port() completes deregistration process and removes | |
521 | * port from the system | |
522 | */ | |
523 | static void serio_destroy_port(struct serio *serio) | |
524 | { | |
525 | struct serio *child; | |
526 | ||
527 | child = serio_get_pending_child(serio); | |
528 | if (child) { | |
529 | serio_remove_pending_events(child); | |
530 | put_device(&child->dev); | |
531 | } | |
532 | ||
533 | if (serio->stop) | |
534 | serio->stop(serio); | |
535 | ||
536 | if (serio->parent) { | |
537 | serio_pause_rx(serio->parent); | |
538 | serio->parent->child = NULL; | |
539 | serio_continue_rx(serio->parent); | |
540 | serio->parent = NULL; | |
541 | } | |
542 | ||
543 | if (serio->registered) { | |
baae9561 | 544 | sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group); |
1da177e4 LT |
545 | device_del(&serio->dev); |
546 | list_del_init(&serio->node); | |
547 | serio->registered = 0; | |
548 | } | |
549 | ||
550 | serio_remove_pending_events(serio); | |
551 | put_device(&serio->dev); | |
552 | } | |
553 | ||
554 | /* | |
555 | * Reconnect serio port and all its children (re-initialize attached devices) | |
556 | */ | |
557 | static void serio_reconnect_port(struct serio *serio) | |
558 | { | |
559 | do { | |
560 | if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) { | |
561 | serio_disconnect_port(serio); | |
562 | serio_find_driver(serio); | |
563 | /* Ok, old children are now gone, we are done */ | |
564 | break; | |
565 | } | |
566 | serio = serio->child; | |
567 | } while (serio); | |
568 | } | |
569 | ||
570 | /* | |
571 | * serio_disconnect_port() unbinds a port from its driver. As a side effect | |
572 | * all child ports are unbound and destroyed. | |
573 | */ | |
574 | static void serio_disconnect_port(struct serio *serio) | |
575 | { | |
576 | struct serio *s, *parent; | |
577 | ||
578 | if (serio->child) { | |
579 | /* | |
580 | * Children ports should be disconnected and destroyed | |
581 | * first, staring with the leaf one, since we don't want | |
582 | * to do recursion | |
583 | */ | |
584 | for (s = serio; s->child; s = s->child) | |
585 | /* empty */; | |
586 | ||
587 | do { | |
588 | parent = s->parent; | |
589 | ||
590 | serio_release_driver(s); | |
591 | serio_destroy_port(s); | |
592 | } while ((s = parent) != serio); | |
593 | } | |
594 | ||
595 | /* | |
596 | * Ok, no children left, now disconnect this port | |
597 | */ | |
598 | serio_release_driver(serio); | |
599 | } | |
600 | ||
601 | void serio_rescan(struct serio *serio) | |
602 | { | |
603 | serio_queue_event(serio, NULL, SERIO_RESCAN); | |
604 | } | |
605 | ||
606 | void serio_reconnect(struct serio *serio) | |
607 | { | |
608 | serio_queue_event(serio, NULL, SERIO_RECONNECT); | |
609 | } | |
610 | ||
611 | /* | |
612 | * Submits register request to kseriod for subsequent execution. | |
613 | * Note that port registration is always asynchronous. | |
614 | */ | |
615 | void __serio_register_port(struct serio *serio, struct module *owner) | |
616 | { | |
617 | serio_init_port(serio); | |
618 | serio_queue_event(serio, owner, SERIO_REGISTER_PORT); | |
619 | } | |
620 | ||
621 | /* | |
622 | * Synchronously unregisters serio port. | |
623 | */ | |
624 | void serio_unregister_port(struct serio *serio) | |
625 | { | |
626 | down(&serio_sem); | |
627 | serio_disconnect_port(serio); | |
628 | serio_destroy_port(serio); | |
629 | up(&serio_sem); | |
630 | } | |
631 | ||
632 | /* | |
633 | * Submits register request to kseriod for subsequent execution. | |
634 | * Can be used when it is not obvious whether the serio_sem is | |
635 | * taken or not and when delayed execution is feasible. | |
636 | */ | |
637 | void __serio_unregister_port_delayed(struct serio *serio, struct module *owner) | |
638 | { | |
639 | serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT); | |
640 | } | |
641 | ||
642 | ||
643 | /* | |
644 | * Serio driver operations | |
645 | */ | |
646 | ||
647 | static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf) | |
648 | { | |
649 | struct serio_driver *driver = to_serio_driver(drv); | |
650 | return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)"); | |
651 | } | |
652 | ||
653 | static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf) | |
654 | { | |
655 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
656 | return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto"); | |
657 | } | |
658 | ||
659 | static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count) | |
660 | { | |
661 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
662 | int retval; | |
663 | ||
664 | retval = count; | |
665 | if (!strncmp(buf, "manual", count)) { | |
666 | serio_drv->manual_bind = 1; | |
667 | } else if (!strncmp(buf, "auto", count)) { | |
668 | serio_drv->manual_bind = 0; | |
669 | } else { | |
670 | retval = -EINVAL; | |
671 | } | |
672 | ||
673 | return retval; | |
674 | } | |
675 | ||
676 | ||
677 | static struct driver_attribute serio_driver_attrs[] = { | |
678 | __ATTR(description, S_IRUGO, serio_driver_show_description, NULL), | |
679 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, | |
680 | serio_driver_show_bind_mode, serio_driver_set_bind_mode), | |
681 | __ATTR_NULL | |
682 | }; | |
683 | ||
684 | static int serio_driver_probe(struct device *dev) | |
685 | { | |
686 | struct serio *serio = to_serio_port(dev); | |
687 | struct serio_driver *drv = to_serio_driver(dev->driver); | |
688 | ||
689 | return drv->connect(serio, drv); | |
690 | } | |
691 | ||
692 | static int serio_driver_remove(struct device *dev) | |
693 | { | |
694 | struct serio *serio = to_serio_port(dev); | |
695 | struct serio_driver *drv = to_serio_driver(dev->driver); | |
696 | ||
697 | drv->disconnect(serio); | |
698 | return 0; | |
699 | } | |
700 | ||
701 | void __serio_register_driver(struct serio_driver *drv, struct module *owner) | |
702 | { | |
703 | drv->driver.bus = &serio_bus; | |
704 | drv->driver.probe = serio_driver_probe; | |
705 | drv->driver.remove = serio_driver_remove; | |
706 | ||
707 | serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER); | |
708 | } | |
709 | ||
710 | void serio_unregister_driver(struct serio_driver *drv) | |
711 | { | |
712 | struct serio *serio; | |
713 | ||
714 | down(&serio_sem); | |
715 | drv->manual_bind = 1; /* so serio_find_driver ignores it */ | |
716 | ||
717 | start_over: | |
718 | list_for_each_entry(serio, &serio_list, node) { | |
719 | if (serio->drv == drv) { | |
720 | serio_disconnect_port(serio); | |
721 | serio_find_driver(serio); | |
722 | /* we could've deleted some ports, restart */ | |
723 | goto start_over; | |
724 | } | |
725 | } | |
726 | ||
727 | driver_unregister(&drv->driver); | |
728 | up(&serio_sem); | |
729 | } | |
730 | ||
731 | static void serio_set_drv(struct serio *serio, struct serio_driver *drv) | |
732 | { | |
733 | down(&serio->drv_sem); | |
734 | serio_pause_rx(serio); | |
735 | serio->drv = drv; | |
736 | serio_continue_rx(serio); | |
737 | up(&serio->drv_sem); | |
738 | } | |
739 | ||
740 | static int serio_bus_match(struct device *dev, struct device_driver *drv) | |
741 | { | |
742 | struct serio *serio = to_serio_port(dev); | |
743 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
744 | ||
745 | if (serio->manual_bind || serio_drv->manual_bind) | |
746 | return 0; | |
747 | ||
748 | return serio_match_port(serio_drv->id_table, serio); | |
749 | } | |
750 | ||
751 | #ifdef CONFIG_HOTPLUG | |
752 | ||
753 | #define PUT_ENVP(fmt, val) \ | |
754 | do { \ | |
755 | envp[i++] = buffer; \ | |
756 | length += snprintf(buffer, buffer_size - length, fmt, val); \ | |
757 | if (buffer_size - length <= 0 || i >= num_envp) \ | |
758 | return -ENOMEM; \ | |
759 | length++; \ | |
760 | buffer += length; \ | |
761 | } while (0) | |
762 | static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) | |
763 | { | |
764 | struct serio *serio; | |
765 | int i = 0; | |
766 | int length = 0; | |
767 | ||
768 | if (!dev) | |
769 | return -ENODEV; | |
770 | ||
771 | serio = to_serio_port(dev); | |
772 | ||
773 | PUT_ENVP("SERIO_TYPE=%02x", serio->id.type); | |
774 | PUT_ENVP("SERIO_PROTO=%02x", serio->id.proto); | |
775 | PUT_ENVP("SERIO_ID=%02x", serio->id.id); | |
776 | PUT_ENVP("SERIO_EXTRA=%02x", serio->id.extra); | |
777 | ||
778 | envp[i] = NULL; | |
779 | ||
780 | return 0; | |
781 | } | |
782 | #undef PUT_ENVP | |
783 | ||
784 | #else | |
785 | ||
786 | static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) | |
787 | { | |
788 | return -ENODEV; | |
789 | } | |
790 | ||
791 | #endif /* CONFIG_HOTPLUG */ | |
792 | ||
793 | static int serio_resume(struct device *dev) | |
794 | { | |
795 | struct serio *serio = to_serio_port(dev); | |
796 | ||
797 | if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) { | |
1da177e4 LT |
798 | /* |
799 | * Driver re-probing can take a while, so better let kseriod | |
800 | * deal with it. | |
801 | */ | |
802 | serio_rescan(serio); | |
803 | } | |
804 | ||
805 | return 0; | |
806 | } | |
807 | ||
808 | /* called from serio_driver->connect/disconnect methods under serio_sem */ | |
809 | int serio_open(struct serio *serio, struct serio_driver *drv) | |
810 | { | |
811 | serio_set_drv(serio, drv); | |
812 | ||
813 | if (serio->open && serio->open(serio)) { | |
814 | serio_set_drv(serio, NULL); | |
815 | return -1; | |
816 | } | |
817 | return 0; | |
818 | } | |
819 | ||
820 | /* called from serio_driver->connect/disconnect methods under serio_sem */ | |
821 | void serio_close(struct serio *serio) | |
822 | { | |
823 | if (serio->close) | |
824 | serio->close(serio); | |
825 | ||
826 | serio_set_drv(serio, NULL); | |
827 | } | |
828 | ||
829 | irqreturn_t serio_interrupt(struct serio *serio, | |
830 | unsigned char data, unsigned int dfl, struct pt_regs *regs) | |
831 | { | |
832 | unsigned long flags; | |
833 | irqreturn_t ret = IRQ_NONE; | |
834 | ||
835 | spin_lock_irqsave(&serio->lock, flags); | |
836 | ||
837 | if (likely(serio->drv)) { | |
838 | ret = serio->drv->interrupt(serio, data, dfl, regs); | |
839 | } else if (!dfl && serio->registered) { | |
840 | serio_rescan(serio); | |
841 | ret = IRQ_HANDLED; | |
842 | } | |
843 | ||
844 | spin_unlock_irqrestore(&serio->lock, flags); | |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
849 | static int __init serio_init(void) | |
850 | { | |
851 | if (!(serio_pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL))) { | |
852 | printk(KERN_ERR "serio: Failed to start kseriod\n"); | |
853 | return -1; | |
854 | } | |
855 | ||
856 | serio_bus.dev_attrs = serio_device_attrs; | |
857 | serio_bus.drv_attrs = serio_driver_attrs; | |
858 | serio_bus.match = serio_bus_match; | |
859 | serio_bus.hotplug = serio_hotplug; | |
860 | serio_bus.resume = serio_resume; | |
861 | bus_register(&serio_bus); | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
866 | static void __exit serio_exit(void) | |
867 | { | |
868 | bus_unregister(&serio_bus); | |
869 | kill_proc(serio_pid, SIGTERM, 1); | |
870 | wait_for_completion(&serio_exited); | |
871 | } | |
872 | ||
873 | module_init(serio_init); | |
874 | module_exit(serio_exit); |