blob: 2f12d60eee3b4e61f4b93363f5458189d471b0c1 [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);
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -080048EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049EXPORT_SYMBOL(serio_unregister_driver);
50EXPORT_SYMBOL(serio_open);
51EXPORT_SYMBOL(serio_close);
52EXPORT_SYMBOL(serio_rescan);
53EXPORT_SYMBOL(serio_reconnect);
54
55/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050056 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * serio port or driver registrered or unregistered.
58 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050059static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static LIST_HEAD(serio_list);
62
Russell King30226f82006-01-05 14:38:53 +000063static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static void serio_add_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040066static int serio_reconnect_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void serio_disconnect_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040068static void serio_reconnect_chain(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050069static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds3c803e82005-06-27 17:49:45 -070071static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
72{
73 int retval;
74
Arjan van de Venc4e32e92006-02-19 00:21:55 -050075 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070076 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050077 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070078
79 return retval;
80}
81
82static int serio_reconnect_driver(struct serio *serio)
83{
84 int retval = -1;
85
Arjan van de Venc4e32e92006-02-19 00:21:55 -050086 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070087 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050089 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070090
91 return retval;
92}
93
94static void serio_disconnect_driver(struct serio *serio)
95{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050096 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070097 if (serio->drv)
98 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050099 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
103{
104 while (ids->type || ids->proto) {
105 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
106 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
107 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
108 (ids->id == SERIO_ANY || ids->id == serio->id.id))
109 return 1;
110 ids++;
111 }
112 return 0;
113}
114
115/*
116 * Basic serio -> driver core mappings
117 */
118
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400119static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400121 int error;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (serio_match_port(drv->id_table, serio)) {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700126 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400128 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400130
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400131 error = device_bind_driver(&serio->dev);
132 if (error) {
133 printk(KERN_WARNING
134 "serio: device_bind_driver() failed "
135 "for %s (%s) and %s, error: %d\n",
136 serio->phys, serio->name,
137 drv->description, error);
138 serio_disconnect_driver(serio);
139 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400140 return error;
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static void serio_find_driver(struct serio *serio)
147{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400148 int error;
149
Randy Dunlap73b59a32006-07-19 01:14:55 -0400150 error = device_attach(&serio->dev);
151 if (error < 0)
152 printk(KERN_WARNING
153 "serio: device_attach() failed for %s (%s), error: %d\n",
154 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157
158/*
159 * Serio event processing.
160 */
161
162enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500163 SERIO_RESCAN_PORT,
164 SERIO_RECONNECT_PORT,
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400165 SERIO_RECONNECT_CHAIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500167 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
170struct serio_event {
171 enum serio_event_type type;
172 void *object;
173 struct module *owner;
174 struct list_head node;
175};
176
177static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
178static LIST_HEAD(serio_event_list);
179static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700180static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500182static int serio_queue_event(void *object, struct module *owner,
183 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 unsigned long flags;
186 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500187 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 spin_lock_irqsave(&serio_event_lock, flags);
190
191 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700192 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * starting with the most recent one. If event is the same we
194 * do not need add new one. If event is of different type we
195 * need to add this event and should not look further because
196 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 list_for_each_entry_reverse(event, &serio_event_list, node) {
199 if (event->object == object) {
200 if (event->type == event_type)
201 goto out;
202 break;
203 }
204 }
205
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500206 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
207 if (!event) {
208 printk(KERN_ERR
209 "serio: Not enough memory to queue event %d\n",
210 event_type);
211 retval = -ENOMEM;
212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500214
215 if (!try_module_get(owner)) {
216 printk(KERN_WARNING
217 "serio: Can't get module reference, dropping event %d\n",
218 event_type);
219 kfree(event);
220 retval = -EINVAL;
221 goto out;
222 }
223
224 event->type = event_type;
225 event->object = object;
226 event->owner = owner;
227
228 list_add_tail(&event->node, &serio_event_list);
229 wake_up(&serio_wait);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231out:
232 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500233 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
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
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500311 case SERIO_RECONNECT_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 serio_reconnect_port(event->object);
313 break;
314
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500315 case SERIO_RESCAN_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 serio_disconnect_port(event->object);
317 serio_find_driver(event->object);
318 break;
319
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400320 case SERIO_RECONNECT_CHAIN:
321 serio_reconnect_chain(event->object);
322 break;
323
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500324 case SERIO_ATTACH_DRIVER:
325 serio_attach_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327
328 default:
329 break;
330 }
331
332 serio_remove_duplicate_events(event);
333 serio_free_event(event);
334 }
335
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500336 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400340 * Remove all events that have been submitted for a given
341 * object, be it serio port or driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400343static void serio_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct list_head *node, *next;
346 struct serio_event *event;
347 unsigned long flags;
348
349 spin_lock_irqsave(&serio_event_lock, flags);
350
351 list_for_each_safe(node, next, &serio_event_list) {
352 event = list_entry(node, struct serio_event, node);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400353 if (event->object == object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 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{
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700394 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500396 serio_handle_event();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700397 wait_event_freezable(serio_wait,
Linus Torvalds3c803e82005-06-27 17:49:45 -0700398 kthread_should_stop() || !list_empty(&serio_event_list));
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;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400470 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400472 error = mutex_lock_interruptible(&serio_mutex);
473 if (error)
474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!strncmp(buf, "none", count)) {
477 serio_disconnect_port(serio);
478 } else if (!strncmp(buf, "reconnect", count)) {
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400479 serio_reconnect_chain(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else if (!strncmp(buf, "rescan", count)) {
481 serio_disconnect_port(serio);
482 serio_find_driver(serio);
483 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
484 serio_disconnect_port(serio);
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400485 error = serio_bind_driver(serio, to_serio_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 put_driver(drv);
487 } else {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400488 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500491 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400493 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Yani Ioannoue404e272005-05-17 06:42:58 -0400496static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct serio *serio = to_serio_port(dev);
499 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
500}
501
Yani Ioannoue404e272005-05-17 06:42:58 -0400502static 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 -0700503{
504 struct serio *serio = to_serio_port(dev);
505 int retval;
506
507 retval = count;
508 if (!strncmp(buf, "manual", count)) {
509 serio->manual_bind = 1;
510 } else if (!strncmp(buf, "auto", count)) {
511 serio->manual_bind = 0;
512 } else {
513 retval = -EINVAL;
514 }
515
516 return retval;
517}
518
519static struct device_attribute serio_device_attrs[] = {
520 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500521 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
523 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
524 __ATTR_NULL
525};
526
527
528static void serio_release_port(struct device *dev)
529{
530 struct serio *serio = to_serio_port(dev);
531
532 kfree(serio);
533 module_put(THIS_MODULE);
534}
535
536/*
537 * Prepare serio port for registration.
538 */
539static void serio_init_port(struct serio *serio)
540{
541 static atomic_t serio_no = ATOMIC_INIT(0);
542
543 __module_get(THIS_MODULE);
544
Randy Dunlap73b59a32006-07-19 01:14:55 -0400545 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500547 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 device_initialize(&serio->dev);
549 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
550 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
551 serio->dev.bus = &serio_bus;
552 serio->dev.release = serio_release_port;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400553 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400555 serio->depth = serio->parent->depth + 1;
556 } else
557 serio->depth = 0;
558 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
562 * Complete serio port registration.
563 * Driver core will attempt to find appropriate driver for the port.
564 */
565static void serio_add_port(struct serio *serio)
566{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400567 int error;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (serio->parent) {
570 serio_pause_rx(serio->parent);
571 serio->parent->child = serio;
572 serio_continue_rx(serio->parent);
573 }
574
575 list_add_tail(&serio->node, &serio_list);
576 if (serio->start)
577 serio->start(serio);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400578 error = device_add(&serio->dev);
579 if (error)
580 printk(KERN_ERR
581 "serio: device_add() failed for %s (%s), error: %d\n",
582 serio->phys, serio->name, error);
583 else {
584 serio->registered = 1;
585 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
586 if (error)
587 printk(KERN_ERR
588 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
589 serio->phys, serio->name, error);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*
594 * serio_destroy_port() completes deregistration process and removes
595 * port from the system
596 */
597static void serio_destroy_port(struct serio *serio)
598{
599 struct serio *child;
600
601 child = serio_get_pending_child(serio);
602 if (child) {
603 serio_remove_pending_events(child);
604 put_device(&child->dev);
605 }
606
607 if (serio->stop)
608 serio->stop(serio);
609
610 if (serio->parent) {
611 serio_pause_rx(serio->parent);
612 serio->parent->child = NULL;
613 serio_continue_rx(serio->parent);
614 serio->parent = NULL;
615 }
616
617 if (serio->registered) {
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700618 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 serio->registered = 0;
621 }
622
Randy Dunlap73b59a32006-07-19 01:14:55 -0400623 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 serio_remove_pending_events(serio);
625 put_device(&serio->dev);
626}
627
628/*
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400629 * Reconnect serio port (re-initialize attached device).
630 * If reconnect fails (old device is no longer attached or
631 * there was no device to begin with) we do full rescan in
632 * hope of finding a driver for the port.
633 */
634static int serio_reconnect_port(struct serio *serio)
635{
636 int error = serio_reconnect_driver(serio);
637
638 if (error) {
639 serio_disconnect_port(serio);
640 serio_find_driver(serio);
641 }
642
643 return error;
644}
645
646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * Reconnect serio port and all its children (re-initialize attached devices)
648 */
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400649static void serio_reconnect_chain(struct serio *serio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 do {
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400652 if (serio_reconnect_port(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Ok, old children are now gone, we are done */
654 break;
655 }
656 serio = serio->child;
657 } while (serio);
658}
659
660/*
661 * serio_disconnect_port() unbinds a port from its driver. As a side effect
662 * all child ports are unbound and destroyed.
663 */
664static void serio_disconnect_port(struct serio *serio)
665{
666 struct serio *s, *parent;
667
668 if (serio->child) {
669 /*
670 * Children ports should be disconnected and destroyed
671 * first, staring with the leaf one, since we don't want
672 * to do recursion
673 */
674 for (s = serio; s->child; s = s->child)
675 /* empty */;
676
677 do {
678 parent = s->parent;
679
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400680 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 serio_destroy_port(s);
682 } while ((s = parent) != serio);
683 }
684
685 /*
686 * Ok, no children left, now disconnect this port
687 */
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400688 device_release_driver(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691void serio_rescan(struct serio *serio)
692{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500693 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696void serio_reconnect(struct serio *serio)
697{
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400698 serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Submits register request to kseriod for subsequent execution.
703 * Note that port registration is always asynchronous.
704 */
705void __serio_register_port(struct serio *serio, struct module *owner)
706{
707 serio_init_port(serio);
708 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
709}
710
711/*
712 * Synchronously unregisters serio port.
713 */
714void serio_unregister_port(struct serio *serio)
715{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500716 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 serio_disconnect_port(serio);
718 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500719 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700723 * Safely unregisters child port if one is present.
724 */
725void serio_unregister_child_port(struct serio *serio)
726{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500727 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700728 if (serio->child) {
729 serio_disconnect_port(serio->child);
730 serio_destroy_port(serio->child);
731 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500732 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736/*
737 * Serio driver operations
738 */
739
740static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
741{
742 struct serio_driver *driver = to_serio_driver(drv);
743 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
744}
745
746static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
747{
748 struct serio_driver *serio_drv = to_serio_driver(drv);
749 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
750}
751
752static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
753{
754 struct serio_driver *serio_drv = to_serio_driver(drv);
755 int retval;
756
757 retval = count;
758 if (!strncmp(buf, "manual", count)) {
759 serio_drv->manual_bind = 1;
760 } else if (!strncmp(buf, "auto", count)) {
761 serio_drv->manual_bind = 0;
762 } else {
763 retval = -EINVAL;
764 }
765
766 return retval;
767}
768
769
770static struct driver_attribute serio_driver_attrs[] = {
771 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
772 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
773 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
774 __ATTR_NULL
775};
776
777static int serio_driver_probe(struct device *dev)
778{
779 struct serio *serio = to_serio_port(dev);
780 struct serio_driver *drv = to_serio_driver(dev->driver);
781
Linus Torvalds3c803e82005-06-27 17:49:45 -0700782 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785static int serio_driver_remove(struct device *dev)
786{
787 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds3c803e82005-06-27 17:49:45 -0700789 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791}
792
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500793static void serio_cleanup(struct serio *serio)
794{
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400795 mutex_lock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500796 if (serio->drv && serio->drv->cleanup)
797 serio->drv->cleanup(serio);
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400798 mutex_unlock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500799}
800
801static void serio_shutdown(struct device *dev)
802{
803 struct serio *serio = to_serio_port(dev);
804
805 serio_cleanup(serio);
806}
807
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500808static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400809{
810 int error;
811
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500812 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400813 if (error)
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500814 printk(KERN_WARNING
815 "serio: driver_attach() failed for %s with error %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400816 drv->driver.name, error);
817}
818
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800819int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500821 int manual_bind = drv->manual_bind;
822 int error;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800825 drv->driver.owner = owner;
826 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500828 /*
829 * Temporarily disable automatic binding because probing
830 * takes long time and we are better off doing it in kseriod
831 */
832 drv->manual_bind = 1;
833
834 error = driver_register(&drv->driver);
835 if (error) {
836 printk(KERN_ERR
837 "serio: driver_register() failed for %s, error: %d\n",
838 drv->driver.name, error);
839 return error;
840 }
841
842 /*
843 * Restore original bind mode and let kseriod bind the
844 * driver to free ports
845 */
846 if (!manual_bind) {
847 drv->manual_bind = 0;
848 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
849 if (error) {
850 driver_unregister(&drv->driver);
851 return error;
852 }
853 }
854
855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858void serio_unregister_driver(struct serio_driver *drv)
859{
860 struct serio *serio;
861
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500862 mutex_lock(&serio_mutex);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 drv->manual_bind = 1; /* so serio_find_driver ignores it */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400865 serio_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867start_over:
868 list_for_each_entry(serio, &serio_list, node) {
869 if (serio->drv == drv) {
870 serio_disconnect_port(serio);
871 serio_find_driver(serio);
872 /* we could've deleted some ports, restart */
873 goto start_over;
874 }
875 }
876
877 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500878 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 serio_pause_rx(serio);
884 serio->drv = drv;
885 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static int serio_bus_match(struct device *dev, struct device_driver *drv)
889{
890 struct serio *serio = to_serio_port(dev);
891 struct serio_driver *serio_drv = to_serio_driver(drv);
892
893 if (serio->manual_bind || serio_drv->manual_bind)
894 return 0;
895
896 return serio_match_port(serio_drv->id_table, serio);
897}
898
899#ifdef CONFIG_HOTPLUG
900
Kay Sievers312c0042005-11-16 09:00:00 +0100901#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500902 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200903 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500904 if (err) \
905 return err; \
906 } while (0)
907
Kay Sievers7eff2e72007-08-14 15:15:12 +0200908static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (!dev)
913 return -ENODEV;
914
915 serio = to_serio_port(dev);
916
Kay Sievers312c0042005-11-16 09:00:00 +0100917 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
918 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
919 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
920 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
921 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500922 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 return 0;
925}
Kay Sievers312c0042005-11-16 09:00:00 +0100926#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928#else
929
Kay Sievers7eff2e72007-08-14 15:15:12 +0200930static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 return -ENODEV;
933}
934
935#endif /* CONFIG_HOTPLUG */
936
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500937#ifdef CONFIG_PM
938static int serio_suspend(struct device *dev, pm_message_t state)
939{
940 if (dev->power.power_state.event != state.event) {
941 if (state.event == PM_EVENT_SUSPEND)
942 serio_cleanup(to_serio_port(dev));
943
944 dev->power.power_state = state;
945 }
946
947 return 0;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int serio_resume(struct device *dev)
951{
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400952 /*
953 * Driver reconnect can take a while, so better let kseriod
954 * deal with it.
955 */
956 if (dev->power.power_state.event != PM_EVENT_ON) {
957 dev->power.power_state = PMSG_ON;
958 serio_queue_event(to_serio_port(dev), NULL,
959 SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 return 0;
963}
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500964#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500966/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967int serio_open(struct serio *serio, struct serio_driver *drv)
968{
969 serio_set_drv(serio, drv);
970
971 if (serio->open && serio->open(serio)) {
972 serio_set_drv(serio, NULL);
973 return -1;
974 }
975 return 0;
976}
977
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500978/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979void serio_close(struct serio *serio)
980{
981 if (serio->close)
982 serio->close(serio);
983
984 serio_set_drv(serio, NULL);
985}
986
987irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100988 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 unsigned long flags;
991 irqreturn_t ret = IRQ_NONE;
992
993 spin_lock_irqsave(&serio->lock, flags);
994
995 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100996 ret = serio->drv->interrupt(serio, data, dfl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else if (!dfl && serio->registered) {
998 serio_rescan(serio);
999 ret = IRQ_HANDLED;
1000 }
1001
1002 spin_unlock_irqrestore(&serio->lock, flags);
1003
1004 return ret;
1005}
1006
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001007static struct bus_type serio_bus = {
1008 .name = "serio",
1009 .dev_attrs = serio_device_attrs,
1010 .drv_attrs = serio_driver_attrs,
1011 .match = serio_bus_match,
1012 .uevent = serio_uevent,
1013 .probe = serio_driver_probe,
1014 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001015 .shutdown = serio_shutdown,
1016#ifdef CONFIG_PM
1017 .suspend = serio_suspend,
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001018 .resume = serio_resume,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001019#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001020};
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static int __init serio_init(void)
1023{
Randy Dunlap73b59a32006-07-19 01:14:55 -04001024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Randy Dunlap73b59a32006-07-19 01:14:55 -04001026 error = bus_register(&serio_bus);
1027 if (error) {
1028 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
1029 return error;
1030 }
1031
1032 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1033 if (IS_ERR(serio_task)) {
1034 bus_unregister(&serio_bus);
1035 error = PTR_ERR(serio_task);
1036 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
1037 return error;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 return 0;
1041}
1042
1043static void __exit serio_exit(void)
1044{
1045 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -07001046 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001049subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050module_exit(serio_exit);