2 * Debug support for HID Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
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 int wiidebug_eeprom_open(struct inode *i, struct file *f)
28 f->private_data = i->i_private;
32 static ssize_t wiidebug_eeprom_read(struct file *f, char __user *u, size_t s,
35 struct wiimote_debug *dbg = f->private_data;
36 struct wiimote_data *wdata = dbg->wdata;
49 ret = wiimote_cmd_acquire(wdata);
53 spin_lock_irqsave(&wdata->state.lock, flags);
54 wdata->state.cmd_read_size = s;
55 wdata->state.cmd_read_buf = buf;
56 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, *off & 0xffff);
57 wiiproto_req_reeprom(wdata, *off, s);
58 spin_unlock_irqrestore(&wdata->state.lock, flags);
60 ret = wiimote_cmd_wait(wdata);
62 size = wdata->state.cmd_read_size;
64 spin_lock_irqsave(&wdata->state.lock, flags);
65 wdata->state.cmd_read_buf = NULL;
66 spin_unlock_irqrestore(&wdata->state.lock, flags);
68 wiimote_cmd_release(wdata);
75 if (copy_to_user(u, buf, size))
84 static const struct file_operations wiidebug_eeprom_fops = {
86 .open = wiidebug_eeprom_open,
87 .read = wiidebug_eeprom_read,
88 .llseek = generic_file_llseek,
91 static const char *wiidebug_drmmap[] = {
92 [WIIPROTO_REQ_NULL] = "NULL",
93 [WIIPROTO_REQ_DRM_K] = "K",
94 [WIIPROTO_REQ_DRM_KA] = "KA",
95 [WIIPROTO_REQ_DRM_KE] = "KE",
96 [WIIPROTO_REQ_DRM_KAI] = "KAI",
97 [WIIPROTO_REQ_DRM_KEE] = "KEE",
98 [WIIPROTO_REQ_DRM_KAE] = "KAE",
99 [WIIPROTO_REQ_DRM_KIE] = "KIE",
100 [WIIPROTO_REQ_DRM_KAIE] = "KAIE",
101 [WIIPROTO_REQ_DRM_E] = "E",
102 [WIIPROTO_REQ_DRM_SKAI1] = "SKAI1",
103 [WIIPROTO_REQ_DRM_SKAI2] = "SKAI2",
104 [WIIPROTO_REQ_MAX] = NULL
107 static int wiidebug_drm_show(struct seq_file *f, void *p)
109 struct wiimote_debug *dbg = f->private;
110 const char *str = NULL;
114 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
115 drm = dbg->wdata->state.drm;
116 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
118 if (drm < WIIPROTO_REQ_MAX)
119 str = wiidebug_drmmap[drm];
123 seq_printf(f, "%s\n", str);
128 static int wiidebug_drm_open(struct inode *i, struct file *f)
130 return single_open(f, wiidebug_drm_show, i->i_private);
133 static ssize_t wiidebug_drm_write(struct file *f, const char __user *u,
134 size_t s, loff_t *off)
136 struct wiimote_debug *dbg = f->private_data;
145 len = min((size_t) 15, s);
146 if (copy_from_user(buf, u, len))
151 for (i = 0; i < WIIPROTO_REQ_MAX; ++i) {
152 if (!wiidebug_drmmap[i])
154 if (!strcasecmp(buf, wiidebug_drmmap[i]))
158 if (i == WIIPROTO_REQ_MAX)
159 i = simple_strtoul(buf, NULL, 10);
161 spin_lock_irqsave(&dbg->wdata->state.lock, flags);
162 wiiproto_req_drm(dbg->wdata, (__u8) i);
163 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags);
168 static const struct file_operations wiidebug_drm_fops = {
169 .owner = THIS_MODULE,
170 .open = wiidebug_drm_open,
173 .write = wiidebug_drm_write,
174 .release = single_release,
177 int wiidebug_init(struct wiimote_data *wdata)
179 struct wiimote_debug *dbg;
183 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
189 dbg->eeprom = debugfs_create_file("eeprom", S_IRUSR,
190 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_eeprom_fops);
194 dbg->drm = debugfs_create_file("drm", S_IRUSR,
195 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_drm_fops);
199 spin_lock_irqsave(&wdata->state.lock, flags);
201 spin_unlock_irqrestore(&wdata->state.lock, flags);
206 debugfs_remove(dbg->eeprom);
212 void wiidebug_deinit(struct wiimote_data *wdata)
214 struct wiimote_debug *dbg = wdata->debug;
220 spin_lock_irqsave(&wdata->state.lock, flags);
222 spin_unlock_irqrestore(&wdata->state.lock, flags);
224 debugfs_remove(dbg->drm);
225 debugfs_remove(dbg->eeprom);