logibm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * James Banks Matthew Dillon
  6. * David Giller Nathan Laredo
  7. * Linus Torvalds Johan Myreen
  8. * Cliff Matthews Philip Blundell
  9. * Russell King
  10. */
  11. /*
  12. * Logitech Bus Mouse Driver for Linux
  13. */
  14. /*
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. * Should you need to contact me, the author, you can do so either by
  30. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  31. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  32. */
  33. #include <linux/module.h>
  34. #include <linux/delay.h>
  35. #include <linux/ioport.h>
  36. #include <linux/init.h>
  37. #include <linux/input.h>
  38. #include <linux/interrupt.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  42. MODULE_DESCRIPTION("Logitech busmouse driver");
  43. MODULE_LICENSE("GPL");
  44. #define LOGIBM_BASE 0x23c
  45. #define LOGIBM_EXTENT 4
  46. #define LOGIBM_DATA_PORT LOGIBM_BASE + 0
  47. #define LOGIBM_SIGNATURE_PORT LOGIBM_BASE + 1
  48. #define LOGIBM_CONTROL_PORT LOGIBM_BASE + 2
  49. #define LOGIBM_CONFIG_PORT LOGIBM_BASE + 3
  50. #define LOGIBM_ENABLE_IRQ 0x00
  51. #define LOGIBM_DISABLE_IRQ 0x10
  52. #define LOGIBM_READ_X_LOW 0x80
  53. #define LOGIBM_READ_X_HIGH 0xa0
  54. #define LOGIBM_READ_Y_LOW 0xc0
  55. #define LOGIBM_READ_Y_HIGH 0xe0
  56. #define LOGIBM_DEFAULT_MODE 0x90
  57. #define LOGIBM_CONFIG_BYTE 0x91
  58. #define LOGIBM_SIGNATURE_BYTE 0xa5
  59. #define LOGIBM_IRQ 5
  60. static int logibm_irq = LOGIBM_IRQ;
  61. module_param_named(irq, logibm_irq, uint, 0);
  62. MODULE_PARM_DESC(irq, "IRQ number (5=default)");
  63. static struct input_dev *logibm_dev;
  64. static irqreturn_t logibm_interrupt(int irq, void *dev_id)
  65. {
  66. char dx, dy;
  67. unsigned char buttons;
  68. outb(LOGIBM_READ_X_LOW, LOGIBM_CONTROL_PORT);
  69. dx = (inb(LOGIBM_DATA_PORT) & 0xf);
  70. outb(LOGIBM_READ_X_HIGH, LOGIBM_CONTROL_PORT);
  71. dx |= (inb(LOGIBM_DATA_PORT) & 0xf) << 4;
  72. outb(LOGIBM_READ_Y_LOW, LOGIBM_CONTROL_PORT);
  73. dy = (inb(LOGIBM_DATA_PORT) & 0xf);
  74. outb(LOGIBM_READ_Y_HIGH, LOGIBM_CONTROL_PORT);
  75. buttons = inb(LOGIBM_DATA_PORT);
  76. dy |= (buttons & 0xf) << 4;
  77. buttons = ~buttons >> 5;
  78. input_report_rel(logibm_dev, REL_X, dx);
  79. input_report_rel(logibm_dev, REL_Y, dy);
  80. input_report_key(logibm_dev, BTN_RIGHT, buttons & 1);
  81. input_report_key(logibm_dev, BTN_MIDDLE, buttons & 2);
  82. input_report_key(logibm_dev, BTN_LEFT, buttons & 4);
  83. input_sync(logibm_dev);
  84. outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
  85. return IRQ_HANDLED;
  86. }
  87. static int logibm_open(struct input_dev *dev)
  88. {
  89. if (request_irq(logibm_irq, logibm_interrupt, 0, "logibm", NULL)) {
  90. printk(KERN_ERR "logibm.c: Can't allocate irq %d\n", logibm_irq);
  91. return -EBUSY;
  92. }
  93. outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
  94. return 0;
  95. }
  96. static void logibm_close(struct input_dev *dev)
  97. {
  98. outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
  99. free_irq(logibm_irq, NULL);
  100. }
  101. static int __init logibm_init(void)
  102. {
  103. int err;
  104. if (!request_region(LOGIBM_BASE, LOGIBM_EXTENT, "logibm")) {
  105. printk(KERN_ERR "logibm.c: Can't allocate ports at %#x\n", LOGIBM_BASE);
  106. return -EBUSY;
  107. }
  108. outb(LOGIBM_CONFIG_BYTE, LOGIBM_CONFIG_PORT);
  109. outb(LOGIBM_SIGNATURE_BYTE, LOGIBM_SIGNATURE_PORT);
  110. udelay(100);
  111. if (inb(LOGIBM_SIGNATURE_PORT) != LOGIBM_SIGNATURE_BYTE) {
  112. printk(KERN_INFO "logibm.c: Didn't find Logitech busmouse at %#x\n", LOGIBM_BASE);
  113. err = -ENODEV;
  114. goto err_release_region;
  115. }
  116. outb(LOGIBM_DEFAULT_MODE, LOGIBM_CONFIG_PORT);
  117. outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
  118. logibm_dev = input_allocate_device();
  119. if (!logibm_dev) {
  120. printk(KERN_ERR "logibm.c: Not enough memory for input device\n");
  121. err = -ENOMEM;
  122. goto err_release_region;
  123. }
  124. logibm_dev->name = "Logitech bus mouse";
  125. logibm_dev->phys = "isa023c/input0";
  126. logibm_dev->id.bustype = BUS_ISA;
  127. logibm_dev->id.vendor = 0x0003;
  128. logibm_dev->id.product = 0x0001;
  129. logibm_dev->id.version = 0x0100;
  130. logibm_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  131. logibm_dev->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
  132. BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  133. logibm_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  134. logibm_dev->open = logibm_open;
  135. logibm_dev->close = logibm_close;
  136. err = input_register_device(logibm_dev);
  137. if (err)
  138. goto err_free_dev;
  139. return 0;
  140. err_free_dev:
  141. input_free_device(logibm_dev);
  142. err_release_region:
  143. release_region(LOGIBM_BASE, LOGIBM_EXTENT);
  144. return err;
  145. }
  146. static void __exit logibm_exit(void)
  147. {
  148. input_unregister_device(logibm_dev);
  149. release_region(LOGIBM_BASE, LOGIBM_EXTENT);
  150. }
  151. module_init(logibm_init);
  152. module_exit(logibm_exit);