summaryrefslogtreecommitdiffstats
path: root/target/linux/adm5120/patches-2.6.25/902-usb_use_debugfs_instead_of_sysfs.patch
blob: 5b513c0f66a5c6387efaf80a3dc6953fb5b598c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
78
79
80
81
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
--- a/drivers/usb/host/adm5120.h
+++ b/drivers/usb/host/adm5120.h
@@ -431,6 +431,13 @@
 #define	OHCI_QUIRK_BE_MMIO	0x10			/* BE registers */
 #define	OHCI_QUIRK_ZFMICRO	0x20			/* Compaq ZFMicro chipset*/
 	// there are also chip quirks/bugs in init logic
+
+#ifdef DEBUG
+	struct dentry		*debug_dir;
+	struct dentry		*debug_async;
+	struct dentry		*debug_periodic;
+	struct dentry		*debug_registers;
+#endif
 };
 
 /* convert between an hcd pointer and the corresponding ahcd_hcd */
--- a/drivers/usb/host/adm5120-hcd.c
+++ b/drivers/usb/host/adm5120-hcd.c
@@ -35,6 +35,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/dmapool.h>
 #include <linux/reboot.h>
+#include <linux/debugfs.h>
 
 #include <asm/io.h>
 #include <asm/irq.h>
@@ -804,6 +805,14 @@
 	pr_info("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
 		sizeof (struct ed), sizeof (struct td));
 
+#ifdef DEBUG
+	admhc_debug_root = debugfs_create_dir("admhc", NULL);
+	if (!admhc_debug_root) {
+		ret = -ENOENT;
+		goto error_debug;
+	}
+#endif
+
 #ifdef PLATFORM_DRIVER
 	ret = platform_driver_register(&PLATFORM_DRIVER);
 	if (ret < 0)
@@ -816,6 +825,12 @@
 	platform_driver_unregister(&PLATFORM_DRIVER);
 error_platform:
 #endif
+
+#ifdef DEBUG
+	debugfs_remove(admhc_debug_root);
+	admhc_debug_root = NULL;
+error_debug:
+#endif
 	return ret;
 }
 module_init(admhc_hcd_mod_init);
@@ -823,6 +838,9 @@
 static void __exit admhc_hcd_mod_exit(void)
 {
 	platform_driver_unregister(&PLATFORM_DRIVER);
+#ifdef DEBUG
+	debugfs_remove(admhc_debug_root);
+#endif
 }
 module_exit(admhc_hcd_mod_exit);
 
--- a/drivers/usb/host/adm5120-dbg.c
+++ b/drivers/usb/host/adm5120-dbg.c
@@ -390,6 +390,42 @@
 
 #else
 
+static int debug_async_open(struct inode *, struct file *);
+static int debug_periodic_open(struct inode *, struct file *);
+static int debug_registers_open(struct inode *, struct file *);
+static int debug_async_open(struct inode *, struct file *);
+static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
+static int debug_close(struct inode *, struct file *);
+
+static const struct file_operations debug_async_fops = {
+	.owner		= THIS_MODULE,
+	.open		= debug_async_open,
+	.read		= debug_output,
+	.release	= debug_close,
+};
+static const struct file_operations debug_periodic_fops = {
+	.owner		= THIS_MODULE,
+	.open		= debug_periodic_open,
+	.read		= debug_output,
+	.release	= debug_close,
+};
+static const struct file_operations debug_registers_fops = {
+	.owner		= THIS_MODULE,
+	.open		= debug_registers_open,
+	.read		= debug_output,
+	.release	= debug_close,
+};
+
+static struct dentry *admhc_debug_root;
+
+struct debug_buffer {
+	ssize_t (*fill_func)(struct debug_buffer *);    /* fill method */
+	struct device *dev;
+	struct mutex mutex;     /* protect filling of buffer */
+	size_t count;           /* number of characters filled into buffer */
+	char *page;
+};
+
 static ssize_t
 show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
 {
@@ -455,8 +491,7 @@
 	return count - size;
 }
 
-static ssize_t
-show_async(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t fill_async_buffer(struct debug_buffer *buf)
 {
 	struct usb_bus		*bus;
 	struct usb_hcd		*hcd;
@@ -464,24 +499,22 @@
 	size_t			temp;
 	unsigned long		flags;
 
-	bus = dev_get_drvdata(dev);
+	bus = dev_get_drvdata(buf->dev);
 	hcd = bus_to_hcd(bus);
 	ahcd = hcd_to_admhcd(hcd);
 
 	/* display control and bulk lists together, for simplicity */
 	spin_lock_irqsave(&ahcd->lock, flags);
-	temp = show_list(ahcd, buf, PAGE_SIZE, ahcd->ed_head);
+	temp = show_list(ahcd, buf->page, buf->count, ahcd->ed_head);
 	spin_unlock_irqrestore(&ahcd->lock, flags);
 
 	return temp;
 }
-static DEVICE_ATTR(async, S_IRUGO, show_async, NULL);
 
 
 #define DBG_SCHED_LIMIT 64
 
-static ssize_t
-show_periodic(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
 {
 	struct usb_bus		*bus;
 	struct usb_hcd		*hcd;
@@ -496,10 +529,10 @@
 		return 0;
 	seen_count = 0;
 
-	bus = dev_get_drvdata(dev);
+	bus = dev_get_drvdata(buf->dev);
 	hcd = bus_to_hcd(bus);
 	ahcd = hcd_to_admhcd(hcd);
-	next = buf;
+	next = buf->page;
 	size = PAGE_SIZE;
 
 	temp = scnprintf(next, size, "size = %d\n", NUM_INTS);
@@ -574,13 +607,11 @@
 
 	return PAGE_SIZE - size;
 }
-static DEVICE_ATTR(periodic, S_IRUGO, show_periodic, NULL);
 
 
 #undef DBG_SCHED_LIMIT
 
-static ssize_t
-show_registers(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t fill_registers_buffer(struct debug_buffer *buf)
 {
 	struct usb_bus		*bus;
 	struct usb_hcd		*hcd;
@@ -591,11 +622,11 @@
 	char			*next;
 	u32			rdata;
 
-	bus = dev_get_drvdata(dev);
+	bus = dev_get_drvdata(buf->dev);
 	hcd = bus_to_hcd(bus);
 	ahcd = hcd_to_admhcd(hcd);
 	regs = ahcd->regs;
-	next = buf;
+	next = buf->page;
 	size = PAGE_SIZE;
 
 	spin_lock_irqsave(&ahcd->lock, flags);
@@ -656,27 +687,154 @@
 	spin_unlock_irqrestore(&ahcd->lock, flags);
 	return PAGE_SIZE - size;
 }
-static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
 
 
-static inline void create_debug_files (struct admhcd *ahcd)
+static struct debug_buffer *alloc_buffer(struct device *dev,
+				ssize_t (*fill_func)(struct debug_buffer *))
 {
-	struct device *dev = admhcd_to_hcd(ahcd)->self.dev;
-	int retval;
+	struct debug_buffer *buf;
 
-	retval = device_create_file(dev, &dev_attr_async);
-	retval = device_create_file(dev, &dev_attr_periodic);
-	retval = device_create_file(dev, &dev_attr_registers);
-	admhc_dbg(ahcd, "created debug files\n");
+	buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
+
+	if (buf) {
+		buf->dev = dev;
+		buf->fill_func = fill_func;
+		mutex_init(&buf->mutex);
+	}
+
+	return buf;
+}
+
+static int fill_buffer(struct debug_buffer *buf)
+{
+	int ret = 0;
+
+	if (!buf->page)
+		buf->page = (char *)get_zeroed_page(GFP_KERNEL);
+
+	if (!buf->page) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = buf->fill_func(buf);
+
+	if (ret >= 0) {
+		buf->count = ret;
+		ret = 0;
+	}
+
+out:
+	return ret;
+}
+
+static ssize_t debug_output(struct file *file, char __user *user_buf,
+			size_t len, loff_t *offset)
+{
+	struct debug_buffer *buf = file->private_data;
+	int ret = 0;
+
+	mutex_lock(&buf->mutex);
+	if (buf->count == 0) {
+		ret = fill_buffer(buf);
+		if (ret != 0) {
+			mutex_unlock(&buf->mutex);
+			goto out;
+		}
+	}
+	mutex_unlock(&buf->mutex);
+
+	ret = simple_read_from_buffer(user_buf, len, offset,
+					buf->page, buf->count);
+
+out:
+	return ret;
+}
+
+static int debug_close(struct inode *inode, struct file *file)
+{
+	struct debug_buffer *buf = file->private_data;
+
+	if (buf) {
+		if (buf->page)
+			free_page((unsigned long)buf->page);
+		kfree(buf);
+	}
+
+	return 0;
 }
 
-static inline void remove_debug_files (struct admhcd *ahcd)
+static int debug_async_open(struct inode *inode, struct file *file)
 {
-	struct device *dev = admhcd_to_hcd(ahcd)->self.dev;
+	file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
+
+	return file->private_data ? 0 : -ENOMEM;
+}
+
+static int debug_periodic_open(struct inode *inode, struct file *file)
+{
+	file->private_data = alloc_buffer(inode->i_private,
+						fill_periodic_buffer);
+
+	return file->private_data ? 0 : -ENOMEM;
+}
 
-	device_remove_file(dev, &dev_attr_async);
-	device_remove_file(dev, &dev_attr_periodic);
-	device_remove_file(dev, &dev_attr_registers);
+static int debug_registers_open(struct inode *inode, struct file *file)
+{
+	file->private_data = alloc_buffer(inode->i_private,
+						fill_registers_buffer);
+
+	return file->private_data ? 0 : -ENOMEM;
+}
+
+static inline void create_debug_files(struct admhcd *ahcd)
+{
+	struct usb_bus *bus = &admhcd_to_hcd(ahcd)->self;
+	struct device *dev = bus->dev;
+
+	ahcd->debug_dir = debugfs_create_dir(bus->bus_name, admhc_debug_root);
+	if (!ahcd->debug_dir)
+		goto dir_error;
+
+	ahcd->debug_async = debugfs_create_file("async", S_IRUGO,
+						ahcd->debug_dir, dev,
+						&debug_async_fops);
+	if (!ahcd->debug_async)
+		goto async_error;
+
+	ahcd->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
+						ahcd->debug_dir, dev,
+						&debug_periodic_fops);
+	if (!ahcd->debug_periodic)
+		goto periodic_error;
+
+	ahcd->debug_registers = debugfs_create_file("registers", S_IRUGO,
+						ahcd->debug_dir, dev,
+						&debug_registers_fops);
+	if (!ahcd->debug_registers)
+		goto registers_error;
+
+	admhc_dbg(ahcd, "created debug files\n");
+	return;
+
+registers_error:
+	debugfs_remove(ahcd->debug_periodic);
+periodic_error:
+	debugfs_remove(ahcd->debug_async);
+async_error:
+	debugfs_remove(ahcd->debug_dir);
+dir_error:
+	ahcd->debug_periodic = NULL;
+	ahcd->debug_async = NULL;
+	ahcd->debug_dir = NULL;
+}
+
+static inline void remove_debug_files(struct admhcd *ahcd)
+{
+	debugfs_remove(ahcd->debug_registers);
+	debugfs_remove(ahcd->debug_periodic);
+	debugfs_remove(ahcd->debug_async);
+	debugfs_remove(ahcd->debug_dir);
 }
 
 #endif