]>
Commit | Line | Data |
---|---|---|
4ee36dc0 MA |
1 | /* |
2 | * Xen para-virtual input device | |
3 | * | |
4 | * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com> | |
5 | * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com> | |
6 | * | |
7 | * Based on linux/drivers/input/mouse/sermouse.c | |
8 | * | |
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. | |
12 | */ | |
13 | ||
14 | /* | |
15 | * TODO: | |
16 | * | |
17 | * Switch to grant tables together with xen-fbfront.c. | |
18 | */ | |
19 | ||
da0c4901 JP |
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
21 | ||
4ee36dc0 MA |
22 | #include <linux/kernel.h> |
23 | #include <linux/errno.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/input.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
1ccbf534 | 27 | |
4ee36dc0 | 28 | #include <asm/xen/hypervisor.h> |
1ccbf534 JF |
29 | |
30 | #include <xen/xen.h> | |
4ee36dc0 MA |
31 | #include <xen/events.h> |
32 | #include <xen/page.h> | |
33 | #include <xen/interface/io/fbif.h> | |
34 | #include <xen/interface/io/kbdif.h> | |
35 | #include <xen/xenbus.h> | |
36 | ||
37 | struct xenkbd_info { | |
38 | struct input_dev *kbd; | |
39 | struct input_dev *ptr; | |
40 | struct xenkbd_page *page; | |
41 | int irq; | |
42 | struct xenbus_device *xbdev; | |
43 | char phys[32]; | |
44 | }; | |
45 | ||
46 | static int xenkbd_remove(struct xenbus_device *); | |
47 | static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *); | |
48 | static void xenkbd_disconnect_backend(struct xenkbd_info *); | |
49 | ||
50 | /* | |
51 | * Note: if you need to send out events, see xenfb_do_update() for how | |
52 | * to do that. | |
53 | */ | |
54 | ||
55 | static irqreturn_t input_handler(int rq, void *dev_id) | |
56 | { | |
57 | struct xenkbd_info *info = dev_id; | |
58 | struct xenkbd_page *page = info->page; | |
59 | __u32 cons, prod; | |
60 | ||
61 | prod = page->in_prod; | |
62 | if (prod == page->in_cons) | |
63 | return IRQ_HANDLED; | |
64 | rmb(); /* ensure we see ring contents up to prod */ | |
65 | for (cons = page->in_cons; cons != prod; cons++) { | |
66 | union xenkbd_in_event *event; | |
67 | struct input_dev *dev; | |
68 | event = &XENKBD_IN_RING_REF(page, cons); | |
69 | ||
70 | dev = info->ptr; | |
71 | switch (event->type) { | |
72 | case XENKBD_TYPE_MOTION: | |
73 | input_report_rel(dev, REL_X, event->motion.rel_x); | |
74 | input_report_rel(dev, REL_Y, event->motion.rel_y); | |
6ba0e7b3 MA |
75 | if (event->motion.rel_z) |
76 | input_report_rel(dev, REL_WHEEL, | |
77 | -event->motion.rel_z); | |
4ee36dc0 MA |
78 | break; |
79 | case XENKBD_TYPE_KEY: | |
80 | dev = NULL; | |
81 | if (test_bit(event->key.keycode, info->kbd->keybit)) | |
82 | dev = info->kbd; | |
83 | if (test_bit(event->key.keycode, info->ptr->keybit)) | |
84 | dev = info->ptr; | |
85 | if (dev) | |
86 | input_report_key(dev, event->key.keycode, | |
87 | event->key.pressed); | |
88 | else | |
da0c4901 JP |
89 | pr_warning("unhandled keycode 0x%x\n", |
90 | event->key.keycode); | |
4ee36dc0 MA |
91 | break; |
92 | case XENKBD_TYPE_POS: | |
93 | input_report_abs(dev, ABS_X, event->pos.abs_x); | |
94 | input_report_abs(dev, ABS_Y, event->pos.abs_y); | |
6ba0e7b3 MA |
95 | if (event->pos.rel_z) |
96 | input_report_rel(dev, REL_WHEEL, | |
97 | -event->pos.rel_z); | |
4ee36dc0 MA |
98 | break; |
99 | } | |
100 | if (dev) | |
101 | input_sync(dev); | |
102 | } | |
103 | mb(); /* ensure we got ring contents */ | |
104 | page->in_cons = cons; | |
105 | notify_remote_via_irq(info->irq); | |
106 | ||
107 | return IRQ_HANDLED; | |
108 | } | |
109 | ||
110 | static int __devinit xenkbd_probe(struct xenbus_device *dev, | |
111 | const struct xenbus_device_id *id) | |
112 | { | |
113 | int ret, i; | |
114 | struct xenkbd_info *info; | |
115 | struct input_dev *kbd, *ptr; | |
116 | ||
117 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
118 | if (!info) { | |
119 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); | |
120 | return -ENOMEM; | |
121 | } | |
1b713e00 | 122 | dev_set_drvdata(&dev->dev, info); |
4ee36dc0 MA |
123 | info->xbdev = dev; |
124 | info->irq = -1; | |
125 | snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename); | |
126 | ||
127 | info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); | |
128 | if (!info->page) | |
129 | goto error_nomem; | |
130 | ||
131 | /* keyboard */ | |
132 | kbd = input_allocate_device(); | |
133 | if (!kbd) | |
134 | goto error_nomem; | |
135 | kbd->name = "Xen Virtual Keyboard"; | |
136 | kbd->phys = info->phys; | |
137 | kbd->id.bustype = BUS_PCI; | |
138 | kbd->id.vendor = 0x5853; | |
139 | kbd->id.product = 0xffff; | |
140 | kbd->evbit[0] = BIT(EV_KEY); | |
141 | for (i = KEY_ESC; i < KEY_UNKNOWN; i++) | |
142 | set_bit(i, kbd->keybit); | |
143 | for (i = KEY_OK; i < KEY_MAX; i++) | |
144 | set_bit(i, kbd->keybit); | |
145 | ||
146 | ret = input_register_device(kbd); | |
147 | if (ret) { | |
148 | input_free_device(kbd); | |
149 | xenbus_dev_fatal(dev, ret, "input_register_device(kbd)"); | |
150 | goto error; | |
151 | } | |
152 | info->kbd = kbd; | |
153 | ||
154 | /* pointing device */ | |
155 | ptr = input_allocate_device(); | |
156 | if (!ptr) | |
157 | goto error_nomem; | |
158 | ptr->name = "Xen Virtual Pointer"; | |
159 | ptr->phys = info->phys; | |
160 | ptr->id.bustype = BUS_PCI; | |
161 | ptr->id.vendor = 0x5853; | |
162 | ptr->id.product = 0xfffe; | |
163 | ptr->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS); | |
164 | for (i = BTN_LEFT; i <= BTN_TASK; i++) | |
165 | set_bit(i, ptr->keybit); | |
6ba0e7b3 | 166 | ptr->relbit[0] = BIT(REL_X) | BIT(REL_Y) | BIT(REL_WHEEL); |
4ee36dc0 MA |
167 | input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0); |
168 | input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0); | |
169 | ||
170 | ret = input_register_device(ptr); | |
171 | if (ret) { | |
172 | input_free_device(ptr); | |
173 | xenbus_dev_fatal(dev, ret, "input_register_device(ptr)"); | |
174 | goto error; | |
175 | } | |
176 | info->ptr = ptr; | |
177 | ||
178 | ret = xenkbd_connect_backend(dev, info); | |
179 | if (ret < 0) | |
180 | goto error; | |
181 | ||
182 | return 0; | |
183 | ||
184 | error_nomem: | |
185 | ret = -ENOMEM; | |
186 | xenbus_dev_fatal(dev, ret, "allocating device memory"); | |
187 | error: | |
188 | xenkbd_remove(dev); | |
189 | return ret; | |
190 | } | |
191 | ||
192 | static int xenkbd_resume(struct xenbus_device *dev) | |
193 | { | |
1b713e00 | 194 | struct xenkbd_info *info = dev_get_drvdata(&dev->dev); |
4ee36dc0 MA |
195 | |
196 | xenkbd_disconnect_backend(info); | |
197 | memset(info->page, 0, PAGE_SIZE); | |
198 | return xenkbd_connect_backend(dev, info); | |
199 | } | |
200 | ||
201 | static int xenkbd_remove(struct xenbus_device *dev) | |
202 | { | |
1b713e00 | 203 | struct xenkbd_info *info = dev_get_drvdata(&dev->dev); |
4ee36dc0 MA |
204 | |
205 | xenkbd_disconnect_backend(info); | |
206 | if (info->kbd) | |
207 | input_unregister_device(info->kbd); | |
208 | if (info->ptr) | |
209 | input_unregister_device(info->ptr); | |
210 | free_page((unsigned long)info->page); | |
211 | kfree(info); | |
212 | return 0; | |
213 | } | |
214 | ||
215 | static int xenkbd_connect_backend(struct xenbus_device *dev, | |
216 | struct xenkbd_info *info) | |
217 | { | |
218 | int ret, evtchn; | |
219 | struct xenbus_transaction xbt; | |
220 | ||
221 | ret = xenbus_alloc_evtchn(dev, &evtchn); | |
222 | if (ret) | |
223 | return ret; | |
224 | ret = bind_evtchn_to_irqhandler(evtchn, input_handler, | |
225 | 0, dev->devicetype, info); | |
226 | if (ret < 0) { | |
227 | xenbus_free_evtchn(dev, evtchn); | |
228 | xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler"); | |
229 | return ret; | |
230 | } | |
231 | info->irq = ret; | |
232 | ||
233 | again: | |
234 | ret = xenbus_transaction_start(&xbt); | |
235 | if (ret) { | |
236 | xenbus_dev_fatal(dev, ret, "starting transaction"); | |
237 | return ret; | |
238 | } | |
239 | ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu", | |
240 | virt_to_mfn(info->page)); | |
241 | if (ret) | |
242 | goto error_xenbus; | |
243 | ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u", | |
244 | evtchn); | |
245 | if (ret) | |
246 | goto error_xenbus; | |
247 | ret = xenbus_transaction_end(xbt, 0); | |
248 | if (ret) { | |
249 | if (ret == -EAGAIN) | |
250 | goto again; | |
251 | xenbus_dev_fatal(dev, ret, "completing transaction"); | |
252 | return ret; | |
253 | } | |
254 | ||
255 | xenbus_switch_state(dev, XenbusStateInitialised); | |
256 | return 0; | |
257 | ||
258 | error_xenbus: | |
259 | xenbus_transaction_end(xbt, 1); | |
260 | xenbus_dev_fatal(dev, ret, "writing xenstore"); | |
261 | return ret; | |
262 | } | |
263 | ||
264 | static void xenkbd_disconnect_backend(struct xenkbd_info *info) | |
265 | { | |
266 | if (info->irq >= 0) | |
267 | unbind_from_irqhandler(info->irq, info); | |
268 | info->irq = -1; | |
269 | } | |
270 | ||
271 | static void xenkbd_backend_changed(struct xenbus_device *dev, | |
272 | enum xenbus_state backend_state) | |
273 | { | |
1b713e00 | 274 | struct xenkbd_info *info = dev_get_drvdata(&dev->dev); |
4ee36dc0 MA |
275 | int ret, val; |
276 | ||
277 | switch (backend_state) { | |
278 | case XenbusStateInitialising: | |
279 | case XenbusStateInitialised: | |
b78c9512 NI |
280 | case XenbusStateReconfiguring: |
281 | case XenbusStateReconfigured: | |
4ee36dc0 MA |
282 | case XenbusStateUnknown: |
283 | case XenbusStateClosed: | |
284 | break; | |
285 | ||
286 | case XenbusStateInitWait: | |
287 | InitWait: | |
288 | ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
289 | "feature-abs-pointer", "%d", &val); | |
290 | if (ret < 0) | |
291 | val = 0; | |
292 | if (val) { | |
293 | ret = xenbus_printf(XBT_NIL, info->xbdev->nodename, | |
294 | "request-abs-pointer", "1"); | |
295 | if (ret) | |
da0c4901 | 296 | pr_warning("can't request abs-pointer\n"); |
4ee36dc0 MA |
297 | } |
298 | xenbus_switch_state(dev, XenbusStateConnected); | |
299 | break; | |
300 | ||
301 | case XenbusStateConnected: | |
302 | /* | |
303 | * Work around xenbus race condition: If backend goes | |
304 | * through InitWait to Connected fast enough, we can | |
305 | * get Connected twice here. | |
306 | */ | |
307 | if (dev->state != XenbusStateConnected) | |
308 | goto InitWait; /* no InitWait seen yet, fudge it */ | |
e4dcff1f MA |
309 | |
310 | /* Set input abs params to match backend screen res */ | |
311 | if (xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
312 | "width", "%d", &val) > 0) | |
313 | input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0); | |
314 | ||
315 | if (xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
316 | "height", "%d", &val) > 0) | |
317 | input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0); | |
318 | ||
4ee36dc0 MA |
319 | break; |
320 | ||
321 | case XenbusStateClosing: | |
322 | xenbus_frontend_closed(dev); | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
c6d57093 | 327 | static const struct xenbus_device_id xenkbd_ids[] = { |
4ee36dc0 MA |
328 | { "vkbd" }, |
329 | { "" } | |
330 | }; | |
331 | ||
ffb78a26 | 332 | static struct xenbus_driver xenkbd_driver = { |
4ee36dc0 MA |
333 | .name = "vkbd", |
334 | .owner = THIS_MODULE, | |
335 | .ids = xenkbd_ids, | |
336 | .probe = xenkbd_probe, | |
337 | .remove = xenkbd_remove, | |
338 | .resume = xenkbd_resume, | |
339 | .otherend_changed = xenkbd_backend_changed, | |
340 | }; | |
341 | ||
342 | static int __init xenkbd_init(void) | |
343 | { | |
bee6ab53 | 344 | if (!xen_pv_domain()) |
4ee36dc0 MA |
345 | return -ENODEV; |
346 | ||
347 | /* Nothing to do if running in dom0. */ | |
6e833587 | 348 | if (xen_initial_domain()) |
4ee36dc0 MA |
349 | return -ENODEV; |
350 | ||
ffb78a26 | 351 | return xenbus_register_frontend(&xenkbd_driver); |
4ee36dc0 MA |
352 | } |
353 | ||
354 | static void __exit xenkbd_cleanup(void) | |
355 | { | |
ffb78a26 | 356 | xenbus_unregister_driver(&xenkbd_driver); |
4ee36dc0 MA |
357 | } |
358 | ||
359 | module_init(xenkbd_init); | |
360 | module_exit(xenkbd_cleanup); | |
361 | ||
1e892c95 | 362 | MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend"); |
4ee36dc0 | 363 | MODULE_LICENSE("GPL"); |
1e892c95 | 364 | MODULE_ALIAS("xen:vkbd"); |