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