q40kbd.c 4.8 KB

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