blob: 405bf214527c33f08c40c04d60c5e863826dbcc7 [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
Dmitry Torokhovcac91692010-01-05 17:56:04 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/stddef.h>
32#include <linux/module.h>
33#include <linux/serio.h>
34#include <linux/errno.h>
35#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070038#include <linux/kthread.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION("Serio abstraction core");
43MODULE_LICENSE("GPL");
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050046 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * serio port or driver registrered or unregistered.
48 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050049static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static LIST_HEAD(serio_list);
52
Russell King30226f82006-01-05 14:38:53 +000053static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static void serio_add_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040056static int serio_reconnect_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void serio_disconnect_port(struct serio *serio);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -070058static void serio_reconnect_subtree(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050059static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds3c803e82005-06-27 17:49:45 -070061static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
62{
63 int retval;
64
Arjan van de Venc4e32e92006-02-19 00:21:55 -050065 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070066 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050067 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070068
69 return retval;
70}
71
72static int serio_reconnect_driver(struct serio *serio)
73{
74 int retval = -1;
75
Arjan van de Venc4e32e92006-02-19 00:21:55 -050076 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070077 if (serio->drv && serio->drv->reconnect)
78 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050079 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070080
81 return retval;
82}
83
84static void serio_disconnect_driver(struct serio *serio)
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)
88 serio->drv->disconnect(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
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
93{
94 while (ids->type || ids->proto) {
95 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
96 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
97 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
98 (ids->id == SERIO_ANY || ids->id == serio->id.id))
99 return 1;
100 ids++;
101 }
102 return 0;
103}
104
105/*
106 * Basic serio -> driver core mappings
107 */
108
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400109static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400111 int error;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (serio_match_port(drv->id_table, serio)) {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700116 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400118 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400120
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400121 error = device_bind_driver(&serio->dev);
122 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800123 dev_warn(&serio->dev,
124 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
125 serio->phys, serio->name,
126 drv->description, error);
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400127 serio_disconnect_driver(serio);
128 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400129 return error;
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static void serio_find_driver(struct serio *serio)
136{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400137 int error;
138
Randy Dunlap73b59a32006-07-19 01:14:55 -0400139 error = device_attach(&serio->dev);
140 if (error < 0)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800141 dev_warn(&serio->dev,
142 "device_attach() failed for %s (%s), error: %d\n",
143 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146
147/*
148 * Serio event processing.
149 */
150
151enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500152 SERIO_RESCAN_PORT,
153 SERIO_RECONNECT_PORT,
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700154 SERIO_RECONNECT_SUBTREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500156 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159struct serio_event {
160 enum serio_event_type type;
161 void *object;
162 struct module *owner;
163 struct list_head node;
164};
165
166static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
167static LIST_HEAD(serio_event_list);
168static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700169static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500171static int serio_queue_event(void *object, struct module *owner,
172 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
175 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500176 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 spin_lock_irqsave(&serio_event_lock, flags);
179
180 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700181 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * starting with the most recent one. If event is the same we
183 * do not need add new one. If event is of different type we
184 * need to add this event and should not look further because
185 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 list_for_each_entry_reverse(event, &serio_event_list, node) {
188 if (event->object == object) {
189 if (event->type == event_type)
190 goto out;
191 break;
192 }
193 }
194
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500195 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
196 if (!event) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800197 pr_err("Not enough memory to queue event %d\n", event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500198 retval = -ENOMEM;
199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500201
202 if (!try_module_get(owner)) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800203 pr_warning("Can't get module reference, dropping event %d\n",
204 event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500205 kfree(event);
206 retval = -EINVAL;
207 goto out;
208 }
209
210 event->type = event_type;
211 event->object = object;
212 event->owner = owner;
213
214 list_add_tail(&event->node, &serio_event_list);
215 wake_up(&serio_wait);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217out:
218 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500219 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static void serio_free_event(struct serio_event *event)
223{
224 module_put(event->owner);
225 kfree(event);
226}
227
228static void serio_remove_duplicate_events(struct serio_event *event)
229{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800230 struct serio_event *e, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned long flags;
232
233 spin_lock_irqsave(&serio_event_lock, flags);
234
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800235 list_for_each_entry_safe(e, next, &serio_event_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (event->object == e->object) {
237 /*
238 * If this event is of different type we should not
239 * look further - we only suppress duplicate events
240 * that were sent back-to-back.
241 */
242 if (event->type != e->type)
243 break;
244
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800245 list_del_init(&e->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 serio_free_event(e);
247 }
248 }
249
250 spin_unlock_irqrestore(&serio_event_lock, flags);
251}
252
253
254static struct serio_event *serio_get_event(void)
255{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800256 struct serio_event *event = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned long flags;
258
259 spin_lock_irqsave(&serio_event_lock, flags);
260
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800261 if (!list_empty(&serio_event_list)) {
262 event = list_first_entry(&serio_event_list,
263 struct serio_event, node);
264 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_unlock_irqrestore(&serio_event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return event;
269}
270
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500271static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500275 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dmitry Torokhovea486e62009-12-24 21:40:43 -0800277 while ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 switch (event->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800281 case SERIO_REGISTER_PORT:
282 serio_add_port(event->object);
283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800285 case SERIO_RECONNECT_PORT:
286 serio_reconnect_port(event->object);
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800289 case SERIO_RESCAN_PORT:
290 serio_disconnect_port(event->object);
291 serio_find_driver(event->object);
292 break;
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400293
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700294 case SERIO_RECONNECT_SUBTREE:
295 serio_reconnect_subtree(event->object);
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800298 case SERIO_ATTACH_DRIVER:
299 serio_attach_driver(event->object);
300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 serio_remove_duplicate_events(event);
304 serio_free_event(event);
305 }
306
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500307 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400311 * Remove all events that have been submitted for a given
312 * object, be it serio port or driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400314static void serio_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800316 struct serio_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
318
319 spin_lock_irqsave(&serio_event_lock, flags);
320
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800321 list_for_each_entry_safe(event, next, &serio_event_list, node) {
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400322 if (event->object == object) {
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800323 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 serio_free_event(event);
325 }
326 }
327
328 spin_unlock_irqrestore(&serio_event_lock, flags);
329}
330
331/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700332 * Locate child serio port (if any) that has not been fully registered yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700334 * Children are registered by driver's connect() handler so there can't be a
335 * grandchild pending registration together with a child.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337static struct serio *serio_get_pending_child(struct serio *parent)
338{
339 struct serio_event *event;
340 struct serio *serio, *child = NULL;
341 unsigned long flags;
342
343 spin_lock_irqsave(&serio_event_lock, flags);
344
345 list_for_each_entry(event, &serio_event_list, node) {
346 if (event->type == SERIO_REGISTER_PORT) {
347 serio = event->object;
348 if (serio->parent == parent) {
349 child = serio;
350 break;
351 }
352 }
353 }
354
355 spin_unlock_irqrestore(&serio_event_lock, flags);
356 return child;
357}
358
359static int serio_thread(void *nothing)
360{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500362 serio_handle_event();
Dmitry Torokhovea486e62009-12-24 21:40:43 -0800363 wait_event_interruptible(serio_wait,
Linus Torvalds3c803e82005-06-27 17:49:45 -0700364 kthread_should_stop() || !list_empty(&serio_event_list));
Linus Torvalds3c803e82005-06-27 17:49:45 -0700365 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds3c803e82005-06-27 17:49:45 -0700367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370
371/*
372 * Serio port operations
373 */
374
Yani Ioannoue404e272005-05-17 06:42:58 -0400375static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct serio *serio = to_serio_port(dev);
378 return sprintf(buf, "%s\n", serio->name);
379}
380
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500381static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
382{
383 struct serio *serio = to_serio_port(dev);
384
385 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
386 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
387}
388
Yani Ioannoue404e272005-05-17 06:42:58 -0400389static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct serio *serio = to_serio_port(dev);
392 return sprintf(buf, "%02x\n", serio->id.type);
393}
394
Yani Ioannoue404e272005-05-17 06:42:58 -0400395static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct serio *serio = to_serio_port(dev);
398 return sprintf(buf, "%02x\n", serio->id.proto);
399}
400
Yani Ioannoue404e272005-05-17 06:42:58 -0400401static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct serio *serio = to_serio_port(dev);
404 return sprintf(buf, "%02x\n", serio->id.id);
405}
406
Yani Ioannoue404e272005-05-17 06:42:58 -0400407static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct serio *serio = to_serio_port(dev);
410 return sprintf(buf, "%02x\n", serio->id.extra);
411}
412
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700413static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
414static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
415static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
416static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
417
418static struct attribute *serio_device_id_attrs[] = {
419 &dev_attr_type.attr,
420 &dev_attr_proto.attr,
421 &dev_attr_id.attr,
422 &dev_attr_extra.attr,
423 NULL
424};
425
426static struct attribute_group serio_id_attr_group = {
427 .name = "id",
428 .attrs = serio_device_id_attrs,
429};
430
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800431static const struct attribute_group *serio_device_attr_groups[] = {
432 &serio_id_attr_group,
433 NULL
434};
435
Yani Ioannoue404e272005-05-17 06:42:58 -0400436static 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 -0700437{
438 struct serio *serio = to_serio_port(dev);
439 struct device_driver *drv;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400440 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400442 error = mutex_lock_interruptible(&serio_mutex);
443 if (error)
444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!strncmp(buf, "none", count)) {
447 serio_disconnect_port(serio);
448 } else if (!strncmp(buf, "reconnect", count)) {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700449 serio_reconnect_subtree(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else if (!strncmp(buf, "rescan", count)) {
451 serio_disconnect_port(serio);
452 serio_find_driver(serio);
453 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
454 serio_disconnect_port(serio);
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400455 error = serio_bind_driver(serio, to_serio_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 put_driver(drv);
457 } else {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400458 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500461 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400463 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Yani Ioannoue404e272005-05-17 06:42:58 -0400466static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct serio *serio = to_serio_port(dev);
469 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
470}
471
Yani Ioannoue404e272005-05-17 06:42:58 -0400472static 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 -0700473{
474 struct serio *serio = to_serio_port(dev);
475 int retval;
476
477 retval = count;
478 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700479 serio->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700481 serio->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
483 retval = -EINVAL;
484 }
485
486 return retval;
487}
488
489static struct device_attribute serio_device_attrs[] = {
490 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500491 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
493 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
494 __ATTR_NULL
495};
496
497
498static void serio_release_port(struct device *dev)
499{
500 struct serio *serio = to_serio_port(dev);
501
502 kfree(serio);
503 module_put(THIS_MODULE);
504}
505
506/*
507 * Prepare serio port for registration.
508 */
509static void serio_init_port(struct serio *serio)
510{
511 static atomic_t serio_no = ATOMIC_INIT(0);
512
513 __module_get(THIS_MODULE);
514
Randy Dunlap73b59a32006-07-19 01:14:55 -0400515 INIT_LIST_HEAD(&serio->node);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700516 INIT_LIST_HEAD(&serio->child_node);
517 INIT_LIST_HEAD(&serio->children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500519 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 device_initialize(&serio->dev);
Kay Sieversa6c24902008-10-30 00:07:50 -0400521 dev_set_name(&serio->dev, "serio%ld",
522 (long)atomic_inc_return(&serio_no) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 serio->dev.bus = &serio_bus;
524 serio->dev.release = serio_release_port;
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800525 serio->dev.groups = serio_device_attr_groups;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400526 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400528 serio->depth = serio->parent->depth + 1;
529 } else
530 serio->depth = 0;
531 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * Complete serio port registration.
536 * Driver core will attempt to find appropriate driver for the port.
537 */
538static void serio_add_port(struct serio *serio)
539{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700540 struct serio *parent = serio->parent;
Randy Dunlap73b59a32006-07-19 01:14:55 -0400541 int error;
542
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700543 if (parent) {
544 serio_pause_rx(parent);
545 list_add_tail(&serio->child_node, &parent->children);
546 serio_continue_rx(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 list_add_tail(&serio->node, &serio_list);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (serio->start)
552 serio->start(serio);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800553
Randy Dunlap73b59a32006-07-19 01:14:55 -0400554 error = device_add(&serio->dev);
555 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800556 dev_err(&serio->dev,
557 "device_add() failed for %s (%s), error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400558 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700562 * serio_destroy_port() completes unregistration process and removes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * port from the system
564 */
565static void serio_destroy_port(struct serio *serio)
566{
567 struct serio *child;
568
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700569 while ((child = serio_get_pending_child(serio)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 serio_remove_pending_events(child);
571 put_device(&child->dev);
572 }
573
574 if (serio->stop)
575 serio->stop(serio);
576
577 if (serio->parent) {
578 serio_pause_rx(serio->parent);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700579 list_del_init(&serio->child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 serio_continue_rx(serio->parent);
581 serio->parent = NULL;
582 }
583
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800584 if (device_is_registered(&serio->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Randy Dunlap73b59a32006-07-19 01:14:55 -0400587 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 serio_remove_pending_events(serio);
589 put_device(&serio->dev);
590}
591
592/*
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400593 * Reconnect serio port (re-initialize attached device).
594 * If reconnect fails (old device is no longer attached or
595 * there was no device to begin with) we do full rescan in
596 * hope of finding a driver for the port.
597 */
598static int serio_reconnect_port(struct serio *serio)
599{
600 int error = serio_reconnect_driver(serio);
601
602 if (error) {
603 serio_disconnect_port(serio);
604 serio_find_driver(serio);
605 }
606
607 return error;
608}
609
610/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700611 * Reconnect serio port and all its children (re-initialize attached
612 * devices).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700614static void serio_reconnect_subtree(struct serio *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700616 struct serio *s = root;
617 int error;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 do {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700620 error = serio_reconnect_port(s);
621 if (!error) {
622 /*
623 * Reconnect was successful, move on to do the
624 * first child.
625 */
626 if (!list_empty(&s->children)) {
627 s = list_first_entry(&s->children,
628 struct serio, child_node);
629 continue;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700632
633 /*
634 * Either it was a leaf node or reconnect failed and it
635 * became a leaf node. Continue reconnecting starting with
636 * the next sibling of the parent node.
637 */
638 while (s != root) {
639 struct serio *parent = s->parent;
640
641 if (!list_is_last(&s->child_node, &parent->children)) {
642 s = list_entry(s->child_node.next,
643 struct serio, child_node);
644 break;
645 }
646
647 s = parent;
648 }
649 } while (s != root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
653 * serio_disconnect_port() unbinds a port from its driver. As a side effect
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700654 * all children ports are unbound and destroyed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656static void serio_disconnect_port(struct serio *serio)
657{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700658 struct serio *s = serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700660 /*
661 * Children ports should be disconnected and destroyed
662 * first; we travel the tree in depth-first order.
663 */
664 while (!list_empty(&serio->children)) {
665
666 /* Locate a leaf */
667 while (!list_empty(&s->children))
668 s = list_first_entry(&s->children,
669 struct serio, child_node);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700672 * Prune this leaf node unless it is the one we
673 * started with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700675 if (s != serio) {
676 struct serio *parent = s->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400678 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 serio_destroy_port(s);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700680
681 s = parent;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700686 * OK, no children left, now disconnect this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
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}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700695EXPORT_SYMBOL(serio_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697void serio_reconnect(struct serio *serio)
698{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700699 serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700701EXPORT_SYMBOL(serio_reconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703/*
704 * Submits register request to kseriod for subsequent execution.
705 * Note that port registration is always asynchronous.
706 */
707void __serio_register_port(struct serio *serio, struct module *owner)
708{
709 serio_init_port(serio);
710 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
711}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700712EXPORT_SYMBOL(__serio_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/*
715 * Synchronously unregisters serio port.
716 */
717void serio_unregister_port(struct serio *serio)
718{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500719 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 serio_disconnect_port(serio);
721 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500722 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700724EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700727 * Safely unregisters children ports if they are present.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700728 */
729void serio_unregister_child_port(struct serio *serio)
730{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700731 struct serio *s, *next;
732
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500733 mutex_lock(&serio_mutex);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700734 list_for_each_entry_safe(s, next, &serio->children, child_node) {
735 serio_disconnect_port(s);
736 serio_destroy_port(s);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700737 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500738 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700739}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700740EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/*
744 * Serio driver operations
745 */
746
747static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
748{
749 struct serio_driver *driver = to_serio_driver(drv);
750 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
751}
752
753static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
754{
755 struct serio_driver *serio_drv = to_serio_driver(drv);
756 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
757}
758
759static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
760{
761 struct serio_driver *serio_drv = to_serio_driver(drv);
762 int retval;
763
764 retval = count;
765 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700766 serio_drv->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700768 serio_drv->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 } else {
770 retval = -EINVAL;
771 }
772
773 return retval;
774}
775
776
777static struct driver_attribute serio_driver_attrs[] = {
778 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
779 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
780 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
781 __ATTR_NULL
782};
783
784static int serio_driver_probe(struct device *dev)
785{
786 struct serio *serio = to_serio_port(dev);
787 struct serio_driver *drv = to_serio_driver(dev->driver);
788
Linus Torvalds3c803e82005-06-27 17:49:45 -0700789 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static int serio_driver_remove(struct device *dev)
793{
794 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvalds3c803e82005-06-27 17:49:45 -0700796 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500800static void serio_cleanup(struct serio *serio)
801{
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400802 mutex_lock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500803 if (serio->drv && serio->drv->cleanup)
804 serio->drv->cleanup(serio);
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400805 mutex_unlock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500806}
807
808static void serio_shutdown(struct device *dev)
809{
810 struct serio *serio = to_serio_port(dev);
811
812 serio_cleanup(serio);
813}
814
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500815static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400816{
817 int error;
818
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500819 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400820 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800821 pr_warning("driver_attach() failed for %s with error %d\n",
822 drv->driver.name, error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400823}
824
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800825int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700827 bool manual_bind = drv->manual_bind;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500828 int error;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800831 drv->driver.owner = owner;
832 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500834 /*
835 * Temporarily disable automatic binding because probing
836 * takes long time and we are better off doing it in kseriod
837 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700838 drv->manual_bind = true;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500839
840 error = driver_register(&drv->driver);
841 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800842 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500843 drv->driver.name, error);
844 return error;
845 }
846
847 /*
848 * Restore original bind mode and let kseriod bind the
849 * driver to free ports
850 */
851 if (!manual_bind) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700852 drv->manual_bind = false;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500853 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
854 if (error) {
855 driver_unregister(&drv->driver);
856 return error;
857 }
858 }
859
860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700862EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864void serio_unregister_driver(struct serio_driver *drv)
865{
866 struct serio *serio;
867
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500868 mutex_lock(&serio_mutex);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400869
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700870 drv->manual_bind = true; /* so serio_find_driver ignores it */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400871 serio_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873start_over:
874 list_for_each_entry(serio, &serio_list, node) {
875 if (serio->drv == drv) {
876 serio_disconnect_port(serio);
877 serio_find_driver(serio);
878 /* we could've deleted some ports, restart */
879 goto start_over;
880 }
881 }
882
883 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500884 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700886EXPORT_SYMBOL(serio_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
889{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 serio_pause_rx(serio);
891 serio->drv = drv;
892 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895static int serio_bus_match(struct device *dev, struct device_driver *drv)
896{
897 struct serio *serio = to_serio_port(dev);
898 struct serio_driver *serio_drv = to_serio_driver(drv);
899
900 if (serio->manual_bind || serio_drv->manual_bind)
901 return 0;
902
903 return serio_match_port(serio_drv->id_table, serio);
904}
905
906#ifdef CONFIG_HOTPLUG
907
Kay Sievers312c0042005-11-16 09:00:00 +0100908#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500909 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200910 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500911 if (err) \
912 return err; \
913 } while (0)
914
Kay Sievers7eff2e72007-08-14 15:15:12 +0200915static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (!dev)
920 return -ENODEV;
921
922 serio = to_serio_port(dev);
923
Kay Sievers312c0042005-11-16 09:00:00 +0100924 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
925 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
926 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
927 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
928 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500929 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 return 0;
932}
Kay Sievers312c0042005-11-16 09:00:00 +0100933#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935#else
936
Kay Sievers7eff2e72007-08-14 15:15:12 +0200937static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 return -ENODEV;
940}
941
942#endif /* CONFIG_HOTPLUG */
943
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500944#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700945static int serio_suspend(struct device *dev)
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500946{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700947 struct serio *serio = to_serio_port(dev);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500948
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700949 serio_cleanup(serio);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500950
951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int serio_resume(struct device *dev)
955{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700956 struct serio *serio = to_serio_port(dev);
957
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400958 /*
959 * Driver reconnect can take a while, so better let kseriod
960 * deal with it.
961 */
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700962 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 return 0;
965}
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700966
967static const struct dev_pm_ops serio_pm_ops = {
968 .suspend = serio_suspend,
969 .resume = serio_resume,
970 .poweroff = serio_suspend,
971 .restore = serio_resume,
972};
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500973#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500975/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976int serio_open(struct serio *serio, struct serio_driver *drv)
977{
978 serio_set_drv(serio, drv);
979
980 if (serio->open && serio->open(serio)) {
981 serio_set_drv(serio, NULL);
982 return -1;
983 }
984 return 0;
985}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700986EXPORT_SYMBOL(serio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500988/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989void serio_close(struct serio *serio)
990{
991 if (serio->close)
992 serio->close(serio);
993
994 serio_set_drv(serio, NULL);
995}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700996EXPORT_SYMBOL(serio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100999 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 unsigned long flags;
1002 irqreturn_t ret = IRQ_NONE;
1003
1004 spin_lock_irqsave(&serio->lock, flags);
1005
1006 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +01001007 ret = serio->drv->interrupt(serio, data, dfl);
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -08001008 } else if (!dfl && device_is_registered(&serio->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 serio_rescan(serio);
1010 ret = IRQ_HANDLED;
1011 }
1012
1013 spin_unlock_irqrestore(&serio->lock, flags);
1014
1015 return ret;
1016}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -07001017EXPORT_SYMBOL(serio_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001019static struct bus_type serio_bus = {
1020 .name = "serio",
1021 .dev_attrs = serio_device_attrs,
1022 .drv_attrs = serio_driver_attrs,
1023 .match = serio_bus_match,
1024 .uevent = serio_uevent,
1025 .probe = serio_driver_probe,
1026 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001027 .shutdown = serio_shutdown,
1028#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -07001029 .pm = &serio_pm_ops,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001030#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001031};
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static int __init serio_init(void)
1034{
Randy Dunlap73b59a32006-07-19 01:14:55 -04001035 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Randy Dunlap73b59a32006-07-19 01:14:55 -04001037 error = bus_register(&serio_bus);
1038 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001039 pr_err("Failed to register serio bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001040 return error;
1041 }
1042
1043 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1044 if (IS_ERR(serio_task)) {
1045 bus_unregister(&serio_bus);
1046 error = PTR_ERR(serio_task);
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001047 pr_err("Failed to start kseriod, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001048 return error;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 return 0;
1052}
1053
1054static void __exit serio_exit(void)
1055{
1056 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -07001057 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001060subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061module_exit(serio_exit);