q40kbd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * $Id: q40kbd.c,v 1.12 2002/02/02 22:26:44 vojtech Exp $
  3. *
  4. * Copyright (c) 2000-2001 Vojtech Pavlik
  5. *
  6. * Based on the work of:
  7. * Richard Zidlicky <Richard.Zidlicky@stud.informatik.uni-erlangen.de>
  8. */
  9. /*
  10. * Q40 PS/2 keyboard controller driver for Linux/m68k
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * Should you need to contact me, the author, you can do so either by
  28. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  29. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  30. */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/serio.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/err.h>
  36. #include <linux/bitops.h>
  37. #include <linux/platform_device.h>
  38. #include <asm/io.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/q40_master.h>
  41. #include <asm/irq.h>
  42. #include <asm/q40ints.h>
  43. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  44. MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
  45. MODULE_LICENSE("GPL");
  46. DEFINE_SPINLOCK(q40kbd_lock);
  47. static struct serio *q40kbd_port;
  48. static struct platform_device *q40kbd_device;
  49. static irqreturn_t q40kbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  50. {
  51. unsigned long flags;
  52. spin_lock_irqsave(&q40kbd_lock, flags);
  53. if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
  54. serio_interrupt(q40kbd_port, master_inb(KEYCODE_REG), 0, regs);
  55. master_outb(-1, KEYBOARD_UNLOCK_REG);
  56. spin_unlock_irqrestore(&q40kbd_lock, flags);
  57. return IRQ_HANDLED;
  58. }
  59. /*
  60. * q40kbd_flush() flushes all data that may be in the keyboard buffers
  61. */
  62. static void q40kbd_flush(void)
  63. {
  64. int maxread = 100;
  65. unsigned long flags;
  66. spin_lock_irqsave(&q40kbd_lock, flags);
  67. while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
  68. master_inb(KEYCODE_REG);
  69. spin_unlock_irqrestore(&q40kbd_lock, flags);
  70. }
  71. /*
  72. * q40kbd_open() is called when a port is open by the higher layer.
  73. * It allocates the interrupt and enables in in the chip.
  74. */
  75. static int q40kbd_open(struct serio *port)
  76. {
  77. q40kbd_flush();
  78. if (request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0, "q40kbd", NULL)) {
  79. printk(KERN_ERR "q40kbd.c: Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
  80. return -1;
  81. }
  82. /* off we go */
  83. master_outb(-1, KEYBOARD_UNLOCK_REG);
  84. master_outb(1, KEY_IRQ_ENABLE_REG);
  85. return 0;
  86. }
  87. static void q40kbd_close(struct serio *port)
  88. {
  89. master_outb(0, KEY_IRQ_ENABLE_REG);
  90. master_outb(-1, KEYBOARD_UNLOCK_REG);
  91. free_irq(Q40_IRQ_KEYBOARD, NULL);
  92. q40kbd_flush();
  93. }
  94. static struct serio * __init q40kbd_allocate_port(void)
  95. {
  96. struct serio *serio;
  97. serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  98. if (serio) {
  99. memset(serio, 0, sizeof(struct serio));
  100. serio->id.type = SERIO_8042;
  101. serio->open = q40kbd_open;
  102. serio->close = q40kbd_close;
  103. serio->dev.parent = &q40kbd_device->dev;
  104. strlcpy(serio->name, "Q40 Kbd Port", sizeof(serio->name));
  105. strlcpy(serio->phys, "Q40", sizeof(serio->phys));
  106. }
  107. return serio;
  108. }
  109. static int __init q40kbd_init(void)
  110. {
  111. if (!MACH_IS_Q40)
  112. return -EIO;
  113. q40kbd_device = platform_device_register_simple("q40kbd", -1, NULL, 0);
  114. if (IS_ERR(q40kbd_device))
  115. return PTR_ERR(q40kbd_device);
  116. if (!(q40kbd_port = q40kbd_allocate_port())) {
  117. platform_device_unregister(q40kbd_device);
  118. return -ENOMEM;
  119. }
  120. serio_register_port(q40kbd_port);
  121. printk(KERN_INFO "serio: Q40 kbd registered\n");
  122. return 0;
  123. }
  124. static void __exit q40kbd_exit(void)
  125. {
  126. serio_unregister_port(q40kbd_port);
  127. platform_device_unregister(q40kbd_device);
  128. }
  129. module_init(q40kbd_init);
  130. module_exit(q40kbd_exit);