serio.c 24.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 *  The Serio abstraction module
 *
 *  Copyright (c) 1999-2004 Vojtech Pavlik
 *  Copyright (c) 2004 Dmitry Torokhov
 *  Copyright (c) 2003 Daniele Bellucci
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 */

#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/serio.h>
#include <linux/errno.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/slab.h>
36
#include <linux/kthread.h>
37
#include <linux/mutex.h>
38
#include <linux/freezer.h>
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Serio abstraction core");
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(serio_interrupt);
EXPORT_SYMBOL(__serio_register_port);
EXPORT_SYMBOL(serio_unregister_port);
47
EXPORT_SYMBOL(serio_unregister_child_port);
48
EXPORT_SYMBOL(__serio_register_driver);
L
Linus Torvalds 已提交
49 50 51 52 53 54 55
EXPORT_SYMBOL(serio_unregister_driver);
EXPORT_SYMBOL(serio_open);
EXPORT_SYMBOL(serio_close);
EXPORT_SYMBOL(serio_rescan);
EXPORT_SYMBOL(serio_reconnect);

/*
56
 * serio_mutex protects entire serio subsystem and is taken every time
L
Linus Torvalds 已提交
57 58
 * serio port or driver registrered or unregistered.
 */
59
static DEFINE_MUTEX(serio_mutex);
L
Linus Torvalds 已提交
60 61 62

static LIST_HEAD(serio_list);

63
static struct bus_type serio_bus;
L
Linus Torvalds 已提交
64 65 66 67

static void serio_add_port(struct serio *serio);
static void serio_reconnect_port(struct serio *serio);
static void serio_disconnect_port(struct serio *serio);
68
static void serio_attach_driver(struct serio_driver *drv);
L
Linus Torvalds 已提交
69

70 71 72 73
static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
{
	int retval;

74
	mutex_lock(&serio->drv_mutex);
75
	retval = drv->connect(serio, drv);
76
	mutex_unlock(&serio->drv_mutex);
77 78 79 80 81 82 83 84

	return retval;
}

static int serio_reconnect_driver(struct serio *serio)
{
	int retval = -1;

85
	mutex_lock(&serio->drv_mutex);
86 87
	if (serio->drv && serio->drv->reconnect)
		retval = serio->drv->reconnect(serio);
88
	mutex_unlock(&serio->drv_mutex);
89 90 91 92 93 94

	return retval;
}

static void serio_disconnect_driver(struct serio *serio)
{
95
	mutex_lock(&serio->drv_mutex);
96 97
	if (serio->drv)
		serio->drv->disconnect(serio);
98
	mutex_unlock(&serio->drv_mutex);
99 100
}

L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
{
	while (ids->type || ids->proto) {
		if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
		    (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
		    (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
		    (ids->id == SERIO_ANY || ids->id == serio->id.id))
			return 1;
		ids++;
	}
	return 0;
}

/*
 * Basic serio -> driver core mappings
 */

118
static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
L
Linus Torvalds 已提交
119
{
120 121
	int error;

L
Linus Torvalds 已提交
122
	if (serio_match_port(drv->id_table, serio)) {
123

L
Linus Torvalds 已提交
124
		serio->dev.driver = &drv->driver;
125
		if (serio_connect_driver(serio, drv)) {
L
Linus Torvalds 已提交
126
			serio->dev.driver = NULL;
127
			return -ENODEV;
L
Linus Torvalds 已提交
128
		}
129

130 131 132 133 134 135 136 137 138
		error = device_bind_driver(&serio->dev);
		if (error) {
			printk(KERN_WARNING
				"serio: device_bind_driver() failed "
				"for %s (%s) and %s, error: %d\n",
				serio->phys, serio->name,
				drv->description, error);
			serio_disconnect_driver(serio);
			serio->dev.driver = NULL;
139
			return error;
140
		}
L
Linus Torvalds 已提交
141
	}
142
	return 0;
L
Linus Torvalds 已提交
143 144 145 146
}

static void serio_find_driver(struct serio *serio)
{
147 148 149 150 151 152 153
	int error;

	error = device_attach(&serio->dev);
	if (error < 0)
		printk(KERN_WARNING
			"serio: device_attach() failed for %s (%s), error: %d\n",
			serio->phys, serio->name, error);
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161
}


/*
 * Serio event processing.
 */

enum serio_event_type {
162 163
	SERIO_RESCAN_PORT,
	SERIO_RECONNECT_PORT,
L
Linus Torvalds 已提交
164
	SERIO_REGISTER_PORT,
165
	SERIO_ATTACH_DRIVER,
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177
};

struct serio_event {
	enum serio_event_type type;
	void *object;
	struct module *owner;
	struct list_head node;
};

static DEFINE_SPINLOCK(serio_event_lock);	/* protects serio_event_list */
static LIST_HEAD(serio_event_list);
static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
178
static struct task_struct *serio_task;
L
Linus Torvalds 已提交
179

180 181
static int serio_queue_event(void *object, struct module *owner,
			     enum serio_event_type event_type)
L
Linus Torvalds 已提交
182 183 184
{
	unsigned long flags;
	struct serio_event *event;
185
	int retval = 0;
L
Linus Torvalds 已提交
186 187 188 189

	spin_lock_irqsave(&serio_event_lock, flags);

	/*
190
	 * Scan event list for the other events for the same serio port,
L
Linus Torvalds 已提交
191 192 193 194
	 * starting with the most recent one. If event is the same we
	 * do not need add new one. If event is of different type we
	 * need to add this event and should not look further because
	 * we need to preseve sequence of distinct events.
195
	 */
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203
	list_for_each_entry_reverse(event, &serio_event_list, node) {
		if (event->object == object) {
			if (event->type == event_type)
				goto out;
			break;
		}
	}

204 205 206 207 208 209 210 211
	event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
	if (!event) {
		printk(KERN_ERR
			"serio: Not enough memory to queue event %d\n",
			event_type);
		retval = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
212

213 214 215 216 217 218 219
	if (!try_module_get(owner)) {
		printk(KERN_WARNING
			"serio: Can't get module reference, dropping event %d\n",
			event_type);
		kfree(event);
		retval = -EINVAL;
		goto out;
L
Linus Torvalds 已提交
220
	}
221 222 223 224 225 226 227 228

	event->type = event_type;
	event->object = object;
	event->owner = owner;

	list_add_tail(&event->node, &serio_event_list);
	wake_up(&serio_wait);

L
Linus Torvalds 已提交
229 230
out:
	spin_unlock_irqrestore(&serio_event_lock, flags);
231
	return retval;
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

static void serio_free_event(struct serio_event *event)
{
	module_put(event->owner);
	kfree(event);
}

static void serio_remove_duplicate_events(struct serio_event *event)
{
	struct list_head *node, *next;
	struct serio_event *e;
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

	list_for_each_safe(node, next, &serio_event_list) {
		e = list_entry(node, struct serio_event, node);
		if (event->object == e->object) {
			/*
			 * If this event is of different type we should not
			 * look further - we only suppress duplicate events
			 * that were sent back-to-back.
			 */
			if (event->type != e->type)
				break;

			list_del_init(node);
			serio_free_event(e);
		}
	}

	spin_unlock_irqrestore(&serio_event_lock, flags);
}


static struct serio_event *serio_get_event(void)
{
	struct serio_event *event;
	struct list_head *node;
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

	if (list_empty(&serio_event_list)) {
		spin_unlock_irqrestore(&serio_event_lock, flags);
		return NULL;
	}

	node = serio_event_list.next;
	event = list_entry(node, struct serio_event, node);
	list_del_init(node);

	spin_unlock_irqrestore(&serio_event_lock, flags);

	return event;
}

290
static void serio_handle_event(void)
L
Linus Torvalds 已提交
291 292 293
{
	struct serio_event *event;

294
	mutex_lock(&serio_mutex);
L
Linus Torvalds 已提交
295

296 297 298 299 300 301 302
	/*
	 * Note that we handle only one event here to give swsusp
	 * a chance to freeze kseriod thread. Serio events should
	 * be pretty rare so we are not concerned about taking
	 * performance hit.
	 */
	if ((event = serio_get_event())) {
L
Linus Torvalds 已提交
303 304 305 306 307 308

		switch (event->type) {
			case SERIO_REGISTER_PORT:
				serio_add_port(event->object);
				break;

309
			case SERIO_RECONNECT_PORT:
L
Linus Torvalds 已提交
310 311 312
				serio_reconnect_port(event->object);
				break;

313
			case SERIO_RESCAN_PORT:
L
Linus Torvalds 已提交
314 315 316 317
				serio_disconnect_port(event->object);
				serio_find_driver(event->object);
				break;

318 319
			case SERIO_ATTACH_DRIVER:
				serio_attach_driver(event->object);
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329
				break;

			default:
				break;
		}

		serio_remove_duplicate_events(event);
		serio_free_event(event);
	}

330
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

/*
 * Remove all events that have been submitted for a given serio port.
 */
static void serio_remove_pending_events(struct serio *serio)
{
	struct list_head *node, *next;
	struct serio_event *event;
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

	list_for_each_safe(node, next, &serio_event_list) {
		event = list_entry(node, struct serio_event, node);
		if (event->object == serio) {
			list_del_init(node);
			serio_free_event(event);
		}
	}

	spin_unlock_irqrestore(&serio_event_lock, flags);
}

/*
 * Destroy child serio port (if any) that has not been fully registered yet.
 *
 * Note that we rely on the fact that port can have only one child and therefore
 * only one child registration request can be pending. Additionally, children
 * are registered by driver's connect() handler so there can't be a grandchild
 * pending registration together with a child.
 */
static struct serio *serio_get_pending_child(struct serio *parent)
{
	struct serio_event *event;
	struct serio *serio, *child = NULL;
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

	list_for_each_entry(event, &serio_event_list, node) {
		if (event->type == SERIO_REGISTER_PORT) {
			serio = event->object;
			if (serio->parent == parent) {
				child = serio;
				break;
			}
		}
	}

	spin_unlock_irqrestore(&serio_event_lock, flags);
	return child;
}

static int serio_thread(void *nothing)
{
387
	set_freezable();
L
Linus Torvalds 已提交
388
	do {
389
		serio_handle_event();
390 391
		wait_event_interruptible(serio_wait,
			kthread_should_stop() || !list_empty(&serio_event_list));
392
		try_to_freeze();
393
	} while (!kthread_should_stop());
L
Linus Torvalds 已提交
394 395

	printk(KERN_DEBUG "serio: kseriod exiting\n");
396
	return 0;
L
Linus Torvalds 已提交
397 398 399 400 401 402 403
}


/*
 * Serio port operations
 */

404
static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
405 406 407 408 409
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%s\n", serio->name);
}

410 411 412 413 414 415 416 417
static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct serio *serio = to_serio_port(dev);

	return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
			serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
}

418
static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
419 420 421 422 423
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.type);
}

424
static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
425 426 427 428 429
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.proto);
}

430
static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
431 432 433 434 435
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.id);
}

436
static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
437 438 439 440 441
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.extra);
}

D
Dmitry Torokhov 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);

static struct attribute *serio_device_id_attrs[] = {
	&dev_attr_type.attr,
	&dev_attr_proto.attr,
	&dev_attr_id.attr,
	&dev_attr_extra.attr,
	NULL
};

static struct attribute_group serio_id_attr_group = {
	.name	= "id",
	.attrs	= serio_device_id_attrs,
};

460
static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
461 462 463
{
	struct serio *serio = to_serio_port(dev);
	struct device_driver *drv;
464
	int error;
L
Linus Torvalds 已提交
465

466 467 468
	error = mutex_lock_interruptible(&serio_mutex);
	if (error)
		return error;
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478

	if (!strncmp(buf, "none", count)) {
		serio_disconnect_port(serio);
	} else if (!strncmp(buf, "reconnect", count)) {
		serio_reconnect_port(serio);
	} else if (!strncmp(buf, "rescan", count)) {
		serio_disconnect_port(serio);
		serio_find_driver(serio);
	} else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
		serio_disconnect_port(serio);
479
		error = serio_bind_driver(serio, to_serio_driver(drv));
L
Linus Torvalds 已提交
480 481
		put_driver(drv);
	} else {
482
		error = -EINVAL;
L
Linus Torvalds 已提交
483 484
	}

485
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
486

487
	return error ? error : count;
L
Linus Torvalds 已提交
488 489
}

490
static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
491 492 493 494 495
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
}

496
static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
{
	struct serio *serio = to_serio_port(dev);
	int retval;

	retval = count;
	if (!strncmp(buf, "manual", count)) {
		serio->manual_bind = 1;
	} else if (!strncmp(buf, "auto", count)) {
		serio->manual_bind = 0;
	} else {
		retval = -EINVAL;
	}

	return retval;
}

static struct device_attribute serio_device_attrs[] = {
	__ATTR(description, S_IRUGO, serio_show_description, NULL),
515
	__ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	__ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
	__ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
	__ATTR_NULL
};


static void serio_release_port(struct device *dev)
{
	struct serio *serio = to_serio_port(dev);

	kfree(serio);
	module_put(THIS_MODULE);
}

/*
 * Prepare serio port for registration.
 */
static void serio_init_port(struct serio *serio)
{
	static atomic_t serio_no = ATOMIC_INIT(0);

	__module_get(THIS_MODULE);

539
	INIT_LIST_HEAD(&serio->node);
L
Linus Torvalds 已提交
540
	spin_lock_init(&serio->lock);
541
	mutex_init(&serio->drv_mutex);
L
Linus Torvalds 已提交
542 543 544 545 546
	device_initialize(&serio->dev);
	snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
		 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
	serio->dev.bus = &serio_bus;
	serio->dev.release = serio_release_port;
547
	if (serio->parent) {
L
Linus Torvalds 已提交
548
		serio->dev.parent = &serio->parent->dev;
549 550 551 552
		serio->depth = serio->parent->depth + 1;
	} else
		serio->depth = 0;
	lockdep_set_subclass(&serio->lock, serio->depth);
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560
}

/*
 * Complete serio port registration.
 * Driver core will attempt to find appropriate driver for the port.
 */
static void serio_add_port(struct serio *serio)
{
561 562
	int error;

L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571
	if (serio->parent) {
		serio_pause_rx(serio->parent);
		serio->parent->child = serio;
		serio_continue_rx(serio->parent);
	}

	list_add_tail(&serio->node, &serio_list);
	if (serio->start)
		serio->start(serio);
572 573 574 575 576 577 578 579 580 581 582 583 584
	error = device_add(&serio->dev);
	if (error)
		printk(KERN_ERR
			"serio: device_add() failed for %s (%s), error: %d\n",
			serio->phys, serio->name, error);
	else {
		serio->registered = 1;
		error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
		if (error)
			printk(KERN_ERR
				"serio: sysfs_create_group() failed for %s (%s), error: %d\n",
				serio->phys, serio->name, error);
	}
L
Linus Torvalds 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
}

/*
 * serio_destroy_port() completes deregistration process and removes
 * port from the system
 */
static void serio_destroy_port(struct serio *serio)
{
	struct serio *child;

	child = serio_get_pending_child(serio);
	if (child) {
		serio_remove_pending_events(child);
		put_device(&child->dev);
	}

	if (serio->stop)
		serio->stop(serio);

	if (serio->parent) {
		serio_pause_rx(serio->parent);
		serio->parent->child = NULL;
		serio_continue_rx(serio->parent);
		serio->parent = NULL;
	}

	if (serio->registered) {
D
Dmitry Torokhov 已提交
612
		sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
L
Linus Torvalds 已提交
613 614 615 616
		device_del(&serio->dev);
		serio->registered = 0;
	}

617
	list_del_init(&serio->node);
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626 627
	serio_remove_pending_events(serio);
	put_device(&serio->dev);
}

/*
 * Reconnect serio port and all its children (re-initialize attached devices)
 */
static void serio_reconnect_port(struct serio *serio)
{
	do {
628
		if (serio_reconnect_driver(serio)) {
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
			serio_disconnect_port(serio);
			serio_find_driver(serio);
			/* Ok, old children are now gone, we are done */
			break;
		}
		serio = serio->child;
	} while (serio);
}

/*
 * serio_disconnect_port() unbinds a port from its driver. As a side effect
 * all child ports are unbound and destroyed.
 */
static void serio_disconnect_port(struct serio *serio)
{
	struct serio *s, *parent;

	if (serio->child) {
		/*
		 * Children ports should be disconnected and destroyed
		 * first, staring with the leaf one, since we don't want
		 * to do recursion
		 */
		for (s = serio; s->child; s = s->child)
			/* empty */;

		do {
			parent = s->parent;

658
			device_release_driver(&s->dev);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665
			serio_destroy_port(s);
		} while ((s = parent) != serio);
	}

	/*
	 * Ok, no children left, now disconnect this port
	 */
666
	device_release_driver(&serio->dev);
L
Linus Torvalds 已提交
667 668 669 670
}

void serio_rescan(struct serio *serio)
{
671
	serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
L
Linus Torvalds 已提交
672 673 674 675
}

void serio_reconnect(struct serio *serio)
{
676
	serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
}

/*
 * Submits register request to kseriod for subsequent execution.
 * Note that port registration is always asynchronous.
 */
void __serio_register_port(struct serio *serio, struct module *owner)
{
	serio_init_port(serio);
	serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
}

/*
 * Synchronously unregisters serio port.
 */
void serio_unregister_port(struct serio *serio)
{
694
	mutex_lock(&serio_mutex);
L
Linus Torvalds 已提交
695 696
	serio_disconnect_port(serio);
	serio_destroy_port(serio);
697
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
698 699
}

700 701 702 703 704
/*
 * Safely unregisters child port if one is present.
 */
void serio_unregister_child_port(struct serio *serio)
{
705
	mutex_lock(&serio_mutex);
706 707 708 709
	if (serio->child) {
		serio_disconnect_port(serio->child);
		serio_destroy_port(serio->child);
	}
710
	mutex_unlock(&serio_mutex);
711 712
}

L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

/*
 * Serio driver operations
 */

static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
{
	struct serio_driver *driver = to_serio_driver(drv);
	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
}

static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
{
	struct serio_driver *serio_drv = to_serio_driver(drv);
	return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
}

static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
{
	struct serio_driver *serio_drv = to_serio_driver(drv);
	int retval;

	retval = count;
	if (!strncmp(buf, "manual", count)) {
		serio_drv->manual_bind = 1;
	} else if (!strncmp(buf, "auto", count)) {
		serio_drv->manual_bind = 0;
	} else {
		retval = -EINVAL;
	}

	return retval;
}


static struct driver_attribute serio_driver_attrs[] = {
	__ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
	__ATTR(bind_mode, S_IWUSR | S_IRUGO,
		serio_driver_show_bind_mode, serio_driver_set_bind_mode),
	__ATTR_NULL
};

static int serio_driver_probe(struct device *dev)
{
	struct serio *serio = to_serio_port(dev);
	struct serio_driver *drv = to_serio_driver(dev->driver);

760
	return serio_connect_driver(serio, drv);
L
Linus Torvalds 已提交
761 762 763 764 765 766
}

static int serio_driver_remove(struct device *dev)
{
	struct serio *serio = to_serio_port(dev);

767
	serio_disconnect_driver(serio);
L
Linus Torvalds 已提交
768 769 770
	return 0;
}

771 772
static void serio_cleanup(struct serio *serio)
{
773
	mutex_lock(&serio->drv_mutex);
774 775
	if (serio->drv && serio->drv->cleanup)
		serio->drv->cleanup(serio);
776
	mutex_unlock(&serio->drv_mutex);
777 778 779 780 781 782 783 784 785
}

static void serio_shutdown(struct device *dev)
{
	struct serio *serio = to_serio_port(dev);

	serio_cleanup(serio);
}

786
static void serio_attach_driver(struct serio_driver *drv)
787 788 789
{
	int error;

790
	error = driver_attach(&drv->driver);
791
	if (error)
792 793
		printk(KERN_WARNING
			"serio: driver_attach() failed for %s with error %d\n",
794 795 796
			drv->driver.name, error);
}

797
int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
L
Linus Torvalds 已提交
798
{
799 800 801
	int manual_bind = drv->manual_bind;
	int error;

L
Linus Torvalds 已提交
802
	drv->driver.bus = &serio_bus;
803 804
	drv->driver.owner = owner;
	drv->driver.mod_name = mod_name;
L
Linus Torvalds 已提交
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	/*
	 * Temporarily disable automatic binding because probing
	 * takes long time and we are better off doing it in kseriod
	 */
	drv->manual_bind = 1;

	error = driver_register(&drv->driver);
	if (error) {
		printk(KERN_ERR
			"serio: driver_register() failed for %s, error: %d\n",
			drv->driver.name, error);
		return error;
	}

	/*
	 * Restore original bind mode and let kseriod bind the
	 * driver to free ports
	 */
	if (!manual_bind) {
		drv->manual_bind = 0;
		error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
		if (error) {
			driver_unregister(&drv->driver);
			return error;
		}
	}

	return 0;
L
Linus Torvalds 已提交
834 835 836 837 838 839
}

void serio_unregister_driver(struct serio_driver *drv)
{
	struct serio *serio;

840
	mutex_lock(&serio_mutex);
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853
	drv->manual_bind = 1;	/* so serio_find_driver ignores it */

start_over:
	list_for_each_entry(serio, &serio_list, node) {
		if (serio->drv == drv) {
			serio_disconnect_port(serio);
			serio_find_driver(serio);
			/* we could've deleted some ports, restart */
			goto start_over;
		}
	}

	driver_unregister(&drv->driver);
854
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
}

static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
{
	serio_pause_rx(serio);
	serio->drv = drv;
	serio_continue_rx(serio);
}

static int serio_bus_match(struct device *dev, struct device_driver *drv)
{
	struct serio *serio = to_serio_port(dev);
	struct serio_driver *serio_drv = to_serio_driver(drv);

	if (serio->manual_bind || serio_drv->manual_bind)
		return 0;

	return serio_match_port(serio_drv->id_table, serio);
}

#ifdef CONFIG_HOTPLUG

877
#define SERIO_ADD_UEVENT_VAR(fmt, val...)				\
878
	do {								\
879
		int err = add_uevent_var(env, fmt, val);		\
880 881 882 883
		if (err)						\
			return err;					\
	} while (0)

884
static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892
{
	struct serio *serio;

	if (!dev)
		return -ENODEV;

	serio = to_serio_port(dev);

893 894 895 896 897
	SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
	SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
	SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
	SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
	SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
898
				serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
L
Linus Torvalds 已提交
899 900 901

	return 0;
}
902
#undef SERIO_ADD_UEVENT_VAR
L
Linus Torvalds 已提交
903 904 905

#else

906
static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
L
Linus Torvalds 已提交
907 908 909 910 911 912
{
	return -ENODEV;
}

#endif /* CONFIG_HOTPLUG */

913 914 915 916 917 918 919 920 921 922 923 924 925
#ifdef CONFIG_PM
static int serio_suspend(struct device *dev, pm_message_t state)
{
	if (dev->power.power_state.event != state.event) {
		if (state.event == PM_EVENT_SUSPEND)
			serio_cleanup(to_serio_port(dev));

		dev->power.power_state = state;
	}

	return 0;
}

L
Linus Torvalds 已提交
926 927 928 929
static int serio_resume(struct device *dev)
{
	struct serio *serio = to_serio_port(dev);

930 931
	if (dev->power.power_state.event != PM_EVENT_ON &&
	    serio_reconnect_driver(serio)) {
L
Linus Torvalds 已提交
932 933 934 935 936 937 938
		/*
		 * Driver re-probing can take a while, so better let kseriod
		 * deal with it.
		 */
		serio_rescan(serio);
	}

939 940
	dev->power.power_state = PMSG_ON;

L
Linus Torvalds 已提交
941 942
	return 0;
}
943
#endif /* CONFIG_PM */
L
Linus Torvalds 已提交
944

945
/* called from serio_driver->connect/disconnect methods under serio_mutex */
L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956
int serio_open(struct serio *serio, struct serio_driver *drv)
{
	serio_set_drv(serio, drv);

	if (serio->open && serio->open(serio)) {
		serio_set_drv(serio, NULL);
		return -1;
	}
	return 0;
}

957
/* called from serio_driver->connect/disconnect methods under serio_mutex */
L
Linus Torvalds 已提交
958 959 960 961 962 963 964 965 966
void serio_close(struct serio *serio)
{
	if (serio->close)
		serio->close(serio);

	serio_set_drv(serio, NULL);
}

irqreturn_t serio_interrupt(struct serio *serio,
967
		unsigned char data, unsigned int dfl)
L
Linus Torvalds 已提交
968 969 970 971 972 973 974
{
	unsigned long flags;
	irqreturn_t ret = IRQ_NONE;

	spin_lock_irqsave(&serio->lock, flags);

        if (likely(serio->drv)) {
975
                ret = serio->drv->interrupt(serio, data, dfl);
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983 984 985
	} else if (!dfl && serio->registered) {
		serio_rescan(serio);
		ret = IRQ_HANDLED;
	}

	spin_unlock_irqrestore(&serio->lock, flags);

	return ret;
}

986 987 988 989 990 991 992 993
static struct bus_type serio_bus = {
	.name		= "serio",
	.dev_attrs	= serio_device_attrs,
	.drv_attrs	= serio_driver_attrs,
	.match		= serio_bus_match,
	.uevent		= serio_uevent,
	.probe		= serio_driver_probe,
	.remove		= serio_driver_remove,
994 995 996
	.shutdown	= serio_shutdown,
#ifdef CONFIG_PM
	.suspend	= serio_suspend,
997
	.resume		= serio_resume,
998
#endif
999 1000
};

L
Linus Torvalds 已提交
1001 1002
static int __init serio_init(void)
{
1003
	int error;
L
Linus Torvalds 已提交
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	error = bus_register(&serio_bus);
	if (error) {
		printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
		return error;
	}

	serio_task = kthread_run(serio_thread, NULL, "kseriod");
	if (IS_ERR(serio_task)) {
		bus_unregister(&serio_bus);
		error = PTR_ERR(serio_task);
		printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
		return error;
	}
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024

	return 0;
}

static void __exit serio_exit(void)
{
	bus_unregister(&serio_bus);
1025
	kthread_stop(serio_task);
L
Linus Torvalds 已提交
1026 1027
}

1028
subsys_initcall(serio_init);
L
Linus Torvalds 已提交
1029
module_exit(serio_exit);