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 <linux/scatterlist.h>
14 #include <asm/uaccess.h>
19 * No, we do not want arbitrarily long data strings.
20 * Use the binary interface if you want to capture bulk data!
25 * Defined by USB 2.0 clause 9.3, table 9.2.
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.
35 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
38 * Potentially unlimited number; we limit it for similar allocations.
39 * The usbfs limits this to 128, but we're not quite as generous.
43 #define PRINTF_DFL 250 /* with 5 ISOs segs */
48 unsigned int length
; /* Unsigned here, signed in URB. Historic. */
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 */
61 int length
; /* Depends on type: xfer length or act length */
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
];
74 #define SLAB_NAME_SZ 30
75 struct mon_reader_text
{
76 struct kmem_cache
*e_slab
;
78 struct list_head e_list
;
79 struct mon_reader r
; /* In C, parent class can be placed anywhere */
81 wait_queue_head_t wait
;
84 struct mutex printf_lock
;
86 char slab_name
[SLAB_NAME_SZ
];
89 static struct dentry
*mon_dir
; /* Usually /sys/kernel/debug/usbmon */
91 static void mon_text_ctor(void *);
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
);
119 * May be called from an interrupt.
121 * This is called with the whole mon_bus locked, so no additional lock.
124 static inline char mon_text_get_setup(struct mon_event_text
*ep
,
125 struct urb
*urb
, char ev_type
, struct mon_bus
*mbus
)
128 if (ep
->xfertype
!= USB_ENDPOINT_XFER_CONTROL
|| ev_type
!= 'S')
131 if (urb
->setup_packet
== NULL
)
132 return 'Z'; /* '0' would be not as pretty. */
134 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
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
)
156 if (urb
->num_sgs
== 0) {
157 src
= urb
->transfer_buffer
;
159 return 'Z'; /* '0' would be not as pretty. */
161 struct scatterlist
*sg
= urb
->sg
->sg
;
163 /* If IOMMU coalescing occurred, we cannot trust sg_page */
164 if (urb
->sg
->nents
!= urb
->num_sgs
||
165 PageHighMem(sg_page(sg
)))
168 /* For the text interface we copy only the first sg buffer */
169 len
= min_t(int, sg
->length
, len
);
173 memcpy(ep
->data
, src
, len
);
177 static inline unsigned int mon_get_timestamp(void)
182 do_gettimeofday(&tval
);
183 stamp
= tval
.tv_sec
& 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
184 stamp
= stamp
* 1000000 + tval
.tv_usec
;
188 static void mon_text_event(struct mon_reader_text
*rp
, struct urb
*urb
,
189 char ev_type
, int status
)
191 struct mon_event_text
*ep
;
193 struct usb_iso_packet_descriptor
*fp
;
194 struct mon_iso_desc
*dp
;
197 stamp
= mon_get_timestamp();
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
++;
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
);
213 ep
->length
= (ev_type
== 'S') ?
214 urb
->transfer_buffer_length
: urb
->actual_length
;
215 /* Collecting status makes debugging sense for submits, too */
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
;
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
)
230 fp
= urb
->iso_frame_desc
;
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
;
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
,
247 list_add_tail(&ep
->e_link
, &rp
->e_list
);
251 static void mon_text_submit(void *data
, struct urb
*urb
)
253 struct mon_reader_text
*rp
= data
;
254 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
257 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
259 struct mon_reader_text
*rp
= data
;
260 mon_text_event(rp
, urb
, 'C', status
);
263 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
265 struct mon_reader_text
*rp
= data
;
266 struct mon_event_text
*ep
;
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
++;
275 ep
->id
= (unsigned long) urb
;
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
);
285 ep
->setup_flag
= '-';
289 list_add_tail(&ep
->e_link
, &rp
->e_list
);
294 * Fetch next event from the circular buffer.
296 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
297 struct mon_bus
*mbus
)
302 spin_lock_irqsave(&mbus
->lock
, flags
);
303 if (list_empty(&rp
->e_list
)) {
304 spin_unlock_irqrestore(&mbus
->lock
, flags
);
310 spin_unlock_irqrestore(&mbus
->lock
, flags
);
311 return list_entry(p
, struct mon_event_text
, e_link
);
316 static int mon_text_open(struct inode
*inode
, struct file
*file
)
318 struct mon_bus
*mbus
;
319 struct mon_reader_text
*rp
;
322 mutex_lock(&mon_lock
);
323 mbus
= inode
->i_private
;
325 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
330 INIT_LIST_HEAD(&rp
->e_list
);
331 init_waitqueue_head(&rp
->wait
);
332 mutex_init(&rp
->printf_lock
);
334 rp
->printf_size
= PRINTF_DFL
;
335 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
336 if (rp
->printf_buf
== NULL
) {
343 rp
->r
.rnf_submit
= mon_text_submit
;
344 rp
->r
.rnf_error
= mon_text_error
;
345 rp
->r
.rnf_complete
= mon_text_complete
;
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,
351 if (rp
->e_slab
== NULL
) {
356 mon_reader_add(mbus
, &rp
->r
);
358 file
->private_data
= rp
;
359 mutex_unlock(&mon_lock
);
363 // kmem_cache_destroy(rp->e_slab);
365 kfree(rp
->printf_buf
);
369 mutex_unlock(&mon_lock
);
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.
379 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
380 size_t nbytes
, loff_t
*ppos
)
382 struct mon_reader_text
*rp
= file
->private_data
;
383 struct mon_event_text
*ep
;
384 struct mon_text_ptr ptr
;
386 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
388 mutex_lock(&rp
->printf_lock
);
390 ptr
.pbuf
= rp
->printf_buf
;
391 ptr
.limit
= rp
->printf_size
;
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
,
397 mon_text_read_data(rp
, &ptr
, ep
);
399 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
401 mutex_unlock(&rp
->printf_lock
);
402 kmem_cache_free(rp
->e_slab
, ep
);
406 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
407 size_t nbytes
, loff_t
*ppos
)
409 struct mon_reader_text
*rp
= file
->private_data
;
410 struct mon_event_text
*ep
;
411 struct mon_text_ptr ptr
;
413 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
415 mutex_lock(&rp
->printf_lock
);
417 ptr
.pbuf
= rp
->printf_buf
;
418 ptr
.limit
= rp
->printf_size
;
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
);
429 mon_text_read_statset(rp
, &ptr
, ep
);
431 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
433 mon_text_read_data(rp
, &ptr
, ep
);
435 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
437 mutex_unlock(&rp
->printf_lock
);
438 kmem_cache_free(rp
->e_slab
, ep
);
442 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
445 struct mon_bus
*mbus
= rp
->r
.m_bus
;
446 DECLARE_WAITQUEUE(waita
, current
);
447 struct mon_event_text
*ep
;
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
);
458 * We do not count nwaiters, because ->release is supposed
459 * to be called when all openers are gone only.
462 if (signal_pending(current
)) {
463 remove_wait_queue(&rp
->wait
, &waita
);
464 return ERR_PTR(-EINTR
);
466 set_current_state(TASK_INTERRUPTIBLE
);
468 set_current_state(TASK_RUNNING
);
469 remove_wait_queue(&rp
->wait
, &waita
);
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
)
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';
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
);
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
)
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';
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
);
509 static void mon_text_read_statset(struct mon_reader_text
*rp
,
510 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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",
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
,
530 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
531 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
533 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
534 " %d:%d", ep
->status
, ep
->interval
);
537 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
538 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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
);
544 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
546 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
550 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
551 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
553 int ndesc
; /* Display this many */
555 const struct mon_iso_desc
*dp
;
557 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
560 if (ndesc
> ISODESC_MAX
)
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
);
572 static void mon_text_read_data(struct mon_reader_text
*rp
,
573 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
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
,
581 if (data_len
>= DATA_MAX
)
583 for (i
= 0; i
< data_len
; i
++) {
585 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
589 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
591 "%02x", ep
->data
[i
]);
593 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
596 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
597 " %c\n", ep
->data_flag
);
600 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
604 static int mon_text_release(struct inode
*inode
, struct file
*file
)
606 struct mon_reader_text
*rp
= file
->private_data
;
607 struct mon_bus
*mbus
;
608 /* unsigned long flags; */
610 struct mon_event_text
*ep
;
612 mutex_lock(&mon_lock
);
613 mbus
= inode
->i_private
;
615 if (mbus
->nreaders
<= 0) {
616 printk(KERN_ERR TAG
": consistency error on close\n");
617 mutex_unlock(&mon_lock
);
620 mon_reader_del(mbus
, &rp
->r
);
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.
629 /* spin_lock_irqsave(&mbus->lock, flags); */
630 while (!list_empty(&rp
->e_list
)) {
632 ep
= list_entry(p
, struct mon_event_text
, e_link
);
635 kmem_cache_free(rp
->e_slab
, ep
);
637 /* spin_unlock_irqrestore(&mbus->lock, flags); */
639 kmem_cache_destroy(rp
->e_slab
);
640 kfree(rp
->printf_buf
);
643 mutex_unlock(&mon_lock
);
647 static const struct file_operations mon_fops_text_t
= {
648 .owner
= THIS_MODULE
,
649 .open
= mon_text_open
,
651 .read
= mon_text_read_t
,
652 .release
= mon_text_release
,
655 static const struct file_operations mon_fops_text_u
= {
656 .owner
= THIS_MODULE
,
657 .open
= mon_text_open
,
659 .read
= mon_text_read_u
,
660 .release
= mon_text_release
,
663 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
666 enum { NAMESZ
= 10 };
668 int busnum
= ubus
? ubus
->busnum
: 0;
672 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
673 if (rc
<= 0 || rc
>= NAMESZ
)
675 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
682 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
683 if (rc
<= 0 || rc
>= NAMESZ
)
685 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
690 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
691 if (rc
<= 0 || rc
>= NAMESZ
)
693 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
702 debugfs_remove(mbus
->dent_u
);
707 debugfs_remove(mbus
->dent_t
);
715 void mon_text_del(struct mon_bus
*mbus
)
717 debugfs_remove(mbus
->dent_u
);
718 if (mbus
->dent_t
!= NULL
)
719 debugfs_remove(mbus
->dent_t
);
720 debugfs_remove(mbus
->dent_s
);
724 * Slab interface: constructor.
726 static void mon_text_ctor(void *mem
)
729 * Nothing to initialize. No, really!
730 * So, we fill it with garbage to emulate a reused object.
732 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
735 int __init
mon_text_init(void)
737 struct dentry
*mondir
;
739 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
740 if (IS_ERR(mondir
)) {
741 printk(KERN_NOTICE TAG
": debugfs is not available\n");
744 if (mondir
== NULL
) {
745 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
752 void mon_text_exit(void)
754 debugfs_remove(mon_dir
);