blob: 960fae3c3ceadfec39309fb7429f67a495ce4b0d [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{
121 down_write(&serio_bus.subsys.rwsem);
122
123 if (serio_match_port(drv->id_table, serio)) {
124 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700125 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 serio->dev.driver = NULL;
127 goto out;
128 }
129 device_bind_driver(&serio->dev);
130 }
131out:
132 up_write(&serio_bus.subsys.rwsem);
133}
134
135static void serio_release_driver(struct serio *serio)
136{
137 down_write(&serio_bus.subsys.rwsem);
138 device_release_driver(&serio->dev);
139 up_write(&serio_bus.subsys.rwsem);
140}
141
142static void serio_find_driver(struct serio *serio)
143{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400144 int error;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 down_write(&serio_bus.subsys.rwsem);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400147 error = device_attach(&serio->dev);
148 if (error < 0)
149 printk(KERN_WARNING
150 "serio: device_attach() failed for %s (%s), error: %d\n",
151 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 up_write(&serio_bus.subsys.rwsem);
153}
154
155
156/*
157 * Serio event processing.
158 */
159
160enum serio_event_type {
161 SERIO_RESCAN,
162 SERIO_RECONNECT,
163 SERIO_REGISTER_PORT,
164 SERIO_UNREGISTER_PORT,
165 SERIO_REGISTER_DRIVER,
166};
167
168struct serio_event {
169 enum serio_event_type type;
170 void *object;
171 struct module *owner;
172 struct list_head node;
173};
174
175static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
176static LIST_HEAD(serio_event_list);
177static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700178static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static void serio_queue_event(void *object, struct module *owner,
181 enum serio_event_type event_type)
182{
183 unsigned long flags;
184 struct serio_event *event;
185
186 spin_lock_irqsave(&serio_event_lock, flags);
187
188 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700189 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * starting with the most recent one. If event is the same we
191 * do not need add new one. If event is of different type we
192 * need to add this event and should not look further because
193 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 list_for_each_entry_reverse(event, &serio_event_list, node) {
196 if (event->object == object) {
197 if (event->type == event_type)
198 goto out;
199 break;
200 }
201 }
202
203 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
204 if (!try_module_get(owner)) {
205 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk9d921112006-03-14 00:13:29 -0500206 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out;
208 }
209
210 event->type = event_type;
211 event->object = object;
212 event->owner = owner;
213
214 list_add_tail(&event->node, &serio_event_list);
215 wake_up(&serio_wait);
216 } else {
217 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
218 }
219out:
220 spin_unlock_irqrestore(&serio_event_lock, flags);
221}
222
223static void serio_free_event(struct serio_event *event)
224{
225 module_put(event->owner);
226 kfree(event);
227}
228
229static void serio_remove_duplicate_events(struct serio_event *event)
230{
231 struct list_head *node, *next;
232 struct serio_event *e;
233 unsigned long flags;
234
235 spin_lock_irqsave(&serio_event_lock, flags);
236
237 list_for_each_safe(node, next, &serio_event_list) {
238 e = list_entry(node, struct serio_event, node);
239 if (event->object == e->object) {
240 /*
241 * If this event is of different type we should not
242 * look further - we only suppress duplicate events
243 * that were sent back-to-back.
244 */
245 if (event->type != e->type)
246 break;
247
248 list_del_init(node);
249 serio_free_event(e);
250 }
251 }
252
253 spin_unlock_irqrestore(&serio_event_lock, flags);
254}
255
256
257static struct serio_event *serio_get_event(void)
258{
259 struct serio_event *event;
260 struct list_head *node;
261 unsigned long flags;
262
263 spin_lock_irqsave(&serio_event_lock, flags);
264
265 if (list_empty(&serio_event_list)) {
266 spin_unlock_irqrestore(&serio_event_lock, flags);
267 return NULL;
268 }
269
270 node = serio_event_list.next;
271 event = list_entry(node, struct serio_event, node);
272 list_del_init(node);
273
274 spin_unlock_irqrestore(&serio_event_lock, flags);
275
276 return event;
277}
278
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500279static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500283 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500285 /*
286 * Note that we handle only one event here to give swsusp
287 * a chance to freeze kseriod thread. Serio events should
288 * be pretty rare so we are not concerned about taking
289 * performance hit.
290 */
291 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 switch (event->type) {
294 case SERIO_REGISTER_PORT:
295 serio_add_port(event->object);
296 break;
297
298 case SERIO_UNREGISTER_PORT:
299 serio_disconnect_port(event->object);
300 serio_destroy_port(event->object);
301 break;
302
303 case SERIO_RECONNECT:
304 serio_reconnect_port(event->object);
305 break;
306
307 case SERIO_RESCAN:
308 serio_disconnect_port(event->object);
309 serio_find_driver(event->object);
310 break;
311
312 case SERIO_REGISTER_DRIVER:
Randy Dunlap73b59a32006-07-19 01:14:55 -0400313 serio_add_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315
316 default:
317 break;
318 }
319
320 serio_remove_duplicate_events(event);
321 serio_free_event(event);
322 }
323
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500324 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/*
328 * Remove all events that have been submitted for a given serio port.
329 */
330static void serio_remove_pending_events(struct serio *serio)
331{
332 struct list_head *node, *next;
333 struct serio_event *event;
334 unsigned long flags;
335
336 spin_lock_irqsave(&serio_event_lock, flags);
337
338 list_for_each_safe(node, next, &serio_event_list) {
339 event = list_entry(node, struct serio_event, node);
340 if (event->object == serio) {
341 list_del_init(node);
342 serio_free_event(event);
343 }
344 }
345
346 spin_unlock_irqrestore(&serio_event_lock, flags);
347}
348
349/*
350 * Destroy child serio port (if any) that has not been fully registered yet.
351 *
352 * Note that we rely on the fact that port can have only one child and therefore
353 * only one child registration request can be pending. Additionally, children
354 * are registered by driver's connect() handler so there can't be a grandchild
355 * pending registration together with a child.
356 */
357static struct serio *serio_get_pending_child(struct serio *parent)
358{
359 struct serio_event *event;
360 struct serio *serio, *child = NULL;
361 unsigned long flags;
362
363 spin_lock_irqsave(&serio_event_lock, flags);
364
365 list_for_each_entry(event, &serio_event_list, node) {
366 if (event->type == SERIO_REGISTER_PORT) {
367 serio = event->object;
368 if (serio->parent == parent) {
369 child = serio;
370 break;
371 }
372 }
373 }
374
375 spin_unlock_irqrestore(&serio_event_lock, flags);
376 return child;
377}
378
379static int serio_thread(void *nothing)
380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500382 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700383 wait_event_interruptible(serio_wait,
384 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700385 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700386 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392
393/*
394 * Serio port operations
395 */
396
Yani Ioannoue404e272005-05-17 06:42:58 -0400397static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct serio *serio = to_serio_port(dev);
400 return sprintf(buf, "%s\n", serio->name);
401}
402
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500403static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
404{
405 struct serio *serio = to_serio_port(dev);
406
407 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
408 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
409}
410
Yani Ioannoue404e272005-05-17 06:42:58 -0400411static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct serio *serio = to_serio_port(dev);
414 return sprintf(buf, "%02x\n", serio->id.type);
415}
416
Yani Ioannoue404e272005-05-17 06:42:58 -0400417static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct serio *serio = to_serio_port(dev);
420 return sprintf(buf, "%02x\n", serio->id.proto);
421}
422
Yani Ioannoue404e272005-05-17 06:42:58 -0400423static ssize_t serio_show_id_id(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.id);
427}
428
Yani Ioannoue404e272005-05-17 06:42:58 -0400429static ssize_t serio_show_id_extra(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.extra);
433}
434
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700435static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
436static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
437static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
438static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
439
440static struct attribute *serio_device_id_attrs[] = {
441 &dev_attr_type.attr,
442 &dev_attr_proto.attr,
443 &dev_attr_id.attr,
444 &dev_attr_extra.attr,
445 NULL
446};
447
448static struct attribute_group serio_id_attr_group = {
449 .name = "id",
450 .attrs = serio_device_id_attrs,
451};
452
Yani Ioannoue404e272005-05-17 06:42:58 -0400453static 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 -0700454{
455 struct serio *serio = to_serio_port(dev);
456 struct device_driver *drv;
457 int retval;
458
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500459 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (retval)
461 return retval;
462
463 retval = count;
464 if (!strncmp(buf, "none", count)) {
465 serio_disconnect_port(serio);
466 } else if (!strncmp(buf, "reconnect", count)) {
467 serio_reconnect_port(serio);
468 } else if (!strncmp(buf, "rescan", count)) {
469 serio_disconnect_port(serio);
470 serio_find_driver(serio);
471 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
472 serio_disconnect_port(serio);
473 serio_bind_driver(serio, to_serio_driver(drv));
474 put_driver(drv);
475 } else {
476 retval = -EINVAL;
477 }
478
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500479 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return retval;
482}
483
Yani Ioannoue404e272005-05-17 06:42:58 -0400484static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct serio *serio = to_serio_port(dev);
487 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
488}
489
Yani Ioannoue404e272005-05-17 06:42:58 -0400490static 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 -0700491{
492 struct serio *serio = to_serio_port(dev);
493 int retval;
494
495 retval = count;
496 if (!strncmp(buf, "manual", count)) {
497 serio->manual_bind = 1;
498 } else if (!strncmp(buf, "auto", count)) {
499 serio->manual_bind = 0;
500 } else {
501 retval = -EINVAL;
502 }
503
504 return retval;
505}
506
507static struct device_attribute serio_device_attrs[] = {
508 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500509 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
511 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
512 __ATTR_NULL
513};
514
515
516static void serio_release_port(struct device *dev)
517{
518 struct serio *serio = to_serio_port(dev);
519
520 kfree(serio);
521 module_put(THIS_MODULE);
522}
523
524/*
525 * Prepare serio port for registration.
526 */
527static void serio_init_port(struct serio *serio)
528{
529 static atomic_t serio_no = ATOMIC_INIT(0);
530
531 __module_get(THIS_MODULE);
532
Randy Dunlap73b59a32006-07-19 01:14:55 -0400533 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500535 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 device_initialize(&serio->dev);
537 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
538 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
539 serio->dev.bus = &serio_bus;
540 serio->dev.release = serio_release_port;
541 if (serio->parent)
542 serio->dev.parent = &serio->parent->dev;
543}
544
545/*
546 * Complete serio port registration.
547 * Driver core will attempt to find appropriate driver for the port.
548 */
549static void serio_add_port(struct serio *serio)
550{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400551 int error;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (serio->parent) {
554 serio_pause_rx(serio->parent);
555 serio->parent->child = serio;
556 serio_continue_rx(serio->parent);
557 }
558
559 list_add_tail(&serio->node, &serio_list);
560 if (serio->start)
561 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400562 error = device_add(&serio->dev);
563 if (error)
564 printk(KERN_ERR
565 "serio: device_add() failed for %s (%s), error: %d\n",
566 serio->phys, serio->name, error);
567 else {
568 serio->registered = 1;
569 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
570 if (error)
571 printk(KERN_ERR
572 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
573 serio->phys, serio->name, error);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * serio_destroy_port() completes deregistration process and removes
579 * port from the system
580 */
581static void serio_destroy_port(struct serio *serio)
582{
583 struct serio *child;
584
585 child = serio_get_pending_child(serio);
586 if (child) {
587 serio_remove_pending_events(child);
588 put_device(&child->dev);
589 }
590
591 if (serio->stop)
592 serio->stop(serio);
593
594 if (serio->parent) {
595 serio_pause_rx(serio->parent);
596 serio->parent->child = NULL;
597 serio_continue_rx(serio->parent);
598 serio->parent = NULL;
599 }
600
601 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700602 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 serio->registered = 0;
605 }
606
Randy Dunlap73b59a32006-07-19 01:14:55 -0400607 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 serio_remove_pending_events(serio);
609 put_device(&serio->dev);
610}
611
612/*
613 * Reconnect serio port and all its children (re-initialize attached devices)
614 */
615static void serio_reconnect_port(struct serio *serio)
616{
617 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700618 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 serio_disconnect_port(serio);
620 serio_find_driver(serio);
621 /* Ok, old children are now gone, we are done */
622 break;
623 }
624 serio = serio->child;
625 } while (serio);
626}
627
628/*
629 * serio_disconnect_port() unbinds a port from its driver. As a side effect
630 * all child ports are unbound and destroyed.
631 */
632static void serio_disconnect_port(struct serio *serio)
633{
634 struct serio *s, *parent;
635
636 if (serio->child) {
637 /*
638 * Children ports should be disconnected and destroyed
639 * first, staring with the leaf one, since we don't want
640 * to do recursion
641 */
642 for (s = serio; s->child; s = s->child)
643 /* empty */;
644
645 do {
646 parent = s->parent;
647
648 serio_release_driver(s);
649 serio_destroy_port(s);
650 } while ((s = parent) != serio);
651 }
652
653 /*
654 * Ok, no children left, now disconnect this port
655 */
656 serio_release_driver(serio);
657}
658
659void serio_rescan(struct serio *serio)
660{
661 serio_queue_event(serio, NULL, SERIO_RESCAN);
662}
663
664void serio_reconnect(struct serio *serio)
665{
666 serio_queue_event(serio, NULL, SERIO_RECONNECT);
667}
668
669/*
670 * Submits register request to kseriod for subsequent execution.
671 * Note that port registration is always asynchronous.
672 */
673void __serio_register_port(struct serio *serio, struct module *owner)
674{
675 serio_init_port(serio);
676 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
677}
678
679/*
680 * Synchronously unregisters serio port.
681 */
682void serio_unregister_port(struct serio *serio)
683{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500684 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 serio_disconnect_port(serio);
686 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500687 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700691 * Safely unregisters child port if one is present.
692 */
693void serio_unregister_child_port(struct serio *serio)
694{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500695 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700696 if (serio->child) {
697 serio_disconnect_port(serio->child);
698 serio_destroy_port(serio->child);
699 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500700 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700701}
702
703/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * Submits register request to kseriod for subsequent execution.
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500705 * Can be used when it is not obvious whether the serio_mutex is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * taken or not and when delayed execution is feasible.
707 */
708void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
709{
710 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
711}
712
713
714/*
715 * Serio driver operations
716 */
717
718static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
719{
720 struct serio_driver *driver = to_serio_driver(drv);
721 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
722}
723
724static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
725{
726 struct serio_driver *serio_drv = to_serio_driver(drv);
727 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
728}
729
730static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
731{
732 struct serio_driver *serio_drv = to_serio_driver(drv);
733 int retval;
734
735 retval = count;
736 if (!strncmp(buf, "manual", count)) {
737 serio_drv->manual_bind = 1;
738 } else if (!strncmp(buf, "auto", count)) {
739 serio_drv->manual_bind = 0;
740 } else {
741 retval = -EINVAL;
742 }
743
744 return retval;
745}
746
747
748static struct driver_attribute serio_driver_attrs[] = {
749 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
750 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
751 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
752 __ATTR_NULL
753};
754
755static int serio_driver_probe(struct device *dev)
756{
757 struct serio *serio = to_serio_port(dev);
758 struct serio_driver *drv = to_serio_driver(dev->driver);
759
Linus Torvalds3c803e82005-06-27 17:49:45 -0700760 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763static int serio_driver_remove(struct device *dev)
764{
765 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds3c803e82005-06-27 17:49:45 -0700767 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769}
770
Russell King30226f82006-01-05 14:38:53 +0000771static struct bus_type serio_bus = {
772 .name = "serio",
773 .probe = serio_driver_probe,
774 .remove = serio_driver_remove,
775};
776
Randy Dunlap73b59a32006-07-19 01:14:55 -0400777static void serio_add_driver(struct serio_driver *drv)
778{
779 int error;
780
781 error = driver_register(&drv->driver);
782 if (error)
783 printk(KERN_ERR
784 "serio: driver_register() failed for %s, error: %d\n",
785 drv->driver.name, error);
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788void __serio_register_driver(struct serio_driver *drv, struct module *owner)
789{
790 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
793}
794
795void serio_unregister_driver(struct serio_driver *drv)
796{
797 struct serio *serio;
798
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500799 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 drv->manual_bind = 1; /* so serio_find_driver ignores it */
801
802start_over:
803 list_for_each_entry(serio, &serio_list, node) {
804 if (serio->drv == drv) {
805 serio_disconnect_port(serio);
806 serio_find_driver(serio);
807 /* we could've deleted some ports, restart */
808 goto start_over;
809 }
810 }
811
812 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500813 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 serio_pause_rx(serio);
819 serio->drv = drv;
820 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823static int serio_bus_match(struct device *dev, struct device_driver *drv)
824{
825 struct serio *serio = to_serio_port(dev);
826 struct serio_driver *serio_drv = to_serio_driver(drv);
827
828 if (serio->manual_bind || serio_drv->manual_bind)
829 return 0;
830
831 return serio_match_port(serio_drv->id_table, serio);
832}
833
834#ifdef CONFIG_HOTPLUG
835
Kay Sievers312c0042005-11-16 09:00:00 +0100836#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500837 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100838 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500839 buffer, buffer_size, &len, \
840 fmt, val); \
841 if (err) \
842 return err; \
843 } while (0)
844
Kay Sievers312c0042005-11-16 09:00:00 +0100845static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct serio *serio;
848 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500849 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (!dev)
852 return -ENODEV;
853
854 serio = to_serio_port(dev);
855
Kay Sievers312c0042005-11-16 09:00:00 +0100856 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
857 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
858 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
859 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
860 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500861 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 envp[i] = NULL;
863
864 return 0;
865}
Kay Sievers312c0042005-11-16 09:00:00 +0100866#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868#else
869
Kay Sievers312c0042005-11-16 09:00:00 +0100870static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 return -ENODEV;
873}
874
875#endif /* CONFIG_HOTPLUG */
876
877static int serio_resume(struct device *dev)
878{
879 struct serio *serio = to_serio_port(dev);
880
Linus Torvalds3c803e82005-06-27 17:49:45 -0700881 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
883 * Driver re-probing can take a while, so better let kseriod
884 * deal with it.
885 */
886 serio_rescan(serio);
887 }
888
889 return 0;
890}
891
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500892/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893int serio_open(struct serio *serio, struct serio_driver *drv)
894{
895 serio_set_drv(serio, drv);
896
897 if (serio->open && serio->open(serio)) {
898 serio_set_drv(serio, NULL);
899 return -1;
900 }
901 return 0;
902}
903
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500904/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905void serio_close(struct serio *serio)
906{
907 if (serio->close)
908 serio->close(serio);
909
910 serio_set_drv(serio, NULL);
911}
912
913irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100914 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 unsigned long flags;
917 irqreturn_t ret = IRQ_NONE;
918
919 spin_lock_irqsave(&serio->lock, flags);
920
921 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100922 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else if (!dfl && serio->registered) {
924 serio_rescan(serio);
925 ret = IRQ_HANDLED;
926 }
927
928 spin_unlock_irqrestore(&serio->lock, flags);
929
930 return ret;
931}
932
933static int __init serio_init(void)
934{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400935 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 serio_bus.dev_attrs = serio_device_attrs;
938 serio_bus.drv_attrs = serio_driver_attrs;
939 serio_bus.match = serio_bus_match;
Kay Sievers312c0042005-11-16 09:00:00 +0100940 serio_bus.uevent = serio_uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 serio_bus.resume = serio_resume;
Randy Dunlap73b59a32006-07-19 01:14:55 -0400942 error = bus_register(&serio_bus);
943 if (error) {
944 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
945 return error;
946 }
947
948 serio_task = kthread_run(serio_thread, NULL, "kseriod");
949 if (IS_ERR(serio_task)) {
950 bus_unregister(&serio_bus);
951 error = PTR_ERR(serio_task);
952 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
953 return error;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return 0;
957}
958
959static void __exit serio_exit(void)
960{
961 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700962 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500965subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966module_exit(serio_exit);