1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Debug support for HID Nintendo Wii / Wii U peripherals
4 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
10 #include <linux/debugfs.h>
11 #include <linux/module.h>
12 #include <linux/seq_file.h>
13 #include <linux/spinlock.h>
14 #include <linux/uaccess.h>
15 #include "hid-wiimote.h"
17 struct wiimote_debug {
18 struct wiimote_data *wdata;
19 struct dentry *eeprom;
23 static ssize_t wiidebug_eeprom_read(struct file *f, char __user *u, size_t s,
26 struct wiimote_debug *dbg = f->private_data;
27 struct wiimote_data *wdata = dbg->wdata;
40 ret = wiimote_cmd_acquire(wdata);
44 spin_lock_irqsave(&wdata->state.lock, flags);
45 wdata->state.cmd_read_size = s;
46 wdata->state.cmd_read_buf = buf;
47 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, *off & 0xffff);
48 wiiproto_req_reeprom(wdata, *off, s);
49 spin_unlock_irqrestore(&wdata->state.lock, flags);
51 ret = wiimote_cmd_wait(wdata);
53 size = wdata->state.cmd_read_size;
55 spin_lock_irqsave(&wdata->state.lock, flags);
56 wdata->state.cmd_read_buf = NULL;
57 spin_unlock_irqrestore(&wdata->state.lock, flags);
59 wiimote_cmd_release(wdata);
66 if (copy_to_user(u, buf, size))
75 static const struct file_operations wiidebug_eeprom_fops = {
78 .read = wiidebug_eeprom_read,
79 .llseek = generic_file_llseek,
82 static const char *wiidebug_drmmap[] = {
83 [WIIPROTO_REQ_NULL] = "NULL",
84 [WIIPROTO_REQ_DRM_K] = "K",
85 [WIIPROTO_REQ_DRM_KA] = "KA",
86 [WIIPROTO_REQ_DRM_KE] = "KE",
87 [WIIPROTO_REQ_DRM_KAI] = "KAI",
88 [WIIPROTO_REQ_DRM_KEE] = "KEE",
89 [WIIPROTO_REQ_DRM_KAE] = "KAE",
90 [WIIPROTO_REQ_DRM_KIE] = "KIE",
91 [WIIPROTO_REQ_DRM_KAIE] = "KAIE",
92 [WIIPROTO_REQ_DRM_E] = "E",
93 [WIIPROTO_REQ_DRM_SKAI1] = "SKAI1",
94 [WIIPROTO_REQ_DRM_SKAI2] = "SKAI2",
95 [WIIPROTO_REQ_MAX] = NULL
98 static int wiidebug_drm_show(struct seq_file *f, void *p)
100 struct wiimote_debug *dbg = f->private;
101 const char *str = NULL;
105 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
106 drm = dbg->wdata->state.drm;
107 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
109 if (drm < WIIPROTO_REQ_MAX)
110 str = wiidebug_drmmap[drm];
114 seq_printf(f, "%s\n", str);
119 static int wiidebug_drm_open(struct inode *i, struct file *f)
121 return single_open(f, wiidebug_drm_show, i->i_private);
124 static ssize_t wiidebug_drm_write(struct file *f, const char __user *u,
125 size_t s, loff_t *off)
127 struct seq_file *sf = f->private_data;
128 struct wiimote_debug *dbg = sf->private;
137 len = min((size_t) 15, s);
138 if (copy_from_user(buf, u, len))
143 for (i = 0; i < WIIPROTO_REQ_MAX; ++i) {
144 if (!wiidebug_drmmap[i])
146 if (!strcasecmp(buf, wiidebug_drmmap[i]))
150 if (i == WIIPROTO_REQ_MAX)
151 i = simple_strtoul(buf, NULL, 16);
153 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
154 dbg->wdata->state.flags &= ~WIIPROTO_FLAG_DRM_LOCKED;
155 wiiproto_req_drm(dbg->wdata, (__u8) i);
156 if (i != WIIPROTO_REQ_NULL)
157 dbg->wdata->state.flags |= WIIPROTO_FLAG_DRM_LOCKED;
158 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
163 static const struct file_operations wiidebug_drm_fops = {
164 .owner = THIS_MODULE,
165 .open = wiidebug_drm_open,
168 .write = wiidebug_drm_write,
169 .release = single_release,
172 int wiidebug_init(struct wiimote_data *wdata)
174 struct wiimote_debug *dbg;
178 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
184 dbg->eeprom = debugfs_create_file("eeprom", S_IRUSR,
185 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_eeprom_fops);
189 dbg->drm = debugfs_create_file("drm", S_IRUSR,
190 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_drm_fops);
194 spin_lock_irqsave(&wdata->state.lock, flags);
196 spin_unlock_irqrestore(&wdata->state.lock, flags);
201 debugfs_remove(dbg->eeprom);
207 void wiidebug_deinit(struct wiimote_data *wdata)
209 struct wiimote_debug *dbg = wdata->debug;
215 spin_lock_irqsave(&wdata->state.lock, flags);
217 spin_unlock_irqrestore(&wdata->state.lock, flags);
219 debugfs_remove(dbg->drm);
220 debugfs_remove(dbg->eeprom);