q40kbd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/serio.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/err.h>
  33. #include <linux/bitops.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/slab.h>
  36. #include <asm/io.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/q40_master.h>
  39. #include <asm/irq.h>
  40. #include <asm/q40ints.h>
  41. #define DRV_NAME "q40kbd"
  42. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  43. MODULE_DESCRIPTION("Q40 PS/2 keyboard controller driver");
  44. MODULE_LICENSE("GPL");
  45. MODULE_ALIAS("platform:" DRV_NAME);
  46. struct q40kbd {
  47. struct serio *port;
  48. spinlock_t lock;
  49. };
  50. static irqreturn_t q40kbd_interrupt(int irq, void *dev_id)
  51. {
  52. struct q40kbd *q40kbd = dev_id;
  53. unsigned long flags;
  54. spin_lock_irqsave(&q40kbd->lock, flags);
  55. if (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG))
  56. serio_interrupt(q40kbd->port, master_inb(KEYCODE_REG), 0);
  57. master_outb(-1, KEYBOARD_UNLOCK_REG);
  58. spin_unlock_irqrestore(&q40kbd->lock, flags);
  59. return IRQ_HANDLED;
  60. }
  61. /*
  62. * q40kbd_flush() flushes all data that may be in the keyboard buffers
  63. */
  64. static void q40kbd_flush(struct q40kbd *q40kbd)
  65. {
  66. int maxread = 100;
  67. unsigned long flags;
  68. spin_lock_irqsave(&q40kbd->lock, flags);
  69. while (maxread-- && (Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG)))
  70. master_inb(KEYCODE_REG);
  71. spin_unlock_irqrestore(&q40kbd->lock, flags);
  72. }
  73. static void q40kbd_stop(void)
  74. {
  75. master_outb(0, KEY_IRQ_ENABLE_REG);
  76. master_outb(-1, KEYBOARD_UNLOCK_REG);
  77. }
  78. /*
  79. * q40kbd_open() is called when a port is open by the higher layer.
  80. * It allocates the interrupt and enables in in the chip.
  81. */
  82. static int q40kbd_open(struct serio *port)
  83. {
  84. struct q40kbd *q40kbd = port->port_data;
  85. q40kbd_flush(q40kbd);
  86. /* off we go */
  87. master_outb(-1, KEYBOARD_UNLOCK_REG);
  88. master_outb(1, KEY_IRQ_ENABLE_REG);
  89. return 0;
  90. }
  91. static void q40kbd_close(struct serio *port)
  92. {
  93. struct q40kbd *q40kbd = port->port_data;
  94. q40kbd_stop();
  95. q40kbd_flush(q40kbd);
  96. }
  97. static int q40kbd_probe(struct platform_device *pdev)
  98. {
  99. struct q40kbd *q40kbd;
  100. struct serio *port;
  101. int error;
  102. q40kbd = kzalloc(sizeof(struct q40kbd), GFP_KERNEL);
  103. port = kzalloc(sizeof(struct serio), GFP_KERNEL);
  104. if (!q40kbd || !port) {
  105. error = -ENOMEM;
  106. goto err_free_mem;
  107. }
  108. q40kbd->port = port;
  109. spin_lock_init(&q40kbd->lock);
  110. port->id.type = SERIO_8042;
  111. port->open = q40kbd_open;
  112. port->close = q40kbd_close;
  113. port->port_data = q40kbd;
  114. port->dev.parent = &pdev->dev;
  115. strlcpy(port->name, "Q40 Kbd Port", sizeof(port->name));
  116. strlcpy(port->phys, "Q40", sizeof(port->phys));
  117. q40kbd_stop();
  118. error = request_irq(Q40_IRQ_KEYBOARD, q40kbd_interrupt, 0,
  119. DRV_NAME, q40kbd);
  120. if (error) {
  121. dev_err(&pdev->dev, "Can't get irq %d.\n", Q40_IRQ_KEYBOARD);
  122. goto err_free_mem;
  123. }
  124. serio_register_port(q40kbd->port);
  125. platform_set_drvdata(pdev, q40kbd);
  126. printk(KERN_INFO "serio: Q40 kbd registered\n");
  127. return 0;
  128. err_free_mem:
  129. kfree(port);
  130. kfree(q40kbd);
  131. return error;
  132. }
  133. static int q40kbd_remove(struct platform_device *pdev)
  134. {
  135. struct q40kbd *q40kbd = platform_get_drvdata(pdev);
  136. /*
  137. * q40kbd_close() will be called as part of unregistering
  138. * and will ensure that IRQ is turned off, so it is safe
  139. * to unregister port first and free IRQ later.
  140. */
  141. serio_unregister_port(q40kbd->port);
  142. free_irq(Q40_IRQ_KEYBOARD, q40kbd);
  143. kfree(q40kbd);
  144. return 0;
  145. }
  146. static struct platform_driver q40kbd_driver = {
  147. .driver = {
  148. .name = "q40kbd",
  149. },
  150. .remove = q40kbd_remove,
  151. };
  152. module_platform_driver_probe(q40kbd_driver, q40kbd_probe);