blob: 6521034bc93328289819d92894fd2388401cecbb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The Serio abstraction module
3 *
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 * Copyright (c) 2003 Daniele Bellucci
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27 */
28
29#include <linux/stddef.h>
30#include <linux/module.h>
31#include <linux/serio.h>
32#include <linux/errno.h>
33#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070036#include <linux/kthread.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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);
Linus Torvalds3c803e82005-06-27 17:49:45 -070046EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047EXPORT_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/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050056 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * serio port or driver registrered or unregistered.
58 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050059static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static LIST_HEAD(serio_list);
62
Russell King30226f82006-01-05 14:38:53 +000063static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static void serio_add_port(struct serio *serio);
66static void serio_destroy_port(struct serio *serio);
67static void serio_reconnect_port(struct serio *serio);
68static void serio_disconnect_port(struct serio *serio);
69
Linus Torvalds3c803e82005-06-27 17:49:45 -070070static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
71{
72 int retval;
73
Arjan van de Venc4e32e92006-02-19 00:21:55 -050074 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070075 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050076 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070077
78 return retval;
79}
80
81static int serio_reconnect_driver(struct serio *serio)
82{
83 int retval = -1;
84
Arjan van de Venc4e32e92006-02-19 00:21:55 -050085 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070086 if (serio->drv && serio->drv->reconnect)
87 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050088 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070089
90 return retval;
91}
92
93static void serio_disconnect_driver(struct serio *serio)
94{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050095 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070096 if (serio->drv)
97 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050098 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
102{
103 while (ids->type || ids->proto) {
104 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
105 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
106 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
107 (ids->id == SERIO_ANY || ids->id == serio->id.id))
108 return 1;
109 ids++;
110 }
111 return 0;
112}
113
114/*
115 * Basic serio -> driver core mappings
116 */
117
118static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
119{
120 down_write(&serio_bus.subsys.rwsem);
121
122 if (serio_match_port(drv->id_table, serio)) {
123 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700124 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 serio->dev.driver = NULL;
126 goto out;
127 }
128 device_bind_driver(&serio->dev);
129 }
130out:
131 up_write(&serio_bus.subsys.rwsem);
132}
133
134static void serio_release_driver(struct serio *serio)
135{
136 down_write(&serio_bus.subsys.rwsem);
137 device_release_driver(&serio->dev);
138 up_write(&serio_bus.subsys.rwsem);
139}
140
141static void serio_find_driver(struct serio *serio)
142{
143 down_write(&serio_bus.subsys.rwsem);
144 device_attach(&serio->dev);
145 up_write(&serio_bus.subsys.rwsem);
146}
147
148
149/*
150 * Serio event processing.
151 */
152
153enum serio_event_type {
154 SERIO_RESCAN,
155 SERIO_RECONNECT,
156 SERIO_REGISTER_PORT,
157 SERIO_UNREGISTER_PORT,
158 SERIO_REGISTER_DRIVER,
159};
160
161struct serio_event {
162 enum serio_event_type type;
163 void *object;
164 struct module *owner;
165 struct list_head node;
166};
167
168static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
169static LIST_HEAD(serio_event_list);
170static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700171static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void serio_queue_event(void *object, struct module *owner,
174 enum serio_event_type event_type)
175{
176 unsigned long flags;
177 struct serio_event *event;
178
179 spin_lock_irqsave(&serio_event_lock, flags);
180
181 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700182 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * starting with the most recent one. If event is the same we
184 * do not need add new one. If event is of different type we
185 * need to add this event and should not look further because
186 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 list_for_each_entry_reverse(event, &serio_event_list, node) {
189 if (event->object == object) {
190 if (event->type == event_type)
191 goto out;
192 break;
193 }
194 }
195
196 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
197 if (!try_module_get(owner)) {
198 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk9d921112006-03-14 00:13:29 -0500199 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
201 }
202
203 event->type = event_type;
204 event->object = object;
205 event->owner = owner;
206
207 list_add_tail(&event->node, &serio_event_list);
208 wake_up(&serio_wait);
209 } else {
210 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
211 }
212out:
213 spin_unlock_irqrestore(&serio_event_lock, flags);
214}
215
216static void serio_free_event(struct serio_event *event)
217{
218 module_put(event->owner);
219 kfree(event);
220}
221
222static void serio_remove_duplicate_events(struct serio_event *event)
223{
224 struct list_head *node, *next;
225 struct serio_event *e;
226 unsigned long flags;
227
228 spin_lock_irqsave(&serio_event_lock, flags);
229
230 list_for_each_safe(node, next, &serio_event_list) {
231 e = list_entry(node, struct serio_event, node);
232 if (event->object == e->object) {
233 /*
234 * If this event is of different type we should not
235 * look further - we only suppress duplicate events
236 * that were sent back-to-back.
237 */
238 if (event->type != e->type)
239 break;
240
241 list_del_init(node);
242 serio_free_event(e);
243 }
244 }
245
246 spin_unlock_irqrestore(&serio_event_lock, flags);
247}
248
249
250static struct serio_event *serio_get_event(void)
251{
252 struct serio_event *event;
253 struct list_head *node;
254 unsigned long flags;
255
256 spin_lock_irqsave(&serio_event_lock, flags);
257
258 if (list_empty(&serio_event_list)) {
259 spin_unlock_irqrestore(&serio_event_lock, flags);
260 return NULL;
261 }
262
263 node = serio_event_list.next;
264 event = list_entry(node, struct serio_event, node);
265 list_del_init(node);
266
267 spin_unlock_irqrestore(&serio_event_lock, flags);
268
269 return event;
270}
271
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500272static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct serio_event *event;
275 struct serio_driver *serio_drv;
276
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500277 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500279 /*
280 * Note that we handle only one event here to give swsusp
281 * a chance to freeze kseriod thread. Serio events should
282 * be pretty rare so we are not concerned about taking
283 * performance hit.
284 */
285 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 switch (event->type) {
288 case SERIO_REGISTER_PORT:
289 serio_add_port(event->object);
290 break;
291
292 case SERIO_UNREGISTER_PORT:
293 serio_disconnect_port(event->object);
294 serio_destroy_port(event->object);
295 break;
296
297 case SERIO_RECONNECT:
298 serio_reconnect_port(event->object);
299 break;
300
301 case SERIO_RESCAN:
302 serio_disconnect_port(event->object);
303 serio_find_driver(event->object);
304 break;
305
306 case SERIO_REGISTER_DRIVER:
307 serio_drv = event->object;
308 driver_register(&serio_drv->driver);
309 break;
310
311 default:
312 break;
313 }
314
315 serio_remove_duplicate_events(event);
316 serio_free_event(event);
317 }
318
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500319 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/*
323 * Remove all events that have been submitted for a given serio port.
324 */
325static void serio_remove_pending_events(struct serio *serio)
326{
327 struct list_head *node, *next;
328 struct serio_event *event;
329 unsigned long flags;
330
331 spin_lock_irqsave(&serio_event_lock, flags);
332
333 list_for_each_safe(node, next, &serio_event_list) {
334 event = list_entry(node, struct serio_event, node);
335 if (event->object == serio) {
336 list_del_init(node);
337 serio_free_event(event);
338 }
339 }
340
341 spin_unlock_irqrestore(&serio_event_lock, flags);
342}
343
344/*
345 * Destroy child serio port (if any) that has not been fully registered yet.
346 *
347 * Note that we rely on the fact that port can have only one child and therefore
348 * only one child registration request can be pending. Additionally, children
349 * are registered by driver's connect() handler so there can't be a grandchild
350 * pending registration together with a child.
351 */
352static struct serio *serio_get_pending_child(struct serio *parent)
353{
354 struct serio_event *event;
355 struct serio *serio, *child = NULL;
356 unsigned long flags;
357
358 spin_lock_irqsave(&serio_event_lock, flags);
359
360 list_for_each_entry(event, &serio_event_list, node) {
361 if (event->type == SERIO_REGISTER_PORT) {
362 serio = event->object;
363 if (serio->parent == parent) {
364 child = serio;
365 break;
366 }
367 }
368 }
369
370 spin_unlock_irqrestore(&serio_event_lock, flags);
371 return child;
372}
373
374static int serio_thread(void *nothing)
375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500377 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700378 wait_event_interruptible(serio_wait,
379 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700380 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700381 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387
388/*
389 * Serio port operations
390 */
391
Yani Ioannoue404e272005-05-17 06:42:58 -0400392static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct serio *serio = to_serio_port(dev);
395 return sprintf(buf, "%s\n", serio->name);
396}
397
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500398static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
399{
400 struct serio *serio = to_serio_port(dev);
401
402 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
403 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
404}
405
Yani Ioannoue404e272005-05-17 06:42:58 -0400406static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct serio *serio = to_serio_port(dev);
409 return sprintf(buf, "%02x\n", serio->id.type);
410}
411
Yani Ioannoue404e272005-05-17 06:42:58 -0400412static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct serio *serio = to_serio_port(dev);
415 return sprintf(buf, "%02x\n", serio->id.proto);
416}
417
Yani Ioannoue404e272005-05-17 06:42:58 -0400418static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct serio *serio = to_serio_port(dev);
421 return sprintf(buf, "%02x\n", serio->id.id);
422}
423
Yani Ioannoue404e272005-05-17 06:42:58 -0400424static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct serio *serio = to_serio_port(dev);
427 return sprintf(buf, "%02x\n", serio->id.extra);
428}
429
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700430static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
431static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
432static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
433static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
434
435static struct attribute *serio_device_id_attrs[] = {
436 &dev_attr_type.attr,
437 &dev_attr_proto.attr,
438 &dev_attr_id.attr,
439 &dev_attr_extra.attr,
440 NULL
441};
442
443static struct attribute_group serio_id_attr_group = {
444 .name = "id",
445 .attrs = serio_device_id_attrs,
446};
447
Yani Ioannoue404e272005-05-17 06:42:58 -0400448static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct serio *serio = to_serio_port(dev);
451 struct device_driver *drv;
452 int retval;
453
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500454 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (retval)
456 return retval;
457
458 retval = count;
459 if (!strncmp(buf, "none", count)) {
460 serio_disconnect_port(serio);
461 } else if (!strncmp(buf, "reconnect", count)) {
462 serio_reconnect_port(serio);
463 } else if (!strncmp(buf, "rescan", count)) {
464 serio_disconnect_port(serio);
465 serio_find_driver(serio);
466 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
467 serio_disconnect_port(serio);
468 serio_bind_driver(serio, to_serio_driver(drv));
469 put_driver(drv);
470 } else {
471 retval = -EINVAL;
472 }
473
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500474 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return retval;
477}
478
Yani Ioannoue404e272005-05-17 06:42:58 -0400479static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct serio *serio = to_serio_port(dev);
482 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
483}
484
Yani Ioannoue404e272005-05-17 06:42:58 -0400485static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct serio *serio = to_serio_port(dev);
488 int retval;
489
490 retval = count;
491 if (!strncmp(buf, "manual", count)) {
492 serio->manual_bind = 1;
493 } else if (!strncmp(buf, "auto", count)) {
494 serio->manual_bind = 0;
495 } else {
496 retval = -EINVAL;
497 }
498
499 return retval;
500}
501
502static struct device_attribute serio_device_attrs[] = {
503 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500504 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
506 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
507 __ATTR_NULL
508};
509
510
511static void serio_release_port(struct device *dev)
512{
513 struct serio *serio = to_serio_port(dev);
514
515 kfree(serio);
516 module_put(THIS_MODULE);
517}
518
519/*
520 * Prepare serio port for registration.
521 */
522static void serio_init_port(struct serio *serio)
523{
524 static atomic_t serio_no = ATOMIC_INIT(0);
525
526 __module_get(THIS_MODULE);
527
528 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500529 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 device_initialize(&serio->dev);
531 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
532 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
533 serio->dev.bus = &serio_bus;
534 serio->dev.release = serio_release_port;
535 if (serio->parent)
536 serio->dev.parent = &serio->parent->dev;
537}
538
539/*
540 * Complete serio port registration.
541 * Driver core will attempt to find appropriate driver for the port.
542 */
543static void serio_add_port(struct serio *serio)
544{
545 if (serio->parent) {
546 serio_pause_rx(serio->parent);
547 serio->parent->child = serio;
548 serio_continue_rx(serio->parent);
549 }
550
551 list_add_tail(&serio->node, &serio_list);
552 if (serio->start)
553 serio->start(serio);
554 device_add(&serio->dev);
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700555 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 serio->registered = 1;
557}
558
559/*
560 * serio_destroy_port() completes deregistration process and removes
561 * port from the system
562 */
563static void serio_destroy_port(struct serio *serio)
564{
565 struct serio *child;
566
567 child = serio_get_pending_child(serio);
568 if (child) {
569 serio_remove_pending_events(child);
570 put_device(&child->dev);
571 }
572
573 if (serio->stop)
574 serio->stop(serio);
575
576 if (serio->parent) {
577 serio_pause_rx(serio->parent);
578 serio->parent->child = NULL;
579 serio_continue_rx(serio->parent);
580 serio->parent = NULL;
581 }
582
583 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700584 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 device_del(&serio->dev);
586 list_del_init(&serio->node);
587 serio->registered = 0;
588 }
589
590 serio_remove_pending_events(serio);
591 put_device(&serio->dev);
592}
593
594/*
595 * Reconnect serio port and all its children (re-initialize attached devices)
596 */
597static void serio_reconnect_port(struct serio *serio)
598{
599 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700600 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 serio_disconnect_port(serio);
602 serio_find_driver(serio);
603 /* Ok, old children are now gone, we are done */
604 break;
605 }
606 serio = serio->child;
607 } while (serio);
608}
609
610/*
611 * serio_disconnect_port() unbinds a port from its driver. As a side effect
612 * all child ports are unbound and destroyed.
613 */
614static void serio_disconnect_port(struct serio *serio)
615{
616 struct serio *s, *parent;
617
618 if (serio->child) {
619 /*
620 * Children ports should be disconnected and destroyed
621 * first, staring with the leaf one, since we don't want
622 * to do recursion
623 */
624 for (s = serio; s->child; s = s->child)
625 /* empty */;
626
627 do {
628 parent = s->parent;
629
630 serio_release_driver(s);
631 serio_destroy_port(s);
632 } while ((s = parent) != serio);
633 }
634
635 /*
636 * Ok, no children left, now disconnect this port
637 */
638 serio_release_driver(serio);
639}
640
641void serio_rescan(struct serio *serio)
642{
643 serio_queue_event(serio, NULL, SERIO_RESCAN);
644}
645
646void serio_reconnect(struct serio *serio)
647{
648 serio_queue_event(serio, NULL, SERIO_RECONNECT);
649}
650
651/*
652 * Submits register request to kseriod for subsequent execution.
653 * Note that port registration is always asynchronous.
654 */
655void __serio_register_port(struct serio *serio, struct module *owner)
656{
657 serio_init_port(serio);
658 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
659}
660
661/*
662 * Synchronously unregisters serio port.
663 */
664void serio_unregister_port(struct serio *serio)
665{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500666 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 serio_disconnect_port(serio);
668 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500669 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700673 * Safely unregisters child port if one is present.
674 */
675void serio_unregister_child_port(struct serio *serio)
676{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500677 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700678 if (serio->child) {
679 serio_disconnect_port(serio->child);
680 serio_destroy_port(serio->child);
681 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500682 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700683}
684
685/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * Submits register request to kseriod for subsequent execution.
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500687 * Can be used when it is not obvious whether the serio_mutex is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * taken or not and when delayed execution is feasible.
689 */
690void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
691{
692 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
693}
694
695
696/*
697 * Serio driver operations
698 */
699
700static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
701{
702 struct serio_driver *driver = to_serio_driver(drv);
703 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
704}
705
706static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
707{
708 struct serio_driver *serio_drv = to_serio_driver(drv);
709 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
710}
711
712static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
713{
714 struct serio_driver *serio_drv = to_serio_driver(drv);
715 int retval;
716
717 retval = count;
718 if (!strncmp(buf, "manual", count)) {
719 serio_drv->manual_bind = 1;
720 } else if (!strncmp(buf, "auto", count)) {
721 serio_drv->manual_bind = 0;
722 } else {
723 retval = -EINVAL;
724 }
725
726 return retval;
727}
728
729
730static struct driver_attribute serio_driver_attrs[] = {
731 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
732 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
733 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
734 __ATTR_NULL
735};
736
737static int serio_driver_probe(struct device *dev)
738{
739 struct serio *serio = to_serio_port(dev);
740 struct serio_driver *drv = to_serio_driver(dev->driver);
741
Linus Torvalds3c803e82005-06-27 17:49:45 -0700742 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static int serio_driver_remove(struct device *dev)
746{
747 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Linus Torvalds3c803e82005-06-27 17:49:45 -0700749 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
Russell King30226f82006-01-05 14:38:53 +0000753static struct bus_type serio_bus = {
754 .name = "serio",
755 .probe = serio_driver_probe,
756 .remove = serio_driver_remove,
757};
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759void __serio_register_driver(struct serio_driver *drv, struct module *owner)
760{
761 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
764}
765
766void serio_unregister_driver(struct serio_driver *drv)
767{
768 struct serio *serio;
769
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500770 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 drv->manual_bind = 1; /* so serio_find_driver ignores it */
772
773start_over:
774 list_for_each_entry(serio, &serio_list, node) {
775 if (serio->drv == drv) {
776 serio_disconnect_port(serio);
777 serio_find_driver(serio);
778 /* we could've deleted some ports, restart */
779 goto start_over;
780 }
781 }
782
783 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500784 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 serio_pause_rx(serio);
790 serio->drv = drv;
791 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794static int serio_bus_match(struct device *dev, struct device_driver *drv)
795{
796 struct serio *serio = to_serio_port(dev);
797 struct serio_driver *serio_drv = to_serio_driver(drv);
798
799 if (serio->manual_bind || serio_drv->manual_bind)
800 return 0;
801
802 return serio_match_port(serio_drv->id_table, serio);
803}
804
805#ifdef CONFIG_HOTPLUG
806
Kay Sievers312c0042005-11-16 09:00:00 +0100807#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500808 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100809 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500810 buffer, buffer_size, &len, \
811 fmt, val); \
812 if (err) \
813 return err; \
814 } while (0)
815
Kay Sievers312c0042005-11-16 09:00:00 +0100816static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 struct serio *serio;
819 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500820 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!dev)
823 return -ENODEV;
824
825 serio = to_serio_port(dev);
826
Kay Sievers312c0042005-11-16 09:00:00 +0100827 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
828 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
829 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
830 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
831 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500832 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 envp[i] = NULL;
834
835 return 0;
836}
Kay Sievers312c0042005-11-16 09:00:00 +0100837#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839#else
840
Kay Sievers312c0042005-11-16 09:00:00 +0100841static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 return -ENODEV;
844}
845
846#endif /* CONFIG_HOTPLUG */
847
848static int serio_resume(struct device *dev)
849{
850 struct serio *serio = to_serio_port(dev);
851
Linus Torvalds3c803e82005-06-27 17:49:45 -0700852 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * Driver re-probing can take a while, so better let kseriod
855 * deal with it.
856 */
857 serio_rescan(serio);
858 }
859
860 return 0;
861}
862
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500863/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864int serio_open(struct serio *serio, struct serio_driver *drv)
865{
866 serio_set_drv(serio, drv);
867
868 if (serio->open && serio->open(serio)) {
869 serio_set_drv(serio, NULL);
870 return -1;
871 }
872 return 0;
873}
874
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500875/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876void serio_close(struct serio *serio)
877{
878 if (serio->close)
879 serio->close(serio);
880
881 serio_set_drv(serio, NULL);
882}
883
884irqreturn_t serio_interrupt(struct serio *serio,
885 unsigned char data, unsigned int dfl, struct pt_regs *regs)
886{
887 unsigned long flags;
888 irqreturn_t ret = IRQ_NONE;
889
890 spin_lock_irqsave(&serio->lock, flags);
891
892 if (likely(serio->drv)) {
893 ret = serio->drv->interrupt(serio, data, dfl, regs);
894 } else if (!dfl && serio->registered) {
895 serio_rescan(serio);
896 ret = IRQ_HANDLED;
897 }
898
899 spin_unlock_irqrestore(&serio->lock, flags);
900
901 return ret;
902}
903
904static int __init serio_init(void)
905{
Linus Torvalds3c803e82005-06-27 17:49:45 -0700906 serio_task = kthread_run(serio_thread, NULL, "kseriod");
907 if (IS_ERR(serio_task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 printk(KERN_ERR "serio: Failed to start kseriod\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700909 return PTR_ERR(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 serio_bus.dev_attrs = serio_device_attrs;
913 serio_bus.drv_attrs = serio_driver_attrs;
914 serio_bus.match = serio_bus_match;
Kay Sievers312c0042005-11-16 09:00:00 +0100915 serio_bus.uevent = serio_uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 serio_bus.resume = serio_resume;
917 bus_register(&serio_bus);
918
919 return 0;
920}
921
922static void __exit serio_exit(void)
923{
924 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700925 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500928subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929module_exit(serio_exit);