blob: 5f1d4032fd57fbddc4faf65635b416c9a8daf74f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The Serio abstraction module
3 *
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 * Copyright (c) 2003 Daniele Bellucci
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27 */
28
29#include <linux/stddef.h>
30#include <linux/module.h>
31#include <linux/serio.h>
32#include <linux/errno.h>
33#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070036#include <linux/kthread.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050037#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION("Serio abstraction core");
42MODULE_LICENSE("GPL");
43
44EXPORT_SYMBOL(serio_interrupt);
45EXPORT_SYMBOL(__serio_register_port);
46EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -070047EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048EXPORT_SYMBOL(__serio_unregister_port_delayed);
49EXPORT_SYMBOL(__serio_register_driver);
50EXPORT_SYMBOL(serio_unregister_driver);
51EXPORT_SYMBOL(serio_open);
52EXPORT_SYMBOL(serio_close);
53EXPORT_SYMBOL(serio_rescan);
54EXPORT_SYMBOL(serio_reconnect);
55
56/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050057 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * serio port or driver registrered or unregistered.
59 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050060static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static LIST_HEAD(serio_list);
63
Russell King30226f82006-01-05 14:38:53 +000064static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Randy Dunlap73b59a32006-07-19 01:14:55 -040066static void serio_add_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void serio_add_port(struct serio *serio);
68static void serio_destroy_port(struct serio *serio);
69static void serio_reconnect_port(struct serio *serio);
70static void serio_disconnect_port(struct serio *serio);
71
Linus Torvalds3c803e82005-06-27 17:49:45 -070072static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
73{
74 int retval;
75
Arjan van de Venc4e32e92006-02-19 00:21:55 -050076 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070077 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050078 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070079
80 return retval;
81}
82
83static int serio_reconnect_driver(struct serio *serio)
84{
85 int retval = -1;
86
Arjan van de Venc4e32e92006-02-19 00:21:55 -050087 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070088 if (serio->drv && serio->drv->reconnect)
89 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050090 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070091
92 return retval;
93}
94
95static void serio_disconnect_driver(struct serio *serio)
96{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050097 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070098 if (serio->drv)
99 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500100 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
104{
105 while (ids->type || ids->proto) {
106 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
107 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
108 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
109 (ids->id == SERIO_ANY || ids->id == serio->id.id))
110 return 1;
111 ids++;
112 }
113 return 0;
114}
115
116/*
117 * Basic serio -> driver core mappings
118 */
119
120static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
121{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400122 int error;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 down_write(&serio_bus.subsys.rwsem);
125
126 if (serio_match_port(drv->id_table, serio)) {
127 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700128 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 serio->dev.driver = NULL;
130 goto out;
131 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400132 error = device_bind_driver(&serio->dev);
133 if (error) {
134 printk(KERN_WARNING
135 "serio: device_bind_driver() failed "
136 "for %s (%s) and %s, error: %d\n",
137 serio->phys, serio->name,
138 drv->description, error);
139 serio_disconnect_driver(serio);
140 serio->dev.driver = NULL;
141 goto out;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400144 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 up_write(&serio_bus.subsys.rwsem);
146}
147
148static void serio_release_driver(struct serio *serio)
149{
150 down_write(&serio_bus.subsys.rwsem);
151 device_release_driver(&serio->dev);
152 up_write(&serio_bus.subsys.rwsem);
153}
154
155static void serio_find_driver(struct serio *serio)
156{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400157 int error;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 down_write(&serio_bus.subsys.rwsem);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400160 error = device_attach(&serio->dev);
161 if (error < 0)
162 printk(KERN_WARNING
163 "serio: device_attach() failed for %s (%s), error: %d\n",
164 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 up_write(&serio_bus.subsys.rwsem);
166}
167
168
169/*
170 * Serio event processing.
171 */
172
173enum serio_event_type {
174 SERIO_RESCAN,
175 SERIO_RECONNECT,
176 SERIO_REGISTER_PORT,
177 SERIO_UNREGISTER_PORT,
178 SERIO_REGISTER_DRIVER,
179};
180
181struct serio_event {
182 enum serio_event_type type;
183 void *object;
184 struct module *owner;
185 struct list_head node;
186};
187
188static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
189static LIST_HEAD(serio_event_list);
190static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700191static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193static void serio_queue_event(void *object, struct module *owner,
194 enum serio_event_type event_type)
195{
196 unsigned long flags;
197 struct serio_event *event;
198
199 spin_lock_irqsave(&serio_event_lock, flags);
200
201 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700202 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * starting with the most recent one. If event is the same we
204 * do not need add new one. If event is of different type we
205 * need to add this event and should not look further because
206 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 list_for_each_entry_reverse(event, &serio_event_list, node) {
209 if (event->object == object) {
210 if (event->type == event_type)
211 goto out;
212 break;
213 }
214 }
215
216 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
217 if (!try_module_get(owner)) {
218 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk9d921112006-03-14 00:13:29 -0500219 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto out;
221 }
222
223 event->type = event_type;
224 event->object = object;
225 event->owner = owner;
226
227 list_add_tail(&event->node, &serio_event_list);
228 wake_up(&serio_wait);
229 } else {
230 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
231 }
232out:
233 spin_unlock_irqrestore(&serio_event_lock, flags);
234}
235
236static void serio_free_event(struct serio_event *event)
237{
238 module_put(event->owner);
239 kfree(event);
240}
241
242static 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
270static 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 Torokhov9e50afd2005-11-20 00:56:43 -0500292static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500296 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500298 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700305
306 switch (event->type) {
307 case SERIO_REGISTER_PORT:
308 serio_add_port(event->object);
309 break;
310
311 case SERIO_UNREGISTER_PORT:
312 serio_disconnect_port(event->object);
313 serio_destroy_port(event->object);
314 break;
315
316 case SERIO_RECONNECT:
317 serio_reconnect_port(event->object);
318 break;
319
320 case SERIO_RESCAN:
321 serio_disconnect_port(event->object);
322 serio_find_driver(event->object);
323 break;
324
325 case SERIO_REGISTER_DRIVER:
Randy Dunlap73b59a32006-07-19 01:14:55 -0400326 serio_add_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328
329 default:
330 break;
331 }
332
333 serio_remove_duplicate_events(event);
334 serio_free_event(event);
335 }
336
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500337 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * Remove all events that have been submitted for a given serio port.
342 */
343static void serio_remove_pending_events(struct serio *serio)
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);
353 if (event->object == serio) {
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 */
370static 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
392static int serio_thread(void *nothing)
393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500395 serio_handle_event();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700396 wait_event_interruptible(serio_wait,
397 kthread_should_stop() || !list_empty(&serio_event_list));
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700398 try_to_freeze();
Linus Torvalds3c803e82005-06-27 17:49:45 -0700399 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 printk(KERN_DEBUG "serio: kseriod exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405
406/*
407 * Serio port operations
408 */
409
Yani Ioannoue404e272005-05-17 06:42:58 -0400410static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct serio *serio = to_serio_port(dev);
413 return sprintf(buf, "%s\n", serio->name);
414}
415
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500416static 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 Ioannoue404e272005-05-17 06:42:58 -0400424static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct serio *serio = to_serio_port(dev);
427 return sprintf(buf, "%02x\n", serio->id.type);
428}
429
Yani Ioannoue404e272005-05-17 06:42:58 -0400430static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct serio *serio = to_serio_port(dev);
433 return sprintf(buf, "%02x\n", serio->id.proto);
434}
435
Yani Ioannoue404e272005-05-17 06:42:58 -0400436static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct serio *serio = to_serio_port(dev);
439 return sprintf(buf, "%02x\n", serio->id.id);
440}
441
Yani Ioannoue404e272005-05-17 06:42:58 -0400442static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct serio *serio = to_serio_port(dev);
445 return sprintf(buf, "%02x\n", serio->id.extra);
446}
447
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700448static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
449static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
450static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
451static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
452
453static 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
461static struct attribute_group serio_id_attr_group = {
462 .name = "id",
463 .attrs = serio_device_id_attrs,
464};
465
Yani Ioannoue404e272005-05-17 06:42:58 -0400466static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct serio *serio = to_serio_port(dev);
469 struct device_driver *drv;
470 int retval;
471
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500472 retval = mutex_lock_interruptible(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (retval)
474 return retval;
475
476 retval = count;
477 if (!strncmp(buf, "none", count)) {
478 serio_disconnect_port(serio);
479 } else if (!strncmp(buf, "reconnect", count)) {
480 serio_reconnect_port(serio);
481 } else if (!strncmp(buf, "rescan", count)) {
482 serio_disconnect_port(serio);
483 serio_find_driver(serio);
484 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
485 serio_disconnect_port(serio);
486 serio_bind_driver(serio, to_serio_driver(drv));
487 put_driver(drv);
488 } else {
489 retval = -EINVAL;
490 }
491
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500492 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 return retval;
495}
496
Yani Ioannoue404e272005-05-17 06:42:58 -0400497static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct serio *serio = to_serio_port(dev);
500 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
501}
502
Yani Ioannoue404e272005-05-17 06:42:58 -0400503static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct serio *serio = to_serio_port(dev);
506 int retval;
507
508 retval = count;
509 if (!strncmp(buf, "manual", count)) {
510 serio->manual_bind = 1;
511 } else if (!strncmp(buf, "auto", count)) {
512 serio->manual_bind = 0;
513 } else {
514 retval = -EINVAL;
515 }
516
517 return retval;
518}
519
520static struct device_attribute serio_device_attrs[] = {
521 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500522 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
524 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
525 __ATTR_NULL
526};
527
528
529static void serio_release_port(struct device *dev)
530{
531 struct serio *serio = to_serio_port(dev);
532
533 kfree(serio);
534 module_put(THIS_MODULE);
535}
536
537/*
538 * Prepare serio port for registration.
539 */
540static void serio_init_port(struct serio *serio)
541{
542 static atomic_t serio_no = ATOMIC_INIT(0);
543
544 __module_get(THIS_MODULE);
545
Randy Dunlap73b59a32006-07-19 01:14:55 -0400546 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500548 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 device_initialize(&serio->dev);
550 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
551 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
552 serio->dev.bus = &serio_bus;
553 serio->dev.release = serio_release_port;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400554 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400556 serio->depth = serio->parent->depth + 1;
557 } else
558 serio->depth = 0;
559 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Complete serio port registration.
564 * Driver core will attempt to find appropriate driver for the port.
565 */
566static void serio_add_port(struct serio *serio)
567{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400568 int error;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (serio->parent) {
571 serio_pause_rx(serio->parent);
572 serio->parent->child = serio;
573 serio_continue_rx(serio->parent);
574 }
575
576 list_add_tail(&serio->node, &serio_list);
577 if (serio->start)
578 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400579 error = device_add(&serio->dev);
580 if (error)
581 printk(KERN_ERR
582 "serio: device_add() failed for %s (%s), error: %d\n",
583 serio->phys, serio->name, error);
584 else {
585 serio->registered = 1;
586 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
587 if (error)
588 printk(KERN_ERR
589 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
590 serio->phys, serio->name, error);
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * serio_destroy_port() completes deregistration process and removes
596 * port from the system
597 */
598static void serio_destroy_port(struct serio *serio)
599{
600 struct serio *child;
601
602 child = serio_get_pending_child(serio);
603 if (child) {
604 serio_remove_pending_events(child);
605 put_device(&child->dev);
606 }
607
608 if (serio->stop)
609 serio->stop(serio);
610
611 if (serio->parent) {
612 serio_pause_rx(serio->parent);
613 serio->parent->child = NULL;
614 serio_continue_rx(serio->parent);
615 serio->parent = NULL;
616 }
617
618 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700619 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 serio->registered = 0;
622 }
623
Randy Dunlap73b59a32006-07-19 01:14:55 -0400624 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 serio_remove_pending_events(serio);
626 put_device(&serio->dev);
627}
628
629/*
630 * Reconnect serio port and all its children (re-initialize attached devices)
631 */
632static void serio_reconnect_port(struct serio *serio)
633{
634 do {
Linus Torvalds3c803e82005-06-27 17:49:45 -0700635 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 serio_disconnect_port(serio);
637 serio_find_driver(serio);
638 /* Ok, old children are now gone, we are done */
639 break;
640 }
641 serio = serio->child;
642 } while (serio);
643}
644
645/*
646 * serio_disconnect_port() unbinds a port from its driver. As a side effect
647 * all child ports are unbound and destroyed.
648 */
649static void serio_disconnect_port(struct serio *serio)
650{
651 struct serio *s, *parent;
652
653 if (serio->child) {
654 /*
655 * Children ports should be disconnected and destroyed
656 * first, staring with the leaf one, since we don't want
657 * to do recursion
658 */
659 for (s = serio; s->child; s = s->child)
660 /* empty */;
661
662 do {
663 parent = s->parent;
664
665 serio_release_driver(s);
666 serio_destroy_port(s);
667 } while ((s = parent) != serio);
668 }
669
670 /*
671 * Ok, no children left, now disconnect this port
672 */
673 serio_release_driver(serio);
674}
675
676void serio_rescan(struct serio *serio)
677{
678 serio_queue_event(serio, NULL, SERIO_RESCAN);
679}
680
681void serio_reconnect(struct serio *serio)
682{
683 serio_queue_event(serio, NULL, SERIO_RECONNECT);
684}
685
686/*
687 * Submits register request to kseriod for subsequent execution.
688 * Note that port registration is always asynchronous.
689 */
690void __serio_register_port(struct serio *serio, struct module *owner)
691{
692 serio_init_port(serio);
693 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
694}
695
696/*
697 * Synchronously unregisters serio port.
698 */
699void serio_unregister_port(struct serio *serio)
700{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500701 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 serio_disconnect_port(serio);
703 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500704 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700708 * Safely unregisters child port if one is present.
709 */
710void serio_unregister_child_port(struct serio *serio)
711{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500712 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700713 if (serio->child) {
714 serio_disconnect_port(serio->child);
715 serio_destroy_port(serio->child);
716 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500717 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700718}
719
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Submits register request to kseriod for subsequent execution.
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500722 * Can be used when it is not obvious whether the serio_mutex is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * taken or not and when delayed execution is feasible.
724 */
725void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
726{
727 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
728}
729
730
731/*
732 * Serio driver operations
733 */
734
735static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
736{
737 struct serio_driver *driver = to_serio_driver(drv);
738 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
739}
740
741static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
742{
743 struct serio_driver *serio_drv = to_serio_driver(drv);
744 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
745}
746
747static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
748{
749 struct serio_driver *serio_drv = to_serio_driver(drv);
750 int retval;
751
752 retval = count;
753 if (!strncmp(buf, "manual", count)) {
754 serio_drv->manual_bind = 1;
755 } else if (!strncmp(buf, "auto", count)) {
756 serio_drv->manual_bind = 0;
757 } else {
758 retval = -EINVAL;
759 }
760
761 return retval;
762}
763
764
765static struct driver_attribute serio_driver_attrs[] = {
766 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
767 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
768 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
769 __ATTR_NULL
770};
771
772static int serio_driver_probe(struct device *dev)
773{
774 struct serio *serio = to_serio_port(dev);
775 struct serio_driver *drv = to_serio_driver(dev->driver);
776
Linus Torvalds3c803e82005-06-27 17:49:45 -0700777 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780static int serio_driver_remove(struct device *dev)
781{
782 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds3c803e82005-06-27 17:49:45 -0700784 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Russell King30226f82006-01-05 14:38:53 +0000788static struct bus_type serio_bus = {
789 .name = "serio",
790 .probe = serio_driver_probe,
791 .remove = serio_driver_remove,
792};
793
Randy Dunlap73b59a32006-07-19 01:14:55 -0400794static void serio_add_driver(struct serio_driver *drv)
795{
796 int error;
797
798 error = driver_register(&drv->driver);
799 if (error)
800 printk(KERN_ERR
801 "serio: driver_register() failed for %s, error: %d\n",
802 drv->driver.name, error);
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805void __serio_register_driver(struct serio_driver *drv, struct module *owner)
806{
807 drv->driver.bus = &serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
810}
811
812void serio_unregister_driver(struct serio_driver *drv)
813{
814 struct serio *serio;
815
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500816 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 drv->manual_bind = 1; /* so serio_find_driver ignores it */
818
819start_over:
820 list_for_each_entry(serio, &serio_list, node) {
821 if (serio->drv == drv) {
822 serio_disconnect_port(serio);
823 serio_find_driver(serio);
824 /* we could've deleted some ports, restart */
825 goto start_over;
826 }
827 }
828
829 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500830 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 serio_pause_rx(serio);
836 serio->drv = drv;
837 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840static int serio_bus_match(struct device *dev, struct device_driver *drv)
841{
842 struct serio *serio = to_serio_port(dev);
843 struct serio_driver *serio_drv = to_serio_driver(drv);
844
845 if (serio->manual_bind || serio_drv->manual_bind)
846 return 0;
847
848 return serio_match_port(serio_drv->id_table, serio);
849}
850
851#ifdef CONFIG_HOTPLUG
852
Kay Sievers312c0042005-11-16 09:00:00 +0100853#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500854 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100855 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500856 buffer, buffer_size, &len, \
857 fmt, val); \
858 if (err) \
859 return err; \
860 } while (0)
861
Kay Sievers312c0042005-11-16 09:00:00 +0100862static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct serio *serio;
865 int i = 0;
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500866 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (!dev)
869 return -ENODEV;
870
871 serio = to_serio_port(dev);
872
Kay Sievers312c0042005-11-16 09:00:00 +0100873 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
874 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
875 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
876 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
877 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500878 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 envp[i] = NULL;
880
881 return 0;
882}
Kay Sievers312c0042005-11-16 09:00:00 +0100883#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885#else
886
Kay Sievers312c0042005-11-16 09:00:00 +0100887static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 return -ENODEV;
890}
891
892#endif /* CONFIG_HOTPLUG */
893
894static int serio_resume(struct device *dev)
895{
896 struct serio *serio = to_serio_port(dev);
897
Linus Torvalds3c803e82005-06-27 17:49:45 -0700898 if (serio_reconnect_driver(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Driver re-probing can take a while, so better let kseriod
901 * deal with it.
902 */
903 serio_rescan(serio);
904 }
905
906 return 0;
907}
908
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500909/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910int serio_open(struct serio *serio, struct serio_driver *drv)
911{
912 serio_set_drv(serio, drv);
913
914 if (serio->open && serio->open(serio)) {
915 serio_set_drv(serio, NULL);
916 return -1;
917 }
918 return 0;
919}
920
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500921/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922void serio_close(struct serio *serio)
923{
924 if (serio->close)
925 serio->close(serio);
926
927 serio_set_drv(serio, NULL);
928}
929
930irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100931 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 unsigned long flags;
934 irqreturn_t ret = IRQ_NONE;
935
936 spin_lock_irqsave(&serio->lock, flags);
937
938 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100939 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } else if (!dfl && serio->registered) {
941 serio_rescan(serio);
942 ret = IRQ_HANDLED;
943 }
944
945 spin_unlock_irqrestore(&serio->lock, flags);
946
947 return ret;
948}
949
950static int __init serio_init(void)
951{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400952 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 serio_bus.dev_attrs = serio_device_attrs;
955 serio_bus.drv_attrs = serio_driver_attrs;
956 serio_bus.match = serio_bus_match;
Kay Sievers312c0042005-11-16 09:00:00 +0100957 serio_bus.uevent = serio_uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 serio_bus.resume = serio_resume;
Randy Dunlap73b59a32006-07-19 01:14:55 -0400959 error = bus_register(&serio_bus);
960 if (error) {
961 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
962 return error;
963 }
964
965 serio_task = kthread_run(serio_thread, NULL, "kseriod");
966 if (IS_ERR(serio_task)) {
967 bus_unregister(&serio_bus);
968 error = PTR_ERR(serio_task);
969 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
970 return error;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 return 0;
974}
975
976static void __exit serio_exit(void)
977{
978 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700979 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500982subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983module_exit(serio_exit);