2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a binary format reader.
10 #include <linux/kernel.h>
11 #include <linux/sched/signal.h>
12 #include <linux/types.h>
14 #include <linux/cdev.h>
15 #include <linux/export.h>
16 #include <linux/usb.h>
17 #include <linux/poll.h>
18 #include <linux/compat.h>
20 #include <linux/scatterlist.h>
21 #include <linux/slab.h>
22 #include <linux/time64.h>
24 #include <linux/uaccess.h>
29 * Defined by USB 2.0 clause 9.3, table 9.2.
34 #define MON_IOC_MAGIC 0x92
36 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
37 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
38 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
39 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
40 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
41 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
42 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
43 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
44 /* #9 was MON_IOCT_SETAPI */
45 #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
48 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
49 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
50 #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
54 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
55 * But it's all right. Just use a simple way to make sure the chunk is never
56 * smaller than a page.
58 * N.B. An application does not know our chunk size.
60 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
61 * page-sized chunks for the time being.
63 #define CHUNK_SIZE PAGE_SIZE
64 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
67 * The magic limit was calculated so that it allows the monitoring
68 * application to pick data once in two ticks. This way, another application,
69 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
70 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
71 * enormous overhead built into the bus protocol, so we need about 1000 KB.
73 * This is still too much for most cases, where we just snoop a few
74 * descriptor fetches for enumeration. So, the default is a "reasonable"
75 * amount for systems with HZ=250 and incomplete bus saturation.
77 * XXX What about multi-megabyte URBs which take minutes to transfer?
79 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
80 #define BUFF_DFL CHUNK_ALIGN(300*1024)
81 #define BUFF_MIN CHUNK_ALIGN(8*1024)
84 * The per-event API header (2 per URB).
86 * This structure is seen in userland as defined by the documentation.
89 u64 id; /* URB ID - from submission to callback */
90 unsigned char type; /* Same as in text API; extensible. */
91 unsigned char xfer_type; /* ISO, Intr, Control, Bulk */
92 unsigned char epnum; /* Endpoint number and transfer direction */
93 unsigned char devnum; /* Device address */
94 unsigned short busnum; /* Bus number */
97 s64 ts_sec; /* getnstimeofday64 */
98 s32 ts_usec; /* getnstimeofday64 */
100 unsigned int len_urb; /* Length of data (submitted or actual) */
101 unsigned int len_cap; /* Delivered length */
103 unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
111 unsigned int xfer_flags;
112 unsigned int ndesc; /* Actual number of ISO descriptors */
116 * ISO vector, packed into the head of data stream.
117 * This has to take 16 bytes to make sure that the end of buffer
118 * wrap is not happening in the middle of a descriptor.
120 struct mon_bin_isodesc {
122 unsigned int iso_off;
123 unsigned int iso_len;
127 /* per file statistic */
128 struct mon_bin_stats {
134 struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
136 size_t alloc; /* Length of data (can be zero) */
139 struct mon_bin_mfetch {
140 u32 __user *offvec; /* Vector of events fetched */
141 u32 nfetch; /* Number of events to fetch (out: fetched) */
142 u32 nflush; /* Number of events to flush */
146 struct mon_bin_get32 {
152 struct mon_bin_mfetch32 {
159 /* Having these two values same prevents wrapping of the mon_bin_hdr */
163 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
164 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
166 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
168 /* max number of USB bus supported */
169 #define MON_BIN_MAX_MINOR 128
172 * The buffer: map of used pages.
176 unsigned char *ptr; /* XXX just use page_to_virt everywhere? */
180 * This gets associated with an open file struct.
182 struct mon_reader_bin {
183 /* The buffer: one per open. */
184 spinlock_t b_lock; /* Protect b_cnt, b_in */
185 unsigned int b_size; /* Current size of the buffer - bytes */
186 unsigned int b_cnt; /* Bytes used */
187 unsigned int b_in, b_out; /* Offsets into buffer - bytes */
188 unsigned int b_read; /* Amount of read data in curr. pkt. */
189 struct mon_pgmap *b_vec; /* The map array */
190 wait_queue_head_t b_wait; /* Wait for data here */
192 struct mutex fetch_lock; /* Protect b_read, b_out */
195 /* A list of these is needed for "bus 0". Some time later. */
199 unsigned int cnt_lost;
202 static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
205 return (struct mon_bin_hdr *)
206 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
209 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
211 static unsigned char xfer_to_pipe[4] = {
212 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
215 static struct class *mon_bin_class;
216 static dev_t mon_bin_dev0;
217 static struct cdev mon_bin_cdev;
219 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
220 unsigned int offset, unsigned int size);
221 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
222 static int mon_alloc_buff(struct mon_pgmap *map, int npages);
223 static void mon_free_buff(struct mon_pgmap *map, int npages);
226 * This is a "chunked memcpy". It does not manipulate any counters.
228 static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
229 unsigned int off, const unsigned char *from, unsigned int length)
231 unsigned int step_len;
233 unsigned int in_page;
237 * Determine step_len.
240 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
241 if (in_page < step_len)
245 * Copy data and advance pointers.
247 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
248 memcpy(buf, from, step_len);
249 if ((off += step_len) >= this->b_size) off = 0;
257 * This is a little worse than the above because it's "chunked copy_to_user".
258 * The return value is an error code, not an offset.
260 static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
261 char __user *to, int length)
263 unsigned int step_len;
265 unsigned int in_page;
269 * Determine step_len.
272 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
273 if (in_page < step_len)
277 * Copy data and advance pointers.
279 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
280 if (copy_to_user(to, buf, step_len))
282 if ((off += step_len) >= this->b_size) off = 0;
290 * Allocate an (aligned) area in the buffer.
291 * This is called under b_lock.
292 * Returns ~0 on failure.
294 static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
299 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
300 if (rp->b_cnt + size > rp->b_size)
304 if ((rp->b_in += size) >= rp->b_size)
305 rp->b_in -= rp->b_size;
310 * This is the same thing as mon_buff_area_alloc, only it does not allow
311 * buffers to wrap. This is needed by applications which pass references
312 * into mmap-ed buffers up their stacks (libpcap can do that).
314 * Currently, we always have the header stuck with the data, although
315 * it is not strictly speaking necessary.
317 * When a buffer would wrap, we place a filler packet to mark the space.
319 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
323 unsigned int fill_size;
325 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
326 if (rp->b_cnt + size > rp->b_size)
328 if (rp->b_in + size > rp->b_size) {
330 * This would wrap. Find if we still have space after
331 * skipping to the end of the buffer. If we do, place
332 * a filler packet and allocate a new packet.
334 fill_size = rp->b_size - rp->b_in;
335 if (rp->b_cnt + size + fill_size > rp->b_size)
337 mon_buff_area_fill(rp, rp->b_in, fill_size);
341 rp->b_cnt += size + fill_size;
342 } else if (rp->b_in + size == rp->b_size) {
355 * Return a few (kilo-)bytes to the head of the buffer.
356 * This is used if a data fetch fails.
358 static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
361 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
364 rp->b_in += rp->b_size;
369 * This has to be called under both b_lock and fetch_lock, because
370 * it accesses both b_cnt and b_out.
372 static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
375 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
377 if ((rp->b_out += size) >= rp->b_size)
378 rp->b_out -= rp->b_size;
381 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
382 unsigned int offset, unsigned int size)
384 struct mon_bin_hdr *ep;
386 ep = MON_OFF2HDR(rp, offset);
387 memset(ep, 0, PKT_SIZE);
389 ep->len_cap = size - PKT_SIZE;
392 static inline char mon_bin_get_setup(unsigned char *setupb,
393 const struct urb *urb, char ev_type)
396 if (urb->setup_packet == NULL)
398 memcpy(setupb, urb->setup_packet, SETUP_LEN);
402 static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
403 unsigned int offset, struct urb *urb, unsigned int length,
407 struct scatterlist *sg;
408 unsigned int this_len;
411 if (urb->num_sgs == 0) {
412 if (urb->transfer_buffer == NULL) {
416 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
420 /* If IOMMU coalescing occurred, we cannot trust sg_page */
421 if (urb->transfer_flags & URB_DMA_SG_COMBINED) {
426 /* Copy up to the first non-addressable segment */
427 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
428 if (length == 0 || PageHighMem(sg_page(sg)))
430 this_len = min_t(unsigned int, sg->length, length);
431 offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
443 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
444 * be used to determine the length of the whole contiguous buffer.
446 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin *rp,
447 struct urb *urb, unsigned int ndesc)
449 struct usb_iso_packet_descriptor *fp;
453 fp = urb->iso_frame_desc;
454 while (ndesc-- != 0) {
455 if (fp->actual_length != 0) {
456 if (fp->offset + fp->actual_length > length)
457 length = fp->offset + fp->actual_length;
464 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
465 unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
467 struct mon_bin_isodesc *dp;
468 struct usb_iso_packet_descriptor *fp;
470 fp = urb->iso_frame_desc;
471 while (ndesc-- != 0) {
472 dp = (struct mon_bin_isodesc *)
473 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
474 dp->iso_status = fp->status;
475 dp->iso_off = fp->offset;
476 dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
478 if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
484 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
485 char ev_type, int status)
487 const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
488 struct timespec64 ts;
490 unsigned int urb_length;
494 unsigned int ndesc, lendesc;
496 struct mon_bin_hdr *ep;
499 getnstimeofday64(&ts);
501 spin_lock_irqsave(&rp->b_lock, flags);
504 * Find the maximum allowable length, then allocate space.
506 urb_length = (ev_type == 'S') ?
507 urb->transfer_buffer_length : urb->actual_length;
510 if (usb_endpoint_xfer_isoc(epd)) {
511 if (urb->number_of_packets < 0) {
513 } else if (urb->number_of_packets >= ISODESC_MAX) {
516 ndesc = urb->number_of_packets;
518 if (ev_type == 'C' && usb_urb_dir_in(urb))
519 length = mon_bin_collate_isodesc(rp, urb, ndesc);
523 lendesc = ndesc*sizeof(struct mon_bin_isodesc);
525 /* not an issue unless there's a subtle bug in a HCD somewhere */
526 if (length >= urb->transfer_buffer_length)
527 length = urb->transfer_buffer_length;
529 if (length >= rp->b_size/5)
530 length = rp->b_size/5;
532 if (usb_urb_dir_in(urb)) {
533 if (ev_type == 'S') {
537 /* Cannot rely on endpoint number in case of control ep.0 */
540 if (ev_type == 'C') {
547 if (rp->mmap_active) {
548 offset = mon_buff_area_alloc_contiguous(rp,
549 length + PKT_SIZE + lendesc);
551 offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
555 spin_unlock_irqrestore(&rp->b_lock, flags);
559 ep = MON_OFF2HDR(rp, offset);
560 if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
563 * Fill the allocated area.
565 memset(ep, 0, PKT_SIZE);
567 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
568 ep->epnum = dir | usb_endpoint_num(epd);
569 ep->devnum = urb->dev->devnum;
570 ep->busnum = urb->dev->bus->busnum;
571 ep->id = (unsigned long) urb;
572 ep->ts_sec = ts.tv_sec;
573 ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
575 ep->len_urb = urb_length;
576 ep->len_cap = length + lendesc;
577 ep->xfer_flags = urb->transfer_flags;
579 if (usb_endpoint_xfer_int(epd)) {
580 ep->interval = urb->interval;
581 } else if (usb_endpoint_xfer_isoc(epd)) {
582 ep->interval = urb->interval;
583 ep->start_frame = urb->start_frame;
584 ep->s.iso.error_count = urb->error_count;
585 ep->s.iso.numdesc = urb->number_of_packets;
588 if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
589 ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
591 ep->flag_setup = '-';
596 mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
597 if ((offset += lendesc) >= rp->b_size)
598 offset -= rp->b_size;
602 length = mon_bin_get_data(rp, offset, urb, length,
605 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
606 ep->len_cap -= length;
607 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
608 mon_buff_area_shrink(rp, delta);
611 ep->flag_data = data_tag;
614 spin_unlock_irqrestore(&rp->b_lock, flags);
616 wake_up(&rp->b_wait);
619 static void mon_bin_submit(void *data, struct urb *urb)
621 struct mon_reader_bin *rp = data;
622 mon_bin_event(rp, urb, 'S', -EINPROGRESS);
625 static void mon_bin_complete(void *data, struct urb *urb, int status)
627 struct mon_reader_bin *rp = data;
628 mon_bin_event(rp, urb, 'C', status);
631 static void mon_bin_error(void *data, struct urb *urb, int error)
633 struct mon_reader_bin *rp = data;
634 struct timespec64 ts;
637 struct mon_bin_hdr *ep;
639 getnstimeofday64(&ts);
641 spin_lock_irqsave(&rp->b_lock, flags);
643 offset = mon_buff_area_alloc(rp, PKT_SIZE);
645 /* Not incrementing cnt_lost. Just because. */
646 spin_unlock_irqrestore(&rp->b_lock, flags);
650 ep = MON_OFF2HDR(rp, offset);
652 memset(ep, 0, PKT_SIZE);
654 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
655 ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
656 ep->epnum |= usb_endpoint_num(&urb->ep->desc);
657 ep->devnum = urb->dev->devnum;
658 ep->busnum = urb->dev->bus->busnum;
659 ep->id = (unsigned long) urb;
660 ep->ts_sec = ts.tv_sec;
661 ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
664 ep->flag_setup = '-';
667 spin_unlock_irqrestore(&rp->b_lock, flags);
669 wake_up(&rp->b_wait);
672 static int mon_bin_open(struct inode *inode, struct file *file)
674 struct mon_bus *mbus;
675 struct mon_reader_bin *rp;
679 mutex_lock(&mon_lock);
680 mbus = mon_bus_lookup(iminor(inode));
682 mutex_unlock(&mon_lock);
685 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
686 printk(KERN_ERR TAG ": consistency error on open\n");
687 mutex_unlock(&mon_lock);
691 rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
696 spin_lock_init(&rp->b_lock);
697 init_waitqueue_head(&rp->b_wait);
698 mutex_init(&rp->fetch_lock);
699 rp->b_size = BUFF_DFL;
701 size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
702 if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
707 if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
712 rp->r.rnf_submit = mon_bin_submit;
713 rp->r.rnf_error = mon_bin_error;
714 rp->r.rnf_complete = mon_bin_complete;
716 mon_reader_add(mbus, &rp->r);
718 file->private_data = rp;
719 mutex_unlock(&mon_lock);
727 mutex_unlock(&mon_lock);
732 * Extract an event from buffer and copy it to user space.
733 * Wait if there is no event ready.
734 * Returns zero or error.
736 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
737 struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
738 void __user *data, unsigned int nbytes)
741 struct mon_bin_hdr *ep;
746 mutex_lock(&rp->fetch_lock);
748 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
749 mutex_unlock(&rp->fetch_lock);
753 ep = MON_OFF2HDR(rp, rp->b_out);
755 if (copy_to_user(hdr, ep, hdrbytes)) {
756 mutex_unlock(&rp->fetch_lock);
760 step_len = min(ep->len_cap, nbytes);
761 if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
763 if (copy_from_buf(rp, offset, data, step_len)) {
764 mutex_unlock(&rp->fetch_lock);
768 spin_lock_irqsave(&rp->b_lock, flags);
769 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
770 spin_unlock_irqrestore(&rp->b_lock, flags);
773 mutex_unlock(&rp->fetch_lock);
777 static int mon_bin_release(struct inode *inode, struct file *file)
779 struct mon_reader_bin *rp = file->private_data;
780 struct mon_bus* mbus = rp->r.m_bus;
782 mutex_lock(&mon_lock);
784 if (mbus->nreaders <= 0) {
785 printk(KERN_ERR TAG ": consistency error on close\n");
786 mutex_unlock(&mon_lock);
789 mon_reader_del(mbus, &rp->r);
791 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
795 mutex_unlock(&mon_lock);
799 static ssize_t mon_bin_read(struct file *file, char __user *buf,
800 size_t nbytes, loff_t *ppos)
802 struct mon_reader_bin *rp = file->private_data;
803 unsigned int hdrbytes = PKT_SZ_API0;
805 struct mon_bin_hdr *ep;
812 mutex_lock(&rp->fetch_lock);
814 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
815 mutex_unlock(&rp->fetch_lock);
819 ep = MON_OFF2HDR(rp, rp->b_out);
821 if (rp->b_read < hdrbytes) {
822 step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
823 ptr = ((char *)ep) + rp->b_read;
824 if (step_len && copy_to_user(buf, ptr, step_len)) {
825 mutex_unlock(&rp->fetch_lock);
830 rp->b_read += step_len;
834 if (rp->b_read >= hdrbytes) {
835 step_len = ep->len_cap;
836 step_len -= rp->b_read - hdrbytes;
837 if (step_len > nbytes)
839 offset = rp->b_out + PKT_SIZE;
840 offset += rp->b_read - hdrbytes;
841 if (offset >= rp->b_size)
842 offset -= rp->b_size;
843 if (copy_from_buf(rp, offset, buf, step_len)) {
844 mutex_unlock(&rp->fetch_lock);
849 rp->b_read += step_len;
854 * Check if whole packet was read, and if so, jump to the next one.
856 if (rp->b_read >= hdrbytes + ep->len_cap) {
857 spin_lock_irqsave(&rp->b_lock, flags);
858 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
859 spin_unlock_irqrestore(&rp->b_lock, flags);
863 mutex_unlock(&rp->fetch_lock);
868 * Remove at most nevents from chunked buffer.
869 * Returns the number of removed events.
871 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
874 struct mon_bin_hdr *ep;
877 mutex_lock(&rp->fetch_lock);
878 spin_lock_irqsave(&rp->b_lock, flags);
879 for (i = 0; i < nevents; ++i) {
880 if (MON_RING_EMPTY(rp))
883 ep = MON_OFF2HDR(rp, rp->b_out);
884 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
886 spin_unlock_irqrestore(&rp->b_lock, flags);
888 mutex_unlock(&rp->fetch_lock);
893 * Fetch at most max event offsets into the buffer and put them into vec.
894 * The events are usually freed later with mon_bin_flush.
895 * Return the effective number of events fetched.
897 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
898 u32 __user *vec, unsigned int max)
900 unsigned int cur_out;
901 unsigned int bytes, avail;
903 unsigned int nevents;
904 struct mon_bin_hdr *ep;
908 mutex_lock(&rp->fetch_lock);
910 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
911 mutex_unlock(&rp->fetch_lock);
915 spin_lock_irqsave(&rp->b_lock, flags);
917 spin_unlock_irqrestore(&rp->b_lock, flags);
922 while (bytes < avail) {
926 ep = MON_OFF2HDR(rp, cur_out);
927 if (put_user(cur_out, &vec[nevents])) {
928 mutex_unlock(&rp->fetch_lock);
933 size = ep->len_cap + PKT_SIZE;
934 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
935 if ((cur_out += size) >= rp->b_size)
936 cur_out -= rp->b_size;
940 mutex_unlock(&rp->fetch_lock);
945 * Count events. This is almost the same as the above mon_bin_fetch,
946 * only we do not store offsets into user vector, and we have no limit.
948 static int mon_bin_queued(struct mon_reader_bin *rp)
950 unsigned int cur_out;
951 unsigned int bytes, avail;
953 unsigned int nevents;
954 struct mon_bin_hdr *ep;
957 mutex_lock(&rp->fetch_lock);
959 spin_lock_irqsave(&rp->b_lock, flags);
961 spin_unlock_irqrestore(&rp->b_lock, flags);
966 while (bytes < avail) {
967 ep = MON_OFF2HDR(rp, cur_out);
970 size = ep->len_cap + PKT_SIZE;
971 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
972 if ((cur_out += size) >= rp->b_size)
973 cur_out -= rp->b_size;
977 mutex_unlock(&rp->fetch_lock);
983 static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
985 struct mon_reader_bin *rp = file->private_data;
986 // struct mon_bus* mbus = rp->r.m_bus;
988 struct mon_bin_hdr *ep;
993 case MON_IOCQ_URB_LEN:
995 * N.B. This only returns the size of data, without the header.
997 spin_lock_irqsave(&rp->b_lock, flags);
998 if (!MON_RING_EMPTY(rp)) {
999 ep = MON_OFF2HDR(rp, rp->b_out);
1002 spin_unlock_irqrestore(&rp->b_lock, flags);
1005 case MON_IOCQ_RING_SIZE:
1009 case MON_IOCT_RING_SIZE:
1011 * Changing the buffer size will flush it's contents; the new
1012 * buffer is allocated before releasing the old one to be sure
1013 * the device will stay functional also in case of memory
1018 struct mon_pgmap *vec;
1020 if (arg < BUFF_MIN || arg > BUFF_MAX)
1023 size = CHUNK_ALIGN(arg);
1024 vec = kzalloc(sizeof(struct mon_pgmap) * (size / CHUNK_SIZE), GFP_KERNEL);
1030 ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
1036 mutex_lock(&rp->fetch_lock);
1037 spin_lock_irqsave(&rp->b_lock, flags);
1038 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
1042 rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
1044 spin_unlock_irqrestore(&rp->b_lock, flags);
1045 mutex_unlock(&rp->fetch_lock);
1049 case MON_IOCH_MFLUSH:
1050 ret = mon_bin_flush(rp, arg);
1056 struct mon_bin_get getb;
1058 if (copy_from_user(&getb, (void __user *)arg,
1059 sizeof(struct mon_bin_get)))
1062 if (getb.alloc > 0x10000000) /* Want to cast to u32 */
1064 ret = mon_bin_get_event(file, rp, getb.hdr,
1065 (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
1066 getb.data, (unsigned int)getb.alloc);
1070 case MON_IOCX_MFETCH:
1072 struct mon_bin_mfetch mfetch;
1073 struct mon_bin_mfetch __user *uptr;
1075 uptr = (struct mon_bin_mfetch __user *)arg;
1077 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1080 if (mfetch.nflush) {
1081 ret = mon_bin_flush(rp, mfetch.nflush);
1084 if (put_user(ret, &uptr->nflush))
1087 ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1090 if (put_user(ret, &uptr->nfetch))
1096 case MON_IOCG_STATS: {
1097 struct mon_bin_stats __user *sp;
1098 unsigned int nevents;
1099 unsigned int ndropped;
1101 spin_lock_irqsave(&rp->b_lock, flags);
1102 ndropped = rp->cnt_lost;
1104 spin_unlock_irqrestore(&rp->b_lock, flags);
1105 nevents = mon_bin_queued(rp);
1107 sp = (struct mon_bin_stats __user *)arg;
1108 if (put_user(ndropped, &sp->dropped))
1110 if (put_user(nevents, &sp->queued))
1123 #ifdef CONFIG_COMPAT
1124 static long mon_bin_compat_ioctl(struct file *file,
1125 unsigned int cmd, unsigned long arg)
1127 struct mon_reader_bin *rp = file->private_data;
1132 case MON_IOCX_GET32:
1133 case MON_IOCX_GETX32:
1135 struct mon_bin_get32 getb;
1137 if (copy_from_user(&getb, (void __user *)arg,
1138 sizeof(struct mon_bin_get32)))
1141 ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1142 (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1143 compat_ptr(getb.data32), getb.alloc32);
1149 case MON_IOCX_MFETCH32:
1151 struct mon_bin_mfetch32 mfetch;
1152 struct mon_bin_mfetch32 __user *uptr;
1154 uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1156 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1159 if (mfetch.nflush32) {
1160 ret = mon_bin_flush(rp, mfetch.nflush32);
1163 if (put_user(ret, &uptr->nflush32))
1166 ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1170 if (put_user(ret, &uptr->nfetch32))
1175 case MON_IOCG_STATS:
1176 return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1178 case MON_IOCQ_URB_LEN:
1179 case MON_IOCQ_RING_SIZE:
1180 case MON_IOCT_RING_SIZE:
1181 case MON_IOCH_MFLUSH:
1182 return mon_bin_ioctl(file, cmd, arg);
1189 #endif /* CONFIG_COMPAT */
1192 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1194 struct mon_reader_bin *rp = file->private_data;
1195 unsigned int mask = 0;
1196 unsigned long flags;
1198 if (file->f_mode & FMODE_READ)
1199 poll_wait(file, &rp->b_wait, wait);
1201 spin_lock_irqsave(&rp->b_lock, flags);
1202 if (!MON_RING_EMPTY(rp))
1203 mask |= POLLIN | POLLRDNORM; /* readable */
1204 spin_unlock_irqrestore(&rp->b_lock, flags);
1209 * open and close: just keep track of how many times the device is
1210 * mapped, to use the proper memory allocation function.
1212 static void mon_bin_vma_open(struct vm_area_struct *vma)
1214 struct mon_reader_bin *rp = vma->vm_private_data;
1218 static void mon_bin_vma_close(struct vm_area_struct *vma)
1220 struct mon_reader_bin *rp = vma->vm_private_data;
1225 * Map ring pages to user space.
1227 static int mon_bin_vma_fault(struct vm_fault *vmf)
1229 struct mon_reader_bin *rp = vmf->vma->vm_private_data;
1230 unsigned long offset, chunk_idx;
1231 struct page *pageptr;
1233 offset = vmf->pgoff << PAGE_SHIFT;
1234 if (offset >= rp->b_size)
1235 return VM_FAULT_SIGBUS;
1236 chunk_idx = offset / CHUNK_SIZE;
1237 pageptr = rp->b_vec[chunk_idx].pg;
1239 vmf->page = pageptr;
1243 static const struct vm_operations_struct mon_bin_vm_ops = {
1244 .open = mon_bin_vma_open,
1245 .close = mon_bin_vma_close,
1246 .fault = mon_bin_vma_fault,
1249 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1251 /* don't do anything here: "fault" will set up page table entries */
1252 vma->vm_ops = &mon_bin_vm_ops;
1253 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1254 vma->vm_private_data = filp->private_data;
1255 mon_bin_vma_open(vma);
1259 static const struct file_operations mon_fops_binary = {
1260 .owner = THIS_MODULE,
1261 .open = mon_bin_open,
1262 .llseek = no_llseek,
1263 .read = mon_bin_read,
1264 /* .write = mon_text_write, */
1265 .poll = mon_bin_poll,
1266 .unlocked_ioctl = mon_bin_ioctl,
1267 #ifdef CONFIG_COMPAT
1268 .compat_ioctl = mon_bin_compat_ioctl,
1270 .release = mon_bin_release,
1271 .mmap = mon_bin_mmap,
1274 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1276 DECLARE_WAITQUEUE(waita, current);
1277 unsigned long flags;
1279 add_wait_queue(&rp->b_wait, &waita);
1280 set_current_state(TASK_INTERRUPTIBLE);
1282 spin_lock_irqsave(&rp->b_lock, flags);
1283 while (MON_RING_EMPTY(rp)) {
1284 spin_unlock_irqrestore(&rp->b_lock, flags);
1286 if (file->f_flags & O_NONBLOCK) {
1287 set_current_state(TASK_RUNNING);
1288 remove_wait_queue(&rp->b_wait, &waita);
1289 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1292 if (signal_pending(current)) {
1293 remove_wait_queue(&rp->b_wait, &waita);
1296 set_current_state(TASK_INTERRUPTIBLE);
1298 spin_lock_irqsave(&rp->b_lock, flags);
1300 spin_unlock_irqrestore(&rp->b_lock, flags);
1302 set_current_state(TASK_RUNNING);
1303 remove_wait_queue(&rp->b_wait, &waita);
1307 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1310 unsigned long vaddr;
1312 for (n = 0; n < npages; n++) {
1313 vaddr = get_zeroed_page(GFP_KERNEL);
1316 free_page((unsigned long) map[n].ptr);
1319 map[n].ptr = (unsigned char *) vaddr;
1320 map[n].pg = virt_to_page((void *) vaddr);
1325 static void mon_free_buff(struct mon_pgmap *map, int npages)
1329 for (n = 0; n < npages; n++)
1330 free_page((unsigned long) map[n].ptr);
1333 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1336 unsigned minor = ubus? ubus->busnum: 0;
1338 if (minor >= MON_BIN_MAX_MINOR)
1341 dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1342 MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1347 mbus->classdev = dev;
1351 void mon_bin_del(struct mon_bus *mbus)
1353 device_destroy(mon_bin_class, mbus->classdev->devt);
1356 int __init mon_bin_init(void)
1360 mon_bin_class = class_create(THIS_MODULE, "usbmon");
1361 if (IS_ERR(mon_bin_class)) {
1362 rc = PTR_ERR(mon_bin_class);
1366 rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1370 cdev_init(&mon_bin_cdev, &mon_fops_binary);
1371 mon_bin_cdev.owner = THIS_MODULE;
1373 rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1380 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1382 class_destroy(mon_bin_class);
1387 void mon_bin_exit(void)
1389 cdev_del(&mon_bin_cdev);
1390 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1391 class_destroy(mon_bin_class);