Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The USB Monitor, inspired by Dave Harding's USBMon.
0004  *
0005  * This is a binary format reader.
0006  *
0007  * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
0008  * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/sched/signal.h>
0013 #include <linux/types.h>
0014 #include <linux/fs.h>
0015 #include <linux/cdev.h>
0016 #include <linux/export.h>
0017 #include <linux/usb.h>
0018 #include <linux/poll.h>
0019 #include <linux/compat.h>
0020 #include <linux/mm.h>
0021 #include <linux/scatterlist.h>
0022 #include <linux/slab.h>
0023 #include <linux/time64.h>
0024 
0025 #include <linux/uaccess.h>
0026 
0027 #include "usb_mon.h"
0028 
0029 /*
0030  * Defined by USB 2.0 clause 9.3, table 9.2.
0031  */
0032 #define SETUP_LEN  8
0033 
0034 /* ioctl macros */
0035 #define MON_IOC_MAGIC 0x92
0036 
0037 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
0038 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
0039 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
0040 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
0041 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
0042 #define MON_IOCX_GET   _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
0043 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
0044 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
0045 /* #9 was MON_IOCT_SETAPI */
0046 #define MON_IOCX_GETX   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
0047 
0048 #ifdef CONFIG_COMPAT
0049 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
0050 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
0051 #define MON_IOCX_GETX32   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
0052 #endif
0053 
0054 /*
0055  * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
0056  * But it's all right. Just use a simple way to make sure the chunk is never
0057  * smaller than a page.
0058  *
0059  * N.B. An application does not know our chunk size.
0060  *
0061  * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
0062  * page-sized chunks for the time being.
0063  */
0064 #define CHUNK_SIZE   PAGE_SIZE
0065 #define CHUNK_ALIGN(x)   (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
0066 
0067 /*
0068  * The magic limit was calculated so that it allows the monitoring
0069  * application to pick data once in two ticks. This way, another application,
0070  * which presumably drives the bus, gets to hog CPU, yet we collect our data.
0071  * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
0072  * enormous overhead built into the bus protocol, so we need about 1000 KB.
0073  *
0074  * This is still too much for most cases, where we just snoop a few
0075  * descriptor fetches for enumeration. So, the default is a "reasonable"
0076  * amount for systems with HZ=250 and incomplete bus saturation.
0077  *
0078  * XXX What about multi-megabyte URBs which take minutes to transfer?
0079  */
0080 #define BUFF_MAX  CHUNK_ALIGN(1200*1024)
0081 #define BUFF_DFL   CHUNK_ALIGN(300*1024)
0082 #define BUFF_MIN     CHUNK_ALIGN(8*1024)
0083 
0084 /*
0085  * The per-event API header (2 per URB).
0086  *
0087  * This structure is seen in userland as defined by the documentation.
0088  */
0089 struct mon_bin_hdr {
0090     u64 id;         /* URB ID - from submission to callback */
0091     unsigned char type; /* Same as in text API; extensible. */
0092     unsigned char xfer_type;    /* ISO, Intr, Control, Bulk */
0093     unsigned char epnum;    /* Endpoint number and transfer direction */
0094     unsigned char devnum;   /* Device address */
0095     unsigned short busnum;  /* Bus number */
0096     char flag_setup;
0097     char flag_data;
0098     s64 ts_sec;     /* ktime_get_real_ts64 */
0099     s32 ts_usec;        /* ktime_get_real_ts64 */
0100     int status;
0101     unsigned int len_urb;   /* Length of data (submitted or actual) */
0102     unsigned int len_cap;   /* Delivered length */
0103     union {
0104         unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
0105         struct iso_rec {
0106             int error_count;
0107             int numdesc;
0108         } iso;
0109     } s;
0110     int interval;
0111     int start_frame;
0112     unsigned int xfer_flags;
0113     unsigned int ndesc; /* Actual number of ISO descriptors */
0114 };
0115 
0116 /*
0117  * ISO vector, packed into the head of data stream.
0118  * This has to take 16 bytes to make sure that the end of buffer
0119  * wrap is not happening in the middle of a descriptor.
0120  */
0121 struct mon_bin_isodesc {
0122     int          iso_status;
0123     unsigned int iso_off;
0124     unsigned int iso_len;
0125     u32 _pad;
0126 };
0127 
0128 /* per file statistic */
0129 struct mon_bin_stats {
0130     u32 queued;
0131     u32 dropped;
0132 };
0133 
0134 struct mon_bin_get {
0135     struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
0136     void __user *data;
0137     size_t alloc;       /* Length of data (can be zero) */
0138 };
0139 
0140 struct mon_bin_mfetch {
0141     u32 __user *offvec; /* Vector of events fetched */
0142     u32 nfetch;     /* Number of events to fetch (out: fetched) */
0143     u32 nflush;     /* Number of events to flush */
0144 };
0145 
0146 #ifdef CONFIG_COMPAT
0147 struct mon_bin_get32 {
0148     u32 hdr32;
0149     u32 data32;
0150     u32 alloc32;
0151 };
0152 
0153 struct mon_bin_mfetch32 {
0154         u32 offvec32;
0155         u32 nfetch32;
0156         u32 nflush32;
0157 };
0158 #endif
0159 
0160 /* Having these two values same prevents wrapping of the mon_bin_hdr */
0161 #define PKT_ALIGN   64
0162 #define PKT_SIZE    64
0163 
0164 #define PKT_SZ_API0 48  /* API 0 (2.6.20) size */
0165 #define PKT_SZ_API1 64  /* API 1 size: extra fields */
0166 
0167 #define ISODESC_MAX   128   /* Same number as usbfs allows, 2048 bytes. */
0168 
0169 /* max number of USB bus supported */
0170 #define MON_BIN_MAX_MINOR 128
0171 
0172 /*
0173  * The buffer: map of used pages.
0174  */
0175 struct mon_pgmap {
0176     struct page *pg;
0177     unsigned char *ptr; /* XXX just use page_to_virt everywhere? */
0178 };
0179 
0180 /*
0181  * This gets associated with an open file struct.
0182  */
0183 struct mon_reader_bin {
0184     /* The buffer: one per open. */
0185     spinlock_t b_lock;      /* Protect b_cnt, b_in */
0186     unsigned int b_size;        /* Current size of the buffer - bytes */
0187     unsigned int b_cnt;     /* Bytes used */
0188     unsigned int b_in, b_out;   /* Offsets into buffer - bytes */
0189     unsigned int b_read;        /* Amount of read data in curr. pkt. */
0190     struct mon_pgmap *b_vec;    /* The map array */
0191     wait_queue_head_t b_wait;   /* Wait for data here */
0192 
0193     struct mutex fetch_lock;    /* Protect b_read, b_out */
0194     int mmap_active;
0195 
0196     /* A list of these is needed for "bus 0". Some time later. */
0197     struct mon_reader r;
0198 
0199     /* Stats */
0200     unsigned int cnt_lost;
0201 };
0202 
0203 static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
0204     unsigned int offset)
0205 {
0206     return (struct mon_bin_hdr *)
0207         (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
0208 }
0209 
0210 #define MON_RING_EMPTY(rp)  ((rp)->b_cnt == 0)
0211 
0212 static unsigned char xfer_to_pipe[4] = {
0213     PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
0214 };
0215 
0216 static struct class *mon_bin_class;
0217 static dev_t mon_bin_dev0;
0218 static struct cdev mon_bin_cdev;
0219 
0220 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
0221     unsigned int offset, unsigned int size);
0222 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
0223 static int mon_alloc_buff(struct mon_pgmap *map, int npages);
0224 static void mon_free_buff(struct mon_pgmap *map, int npages);
0225 
0226 /*
0227  * This is a "chunked memcpy". It does not manipulate any counters.
0228  */
0229 static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
0230     unsigned int off, const unsigned char *from, unsigned int length)
0231 {
0232     unsigned int step_len;
0233     unsigned char *buf;
0234     unsigned int in_page;
0235 
0236     while (length) {
0237         /*
0238          * Determine step_len.
0239          */
0240         step_len = length;
0241         in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
0242         if (in_page < step_len)
0243             step_len = in_page;
0244 
0245         /*
0246          * Copy data and advance pointers.
0247          */
0248         buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
0249         memcpy(buf, from, step_len);
0250         if ((off += step_len) >= this->b_size) off = 0;
0251         from += step_len;
0252         length -= step_len;
0253     }
0254     return off;
0255 }
0256 
0257 /*
0258  * This is a little worse than the above because it's "chunked copy_to_user".
0259  * The return value is an error code, not an offset.
0260  */
0261 static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
0262     char __user *to, int length)
0263 {
0264     unsigned int step_len;
0265     unsigned char *buf;
0266     unsigned int in_page;
0267 
0268     while (length) {
0269         /*
0270          * Determine step_len.
0271          */
0272         step_len = length;
0273         in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
0274         if (in_page < step_len)
0275             step_len = in_page;
0276 
0277         /*
0278          * Copy data and advance pointers.
0279          */
0280         buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
0281         if (copy_to_user(to, buf, step_len))
0282             return -EINVAL;
0283         if ((off += step_len) >= this->b_size) off = 0;
0284         to += step_len;
0285         length -= step_len;
0286     }
0287     return 0;
0288 }
0289 
0290 /*
0291  * Allocate an (aligned) area in the buffer.
0292  * This is called under b_lock.
0293  * Returns ~0 on failure.
0294  */
0295 static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
0296     unsigned int size)
0297 {
0298     unsigned int offset;
0299 
0300     size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0301     if (rp->b_cnt + size > rp->b_size)
0302         return ~0;
0303     offset = rp->b_in;
0304     rp->b_cnt += size;
0305     if ((rp->b_in += size) >= rp->b_size)
0306         rp->b_in -= rp->b_size;
0307     return offset;
0308 }
0309 
0310 /*
0311  * This is the same thing as mon_buff_area_alloc, only it does not allow
0312  * buffers to wrap. This is needed by applications which pass references
0313  * into mmap-ed buffers up their stacks (libpcap can do that).
0314  *
0315  * Currently, we always have the header stuck with the data, although
0316  * it is not strictly speaking necessary.
0317  *
0318  * When a buffer would wrap, we place a filler packet to mark the space.
0319  */
0320 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
0321     unsigned int size)
0322 {
0323     unsigned int offset;
0324     unsigned int fill_size;
0325 
0326     size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0327     if (rp->b_cnt + size > rp->b_size)
0328         return ~0;
0329     if (rp->b_in + size > rp->b_size) {
0330         /*
0331          * This would wrap. Find if we still have space after
0332          * skipping to the end of the buffer. If we do, place
0333          * a filler packet and allocate a new packet.
0334          */
0335         fill_size = rp->b_size - rp->b_in;
0336         if (rp->b_cnt + size + fill_size > rp->b_size)
0337             return ~0;
0338         mon_buff_area_fill(rp, rp->b_in, fill_size);
0339 
0340         offset = 0;
0341         rp->b_in = size;
0342         rp->b_cnt += size + fill_size;
0343     } else if (rp->b_in + size == rp->b_size) {
0344         offset = rp->b_in;
0345         rp->b_in = 0;
0346         rp->b_cnt += size;
0347     } else {
0348         offset = rp->b_in;
0349         rp->b_in += size;
0350         rp->b_cnt += size;
0351     }
0352     return offset;
0353 }
0354 
0355 /*
0356  * Return a few (kilo-)bytes to the head of the buffer.
0357  * This is used if a data fetch fails.
0358  */
0359 static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
0360 {
0361 
0362     /* size &= ~(PKT_ALIGN-1);  -- we're called with aligned size */
0363     rp->b_cnt -= size;
0364     if (rp->b_in < size)
0365         rp->b_in += rp->b_size;
0366     rp->b_in -= size;
0367 }
0368 
0369 /*
0370  * This has to be called under both b_lock and fetch_lock, because
0371  * it accesses both b_cnt and b_out.
0372  */
0373 static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
0374 {
0375 
0376     size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0377     rp->b_cnt -= size;
0378     if ((rp->b_out += size) >= rp->b_size)
0379         rp->b_out -= rp->b_size;
0380 }
0381 
0382 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
0383     unsigned int offset, unsigned int size)
0384 {
0385     struct mon_bin_hdr *ep;
0386 
0387     ep = MON_OFF2HDR(rp, offset);
0388     memset(ep, 0, PKT_SIZE);
0389     ep->type = '@';
0390     ep->len_cap = size - PKT_SIZE;
0391 }
0392 
0393 static inline char mon_bin_get_setup(unsigned char *setupb,
0394     const struct urb *urb, char ev_type)
0395 {
0396 
0397     if (urb->setup_packet == NULL)
0398         return 'Z';
0399     memcpy(setupb, urb->setup_packet, SETUP_LEN);
0400     return 0;
0401 }
0402 
0403 static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
0404     unsigned int offset, struct urb *urb, unsigned int length,
0405     char *flag)
0406 {
0407     int i;
0408     struct scatterlist *sg;
0409     unsigned int this_len;
0410 
0411     *flag = 0;
0412     if (urb->num_sgs == 0) {
0413         if (urb->transfer_buffer == NULL) {
0414             *flag = 'Z';
0415             return length;
0416         }
0417         mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
0418         length = 0;
0419 
0420     } else {
0421         /* If IOMMU coalescing occurred, we cannot trust sg_page */
0422         if (urb->transfer_flags & URB_DMA_SG_COMBINED) {
0423             *flag = 'D';
0424             return length;
0425         }
0426 
0427         /* Copy up to the first non-addressable segment */
0428         for_each_sg(urb->sg, sg, urb->num_sgs, i) {
0429             if (length == 0 || PageHighMem(sg_page(sg)))
0430                 break;
0431             this_len = min_t(unsigned int, sg->length, length);
0432             offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
0433                     this_len);
0434             length -= this_len;
0435         }
0436         if (i == 0)
0437             *flag = 'D';
0438     }
0439 
0440     return length;
0441 }
0442 
0443 /*
0444  * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
0445  * be used to determine the length of the whole contiguous buffer.
0446  */
0447 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin *rp,
0448     struct urb *urb, unsigned int ndesc)
0449 {
0450     struct usb_iso_packet_descriptor *fp;
0451     unsigned int length;
0452 
0453     length = 0;
0454     fp = urb->iso_frame_desc;
0455     while (ndesc-- != 0) {
0456         if (fp->actual_length != 0) {
0457             if (fp->offset + fp->actual_length > length)
0458                 length = fp->offset + fp->actual_length;
0459         }
0460         fp++;
0461     }
0462     return length;
0463 }
0464 
0465 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
0466     unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
0467 {
0468     struct mon_bin_isodesc *dp;
0469     struct usb_iso_packet_descriptor *fp;
0470 
0471     fp = urb->iso_frame_desc;
0472     while (ndesc-- != 0) {
0473         dp = (struct mon_bin_isodesc *)
0474             (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
0475         dp->iso_status = fp->status;
0476         dp->iso_off = fp->offset;
0477         dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
0478         dp->_pad = 0;
0479         if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
0480             offset = 0;
0481         fp++;
0482     }
0483 }
0484 
0485 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
0486     char ev_type, int status)
0487 {
0488     const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
0489     struct timespec64 ts;
0490     unsigned long flags;
0491     unsigned int urb_length;
0492     unsigned int offset;
0493     unsigned int length;
0494     unsigned int delta;
0495     unsigned int ndesc, lendesc;
0496     unsigned char dir;
0497     struct mon_bin_hdr *ep;
0498     char data_tag = 0;
0499 
0500     ktime_get_real_ts64(&ts);
0501 
0502     spin_lock_irqsave(&rp->b_lock, flags);
0503 
0504     /*
0505      * Find the maximum allowable length, then allocate space.
0506      */
0507     urb_length = (ev_type == 'S') ?
0508         urb->transfer_buffer_length : urb->actual_length;
0509     length = urb_length;
0510 
0511     if (usb_endpoint_xfer_isoc(epd)) {
0512         if (urb->number_of_packets < 0) {
0513             ndesc = 0;
0514         } else if (urb->number_of_packets >= ISODESC_MAX) {
0515             ndesc = ISODESC_MAX;
0516         } else {
0517             ndesc = urb->number_of_packets;
0518         }
0519         if (ev_type == 'C' && usb_urb_dir_in(urb))
0520             length = mon_bin_collate_isodesc(rp, urb, ndesc);
0521     } else {
0522         ndesc = 0;
0523     }
0524     lendesc = ndesc*sizeof(struct mon_bin_isodesc);
0525 
0526     /* not an issue unless there's a subtle bug in a HCD somewhere */
0527     if (length >= urb->transfer_buffer_length)
0528         length = urb->transfer_buffer_length;
0529 
0530     if (length >= rp->b_size/5)
0531         length = rp->b_size/5;
0532 
0533     if (usb_urb_dir_in(urb)) {
0534         if (ev_type == 'S') {
0535             length = 0;
0536             data_tag = '<';
0537         }
0538         /* Cannot rely on endpoint number in case of control ep.0 */
0539         dir = USB_DIR_IN;
0540     } else {
0541         if (ev_type == 'C') {
0542             length = 0;
0543             data_tag = '>';
0544         }
0545         dir = 0;
0546     }
0547 
0548     if (rp->mmap_active) {
0549         offset = mon_buff_area_alloc_contiguous(rp,
0550                          length + PKT_SIZE + lendesc);
0551     } else {
0552         offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
0553     }
0554     if (offset == ~0) {
0555         rp->cnt_lost++;
0556         spin_unlock_irqrestore(&rp->b_lock, flags);
0557         return;
0558     }
0559 
0560     ep = MON_OFF2HDR(rp, offset);
0561     if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
0562 
0563     /*
0564      * Fill the allocated area.
0565      */
0566     memset(ep, 0, PKT_SIZE);
0567     ep->type = ev_type;
0568     ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
0569     ep->epnum = dir | usb_endpoint_num(epd);
0570     ep->devnum = urb->dev->devnum;
0571     ep->busnum = urb->dev->bus->busnum;
0572     ep->id = (unsigned long) urb;
0573     ep->ts_sec = ts.tv_sec;
0574     ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
0575     ep->status = status;
0576     ep->len_urb = urb_length;
0577     ep->len_cap = length + lendesc;
0578     ep->xfer_flags = urb->transfer_flags;
0579 
0580     if (usb_endpoint_xfer_int(epd)) {
0581         ep->interval = urb->interval;
0582     } else if (usb_endpoint_xfer_isoc(epd)) {
0583         ep->interval = urb->interval;
0584         ep->start_frame = urb->start_frame;
0585         ep->s.iso.error_count = urb->error_count;
0586         ep->s.iso.numdesc = urb->number_of_packets;
0587     }
0588 
0589     if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
0590         ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
0591     } else {
0592         ep->flag_setup = '-';
0593     }
0594 
0595     if (ndesc != 0) {
0596         ep->ndesc = ndesc;
0597         mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
0598         if ((offset += lendesc) >= rp->b_size)
0599             offset -= rp->b_size;
0600     }
0601 
0602     if (length != 0) {
0603         length = mon_bin_get_data(rp, offset, urb, length,
0604                 &ep->flag_data);
0605         if (length > 0) {
0606             delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0607             ep->len_cap -= length;
0608             delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0609             mon_buff_area_shrink(rp, delta);
0610         }
0611     } else {
0612         ep->flag_data = data_tag;
0613     }
0614 
0615     spin_unlock_irqrestore(&rp->b_lock, flags);
0616 
0617     wake_up(&rp->b_wait);
0618 }
0619 
0620 static void mon_bin_submit(void *data, struct urb *urb)
0621 {
0622     struct mon_reader_bin *rp = data;
0623     mon_bin_event(rp, urb, 'S', -EINPROGRESS);
0624 }
0625 
0626 static void mon_bin_complete(void *data, struct urb *urb, int status)
0627 {
0628     struct mon_reader_bin *rp = data;
0629     mon_bin_event(rp, urb, 'C', status);
0630 }
0631 
0632 static void mon_bin_error(void *data, struct urb *urb, int error)
0633 {
0634     struct mon_reader_bin *rp = data;
0635     struct timespec64 ts;
0636     unsigned long flags;
0637     unsigned int offset;
0638     struct mon_bin_hdr *ep;
0639 
0640     ktime_get_real_ts64(&ts);
0641 
0642     spin_lock_irqsave(&rp->b_lock, flags);
0643 
0644     offset = mon_buff_area_alloc(rp, PKT_SIZE);
0645     if (offset == ~0) {
0646         /* Not incrementing cnt_lost. Just because. */
0647         spin_unlock_irqrestore(&rp->b_lock, flags);
0648         return;
0649     }
0650 
0651     ep = MON_OFF2HDR(rp, offset);
0652 
0653     memset(ep, 0, PKT_SIZE);
0654     ep->type = 'E';
0655     ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
0656     ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
0657     ep->epnum |= usb_endpoint_num(&urb->ep->desc);
0658     ep->devnum = urb->dev->devnum;
0659     ep->busnum = urb->dev->bus->busnum;
0660     ep->id = (unsigned long) urb;
0661     ep->ts_sec = ts.tv_sec;
0662     ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
0663     ep->status = error;
0664 
0665     ep->flag_setup = '-';
0666     ep->flag_data = 'E';
0667 
0668     spin_unlock_irqrestore(&rp->b_lock, flags);
0669 
0670     wake_up(&rp->b_wait);
0671 }
0672 
0673 static int mon_bin_open(struct inode *inode, struct file *file)
0674 {
0675     struct mon_bus *mbus;
0676     struct mon_reader_bin *rp;
0677     size_t size;
0678     int rc;
0679 
0680     mutex_lock(&mon_lock);
0681     mbus = mon_bus_lookup(iminor(inode));
0682     if (mbus == NULL) {
0683         mutex_unlock(&mon_lock);
0684         return -ENODEV;
0685     }
0686     if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
0687         printk(KERN_ERR TAG ": consistency error on open\n");
0688         mutex_unlock(&mon_lock);
0689         return -ENODEV;
0690     }
0691 
0692     rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
0693     if (rp == NULL) {
0694         rc = -ENOMEM;
0695         goto err_alloc;
0696     }
0697     spin_lock_init(&rp->b_lock);
0698     init_waitqueue_head(&rp->b_wait);
0699     mutex_init(&rp->fetch_lock);
0700     rp->b_size = BUFF_DFL;
0701 
0702     size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
0703     if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
0704         rc = -ENOMEM;
0705         goto err_allocvec;
0706     }
0707 
0708     if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
0709         goto err_allocbuff;
0710 
0711     rp->r.m_bus = mbus;
0712     rp->r.r_data = rp;
0713     rp->r.rnf_submit = mon_bin_submit;
0714     rp->r.rnf_error = mon_bin_error;
0715     rp->r.rnf_complete = mon_bin_complete;
0716 
0717     mon_reader_add(mbus, &rp->r);
0718 
0719     file->private_data = rp;
0720     mutex_unlock(&mon_lock);
0721     return 0;
0722 
0723 err_allocbuff:
0724     kfree(rp->b_vec);
0725 err_allocvec:
0726     kfree(rp);
0727 err_alloc:
0728     mutex_unlock(&mon_lock);
0729     return rc;
0730 }
0731 
0732 /*
0733  * Extract an event from buffer and copy it to user space.
0734  * Wait if there is no event ready.
0735  * Returns zero or error.
0736  */
0737 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
0738     struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
0739     void __user *data, unsigned int nbytes)
0740 {
0741     unsigned long flags;
0742     struct mon_bin_hdr *ep;
0743     size_t step_len;
0744     unsigned int offset;
0745     int rc;
0746 
0747     mutex_lock(&rp->fetch_lock);
0748 
0749     if ((rc = mon_bin_wait_event(file, rp)) < 0) {
0750         mutex_unlock(&rp->fetch_lock);
0751         return rc;
0752     }
0753 
0754     ep = MON_OFF2HDR(rp, rp->b_out);
0755 
0756     if (copy_to_user(hdr, ep, hdrbytes)) {
0757         mutex_unlock(&rp->fetch_lock);
0758         return -EFAULT;
0759     }
0760 
0761     step_len = min(ep->len_cap, nbytes);
0762     if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
0763 
0764     if (copy_from_buf(rp, offset, data, step_len)) {
0765         mutex_unlock(&rp->fetch_lock);
0766         return -EFAULT;
0767     }
0768 
0769     spin_lock_irqsave(&rp->b_lock, flags);
0770     mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
0771     spin_unlock_irqrestore(&rp->b_lock, flags);
0772     rp->b_read = 0;
0773 
0774     mutex_unlock(&rp->fetch_lock);
0775     return 0;
0776 }
0777 
0778 static int mon_bin_release(struct inode *inode, struct file *file)
0779 {
0780     struct mon_reader_bin *rp = file->private_data;
0781     struct mon_bus* mbus = rp->r.m_bus;
0782 
0783     mutex_lock(&mon_lock);
0784 
0785     if (mbus->nreaders <= 0) {
0786         printk(KERN_ERR TAG ": consistency error on close\n");
0787         mutex_unlock(&mon_lock);
0788         return 0;
0789     }
0790     mon_reader_del(mbus, &rp->r);
0791 
0792     mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
0793     kfree(rp->b_vec);
0794     kfree(rp);
0795 
0796     mutex_unlock(&mon_lock);
0797     return 0;
0798 }
0799 
0800 static ssize_t mon_bin_read(struct file *file, char __user *buf,
0801     size_t nbytes, loff_t *ppos)
0802 {
0803     struct mon_reader_bin *rp = file->private_data;
0804     unsigned int hdrbytes = PKT_SZ_API0;
0805     unsigned long flags;
0806     struct mon_bin_hdr *ep;
0807     unsigned int offset;
0808     size_t step_len;
0809     char *ptr;
0810     ssize_t done = 0;
0811     int rc;
0812 
0813     mutex_lock(&rp->fetch_lock);
0814 
0815     if ((rc = mon_bin_wait_event(file, rp)) < 0) {
0816         mutex_unlock(&rp->fetch_lock);
0817         return rc;
0818     }
0819 
0820     ep = MON_OFF2HDR(rp, rp->b_out);
0821 
0822     if (rp->b_read < hdrbytes) {
0823         step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
0824         ptr = ((char *)ep) + rp->b_read;
0825         if (step_len && copy_to_user(buf, ptr, step_len)) {
0826             mutex_unlock(&rp->fetch_lock);
0827             return -EFAULT;
0828         }
0829         nbytes -= step_len;
0830         buf += step_len;
0831         rp->b_read += step_len;
0832         done += step_len;
0833     }
0834 
0835     if (rp->b_read >= hdrbytes) {
0836         step_len = ep->len_cap;
0837         step_len -= rp->b_read - hdrbytes;
0838         if (step_len > nbytes)
0839             step_len = nbytes;
0840         offset = rp->b_out + PKT_SIZE;
0841         offset += rp->b_read - hdrbytes;
0842         if (offset >= rp->b_size)
0843             offset -= rp->b_size;
0844         if (copy_from_buf(rp, offset, buf, step_len)) {
0845             mutex_unlock(&rp->fetch_lock);
0846             return -EFAULT;
0847         }
0848         nbytes -= step_len;
0849         buf += step_len;
0850         rp->b_read += step_len;
0851         done += step_len;
0852     }
0853 
0854     /*
0855      * Check if whole packet was read, and if so, jump to the next one.
0856      */
0857     if (rp->b_read >= hdrbytes + ep->len_cap) {
0858         spin_lock_irqsave(&rp->b_lock, flags);
0859         mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
0860         spin_unlock_irqrestore(&rp->b_lock, flags);
0861         rp->b_read = 0;
0862     }
0863 
0864     mutex_unlock(&rp->fetch_lock);
0865     return done;
0866 }
0867 
0868 /*
0869  * Remove at most nevents from chunked buffer.
0870  * Returns the number of removed events.
0871  */
0872 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
0873 {
0874     unsigned long flags;
0875     struct mon_bin_hdr *ep;
0876     int i;
0877 
0878     mutex_lock(&rp->fetch_lock);
0879     spin_lock_irqsave(&rp->b_lock, flags);
0880     for (i = 0; i < nevents; ++i) {
0881         if (MON_RING_EMPTY(rp))
0882             break;
0883 
0884         ep = MON_OFF2HDR(rp, rp->b_out);
0885         mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
0886     }
0887     spin_unlock_irqrestore(&rp->b_lock, flags);
0888     rp->b_read = 0;
0889     mutex_unlock(&rp->fetch_lock);
0890     return i;
0891 }
0892 
0893 /*
0894  * Fetch at most max event offsets into the buffer and put them into vec.
0895  * The events are usually freed later with mon_bin_flush.
0896  * Return the effective number of events fetched.
0897  */
0898 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
0899     u32 __user *vec, unsigned int max)
0900 {
0901     unsigned int cur_out;
0902     unsigned int bytes, avail;
0903     unsigned int size;
0904     unsigned int nevents;
0905     struct mon_bin_hdr *ep;
0906     unsigned long flags;
0907     int rc;
0908 
0909     mutex_lock(&rp->fetch_lock);
0910 
0911     if ((rc = mon_bin_wait_event(file, rp)) < 0) {
0912         mutex_unlock(&rp->fetch_lock);
0913         return rc;
0914     }
0915 
0916     spin_lock_irqsave(&rp->b_lock, flags);
0917     avail = rp->b_cnt;
0918     spin_unlock_irqrestore(&rp->b_lock, flags);
0919 
0920     cur_out = rp->b_out;
0921     nevents = 0;
0922     bytes = 0;
0923     while (bytes < avail) {
0924         if (nevents >= max)
0925             break;
0926 
0927         ep = MON_OFF2HDR(rp, cur_out);
0928         if (put_user(cur_out, &vec[nevents])) {
0929             mutex_unlock(&rp->fetch_lock);
0930             return -EFAULT;
0931         }
0932 
0933         nevents++;
0934         size = ep->len_cap + PKT_SIZE;
0935         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0936         if ((cur_out += size) >= rp->b_size)
0937             cur_out -= rp->b_size;
0938         bytes += size;
0939     }
0940 
0941     mutex_unlock(&rp->fetch_lock);
0942     return nevents;
0943 }
0944 
0945 /*
0946  * Count events. This is almost the same as the above mon_bin_fetch,
0947  * only we do not store offsets into user vector, and we have no limit.
0948  */
0949 static int mon_bin_queued(struct mon_reader_bin *rp)
0950 {
0951     unsigned int cur_out;
0952     unsigned int bytes, avail;
0953     unsigned int size;
0954     unsigned int nevents;
0955     struct mon_bin_hdr *ep;
0956     unsigned long flags;
0957 
0958     mutex_lock(&rp->fetch_lock);
0959 
0960     spin_lock_irqsave(&rp->b_lock, flags);
0961     avail = rp->b_cnt;
0962     spin_unlock_irqrestore(&rp->b_lock, flags);
0963 
0964     cur_out = rp->b_out;
0965     nevents = 0;
0966     bytes = 0;
0967     while (bytes < avail) {
0968         ep = MON_OFF2HDR(rp, cur_out);
0969 
0970         nevents++;
0971         size = ep->len_cap + PKT_SIZE;
0972         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
0973         if ((cur_out += size) >= rp->b_size)
0974             cur_out -= rp->b_size;
0975         bytes += size;
0976     }
0977 
0978     mutex_unlock(&rp->fetch_lock);
0979     return nevents;
0980 }
0981 
0982 /*
0983  */
0984 static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0985 {
0986     struct mon_reader_bin *rp = file->private_data;
0987     // struct mon_bus* mbus = rp->r.m_bus;
0988     int ret = 0;
0989     struct mon_bin_hdr *ep;
0990     unsigned long flags;
0991 
0992     switch (cmd) {
0993 
0994     case MON_IOCQ_URB_LEN:
0995         /*
0996          * N.B. This only returns the size of data, without the header.
0997          */
0998         spin_lock_irqsave(&rp->b_lock, flags);
0999         if (!MON_RING_EMPTY(rp)) {
1000             ep = MON_OFF2HDR(rp, rp->b_out);
1001             ret = ep->len_cap;
1002         }
1003         spin_unlock_irqrestore(&rp->b_lock, flags);
1004         break;
1005 
1006     case MON_IOCQ_RING_SIZE:
1007         mutex_lock(&rp->fetch_lock);
1008         ret = rp->b_size;
1009         mutex_unlock(&rp->fetch_lock);
1010         break;
1011 
1012     case MON_IOCT_RING_SIZE:
1013         /*
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
1017          * pressure.
1018          */
1019         {
1020         int size;
1021         struct mon_pgmap *vec;
1022 
1023         if (arg < BUFF_MIN || arg > BUFF_MAX)
1024             return -EINVAL;
1025 
1026         size = CHUNK_ALIGN(arg);
1027         vec = kcalloc(size / CHUNK_SIZE, sizeof(struct mon_pgmap),
1028                   GFP_KERNEL);
1029         if (vec == NULL) {
1030             ret = -ENOMEM;
1031             break;
1032         }
1033 
1034         ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
1035         if (ret < 0) {
1036             kfree(vec);
1037             break;
1038         }
1039 
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);
1044             kfree(vec);
1045             ret = -EBUSY;
1046         } else {
1047             mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
1048             kfree(rp->b_vec);
1049             rp->b_vec  = vec;
1050             rp->b_size = size;
1051             rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
1052             rp->cnt_lost = 0;
1053         }
1054         spin_unlock_irqrestore(&rp->b_lock, flags);
1055         mutex_unlock(&rp->fetch_lock);
1056         }
1057         break;
1058 
1059     case MON_IOCH_MFLUSH:
1060         ret = mon_bin_flush(rp, arg);
1061         break;
1062 
1063     case MON_IOCX_GET:
1064     case MON_IOCX_GETX:
1065         {
1066         struct mon_bin_get getb;
1067 
1068         if (copy_from_user(&getb, (void __user *)arg,
1069                         sizeof(struct mon_bin_get)))
1070             return -EFAULT;
1071 
1072         if (getb.alloc > 0x10000000)    /* Want to cast to u32 */
1073             return -EINVAL;
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);
1077         }
1078         break;
1079 
1080     case MON_IOCX_MFETCH:
1081         {
1082         struct mon_bin_mfetch mfetch;
1083         struct mon_bin_mfetch __user *uptr;
1084 
1085         uptr = (struct mon_bin_mfetch __user *)arg;
1086 
1087         if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1088             return -EFAULT;
1089 
1090         if (mfetch.nflush) {
1091             ret = mon_bin_flush(rp, mfetch.nflush);
1092             if (ret < 0)
1093                 return ret;
1094             if (put_user(ret, &uptr->nflush))
1095                 return -EFAULT;
1096         }
1097         ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1098         if (ret < 0)
1099             return ret;
1100         if (put_user(ret, &uptr->nfetch))
1101             return -EFAULT;
1102         ret = 0;
1103         }
1104         break;
1105 
1106     case MON_IOCG_STATS: {
1107         struct mon_bin_stats __user *sp;
1108         unsigned int nevents;
1109         unsigned int ndropped;
1110 
1111         spin_lock_irqsave(&rp->b_lock, flags);
1112         ndropped = rp->cnt_lost;
1113         rp->cnt_lost = 0;
1114         spin_unlock_irqrestore(&rp->b_lock, flags);
1115         nevents = mon_bin_queued(rp);
1116 
1117         sp = (struct mon_bin_stats __user *)arg;
1118         if (put_user(ndropped, &sp->dropped))
1119             return -EFAULT;
1120         if (put_user(nevents, &sp->queued))
1121             return -EFAULT;
1122 
1123         }
1124         break;
1125 
1126     default:
1127         return -ENOTTY;
1128     }
1129 
1130     return ret;
1131 }
1132 
1133 #ifdef CONFIG_COMPAT
1134 static long mon_bin_compat_ioctl(struct file *file,
1135     unsigned int cmd, unsigned long arg)
1136 {
1137     struct mon_reader_bin *rp = file->private_data;
1138     int ret;
1139 
1140     switch (cmd) {
1141 
1142     case MON_IOCX_GET32:
1143     case MON_IOCX_GETX32:
1144         {
1145         struct mon_bin_get32 getb;
1146 
1147         if (copy_from_user(&getb, (void __user *)arg,
1148                         sizeof(struct mon_bin_get32)))
1149             return -EFAULT;
1150 
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);
1154         if (ret < 0)
1155             return ret;
1156         }
1157         return 0;
1158 
1159     case MON_IOCX_MFETCH32:
1160         {
1161         struct mon_bin_mfetch32 mfetch;
1162         struct mon_bin_mfetch32 __user *uptr;
1163 
1164         uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1165 
1166         if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1167             return -EFAULT;
1168 
1169         if (mfetch.nflush32) {
1170             ret = mon_bin_flush(rp, mfetch.nflush32);
1171             if (ret < 0)
1172                 return ret;
1173             if (put_user(ret, &uptr->nflush32))
1174                 return -EFAULT;
1175         }
1176         ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1177             mfetch.nfetch32);
1178         if (ret < 0)
1179             return ret;
1180         if (put_user(ret, &uptr->nfetch32))
1181             return -EFAULT;
1182         }
1183         return 0;
1184 
1185     case MON_IOCG_STATS:
1186         return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1187 
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);
1193 
1194     default:
1195         ;
1196     }
1197     return -ENOTTY;
1198 }
1199 #endif /* CONFIG_COMPAT */
1200 
1201 static __poll_t
1202 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1203 {
1204     struct mon_reader_bin *rp = file->private_data;
1205     __poll_t mask = 0;
1206     unsigned long flags;
1207 
1208     if (file->f_mode & FMODE_READ)
1209         poll_wait(file, &rp->b_wait, wait);
1210 
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);
1215     return mask;
1216 }
1217 
1218 /*
1219  * open and close: just keep track of how many times the device is
1220  * mapped, to use the proper memory allocation function.
1221  */
1222 static void mon_bin_vma_open(struct vm_area_struct *vma)
1223 {
1224     struct mon_reader_bin *rp = vma->vm_private_data;
1225     unsigned long flags;
1226 
1227     spin_lock_irqsave(&rp->b_lock, flags);
1228     rp->mmap_active++;
1229     spin_unlock_irqrestore(&rp->b_lock, flags);
1230 }
1231 
1232 static void mon_bin_vma_close(struct vm_area_struct *vma)
1233 {
1234     unsigned long flags;
1235 
1236     struct mon_reader_bin *rp = vma->vm_private_data;
1237     spin_lock_irqsave(&rp->b_lock, flags);
1238     rp->mmap_active--;
1239     spin_unlock_irqrestore(&rp->b_lock, flags);
1240 }
1241 
1242 /*
1243  * Map ring pages to user space.
1244  */
1245 static vm_fault_t mon_bin_vma_fault(struct vm_fault *vmf)
1246 {
1247     struct mon_reader_bin *rp = vmf->vma->vm_private_data;
1248     unsigned long offset, chunk_idx;
1249     struct page *pageptr;
1250 
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;
1256     get_page(pageptr);
1257     vmf->page = pageptr;
1258     return 0;
1259 }
1260 
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,
1265 };
1266 
1267 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1268 {
1269     /* don't do anything here: "fault" will set up page table entries */
1270     vma->vm_ops = &mon_bin_vm_ops;
1271 
1272     if (vma->vm_flags & VM_WRITE)
1273         return -EPERM;
1274 
1275     vma->vm_flags &= ~VM_MAYWRITE;
1276     vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1277     vma->vm_private_data = filp->private_data;
1278     mon_bin_vma_open(vma);
1279     return 0;
1280 }
1281 
1282 static const struct file_operations mon_fops_binary = {
1283     .owner =    THIS_MODULE,
1284     .open =     mon_bin_open,
1285     .llseek =   no_llseek,
1286     .read =     mon_bin_read,
1287     /* .write = mon_text_write, */
1288     .poll =     mon_bin_poll,
1289     .unlocked_ioctl = mon_bin_ioctl,
1290 #ifdef CONFIG_COMPAT
1291     .compat_ioctl = mon_bin_compat_ioctl,
1292 #endif
1293     .release =  mon_bin_release,
1294     .mmap =     mon_bin_mmap,
1295 };
1296 
1297 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1298 {
1299     DECLARE_WAITQUEUE(waita, current);
1300     unsigned long flags;
1301 
1302     add_wait_queue(&rp->b_wait, &waita);
1303     set_current_state(TASK_INTERRUPTIBLE);
1304 
1305     spin_lock_irqsave(&rp->b_lock, flags);
1306     while (MON_RING_EMPTY(rp)) {
1307         spin_unlock_irqrestore(&rp->b_lock, flags);
1308 
1309         if (file->f_flags & O_NONBLOCK) {
1310             set_current_state(TASK_RUNNING);
1311             remove_wait_queue(&rp->b_wait, &waita);
1312             return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1313         }
1314         schedule();
1315         if (signal_pending(current)) {
1316             remove_wait_queue(&rp->b_wait, &waita);
1317             return -EINTR;
1318         }
1319         set_current_state(TASK_INTERRUPTIBLE);
1320 
1321         spin_lock_irqsave(&rp->b_lock, flags);
1322     }
1323     spin_unlock_irqrestore(&rp->b_lock, flags);
1324 
1325     set_current_state(TASK_RUNNING);
1326     remove_wait_queue(&rp->b_wait, &waita);
1327     return 0;
1328 }
1329 
1330 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1331 {
1332     int n;
1333     unsigned long vaddr;
1334 
1335     for (n = 0; n < npages; n++) {
1336         vaddr = get_zeroed_page(GFP_KERNEL);
1337         if (vaddr == 0) {
1338             while (n-- != 0)
1339                 free_page((unsigned long) map[n].ptr);
1340             return -ENOMEM;
1341         }
1342         map[n].ptr = (unsigned char *) vaddr;
1343         map[n].pg = virt_to_page((void *) vaddr);
1344     }
1345     return 0;
1346 }
1347 
1348 static void mon_free_buff(struct mon_pgmap *map, int npages)
1349 {
1350     int n;
1351 
1352     for (n = 0; n < npages; n++)
1353         free_page((unsigned long) map[n].ptr);
1354 }
1355 
1356 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1357 {
1358     struct device *dev;
1359     unsigned minor = ubus? ubus->busnum: 0;
1360 
1361     if (minor >= MON_BIN_MAX_MINOR)
1362         return 0;
1363 
1364     dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1365                 MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1366                 "usbmon%d", minor);
1367     if (IS_ERR(dev))
1368         return 0;
1369 
1370     mbus->classdev = dev;
1371     return 1;
1372 }
1373 
1374 void mon_bin_del(struct mon_bus *mbus)
1375 {
1376     device_destroy(mon_bin_class, mbus->classdev->devt);
1377 }
1378 
1379 int __init mon_bin_init(void)
1380 {
1381     int rc;
1382 
1383     mon_bin_class = class_create(THIS_MODULE, "usbmon");
1384     if (IS_ERR(mon_bin_class)) {
1385         rc = PTR_ERR(mon_bin_class);
1386         goto err_class;
1387     }
1388 
1389     rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1390     if (rc < 0)
1391         goto err_dev;
1392 
1393     cdev_init(&mon_bin_cdev, &mon_fops_binary);
1394     mon_bin_cdev.owner = THIS_MODULE;
1395 
1396     rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1397     if (rc < 0)
1398         goto err_add;
1399 
1400     return 0;
1401 
1402 err_add:
1403     unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1404 err_dev:
1405     class_destroy(mon_bin_class);
1406 err_class:
1407     return rc;
1408 }
1409 
1410 void mon_bin_exit(void)
1411 {
1412     cdev_del(&mon_bin_cdev);
1413     unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1414     class_destroy(mon_bin_class);
1415 }