refresh kernel patches
[librecmc/librecmc.git] / target / linux / adm5120 / patches-2.6.25 / 902-usb_use_debugfs_instead_of_sysfs.patch
1 Index: linux-2.6.25.4/drivers/usb/host/adm5120.h
2 ===================================================================
3 --- linux-2.6.25.4.orig/drivers/usb/host/adm5120.h
4 +++ linux-2.6.25.4/drivers/usb/host/adm5120.h
5 @@ -431,6 +431,13 @@ struct admhcd {
6  #define        OHCI_QUIRK_BE_MMIO      0x10                    /* BE registers */
7  #define        OHCI_QUIRK_ZFMICRO      0x20                    /* Compaq ZFMicro chipset*/
8         // there are also chip quirks/bugs in init logic
9 +
10 +#ifdef DEBUG
11 +       struct dentry           *debug_dir;
12 +       struct dentry           *debug_async;
13 +       struct dentry           *debug_periodic;
14 +       struct dentry           *debug_registers;
15 +#endif
16  };
17  
18  /* convert between an hcd pointer and the corresponding ahcd_hcd */
19 Index: linux-2.6.25.4/drivers/usb/host/adm5120-hcd.c
20 ===================================================================
21 --- linux-2.6.25.4.orig/drivers/usb/host/adm5120-hcd.c
22 +++ linux-2.6.25.4/drivers/usb/host/adm5120-hcd.c
23 @@ -35,6 +35,7 @@
24  #include <linux/dma-mapping.h>
25  #include <linux/dmapool.h>
26  #include <linux/reboot.h>
27 +#include <linux/debugfs.h>
28  
29  #include <asm/io.h>
30  #include <asm/irq.h>
31 @@ -799,6 +800,14 @@ static int __init admhc_hcd_mod_init(voi
32         pr_info("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
33                 sizeof (struct ed), sizeof (struct td));
34  
35 +#ifdef DEBUG
36 +       admhc_debug_root = debugfs_create_dir("admhc", NULL);
37 +       if (!admhc_debug_root) {
38 +               ret = -ENOENT;
39 +               goto error_debug;
40 +       }
41 +#endif
42 +
43  #ifdef PLATFORM_DRIVER
44         ret = platform_driver_register(&PLATFORM_DRIVER);
45         if (ret < 0)
46 @@ -811,6 +820,12 @@ static int __init admhc_hcd_mod_init(voi
47         platform_driver_unregister(&PLATFORM_DRIVER);
48  error_platform:
49  #endif
50 +
51 +#ifdef DEBUG
52 +       debugfs_remove(admhc_debug_root);
53 +       admhc_debug_root = NULL;
54 +error_debug:
55 +#endif
56         return ret;
57  }
58  module_init(admhc_hcd_mod_init);
59 @@ -818,6 +833,9 @@ module_init(admhc_hcd_mod_init);
60  static void __exit admhc_hcd_mod_exit(void)
61  {
62         platform_driver_unregister(&PLATFORM_DRIVER);
63 +#ifdef DEBUG
64 +       debugfs_remove(admhc_debug_root);
65 +#endif
66  }
67  module_exit(admhc_hcd_mod_exit);
68  
69 Index: linux-2.6.25.4/drivers/usb/host/adm5120-dbg.c
70 ===================================================================
71 --- linux-2.6.25.4.orig/drivers/usb/host/adm5120-dbg.c
72 +++ linux-2.6.25.4/drivers/usb/host/adm5120-dbg.c
73 @@ -390,6 +390,42 @@ static inline void remove_debug_files(st
74  
75  #else
76  
77 +static int debug_async_open(struct inode *, struct file *);
78 +static int debug_periodic_open(struct inode *, struct file *);
79 +static int debug_registers_open(struct inode *, struct file *);
80 +static int debug_async_open(struct inode *, struct file *);
81 +static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
82 +static int debug_close(struct inode *, struct file *);
83 +
84 +static const struct file_operations debug_async_fops = {
85 +       .owner          = THIS_MODULE,
86 +       .open           = debug_async_open,
87 +       .read           = debug_output,
88 +       .release        = debug_close,
89 +};
90 +static const struct file_operations debug_periodic_fops = {
91 +       .owner          = THIS_MODULE,
92 +       .open           = debug_periodic_open,
93 +       .read           = debug_output,
94 +       .release        = debug_close,
95 +};
96 +static const struct file_operations debug_registers_fops = {
97 +       .owner          = THIS_MODULE,
98 +       .open           = debug_registers_open,
99 +       .read           = debug_output,
100 +       .release        = debug_close,
101 +};
102 +
103 +static struct dentry *admhc_debug_root;
104 +
105 +struct debug_buffer {
106 +       ssize_t (*fill_func)(struct debug_buffer *);    /* fill method */
107 +       struct device *dev;
108 +       struct mutex mutex;     /* protect filling of buffer */
109 +       size_t count;           /* number of characters filled into buffer */
110 +       char *page;
111 +};
112 +
113  static ssize_t
114  show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
115  {
116 @@ -455,8 +491,7 @@ show_list(struct admhcd *ahcd, char *buf
117         return count - size;
118  }
119  
120 -static ssize_t
121 -show_async(struct device *dev, struct device_attribute *attr, char *buf)
122 +static ssize_t fill_async_buffer(struct debug_buffer *buf)
123  {
124         struct usb_bus          *bus;
125         struct usb_hcd          *hcd;
126 @@ -464,24 +499,22 @@ show_async(struct device *dev, struct de
127         size_t                  temp;
128         unsigned long           flags;
129  
130 -       bus = dev_get_drvdata(dev);
131 +       bus = dev_get_drvdata(buf->dev);
132         hcd = bus_to_hcd(bus);
133         ahcd = hcd_to_admhcd(hcd);
134  
135         /* display control and bulk lists together, for simplicity */
136         spin_lock_irqsave(&ahcd->lock, flags);
137 -       temp = show_list(ahcd, buf, PAGE_SIZE, ahcd->ed_head);
138 +       temp = show_list(ahcd, buf->page, buf->count, ahcd->ed_head);
139         spin_unlock_irqrestore(&ahcd->lock, flags);
140  
141         return temp;
142  }
143 -static DEVICE_ATTR(async, S_IRUGO, show_async, NULL);
144  
145  
146  #define DBG_SCHED_LIMIT 64
147  
148 -static ssize_t
149 -show_periodic(struct device *dev, struct device_attribute *attr, char *buf)
150 +static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
151  {
152         struct usb_bus          *bus;
153         struct usb_hcd          *hcd;
154 @@ -496,10 +529,10 @@ show_periodic(struct device *dev, struct
155                 return 0;
156         seen_count = 0;
157  
158 -       bus = dev_get_drvdata(dev);
159 +       bus = dev_get_drvdata(buf->dev);
160         hcd = bus_to_hcd(bus);
161         ahcd = hcd_to_admhcd(hcd);
162 -       next = buf;
163 +       next = buf->page;
164         size = PAGE_SIZE;
165  
166         temp = scnprintf(next, size, "size = %d\n", NUM_INTS);
167 @@ -574,13 +607,11 @@ show_periodic(struct device *dev, struct
168  
169         return PAGE_SIZE - size;
170  }
171 -static DEVICE_ATTR(periodic, S_IRUGO, show_periodic, NULL);
172  
173  
174  #undef DBG_SCHED_LIMIT
175  
176 -static ssize_t
177 -show_registers(struct device *dev, struct device_attribute *attr, char *buf)
178 +static ssize_t fill_registers_buffer(struct debug_buffer *buf)
179  {
180         struct usb_bus          *bus;
181         struct usb_hcd          *hcd;
182 @@ -591,11 +622,11 @@ show_registers(struct device *dev, struc
183         char                    *next;
184         u32                     rdata;
185  
186 -       bus = dev_get_drvdata(dev);
187 +       bus = dev_get_drvdata(buf->dev);
188         hcd = bus_to_hcd(bus);
189         ahcd = hcd_to_admhcd(hcd);
190         regs = ahcd->regs;
191 -       next = buf;
192 +       next = buf->page;
193         size = PAGE_SIZE;
194  
195         spin_lock_irqsave(&ahcd->lock, flags);
196 @@ -656,27 +687,154 @@ done:
197         spin_unlock_irqrestore(&ahcd->lock, flags);
198         return PAGE_SIZE - size;
199  }
200 -static DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
201  
202  
203 -static inline void create_debug_files (struct admhcd *ahcd)
204 +static struct debug_buffer *alloc_buffer(struct device *dev,
205 +                               ssize_t (*fill_func)(struct debug_buffer *))
206  {
207 -       struct device *dev = admhcd_to_hcd(ahcd)->self.dev;
208 -       int retval;
209 +       struct debug_buffer *buf;
210  
211 -       retval = device_create_file(dev, &dev_attr_async);
212 -       retval = device_create_file(dev, &dev_attr_periodic);
213 -       retval = device_create_file(dev, &dev_attr_registers);
214 -       admhc_dbg(ahcd, "created debug files\n");
215 +       buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
216 +
217 +       if (buf) {
218 +               buf->dev = dev;
219 +               buf->fill_func = fill_func;
220 +               mutex_init(&buf->mutex);
221 +       }
222 +
223 +       return buf;
224 +}
225 +
226 +static int fill_buffer(struct debug_buffer *buf)
227 +{
228 +       int ret = 0;
229 +
230 +       if (!buf->page)
231 +               buf->page = (char *)get_zeroed_page(GFP_KERNEL);
232 +
233 +       if (!buf->page) {
234 +               ret = -ENOMEM;
235 +               goto out;
236 +       }
237 +
238 +       ret = buf->fill_func(buf);
239 +
240 +       if (ret >= 0) {
241 +               buf->count = ret;
242 +               ret = 0;
243 +       }
244 +
245 +out:
246 +       return ret;
247 +}
248 +
249 +static ssize_t debug_output(struct file *file, char __user *user_buf,
250 +                       size_t len, loff_t *offset)
251 +{
252 +       struct debug_buffer *buf = file->private_data;
253 +       int ret = 0;
254 +
255 +       mutex_lock(&buf->mutex);
256 +       if (buf->count == 0) {
257 +               ret = fill_buffer(buf);
258 +               if (ret != 0) {
259 +                       mutex_unlock(&buf->mutex);
260 +                       goto out;
261 +               }
262 +       }
263 +       mutex_unlock(&buf->mutex);
264 +
265 +       ret = simple_read_from_buffer(user_buf, len, offset,
266 +                                       buf->page, buf->count);
267 +
268 +out:
269 +       return ret;
270 +}
271 +
272 +static int debug_close(struct inode *inode, struct file *file)
273 +{
274 +       struct debug_buffer *buf = file->private_data;
275 +
276 +       if (buf) {
277 +               if (buf->page)
278 +                       free_page((unsigned long)buf->page);
279 +               kfree(buf);
280 +       }
281 +
282 +       return 0;
283  }
284  
285 -static inline void remove_debug_files (struct admhcd *ahcd)
286 +static int debug_async_open(struct inode *inode, struct file *file)
287  {
288 -       struct device *dev = admhcd_to_hcd(ahcd)->self.dev;
289 +       file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
290 +
291 +       return file->private_data ? 0 : -ENOMEM;
292 +}
293 +
294 +static int debug_periodic_open(struct inode *inode, struct file *file)
295 +{
296 +       file->private_data = alloc_buffer(inode->i_private,
297 +                                               fill_periodic_buffer);
298 +
299 +       return file->private_data ? 0 : -ENOMEM;
300 +}
301  
302 -       device_remove_file(dev, &dev_attr_async);
303 -       device_remove_file(dev, &dev_attr_periodic);
304 -       device_remove_file(dev, &dev_attr_registers);
305 +static int debug_registers_open(struct inode *inode, struct file *file)
306 +{
307 +       file->private_data = alloc_buffer(inode->i_private,
308 +                                               fill_registers_buffer);
309 +
310 +       return file->private_data ? 0 : -ENOMEM;
311 +}
312 +
313 +static inline void create_debug_files(struct admhcd *ahcd)
314 +{
315 +       struct usb_bus *bus = &admhcd_to_hcd(ahcd)->self;
316 +       struct device *dev = bus->dev;
317 +
318 +       ahcd->debug_dir = debugfs_create_dir(bus->bus_name, admhc_debug_root);
319 +       if (!ahcd->debug_dir)
320 +               goto dir_error;
321 +
322 +       ahcd->debug_async = debugfs_create_file("async", S_IRUGO,
323 +                                               ahcd->debug_dir, dev,
324 +                                               &debug_async_fops);
325 +       if (!ahcd->debug_async)
326 +               goto async_error;
327 +
328 +       ahcd->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
329 +                                               ahcd->debug_dir, dev,
330 +                                               &debug_periodic_fops);
331 +       if (!ahcd->debug_periodic)
332 +               goto periodic_error;
333 +
334 +       ahcd->debug_registers = debugfs_create_file("registers", S_IRUGO,
335 +                                               ahcd->debug_dir, dev,
336 +                                               &debug_registers_fops);
337 +       if (!ahcd->debug_registers)
338 +               goto registers_error;
339 +
340 +       admhc_dbg(ahcd, "created debug files\n");
341 +       return;
342 +
343 +registers_error:
344 +       debugfs_remove(ahcd->debug_periodic);
345 +periodic_error:
346 +       debugfs_remove(ahcd->debug_async);
347 +async_error:
348 +       debugfs_remove(ahcd->debug_dir);
349 +dir_error:
350 +       ahcd->debug_periodic = NULL;
351 +       ahcd->debug_async = NULL;
352 +       ahcd->debug_dir = NULL;
353 +}
354 +
355 +static inline void remove_debug_files(struct admhcd *ahcd)
356 +{
357 +       debugfs_remove(ahcd->debug_registers);
358 +       debugfs_remove(ahcd->debug_periodic);
359 +       debugfs_remove(ahcd->debug_async);
360 +       debugfs_remove(ahcd->debug_dir);
361  }
362  
363  #endif