linux/drivers/usb/mon/mon_text.c
<<
>>
Prefs
   1/*
   2 * The USB Monitor, inspired by Dave Harding's USBMon.
   3 *
   4 * This is a text format reader.
   5 */
   6
   7#include <linux/kernel.h>
   8#include <linux/list.h>
   9#include <linux/usb.h>
  10#include <linux/time.h>
  11#include <linux/mutex.h>
  12#include <linux/debugfs.h>
  13#include <asm/uaccess.h>
  14
  15#include "usb_mon.h"
  16
  17/*
  18 * No, we do not want arbitrarily long data strings.
  19 * Use the binary interface if you want to capture bulk data!
  20 */
  21#define DATA_MAX  32
  22
  23/*
  24 * Defined by USB 2.0 clause 9.3, table 9.2.
  25 */
  26#define SETUP_MAX  8
  27
  28/*
  29 * This limit exists to prevent OOMs when the user process stops reading.
  30 * If usbmon were available to unprivileged processes, it might be open
  31 * to a local DoS. But we have to keep to root in order to prevent
  32 * password sniffing from HID devices.
  33 */
  34#define EVENT_MAX  (4*PAGE_SIZE / sizeof(struct mon_event_text))
  35
  36/*
  37 * Potentially unlimited number; we limit it for similar allocations.
  38 * The usbfs limits this to 128, but we're not quite as generous.
  39 */
  40#define ISODESC_MAX   5
  41
  42#define PRINTF_DFL  250   /* with 5 ISOs segs */
  43
  44struct mon_iso_desc {
  45        int status;
  46        unsigned int offset;
  47        unsigned int length;    /* Unsigned here, signed in URB. Historic. */
  48};
  49
  50struct mon_event_text {
  51        struct list_head e_link;
  52        int type;               /* submit, complete, etc. */
  53        unsigned long id;       /* From pointer, most of the time */
  54        unsigned int tstamp;
  55        int busnum;
  56        char devnum;
  57        char epnum;
  58        char is_in;
  59        char xfertype;
  60        int length;             /* Depends on type: xfer length or act length */
  61        int status;
  62        int interval;
  63        int start_frame;
  64        int error_count;
  65        char setup_flag;
  66        char data_flag;
  67        int numdesc;            /* Full number */
  68        struct mon_iso_desc isodesc[ISODESC_MAX];
  69        unsigned char setup[SETUP_MAX];
  70        unsigned char data[DATA_MAX];
  71};
  72
  73#define SLAB_NAME_SZ  30
  74struct mon_reader_text {
  75        struct kmem_cache *e_slab;
  76        int nevents;
  77        struct list_head e_list;
  78        struct mon_reader r;    /* In C, parent class can be placed anywhere */
  79
  80        wait_queue_head_t wait;
  81        int printf_size;
  82        char *printf_buf;
  83        struct mutex printf_lock;
  84
  85        char slab_name[SLAB_NAME_SZ];
  86};
  87
  88static struct dentry *mon_dir;          /* Usually /sys/kernel/debug/usbmon */
  89
  90static void mon_text_ctor(void *);
  91
  92struct mon_text_ptr {
  93        int cnt, limit;
  94        char *pbuf;
  95};
  96
  97static struct mon_event_text *
  98    mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
  99static void mon_text_read_head_t(struct mon_reader_text *rp,
 100        struct mon_text_ptr *p, const struct mon_event_text *ep);
 101static void mon_text_read_head_u(struct mon_reader_text *rp,
 102        struct mon_text_ptr *p, const struct mon_event_text *ep);
 103static void mon_text_read_statset(struct mon_reader_text *rp,
 104        struct mon_text_ptr *p, const struct mon_event_text *ep);
 105static void mon_text_read_intstat(struct mon_reader_text *rp,
 106        struct mon_text_ptr *p, const struct mon_event_text *ep);
 107static void mon_text_read_isostat(struct mon_reader_text *rp,
 108        struct mon_text_ptr *p, const struct mon_event_text *ep);
 109static void mon_text_read_isodesc(struct mon_reader_text *rp,
 110        struct mon_text_ptr *p, const struct mon_event_text *ep);
 111static void mon_text_read_data(struct mon_reader_text *rp,
 112    struct mon_text_ptr *p, const struct mon_event_text *ep);
 113
 114/*
 115 * mon_text_submit
 116 * mon_text_complete
 117 *
 118 * May be called from an interrupt.
 119 *
 120 * This is called with the whole mon_bus locked, so no additional lock.
 121 */
 122
 123static inline char mon_text_get_setup(struct mon_event_text *ep,
 124    struct urb *urb, char ev_type, struct mon_bus *mbus)
 125{
 126
 127        if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
 128                return '-';
 129
 130        if (urb->setup_packet == NULL)
 131                return 'Z';     /* '0' would be not as pretty. */
 132
 133        memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
 134        return 0;
 135}
 136
 137static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
 138    int len, char ev_type, struct mon_bus *mbus)
 139{
 140        if (len <= 0)
 141                return 'L';
 142        if (len >= DATA_MAX)
 143                len = DATA_MAX;
 144
 145        if (ep->is_in) {
 146                if (ev_type != 'C')
 147                        return '<';
 148        } else {
 149                if (ev_type != 'S')
 150                        return '>';
 151        }
 152
 153        if (urb->transfer_buffer == NULL)
 154                return 'Z';     /* '0' would be not as pretty. */
 155
 156        memcpy(ep->data, urb->transfer_buffer, len);
 157        return 0;
 158}
 159
 160static inline unsigned int mon_get_timestamp(void)
 161{
 162        struct timeval tval;
 163        unsigned int stamp;
 164
 165        do_gettimeofday(&tval);
 166        stamp = tval.tv_sec & 0xFFFF;   /* 2^32 = 4294967296. Limit to 4096s. */
 167        stamp = stamp * 1000000 + tval.tv_usec;
 168        return stamp;
 169}
 170
 171static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
 172    char ev_type, int status)
 173{
 174        struct mon_event_text *ep;
 175        unsigned int stamp;
 176        struct usb_iso_packet_descriptor *fp;
 177        struct mon_iso_desc *dp;
 178        int i, ndesc;
 179
 180        stamp = mon_get_timestamp();
 181
 182        if (rp->nevents >= EVENT_MAX ||
 183            (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
 184                rp->r.m_bus->cnt_text_lost++;
 185                return;
 186        }
 187
 188        ep->type = ev_type;
 189        ep->id = (unsigned long) urb;
 190        ep->busnum = urb->dev->bus->busnum;
 191        ep->devnum = urb->dev->devnum;
 192        ep->epnum = usb_endpoint_num(&urb->ep->desc);
 193        ep->xfertype = usb_endpoint_type(&urb->ep->desc);
 194        ep->is_in = usb_urb_dir_in(urb);
 195        ep->tstamp = stamp;
 196        ep->length = (ev_type == 'S') ?
 197            urb->transfer_buffer_length : urb->actual_length;
 198        /* Collecting status makes debugging sense for submits, too */
 199        ep->status = status;
 200
 201        if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
 202                ep->interval = urb->interval;
 203        } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
 204                ep->interval = urb->interval;
 205                ep->start_frame = urb->start_frame;
 206                ep->error_count = urb->error_count;
 207        }
 208        ep->numdesc = urb->number_of_packets;
 209        if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
 210                        urb->number_of_packets > 0) {
 211                if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
 212                        ndesc = ISODESC_MAX;
 213                fp = urb->iso_frame_desc;
 214                dp = ep->isodesc;
 215                for (i = 0; i < ndesc; i++) {
 216                        dp->status = fp->status;
 217                        dp->offset = fp->offset;
 218                        dp->length = (ev_type == 'S') ?
 219                            fp->length : fp->actual_length;
 220                        fp++;
 221                        dp++;
 222                }
 223        }
 224
 225        ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
 226        ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
 227                        rp->r.m_bus);
 228
 229        rp->nevents++;
 230        list_add_tail(&ep->e_link, &rp->e_list);
 231        wake_up(&rp->wait);
 232}
 233
 234static void mon_text_submit(void *data, struct urb *urb)
 235{
 236        struct mon_reader_text *rp = data;
 237        mon_text_event(rp, urb, 'S', -EINPROGRESS);
 238}
 239
 240static void mon_text_complete(void *data, struct urb *urb, int status)
 241{
 242        struct mon_reader_text *rp = data;
 243        mon_text_event(rp, urb, 'C', status);
 244}
 245
 246static void mon_text_error(void *data, struct urb *urb, int error)
 247{
 248        struct mon_reader_text *rp = data;
 249        struct mon_event_text *ep;
 250
 251        if (rp->nevents >= EVENT_MAX ||
 252            (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
 253                rp->r.m_bus->cnt_text_lost++;
 254                return;
 255        }
 256
 257        ep->type = 'E';
 258        ep->id = (unsigned long) urb;
 259        ep->busnum = 0;
 260        ep->devnum = urb->dev->devnum;
 261        ep->epnum = usb_endpoint_num(&urb->ep->desc);
 262        ep->xfertype = usb_endpoint_type(&urb->ep->desc);
 263        ep->is_in = usb_urb_dir_in(urb);
 264        ep->tstamp = 0;
 265        ep->length = 0;
 266        ep->status = error;
 267
 268        ep->setup_flag = '-';
 269        ep->data_flag = 'E';
 270
 271        rp->nevents++;
 272        list_add_tail(&ep->e_link, &rp->e_list);
 273        wake_up(&rp->wait);
 274}
 275
 276/*
 277 * Fetch next event from the circular buffer.
 278 */
 279static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
 280    struct mon_bus *mbus)
 281{
 282        struct list_head *p;
 283        unsigned long flags;
 284
 285        spin_lock_irqsave(&mbus->lock, flags);
 286        if (list_empty(&rp->e_list)) {
 287                spin_unlock_irqrestore(&mbus->lock, flags);
 288                return NULL;
 289        }
 290        p = rp->e_list.next;
 291        list_del(p);
 292        --rp->nevents;
 293        spin_unlock_irqrestore(&mbus->lock, flags);
 294        return list_entry(p, struct mon_event_text, e_link);
 295}
 296
 297/*
 298 */
 299static int mon_text_open(struct inode *inode, struct file *file)
 300{
 301        struct mon_bus *mbus;
 302        struct mon_reader_text *rp;
 303        int rc;
 304
 305        mutex_lock(&mon_lock);
 306        mbus = inode->i_private;
 307
 308        rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
 309        if (rp == NULL) {
 310                rc = -ENOMEM;
 311                goto err_alloc;
 312        }
 313        INIT_LIST_HEAD(&rp->e_list);
 314        init_waitqueue_head(&rp->wait);
 315        mutex_init(&rp->printf_lock);
 316
 317        rp->printf_size = PRINTF_DFL;
 318        rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
 319        if (rp->printf_buf == NULL) {
 320                rc = -ENOMEM;
 321                goto err_alloc_pr;
 322        }
 323
 324        rp->r.m_bus = mbus;
 325        rp->r.r_data = rp;
 326        rp->r.rnf_submit = mon_text_submit;
 327        rp->r.rnf_error = mon_text_error;
 328        rp->r.rnf_complete = mon_text_complete;
 329
 330        snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
 331        rp->e_slab = kmem_cache_create(rp->slab_name,
 332            sizeof(struct mon_event_text), sizeof(long), 0,
 333            mon_text_ctor);
 334        if (rp->e_slab == NULL) {
 335                rc = -ENOMEM;
 336                goto err_slab;
 337        }
 338
 339        mon_reader_add(mbus, &rp->r);
 340
 341        file->private_data = rp;
 342        mutex_unlock(&mon_lock);
 343        return 0;
 344
 345// err_busy:
 346//      kmem_cache_destroy(rp->e_slab);
 347err_slab:
 348        kfree(rp->printf_buf);
 349err_alloc_pr:
 350        kfree(rp);
 351err_alloc:
 352        mutex_unlock(&mon_lock);
 353        return rc;
 354}
 355
 356/*
 357 * For simplicity, we read one record in one system call and throw out
 358 * what does not fit. This means that the following does not work:
 359 *   dd if=/dbg/usbmon/0t bs=10
 360 * Also, we do not allow seeks and do not bother advancing the offset.
 361 */
 362static ssize_t mon_text_read_t(struct file *file, char __user *buf,
 363                                size_t nbytes, loff_t *ppos)
 364{
 365        struct mon_reader_text *rp = file->private_data;
 366        struct mon_event_text *ep;
 367        struct mon_text_ptr ptr;
 368
 369        if (IS_ERR(ep = mon_text_read_wait(rp, file)))
 370                return PTR_ERR(ep);
 371        mutex_lock(&rp->printf_lock);
 372        ptr.cnt = 0;
 373        ptr.pbuf = rp->printf_buf;
 374        ptr.limit = rp->printf_size;
 375
 376        mon_text_read_head_t(rp, &ptr, ep);
 377        mon_text_read_statset(rp, &ptr, ep);
 378        ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
 379            " %d", ep->length);
 380        mon_text_read_data(rp, &ptr, ep);
 381
 382        if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
 383                ptr.cnt = -EFAULT;
 384        mutex_unlock(&rp->printf_lock);
 385        kmem_cache_free(rp->e_slab, ep);
 386        return ptr.cnt;
 387}
 388
 389static ssize_t mon_text_read_u(struct file *file, char __user *buf,
 390                                size_t nbytes, loff_t *ppos)
 391{
 392        struct mon_reader_text *rp = file->private_data;
 393        struct mon_event_text *ep;
 394        struct mon_text_ptr ptr;
 395
 396        if (IS_ERR(ep = mon_text_read_wait(rp, file)))
 397                return PTR_ERR(ep);
 398        mutex_lock(&rp->printf_lock);
 399        ptr.cnt = 0;
 400        ptr.pbuf = rp->printf_buf;
 401        ptr.limit = rp->printf_size;
 402
 403        mon_text_read_head_u(rp, &ptr, ep);
 404        if (ep->type == 'E') {
 405                mon_text_read_statset(rp, &ptr, ep);
 406        } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
 407                mon_text_read_isostat(rp, &ptr, ep);
 408                mon_text_read_isodesc(rp, &ptr, ep);
 409        } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
 410                mon_text_read_intstat(rp, &ptr, ep);
 411        } else {
 412                mon_text_read_statset(rp, &ptr, ep);
 413        }
 414        ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
 415            " %d", ep->length);
 416        mon_text_read_data(rp, &ptr, ep);
 417
 418        if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
 419                ptr.cnt = -EFAULT;
 420        mutex_unlock(&rp->printf_lock);
 421        kmem_cache_free(rp->e_slab, ep);
 422        return ptr.cnt;
 423}
 424
 425static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
 426    struct file *file)
 427{
 428        struct mon_bus *mbus = rp->r.m_bus;
 429        DECLARE_WAITQUEUE(waita, current);
 430        struct mon_event_text *ep;
 431
 432        add_wait_queue(&rp->wait, &waita);
 433        set_current_state(TASK_INTERRUPTIBLE);
 434        while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
 435                if (file->f_flags & O_NONBLOCK) {
 436                        set_current_state(TASK_RUNNING);
 437                        remove_wait_queue(&rp->wait, &waita);
 438                        return ERR_PTR(-EWOULDBLOCK);
 439                }
 440                /*
 441                 * We do not count nwaiters, because ->release is supposed
 442                 * to be called when all openers are gone only.
 443                 */
 444                schedule();
 445                if (signal_pending(current)) {
 446                        remove_wait_queue(&rp->wait, &waita);
 447                        return ERR_PTR(-EINTR);
 448                }
 449                set_current_state(TASK_INTERRUPTIBLE);
 450        }
 451        set_current_state(TASK_RUNNING);
 452        remove_wait_queue(&rp->wait, &waita);
 453        return ep;
 454}
 455
 456static void mon_text_read_head_t(struct mon_reader_text *rp,
 457        struct mon_text_ptr *p, const struct mon_event_text *ep)
 458{
 459        char udir, utype;
 460
 461        udir = (ep->is_in ? 'i' : 'o');
 462        switch (ep->xfertype) {
 463        case USB_ENDPOINT_XFER_ISOC:    utype = 'Z'; break;
 464        case USB_ENDPOINT_XFER_INT:     utype = 'I'; break;
 465        case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
 466        default: /* PIPE_BULK */  utype = 'B';
 467        }
 468        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 469            "%lx %u %c %c%c:%03u:%02u",
 470            ep->id, ep->tstamp, ep->type,
 471            utype, udir, ep->devnum, ep->epnum);
 472}
 473
 474static void mon_text_read_head_u(struct mon_reader_text *rp,
 475        struct mon_text_ptr *p, const struct mon_event_text *ep)
 476{
 477        char udir, utype;
 478
 479        udir = (ep->is_in ? 'i' : 'o');
 480        switch (ep->xfertype) {
 481        case USB_ENDPOINT_XFER_ISOC:    utype = 'Z'; break;
 482        case USB_ENDPOINT_XFER_INT:     utype = 'I'; break;
 483        case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
 484        default: /* PIPE_BULK */  utype = 'B';
 485        }
 486        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 487            "%lx %u %c %c%c:%d:%03u:%u",
 488            ep->id, ep->tstamp, ep->type,
 489            utype, udir, ep->busnum, ep->devnum, ep->epnum);
 490}
 491
 492static void mon_text_read_statset(struct mon_reader_text *rp,
 493        struct mon_text_ptr *p, const struct mon_event_text *ep)
 494{
 495
 496        if (ep->setup_flag == 0) {   /* Setup packet is present and captured */
 497                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 498                    " s %02x %02x %04x %04x %04x",
 499                    ep->setup[0],
 500                    ep->setup[1],
 501                    (ep->setup[3] << 8) | ep->setup[2],
 502                    (ep->setup[5] << 8) | ep->setup[4],
 503                    (ep->setup[7] << 8) | ep->setup[6]);
 504        } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
 505                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 506                    " %c __ __ ____ ____ ____", ep->setup_flag);
 507        } else {                     /* No setup for this kind of URB */
 508                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 509                    " %d", ep->status);
 510        }
 511}
 512
 513static void mon_text_read_intstat(struct mon_reader_text *rp,
 514        struct mon_text_ptr *p, const struct mon_event_text *ep)
 515{
 516        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 517            " %d:%d", ep->status, ep->interval);
 518}
 519
 520static void mon_text_read_isostat(struct mon_reader_text *rp,
 521        struct mon_text_ptr *p, const struct mon_event_text *ep)
 522{
 523        if (ep->type == 'S') {
 524                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 525                    " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
 526        } else {
 527                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 528                    " %d:%d:%d:%d",
 529                    ep->status, ep->interval, ep->start_frame, ep->error_count);
 530        }
 531}
 532
 533static void mon_text_read_isodesc(struct mon_reader_text *rp,
 534        struct mon_text_ptr *p, const struct mon_event_text *ep)
 535{
 536        int ndesc;      /* Display this many */
 537        int i;
 538        const struct mon_iso_desc *dp;
 539
 540        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 541            " %d", ep->numdesc);
 542        ndesc = ep->numdesc;
 543        if (ndesc > ISODESC_MAX)
 544                ndesc = ISODESC_MAX;
 545        if (ndesc < 0)
 546                ndesc = 0;
 547        dp = ep->isodesc;
 548        for (i = 0; i < ndesc; i++) {
 549                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 550                    " %d:%u:%u", dp->status, dp->offset, dp->length);
 551                dp++;
 552        }
 553}
 554
 555static void mon_text_read_data(struct mon_reader_text *rp,
 556    struct mon_text_ptr *p, const struct mon_event_text *ep)
 557{
 558        int data_len, i;
 559
 560        if ((data_len = ep->length) > 0) {
 561                if (ep->data_flag == 0) {
 562                        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 563                            " =");
 564                        if (data_len >= DATA_MAX)
 565                                data_len = DATA_MAX;
 566                        for (i = 0; i < data_len; i++) {
 567                                if (i % 4 == 0) {
 568                                        p->cnt += snprintf(p->pbuf + p->cnt,
 569                                            p->limit - p->cnt,
 570                                            " ");
 571                                }
 572                                p->cnt += snprintf(p->pbuf + p->cnt,
 573                                    p->limit - p->cnt,
 574                                    "%02x", ep->data[i]);
 575                        }
 576                        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 577                            "\n");
 578                } else {
 579                        p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
 580                            " %c\n", ep->data_flag);
 581                }
 582        } else {
 583                p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
 584        }
 585}
 586
 587static int mon_text_release(struct inode *inode, struct file *file)
 588{
 589        struct mon_reader_text *rp = file->private_data;
 590        struct mon_bus *mbus;
 591        /* unsigned long flags; */
 592        struct list_head *p;
 593        struct mon_event_text *ep;
 594
 595        mutex_lock(&mon_lock);
 596        mbus = inode->i_private;
 597
 598        if (mbus->nreaders <= 0) {
 599                printk(KERN_ERR TAG ": consistency error on close\n");
 600                mutex_unlock(&mon_lock);
 601                return 0;
 602        }
 603        mon_reader_del(mbus, &rp->r);
 604
 605        /*
 606         * In theory, e_list is protected by mbus->lock. However,
 607         * after mon_reader_del has finished, the following is the case:
 608         *  - we are not on reader list anymore, so new events won't be added;
 609         *  - whole mbus may be dropped if it was orphaned.
 610         * So, we better not touch mbus.
 611         */
 612        /* spin_lock_irqsave(&mbus->lock, flags); */
 613        while (!list_empty(&rp->e_list)) {
 614                p = rp->e_list.next;
 615                ep = list_entry(p, struct mon_event_text, e_link);
 616                list_del(p);
 617                --rp->nevents;
 618                kmem_cache_free(rp->e_slab, ep);
 619        }
 620        /* spin_unlock_irqrestore(&mbus->lock, flags); */
 621
 622        kmem_cache_destroy(rp->e_slab);
 623        kfree(rp->printf_buf);
 624        kfree(rp);
 625
 626        mutex_unlock(&mon_lock);
 627        return 0;
 628}
 629
 630static const struct file_operations mon_fops_text_t = {
 631        .owner =        THIS_MODULE,
 632        .open =         mon_text_open,
 633        .llseek =       no_llseek,
 634        .read =         mon_text_read_t,
 635        .release =      mon_text_release,
 636};
 637
 638static const struct file_operations mon_fops_text_u = {
 639        .owner =        THIS_MODULE,
 640        .open =         mon_text_open,
 641        .llseek =       no_llseek,
 642        .read =         mon_text_read_u,
 643        .release =      mon_text_release,
 644};
 645
 646int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
 647{
 648        struct dentry *d;
 649        enum { NAMESZ = 10 };
 650        char name[NAMESZ];
 651        int busnum = ubus? ubus->busnum: 0;
 652        int rc;
 653
 654        if (ubus != NULL) {
 655                rc = snprintf(name, NAMESZ, "%dt", busnum);
 656                if (rc <= 0 || rc >= NAMESZ)
 657                        goto err_print_t;
 658                d = debugfs_create_file(name, 0600, mon_dir, mbus,
 659                                                             &mon_fops_text_t);
 660                if (d == NULL)
 661                        goto err_create_t;
 662                mbus->dent_t = d;
 663        }
 664
 665        rc = snprintf(name, NAMESZ, "%du", busnum);
 666        if (rc <= 0 || rc >= NAMESZ)
 667                goto err_print_u;
 668        d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
 669        if (d == NULL)
 670                goto err_create_u;
 671        mbus->dent_u = d;
 672
 673        rc = snprintf(name, NAMESZ, "%ds", busnum);
 674        if (rc <= 0 || rc >= NAMESZ)
 675                goto err_print_s;
 676        d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
 677        if (d == NULL)
 678                goto err_create_s;
 679        mbus->dent_s = d;
 680
 681        return 1;
 682
 683err_create_s:
 684err_print_s:
 685        debugfs_remove(mbus->dent_u);
 686        mbus->dent_u = NULL;
 687err_create_u:
 688err_print_u:
 689        if (ubus != NULL) {
 690                debugfs_remove(mbus->dent_t);
 691                mbus->dent_t = NULL;
 692        }
 693err_create_t:
 694err_print_t:
 695        return 0;
 696}
 697
 698void mon_text_del(struct mon_bus *mbus)
 699{
 700        debugfs_remove(mbus->dent_u);
 701        if (mbus->dent_t != NULL)
 702                debugfs_remove(mbus->dent_t);
 703        debugfs_remove(mbus->dent_s);
 704}
 705
 706/*
 707 * Slab interface: constructor.
 708 */
 709static void mon_text_ctor(void *mem)
 710{
 711        /*
 712         * Nothing to initialize. No, really!
 713         * So, we fill it with garbage to emulate a reused object.
 714         */
 715        memset(mem, 0xe5, sizeof(struct mon_event_text));
 716}
 717
 718int __init mon_text_init(void)
 719{
 720        struct dentry *mondir;
 721
 722        mondir = debugfs_create_dir("usbmon", usb_debug_root);
 723        if (IS_ERR(mondir)) {
 724                printk(KERN_NOTICE TAG ": debugfs is not available\n");
 725                return -ENODEV;
 726        }
 727        if (mondir == NULL) {
 728                printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
 729                return -ENODEV;
 730        }
 731        mon_dir = mondir;
 732        return 0;
 733}
 734
 735void mon_text_exit(void)
 736{
 737        debugfs_remove(mon_dir);
 738}
 739