blob: 2f76813c3a645c7d3d6a9e797df4525f19c6ac3d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070036#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39MODULE_DESCRIPTION("Serio abstraction core");
40MODULE_LICENSE("GPL");
41
42EXPORT_SYMBOL(serio_interrupt);
43EXPORT_SYMBOL(__serio_register_port);
44EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -070045EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046EXPORT_SYMBOL(__serio_unregister_port_delayed);
47EXPORT_SYMBOL(__serio_register_driver);
48EXPORT_SYMBOL(serio_unregister_driver);
49EXPORT_SYMBOL(serio_open);
50EXPORT_SYMBOL(serio_close);
51EXPORT_SYMBOL(serio_rescan);
52EXPORT_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 */
58static DECLARE_MUTEX(serio_sem);
59
60static LIST_HEAD(serio_list);
61
Russell King30226f82006-01-05 14:38:53 +000062static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static void serio_add_port(struct serio *serio);
65static void serio_destroy_port(struct serio *serio);
66static void serio_reconnect_port(struct serio *serio);
67static void serio_disconnect_port(struct serio *serio);
68
Linus Torvalds3c803e82005-06-27 17:49:45 -070069static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
70{
71 int retval;
72
73 down(&serio->drv_sem);
74 retval = drv->connect(serio, drv);
75 up(&serio->drv_sem);
76
77 return retval;
78}
79
80static int serio_reconnect_driver(struct serio *serio)
81{
82 int retval = -1;
83
84 down(&serio->drv_sem);
85 if (serio->drv && serio->drv->reconnect)
86 retval = serio->drv->reconnect(serio);
87 up(&serio->drv_sem);
88
89 return retval;
90}
91
92static void serio_disconnect_driver(struct serio *serio)
93{
94 down(&serio->drv_sem);
95 if (serio->drv)
96 serio->drv->disconnect(serio);
97 up(&serio->drv_sem);
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
101{
102 while (ids->type || ids->proto) {
103 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
104 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
105 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
106 (ids->id == SERIO_ANY || ids->id == serio->id.id))
107 return 1;
108 ids++;
109 }
110 return 0;
111}
112
113/*
114 * Basic serio -> driver core mappings
115 */
116
117static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
118{
119 down_write(&serio_bus.subsys.rwsem);
120
121 if (serio_match_port(drv->id_table, serio)) {
122 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700123 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 serio->dev.driver = NULL;
125 goto out;
126 }
127 device_bind_driver(&serio->dev);
128 }
129out:
130 up_write(&serio_bus.subsys.rwsem);
131}
132
133static void serio_release_driver(struct serio *serio)
134{
135 down_write(&serio_bus.subsys.rwsem);
136 device_release_driver(&serio->dev);
137 up_write(&serio_bus.subsys.rwsem);
138}
139
140static void serio_find_driver(struct serio *serio)
141{
142 down_write(&serio_bus.subsys.rwsem);
143 device_attach(&serio->dev);
144 up_write(&serio_bus.subsys.rwsem);
145}
146
147
148/*
149 * Serio event processing.
150 */
151
152enum serio_event_type {
153 SERIO_RESCAN,
154 SERIO_RECONNECT,
155 SERIO_REGISTER_PORT,
156 SERIO_UNREGISTER_PORT,
157 SERIO_REGISTER_DRIVER,
158};
159
160struct serio_event {
161 enum serio_event_type type;
162 void *object;
163 struct module *owner;
164 struct list_head node;
165};
166
167static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
168static LIST_HEAD(serio_event_list);
169static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700170static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static void serio_queue_event(void *object, struct module *owner,
173 enum serio_event_type event_type)
174{
175 unsigned long flags;
176 struct serio_event *event;
177
178 spin_lock_irqsave(&serio_event_lock, flags);
179
180 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700181 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * starting with the most recent one. If event is the same we
183 * do not need add new one. If event is of different type we
184 * need to add this event and should not look further because
185 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 list_for_each_entry_reverse(event, &serio_event_list, node) {
188 if (event->object == object) {
189 if (event->type == event_type)
190 goto out;
191 break;
192 }
193 }
194
195 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
196 if (!try_module_get(owner)) {
197 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
198 goto out;
199 }
200
201 event->type = event_type;
202 event->object = object;
203 event->owner = owner;
204
205 list_add_tail(&event->node, &serio_event_list);
206 wake_up(&serio_wait);
207 } else {
208 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
209 }
210out:
211 spin_unlock_irqrestore(&serio_event_lock, flags);
212}
213
214static void serio_free_event(struct serio_event *event)
215{
216 module_put(event->owner);
217 kfree(event);
218}
219
220static void serio_remove_duplicate_events(struct serio_event *event)
221{
222 struct list_head *node, *next;
223 struct serio_event *e;
224 unsigned long flags;
225
226 spin_lock_irqsave(&serio_event_lock, flags);
227
228 list_for_each_safe(node, next, &serio_event_list) {
229 e = list_entry(node, struct serio_event, node);
230 if (event->object == e->object) {
231 /*
232 * If this event is of different type we should not
233 * look further - we only suppress duplicate events
234 * that were sent back-to-back.
235 */
236 if (event->type != e->type)
237 break;
238
239 list_del_init(node);
240 serio_free_event(e);
241 }
242 }
243
244 spin_unlock_irqrestore(&serio_event_lock, flags);
245}
246
247
248static struct serio_event *serio_get_event(void)
249{
250 struct serio_event *event;
251 struct list_head *node;
252 unsigned long flags;
253
254 spin_lock_irqsave(&serio_event_lock, flags);
255
256 if (list_empty(&serio_event_list)) {
257 spin_unlock_irqrestore(&serio_event_lock, flags);
258 return NULL;
259 }
260
261 node = serio_event_list.next;
262 event = list_entry(node, struct serio_event, node);
263 list_del_init(node);
264
265 spin_unlock_irqrestore(&serio_event_lock, flags);
266
267 return event;
268}
269
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500270static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct serio_event *event;
273 struct serio_driver *serio_drv;
274
275 down(&serio_sem);
276
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500277 /*
278 * Note that we handle only one event here to give swsusp
279 * a chance to freeze kseriod thread. Serio events should
280 * be pretty rare so we are not concerned about taking
281 * performance hit.
282 */
283 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 switch (event->type) {
286 case SERIO_REGISTER_PORT:
287 serio_add_port(event->object);
288 break;
289
290 case SERIO_UNREGISTER_PORT:
291 serio_disconnect_port(event->object);
292 serio_destroy_port(event->object);
293 break;
294
295 case SERIO_RECONNECT:
296 serio_reconnect_port(event->object);
297 break;
298
299 case SERIO_RESCAN:
300 serio_disconnect_port(event->object);
301 serio_find_driver(event->object);
302 break;
303
304 case SERIO_REGISTER_DRIVER:
305 serio_drv = event->object;
306 driver_register(&serio_drv->driver);
307 break;
308
309 default:
310 break;
311 }
312
313 serio_remove_duplicate_events(event);
314 serio_free_event(event);
315 }
316
317 up(&serio_sem);
318}
319
320/*
321 * Remove all events that have been submitted for a given serio port.
322 */
323static void serio_remove_pending_events(struct serio *serio)
324{
325 struct list_head *node, *next;
326 struct serio_event *event;
327 unsigned long flags;
328
329 spin_lock_irqsave(&serio_event_lock, flags);
330
331 list_for_each_safe(node, next, &serio_event_list) {
332 event = list_entry(node, struct serio_event, node);
333 if (event->object == serio) {
334 list_del_init(node);
335 serio_free_event(event);
336 }
337 }
338
339 spin_unlock_irqrestore(&serio_event_lock, flags);
340}
341
342/*
343 * Destroy child serio port (if any) that has not been fully registered yet.
344 *
345 * Note that we rely on the fact that port can have only one child and therefore
346 * only one child registration request can be pending. Additionally, children
347 * are registered by driver's connect() handler so there can't be a grandchild
348 * pending registration together with a child.
349 */
350static struct serio *serio_get_pending_child(struct serio *parent)
351{
352 struct serio_event *event;
353 struct serio *serio, *child = NULL;
354 unsigned long flags;
355
356 spin_lock_irqsave(&serio_event_lock, flags);
357
358 list_for_each_entry(event, &serio_event_list, node) {
359 if (event->type == SERIO_REGISTER_PORT) {
360 serio = event->object;
361 if (serio->parent == parent) {
362 child = serio;
363 break;
364 }
365 }
366 }
367
368 spin_unlock_irqrestore(&serio_event_lock, flags);
369 return child;
370}
371
372static int serio_thread(void *nothing)
373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500375 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700376 wait_event_interruptible(serio_wait,
377 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700378 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700379 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385
386/*
387 * Serio port operations
388 */
389
Yani Ioannoue404e272005-05-17 06:42:58 -0400390static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct serio *serio = to_serio_port(dev);
393 return sprintf(buf, "%s\n", serio->name);
394}
395
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500396static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
397{
398 struct serio *serio = to_serio_port(dev);
399
400 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
401 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
402}
403
Yani Ioannoue404e272005-05-17 06:42:58 -0400404static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct serio *serio = to_serio_port(dev);
407 return sprintf(buf, "%02x\n", serio->id.type);
408}
409
Yani Ioannoue404e272005-05-17 06:42:58 -0400410static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct serio *serio = to_serio_port(dev);
413 return sprintf(buf, "%02x\n", serio->id.proto);
414}
415
Yani Ioannoue404e272005-05-17 06:42:58 -0400416static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct serio *serio = to_serio_port(dev);
419 return sprintf(buf, "%02x\n", serio->id.id);
420}
421
Yani Ioannoue404e272005-05-17 06:42:58 -0400422static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct serio *serio = to_serio_port(dev);
425 return sprintf(buf, "%02x\n", serio->id.extra);
426}
427
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700428static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
429static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
430static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
431static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
432
433static struct attribute *serio_device_id_attrs[] = {
434 &dev_attr_type.attr,
435 &dev_attr_proto.attr,
436 &dev_attr_id.attr,
437 &dev_attr_extra.attr,
438 NULL
439};
440
441static struct attribute_group serio_id_attr_group = {
442 .name = "id",
443 .attrs = serio_device_id_attrs,
444};
445
Yani Ioannoue404e272005-05-17 06:42:58 -0400446static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct serio *serio = to_serio_port(dev);
449 struct device_driver *drv;
450 int retval;
451
452 retval = down_interruptible(&serio_sem);
453 if (retval)
454 return retval;
455
456 retval = count;
457 if (!strncmp(buf, "none", count)) {
458 serio_disconnect_port(serio);
459 } else if (!strncmp(buf, "reconnect", count)) {
460 serio_reconnect_port(serio);
461 } else if (!strncmp(buf, "rescan", count)) {
462 serio_disconnect_port(serio);
463 serio_find_driver(serio);
464 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
465 serio_disconnect_port(serio);
466 serio_bind_driver(serio, to_serio_driver(drv));
467 put_driver(drv);
468 } else {
469 retval = -EINVAL;
470 }
471
472 up(&serio_sem);
473
474 return retval;
475}
476
Yani Ioannoue404e272005-05-17 06:42:58 -0400477static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct serio *serio = to_serio_port(dev);
480 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
481}
482
Yani Ioannoue404e272005-05-17 06:42:58 -0400483static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct serio *serio = to_serio_port(dev);
486 int retval;
487
488 retval = count;
489 if (!strncmp(buf, "manual", count)) {
490 serio->manual_bind = 1;
491 } else if (!strncmp(buf, "auto", count)) {
492 serio->manual_bind = 0;
493 } else {
494 retval = -EINVAL;
495 }
496
497 return retval;
498}
499
500static struct device_attribute serio_device_attrs[] = {
501 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500502 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
504 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
505 __ATTR_NULL
506};
507
508
509static void serio_release_port(struct device *dev)
510{
511 struct serio *serio = to_serio_port(dev);
512
513 kfree(serio);
514 module_put(THIS_MODULE);
515}
516
517/*
518 * Prepare serio port for registration.
519 */
520static void serio_init_port(struct serio *serio)
521{
522 static atomic_t serio_no = ATOMIC_INIT(0);
523
524 __module_get(THIS_MODULE);
525
526 spin_lock_init(&serio->lock);
527 init_MUTEX(&serio->drv_sem);
528 device_initialize(&serio->dev);
529 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
530 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
531 serio->dev.bus = &serio_bus;
532 serio->dev.release = serio_release_port;
533 if (serio->parent)
534 serio->dev.parent = &serio->parent->dev;
535}
536
537/*
538 * Complete serio port registration.
539 * Driver core will attempt to find appropriate driver for the port.
540 */
541static void serio_add_port(struct serio *serio)
542{
543 if (serio->parent) {
544 serio_pause_rx(serio->parent);
545 serio->parent->child = serio;
546 serio_continue_rx(serio->parent);
547 }
548
549 list_add_tail(&serio->node, &serio_list);
550 if (serio->start)
551 serio->start(serio);
552 device_add(&serio->dev);
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700553 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 serio->registered = 1;
555}
556
557/*
558 * serio_destroy_port() completes deregistration process and removes
559 * port from the system
560 */
561static void serio_destroy_port(struct serio *serio)
562{
563 struct serio *child;
564
565 child = serio_get_pending_child(serio);
566 if (child) {
567 serio_remove_pending_events(child);
568 put_device(&child->dev);
569 }
570
571 if (serio->stop)
572 serio->stop(serio);
573
574 if (serio->parent) {
575 serio_pause_rx(serio->parent);
576 serio->parent->child = NULL;
577 serio_continue_rx(serio->parent);
578 serio->parent = NULL;
579 }
580
581 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700582 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 device_del(&serio->dev);
584 list_del_init(&serio->node);
585 serio->registered = 0;
586 }
587
588 serio_remove_pending_events(serio);
589 put_device(&serio->dev);
590}
591
592/*
593 * Reconnect serio port and all its children (re-initialize attached devices)
594 */
595static void serio_reconnect_port(struct serio *serio)
596{
597 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700598 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 serio_disconnect_port(serio);
600 serio_find_driver(serio);
601 /* Ok, old children are now gone, we are done */
602 break;
603 }
604 serio = serio->child;
605 } while (serio);
606}
607
608/*
609 * serio_disconnect_port() unbinds a port from its driver. As a side effect
610 * all child ports are unbound and destroyed.
611 */
612static void serio_disconnect_port(struct serio *serio)
613{
614 struct serio *s, *parent;
615
616 if (serio->child) {
617 /*
618 * Children ports should be disconnected and destroyed
619 * first, staring with the leaf one, since we don't want
620 * to do recursion
621 */
622 for (s = serio; s->child; s = s->child)
623 /* empty */;
624
625 do {
626 parent = s->parent;
627
628 serio_release_driver(s);
629 serio_destroy_port(s);
630 } while ((s = parent) != serio);
631 }
632
633 /*
634 * Ok, no children left, now disconnect this port
635 */
636 serio_release_driver(serio);
637}
638
639void serio_rescan(struct serio *serio)
640{
641 serio_queue_event(serio, NULL, SERIO_RESCAN);
642}
643
644void serio_reconnect(struct serio *serio)
645{
646 serio_queue_event(serio, NULL, SERIO_RECONNECT);
647}
648
649/*
650 * Submits register request to kseriod for subsequent execution.
651 * Note that port registration is always asynchronous.
652 */
653void __serio_register_port(struct serio *serio, struct module *owner)
654{
655 serio_init_port(serio);
656 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
657}
658
659/*
660 * Synchronously unregisters serio port.
661 */
662void serio_unregister_port(struct serio *serio)
663{
664 down(&serio_sem);
665 serio_disconnect_port(serio);
666 serio_destroy_port(serio);
667 up(&serio_sem);
668}
669
670/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700671 * Safely unregisters child port if one is present.
672 */
673void serio_unregister_child_port(struct serio *serio)
674{
675 down(&serio_sem);
676 if (serio->child) {
677 serio_disconnect_port(serio->child);
678 serio_destroy_port(serio->child);
679 }
680 up(&serio_sem);
681}
682
683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * Submits register request to kseriod for subsequent execution.
685 * Can be used when it is not obvious whether the serio_sem is
686 * taken or not and when delayed execution is feasible.
687 */
688void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
689{
690 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
691}
692
693
694/*
695 * Serio driver operations
696 */
697
698static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
699{
700 struct serio_driver *driver = to_serio_driver(drv);
701 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
702}
703
704static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
705{
706 struct serio_driver *serio_drv = to_serio_driver(drv);
707 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
708}
709
710static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
711{
712 struct serio_driver *serio_drv = to_serio_driver(drv);
713 int retval;
714
715 retval = count;
716 if (!strncmp(buf, "manual", count)) {
717 serio_drv->manual_bind = 1;
718 } else if (!strncmp(buf, "auto", count)) {
719 serio_drv->manual_bind = 0;
720 } else {
721 retval = -EINVAL;
722 }
723
724 return retval;
725}
726
727
728static struct driver_attribute serio_driver_attrs[] = {
729 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
730 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
731 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
732 __ATTR_NULL
733};
734
735static int serio_driver_probe(struct device *dev)
736{
737 struct serio *serio = to_serio_port(dev);
738 struct serio_driver *drv = to_serio_driver(dev->driver);
739
Linus Torvalds3c803e82005-06-27 17:49:45 -0700740 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static int serio_driver_remove(struct device *dev)
744{
745 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Linus Torvalds3c803e82005-06-27 17:49:45 -0700747 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
Russell King30226f82006-01-05 14:38:53 +0000751static struct bus_type serio_bus = {
752 .name = "serio",
753 .probe = serio_driver_probe,
754 .remove = serio_driver_remove,
755};
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757void __serio_register_driver(struct serio_driver *drv, struct module *owner)
758{
759 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
762}
763
764void serio_unregister_driver(struct serio_driver *drv)
765{
766 struct serio *serio;
767
768 down(&serio_sem);
769 drv->manual_bind = 1; /* so serio_find_driver ignores it */
770
771start_over:
772 list_for_each_entry(serio, &serio_list, node) {
773 if (serio->drv == drv) {
774 serio_disconnect_port(serio);
775 serio_find_driver(serio);
776 /* we could've deleted some ports, restart */
777 goto start_over;
778 }
779 }
780
781 driver_unregister(&drv->driver);
782 up(&serio_sem);
783}
784
785static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 serio_pause_rx(serio);
788 serio->drv = drv;
789 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static int serio_bus_match(struct device *dev, struct device_driver *drv)
793{
794 struct serio *serio = to_serio_port(dev);
795 struct serio_driver *serio_drv = to_serio_driver(drv);
796
797 if (serio->manual_bind || serio_drv->manual_bind)
798 return 0;
799
800 return serio_match_port(serio_drv->id_table, serio);
801}
802
803#ifdef CONFIG_HOTPLUG
804
Kay Sievers312c0042005-11-16 09:00:00 +0100805#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500806 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100807 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500808 buffer, buffer_size, &len, \
809 fmt, val); \
810 if (err) \
811 return err; \
812 } while (0)
813
Kay Sievers312c0042005-11-16 09:00:00 +0100814static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct serio *serio;
817 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500818 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!dev)
821 return -ENODEV;
822
823 serio = to_serio_port(dev);
824
Kay Sievers312c0042005-11-16 09:00:00 +0100825 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
826 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
827 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
828 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
829 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500830 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 envp[i] = NULL;
832
833 return 0;
834}
Kay Sievers312c0042005-11-16 09:00:00 +0100835#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837#else
838
Kay Sievers312c0042005-11-16 09:00:00 +0100839static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 return -ENODEV;
842}
843
844#endif /* CONFIG_HOTPLUG */
845
846static int serio_resume(struct device *dev)
847{
848 struct serio *serio = to_serio_port(dev);
849
Linus Torvalds3c803e82005-06-27 17:49:45 -0700850 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /*
852 * Driver re-probing can take a while, so better let kseriod
853 * deal with it.
854 */
855 serio_rescan(serio);
856 }
857
858 return 0;
859}
860
861/* called from serio_driver->connect/disconnect methods under serio_sem */
862int serio_open(struct serio *serio, struct serio_driver *drv)
863{
864 serio_set_drv(serio, drv);
865
866 if (serio->open && serio->open(serio)) {
867 serio_set_drv(serio, NULL);
868 return -1;
869 }
870 return 0;
871}
872
873/* called from serio_driver->connect/disconnect methods under serio_sem */
874void serio_close(struct serio *serio)
875{
876 if (serio->close)
877 serio->close(serio);
878
879 serio_set_drv(serio, NULL);
880}
881
882irqreturn_t serio_interrupt(struct serio *serio,
883 unsigned char data, unsigned int dfl, struct pt_regs *regs)
884{
885 unsigned long flags;
886 irqreturn_t ret = IRQ_NONE;
887
888 spin_lock_irqsave(&serio->lock, flags);
889
890 if (likely(serio->drv)) {
891 ret = serio->drv->interrupt(serio, data, dfl, regs);
892 } else if (!dfl && serio->registered) {
893 serio_rescan(serio);
894 ret = IRQ_HANDLED;
895 }
896
897 spin_unlock_irqrestore(&serio->lock, flags);
898
899 return ret;
900}
901
902static int __init serio_init(void)
903{
Linus Torvalds3c803e82005-06-27 17:49:45 -0700904 serio_task = kthread_run(serio_thread, NULL, "kseriod");
905 if (IS_ERR(serio_task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 printk(KERN_ERR "serio: Failed to start kseriod\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700907 return PTR_ERR(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
910 serio_bus.dev_attrs = serio_device_attrs;
911 serio_bus.drv_attrs = serio_driver_attrs;
912 serio_bus.match = serio_bus_match;
Kay Sievers312c0042005-11-16 09:00:00 +0100913 serio_bus.uevent = serio_uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 serio_bus.resume = serio_resume;
915 bus_register(&serio_bus);
916
917 return 0;
918}
919
920static void __exit serio_exit(void)
921{
922 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700923 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926module_init(serio_init);
927module_exit(serio_exit);