blob: 8c717042f6113bce589ab5f9f5fb47cf181f05a9 [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);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050047EXPORT_SYMBOL(serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048EXPORT_SYMBOL(serio_unregister_driver);
49EXPORT_SYMBOL(serio_open);
50EXPORT_SYMBOL(serio_close);
51EXPORT_SYMBOL(serio_rescan);
52EXPORT_SYMBOL(serio_reconnect);
53
54/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050055 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * serio port or driver registrered or unregistered.
57 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050058static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static LIST_HEAD(serio_list);
61
Russell King30226f82006-01-05 14:38:53 +000062static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static void serio_add_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void serio_reconnect_port(struct serio *serio);
66static void serio_disconnect_port(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050067static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds3c803e82005-06-27 17:49:45 -070069static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
70{
71 int retval;
72
Arjan van de Venc4e32e92006-02-19 00:21:55 -050073 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070074 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050075 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070076
77 return retval;
78}
79
80static int serio_reconnect_driver(struct serio *serio)
81{
82 int retval = -1;
83
Arjan van de Venc4e32e92006-02-19 00:21:55 -050084 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070085 if (serio->drv && serio->drv->reconnect)
86 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050087 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070088
89 return retval;
90}
91
92static void serio_disconnect_driver(struct serio *serio)
93{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050094 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070095 if (serio->drv)
96 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050097 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070098}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
101{
102 while (ids->type || ids->proto) {
103 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
104 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
105 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
106 (ids->id == SERIO_ANY || ids->id == serio->id.id))
107 return 1;
108 ids++;
109 }
110 return 0;
111}
112
113/*
114 * Basic serio -> driver core mappings
115 */
116
117static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
118{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400119 int error;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400129 error = device_bind_driver(&serio->dev);
130 if (error) {
131 printk(KERN_WARNING
132 "serio: device_bind_driver() failed "
133 "for %s (%s) and %s, error: %d\n",
134 serio->phys, serio->name,
135 drv->description, error);
136 serio_disconnect_driver(serio);
137 serio->dev.driver = NULL;
138 goto out;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400141 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 up_write(&serio_bus.subsys.rwsem);
143}
144
145static void serio_release_driver(struct serio *serio)
146{
147 down_write(&serio_bus.subsys.rwsem);
148 device_release_driver(&serio->dev);
149 up_write(&serio_bus.subsys.rwsem);
150}
151
152static void serio_find_driver(struct serio *serio)
153{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400154 int error;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 down_write(&serio_bus.subsys.rwsem);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400157 error = device_attach(&serio->dev);
158 if (error < 0)
159 printk(KERN_WARNING
160 "serio: device_attach() failed for %s (%s), error: %d\n",
161 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 up_write(&serio_bus.subsys.rwsem);
163}
164
165
166/*
167 * Serio event processing.
168 */
169
170enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500171 SERIO_RESCAN_PORT,
172 SERIO_RECONNECT_PORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500174 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177struct serio_event {
178 enum serio_event_type type;
179 void *object;
180 struct module *owner;
181 struct list_head node;
182};
183
184static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
185static LIST_HEAD(serio_event_list);
186static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700187static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500189static int serio_queue_event(void *object, struct module *owner,
190 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned long flags;
193 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500194 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 spin_lock_irqsave(&serio_event_lock, flags);
197
198 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700199 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * starting with the most recent one. If event is the same we
201 * do not need add new one. If event is of different type we
202 * need to add this event and should not look further because
203 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_for_each_entry_reverse(event, &serio_event_list, node) {
206 if (event->object == object) {
207 if (event->type == event_type)
208 goto out;
209 break;
210 }
211 }
212
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500213 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
214 if (!event) {
215 printk(KERN_ERR
216 "serio: Not enough memory to queue event %d\n",
217 event_type);
218 retval = -ENOMEM;
219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500221
222 if (!try_module_get(owner)) {
223 printk(KERN_WARNING
224 "serio: Can't get module reference, dropping event %d\n",
225 event_type);
226 kfree(event);
227 retval = -EINVAL;
228 goto out;
229 }
230
231 event->type = event_type;
232 event->object = object;
233 event->owner = owner;
234
235 list_add_tail(&event->node, &serio_event_list);
236 wake_up(&serio_wait);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238out:
239 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500240 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static void serio_free_event(struct serio_event *event)
244{
245 module_put(event->owner);
246 kfree(event);
247}
248
249static void serio_remove_duplicate_events(struct serio_event *event)
250{
251 struct list_head *node, *next;
252 struct serio_event *e;
253 unsigned long flags;
254
255 spin_lock_irqsave(&serio_event_lock, flags);
256
257 list_for_each_safe(node, next, &serio_event_list) {
258 e = list_entry(node, struct serio_event, node);
259 if (event->object == e->object) {
260 /*
261 * If this event is of different type we should not
262 * look further - we only suppress duplicate events
263 * that were sent back-to-back.
264 */
265 if (event->type != e->type)
266 break;
267
268 list_del_init(node);
269 serio_free_event(e);
270 }
271 }
272
273 spin_unlock_irqrestore(&serio_event_lock, flags);
274}
275
276
277static struct serio_event *serio_get_event(void)
278{
279 struct serio_event *event;
280 struct list_head *node;
281 unsigned long flags;
282
283 spin_lock_irqsave(&serio_event_lock, flags);
284
285 if (list_empty(&serio_event_list)) {
286 spin_unlock_irqrestore(&serio_event_lock, flags);
287 return NULL;
288 }
289
290 node = serio_event_list.next;
291 event = list_entry(node, struct serio_event, node);
292 list_del_init(node);
293
294 spin_unlock_irqrestore(&serio_event_lock, flags);
295
296 return event;
297}
298
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500299static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500303 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500305 /*
306 * Note that we handle only one event here to give swsusp
307 * a chance to freeze kseriod thread. Serio events should
308 * be pretty rare so we are not concerned about taking
309 * performance hit.
310 */
311 if ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 switch (event->type) {
314 case SERIO_REGISTER_PORT:
315 serio_add_port(event->object);
316 break;
317
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500318 case SERIO_RECONNECT_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 serio_reconnect_port(event->object);
320 break;
321
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500322 case SERIO_RESCAN_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 serio_disconnect_port(event->object);
324 serio_find_driver(event->object);
325 break;
326
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500327 case SERIO_ATTACH_DRIVER:
328 serio_attach_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330
331 default:
332 break;
333 }
334
335 serio_remove_duplicate_events(event);
336 serio_free_event(event);
337 }
338
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500339 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/*
343 * Remove all events that have been submitted for a given serio port.
344 */
345static void serio_remove_pending_events(struct serio *serio)
346{
347 struct list_head *node, *next;
348 struct serio_event *event;
349 unsigned long flags;
350
351 spin_lock_irqsave(&serio_event_lock, flags);
352
353 list_for_each_safe(node, next, &serio_event_list) {
354 event = list_entry(node, struct serio_event, node);
355 if (event->object == serio) {
356 list_del_init(node);
357 serio_free_event(event);
358 }
359 }
360
361 spin_unlock_irqrestore(&serio_event_lock, flags);
362}
363
364/*
365 * Destroy child serio port (if any) that has not been fully registered yet.
366 *
367 * Note that we rely on the fact that port can have only one child and therefore
368 * only one child registration request can be pending. Additionally, children
369 * are registered by driver's connect() handler so there can't be a grandchild
370 * pending registration together with a child.
371 */
372static struct serio *serio_get_pending_child(struct serio *parent)
373{
374 struct serio_event *event;
375 struct serio *serio, *child = NULL;
376 unsigned long flags;
377
378 spin_lock_irqsave(&serio_event_lock, flags);
379
380 list_for_each_entry(event, &serio_event_list, node) {
381 if (event->type == SERIO_REGISTER_PORT) {
382 serio = event->object;
383 if (serio->parent == parent) {
384 child = serio;
385 break;
386 }
387 }
388 }
389
390 spin_unlock_irqrestore(&serio_event_lock, flags);
391 return child;
392}
393
394static int serio_thread(void *nothing)
395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500397 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700398 wait_event_interruptible(serio_wait,
399 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700400 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700401 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407
408/*
409 * Serio port operations
410 */
411
Yani Ioannoue404e272005-05-17 06:42:58 -0400412static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct serio *serio = to_serio_port(dev);
415 return sprintf(buf, "%s\n", serio->name);
416}
417
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500418static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
419{
420 struct serio *serio = to_serio_port(dev);
421
422 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
423 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
424}
425
Yani Ioannoue404e272005-05-17 06:42:58 -0400426static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct serio *serio = to_serio_port(dev);
429 return sprintf(buf, "%02x\n", serio->id.type);
430}
431
Yani Ioannoue404e272005-05-17 06:42:58 -0400432static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct serio *serio = to_serio_port(dev);
435 return sprintf(buf, "%02x\n", serio->id.proto);
436}
437
Yani Ioannoue404e272005-05-17 06:42:58 -0400438static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct serio *serio = to_serio_port(dev);
441 return sprintf(buf, "%02x\n", serio->id.id);
442}
443
Yani Ioannoue404e272005-05-17 06:42:58 -0400444static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct serio *serio = to_serio_port(dev);
447 return sprintf(buf, "%02x\n", serio->id.extra);
448}
449
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700450static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
451static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
452static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
453static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
454
455static struct attribute *serio_device_id_attrs[] = {
456 &dev_attr_type.attr,
457 &dev_attr_proto.attr,
458 &dev_attr_id.attr,
459 &dev_attr_extra.attr,
460 NULL
461};
462
463static struct attribute_group serio_id_attr_group = {
464 .name = "id",
465 .attrs = serio_device_id_attrs,
466};
467
Yani Ioannoue404e272005-05-17 06:42:58 -0400468static 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 -0700469{
470 struct serio *serio = to_serio_port(dev);
471 struct device_driver *drv;
472 int retval;
473
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500474 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (retval)
476 return retval;
477
478 retval = count;
479 if (!strncmp(buf, "none", count)) {
480 serio_disconnect_port(serio);
481 } else if (!strncmp(buf, "reconnect", count)) {
482 serio_reconnect_port(serio);
483 } else if (!strncmp(buf, "rescan", count)) {
484 serio_disconnect_port(serio);
485 serio_find_driver(serio);
486 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
487 serio_disconnect_port(serio);
488 serio_bind_driver(serio, to_serio_driver(drv));
489 put_driver(drv);
490 } else {
491 retval = -EINVAL;
492 }
493
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500494 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 return retval;
497}
498
Yani Ioannoue404e272005-05-17 06:42:58 -0400499static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct serio *serio = to_serio_port(dev);
502 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
503}
504
Yani Ioannoue404e272005-05-17 06:42:58 -0400505static 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 -0700506{
507 struct serio *serio = to_serio_port(dev);
508 int retval;
509
510 retval = count;
511 if (!strncmp(buf, "manual", count)) {
512 serio->manual_bind = 1;
513 } else if (!strncmp(buf, "auto", count)) {
514 serio->manual_bind = 0;
515 } else {
516 retval = -EINVAL;
517 }
518
519 return retval;
520}
521
522static struct device_attribute serio_device_attrs[] = {
523 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500524 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
526 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
527 __ATTR_NULL
528};
529
530
531static void serio_release_port(struct device *dev)
532{
533 struct serio *serio = to_serio_port(dev);
534
535 kfree(serio);
536 module_put(THIS_MODULE);
537}
538
539/*
540 * Prepare serio port for registration.
541 */
542static void serio_init_port(struct serio *serio)
543{
544 static atomic_t serio_no = ATOMIC_INIT(0);
545
546 __module_get(THIS_MODULE);
547
Randy Dunlap73b59a32006-07-19 01:14:55 -0400548 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500550 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 device_initialize(&serio->dev);
552 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
553 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
554 serio->dev.bus = &serio_bus;
555 serio->dev.release = serio_release_port;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400556 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400558 serio->depth = serio->parent->depth + 1;
559 } else
560 serio->depth = 0;
561 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
565 * Complete serio port registration.
566 * Driver core will attempt to find appropriate driver for the port.
567 */
568static void serio_add_port(struct serio *serio)
569{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400570 int error;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (serio->parent) {
573 serio_pause_rx(serio->parent);
574 serio->parent->child = serio;
575 serio_continue_rx(serio->parent);
576 }
577
578 list_add_tail(&serio->node, &serio_list);
579 if (serio->start)
580 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400581 error = device_add(&serio->dev);
582 if (error)
583 printk(KERN_ERR
584 "serio: device_add() failed for %s (%s), error: %d\n",
585 serio->phys, serio->name, error);
586 else {
587 serio->registered = 1;
588 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
589 if (error)
590 printk(KERN_ERR
591 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
592 serio->phys, serio->name, error);
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596/*
597 * serio_destroy_port() completes deregistration process and removes
598 * port from the system
599 */
600static void serio_destroy_port(struct serio *serio)
601{
602 struct serio *child;
603
604 child = serio_get_pending_child(serio);
605 if (child) {
606 serio_remove_pending_events(child);
607 put_device(&child->dev);
608 }
609
610 if (serio->stop)
611 serio->stop(serio);
612
613 if (serio->parent) {
614 serio_pause_rx(serio->parent);
615 serio->parent->child = NULL;
616 serio_continue_rx(serio->parent);
617 serio->parent = NULL;
618 }
619
620 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700621 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 serio->registered = 0;
624 }
625
Randy Dunlap73b59a32006-07-19 01:14:55 -0400626 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 serio_remove_pending_events(serio);
628 put_device(&serio->dev);
629}
630
631/*
632 * Reconnect serio port and all its children (re-initialize attached devices)
633 */
634static void serio_reconnect_port(struct serio *serio)
635{
636 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700637 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 serio_disconnect_port(serio);
639 serio_find_driver(serio);
640 /* Ok, old children are now gone, we are done */
641 break;
642 }
643 serio = serio->child;
644 } while (serio);
645}
646
647/*
648 * serio_disconnect_port() unbinds a port from its driver. As a side effect
649 * all child ports are unbound and destroyed.
650 */
651static void serio_disconnect_port(struct serio *serio)
652{
653 struct serio *s, *parent;
654
655 if (serio->child) {
656 /*
657 * Children ports should be disconnected and destroyed
658 * first, staring with the leaf one, since we don't want
659 * to do recursion
660 */
661 for (s = serio; s->child; s = s->child)
662 /* empty */;
663
664 do {
665 parent = s->parent;
666
667 serio_release_driver(s);
668 serio_destroy_port(s);
669 } while ((s = parent) != serio);
670 }
671
672 /*
673 * Ok, no children left, now disconnect this port
674 */
675 serio_release_driver(serio);
676}
677
678void serio_rescan(struct serio *serio)
679{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500680 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683void serio_reconnect(struct serio *serio)
684{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500685 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/*
689 * Submits register request to kseriod for subsequent execution.
690 * Note that port registration is always asynchronous.
691 */
692void __serio_register_port(struct serio *serio, struct module *owner)
693{
694 serio_init_port(serio);
695 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
696}
697
698/*
699 * Synchronously unregisters serio port.
700 */
701void serio_unregister_port(struct serio *serio)
702{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500703 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 serio_disconnect_port(serio);
705 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500706 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700710 * Safely unregisters child port if one is present.
711 */
712void serio_unregister_child_port(struct serio *serio)
713{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500714 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700715 if (serio->child) {
716 serio_disconnect_port(serio->child);
717 serio_destroy_port(serio->child);
718 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500719 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723/*
724 * Serio driver operations
725 */
726
727static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
728{
729 struct serio_driver *driver = to_serio_driver(drv);
730 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
731}
732
733static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
734{
735 struct serio_driver *serio_drv = to_serio_driver(drv);
736 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
737}
738
739static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
740{
741 struct serio_driver *serio_drv = to_serio_driver(drv);
742 int retval;
743
744 retval = count;
745 if (!strncmp(buf, "manual", count)) {
746 serio_drv->manual_bind = 1;
747 } else if (!strncmp(buf, "auto", count)) {
748 serio_drv->manual_bind = 0;
749 } else {
750 retval = -EINVAL;
751 }
752
753 return retval;
754}
755
756
757static struct driver_attribute serio_driver_attrs[] = {
758 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
759 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
760 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
761 __ATTR_NULL
762};
763
764static int serio_driver_probe(struct device *dev)
765{
766 struct serio *serio = to_serio_port(dev);
767 struct serio_driver *drv = to_serio_driver(dev->driver);
768
Linus Torvalds3c803e82005-06-27 17:49:45 -0700769 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772static int serio_driver_remove(struct device *dev)
773{
774 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvalds3c803e82005-06-27 17:49:45 -0700776 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778}
779
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500780static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400781{
782 int error;
783
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500784 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400785 if (error)
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500786 printk(KERN_WARNING
787 "serio: driver_attach() failed for %s with error %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400788 drv->driver.name, error);
789}
790
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500791int serio_register_driver(struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500793 int manual_bind = drv->manual_bind;
794 int error;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500798 /*
799 * Temporarily disable automatic binding because probing
800 * takes long time and we are better off doing it in kseriod
801 */
802 drv->manual_bind = 1;
803
804 error = driver_register(&drv->driver);
805 if (error) {
806 printk(KERN_ERR
807 "serio: driver_register() failed for %s, error: %d\n",
808 drv->driver.name, error);
809 return error;
810 }
811
812 /*
813 * Restore original bind mode and let kseriod bind the
814 * driver to free ports
815 */
816 if (!manual_bind) {
817 drv->manual_bind = 0;
818 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
819 if (error) {
820 driver_unregister(&drv->driver);
821 return error;
822 }
823 }
824
825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828void serio_unregister_driver(struct serio_driver *drv)
829{
830 struct serio *serio;
831
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500832 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 drv->manual_bind = 1; /* so serio_find_driver ignores it */
834
835start_over:
836 list_for_each_entry(serio, &serio_list, node) {
837 if (serio->drv == drv) {
838 serio_disconnect_port(serio);
839 serio_find_driver(serio);
840 /* we could've deleted some ports, restart */
841 goto start_over;
842 }
843 }
844
845 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500846 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
850{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 serio_pause_rx(serio);
852 serio->drv = drv;
853 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static int serio_bus_match(struct device *dev, struct device_driver *drv)
857{
858 struct serio *serio = to_serio_port(dev);
859 struct serio_driver *serio_drv = to_serio_driver(drv);
860
861 if (serio->manual_bind || serio_drv->manual_bind)
862 return 0;
863
864 return serio_match_port(serio_drv->id_table, serio);
865}
866
867#ifdef CONFIG_HOTPLUG
868
Kay Sievers312c0042005-11-16 09:00:00 +0100869#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500870 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100871 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500872 buffer, buffer_size, &len, \
873 fmt, val); \
874 if (err) \
875 return err; \
876 } while (0)
877
Kay Sievers312c0042005-11-16 09:00:00 +0100878static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct serio *serio;
881 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500882 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (!dev)
885 return -ENODEV;
886
887 serio = to_serio_port(dev);
888
Kay Sievers312c0042005-11-16 09:00:00 +0100889 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
890 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
891 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
892 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
893 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500894 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 envp[i] = NULL;
896
897 return 0;
898}
Kay Sievers312c0042005-11-16 09:00:00 +0100899#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901#else
902
Kay Sievers312c0042005-11-16 09:00:00 +0100903static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 return -ENODEV;
906}
907
908#endif /* CONFIG_HOTPLUG */
909
910static int serio_resume(struct device *dev)
911{
912 struct serio *serio = to_serio_port(dev);
913
Linus Torvalds3c803e82005-06-27 17:49:45 -0700914 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * Driver re-probing can take a while, so better let kseriod
917 * deal with it.
918 */
919 serio_rescan(serio);
920 }
921
922 return 0;
923}
924
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500925/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926int serio_open(struct serio *serio, struct serio_driver *drv)
927{
928 serio_set_drv(serio, drv);
929
930 if (serio->open && serio->open(serio)) {
931 serio_set_drv(serio, NULL);
932 return -1;
933 }
934 return 0;
935}
936
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500937/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938void serio_close(struct serio *serio)
939{
940 if (serio->close)
941 serio->close(serio);
942
943 serio_set_drv(serio, NULL);
944}
945
946irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100947 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 unsigned long flags;
950 irqreturn_t ret = IRQ_NONE;
951
952 spin_lock_irqsave(&serio->lock, flags);
953
954 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100955 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 } else if (!dfl && serio->registered) {
957 serio_rescan(serio);
958 ret = IRQ_HANDLED;
959 }
960
961 spin_unlock_irqrestore(&serio->lock, flags);
962
963 return ret;
964}
965
Marton Nemeth1ea2a692006-11-02 23:27:21 -0500966static struct bus_type serio_bus = {
967 .name = "serio",
968 .dev_attrs = serio_device_attrs,
969 .drv_attrs = serio_driver_attrs,
970 .match = serio_bus_match,
971 .uevent = serio_uevent,
972 .probe = serio_driver_probe,
973 .remove = serio_driver_remove,
974 .resume = serio_resume,
975};
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int __init serio_init(void)
978{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400979 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Randy Dunlap73b59a32006-07-19 01:14:55 -0400981 error = bus_register(&serio_bus);
982 if (error) {
983 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
984 return error;
985 }
986
987 serio_task = kthread_run(serio_thread, NULL, "kseriod");
988 if (IS_ERR(serio_task)) {
989 bus_unregister(&serio_bus);
990 error = PTR_ERR(serio_task);
991 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
992 return error;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 return 0;
996}
997
998static void __exit serio_exit(void)
999{
1000 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -07001001 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001004subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005module_exit(serio_exit);