ct82c710.c 6.6 KB

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