Lockdep: fix compile error in drivers/input/serio/serio.c
[deliverable/linux.git] / drivers / input / serio / serio.c
CommitLineData
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>
1da177e4 34#include <linux/sched.h>
1da177e4 35#include <linux/slab.h>
3c803e8e 36#include <linux/kthread.h>
c4e32e9f 37#include <linux/mutex.h>
1da177e4
LT
38
39MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40MODULE_DESCRIPTION("Serio abstraction core");
41MODULE_LICENSE("GPL");
42
43EXPORT_SYMBOL(serio_interrupt);
44EXPORT_SYMBOL(__serio_register_port);
45EXPORT_SYMBOL(serio_unregister_port);
3c803e8e 46EXPORT_SYMBOL(serio_unregister_child_port);
1da177e4
LT
47EXPORT_SYMBOL(__serio_unregister_port_delayed);
48EXPORT_SYMBOL(__serio_register_driver);
49EXPORT_SYMBOL(serio_unregister_driver);
50EXPORT_SYMBOL(serio_open);
51EXPORT_SYMBOL(serio_close);
52EXPORT_SYMBOL(serio_rescan);
53EXPORT_SYMBOL(serio_reconnect);
54
55/*
c4e32e9f 56 * serio_mutex protects entire serio subsystem and is taken every time
1da177e4
LT
57 * serio port or driver registrered or unregistered.
58 */
c4e32e9f 59static DEFINE_MUTEX(serio_mutex);
1da177e4
LT
60
61static LIST_HEAD(serio_list);
62
30226f81 63static struct bus_type serio_bus;
1da177e4 64
73b59a3b 65static void serio_add_driver(struct serio_driver *drv);
1da177e4
LT
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
3c803e8e
LT
71static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
72{
73 int retval;
74
c4e32e9f 75 mutex_lock(&serio->drv_mutex);
3c803e8e 76 retval = drv->connect(serio, drv);
c4e32e9f 77 mutex_unlock(&serio->drv_mutex);
3c803e8e
LT
78
79 return retval;
80}
81
82static int serio_reconnect_driver(struct serio *serio)
83{
84 int retval = -1;
85
c4e32e9f 86 mutex_lock(&serio->drv_mutex);
3c803e8e
LT
87 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
c4e32e9f 89 mutex_unlock(&serio->drv_mutex);
3c803e8e
LT
90
91 return retval;
92}
93
94static void serio_disconnect_driver(struct serio *serio)
95{
c4e32e9f 96 mutex_lock(&serio->drv_mutex);
3c803e8e
LT
97 if (serio->drv)
98 serio->drv->disconnect(serio);
c4e32e9f 99 mutex_unlock(&serio->drv_mutex);
3c803e8e
LT
100}
101
1da177e4
LT
102static 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;
3c803e8e 125 if (serio_connect_driver(serio, drv)) {
1da177e4
LT
126 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{
73b59a3b
RD
144 int error;
145
1da177e4 146 down_write(&serio_bus.subsys.rwsem);
73b59a3b
RD
147 error = device_attach(&serio->dev);
148 if (error < 0)
149 printk(KERN_WARNING
150 "serio: device_attach() failed for %s (%s), error: %d\n",
151 serio->phys, serio->name, error);
1da177e4
LT
152 up_write(&serio_bus.subsys.rwsem);
153}
154
155
156/*
157 * Serio event processing.
158 */
159
160enum serio_event_type {
161 SERIO_RESCAN,
162 SERIO_RECONNECT,
163 SERIO_REGISTER_PORT,
164 SERIO_UNREGISTER_PORT,
165 SERIO_REGISTER_DRIVER,
166};
167
168struct serio_event {
169 enum serio_event_type type;
170 void *object;
171 struct module *owner;
172 struct list_head node;
173};
174
175static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
176static LIST_HEAD(serio_event_list);
177static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
3c803e8e 178static struct task_struct *serio_task;
1da177e4
LT
179
180static void serio_queue_event(void *object, struct module *owner,
181 enum serio_event_type event_type)
182{
183 unsigned long flags;
184 struct serio_event *event;
185
186 spin_lock_irqsave(&serio_event_lock, flags);
187
188 /*
3c803e8e 189 * Scan event list for the other events for the same serio port,
1da177e4
LT
190 * starting with the most recent one. If event is the same we
191 * do not need add new one. If event is of different type we
192 * need to add this event and should not look further because
193 * we need to preseve sequence of distinct events.
3c803e8e 194 */
1da177e4
LT
195 list_for_each_entry_reverse(event, &serio_event_list, node) {
196 if (event->object == object) {
197 if (event->type == event_type)
198 goto out;
199 break;
200 }
201 }
202
203 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
204 if (!try_module_get(owner)) {
205 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
9d921116 206 kfree(event);
1da177e4
LT
207 goto out;
208 }
209
210 event->type = event_type;
211 event->object = object;
212 event->owner = owner;
213
214 list_add_tail(&event->node, &serio_event_list);
215 wake_up(&serio_wait);
216 } else {
217 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
218 }
219out:
220 spin_unlock_irqrestore(&serio_event_lock, flags);
221}
222
223static void serio_free_event(struct serio_event *event)
224{
225 module_put(event->owner);
226 kfree(event);
227}
228
229static void serio_remove_duplicate_events(struct serio_event *event)
230{
231 struct list_head *node, *next;
232 struct serio_event *e;
233 unsigned long flags;
234
235 spin_lock_irqsave(&serio_event_lock, flags);
236
237 list_for_each_safe(node, next, &serio_event_list) {
238 e = list_entry(node, struct serio_event, node);
239 if (event->object == e->object) {
240 /*
241 * If this event is of different type we should not
242 * look further - we only suppress duplicate events
243 * that were sent back-to-back.
244 */
245 if (event->type != e->type)
246 break;
247
248 list_del_init(node);
249 serio_free_event(e);
250 }
251 }
252
253 spin_unlock_irqrestore(&serio_event_lock, flags);
254}
255
256
257static struct serio_event *serio_get_event(void)
258{
259 struct serio_event *event;
260 struct list_head *node;
261 unsigned long flags;
262
263 spin_lock_irqsave(&serio_event_lock, flags);
264
265 if (list_empty(&serio_event_list)) {
266 spin_unlock_irqrestore(&serio_event_lock, flags);
267 return NULL;
268 }
269
270 node = serio_event_list.next;
271 event = list_entry(node, struct serio_event, node);
272 list_del_init(node);
273
274 spin_unlock_irqrestore(&serio_event_lock, flags);
275
276 return event;
277}
278
9e50afd0 279static void serio_handle_event(void)
1da177e4
LT
280{
281 struct serio_event *event;
1da177e4 282
c4e32e9f 283 mutex_lock(&serio_mutex);
1da177e4 284
9e50afd0
DT
285 /*
286 * Note that we handle only one event here to give swsusp
287 * a chance to freeze kseriod thread. Serio events should
288 * be pretty rare so we are not concerned about taking
289 * performance hit.
290 */
291 if ((event = serio_get_event())) {
1da177e4
LT
292
293 switch (event->type) {
294 case SERIO_REGISTER_PORT:
295 serio_add_port(event->object);
296 break;
297
298 case SERIO_UNREGISTER_PORT:
299 serio_disconnect_port(event->object);
300 serio_destroy_port(event->object);
301 break;
302
303 case SERIO_RECONNECT:
304 serio_reconnect_port(event->object);
305 break;
306
307 case SERIO_RESCAN:
308 serio_disconnect_port(event->object);
309 serio_find_driver(event->object);
310 break;
311
312 case SERIO_REGISTER_DRIVER:
73b59a3b 313 serio_add_driver(event->object);
1da177e4
LT
314 break;
315
316 default:
317 break;
318 }
319
320 serio_remove_duplicate_events(event);
321 serio_free_event(event);
322 }
323
c4e32e9f 324 mutex_unlock(&serio_mutex);
1da177e4
LT
325}
326
327/*
328 * Remove all events that have been submitted for a given serio port.
329 */
330static void serio_remove_pending_events(struct serio *serio)
331{
332 struct list_head *node, *next;
333 struct serio_event *event;
334 unsigned long flags;
335
336 spin_lock_irqsave(&serio_event_lock, flags);
337
338 list_for_each_safe(node, next, &serio_event_list) {
339 event = list_entry(node, struct serio_event, node);
340 if (event->object == serio) {
341 list_del_init(node);
342 serio_free_event(event);
343 }
344 }
345
346 spin_unlock_irqrestore(&serio_event_lock, flags);
347}
348
349/*
350 * Destroy child serio port (if any) that has not been fully registered yet.
351 *
352 * Note that we rely on the fact that port can have only one child and therefore
353 * only one child registration request can be pending. Additionally, children
354 * are registered by driver's connect() handler so there can't be a grandchild
355 * pending registration together with a child.
356 */
357static struct serio *serio_get_pending_child(struct serio *parent)
358{
359 struct serio_event *event;
360 struct serio *serio, *child = NULL;
361 unsigned long flags;
362
363 spin_lock_irqsave(&serio_event_lock, flags);
364
365 list_for_each_entry(event, &serio_event_list, node) {
366 if (event->type == SERIO_REGISTER_PORT) {
367 serio = event->object;
368 if (serio->parent == parent) {
369 child = serio;
370 break;
371 }
372 }
373 }
374
375 spin_unlock_irqrestore(&serio_event_lock, flags);
376 return child;
377}
378
379static int serio_thread(void *nothing)
380{
1da177e4 381 do {
9e50afd0 382 serio_handle_event();
3c803e8e
LT
383 wait_event_interruptible(serio_wait,
384 kthread_should_stop() || !list_empty(&serio_event_list));
3e1d1d28 385 try_to_freeze();
3c803e8e 386 } while (!kthread_should_stop());
1da177e4
LT
387
388 printk(KERN_DEBUG "serio: kseriod exiting\n");
3c803e8e 389 return 0;
1da177e4
LT
390}
391
392
393/*
394 * Serio port operations
395 */
396
e404e274 397static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
398{
399 struct serio *serio = to_serio_port(dev);
400 return sprintf(buf, "%s\n", serio->name);
401}
402
ae87dff7
DT
403static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
404{
405 struct serio *serio = to_serio_port(dev);
406
407 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
408 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
409}
410
e404e274 411static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
412{
413 struct serio *serio = to_serio_port(dev);
414 return sprintf(buf, "%02x\n", serio->id.type);
415}
416
e404e274 417static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
418{
419 struct serio *serio = to_serio_port(dev);
420 return sprintf(buf, "%02x\n", serio->id.proto);
421}
422
e404e274 423static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
424{
425 struct serio *serio = to_serio_port(dev);
426 return sprintf(buf, "%02x\n", serio->id.id);
427}
428
e404e274 429static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
430{
431 struct serio *serio = to_serio_port(dev);
432 return sprintf(buf, "%02x\n", serio->id.extra);
433}
434
baae9561
DT
435static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
436static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
437static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
438static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
439
440static struct attribute *serio_device_id_attrs[] = {
441 &dev_attr_type.attr,
442 &dev_attr_proto.attr,
443 &dev_attr_id.attr,
444 &dev_attr_extra.attr,
445 NULL
446};
447
448static struct attribute_group serio_id_attr_group = {
449 .name = "id",
450 .attrs = serio_device_id_attrs,
451};
452
e404e274 453static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
454{
455 struct serio *serio = to_serio_port(dev);
456 struct device_driver *drv;
457 int retval;
458
c4e32e9f 459 retval = mutex_lock_interruptible(&serio_mutex);
1da177e4
LT
460 if (retval)
461 return retval;
462
463 retval = count;
464 if (!strncmp(buf, "none", count)) {
465 serio_disconnect_port(serio);
466 } else if (!strncmp(buf, "reconnect", count)) {
467 serio_reconnect_port(serio);
468 } else if (!strncmp(buf, "rescan", count)) {
469 serio_disconnect_port(serio);
470 serio_find_driver(serio);
471 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
472 serio_disconnect_port(serio);
473 serio_bind_driver(serio, to_serio_driver(drv));
474 put_driver(drv);
475 } else {
476 retval = -EINVAL;
477 }
478
c4e32e9f 479 mutex_unlock(&serio_mutex);
1da177e4
LT
480
481 return retval;
482}
483
e404e274 484static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
485{
486 struct serio *serio = to_serio_port(dev);
487 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
488}
489
e404e274 490static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
491{
492 struct serio *serio = to_serio_port(dev);
493 int retval;
494
495 retval = count;
496 if (!strncmp(buf, "manual", count)) {
497 serio->manual_bind = 1;
498 } else if (!strncmp(buf, "auto", count)) {
499 serio->manual_bind = 0;
500 } else {
501 retval = -EINVAL;
502 }
503
504 return retval;
505}
506
507static struct device_attribute serio_device_attrs[] = {
508 __ATTR(description, S_IRUGO, serio_show_description, NULL),
ae87dff7 509 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
1da177e4
LT
510 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
511 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
512 __ATTR_NULL
513};
514
515
516static void serio_release_port(struct device *dev)
517{
518 struct serio *serio = to_serio_port(dev);
519
520 kfree(serio);
521 module_put(THIS_MODULE);
522}
523
524/*
525 * Prepare serio port for registration.
526 */
527static void serio_init_port(struct serio *serio)
528{
529 static atomic_t serio_no = ATOMIC_INIT(0);
530
531 __module_get(THIS_MODULE);
532
73b59a3b 533 INIT_LIST_HEAD(&serio->node);
1da177e4 534 spin_lock_init(&serio->lock);
c4e32e9f 535 mutex_init(&serio->drv_mutex);
1da177e4
LT
536 device_initialize(&serio->dev);
537 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
538 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
539 serio->dev.bus = &serio_bus;
540 serio->dev.release = serio_release_port;
88aa0103 541 if (serio->parent) {
1da177e4 542 serio->dev.parent = &serio->parent->dev;
88aa0103
JK
543 serio->depth = serio->parent->depth + 1;
544 } else
545 serio->depth = 0;
546 lockdep_set_subclass(&serio->lock, serio->depth);
1da177e4
LT
547}
548
549/*
550 * Complete serio port registration.
551 * Driver core will attempt to find appropriate driver for the port.
552 */
553static void serio_add_port(struct serio *serio)
554{
73b59a3b
RD
555 int error;
556
1da177e4
LT
557 if (serio->parent) {
558 serio_pause_rx(serio->parent);
559 serio->parent->child = serio;
560 serio_continue_rx(serio->parent);
561 }
562
563 list_add_tail(&serio->node, &serio_list);
564 if (serio->start)
565 serio->start(serio);
73b59a3b
RD
566 error = device_add(&serio->dev);
567 if (error)
568 printk(KERN_ERR
569 "serio: device_add() failed for %s (%s), error: %d\n",
570 serio->phys, serio->name, error);
571 else {
572 serio->registered = 1;
573 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
574 if (error)
575 printk(KERN_ERR
576 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
577 serio->phys, serio->name, error);
578 }
1da177e4
LT
579}
580
581/*
582 * serio_destroy_port() completes deregistration process and removes
583 * port from the system
584 */
585static void serio_destroy_port(struct serio *serio)
586{
587 struct serio *child;
588
589 child = serio_get_pending_child(serio);
590 if (child) {
591 serio_remove_pending_events(child);
592 put_device(&child->dev);
593 }
594
595 if (serio->stop)
596 serio->stop(serio);
597
598 if (serio->parent) {
599 serio_pause_rx(serio->parent);
600 serio->parent->child = NULL;
601 serio_continue_rx(serio->parent);
602 serio->parent = NULL;
603 }
604
605 if (serio->registered) {
baae9561 606 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
1da177e4 607 device_del(&serio->dev);
1da177e4
LT
608 serio->registered = 0;
609 }
610
73b59a3b 611 list_del_init(&serio->node);
1da177e4
LT
612 serio_remove_pending_events(serio);
613 put_device(&serio->dev);
614}
615
616/*
617 * Reconnect serio port and all its children (re-initialize attached devices)
618 */
619static void serio_reconnect_port(struct serio *serio)
620{
621 do {
3c803e8e 622 if (serio_reconnect_driver(serio)) {
1da177e4
LT
623 serio_disconnect_port(serio);
624 serio_find_driver(serio);
625 /* Ok, old children are now gone, we are done */
626 break;
627 }
628 serio = serio->child;
629 } while (serio);
630}
631
632/*
633 * serio_disconnect_port() unbinds a port from its driver. As a side effect
634 * all child ports are unbound and destroyed.
635 */
636static void serio_disconnect_port(struct serio *serio)
637{
638 struct serio *s, *parent;
639
640 if (serio->child) {
641 /*
642 * Children ports should be disconnected and destroyed
643 * first, staring with the leaf one, since we don't want
644 * to do recursion
645 */
646 for (s = serio; s->child; s = s->child)
647 /* empty */;
648
649 do {
650 parent = s->parent;
651
652 serio_release_driver(s);
653 serio_destroy_port(s);
654 } while ((s = parent) != serio);
655 }
656
657 /*
658 * Ok, no children left, now disconnect this port
659 */
660 serio_release_driver(serio);
661}
662
663void serio_rescan(struct serio *serio)
664{
665 serio_queue_event(serio, NULL, SERIO_RESCAN);
666}
667
668void serio_reconnect(struct serio *serio)
669{
670 serio_queue_event(serio, NULL, SERIO_RECONNECT);
671}
672
673/*
674 * Submits register request to kseriod for subsequent execution.
675 * Note that port registration is always asynchronous.
676 */
677void __serio_register_port(struct serio *serio, struct module *owner)
678{
679 serio_init_port(serio);
680 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
681}
682
683/*
684 * Synchronously unregisters serio port.
685 */
686void serio_unregister_port(struct serio *serio)
687{
c4e32e9f 688 mutex_lock(&serio_mutex);
1da177e4
LT
689 serio_disconnect_port(serio);
690 serio_destroy_port(serio);
c4e32e9f 691 mutex_unlock(&serio_mutex);
1da177e4
LT
692}
693
3c803e8e
LT
694/*
695 * Safely unregisters child port if one is present.
696 */
697void serio_unregister_child_port(struct serio *serio)
698{
c4e32e9f 699 mutex_lock(&serio_mutex);
3c803e8e
LT
700 if (serio->child) {
701 serio_disconnect_port(serio->child);
702 serio_destroy_port(serio->child);
703 }
c4e32e9f 704 mutex_unlock(&serio_mutex);
3c803e8e
LT
705}
706
1da177e4
LT
707/*
708 * Submits register request to kseriod for subsequent execution.
c4e32e9f 709 * Can be used when it is not obvious whether the serio_mutex is
1da177e4
LT
710 * taken or not and when delayed execution is feasible.
711 */
712void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
713{
714 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
715}
716
717
718/*
719 * Serio driver operations
720 */
721
722static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
723{
724 struct serio_driver *driver = to_serio_driver(drv);
725 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
726}
727
728static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
729{
730 struct serio_driver *serio_drv = to_serio_driver(drv);
731 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
732}
733
734static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
735{
736 struct serio_driver *serio_drv = to_serio_driver(drv);
737 int retval;
738
739 retval = count;
740 if (!strncmp(buf, "manual", count)) {
741 serio_drv->manual_bind = 1;
742 } else if (!strncmp(buf, "auto", count)) {
743 serio_drv->manual_bind = 0;
744 } else {
745 retval = -EINVAL;
746 }
747
748 return retval;
749}
750
751
752static struct driver_attribute serio_driver_attrs[] = {
753 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
754 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
755 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
756 __ATTR_NULL
757};
758
759static int serio_driver_probe(struct device *dev)
760{
761 struct serio *serio = to_serio_port(dev);
762 struct serio_driver *drv = to_serio_driver(dev->driver);
763
3c803e8e 764 return serio_connect_driver(serio, drv);
1da177e4
LT
765}
766
767static int serio_driver_remove(struct device *dev)
768{
769 struct serio *serio = to_serio_port(dev);
1da177e4 770
3c803e8e 771 serio_disconnect_driver(serio);
1da177e4
LT
772 return 0;
773}
774
30226f81
RK
775static struct bus_type serio_bus = {
776 .name = "serio",
777 .probe = serio_driver_probe,
778 .remove = serio_driver_remove,
779};
780
73b59a3b
RD
781static void serio_add_driver(struct serio_driver *drv)
782{
783 int error;
784
785 error = driver_register(&drv->driver);
786 if (error)
787 printk(KERN_ERR
788 "serio: driver_register() failed for %s, error: %d\n",
789 drv->driver.name, error);
790}
791
1da177e4
LT
792void __serio_register_driver(struct serio_driver *drv, struct module *owner)
793{
794 drv->driver.bus = &serio_bus;
1da177e4
LT
795
796 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
797}
798
799void serio_unregister_driver(struct serio_driver *drv)
800{
801 struct serio *serio;
802
c4e32e9f 803 mutex_lock(&serio_mutex);
1da177e4
LT
804 drv->manual_bind = 1; /* so serio_find_driver ignores it */
805
806start_over:
807 list_for_each_entry(serio, &serio_list, node) {
808 if (serio->drv == drv) {
809 serio_disconnect_port(serio);
810 serio_find_driver(serio);
811 /* we could've deleted some ports, restart */
812 goto start_over;
813 }
814 }
815
816 driver_unregister(&drv->driver);
c4e32e9f 817 mutex_unlock(&serio_mutex);
1da177e4
LT
818}
819
820static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
821{
1da177e4
LT
822 serio_pause_rx(serio);
823 serio->drv = drv;
824 serio_continue_rx(serio);
1da177e4
LT
825}
826
827static int serio_bus_match(struct device *dev, struct device_driver *drv)
828{
829 struct serio *serio = to_serio_port(dev);
830 struct serio_driver *serio_drv = to_serio_driver(drv);
831
832 if (serio->manual_bind || serio_drv->manual_bind)
833 return 0;
834
835 return serio_match_port(serio_drv->id_table, serio);
836}
837
838#ifdef CONFIG_HOTPLUG
839
312c004d 840#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
ae87dff7 841 do { \
312c004d 842 int err = add_uevent_var(envp, num_envp, &i, \
ae87dff7
DT
843 buffer, buffer_size, &len, \
844 fmt, val); \
845 if (err) \
846 return err; \
847 } while (0)
848
312c004d 849static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
1da177e4
LT
850{
851 struct serio *serio;
852 int i = 0;
ae87dff7 853 int len = 0;
1da177e4
LT
854
855 if (!dev)
856 return -ENODEV;
857
858 serio = to_serio_port(dev);
859
312c004d
KS
860 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
861 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
862 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
863 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
864 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
ae87dff7 865 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
1da177e4
LT
866 envp[i] = NULL;
867
868 return 0;
869}
312c004d 870#undef SERIO_ADD_UEVENT_VAR
1da177e4
LT
871
872#else
873
312c004d 874static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
1da177e4
LT
875{
876 return -ENODEV;
877}
878
879#endif /* CONFIG_HOTPLUG */
880
881static int serio_resume(struct device *dev)
882{
883 struct serio *serio = to_serio_port(dev);
884
3c803e8e 885 if (serio_reconnect_driver(serio)) {
1da177e4
LT
886 /*
887 * Driver re-probing can take a while, so better let kseriod
888 * deal with it.
889 */
890 serio_rescan(serio);
891 }
892
893 return 0;
894}
895
c4e32e9f 896/* called from serio_driver->connect/disconnect methods under serio_mutex */
1da177e4
LT
897int serio_open(struct serio *serio, struct serio_driver *drv)
898{
899 serio_set_drv(serio, drv);
900
901 if (serio->open && serio->open(serio)) {
902 serio_set_drv(serio, NULL);
903 return -1;
904 }
905 return 0;
906}
907
c4e32e9f 908/* called from serio_driver->connect/disconnect methods under serio_mutex */
1da177e4
LT
909void serio_close(struct serio *serio)
910{
911 if (serio->close)
912 serio->close(serio);
913
914 serio_set_drv(serio, NULL);
915}
916
917irqreturn_t serio_interrupt(struct serio *serio,
7d12e780 918 unsigned char data, unsigned int dfl)
1da177e4
LT
919{
920 unsigned long flags;
921 irqreturn_t ret = IRQ_NONE;
922
923 spin_lock_irqsave(&serio->lock, flags);
924
925 if (likely(serio->drv)) {
7d12e780 926 ret = serio->drv->interrupt(serio, data, dfl);
1da177e4
LT
927 } else if (!dfl && serio->registered) {
928 serio_rescan(serio);
929 ret = IRQ_HANDLED;
930 }
931
932 spin_unlock_irqrestore(&serio->lock, flags);
933
934 return ret;
935}
936
937static int __init serio_init(void)
938{
73b59a3b 939 int error;
1da177e4
LT
940
941 serio_bus.dev_attrs = serio_device_attrs;
942 serio_bus.drv_attrs = serio_driver_attrs;
943 serio_bus.match = serio_bus_match;
312c004d 944 serio_bus.uevent = serio_uevent;
1da177e4 945 serio_bus.resume = serio_resume;
73b59a3b
RD
946 error = bus_register(&serio_bus);
947 if (error) {
948 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
949 return error;
950 }
951
952 serio_task = kthread_run(serio_thread, NULL, "kseriod");
953 if (IS_ERR(serio_task)) {
954 bus_unregister(&serio_bus);
955 error = PTR_ERR(serio_task);
956 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
957 return error;
958 }
1da177e4
LT
959
960 return 0;
961}
962
963static void __exit serio_exit(void)
964{
965 bus_unregister(&serio_bus);
3c803e8e 966 kthread_stop(serio_task);
1da177e4
LT
967}
968
51c38f9b 969subsys_initcall(serio_init);
1da177e4 970module_exit(serio_exit);
This page took 0.196543 seconds and 5 git commands to generate.