maplecontrol.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * SEGA Dreamcast controller driver
  3. * Based on drivers/usb/iforce.c
  4. *
  5. * Copyright Yaegashi Takeshi, 2001
  6. * 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 controller driver");
  17. MODULE_LICENSE("GPL");
  18. struct dc_pad {
  19. struct input_dev *dev;
  20. struct maple_device *mdev;
  21. };
  22. static void dc_pad_callback(struct mapleq *mq)
  23. {
  24. unsigned short buttons;
  25. struct maple_device *mapledev = mq->dev;
  26. struct dc_pad *pad = maple_get_drvdata(mapledev);
  27. struct input_dev *dev = pad->dev;
  28. unsigned char *res = mq->recvbuf->buf;
  29. buttons = ~le16_to_cpup((__le16 *)(res + 8));
  30. input_report_abs(dev, ABS_HAT0Y,
  31. (buttons & 0x0010 ? -1 : 0) + (buttons & 0x0020 ? 1 : 0));
  32. input_report_abs(dev, ABS_HAT0X,
  33. (buttons & 0x0040 ? -1 : 0) + (buttons & 0x0080 ? 1 : 0));
  34. input_report_abs(dev, ABS_HAT1Y,
  35. (buttons & 0x1000 ? -1 : 0) + (buttons & 0x2000 ? 1 : 0));
  36. input_report_abs(dev, ABS_HAT1X,
  37. (buttons & 0x4000 ? -1 : 0) + (buttons & 0x8000 ? 1 : 0));
  38. input_report_key(dev, BTN_C, buttons & 0x0001);
  39. input_report_key(dev, BTN_B, buttons & 0x0002);
  40. input_report_key(dev, BTN_A, buttons & 0x0004);
  41. input_report_key(dev, BTN_START, buttons & 0x0008);
  42. input_report_key(dev, BTN_Z, buttons & 0x0100);
  43. input_report_key(dev, BTN_Y, buttons & 0x0200);
  44. input_report_key(dev, BTN_X, buttons & 0x0400);
  45. input_report_key(dev, BTN_SELECT, buttons & 0x0800);
  46. input_report_abs(dev, ABS_GAS, res[10]);
  47. input_report_abs(dev, ABS_BRAKE, res[11]);
  48. input_report_abs(dev, ABS_X, res[12]);
  49. input_report_abs(dev, ABS_Y, res[13]);
  50. input_report_abs(dev, ABS_RX, res[14]);
  51. input_report_abs(dev, ABS_RY, res[15]);
  52. }
  53. static int dc_pad_open(struct input_dev *dev)
  54. {
  55. struct dc_pad *pad = dev_get_platdata(&dev->dev);
  56. maple_getcond_callback(pad->mdev, dc_pad_callback, HZ/20,
  57. MAPLE_FUNC_CONTROLLER);
  58. return 0;
  59. }
  60. static void dc_pad_close(struct input_dev *dev)
  61. {
  62. struct dc_pad *pad = dev_get_platdata(&dev->dev);
  63. maple_getcond_callback(pad->mdev, dc_pad_callback, 0,
  64. MAPLE_FUNC_CONTROLLER);
  65. }
  66. /* allow the controller to be used */
  67. static int probe_maple_controller(struct device *dev)
  68. {
  69. static const short btn_bit[32] = {
  70. BTN_C, BTN_B, BTN_A, BTN_START, -1, -1, -1, -1,
  71. BTN_Z, BTN_Y, BTN_X, BTN_SELECT, -1, -1, -1, -1,
  72. -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1,
  74. };
  75. static const short abs_bit[32] = {
  76. -1, -1, -1, -1, ABS_HAT0Y, ABS_HAT0Y, ABS_HAT0X, ABS_HAT0X,
  77. -1, -1, -1, -1, ABS_HAT1Y, ABS_HAT1Y, ABS_HAT1X, ABS_HAT1X,
  78. ABS_GAS, ABS_BRAKE, ABS_X, ABS_Y, ABS_RX, ABS_RY, -1, -1,
  79. -1, -1, -1, -1, -1, -1, -1, -1,
  80. };
  81. struct maple_device *mdev = to_maple_dev(dev);
  82. struct maple_driver *mdrv = to_maple_driver(dev->driver);
  83. int i, error;
  84. struct dc_pad *pad;
  85. struct input_dev *idev;
  86. unsigned long data = be32_to_cpu(mdev->devinfo.function_data[0]);
  87. pad = kzalloc(sizeof(struct dc_pad), GFP_KERNEL);
  88. idev = input_allocate_device();
  89. if (!pad || !idev) {
  90. error = -ENOMEM;
  91. goto fail;
  92. }
  93. pad->dev = idev;
  94. pad->mdev = mdev;
  95. idev->open = dc_pad_open;
  96. idev->close = dc_pad_close;
  97. for (i = 0; i < 32; i++) {
  98. if (data & (1 << i)) {
  99. if (btn_bit[i] >= 0)
  100. __set_bit(btn_bit[i], idev->keybit);
  101. else if (abs_bit[i] >= 0)
  102. __set_bit(abs_bit[i], idev->absbit);
  103. }
  104. }
  105. if (idev->keybit[BIT_WORD(BTN_JOYSTICK)])
  106. idev->evbit[0] |= BIT_MASK(EV_KEY);
  107. if (idev->absbit[0])
  108. idev->evbit[0] |= BIT_MASK(EV_ABS);
  109. for (i = ABS_X; i <= ABS_BRAKE; i++)
  110. input_set_abs_params(idev, i, 0, 255, 0, 0);
  111. for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++)
  112. input_set_abs_params(idev, i, 1, -1, 0, 0);
  113. idev->dev.platform_data = pad;
  114. idev->dev.parent = &mdev->dev;
  115. idev->name = mdev->product_name;
  116. idev->id.bustype = BUS_HOST;
  117. input_set_drvdata(idev, pad);
  118. error = input_register_device(idev);
  119. if (error)
  120. goto fail;
  121. mdev->driver = mdrv;
  122. maple_set_drvdata(mdev, pad);
  123. return 0;
  124. fail:
  125. input_free_device(idev);
  126. kfree(pad);
  127. maple_set_drvdata(mdev, NULL);
  128. return error;
  129. }
  130. static int remove_maple_controller(struct device *dev)
  131. {
  132. struct maple_device *mdev = to_maple_dev(dev);
  133. struct dc_pad *pad = maple_get_drvdata(mdev);
  134. mdev->callback = NULL;
  135. input_unregister_device(pad->dev);
  136. maple_set_drvdata(mdev, NULL);
  137. kfree(pad);
  138. return 0;
  139. }
  140. static struct maple_driver dc_pad_driver = {
  141. .function = MAPLE_FUNC_CONTROLLER,
  142. .drv = {
  143. .name = "Dreamcast_controller",
  144. .probe = probe_maple_controller,
  145. .remove = remove_maple_controller,
  146. },
  147. };
  148. static int __init dc_pad_init(void)
  149. {
  150. return maple_driver_register(&dc_pad_driver);
  151. }
  152. static void __exit dc_pad_exit(void)
  153. {
  154. maple_driver_unregister(&dc_pad_driver);
  155. }
  156. module_init(dc_pad_init);
  157. module_exit(dc_pad_exit);