gscps2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 __iomem *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 __iomem *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 __iomem *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(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. unsigned long flags;
  167. /* reset the interface */
  168. spin_lock_irqsave(&ps2port->lock, flags);
  169. gscps2_flush(ps2port);
  170. writeb(0xff, ps2port->addr + GSC_RESET);
  171. gscps2_flush(ps2port);
  172. spin_unlock_irqrestore(&ps2port->lock, flags);
  173. }
  174. static LIST_HEAD(ps2port_list);
  175. /**
  176. * gscps2_interrupt() - Interruption service routine
  177. *
  178. * This function reads received PS/2 bytes and processes them on
  179. * all interfaces.
  180. * The problematic part here is, that the keyboard and mouse PS/2 port
  181. * share the same interrupt and it's not possible to send data if any
  182. * one of them holds input data. To solve this problem we try to receive
  183. * the data as fast as possible and handle the reporting to the upper layer
  184. * later.
  185. */
  186. static irqreturn_t gscps2_interrupt(int irq, void *dev)
  187. {
  188. struct gscps2port *ps2port;
  189. list_for_each_entry(ps2port, &ps2port_list, node) {
  190. unsigned long flags;
  191. spin_lock_irqsave(&ps2port->lock, flags);
  192. while ( (ps2port->buffer[ps2port->append].str =
  193. gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
  194. ps2port->buffer[ps2port->append].data =
  195. gscps2_readb_input(ps2port->addr);
  196. ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
  197. }
  198. spin_unlock_irqrestore(&ps2port->lock, flags);
  199. } /* list_for_each_entry */
  200. /* all data was read from the ports - now report the data to upper layer */
  201. list_for_each_entry(ps2port, &ps2port_list, node) {
  202. while (ps2port->act != ps2port->append) {
  203. unsigned int rxflags;
  204. u8 data, status;
  205. /* Did new data arrived while we read existing data ?
  206. If yes, exit now and let the new irq handler start over again */
  207. if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
  208. return IRQ_HANDLED;
  209. status = ps2port->buffer[ps2port->act].str;
  210. data = ps2port->buffer[ps2port->act].data;
  211. ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
  212. rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
  213. ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
  214. serio_interrupt(ps2port->port, data, rxflags);
  215. } /* while() */
  216. } /* list_for_each_entry */
  217. return IRQ_HANDLED;
  218. }
  219. /*
  220. * gscps2_write() - send a byte out through the aux interface.
  221. */
  222. static int gscps2_write(struct serio *port, unsigned char data)
  223. {
  224. struct gscps2port *ps2port = port->port_data;
  225. if (!gscps2_writeb_output(ps2port, data)) {
  226. printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. /*
  232. * gscps2_open() is called when a port is opened by the higher layer.
  233. * It resets and enables the port.
  234. */
  235. static int gscps2_open(struct serio *port)
  236. {
  237. struct gscps2port *ps2port = port->port_data;
  238. gscps2_reset(ps2port);
  239. /* enable it */
  240. gscps2_enable(ps2port, ENABLE);
  241. gscps2_interrupt(0, NULL);
  242. return 0;
  243. }
  244. /*
  245. * gscps2_close() disables the port
  246. */
  247. static void gscps2_close(struct serio *port)
  248. {
  249. struct gscps2port *ps2port = port->port_data;
  250. gscps2_enable(ps2port, DISABLE);
  251. }
  252. /**
  253. * gscps2_probe() - Probes PS2 devices
  254. * @return: success/error report
  255. */
  256. static int __init gscps2_probe(struct parisc_device *dev)
  257. {
  258. struct gscps2port *ps2port;
  259. struct serio *serio;
  260. unsigned long hpa = dev->hpa.start;
  261. int ret;
  262. if (!dev->irq)
  263. return -ENODEV;
  264. /* Offset for DINO PS/2. Works with LASI even */
  265. if (dev->id.sversion == 0x96)
  266. hpa += GSC_DINO_OFFSET;
  267. ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
  268. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  269. if (!ps2port || !serio) {
  270. ret = -ENOMEM;
  271. goto fail_nomem;
  272. }
  273. dev_set_drvdata(&dev->dev, ps2port);
  274. ps2port->port = serio;
  275. ps2port->padev = dev;
  276. ps2port->addr = ioremap(hpa, GSC_STATUS + 4);
  277. spin_lock_init(&ps2port->lock);
  278. gscps2_reset(ps2port);
  279. ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
  280. snprintf(serio->name, sizeof(serio->name), "gsc-ps2-%s",
  281. (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
  282. strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  283. serio->id.type = SERIO_8042;
  284. serio->write = gscps2_write;
  285. serio->open = gscps2_open;
  286. serio->close = gscps2_close;
  287. serio->port_data = ps2port;
  288. serio->dev.parent = &dev->dev;
  289. ret = -EBUSY;
  290. if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
  291. goto fail_miserably;
  292. if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
  293. printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
  294. hpa, ps2port->id);
  295. ret = -ENODEV;
  296. goto fail;
  297. }
  298. #if 0
  299. if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
  300. goto fail;
  301. #endif
  302. pr_info("serio: %s port at 0x%08lx irq %d @ %s\n",
  303. ps2port->port->name,
  304. hpa,
  305. ps2port->padev->irq,
  306. ps2port->port->phys);
  307. serio_register_port(ps2port->port);
  308. list_add_tail(&ps2port->node, &ps2port_list);
  309. return 0;
  310. fail:
  311. free_irq(dev->irq, ps2port);
  312. fail_miserably:
  313. iounmap(ps2port->addr);
  314. release_mem_region(dev->hpa.start, GSC_STATUS + 4);
  315. fail_nomem:
  316. kfree(ps2port);
  317. kfree(serio);
  318. return ret;
  319. }
  320. /**
  321. * gscps2_remove() - Removes PS2 devices
  322. * @return: success/error report
  323. */
  324. static int __exit gscps2_remove(struct parisc_device *dev)
  325. {
  326. struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
  327. serio_unregister_port(ps2port->port);
  328. free_irq(dev->irq, ps2port);
  329. gscps2_flush(ps2port);
  330. list_del(&ps2port->node);
  331. iounmap(ps2port->addr);
  332. #if 0
  333. release_mem_region(dev->hpa, GSC_STATUS + 4);
  334. #endif
  335. dev_set_drvdata(&dev->dev, NULL);
  336. kfree(ps2port);
  337. return 0;
  338. }
  339. static const struct parisc_device_id gscps2_device_tbl[] __initconst = {
  340. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
  341. #ifdef DINO_TESTED
  342. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
  343. #endif
  344. { 0, } /* 0 terminated list */
  345. };
  346. MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
  347. static struct parisc_driver parisc_ps2_driver __refdata = {
  348. .name = "gsc_ps2",
  349. .id_table = gscps2_device_tbl,
  350. .probe = gscps2_probe,
  351. .remove = __exit_p(gscps2_remove),
  352. };
  353. static int __init gscps2_init(void)
  354. {
  355. register_parisc_driver(&parisc_ps2_driver);
  356. return 0;
  357. }
  358. static void __exit gscps2_exit(void)
  359. {
  360. unregister_parisc_driver(&parisc_ps2_driver);
  361. }
  362. module_init(gscps2_init);
  363. module_exit(gscps2_exit);