blob: f367695e69b5c78e35a66ca731369f3e67b0550f [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
62static struct bus_type serio_bus = {
63 .name = "serio",
64};
65
66static void serio_add_port(struct serio *serio);
67static void serio_destroy_port(struct serio *serio);
68static void serio_reconnect_port(struct serio *serio);
69static void serio_disconnect_port(struct serio *serio);
70
Linus Torvalds3c803e82005-06-27 17:49:45 -070071static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
72{
73 int retval;
74
75 down(&serio->drv_sem);
76 retval = drv->connect(serio, drv);
77 up(&serio->drv_sem);
78
79 return retval;
80}
81
82static int serio_reconnect_driver(struct serio *serio)
83{
84 int retval = -1;
85
86 down(&serio->drv_sem);
87 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
89 up(&serio->drv_sem);
90
91 return retval;
92}
93
94static void serio_disconnect_driver(struct serio *serio)
95{
96 down(&serio->drv_sem);
97 if (serio->drv)
98 serio->drv->disconnect(serio);
99 up(&serio->drv_sem);
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
103{
104 while (ids->type || ids->proto) {
105 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
106 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
107 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
108 (ids->id == SERIO_ANY || ids->id == serio->id.id))
109 return 1;
110 ids++;
111 }
112 return 0;
113}
114
115/*
116 * Basic serio -> driver core mappings
117 */
118
119static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
120{
121 down_write(&serio_bus.subsys.rwsem);
122
123 if (serio_match_port(drv->id_table, serio)) {
124 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700125 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 serio->dev.driver = NULL;
127 goto out;
128 }
129 device_bind_driver(&serio->dev);
130 }
131out:
132 up_write(&serio_bus.subsys.rwsem);
133}
134
135static void serio_release_driver(struct serio *serio)
136{
137 down_write(&serio_bus.subsys.rwsem);
138 device_release_driver(&serio->dev);
139 up_write(&serio_bus.subsys.rwsem);
140}
141
142static void serio_find_driver(struct serio *serio)
143{
144 down_write(&serio_bus.subsys.rwsem);
145 device_attach(&serio->dev);
146 up_write(&serio_bus.subsys.rwsem);
147}
148
149
150/*
151 * Serio event processing.
152 */
153
154enum serio_event_type {
155 SERIO_RESCAN,
156 SERIO_RECONNECT,
157 SERIO_REGISTER_PORT,
158 SERIO_UNREGISTER_PORT,
159 SERIO_REGISTER_DRIVER,
160};
161
162struct serio_event {
163 enum serio_event_type type;
164 void *object;
165 struct module *owner;
166 struct list_head node;
167};
168
169static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
170static LIST_HEAD(serio_event_list);
171static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700172static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174static void serio_queue_event(void *object, struct module *owner,
175 enum serio_event_type event_type)
176{
177 unsigned long flags;
178 struct serio_event *event;
179
180 spin_lock_irqsave(&serio_event_lock, flags);
181
182 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700183 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * starting with the most recent one. If event is the same we
185 * do not need add new one. If event is of different type we
186 * need to add this event and should not look further because
187 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 list_for_each_entry_reverse(event, &serio_event_list, node) {
190 if (event->object == object) {
191 if (event->type == event_type)
192 goto out;
193 break;
194 }
195 }
196
197 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
198 if (!try_module_get(owner)) {
199 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
200 goto out;
201 }
202
203 event->type = event_type;
204 event->object = object;
205 event->owner = owner;
206
207 list_add_tail(&event->node, &serio_event_list);
208 wake_up(&serio_wait);
209 } else {
210 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
211 }
212out:
213 spin_unlock_irqrestore(&serio_event_lock, flags);
214}
215
216static void serio_free_event(struct serio_event *event)
217{
218 module_put(event->owner);
219 kfree(event);
220}
221
222static void serio_remove_duplicate_events(struct serio_event *event)
223{
224 struct list_head *node, *next;
225 struct serio_event *e;
226 unsigned long flags;
227
228 spin_lock_irqsave(&serio_event_lock, flags);
229
230 list_for_each_safe(node, next, &serio_event_list) {
231 e = list_entry(node, struct serio_event, node);
232 if (event->object == e->object) {
233 /*
234 * If this event is of different type we should not
235 * look further - we only suppress duplicate events
236 * that were sent back-to-back.
237 */
238 if (event->type != e->type)
239 break;
240
241 list_del_init(node);
242 serio_free_event(e);
243 }
244 }
245
246 spin_unlock_irqrestore(&serio_event_lock, flags);
247}
248
249
250static struct serio_event *serio_get_event(void)
251{
252 struct serio_event *event;
253 struct list_head *node;
254 unsigned long flags;
255
256 spin_lock_irqsave(&serio_event_lock, flags);
257
258 if (list_empty(&serio_event_list)) {
259 spin_unlock_irqrestore(&serio_event_lock, flags);
260 return NULL;
261 }
262
263 node = serio_event_list.next;
264 event = list_entry(node, struct serio_event, node);
265 list_del_init(node);
266
267 spin_unlock_irqrestore(&serio_event_lock, flags);
268
269 return event;
270}
271
272static void serio_handle_events(void)
273{
274 struct serio_event *event;
275 struct serio_driver *serio_drv;
276
277 down(&serio_sem);
278
279 while ((event = serio_get_event())) {
280
281 switch (event->type) {
282 case SERIO_REGISTER_PORT:
283 serio_add_port(event->object);
284 break;
285
286 case SERIO_UNREGISTER_PORT:
287 serio_disconnect_port(event->object);
288 serio_destroy_port(event->object);
289 break;
290
291 case SERIO_RECONNECT:
292 serio_reconnect_port(event->object);
293 break;
294
295 case SERIO_RESCAN:
296 serio_disconnect_port(event->object);
297 serio_find_driver(event->object);
298 break;
299
300 case SERIO_REGISTER_DRIVER:
301 serio_drv = event->object;
302 driver_register(&serio_drv->driver);
303 break;
304
305 default:
306 break;
307 }
308
309 serio_remove_duplicate_events(event);
310 serio_free_event(event);
311 }
312
313 up(&serio_sem);
314}
315
316/*
317 * Remove all events that have been submitted for a given serio port.
318 */
319static void serio_remove_pending_events(struct serio *serio)
320{
321 struct list_head *node, *next;
322 struct serio_event *event;
323 unsigned long flags;
324
325 spin_lock_irqsave(&serio_event_lock, flags);
326
327 list_for_each_safe(node, next, &serio_event_list) {
328 event = list_entry(node, struct serio_event, node);
329 if (event->object == serio) {
330 list_del_init(node);
331 serio_free_event(event);
332 }
333 }
334
335 spin_unlock_irqrestore(&serio_event_lock, flags);
336}
337
338/*
339 * Destroy child serio port (if any) that has not been fully registered yet.
340 *
341 * Note that we rely on the fact that port can have only one child and therefore
342 * only one child registration request can be pending. Additionally, children
343 * are registered by driver's connect() handler so there can't be a grandchild
344 * pending registration together with a child.
345 */
346static struct serio *serio_get_pending_child(struct serio *parent)
347{
348 struct serio_event *event;
349 struct serio *serio, *child = NULL;
350 unsigned long flags;
351
352 spin_lock_irqsave(&serio_event_lock, flags);
353
354 list_for_each_entry(event, &serio_event_list, node) {
355 if (event->type == SERIO_REGISTER_PORT) {
356 serio = event->object;
357 if (serio->parent == parent) {
358 child = serio;
359 break;
360 }
361 }
362 }
363
364 spin_unlock_irqrestore(&serio_event_lock, flags);
365 return child;
366}
367
368static int serio_thread(void *nothing)
369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 do {
371 serio_handle_events();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700372 wait_event_interruptible(serio_wait,
373 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700374 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700375 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381
382/*
383 * Serio port operations
384 */
385
Yani Ioannoue404e272005-05-17 06:42:58 -0400386static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct serio *serio = to_serio_port(dev);
389 return sprintf(buf, "%s\n", serio->name);
390}
391
Yani Ioannoue404e272005-05-17 06:42:58 -0400392static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct serio *serio = to_serio_port(dev);
395 return sprintf(buf, "%02x\n", serio->id.type);
396}
397
Yani Ioannoue404e272005-05-17 06:42:58 -0400398static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct serio *serio = to_serio_port(dev);
401 return sprintf(buf, "%02x\n", serio->id.proto);
402}
403
Yani Ioannoue404e272005-05-17 06:42:58 -0400404static ssize_t serio_show_id_id(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.id);
408}
409
Yani Ioannoue404e272005-05-17 06:42:58 -0400410static ssize_t serio_show_id_extra(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.extra);
414}
415
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700416static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
417static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
418static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
419static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
420
421static struct attribute *serio_device_id_attrs[] = {
422 &dev_attr_type.attr,
423 &dev_attr_proto.attr,
424 &dev_attr_id.attr,
425 &dev_attr_extra.attr,
426 NULL
427};
428
429static struct attribute_group serio_id_attr_group = {
430 .name = "id",
431 .attrs = serio_device_id_attrs,
432};
433
Yani Ioannoue404e272005-05-17 06:42:58 -0400434static 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 -0700435{
436 struct serio *serio = to_serio_port(dev);
437 struct device_driver *drv;
438 int retval;
439
440 retval = down_interruptible(&serio_sem);
441 if (retval)
442 return retval;
443
444 retval = count;
445 if (!strncmp(buf, "none", count)) {
446 serio_disconnect_port(serio);
447 } else if (!strncmp(buf, "reconnect", count)) {
448 serio_reconnect_port(serio);
449 } else if (!strncmp(buf, "rescan", count)) {
450 serio_disconnect_port(serio);
451 serio_find_driver(serio);
452 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
453 serio_disconnect_port(serio);
454 serio_bind_driver(serio, to_serio_driver(drv));
455 put_driver(drv);
456 } else {
457 retval = -EINVAL;
458 }
459
460 up(&serio_sem);
461
462 return retval;
463}
464
Yani Ioannoue404e272005-05-17 06:42:58 -0400465static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct serio *serio = to_serio_port(dev);
468 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
469}
470
Yani Ioannoue404e272005-05-17 06:42:58 -0400471static 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 -0700472{
473 struct serio *serio = to_serio_port(dev);
474 int retval;
475
476 retval = count;
477 if (!strncmp(buf, "manual", count)) {
478 serio->manual_bind = 1;
479 } else if (!strncmp(buf, "auto", count)) {
480 serio->manual_bind = 0;
481 } else {
482 retval = -EINVAL;
483 }
484
485 return retval;
486}
487
488static struct device_attribute serio_device_attrs[] = {
489 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
491 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
492 __ATTR_NULL
493};
494
495
496static void serio_release_port(struct device *dev)
497{
498 struct serio *serio = to_serio_port(dev);
499
500 kfree(serio);
501 module_put(THIS_MODULE);
502}
503
504/*
505 * Prepare serio port for registration.
506 */
507static void serio_init_port(struct serio *serio)
508{
509 static atomic_t serio_no = ATOMIC_INIT(0);
510
511 __module_get(THIS_MODULE);
512
513 spin_lock_init(&serio->lock);
514 init_MUTEX(&serio->drv_sem);
515 device_initialize(&serio->dev);
516 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
517 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
518 serio->dev.bus = &serio_bus;
519 serio->dev.release = serio_release_port;
520 if (serio->parent)
521 serio->dev.parent = &serio->parent->dev;
522}
523
524/*
525 * Complete serio port registration.
526 * Driver core will attempt to find appropriate driver for the port.
527 */
528static void serio_add_port(struct serio *serio)
529{
530 if (serio->parent) {
531 serio_pause_rx(serio->parent);
532 serio->parent->child = serio;
533 serio_continue_rx(serio->parent);
534 }
535
536 list_add_tail(&serio->node, &serio_list);
537 if (serio->start)
538 serio->start(serio);
539 device_add(&serio->dev);
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700540 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 serio->registered = 1;
542}
543
544/*
545 * serio_destroy_port() completes deregistration process and removes
546 * port from the system
547 */
548static void serio_destroy_port(struct serio *serio)
549{
550 struct serio *child;
551
552 child = serio_get_pending_child(serio);
553 if (child) {
554 serio_remove_pending_events(child);
555 put_device(&child->dev);
556 }
557
558 if (serio->stop)
559 serio->stop(serio);
560
561 if (serio->parent) {
562 serio_pause_rx(serio->parent);
563 serio->parent->child = NULL;
564 serio_continue_rx(serio->parent);
565 serio->parent = NULL;
566 }
567
568 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700569 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 device_del(&serio->dev);
571 list_del_init(&serio->node);
572 serio->registered = 0;
573 }
574
575 serio_remove_pending_events(serio);
576 put_device(&serio->dev);
577}
578
579/*
580 * Reconnect serio port and all its children (re-initialize attached devices)
581 */
582static void serio_reconnect_port(struct serio *serio)
583{
584 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700585 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 serio_disconnect_port(serio);
587 serio_find_driver(serio);
588 /* Ok, old children are now gone, we are done */
589 break;
590 }
591 serio = serio->child;
592 } while (serio);
593}
594
595/*
596 * serio_disconnect_port() unbinds a port from its driver. As a side effect
597 * all child ports are unbound and destroyed.
598 */
599static void serio_disconnect_port(struct serio *serio)
600{
601 struct serio *s, *parent;
602
603 if (serio->child) {
604 /*
605 * Children ports should be disconnected and destroyed
606 * first, staring with the leaf one, since we don't want
607 * to do recursion
608 */
609 for (s = serio; s->child; s = s->child)
610 /* empty */;
611
612 do {
613 parent = s->parent;
614
615 serio_release_driver(s);
616 serio_destroy_port(s);
617 } while ((s = parent) != serio);
618 }
619
620 /*
621 * Ok, no children left, now disconnect this port
622 */
623 serio_release_driver(serio);
624}
625
626void serio_rescan(struct serio *serio)
627{
628 serio_queue_event(serio, NULL, SERIO_RESCAN);
629}
630
631void serio_reconnect(struct serio *serio)
632{
633 serio_queue_event(serio, NULL, SERIO_RECONNECT);
634}
635
636/*
637 * Submits register request to kseriod for subsequent execution.
638 * Note that port registration is always asynchronous.
639 */
640void __serio_register_port(struct serio *serio, struct module *owner)
641{
642 serio_init_port(serio);
643 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
644}
645
646/*
647 * Synchronously unregisters serio port.
648 */
649void serio_unregister_port(struct serio *serio)
650{
651 down(&serio_sem);
652 serio_disconnect_port(serio);
653 serio_destroy_port(serio);
654 up(&serio_sem);
655}
656
657/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700658 * Safely unregisters child port if one is present.
659 */
660void serio_unregister_child_port(struct serio *serio)
661{
662 down(&serio_sem);
663 if (serio->child) {
664 serio_disconnect_port(serio->child);
665 serio_destroy_port(serio->child);
666 }
667 up(&serio_sem);
668}
669
670/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * Submits register request to kseriod for subsequent execution.
672 * Can be used when it is not obvious whether the serio_sem is
673 * taken or not and when delayed execution is feasible.
674 */
675void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
676{
677 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
678}
679
680
681/*
682 * Serio driver operations
683 */
684
685static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
686{
687 struct serio_driver *driver = to_serio_driver(drv);
688 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
689}
690
691static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
692{
693 struct serio_driver *serio_drv = to_serio_driver(drv);
694 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
695}
696
697static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
698{
699 struct serio_driver *serio_drv = to_serio_driver(drv);
700 int retval;
701
702 retval = count;
703 if (!strncmp(buf, "manual", count)) {
704 serio_drv->manual_bind = 1;
705 } else if (!strncmp(buf, "auto", count)) {
706 serio_drv->manual_bind = 0;
707 } else {
708 retval = -EINVAL;
709 }
710
711 return retval;
712}
713
714
715static struct driver_attribute serio_driver_attrs[] = {
716 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
717 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
718 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
719 __ATTR_NULL
720};
721
722static int serio_driver_probe(struct device *dev)
723{
724 struct serio *serio = to_serio_port(dev);
725 struct serio_driver *drv = to_serio_driver(dev->driver);
726
Linus Torvalds3c803e82005-06-27 17:49:45 -0700727 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static int serio_driver_remove(struct device *dev)
731{
732 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvalds3c803e82005-06-27 17:49:45 -0700734 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738void __serio_register_driver(struct serio_driver *drv, struct module *owner)
739{
740 drv->driver.bus = &serio_bus;
741 drv->driver.probe = serio_driver_probe;
742 drv->driver.remove = serio_driver_remove;
743
744 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
745}
746
747void serio_unregister_driver(struct serio_driver *drv)
748{
749 struct serio *serio;
750
751 down(&serio_sem);
752 drv->manual_bind = 1; /* so serio_find_driver ignores it */
753
754start_over:
755 list_for_each_entry(serio, &serio_list, node) {
756 if (serio->drv == drv) {
757 serio_disconnect_port(serio);
758 serio_find_driver(serio);
759 /* we could've deleted some ports, restart */
760 goto start_over;
761 }
762 }
763
764 driver_unregister(&drv->driver);
765 up(&serio_sem);
766}
767
768static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
769{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 serio_pause_rx(serio);
771 serio->drv = drv;
772 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775static int serio_bus_match(struct device *dev, struct device_driver *drv)
776{
777 struct serio *serio = to_serio_port(dev);
778 struct serio_driver *serio_drv = to_serio_driver(drv);
779
780 if (serio->manual_bind || serio_drv->manual_bind)
781 return 0;
782
783 return serio_match_port(serio_drv->id_table, serio);
784}
785
786#ifdef CONFIG_HOTPLUG
787
788#define PUT_ENVP(fmt, val) \
789do { \
790 envp[i++] = buffer; \
791 length += snprintf(buffer, buffer_size - length, fmt, val); \
792 if (buffer_size - length <= 0 || i >= num_envp) \
793 return -ENOMEM; \
794 length++; \
795 buffer += length; \
796} while (0)
797static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
798{
799 struct serio *serio;
800 int i = 0;
801 int length = 0;
802
803 if (!dev)
804 return -ENODEV;
805
806 serio = to_serio_port(dev);
807
808 PUT_ENVP("SERIO_TYPE=%02x", serio->id.type);
809 PUT_ENVP("SERIO_PROTO=%02x", serio->id.proto);
810 PUT_ENVP("SERIO_ID=%02x", serio->id.id);
811 PUT_ENVP("SERIO_EXTRA=%02x", serio->id.extra);
812
813 envp[i] = NULL;
814
815 return 0;
816}
817#undef PUT_ENVP
818
819#else
820
821static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
822{
823 return -ENODEV;
824}
825
826#endif /* CONFIG_HOTPLUG */
827
828static int serio_resume(struct device *dev)
829{
830 struct serio *serio = to_serio_port(dev);
831
Linus Torvalds3c803e82005-06-27 17:49:45 -0700832 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * Driver re-probing can take a while, so better let kseriod
835 * deal with it.
836 */
837 serio_rescan(serio);
838 }
839
840 return 0;
841}
842
843/* called from serio_driver->connect/disconnect methods under serio_sem */
844int serio_open(struct serio *serio, struct serio_driver *drv)
845{
846 serio_set_drv(serio, drv);
847
848 if (serio->open && serio->open(serio)) {
849 serio_set_drv(serio, NULL);
850 return -1;
851 }
852 return 0;
853}
854
855/* called from serio_driver->connect/disconnect methods under serio_sem */
856void serio_close(struct serio *serio)
857{
858 if (serio->close)
859 serio->close(serio);
860
861 serio_set_drv(serio, NULL);
862}
863
864irqreturn_t serio_interrupt(struct serio *serio,
865 unsigned char data, unsigned int dfl, struct pt_regs *regs)
866{
867 unsigned long flags;
868 irqreturn_t ret = IRQ_NONE;
869
870 spin_lock_irqsave(&serio->lock, flags);
871
872 if (likely(serio->drv)) {
873 ret = serio->drv->interrupt(serio, data, dfl, regs);
874 } else if (!dfl && serio->registered) {
875 serio_rescan(serio);
876 ret = IRQ_HANDLED;
877 }
878
879 spin_unlock_irqrestore(&serio->lock, flags);
880
881 return ret;
882}
883
884static int __init serio_init(void)
885{
Linus Torvalds3c803e82005-06-27 17:49:45 -0700886 serio_task = kthread_run(serio_thread, NULL, "kseriod");
887 if (IS_ERR(serio_task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 printk(KERN_ERR "serio: Failed to start kseriod\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700889 return PTR_ERR(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 serio_bus.dev_attrs = serio_device_attrs;
893 serio_bus.drv_attrs = serio_driver_attrs;
894 serio_bus.match = serio_bus_match;
895 serio_bus.hotplug = serio_hotplug;
896 serio_bus.resume = serio_resume;
897 bus_register(&serio_bus);
898
899 return 0;
900}
901
902static void __exit serio_exit(void)
903{
904 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700905 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908module_init(serio_init);
909module_exit(serio_exit);