adm5120: merge 3.3 patches
[openwrt/openwrt.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-dbg.c
index 1027e5c8cd652f43d32213c0a3dc0c6e7c66152c..2d5dc2a5a7c5873758bc095f5f32a7389f1dc670 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ADM5120 HCD (Host Controller Driver) for USB
  *
- * Copyright (C) 2007,2008 Gabor Juhos <juhosg at openwrt.org>
+ * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  *
  * This file was derived from: drivers/usb/host/ohci-dbg.c
  *   (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
@@ -82,7 +82,7 @@ static inline char *td_togglestring(u32 info)
  * small: 0) header + data packets 1) just header
  */
 static void __attribute__((unused))
-urb_print(struct admhcd *ahcd, struct urb *urb, char *str, int small)
+urb_print(struct admhcd *ahcd, struct urb *urb, char *str, int small, int status)
 {
        unsigned int pipe = urb->pipe;
 
@@ -92,20 +92,20 @@ urb_print(struct admhcd *ahcd, struct urb *urb, char *str, int small)
        }
 
 #ifndef        ADMHC_VERBOSE_DEBUG
-       if (urb->status != 0)
+       if (status != 0)
 #endif
        admhc_dbg(ahcd, "URB-%s %p dev=%d ep=%d%s-%s flags=%x len=%d/%d "
                        "stat=%d\n",
                        str,
                        urb,
-                       usb_pipedevice (pipe),
-                       usb_pipeendpoint (pipe),
-                       usb_pipeout(pipe)? "out" : "in",
+                       usb_pipedevice(pipe),
+                       usb_pipeendpoint(pipe),
+                       usb_pipeout(pipe) ? "out" : "in",
                        pipestring(pipe),
                        urb->transfer_flags,
                        urb->actual_length,
                        urb->transfer_buffer_length,
-                       urb->status);
+                       status);
 
 #ifdef ADMHC_VERBOSE_DEBUG
        if (!small) {
@@ -114,18 +114,18 @@ urb_print(struct admhcd *ahcd, struct urb *urb, char *str, int small)
                if (usb_pipecontrol(pipe)) {
                        admhc_dbg(ahcd, "setup(8):");
                        for (i = 0; i < 8 ; i++)
-                               printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
-                       printk ("\n");
+                               printk(KERN_INFO" %02x", ((__u8 *)urb->setup_packet)[i]);
+                       printk(KERN_INFO "\n");
                }
                if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
                        admhc_dbg(ahcd, "data(%d/%d):",
                                urb->actual_length,
                                urb->transfer_buffer_length);
-                       len = usb_pipeout(pipe)?
-                                               urb->transfer_buffer_length: urb->actual_length;
+                       len = usb_pipeout(pipe) ?
+                                               urb->transfer_buffer_length : urb->actual_length;
                        for (i = 0; i < 16 && i < len; i++)
-                               printk(" %02x", ((__u8 *)urb->transfer_buffer)[i]);
-                       printk("%s stat:%d\n", i < len? "...": "", urb->status);
+                               printk(KERN_INFO " %02x", ((__u8 *)urb->transfer_buffer)[i]);
+                       printk(KERN_INFO "%s stat:%d\n", i < len ? "..." : "", status);
                }
        }
 #endif /* ADMHC_VERBOSE_DEBUG */
@@ -133,12 +133,12 @@ urb_print(struct admhcd *ahcd, struct urb *urb, char *str, int small)
 
 #define admhc_dbg_sw(ahcd, next, size, format, arg...) \
        do { \
-       if (next) { \
-               unsigned s_len; \
-               s_len = scnprintf(*next, *size, format, ## arg ); \
-               *size -= s_len; *next += s_len; \
-       } else \
-               admhc_dbg(ahcd,format, ## arg ); \
+               if (next) { \
+                       unsigned s_len; \
+                       s_len = scnprintf(*next, *size, format, ## arg); \
+                       *size -= s_len; *next += s_len; \
+               } else \
+                       admhc_dbg(ahcd, format, ## arg); \
        } while (0);
 
 
@@ -170,7 +170,7 @@ static void maybe_print_eds(struct admhcd *ahcd, char *label, u32 value,
                admhc_dbg_sw(ahcd, next, size, "%s %08x\n", label, value);
 }
 
-static char *buss2string (int state)
+static char *buss2string(int state)
 {
        switch (state) {
        case ADMHC_BUSS_RESET:
@@ -220,7 +220,7 @@ admhc_dump_status(struct admhcd *ahcd, char **next, unsigned *size)
                        admhc_readl(ahcd, &regs->hosthead), next, size);
 }
 
-#define dbg_port_sw(hc,num,value,next,size) \
+#define dbg_port_sw(hc, num, value, next, size) \
        admhc_dbg_sw(hc, next, size, \
                "portstatus [%d] " \
                "0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
@@ -286,7 +286,7 @@ static void admhc_dump(struct admhcd *ahcd, int verbose)
 
        /* dumps some of the state we know about */
        admhc_dump_status(ahcd, NULL, NULL);
-       admhc_dbg(ahcd,"current frame #%04x\n",
+       admhc_dbg(ahcd, "current frame #%04x\n",
                admhc_frame_no(ahcd));
 
        admhc_dump_roothub(ahcd, verbose, NULL, NULL);
@@ -348,7 +348,7 @@ admhc_dump_ed(const struct admhcd *ahcd, const char *label,
        tmp = hc32_to_cpup(ahcd, &ed->hwHeadP);
        admhc_dbg(ahcd, "  tds: head %08x tail %08x %s%s%s\n",
                tmp & TD_MASK,
-               hc32_to_cpup (ahcd, &ed->hwTailP),
+               hc32_to_cpup(ahcd, &ed->hwTailP),
                (tmp & ED_C) ? data1 : data0,
                (tmp & ED_H) ? " HALT" : "",
                verbose ? " td list follows" : " (not listing)");
@@ -362,7 +362,7 @@ admhc_dump_ed(const struct admhcd *ahcd, const char *label,
                list_for_each(tmp, &ed->td_list) {
                        struct td               *td;
                        td = list_entry(tmp, struct td, td_list);
-                       admhc_dump_td (ahcd, "  ->", td);
+                       admhc_dump_td(ahcd, "  ->", td);
                }
        }
 }
@@ -370,7 +370,7 @@ admhc_dump_ed(const struct admhcd *ahcd, const char *label,
 #else /* ifdef DEBUG */
 
 static inline void urb_print(struct admhcd *ahcd, struct urb * urb, char * str,
-       int small) {}
+       int small, int status) {}
 static inline void admhc_dump_ed(const struct admhcd *ahcd, const char *label,
        const struct ed *ed, int verbose) {}
 static inline void admhc_dump_td(const struct admhcd *ahcd, const char *label,
@@ -390,10 +390,49 @@ static inline void remove_debug_files(struct admhcd *bus) { }
 
 #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 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,
+       .llseek         = default_llseek,
+};
+static const struct file_operations debug_periodic_fops = {
+       .owner          = THIS_MODULE,
+       .open           = debug_periodic_open,
+       .read           = debug_output,
+       .release        = debug_close,
+       .llseek         = default_llseek,
+};
+static const struct file_operations debug_registers_fops = {
+       .owner          = THIS_MODULE,
+       .open           = debug_registers_open,
+       .read           = debug_output,
+       .release        = debug_close,
+       .llseek         = default_llseek,
+};
+
+static struct dentry *admhc_debug_root;
+
+struct debug_buffer {
+       ssize_t (*fill_func)(struct debug_buffer *);    /* fill method */
+       struct admhcd *ahcd;
+       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)
 {
-       unsigned                temp, size = count;
+       unsigned temp;
+       unsigned size = count;
 
        if (!ed)
                return 0;
@@ -411,7 +450,7 @@ show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
                        " h:%08x t:%08x",
                        ed,
                        ed_statestring(ed->state),
-                       ed_typestring (ed->type),
+                       ed_typestring(ed->type),
                        (info & ED_SPEED_FULL) ? 'f' : 'l',
                        info & ED_FA_MASK,
                        (info >> ED_EN_SHIFT) & ED_EN_MASK,
@@ -422,7 +461,7 @@ show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
                        (info & ED_SKIP) ? " S" : "",
                        (headp & ED_H) ? " H" : "",
                        (headp & ED_C) ? data1 : data0,
-                       headp & ED_MASK,tailp);
+                       headp & ED_MASK, tailp);
                size -= temp;
                buf += temp;
 
@@ -430,9 +469,9 @@ show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
                        u32             dbp, cbl;
 
                        td = list_entry(entry, struct td, td_list);
-                       info = hc32_to_cpup (ahcd, &td->hwINFO);
-                       dbp = hc32_to_cpup (ahcd, &td->hwDBP);
-                       cbl = hc32_to_cpup (ahcd, &td->hwCBL);
+                       info = hc32_to_cpup(ahcd, &td->hwINFO);
+                       dbp = hc32_to_cpup(ahcd, &td->hwDBP);
+                       cbl = hc32_to_cpup(ahcd, &td->hwCBL);
 
                        temp = scnprintf(buf, size,
                                "\n\ttd/%p %s %d %s%scc=%x urb %p (%08x,%08x)",
@@ -441,7 +480,7 @@ show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
                                TD_BL_GET(cbl),
                                (info & TD_OWN) ? "" : "DONE ",
                                (cbl & TD_IE) ? "IE " : "",
-                               TD_CC_GET (info), td->urb, info, cbl);
+                               TD_CC_GET(info), td->urb, info, cbl);
                        size -= temp;
                        buf += temp;
                }
@@ -452,39 +491,30 @@ show_list(struct admhcd *ahcd, char *buf, size_t count, struct ed *ed)
 
                ed = ed->ed_next;
        }
+
        return count - size;
 }
 
-static ssize_t
-show_async(struct class_device *class_dev, char *buf)
+static ssize_t fill_async_buffer(struct debug_buffer *buf)
 {
-       struct usb_bus          *bus;
-       struct usb_hcd          *hcd;
        struct admhcd           *ahcd;
        size_t                  temp;
        unsigned long           flags;
 
-       bus = class_get_devdata(class_dev);
-       hcd = bus_to_hcd(bus);
-       ahcd = hcd_to_admhcd(hcd);
+       ahcd = buf->ahcd;
 
-       /* 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, PAGE_SIZE, ahcd->ed_head);
        spin_unlock_irqrestore(&ahcd->lock, flags);
 
        return temp;
 }
-static CLASS_DEVICE_ATTR(async, S_IRUGO, show_async, NULL);
 
 
 #define DBG_SCHED_LIMIT 64
 
-static ssize_t
-show_periodic(struct class_device *class_dev, char *buf)
+static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
 {
-       struct usb_bus          *bus;
-       struct usb_hcd          *hcd;
        struct admhcd           *ahcd;
        struct ed               **seen, *ed;
        unsigned long           flags;
@@ -492,14 +522,13 @@ show_periodic(struct class_device *class_dev, char *buf)
        char                    *next;
        unsigned                i;
 
-       if (!(seen = kmalloc(DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
+       seen = kmalloc(DBG_SCHED_LIMIT * sizeof(*seen), GFP_ATOMIC);
+       if (!seen)
                return 0;
        seen_count = 0;
 
-       bus = class_get_devdata(class_dev);
-       hcd = bus_to_hcd(bus);
-       ahcd = hcd_to_admhcd(hcd);
-       next = buf;
+       ahcd = buf->ahcd;
+       next = buf->page;
        size = PAGE_SIZE;
 
        temp = scnprintf(next, size, "size = %d\n", NUM_INTS);
@@ -509,10 +538,11 @@ show_periodic(struct class_device *class_dev, char *buf)
        /* dump a snapshot of the periodic schedule (and load) */
        spin_lock_irqsave(&ahcd->lock, flags);
        for (i = 0; i < NUM_INTS; i++) {
-               if (!(ed = ahcd->periodic [i]))
+               ed = ahcd->periodic[i];
+               if (!ed)
                        continue;
 
-               temp = scnprintf(next, size, "%2d [%3d]:", i, ahcd->load [i]);
+               temp = scnprintf(next, size, "%2d [%3d]:", i, ahcd->load[i]);
                size -= temp;
                next += temp;
 
@@ -522,18 +552,18 @@ show_periodic(struct class_device *class_dev, char *buf)
                        size -= temp;
                        next += temp;
                        for (temp = 0; temp < seen_count; temp++) {
-                               if (seen [temp] == ed)
+                               if (seen[temp] == ed)
                                        break;
                        }
 
                        /* show more info the first time around */
                        if (temp == seen_count) {
-                               u32     info = hc32_to_cpu (ahcd, ed->hwINFO);
+                               u32     info = hc32_to_cpu(ahcd, ed->hwINFO);
                                struct list_head        *entry;
                                unsigned                qlen = 0;
 
                                /* qlen measured here in TDs, not urbs */
-                               list_for_each (entry, &ed->td_list)
+                               list_for_each(entry, &ed->td_list)
                                        qlen++;
                                temp = scnprintf(next, size,
                                        " (%cs dev%d ep%d%s qlen %u"
@@ -553,7 +583,7 @@ show_periodic(struct class_device *class_dev, char *buf)
                                next += temp;
 
                                if (seen_count < DBG_SCHED_LIMIT)
-                                       seen [seen_count++] = ed;
+                                       seen[seen_count++] = ed;
 
                                ed = ed->ed_next;
 
@@ -570,19 +600,16 @@ show_periodic(struct class_device *class_dev, char *buf)
                next += temp;
        }
        spin_unlock_irqrestore(&ahcd->lock, flags);
-       kfree (seen);
+       kfree(seen);
 
        return PAGE_SIZE - size;
 }
-static CLASS_DEVICE_ATTR(periodic, S_IRUGO, show_periodic, NULL);
 
 
 #undef DBG_SCHED_LIMIT
 
-static ssize_t
-show_registers(struct class_device *class_dev, char *buf)
+static ssize_t fill_registers_buffer(struct debug_buffer *buf)
 {
-       struct usb_bus          *bus;
        struct usb_hcd          *hcd;
        struct admhcd           *ahcd;
        struct admhcd_regs __iomem *regs;
@@ -591,11 +618,10 @@ show_registers(struct class_device *class_dev, char *buf)
        char                    *next;
        u32                     rdata;
 
-       bus = class_get_devdata(class_dev);
-       hcd = bus_to_hcd(bus);
-       ahcd = hcd_to_admhcd(hcd);
+       ahcd = buf->ahcd;
+       hcd = admhc_to_hcd(ahcd);
        regs = ahcd->regs;
-       next = buf;
+       next = buf->page;
        size = PAGE_SIZE;
 
        spin_lock_irqsave(&ahcd->lock, flags);
@@ -605,13 +631,13 @@ show_registers(struct class_device *class_dev, char *buf)
        admhc_dbg_sw(ahcd, &next, &size,
                "bus %s, device %s\n"
                "%s\n"
-               "%s version " DRIVER_VERSION "\n",
+               "%s\n",
                hcd->self.controller->bus->name,
-               hcd->self.controller->bus_id,
+               dev_name(hcd->self.controller),
                hcd->product_desc,
                hcd_name);
 
-       if (bus->controller->power.power_state.event) {
+       if (!HCD_HW_ACCESSIBLE(hcd)) {
                size -= scnprintf(next, size,
                        "SUSPENDED (no register access)\n");
                goto done;
@@ -656,27 +682,153 @@ done:
        spin_unlock_irqrestore(&ahcd->lock, flags);
        return PAGE_SIZE - size;
 }
-static CLASS_DEVICE_ATTR(registers, S_IRUGO, show_registers, NULL);
 
 
-static inline void create_debug_files (struct admhcd *ahcd)
+static struct debug_buffer *alloc_buffer(struct admhcd *ahcd,
+                               ssize_t (*fill_func)(struct debug_buffer *))
 {
-       struct class_device *cldev = admhcd_to_hcd(ahcd)->self.class_dev;
-       int retval;
+       struct debug_buffer *buf;
 
-       retval = class_device_create_file(cldev, &class_device_attr_async);
-       retval = class_device_create_file(cldev, &class_device_attr_periodic);
-       retval = class_device_create_file(cldev, &class_device_attr_registers);
-       admhc_dbg(ahcd, "created debug files\n");
+       buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
+
+       if (buf) {
+               buf->ahcd = ahcd;
+               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 int debug_async_open(struct inode *inode, struct file *file)
+{
+       file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
+
+       return file->private_data ? 0 : -ENOMEM;
 }
 
-static inline void remove_debug_files (struct admhcd *ahcd)
+static int debug_periodic_open(struct inode *inode, struct file *file)
 {
-       struct class_device *cldev = admhcd_to_hcd(ahcd)->self.class_dev;
+       file->private_data = alloc_buffer(inode->i_private,
+                                               fill_periodic_buffer);
 
-       class_device_remove_file(cldev, &class_device_attr_async);
-       class_device_remove_file(cldev, &class_device_attr_periodic);
-       class_device_remove_file(cldev, &class_device_attr_registers);
+       return file->private_data ? 0 : -ENOMEM;
+}
+
+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;
+
+       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, ahcd,
+                                               &debug_async_fops);
+       if (!ahcd->debug_async)
+               goto async_error;
+
+       ahcd->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
+                                               ahcd->debug_dir, ahcd,
+                                               &debug_periodic_fops);
+       if (!ahcd->debug_periodic)
+               goto periodic_error;
+
+       ahcd->debug_registers = debugfs_create_file("registers", S_IRUGO,
+                                               ahcd->debug_dir, ahcd,
+                                               &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