gscps2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. char __iomem *addr = ps2port->addr;
  124. if (!wait_TBE(addr)) {
  125. printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
  126. return 0;
  127. }
  128. while (gscps2_readb_status(addr) & GSC_STAT_RBNE)
  129. /* wait */;
  130. scoped_guard(spinlock_irqsave, &ps2port->lock)
  131. writeb(data, addr+GSC_XMTDATA);
  132. /* this is ugly, but due to timing of the port it seems to be necessary. */
  133. mdelay(6);
  134. /* make sure any received data is returned as fast as possible */
  135. /* this is important e.g. when we set the LEDs on the keyboard */
  136. gscps2_interrupt(0, NULL);
  137. return 1;
  138. }
  139. /*
  140. * gscps2_enable() - enables or disables the port
  141. */
  142. static void gscps2_enable(struct gscps2port *ps2port, int enable)
  143. {
  144. u8 data;
  145. /* now enable/disable the port */
  146. scoped_guard(spinlock_irqsave, &ps2port->lock) {
  147. gscps2_flush(ps2port);
  148. data = gscps2_readb_control(ps2port->addr);
  149. if (enable)
  150. data |= GSC_CTRL_ENBL;
  151. else
  152. data &= ~GSC_CTRL_ENBL;
  153. gscps2_writeb_control(data, ps2port->addr);
  154. }
  155. wait_TBE(ps2port->addr);
  156. gscps2_flush(ps2port);
  157. }
  158. /*
  159. * gscps2_reset() - resets the PS/2 port
  160. */
  161. static void gscps2_reset(struct gscps2port *ps2port)
  162. {
  163. /* reset the interface */
  164. guard(spinlock_irqsave)(&ps2port->lock);
  165. gscps2_flush(ps2port);
  166. writeb(0xff, ps2port->addr + GSC_RESET);
  167. gscps2_flush(ps2port);
  168. }
  169. static LIST_HEAD(ps2port_list);
  170. static void gscps2_read_data(struct gscps2port *ps2port)
  171. {
  172. u8 status;
  173. do {
  174. status = gscps2_readb_status(ps2port->addr);
  175. if (!(status & GSC_STAT_RBNE))
  176. break;
  177. ps2port->buffer[ps2port->append].str = status;
  178. ps2port->buffer[ps2port->append].data =
  179. gscps2_readb_input(ps2port->addr);
  180. } while (true);
  181. }
  182. static bool gscps2_report_data(struct gscps2port *ps2port)
  183. {
  184. unsigned int rxflags;
  185. u8 data, status;
  186. while (ps2port->act != ps2port->append) {
  187. /*
  188. * Did new data arrived while we read existing data ?
  189. * If yes, exit now and let the new irq handler start
  190. * over again.
  191. */
  192. if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
  193. return true;
  194. status = ps2port->buffer[ps2port->act].str;
  195. data = ps2port->buffer[ps2port->act].data;
  196. ps2port->act = (ps2port->act + 1) & BUFFER_SIZE;
  197. rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
  198. ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
  199. serio_interrupt(ps2port->port, data, rxflags);
  200. }
  201. return false;
  202. }
  203. /**
  204. * gscps2_interrupt() - Interruption service routine
  205. * @irq: interrupt number which triggered (unused)
  206. * @dev: device pointer (unused)
  207. *
  208. * This function reads received PS/2 bytes and processes them on
  209. * all interfaces.
  210. * The problematic part here is, that the keyboard and mouse PS/2 port
  211. * share the same interrupt and it's not possible to send data if any
  212. * one of them holds input data. To solve this problem we try to receive
  213. * the data as fast as possible and handle the reporting to the upper layer
  214. * later.
  215. */
  216. static irqreturn_t gscps2_interrupt(int irq, void *dev)
  217. {
  218. struct gscps2port *ps2port;
  219. list_for_each_entry(ps2port, &ps2port_list, node) {
  220. guard(spinlock_irqsave)(&ps2port->lock);
  221. gscps2_read_data(ps2port);
  222. } /* list_for_each_entry */
  223. /* all data was read from the ports - now report the data to upper layer */
  224. list_for_each_entry(ps2port, &ps2port_list, node) {
  225. if (gscps2_report_data(ps2port)) {
  226. /* More data ready - break early to restart interrupt */
  227. break;
  228. }
  229. }
  230. return IRQ_HANDLED;
  231. }
  232. /*
  233. * gscps2_write() - send a byte out through the aux interface.
  234. */
  235. static int gscps2_write(struct serio *port, unsigned char data)
  236. {
  237. struct gscps2port *ps2port = port->port_data;
  238. if (!gscps2_writeb_output(ps2port, data)) {
  239. printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. /*
  245. * gscps2_open() is called when a port is opened by the higher layer.
  246. * It resets and enables the port.
  247. */
  248. static int gscps2_open(struct serio *port)
  249. {
  250. struct gscps2port *ps2port = port->port_data;
  251. gscps2_reset(ps2port);
  252. /* enable it */
  253. gscps2_enable(ps2port, ENABLE);
  254. gscps2_interrupt(0, NULL);
  255. return 0;
  256. }
  257. /*
  258. * gscps2_close() disables the port
  259. */
  260. static void gscps2_close(struct serio *port)
  261. {
  262. struct gscps2port *ps2port = port->port_data;
  263. gscps2_enable(ps2port, DISABLE);
  264. }
  265. /**
  266. * gscps2_probe() - Probes PS2 devices
  267. * @dev: pointer to parisc_device struct which will be probed
  268. *
  269. * @return: success/error report
  270. */
  271. static int __init gscps2_probe(struct parisc_device *dev)
  272. {
  273. struct gscps2port *ps2port;
  274. struct serio *serio;
  275. unsigned long hpa = dev->hpa.start;
  276. int ret;
  277. if (!dev->irq)
  278. return -ENODEV;
  279. /* Offset for DINO PS/2. Works with LASI even */
  280. if (dev->id.sversion == 0x96)
  281. hpa += GSC_DINO_OFFSET;
  282. ps2port = kzalloc_obj(*ps2port);
  283. serio = kzalloc_obj(*serio);
  284. if (!ps2port || !serio) {
  285. ret = -ENOMEM;
  286. goto fail_nomem;
  287. }
  288. dev_set_drvdata(&dev->dev, ps2port);
  289. ps2port->port = serio;
  290. ps2port->padev = dev;
  291. ps2port->addr = ioremap(hpa, GSC_STATUS + 4);
  292. if (!ps2port->addr) {
  293. ret = -ENOMEM;
  294. goto fail_nomem;
  295. }
  296. spin_lock_init(&ps2port->lock);
  297. gscps2_reset(ps2port);
  298. ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
  299. snprintf(serio->name, sizeof(serio->name), "gsc-ps2-%s",
  300. (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
  301. strscpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  302. serio->id.type = SERIO_8042;
  303. serio->write = gscps2_write;
  304. serio->open = gscps2_open;
  305. serio->close = gscps2_close;
  306. serio->port_data = ps2port;
  307. serio->dev.parent = &dev->dev;
  308. ret = -EBUSY;
  309. if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
  310. goto fail_miserably;
  311. if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
  312. printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
  313. hpa, ps2port->id);
  314. ret = -ENODEV;
  315. goto fail;
  316. }
  317. #if 0
  318. if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
  319. goto fail;
  320. #endif
  321. pr_info("serio: %s port at 0x%08lx irq %d @ %s\n",
  322. ps2port->port->name,
  323. hpa,
  324. ps2port->padev->irq,
  325. ps2port->port->phys);
  326. serio_register_port(ps2port->port);
  327. list_add_tail(&ps2port->node, &ps2port_list);
  328. return 0;
  329. fail:
  330. free_irq(dev->irq, ps2port);
  331. fail_miserably:
  332. iounmap(ps2port->addr);
  333. release_mem_region(dev->hpa.start, GSC_STATUS + 4);
  334. fail_nomem:
  335. kfree(ps2port);
  336. kfree(serio);
  337. return ret;
  338. }
  339. /**
  340. * gscps2_remove() - Removes PS2 devices
  341. * @dev: pointer to parisc_device which shall be removed
  342. *
  343. * @return: success/error report
  344. */
  345. static void __exit gscps2_remove(struct parisc_device *dev)
  346. {
  347. struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
  348. serio_unregister_port(ps2port->port);
  349. free_irq(dev->irq, ps2port);
  350. gscps2_flush(ps2port);
  351. list_del(&ps2port->node);
  352. iounmap(ps2port->addr);
  353. #if 0
  354. release_mem_region(dev->hpa, GSC_STATUS + 4);
  355. #endif
  356. dev_set_drvdata(&dev->dev, NULL);
  357. kfree(ps2port);
  358. }
  359. static const struct parisc_device_id gscps2_device_tbl[] __initconst = {
  360. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
  361. #ifdef DINO_TESTED
  362. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
  363. #endif
  364. { 0, } /* 0 terminated list */
  365. };
  366. MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
  367. static struct parisc_driver parisc_ps2_driver __refdata = {
  368. .name = "gsc_ps2",
  369. .id_table = gscps2_device_tbl,
  370. .probe = gscps2_probe,
  371. .remove = __exit_p(gscps2_remove),
  372. };
  373. static int __init gscps2_init(void)
  374. {
  375. register_parisc_driver(&parisc_ps2_driver);
  376. return 0;
  377. }
  378. static void __exit gscps2_exit(void)
  379. {
  380. unregister_parisc_driver(&parisc_ps2_driver);
  381. }
  382. module_init(gscps2_init);
  383. module_exit(gscps2_exit);