ct82c710.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * $Id: ct82c710.c,v 1.11 2001/09/25 10:12:07 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. */
  6. /*
  7. * 82C710 C&T mouse port chip driver for Linux
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/module.h>
  30. #include <linux/ioport.h>
  31. #include <linux/config.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/serio.h>
  35. #include <linux/errno.h>
  36. #include <linux/err.h>
  37. #include <asm/io.h>
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  39. MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * ct82c710 interface
  43. */
  44. #define CT82C710_DEV_IDLE 0x01 /* Device Idle */
  45. #define CT82C710_RX_FULL 0x02 /* Device Char received */
  46. #define CT82C710_TX_IDLE 0x04 /* Device XMIT Idle */
  47. #define CT82C710_RESET 0x08 /* Device Reset */
  48. #define CT82C710_INTS_ON 0x10 /* Device Interrupt On */
  49. #define CT82C710_ERROR_FLAG 0x20 /* Device Error */
  50. #define CT82C710_CLEAR 0x40 /* Device Clear */
  51. #define CT82C710_ENABLE 0x80 /* Device Enable */
  52. #define CT82C710_IRQ 12
  53. #define CT82C710_DATA ct82c710_iores.start
  54. #define CT82C710_STATUS (ct82c710_iores.start + 1)
  55. static struct serio *ct82c710_port;
  56. static struct platform_device *ct82c710_device;
  57. static struct resource ct82c710_iores;
  58. /*
  59. * Interrupt handler for the 82C710 mouse port. A character
  60. * is waiting in the 82C710.
  61. */
  62. static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id, struct pt_regs * regs)
  63. {
  64. return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0, regs);
  65. }
  66. /*
  67. * Wait for device to send output char and flush any input char.
  68. */
  69. static int ct82c170_wait(void)
  70. {
  71. int timeout = 60000;
  72. while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
  73. != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
  74. if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
  75. udelay(1);
  76. timeout--;
  77. }
  78. return !timeout;
  79. }
  80. static void ct82c710_close(struct serio *serio)
  81. {
  82. if (ct82c170_wait())
  83. printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
  84. outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
  85. if (ct82c170_wait())
  86. printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
  87. free_irq(CT82C710_IRQ, NULL);
  88. }
  89. static int ct82c710_open(struct serio *serio)
  90. {
  91. unsigned char status;
  92. if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
  93. return -1;
  94. status = inb_p(CT82C710_STATUS);
  95. status |= (CT82C710_ENABLE | CT82C710_RESET);
  96. outb_p(status, CT82C710_STATUS);
  97. status &= ~(CT82C710_RESET);
  98. outb_p(status, CT82C710_STATUS);
  99. status |= CT82C710_INTS_ON;
  100. outb_p(status, CT82C710_STATUS); /* Enable interrupts */
  101. while (ct82c170_wait()) {
  102. printk(KERN_ERR "ct82c710: Device busy in open()\n");
  103. status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
  104. outb_p(status, CT82C710_STATUS);
  105. free_irq(CT82C710_IRQ, NULL);
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. /*
  111. * Write to the 82C710 mouse device.
  112. */
  113. static int ct82c710_write(struct serio *port, unsigned char c)
  114. {
  115. if (ct82c170_wait()) return -1;
  116. outb_p(c, CT82C710_DATA);
  117. return 0;
  118. }
  119. /*
  120. * See if we can find a 82C710 device. Read mouse address.
  121. */
  122. static int __init ct82c710_probe(void)
  123. {
  124. outb_p(0x55, 0x2fa); /* Any value except 9, ff or 36 */
  125. outb_p(0xaa, 0x3fa); /* Inverse of 55 */
  126. outb_p(0x36, 0x3fa); /* Address the chip */
  127. outb_p(0xe4, 0x3fa); /* 390/4; 390 = config address */
  128. outb_p(0x1b, 0x2fa); /* Inverse of e4 */
  129. outb_p(0x0f, 0x390); /* Write index */
  130. if (inb_p(0x391) != 0xe4) /* Config address found? */
  131. return -1; /* No: no 82C710 here */
  132. outb_p(0x0d, 0x390); /* Write index */
  133. ct82c710_iores.start = inb_p(0x391) << 2; /* Get mouse I/O address */
  134. ct82c710_iores.end = ct82c710_iores.start + 1;
  135. ct82c710_iores.flags = IORESOURCE_IO;
  136. outb_p(0x0f, 0x390);
  137. outb_p(0x0f, 0x391); /* Close config mode */
  138. return 0;
  139. }
  140. static struct serio * __init ct82c710_allocate_port(void)
  141. {
  142. struct serio *serio;
  143. serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  144. if (serio) {
  145. memset(serio, 0, sizeof(struct serio));
  146. serio->id.type = SERIO_8042;
  147. serio->open = ct82c710_open;
  148. serio->close = ct82c710_close;
  149. serio->write = ct82c710_write;
  150. serio->dev.parent = &ct82c710_device->dev;
  151. strlcpy(serio->name, "C&T 82c710 mouse port", sizeof(serio->name));
  152. snprintf(serio->phys, sizeof(serio->phys), "isa%04lx/serio0", CT82C710_DATA);
  153. }
  154. return serio;
  155. }
  156. static int __init ct82c710_init(void)
  157. {
  158. if (ct82c710_probe())
  159. return -ENODEV;
  160. ct82c710_device = platform_device_register_simple("ct82c710", -1, &ct82c710_iores, 1);
  161. if (IS_ERR(ct82c710_device))
  162. return PTR_ERR(ct82c710_device);
  163. if (!(ct82c710_port = ct82c710_allocate_port())) {
  164. platform_device_unregister(ct82c710_device);
  165. return -ENOMEM;
  166. }
  167. serio_register_port(ct82c710_port);
  168. printk(KERN_INFO "serio: C&T 82c710 mouse port at %#lx irq %d\n",
  169. CT82C710_DATA, CT82C710_IRQ);
  170. return 0;
  171. }
  172. static void __exit ct82c710_exit(void)
  173. {
  174. serio_unregister_port(ct82c710_port);
  175. platform_device_unregister(ct82c710_device);
  176. }
  177. module_init(ct82c710_init);
  178. module_exit(ct82c710_exit);