logibm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1999-2001 Vojtech Pavlik
  4. *
  5. * Based on the work of:
  6. * James Banks Matthew Dillon
  7. * David Giller Nathan Laredo
  8. * Linus Torvalds Johan Myreen
  9. * Cliff Matthews Philip Blundell
  10. * Russell King
  11. */
  12. /*
  13. * Logitech Bus Mouse Driver for Linux
  14. */
  15. /*
  16. */
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/ioport.h>
  20. #include <linux/init.h>
  21. #include <linux/input.h>
  22. #include <linux/interrupt.h>
  23. #include <asm/io.h>
  24. #include <asm/irq.h>
  25. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  26. MODULE_DESCRIPTION("Logitech busmouse driver");
  27. MODULE_LICENSE("GPL");
  28. #define LOGIBM_BASE 0x23c
  29. #define LOGIBM_EXTENT 4
  30. #define LOGIBM_DATA_PORT LOGIBM_BASE + 0
  31. #define LOGIBM_SIGNATURE_PORT LOGIBM_BASE + 1
  32. #define LOGIBM_CONTROL_PORT LOGIBM_BASE + 2
  33. #define LOGIBM_CONFIG_PORT LOGIBM_BASE + 3
  34. #define LOGIBM_ENABLE_IRQ 0x00
  35. #define LOGIBM_DISABLE_IRQ 0x10
  36. #define LOGIBM_READ_X_LOW 0x80
  37. #define LOGIBM_READ_X_HIGH 0xa0
  38. #define LOGIBM_READ_Y_LOW 0xc0
  39. #define LOGIBM_READ_Y_HIGH 0xe0
  40. #define LOGIBM_DEFAULT_MODE 0x90
  41. #define LOGIBM_CONFIG_BYTE 0x91
  42. #define LOGIBM_SIGNATURE_BYTE 0xa5
  43. #define LOGIBM_IRQ 5
  44. static int logibm_irq = LOGIBM_IRQ;
  45. module_param_hw_named(irq, logibm_irq, uint, irq, 0);
  46. MODULE_PARM_DESC(irq, "IRQ number (5=default)");
  47. static struct input_dev *logibm_dev;
  48. static irqreturn_t logibm_interrupt(int irq, void *dev_id)
  49. {
  50. char dx, dy;
  51. unsigned char buttons;
  52. outb(LOGIBM_READ_X_LOW, LOGIBM_CONTROL_PORT);
  53. dx = (inb(LOGIBM_DATA_PORT) & 0xf);
  54. outb(LOGIBM_READ_X_HIGH, LOGIBM_CONTROL_PORT);
  55. dx |= (inb(LOGIBM_DATA_PORT) & 0xf) << 4;
  56. outb(LOGIBM_READ_Y_LOW, LOGIBM_CONTROL_PORT);
  57. dy = (inb(LOGIBM_DATA_PORT) & 0xf);
  58. outb(LOGIBM_READ_Y_HIGH, LOGIBM_CONTROL_PORT);
  59. buttons = inb(LOGIBM_DATA_PORT);
  60. dy |= (buttons & 0xf) << 4;
  61. buttons = ~buttons >> 5;
  62. input_report_rel(logibm_dev, REL_X, dx);
  63. input_report_rel(logibm_dev, REL_Y, dy);
  64. input_report_key(logibm_dev, BTN_RIGHT, buttons & 1);
  65. input_report_key(logibm_dev, BTN_MIDDLE, buttons & 2);
  66. input_report_key(logibm_dev, BTN_LEFT, buttons & 4);
  67. input_sync(logibm_dev);
  68. outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
  69. return IRQ_HANDLED;
  70. }
  71. static int logibm_open(struct input_dev *dev)
  72. {
  73. if (request_irq(logibm_irq, logibm_interrupt, 0, "logibm", NULL)) {
  74. printk(KERN_ERR "logibm.c: Can't allocate irq %d\n", logibm_irq);
  75. return -EBUSY;
  76. }
  77. outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
  78. return 0;
  79. }
  80. static void logibm_close(struct input_dev *dev)
  81. {
  82. outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
  83. free_irq(logibm_irq, NULL);
  84. }
  85. static int __init logibm_init(void)
  86. {
  87. int err;
  88. if (!request_region(LOGIBM_BASE, LOGIBM_EXTENT, "logibm")) {
  89. printk(KERN_ERR "logibm.c: Can't allocate ports at %#x\n", LOGIBM_BASE);
  90. return -EBUSY;
  91. }
  92. outb(LOGIBM_CONFIG_BYTE, LOGIBM_CONFIG_PORT);
  93. outb(LOGIBM_SIGNATURE_BYTE, LOGIBM_SIGNATURE_PORT);
  94. udelay(100);
  95. if (inb(LOGIBM_SIGNATURE_PORT) != LOGIBM_SIGNATURE_BYTE) {
  96. printk(KERN_INFO "logibm.c: Didn't find Logitech busmouse at %#x\n", LOGIBM_BASE);
  97. err = -ENODEV;
  98. goto err_release_region;
  99. }
  100. outb(LOGIBM_DEFAULT_MODE, LOGIBM_CONFIG_PORT);
  101. outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
  102. logibm_dev = input_allocate_device();
  103. if (!logibm_dev) {
  104. printk(KERN_ERR "logibm.c: Not enough memory for input device\n");
  105. err = -ENOMEM;
  106. goto err_release_region;
  107. }
  108. logibm_dev->name = "Logitech bus mouse";
  109. logibm_dev->phys = "isa023c/input0";
  110. logibm_dev->id.bustype = BUS_ISA;
  111. logibm_dev->id.vendor = 0x0003;
  112. logibm_dev->id.product = 0x0001;
  113. logibm_dev->id.version = 0x0100;
  114. logibm_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  115. logibm_dev->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
  116. BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  117. logibm_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  118. logibm_dev->open = logibm_open;
  119. logibm_dev->close = logibm_close;
  120. err = input_register_device(logibm_dev);
  121. if (err)
  122. goto err_free_dev;
  123. return 0;
  124. err_free_dev:
  125. input_free_device(logibm_dev);
  126. err_release_region:
  127. release_region(LOGIBM_BASE, LOGIBM_EXTENT);
  128. return err;
  129. }
  130. static void __exit logibm_exit(void)
  131. {
  132. input_unregister_device(logibm_dev);
  133. release_region(LOGIBM_BASE, LOGIBM_EXTENT);
  134. }
  135. module_init(logibm_init);
  136. module_exit(logibm_exit);