2 * Debug support for HID Nintendo Wii / Wii U peripherals
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
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)
13 #include <linux/debugfs.h>
14 #include <linux/module.h>
15 #include <linux/seq_file.h>
16 #include <linux/spinlock.h>
17 #include <linux/uaccess.h>
18 #include "hid-wiimote.h"
20 struct wiimote_debug {
21 struct wiimote_data *wdata;
22 struct dentry *eeprom;
26 static ssize_t wiidebug_eeprom_read(struct file *f, char __user *u, size_t s,
29 struct wiimote_debug *dbg = f->private_data;
30 struct wiimote_data *wdata = dbg->wdata;
43 ret = wiimote_cmd_acquire(wdata);
47 spin_lock_irqsave(&wdata->state.lock, flags);
48 wdata->state.cmd_read_size = s;
49 wdata->state.cmd_read_buf = buf;
50 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, *off & 0xffff);
51 wiiproto_req_reeprom(wdata, *off, s);
52 spin_unlock_irqrestore(&wdata->state.lock, flags);
54 ret = wiimote_cmd_wait(wdata);
56 size = wdata->state.cmd_read_size;
58 spin_lock_irqsave(&wdata->state.lock, flags);
59 wdata->state.cmd_read_buf = NULL;
60 spin_unlock_irqrestore(&wdata->state.lock, flags);
62 wiimote_cmd_release(wdata);
69 if (copy_to_user(u, buf, size))
78 static const struct file_operations wiidebug_eeprom_fops = {
81 .read = wiidebug_eeprom_read,
82 .llseek = generic_file_llseek,
85 static const char *wiidebug_drmmap[] = {
86 [WIIPROTO_REQ_NULL] = "NULL",
87 [WIIPROTO_REQ_DRM_K] = "K",
88 [WIIPROTO_REQ_DRM_KA] = "KA",
89 [WIIPROTO_REQ_DRM_KE] = "KE",
90 [WIIPROTO_REQ_DRM_KAI] = "KAI",
91 [WIIPROTO_REQ_DRM_KEE] = "KEE",
92 [WIIPROTO_REQ_DRM_KAE] = "KAE",
93 [WIIPROTO_REQ_DRM_KIE] = "KIE",
94 [WIIPROTO_REQ_DRM_KAIE] = "KAIE",
95 [WIIPROTO_REQ_DRM_E] = "E",
96 [WIIPROTO_REQ_DRM_SKAI1] = "SKAI1",
97 [WIIPROTO_REQ_DRM_SKAI2] = "SKAI2",
98 [WIIPROTO_REQ_MAX] = NULL
101 static int wiidebug_drm_show(struct seq_file *f, void *p)
103 struct wiimote_debug *dbg = f->private;
104 const char *str = NULL;
108 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
109 drm = dbg->wdata->state.drm;
110 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
112 if (drm < WIIPROTO_REQ_MAX)
113 str = wiidebug_drmmap[drm];
117 seq_printf(f, "%s\n", str);
122 static int wiidebug_drm_open(struct inode *i, struct file *f)
124 return single_open(f, wiidebug_drm_show, i->i_private);
127 static ssize_t wiidebug_drm_write(struct file *f, const char __user *u,
128 size_t s, loff_t *off)
130 struct seq_file *sf = f->private_data;
131 struct wiimote_debug *dbg = sf->private;
140 len = min((size_t) 15, s);
141 if (copy_from_user(buf, u, len))
146 for (i = 0; i < WIIPROTO_REQ_MAX; ++i) {
147 if (!wiidebug_drmmap[i])
149 if (!strcasecmp(buf, wiidebug_drmmap[i]))
153 if (i == WIIPROTO_REQ_MAX)
154 i = simple_strtoul(buf, NULL, 16);
156 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
157 dbg->wdata->state.flags &= ~WIIPROTO_FLAG_DRM_LOCKED;
158 wiiproto_req_drm(dbg->wdata, (__u8) i);
159 if (i != WIIPROTO_REQ_NULL)
160 dbg->wdata->state.flags |= WIIPROTO_FLAG_DRM_LOCKED;
161 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
166 static const struct file_operations wiidebug_drm_fops = {
167 .owner = THIS_MODULE,
168 .open = wiidebug_drm_open,
171 .write = wiidebug_drm_write,
172 .release = single_release,
175 int wiidebug_init(struct wiimote_data *wdata)
177 struct wiimote_debug *dbg;
181 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
187 dbg->eeprom = debugfs_create_file("eeprom", S_IRUSR,
188 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_eeprom_fops);
192 dbg->drm = debugfs_create_file("drm", S_IRUSR,
193 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_drm_fops);
197 spin_lock_irqsave(&wdata->state.lock, flags);
199 spin_unlock_irqrestore(&wdata->state.lock, flags);
204 debugfs_remove(dbg->eeprom);
210 void wiidebug_deinit(struct wiimote_data *wdata)
212 struct wiimote_debug *dbg = wdata->debug;
218 spin_lock_irqsave(&wdata->state.lock, flags);
220 spin_unlock_irqrestore(&wdata->state.lock, flags);
222 debugfs_remove(dbg->drm);
223 debugfs_remove(dbg->eeprom);