gscps2.c 11 KB

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