aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/misc')
-rw-r--r--drivers/input/misc/Kconfig10
-rw-r--r--drivers/input/misc/Makefile1
-rw-r--r--drivers/input/misc/uinput.c325
-rw-r--r--drivers/input/misc/wistron_btns.c561
4 files changed, 742 insertions, 155 deletions
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index b3eaac1b35b6..07813fc0523f 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -40,6 +40,16 @@ config INPUT_M68K_BEEP
40 tristate "M68k Beeper support" 40 tristate "M68k Beeper support"
41 depends on M68K 41 depends on M68K
42 42
43config INPUT_WISTRON_BTNS
44 tristate "x86 Wistron laptop button interface"
45 depends on X86 && !X86_64
46 help
47 Say Y here for support of Winstron laptop button interface, used on
48 laptops of various brands, including Acer and Fujitsu-Siemens.
49
50 To compile this driver as a module, choose M here: the module will
51 be called wistron_btns.
52
43config INPUT_UINPUT 53config INPUT_UINPUT
44 tristate "User level driver support" 54 tristate "User level driver support"
45 help 55 help
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index f8d01c69f349..ce44cce01285 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -9,4 +9,5 @@ obj-$(CONFIG_INPUT_PCSPKR) += pcspkr.o
9obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o 9obj-$(CONFIG_INPUT_M68K_BEEP) += m68kspkr.o
10obj-$(CONFIG_INPUT_98SPKR) += 98spkr.o 10obj-$(CONFIG_INPUT_98SPKR) += 98spkr.o
11obj-$(CONFIG_INPUT_UINPUT) += uinput.o 11obj-$(CONFIG_INPUT_UINPUT) += uinput.o
12obj-$(CONFIG_INPUT_WISTRON_BTNS) += wistron_btns.o
12obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o 13obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
index 948c1cc01bc9..546ed9b4901d 100644
--- a/drivers/input/misc/uinput.c
+++ b/drivers/input/misc/uinput.c
@@ -92,24 +92,19 @@ static void uinput_request_done(struct uinput_device *udev, struct uinput_reques
92{ 92{
93 /* Mark slot as available */ 93 /* Mark slot as available */
94 udev->requests[request->id] = NULL; 94 udev->requests[request->id] = NULL;
95 wake_up_interruptible(&udev->requests_waitq); 95 wake_up(&udev->requests_waitq);
96 96
97 complete(&request->done); 97 complete(&request->done);
98} 98}
99 99
100static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request) 100static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
101{ 101{
102 int retval;
103
104 /* Tell our userspace app about this new request by queueing an input event */ 102 /* Tell our userspace app about this new request by queueing an input event */
105 uinput_dev_event(dev, EV_UINPUT, request->code, request->id); 103 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
106 104
107 /* Wait for the request to complete */ 105 /* Wait for the request to complete */
108 retval = wait_for_completion_interruptible(&request->done); 106 wait_for_completion(&request->done);
109 if (!retval) 107 return request->retval;
110 retval = request->retval;
111
112 return retval;
113} 108}
114 109
115static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect) 110static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
@@ -152,67 +147,62 @@ static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
152 return retval; 147 return retval;
153} 148}
154 149
155static int uinput_create_device(struct uinput_device *udev) 150static void uinput_destroy_device(struct uinput_device *udev)
156{ 151{
157 if (!udev->dev->name) { 152 const char *name, *phys;
158 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME); 153
159 return -EINVAL; 154 if (udev->dev) {
155 name = udev->dev->name;
156 phys = udev->dev->phys;
157 if (udev->state == UIST_CREATED)
158 input_unregister_device(udev->dev);
159 else
160 input_free_device(udev->dev);
161 kfree(name);
162 kfree(phys);
163 udev->dev = NULL;
160 } 164 }
161 165
162 udev->dev->event = uinput_dev_event; 166 udev->state = UIST_NEW_DEVICE;
163 udev->dev->upload_effect = uinput_dev_upload_effect;
164 udev->dev->erase_effect = uinput_dev_erase_effect;
165 udev->dev->private = udev;
166
167 init_waitqueue_head(&udev->waitq);
168
169 input_register_device(udev->dev);
170
171 set_bit(UIST_CREATED, &udev->state);
172
173 return 0;
174} 167}
175 168
176static int uinput_destroy_device(struct uinput_device *udev) 169static int uinput_create_device(struct uinput_device *udev)
177{ 170{
178 if (!test_bit(UIST_CREATED, &udev->state)) { 171 int error;
179 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME); 172
173 if (udev->state != UIST_SETUP_COMPLETE) {
174 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
180 return -EINVAL; 175 return -EINVAL;
181 } 176 }
182 177
183 input_unregister_device(udev->dev); 178 error = input_register_device(udev->dev);
179 if (error) {
180 uinput_destroy_device(udev);
181 return error;
182 }
184 183
185 clear_bit(UIST_CREATED, &udev->state); 184 udev->state = UIST_CREATED;
186 185
187 return 0; 186 return 0;
188} 187}
189 188
190static int uinput_open(struct inode *inode, struct file *file) 189static int uinput_open(struct inode *inode, struct file *file)
191{ 190{
192 struct uinput_device *newdev; 191 struct uinput_device *newdev;
193 struct input_dev *newinput;
194 192
195 newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL); 193 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
196 if (!newdev) 194 if (!newdev)
197 goto error; 195 return -ENOMEM;
198 memset(newdev, 0, sizeof(struct uinput_device)); 196
197 init_MUTEX(&newdev->sem);
199 spin_lock_init(&newdev->requests_lock); 198 spin_lock_init(&newdev->requests_lock);
200 init_waitqueue_head(&newdev->requests_waitq); 199 init_waitqueue_head(&newdev->requests_waitq);
201 200 init_waitqueue_head(&newdev->waitq);
202 newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL); 201 newdev->state = UIST_NEW_DEVICE;
203 if (!newinput)
204 goto cleanup;
205 memset(newinput, 0, sizeof(struct input_dev));
206
207 newdev->dev = newinput;
208 202
209 file->private_data = newdev; 203 file->private_data = newdev;
210 204
211 return 0; 205 return 0;
212cleanup:
213 kfree(newdev);
214error:
215 return -ENOMEM;
216} 206}
217 207
218static int uinput_validate_absbits(struct input_dev *dev) 208static int uinput_validate_absbits(struct input_dev *dev)
@@ -246,34 +236,55 @@ static int uinput_validate_absbits(struct input_dev *dev)
246 return retval; 236 return retval;
247} 237}
248 238
249static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count) 239static int uinput_allocate_device(struct uinput_device *udev)
240{
241 udev->dev = input_allocate_device();
242 if (!udev->dev)
243 return -ENOMEM;
244
245 udev->dev->event = uinput_dev_event;
246 udev->dev->upload_effect = uinput_dev_upload_effect;
247 udev->dev->erase_effect = uinput_dev_erase_effect;
248 udev->dev->private = udev;
249
250 return 0;
251}
252
253static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
250{ 254{
251 struct uinput_user_dev *user_dev; 255 struct uinput_user_dev *user_dev;
252 struct input_dev *dev; 256 struct input_dev *dev;
253 struct uinput_device *udev;
254 char *name; 257 char *name;
255 int size; 258 int size;
256 int retval; 259 int retval;
257 260
258 retval = count; 261 if (count != sizeof(struct uinput_user_dev))
262 return -EINVAL;
263
264 if (!udev->dev) {
265 retval = uinput_allocate_device(udev);
266 if (retval)
267 return retval;
268 }
259 269
260 udev = file->private_data;
261 dev = udev->dev; 270 dev = udev->dev;
262 271
263 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL); 272 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
264 if (!user_dev) { 273 if (!user_dev)
265 retval = -ENOMEM; 274 return -ENOMEM;
266 goto exit;
267 }
268 275
269 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) { 276 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
270 retval = -EFAULT; 277 retval = -EFAULT;
271 goto exit; 278 goto exit;
272 } 279 }
273 280
274 kfree(dev->name);
275
276 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1; 281 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
282 if (!size) {
283 retval = -EINVAL;
284 goto exit;
285 }
286
287 kfree(dev->name);
277 dev->name = name = kmalloc(size, GFP_KERNEL); 288 dev->name = name = kmalloc(size, GFP_KERNEL);
278 if (!name) { 289 if (!name) {
279 retval = -ENOMEM; 290 retval = -ENOMEM;
@@ -296,32 +307,50 @@ static int uinput_alloc_device(struct file *file, const char __user *buffer, siz
296 /* check if absmin/absmax/absfuzz/absflat are filled as 307 /* check if absmin/absmax/absfuzz/absflat are filled as
297 * told in Documentation/input/input-programming.txt */ 308 * told in Documentation/input/input-programming.txt */
298 if (test_bit(EV_ABS, dev->evbit)) { 309 if (test_bit(EV_ABS, dev->evbit)) {
299 int err = uinput_validate_absbits(dev); 310 retval = uinput_validate_absbits(dev);
300 if (err < 0) { 311 if (retval < 0)
301 retval = err; 312 goto exit;
302 kfree(dev->name);
303 }
304 } 313 }
305 314
306exit: 315 udev->state = UIST_SETUP_COMPLETE;
316 retval = count;
317
318 exit:
307 kfree(user_dev); 319 kfree(user_dev);
308 return retval; 320 return retval;
309} 321}
310 322
323static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
324{
325 struct input_event ev;
326
327 if (count != sizeof(struct input_event))
328 return -EINVAL;
329
330 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
331 return -EFAULT;
332
333 input_event(udev->dev, ev.type, ev.code, ev.value);
334
335 return sizeof(struct input_event);
336}
337
311static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 338static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
312{ 339{
313 struct uinput_device *udev = file->private_data; 340 struct uinput_device *udev = file->private_data;
341 int retval;
342
343 retval = down_interruptible(&udev->sem);
344 if (retval)
345 return retval;
314 346
315 if (test_bit(UIST_CREATED, &udev->state)) { 347 retval = udev->state == UIST_CREATED ?
316 struct input_event ev; 348 uinput_inject_event(udev, buffer, count) :
349 uinput_setup_device(udev, buffer, count);
317 350
318 if (copy_from_user(&ev, buffer, sizeof(struct input_event))) 351 up(&udev->sem);
319 return -EFAULT;
320 input_event(udev->dev, ev.type, ev.code, ev.value);
321 } else
322 count = uinput_alloc_device(file, buffer, count);
323 352
324 return count; 353 return retval;
325} 354}
326 355
327static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 356static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
@@ -329,28 +358,38 @@ static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count,
329 struct uinput_device *udev = file->private_data; 358 struct uinput_device *udev = file->private_data;
330 int retval = 0; 359 int retval = 0;
331 360
332 if (!test_bit(UIST_CREATED, &udev->state)) 361 if (udev->state != UIST_CREATED)
333 return -ENODEV; 362 return -ENODEV;
334 363
335 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK)) 364 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
336 return -EAGAIN; 365 return -EAGAIN;
337 366
338 retval = wait_event_interruptible(udev->waitq, 367 retval = wait_event_interruptible(udev->waitq,
339 udev->head != udev->tail || !test_bit(UIST_CREATED, &udev->state)); 368 udev->head != udev->tail || udev->state != UIST_CREATED);
340 if (retval) 369 if (retval)
341 return retval; 370 return retval;
342 371
343 if (!test_bit(UIST_CREATED, &udev->state)) 372 retval = down_interruptible(&udev->sem);
344 return -ENODEV; 373 if (retval)
374 return retval;
345 375
346 while ((udev->head != udev->tail) && 376 if (udev->state != UIST_CREATED) {
347 (retval + sizeof(struct input_event) <= count)) { 377 retval = -ENODEV;
348 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) 378 goto out;
349 return -EFAULT; 379 }
380
381 while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
382 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
383 retval = -EFAULT;
384 goto out;
385 }
350 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE; 386 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
351 retval += sizeof(struct input_event); 387 retval += sizeof(struct input_event);
352 } 388 }
353 389
390 out:
391 up(&udev->sem);
392
354 return retval; 393 return retval;
355} 394}
356 395
@@ -366,28 +405,30 @@ static unsigned int uinput_poll(struct file *file, poll_table *wait)
366 return 0; 405 return 0;
367} 406}
368 407
369static int uinput_burn_device(struct uinput_device *udev) 408static int uinput_release(struct inode *inode, struct file *file)
370{ 409{
371 if (test_bit(UIST_CREATED, &udev->state)) 410 struct uinput_device *udev = file->private_data;
372 uinput_destroy_device(udev);
373 411
374 kfree(udev->dev->name); 412 uinput_destroy_device(udev);
375 kfree(udev->dev->phys);
376 kfree(udev->dev);
377 kfree(udev); 413 kfree(udev);
378 414
379 return 0; 415 return 0;
380} 416}
381 417
382static int uinput_close(struct inode *inode, struct file *file) 418#define uinput_set_bit(_arg, _bit, _max) \
419({ \
420 int __ret = 0; \
421 if (udev->state == UIST_CREATED) \
422 __ret = -EINVAL; \
423 else if ((_arg) > (_max)) \
424 __ret = -EINVAL; \
425 else set_bit((_arg), udev->dev->_bit); \
426 __ret; \
427})
428
429static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
383{ 430{
384 uinput_burn_device(file->private_data); 431 int retval;
385 return 0;
386}
387
388static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
389{
390 int retval = 0;
391 struct uinput_device *udev; 432 struct uinput_device *udev;
392 void __user *p = (void __user *)arg; 433 void __user *p = (void __user *)arg;
393 struct uinput_ff_upload ff_up; 434 struct uinput_ff_upload ff_up;
@@ -398,19 +439,14 @@ static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd
398 439
399 udev = file->private_data; 440 udev = file->private_data;
400 441
401 /* device attributes can not be changed after the device is created */ 442 retval = down_interruptible(&udev->sem);
402 switch (cmd) { 443 if (retval)
403 case UI_SET_EVBIT: 444 return retval;
404 case UI_SET_KEYBIT: 445
405 case UI_SET_RELBIT: 446 if (!udev->dev) {
406 case UI_SET_ABSBIT: 447 retval = uinput_allocate_device(udev);
407 case UI_SET_MSCBIT: 448 if (retval)
408 case UI_SET_LEDBIT: 449 goto out;
409 case UI_SET_SNDBIT:
410 case UI_SET_FFBIT:
411 case UI_SET_PHYS:
412 if (test_bit(UIST_CREATED, &udev->state))
413 return -EINVAL;
414 } 450 }
415 451
416 switch (cmd) { 452 switch (cmd) {
@@ -419,74 +455,50 @@ static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd
419 break; 455 break;
420 456
421 case UI_DEV_DESTROY: 457 case UI_DEV_DESTROY:
422 retval = uinput_destroy_device(udev); 458 uinput_destroy_device(udev);
423 break; 459 break;
424 460
425 case UI_SET_EVBIT: 461 case UI_SET_EVBIT:
426 if (arg > EV_MAX) { 462 retval = uinput_set_bit(arg, evbit, EV_MAX);
427 retval = -EINVAL;
428 break;
429 }
430 set_bit(arg, udev->dev->evbit);
431 break; 463 break;
432 464
433 case UI_SET_KEYBIT: 465 case UI_SET_KEYBIT:
434 if (arg > KEY_MAX) { 466 retval = uinput_set_bit(arg, keybit, KEY_MAX);
435 retval = -EINVAL;
436 break;
437 }
438 set_bit(arg, udev->dev->keybit);
439 break; 467 break;
440 468
441 case UI_SET_RELBIT: 469 case UI_SET_RELBIT:
442 if (arg > REL_MAX) { 470 retval = uinput_set_bit(arg, relbit, REL_MAX);
443 retval = -EINVAL;
444 break;
445 }
446 set_bit(arg, udev->dev->relbit);
447 break; 471 break;
448 472
449 case UI_SET_ABSBIT: 473 case UI_SET_ABSBIT:
450 if (arg > ABS_MAX) { 474 retval = uinput_set_bit(arg, absbit, ABS_MAX);
451 retval = -EINVAL;
452 break;
453 }
454 set_bit(arg, udev->dev->absbit);
455 break; 475 break;
456 476
457 case UI_SET_MSCBIT: 477 case UI_SET_MSCBIT:
458 if (arg > MSC_MAX) { 478 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
459 retval = -EINVAL;
460 break;
461 }
462 set_bit(arg, udev->dev->mscbit);
463 break; 479 break;
464 480
465 case UI_SET_LEDBIT: 481 case UI_SET_LEDBIT:
466 if (arg > LED_MAX) { 482 retval = uinput_set_bit(arg, ledbit, LED_MAX);
467 retval = -EINVAL;
468 break;
469 }
470 set_bit(arg, udev->dev->ledbit);
471 break; 483 break;
472 484
473 case UI_SET_SNDBIT: 485 case UI_SET_SNDBIT:
474 if (arg > SND_MAX) { 486 retval = uinput_set_bit(arg, sndbit, SND_MAX);
475 retval = -EINVAL;
476 break;
477 }
478 set_bit(arg, udev->dev->sndbit);
479 break; 487 break;
480 488
481 case UI_SET_FFBIT: 489 case UI_SET_FFBIT:
482 if (arg > FF_MAX) { 490 retval = uinput_set_bit(arg, ffbit, FF_MAX);
483 retval = -EINVAL; 491 break;
484 break; 492
485 } 493 case UI_SET_SWBIT:
486 set_bit(arg, udev->dev->ffbit); 494 retval = uinput_set_bit(arg, swbit, SW_MAX);
487 break; 495 break;
488 496
489 case UI_SET_PHYS: 497 case UI_SET_PHYS:
498 if (udev->state == UIST_CREATED) {
499 retval = -EINVAL;
500 goto out;
501 }
490 length = strnlen_user(p, 1024); 502 length = strnlen_user(p, 1024);
491 if (length <= 0) { 503 if (length <= 0) {
492 retval = -EFAULT; 504 retval = -EFAULT;
@@ -575,23 +587,26 @@ static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd
575 default: 587 default:
576 retval = -EINVAL; 588 retval = -EINVAL;
577 } 589 }
590
591 out:
592 up(&udev->sem);
578 return retval; 593 return retval;
579} 594}
580 595
581static struct file_operations uinput_fops = { 596static struct file_operations uinput_fops = {
582 .owner = THIS_MODULE, 597 .owner = THIS_MODULE,
583 .open = uinput_open, 598 .open = uinput_open,
584 .release = uinput_close, 599 .release = uinput_release,
585 .read = uinput_read, 600 .read = uinput_read,
586 .write = uinput_write, 601 .write = uinput_write,
587 .poll = uinput_poll, 602 .poll = uinput_poll,
588 .ioctl = uinput_ioctl, 603 .unlocked_ioctl = uinput_ioctl,
589}; 604};
590 605
591static struct miscdevice uinput_misc = { 606static struct miscdevice uinput_misc = {
592 .fops = &uinput_fops, 607 .fops = &uinput_fops,
593 .minor = UINPUT_MINOR, 608 .minor = UINPUT_MINOR,
594 .name = UINPUT_NAME, 609 .name = UINPUT_NAME,
595}; 610};
596 611
597static int __init uinput_init(void) 612static int __init uinput_init(void)
diff --git a/drivers/input/misc/wistron_btns.c b/drivers/input/misc/wistron_btns.c
new file mode 100644
index 000000000000..49d0416a2a9a
--- /dev/null
+++ b/drivers/input/misc/wistron_btns.c
@@ -0,0 +1,561 @@
1/*
2 * Wistron laptop button driver
3 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
4 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
5 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
6 *
7 * You can redistribute and/or modify this program under the terms of the
8 * GNU General Public License version 2 as published by the Free Software
9 * Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
19 */
20#include <asm/io.h>
21#include <linux/dmi.h>
22#include <linux/init.h>
23#include <linux/input.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mc146818rtc.h>
27#include <linux/module.h>
28#include <linux/preempt.h>
29#include <linux/string.h>
30#include <linux/timer.h>
31#include <linux/types.h>
32#include <linux/platform_device.h>
33
34/*
35 * Number of attempts to read data from queue per poll;
36 * the queue can hold up to 31 entries
37 */
38#define MAX_POLL_ITERATIONS 64
39
40#define POLL_FREQUENCY 10 /* Number of polls per second */
41
42#if POLL_FREQUENCY > HZ
43#error "POLL_FREQUENCY too high"
44#endif
45
46/* BIOS subsystem IDs */
47#define WIFI 0x35
48#define BLUETOOTH 0x34
49
50MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
51MODULE_DESCRIPTION("Wistron laptop button driver");
52MODULE_LICENSE("GPL v2");
53MODULE_VERSION("0.1");
54
55static int force; /* = 0; */
56module_param(force, bool, 0);
57MODULE_PARM_DESC(force, "Load even if computer is not in database");
58
59static char *keymap_name; /* = NULL; */
60module_param_named(keymap, keymap_name, charp, 0);
61MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
62
63static struct platform_device *wistron_device;
64
65 /* BIOS interface implementation */
66
67static void __iomem *bios_entry_point; /* BIOS routine entry point */
68static void __iomem *bios_code_map_base;
69static void __iomem *bios_data_map_base;
70
71static u8 cmos_address;
72
73struct regs {
74 u32 eax, ebx, ecx;
75};
76
77static void call_bios(struct regs *regs)
78{
79 unsigned long flags;
80
81 preempt_disable();
82 local_irq_save(flags);
83 asm volatile ("pushl %%ebp;"
84 "movl %7, %%ebp;"
85 "call *%6;"
86 "popl %%ebp"
87 : "=a" (regs->eax), "=b" (regs->ebx), "=c" (regs->ecx)
88 : "0" (regs->eax), "1" (regs->ebx), "2" (regs->ecx),
89 "m" (bios_entry_point), "m" (bios_data_map_base)
90 : "edx", "edi", "esi", "memory");
91 local_irq_restore(flags);
92 preempt_enable();
93}
94
95static size_t __init locate_wistron_bios(void __iomem *base)
96{
97 static const unsigned char __initdata signature[] =
98 { 0x42, 0x21, 0x55, 0x30 };
99 size_t offset;
100
101 for (offset = 0; offset < 0x10000; offset += 0x10) {
102 if (check_signature(base + offset, signature,
103 sizeof(signature)) != 0)
104 return offset;
105 }
106 return -1;
107}
108
109static int __init map_bios(void)
110{
111 void __iomem *base;
112 size_t offset;
113 u32 entry_point;
114
115 base = ioremap(0xF0000, 0x10000); /* Can't fail */
116 offset = locate_wistron_bios(base);
117 if (offset < 0) {
118 printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
119 iounmap(base);
120 return -ENODEV;
121 }
122
123 entry_point = readl(base + offset + 5);
124 printk(KERN_DEBUG
125 "wistron_btns: BIOS signature found at %p, entry point %08X\n",
126 base + offset, entry_point);
127
128 if (entry_point >= 0xF0000) {
129 bios_code_map_base = base;
130 bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
131 } else {
132 iounmap(base);
133 bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
134 if (bios_code_map_base == NULL) {
135 printk(KERN_ERR
136 "wistron_btns: Can't map BIOS code at %08X\n",
137 entry_point & ~0x3FFF);
138 goto err;
139 }
140 bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
141 }
142 /* The Windows driver maps 0x10000 bytes, we keep only one page... */
143 bios_data_map_base = ioremap(0x400, 0xc00);
144 if (bios_data_map_base == NULL) {
145 printk(KERN_ERR "wistron_btns: Can't map BIOS data\n");
146 goto err_code;
147 }
148 return 0;
149
150err_code:
151 iounmap(bios_code_map_base);
152err:
153 return -ENOMEM;
154}
155
156static inline void unmap_bios(void)
157{
158 iounmap(bios_code_map_base);
159 iounmap(bios_data_map_base);
160}
161
162 /* BIOS calls */
163
164static u16 bios_pop_queue(void)
165{
166 struct regs regs;
167
168 memset(&regs, 0, sizeof (regs));
169 regs.eax = 0x9610;
170 regs.ebx = 0x061C;
171 regs.ecx = 0x0000;
172 call_bios(&regs);
173
174 return regs.eax;
175}
176
177static void __init bios_attach(void)
178{
179 struct regs regs;
180
181 memset(&regs, 0, sizeof (regs));
182 regs.eax = 0x9610;
183 regs.ebx = 0x012E;
184 call_bios(&regs);
185}
186
187static void bios_detach(void)
188{
189 struct regs regs;
190
191 memset(&regs, 0, sizeof (regs));
192 regs.eax = 0x9610;
193 regs.ebx = 0x002E;
194 call_bios(&regs);
195}
196
197static u8 __init bios_get_cmos_address(void)
198{
199 struct regs regs;
200
201 memset(&regs, 0, sizeof (regs));
202 regs.eax = 0x9610;
203 regs.ebx = 0x051C;
204 call_bios(&regs);
205
206 return regs.ecx;
207}
208
209static u16 __init bios_get_default_setting(u8 subsys)
210{
211 struct regs regs;
212
213 memset(&regs, 0, sizeof (regs));
214 regs.eax = 0x9610;
215 regs.ebx = 0x0200 | subsys;
216 call_bios(&regs);
217
218 return regs.eax;
219}
220
221static void bios_set_state(u8 subsys, int enable)
222{
223 struct regs regs;
224
225 memset(&regs, 0, sizeof (regs));
226 regs.eax = 0x9610;
227 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
228 call_bios(&regs);
229}
230
231/* Hardware database */
232
233struct key_entry {
234 char type; /* See KE_* below */
235 u8 code;
236 unsigned keycode; /* For KE_KEY */
237};
238
239enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
240
241static const struct key_entry *keymap; /* = NULL; Current key map */
242static int have_wifi;
243static int have_bluetooth;
244
245static int __init dmi_matched(struct dmi_system_id *dmi)
246{
247 const struct key_entry *key;
248
249 keymap = dmi->driver_data;
250 for (key = keymap; key->type != KE_END; key++) {
251 if (key->type == KE_WIFI) {
252 have_wifi = 1;
253 break;
254 } else if (key->type == KE_BLUETOOTH) {
255 have_bluetooth = 1;
256 break;
257 }
258 }
259 return 1;
260}
261
262static struct key_entry keymap_empty[] = {
263 { KE_END, 0 }
264};
265
266static struct key_entry keymap_fs_amilo_pro_v2000[] = {
267 { KE_KEY, 0x01, KEY_HELP },
268 { KE_KEY, 0x11, KEY_PROG1 },
269 { KE_KEY, 0x12, KEY_PROG2 },
270 { KE_WIFI, 0x30, 0 },
271 { KE_KEY, 0x31, KEY_MAIL },
272 { KE_KEY, 0x36, KEY_WWW },
273 { KE_END, 0 }
274};
275
276static struct key_entry keymap_wistron_ms2141[] = {
277 { KE_KEY, 0x11, KEY_PROG1 },
278 { KE_KEY, 0x12, KEY_PROG2 },
279 { KE_WIFI, 0x30, 0 },
280 { KE_KEY, 0x22, KEY_REWIND },
281 { KE_KEY, 0x23, KEY_FORWARD },
282 { KE_KEY, 0x24, KEY_PLAYPAUSE },
283 { KE_KEY, 0x25, KEY_STOPCD },
284 { KE_KEY, 0x31, KEY_MAIL },
285 { KE_KEY, 0x36, KEY_WWW },
286 { KE_END, 0 }
287};
288
289static struct key_entry keymap_acer_aspire_1500[] = {
290 { KE_KEY, 0x11, KEY_PROG1 },
291 { KE_KEY, 0x12, KEY_PROG2 },
292 { KE_WIFI, 0x30, 0 },
293 { KE_KEY, 0x31, KEY_MAIL },
294 { KE_KEY, 0x36, KEY_WWW },
295 { KE_BLUETOOTH, 0x44, 0 },
296 { KE_END, 0 }
297};
298
299/*
300 * If your machine is not here (which is currently rather likely), please send
301 * a list of buttons and their key codes (reported when loading this module
302 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
303 */
304static struct dmi_system_id dmi_ids[] = {
305 {
306 .callback = dmi_matched,
307 .ident = "Fujitsu-Siemens Amilo Pro V2000",
308 .matches = {
309 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
310 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
311 },
312 .driver_data = keymap_fs_amilo_pro_v2000
313 },
314 {
315 .callback = dmi_matched,
316 .ident = "Acer Aspire 1500",
317 .matches = {
318 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
319 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
320 },
321 .driver_data = keymap_acer_aspire_1500
322 },
323 { 0, }
324};
325
326static int __init select_keymap(void)
327{
328 if (keymap_name != NULL) {
329 if (strcmp (keymap_name, "1557/MS2141") == 0)
330 keymap = keymap_wistron_ms2141;
331 else {
332 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
333 return -EINVAL;
334 }
335 }
336 dmi_check_system(dmi_ids);
337 if (keymap == NULL) {
338 if (!force) {
339 printk(KERN_ERR "wistron_btns: System unknown\n");
340 return -ENODEV;
341 }
342 keymap = keymap_empty;
343 }
344 return 0;
345}
346
347 /* Input layer interface */
348
349static struct input_dev *input_dev;
350
351static int __init setup_input_dev(void)
352{
353 const struct key_entry *key;
354 int error;
355
356 input_dev = input_allocate_device();
357 if (!input_dev)
358 return -ENOMEM;
359
360 input_dev->name = "Wistron laptop buttons";
361 input_dev->phys = "wistron/input0";
362 input_dev->id.bustype = BUS_HOST;
363 input_dev->cdev.dev = &wistron_device->dev;
364
365 for (key = keymap; key->type != KE_END; key++) {
366 if (key->type == KE_KEY) {
367 input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
368 set_bit(key->keycode, input_dev->keybit);
369 }
370 }
371
372 error = input_register_device(input_dev);
373 if (error) {
374 input_free_device(input_dev);
375 return error;
376 }
377
378 return 0;
379}
380
381static void report_key(unsigned keycode)
382{
383 input_report_key(input_dev, keycode, 1);
384 input_sync(input_dev);
385 input_report_key(input_dev, keycode, 0);
386 input_sync(input_dev);
387}
388
389 /* Driver core */
390
391static int wifi_enabled;
392static int bluetooth_enabled;
393
394static void poll_bios(unsigned long);
395
396static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
397
398static void handle_key(u8 code)
399{
400 const struct key_entry *key;
401
402 for (key = keymap; key->type != KE_END; key++) {
403 if (code == key->code) {
404 switch (key->type) {
405 case KE_KEY:
406 report_key(key->keycode);
407 break;
408
409 case KE_WIFI:
410 if (have_wifi) {
411 wifi_enabled = !wifi_enabled;
412 bios_set_state(WIFI, wifi_enabled);
413 }
414 break;
415
416 case KE_BLUETOOTH:
417 if (have_bluetooth) {
418 bluetooth_enabled = !bluetooth_enabled;
419 bios_set_state(BLUETOOTH, bluetooth_enabled);
420 }
421 break;
422
423 case KE_END:
424 default:
425 BUG();
426 }
427 return;
428 }
429 }
430 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
431}
432
433static void poll_bios(unsigned long discard)
434{
435 u8 qlen;
436 u16 val;
437
438 for (;;) {
439 qlen = CMOS_READ(cmos_address);
440 if (qlen == 0)
441 break;
442 val = bios_pop_queue();
443 if (val != 0 && !discard)
444 handle_key((u8)val);
445 }
446
447 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
448}
449
450static int wistron_suspend(struct platform_device *dev, pm_message_t state)
451{
452 del_timer_sync(&poll_timer);
453
454 if (have_wifi)
455 bios_set_state(WIFI, 0);
456
457 if (have_bluetooth)
458 bios_set_state(BLUETOOTH, 0);
459
460 return 0;
461}
462
463static int wistron_resume(struct platform_device *dev)
464{
465 if (have_wifi)
466 bios_set_state(WIFI, wifi_enabled);
467
468 if (have_bluetooth)
469 bios_set_state(BLUETOOTH, bluetooth_enabled);
470
471 poll_bios(1);
472
473 return 0;
474}
475
476static struct platform_driver wistron_driver = {
477 .suspend = wistron_suspend,
478 .resume = wistron_resume,
479 .driver = {
480 .name = "wistron-bios",
481 },
482};
483
484static int __init wb_module_init(void)
485{
486 int err;
487
488 err = select_keymap();
489 if (err)
490 return err;
491
492 err = map_bios();
493 if (err)
494 return err;
495
496 bios_attach();
497 cmos_address = bios_get_cmos_address();
498
499 err = platform_driver_register(&wistron_driver);
500 if (err)
501 goto err_detach_bios;
502
503 wistron_device = platform_device_register_simple("wistron-bios", -1, NULL, 0);
504 if (IS_ERR(wistron_device)) {
505 err = PTR_ERR(wistron_device);
506 goto err_unregister_driver;
507 }
508
509 if (have_wifi) {
510 u16 wifi = bios_get_default_setting(WIFI);
511 if (wifi & 1)
512 wifi_enabled = (wifi & 2) ? 1 : 0;
513 else
514 have_wifi = 0;
515
516 if (have_wifi)
517 bios_set_state(WIFI, wifi_enabled);
518 }
519
520 if (have_bluetooth) {
521 u16 bt = bios_get_default_setting(BLUETOOTH);
522 if (bt & 1)
523 bluetooth_enabled = (bt & 2) ? 1 : 0;
524 else
525 have_bluetooth = 0;
526
527 if (have_bluetooth)
528 bios_set_state(BLUETOOTH, bluetooth_enabled);
529 }
530
531 err = setup_input_dev();
532 if (err)
533 goto err_unregister_device;
534
535 poll_bios(1); /* Flush stale event queue and arm timer */
536
537 return 0;
538
539 err_unregister_device:
540 platform_device_unregister(wistron_device);
541 err_unregister_driver:
542 platform_driver_unregister(&wistron_driver);
543 err_detach_bios:
544 bios_detach();
545 unmap_bios();
546
547 return err;
548}
549
550static void __exit wb_module_exit(void)
551{
552 del_timer_sync(&poll_timer);
553 input_unregister_device(input_dev);
554 platform_device_unregister(wistron_device);
555 platform_driver_unregister(&wistron_driver);
556 bios_detach();
557 unmap_bios();
558}
559
560module_init(wb_module_init);
561module_exit(wb_module_exit);