blob: f81243c86790374213ed3917d330574fe885b59c [file] [log] [blame]
David Herrmann43e5e7c2011-11-17 14:12:10 +01001/*
2 * Debug support for HID Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
4 */
5
6/*
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)
10 * any later version.
11 */
12
David Herrmann1d3452c2011-11-17 14:12:11 +010013#include <linux/debugfs.h>
David Herrmann43e5e7c2011-11-17 14:12:10 +010014#include <linux/module.h>
15#include <linux/spinlock.h>
David Herrmann1d3452c2011-11-17 14:12:11 +010016#include <linux/uaccess.h>
David Herrmann43e5e7c2011-11-17 14:12:10 +010017#include "hid-wiimote.h"
18
19struct wiimote_debug {
20 struct wiimote_data *wdata;
David Herrmann1d3452c2011-11-17 14:12:11 +010021 struct dentry *eeprom;
22};
23
24static int wiidebug_eeprom_open(struct inode *i, struct file *f)
25{
26 f->private_data = i->i_private;
27 return 0;
28}
29
30static ssize_t wiidebug_eeprom_read(struct file *f, char __user *u, size_t s,
31 loff_t *off)
32{
33 struct wiimote_debug *dbg = f->private_data;
34 struct wiimote_data *wdata = dbg->wdata;
35 unsigned long flags;
36 ssize_t ret;
37 char buf[16];
38 __u16 size;
39
40 if (s == 0)
41 return -EINVAL;
42 if (*off > 0xffffff)
43 return 0;
44 if (s > 16)
45 s = 16;
46
47 ret = wiimote_cmd_acquire(wdata);
48 if (ret)
49 return ret;
50
51 spin_lock_irqsave(&wdata->state.lock, flags);
52 wdata->state.cmd_read_size = s;
53 wdata->state.cmd_read_buf = buf;
54 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, *off & 0xffff);
55 wiiproto_req_reeprom(wdata, *off, s);
56 spin_unlock_irqrestore(&wdata->state.lock, flags);
57
58 ret = wiimote_cmd_wait(wdata);
59 if (!ret)
60 size = wdata->state.cmd_read_size;
61
62 spin_lock_irqsave(&wdata->state.lock, flags);
63 wdata->state.cmd_read_buf = NULL;
64 spin_unlock_irqrestore(&wdata->state.lock, flags);
65
66 wiimote_cmd_release(wdata);
67
68 if (ret)
69 return ret;
70 else if (size == 0)
71 return -EIO;
72
73 if (copy_to_user(u, buf, size))
74 return -EFAULT;
75
76 *off += size;
77 ret = size;
78
79 return ret;
80}
81
82static const struct file_operations wiidebug_eeprom_fops = {
83 .owner = THIS_MODULE,
84 .open = wiidebug_eeprom_open,
85 .read = wiidebug_eeprom_read,
86 .llseek = generic_file_llseek,
David Herrmann43e5e7c2011-11-17 14:12:10 +010087};
88
89int wiidebug_init(struct wiimote_data *wdata)
90{
91 struct wiimote_debug *dbg;
92 unsigned long flags;
93
94 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
95 if (!dbg)
96 return -ENOMEM;
97
98 dbg->wdata = wdata;
99
David Herrmann1d3452c2011-11-17 14:12:11 +0100100 dbg->eeprom = debugfs_create_file("eeprom", S_IRUSR,
101 dbg->wdata->hdev->debug_dir, dbg, &wiidebug_eeprom_fops);
102 if (!dbg->eeprom) {
103 kfree(dbg);
104 return -ENOMEM;
105 }
106
David Herrmann43e5e7c2011-11-17 14:12:10 +0100107 spin_lock_irqsave(&wdata->state.lock, flags);
108 wdata->debug = dbg;
109 spin_unlock_irqrestore(&wdata->state.lock, flags);
110
111 return 0;
112}
113
114void wiidebug_deinit(struct wiimote_data *wdata)
115{
116 struct wiimote_debug *dbg = wdata->debug;
117 unsigned long flags;
118
119 if (!dbg)
120 return;
121
122 spin_lock_irqsave(&wdata->state.lock, flags);
123 wdata->debug = NULL;
124 spin_unlock_irqrestore(&wdata->state.lock, flags);
125
David Herrmann1d3452c2011-11-17 14:12:11 +0100126 debugfs_remove(dbg->eeprom);
David Herrmann43e5e7c2011-11-17 14:12:10 +0100127 kfree(dbg);
128}