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