Staging: wlan-ng: Explicitly set some fields in cfg80211 interface
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / xen-kbdfront.c
blobebb11907d402befd3a777e21e5bed63a0e66dfaf
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 <linux/slab.h>
26 #include <asm/xen/hypervisor.h>
28 #include <xen/xen.h>
29 #include <xen/events.h>
30 #include <xen/page.h>
31 #include <xen/interface/io/fbif.h>
32 #include <xen/interface/io/kbdif.h>
33 #include <xen/xenbus.h>
35 struct xenkbd_info {
36 struct input_dev *kbd;
37 struct input_dev *ptr;
38 struct xenkbd_page *page;
39 int irq;
40 struct xenbus_device *xbdev;
41 char phys[32];
44 static int xenkbd_remove(struct xenbus_device *);
45 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
46 static void xenkbd_disconnect_backend(struct xenkbd_info *);
49 * Note: if you need to send out events, see xenfb_do_update() for how
50 * to do that.
53 static irqreturn_t input_handler(int rq, void *dev_id)
55 struct xenkbd_info *info = dev_id;
56 struct xenkbd_page *page = info->page;
57 __u32 cons, prod;
59 prod = page->in_prod;
60 if (prod == page->in_cons)
61 return IRQ_HANDLED;
62 rmb(); /* ensure we see ring contents up to prod */
63 for (cons = page->in_cons; cons != prod; cons++) {
64 union xenkbd_in_event *event;
65 struct input_dev *dev;
66 event = &XENKBD_IN_RING_REF(page, cons);
68 dev = info->ptr;
69 switch (event->type) {
70 case XENKBD_TYPE_MOTION:
71 input_report_rel(dev, REL_X, event->motion.rel_x);
72 input_report_rel(dev, REL_Y, event->motion.rel_y);
73 if (event->motion.rel_z)
74 input_report_rel(dev, REL_WHEEL,
75 -event->motion.rel_z);
76 break;
77 case XENKBD_TYPE_KEY:
78 dev = NULL;
79 if (test_bit(event->key.keycode, info->kbd->keybit))
80 dev = info->kbd;
81 if (test_bit(event->key.keycode, info->ptr->keybit))
82 dev = info->ptr;
83 if (dev)
84 input_report_key(dev, event->key.keycode,
85 event->key.pressed);
86 else
87 printk(KERN_WARNING
88 "xenkbd: unhandled keycode 0x%x\n",
89 event->key.keycode);
90 break;
91 case XENKBD_TYPE_POS:
92 input_report_abs(dev, ABS_X, event->pos.abs_x);
93 input_report_abs(dev, ABS_Y, event->pos.abs_y);
94 if (event->pos.rel_z)
95 input_report_rel(dev, REL_WHEEL,
96 -event->pos.rel_z);
97 break;
99 if (dev)
100 input_sync(dev);
102 mb(); /* ensure we got ring contents */
103 page->in_cons = cons;
104 notify_remote_via_irq(info->irq);
106 return IRQ_HANDLED;
109 static int __devinit xenkbd_probe(struct xenbus_device *dev,
110 const struct xenbus_device_id *id)
112 int ret, i;
113 struct xenkbd_info *info;
114 struct input_dev *kbd, *ptr;
116 info = kzalloc(sizeof(*info), GFP_KERNEL);
117 if (!info) {
118 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
119 return -ENOMEM;
121 dev_set_drvdata(&dev->dev, info);
122 info->xbdev = dev;
123 info->irq = -1;
124 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
126 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
127 if (!info->page)
128 goto error_nomem;
130 /* keyboard */
131 kbd = input_allocate_device();
132 if (!kbd)
133 goto error_nomem;
134 kbd->name = "Xen Virtual Keyboard";
135 kbd->phys = info->phys;
136 kbd->id.bustype = BUS_PCI;
137 kbd->id.vendor = 0x5853;
138 kbd->id.product = 0xffff;
139 kbd->evbit[0] = BIT(EV_KEY);
140 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
141 set_bit(i, kbd->keybit);
142 for (i = KEY_OK; i < KEY_MAX; i++)
143 set_bit(i, kbd->keybit);
145 ret = input_register_device(kbd);
146 if (ret) {
147 input_free_device(kbd);
148 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
149 goto error;
151 info->kbd = kbd;
153 /* pointing device */
154 ptr = input_allocate_device();
155 if (!ptr)
156 goto error_nomem;
157 ptr->name = "Xen Virtual Pointer";
158 ptr->phys = info->phys;
159 ptr->id.bustype = BUS_PCI;
160 ptr->id.vendor = 0x5853;
161 ptr->id.product = 0xfffe;
162 ptr->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
163 for (i = BTN_LEFT; i <= BTN_TASK; i++)
164 set_bit(i, ptr->keybit);
165 ptr->relbit[0] = BIT(REL_X) | BIT(REL_Y) | BIT(REL_WHEEL);
166 input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
167 input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
169 ret = input_register_device(ptr);
170 if (ret) {
171 input_free_device(ptr);
172 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
173 goto error;
175 info->ptr = ptr;
177 ret = xenkbd_connect_backend(dev, info);
178 if (ret < 0)
179 goto error;
181 return 0;
183 error_nomem:
184 ret = -ENOMEM;
185 xenbus_dev_fatal(dev, ret, "allocating device memory");
186 error:
187 xenkbd_remove(dev);
188 return ret;
191 static int xenkbd_resume(struct xenbus_device *dev)
193 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
195 xenkbd_disconnect_backend(info);
196 memset(info->page, 0, PAGE_SIZE);
197 return xenkbd_connect_backend(dev, info);
200 static int xenkbd_remove(struct xenbus_device *dev)
202 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
204 xenkbd_disconnect_backend(info);
205 if (info->kbd)
206 input_unregister_device(info->kbd);
207 if (info->ptr)
208 input_unregister_device(info->ptr);
209 free_page((unsigned long)info->page);
210 kfree(info);
211 return 0;
214 static int xenkbd_connect_backend(struct xenbus_device *dev,
215 struct xenkbd_info *info)
217 int ret, evtchn;
218 struct xenbus_transaction xbt;
220 ret = xenbus_alloc_evtchn(dev, &evtchn);
221 if (ret)
222 return ret;
223 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
224 0, dev->devicetype, info);
225 if (ret < 0) {
226 xenbus_free_evtchn(dev, evtchn);
227 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
228 return ret;
230 info->irq = ret;
232 again:
233 ret = xenbus_transaction_start(&xbt);
234 if (ret) {
235 xenbus_dev_fatal(dev, ret, "starting transaction");
236 return ret;
238 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
239 virt_to_mfn(info->page));
240 if (ret)
241 goto error_xenbus;
242 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
243 evtchn);
244 if (ret)
245 goto error_xenbus;
246 ret = xenbus_transaction_end(xbt, 0);
247 if (ret) {
248 if (ret == -EAGAIN)
249 goto again;
250 xenbus_dev_fatal(dev, ret, "completing transaction");
251 return ret;
254 xenbus_switch_state(dev, XenbusStateInitialised);
255 return 0;
257 error_xenbus:
258 xenbus_transaction_end(xbt, 1);
259 xenbus_dev_fatal(dev, ret, "writing xenstore");
260 return ret;
263 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
265 if (info->irq >= 0)
266 unbind_from_irqhandler(info->irq, info);
267 info->irq = -1;
270 static void xenkbd_backend_changed(struct xenbus_device *dev,
271 enum xenbus_state backend_state)
273 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
274 int ret, val;
276 switch (backend_state) {
277 case XenbusStateInitialising:
278 case XenbusStateInitialised:
279 case XenbusStateUnknown:
280 case XenbusStateClosed:
281 break;
283 case XenbusStateInitWait:
284 InitWait:
285 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
286 "feature-abs-pointer", "%d", &val);
287 if (ret < 0)
288 val = 0;
289 if (val) {
290 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
291 "request-abs-pointer", "1");
292 if (ret)
293 printk(KERN_WARNING
294 "xenkbd: can't request abs-pointer");
296 xenbus_switch_state(dev, XenbusStateConnected);
297 break;
299 case XenbusStateConnected:
301 * Work around xenbus race condition: If backend goes
302 * through InitWait to Connected fast enough, we can
303 * get Connected twice here.
305 if (dev->state != XenbusStateConnected)
306 goto InitWait; /* no InitWait seen yet, fudge it */
308 /* Set input abs params to match backend screen res */
309 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
310 "width", "%d", &val) > 0)
311 input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
313 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
314 "height", "%d", &val) > 0)
315 input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
317 break;
319 case XenbusStateClosing:
320 xenbus_frontend_closed(dev);
321 break;
325 static const struct xenbus_device_id xenkbd_ids[] = {
326 { "vkbd" },
327 { "" }
330 static struct xenbus_driver xenkbd_driver = {
331 .name = "vkbd",
332 .owner = THIS_MODULE,
333 .ids = xenkbd_ids,
334 .probe = xenkbd_probe,
335 .remove = xenkbd_remove,
336 .resume = xenkbd_resume,
337 .otherend_changed = xenkbd_backend_changed,
340 static int __init xenkbd_init(void)
342 if (!xen_pv_domain())
343 return -ENODEV;
345 /* Nothing to do if running in dom0. */
346 if (xen_initial_domain())
347 return -ENODEV;
349 return xenbus_register_frontend(&xenkbd_driver);
352 static void __exit xenkbd_cleanup(void)
354 xenbus_unregister_driver(&xenkbd_driver);
357 module_init(xenkbd_init);
358 module_exit(xenkbd_cleanup);
360 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
361 MODULE_LICENSE("GPL");
362 MODULE_ALIAS("xen:vkbd");