2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a binary format reader.
6 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
7 * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
10 #include <linux/kernel.h>
11 #include <linux/types.h>
13 #include <linux/cdev.h>
14 #include <linux/export.h>
15 #include <linux/usb.h>
16 #include <linux/poll.h>
17 #include <linux/compat.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/time64.h>
23 #include <asm/uaccess.h>
28 * Defined by USB 2.0 clause 9.3, table 9.2.
33 #define MON_IOC_MAGIC 0x92
35 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
36 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
37 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
38 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
39 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
40 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
41 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
42 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
43 /* #9 was MON_IOCT_SETAPI */
44 #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
47 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
48 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
49 #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
53 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
54 * But it's all right. Just use a simple way to make sure the chunk is never
55 * smaller than a page.
57 * N.B. An application does not know our chunk size.
59 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
60 * page-sized chunks for the time being.
62 #define CHUNK_SIZE PAGE_SIZE
63 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
66 * The magic limit was calculated so that it allows the monitoring
67 * application to pick data once in two ticks. This way, another application,
68 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
69 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
70 * enormous overhead built into the bus protocol, so we need about 1000 KB.
72 * This is still too much for most cases, where we just snoop a few
73 * descriptor fetches for enumeration. So, the default is a "reasonable"
74 * amount for systems with HZ=250 and incomplete bus saturation.
76 * XXX What about multi-megabyte URBs which take minutes to transfer?
78 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
79 #define BUFF_DFL CHUNK_ALIGN(300*1024)
80 #define BUFF_MIN CHUNK_ALIGN(8*1024)
83 * The per-event API header (2 per URB).
85 * This structure is seen in userland as defined by the documentation.
88 u64 id
; /* URB ID - from submission to callback */
89 unsigned char type
; /* Same as in text API; extensible. */
90 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
91 unsigned char epnum
; /* Endpoint number and transfer direction */
92 unsigned char devnum
; /* Device address */
93 unsigned short busnum
; /* Bus number */
96 s64 ts_sec
; /* getnstimeofday64 */
97 s32 ts_usec
; /* getnstimeofday64 */
99 unsigned int len_urb
; /* Length of data (submitted or actual) */
100 unsigned int len_cap
; /* Delivered length */
102 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
110 unsigned int xfer_flags
;
111 unsigned int ndesc
; /* Actual number of ISO descriptors */
115 * ISO vector, packed into the head of data stream.
116 * This has to take 16 bytes to make sure that the end of buffer
117 * wrap is not happening in the middle of a descriptor.
119 struct mon_bin_isodesc
{
121 unsigned int iso_off
;
122 unsigned int iso_len
;
126 /* per file statistic */
127 struct mon_bin_stats
{
133 struct mon_bin_hdr __user
*hdr
; /* Can be 48 bytes or 64. */
135 size_t alloc
; /* Length of data (can be zero) */
138 struct mon_bin_mfetch
{
139 u32 __user
*offvec
; /* Vector of events fetched */
140 u32 nfetch
; /* Number of events to fetch (out: fetched) */
141 u32 nflush
; /* Number of events to flush */
145 struct mon_bin_get32
{
151 struct mon_bin_mfetch32
{
158 /* Having these two values same prevents wrapping of the mon_bin_hdr */
162 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
163 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
165 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
167 /* max number of USB bus supported */
168 #define MON_BIN_MAX_MINOR 128
171 * The buffer: map of used pages.
175 unsigned char *ptr
; /* XXX just use page_to_virt everywhere? */
179 * This gets associated with an open file struct.
181 struct mon_reader_bin
{
182 /* The buffer: one per open. */
183 spinlock_t b_lock
; /* Protect b_cnt, b_in */
184 unsigned int b_size
; /* Current size of the buffer - bytes */
185 unsigned int b_cnt
; /* Bytes used */
186 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
187 unsigned int b_read
; /* Amount of read data in curr. pkt. */
188 struct mon_pgmap
*b_vec
; /* The map array */
189 wait_queue_head_t b_wait
; /* Wait for data here */
191 struct mutex fetch_lock
; /* Protect b_read, b_out */
194 /* A list of these is needed for "bus 0". Some time later. */
198 unsigned int cnt_lost
;
201 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
204 return (struct mon_bin_hdr
*)
205 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
208 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
210 static unsigned char xfer_to_pipe
[4] = {
211 PIPE_CONTROL
, PIPE_ISOCHRONOUS
, PIPE_BULK
, PIPE_INTERRUPT
214 static struct class *mon_bin_class
;
215 static dev_t mon_bin_dev0
;
216 static struct cdev mon_bin_cdev
;
218 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
219 unsigned int offset
, unsigned int size
);
220 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
221 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
222 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
225 * This is a "chunked memcpy". It does not manipulate any counters.
227 static unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
228 unsigned int off
, const unsigned char *from
, unsigned int length
)
230 unsigned int step_len
;
232 unsigned int in_page
;
236 * Determine step_len.
239 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
240 if (in_page
< step_len
)
244 * Copy data and advance pointers.
246 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
247 memcpy(buf
, from
, step_len
);
248 if ((off
+= step_len
) >= this->b_size
) off
= 0;
256 * This is a little worse than the above because it's "chunked copy_to_user".
257 * The return value is an error code, not an offset.
259 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
260 char __user
*to
, int length
)
262 unsigned int step_len
;
264 unsigned int in_page
;
268 * Determine step_len.
271 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
272 if (in_page
< step_len
)
276 * Copy data and advance pointers.
278 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
279 if (copy_to_user(to
, buf
, step_len
))
281 if ((off
+= step_len
) >= this->b_size
) off
= 0;
289 * Allocate an (aligned) area in the buffer.
290 * This is called under b_lock.
291 * Returns ~0 on failure.
293 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
298 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
299 if (rp
->b_cnt
+ size
> rp
->b_size
)
303 if ((rp
->b_in
+= size
) >= rp
->b_size
)
304 rp
->b_in
-= rp
->b_size
;
309 * This is the same thing as mon_buff_area_alloc, only it does not allow
310 * buffers to wrap. This is needed by applications which pass references
311 * into mmap-ed buffers up their stacks (libpcap can do that).
313 * Currently, we always have the header stuck with the data, although
314 * it is not strictly speaking necessary.
316 * When a buffer would wrap, we place a filler packet to mark the space.
318 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
322 unsigned int fill_size
;
324 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
325 if (rp
->b_cnt
+ size
> rp
->b_size
)
327 if (rp
->b_in
+ size
> rp
->b_size
) {
329 * This would wrap. Find if we still have space after
330 * skipping to the end of the buffer. If we do, place
331 * a filler packet and allocate a new packet.
333 fill_size
= rp
->b_size
- rp
->b_in
;
334 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
336 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
340 rp
->b_cnt
+= size
+ fill_size
;
341 } else if (rp
->b_in
+ size
== rp
->b_size
) {
354 * Return a few (kilo-)bytes to the head of the buffer.
355 * This is used if a data fetch fails.
357 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
360 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
363 rp
->b_in
+= rp
->b_size
;
368 * This has to be called under both b_lock and fetch_lock, because
369 * it accesses both b_cnt and b_out.
371 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
374 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
376 if ((rp
->b_out
+= size
) >= rp
->b_size
)
377 rp
->b_out
-= rp
->b_size
;
380 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
381 unsigned int offset
, unsigned int size
)
383 struct mon_bin_hdr
*ep
;
385 ep
= MON_OFF2HDR(rp
, offset
);
386 memset(ep
, 0, PKT_SIZE
);
388 ep
->len_cap
= size
- PKT_SIZE
;
391 static inline char mon_bin_get_setup(unsigned char *setupb
,
392 const struct urb
*urb
, char ev_type
)
395 if (urb
->setup_packet
== NULL
)
397 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
401 static unsigned int mon_bin_get_data(const struct mon_reader_bin
*rp
,
402 unsigned int offset
, struct urb
*urb
, unsigned int length
,
406 struct scatterlist
*sg
;
407 unsigned int this_len
;
410 if (urb
->num_sgs
== 0) {
411 if (urb
->transfer_buffer
== NULL
) {
415 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
419 /* If IOMMU coalescing occurred, we cannot trust sg_page */
420 if (urb
->transfer_flags
& URB_DMA_SG_COMBINED
) {
425 /* Copy up to the first non-addressable segment */
426 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
427 if (length
== 0 || PageHighMem(sg_page(sg
)))
429 this_len
= min_t(unsigned int, sg
->length
, length
);
430 offset
= mon_copy_to_buff(rp
, offset
, sg_virt(sg
),
442 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
443 * be used to determine the length of the whole contiguous buffer.
445 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin
*rp
,
446 struct urb
*urb
, unsigned int ndesc
)
448 struct usb_iso_packet_descriptor
*fp
;
452 fp
= urb
->iso_frame_desc
;
453 while (ndesc
-- != 0) {
454 if (fp
->actual_length
!= 0) {
455 if (fp
->offset
+ fp
->actual_length
> length
)
456 length
= fp
->offset
+ fp
->actual_length
;
463 static void mon_bin_get_isodesc(const struct mon_reader_bin
*rp
,
464 unsigned int offset
, struct urb
*urb
, char ev_type
, unsigned int ndesc
)
466 struct mon_bin_isodesc
*dp
;
467 struct usb_iso_packet_descriptor
*fp
;
469 fp
= urb
->iso_frame_desc
;
470 while (ndesc
-- != 0) {
471 dp
= (struct mon_bin_isodesc
*)
472 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
473 dp
->iso_status
= fp
->status
;
474 dp
->iso_off
= fp
->offset
;
475 dp
->iso_len
= (ev_type
== 'S') ? fp
->length
: fp
->actual_length
;
477 if ((offset
+= sizeof(struct mon_bin_isodesc
)) >= rp
->b_size
)
483 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
484 char ev_type
, int status
)
486 const struct usb_endpoint_descriptor
*epd
= &urb
->ep
->desc
;
487 struct timespec64 ts
;
489 unsigned int urb_length
;
493 unsigned int ndesc
, lendesc
;
495 struct mon_bin_hdr
*ep
;
498 getnstimeofday64(&ts
);
500 spin_lock_irqsave(&rp
->b_lock
, flags
);
503 * Find the maximum allowable length, then allocate space.
505 urb_length
= (ev_type
== 'S') ?
506 urb
->transfer_buffer_length
: urb
->actual_length
;
509 if (usb_endpoint_xfer_isoc(epd
)) {
510 if (urb
->number_of_packets
< 0) {
512 } else if (urb
->number_of_packets
>= ISODESC_MAX
) {
515 ndesc
= urb
->number_of_packets
;
517 if (ev_type
== 'C' && usb_urb_dir_in(urb
))
518 length
= mon_bin_collate_isodesc(rp
, urb
, ndesc
);
522 lendesc
= ndesc
*sizeof(struct mon_bin_isodesc
);
524 /* not an issue unless there's a subtle bug in a HCD somewhere */
525 if (length
>= urb
->transfer_buffer_length
)
526 length
= urb
->transfer_buffer_length
;
528 if (length
>= rp
->b_size
/5)
529 length
= rp
->b_size
/5;
531 if (usb_urb_dir_in(urb
)) {
532 if (ev_type
== 'S') {
536 /* Cannot rely on endpoint number in case of control ep.0 */
539 if (ev_type
== 'C') {
546 if (rp
->mmap_active
) {
547 offset
= mon_buff_area_alloc_contiguous(rp
,
548 length
+ PKT_SIZE
+ lendesc
);
550 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
+ lendesc
);
554 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
558 ep
= MON_OFF2HDR(rp
, offset
);
559 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
562 * Fill the allocated area.
564 memset(ep
, 0, PKT_SIZE
);
566 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(epd
)];
567 ep
->epnum
= dir
| usb_endpoint_num(epd
);
568 ep
->devnum
= urb
->dev
->devnum
;
569 ep
->busnum
= urb
->dev
->bus
->busnum
;
570 ep
->id
= (unsigned long) urb
;
571 ep
->ts_sec
= ts
.tv_sec
;
572 ep
->ts_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
574 ep
->len_urb
= urb_length
;
575 ep
->len_cap
= length
+ lendesc
;
576 ep
->xfer_flags
= urb
->transfer_flags
;
578 if (usb_endpoint_xfer_int(epd
)) {
579 ep
->interval
= urb
->interval
;
580 } else if (usb_endpoint_xfer_isoc(epd
)) {
581 ep
->interval
= urb
->interval
;
582 ep
->start_frame
= urb
->start_frame
;
583 ep
->s
.iso
.error_count
= urb
->error_count
;
584 ep
->s
.iso
.numdesc
= urb
->number_of_packets
;
587 if (usb_endpoint_xfer_control(epd
) && ev_type
== 'S') {
588 ep
->flag_setup
= mon_bin_get_setup(ep
->s
.setup
, urb
, ev_type
);
590 ep
->flag_setup
= '-';
595 mon_bin_get_isodesc(rp
, offset
, urb
, ev_type
, ndesc
);
596 if ((offset
+= lendesc
) >= rp
->b_size
)
597 offset
-= rp
->b_size
;
601 length
= mon_bin_get_data(rp
, offset
, urb
, length
,
604 delta
= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
605 ep
->len_cap
-= length
;
606 delta
-= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
607 mon_buff_area_shrink(rp
, delta
);
610 ep
->flag_data
= data_tag
;
613 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
615 wake_up(&rp
->b_wait
);
618 static void mon_bin_submit(void *data
, struct urb
*urb
)
620 struct mon_reader_bin
*rp
= data
;
621 mon_bin_event(rp
, urb
, 'S', -EINPROGRESS
);
624 static void mon_bin_complete(void *data
, struct urb
*urb
, int status
)
626 struct mon_reader_bin
*rp
= data
;
627 mon_bin_event(rp
, urb
, 'C', status
);
630 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
632 struct mon_reader_bin
*rp
= data
;
633 struct timespec64 ts
;
636 struct mon_bin_hdr
*ep
;
638 getnstimeofday64(&ts
);
640 spin_lock_irqsave(&rp
->b_lock
, flags
);
642 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
644 /* Not incrementing cnt_lost. Just because. */
645 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
649 ep
= MON_OFF2HDR(rp
, offset
);
651 memset(ep
, 0, PKT_SIZE
);
653 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(&urb
->ep
->desc
)];
654 ep
->epnum
= usb_urb_dir_in(urb
) ? USB_DIR_IN
: 0;
655 ep
->epnum
|= usb_endpoint_num(&urb
->ep
->desc
);
656 ep
->devnum
= urb
->dev
->devnum
;
657 ep
->busnum
= urb
->dev
->bus
->busnum
;
658 ep
->id
= (unsigned long) urb
;
659 ep
->ts_sec
= ts
.tv_sec
;
660 ep
->ts_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
663 ep
->flag_setup
= '-';
666 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
668 wake_up(&rp
->b_wait
);
671 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
673 struct mon_bus
*mbus
;
674 struct mon_reader_bin
*rp
;
678 mutex_lock(&mon_lock
);
679 mbus
= mon_bus_lookup(iminor(inode
));
681 mutex_unlock(&mon_lock
);
684 if (mbus
!= &mon_bus0
&& mbus
->u_bus
== NULL
) {
685 printk(KERN_ERR TAG
": consistency error on open\n");
686 mutex_unlock(&mon_lock
);
690 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
695 spin_lock_init(&rp
->b_lock
);
696 init_waitqueue_head(&rp
->b_wait
);
697 mutex_init(&rp
->fetch_lock
);
698 rp
->b_size
= BUFF_DFL
;
700 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
701 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
706 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
711 rp
->r
.rnf_submit
= mon_bin_submit
;
712 rp
->r
.rnf_error
= mon_bin_error
;
713 rp
->r
.rnf_complete
= mon_bin_complete
;
715 mon_reader_add(mbus
, &rp
->r
);
717 file
->private_data
= rp
;
718 mutex_unlock(&mon_lock
);
726 mutex_unlock(&mon_lock
);
731 * Extract an event from buffer and copy it to user space.
732 * Wait if there is no event ready.
733 * Returns zero or error.
735 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
736 struct mon_bin_hdr __user
*hdr
, unsigned int hdrbytes
,
737 void __user
*data
, unsigned int nbytes
)
740 struct mon_bin_hdr
*ep
;
745 mutex_lock(&rp
->fetch_lock
);
747 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
748 mutex_unlock(&rp
->fetch_lock
);
752 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
754 if (copy_to_user(hdr
, ep
, hdrbytes
)) {
755 mutex_unlock(&rp
->fetch_lock
);
759 step_len
= min(ep
->len_cap
, nbytes
);
760 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
762 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
763 mutex_unlock(&rp
->fetch_lock
);
767 spin_lock_irqsave(&rp
->b_lock
, flags
);
768 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
769 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
772 mutex_unlock(&rp
->fetch_lock
);
776 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
778 struct mon_reader_bin
*rp
= file
->private_data
;
779 struct mon_bus
* mbus
= rp
->r
.m_bus
;
781 mutex_lock(&mon_lock
);
783 if (mbus
->nreaders
<= 0) {
784 printk(KERN_ERR TAG
": consistency error on close\n");
785 mutex_unlock(&mon_lock
);
788 mon_reader_del(mbus
, &rp
->r
);
790 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
794 mutex_unlock(&mon_lock
);
798 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
799 size_t nbytes
, loff_t
*ppos
)
801 struct mon_reader_bin
*rp
= file
->private_data
;
802 unsigned int hdrbytes
= PKT_SZ_API0
;
804 struct mon_bin_hdr
*ep
;
811 mutex_lock(&rp
->fetch_lock
);
813 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
814 mutex_unlock(&rp
->fetch_lock
);
818 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
820 if (rp
->b_read
< hdrbytes
) {
821 step_len
= min(nbytes
, (size_t)(hdrbytes
- rp
->b_read
));
822 ptr
= ((char *)ep
) + rp
->b_read
;
823 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
824 mutex_unlock(&rp
->fetch_lock
);
829 rp
->b_read
+= step_len
;
833 if (rp
->b_read
>= hdrbytes
) {
834 step_len
= ep
->len_cap
;
835 step_len
-= rp
->b_read
- hdrbytes
;
836 if (step_len
> nbytes
)
838 offset
= rp
->b_out
+ PKT_SIZE
;
839 offset
+= rp
->b_read
- hdrbytes
;
840 if (offset
>= rp
->b_size
)
841 offset
-= rp
->b_size
;
842 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
843 mutex_unlock(&rp
->fetch_lock
);
848 rp
->b_read
+= step_len
;
853 * Check if whole packet was read, and if so, jump to the next one.
855 if (rp
->b_read
>= hdrbytes
+ ep
->len_cap
) {
856 spin_lock_irqsave(&rp
->b_lock
, flags
);
857 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
858 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
862 mutex_unlock(&rp
->fetch_lock
);
867 * Remove at most nevents from chunked buffer.
868 * Returns the number of removed events.
870 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
873 struct mon_bin_hdr
*ep
;
876 mutex_lock(&rp
->fetch_lock
);
877 spin_lock_irqsave(&rp
->b_lock
, flags
);
878 for (i
= 0; i
< nevents
; ++i
) {
879 if (MON_RING_EMPTY(rp
))
882 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
883 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
885 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
887 mutex_unlock(&rp
->fetch_lock
);
892 * Fetch at most max event offsets into the buffer and put them into vec.
893 * The events are usually freed later with mon_bin_flush.
894 * Return the effective number of events fetched.
896 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
897 u32 __user
*vec
, unsigned int max
)
899 unsigned int cur_out
;
900 unsigned int bytes
, avail
;
902 unsigned int nevents
;
903 struct mon_bin_hdr
*ep
;
907 mutex_lock(&rp
->fetch_lock
);
909 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
910 mutex_unlock(&rp
->fetch_lock
);
914 spin_lock_irqsave(&rp
->b_lock
, flags
);
916 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
921 while (bytes
< avail
) {
925 ep
= MON_OFF2HDR(rp
, cur_out
);
926 if (put_user(cur_out
, &vec
[nevents
])) {
927 mutex_unlock(&rp
->fetch_lock
);
932 size
= ep
->len_cap
+ PKT_SIZE
;
933 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
934 if ((cur_out
+= size
) >= rp
->b_size
)
935 cur_out
-= rp
->b_size
;
939 mutex_unlock(&rp
->fetch_lock
);
944 * Count events. This is almost the same as the above mon_bin_fetch,
945 * only we do not store offsets into user vector, and we have no limit.
947 static int mon_bin_queued(struct mon_reader_bin
*rp
)
949 unsigned int cur_out
;
950 unsigned int bytes
, avail
;
952 unsigned int nevents
;
953 struct mon_bin_hdr
*ep
;
956 mutex_lock(&rp
->fetch_lock
);
958 spin_lock_irqsave(&rp
->b_lock
, flags
);
960 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
965 while (bytes
< avail
) {
966 ep
= MON_OFF2HDR(rp
, cur_out
);
969 size
= ep
->len_cap
+ PKT_SIZE
;
970 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
971 if ((cur_out
+= size
) >= rp
->b_size
)
972 cur_out
-= rp
->b_size
;
976 mutex_unlock(&rp
->fetch_lock
);
982 static long mon_bin_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
984 struct mon_reader_bin
*rp
= file
->private_data
;
985 // struct mon_bus* mbus = rp->r.m_bus;
987 struct mon_bin_hdr
*ep
;
992 case MON_IOCQ_URB_LEN
:
994 * N.B. This only returns the size of data, without the header.
996 spin_lock_irqsave(&rp
->b_lock
, flags
);
997 if (!MON_RING_EMPTY(rp
)) {
998 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
1001 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1004 case MON_IOCQ_RING_SIZE
:
1008 case MON_IOCT_RING_SIZE
:
1010 * Changing the buffer size will flush it's contents; the new
1011 * buffer is allocated before releasing the old one to be sure
1012 * the device will stay functional also in case of memory
1017 struct mon_pgmap
*vec
;
1019 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
1022 size
= CHUNK_ALIGN(arg
);
1023 vec
= kzalloc(sizeof(struct mon_pgmap
) * (size
/ CHUNK_SIZE
), GFP_KERNEL
);
1029 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
1035 mutex_lock(&rp
->fetch_lock
);
1036 spin_lock_irqsave(&rp
->b_lock
, flags
);
1037 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
1041 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
1043 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1044 mutex_unlock(&rp
->fetch_lock
);
1048 case MON_IOCH_MFLUSH
:
1049 ret
= mon_bin_flush(rp
, arg
);
1055 struct mon_bin_get getb
;
1057 if (copy_from_user(&getb
, (void __user
*)arg
,
1058 sizeof(struct mon_bin_get
)))
1061 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
1063 ret
= mon_bin_get_event(file
, rp
, getb
.hdr
,
1064 (cmd
== MON_IOCX_GET
)? PKT_SZ_API0
: PKT_SZ_API1
,
1065 getb
.data
, (unsigned int)getb
.alloc
);
1069 case MON_IOCX_MFETCH
:
1071 struct mon_bin_mfetch mfetch
;
1072 struct mon_bin_mfetch __user
*uptr
;
1074 uptr
= (struct mon_bin_mfetch __user
*)arg
;
1076 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1079 if (mfetch
.nflush
) {
1080 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
1083 if (put_user(ret
, &uptr
->nflush
))
1086 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
1089 if (put_user(ret
, &uptr
->nfetch
))
1095 case MON_IOCG_STATS
: {
1096 struct mon_bin_stats __user
*sp
;
1097 unsigned int nevents
;
1098 unsigned int ndropped
;
1100 spin_lock_irqsave(&rp
->b_lock
, flags
);
1101 ndropped
= rp
->cnt_lost
;
1103 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1104 nevents
= mon_bin_queued(rp
);
1106 sp
= (struct mon_bin_stats __user
*)arg
;
1107 if (put_user(ndropped
, &sp
->dropped
))
1109 if (put_user(nevents
, &sp
->queued
))
1122 #ifdef CONFIG_COMPAT
1123 static long mon_bin_compat_ioctl(struct file
*file
,
1124 unsigned int cmd
, unsigned long arg
)
1126 struct mon_reader_bin
*rp
= file
->private_data
;
1131 case MON_IOCX_GET32
:
1132 case MON_IOCX_GETX32
:
1134 struct mon_bin_get32 getb
;
1136 if (copy_from_user(&getb
, (void __user
*)arg
,
1137 sizeof(struct mon_bin_get32
)))
1140 ret
= mon_bin_get_event(file
, rp
, compat_ptr(getb
.hdr32
),
1141 (cmd
== MON_IOCX_GET32
)? PKT_SZ_API0
: PKT_SZ_API1
,
1142 compat_ptr(getb
.data32
), getb
.alloc32
);
1148 case MON_IOCX_MFETCH32
:
1150 struct mon_bin_mfetch32 mfetch
;
1151 struct mon_bin_mfetch32 __user
*uptr
;
1153 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
1155 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1158 if (mfetch
.nflush32
) {
1159 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
1162 if (put_user(ret
, &uptr
->nflush32
))
1165 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
1169 if (put_user(ret
, &uptr
->nfetch32
))
1174 case MON_IOCG_STATS
:
1175 return mon_bin_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1177 case MON_IOCQ_URB_LEN
:
1178 case MON_IOCQ_RING_SIZE
:
1179 case MON_IOCT_RING_SIZE
:
1180 case MON_IOCH_MFLUSH
:
1181 return mon_bin_ioctl(file
, cmd
, arg
);
1188 #endif /* CONFIG_COMPAT */
1191 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1193 struct mon_reader_bin
*rp
= file
->private_data
;
1194 unsigned int mask
= 0;
1195 unsigned long flags
;
1197 if (file
->f_mode
& FMODE_READ
)
1198 poll_wait(file
, &rp
->b_wait
, wait
);
1200 spin_lock_irqsave(&rp
->b_lock
, flags
);
1201 if (!MON_RING_EMPTY(rp
))
1202 mask
|= POLLIN
| POLLRDNORM
; /* readable */
1203 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1208 * open and close: just keep track of how many times the device is
1209 * mapped, to use the proper memory allocation function.
1211 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1213 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1217 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1219 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1224 * Map ring pages to user space.
1226 static int mon_bin_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1228 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1229 unsigned long offset
, chunk_idx
;
1230 struct page
*pageptr
;
1232 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1233 if (offset
>= rp
->b_size
)
1234 return VM_FAULT_SIGBUS
;
1235 chunk_idx
= offset
/ CHUNK_SIZE
;
1236 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1238 vmf
->page
= pageptr
;
1242 static const struct vm_operations_struct mon_bin_vm_ops
= {
1243 .open
= mon_bin_vma_open
,
1244 .close
= mon_bin_vma_close
,
1245 .fault
= mon_bin_vma_fault
,
1248 static int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1250 /* don't do anything here: "fault" will set up page table entries */
1251 vma
->vm_ops
= &mon_bin_vm_ops
;
1252 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1253 vma
->vm_private_data
= filp
->private_data
;
1254 mon_bin_vma_open(vma
);
1258 static const struct file_operations mon_fops_binary
= {
1259 .owner
= THIS_MODULE
,
1260 .open
= mon_bin_open
,
1261 .llseek
= no_llseek
,
1262 .read
= mon_bin_read
,
1263 /* .write = mon_text_write, */
1264 .poll
= mon_bin_poll
,
1265 .unlocked_ioctl
= mon_bin_ioctl
,
1266 #ifdef CONFIG_COMPAT
1267 .compat_ioctl
= mon_bin_compat_ioctl
,
1269 .release
= mon_bin_release
,
1270 .mmap
= mon_bin_mmap
,
1273 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1275 DECLARE_WAITQUEUE(waita
, current
);
1276 unsigned long flags
;
1278 add_wait_queue(&rp
->b_wait
, &waita
);
1279 set_current_state(TASK_INTERRUPTIBLE
);
1281 spin_lock_irqsave(&rp
->b_lock
, flags
);
1282 while (MON_RING_EMPTY(rp
)) {
1283 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1285 if (file
->f_flags
& O_NONBLOCK
) {
1286 set_current_state(TASK_RUNNING
);
1287 remove_wait_queue(&rp
->b_wait
, &waita
);
1288 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1291 if (signal_pending(current
)) {
1292 remove_wait_queue(&rp
->b_wait
, &waita
);
1295 set_current_state(TASK_INTERRUPTIBLE
);
1297 spin_lock_irqsave(&rp
->b_lock
, flags
);
1299 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1301 set_current_state(TASK_RUNNING
);
1302 remove_wait_queue(&rp
->b_wait
, &waita
);
1306 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1309 unsigned long vaddr
;
1311 for (n
= 0; n
< npages
; n
++) {
1312 vaddr
= get_zeroed_page(GFP_KERNEL
);
1315 free_page((unsigned long) map
[n
].ptr
);
1318 map
[n
].ptr
= (unsigned char *) vaddr
;
1319 map
[n
].pg
= virt_to_page((void *) vaddr
);
1324 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1328 for (n
= 0; n
< npages
; n
++)
1329 free_page((unsigned long) map
[n
].ptr
);
1332 int mon_bin_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
1335 unsigned minor
= ubus
? ubus
->busnum
: 0;
1337 if (minor
>= MON_BIN_MAX_MINOR
)
1340 dev
= device_create(mon_bin_class
, ubus
? ubus
->controller
: NULL
,
1341 MKDEV(MAJOR(mon_bin_dev0
), minor
), NULL
,
1346 mbus
->classdev
= dev
;
1350 void mon_bin_del(struct mon_bus
*mbus
)
1352 device_destroy(mon_bin_class
, mbus
->classdev
->devt
);
1355 int __init
mon_bin_init(void)
1359 mon_bin_class
= class_create(THIS_MODULE
, "usbmon");
1360 if (IS_ERR(mon_bin_class
)) {
1361 rc
= PTR_ERR(mon_bin_class
);
1365 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1369 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1370 mon_bin_cdev
.owner
= THIS_MODULE
;
1372 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1379 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1381 class_destroy(mon_bin_class
);
1386 void mon_bin_exit(void)
1388 cdev_del(&mon_bin_cdev
);
1389 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1390 class_destroy(mon_bin_class
);