fix asm-alpha/types.h breakage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / xen-kbdfront.c
blob0f47f4697cdf4f97f1b591e2677c40f070cfccf7
1 /*
2 * Xen para-virtual input device
4 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
7 * Based on linux/drivers/input/mouse/sermouse.c
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
11 * more details.
15 * TODO:
17 * Switch to grant tables together with xen-fbfront.c.
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <asm/xen/hypervisor.h>
25 #include <xen/events.h>
26 #include <xen/page.h>
27 #include <xen/interface/io/fbif.h>
28 #include <xen/interface/io/kbdif.h>
29 #include <xen/xenbus.h>
31 struct xenkbd_info {
32 struct input_dev *kbd;
33 struct input_dev *ptr;
34 struct xenkbd_page *page;
35 int irq;
36 struct xenbus_device *xbdev;
37 char phys[32];
40 static int xenkbd_remove(struct xenbus_device *);
41 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
42 static void xenkbd_disconnect_backend(struct xenkbd_info *);
45 * Note: if you need to send out events, see xenfb_do_update() for how
46 * to do that.
49 static irqreturn_t input_handler(int rq, void *dev_id)
51 struct xenkbd_info *info = dev_id;
52 struct xenkbd_page *page = info->page;
53 __u32 cons, prod;
55 prod = page->in_prod;
56 if (prod == page->in_cons)
57 return IRQ_HANDLED;
58 rmb(); /* ensure we see ring contents up to prod */
59 for (cons = page->in_cons; cons != prod; cons++) {
60 union xenkbd_in_event *event;
61 struct input_dev *dev;
62 event = &XENKBD_IN_RING_REF(page, cons);
64 dev = info->ptr;
65 switch (event->type) {
66 case XENKBD_TYPE_MOTION:
67 input_report_rel(dev, REL_X, event->motion.rel_x);
68 input_report_rel(dev, REL_Y, event->motion.rel_y);
69 break;
70 case XENKBD_TYPE_KEY:
71 dev = NULL;
72 if (test_bit(event->key.keycode, info->kbd->keybit))
73 dev = info->kbd;
74 if (test_bit(event->key.keycode, info->ptr->keybit))
75 dev = info->ptr;
76 if (dev)
77 input_report_key(dev, event->key.keycode,
78 event->key.pressed);
79 else
80 printk(KERN_WARNING
81 "xenkbd: unhandled keycode 0x%x\n",
82 event->key.keycode);
83 break;
84 case XENKBD_TYPE_POS:
85 input_report_abs(dev, ABS_X, event->pos.abs_x);
86 input_report_abs(dev, ABS_Y, event->pos.abs_y);
87 break;
89 if (dev)
90 input_sync(dev);
92 mb(); /* ensure we got ring contents */
93 page->in_cons = cons;
94 notify_remote_via_irq(info->irq);
96 return IRQ_HANDLED;
99 static int __devinit xenkbd_probe(struct xenbus_device *dev,
100 const struct xenbus_device_id *id)
102 int ret, i;
103 struct xenkbd_info *info;
104 struct input_dev *kbd, *ptr;
106 info = kzalloc(sizeof(*info), GFP_KERNEL);
107 if (!info) {
108 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
109 return -ENOMEM;
111 dev->dev.driver_data = info;
112 info->xbdev = dev;
113 info->irq = -1;
114 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
116 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
117 if (!info->page)
118 goto error_nomem;
120 /* keyboard */
121 kbd = input_allocate_device();
122 if (!kbd)
123 goto error_nomem;
124 kbd->name = "Xen Virtual Keyboard";
125 kbd->phys = info->phys;
126 kbd->id.bustype = BUS_PCI;
127 kbd->id.vendor = 0x5853;
128 kbd->id.product = 0xffff;
129 kbd->evbit[0] = BIT(EV_KEY);
130 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
131 set_bit(i, kbd->keybit);
132 for (i = KEY_OK; i < KEY_MAX; i++)
133 set_bit(i, kbd->keybit);
135 ret = input_register_device(kbd);
136 if (ret) {
137 input_free_device(kbd);
138 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
139 goto error;
141 info->kbd = kbd;
143 /* pointing device */
144 ptr = input_allocate_device();
145 if (!ptr)
146 goto error_nomem;
147 ptr->name = "Xen Virtual Pointer";
148 ptr->phys = info->phys;
149 ptr->id.bustype = BUS_PCI;
150 ptr->id.vendor = 0x5853;
151 ptr->id.product = 0xfffe;
152 ptr->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
153 for (i = BTN_LEFT; i <= BTN_TASK; i++)
154 set_bit(i, ptr->keybit);
155 ptr->relbit[0] = BIT(REL_X) | BIT(REL_Y);
156 input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
157 input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
159 ret = input_register_device(ptr);
160 if (ret) {
161 input_free_device(ptr);
162 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
163 goto error;
165 info->ptr = ptr;
167 ret = xenkbd_connect_backend(dev, info);
168 if (ret < 0)
169 goto error;
171 return 0;
173 error_nomem:
174 ret = -ENOMEM;
175 xenbus_dev_fatal(dev, ret, "allocating device memory");
176 error:
177 xenkbd_remove(dev);
178 return ret;
181 static int xenkbd_resume(struct xenbus_device *dev)
183 struct xenkbd_info *info = dev->dev.driver_data;
185 xenkbd_disconnect_backend(info);
186 memset(info->page, 0, PAGE_SIZE);
187 return xenkbd_connect_backend(dev, info);
190 static int xenkbd_remove(struct xenbus_device *dev)
192 struct xenkbd_info *info = dev->dev.driver_data;
194 xenkbd_disconnect_backend(info);
195 if (info->kbd)
196 input_unregister_device(info->kbd);
197 if (info->ptr)
198 input_unregister_device(info->ptr);
199 free_page((unsigned long)info->page);
200 kfree(info);
201 return 0;
204 static int xenkbd_connect_backend(struct xenbus_device *dev,
205 struct xenkbd_info *info)
207 int ret, evtchn;
208 struct xenbus_transaction xbt;
210 ret = xenbus_alloc_evtchn(dev, &evtchn);
211 if (ret)
212 return ret;
213 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
214 0, dev->devicetype, info);
215 if (ret < 0) {
216 xenbus_free_evtchn(dev, evtchn);
217 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
218 return ret;
220 info->irq = ret;
222 again:
223 ret = xenbus_transaction_start(&xbt);
224 if (ret) {
225 xenbus_dev_fatal(dev, ret, "starting transaction");
226 return ret;
228 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
229 virt_to_mfn(info->page));
230 if (ret)
231 goto error_xenbus;
232 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
233 evtchn);
234 if (ret)
235 goto error_xenbus;
236 ret = xenbus_transaction_end(xbt, 0);
237 if (ret) {
238 if (ret == -EAGAIN)
239 goto again;
240 xenbus_dev_fatal(dev, ret, "completing transaction");
241 return ret;
244 xenbus_switch_state(dev, XenbusStateInitialised);
245 return 0;
247 error_xenbus:
248 xenbus_transaction_end(xbt, 1);
249 xenbus_dev_fatal(dev, ret, "writing xenstore");
250 return ret;
253 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
255 if (info->irq >= 0)
256 unbind_from_irqhandler(info->irq, info);
257 info->irq = -1;
260 static void xenkbd_backend_changed(struct xenbus_device *dev,
261 enum xenbus_state backend_state)
263 struct xenkbd_info *info = dev->dev.driver_data;
264 int ret, val;
266 switch (backend_state) {
267 case XenbusStateInitialising:
268 case XenbusStateInitialised:
269 case XenbusStateUnknown:
270 case XenbusStateClosed:
271 break;
273 case XenbusStateInitWait:
274 InitWait:
275 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
276 "feature-abs-pointer", "%d", &val);
277 if (ret < 0)
278 val = 0;
279 if (val) {
280 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
281 "request-abs-pointer", "1");
282 if (ret)
283 printk(KERN_WARNING
284 "xenkbd: can't request abs-pointer");
286 xenbus_switch_state(dev, XenbusStateConnected);
287 break;
289 case XenbusStateConnected:
291 * Work around xenbus race condition: If backend goes
292 * through InitWait to Connected fast enough, we can
293 * get Connected twice here.
295 if (dev->state != XenbusStateConnected)
296 goto InitWait; /* no InitWait seen yet, fudge it */
297 break;
299 case XenbusStateClosing:
300 xenbus_frontend_closed(dev);
301 break;
305 static struct xenbus_device_id xenkbd_ids[] = {
306 { "vkbd" },
307 { "" }
310 static struct xenbus_driver xenkbd = {
311 .name = "vkbd",
312 .owner = THIS_MODULE,
313 .ids = xenkbd_ids,
314 .probe = xenkbd_probe,
315 .remove = xenkbd_remove,
316 .resume = xenkbd_resume,
317 .otherend_changed = xenkbd_backend_changed,
320 static int __init xenkbd_init(void)
322 if (!is_running_on_xen())
323 return -ENODEV;
325 /* Nothing to do if running in dom0. */
326 if (is_initial_xendomain())
327 return -ENODEV;
329 return xenbus_register_frontend(&xenkbd);
332 static void __exit xenkbd_cleanup(void)
334 xenbus_unregister_driver(&xenkbd);
337 module_init(xenkbd_init);
338 module_exit(xenkbd_cleanup);
340 MODULE_LICENSE("GPL");