blob: a15e531ec755fae2d8befa7615530676914a703b [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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION("Serio abstraction core");
42MODULE_LICENSE("GPL");
43
44EXPORT_SYMBOL(serio_interrupt);
45EXPORT_SYMBOL(__serio_register_port);
46EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -070047EXPORT_SYMBOL(serio_unregister_child_port);
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -080048EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049EXPORT_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void serio_reconnect_port(struct serio *serio);
67static void serio_disconnect_port(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050068static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
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{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400120 int error;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 down_write(&serio_bus.subsys.rwsem);
123
124 if (serio_match_port(drv->id_table, serio)) {
125 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700126 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 serio->dev.driver = NULL;
128 goto out;
129 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400130 error = device_bind_driver(&serio->dev);
131 if (error) {
132 printk(KERN_WARNING
133 "serio: device_bind_driver() failed "
134 "for %s (%s) and %s, error: %d\n",
135 serio->phys, serio->name,
136 drv->description, error);
137 serio_disconnect_driver(serio);
138 serio->dev.driver = NULL;
139 goto out;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400142 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 up_write(&serio_bus.subsys.rwsem);
144}
145
146static void serio_release_driver(struct serio *serio)
147{
148 down_write(&serio_bus.subsys.rwsem);
149 device_release_driver(&serio->dev);
150 up_write(&serio_bus.subsys.rwsem);
151}
152
153static void serio_find_driver(struct serio *serio)
154{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400155 int error;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 down_write(&serio_bus.subsys.rwsem);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400158 error = device_attach(&serio->dev);
159 if (error < 0)
160 printk(KERN_WARNING
161 "serio: device_attach() failed for %s (%s), error: %d\n",
162 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 up_write(&serio_bus.subsys.rwsem);
164}
165
166
167/*
168 * Serio event processing.
169 */
170
171enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500172 SERIO_RESCAN_PORT,
173 SERIO_RECONNECT_PORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500175 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
178struct serio_event {
179 enum serio_event_type type;
180 void *object;
181 struct module *owner;
182 struct list_head node;
183};
184
185static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
186static LIST_HEAD(serio_event_list);
187static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700188static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500190static int serio_queue_event(void *object, struct module *owner,
191 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 unsigned long flags;
194 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500195 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 spin_lock_irqsave(&serio_event_lock, flags);
198
199 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700200 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * starting with the most recent one. If event is the same we
202 * do not need add new one. If event is of different type we
203 * need to add this event and should not look further because
204 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_for_each_entry_reverse(event, &serio_event_list, node) {
207 if (event->object == object) {
208 if (event->type == event_type)
209 goto out;
210 break;
211 }
212 }
213
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500214 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
215 if (!event) {
216 printk(KERN_ERR
217 "serio: Not enough memory to queue event %d\n",
218 event_type);
219 retval = -ENOMEM;
220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500222
223 if (!try_module_get(owner)) {
224 printk(KERN_WARNING
225 "serio: Can't get module reference, dropping event %d\n",
226 event_type);
227 kfree(event);
228 retval = -EINVAL;
229 goto out;
230 }
231
232 event->type = event_type;
233 event->object = object;
234 event->owner = owner;
235
236 list_add_tail(&event->node, &serio_event_list);
237 wake_up(&serio_wait);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239out:
240 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500241 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static void serio_free_event(struct serio_event *event)
245{
246 module_put(event->owner);
247 kfree(event);
248}
249
250static void serio_remove_duplicate_events(struct serio_event *event)
251{
252 struct list_head *node, *next;
253 struct serio_event *e;
254 unsigned long flags;
255
256 spin_lock_irqsave(&serio_event_lock, flags);
257
258 list_for_each_safe(node, next, &serio_event_list) {
259 e = list_entry(node, struct serio_event, node);
260 if (event->object == e->object) {
261 /*
262 * If this event is of different type we should not
263 * look further - we only suppress duplicate events
264 * that were sent back-to-back.
265 */
266 if (event->type != e->type)
267 break;
268
269 list_del_init(node);
270 serio_free_event(e);
271 }
272 }
273
274 spin_unlock_irqrestore(&serio_event_lock, flags);
275}
276
277
278static struct serio_event *serio_get_event(void)
279{
280 struct serio_event *event;
281 struct list_head *node;
282 unsigned long flags;
283
284 spin_lock_irqsave(&serio_event_lock, flags);
285
286 if (list_empty(&serio_event_list)) {
287 spin_unlock_irqrestore(&serio_event_lock, flags);
288 return NULL;
289 }
290
291 node = serio_event_list.next;
292 event = list_entry(node, struct serio_event, node);
293 list_del_init(node);
294
295 spin_unlock_irqrestore(&serio_event_lock, flags);
296
297 return event;
298}
299
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500300static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500304 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500306 /*
307 * Note that we handle only one event here to give swsusp
308 * a chance to freeze kseriod thread. Serio events should
309 * be pretty rare so we are not concerned about taking
310 * performance hit.
311 */
312 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 switch (event->type) {
315 case SERIO_REGISTER_PORT:
316 serio_add_port(event->object);
317 break;
318
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500319 case SERIO_RECONNECT_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 serio_reconnect_port(event->object);
321 break;
322
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500323 case SERIO_RESCAN_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 serio_disconnect_port(event->object);
325 serio_find_driver(event->object);
326 break;
327
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500328 case SERIO_ATTACH_DRIVER:
329 serio_attach_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331
332 default:
333 break;
334 }
335
336 serio_remove_duplicate_events(event);
337 serio_free_event(event);
338 }
339
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500340 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Remove all events that have been submitted for a given serio port.
345 */
346static void serio_remove_pending_events(struct serio *serio)
347{
348 struct list_head *node, *next;
349 struct serio_event *event;
350 unsigned long flags;
351
352 spin_lock_irqsave(&serio_event_lock, flags);
353
354 list_for_each_safe(node, next, &serio_event_list) {
355 event = list_entry(node, struct serio_event, node);
356 if (event->object == serio) {
357 list_del_init(node);
358 serio_free_event(event);
359 }
360 }
361
362 spin_unlock_irqrestore(&serio_event_lock, flags);
363}
364
365/*
366 * Destroy child serio port (if any) that has not been fully registered yet.
367 *
368 * Note that we rely on the fact that port can have only one child and therefore
369 * only one child registration request can be pending. Additionally, children
370 * are registered by driver's connect() handler so there can't be a grandchild
371 * pending registration together with a child.
372 */
373static struct serio *serio_get_pending_child(struct serio *parent)
374{
375 struct serio_event *event;
376 struct serio *serio, *child = NULL;
377 unsigned long flags;
378
379 spin_lock_irqsave(&serio_event_lock, flags);
380
381 list_for_each_entry(event, &serio_event_list, node) {
382 if (event->type == SERIO_REGISTER_PORT) {
383 serio = event->object;
384 if (serio->parent == parent) {
385 child = serio;
386 break;
387 }
388 }
389 }
390
391 spin_unlock_irqrestore(&serio_event_lock, flags);
392 return child;
393}
394
395static int serio_thread(void *nothing)
396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500398 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700399 wait_event_interruptible(serio_wait,
400 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700401 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700402 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408
409/*
410 * Serio port operations
411 */
412
Yani Ioannoue404e272005-05-17 06:42:58 -0400413static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct serio *serio = to_serio_port(dev);
416 return sprintf(buf, "%s\n", serio->name);
417}
418
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500419static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
420{
421 struct serio *serio = to_serio_port(dev);
422
423 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
424 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
425}
426
Yani Ioannoue404e272005-05-17 06:42:58 -0400427static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct serio *serio = to_serio_port(dev);
430 return sprintf(buf, "%02x\n", serio->id.type);
431}
432
Yani Ioannoue404e272005-05-17 06:42:58 -0400433static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct serio *serio = to_serio_port(dev);
436 return sprintf(buf, "%02x\n", serio->id.proto);
437}
438
Yani Ioannoue404e272005-05-17 06:42:58 -0400439static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct serio *serio = to_serio_port(dev);
442 return sprintf(buf, "%02x\n", serio->id.id);
443}
444
Yani Ioannoue404e272005-05-17 06:42:58 -0400445static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct serio *serio = to_serio_port(dev);
448 return sprintf(buf, "%02x\n", serio->id.extra);
449}
450
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700451static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
452static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
453static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
454static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
455
456static struct attribute *serio_device_id_attrs[] = {
457 &dev_attr_type.attr,
458 &dev_attr_proto.attr,
459 &dev_attr_id.attr,
460 &dev_attr_extra.attr,
461 NULL
462};
463
464static struct attribute_group serio_id_attr_group = {
465 .name = "id",
466 .attrs = serio_device_id_attrs,
467};
468
Yani Ioannoue404e272005-05-17 06:42:58 -0400469static 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 -0700470{
471 struct serio *serio = to_serio_port(dev);
472 struct device_driver *drv;
473 int retval;
474
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500475 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (retval)
477 return retval;
478
479 retval = count;
480 if (!strncmp(buf, "none", count)) {
481 serio_disconnect_port(serio);
482 } else if (!strncmp(buf, "reconnect", count)) {
483 serio_reconnect_port(serio);
484 } else if (!strncmp(buf, "rescan", count)) {
485 serio_disconnect_port(serio);
486 serio_find_driver(serio);
487 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
488 serio_disconnect_port(serio);
489 serio_bind_driver(serio, to_serio_driver(drv));
490 put_driver(drv);
491 } else {
492 retval = -EINVAL;
493 }
494
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500495 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 return retval;
498}
499
Yani Ioannoue404e272005-05-17 06:42:58 -0400500static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct serio *serio = to_serio_port(dev);
503 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
504}
505
Yani Ioannoue404e272005-05-17 06:42:58 -0400506static 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 -0700507{
508 struct serio *serio = to_serio_port(dev);
509 int retval;
510
511 retval = count;
512 if (!strncmp(buf, "manual", count)) {
513 serio->manual_bind = 1;
514 } else if (!strncmp(buf, "auto", count)) {
515 serio->manual_bind = 0;
516 } else {
517 retval = -EINVAL;
518 }
519
520 return retval;
521}
522
523static struct device_attribute serio_device_attrs[] = {
524 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500525 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
527 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
528 __ATTR_NULL
529};
530
531
532static void serio_release_port(struct device *dev)
533{
534 struct serio *serio = to_serio_port(dev);
535
536 kfree(serio);
537 module_put(THIS_MODULE);
538}
539
540/*
541 * Prepare serio port for registration.
542 */
543static void serio_init_port(struct serio *serio)
544{
545 static atomic_t serio_no = ATOMIC_INIT(0);
546
547 __module_get(THIS_MODULE);
548
Randy Dunlap73b59a32006-07-19 01:14:55 -0400549 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500551 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 device_initialize(&serio->dev);
553 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
554 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
555 serio->dev.bus = &serio_bus;
556 serio->dev.release = serio_release_port;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400557 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400559 serio->depth = serio->parent->depth + 1;
560 } else
561 serio->depth = 0;
562 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * Complete serio port registration.
567 * Driver core will attempt to find appropriate driver for the port.
568 */
569static void serio_add_port(struct serio *serio)
570{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400571 int error;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (serio->parent) {
574 serio_pause_rx(serio->parent);
575 serio->parent->child = serio;
576 serio_continue_rx(serio->parent);
577 }
578
579 list_add_tail(&serio->node, &serio_list);
580 if (serio->start)
581 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400582 error = device_add(&serio->dev);
583 if (error)
584 printk(KERN_ERR
585 "serio: device_add() failed for %s (%s), error: %d\n",
586 serio->phys, serio->name, error);
587 else {
588 serio->registered = 1;
589 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
590 if (error)
591 printk(KERN_ERR
592 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
593 serio->phys, serio->name, error);
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * serio_destroy_port() completes deregistration process and removes
599 * port from the system
600 */
601static void serio_destroy_port(struct serio *serio)
602{
603 struct serio *child;
604
605 child = serio_get_pending_child(serio);
606 if (child) {
607 serio_remove_pending_events(child);
608 put_device(&child->dev);
609 }
610
611 if (serio->stop)
612 serio->stop(serio);
613
614 if (serio->parent) {
615 serio_pause_rx(serio->parent);
616 serio->parent->child = NULL;
617 serio_continue_rx(serio->parent);
618 serio->parent = NULL;
619 }
620
621 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700622 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 serio->registered = 0;
625 }
626
Randy Dunlap73b59a32006-07-19 01:14:55 -0400627 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 serio_remove_pending_events(serio);
629 put_device(&serio->dev);
630}
631
632/*
633 * Reconnect serio port and all its children (re-initialize attached devices)
634 */
635static void serio_reconnect_port(struct serio *serio)
636{
637 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700638 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 serio_disconnect_port(serio);
640 serio_find_driver(serio);
641 /* Ok, old children are now gone, we are done */
642 break;
643 }
644 serio = serio->child;
645 } while (serio);
646}
647
648/*
649 * serio_disconnect_port() unbinds a port from its driver. As a side effect
650 * all child ports are unbound and destroyed.
651 */
652static void serio_disconnect_port(struct serio *serio)
653{
654 struct serio *s, *parent;
655
656 if (serio->child) {
657 /*
658 * Children ports should be disconnected and destroyed
659 * first, staring with the leaf one, since we don't want
660 * to do recursion
661 */
662 for (s = serio; s->child; s = s->child)
663 /* empty */;
664
665 do {
666 parent = s->parent;
667
668 serio_release_driver(s);
669 serio_destroy_port(s);
670 } while ((s = parent) != serio);
671 }
672
673 /*
674 * Ok, no children left, now disconnect this port
675 */
676 serio_release_driver(serio);
677}
678
679void serio_rescan(struct serio *serio)
680{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500681 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684void serio_reconnect(struct serio *serio)
685{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500686 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*
690 * Submits register request to kseriod for subsequent execution.
691 * Note that port registration is always asynchronous.
692 */
693void __serio_register_port(struct serio *serio, struct module *owner)
694{
695 serio_init_port(serio);
696 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
697}
698
699/*
700 * Synchronously unregisters serio port.
701 */
702void serio_unregister_port(struct serio *serio)
703{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500704 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 serio_disconnect_port(serio);
706 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500707 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700711 * Safely unregisters child port if one is present.
712 */
713void serio_unregister_child_port(struct serio *serio)
714{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500715 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700716 if (serio->child) {
717 serio_disconnect_port(serio->child);
718 serio_destroy_port(serio->child);
719 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500720 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724/*
725 * Serio driver operations
726 */
727
728static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
729{
730 struct serio_driver *driver = to_serio_driver(drv);
731 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
732}
733
734static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
735{
736 struct serio_driver *serio_drv = to_serio_driver(drv);
737 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
738}
739
740static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
741{
742 struct serio_driver *serio_drv = to_serio_driver(drv);
743 int retval;
744
745 retval = count;
746 if (!strncmp(buf, "manual", count)) {
747 serio_drv->manual_bind = 1;
748 } else if (!strncmp(buf, "auto", count)) {
749 serio_drv->manual_bind = 0;
750 } else {
751 retval = -EINVAL;
752 }
753
754 return retval;
755}
756
757
758static struct driver_attribute serio_driver_attrs[] = {
759 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
760 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
761 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
762 __ATTR_NULL
763};
764
765static int serio_driver_probe(struct device *dev)
766{
767 struct serio *serio = to_serio_port(dev);
768 struct serio_driver *drv = to_serio_driver(dev->driver);
769
Linus Torvalds3c803e82005-06-27 17:49:45 -0700770 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773static int serio_driver_remove(struct device *dev)
774{
775 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds3c803e82005-06-27 17:49:45 -0700777 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779}
780
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500781static void serio_cleanup(struct serio *serio)
782{
783 if (serio->drv && serio->drv->cleanup)
784 serio->drv->cleanup(serio);
785}
786
787static void serio_shutdown(struct device *dev)
788{
789 struct serio *serio = to_serio_port(dev);
790
791 serio_cleanup(serio);
792}
793
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500794static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400795{
796 int error;
797
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500798 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400799 if (error)
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500800 printk(KERN_WARNING
801 "serio: driver_attach() failed for %s with error %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400802 drv->driver.name, error);
803}
804
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800805int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500807 int manual_bind = drv->manual_bind;
808 int error;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800811 drv->driver.owner = owner;
812 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500814 /*
815 * Temporarily disable automatic binding because probing
816 * takes long time and we are better off doing it in kseriod
817 */
818 drv->manual_bind = 1;
819
820 error = driver_register(&drv->driver);
821 if (error) {
822 printk(KERN_ERR
823 "serio: driver_register() failed for %s, error: %d\n",
824 drv->driver.name, error);
825 return error;
826 }
827
828 /*
829 * Restore original bind mode and let kseriod bind the
830 * driver to free ports
831 */
832 if (!manual_bind) {
833 drv->manual_bind = 0;
834 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
835 if (error) {
836 driver_unregister(&drv->driver);
837 return error;
838 }
839 }
840
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844void serio_unregister_driver(struct serio_driver *drv)
845{
846 struct serio *serio;
847
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500848 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 drv->manual_bind = 1; /* so serio_find_driver ignores it */
850
851start_over:
852 list_for_each_entry(serio, &serio_list, node) {
853 if (serio->drv == drv) {
854 serio_disconnect_port(serio);
855 serio_find_driver(serio);
856 /* we could've deleted some ports, restart */
857 goto start_over;
858 }
859 }
860
861 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500862 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 serio_pause_rx(serio);
868 serio->drv = drv;
869 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872static int serio_bus_match(struct device *dev, struct device_driver *drv)
873{
874 struct serio *serio = to_serio_port(dev);
875 struct serio_driver *serio_drv = to_serio_driver(drv);
876
877 if (serio->manual_bind || serio_drv->manual_bind)
878 return 0;
879
880 return serio_match_port(serio_drv->id_table, serio);
881}
882
883#ifdef CONFIG_HOTPLUG
884
Kay Sievers312c0042005-11-16 09:00:00 +0100885#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500886 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100887 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500888 buffer, buffer_size, &len, \
889 fmt, val); \
890 if (err) \
891 return err; \
892 } while (0)
893
Kay Sievers312c0042005-11-16 09:00:00 +0100894static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct serio *serio;
897 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500898 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (!dev)
901 return -ENODEV;
902
903 serio = to_serio_port(dev);
904
Kay Sievers312c0042005-11-16 09:00:00 +0100905 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
906 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
907 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
908 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
909 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500910 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 envp[i] = NULL;
912
913 return 0;
914}
Kay Sievers312c0042005-11-16 09:00:00 +0100915#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917#else
918
Kay Sievers312c0042005-11-16 09:00:00 +0100919static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 return -ENODEV;
922}
923
924#endif /* CONFIG_HOTPLUG */
925
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500926#ifdef CONFIG_PM
927static int serio_suspend(struct device *dev, pm_message_t state)
928{
929 if (dev->power.power_state.event != state.event) {
930 if (state.event == PM_EVENT_SUSPEND)
931 serio_cleanup(to_serio_port(dev));
932
933 dev->power.power_state = state;
934 }
935
936 return 0;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int serio_resume(struct device *dev)
940{
941 struct serio *serio = to_serio_port(dev);
942
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500943 if (dev->power.power_state.event != PM_EVENT_ON &&
944 serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /*
946 * Driver re-probing can take a while, so better let kseriod
947 * deal with it.
948 */
949 serio_rescan(serio);
950 }
951
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500952 dev->power.power_state = PMSG_ON;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500956#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500958/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959int serio_open(struct serio *serio, struct serio_driver *drv)
960{
961 serio_set_drv(serio, drv);
962
963 if (serio->open && serio->open(serio)) {
964 serio_set_drv(serio, NULL);
965 return -1;
966 }
967 return 0;
968}
969
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500970/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971void serio_close(struct serio *serio)
972{
973 if (serio->close)
974 serio->close(serio);
975
976 serio_set_drv(serio, NULL);
977}
978
979irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100980 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 unsigned long flags;
983 irqreturn_t ret = IRQ_NONE;
984
985 spin_lock_irqsave(&serio->lock, flags);
986
987 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100988 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } else if (!dfl && serio->registered) {
990 serio_rescan(serio);
991 ret = IRQ_HANDLED;
992 }
993
994 spin_unlock_irqrestore(&serio->lock, flags);
995
996 return ret;
997}
998
Marton Nemeth1ea2a692006-11-02 23:27:21 -0500999static struct bus_type serio_bus = {
1000 .name = "serio",
1001 .dev_attrs = serio_device_attrs,
1002 .drv_attrs = serio_driver_attrs,
1003 .match = serio_bus_match,
1004 .uevent = serio_uevent,
1005 .probe = serio_driver_probe,
1006 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001007 .shutdown = serio_shutdown,
1008#ifdef CONFIG_PM
1009 .suspend = serio_suspend,
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001010 .resume = serio_resume,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001011#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001012};
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static int __init serio_init(void)
1015{
Randy Dunlap73b59a32006-07-19 01:14:55 -04001016 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Randy Dunlap73b59a32006-07-19 01:14:55 -04001018 error = bus_register(&serio_bus);
1019 if (error) {
1020 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
1021 return error;
1022 }
1023
1024 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1025 if (IS_ERR(serio_task)) {
1026 bus_unregister(&serio_bus);
1027 error = PTR_ERR(serio_task);
1028 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
1029 return error;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 return 0;
1033}
1034
1035static void __exit serio_exit(void)
1036{
1037 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -07001038 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001041subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042module_exit(serio_exit);