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