Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/slab.h> |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 36 | #include <linux/kthread.h> |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 38 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 41 | MODULE_DESCRIPTION("Serio abstraction core"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
| 44 | EXPORT_SYMBOL(serio_interrupt); |
| 45 | EXPORT_SYMBOL(__serio_register_port); |
| 46 | EXPORT_SYMBOL(serio_unregister_port); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 47 | EXPORT_SYMBOL(serio_unregister_child_port); |
Greg Kroah-Hartman | 4b31562 | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 48 | EXPORT_SYMBOL(__serio_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | EXPORT_SYMBOL(serio_unregister_driver); |
| 50 | EXPORT_SYMBOL(serio_open); |
| 51 | EXPORT_SYMBOL(serio_close); |
| 52 | EXPORT_SYMBOL(serio_rescan); |
| 53 | EXPORT_SYMBOL(serio_reconnect); |
| 54 | |
| 55 | /* |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 56 | * serio_mutex protects entire serio subsystem and is taken every time |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * serio port or driver registrered or unregistered. |
| 58 | */ |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 59 | static DEFINE_MUTEX(serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static LIST_HEAD(serio_list); |
| 62 | |
Russell King | 30226f8 | 2006-01-05 14:38:53 +0000 | [diff] [blame] | 63 | static struct bus_type serio_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | static void serio_add_port(struct serio *serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static void serio_reconnect_port(struct serio *serio); |
| 67 | static void serio_disconnect_port(struct serio *serio); |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 68 | static void serio_attach_driver(struct serio_driver *drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 70 | static int serio_connect_driver(struct serio *serio, struct serio_driver *drv) |
| 71 | { |
| 72 | int retval; |
| 73 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 74 | mutex_lock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 75 | retval = drv->connect(serio, drv); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 76 | mutex_unlock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 77 | |
| 78 | return retval; |
| 79 | } |
| 80 | |
| 81 | static int serio_reconnect_driver(struct serio *serio) |
| 82 | { |
| 83 | int retval = -1; |
| 84 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 85 | mutex_lock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 86 | if (serio->drv && serio->drv->reconnect) |
| 87 | retval = serio->drv->reconnect(serio); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 88 | mutex_unlock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 89 | |
| 90 | return retval; |
| 91 | } |
| 92 | |
| 93 | static void serio_disconnect_driver(struct serio *serio) |
| 94 | { |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 95 | mutex_lock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 96 | if (serio->drv) |
| 97 | serio->drv->disconnect(serio); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 98 | mutex_unlock(&serio->drv_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | static 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 | |
| 118 | static void serio_bind_driver(struct serio *serio, struct serio_driver *drv) |
| 119 | { |
Dmitry Torokhov | 0a66045 | 2006-10-12 01:06:23 -0400 | [diff] [blame] | 120 | int error; |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | down_write(&serio_bus.subsys.rwsem); |
| 123 | |
| 124 | if (serio_match_port(drv->id_table, serio)) { |
| 125 | serio->dev.driver = &drv->driver; |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 126 | if (serio_connect_driver(serio, drv)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | serio->dev.driver = NULL; |
| 128 | goto out; |
| 129 | } |
Dmitry Torokhov | 0a66045 | 2006-10-12 01:06:23 -0400 | [diff] [blame] | 130 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
Dmitry Torokhov | 0a66045 | 2006-10-12 01:06:23 -0400 | [diff] [blame] | 142 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | up_write(&serio_bus.subsys.rwsem); |
| 144 | } |
| 145 | |
| 146 | static 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 | |
| 153 | static void serio_find_driver(struct serio *serio) |
| 154 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 155 | int error; |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | down_write(&serio_bus.subsys.rwsem); |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 158 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | up_write(&serio_bus.subsys.rwsem); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * Serio event processing. |
| 169 | */ |
| 170 | |
| 171 | enum serio_event_type { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 172 | SERIO_RESCAN_PORT, |
| 173 | SERIO_RECONNECT_PORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | SERIO_REGISTER_PORT, |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 175 | SERIO_ATTACH_DRIVER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | struct serio_event { |
| 179 | enum serio_event_type type; |
| 180 | void *object; |
| 181 | struct module *owner; |
| 182 | struct list_head node; |
| 183 | }; |
| 184 | |
| 185 | static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */ |
| 186 | static LIST_HEAD(serio_event_list); |
| 187 | static DECLARE_WAIT_QUEUE_HEAD(serio_wait); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 188 | static struct task_struct *serio_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 190 | static int serio_queue_event(void *object, struct module *owner, |
| 191 | enum serio_event_type event_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | unsigned long flags; |
| 194 | struct serio_event *event; |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 195 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | spin_lock_irqsave(&serio_event_lock, flags); |
| 198 | |
| 199 | /* |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 200 | * Scan event list for the other events for the same serio port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * 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 Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 205 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 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 Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 214 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 222 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | out: |
| 240 | spin_unlock_irqrestore(&serio_event_lock, flags); |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 241 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void serio_free_event(struct serio_event *event) |
| 245 | { |
| 246 | module_put(event->owner); |
| 247 | kfree(event); |
| 248 | } |
| 249 | |
| 250 | static 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 | |
| 278 | static 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 Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 300 | static void serio_handle_event(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
| 302 | struct serio_event *event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 304 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Dmitry Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 306 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | switch (event->type) { |
| 315 | case SERIO_REGISTER_PORT: |
| 316 | serio_add_port(event->object); |
| 317 | break; |
| 318 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 319 | case SERIO_RECONNECT_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | serio_reconnect_port(event->object); |
| 321 | break; |
| 322 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 323 | case SERIO_RESCAN_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | serio_disconnect_port(event->object); |
| 325 | serio_find_driver(event->object); |
| 326 | break; |
| 327 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 328 | case SERIO_ATTACH_DRIVER: |
| 329 | serio_attach_driver(event->object); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | default: |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | serio_remove_duplicate_events(event); |
| 337 | serio_free_event(event); |
| 338 | } |
| 339 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 340 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Remove all events that have been submitted for a given serio port. |
| 345 | */ |
| 346 | static 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 | */ |
| 373 | static 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 | |
| 395 | static int serio_thread(void *nothing) |
| 396 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | do { |
Dmitry Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 398 | serio_handle_event(); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 399 | wait_event_interruptible(serio_wait, |
| 400 | kthread_should_stop() || !list_empty(&serio_event_list)); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 401 | try_to_freeze(); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 402 | } while (!kthread_should_stop()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | printk(KERN_DEBUG "serio: kseriod exiting\n"); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 405 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
| 409 | /* |
| 410 | * Serio port operations |
| 411 | */ |
| 412 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 413 | static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | struct serio *serio = to_serio_port(dev); |
| 416 | return sprintf(buf, "%s\n", serio->name); |
| 417 | } |
| 418 | |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 419 | static 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 Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 427 | static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
| 429 | struct serio *serio = to_serio_port(dev); |
| 430 | return sprintf(buf, "%02x\n", serio->id.type); |
| 431 | } |
| 432 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 433 | static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | struct serio *serio = to_serio_port(dev); |
| 436 | return sprintf(buf, "%02x\n", serio->id.proto); |
| 437 | } |
| 438 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 439 | static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | struct serio *serio = to_serio_port(dev); |
| 442 | return sprintf(buf, "%02x\n", serio->id.id); |
| 443 | } |
| 444 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 445 | static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | struct serio *serio = to_serio_port(dev); |
| 448 | return sprintf(buf, "%02x\n", serio->id.extra); |
| 449 | } |
| 450 | |
Dmitry Torokhov | baae956 | 2005-05-16 21:53:09 -0700 | [diff] [blame] | 451 | static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL); |
| 452 | static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL); |
| 453 | static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL); |
| 454 | static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL); |
| 455 | |
| 456 | static 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 | |
| 464 | static struct attribute_group serio_id_attr_group = { |
| 465 | .name = "id", |
| 466 | .attrs = serio_device_id_attrs, |
| 467 | }; |
| 468 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 469 | static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct serio *serio = to_serio_port(dev); |
| 472 | struct device_driver *drv; |
| 473 | int retval; |
| 474 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 475 | retval = mutex_lock_interruptible(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | 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 Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 495 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | return retval; |
| 498 | } |
| 499 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 500 | static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | struct serio *serio = to_serio_port(dev); |
| 503 | return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto"); |
| 504 | } |
| 505 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 506 | static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
| 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 | |
| 523 | static struct device_attribute serio_device_attrs[] = { |
| 524 | __ATTR(description, S_IRUGO, serio_show_description, NULL), |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 525 | __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | __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 | |
| 532 | static 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 | */ |
| 543 | static 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 Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 549 | INIT_LIST_HEAD(&serio->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | spin_lock_init(&serio->lock); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 551 | mutex_init(&serio->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | 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 Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 557 | if (serio->parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | serio->dev.parent = &serio->parent->dev; |
Jiri Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 559 | serio->depth = serio->parent->depth + 1; |
| 560 | } else |
| 561 | serio->depth = 0; |
| 562 | lockdep_set_subclass(&serio->lock, serio->depth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Complete serio port registration. |
| 567 | * Driver core will attempt to find appropriate driver for the port. |
| 568 | */ |
| 569 | static void serio_add_port(struct serio *serio) |
| 570 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 571 | int error; |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 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 Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 582 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
| 598 | * serio_destroy_port() completes deregistration process and removes |
| 599 | * port from the system |
| 600 | */ |
| 601 | static 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 Torokhov | baae956 | 2005-05-16 21:53:09 -0700 | [diff] [blame] | 622 | sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | device_del(&serio->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | serio->registered = 0; |
| 625 | } |
| 626 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 627 | list_del_init(&serio->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | 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 | */ |
| 635 | static void serio_reconnect_port(struct serio *serio) |
| 636 | { |
| 637 | do { |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 638 | if (serio_reconnect_driver(serio)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | 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 | */ |
| 652 | static 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 | |
| 679 | void serio_rescan(struct serio *serio) |
| 680 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 681 | serio_queue_event(serio, NULL, SERIO_RESCAN_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void serio_reconnect(struct serio *serio) |
| 685 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 686 | serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Submits register request to kseriod for subsequent execution. |
| 691 | * Note that port registration is always asynchronous. |
| 692 | */ |
| 693 | void __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 | */ |
| 702 | void serio_unregister_port(struct serio *serio) |
| 703 | { |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 704 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | serio_disconnect_port(serio); |
| 706 | serio_destroy_port(serio); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 707 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 711 | * Safely unregisters child port if one is present. |
| 712 | */ |
| 713 | void serio_unregister_child_port(struct serio *serio) |
| 714 | { |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 715 | mutex_lock(&serio_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 716 | if (serio->child) { |
| 717 | serio_disconnect_port(serio->child); |
| 718 | serio_destroy_port(serio->child); |
| 719 | } |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 720 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * Serio driver operations |
| 726 | */ |
| 727 | |
| 728 | static 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 | |
| 734 | static 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 | |
| 740 | static 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 | |
| 758 | static 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 | |
| 765 | static 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 Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 770 | return serio_connect_driver(serio, drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static int serio_driver_remove(struct device *dev) |
| 774 | { |
| 775 | struct serio *serio = to_serio_port(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 777 | serio_disconnect_driver(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
| 780 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 781 | static void serio_cleanup(struct serio *serio) |
| 782 | { |
| 783 | if (serio->drv && serio->drv->cleanup) |
| 784 | serio->drv->cleanup(serio); |
| 785 | } |
| 786 | |
| 787 | static void serio_shutdown(struct device *dev) |
| 788 | { |
| 789 | struct serio *serio = to_serio_port(dev); |
| 790 | |
| 791 | serio_cleanup(serio); |
| 792 | } |
| 793 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 794 | static void serio_attach_driver(struct serio_driver *drv) |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 795 | { |
| 796 | int error; |
| 797 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 798 | error = driver_attach(&drv->driver); |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 799 | if (error) |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 800 | printk(KERN_WARNING |
| 801 | "serio: driver_attach() failed for %s with error %d\n", |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 802 | drv->driver.name, error); |
| 803 | } |
| 804 | |
Greg Kroah-Hartman | 4b31562 | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 805 | int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 807 | int manual_bind = drv->manual_bind; |
| 808 | int error; |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | drv->driver.bus = &serio_bus; |
Greg Kroah-Hartman | 4b31562 | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 811 | drv->driver.owner = owner; |
| 812 | drv->driver.mod_name = mod_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 814 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | void serio_unregister_driver(struct serio_driver *drv) |
| 845 | { |
| 846 | struct serio *serio; |
| 847 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 848 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | drv->manual_bind = 1; /* so serio_find_driver ignores it */ |
| 850 | |
| 851 | start_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 Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 862 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void serio_set_drv(struct serio *serio, struct serio_driver *drv) |
| 866 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | serio_pause_rx(serio); |
| 868 | serio->drv = drv; |
| 869 | serio_continue_rx(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static 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 Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 885 | #define SERIO_ADD_UEVENT_VAR(fmt, val...) \ |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 886 | do { \ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 887 | int err = add_uevent_var(envp, num_envp, &i, \ |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 888 | buffer, buffer_size, &len, \ |
| 889 | fmt, val); \ |
| 890 | if (err) \ |
| 891 | return err; \ |
| 892 | } while (0) |
| 893 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 894 | static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
| 896 | struct serio *serio; |
| 897 | int i = 0; |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 898 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
| 900 | if (!dev) |
| 901 | return -ENODEV; |
| 902 | |
| 903 | serio = to_serio_port(dev); |
| 904 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 905 | 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 Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 910 | serio->id.type, serio->id.proto, serio->id.id, serio->id.extra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | envp[i] = NULL; |
| 912 | |
| 913 | return 0; |
| 914 | } |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 915 | #undef SERIO_ADD_UEVENT_VAR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | #else |
| 918 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 919 | static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
| 921 | return -ENODEV; |
| 922 | } |
| 923 | |
| 924 | #endif /* CONFIG_HOTPLUG */ |
| 925 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 926 | #ifdef CONFIG_PM |
| 927 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | static int serio_resume(struct device *dev) |
| 940 | { |
| 941 | struct serio *serio = to_serio_port(dev); |
| 942 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 943 | if (dev->power.power_state.event != PM_EVENT_ON && |
| 944 | serio_reconnect_driver(serio)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* |
| 946 | * Driver re-probing can take a while, so better let kseriod |
| 947 | * deal with it. |
| 948 | */ |
| 949 | serio_rescan(serio); |
| 950 | } |
| 951 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 952 | dev->power.power_state = PMSG_ON; |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return 0; |
| 955 | } |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 956 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 958 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | int 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 Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 970 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | void serio_close(struct serio *serio) |
| 972 | { |
| 973 | if (serio->close) |
| 974 | serio->close(serio); |
| 975 | |
| 976 | serio_set_drv(serio, NULL); |
| 977 | } |
| 978 | |
| 979 | irqreturn_t serio_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 980 | unsigned char data, unsigned int dfl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
| 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 Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 988 | ret = serio->drv->interrupt(serio, data, dfl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } 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 Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 999 | static 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 Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 1007 | .shutdown = serio_shutdown, |
| 1008 | #ifdef CONFIG_PM |
| 1009 | .suspend = serio_suspend, |
Marton Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 1010 | .resume = serio_resume, |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame^] | 1011 | #endif |
Marton Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 1012 | }; |
| 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | static int __init serio_init(void) |
| 1015 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 1016 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 1018 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | static void __exit serio_exit(void) |
| 1036 | { |
| 1037 | bus_unregister(&serio_bus); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 1038 | kthread_stop(serio_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Dmitry Torokhov | 51c38f9 | 2006-02-19 00:22:51 -0500 | [diff] [blame] | 1041 | subsys_initcall(serio_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | module_exit(serio_exit); |