ct82c710.c 5.7 KB

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