maplemouse.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SEGA Dreamcast mouse driver
  3. * Based on drivers/usb/usbmouse.c
  4. *
  5. * Copyright (c) Yaegashi Takeshi, 2001
  6. * Copyright (c) Adrian McMenamin, 2008 - 2009
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/input.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/timer.h>
  14. #include <linux/maple.h>
  15. MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
  16. MODULE_DESCRIPTION("SEGA Dreamcast mouse driver");
  17. MODULE_LICENSE("GPL");
  18. struct dc_mouse {
  19. struct input_dev *dev;
  20. struct maple_device *mdev;
  21. };
  22. static void dc_mouse_callback(struct mapleq *mq)
  23. {
  24. int buttons, relx, rely, relz;
  25. struct maple_device *mapledev = mq->dev;
  26. struct dc_mouse *mse = maple_get_drvdata(mapledev);
  27. struct input_dev *dev = mse->dev;
  28. unsigned char *res = mq->recvbuf->buf;
  29. buttons = ~res[8];
  30. relx = *(unsigned short *)(res + 12) - 512;
  31. rely = *(unsigned short *)(res + 14) - 512;
  32. relz = *(unsigned short *)(res + 16) - 512;
  33. input_report_key(dev, BTN_LEFT, buttons & 4);
  34. input_report_key(dev, BTN_MIDDLE, buttons & 9);
  35. input_report_key(dev, BTN_RIGHT, buttons & 2);
  36. input_report_rel(dev, REL_X, relx);
  37. input_report_rel(dev, REL_Y, rely);
  38. input_report_rel(dev, REL_WHEEL, relz);
  39. input_sync(dev);
  40. }
  41. static int dc_mouse_open(struct input_dev *dev)
  42. {
  43. struct dc_mouse *mse = maple_get_drvdata(to_maple_dev(&dev->dev));
  44. maple_getcond_callback(mse->mdev, dc_mouse_callback, HZ/50,
  45. MAPLE_FUNC_MOUSE);
  46. return 0;
  47. }
  48. static void dc_mouse_close(struct input_dev *dev)
  49. {
  50. struct dc_mouse *mse = maple_get_drvdata(to_maple_dev(&dev->dev));
  51. maple_getcond_callback(mse->mdev, dc_mouse_callback, 0,
  52. MAPLE_FUNC_MOUSE);
  53. }
  54. /* allow the mouse to be used */
  55. static int probe_maple_mouse(struct device *dev)
  56. {
  57. struct maple_device *mdev = to_maple_dev(dev);
  58. struct maple_driver *mdrv = to_maple_driver(dev->driver);
  59. int error;
  60. struct input_dev *input_dev;
  61. struct dc_mouse *mse;
  62. mse = kzalloc(sizeof(struct dc_mouse), GFP_KERNEL);
  63. if (!mse) {
  64. error = -ENOMEM;
  65. goto fail;
  66. }
  67. input_dev = input_allocate_device();
  68. if (!input_dev) {
  69. error = -ENOMEM;
  70. goto fail_nomem;
  71. }
  72. mse->dev = input_dev;
  73. mse->mdev = mdev;
  74. input_set_drvdata(input_dev, mse);
  75. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  76. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  77. BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  78. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) |
  79. BIT_MASK(REL_WHEEL);
  80. input_dev->open = dc_mouse_open;
  81. input_dev->close = dc_mouse_close;
  82. input_dev->name = mdev->product_name;
  83. input_dev->id.bustype = BUS_HOST;
  84. error = input_register_device(input_dev);
  85. if (error)
  86. goto fail_register;
  87. mdev->driver = mdrv;
  88. maple_set_drvdata(mdev, mse);
  89. return error;
  90. fail_register:
  91. input_free_device(input_dev);
  92. fail_nomem:
  93. kfree(mse);
  94. fail:
  95. return error;
  96. }
  97. static int remove_maple_mouse(struct device *dev)
  98. {
  99. struct maple_device *mdev = to_maple_dev(dev);
  100. struct dc_mouse *mse = maple_get_drvdata(mdev);
  101. mdev->callback = NULL;
  102. input_unregister_device(mse->dev);
  103. maple_set_drvdata(mdev, NULL);
  104. kfree(mse);
  105. return 0;
  106. }
  107. static struct maple_driver dc_mouse_driver = {
  108. .function = MAPLE_FUNC_MOUSE,
  109. .drv = {
  110. .name = "Dreamcast_mouse",
  111. .probe = probe_maple_mouse,
  112. .remove = remove_maple_mouse,
  113. },
  114. };
  115. static int __init dc_mouse_init(void)
  116. {
  117. return maple_driver_register(&dc_mouse_driver);
  118. }
  119. static void __exit dc_mouse_exit(void)
  120. {
  121. maple_driver_unregister(&dc_mouse_driver);
  122. }
  123. module_init(dc_mouse_init);
  124. module_exit(dc_mouse_exit);