gscps2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * drivers/input/serio/gscps2.c
  3. *
  4. * Copyright (c) 2004-2006 Helge Deller <deller@gmx.de>
  5. * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
  6. * Copyright (c) 2002 Thibaut Varene <varenet@parisc-linux.org>
  7. *
  8. * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
  9. * Copyright (c) 1999 Alex deVries <alex@onefishtwo.ca>
  10. * Copyright (c) 1999-2000 Philipp Rumpf <prumpf@tux.org>
  11. * Copyright (c) 2000 Xavier Debacker <debackex@esiee.fr>
  12. * Copyright (c) 2000-2001 Thomas Marteau <marteaut@esiee.fr>
  13. *
  14. * HP GSC PS/2 port driver, found in PA/RISC Workstations
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file "COPYING" in the main directory of this archive
  18. * for more details.
  19. *
  20. * TODO:
  21. * - Dino testing (did HP ever shipped a machine on which this port
  22. * was usable/enabled ?)
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/serio.h>
  27. #include <linux/input.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/delay.h>
  31. #include <linux/ioport.h>
  32. #include <linux/pci_ids.h>
  33. #include <asm/irq.h>
  34. #include <asm/io.h>
  35. #include <asm/parisc-device.h>
  36. MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
  37. MODULE_DESCRIPTION("HP GSC PS2 port driver");
  38. MODULE_LICENSE("GPL");
  39. MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
  40. #define PFX "gscps2.c: "
  41. /*
  42. * Driver constants
  43. */
  44. /* various constants */
  45. #define ENABLE 1
  46. #define DISABLE 0
  47. #define GSC_DINO_OFFSET 0x0800 /* offset for DINO controller versus LASI one */
  48. /* PS/2 IO port offsets */
  49. #define GSC_ID 0x00 /* device ID offset (see: GSC_ID_XXX) */
  50. #define GSC_RESET 0x00 /* reset port offset */
  51. #define GSC_RCVDATA 0x04 /* receive port offset */
  52. #define GSC_XMTDATA 0x04 /* transmit port offset */
  53. #define GSC_CONTROL 0x08 /* see: Control register bits */
  54. #define GSC_STATUS 0x0C /* see: Status register bits */
  55. /* Control register bits */
  56. #define GSC_CTRL_ENBL 0x01 /* enable interface */
  57. #define GSC_CTRL_LPBXR 0x02 /* loopback operation */
  58. #define GSC_CTRL_DIAG 0x20 /* directly control clock/data line */
  59. #define GSC_CTRL_DATDIR 0x40 /* data line direct control */
  60. #define GSC_CTRL_CLKDIR 0x80 /* clock line direct control */
  61. /* Status register bits */
  62. #define GSC_STAT_RBNE 0x01 /* Receive Buffer Not Empty */
  63. #define GSC_STAT_TBNE 0x02 /* Transmit Buffer Not Empty */
  64. #define GSC_STAT_TERR 0x04 /* Timeout Error */
  65. #define GSC_STAT_PERR 0x08 /* Parity Error */
  66. #define GSC_STAT_CMPINTR 0x10 /* Composite Interrupt = irq on any port */
  67. #define GSC_STAT_DATSHD 0x40 /* Data Line Shadow */
  68. #define GSC_STAT_CLKSHD 0x80 /* Clock Line Shadow */
  69. /* IDs returned by GSC_ID port register */
  70. #define GSC_ID_KEYBOARD 0 /* device ID values */
  71. #define GSC_ID_MOUSE 1
  72. static irqreturn_t gscps2_interrupt(int irq, void *dev);
  73. #define BUFFER_SIZE 0x0f
  74. /* GSC PS/2 port device struct */
  75. struct gscps2port {
  76. struct list_head node;
  77. struct parisc_device *padev;
  78. struct serio *port;
  79. spinlock_t lock;
  80. char *addr;
  81. u8 act, append; /* position in buffer[] */
  82. struct {
  83. u8 data;
  84. u8 str;
  85. } buffer[BUFFER_SIZE+1];
  86. int id;
  87. };
  88. /*
  89. * Various HW level routines
  90. */
  91. #define gscps2_readb_input(x) readb((x)+GSC_RCVDATA)
  92. #define gscps2_readb_control(x) readb((x)+GSC_CONTROL)
  93. #define gscps2_readb_status(x) readb((x)+GSC_STATUS)
  94. #define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
  95. /*
  96. * wait_TBE() - wait for Transmit Buffer Empty
  97. */
  98. static int wait_TBE(char *addr)
  99. {
  100. int timeout = 25000; /* device is expected to react within 250 msec */
  101. while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
  102. if (!--timeout)
  103. return 0; /* This should not happen */
  104. udelay(10);
  105. }
  106. return 1;
  107. }
  108. /*
  109. * gscps2_flush() - flush the receive buffer
  110. */
  111. static void gscps2_flush(struct gscps2port *ps2port)
  112. {
  113. while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
  114. gscps2_readb_input(ps2port->addr);
  115. ps2port->act = ps2port->append = 0;
  116. }
  117. /*
  118. * gscps2_writeb_output() - write a byte to the port
  119. *
  120. * returns 1 on success, 0 on error
  121. */
  122. static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
  123. {
  124. unsigned long flags;
  125. char *addr = ps2port->addr;
  126. if (!wait_TBE(addr)) {
  127. printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
  128. return 0;
  129. }
  130. while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
  131. /* wait */;
  132. spin_lock_irqsave(&ps2port->lock, flags);
  133. writeb(data, addr+GSC_XMTDATA);
  134. spin_unlock_irqrestore(&ps2port->lock, flags);
  135. /* this is ugly, but due to timing of the port it seems to be necessary. */
  136. mdelay(6);
  137. /* make sure any received data is returned as fast as possible */
  138. /* this is important e.g. when we set the LEDs on the keyboard */
  139. gscps2_interrupt(0, NULL);
  140. return 1;
  141. }
  142. /*
  143. * gscps2_enable() - enables or disables the port
  144. */
  145. static void gscps2_enable(struct gscps2port *ps2port, int enable)
  146. {
  147. unsigned long flags;
  148. u8 data;
  149. /* now enable/disable the port */
  150. spin_lock_irqsave(&ps2port->lock, flags);
  151. gscps2_flush(ps2port);
  152. data = gscps2_readb_control(ps2port->addr);
  153. if (enable)
  154. data |= GSC_CTRL_ENBL;
  155. else
  156. data &= ~GSC_CTRL_ENBL;
  157. gscps2_writeb_control(data, ps2port->addr);
  158. spin_unlock_irqrestore(&ps2port->lock, flags);
  159. wait_TBE(ps2port->addr);
  160. gscps2_flush(ps2port);
  161. }
  162. /*
  163. * gscps2_reset() - resets the PS/2 port
  164. */
  165. static void gscps2_reset(struct gscps2port *ps2port)
  166. {
  167. char *addr = ps2port->addr;
  168. unsigned long flags;
  169. /* reset the interface */
  170. spin_lock_irqsave(&ps2port->lock, flags);
  171. gscps2_flush(ps2port);
  172. writeb(0xff, addr+GSC_RESET);
  173. gscps2_flush(ps2port);
  174. spin_unlock_irqrestore(&ps2port->lock, flags);
  175. }
  176. static LIST_HEAD(ps2port_list);
  177. /**
  178. * gscps2_interrupt() - Interruption service routine
  179. *
  180. * This function reads received PS/2 bytes and processes them on
  181. * all interfaces.
  182. * The problematic part here is, that the keyboard and mouse PS/2 port
  183. * share the same interrupt and it's not possible to send data if any
  184. * one of them holds input data. To solve this problem we try to receive
  185. * the data as fast as possible and handle the reporting to the upper layer
  186. * later.
  187. */
  188. static irqreturn_t gscps2_interrupt(int irq, void *dev)
  189. {
  190. struct gscps2port *ps2port;
  191. list_for_each_entry(ps2port, &ps2port_list, node) {
  192. unsigned long flags;
  193. spin_lock_irqsave(&ps2port->lock, flags);
  194. while ( (ps2port->buffer[ps2port->append].str =
  195. gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
  196. ps2port->buffer[ps2port->append].data =
  197. gscps2_readb_input(ps2port->addr);
  198. ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
  199. }
  200. spin_unlock_irqrestore(&ps2port->lock, flags);
  201. } /* list_for_each_entry */
  202. /* all data was read from the ports - now report the data to upper layer */
  203. list_for_each_entry(ps2port, &ps2port_list, node) {
  204. while (ps2port->act != ps2port->append) {
  205. unsigned int rxflags;
  206. u8 data, status;
  207. /* Did new data arrived while we read existing data ?
  208. If yes, exit now and let the new irq handler start over again */
  209. if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
  210. return IRQ_HANDLED;
  211. status = ps2port->buffer[ps2port->act].str;
  212. data = ps2port->buffer[ps2port->act].data;
  213. ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
  214. rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
  215. ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
  216. serio_interrupt(ps2port->port, data, rxflags);
  217. } /* while() */
  218. } /* list_for_each_entry */
  219. return IRQ_HANDLED;
  220. }
  221. /*
  222. * gscps2_write() - send a byte out through the aux interface.
  223. */
  224. static int gscps2_write(struct serio *port, unsigned char data)
  225. {
  226. struct gscps2port *ps2port = port->port_data;
  227. if (!gscps2_writeb_output(ps2port, data)) {
  228. printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. /*
  234. * gscps2_open() is called when a port is opened by the higher layer.
  235. * It resets and enables the port.
  236. */
  237. static int gscps2_open(struct serio *port)
  238. {
  239. struct gscps2port *ps2port = port->port_data;
  240. gscps2_reset(ps2port);
  241. /* enable it */
  242. gscps2_enable(ps2port, ENABLE);
  243. gscps2_interrupt(0, NULL);
  244. return 0;
  245. }
  246. /*
  247. * gscps2_close() disables the port
  248. */
  249. static void gscps2_close(struct serio *port)
  250. {
  251. struct gscps2port *ps2port = port->port_data;
  252. gscps2_enable(ps2port, DISABLE);
  253. }
  254. /**
  255. * gscps2_probe() - Probes PS2 devices
  256. * @return: success/error report
  257. */
  258. static int __init gscps2_probe(struct parisc_device *dev)
  259. {
  260. struct gscps2port *ps2port;
  261. struct serio *serio;
  262. unsigned long hpa = dev->hpa.start;
  263. int ret;
  264. if (!dev->irq)
  265. return -ENODEV;
  266. /* Offset for DINO PS/2. Works with LASI even */
  267. if (dev->id.sversion == 0x96)
  268. hpa += GSC_DINO_OFFSET;
  269. ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
  270. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  271. if (!ps2port || !serio) {
  272. ret = -ENOMEM;
  273. goto fail_nomem;
  274. }
  275. dev_set_drvdata(&dev->dev, ps2port);
  276. ps2port->port = serio;
  277. ps2port->padev = dev;
  278. ps2port->addr = ioremap_nocache(hpa, GSC_STATUS + 4);
  279. spin_lock_init(&ps2port->lock);
  280. gscps2_reset(ps2port);
  281. ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
  282. snprintf(serio->name, sizeof(serio->name), "GSC PS/2 %s",
  283. (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
  284. strlcpy(serio->phys, dev->dev.bus_id, sizeof(serio->phys));
  285. serio->id.type = SERIO_8042;
  286. serio->write = gscps2_write;
  287. serio->open = gscps2_open;
  288. serio->close = gscps2_close;
  289. serio->port_data = ps2port;
  290. serio->dev.parent = &dev->dev;
  291. ret = -EBUSY;
  292. if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
  293. goto fail_miserably;
  294. if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
  295. printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
  296. hpa, ps2port->id);
  297. ret = -ENODEV;
  298. goto fail;
  299. }
  300. #if 0
  301. if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
  302. goto fail;
  303. #endif
  304. printk(KERN_INFO "serio: %s port at 0x%p irq %d @ %s\n",
  305. ps2port->port->name,
  306. ps2port->addr,
  307. ps2port->padev->irq,
  308. ps2port->port->phys);
  309. serio_register_port(ps2port->port);
  310. list_add_tail(&ps2port->node, &ps2port_list);
  311. return 0;
  312. fail:
  313. free_irq(dev->irq, ps2port);
  314. fail_miserably:
  315. iounmap(ps2port->addr);
  316. release_mem_region(dev->hpa.start, GSC_STATUS + 4);
  317. fail_nomem:
  318. kfree(ps2port);
  319. kfree(serio);
  320. return ret;
  321. }
  322. /**
  323. * gscps2_remove() - Removes PS2 devices
  324. * @return: success/error report
  325. */
  326. static int __devexit gscps2_remove(struct parisc_device *dev)
  327. {
  328. struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
  329. serio_unregister_port(ps2port->port);
  330. free_irq(dev->irq, ps2port);
  331. gscps2_flush(ps2port);
  332. list_del(&ps2port->node);
  333. iounmap(ps2port->addr);
  334. #if 0
  335. release_mem_region(dev->hpa, GSC_STATUS + 4);
  336. #endif
  337. dev_set_drvdata(&dev->dev, NULL);
  338. kfree(ps2port);
  339. return 0;
  340. }
  341. static struct parisc_device_id gscps2_device_tbl[] = {
  342. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
  343. #ifdef DINO_TESTED
  344. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
  345. #endif
  346. { 0, } /* 0 terminated list */
  347. };
  348. static struct parisc_driver parisc_ps2_driver = {
  349. .name = "gsc_ps2",
  350. .id_table = gscps2_device_tbl,
  351. .probe = gscps2_probe,
  352. .remove = gscps2_remove,
  353. };
  354. static int __init gscps2_init(void)
  355. {
  356. register_parisc_driver(&parisc_ps2_driver);
  357. return 0;
  358. }
  359. static void __exit gscps2_exit(void)
  360. {
  361. unregister_parisc_driver(&parisc_ps2_driver);
  362. }
  363. module_init(gscps2_init);
  364. module_exit(gscps2_exit);