hid-retrode.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * HID driver for Retrode 2 controller adapter and plug-in extensions
  3. *
  4. * Copyright (c) 2017 Bastien Nocera <hadess@hadess.net>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/input.h>
  13. #include <linux/slab.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #include "hid-ids.h"
  17. #define CONTROLLER_NAME_BASE "Retrode"
  18. static int retrode_input_configured(struct hid_device *hdev,
  19. struct hid_input *hi)
  20. {
  21. struct hid_field *field = hi->report->field[0];
  22. const char *suffix;
  23. int number = 0;
  24. char *name;
  25. switch (field->report->id) {
  26. case 0:
  27. suffix = "SNES Mouse";
  28. break;
  29. case 1:
  30. case 2:
  31. suffix = "SNES / N64";
  32. number = field->report->id;
  33. break;
  34. case 3:
  35. case 4:
  36. suffix = "Mega Drive";
  37. number = field->report->id - 2;
  38. break;
  39. default:
  40. hid_err(hdev, "Got unhandled report id %d\n", field->report->id);
  41. suffix = "Unknown";
  42. }
  43. if (number)
  44. name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  45. "%s %s #%d", CONTROLLER_NAME_BASE,
  46. suffix, number);
  47. else
  48. name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  49. "%s %s", CONTROLLER_NAME_BASE, suffix);
  50. if (!name)
  51. return -ENOMEM;
  52. hi->input->name = name;
  53. return 0;
  54. }
  55. static int retrode_probe(struct hid_device *hdev,
  56. const struct hid_device_id *id)
  57. {
  58. int ret;
  59. /* Has no effect on the mouse device */
  60. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  61. ret = hid_parse(hdev);
  62. if (ret)
  63. return ret;
  64. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  65. if (ret)
  66. return ret;
  67. return 0;
  68. }
  69. static const struct hid_device_id retrode_devices[] = {
  70. { HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY, USB_DEVICE_ID_RETRODE2) },
  71. { }
  72. };
  73. MODULE_DEVICE_TABLE(hid, retrode_devices);
  74. static struct hid_driver retrode_driver = {
  75. .name = "hid-retrode",
  76. .id_table = retrode_devices,
  77. .input_configured = retrode_input_configured,
  78. .probe = retrode_probe,
  79. };
  80. module_hid_driver(retrode_driver);
  81. MODULE_LICENSE("GPL");