blob: c3b626e9eae7c075cd7597779ddbef4becb731f6 [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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43MODULE_DESCRIPTION("Serio abstraction core");
44MODULE_LICENSE("GPL");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050047 * serio_mutex protects entire serio subsystem and is taken every time
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * serio port or driver registrered or unregistered.
49 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050050static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static LIST_HEAD(serio_list);
53
Russell King30226f82006-01-05 14:38:53 +000054static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static void serio_add_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040057static int serio_reconnect_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void serio_disconnect_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040059static void serio_reconnect_chain(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050060static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds3c803e82005-06-27 17:49:45 -070062static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
63{
64 int retval;
65
Arjan van de Venc4e32e92006-02-19 00:21:55 -050066 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070067 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050068 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070069
70 return retval;
71}
72
73static int serio_reconnect_driver(struct serio *serio)
74{
75 int retval = -1;
76
Arjan van de Venc4e32e92006-02-19 00:21:55 -050077 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070078 if (serio->drv && serio->drv->reconnect)
79 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050080 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070081
82 return retval;
83}
84
85static void serio_disconnect_driver(struct serio *serio)
86{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050087 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070088 if (serio->drv)
89 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050090 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static 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
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400110static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400112 int error;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (serio_match_port(drv->id_table, serio)) {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700117 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400119 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400121
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400122 error = device_bind_driver(&serio->dev);
123 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800124 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);
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400128 serio_disconnect_driver(serio);
129 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400130 return error;
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static void serio_find_driver(struct serio *serio)
137{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400138 int error;
139
Randy Dunlap73b59a32006-07-19 01:14:55 -0400140 error = device_attach(&serio->dev);
141 if (error < 0)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800142 dev_warn(&serio->dev,
143 "device_attach() failed for %s (%s), error: %d\n",
144 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147
148/*
149 * Serio event processing.
150 */
151
152enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500153 SERIO_RESCAN_PORT,
154 SERIO_RECONNECT_PORT,
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400155 SERIO_RECONNECT_CHAIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500157 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
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);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700170static struct task_struct *serio_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500172static int serio_queue_event(void *object, struct module *owner,
173 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 unsigned long flags;
176 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500177 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 spin_lock_irqsave(&serio_event_lock, flags);
180
181 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700182 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * 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.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 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
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500196 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
197 if (!event) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800198 pr_err("Not enough memory to queue event %d\n", event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500199 retval = -ENOMEM;
200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500202
203 if (!try_module_get(owner)) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800204 pr_warning("Can't get module reference, dropping event %d\n",
205 event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500206 kfree(event);
207 retval = -EINVAL;
208 goto out;
209 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218out:
219 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
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{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800231 struct serio_event *e, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned long flags;
233
234 spin_lock_irqsave(&serio_event_lock, flags);
235
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800236 list_for_each_entry_safe(e, next, &serio_event_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 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
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800246 list_del_init(&e->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 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{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800257 struct serio_event *event = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned long flags;
259
260 spin_lock_irqsave(&serio_event_lock, flags);
261
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800262 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_unlock_irqrestore(&serio_event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return event;
270}
271
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500272static void serio_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct serio_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500276 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Dmitry Torokhovea486e62009-12-24 21:40:43 -0800278 while ((event = serio_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 switch (event->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800282 case SERIO_REGISTER_PORT:
283 serio_add_port(event->object);
284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800286 case SERIO_RECONNECT_PORT:
287 serio_reconnect_port(event->object);
288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800290 case SERIO_RESCAN_PORT:
291 serio_disconnect_port(event->object);
292 serio_find_driver(event->object);
293 break;
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400294
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800295 case SERIO_RECONNECT_CHAIN:
296 serio_reconnect_chain(event->object);
297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800299 case SERIO_ATTACH_DRIVER:
300 serio_attach_driver(event->object);
301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 serio_remove_duplicate_events(event);
305 serio_free_event(event);
306 }
307
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500308 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400312 * Remove all events that have been submitted for a given
313 * object, be it serio port or driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400315static void serio_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800317 struct serio_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned long flags;
319
320 spin_lock_irqsave(&serio_event_lock, flags);
321
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800322 list_for_each_entry_safe(event, next, &serio_event_list, node) {
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400323 if (event->object == object) {
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800324 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 serio_free_event(event);
326 }
327 }
328
329 spin_unlock_irqrestore(&serio_event_lock, flags);
330}
331
332/*
333 * Destroy child serio port (if any) that has not been fully registered yet.
334 *
335 * Note that we rely on the fact that port can have only one child and therefore
336 * only one child registration request can be pending. Additionally, children
337 * are registered by driver's connect() handler so there can't be a grandchild
338 * pending registration together with a child.
339 */
340static struct serio *serio_get_pending_child(struct serio *parent)
341{
342 struct serio_event *event;
343 struct serio *serio, *child = NULL;
344 unsigned long flags;
345
346 spin_lock_irqsave(&serio_event_lock, flags);
347
348 list_for_each_entry(event, &serio_event_list, node) {
349 if (event->type == SERIO_REGISTER_PORT) {
350 serio = event->object;
351 if (serio->parent == parent) {
352 child = serio;
353 break;
354 }
355 }
356 }
357
358 spin_unlock_irqrestore(&serio_event_lock, flags);
359 return child;
360}
361
362static int serio_thread(void *nothing)
363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500365 serio_handle_event();
Dmitry Torokhovea486e62009-12-24 21:40:43 -0800366 wait_event_interruptible(serio_wait,
Linus Torvalds3c803e82005-06-27 17:49:45 -0700367 kthread_should_stop() || !list_empty(&serio_event_list));
Linus Torvalds3c803e82005-06-27 17:49:45 -0700368 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds3c803e82005-06-27 17:49:45 -0700370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373
374/*
375 * Serio port operations
376 */
377
Yani Ioannoue404e272005-05-17 06:42:58 -0400378static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct serio *serio = to_serio_port(dev);
381 return sprintf(buf, "%s\n", serio->name);
382}
383
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500384static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
385{
386 struct serio *serio = to_serio_port(dev);
387
388 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
389 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
390}
391
Yani Ioannoue404e272005-05-17 06:42:58 -0400392static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct serio *serio = to_serio_port(dev);
395 return sprintf(buf, "%02x\n", serio->id.type);
396}
397
Yani Ioannoue404e272005-05-17 06:42:58 -0400398static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct serio *serio = to_serio_port(dev);
401 return sprintf(buf, "%02x\n", serio->id.proto);
402}
403
Yani Ioannoue404e272005-05-17 06:42:58 -0400404static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct serio *serio = to_serio_port(dev);
407 return sprintf(buf, "%02x\n", serio->id.id);
408}
409
Yani Ioannoue404e272005-05-17 06:42:58 -0400410static ssize_t serio_show_id_extra(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, "%02x\n", serio->id.extra);
414}
415
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700416static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
417static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
418static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
419static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
420
421static struct attribute *serio_device_id_attrs[] = {
422 &dev_attr_type.attr,
423 &dev_attr_proto.attr,
424 &dev_attr_id.attr,
425 &dev_attr_extra.attr,
426 NULL
427};
428
429static struct attribute_group serio_id_attr_group = {
430 .name = "id",
431 .attrs = serio_device_id_attrs,
432};
433
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800434static const struct attribute_group *serio_device_attr_groups[] = {
435 &serio_id_attr_group,
436 NULL
437};
438
Yani Ioannoue404e272005-05-17 06:42:58 -0400439static 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 -0700440{
441 struct serio *serio = to_serio_port(dev);
442 struct device_driver *drv;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400445 error = mutex_lock_interruptible(&serio_mutex);
446 if (error)
447 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!strncmp(buf, "none", count)) {
450 serio_disconnect_port(serio);
451 } else if (!strncmp(buf, "reconnect", count)) {
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400452 serio_reconnect_chain(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else if (!strncmp(buf, "rescan", count)) {
454 serio_disconnect_port(serio);
455 serio_find_driver(serio);
456 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
457 serio_disconnect_port(serio);
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400458 error = serio_bind_driver(serio, to_serio_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 put_driver(drv);
460 } else {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400461 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500464 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400466 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Yani Ioannoue404e272005-05-17 06:42:58 -0400469static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct serio *serio = to_serio_port(dev);
472 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
473}
474
Yani Ioannoue404e272005-05-17 06:42:58 -0400475static 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 -0700476{
477 struct serio *serio = to_serio_port(dev);
478 int retval;
479
480 retval = count;
481 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700482 serio->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700484 serio->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else {
486 retval = -EINVAL;
487 }
488
489 return retval;
490}
491
492static struct device_attribute serio_device_attrs[] = {
493 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500494 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
496 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
497 __ATTR_NULL
498};
499
500
501static void serio_release_port(struct device *dev)
502{
503 struct serio *serio = to_serio_port(dev);
504
505 kfree(serio);
506 module_put(THIS_MODULE);
507}
508
509/*
510 * Prepare serio port for registration.
511 */
512static void serio_init_port(struct serio *serio)
513{
514 static atomic_t serio_no = ATOMIC_INIT(0);
515
516 __module_get(THIS_MODULE);
517
Randy Dunlap73b59a32006-07-19 01:14:55 -0400518 INIT_LIST_HEAD(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500520 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 device_initialize(&serio->dev);
Kay Sieversa6c24902008-10-30 00:07:50 -0400522 dev_set_name(&serio->dev, "serio%ld",
523 (long)atomic_inc_return(&serio_no) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 serio->dev.bus = &serio_bus;
525 serio->dev.release = serio_release_port;
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800526 serio->dev.groups = serio_device_attr_groups;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400527 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400529 serio->depth = serio->parent->depth + 1;
530 } else
531 serio->depth = 0;
532 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * Complete serio port registration.
537 * Driver core will attempt to find appropriate driver for the port.
538 */
539static void serio_add_port(struct serio *serio)
540{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400541 int error;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (serio->parent) {
544 serio_pause_rx(serio->parent);
545 serio->parent->child = serio;
546 serio_continue_rx(serio->parent);
547 }
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/*
562 * serio_destroy_port() completes deregistration process and removes
563 * port from the system
564 */
565static void serio_destroy_port(struct serio *serio)
566{
567 struct serio *child;
568
569 child = serio_get_pending_child(serio);
570 if (child) {
571 serio_remove_pending_events(child);
572 put_device(&child->dev);
573 }
574
575 if (serio->stop)
576 serio->stop(serio);
577
578 if (serio->parent) {
579 serio_pause_rx(serio->parent);
580 serio->parent->child = NULL;
581 serio_continue_rx(serio->parent);
582 serio->parent = NULL;
583 }
584
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800585 if (device_is_registered(&serio->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Randy Dunlap73b59a32006-07-19 01:14:55 -0400588 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 serio_remove_pending_events(serio);
590 put_device(&serio->dev);
591}
592
593/*
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400594 * Reconnect serio port (re-initialize attached device).
595 * If reconnect fails (old device is no longer attached or
596 * there was no device to begin with) we do full rescan in
597 * hope of finding a driver for the port.
598 */
599static int serio_reconnect_port(struct serio *serio)
600{
601 int error = serio_reconnect_driver(serio);
602
603 if (error) {
604 serio_disconnect_port(serio);
605 serio_find_driver(serio);
606 }
607
608 return error;
609}
610
611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * Reconnect serio port and all its children (re-initialize attached devices)
613 */
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400614static void serio_reconnect_chain(struct serio *serio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 do {
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400617 if (serio_reconnect_port(serio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Ok, old children are now gone, we are done */
619 break;
620 }
621 serio = serio->child;
622 } while (serio);
623}
624
625/*
626 * serio_disconnect_port() unbinds a port from its driver. As a side effect
627 * all child ports are unbound and destroyed.
628 */
629static void serio_disconnect_port(struct serio *serio)
630{
631 struct serio *s, *parent;
632
633 if (serio->child) {
634 /*
635 * Children ports should be disconnected and destroyed
636 * first, staring with the leaf one, since we don't want
637 * to do recursion
638 */
639 for (s = serio; s->child; s = s->child)
640 /* empty */;
641
642 do {
643 parent = s->parent;
644
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400645 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 serio_destroy_port(s);
647 } while ((s = parent) != serio);
648 }
649
650 /*
651 * Ok, no children left, now disconnect this port
652 */
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400653 device_release_driver(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656void serio_rescan(struct serio *serio)
657{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500658 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700660EXPORT_SYMBOL(serio_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662void serio_reconnect(struct serio *serio)
663{
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400664 serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700666EXPORT_SYMBOL(serio_reconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/*
669 * Submits register request to kseriod for subsequent execution.
670 * Note that port registration is always asynchronous.
671 */
672void __serio_register_port(struct serio *serio, struct module *owner)
673{
674 serio_init_port(serio);
675 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
676}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700677EXPORT_SYMBOL(__serio_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/*
680 * Synchronously unregisters serio port.
681 */
682void serio_unregister_port(struct serio *serio)
683{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500684 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 serio_disconnect_port(serio);
686 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500687 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700689EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700692 * Safely unregisters child port if one is present.
693 */
694void serio_unregister_child_port(struct serio *serio)
695{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500696 mutex_lock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700697 if (serio->child) {
698 serio_disconnect_port(serio->child);
699 serio_destroy_port(serio->child);
700 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500701 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700702}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700703EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/*
707 * Serio driver operations
708 */
709
710static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
711{
712 struct serio_driver *driver = to_serio_driver(drv);
713 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
714}
715
716static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
717{
718 struct serio_driver *serio_drv = to_serio_driver(drv);
719 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
720}
721
722static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
723{
724 struct serio_driver *serio_drv = to_serio_driver(drv);
725 int retval;
726
727 retval = count;
728 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700729 serio_drv->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700731 serio_drv->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else {
733 retval = -EINVAL;
734 }
735
736 return retval;
737}
738
739
740static struct driver_attribute serio_driver_attrs[] = {
741 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
742 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
743 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
744 __ATTR_NULL
745};
746
747static int serio_driver_probe(struct device *dev)
748{
749 struct serio *serio = to_serio_port(dev);
750 struct serio_driver *drv = to_serio_driver(dev->driver);
751
Linus Torvalds3c803e82005-06-27 17:49:45 -0700752 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static int serio_driver_remove(struct device *dev)
756{
757 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvalds3c803e82005-06-27 17:49:45 -0700759 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 0;
761}
762
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500763static void serio_cleanup(struct serio *serio)
764{
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400765 mutex_lock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500766 if (serio->drv && serio->drv->cleanup)
767 serio->drv->cleanup(serio);
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400768 mutex_unlock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500769}
770
771static void serio_shutdown(struct device *dev)
772{
773 struct serio *serio = to_serio_port(dev);
774
775 serio_cleanup(serio);
776}
777
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500778static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400779{
780 int error;
781
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500782 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400783 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800784 pr_warning("driver_attach() failed for %s with error %d\n",
785 drv->driver.name, error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400786}
787
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800788int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700790 bool manual_bind = drv->manual_bind;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500791 int error;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800794 drv->driver.owner = owner;
795 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500797 /*
798 * Temporarily disable automatic binding because probing
799 * takes long time and we are better off doing it in kseriod
800 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700801 drv->manual_bind = true;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500802
803 error = driver_register(&drv->driver);
804 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800805 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500806 drv->driver.name, error);
807 return error;
808 }
809
810 /*
811 * Restore original bind mode and let kseriod bind the
812 * driver to free ports
813 */
814 if (!manual_bind) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700815 drv->manual_bind = false;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500816 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
817 if (error) {
818 driver_unregister(&drv->driver);
819 return error;
820 }
821 }
822
823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700825EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827void serio_unregister_driver(struct serio_driver *drv)
828{
829 struct serio *serio;
830
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500831 mutex_lock(&serio_mutex);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400832
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700833 drv->manual_bind = true; /* so serio_find_driver ignores it */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400834 serio_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836start_over:
837 list_for_each_entry(serio, &serio_list, node) {
838 if (serio->drv == drv) {
839 serio_disconnect_port(serio);
840 serio_find_driver(serio);
841 /* we could've deleted some ports, restart */
842 goto start_over;
843 }
844 }
845
846 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500847 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700849EXPORT_SYMBOL(serio_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 serio_pause_rx(serio);
854 serio->drv = drv;
855 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858static int serio_bus_match(struct device *dev, struct device_driver *drv)
859{
860 struct serio *serio = to_serio_port(dev);
861 struct serio_driver *serio_drv = to_serio_driver(drv);
862
863 if (serio->manual_bind || serio_drv->manual_bind)
864 return 0;
865
866 return serio_match_port(serio_drv->id_table, serio);
867}
868
869#ifdef CONFIG_HOTPLUG
870
Kay Sievers312c0042005-11-16 09:00:00 +0100871#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500872 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200873 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500874 if (err) \
875 return err; \
876 } while (0)
877
Kay Sievers7eff2e72007-08-14 15:15:12 +0200878static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (!dev)
883 return -ENODEV;
884
885 serio = to_serio_port(dev);
886
Kay Sievers312c0042005-11-16 09:00:00 +0100887 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
888 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
889 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
890 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
891 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500892 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return 0;
895}
Kay Sievers312c0042005-11-16 09:00:00 +0100896#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898#else
899
Kay Sievers7eff2e72007-08-14 15:15:12 +0200900static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 return -ENODEV;
903}
904
905#endif /* CONFIG_HOTPLUG */
906
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500907#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700908static int serio_suspend(struct device *dev)
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500909{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700910 struct serio *serio = to_serio_port(dev);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500911
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700912 serio_cleanup(serio);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500913
914 return 0;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static int serio_resume(struct device *dev)
918{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700919 struct serio *serio = to_serio_port(dev);
920
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400921 /*
922 * Driver reconnect can take a while, so better let kseriod
923 * deal with it.
924 */
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700925 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 return 0;
928}
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700929
930static const struct dev_pm_ops serio_pm_ops = {
931 .suspend = serio_suspend,
932 .resume = serio_resume,
933 .poweroff = serio_suspend,
934 .restore = serio_resume,
935};
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500936#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500938/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939int serio_open(struct serio *serio, struct serio_driver *drv)
940{
941 serio_set_drv(serio, drv);
942
943 if (serio->open && serio->open(serio)) {
944 serio_set_drv(serio, NULL);
945 return -1;
946 }
947 return 0;
948}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700949EXPORT_SYMBOL(serio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500951/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952void serio_close(struct serio *serio)
953{
954 if (serio->close)
955 serio->close(serio);
956
957 serio_set_drv(serio, NULL);
958}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700959EXPORT_SYMBOL(serio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100962 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 unsigned long flags;
965 irqreturn_t ret = IRQ_NONE;
966
967 spin_lock_irqsave(&serio->lock, flags);
968
969 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100970 ret = serio->drv->interrupt(serio, data, dfl);
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800971 } else if (!dfl && device_is_registered(&serio->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 serio_rescan(serio);
973 ret = IRQ_HANDLED;
974 }
975
976 spin_unlock_irqrestore(&serio->lock, flags);
977
978 return ret;
979}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700980EXPORT_SYMBOL(serio_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Marton Nemeth1ea2a692006-11-02 23:27:21 -0500982static struct bus_type serio_bus = {
983 .name = "serio",
984 .dev_attrs = serio_device_attrs,
985 .drv_attrs = serio_driver_attrs,
986 .match = serio_bus_match,
987 .uevent = serio_uevent,
988 .probe = serio_driver_probe,
989 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500990 .shutdown = serio_shutdown,
991#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700992 .pm = &serio_pm_ops,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500993#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -0500994};
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static int __init serio_init(void)
997{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400998 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Randy Dunlap73b59a32006-07-19 01:14:55 -04001000 error = bus_register(&serio_bus);
1001 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001002 pr_err("Failed to register serio bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001003 return error;
1004 }
1005
1006 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1007 if (IS_ERR(serio_task)) {
1008 bus_unregister(&serio_bus);
1009 error = PTR_ERR(serio_task);
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001010 pr_err("Failed to start kseriod, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001011 return error;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return 0;
1015}
1016
1017static void __exit serio_exit(void)
1018{
1019 bus_unregister(&serio_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -07001020 kthread_stop(serio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001023subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024module_exit(serio_exit);