Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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