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