pcips2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * linux/drivers/input/serio/pcips2.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License.
  9. *
  10. * I'm not sure if this is a generic PS/2 PCI interface or specific to
  11. * the Mobility Electronics docking station.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/input.h>
  17. #include <linux/pci.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/serio.h>
  21. #include <linux/delay.h>
  22. #include <asm/io.h>
  23. #define PS2_CTRL (0)
  24. #define PS2_STATUS (1)
  25. #define PS2_DATA (2)
  26. #define PS2_CTRL_CLK (1<<0)
  27. #define PS2_CTRL_DAT (1<<1)
  28. #define PS2_CTRL_TXIRQ (1<<2)
  29. #define PS2_CTRL_ENABLE (1<<3)
  30. #define PS2_CTRL_RXIRQ (1<<4)
  31. #define PS2_STAT_CLK (1<<0)
  32. #define PS2_STAT_DAT (1<<1)
  33. #define PS2_STAT_PARITY (1<<2)
  34. #define PS2_STAT_RXFULL (1<<5)
  35. #define PS2_STAT_TXBUSY (1<<6)
  36. #define PS2_STAT_TXEMPTY (1<<7)
  37. struct pcips2_data {
  38. struct serio *io;
  39. unsigned int base;
  40. struct pci_dev *dev;
  41. };
  42. static int pcips2_write(struct serio *io, unsigned char val)
  43. {
  44. struct pcips2_data *ps2if = io->port_data;
  45. unsigned int stat;
  46. do {
  47. stat = inb(ps2if->base + PS2_STATUS);
  48. cpu_relax();
  49. } while (!(stat & PS2_STAT_TXEMPTY));
  50. outb(val, ps2if->base + PS2_DATA);
  51. return 0;
  52. }
  53. static irqreturn_t pcips2_interrupt(int irq, void *devid)
  54. {
  55. struct pcips2_data *ps2if = devid;
  56. unsigned char status, scancode;
  57. int handled = 0;
  58. do {
  59. unsigned int flag;
  60. status = inb(ps2if->base + PS2_STATUS);
  61. if (!(status & PS2_STAT_RXFULL))
  62. break;
  63. handled = 1;
  64. scancode = inb(ps2if->base + PS2_DATA);
  65. if (status == 0xff && scancode == 0xff)
  66. break;
  67. flag = (status & PS2_STAT_PARITY) ? 0 : SERIO_PARITY;
  68. if (hweight8(scancode) & 1)
  69. flag ^= SERIO_PARITY;
  70. serio_interrupt(ps2if->io, scancode, flag);
  71. } while (1);
  72. return IRQ_RETVAL(handled);
  73. }
  74. static void pcips2_flush_input(struct pcips2_data *ps2if)
  75. {
  76. unsigned char status, scancode;
  77. do {
  78. status = inb(ps2if->base + PS2_STATUS);
  79. if (!(status & PS2_STAT_RXFULL))
  80. break;
  81. scancode = inb(ps2if->base + PS2_DATA);
  82. if (status == 0xff && scancode == 0xff)
  83. break;
  84. } while (1);
  85. }
  86. static int pcips2_open(struct serio *io)
  87. {
  88. struct pcips2_data *ps2if = io->port_data;
  89. int ret, val = 0;
  90. outb(PS2_CTRL_ENABLE, ps2if->base);
  91. pcips2_flush_input(ps2if);
  92. ret = request_irq(ps2if->dev->irq, pcips2_interrupt, IRQF_SHARED,
  93. "pcips2", ps2if);
  94. if (ret == 0)
  95. val = PS2_CTRL_ENABLE | PS2_CTRL_RXIRQ;
  96. outb(val, ps2if->base);
  97. return ret;
  98. }
  99. static void pcips2_close(struct serio *io)
  100. {
  101. struct pcips2_data *ps2if = io->port_data;
  102. outb(0, ps2if->base);
  103. free_irq(ps2if->dev->irq, ps2if);
  104. }
  105. static int __devinit pcips2_probe(struct pci_dev *dev, const struct pci_device_id *id)
  106. {
  107. struct pcips2_data *ps2if;
  108. struct serio *serio;
  109. int ret;
  110. ret = pci_enable_device(dev);
  111. if (ret)
  112. goto out;
  113. ret = pci_request_regions(dev, "pcips2");
  114. if (ret)
  115. goto disable;
  116. ps2if = kzalloc(sizeof(struct pcips2_data), GFP_KERNEL);
  117. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  118. if (!ps2if || !serio) {
  119. ret = -ENOMEM;
  120. goto release;
  121. }
  122. serio->id.type = SERIO_8042;
  123. serio->write = pcips2_write;
  124. serio->open = pcips2_open;
  125. serio->close = pcips2_close;
  126. strlcpy(serio->name, pci_name(dev), sizeof(serio->name));
  127. strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  128. serio->port_data = ps2if;
  129. serio->dev.parent = &dev->dev;
  130. ps2if->io = serio;
  131. ps2if->dev = dev;
  132. ps2if->base = pci_resource_start(dev, 0);
  133. pci_set_drvdata(dev, ps2if);
  134. serio_register_port(ps2if->io);
  135. return 0;
  136. release:
  137. kfree(ps2if);
  138. kfree(serio);
  139. pci_release_regions(dev);
  140. disable:
  141. pci_disable_device(dev);
  142. out:
  143. return ret;
  144. }
  145. static void __devexit pcips2_remove(struct pci_dev *dev)
  146. {
  147. struct pcips2_data *ps2if = pci_get_drvdata(dev);
  148. serio_unregister_port(ps2if->io);
  149. pci_set_drvdata(dev, NULL);
  150. kfree(ps2if);
  151. pci_release_regions(dev);
  152. pci_disable_device(dev);
  153. }
  154. static const struct pci_device_id pcips2_ids[] = {
  155. {
  156. .vendor = 0x14f2, /* MOBILITY */
  157. .device = 0x0123, /* Keyboard */
  158. .subvendor = PCI_ANY_ID,
  159. .subdevice = PCI_ANY_ID,
  160. .class = PCI_CLASS_INPUT_KEYBOARD << 8,
  161. .class_mask = 0xffff00,
  162. },
  163. {
  164. .vendor = 0x14f2, /* MOBILITY */
  165. .device = 0x0124, /* Mouse */
  166. .subvendor = PCI_ANY_ID,
  167. .subdevice = PCI_ANY_ID,
  168. .class = PCI_CLASS_INPUT_MOUSE << 8,
  169. .class_mask = 0xffff00,
  170. },
  171. { 0, }
  172. };
  173. static struct pci_driver pcips2_driver = {
  174. .name = "pcips2",
  175. .id_table = pcips2_ids,
  176. .probe = pcips2_probe,
  177. .remove = __devexit_p(pcips2_remove),
  178. };
  179. static int __init pcips2_init(void)
  180. {
  181. return pci_register_driver(&pcips2_driver);
  182. }
  183. static void __exit pcips2_exit(void)
  184. {
  185. pci_unregister_driver(&pcips2_driver);
  186. }
  187. module_init(pcips2_init);
  188. module_exit(pcips2_exit);
  189. MODULE_LICENSE("GPL");
  190. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  191. MODULE_DESCRIPTION("PCI PS/2 keyboard/mouse driver");
  192. MODULE_DEVICE_TABLE(pci, pcips2_ids);