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 | |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 118 | static int serio_bind_driver(struct serio *serio, struct serio_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | if (serio_match_port(drv->id_table, serio)) { |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | serio->dev.driver = &drv->driver; |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 125 | if (serio_connect_driver(serio, drv)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | serio->dev.driver = NULL; |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 127 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 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; |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 139 | return error; |
Dmitry Torokhov | 0a66045 | 2006-10-12 01:06:23 -0400 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 142 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void serio_find_driver(struct serio *serio) |
| 146 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 147 | int error; |
| 148 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 149 | error = device_attach(&serio->dev); |
| 150 | if (error < 0) |
| 151 | printk(KERN_WARNING |
| 152 | "serio: device_attach() failed for %s (%s), error: %d\n", |
| 153 | serio->phys, serio->name, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | /* |
| 158 | * Serio event processing. |
| 159 | */ |
| 160 | |
| 161 | enum serio_event_type { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 162 | SERIO_RESCAN_PORT, |
| 163 | SERIO_RECONNECT_PORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | SERIO_REGISTER_PORT, |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 165 | SERIO_ATTACH_DRIVER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | struct serio_event { |
| 169 | enum serio_event_type type; |
| 170 | void *object; |
| 171 | struct module *owner; |
| 172 | struct list_head node; |
| 173 | }; |
| 174 | |
| 175 | static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */ |
| 176 | static LIST_HEAD(serio_event_list); |
| 177 | static DECLARE_WAIT_QUEUE_HEAD(serio_wait); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 178 | static struct task_struct *serio_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 180 | static int serio_queue_event(void *object, struct module *owner, |
| 181 | enum serio_event_type event_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | unsigned long flags; |
| 184 | struct serio_event *event; |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 185 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | spin_lock_irqsave(&serio_event_lock, flags); |
| 188 | |
| 189 | /* |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 190 | * Scan event list for the other events for the same serio port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * starting with the most recent one. If event is the same we |
| 192 | * do not need add new one. If event is of different type we |
| 193 | * need to add this event and should not look further because |
| 194 | * we need to preseve sequence of distinct events. |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 195 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | list_for_each_entry_reverse(event, &serio_event_list, node) { |
| 197 | if (event->object == object) { |
| 198 | if (event->type == event_type) |
| 199 | goto out; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 204 | event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC); |
| 205 | if (!event) { |
| 206 | printk(KERN_ERR |
| 207 | "serio: Not enough memory to queue event %d\n", |
| 208 | event_type); |
| 209 | retval = -ENOMEM; |
| 210 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 212 | |
| 213 | if (!try_module_get(owner)) { |
| 214 | printk(KERN_WARNING |
| 215 | "serio: Can't get module reference, dropping event %d\n", |
| 216 | event_type); |
| 217 | kfree(event); |
| 218 | retval = -EINVAL; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | event->type = event_type; |
| 223 | event->object = object; |
| 224 | event->owner = owner; |
| 225 | |
| 226 | list_add_tail(&event->node, &serio_event_list); |
| 227 | wake_up(&serio_wait); |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | out: |
| 230 | spin_unlock_irqrestore(&serio_event_lock, flags); |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 231 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void serio_free_event(struct serio_event *event) |
| 235 | { |
| 236 | module_put(event->owner); |
| 237 | kfree(event); |
| 238 | } |
| 239 | |
| 240 | static void serio_remove_duplicate_events(struct serio_event *event) |
| 241 | { |
| 242 | struct list_head *node, *next; |
| 243 | struct serio_event *e; |
| 244 | unsigned long flags; |
| 245 | |
| 246 | spin_lock_irqsave(&serio_event_lock, flags); |
| 247 | |
| 248 | list_for_each_safe(node, next, &serio_event_list) { |
| 249 | e = list_entry(node, struct serio_event, node); |
| 250 | if (event->object == e->object) { |
| 251 | /* |
| 252 | * If this event is of different type we should not |
| 253 | * look further - we only suppress duplicate events |
| 254 | * that were sent back-to-back. |
| 255 | */ |
| 256 | if (event->type != e->type) |
| 257 | break; |
| 258 | |
| 259 | list_del_init(node); |
| 260 | serio_free_event(e); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | spin_unlock_irqrestore(&serio_event_lock, flags); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static struct serio_event *serio_get_event(void) |
| 269 | { |
| 270 | struct serio_event *event; |
| 271 | struct list_head *node; |
| 272 | unsigned long flags; |
| 273 | |
| 274 | spin_lock_irqsave(&serio_event_lock, flags); |
| 275 | |
| 276 | if (list_empty(&serio_event_list)) { |
| 277 | spin_unlock_irqrestore(&serio_event_lock, flags); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | node = serio_event_list.next; |
| 282 | event = list_entry(node, struct serio_event, node); |
| 283 | list_del_init(node); |
| 284 | |
| 285 | spin_unlock_irqrestore(&serio_event_lock, flags); |
| 286 | |
| 287 | return event; |
| 288 | } |
| 289 | |
Dmitry Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 290 | static void serio_handle_event(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | struct serio_event *event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 294 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Dmitry Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 296 | /* |
| 297 | * Note that we handle only one event here to give swsusp |
| 298 | * a chance to freeze kseriod thread. Serio events should |
| 299 | * be pretty rare so we are not concerned about taking |
| 300 | * performance hit. |
| 301 | */ |
| 302 | if ((event = serio_get_event())) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | switch (event->type) { |
| 305 | case SERIO_REGISTER_PORT: |
| 306 | serio_add_port(event->object); |
| 307 | break; |
| 308 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 309 | case SERIO_RECONNECT_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | serio_reconnect_port(event->object); |
| 311 | break; |
| 312 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 313 | case SERIO_RESCAN_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | serio_disconnect_port(event->object); |
| 315 | serio_find_driver(event->object); |
| 316 | break; |
| 317 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 318 | case SERIO_ATTACH_DRIVER: |
| 319 | serio_attach_driver(event->object); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | break; |
| 321 | |
| 322 | default: |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | serio_remove_duplicate_events(event); |
| 327 | serio_free_event(event); |
| 328 | } |
| 329 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 330 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Remove all events that have been submitted for a given serio port. |
| 335 | */ |
| 336 | static void serio_remove_pending_events(struct serio *serio) |
| 337 | { |
| 338 | struct list_head *node, *next; |
| 339 | struct serio_event *event; |
| 340 | unsigned long flags; |
| 341 | |
| 342 | spin_lock_irqsave(&serio_event_lock, flags); |
| 343 | |
| 344 | list_for_each_safe(node, next, &serio_event_list) { |
| 345 | event = list_entry(node, struct serio_event, node); |
| 346 | if (event->object == serio) { |
| 347 | list_del_init(node); |
| 348 | serio_free_event(event); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | spin_unlock_irqrestore(&serio_event_lock, flags); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Destroy child serio port (if any) that has not been fully registered yet. |
| 357 | * |
| 358 | * Note that we rely on the fact that port can have only one child and therefore |
| 359 | * only one child registration request can be pending. Additionally, children |
| 360 | * are registered by driver's connect() handler so there can't be a grandchild |
| 361 | * pending registration together with a child. |
| 362 | */ |
| 363 | static struct serio *serio_get_pending_child(struct serio *parent) |
| 364 | { |
| 365 | struct serio_event *event; |
| 366 | struct serio *serio, *child = NULL; |
| 367 | unsigned long flags; |
| 368 | |
| 369 | spin_lock_irqsave(&serio_event_lock, flags); |
| 370 | |
| 371 | list_for_each_entry(event, &serio_event_list, node) { |
| 372 | if (event->type == SERIO_REGISTER_PORT) { |
| 373 | serio = event->object; |
| 374 | if (serio->parent == parent) { |
| 375 | child = serio; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | spin_unlock_irqrestore(&serio_event_lock, flags); |
| 382 | return child; |
| 383 | } |
| 384 | |
| 385 | static int serio_thread(void *nothing) |
| 386 | { |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 387 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | do { |
Dmitry Torokhov | 9e50afd | 2005-11-20 00:56:43 -0500 | [diff] [blame] | 389 | serio_handle_event(); |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 390 | wait_event_freezable(serio_wait, |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 391 | kthread_should_stop() || !list_empty(&serio_event_list)); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 392 | } while (!kthread_should_stop()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | printk(KERN_DEBUG "serio: kseriod exiting\n"); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 395 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
| 399 | /* |
| 400 | * Serio port operations |
| 401 | */ |
| 402 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 403 | 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] | 404 | { |
| 405 | struct serio *serio = to_serio_port(dev); |
| 406 | return sprintf(buf, "%s\n", serio->name); |
| 407 | } |
| 408 | |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 409 | static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) |
| 410 | { |
| 411 | struct serio *serio = to_serio_port(dev); |
| 412 | |
| 413 | return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n", |
| 414 | serio->id.type, serio->id.proto, serio->id.id, serio->id.extra); |
| 415 | } |
| 416 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | struct serio *serio = to_serio_port(dev); |
| 420 | return sprintf(buf, "%02x\n", serio->id.type); |
| 421 | } |
| 422 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 423 | 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] | 424 | { |
| 425 | struct serio *serio = to_serio_port(dev); |
| 426 | return sprintf(buf, "%02x\n", serio->id.proto); |
| 427 | } |
| 428 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 429 | 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] | 430 | { |
| 431 | struct serio *serio = to_serio_port(dev); |
| 432 | return sprintf(buf, "%02x\n", serio->id.id); |
| 433 | } |
| 434 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 435 | 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] | 436 | { |
| 437 | struct serio *serio = to_serio_port(dev); |
| 438 | return sprintf(buf, "%02x\n", serio->id.extra); |
| 439 | } |
| 440 | |
Dmitry Torokhov | baae956 | 2005-05-16 21:53:09 -0700 | [diff] [blame] | 441 | static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL); |
| 442 | static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL); |
| 443 | static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL); |
| 444 | static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL); |
| 445 | |
| 446 | static struct attribute *serio_device_id_attrs[] = { |
| 447 | &dev_attr_type.attr, |
| 448 | &dev_attr_proto.attr, |
| 449 | &dev_attr_id.attr, |
| 450 | &dev_attr_extra.attr, |
| 451 | NULL |
| 452 | }; |
| 453 | |
| 454 | static struct attribute_group serio_id_attr_group = { |
| 455 | .name = "id", |
| 456 | .attrs = serio_device_id_attrs, |
| 457 | }; |
| 458 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 459 | 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] | 460 | { |
| 461 | struct serio *serio = to_serio_port(dev); |
| 462 | struct device_driver *drv; |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 463 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 465 | error = mutex_lock_interruptible(&serio_mutex); |
| 466 | if (error) |
| 467 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (!strncmp(buf, "none", count)) { |
| 470 | serio_disconnect_port(serio); |
| 471 | } else if (!strncmp(buf, "reconnect", count)) { |
| 472 | serio_reconnect_port(serio); |
| 473 | } else if (!strncmp(buf, "rescan", count)) { |
| 474 | serio_disconnect_port(serio); |
| 475 | serio_find_driver(serio); |
| 476 | } else if ((drv = driver_find(buf, &serio_bus)) != NULL) { |
| 477 | serio_disconnect_port(serio); |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 478 | error = serio_bind_driver(serio, to_serio_driver(drv)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | put_driver(drv); |
| 480 | } else { |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 481 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 484 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 486 | return error ? error : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 489 | 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] | 490 | { |
| 491 | struct serio *serio = to_serio_port(dev); |
| 492 | return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto"); |
| 493 | } |
| 494 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 495 | 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] | 496 | { |
| 497 | struct serio *serio = to_serio_port(dev); |
| 498 | int retval; |
| 499 | |
| 500 | retval = count; |
| 501 | if (!strncmp(buf, "manual", count)) { |
| 502 | serio->manual_bind = 1; |
| 503 | } else if (!strncmp(buf, "auto", count)) { |
| 504 | serio->manual_bind = 0; |
| 505 | } else { |
| 506 | retval = -EINVAL; |
| 507 | } |
| 508 | |
| 509 | return retval; |
| 510 | } |
| 511 | |
| 512 | static struct device_attribute serio_device_attrs[] = { |
| 513 | __ATTR(description, S_IRUGO, serio_show_description, NULL), |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 514 | __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver), |
| 516 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode), |
| 517 | __ATTR_NULL |
| 518 | }; |
| 519 | |
| 520 | |
| 521 | static void serio_release_port(struct device *dev) |
| 522 | { |
| 523 | struct serio *serio = to_serio_port(dev); |
| 524 | |
| 525 | kfree(serio); |
| 526 | module_put(THIS_MODULE); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Prepare serio port for registration. |
| 531 | */ |
| 532 | static void serio_init_port(struct serio *serio) |
| 533 | { |
| 534 | static atomic_t serio_no = ATOMIC_INIT(0); |
| 535 | |
| 536 | __module_get(THIS_MODULE); |
| 537 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 538 | INIT_LIST_HEAD(&serio->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | spin_lock_init(&serio->lock); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 540 | mutex_init(&serio->drv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | device_initialize(&serio->dev); |
| 542 | snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), |
| 543 | "serio%ld", (long)atomic_inc_return(&serio_no) - 1); |
| 544 | serio->dev.bus = &serio_bus; |
| 545 | serio->dev.release = serio_release_port; |
Jiri Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 546 | if (serio->parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | serio->dev.parent = &serio->parent->dev; |
Jiri Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 548 | serio->depth = serio->parent->depth + 1; |
| 549 | } else |
| 550 | serio->depth = 0; |
| 551 | lockdep_set_subclass(&serio->lock, serio->depth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Complete serio port registration. |
| 556 | * Driver core will attempt to find appropriate driver for the port. |
| 557 | */ |
| 558 | static void serio_add_port(struct serio *serio) |
| 559 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 560 | int error; |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | if (serio->parent) { |
| 563 | serio_pause_rx(serio->parent); |
| 564 | serio->parent->child = serio; |
| 565 | serio_continue_rx(serio->parent); |
| 566 | } |
| 567 | |
| 568 | list_add_tail(&serio->node, &serio_list); |
| 569 | if (serio->start) |
| 570 | serio->start(serio); |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 571 | error = device_add(&serio->dev); |
| 572 | if (error) |
| 573 | printk(KERN_ERR |
| 574 | "serio: device_add() failed for %s (%s), error: %d\n", |
| 575 | serio->phys, serio->name, error); |
| 576 | else { |
| 577 | serio->registered = 1; |
| 578 | error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group); |
| 579 | if (error) |
| 580 | printk(KERN_ERR |
| 581 | "serio: sysfs_create_group() failed for %s (%s), error: %d\n", |
| 582 | serio->phys, serio->name, error); |
| 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
| 587 | * serio_destroy_port() completes deregistration process and removes |
| 588 | * port from the system |
| 589 | */ |
| 590 | static void serio_destroy_port(struct serio *serio) |
| 591 | { |
| 592 | struct serio *child; |
| 593 | |
| 594 | child = serio_get_pending_child(serio); |
| 595 | if (child) { |
| 596 | serio_remove_pending_events(child); |
| 597 | put_device(&child->dev); |
| 598 | } |
| 599 | |
| 600 | if (serio->stop) |
| 601 | serio->stop(serio); |
| 602 | |
| 603 | if (serio->parent) { |
| 604 | serio_pause_rx(serio->parent); |
| 605 | serio->parent->child = NULL; |
| 606 | serio_continue_rx(serio->parent); |
| 607 | serio->parent = NULL; |
| 608 | } |
| 609 | |
| 610 | if (serio->registered) { |
Dmitry Torokhov | baae956 | 2005-05-16 21:53:09 -0700 | [diff] [blame] | 611 | sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | device_del(&serio->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | serio->registered = 0; |
| 614 | } |
| 615 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 616 | list_del_init(&serio->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | serio_remove_pending_events(serio); |
| 618 | put_device(&serio->dev); |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Reconnect serio port and all its children (re-initialize attached devices) |
| 623 | */ |
| 624 | static void serio_reconnect_port(struct serio *serio) |
| 625 | { |
| 626 | do { |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 627 | if (serio_reconnect_driver(serio)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | serio_disconnect_port(serio); |
| 629 | serio_find_driver(serio); |
| 630 | /* Ok, old children are now gone, we are done */ |
| 631 | break; |
| 632 | } |
| 633 | serio = serio->child; |
| 634 | } while (serio); |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * serio_disconnect_port() unbinds a port from its driver. As a side effect |
| 639 | * all child ports are unbound and destroyed. |
| 640 | */ |
| 641 | static void serio_disconnect_port(struct serio *serio) |
| 642 | { |
| 643 | struct serio *s, *parent; |
| 644 | |
| 645 | if (serio->child) { |
| 646 | /* |
| 647 | * Children ports should be disconnected and destroyed |
| 648 | * first, staring with the leaf one, since we don't want |
| 649 | * to do recursion |
| 650 | */ |
| 651 | for (s = serio; s->child; s = s->child) |
| 652 | /* empty */; |
| 653 | |
| 654 | do { |
| 655 | parent = s->parent; |
| 656 | |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 657 | device_release_driver(&s->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | serio_destroy_port(s); |
| 659 | } while ((s = parent) != serio); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Ok, no children left, now disconnect this port |
| 664 | */ |
Dmitry Torokhov | 70f256f | 2007-04-10 00:40:27 -0400 | [diff] [blame] | 665 | device_release_driver(&serio->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | void serio_rescan(struct serio *serio) |
| 669 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 670 | serio_queue_event(serio, NULL, SERIO_RESCAN_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void serio_reconnect(struct serio *serio) |
| 674 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 675 | serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Submits register request to kseriod for subsequent execution. |
| 680 | * Note that port registration is always asynchronous. |
| 681 | */ |
| 682 | void __serio_register_port(struct serio *serio, struct module *owner) |
| 683 | { |
| 684 | serio_init_port(serio); |
| 685 | serio_queue_event(serio, owner, SERIO_REGISTER_PORT); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Synchronously unregisters serio port. |
| 690 | */ |
| 691 | void serio_unregister_port(struct serio *serio) |
| 692 | { |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 693 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | serio_disconnect_port(serio); |
| 695 | serio_destroy_port(serio); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 696 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 700 | * Safely unregisters child port if one is present. |
| 701 | */ |
| 702 | void serio_unregister_child_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 | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 705 | if (serio->child) { |
| 706 | serio_disconnect_port(serio->child); |
| 707 | serio_destroy_port(serio->child); |
| 708 | } |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 709 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * Serio driver operations |
| 715 | */ |
| 716 | |
| 717 | static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf) |
| 718 | { |
| 719 | struct serio_driver *driver = to_serio_driver(drv); |
| 720 | return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)"); |
| 721 | } |
| 722 | |
| 723 | static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf) |
| 724 | { |
| 725 | struct serio_driver *serio_drv = to_serio_driver(drv); |
| 726 | return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto"); |
| 727 | } |
| 728 | |
| 729 | static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count) |
| 730 | { |
| 731 | struct serio_driver *serio_drv = to_serio_driver(drv); |
| 732 | int retval; |
| 733 | |
| 734 | retval = count; |
| 735 | if (!strncmp(buf, "manual", count)) { |
| 736 | serio_drv->manual_bind = 1; |
| 737 | } else if (!strncmp(buf, "auto", count)) { |
| 738 | serio_drv->manual_bind = 0; |
| 739 | } else { |
| 740 | retval = -EINVAL; |
| 741 | } |
| 742 | |
| 743 | return retval; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | static struct driver_attribute serio_driver_attrs[] = { |
| 748 | __ATTR(description, S_IRUGO, serio_driver_show_description, NULL), |
| 749 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, |
| 750 | serio_driver_show_bind_mode, serio_driver_set_bind_mode), |
| 751 | __ATTR_NULL |
| 752 | }; |
| 753 | |
| 754 | static int serio_driver_probe(struct device *dev) |
| 755 | { |
| 756 | struct serio *serio = to_serio_port(dev); |
| 757 | struct serio_driver *drv = to_serio_driver(dev->driver); |
| 758 | |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 759 | return serio_connect_driver(serio, drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static int serio_driver_remove(struct device *dev) |
| 763 | { |
| 764 | struct serio *serio = to_serio_port(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 766 | serio_disconnect_driver(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 770 | static void serio_cleanup(struct serio *serio) |
| 771 | { |
Dmitry Torokhov | 33143ea | 2007-06-29 01:06:35 -0400 | [diff] [blame] | 772 | mutex_lock(&serio->drv_mutex); |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 773 | if (serio->drv && serio->drv->cleanup) |
| 774 | serio->drv->cleanup(serio); |
Dmitry Torokhov | 33143ea | 2007-06-29 01:06:35 -0400 | [diff] [blame] | 775 | mutex_unlock(&serio->drv_mutex); |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void serio_shutdown(struct device *dev) |
| 779 | { |
| 780 | struct serio *serio = to_serio_port(dev); |
| 781 | |
| 782 | serio_cleanup(serio); |
| 783 | } |
| 784 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 785 | static void serio_attach_driver(struct serio_driver *drv) |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 786 | { |
| 787 | int error; |
| 788 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 789 | error = driver_attach(&drv->driver); |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 790 | if (error) |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 791 | printk(KERN_WARNING |
| 792 | "serio: driver_attach() failed for %s with error %d\n", |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 793 | drv->driver.name, error); |
| 794 | } |
| 795 | |
Greg Kroah-Hartman | 4b31562 | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 796 | 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] | 797 | { |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 798 | int manual_bind = drv->manual_bind; |
| 799 | int error; |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | drv->driver.bus = &serio_bus; |
Greg Kroah-Hartman | 4b31562 | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 802 | drv->driver.owner = owner; |
| 803 | drv->driver.mod_name = mod_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Dmitry Torokhov | ed7b1f6 | 2006-11-23 23:34:49 -0500 | [diff] [blame] | 805 | /* |
| 806 | * Temporarily disable automatic binding because probing |
| 807 | * takes long time and we are better off doing it in kseriod |
| 808 | */ |
| 809 | drv->manual_bind = 1; |
| 810 | |
| 811 | error = driver_register(&drv->driver); |
| 812 | if (error) { |
| 813 | printk(KERN_ERR |
| 814 | "serio: driver_register() failed for %s, error: %d\n", |
| 815 | drv->driver.name, error); |
| 816 | return error; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Restore original bind mode and let kseriod bind the |
| 821 | * driver to free ports |
| 822 | */ |
| 823 | if (!manual_bind) { |
| 824 | drv->manual_bind = 0; |
| 825 | error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER); |
| 826 | if (error) { |
| 827 | driver_unregister(&drv->driver); |
| 828 | return error; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | void serio_unregister_driver(struct serio_driver *drv) |
| 836 | { |
| 837 | struct serio *serio; |
| 838 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 839 | mutex_lock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | drv->manual_bind = 1; /* so serio_find_driver ignores it */ |
| 841 | |
| 842 | start_over: |
| 843 | list_for_each_entry(serio, &serio_list, node) { |
| 844 | if (serio->drv == drv) { |
| 845 | serio_disconnect_port(serio); |
| 846 | serio_find_driver(serio); |
| 847 | /* we could've deleted some ports, restart */ |
| 848 | goto start_over; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | driver_unregister(&drv->driver); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 853 | mutex_unlock(&serio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void serio_set_drv(struct serio *serio, struct serio_driver *drv) |
| 857 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | serio_pause_rx(serio); |
| 859 | serio->drv = drv; |
| 860 | serio_continue_rx(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static int serio_bus_match(struct device *dev, struct device_driver *drv) |
| 864 | { |
| 865 | struct serio *serio = to_serio_port(dev); |
| 866 | struct serio_driver *serio_drv = to_serio_driver(drv); |
| 867 | |
| 868 | if (serio->manual_bind || serio_drv->manual_bind) |
| 869 | return 0; |
| 870 | |
| 871 | return serio_match_port(serio_drv->id_table, serio); |
| 872 | } |
| 873 | |
| 874 | #ifdef CONFIG_HOTPLUG |
| 875 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 876 | #define SERIO_ADD_UEVENT_VAR(fmt, val...) \ |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 877 | do { \ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 878 | int err = add_uevent_var(env, fmt, val); \ |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 879 | if (err) \ |
| 880 | return err; \ |
| 881 | } while (0) |
| 882 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 883 | static int serio_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | { |
| 885 | struct serio *serio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | if (!dev) |
| 888 | return -ENODEV; |
| 889 | |
| 890 | serio = to_serio_port(dev); |
| 891 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 892 | SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type); |
| 893 | SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto); |
| 894 | SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id); |
| 895 | SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra); |
| 896 | SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X", |
Dmitry Torokhov | ae87dff | 2005-06-30 00:48:34 -0500 | [diff] [blame] | 897 | serio->id.type, serio->id.proto, serio->id.id, serio->id.extra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | return 0; |
| 900 | } |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 901 | #undef SERIO_ADD_UEVENT_VAR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | #else |
| 904 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 905 | static int serio_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
| 907 | return -ENODEV; |
| 908 | } |
| 909 | |
| 910 | #endif /* CONFIG_HOTPLUG */ |
| 911 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 912 | #ifdef CONFIG_PM |
| 913 | static int serio_suspend(struct device *dev, pm_message_t state) |
| 914 | { |
| 915 | if (dev->power.power_state.event != state.event) { |
| 916 | if (state.event == PM_EVENT_SUSPEND) |
| 917 | serio_cleanup(to_serio_port(dev)); |
| 918 | |
| 919 | dev->power.power_state = state; |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | static int serio_resume(struct device *dev) |
| 926 | { |
| 927 | struct serio *serio = to_serio_port(dev); |
| 928 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 929 | if (dev->power.power_state.event != PM_EVENT_ON && |
| 930 | serio_reconnect_driver(serio)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | /* |
| 932 | * Driver re-probing can take a while, so better let kseriod |
| 933 | * deal with it. |
| 934 | */ |
| 935 | serio_rescan(serio); |
| 936 | } |
| 937 | |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 938 | dev->power.power_state = PMSG_ON; |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return 0; |
| 941 | } |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 942 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 944 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | int serio_open(struct serio *serio, struct serio_driver *drv) |
| 946 | { |
| 947 | serio_set_drv(serio, drv); |
| 948 | |
| 949 | if (serio->open && serio->open(serio)) { |
| 950 | serio_set_drv(serio, NULL); |
| 951 | return -1; |
| 952 | } |
| 953 | return 0; |
| 954 | } |
| 955 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 956 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | void serio_close(struct serio *serio) |
| 958 | { |
| 959 | if (serio->close) |
| 960 | serio->close(serio); |
| 961 | |
| 962 | serio_set_drv(serio, NULL); |
| 963 | } |
| 964 | |
| 965 | irqreturn_t serio_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 966 | unsigned char data, unsigned int dfl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
| 968 | unsigned long flags; |
| 969 | irqreturn_t ret = IRQ_NONE; |
| 970 | |
| 971 | spin_lock_irqsave(&serio->lock, flags); |
| 972 | |
| 973 | if (likely(serio->drv)) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 974 | ret = serio->drv->interrupt(serio, data, dfl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } else if (!dfl && serio->registered) { |
| 976 | serio_rescan(serio); |
| 977 | ret = IRQ_HANDLED; |
| 978 | } |
| 979 | |
| 980 | spin_unlock_irqrestore(&serio->lock, flags); |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
Marton Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 985 | static struct bus_type serio_bus = { |
| 986 | .name = "serio", |
| 987 | .dev_attrs = serio_device_attrs, |
| 988 | .drv_attrs = serio_driver_attrs, |
| 989 | .match = serio_bus_match, |
| 990 | .uevent = serio_uevent, |
| 991 | .probe = serio_driver_probe, |
| 992 | .remove = serio_driver_remove, |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 993 | .shutdown = serio_shutdown, |
| 994 | #ifdef CONFIG_PM |
| 995 | .suspend = serio_suspend, |
Marton Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 996 | .resume = serio_resume, |
Dmitry Torokhov | 82dd9ef | 2007-02-18 01:40:30 -0500 | [diff] [blame] | 997 | #endif |
Marton Nemeth | 1ea2a69 | 2006-11-02 23:27:21 -0500 | [diff] [blame] | 998 | }; |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | static int __init serio_init(void) |
| 1001 | { |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 1002 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Randy Dunlap | 73b59a3 | 2006-07-19 01:14:55 -0400 | [diff] [blame] | 1004 | error = bus_register(&serio_bus); |
| 1005 | if (error) { |
| 1006 | printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error); |
| 1007 | return error; |
| 1008 | } |
| 1009 | |
| 1010 | serio_task = kthread_run(serio_thread, NULL, "kseriod"); |
| 1011 | if (IS_ERR(serio_task)) { |
| 1012 | bus_unregister(&serio_bus); |
| 1013 | error = PTR_ERR(serio_task); |
| 1014 | printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error); |
| 1015 | return error; |
| 1016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static void __exit serio_exit(void) |
| 1022 | { |
| 1023 | bus_unregister(&serio_bus); |
Linus Torvalds | 3c803e8 | 2005-06-27 17:49:45 -0700 | [diff] [blame] | 1024 | kthread_stop(serio_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Dmitry Torokhov | 51c38f9 | 2006-02-19 00:22:51 -0500 | [diff] [blame] | 1027 | subsys_initcall(serio_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | module_exit(serio_exit); |