q40kbd.c 4.5 KB

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