Blame view

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

43e5e7c60   David Herrmann   HID: wiimote: Add...
192
193
194
195
196
  	spin_lock_irqsave(&wdata->state.lock, flags);
  	wdata->debug = dbg;
  	spin_unlock_irqrestore(&wdata->state.lock, flags);
  
  	return 0;
43d782ae8   David Herrmann   HID: wiimote: All...
197
198
199
200
201
202
  
  err_drm:
  	debugfs_remove(dbg->eeprom);
  err:
  	kfree(dbg);
  	return ret;
43e5e7c60   David Herrmann   HID: wiimote: Add...
203
204
205
206
207
208
209
210
211
212
213
214
215
  }
  
  void wiidebug_deinit(struct wiimote_data *wdata)
  {
  	struct wiimote_debug *dbg = wdata->debug;
  	unsigned long flags;
  
  	if (!dbg)
  		return;
  
  	spin_lock_irqsave(&wdata->state.lock, flags);
  	wdata->debug = NULL;
  	spin_unlock_irqrestore(&wdata->state.lock, flags);
43d782ae8   David Herrmann   HID: wiimote: All...
216
  	debugfs_remove(dbg->drm);
1d3452c63   David Herrmann   HID: wiimote: All...
217
  	debugfs_remove(dbg->eeprom);
43e5e7c60   David Herrmann   HID: wiimote: Add...
218
219
  	kfree(dbg);
  }