2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a text format reader.
7 #include <linux/kernel.h>
8 #include <linux/list.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <linux/debugfs.h>
13 #include <asm/uaccess.h>
18 * No, we do not want arbitrarily long data strings.
19 * Use the binary interface if you want to capture bulk data!
24 * Defined by USB 2.0 clause 9.3, table 9.2.
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.
34 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
42 #define PRINTF_DFL 250 /* with 5 ISOs segs */
47 unsigned int length
; /* Unsigned here, signed in URB. Historic. */
50 struct 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 */
60 int length
; /* Depends on type: xfer length or act length */
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
];
73 #define SLAB_NAME_SZ 30
74 struct mon_reader_text
{
75 struct kmem_cache
*e_slab
;
77 struct list_head e_list
;
78 struct mon_reader r
; /* In C, parent class can be placed anywhere */
80 wait_queue_head_t wait
;
83 struct mutex printf_lock
;
85 char slab_name
[SLAB_NAME_SZ
];
88 static struct dentry
*mon_dir
; /* Usually /sys/kernel/debug/usbmon */
90 static void mon_text_ctor(void *);
97 static struct mon_event_text
*
98 mon_text_read_wait(struct mon_reader_text
*rp
, struct file
*file
);
99 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
100 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
101 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
102 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
103 static void mon_text_read_statset(struct mon_reader_text
*rp
,
104 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
105 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
106 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
107 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
108 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
109 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
110 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
111 static void mon_text_read_data(struct mon_reader_text
*rp
,
112 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
118 * May be called from an interrupt.
120 * This is called with the whole mon_bus locked, so no additional lock.
123 static inline char mon_text_get_setup(struct mon_event_text
*ep
,
124 struct urb
*urb
, char ev_type
, struct mon_bus
*mbus
)
127 if (ep
->xfertype
!= USB_ENDPOINT_XFER_CONTROL
|| ev_type
!= 'S')
130 if (urb
->setup_packet
== NULL
)
131 return 'Z'; /* '0' would be not as pretty. */
133 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
137 static inline char mon_text_get_data(struct mon_event_text
*ep
, struct urb
*urb
,
138 int len
, char ev_type
, struct mon_bus
*mbus
)
153 if (urb
->transfer_buffer
== NULL
)
154 return 'Z'; /* '0' would be not as pretty. */
156 memcpy(ep
->data
, urb
->transfer_buffer
, len
);
160 static inline unsigned int mon_get_timestamp(void)
165 do_gettimeofday(&tval
);
166 stamp
= tval
.tv_sec
& 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
167 stamp
= stamp
* 1000000 + tval
.tv_usec
;
171 static void mon_text_event(struct mon_reader_text
*rp
, struct urb
*urb
,
172 char ev_type
, int status
)
174 struct mon_event_text
*ep
;
176 struct usb_iso_packet_descriptor
*fp
;
177 struct mon_iso_desc
*dp
;
180 stamp
= mon_get_timestamp();
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
++;
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
);
196 ep
->length
= (ev_type
== 'S') ?
197 urb
->transfer_buffer_length
: urb
->actual_length
;
198 /* Collecting status makes debugging sense for submits, too */
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
;
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
)
213 fp
= urb
->iso_frame_desc
;
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
;
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
,
230 list_add_tail(&ep
->e_link
, &rp
->e_list
);
234 static void mon_text_submit(void *data
, struct urb
*urb
)
236 struct mon_reader_text
*rp
= data
;
237 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
240 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
242 struct mon_reader_text
*rp
= data
;
243 mon_text_event(rp
, urb
, 'C', status
);
246 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
248 struct mon_reader_text
*rp
= data
;
249 struct mon_event_text
*ep
;
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
++;
258 ep
->id
= (unsigned long) urb
;
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
);
268 ep
->setup_flag
= '-';
272 list_add_tail(&ep
->e_link
, &rp
->e_list
);
277 * Fetch next event from the circular buffer.
279 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
280 struct mon_bus
*mbus
)
285 spin_lock_irqsave(&mbus
->lock
, flags
);
286 if (list_empty(&rp
->e_list
)) {
287 spin_unlock_irqrestore(&mbus
->lock
, flags
);
293 spin_unlock_irqrestore(&mbus
->lock
, flags
);
294 return list_entry(p
, struct mon_event_text
, e_link
);
299 static int mon_text_open(struct inode
*inode
, struct file
*file
)
301 struct mon_bus
*mbus
;
302 struct mon_reader_text
*rp
;
305 mutex_lock(&mon_lock
);
306 mbus
= inode
->i_private
;
308 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
313 INIT_LIST_HEAD(&rp
->e_list
);
314 init_waitqueue_head(&rp
->wait
);
315 mutex_init(&rp
->printf_lock
);
317 rp
->printf_size
= PRINTF_DFL
;
318 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
319 if (rp
->printf_buf
== NULL
) {
326 rp
->r
.rnf_submit
= mon_text_submit
;
327 rp
->r
.rnf_error
= mon_text_error
;
328 rp
->r
.rnf_complete
= mon_text_complete
;
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,
334 if (rp
->e_slab
== NULL
) {
339 mon_reader_add(mbus
, &rp
->r
);
341 file
->private_data
= rp
;
342 mutex_unlock(&mon_lock
);
346 // kmem_cache_destroy(rp->e_slab);
348 kfree(rp
->printf_buf
);
352 mutex_unlock(&mon_lock
);
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.
362 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
363 size_t nbytes
, loff_t
*ppos
)
365 struct mon_reader_text
*rp
= file
->private_data
;
366 struct mon_event_text
*ep
;
367 struct mon_text_ptr ptr
;
369 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
371 mutex_lock(&rp
->printf_lock
);
373 ptr
.pbuf
= rp
->printf_buf
;
374 ptr
.limit
= rp
->printf_size
;
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
,
380 mon_text_read_data(rp
, &ptr
, ep
);
382 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
384 mutex_unlock(&rp
->printf_lock
);
385 kmem_cache_free(rp
->e_slab
, ep
);
389 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
390 size_t nbytes
, loff_t
*ppos
)
392 struct mon_reader_text
*rp
= file
->private_data
;
393 struct mon_event_text
*ep
;
394 struct mon_text_ptr ptr
;
396 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
398 mutex_lock(&rp
->printf_lock
);
400 ptr
.pbuf
= rp
->printf_buf
;
401 ptr
.limit
= rp
->printf_size
;
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
);
412 mon_text_read_statset(rp
, &ptr
, ep
);
414 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
416 mon_text_read_data(rp
, &ptr
, ep
);
418 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
420 mutex_unlock(&rp
->printf_lock
);
421 kmem_cache_free(rp
->e_slab
, ep
);
425 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
428 struct mon_bus
*mbus
= rp
->r
.m_bus
;
429 DECLARE_WAITQUEUE(waita
, current
);
430 struct mon_event_text
*ep
;
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
);
441 * We do not count nwaiters, because ->release is supposed
442 * to be called when all openers are gone only.
445 if (signal_pending(current
)) {
446 remove_wait_queue(&rp
->wait
, &waita
);
447 return ERR_PTR(-EINTR
);
449 set_current_state(TASK_INTERRUPTIBLE
);
451 set_current_state(TASK_RUNNING
);
452 remove_wait_queue(&rp
->wait
, &waita
);
456 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
457 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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';
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
);
474 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
475 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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';
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
);
492 static void mon_text_read_statset(struct mon_reader_text
*rp
,
493 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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",
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
,
513 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
514 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
516 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
517 " %d:%d", ep
->status
, ep
->interval
);
520 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
521 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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
);
527 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
529 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
533 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
534 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
536 int ndesc
; /* Display this many */
538 const struct mon_iso_desc
*dp
;
540 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
543 if (ndesc
> ISODESC_MAX
)
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
);
555 static void mon_text_read_data(struct mon_reader_text
*rp
,
556 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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
,
564 if (data_len
>= DATA_MAX
)
566 for (i
= 0; i
< data_len
; i
++) {
568 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
572 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
574 "%02x", ep
->data
[i
]);
576 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
579 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
580 " %c\n", ep
->data_flag
);
583 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
587 static int mon_text_release(struct inode
*inode
, struct file
*file
)
589 struct mon_reader_text
*rp
= file
->private_data
;
590 struct mon_bus
*mbus
;
591 /* unsigned long flags; */
593 struct mon_event_text
*ep
;
595 mutex_lock(&mon_lock
);
596 mbus
= inode
->i_private
;
598 if (mbus
->nreaders
<= 0) {
599 printk(KERN_ERR TAG
": consistency error on close\n");
600 mutex_unlock(&mon_lock
);
603 mon_reader_del(mbus
, &rp
->r
);
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.
612 /* spin_lock_irqsave(&mbus->lock, flags); */
613 while (!list_empty(&rp
->e_list
)) {
615 ep
= list_entry(p
, struct mon_event_text
, e_link
);
618 kmem_cache_free(rp
->e_slab
, ep
);
620 /* spin_unlock_irqrestore(&mbus->lock, flags); */
622 kmem_cache_destroy(rp
->e_slab
);
623 kfree(rp
->printf_buf
);
626 mutex_unlock(&mon_lock
);
630 static const struct file_operations mon_fops_text_t
= {
631 .owner
= THIS_MODULE
,
632 .open
= mon_text_open
,
634 .read
= mon_text_read_t
,
635 .release
= mon_text_release
,
638 static const struct file_operations mon_fops_text_u
= {
639 .owner
= THIS_MODULE
,
640 .open
= mon_text_open
,
642 .read
= mon_text_read_u
,
643 .release
= mon_text_release
,
646 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
649 enum { NAMESZ
= 10 };
651 int busnum
= ubus
? ubus
->busnum
: 0;
655 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
656 if (rc
<= 0 || rc
>= NAMESZ
)
658 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
665 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
666 if (rc
<= 0 || rc
>= NAMESZ
)
668 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
673 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
674 if (rc
<= 0 || rc
>= NAMESZ
)
676 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
685 debugfs_remove(mbus
->dent_u
);
690 debugfs_remove(mbus
->dent_t
);
698 void mon_text_del(struct mon_bus
*mbus
)
700 debugfs_remove(mbus
->dent_u
);
701 if (mbus
->dent_t
!= NULL
)
702 debugfs_remove(mbus
->dent_t
);
703 debugfs_remove(mbus
->dent_s
);
707 * Slab interface: constructor.
709 static void mon_text_ctor(void *mem
)
712 * Nothing to initialize. No, really!
713 * So, we fill it with garbage to emulate a reused object.
715 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
718 int __init
mon_text_init(void)
720 struct dentry
*mondir
;
722 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
723 if (IS_ERR(mondir
)) {
724 printk(KERN_NOTICE TAG
": debugfs is not available\n");
727 if (mondir
== NULL
) {
728 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
735 void mon_text_exit(void)
737 debugfs_remove(mon_dir
);