blob: 3cb99d454ecd89b7bb4006b7b20c304935f7ef8a [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
Randy Dunlap73b59a32006-07-19 01:14:55 -040065static void serio_add_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void serio_add_port(struct serio *serio);
67static void serio_destroy_port(struct serio *serio);
68static void serio_reconnect_port(struct serio *serio);
69static void serio_disconnect_port(struct serio *serio);
70
Linus Torvalds3c803e82005-06-27 17:49:45 -070071static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
72{
73 int retval;
74
Arjan van de Venc4e32e92006-02-19 00:21:55 -050075 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070076 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050077 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070078
79 return retval;
80}
81
82static int serio_reconnect_driver(struct serio *serio)
83{
84 int retval = -1;
85
Arjan van de Venc4e32e92006-02-19 00:21:55 -050086 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070087 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050089 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070090
91 return retval;
92}
93
94static void serio_disconnect_driver(struct serio *serio)
95{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050096 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070097 if (serio->drv)
98 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050099 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
103{
104 while (ids->type || ids->proto) {
105 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
106 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
107 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
108 (ids->id == SERIO_ANY || ids->id == serio->id.id))
109 return 1;
110 ids++;
111 }
112 return 0;
113}
114
115/*
116 * Basic serio -> driver core mappings
117 */
118
119static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
120{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400121 int error;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 down_write(&serio_bus.subsys.rwsem);
124
125 if (serio_match_port(drv->id_table, serio)) {
126 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700127 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 serio->dev.driver = NULL;
129 goto out;
130 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400131 error = device_bind_driver(&serio->dev);
132 if (error) {
133 printk(KERN_WARNING
134 "serio: device_bind_driver() failed "
135 "for %s (%s) and %s, error: %d\n",
136 serio->phys, serio->name,
137 drv->description, error);
138 serio_disconnect_driver(serio);
139 serio->dev.driver = NULL;
140 goto out;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400143 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 up_write(&serio_bus.subsys.rwsem);
145}
146
147static void serio_release_driver(struct serio *serio)
148{
149 down_write(&serio_bus.subsys.rwsem);
150 device_release_driver(&serio->dev);
151 up_write(&serio_bus.subsys.rwsem);
152}
153
154static void serio_find_driver(struct serio *serio)
155{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400156 int error;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 down_write(&serio_bus.subsys.rwsem);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400159 error = device_attach(&serio->dev);
160 if (error < 0)
161 printk(KERN_WARNING
162 "serio: device_attach() failed for %s (%s), error: %d\n",
163 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 up_write(&serio_bus.subsys.rwsem);
165}
166
167
168/*
169 * Serio event processing.
170 */
171
172enum serio_event_type {
173 SERIO_RESCAN,
174 SERIO_RECONNECT,
175 SERIO_REGISTER_PORT,
176 SERIO_UNREGISTER_PORT,
177 SERIO_REGISTER_DRIVER,
178};
179
180struct serio_event {
181 enum serio_event_type type;
182 void *object;
183 struct module *owner;
184 struct list_head node;
185};
186
187static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
188static LIST_HEAD(serio_event_list);
189static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700190static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192static void serio_queue_event(void *object, struct module *owner,
193 enum serio_event_type event_type)
194{
195 unsigned long flags;
196 struct serio_event *event;
197
198 spin_lock_irqsave(&serio_event_lock, flags);
199
200 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700201 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * starting with the most recent one. If event is the same we
203 * do not need add new one. If event is of different type we
204 * need to add this event and should not look further because
205 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 list_for_each_entry_reverse(event, &serio_event_list, node) {
208 if (event->object == object) {
209 if (event->type == event_type)
210 goto out;
211 break;
212 }
213 }
214
215 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
216 if (!try_module_get(owner)) {
217 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk9d921112006-03-14 00:13:29 -0500218 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 goto out;
220 }
221
222 event->type = event_type;
223 event->object = object;
224 event->owner = owner;
225
226 list_add_tail(&event->node, &serio_event_list);
227 wake_up(&serio_wait);
228 } else {
229 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
230 }
231out:
232 spin_unlock_irqrestore(&serio_event_lock, flags);
233}
234
235static void serio_free_event(struct serio_event *event)
236{
237 module_put(event->owner);
238 kfree(event);
239}
240
241static void serio_remove_duplicate_events(struct serio_event *event)
242{
243 struct list_head *node, *next;
244 struct serio_event *e;
245 unsigned long flags;
246
247 spin_lock_irqsave(&serio_event_lock, flags);
248
249 list_for_each_safe(node, next, &serio_event_list) {
250 e = list_entry(node, struct serio_event, node);
251 if (event->object == e->object) {
252 /*
253 * If this event is of different type we should not
254 * look further - we only suppress duplicate events
255 * that were sent back-to-back.
256 */
257 if (event->type != e->type)
258 break;
259
260 list_del_init(node);
261 serio_free_event(e);
262 }
263 }
264
265 spin_unlock_irqrestore(&serio_event_lock, flags);
266}
267
268
269static struct serio_event *serio_get_event(void)
270{
271 struct serio_event *event;
272 struct list_head *node;
273 unsigned long flags;
274
275 spin_lock_irqsave(&serio_event_lock, flags);
276
277 if (list_empty(&serio_event_list)) {
278 spin_unlock_irqrestore(&serio_event_lock, flags);
279 return NULL;
280 }
281
282 node = serio_event_list.next;
283 event = list_entry(node, struct serio_event, node);
284 list_del_init(node);
285
286 spin_unlock_irqrestore(&serio_event_lock, flags);
287
288 return event;
289}
290
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500291static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500295 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500297 /*
298 * Note that we handle only one event here to give swsusp
299 * a chance to freeze kseriod thread. Serio events should
300 * be pretty rare so we are not concerned about taking
301 * performance hit.
302 */
303 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 switch (event->type) {
306 case SERIO_REGISTER_PORT:
307 serio_add_port(event->object);
308 break;
309
310 case SERIO_UNREGISTER_PORT:
311 serio_disconnect_port(event->object);
312 serio_destroy_port(event->object);
313 break;
314
315 case SERIO_RECONNECT:
316 serio_reconnect_port(event->object);
317 break;
318
319 case SERIO_RESCAN:
320 serio_disconnect_port(event->object);
321 serio_find_driver(event->object);
322 break;
323
324 case SERIO_REGISTER_DRIVER:
Randy Dunlap73b59a32006-07-19 01:14:55 -0400325 serio_add_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327
328 default:
329 break;
330 }
331
332 serio_remove_duplicate_events(event);
333 serio_free_event(event);
334 }
335
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500336 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
340 * Remove all events that have been submitted for a given serio port.
341 */
342static void serio_remove_pending_events(struct serio *serio)
343{
344 struct list_head *node, *next;
345 struct serio_event *event;
346 unsigned long flags;
347
348 spin_lock_irqsave(&serio_event_lock, flags);
349
350 list_for_each_safe(node, next, &serio_event_list) {
351 event = list_entry(node, struct serio_event, node);
352 if (event->object == serio) {
353 list_del_init(node);
354 serio_free_event(event);
355 }
356 }
357
358 spin_unlock_irqrestore(&serio_event_lock, flags);
359}
360
361/*
362 * Destroy child serio port (if any) that has not been fully registered yet.
363 *
364 * Note that we rely on the fact that port can have only one child and therefore
365 * only one child registration request can be pending. Additionally, children
366 * are registered by driver's connect() handler so there can't be a grandchild
367 * pending registration together with a child.
368 */
369static struct serio *serio_get_pending_child(struct serio *parent)
370{
371 struct serio_event *event;
372 struct serio *serio, *child = NULL;
373 unsigned long flags;
374
375 spin_lock_irqsave(&serio_event_lock, flags);
376
377 list_for_each_entry(event, &serio_event_list, node) {
378 if (event->type == SERIO_REGISTER_PORT) {
379 serio = event->object;
380 if (serio->parent == parent) {
381 child = serio;
382 break;
383 }
384 }
385 }
386
387 spin_unlock_irqrestore(&serio_event_lock, flags);
388 return child;
389}
390
391static int serio_thread(void *nothing)
392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500394 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700395 wait_event_interruptible(serio_wait,
396 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700397 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700398 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404
405/*
406 * Serio port operations
407 */
408
Yani Ioannoue404e272005-05-17 06:42:58 -0400409static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct serio *serio = to_serio_port(dev);
412 return sprintf(buf, "%s\n", serio->name);
413}
414
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500415static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
416{
417 struct serio *serio = to_serio_port(dev);
418
419 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
420 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
421}
422
Yani Ioannoue404e272005-05-17 06:42:58 -0400423static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct serio *serio = to_serio_port(dev);
426 return sprintf(buf, "%02x\n", serio->id.type);
427}
428
Yani Ioannoue404e272005-05-17 06:42:58 -0400429static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct serio *serio = to_serio_port(dev);
432 return sprintf(buf, "%02x\n", serio->id.proto);
433}
434
Yani Ioannoue404e272005-05-17 06:42:58 -0400435static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct serio *serio = to_serio_port(dev);
438 return sprintf(buf, "%02x\n", serio->id.id);
439}
440
Yani Ioannoue404e272005-05-17 06:42:58 -0400441static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct serio *serio = to_serio_port(dev);
444 return sprintf(buf, "%02x\n", serio->id.extra);
445}
446
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700447static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
448static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
449static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
450static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
451
452static struct attribute *serio_device_id_attrs[] = {
453 &dev_attr_type.attr,
454 &dev_attr_proto.attr,
455 &dev_attr_id.attr,
456 &dev_attr_extra.attr,
457 NULL
458};
459
460static struct attribute_group serio_id_attr_group = {
461 .name = "id",
462 .attrs = serio_device_id_attrs,
463};
464
Yani Ioannoue404e272005-05-17 06:42:58 -0400465static 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 -0700466{
467 struct serio *serio = to_serio_port(dev);
468 struct device_driver *drv;
469 int retval;
470
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500471 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (retval)
473 return retval;
474
475 retval = count;
476 if (!strncmp(buf, "none", count)) {
477 serio_disconnect_port(serio);
478 } else if (!strncmp(buf, "reconnect", count)) {
479 serio_reconnect_port(serio);
480 } else if (!strncmp(buf, "rescan", count)) {
481 serio_disconnect_port(serio);
482 serio_find_driver(serio);
483 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
484 serio_disconnect_port(serio);
485 serio_bind_driver(serio, to_serio_driver(drv));
486 put_driver(drv);
487 } else {
488 retval = -EINVAL;
489 }
490
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500491 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 return retval;
494}
495
Yani Ioannoue404e272005-05-17 06:42:58 -0400496static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct serio *serio = to_serio_port(dev);
499 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
500}
501
Yani Ioannoue404e272005-05-17 06:42:58 -0400502static 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 -0700503{
504 struct serio *serio = to_serio_port(dev);
505 int retval;
506
507 retval = count;
508 if (!strncmp(buf, "manual", count)) {
509 serio->manual_bind = 1;
510 } else if (!strncmp(buf, "auto", count)) {
511 serio->manual_bind = 0;
512 } else {
513 retval = -EINVAL;
514 }
515
516 return retval;
517}
518
519static struct device_attribute serio_device_attrs[] = {
520 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500521 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
523 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
524 __ATTR_NULL
525};
526
527
528static void serio_release_port(struct device *dev)
529{
530 struct serio *serio = to_serio_port(dev);
531
532 kfree(serio);
533 module_put(THIS_MODULE);
534}
535
536/*
537 * Prepare serio port for registration.
538 */
539static void serio_init_port(struct serio *serio)
540{
541 static atomic_t serio_no = ATOMIC_INIT(0);
542
543 __module_get(THIS_MODULE);
544
Randy Dunlap73b59a32006-07-19 01:14:55 -0400545 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500547 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 device_initialize(&serio->dev);
549 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
550 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
551 serio->dev.bus = &serio_bus;
552 serio->dev.release = serio_release_port;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400553 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400555 serio->depth = serio->parent->depth + 1;
556 } else
557 serio->depth = 0;
558 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
562 * Complete serio port registration.
563 * Driver core will attempt to find appropriate driver for the port.
564 */
565static void serio_add_port(struct serio *serio)
566{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400567 int error;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (serio->parent) {
570 serio_pause_rx(serio->parent);
571 serio->parent->child = serio;
572 serio_continue_rx(serio->parent);
573 }
574
575 list_add_tail(&serio->node, &serio_list);
576 if (serio->start)
577 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400578 error = device_add(&serio->dev);
579 if (error)
580 printk(KERN_ERR
581 "serio: device_add() failed for %s (%s), error: %d\n",
582 serio->phys, serio->name, error);
583 else {
584 serio->registered = 1;
585 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
586 if (error)
587 printk(KERN_ERR
588 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
589 serio->phys, serio->name, error);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*
594 * serio_destroy_port() completes deregistration process and removes
595 * port from the system
596 */
597static void serio_destroy_port(struct serio *serio)
598{
599 struct serio *child;
600
601 child = serio_get_pending_child(serio);
602 if (child) {
603 serio_remove_pending_events(child);
604 put_device(&child->dev);
605 }
606
607 if (serio->stop)
608 serio->stop(serio);
609
610 if (serio->parent) {
611 serio_pause_rx(serio->parent);
612 serio->parent->child = NULL;
613 serio_continue_rx(serio->parent);
614 serio->parent = NULL;
615 }
616
617 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700618 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 serio->registered = 0;
621 }
622
Randy Dunlap73b59a32006-07-19 01:14:55 -0400623 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 serio_remove_pending_events(serio);
625 put_device(&serio->dev);
626}
627
628/*
629 * Reconnect serio port and all its children (re-initialize attached devices)
630 */
631static void serio_reconnect_port(struct serio *serio)
632{
633 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700634 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 serio_disconnect_port(serio);
636 serio_find_driver(serio);
637 /* Ok, old children are now gone, we are done */
638 break;
639 }
640 serio = serio->child;
641 } while (serio);
642}
643
644/*
645 * serio_disconnect_port() unbinds a port from its driver. As a side effect
646 * all child ports are unbound and destroyed.
647 */
648static void serio_disconnect_port(struct serio *serio)
649{
650 struct serio *s, *parent;
651
652 if (serio->child) {
653 /*
654 * Children ports should be disconnected and destroyed
655 * first, staring with the leaf one, since we don't want
656 * to do recursion
657 */
658 for (s = serio; s->child; s = s->child)
659 /* empty */;
660
661 do {
662 parent = s->parent;
663
664 serio_release_driver(s);
665 serio_destroy_port(s);
666 } while ((s = parent) != serio);
667 }
668
669 /*
670 * Ok, no children left, now disconnect this port
671 */
672 serio_release_driver(serio);
673}
674
675void serio_rescan(struct serio *serio)
676{
677 serio_queue_event(serio, NULL, SERIO_RESCAN);
678}
679
680void serio_reconnect(struct serio *serio)
681{
682 serio_queue_event(serio, NULL, SERIO_RECONNECT);
683}
684
685/*
686 * Submits register request to kseriod for subsequent execution.
687 * Note that port registration is always asynchronous.
688 */
689void __serio_register_port(struct serio *serio, struct module *owner)
690{
691 serio_init_port(serio);
692 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
693}
694
695/*
696 * Synchronously unregisters serio port.
697 */
698void serio_unregister_port(struct serio *serio)
699{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500700 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 serio_disconnect_port(serio);
702 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500703 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700707 * Safely unregisters child port if one is present.
708 */
709void serio_unregister_child_port(struct serio *serio)
710{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500711 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700712 if (serio->child) {
713 serio_disconnect_port(serio->child);
714 serio_destroy_port(serio->child);
715 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500716 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700717}
718
719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * Submits register request to kseriod for subsequent execution.
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500721 * Can be used when it is not obvious whether the serio_mutex is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * taken or not and when delayed execution is feasible.
723 */
724void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
725{
726 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
727}
728
729
730/*
731 * Serio driver operations
732 */
733
734static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
735{
736 struct serio_driver *driver = to_serio_driver(drv);
737 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
738}
739
740static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
741{
742 struct serio_driver *serio_drv = to_serio_driver(drv);
743 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
744}
745
746static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
747{
748 struct serio_driver *serio_drv = to_serio_driver(drv);
749 int retval;
750
751 retval = count;
752 if (!strncmp(buf, "manual", count)) {
753 serio_drv->manual_bind = 1;
754 } else if (!strncmp(buf, "auto", count)) {
755 serio_drv->manual_bind = 0;
756 } else {
757 retval = -EINVAL;
758 }
759
760 return retval;
761}
762
763
764static struct driver_attribute serio_driver_attrs[] = {
765 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
766 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
767 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
768 __ATTR_NULL
769};
770
771static int serio_driver_probe(struct device *dev)
772{
773 struct serio *serio = to_serio_port(dev);
774 struct serio_driver *drv = to_serio_driver(dev->driver);
775
Linus Torvalds3c803e82005-06-27 17:49:45 -0700776 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779static int serio_driver_remove(struct device *dev)
780{
781 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds3c803e82005-06-27 17:49:45 -0700783 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
786
Randy Dunlap73b59a32006-07-19 01:14:55 -0400787static void serio_add_driver(struct serio_driver *drv)
788{
789 int error;
790
791 error = driver_register(&drv->driver);
792 if (error)
793 printk(KERN_ERR
794 "serio: driver_register() failed for %s, error: %d\n",
795 drv->driver.name, error);
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798void __serio_register_driver(struct serio_driver *drv, struct module *owner)
799{
800 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
803}
804
805void serio_unregister_driver(struct serio_driver *drv)
806{
807 struct serio *serio;
808
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500809 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 drv->manual_bind = 1; /* so serio_find_driver ignores it */
811
812start_over:
813 list_for_each_entry(serio, &serio_list, node) {
814 if (serio->drv == drv) {
815 serio_disconnect_port(serio);
816 serio_find_driver(serio);
817 /* we could've deleted some ports, restart */
818 goto start_over;
819 }
820 }
821
822 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500823 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 serio_pause_rx(serio);
829 serio->drv = drv;
830 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static int serio_bus_match(struct device *dev, struct device_driver *drv)
834{
835 struct serio *serio = to_serio_port(dev);
836 struct serio_driver *serio_drv = to_serio_driver(drv);
837
838 if (serio->manual_bind || serio_drv->manual_bind)
839 return 0;
840
841 return serio_match_port(serio_drv->id_table, serio);
842}
843
844#ifdef CONFIG_HOTPLUG
845
Kay Sievers312c0042005-11-16 09:00:00 +0100846#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500847 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100848 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500849 buffer, buffer_size, &len, \
850 fmt, val); \
851 if (err) \
852 return err; \
853 } while (0)
854
Kay Sievers312c0042005-11-16 09:00:00 +0100855static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct serio *serio;
858 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500859 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (!dev)
862 return -ENODEV;
863
864 serio = to_serio_port(dev);
865
Kay Sievers312c0042005-11-16 09:00:00 +0100866 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
867 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
868 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
869 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
870 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500871 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 envp[i] = NULL;
873
874 return 0;
875}
Kay Sievers312c0042005-11-16 09:00:00 +0100876#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878#else
879
Kay Sievers312c0042005-11-16 09:00:00 +0100880static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 return -ENODEV;
883}
884
885#endif /* CONFIG_HOTPLUG */
886
887static int serio_resume(struct device *dev)
888{
889 struct serio *serio = to_serio_port(dev);
890
Linus Torvalds3c803e82005-06-27 17:49:45 -0700891 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * Driver re-probing can take a while, so better let kseriod
894 * deal with it.
895 */
896 serio_rescan(serio);
897 }
898
899 return 0;
900}
901
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500902/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903int serio_open(struct serio *serio, struct serio_driver *drv)
904{
905 serio_set_drv(serio, drv);
906
907 if (serio->open && serio->open(serio)) {
908 serio_set_drv(serio, NULL);
909 return -1;
910 }
911 return 0;
912}
913
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500914/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915void serio_close(struct serio *serio)
916{
917 if (serio->close)
918 serio->close(serio);
919
920 serio_set_drv(serio, NULL);
921}
922
923irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100924 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 unsigned long flags;
927 irqreturn_t ret = IRQ_NONE;
928
929 spin_lock_irqsave(&serio->lock, flags);
930
931 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100932 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else if (!dfl && serio->registered) {
934 serio_rescan(serio);
935 ret = IRQ_HANDLED;
936 }
937
938 spin_unlock_irqrestore(&serio->lock, flags);
939
940 return ret;
941}
942
Marton Nemeth1ea2a692006-11-02 23:27:21 -0500943static struct bus_type serio_bus = {
944 .name = "serio",
945 .dev_attrs = serio_device_attrs,
946 .drv_attrs = serio_driver_attrs,
947 .match = serio_bus_match,
948 .uevent = serio_uevent,
949 .probe = serio_driver_probe,
950 .remove = serio_driver_remove,
951 .resume = serio_resume,
952};
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int __init serio_init(void)
955{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400956 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Randy Dunlap73b59a32006-07-19 01:14:55 -0400958 error = bus_register(&serio_bus);
959 if (error) {
960 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
961 return error;
962 }
963
964 serio_task = kthread_run(serio_thread, NULL, "kseriod");
965 if (IS_ERR(serio_task)) {
966 bus_unregister(&serio_bus);
967 error = PTR_ERR(serio_task);
968 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
969 return error;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 return 0;
973}
974
975static void __exit serio_exit(void)
976{
977 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700978 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500981subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982module_exit(serio_exit);