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