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