1 // SPDX-License-Identifier: GPL-2.0
3 * The USB Monitor, inspired by Dave Harding's USBMon.
5 * This is a binary format reader.
7 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
8 * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
11 #include <linux/kernel.h>
12 #include <linux/sched/signal.h>
13 #include <linux/types.h>
15 #include <linux/cdev.h>
16 #include <linux/export.h>
17 #include <linux/usb.h>
18 #include <linux/poll.h>
19 #include <linux/compat.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
23 #include <linux/time64.h>
25 #include <linux/uaccess.h>
30 * Defined by USB 2.0 clause 9.3, table 9.2.
35 #define MON_IOC_MAGIC 0x92
37 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
38 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
39 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
40 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
41 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
42 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
43 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
44 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
45 /* #9 was MON_IOCT_SETAPI */
46 #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
49 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
50 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
51 #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
55 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
56 * But it's all right. Just use a simple way to make sure the chunk is never
57 * smaller than a page.
59 * N.B. An application does not know our chunk size.
61 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
62 * page-sized chunks for the time being.
64 #define CHUNK_SIZE PAGE_SIZE
65 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
68 * The magic limit was calculated so that it allows the monitoring
69 * application to pick data once in two ticks. This way, another application,
70 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
71 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
72 * enormous overhead built into the bus protocol, so we need about 1000 KB.
74 * This is still too much for most cases, where we just snoop a few
75 * descriptor fetches for enumeration. So, the default is a "reasonable"
76 * amount for systems with HZ=250 and incomplete bus saturation.
78 * XXX What about multi-megabyte URBs which take minutes to transfer?
80 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
81 #define BUFF_DFL CHUNK_ALIGN(300*1024)
82 #define BUFF_MIN CHUNK_ALIGN(8*1024)
85 * The per-event API header (2 per URB).
87 * This structure is seen in userland as defined by the documentation.
90 u64 id
; /* URB ID - from submission to callback */
91 unsigned char type
; /* Same as in text API; extensible. */
92 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
93 unsigned char epnum
; /* Endpoint number and transfer direction */
94 unsigned char devnum
; /* Device address */
95 unsigned short busnum
; /* Bus number */
98 s64 ts_sec
; /* ktime_get_real_ts64 */
99 s32 ts_usec
; /* ktime_get_real_ts64 */
101 unsigned int len_urb
; /* Length of data (submitted or actual) */
102 unsigned int len_cap
; /* Delivered length */
104 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
112 unsigned int xfer_flags
;
113 unsigned int ndesc
; /* Actual number of ISO descriptors */
117 * ISO vector, packed into the head of data stream.
118 * This has to take 16 bytes to make sure that the end of buffer
119 * wrap is not happening in the middle of a descriptor.
121 struct mon_bin_isodesc
{
123 unsigned int iso_off
;
124 unsigned int iso_len
;
128 /* per file statistic */
129 struct mon_bin_stats
{
135 struct mon_bin_hdr __user
*hdr
; /* Can be 48 bytes or 64. */
137 size_t alloc
; /* Length of data (can be zero) */
140 struct mon_bin_mfetch
{
141 u32 __user
*offvec
; /* Vector of events fetched */
142 u32 nfetch
; /* Number of events to fetch (out: fetched) */
143 u32 nflush
; /* Number of events to flush */
147 struct mon_bin_get32
{
153 struct mon_bin_mfetch32
{
160 /* Having these two values same prevents wrapping of the mon_bin_hdr */
164 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
165 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
167 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
169 /* max number of USB bus supported */
170 #define MON_BIN_MAX_MINOR 128
173 * The buffer: map of used pages.
177 unsigned char *ptr
; /* XXX just use page_to_virt everywhere? */
181 * This gets associated with an open file struct.
183 struct mon_reader_bin
{
184 /* The buffer: one per open. */
185 spinlock_t b_lock
; /* Protect b_cnt, b_in */
186 unsigned int b_size
; /* Current size of the buffer - bytes */
187 unsigned int b_cnt
; /* Bytes used */
188 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
189 unsigned int b_read
; /* Amount of read data in curr. pkt. */
190 struct mon_pgmap
*b_vec
; /* The map array */
191 wait_queue_head_t b_wait
; /* Wait for data here */
193 struct mutex fetch_lock
; /* Protect b_read, b_out */
196 /* A list of these is needed for "bus 0". Some time later. */
200 unsigned int cnt_lost
;
203 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
206 return (struct mon_bin_hdr
*)
207 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
210 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
212 static unsigned char xfer_to_pipe
[4] = {
213 PIPE_CONTROL
, PIPE_ISOCHRONOUS
, PIPE_BULK
, PIPE_INTERRUPT
216 static struct class *mon_bin_class
;
217 static dev_t mon_bin_dev0
;
218 static struct cdev mon_bin_cdev
;
220 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
221 unsigned int offset
, unsigned int size
);
222 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
223 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
224 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
227 * This is a "chunked memcpy". It does not manipulate any counters.
229 static unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
230 unsigned int off
, const unsigned char *from
, unsigned int length
)
232 unsigned int step_len
;
234 unsigned int in_page
;
238 * Determine step_len.
241 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
242 if (in_page
< step_len
)
246 * Copy data and advance pointers.
248 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
249 memcpy(buf
, from
, step_len
);
250 if ((off
+= step_len
) >= this->b_size
) off
= 0;
258 * This is a little worse than the above because it's "chunked copy_to_user".
259 * The return value is an error code, not an offset.
261 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
262 char __user
*to
, int length
)
264 unsigned int step_len
;
266 unsigned int in_page
;
270 * Determine step_len.
273 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
274 if (in_page
< step_len
)
278 * Copy data and advance pointers.
280 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
281 if (copy_to_user(to
, buf
, step_len
))
283 if ((off
+= step_len
) >= this->b_size
) off
= 0;
291 * Allocate an (aligned) area in the buffer.
292 * This is called under b_lock.
293 * Returns ~0 on failure.
295 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
300 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
301 if (rp
->b_cnt
+ size
> rp
->b_size
)
305 if ((rp
->b_in
+= size
) >= rp
->b_size
)
306 rp
->b_in
-= rp
->b_size
;
311 * This is the same thing as mon_buff_area_alloc, only it does not allow
312 * buffers to wrap. This is needed by applications which pass references
313 * into mmap-ed buffers up their stacks (libpcap can do that).
315 * Currently, we always have the header stuck with the data, although
316 * it is not strictly speaking necessary.
318 * When a buffer would wrap, we place a filler packet to mark the space.
320 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
324 unsigned int fill_size
;
326 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
327 if (rp
->b_cnt
+ size
> rp
->b_size
)
329 if (rp
->b_in
+ size
> rp
->b_size
) {
331 * This would wrap. Find if we still have space after
332 * skipping to the end of the buffer. If we do, place
333 * a filler packet and allocate a new packet.
335 fill_size
= rp
->b_size
- rp
->b_in
;
336 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
338 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
342 rp
->b_cnt
+= size
+ fill_size
;
343 } else if (rp
->b_in
+ size
== rp
->b_size
) {
356 * Return a few (kilo-)bytes to the head of the buffer.
357 * This is used if a data fetch fails.
359 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
362 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
365 rp
->b_in
+= rp
->b_size
;
370 * This has to be called under both b_lock and fetch_lock, because
371 * it accesses both b_cnt and b_out.
373 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
376 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
378 if ((rp
->b_out
+= size
) >= rp
->b_size
)
379 rp
->b_out
-= rp
->b_size
;
382 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
383 unsigned int offset
, unsigned int size
)
385 struct mon_bin_hdr
*ep
;
387 ep
= MON_OFF2HDR(rp
, offset
);
388 memset(ep
, 0, PKT_SIZE
);
390 ep
->len_cap
= size
- PKT_SIZE
;
393 static inline char mon_bin_get_setup(unsigned char *setupb
,
394 const struct urb
*urb
, char ev_type
)
397 if (urb
->setup_packet
== NULL
)
399 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
403 static unsigned int mon_bin_get_data(const struct mon_reader_bin
*rp
,
404 unsigned int offset
, struct urb
*urb
, unsigned int length
,
408 struct scatterlist
*sg
;
409 unsigned int this_len
;
412 if (urb
->num_sgs
== 0) {
413 if (urb
->transfer_buffer
== NULL
) {
417 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
421 /* If IOMMU coalescing occurred, we cannot trust sg_page */
422 if (urb
->transfer_flags
& URB_DMA_SG_COMBINED
) {
427 /* Copy up to the first non-addressable segment */
428 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
429 if (length
== 0 || PageHighMem(sg_page(sg
)))
431 this_len
= min_t(unsigned int, sg
->length
, length
);
432 offset
= mon_copy_to_buff(rp
, offset
, sg_virt(sg
),
444 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
445 * be used to determine the length of the whole contiguous buffer.
447 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin
*rp
,
448 struct urb
*urb
, unsigned int ndesc
)
450 struct usb_iso_packet_descriptor
*fp
;
454 fp
= urb
->iso_frame_desc
;
455 while (ndesc
-- != 0) {
456 if (fp
->actual_length
!= 0) {
457 if (fp
->offset
+ fp
->actual_length
> length
)
458 length
= fp
->offset
+ fp
->actual_length
;
465 static void mon_bin_get_isodesc(const struct mon_reader_bin
*rp
,
466 unsigned int offset
, struct urb
*urb
, char ev_type
, unsigned int ndesc
)
468 struct mon_bin_isodesc
*dp
;
469 struct usb_iso_packet_descriptor
*fp
;
471 fp
= urb
->iso_frame_desc
;
472 while (ndesc
-- != 0) {
473 dp
= (struct mon_bin_isodesc
*)
474 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
475 dp
->iso_status
= fp
->status
;
476 dp
->iso_off
= fp
->offset
;
477 dp
->iso_len
= (ev_type
== 'S') ? fp
->length
: fp
->actual_length
;
479 if ((offset
+= sizeof(struct mon_bin_isodesc
)) >= rp
->b_size
)
485 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
486 char ev_type
, int status
)
488 const struct usb_endpoint_descriptor
*epd
= &urb
->ep
->desc
;
489 struct timespec64 ts
;
491 unsigned int urb_length
;
495 unsigned int ndesc
, lendesc
;
497 struct mon_bin_hdr
*ep
;
500 ktime_get_real_ts64(&ts
);
502 spin_lock_irqsave(&rp
->b_lock
, flags
);
505 * Find the maximum allowable length, then allocate space.
507 urb_length
= (ev_type
== 'S') ?
508 urb
->transfer_buffer_length
: urb
->actual_length
;
511 if (usb_endpoint_xfer_isoc(epd
)) {
512 if (urb
->number_of_packets
< 0) {
514 } else if (urb
->number_of_packets
>= ISODESC_MAX
) {
517 ndesc
= urb
->number_of_packets
;
519 if (ev_type
== 'C' && usb_urb_dir_in(urb
))
520 length
= mon_bin_collate_isodesc(rp
, urb
, ndesc
);
524 lendesc
= ndesc
*sizeof(struct mon_bin_isodesc
);
526 /* not an issue unless there's a subtle bug in a HCD somewhere */
527 if (length
>= urb
->transfer_buffer_length
)
528 length
= urb
->transfer_buffer_length
;
530 if (length
>= rp
->b_size
/5)
531 length
= rp
->b_size
/5;
533 if (usb_urb_dir_in(urb
)) {
534 if (ev_type
== 'S') {
538 /* Cannot rely on endpoint number in case of control ep.0 */
541 if (ev_type
== 'C') {
548 if (rp
->mmap_active
) {
549 offset
= mon_buff_area_alloc_contiguous(rp
,
550 length
+ PKT_SIZE
+ lendesc
);
552 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
+ lendesc
);
556 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
560 ep
= MON_OFF2HDR(rp
, offset
);
561 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
564 * Fill the allocated area.
566 memset(ep
, 0, PKT_SIZE
);
568 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(epd
)];
569 ep
->epnum
= dir
| usb_endpoint_num(epd
);
570 ep
->devnum
= urb
->dev
->devnum
;
571 ep
->busnum
= urb
->dev
->bus
->busnum
;
572 ep
->id
= (unsigned long) urb
;
573 ep
->ts_sec
= ts
.tv_sec
;
574 ep
->ts_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
576 ep
->len_urb
= urb_length
;
577 ep
->len_cap
= length
+ lendesc
;
578 ep
->xfer_flags
= urb
->transfer_flags
;
580 if (usb_endpoint_xfer_int(epd
)) {
581 ep
->interval
= urb
->interval
;
582 } else if (usb_endpoint_xfer_isoc(epd
)) {
583 ep
->interval
= urb
->interval
;
584 ep
->start_frame
= urb
->start_frame
;
585 ep
->s
.iso
.error_count
= urb
->error_count
;
586 ep
->s
.iso
.numdesc
= urb
->number_of_packets
;
589 if (usb_endpoint_xfer_control(epd
) && ev_type
== 'S') {
590 ep
->flag_setup
= mon_bin_get_setup(ep
->s
.setup
, urb
, ev_type
);
592 ep
->flag_setup
= '-';
597 mon_bin_get_isodesc(rp
, offset
, urb
, ev_type
, ndesc
);
598 if ((offset
+= lendesc
) >= rp
->b_size
)
599 offset
-= rp
->b_size
;
603 length
= mon_bin_get_data(rp
, offset
, urb
, length
,
606 delta
= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
607 ep
->len_cap
-= length
;
608 delta
-= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
609 mon_buff_area_shrink(rp
, delta
);
612 ep
->flag_data
= data_tag
;
615 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
617 wake_up(&rp
->b_wait
);
620 static void mon_bin_submit(void *data
, struct urb
*urb
)
622 struct mon_reader_bin
*rp
= data
;
623 mon_bin_event(rp
, urb
, 'S', -EINPROGRESS
);
626 static void mon_bin_complete(void *data
, struct urb
*urb
, int status
)
628 struct mon_reader_bin
*rp
= data
;
629 mon_bin_event(rp
, urb
, 'C', status
);
632 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
634 struct mon_reader_bin
*rp
= data
;
635 struct timespec64 ts
;
638 struct mon_bin_hdr
*ep
;
640 ktime_get_real_ts64(&ts
);
642 spin_lock_irqsave(&rp
->b_lock
, flags
);
644 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
646 /* Not incrementing cnt_lost. Just because. */
647 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
651 ep
= MON_OFF2HDR(rp
, offset
);
653 memset(ep
, 0, PKT_SIZE
);
655 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(&urb
->ep
->desc
)];
656 ep
->epnum
= usb_urb_dir_in(urb
) ? USB_DIR_IN
: 0;
657 ep
->epnum
|= usb_endpoint_num(&urb
->ep
->desc
);
658 ep
->devnum
= urb
->dev
->devnum
;
659 ep
->busnum
= urb
->dev
->bus
->busnum
;
660 ep
->id
= (unsigned long) urb
;
661 ep
->ts_sec
= ts
.tv_sec
;
662 ep
->ts_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
665 ep
->flag_setup
= '-';
668 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
670 wake_up(&rp
->b_wait
);
673 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
675 struct mon_bus
*mbus
;
676 struct mon_reader_bin
*rp
;
680 mutex_lock(&mon_lock
);
681 mbus
= mon_bus_lookup(iminor(inode
));
683 mutex_unlock(&mon_lock
);
686 if (mbus
!= &mon_bus0
&& mbus
->u_bus
== NULL
) {
687 printk(KERN_ERR TAG
": consistency error on open\n");
688 mutex_unlock(&mon_lock
);
692 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
697 spin_lock_init(&rp
->b_lock
);
698 init_waitqueue_head(&rp
->b_wait
);
699 mutex_init(&rp
->fetch_lock
);
700 rp
->b_size
= BUFF_DFL
;
702 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
703 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
708 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
713 rp
->r
.rnf_submit
= mon_bin_submit
;
714 rp
->r
.rnf_error
= mon_bin_error
;
715 rp
->r
.rnf_complete
= mon_bin_complete
;
717 mon_reader_add(mbus
, &rp
->r
);
719 file
->private_data
= rp
;
720 mutex_unlock(&mon_lock
);
728 mutex_unlock(&mon_lock
);
733 * Extract an event from buffer and copy it to user space.
734 * Wait if there is no event ready.
735 * Returns zero or error.
737 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
738 struct mon_bin_hdr __user
*hdr
, unsigned int hdrbytes
,
739 void __user
*data
, unsigned int nbytes
)
742 struct mon_bin_hdr
*ep
;
747 mutex_lock(&rp
->fetch_lock
);
749 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
750 mutex_unlock(&rp
->fetch_lock
);
754 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
756 if (copy_to_user(hdr
, ep
, hdrbytes
)) {
757 mutex_unlock(&rp
->fetch_lock
);
761 step_len
= min(ep
->len_cap
, nbytes
);
762 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
764 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
765 mutex_unlock(&rp
->fetch_lock
);
769 spin_lock_irqsave(&rp
->b_lock
, flags
);
770 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
771 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
774 mutex_unlock(&rp
->fetch_lock
);
778 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
780 struct mon_reader_bin
*rp
= file
->private_data
;
781 struct mon_bus
* mbus
= rp
->r
.m_bus
;
783 mutex_lock(&mon_lock
);
785 if (mbus
->nreaders
<= 0) {
786 printk(KERN_ERR TAG
": consistency error on close\n");
787 mutex_unlock(&mon_lock
);
790 mon_reader_del(mbus
, &rp
->r
);
792 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
796 mutex_unlock(&mon_lock
);
800 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
801 size_t nbytes
, loff_t
*ppos
)
803 struct mon_reader_bin
*rp
= file
->private_data
;
804 unsigned int hdrbytes
= PKT_SZ_API0
;
806 struct mon_bin_hdr
*ep
;
813 mutex_lock(&rp
->fetch_lock
);
815 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
816 mutex_unlock(&rp
->fetch_lock
);
820 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
822 if (rp
->b_read
< hdrbytes
) {
823 step_len
= min(nbytes
, (size_t)(hdrbytes
- rp
->b_read
));
824 ptr
= ((char *)ep
) + rp
->b_read
;
825 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
826 mutex_unlock(&rp
->fetch_lock
);
831 rp
->b_read
+= step_len
;
835 if (rp
->b_read
>= hdrbytes
) {
836 step_len
= ep
->len_cap
;
837 step_len
-= rp
->b_read
- hdrbytes
;
838 if (step_len
> nbytes
)
840 offset
= rp
->b_out
+ PKT_SIZE
;
841 offset
+= rp
->b_read
- hdrbytes
;
842 if (offset
>= rp
->b_size
)
843 offset
-= rp
->b_size
;
844 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
845 mutex_unlock(&rp
->fetch_lock
);
850 rp
->b_read
+= step_len
;
855 * Check if whole packet was read, and if so, jump to the next one.
857 if (rp
->b_read
>= hdrbytes
+ ep
->len_cap
) {
858 spin_lock_irqsave(&rp
->b_lock
, flags
);
859 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
860 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
864 mutex_unlock(&rp
->fetch_lock
);
869 * Remove at most nevents from chunked buffer.
870 * Returns the number of removed events.
872 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
875 struct mon_bin_hdr
*ep
;
878 mutex_lock(&rp
->fetch_lock
);
879 spin_lock_irqsave(&rp
->b_lock
, flags
);
880 for (i
= 0; i
< nevents
; ++i
) {
881 if (MON_RING_EMPTY(rp
))
884 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
885 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
887 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
889 mutex_unlock(&rp
->fetch_lock
);
894 * Fetch at most max event offsets into the buffer and put them into vec.
895 * The events are usually freed later with mon_bin_flush.
896 * Return the effective number of events fetched.
898 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
899 u32 __user
*vec
, unsigned int max
)
901 unsigned int cur_out
;
902 unsigned int bytes
, avail
;
904 unsigned int nevents
;
905 struct mon_bin_hdr
*ep
;
909 mutex_lock(&rp
->fetch_lock
);
911 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
912 mutex_unlock(&rp
->fetch_lock
);
916 spin_lock_irqsave(&rp
->b_lock
, flags
);
918 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
923 while (bytes
< avail
) {
927 ep
= MON_OFF2HDR(rp
, cur_out
);
928 if (put_user(cur_out
, &vec
[nevents
])) {
929 mutex_unlock(&rp
->fetch_lock
);
934 size
= ep
->len_cap
+ PKT_SIZE
;
935 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
936 if ((cur_out
+= size
) >= rp
->b_size
)
937 cur_out
-= rp
->b_size
;
941 mutex_unlock(&rp
->fetch_lock
);
946 * Count events. This is almost the same as the above mon_bin_fetch,
947 * only we do not store offsets into user vector, and we have no limit.
949 static int mon_bin_queued(struct mon_reader_bin
*rp
)
951 unsigned int cur_out
;
952 unsigned int bytes
, avail
;
954 unsigned int nevents
;
955 struct mon_bin_hdr
*ep
;
958 mutex_lock(&rp
->fetch_lock
);
960 spin_lock_irqsave(&rp
->b_lock
, flags
);
962 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
967 while (bytes
< avail
) {
968 ep
= MON_OFF2HDR(rp
, cur_out
);
971 size
= ep
->len_cap
+ PKT_SIZE
;
972 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
973 if ((cur_out
+= size
) >= rp
->b_size
)
974 cur_out
-= rp
->b_size
;
978 mutex_unlock(&rp
->fetch_lock
);
984 static long mon_bin_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
986 struct mon_reader_bin
*rp
= file
->private_data
;
987 // struct mon_bus* mbus = rp->r.m_bus;
989 struct mon_bin_hdr
*ep
;
994 case MON_IOCQ_URB_LEN
:
996 * N.B. This only returns the size of data, without the header.
998 spin_lock_irqsave(&rp
->b_lock
, flags
);
999 if (!MON_RING_EMPTY(rp
)) {
1000 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
1003 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1006 case MON_IOCQ_RING_SIZE
:
1007 mutex_lock(&rp
->fetch_lock
);
1009 mutex_unlock(&rp
->fetch_lock
);
1012 case MON_IOCT_RING_SIZE
:
1014 * Changing the buffer size will flush it's contents; the new
1015 * buffer is allocated before releasing the old one to be sure
1016 * the device will stay functional also in case of memory
1021 struct mon_pgmap
*vec
;
1023 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
1026 size
= CHUNK_ALIGN(arg
);
1027 vec
= kcalloc(size
/ CHUNK_SIZE
, sizeof(struct mon_pgmap
),
1034 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
1040 mutex_lock(&rp
->fetch_lock
);
1041 spin_lock_irqsave(&rp
->b_lock
, flags
);
1042 if (rp
->mmap_active
) {
1043 mon_free_buff(vec
, size
/CHUNK_SIZE
);
1047 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
1051 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
1054 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1055 mutex_unlock(&rp
->fetch_lock
);
1059 case MON_IOCH_MFLUSH
:
1060 ret
= mon_bin_flush(rp
, arg
);
1066 struct mon_bin_get getb
;
1068 if (copy_from_user(&getb
, (void __user
*)arg
,
1069 sizeof(struct mon_bin_get
)))
1072 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
1074 ret
= mon_bin_get_event(file
, rp
, getb
.hdr
,
1075 (cmd
== MON_IOCX_GET
)? PKT_SZ_API0
: PKT_SZ_API1
,
1076 getb
.data
, (unsigned int)getb
.alloc
);
1080 case MON_IOCX_MFETCH
:
1082 struct mon_bin_mfetch mfetch
;
1083 struct mon_bin_mfetch __user
*uptr
;
1085 uptr
= (struct mon_bin_mfetch __user
*)arg
;
1087 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1090 if (mfetch
.nflush
) {
1091 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
1094 if (put_user(ret
, &uptr
->nflush
))
1097 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
1100 if (put_user(ret
, &uptr
->nfetch
))
1106 case MON_IOCG_STATS
: {
1107 struct mon_bin_stats __user
*sp
;
1108 unsigned int nevents
;
1109 unsigned int ndropped
;
1111 spin_lock_irqsave(&rp
->b_lock
, flags
);
1112 ndropped
= rp
->cnt_lost
;
1114 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1115 nevents
= mon_bin_queued(rp
);
1117 sp
= (struct mon_bin_stats __user
*)arg
;
1118 if (put_user(ndropped
, &sp
->dropped
))
1120 if (put_user(nevents
, &sp
->queued
))
1133 #ifdef CONFIG_COMPAT
1134 static long mon_bin_compat_ioctl(struct file
*file
,
1135 unsigned int cmd
, unsigned long arg
)
1137 struct mon_reader_bin
*rp
= file
->private_data
;
1142 case MON_IOCX_GET32
:
1143 case MON_IOCX_GETX32
:
1145 struct mon_bin_get32 getb
;
1147 if (copy_from_user(&getb
, (void __user
*)arg
,
1148 sizeof(struct mon_bin_get32
)))
1151 ret
= mon_bin_get_event(file
, rp
, compat_ptr(getb
.hdr32
),
1152 (cmd
== MON_IOCX_GET32
)? PKT_SZ_API0
: PKT_SZ_API1
,
1153 compat_ptr(getb
.data32
), getb
.alloc32
);
1159 case MON_IOCX_MFETCH32
:
1161 struct mon_bin_mfetch32 mfetch
;
1162 struct mon_bin_mfetch32 __user
*uptr
;
1164 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
1166 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1169 if (mfetch
.nflush32
) {
1170 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
1173 if (put_user(ret
, &uptr
->nflush32
))
1176 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
1180 if (put_user(ret
, &uptr
->nfetch32
))
1185 case MON_IOCG_STATS
:
1186 return mon_bin_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1188 case MON_IOCQ_URB_LEN
:
1189 case MON_IOCQ_RING_SIZE
:
1190 case MON_IOCT_RING_SIZE
:
1191 case MON_IOCH_MFLUSH
:
1192 return mon_bin_ioctl(file
, cmd
, arg
);
1199 #endif /* CONFIG_COMPAT */
1202 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1204 struct mon_reader_bin
*rp
= file
->private_data
;
1206 unsigned long flags
;
1208 if (file
->f_mode
& FMODE_READ
)
1209 poll_wait(file
, &rp
->b_wait
, wait
);
1211 spin_lock_irqsave(&rp
->b_lock
, flags
);
1212 if (!MON_RING_EMPTY(rp
))
1213 mask
|= EPOLLIN
| EPOLLRDNORM
; /* readable */
1214 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1219 * open and close: just keep track of how many times the device is
1220 * mapped, to use the proper memory allocation function.
1222 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1224 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1225 unsigned long flags
;
1227 spin_lock_irqsave(&rp
->b_lock
, flags
);
1229 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1232 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1234 unsigned long flags
;
1236 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1237 spin_lock_irqsave(&rp
->b_lock
, flags
);
1239 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1243 * Map ring pages to user space.
1245 static vm_fault_t
mon_bin_vma_fault(struct vm_fault
*vmf
)
1247 struct mon_reader_bin
*rp
= vmf
->vma
->vm_private_data
;
1248 unsigned long offset
, chunk_idx
;
1249 struct page
*pageptr
;
1251 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1252 if (offset
>= rp
->b_size
)
1253 return VM_FAULT_SIGBUS
;
1254 chunk_idx
= offset
/ CHUNK_SIZE
;
1255 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1257 vmf
->page
= pageptr
;
1261 static const struct vm_operations_struct mon_bin_vm_ops
= {
1262 .open
= mon_bin_vma_open
,
1263 .close
= mon_bin_vma_close
,
1264 .fault
= mon_bin_vma_fault
,
1267 static int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1269 /* don't do anything here: "fault" will set up page table entries */
1270 vma
->vm_ops
= &mon_bin_vm_ops
;
1271 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1272 vma
->vm_private_data
= filp
->private_data
;
1273 mon_bin_vma_open(vma
);
1277 static const struct file_operations mon_fops_binary
= {
1278 .owner
= THIS_MODULE
,
1279 .open
= mon_bin_open
,
1280 .llseek
= no_llseek
,
1281 .read
= mon_bin_read
,
1282 /* .write = mon_text_write, */
1283 .poll
= mon_bin_poll
,
1284 .unlocked_ioctl
= mon_bin_ioctl
,
1285 #ifdef CONFIG_COMPAT
1286 .compat_ioctl
= mon_bin_compat_ioctl
,
1288 .release
= mon_bin_release
,
1289 .mmap
= mon_bin_mmap
,
1292 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1294 DECLARE_WAITQUEUE(waita
, current
);
1295 unsigned long flags
;
1297 add_wait_queue(&rp
->b_wait
, &waita
);
1298 set_current_state(TASK_INTERRUPTIBLE
);
1300 spin_lock_irqsave(&rp
->b_lock
, flags
);
1301 while (MON_RING_EMPTY(rp
)) {
1302 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1304 if (file
->f_flags
& O_NONBLOCK
) {
1305 set_current_state(TASK_RUNNING
);
1306 remove_wait_queue(&rp
->b_wait
, &waita
);
1307 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1310 if (signal_pending(current
)) {
1311 remove_wait_queue(&rp
->b_wait
, &waita
);
1314 set_current_state(TASK_INTERRUPTIBLE
);
1316 spin_lock_irqsave(&rp
->b_lock
, flags
);
1318 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1320 set_current_state(TASK_RUNNING
);
1321 remove_wait_queue(&rp
->b_wait
, &waita
);
1325 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1328 unsigned long vaddr
;
1330 for (n
= 0; n
< npages
; n
++) {
1331 vaddr
= get_zeroed_page(GFP_KERNEL
);
1334 free_page((unsigned long) map
[n
].ptr
);
1337 map
[n
].ptr
= (unsigned char *) vaddr
;
1338 map
[n
].pg
= virt_to_page((void *) vaddr
);
1343 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1347 for (n
= 0; n
< npages
; n
++)
1348 free_page((unsigned long) map
[n
].ptr
);
1351 int mon_bin_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
1354 unsigned minor
= ubus
? ubus
->busnum
: 0;
1356 if (minor
>= MON_BIN_MAX_MINOR
)
1359 dev
= device_create(mon_bin_class
, ubus
? ubus
->controller
: NULL
,
1360 MKDEV(MAJOR(mon_bin_dev0
), minor
), NULL
,
1365 mbus
->classdev
= dev
;
1369 void mon_bin_del(struct mon_bus
*mbus
)
1371 device_destroy(mon_bin_class
, mbus
->classdev
->devt
);
1374 int __init
mon_bin_init(void)
1378 mon_bin_class
= class_create(THIS_MODULE
, "usbmon");
1379 if (IS_ERR(mon_bin_class
)) {
1380 rc
= PTR_ERR(mon_bin_class
);
1384 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1388 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1389 mon_bin_cdev
.owner
= THIS_MODULE
;
1391 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1398 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1400 class_destroy(mon_bin_class
);
1405 void mon_bin_exit(void)
1407 cdev_del(&mon_bin_cdev
);
1408 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1409 class_destroy(mon_bin_class
);