ct82c710.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1999-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * 82C710 C&T mouse port chip driver for Linux
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/ioport.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/serio.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <asm/io.h>
  19. MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
  20. MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
  21. MODULE_LICENSE("GPL");
  22. /*
  23. * ct82c710 interface
  24. */
  25. #define CT82C710_DEV_IDLE 0x01 /* Device Idle */
  26. #define CT82C710_RX_FULL 0x02 /* Device Char received */
  27. #define CT82C710_TX_IDLE 0x04 /* Device XMIT Idle */
  28. #define CT82C710_RESET 0x08 /* Device Reset */
  29. #define CT82C710_INTS_ON 0x10 /* Device Interrupt On */
  30. #define CT82C710_ERROR_FLAG 0x20 /* Device Error */
  31. #define CT82C710_CLEAR 0x40 /* Device Clear */
  32. #define CT82C710_ENABLE 0x80 /* Device Enable */
  33. #define CT82C710_IRQ 12
  34. #define CT82C710_DATA ct82c710_iores.start
  35. #define CT82C710_STATUS (ct82c710_iores.start + 1)
  36. static struct serio *ct82c710_port;
  37. static struct platform_device *ct82c710_device;
  38. static struct resource ct82c710_iores;
  39. /*
  40. * Interrupt handler for the 82C710 mouse port. A character
  41. * is waiting in the 82C710.
  42. */
  43. static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
  44. {
  45. return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
  46. }
  47. /*
  48. * Wait for device to send output char and flush any input char.
  49. */
  50. static int ct82c170_wait(void)
  51. {
  52. int timeout = 60000;
  53. while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
  54. != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
  55. if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
  56. udelay(1);
  57. timeout--;
  58. }
  59. return !timeout;
  60. }
  61. static void ct82c710_close(struct serio *serio)
  62. {
  63. if (ct82c170_wait())
  64. printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
  65. outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
  66. if (ct82c170_wait())
  67. printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
  68. free_irq(CT82C710_IRQ, NULL);
  69. }
  70. static int ct82c710_open(struct serio *serio)
  71. {
  72. unsigned char status;
  73. int err;
  74. err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
  75. if (err)
  76. return err;
  77. status = inb_p(CT82C710_STATUS);
  78. status |= (CT82C710_ENABLE | CT82C710_RESET);
  79. outb_p(status, CT82C710_STATUS);
  80. status &= ~(CT82C710_RESET);
  81. outb_p(status, CT82C710_STATUS);
  82. status |= CT82C710_INTS_ON;
  83. outb_p(status, CT82C710_STATUS); /* Enable interrupts */
  84. while (ct82c170_wait()) {
  85. printk(KERN_ERR "ct82c710: Device busy in open()\n");
  86. status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
  87. outb_p(status, CT82C710_STATUS);
  88. free_irq(CT82C710_IRQ, NULL);
  89. return -EBUSY;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Write to the 82C710 mouse device.
  95. */
  96. static int ct82c710_write(struct serio *port, unsigned char c)
  97. {
  98. if (ct82c170_wait()) return -1;
  99. outb_p(c, CT82C710_DATA);
  100. return 0;
  101. }
  102. /*
  103. * See if we can find a 82C710 device. Read mouse address.
  104. */
  105. static int __init ct82c710_detect(void)
  106. {
  107. outb_p(0x55, 0x2fa); /* Any value except 9, ff or 36 */
  108. outb_p(0xaa, 0x3fa); /* Inverse of 55 */
  109. outb_p(0x36, 0x3fa); /* Address the chip */
  110. outb_p(0xe4, 0x3fa); /* 390/4; 390 = config address */
  111. outb_p(0x1b, 0x2fa); /* Inverse of e4 */
  112. outb_p(0x0f, 0x390); /* Write index */
  113. if (inb_p(0x391) != 0xe4) /* Config address found? */
  114. return -ENODEV; /* No: no 82C710 here */
  115. outb_p(0x0d, 0x390); /* Write index */
  116. ct82c710_iores.start = inb_p(0x391) << 2; /* Get mouse I/O address */
  117. ct82c710_iores.end = ct82c710_iores.start + 1;
  118. ct82c710_iores.flags = IORESOURCE_IO;
  119. outb_p(0x0f, 0x390);
  120. outb_p(0x0f, 0x391); /* Close config mode */
  121. return 0;
  122. }
  123. static int ct82c710_probe(struct platform_device *dev)
  124. {
  125. ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
  126. if (!ct82c710_port)
  127. return -ENOMEM;
  128. ct82c710_port->id.type = SERIO_8042;
  129. ct82c710_port->dev.parent = &dev->dev;
  130. ct82c710_port->open = ct82c710_open;
  131. ct82c710_port->close = ct82c710_close;
  132. ct82c710_port->write = ct82c710_write;
  133. strscpy(ct82c710_port->name, "C&T 82c710 mouse port",
  134. sizeof(ct82c710_port->name));
  135. snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
  136. "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
  137. serio_register_port(ct82c710_port);
  138. printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
  139. (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
  140. return 0;
  141. }
  142. static int ct82c710_remove(struct platform_device *dev)
  143. {
  144. serio_unregister_port(ct82c710_port);
  145. return 0;
  146. }
  147. static struct platform_driver ct82c710_driver = {
  148. .driver = {
  149. .name = "ct82c710",
  150. },
  151. .probe = ct82c710_probe,
  152. .remove = ct82c710_remove,
  153. };
  154. static int __init ct82c710_init(void)
  155. {
  156. int error;
  157. error = ct82c710_detect();
  158. if (error)
  159. return error;
  160. error = platform_driver_register(&ct82c710_driver);
  161. if (error)
  162. return error;
  163. ct82c710_device = platform_device_alloc("ct82c710", -1);
  164. if (!ct82c710_device) {
  165. error = -ENOMEM;
  166. goto err_unregister_driver;
  167. }
  168. error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
  169. if (error)
  170. goto err_free_device;
  171. error = platform_device_add(ct82c710_device);
  172. if (error)
  173. goto err_free_device;
  174. return 0;
  175. err_free_device:
  176. platform_device_put(ct82c710_device);
  177. err_unregister_driver:
  178. platform_driver_unregister(&ct82c710_driver);
  179. return error;
  180. }
  181. static void __exit ct82c710_exit(void)
  182. {
  183. platform_device_unregister(ct82c710_device);
  184. platform_driver_unregister(&ct82c710_driver);
  185. }
  186. module_init(ct82c710_init);
  187. module_exit(ct82c710_exit);