q40kbd.c 4.3 KB

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