Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 | 34 | #include <linux/sched.h> |
1da177e4 | 35 | #include <linux/slab.h> |
3c803e8e | 36 | #include <linux/kthread.h> |
c4e32e9f | 37 | #include <linux/mutex.h> |
7dfb7103 | 38 | #include <linux/freezer.h> |
1da177e4 LT |
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); | |
3c803e8e | 47 | EXPORT_SYMBOL(serio_unregister_child_port); |
4b315627 | 48 | EXPORT_SYMBOL(__serio_register_driver); |
1da177e4 LT |
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 | /* | |
c4e32e9f | 56 | * serio_mutex protects entire serio subsystem and is taken every time |
1da177e4 LT |
57 | * serio port or driver registrered or unregistered. |
58 | */ | |
c4e32e9f | 59 | static DEFINE_MUTEX(serio_mutex); |
1da177e4 LT |
60 | |
61 | static LIST_HEAD(serio_list); | |
62 | ||
30226f81 | 63 | static struct bus_type serio_bus; |
1da177e4 LT |
64 | |
65 | static void serio_add_port(struct serio *serio); | |
1da177e4 LT |
66 | static void serio_reconnect_port(struct serio *serio); |
67 | static void serio_disconnect_port(struct serio *serio); | |
ed7b1f6d | 68 | static void serio_attach_driver(struct serio_driver *drv); |
1da177e4 | 69 | |
3c803e8e LT |
70 | static int serio_connect_driver(struct serio *serio, struct serio_driver *drv) |
71 | { | |
72 | int retval; | |
73 | ||
c4e32e9f | 74 | mutex_lock(&serio->drv_mutex); |
3c803e8e | 75 | retval = drv->connect(serio, drv); |
c4e32e9f | 76 | mutex_unlock(&serio->drv_mutex); |
3c803e8e LT |
77 | |
78 | return retval; | |
79 | } | |
80 | ||
81 | static int serio_reconnect_driver(struct serio *serio) | |
82 | { | |
83 | int retval = -1; | |
84 | ||
c4e32e9f | 85 | mutex_lock(&serio->drv_mutex); |
3c803e8e LT |
86 | if (serio->drv && serio->drv->reconnect) |
87 | retval = serio->drv->reconnect(serio); | |
c4e32e9f | 88 | mutex_unlock(&serio->drv_mutex); |
3c803e8e LT |
89 | |
90 | return retval; | |
91 | } | |
92 | ||
93 | static void serio_disconnect_driver(struct serio *serio) | |
94 | { | |
c4e32e9f | 95 | mutex_lock(&serio->drv_mutex); |
3c803e8e LT |
96 | if (serio->drv) |
97 | serio->drv->disconnect(serio); | |
c4e32e9f | 98 | mutex_unlock(&serio->drv_mutex); |
3c803e8e LT |
99 | } |
100 | ||
1da177e4 LT |
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 | ||
70f256fd | 118 | static int serio_bind_driver(struct serio *serio, struct serio_driver *drv) |
1da177e4 | 119 | { |
0a66045b DT |
120 | int error; |
121 | ||
1da177e4 | 122 | if (serio_match_port(drv->id_table, serio)) { |
70f256fd | 123 | |
1da177e4 | 124 | serio->dev.driver = &drv->driver; |
3c803e8e | 125 | if (serio_connect_driver(serio, drv)) { |
1da177e4 | 126 | serio->dev.driver = NULL; |
70f256fd | 127 | return -ENODEV; |
1da177e4 | 128 | } |
70f256fd | 129 | |
0a66045b DT |
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; | |
70f256fd | 139 | return error; |
0a66045b | 140 | } |
1da177e4 | 141 | } |
70f256fd | 142 | return 0; |
1da177e4 LT |
143 | } |
144 | ||
145 | static void serio_find_driver(struct serio *serio) | |
146 | { | |
73b59a3b RD |
147 | int error; |
148 | ||
73b59a3b RD |
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); | |
1da177e4 LT |
154 | } |
155 | ||
156 | ||
157 | /* | |
158 | * Serio event processing. | |
159 | */ | |
160 | ||
161 | enum serio_event_type { | |
ed7b1f6d DT |
162 | SERIO_RESCAN_PORT, |
163 | SERIO_RECONNECT_PORT, | |
1da177e4 | 164 | SERIO_REGISTER_PORT, |
ed7b1f6d | 165 | SERIO_ATTACH_DRIVER, |
1da177e4 LT |
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); | |
3c803e8e | 178 | static struct task_struct *serio_task; |
1da177e4 | 179 | |
ed7b1f6d DT |
180 | static int serio_queue_event(void *object, struct module *owner, |
181 | enum serio_event_type event_type) | |
1da177e4 LT |
182 | { |
183 | unsigned long flags; | |
184 | struct serio_event *event; | |
ed7b1f6d | 185 | int retval = 0; |
1da177e4 LT |
186 | |
187 | spin_lock_irqsave(&serio_event_lock, flags); | |
188 | ||
189 | /* | |
3c803e8e | 190 | * Scan event list for the other events for the same serio port, |
1da177e4 LT |
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. | |
3c803e8e | 195 | */ |
1da177e4 LT |
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 | ||
ed7b1f6d DT |
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; | |
211 | } | |
1da177e4 | 212 | |
ed7b1f6d DT |
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; | |
1da177e4 | 220 | } |
ed7b1f6d DT |
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 | ||
1da177e4 LT |
229 | out: |
230 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
ed7b1f6d | 231 | return retval; |
1da177e4 LT |
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 | ||
9e50afd0 | 290 | static void serio_handle_event(void) |
1da177e4 LT |
291 | { |
292 | struct serio_event *event; | |
1da177e4 | 293 | |
c4e32e9f | 294 | mutex_lock(&serio_mutex); |
1da177e4 | 295 | |
9e50afd0 DT |
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())) { | |
1da177e4 LT |
303 | |
304 | switch (event->type) { | |
305 | case SERIO_REGISTER_PORT: | |
306 | serio_add_port(event->object); | |
307 | break; | |
308 | ||
ed7b1f6d | 309 | case SERIO_RECONNECT_PORT: |
1da177e4 LT |
310 | serio_reconnect_port(event->object); |
311 | break; | |
312 | ||
ed7b1f6d | 313 | case SERIO_RESCAN_PORT: |
1da177e4 LT |
314 | serio_disconnect_port(event->object); |
315 | serio_find_driver(event->object); | |
316 | break; | |
317 | ||
ed7b1f6d DT |
318 | case SERIO_ATTACH_DRIVER: |
319 | serio_attach_driver(event->object); | |
1da177e4 LT |
320 | break; |
321 | ||
322 | default: | |
323 | break; | |
324 | } | |
325 | ||
326 | serio_remove_duplicate_events(event); | |
327 | serio_free_event(event); | |
328 | } | |
329 | ||
c4e32e9f | 330 | mutex_unlock(&serio_mutex); |
1da177e4 LT |
331 | } |
332 | ||
333 | /* | |
e8ef4347 DT |
334 | * Remove all events that have been submitted for a given |
335 | * object, be it serio port or driver. | |
1da177e4 | 336 | */ |
e8ef4347 | 337 | static void serio_remove_pending_events(void *object) |
1da177e4 LT |
338 | { |
339 | struct list_head *node, *next; | |
340 | struct serio_event *event; | |
341 | unsigned long flags; | |
342 | ||
343 | spin_lock_irqsave(&serio_event_lock, flags); | |
344 | ||
345 | list_for_each_safe(node, next, &serio_event_list) { | |
346 | event = list_entry(node, struct serio_event, node); | |
e8ef4347 | 347 | if (event->object == object) { |
1da177e4 LT |
348 | list_del_init(node); |
349 | serio_free_event(event); | |
350 | } | |
351 | } | |
352 | ||
353 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
354 | } | |
355 | ||
356 | /* | |
357 | * Destroy child serio port (if any) that has not been fully registered yet. | |
358 | * | |
359 | * Note that we rely on the fact that port can have only one child and therefore | |
360 | * only one child registration request can be pending. Additionally, children | |
361 | * are registered by driver's connect() handler so there can't be a grandchild | |
362 | * pending registration together with a child. | |
363 | */ | |
364 | static struct serio *serio_get_pending_child(struct serio *parent) | |
365 | { | |
366 | struct serio_event *event; | |
367 | struct serio *serio, *child = NULL; | |
368 | unsigned long flags; | |
369 | ||
370 | spin_lock_irqsave(&serio_event_lock, flags); | |
371 | ||
372 | list_for_each_entry(event, &serio_event_list, node) { | |
373 | if (event->type == SERIO_REGISTER_PORT) { | |
374 | serio = event->object; | |
375 | if (serio->parent == parent) { | |
376 | child = serio; | |
377 | break; | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | spin_unlock_irqrestore(&serio_event_lock, flags); | |
383 | return child; | |
384 | } | |
385 | ||
386 | static int serio_thread(void *nothing) | |
387 | { | |
83144186 | 388 | set_freezable(); |
1da177e4 | 389 | do { |
9e50afd0 | 390 | serio_handle_event(); |
e42837bc | 391 | wait_event_freezable(serio_wait, |
3c803e8e | 392 | kthread_should_stop() || !list_empty(&serio_event_list)); |
3c803e8e | 393 | } while (!kthread_should_stop()); |
1da177e4 LT |
394 | |
395 | printk(KERN_DEBUG "serio: kseriod exiting\n"); | |
3c803e8e | 396 | return 0; |
1da177e4 LT |
397 | } |
398 | ||
399 | ||
400 | /* | |
401 | * Serio port operations | |
402 | */ | |
403 | ||
e404e274 | 404 | static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
405 | { |
406 | struct serio *serio = to_serio_port(dev); | |
407 | return sprintf(buf, "%s\n", serio->name); | |
408 | } | |
409 | ||
ae87dff7 DT |
410 | static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) |
411 | { | |
412 | struct serio *serio = to_serio_port(dev); | |
413 | ||
414 | return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n", | |
415 | serio->id.type, serio->id.proto, serio->id.id, serio->id.extra); | |
416 | } | |
417 | ||
e404e274 | 418 | static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
419 | { |
420 | struct serio *serio = to_serio_port(dev); | |
421 | return sprintf(buf, "%02x\n", serio->id.type); | |
422 | } | |
423 | ||
e404e274 | 424 | static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
425 | { |
426 | struct serio *serio = to_serio_port(dev); | |
427 | return sprintf(buf, "%02x\n", serio->id.proto); | |
428 | } | |
429 | ||
e404e274 | 430 | static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
431 | { |
432 | struct serio *serio = to_serio_port(dev); | |
433 | return sprintf(buf, "%02x\n", serio->id.id); | |
434 | } | |
435 | ||
e404e274 | 436 | static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
437 | { |
438 | struct serio *serio = to_serio_port(dev); | |
439 | return sprintf(buf, "%02x\n", serio->id.extra); | |
440 | } | |
441 | ||
baae9561 DT |
442 | static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL); |
443 | static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL); | |
444 | static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL); | |
445 | static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL); | |
446 | ||
447 | static struct attribute *serio_device_id_attrs[] = { | |
448 | &dev_attr_type.attr, | |
449 | &dev_attr_proto.attr, | |
450 | &dev_attr_id.attr, | |
451 | &dev_attr_extra.attr, | |
452 | NULL | |
453 | }; | |
454 | ||
455 | static struct attribute_group serio_id_attr_group = { | |
456 | .name = "id", | |
457 | .attrs = serio_device_id_attrs, | |
458 | }; | |
459 | ||
e404e274 | 460 | static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
461 | { |
462 | struct serio *serio = to_serio_port(dev); | |
463 | struct device_driver *drv; | |
70f256fd | 464 | int error; |
1da177e4 | 465 | |
70f256fd DT |
466 | error = mutex_lock_interruptible(&serio_mutex); |
467 | if (error) | |
468 | return error; | |
1da177e4 | 469 | |
1da177e4 LT |
470 | if (!strncmp(buf, "none", count)) { |
471 | serio_disconnect_port(serio); | |
472 | } else if (!strncmp(buf, "reconnect", count)) { | |
473 | serio_reconnect_port(serio); | |
474 | } else if (!strncmp(buf, "rescan", count)) { | |
475 | serio_disconnect_port(serio); | |
476 | serio_find_driver(serio); | |
477 | } else if ((drv = driver_find(buf, &serio_bus)) != NULL) { | |
478 | serio_disconnect_port(serio); | |
70f256fd | 479 | error = serio_bind_driver(serio, to_serio_driver(drv)); |
1da177e4 LT |
480 | put_driver(drv); |
481 | } else { | |
70f256fd | 482 | error = -EINVAL; |
1da177e4 LT |
483 | } |
484 | ||
c4e32e9f | 485 | mutex_unlock(&serio_mutex); |
1da177e4 | 486 | |
70f256fd | 487 | return error ? error : count; |
1da177e4 LT |
488 | } |
489 | ||
e404e274 | 490 | static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
491 | { |
492 | struct serio *serio = to_serio_port(dev); | |
493 | return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto"); | |
494 | } | |
495 | ||
e404e274 | 496 | static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 LT |
497 | { |
498 | struct serio *serio = to_serio_port(dev); | |
499 | int retval; | |
500 | ||
501 | retval = count; | |
502 | if (!strncmp(buf, "manual", count)) { | |
503 | serio->manual_bind = 1; | |
504 | } else if (!strncmp(buf, "auto", count)) { | |
505 | serio->manual_bind = 0; | |
506 | } else { | |
507 | retval = -EINVAL; | |
508 | } | |
509 | ||
510 | return retval; | |
511 | } | |
512 | ||
513 | static struct device_attribute serio_device_attrs[] = { | |
514 | __ATTR(description, S_IRUGO, serio_show_description, NULL), | |
ae87dff7 | 515 | __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL), |
1da177e4 LT |
516 | __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver), |
517 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode), | |
518 | __ATTR_NULL | |
519 | }; | |
520 | ||
521 | ||
522 | static void serio_release_port(struct device *dev) | |
523 | { | |
524 | struct serio *serio = to_serio_port(dev); | |
525 | ||
526 | kfree(serio); | |
527 | module_put(THIS_MODULE); | |
528 | } | |
529 | ||
530 | /* | |
531 | * Prepare serio port for registration. | |
532 | */ | |
533 | static void serio_init_port(struct serio *serio) | |
534 | { | |
535 | static atomic_t serio_no = ATOMIC_INIT(0); | |
536 | ||
537 | __module_get(THIS_MODULE); | |
538 | ||
73b59a3b | 539 | INIT_LIST_HEAD(&serio->node); |
1da177e4 | 540 | spin_lock_init(&serio->lock); |
c4e32e9f | 541 | mutex_init(&serio->drv_mutex); |
1da177e4 LT |
542 | device_initialize(&serio->dev); |
543 | snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), | |
544 | "serio%ld", (long)atomic_inc_return(&serio_no) - 1); | |
545 | serio->dev.bus = &serio_bus; | |
546 | serio->dev.release = serio_release_port; | |
88aa0103 | 547 | if (serio->parent) { |
1da177e4 | 548 | serio->dev.parent = &serio->parent->dev; |
88aa0103 JK |
549 | serio->depth = serio->parent->depth + 1; |
550 | } else | |
551 | serio->depth = 0; | |
552 | lockdep_set_subclass(&serio->lock, serio->depth); | |
1da177e4 LT |
553 | } |
554 | ||
555 | /* | |
556 | * Complete serio port registration. | |
557 | * Driver core will attempt to find appropriate driver for the port. | |
558 | */ | |
559 | static void serio_add_port(struct serio *serio) | |
560 | { | |
73b59a3b RD |
561 | int error; |
562 | ||
1da177e4 LT |
563 | if (serio->parent) { |
564 | serio_pause_rx(serio->parent); | |
565 | serio->parent->child = serio; | |
566 | serio_continue_rx(serio->parent); | |
567 | } | |
568 | ||
569 | list_add_tail(&serio->node, &serio_list); | |
570 | if (serio->start) | |
571 | serio->start(serio); | |
73b59a3b RD |
572 | error = device_add(&serio->dev); |
573 | if (error) | |
574 | printk(KERN_ERR | |
575 | "serio: device_add() failed for %s (%s), error: %d\n", | |
576 | serio->phys, serio->name, error); | |
577 | else { | |
578 | serio->registered = 1; | |
579 | error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group); | |
580 | if (error) | |
581 | printk(KERN_ERR | |
582 | "serio: sysfs_create_group() failed for %s (%s), error: %d\n", | |
583 | serio->phys, serio->name, error); | |
584 | } | |
1da177e4 LT |
585 | } |
586 | ||
587 | /* | |
588 | * serio_destroy_port() completes deregistration process and removes | |
589 | * port from the system | |
590 | */ | |
591 | static void serio_destroy_port(struct serio *serio) | |
592 | { | |
593 | struct serio *child; | |
594 | ||
595 | child = serio_get_pending_child(serio); | |
596 | if (child) { | |
597 | serio_remove_pending_events(child); | |
598 | put_device(&child->dev); | |
599 | } | |
600 | ||
601 | if (serio->stop) | |
602 | serio->stop(serio); | |
603 | ||
604 | if (serio->parent) { | |
605 | serio_pause_rx(serio->parent); | |
606 | serio->parent->child = NULL; | |
607 | serio_continue_rx(serio->parent); | |
608 | serio->parent = NULL; | |
609 | } | |
610 | ||
611 | if (serio->registered) { | |
baae9561 | 612 | sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group); |
1da177e4 | 613 | device_del(&serio->dev); |
1da177e4 LT |
614 | serio->registered = 0; |
615 | } | |
616 | ||
73b59a3b | 617 | list_del_init(&serio->node); |
1da177e4 LT |
618 | serio_remove_pending_events(serio); |
619 | put_device(&serio->dev); | |
620 | } | |
621 | ||
622 | /* | |
623 | * Reconnect serio port and all its children (re-initialize attached devices) | |
624 | */ | |
625 | static void serio_reconnect_port(struct serio *serio) | |
626 | { | |
627 | do { | |
3c803e8e | 628 | if (serio_reconnect_driver(serio)) { |
1da177e4 LT |
629 | serio_disconnect_port(serio); |
630 | serio_find_driver(serio); | |
631 | /* Ok, old children are now gone, we are done */ | |
632 | break; | |
633 | } | |
634 | serio = serio->child; | |
635 | } while (serio); | |
636 | } | |
637 | ||
638 | /* | |
639 | * serio_disconnect_port() unbinds a port from its driver. As a side effect | |
640 | * all child ports are unbound and destroyed. | |
641 | */ | |
642 | static void serio_disconnect_port(struct serio *serio) | |
643 | { | |
644 | struct serio *s, *parent; | |
645 | ||
646 | if (serio->child) { | |
647 | /* | |
648 | * Children ports should be disconnected and destroyed | |
649 | * first, staring with the leaf one, since we don't want | |
650 | * to do recursion | |
651 | */ | |
652 | for (s = serio; s->child; s = s->child) | |
653 | /* empty */; | |
654 | ||
655 | do { | |
656 | parent = s->parent; | |
657 | ||
70f256fd | 658 | device_release_driver(&s->dev); |
1da177e4 LT |
659 | serio_destroy_port(s); |
660 | } while ((s = parent) != serio); | |
661 | } | |
662 | ||
663 | /* | |
664 | * Ok, no children left, now disconnect this port | |
665 | */ | |
70f256fd | 666 | device_release_driver(&serio->dev); |
1da177e4 LT |
667 | } |
668 | ||
669 | void serio_rescan(struct serio *serio) | |
670 | { | |
ed7b1f6d | 671 | serio_queue_event(serio, NULL, SERIO_RESCAN_PORT); |
1da177e4 LT |
672 | } |
673 | ||
674 | void serio_reconnect(struct serio *serio) | |
675 | { | |
ed7b1f6d | 676 | serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT); |
1da177e4 LT |
677 | } |
678 | ||
679 | /* | |
680 | * Submits register request to kseriod for subsequent execution. | |
681 | * Note that port registration is always asynchronous. | |
682 | */ | |
683 | void __serio_register_port(struct serio *serio, struct module *owner) | |
684 | { | |
685 | serio_init_port(serio); | |
686 | serio_queue_event(serio, owner, SERIO_REGISTER_PORT); | |
687 | } | |
688 | ||
689 | /* | |
690 | * Synchronously unregisters serio port. | |
691 | */ | |
692 | void serio_unregister_port(struct serio *serio) | |
693 | { | |
c4e32e9f | 694 | mutex_lock(&serio_mutex); |
1da177e4 LT |
695 | serio_disconnect_port(serio); |
696 | serio_destroy_port(serio); | |
c4e32e9f | 697 | mutex_unlock(&serio_mutex); |
1da177e4 LT |
698 | } |
699 | ||
3c803e8e LT |
700 | /* |
701 | * Safely unregisters child port if one is present. | |
702 | */ | |
703 | void serio_unregister_child_port(struct serio *serio) | |
704 | { | |
c4e32e9f | 705 | mutex_lock(&serio_mutex); |
3c803e8e LT |
706 | if (serio->child) { |
707 | serio_disconnect_port(serio->child); | |
708 | serio_destroy_port(serio->child); | |
709 | } | |
c4e32e9f | 710 | mutex_unlock(&serio_mutex); |
3c803e8e LT |
711 | } |
712 | ||
1da177e4 LT |
713 | |
714 | /* | |
715 | * Serio driver operations | |
716 | */ | |
717 | ||
718 | static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf) | |
719 | { | |
720 | struct serio_driver *driver = to_serio_driver(drv); | |
721 | return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)"); | |
722 | } | |
723 | ||
724 | static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf) | |
725 | { | |
726 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
727 | return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto"); | |
728 | } | |
729 | ||
730 | static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count) | |
731 | { | |
732 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
733 | int retval; | |
734 | ||
735 | retval = count; | |
736 | if (!strncmp(buf, "manual", count)) { | |
737 | serio_drv->manual_bind = 1; | |
738 | } else if (!strncmp(buf, "auto", count)) { | |
739 | serio_drv->manual_bind = 0; | |
740 | } else { | |
741 | retval = -EINVAL; | |
742 | } | |
743 | ||
744 | return retval; | |
745 | } | |
746 | ||
747 | ||
748 | static struct driver_attribute serio_driver_attrs[] = { | |
749 | __ATTR(description, S_IRUGO, serio_driver_show_description, NULL), | |
750 | __ATTR(bind_mode, S_IWUSR | S_IRUGO, | |
751 | serio_driver_show_bind_mode, serio_driver_set_bind_mode), | |
752 | __ATTR_NULL | |
753 | }; | |
754 | ||
755 | static int serio_driver_probe(struct device *dev) | |
756 | { | |
757 | struct serio *serio = to_serio_port(dev); | |
758 | struct serio_driver *drv = to_serio_driver(dev->driver); | |
759 | ||
3c803e8e | 760 | return serio_connect_driver(serio, drv); |
1da177e4 LT |
761 | } |
762 | ||
763 | static int serio_driver_remove(struct device *dev) | |
764 | { | |
765 | struct serio *serio = to_serio_port(dev); | |
1da177e4 | 766 | |
3c803e8e | 767 | serio_disconnect_driver(serio); |
1da177e4 LT |
768 | return 0; |
769 | } | |
770 | ||
82dd9eff DT |
771 | static void serio_cleanup(struct serio *serio) |
772 | { | |
33143ea1 | 773 | mutex_lock(&serio->drv_mutex); |
82dd9eff DT |
774 | if (serio->drv && serio->drv->cleanup) |
775 | serio->drv->cleanup(serio); | |
33143ea1 | 776 | mutex_unlock(&serio->drv_mutex); |
82dd9eff DT |
777 | } |
778 | ||
779 | static void serio_shutdown(struct device *dev) | |
780 | { | |
781 | struct serio *serio = to_serio_port(dev); | |
782 | ||
783 | serio_cleanup(serio); | |
784 | } | |
785 | ||
ed7b1f6d | 786 | static void serio_attach_driver(struct serio_driver *drv) |
73b59a3b RD |
787 | { |
788 | int error; | |
789 | ||
ed7b1f6d | 790 | error = driver_attach(&drv->driver); |
73b59a3b | 791 | if (error) |
ed7b1f6d DT |
792 | printk(KERN_WARNING |
793 | "serio: driver_attach() failed for %s with error %d\n", | |
73b59a3b RD |
794 | drv->driver.name, error); |
795 | } | |
796 | ||
4b315627 | 797 | int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name) |
1da177e4 | 798 | { |
ed7b1f6d DT |
799 | int manual_bind = drv->manual_bind; |
800 | int error; | |
801 | ||
1da177e4 | 802 | drv->driver.bus = &serio_bus; |
4b315627 GKH |
803 | drv->driver.owner = owner; |
804 | drv->driver.mod_name = mod_name; | |
1da177e4 | 805 | |
ed7b1f6d DT |
806 | /* |
807 | * Temporarily disable automatic binding because probing | |
808 | * takes long time and we are better off doing it in kseriod | |
809 | */ | |
810 | drv->manual_bind = 1; | |
811 | ||
812 | error = driver_register(&drv->driver); | |
813 | if (error) { | |
814 | printk(KERN_ERR | |
815 | "serio: driver_register() failed for %s, error: %d\n", | |
816 | drv->driver.name, error); | |
817 | return error; | |
818 | } | |
819 | ||
820 | /* | |
821 | * Restore original bind mode and let kseriod bind the | |
822 | * driver to free ports | |
823 | */ | |
824 | if (!manual_bind) { | |
825 | drv->manual_bind = 0; | |
826 | error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER); | |
827 | if (error) { | |
828 | driver_unregister(&drv->driver); | |
829 | return error; | |
830 | } | |
831 | } | |
832 | ||
833 | return 0; | |
1da177e4 LT |
834 | } |
835 | ||
836 | void serio_unregister_driver(struct serio_driver *drv) | |
837 | { | |
838 | struct serio *serio; | |
839 | ||
c4e32e9f | 840 | mutex_lock(&serio_mutex); |
e8ef4347 | 841 | |
1da177e4 | 842 | drv->manual_bind = 1; /* so serio_find_driver ignores it */ |
e8ef4347 | 843 | serio_remove_pending_events(drv); |
1da177e4 LT |
844 | |
845 | start_over: | |
846 | list_for_each_entry(serio, &serio_list, node) { | |
847 | if (serio->drv == drv) { | |
848 | serio_disconnect_port(serio); | |
849 | serio_find_driver(serio); | |
850 | /* we could've deleted some ports, restart */ | |
851 | goto start_over; | |
852 | } | |
853 | } | |
854 | ||
855 | driver_unregister(&drv->driver); | |
c4e32e9f | 856 | mutex_unlock(&serio_mutex); |
1da177e4 LT |
857 | } |
858 | ||
859 | static void serio_set_drv(struct serio *serio, struct serio_driver *drv) | |
860 | { | |
1da177e4 LT |
861 | serio_pause_rx(serio); |
862 | serio->drv = drv; | |
863 | serio_continue_rx(serio); | |
1da177e4 LT |
864 | } |
865 | ||
866 | static int serio_bus_match(struct device *dev, struct device_driver *drv) | |
867 | { | |
868 | struct serio *serio = to_serio_port(dev); | |
869 | struct serio_driver *serio_drv = to_serio_driver(drv); | |
870 | ||
871 | if (serio->manual_bind || serio_drv->manual_bind) | |
872 | return 0; | |
873 | ||
874 | return serio_match_port(serio_drv->id_table, serio); | |
875 | } | |
876 | ||
877 | #ifdef CONFIG_HOTPLUG | |
878 | ||
312c004d | 879 | #define SERIO_ADD_UEVENT_VAR(fmt, val...) \ |
ae87dff7 | 880 | do { \ |
7eff2e7a | 881 | int err = add_uevent_var(env, fmt, val); \ |
ae87dff7 DT |
882 | if (err) \ |
883 | return err; \ | |
884 | } while (0) | |
885 | ||
7eff2e7a | 886 | static int serio_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 LT |
887 | { |
888 | struct serio *serio; | |
1da177e4 LT |
889 | |
890 | if (!dev) | |
891 | return -ENODEV; | |
892 | ||
893 | serio = to_serio_port(dev); | |
894 | ||
312c004d KS |
895 | SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type); |
896 | SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto); | |
897 | SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id); | |
898 | SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra); | |
899 | SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X", | |
ae87dff7 | 900 | serio->id.type, serio->id.proto, serio->id.id, serio->id.extra); |
1da177e4 LT |
901 | |
902 | return 0; | |
903 | } | |
312c004d | 904 | #undef SERIO_ADD_UEVENT_VAR |
1da177e4 LT |
905 | |
906 | #else | |
907 | ||
7eff2e7a | 908 | static int serio_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 LT |
909 | { |
910 | return -ENODEV; | |
911 | } | |
912 | ||
913 | #endif /* CONFIG_HOTPLUG */ | |
914 | ||
82dd9eff DT |
915 | #ifdef CONFIG_PM |
916 | static int serio_suspend(struct device *dev, pm_message_t state) | |
917 | { | |
918 | if (dev->power.power_state.event != state.event) { | |
919 | if (state.event == PM_EVENT_SUSPEND) | |
920 | serio_cleanup(to_serio_port(dev)); | |
921 | ||
922 | dev->power.power_state = state; | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
1da177e4 LT |
928 | static int serio_resume(struct device *dev) |
929 | { | |
930 | struct serio *serio = to_serio_port(dev); | |
931 | ||
82dd9eff DT |
932 | if (dev->power.power_state.event != PM_EVENT_ON && |
933 | serio_reconnect_driver(serio)) { | |
1da177e4 LT |
934 | /* |
935 | * Driver re-probing can take a while, so better let kseriod | |
936 | * deal with it. | |
937 | */ | |
938 | serio_rescan(serio); | |
939 | } | |
940 | ||
82dd9eff DT |
941 | dev->power.power_state = PMSG_ON; |
942 | ||
1da177e4 LT |
943 | return 0; |
944 | } | |
82dd9eff | 945 | #endif /* CONFIG_PM */ |
1da177e4 | 946 | |
c4e32e9f | 947 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
1da177e4 LT |
948 | int serio_open(struct serio *serio, struct serio_driver *drv) |
949 | { | |
950 | serio_set_drv(serio, drv); | |
951 | ||
952 | if (serio->open && serio->open(serio)) { | |
953 | serio_set_drv(serio, NULL); | |
954 | return -1; | |
955 | } | |
956 | return 0; | |
957 | } | |
958 | ||
c4e32e9f | 959 | /* called from serio_driver->connect/disconnect methods under serio_mutex */ |
1da177e4 LT |
960 | void serio_close(struct serio *serio) |
961 | { | |
962 | if (serio->close) | |
963 | serio->close(serio); | |
964 | ||
965 | serio_set_drv(serio, NULL); | |
966 | } | |
967 | ||
968 | irqreturn_t serio_interrupt(struct serio *serio, | |
7d12e780 | 969 | unsigned char data, unsigned int dfl) |
1da177e4 LT |
970 | { |
971 | unsigned long flags; | |
972 | irqreturn_t ret = IRQ_NONE; | |
973 | ||
974 | spin_lock_irqsave(&serio->lock, flags); | |
975 | ||
976 | if (likely(serio->drv)) { | |
7d12e780 | 977 | ret = serio->drv->interrupt(serio, data, dfl); |
1da177e4 LT |
978 | } else if (!dfl && serio->registered) { |
979 | serio_rescan(serio); | |
980 | ret = IRQ_HANDLED; | |
981 | } | |
982 | ||
983 | spin_unlock_irqrestore(&serio->lock, flags); | |
984 | ||
985 | return ret; | |
986 | } | |
987 | ||
1ea2a69d MN |
988 | static struct bus_type serio_bus = { |
989 | .name = "serio", | |
990 | .dev_attrs = serio_device_attrs, | |
991 | .drv_attrs = serio_driver_attrs, | |
992 | .match = serio_bus_match, | |
993 | .uevent = serio_uevent, | |
994 | .probe = serio_driver_probe, | |
995 | .remove = serio_driver_remove, | |
82dd9eff DT |
996 | .shutdown = serio_shutdown, |
997 | #ifdef CONFIG_PM | |
998 | .suspend = serio_suspend, | |
1ea2a69d | 999 | .resume = serio_resume, |
82dd9eff | 1000 | #endif |
1ea2a69d MN |
1001 | }; |
1002 | ||
1da177e4 LT |
1003 | static int __init serio_init(void) |
1004 | { | |
73b59a3b | 1005 | int error; |
1da177e4 | 1006 | |
73b59a3b RD |
1007 | error = bus_register(&serio_bus); |
1008 | if (error) { | |
1009 | printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error); | |
1010 | return error; | |
1011 | } | |
1012 | ||
1013 | serio_task = kthread_run(serio_thread, NULL, "kseriod"); | |
1014 | if (IS_ERR(serio_task)) { | |
1015 | bus_unregister(&serio_bus); | |
1016 | error = PTR_ERR(serio_task); | |
1017 | printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error); | |
1018 | return error; | |
1019 | } | |
1da177e4 LT |
1020 | |
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static void __exit serio_exit(void) | |
1025 | { | |
1026 | bus_unregister(&serio_bus); | |
3c803e8e | 1027 | kthread_stop(serio_task); |
1da177e4 LT |
1028 | } |
1029 | ||
51c38f9b | 1030 | subsys_initcall(serio_init); |
1da177e4 | 1031 | module_exit(serio_exit); |