hid-wiimote-debug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Debug support for HID Nintendo Wii / Wii U peripherals
  3. * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
  4. */
  5. /*
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/debugfs.h>
  12. #include <linux/module.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/uaccess.h>
  16. #include "hid-wiimote.h"
  17. struct wiimote_debug {
  18. struct wiimote_data *wdata;
  19. struct dentry *eeprom;
  20. struct dentry *drm;
  21. };
  22. static ssize_t wiidebug_eeprom_read(struct file *f, char __user *u, size_t s,
  23. loff_t *off)
  24. {
  25. struct wiimote_debug *dbg = f->private_data;
  26. struct wiimote_data *wdata = dbg->wdata;
  27. unsigned long flags;
  28. ssize_t ret;
  29. char buf[16];
  30. __u16 size = 0;
  31. if (s == 0)
  32. return -EINVAL;
  33. if (*off > 0xffffff)
  34. return 0;
  35. if (s > 16)
  36. s = 16;
  37. ret = wiimote_cmd_acquire(wdata);
  38. if (ret)
  39. return ret;
  40. spin_lock_irqsave(&wdata->state.lock, flags);
  41. wdata->state.cmd_read_size = s;
  42. wdata->state.cmd_read_buf = buf;
  43. wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, *off & 0xffff);
  44. wiiproto_req_reeprom(wdata, *off, s);
  45. spin_unlock_irqrestore(&wdata->state.lock, flags);
  46. ret = wiimote_cmd_wait(wdata);
  47. if (!ret)
  48. size = wdata->state.cmd_read_size;
  49. spin_lock_irqsave(&wdata->state.lock, flags);
  50. wdata->state.cmd_read_buf = NULL;
  51. spin_unlock_irqrestore(&wdata->state.lock, flags);
  52. wiimote_cmd_release(wdata);
  53. if (ret)
  54. return ret;
  55. else if (size == 0)
  56. return -EIO;
  57. if (copy_to_user(u, buf, size))
  58. return -EFAULT;
  59. *off += size;
  60. ret = size;
  61. return ret;
  62. }
  63. static const struct file_operations wiidebug_eeprom_fops = {
  64. .owner = THIS_MODULE,
  65. .open = simple_open,
  66. .read = wiidebug_eeprom_read,
  67. .llseek = generic_file_llseek,
  68. };
  69. static const char *wiidebug_drmmap[] = {
  70. [WIIPROTO_REQ_NULL] = "NULL",
  71. [WIIPROTO_REQ_DRM_K] = "K",
  72. [WIIPROTO_REQ_DRM_KA] = "KA",
  73. [WIIPROTO_REQ_DRM_KE] = "KE",
  74. [WIIPROTO_REQ_DRM_KAI] = "KAI",
  75. [WIIPROTO_REQ_DRM_KEE] = "KEE",
  76. [WIIPROTO_REQ_DRM_KAE] = "KAE",
  77. [WIIPROTO_REQ_DRM_KIE] = "KIE",
  78. [WIIPROTO_REQ_DRM_KAIE] = "KAIE",
  79. [WIIPROTO_REQ_DRM_E] = "E",
  80. [WIIPROTO_REQ_DRM_SKAI1] = "SKAI1",
  81. [WIIPROTO_REQ_DRM_SKAI2] = "SKAI2",
  82. [WIIPROTO_REQ_MAX] = NULL
  83. };
  84. static int wiidebug_drm_show(struct seq_file *f, void *p)
  85. {
  86. struct wiimote_debug *dbg = f->private;
  87. const char *str = NULL;
  88. unsigned long flags;
  89. __u8 drm;
  90. spin_lock_irqsave(&dbg->wdata->state.lock, flags);
  91. drm = dbg->wdata->state.drm;
  92. spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
  93. if (drm < WIIPROTO_REQ_MAX)
  94. str = wiidebug_drmmap[drm];
  95. if (!str)
  96. str = "unknown";
  97. seq_printf(f, "%s\n", str);
  98. return 0;
  99. }
  100. static int wiidebug_drm_open(struct inode *i, struct file *f)
  101. {
  102. return single_open(f, wiidebug_drm_show, i->i_private);
  103. }
  104. static ssize_t wiidebug_drm_write(struct file *f, const char __user *u,
  105. size_t s, loff_t *off)
  106. {
  107. struct seq_file *sf = f->private_data;
  108. struct wiimote_debug *dbg = sf->private;
  109. unsigned long flags;
  110. char buf[16];
  111. ssize_t len;
  112. int i;
  113. if (s == 0)
  114. return -EINVAL;
  115. len = min((size_t) 15, s);
  116. if (copy_from_user(buf, u, len))
  117. return -EFAULT;
  118. buf[len] = 0;
  119. for (i = 0; i < WIIPROTO_REQ_MAX; ++i) {
  120. if (!wiidebug_drmmap[i])
  121. continue;
  122. if (!strcasecmp(buf, wiidebug_drmmap[i]))
  123. break;
  124. }
  125. if (i == WIIPROTO_REQ_MAX)
  126. i = simple_strtoul(buf, NULL, 16);
  127. spin_lock_irqsave(&dbg->wdata->state.lock, flags);
  128. dbg->wdata->state.flags &= ~WIIPROTO_FLAG_DRM_LOCKED;
  129. wiiproto_req_drm(dbg->wdata, (__u8) i);
  130. if (i != WIIPROTO_REQ_NULL)
  131. dbg->wdata->state.flags |= WIIPROTO_FLAG_DRM_LOCKED;
  132. spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
  133. return len;
  134. }
  135. static const struct file_operations wiidebug_drm_fops = {
  136. .owner = THIS_MODULE,
  137. .open = wiidebug_drm_open,
  138. .read = seq_read,
  139. .llseek = seq_lseek,
  140. .write = wiidebug_drm_write,
  141. .release = single_release,
  142. };
  143. int wiidebug_init(struct wiimote_data *wdata)
  144. {
  145. struct wiimote_debug *dbg;
  146. unsigned long flags;
  147. int ret = -ENOMEM;
  148. dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
  149. if (!dbg)
  150. return -ENOMEM;
  151. dbg->wdata = wdata;
  152. dbg->eeprom = debugfs_create_file("eeprom", S_IRUSR,
  153. dbg->wdata->hdev->debug_dir, dbg, &wiidebug_eeprom_fops);
  154. if (!dbg->eeprom)
  155. goto err;
  156. dbg->drm = debugfs_create_file("drm", S_IRUSR,
  157. dbg->wdata->hdev->debug_dir, dbg, &wiidebug_drm_fops);
  158. if (!dbg->drm)
  159. goto err_drm;
  160. spin_lock_irqsave(&wdata->state.lock, flags);
  161. wdata->debug = dbg;
  162. spin_unlock_irqrestore(&wdata->state.lock, flags);
  163. return 0;
  164. err_drm:
  165. debugfs_remove(dbg->eeprom);
  166. err:
  167. kfree(dbg);
  168. return ret;
  169. }
  170. void wiidebug_deinit(struct wiimote_data *wdata)
  171. {
  172. struct wiimote_debug *dbg = wdata->debug;
  173. unsigned long flags;
  174. if (!dbg)
  175. return;
  176. spin_lock_irqsave(&wdata->state.lock, flags);
  177. wdata->debug = NULL;
  178. spin_unlock_irqrestore(&wdata->state.lock, flags);
  179. debugfs_remove(dbg->drm);
  180. debugfs_remove(dbg->eeprom);
  181. kfree(dbg);
  182. }