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