serio.c 24.8 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
/*
 *  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
 */

29 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
31 32 33 34 35 36
#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/serio.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/slab.h>
37
#include <linux/workqueue.h>
38
#include <linux/mutex.h>
L
Linus Torvalds 已提交
39 40 41 42 43 44

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

/*
45
 * serio_mutex protects entire serio subsystem and is taken every time
46
 * serio port or driver registered or unregistered.
L
Linus Torvalds 已提交
47
 */
48
static DEFINE_MUTEX(serio_mutex);
L
Linus Torvalds 已提交
49 50 51 52

static LIST_HEAD(serio_list);

static void serio_add_port(struct serio *serio);
53
static int serio_reconnect_port(struct serio *serio);
L
Linus Torvalds 已提交
54
static void serio_disconnect_port(struct serio *serio);
55
static void serio_reconnect_subtree(struct serio *serio);
56
static void serio_attach_driver(struct serio_driver *drv);
L
Linus Torvalds 已提交
57

58 59 60 61
static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
{
	int retval;

62
	mutex_lock(&serio->drv_mutex);
63
	retval = drv->connect(serio, drv);
64
	mutex_unlock(&serio->drv_mutex);
65 66 67 68 69 70 71 72

	return retval;
}

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

73
	mutex_lock(&serio->drv_mutex);
74 75
	if (serio->drv && serio->drv->reconnect)
		retval = serio->drv->reconnect(serio);
76
	mutex_unlock(&serio->drv_mutex);
77 78 79 80 81 82

	return retval;
}

static void serio_disconnect_driver(struct serio *serio)
{
83
	mutex_lock(&serio->drv_mutex);
84 85
	if (serio->drv)
		serio->drv->disconnect(serio);
86
	mutex_unlock(&serio->drv_mutex);
87 88
}

L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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
 */

106
static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
L
Linus Torvalds 已提交
107
{
108 109
	int error;

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

L
Linus Torvalds 已提交
112
		serio->dev.driver = &drv->driver;
113
		if (serio_connect_driver(serio, drv)) {
L
Linus Torvalds 已提交
114
			serio->dev.driver = NULL;
115
			return -ENODEV;
L
Linus Torvalds 已提交
116
		}
117

118 119
		error = device_bind_driver(&serio->dev);
		if (error) {
120 121 122 123
			dev_warn(&serio->dev,
				 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
				 serio->phys, serio->name,
				 drv->description, error);
124 125
			serio_disconnect_driver(serio);
			serio->dev.driver = NULL;
126
			return error;
127
		}
L
Linus Torvalds 已提交
128
	}
129
	return 0;
L
Linus Torvalds 已提交
130 131 132 133
}

static void serio_find_driver(struct serio *serio)
{
134 135 136
	int error;

	error = device_attach(&serio->dev);
137
	if (error < 0 && error != -EPROBE_DEFER)
138 139 140
		dev_warn(&serio->dev,
			 "device_attach() failed for %s (%s), error: %d\n",
			 serio->phys, serio->name, error);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148
}


/*
 * Serio event processing.
 */

enum serio_event_type {
149 150
	SERIO_RESCAN_PORT,
	SERIO_RECONNECT_PORT,
151
	SERIO_RECONNECT_SUBTREE,
L
Linus Torvalds 已提交
152
	SERIO_REGISTER_PORT,
153
	SERIO_ATTACH_DRIVER,
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165
};

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);

166
static struct serio_event *serio_get_event(void)
L
Linus Torvalds 已提交
167
{
168
	struct serio_event *event = NULL;
L
Linus Torvalds 已提交
169 170 171 172
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

173 174 175 176
	if (!list_empty(&serio_event_list)) {
		event = list_first_entry(&serio_event_list,
					 struct serio_event, node);
		list_del_init(&event->node);
L
Linus Torvalds 已提交
177
	}
178

L
Linus Torvalds 已提交
179
	spin_unlock_irqrestore(&serio_event_lock, flags);
180
	return event;
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188
}

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

189 190
static void serio_remove_duplicate_events(void *object,
					  enum serio_event_type type)
L
Linus Torvalds 已提交
191
{
192
	struct serio_event *e, *next;
L
Linus Torvalds 已提交
193 194 195 196
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

197
	list_for_each_entry_safe(e, next, &serio_event_list, node) {
198
		if (object == e->object) {
L
Linus Torvalds 已提交
199 200 201 202 203
			/*
			 * If this event is of different type we should not
			 * look further - we only suppress duplicate events
			 * that were sent back-to-back.
			 */
204
			if (type != e->type)
L
Linus Torvalds 已提交
205 206
				break;

207
			list_del_init(&e->node);
L
Linus Torvalds 已提交
208 209 210 211 212 213 214
			serio_free_event(e);
		}
	}

	spin_unlock_irqrestore(&serio_event_lock, flags);
}

215
static void serio_handle_event(struct work_struct *work)
L
Linus Torvalds 已提交
216 217 218
{
	struct serio_event *event;

219
	mutex_lock(&serio_mutex);
L
Linus Torvalds 已提交
220

221
	while ((event = serio_get_event())) {
L
Linus Torvalds 已提交
222 223 224

		switch (event->type) {

225 226 227
		case SERIO_REGISTER_PORT:
			serio_add_port(event->object);
			break;
L
Linus Torvalds 已提交
228

229 230 231
		case SERIO_RECONNECT_PORT:
			serio_reconnect_port(event->object);
			break;
L
Linus Torvalds 已提交
232

233 234 235 236
		case SERIO_RESCAN_PORT:
			serio_disconnect_port(event->object);
			serio_find_driver(event->object);
			break;
237

238 239
		case SERIO_RECONNECT_SUBTREE:
			serio_reconnect_subtree(event->object);
240
			break;
L
Linus Torvalds 已提交
241

242 243 244
		case SERIO_ATTACH_DRIVER:
			serio_attach_driver(event->object);
			break;
L
Linus Torvalds 已提交
245 246
		}

247
		serio_remove_duplicate_events(event->object, event->type);
L
Linus Torvalds 已提交
248 249 250
		serio_free_event(event);
	}

251
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
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
static DECLARE_WORK(serio_event_work, serio_handle_event);

static int serio_queue_event(void *object, struct module *owner,
			     enum serio_event_type event_type)
{
	unsigned long flags;
	struct serio_event *event;
	int retval = 0;

	spin_lock_irqsave(&serio_event_lock, flags);

	/*
	 * Scan event list for the other events for the same serio port,
	 * 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.
	 */
	list_for_each_entry_reverse(event, &serio_event_list, node) {
		if (event->object == object) {
			if (event->type == event_type)
				goto out;
			break;
		}
	}

	event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
	if (!event) {
		pr_err("Not enough memory to queue event %d\n", event_type);
		retval = -ENOMEM;
		goto out;
	}

	if (!try_module_get(owner)) {
288 289
		pr_warn("Can't get module reference, dropping event %d\n",
			event_type);
290 291 292 293 294 295 296 297 298 299
		kfree(event);
		retval = -EINVAL;
		goto out;
	}

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

	list_add_tail(&event->node, &serio_event_list);
300
	queue_work(system_long_wq, &serio_event_work);
301 302 303 304 305 306

out:
	spin_unlock_irqrestore(&serio_event_lock, flags);
	return retval;
}

L
Linus Torvalds 已提交
307
/*
308 309
 * Remove all events that have been submitted for a given
 * object, be it serio port or driver.
L
Linus Torvalds 已提交
310
 */
311
static void serio_remove_pending_events(void *object)
L
Linus Torvalds 已提交
312
{
313
	struct serio_event *event, *next;
L
Linus Torvalds 已提交
314 315 316 317
	unsigned long flags;

	spin_lock_irqsave(&serio_event_lock, flags);

318
	list_for_each_entry_safe(event, next, &serio_event_list, node) {
319
		if (event->object == object) {
320
			list_del_init(&event->node);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328
			serio_free_event(event);
		}
	}

	spin_unlock_irqrestore(&serio_event_lock, flags);
}

/*
329
 * Locate child serio port (if any) that has not been fully registered yet.
L
Linus Torvalds 已提交
330
 *
331 332
 * Children are registered by driver's connect() handler so there can't be a
 * grandchild pending registration together with a child.
L
Linus Torvalds 已提交
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
 */
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;
}

/*
 * Serio port operations
 */

360
static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
361 362 363 364 365
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%s\n", serio->name);
}

366
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
367 368 369 370 371 372 373
{
	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);
}

374
static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
375 376 377 378 379
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.type);
}

380
static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
381 382 383 384 385
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.proto);
}

386
static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
387 388 389 390 391
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.id);
}

392
static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
393 394 395 396 397
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%02x\n", serio->id.extra);
}

398
static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
399 400 401
{
	struct serio *serio = to_serio_port(dev);
	struct device_driver *drv;
402
	int error;
L
Linus Torvalds 已提交
403

404 405 406
	error = mutex_lock_interruptible(&serio_mutex);
	if (error)
		return error;
L
Linus Torvalds 已提交
407 408 409 410

	if (!strncmp(buf, "none", count)) {
		serio_disconnect_port(serio);
	} else if (!strncmp(buf, "reconnect", count)) {
411
		serio_reconnect_subtree(serio);
L
Linus Torvalds 已提交
412 413 414
	} else if (!strncmp(buf, "rescan", count)) {
		serio_disconnect_port(serio);
		serio_find_driver(serio);
415
		serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
L
Linus Torvalds 已提交
416 417
	} else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
		serio_disconnect_port(serio);
418
		error = serio_bind_driver(serio, to_serio_driver(drv));
419
		serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
L
Linus Torvalds 已提交
420
	} else {
421
		error = -EINVAL;
L
Linus Torvalds 已提交
422 423
	}

424
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
425

426
	return error ? error : count;
L
Linus Torvalds 已提交
427 428
}

429
static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
430 431 432 433 434
{
	struct serio *serio = to_serio_port(dev);
	return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
}

435
static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
436 437 438 439 440 441
{
	struct serio *serio = to_serio_port(dev);
	int retval;

	retval = count;
	if (!strncmp(buf, "manual", count)) {
442
		serio->manual_bind = true;
L
Linus Torvalds 已提交
443
	} else if (!strncmp(buf, "auto", count)) {
444
		serio->manual_bind = false;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451
	} else {
		retval = -EINVAL;
	}

	return retval;
}

452 453 454 455 456 457 458
static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct serio *serio = to_serio_port(dev);

	return sprintf(buf, "%s\n", serio->firmware_id);
}

459 460 461 462 463 464 465 466 467 468
static DEVICE_ATTR_RO(type);
static DEVICE_ATTR_RO(proto);
static DEVICE_ATTR_RO(id);
static DEVICE_ATTR_RO(extra);

static struct attribute *serio_device_id_attrs[] = {
	&dev_attr_type.attr,
	&dev_attr_proto.attr,
	&dev_attr_id.attr,
	&dev_attr_extra.attr,
D
Dmitry Torokhov 已提交
469 470 471 472 473 474 475 476 477 478 479 480
	NULL
};

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

static DEVICE_ATTR_RO(modalias);
static DEVICE_ATTR_WO(drvctl);
static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
481
static DEVICE_ATTR_RO(firmware_id);
D
Dmitry Torokhov 已提交
482 483

static struct attribute *serio_device_attrs[] = {
484 485 486 487
	&dev_attr_modalias.attr,
	&dev_attr_description.attr,
	&dev_attr_drvctl.attr,
	&dev_attr_bind_mode.attr,
488
	&dev_attr_firmware_id.attr,
489 490 491
	NULL
};

D
Dmitry Torokhov 已提交
492 493
static struct attribute_group serio_device_attr_group = {
	.attrs	= serio_device_attrs,
L
Linus Torvalds 已提交
494 495
};

496 497
static const struct attribute_group *serio_device_attr_groups[] = {
	&serio_id_attr_group,
D
Dmitry Torokhov 已提交
498
	&serio_device_attr_group,
499 500
	NULL
};
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514

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)
{
515
	static atomic_t serio_no = ATOMIC_INIT(-1);
L
Linus Torvalds 已提交
516 517 518

	__module_get(THIS_MODULE);

519
	INIT_LIST_HEAD(&serio->node);
520 521
	INIT_LIST_HEAD(&serio->child_node);
	INIT_LIST_HEAD(&serio->children);
L
Linus Torvalds 已提交
522
	spin_lock_init(&serio->lock);
523
	mutex_init(&serio->drv_mutex);
L
Linus Torvalds 已提交
524
	device_initialize(&serio->dev);
525
	dev_set_name(&serio->dev, "serio%lu",
526
		     (unsigned long)atomic_inc_return(&serio_no));
L
Linus Torvalds 已提交
527 528
	serio->dev.bus = &serio_bus;
	serio->dev.release = serio_release_port;
529
	serio->dev.groups = serio_device_attr_groups;
530
	if (serio->parent) {
L
Linus Torvalds 已提交
531
		serio->dev.parent = &serio->parent->dev;
532 533 534 535
		serio->depth = serio->parent->depth + 1;
	} else
		serio->depth = 0;
	lockdep_set_subclass(&serio->lock, serio->depth);
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543
}

/*
 * Complete serio port registration.
 * Driver core will attempt to find appropriate driver for the port.
 */
static void serio_add_port(struct serio *serio)
{
544
	struct serio *parent = serio->parent;
545 546
	int error;

547 548 549 550
	if (parent) {
		serio_pause_rx(parent);
		list_add_tail(&serio->child_node, &parent->children);
		serio_continue_rx(parent);
L
Linus Torvalds 已提交
551 552 553
	}

	list_add_tail(&serio->node, &serio_list);
554

L
Linus Torvalds 已提交
555 556
	if (serio->start)
		serio->start(serio);
557

558 559
	error = device_add(&serio->dev);
	if (error)
560 561
		dev_err(&serio->dev,
			"device_add() failed for %s (%s), error: %d\n",
562
			serio->phys, serio->name, error);
L
Linus Torvalds 已提交
563 564 565
}

/*
566
 * serio_destroy_port() completes unregistration process and removes
L
Linus Torvalds 已提交
567 568 569 570 571 572
 * port from the system
 */
static void serio_destroy_port(struct serio *serio)
{
	struct serio *child;

573
	while ((child = serio_get_pending_child(serio)) != NULL) {
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582
		serio_remove_pending_events(child);
		put_device(&child->dev);
	}

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

	if (serio->parent) {
		serio_pause_rx(serio->parent);
583
		list_del_init(&serio->child_node);
L
Linus Torvalds 已提交
584 585 586 587
		serio_continue_rx(serio->parent);
		serio->parent = NULL;
	}

588
	if (device_is_registered(&serio->dev))
L
Linus Torvalds 已提交
589 590
		device_del(&serio->dev);

591
	list_del_init(&serio->node);
L
Linus Torvalds 已提交
592 593 594 595
	serio_remove_pending_events(serio);
	put_device(&serio->dev);
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/*
 * Reconnect serio port (re-initialize attached device).
 * If reconnect fails (old device is no longer attached or
 * there was no device to begin with) we do full rescan in
 * hope of finding a driver for the port.
 */
static int serio_reconnect_port(struct serio *serio)
{
	int error = serio_reconnect_driver(serio);

	if (error) {
		serio_disconnect_port(serio);
		serio_find_driver(serio);
	}

	return error;
}

L
Linus Torvalds 已提交
614
/*
615 616
 * Reconnect serio port and all its children (re-initialize attached
 * devices).
L
Linus Torvalds 已提交
617
 */
618
static void serio_reconnect_subtree(struct serio *root)
L
Linus Torvalds 已提交
619
{
620 621 622
	struct serio *s = root;
	int error;

L
Linus Torvalds 已提交
623
	do {
624 625 626 627 628 629 630 631 632 633 634
		error = serio_reconnect_port(s);
		if (!error) {
			/*
			 * Reconnect was successful, move on to do the
			 * first child.
			 */
			if (!list_empty(&s->children)) {
				s = list_first_entry(&s->children,
						     struct serio, child_node);
				continue;
			}
L
Linus Torvalds 已提交
635
		}
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

		/*
		 * Either it was a leaf node or reconnect failed and it
		 * became a leaf node. Continue reconnecting starting with
		 * the next sibling of the parent node.
		 */
		while (s != root) {
			struct serio *parent = s->parent;

			if (!list_is_last(&s->child_node, &parent->children)) {
				s = list_entry(s->child_node.next,
					       struct serio, child_node);
				break;
			}

			s = parent;
		}
	} while (s != root);
L
Linus Torvalds 已提交
654 655 656 657
}

/*
 * serio_disconnect_port() unbinds a port from its driver. As a side effect
658
 * all children ports are unbound and destroyed.
L
Linus Torvalds 已提交
659 660 661
 */
static void serio_disconnect_port(struct serio *serio)
{
662 663 664 665 666 667 668 669 670 671 672 673
	struct serio *s = serio;

	/*
	 * Children ports should be disconnected and destroyed
	 * first; we travel the tree in depth-first order.
	 */
	while (!list_empty(&serio->children)) {

		/* Locate a leaf */
		while (!list_empty(&s->children))
			s = list_first_entry(&s->children,
					     struct serio, child_node);
L
Linus Torvalds 已提交
674 675

		/*
676 677
		 * Prune this leaf node unless it is the one we
		 * started with.
L
Linus Torvalds 已提交
678
		 */
679 680
		if (s != serio) {
			struct serio *parent = s->parent;
L
Linus Torvalds 已提交
681

682
			device_release_driver(&s->dev);
L
Linus Torvalds 已提交
683
			serio_destroy_port(s);
684 685 686

			s = parent;
		}
L
Linus Torvalds 已提交
687 688 689
	}

	/*
690
	 * OK, no children left, now disconnect this port.
L
Linus Torvalds 已提交
691
	 */
692
	device_release_driver(&serio->dev);
L
Linus Torvalds 已提交
693 694 695 696
}

void serio_rescan(struct serio *serio)
{
697
	serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
L
Linus Torvalds 已提交
698
}
699
EXPORT_SYMBOL(serio_rescan);
L
Linus Torvalds 已提交
700 701 702

void serio_reconnect(struct serio *serio)
{
703
	serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
L
Linus Torvalds 已提交
704
}
705
EXPORT_SYMBOL(serio_reconnect);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713 714 715

/*
 * 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);
}
716
EXPORT_SYMBOL(__serio_register_port);
L
Linus Torvalds 已提交
717 718 719 720 721 722

/*
 * Synchronously unregisters serio port.
 */
void serio_unregister_port(struct serio *serio)
{
723
	mutex_lock(&serio_mutex);
L
Linus Torvalds 已提交
724 725
	serio_disconnect_port(serio);
	serio_destroy_port(serio);
726
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
727
}
728
EXPORT_SYMBOL(serio_unregister_port);
L
Linus Torvalds 已提交
729

730
/*
731
 * Safely unregisters children ports if they are present.
732 733 734
 */
void serio_unregister_child_port(struct serio *serio)
{
735 736
	struct serio *s, *next;

737
	mutex_lock(&serio_mutex);
738 739 740
	list_for_each_entry_safe(s, next, &serio->children, child_node) {
		serio_disconnect_port(s);
		serio_destroy_port(s);
741
	}
742
	mutex_unlock(&serio_mutex);
743
}
744
EXPORT_SYMBOL(serio_unregister_child_port);
745

L
Linus Torvalds 已提交
746 747 748 749 750

/*
 * Serio driver operations
 */

751
static ssize_t description_show(struct device_driver *drv, char *buf)
L
Linus Torvalds 已提交
752 753 754 755
{
	struct serio_driver *driver = to_serio_driver(drv);
	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
}
756
static DRIVER_ATTR_RO(description);
L
Linus Torvalds 已提交
757

758
static ssize_t bind_mode_show(struct device_driver *drv, char *buf)
L
Linus Torvalds 已提交
759 760 761 762 763
{
	struct serio_driver *serio_drv = to_serio_driver(drv);
	return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
}

764
static ssize_t bind_mode_store(struct device_driver *drv, const char *buf, size_t count)
L
Linus Torvalds 已提交
765 766 767 768 769 770
{
	struct serio_driver *serio_drv = to_serio_driver(drv);
	int retval;

	retval = count;
	if (!strncmp(buf, "manual", count)) {
771
		serio_drv->manual_bind = true;
L
Linus Torvalds 已提交
772
	} else if (!strncmp(buf, "auto", count)) {
773
		serio_drv->manual_bind = false;
L
Linus Torvalds 已提交
774 775 776 777 778 779
	} else {
		retval = -EINVAL;
	}

	return retval;
}
780
static DRIVER_ATTR_RW(bind_mode);
L
Linus Torvalds 已提交
781

782 783 784 785
static struct attribute *serio_driver_attrs[] = {
	&driver_attr_description.attr,
	&driver_attr_bind_mode.attr,
	NULL,
L
Linus Torvalds 已提交
786
};
787
ATTRIBUTE_GROUPS(serio_driver);
L
Linus Torvalds 已提交
788 789 790 791 792 793

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

794
	return serio_connect_driver(serio, drv);
L
Linus Torvalds 已提交
795 796 797 798 799 800
}

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

801
	serio_disconnect_driver(serio);
L
Linus Torvalds 已提交
802 803 804
	return 0;
}

805 806
static void serio_cleanup(struct serio *serio)
{
807
	mutex_lock(&serio->drv_mutex);
808 809
	if (serio->drv && serio->drv->cleanup)
		serio->drv->cleanup(serio);
810
	mutex_unlock(&serio->drv_mutex);
811 812 813 814 815 816 817 818 819
}

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

	serio_cleanup(serio);
}

820
static void serio_attach_driver(struct serio_driver *drv)
821 822 823
{
	int error;

824
	error = driver_attach(&drv->driver);
825
	if (error)
826 827
		pr_warn("driver_attach() failed for %s with error %d\n",
			drv->driver.name, error);
828 829
}

830
int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
L
Linus Torvalds 已提交
831
{
832
	bool manual_bind = drv->manual_bind;
833 834
	int error;

L
Linus Torvalds 已提交
835
	drv->driver.bus = &serio_bus;
836 837
	drv->driver.owner = owner;
	drv->driver.mod_name = mod_name;
L
Linus Torvalds 已提交
838

839 840 841 842
	/*
	 * Temporarily disable automatic binding because probing
	 * takes long time and we are better off doing it in kseriod
	 */
843
	drv->manual_bind = true;
844 845 846

	error = driver_register(&drv->driver);
	if (error) {
847
		pr_err("driver_register() failed for %s, error: %d\n",
848 849 850 851 852 853 854 855 856
			drv->driver.name, error);
		return error;
	}

	/*
	 * Restore original bind mode and let kseriod bind the
	 * driver to free ports
	 */
	if (!manual_bind) {
857
		drv->manual_bind = false;
858 859 860 861 862 863 864 865
		error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
		if (error) {
			driver_unregister(&drv->driver);
			return error;
		}
	}

	return 0;
L
Linus Torvalds 已提交
866
}
867
EXPORT_SYMBOL(__serio_register_driver);
L
Linus Torvalds 已提交
868 869 870 871 872

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

873
	mutex_lock(&serio_mutex);
874

875
	drv->manual_bind = true;	/* so serio_find_driver ignores it */
876
	serio_remove_pending_events(drv);
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885 886 887 888

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);
889
	mutex_unlock(&serio_mutex);
L
Linus Torvalds 已提交
890
}
891
EXPORT_SYMBOL(serio_unregister_driver);
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

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);
}

911
#define SERIO_ADD_UEVENT_VAR(fmt, val...)				\
912
	do {								\
913
		int err = add_uevent_var(env, fmt, val);		\
914 915 916 917
		if (err)						\
			return err;					\
	} while (0)

918
static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926
{
	struct serio *serio;

	if (!dev)
		return -ENODEV;

	serio = to_serio_port(dev);

927 928 929 930
	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);
931

932
	SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
933
				serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
L
Linus Torvalds 已提交
934

935 936 937 938
	if (serio->firmware_id[0])
		SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
				     serio->firmware_id);

L
Linus Torvalds 已提交
939 940
	return 0;
}
941
#undef SERIO_ADD_UEVENT_VAR
L
Linus Torvalds 已提交
942

943
#ifdef CONFIG_PM
944
static int serio_suspend(struct device *dev)
945
{
946
	struct serio *serio = to_serio_port(dev);
947

948
	serio_cleanup(serio);
949 950 951 952

	return 0;
}

L
Linus Torvalds 已提交
953 954
static int serio_resume(struct device *dev)
{
955
	struct serio *serio = to_serio_port(dev);
956
	int error = -ENOENT;
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	mutex_lock(&serio->drv_mutex);
	if (serio->drv && serio->drv->fast_reconnect) {
		error = serio->drv->fast_reconnect(serio);
		if (error && error != -ENOENT)
			dev_warn(dev, "fast reconnect failed with error %d\n",
				 error);
	}
	mutex_unlock(&serio->drv_mutex);

	if (error) {
		/*
		 * Driver reconnect can take a while, so better let
		 * kseriod deal with it.
		 */
		serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
	}
L
Linus Torvalds 已提交
974 975 976

	return 0;
}
977 978 979 980 981 982 983

static const struct dev_pm_ops serio_pm_ops = {
	.suspend	= serio_suspend,
	.resume		= serio_resume,
	.poweroff	= serio_suspend,
	.restore	= serio_resume,
};
984
#endif /* CONFIG_PM */
L
Linus Torvalds 已提交
985

986
/* called from serio_driver->connect/disconnect methods under serio_mutex */
L
Linus Torvalds 已提交
987 988 989 990 991 992 993 994 995 996
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;
}
997
EXPORT_SYMBOL(serio_open);
L
Linus Torvalds 已提交
998

999
/* called from serio_driver->connect/disconnect methods under serio_mutex */
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006
void serio_close(struct serio *serio)
{
	if (serio->close)
		serio->close(serio);

	serio_set_drv(serio, NULL);
}
1007
EXPORT_SYMBOL(serio_close);
L
Linus Torvalds 已提交
1008 1009

irqreturn_t serio_interrupt(struct serio *serio,
1010
		unsigned char data, unsigned int dfl)
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017
{
	unsigned long flags;
	irqreturn_t ret = IRQ_NONE;

	spin_lock_irqsave(&serio->lock, flags);

        if (likely(serio->drv)) {
1018
                ret = serio->drv->interrupt(serio, data, dfl);
1019
	} else if (!dfl && device_is_registered(&serio->dev)) {
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027
		serio_rescan(serio);
		ret = IRQ_HANDLED;
	}

	spin_unlock_irqrestore(&serio->lock, flags);

	return ret;
}
1028
EXPORT_SYMBOL(serio_interrupt);
L
Linus Torvalds 已提交
1029

1030
struct bus_type serio_bus = {
1031
	.name		= "serio",
1032
	.drv_groups	= serio_driver_groups,
1033 1034 1035 1036
	.match		= serio_bus_match,
	.uevent		= serio_uevent,
	.probe		= serio_driver_probe,
	.remove		= serio_driver_remove,
1037 1038
	.shutdown	= serio_shutdown,
#ifdef CONFIG_PM
1039
	.pm		= &serio_pm_ops,
1040
#endif
1041
};
1042
EXPORT_SYMBOL(serio_bus);
1043

L
Linus Torvalds 已提交
1044 1045
static int __init serio_init(void)
{
1046
	int error;
L
Linus Torvalds 已提交
1047

1048 1049
	error = bus_register(&serio_bus);
	if (error) {
1050
		pr_err("Failed to register serio bus, error: %d\n", error);
1051 1052 1053
		return error;
	}

L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059
	return 0;
}

static void __exit serio_exit(void)
{
	bus_unregister(&serio_bus);
1060 1061 1062 1063 1064 1065

	/*
	 * There should not be any outstanding events but work may
	 * still be scheduled so simply cancel it.
	 */
	cancel_work_sync(&serio_event_work);
L
Linus Torvalds 已提交
1066 1067
}

1068
subsys_initcall(serio_init);
L
Linus Torvalds 已提交
1069
module_exit(serio_exit);