pcips2.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/serio.h>
  20. #include <linux/delay.h>
  21. #include <asm/io.h>
  22. #define PS2_CTRL (0)
  23. #define PS2_STATUS (1)
  24. #define PS2_DATA (2)
  25. #define PS2_CTRL_CLK (1<<0)
  26. #define PS2_CTRL_DAT (1<<1)
  27. #define PS2_CTRL_TXIRQ (1<<2)
  28. #define PS2_CTRL_ENABLE (1<<3)
  29. #define PS2_CTRL_RXIRQ (1<<4)
  30. #define PS2_STAT_CLK (1<<0)
  31. #define PS2_STAT_DAT (1<<1)
  32. #define PS2_STAT_PARITY (1<<2)
  33. #define PS2_STAT_RXFULL (1<<5)
  34. #define PS2_STAT_TXBUSY (1<<6)
  35. #define PS2_STAT_TXEMPTY (1<<7)
  36. struct pcips2_data {
  37. struct serio *io;
  38. unsigned int base;
  39. struct pci_dev *dev;
  40. };
  41. static int pcips2_write(struct serio *io, unsigned char val)
  42. {
  43. struct pcips2_data *ps2if = io->port_data;
  44. unsigned int stat;
  45. do {
  46. stat = inb(ps2if->base + PS2_STATUS);
  47. cpu_relax();
  48. } while (!(stat & PS2_STAT_TXEMPTY));
  49. outb(val, ps2if->base + PS2_DATA);
  50. return 0;
  51. }
  52. static irqreturn_t pcips2_interrupt(int irq, void *devid)
  53. {
  54. struct pcips2_data *ps2if = devid;
  55. unsigned char status, scancode;
  56. int handled = 0;
  57. do {
  58. unsigned int flag;
  59. status = inb(ps2if->base + PS2_STATUS);
  60. if (!(status & PS2_STAT_RXFULL))
  61. break;
  62. handled = 1;
  63. scancode = inb(ps2if->base + PS2_DATA);
  64. if (status == 0xff && scancode == 0xff)
  65. break;
  66. flag = (status & PS2_STAT_PARITY) ? 0 : SERIO_PARITY;
  67. if (hweight8(scancode) & 1)
  68. flag ^= SERIO_PARITY;
  69. serio_interrupt(ps2if->io, scancode, flag);
  70. } while (1);
  71. return IRQ_RETVAL(handled);
  72. }
  73. static void pcips2_flush_input(struct pcips2_data *ps2if)
  74. {
  75. unsigned char status, scancode;
  76. do {
  77. status = inb(ps2if->base + PS2_STATUS);
  78. if (!(status & PS2_STAT_RXFULL))
  79. break;
  80. scancode = inb(ps2if->base + PS2_DATA);
  81. if (status == 0xff && scancode == 0xff)
  82. break;
  83. } while (1);
  84. }
  85. static int pcips2_open(struct serio *io)
  86. {
  87. struct pcips2_data *ps2if = io->port_data;
  88. int ret, val = 0;
  89. outb(PS2_CTRL_ENABLE, ps2if->base);
  90. pcips2_flush_input(ps2if);
  91. ret = request_irq(ps2if->dev->irq, pcips2_interrupt, IRQF_SHARED,
  92. "pcips2", ps2if);
  93. if (ret == 0)
  94. val = PS2_CTRL_ENABLE | PS2_CTRL_RXIRQ;
  95. outb(val, ps2if->base);
  96. return ret;
  97. }
  98. static void pcips2_close(struct serio *io)
  99. {
  100. struct pcips2_data *ps2if = io->port_data;
  101. outb(0, ps2if->base);
  102. free_irq(ps2if->dev->irq, ps2if);
  103. }
  104. static int pcips2_probe(struct pci_dev *dev, const struct pci_device_id *id)
  105. {
  106. struct pcips2_data *ps2if;
  107. struct serio *serio;
  108. int ret;
  109. ret = pci_enable_device(dev);
  110. if (ret)
  111. goto out;
  112. ret = pci_request_regions(dev, "pcips2");
  113. if (ret)
  114. goto disable;
  115. ps2if = kzalloc(sizeof(struct pcips2_data), GFP_KERNEL);
  116. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  117. if (!ps2if || !serio) {
  118. ret = -ENOMEM;
  119. goto release;
  120. }
  121. serio->id.type = SERIO_8042;
  122. serio->write = pcips2_write;
  123. serio->open = pcips2_open;
  124. serio->close = pcips2_close;
  125. strlcpy(serio->name, pci_name(dev), sizeof(serio->name));
  126. strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  127. serio->port_data = ps2if;
  128. serio->dev.parent = &dev->dev;
  129. ps2if->io = serio;
  130. ps2if->dev = dev;
  131. ps2if->base = pci_resource_start(dev, 0);
  132. pci_set_drvdata(dev, ps2if);
  133. serio_register_port(ps2if->io);
  134. return 0;
  135. release:
  136. kfree(ps2if);
  137. kfree(serio);
  138. pci_release_regions(dev);
  139. disable:
  140. pci_disable_device(dev);
  141. out:
  142. return ret;
  143. }
  144. static void pcips2_remove(struct pci_dev *dev)
  145. {
  146. struct pcips2_data *ps2if = pci_get_drvdata(dev);
  147. serio_unregister_port(ps2if->io);
  148. kfree(ps2if);
  149. pci_release_regions(dev);
  150. pci_disable_device(dev);
  151. }
  152. static const struct pci_device_id pcips2_ids[] = {
  153. {
  154. .vendor = 0x14f2, /* MOBILITY */
  155. .device = 0x0123, /* Keyboard */
  156. .subvendor = PCI_ANY_ID,
  157. .subdevice = PCI_ANY_ID,
  158. .class = PCI_CLASS_INPUT_KEYBOARD << 8,
  159. .class_mask = 0xffff00,
  160. },
  161. {
  162. .vendor = 0x14f2, /* MOBILITY */
  163. .device = 0x0124, /* Mouse */
  164. .subvendor = PCI_ANY_ID,
  165. .subdevice = PCI_ANY_ID,
  166. .class = PCI_CLASS_INPUT_MOUSE << 8,
  167. .class_mask = 0xffff00,
  168. },
  169. { 0, }
  170. };
  171. MODULE_DEVICE_TABLE(pci, pcips2_ids);
  172. static struct pci_driver pcips2_driver = {
  173. .name = "pcips2",
  174. .id_table = pcips2_ids,
  175. .probe = pcips2_probe,
  176. .remove = pcips2_remove,
  177. };
  178. module_pci_driver(pcips2_driver);
  179. MODULE_LICENSE("GPL");
  180. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  181. MODULE_DESCRIPTION("PCI PS/2 keyboard/mouse driver");