File Index
Files and directories
Sign in
FILE

mon_bin.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/mon/mon_bin.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * The USB Monitor, inspired by Dave Harding's USBMon.
   4 *
   5 * This is a binary format reader.
   6 *
   7 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
   8 * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
   9 */
  10
  11#include <linux/kernel.h>
  12#include <linux/sched/signal.h>
  13#include <linux/types.h>
  14#include <linux/fs.h>
  15#include <linux/cdev.h>
  16#include <linux/export.h>
  17#include <linux/usb.h>
  18#include <linux/poll.h>
  19#include <linux/compat.h>
  20#include <linux/mm.h>
  21#include <linux/scatterlist.h>
  22#include <linux/slab.h>
  23#include <linux/time64.h>
  24
  25#include <linux/uaccess.h>
  26
  27#include "usb_mon.h"
  28
  29/*
  30 * Defined by USB 2.0 clause 9.3, table 9.2.
  31 */
  32#define SETUP_LEN  8
  33
  34/* ioctl macros */
  35#define MON_IOC_MAGIC 0x92
  36
  37#define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
  38/* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
  39#define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
  40#define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
  41#define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
  42#define MON_IOCX_GET   _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
  43#define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
  44#define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
  45/* #9 was MON_IOCT_SETAPI */
  46#define MON_IOCX_GETX   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
  47
  48#ifdef CONFIG_COMPAT
  49#define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
  50#define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
  51#define MON_IOCX_GETX32   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
  52#endif
  53
  54/*
  55 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
  56 * But it's all right. Just use a simple way to make sure the chunk is never
  57 * smaller than a page.
  58 *
  59 * N.B. An application does not know our chunk size.
  60 *
  61 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
  62 * page-sized chunks for the time being.
  63 */
  64#define CHUNK_SIZE   PAGE_SIZE
  65#define CHUNK_ALIGN(x)   (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
  66
  67/*
  68 * The magic limit was calculated so that it allows the monitoring
  69 * application to pick data once in two ticks. This way, another application,
  70 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
  71 *
  72 * Originally, for a 480 Mbit/s bus this required a buffer of about 1 MB. For
  73 * modern 20 Gbps buses, this value increases to over 50 MB. The maximum
  74 * buffer size is set to 64 MiB to accommodate this.
  75 *
  76 * This is still too much for most cases, where we just snoop a few
  77 * descriptor fetches for enumeration. So, the default is a "reasonable"
  78 * amount for typical, low-throughput use cases.
  79 *
  80 * XXX What about multi-megabyte URBs which take minutes to transfer?
  81 */
  82#define BUFF_MAX  CHUNK_ALIGN(64*1024*1024)
  83#define BUFF_DFL      CHUNK_ALIGN(300*1024)
  84#define BUFF_MIN        CHUNK_ALIGN(8*1024)
  85
  86/*
  87 * The per-event API header (2 per URB).
  88 *
  89 * This structure is seen in userland as defined by the documentation.
  90 */
  91struct mon_bin_hdr {
  92	u64 id;			/* URB ID - from submission to callback */
  93	unsigned char type;	/* Same as in text API; extensible. */
  94	unsigned char xfer_type;	/* ISO, Intr, Control, Bulk */
  95	unsigned char epnum;	/* Endpoint number and transfer direction */
  96	unsigned char devnum;	/* Device address */
  97	unsigned short busnum;	/* Bus number */
  98	char flag_setup;
  99	char flag_data;
 100	s64 ts_sec;		/* ktime_get_real_ts64 */
 101	s32 ts_usec;		/* ktime_get_real_ts64 */
 102	int status;
 103	unsigned int len_urb;	/* Length of data (submitted or actual) */
 104	unsigned int len_cap;	/* Delivered length */
 105	union {
 106		unsigned char setup[SETUP_LEN];	/* Only for Control S-type */
 107		struct iso_rec {
 108			int error_count;
 109			int numdesc;
 110		} iso;
 111	} s;
 112	int interval;
 113	int start_frame;
 114	unsigned int xfer_flags;
 115	unsigned int ndesc;	/* Actual number of ISO descriptors */
 116};
 117
 118/*
 119 * ISO vector, packed into the head of data stream.
 120 * This has to take 16 bytes to make sure that the end of buffer
 121 * wrap is not happening in the middle of a descriptor.
 122 */
 123struct mon_bin_isodesc {
 124	int          iso_status;
 125	unsigned int iso_off;
 126	unsigned int iso_len;
 127	u32 _pad;
 128};
 129
 130/* per file statistic */
 131struct mon_bin_stats {
 132	u32 queued;
 133	u32 dropped;
 134};
 135
 136struct mon_bin_get {
 137	struct mon_bin_hdr __user *hdr;	/* Can be 48 bytes or 64. */
 138	void __user *data;
 139	size_t alloc;		/* Length of data (can be zero) */
 140};
 141
 142struct mon_bin_mfetch {
 143	u32 __user *offvec;	/* Vector of events fetched */
 144	u32 nfetch;		/* Number of events to fetch (out: fetched) */
 145	u32 nflush;		/* Number of events to flush */
 146};
 147
 148#ifdef CONFIG_COMPAT
 149struct mon_bin_get32 {
 150	u32 hdr32;
 151	u32 data32;
 152	u32 alloc32;
 153};
 154
 155struct mon_bin_mfetch32 {
 156        u32 offvec32;
 157        u32 nfetch32;
 158        u32 nflush32;
 159};
 160#endif
 161
 162/* Having these two values same prevents wrapping of the mon_bin_hdr */
 163#define PKT_ALIGN   64
 164#define PKT_SIZE    64
 165
 166#define PKT_SZ_API0 48	/* API 0 (2.6.20) size */
 167#define PKT_SZ_API1 64	/* API 1 size: extra fields */
 168
 169#define ISODESC_MAX   128	/* Same number as usbfs allows, 2048 bytes. */
 170
 171/* max number of USB bus supported */
 172#define MON_BIN_MAX_MINOR 128
 173
 174/*
 175 * The buffer: map of used pages.
 176 */
 177struct mon_pgmap {
 178	struct page *pg;
 179	unsigned char *ptr;	/* XXX just use page_to_virt everywhere? */
 180};
 181
 182/*
 183 * This gets associated with an open file struct.
 184 */
 185struct mon_reader_bin {
 186	/* The buffer: one per open. */
 187	spinlock_t b_lock;		/* Protect b_cnt, b_in */
 188	unsigned int b_size;		/* Current size of the buffer - bytes */
 189	unsigned int b_cnt;		/* Bytes used */
 190	unsigned int b_in, b_out;	/* Offsets into buffer - bytes */
 191	unsigned int b_read;		/* Amount of read data in curr. pkt. */
 192	struct mon_pgmap *b_vec;	/* The map array */
 193	wait_queue_head_t b_wait;	/* Wait for data here */
 194
 195	struct mutex fetch_lock;	/* Protect b_read, b_out */
 196	int mmap_active;
 197
 198	/* A list of these is needed for "bus 0". Some time later. */
 199	struct mon_reader r;
 200
 201	/* Stats */
 202	unsigned int cnt_lost;
 203};
 204
 205static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
 206    unsigned int offset)
 207{
 208	return (struct mon_bin_hdr *)
 209	    (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
 210}
 211
 212#define MON_RING_EMPTY(rp)	((rp)->b_cnt == 0)
 213
 214static unsigned char xfer_to_pipe[4] = {
 215	PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
 216};
 217
 218static const struct class mon_bin_class = {
 219	.name = "usbmon",
 220};
 221
 222static dev_t mon_bin_dev0;
 223static struct cdev mon_bin_cdev;
 224
 225static void mon_buff_area_fill(const struct mon_reader_bin *rp,
 226    unsigned int offset, unsigned int size);
 227static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
 228static int mon_alloc_buff(struct mon_pgmap *map, int npages);
 229static void mon_free_buff(struct mon_pgmap *map, int npages);
 230
 231/*
 232 * This is a "chunked memcpy". It does not manipulate any counters.
 233 */
 234static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
 235    unsigned int off, const unsigned char *from, unsigned int length)
 236{
 237	unsigned int step_len;
 238	unsigned char *buf;
 239	unsigned int in_page;
 240
 241	while (length) {
 242		/*
 243		 * Determine step_len.
 244		 */
 245		step_len = length;
 246		in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
 247		if (in_page < step_len)
 248			step_len = in_page;
 249
 250		/*
 251		 * Copy data and advance pointers.
 252		 */
 253		buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
 254		memcpy(buf, from, step_len);
 255		if ((off += step_len) >= this->b_size) off = 0;
 256		from += step_len;
 257		length -= step_len;
 258	}
 259	return off;
 260}
 261
 262/*
 263 * This is a little worse than the above because it's "chunked copy_to_user".
 264 * The return value is an error code, not an offset.
 265 */
 266static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
 267    char __user *to, int length)
 268{
 269	unsigned int step_len;
 270	unsigned char *buf;
 271	unsigned int in_page;
 272
 273	while (length) {
 274		/*
 275		 * Determine step_len.
 276		 */
 277		step_len = length;
 278		in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
 279		if (in_page < step_len)
 280			step_len = in_page;
 281
 282		/*
 283		 * Copy data and advance pointers.
 284		 */
 285		buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
 286		if (copy_to_user(to, buf, step_len))
 287			return -EINVAL;
 288		if ((off += step_len) >= this->b_size) off = 0;
 289		to += step_len;
 290		length -= step_len;
 291	}
 292	return 0;
 293}
 294
 295/*
 296 * Allocate an (aligned) area in the buffer.
 297 * This is called under b_lock.
 298 * Returns ~0 on failure.
 299 */
 300static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
 301    unsigned int size)
 302{
 303	unsigned int offset;
 304
 305	size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 306	if (rp->b_cnt + size > rp->b_size)
 307		return ~0;
 308	offset = rp->b_in;
 309	rp->b_cnt += size;
 310	if ((rp->b_in += size) >= rp->b_size)
 311		rp->b_in -= rp->b_size;
 312	return offset;
 313}
 314
 315/*
 316 * This is the same thing as mon_buff_area_alloc, only it does not allow
 317 * buffers to wrap. This is needed by applications which pass references
 318 * into mmap-ed buffers up their stacks (libpcap can do that).
 319 *
 320 * Currently, we always have the header stuck with the data, although
 321 * it is not strictly speaking necessary.
 322 *
 323 * When a buffer would wrap, we place a filler packet to mark the space.
 324 */
 325static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
 326    unsigned int size)
 327{
 328	unsigned int offset;
 329	unsigned int fill_size;
 330
 331	size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 332	if (rp->b_cnt + size > rp->b_size)
 333		return ~0;
 334	if (rp->b_in + size > rp->b_size) {
 335		/*
 336		 * This would wrap. Find if we still have space after
 337		 * skipping to the end of the buffer. If we do, place
 338		 * a filler packet and allocate a new packet.
 339		 */
 340		fill_size = rp->b_size - rp->b_in;
 341		if (rp->b_cnt + size + fill_size > rp->b_size)
 342			return ~0;
 343		mon_buff_area_fill(rp, rp->b_in, fill_size);
 344
 345		offset = 0;
 346		rp->b_in = size;
 347		rp->b_cnt += size + fill_size;
 348	} else if (rp->b_in + size == rp->b_size) {
 349		offset = rp->b_in;
 350		rp->b_in = 0;
 351		rp->b_cnt += size;
 352	} else {
 353		offset = rp->b_in;
 354		rp->b_in += size;
 355		rp->b_cnt += size;
 356	}
 357	return offset;
 358}
 359
 360/*
 361 * Return a few (kilo-)bytes to the head of the buffer.
 362 * This is used if a data fetch fails.
 363 */
 364static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
 365{
 366
 367	/* size &= ~(PKT_ALIGN-1);  -- we're called with aligned size */
 368	rp->b_cnt -= size;
 369	if (rp->b_in < size)
 370		rp->b_in += rp->b_size;
 371	rp->b_in -= size;
 372}
 373
 374/*
 375 * This has to be called under both b_lock and fetch_lock, because
 376 * it accesses both b_cnt and b_out.
 377 */
 378static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
 379{
 380
 381	size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 382	rp->b_cnt -= size;
 383	if ((rp->b_out += size) >= rp->b_size)
 384		rp->b_out -= rp->b_size;
 385}
 386
 387static void mon_buff_area_fill(const struct mon_reader_bin *rp,
 388    unsigned int offset, unsigned int size)
 389{
 390	struct mon_bin_hdr *ep;
 391
 392	ep = MON_OFF2HDR(rp, offset);
 393	memset(ep, 0, PKT_SIZE);
 394	ep->type = '@';
 395	ep->len_cap = size - PKT_SIZE;
 396}
 397
 398static inline char mon_bin_get_setup(unsigned char *setupb,
 399    const struct urb *urb, char ev_type)
 400{
 401
 402	if (urb->setup_packet == NULL)
 403		return 'Z';
 404	memcpy(setupb, urb->setup_packet, SETUP_LEN);
 405	return 0;
 406}
 407
 408static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
 409    unsigned int offset, struct urb *urb, unsigned int length,
 410    char *flag)
 411{
 412	int i;
 413	struct scatterlist *sg;
 414	unsigned int this_len;
 415
 416	*flag = 0;
 417	if (urb->num_sgs == 0) {
 418		if (urb->transfer_buffer == NULL) {
 419			*flag = 'Z';
 420			return length;
 421		}
 422		mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
 423		length = 0;
 424
 425	} else {
 426		/* If IOMMU coalescing occurred, we cannot trust sg_page */
 427		if (urb->transfer_flags & URB_DMA_SG_COMBINED) {
 428			*flag = 'D';
 429			return length;
 430		}
 431
 432		/* Copy up to the first non-addressable segment */
 433		for_each_sg(urb->sg, sg, urb->num_sgs, i) {
 434			if (length == 0 || PageHighMem(sg_page(sg)))
 435				break;
 436			this_len = min_t(unsigned int, sg->length, length);
 437			offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
 438					this_len);
 439			length -= this_len;
 440		}
 441		if (i == 0)
 442			*flag = 'D';
 443	}
 444
 445	return length;
 446}
 447
 448/*
 449 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
 450 * be used to determine the length of the whole contiguous buffer.
 451 */
 452static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin *rp,
 453    struct urb *urb, unsigned int ndesc)
 454{
 455	struct usb_iso_packet_descriptor *fp;
 456	unsigned int length;
 457
 458	length = 0;
 459	fp = urb->iso_frame_desc;
 460	while (ndesc-- != 0) {
 461		if (fp->actual_length != 0) {
 462			if (fp->offset + fp->actual_length > length)
 463				length = fp->offset + fp->actual_length;
 464		}
 465		fp++;
 466	}
 467	return length;
 468}
 469
 470static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
 471    unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
 472{
 473	struct mon_bin_isodesc *dp;
 474	struct usb_iso_packet_descriptor *fp;
 475
 476	fp = urb->iso_frame_desc;
 477	while (ndesc-- != 0) {
 478		dp = (struct mon_bin_isodesc *)
 479		    (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
 480		dp->iso_status = fp->status;
 481		dp->iso_off = fp->offset;
 482		dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
 483		dp->_pad = 0;
 484		if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
 485			offset = 0;
 486		fp++;
 487	}
 488}
 489
 490static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
 491    char ev_type, int status)
 492{
 493	const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
 494	struct timespec64 ts;
 495	unsigned long flags;
 496	unsigned int urb_length;
 497	unsigned int offset;
 498	unsigned int length;
 499	unsigned int delta;
 500	unsigned int ndesc, lendesc;
 501	unsigned char dir;
 502	struct mon_bin_hdr *ep;
 503	char data_tag = 0;
 504
 505	ktime_get_real_ts64(&ts);
 506
 507	spin_lock_irqsave(&rp->b_lock, flags);
 508
 509	/*
 510	 * Find the maximum allowable length, then allocate space.
 511	 */
 512	urb_length = (ev_type == 'S') ?
 513	    urb->transfer_buffer_length : urb->actual_length;
 514	length = urb_length;
 515
 516	if (usb_endpoint_xfer_isoc(epd)) {
 517		if (urb->number_of_packets < 0) {
 518			ndesc = 0;
 519		} else if (urb->number_of_packets >= ISODESC_MAX) {
 520			ndesc = ISODESC_MAX;
 521		} else {
 522			ndesc = urb->number_of_packets;
 523		}
 524		if (ev_type == 'C' && usb_urb_dir_in(urb))
 525			length = mon_bin_collate_isodesc(rp, urb, ndesc);
 526	} else {
 527		ndesc = 0;
 528	}
 529	lendesc = ndesc*sizeof(struct mon_bin_isodesc);
 530
 531	/* not an issue unless there's a subtle bug in a HCD somewhere */
 532	if (length >= urb->transfer_buffer_length)
 533		length = urb->transfer_buffer_length;
 534
 535	if (length >= rp->b_size/5)
 536		length = rp->b_size/5;
 537
 538	if (usb_urb_dir_in(urb)) {
 539		if (ev_type == 'S') {
 540			length = 0;
 541			data_tag = '<';
 542		}
 543		/* Cannot rely on endpoint number in case of control ep.0 */
 544		dir = USB_DIR_IN;
 545	} else {
 546		if (ev_type == 'C') {
 547			length = 0;
 548			data_tag = '>';
 549		}
 550		dir = 0;
 551	}
 552
 553	if (rp->mmap_active) {
 554		offset = mon_buff_area_alloc_contiguous(rp,
 555						 length + PKT_SIZE + lendesc);
 556	} else {
 557		offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
 558	}
 559	if (offset == ~0) {
 560		rp->cnt_lost++;
 561		spin_unlock_irqrestore(&rp->b_lock, flags);
 562		return;
 563	}
 564
 565	ep = MON_OFF2HDR(rp, offset);
 566	if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
 567
 568	/*
 569	 * Fill the allocated area.
 570	 */
 571	memset(ep, 0, PKT_SIZE);
 572	ep->type = ev_type;
 573	ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
 574	ep->epnum = dir | usb_endpoint_num(epd);
 575	ep->devnum = urb->dev->devnum;
 576	ep->busnum = urb->dev->bus->busnum;
 577	ep->id = (unsigned long) urb;
 578	ep->ts_sec = ts.tv_sec;
 579	ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
 580	ep->status = status;
 581	ep->len_urb = urb_length;
 582	ep->len_cap = length + lendesc;
 583	ep->xfer_flags = urb->transfer_flags;
 584
 585	if (usb_endpoint_xfer_int(epd)) {
 586		ep->interval = urb->interval;
 587	} else if (usb_endpoint_xfer_isoc(epd)) {
 588		ep->interval = urb->interval;
 589		ep->start_frame = urb->start_frame;
 590		ep->s.iso.error_count = urb->error_count;
 591		ep->s.iso.numdesc = urb->number_of_packets;
 592	}
 593
 594	if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
 595		ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
 596	} else {
 597		ep->flag_setup = '-';
 598	}
 599
 600	if (ndesc != 0) {
 601		ep->ndesc = ndesc;
 602		mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
 603		if ((offset += lendesc) >= rp->b_size)
 604			offset -= rp->b_size;
 605	}
 606
 607	if (length != 0) {
 608		length = mon_bin_get_data(rp, offset, urb, length,
 609				&ep->flag_data);
 610		if (length > 0) {
 611			delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 612			ep->len_cap -= length;
 613			delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 614			mon_buff_area_shrink(rp, delta);
 615		}
 616	} else {
 617		ep->flag_data = data_tag;
 618	}
 619
 620	spin_unlock_irqrestore(&rp->b_lock, flags);
 621
 622	wake_up(&rp->b_wait);
 623}
 624
 625static void mon_bin_submit(void *data, struct urb *urb)
 626{
 627	struct mon_reader_bin *rp = data;
 628	mon_bin_event(rp, urb, 'S', -EINPROGRESS);
 629}
 630
 631static void mon_bin_complete(void *data, struct urb *urb, int status)
 632{
 633	struct mon_reader_bin *rp = data;
 634	mon_bin_event(rp, urb, 'C', status);
 635}
 636
 637static void mon_bin_error(void *data, struct urb *urb, int error)
 638{
 639	struct mon_reader_bin *rp = data;
 640	struct timespec64 ts;
 641	unsigned long flags;
 642	unsigned int offset;
 643	struct mon_bin_hdr *ep;
 644
 645	ktime_get_real_ts64(&ts);
 646
 647	spin_lock_irqsave(&rp->b_lock, flags);
 648
 649	offset = mon_buff_area_alloc(rp, PKT_SIZE);
 650	if (offset == ~0) {
 651		/* Not incrementing cnt_lost. Just because. */
 652		spin_unlock_irqrestore(&rp->b_lock, flags);
 653		return;
 654	}
 655
 656	ep = MON_OFF2HDR(rp, offset);
 657
 658	memset(ep, 0, PKT_SIZE);
 659	ep->type = 'E';
 660	ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
 661	ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
 662	ep->epnum |= usb_endpoint_num(&urb->ep->desc);
 663	ep->devnum = urb->dev->devnum;
 664	ep->busnum = urb->dev->bus->busnum;
 665	ep->id = (unsigned long) urb;
 666	ep->ts_sec = ts.tv_sec;
 667	ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
 668	ep->status = error;
 669
 670	ep->flag_setup = '-';
 671	ep->flag_data = 'E';
 672
 673	spin_unlock_irqrestore(&rp->b_lock, flags);
 674
 675	wake_up(&rp->b_wait);
 676}
 677
 678static int mon_bin_open(struct inode *inode, struct file *file)
 679{
 680	struct mon_bus *mbus;
 681	struct mon_reader_bin *rp;
 682	size_t size;
 683	int rc;
 684
 685	mutex_lock(&mon_lock);
 686	mbus = mon_bus_lookup(iminor(inode));
 687	if (mbus == NULL) {
 688		mutex_unlock(&mon_lock);
 689		return -ENODEV;
 690	}
 691	if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
 692		printk(KERN_ERR TAG ": consistency error on open\n");
 693		mutex_unlock(&mon_lock);
 694		return -ENODEV;
 695	}
 696
 697	rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
 698	if (rp == NULL) {
 699		rc = -ENOMEM;
 700		goto err_alloc;
 701	}
 702	spin_lock_init(&rp->b_lock);
 703	init_waitqueue_head(&rp->b_wait);
 704	mutex_init(&rp->fetch_lock);
 705	rp->b_size = BUFF_DFL;
 706
 707	size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
 708	if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
 709		rc = -ENOMEM;
 710		goto err_allocvec;
 711	}
 712
 713	if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
 714		goto err_allocbuff;
 715
 716	rp->r.m_bus = mbus;
 717	rp->r.r_data = rp;
 718	rp->r.rnf_submit = mon_bin_submit;
 719	rp->r.rnf_error = mon_bin_error;
 720	rp->r.rnf_complete = mon_bin_complete;
 721
 722	mon_reader_add(mbus, &rp->r);
 723
 724	file->private_data = rp;
 725	mutex_unlock(&mon_lock);
 726	return 0;
 727
 728err_allocbuff:
 729	kfree(rp->b_vec);
 730err_allocvec:
 731	kfree(rp);
 732err_alloc:
 733	mutex_unlock(&mon_lock);
 734	return rc;
 735}
 736
 737/*
 738 * Extract an event from buffer and copy it to user space.
 739 * Wait if there is no event ready.
 740 * Returns zero or error.
 741 */
 742static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
 743    struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
 744    void __user *data, unsigned int nbytes)
 745{
 746	unsigned long flags;
 747	struct mon_bin_hdr *ep;
 748	size_t step_len;
 749	unsigned int offset;
 750	int rc;
 751
 752	mutex_lock(&rp->fetch_lock);
 753
 754	if ((rc = mon_bin_wait_event(file, rp)) < 0) {
 755		mutex_unlock(&rp->fetch_lock);
 756		return rc;
 757	}
 758
 759	ep = MON_OFF2HDR(rp, rp->b_out);
 760
 761	if (copy_to_user(hdr, ep, hdrbytes)) {
 762		mutex_unlock(&rp->fetch_lock);
 763		return -EFAULT;
 764	}
 765
 766	step_len = min(ep->len_cap, nbytes);
 767	if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
 768
 769	if (copy_from_buf(rp, offset, data, step_len)) {
 770		mutex_unlock(&rp->fetch_lock);
 771		return -EFAULT;
 772	}
 773
 774	spin_lock_irqsave(&rp->b_lock, flags);
 775	mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
 776	spin_unlock_irqrestore(&rp->b_lock, flags);
 777	rp->b_read = 0;
 778
 779	mutex_unlock(&rp->fetch_lock);
 780	return 0;
 781}
 782
 783static int mon_bin_release(struct inode *inode, struct file *file)
 784{
 785	struct mon_reader_bin *rp = file->private_data;
 786	struct mon_bus* mbus = rp->r.m_bus;
 787
 788	mutex_lock(&mon_lock);
 789
 790	if (mbus->nreaders <= 0) {
 791		printk(KERN_ERR TAG ": consistency error on close\n");
 792		mutex_unlock(&mon_lock);
 793		return 0;
 794	}
 795	mon_reader_del(mbus, &rp->r);
 796
 797	mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
 798	kfree(rp->b_vec);
 799	kfree(rp);
 800
 801	mutex_unlock(&mon_lock);
 802	return 0;
 803}
 804
 805static ssize_t mon_bin_read(struct file *file, char __user *buf,
 806    size_t nbytes, loff_t *ppos)
 807{
 808	struct mon_reader_bin *rp = file->private_data;
 809	unsigned int hdrbytes = PKT_SZ_API0;
 810	unsigned long flags;
 811	struct mon_bin_hdr *ep;
 812	unsigned int offset;
 813	size_t step_len;
 814	char *ptr;
 815	ssize_t done = 0;
 816	int rc;
 817
 818	mutex_lock(&rp->fetch_lock);
 819
 820	if ((rc = mon_bin_wait_event(file, rp)) < 0) {
 821		mutex_unlock(&rp->fetch_lock);
 822		return rc;
 823	}
 824
 825	ep = MON_OFF2HDR(rp, rp->b_out);
 826
 827	if (rp->b_read < hdrbytes) {
 828		step_len = min_t(size_t, nbytes, hdrbytes - rp->b_read);
 829		ptr = ((char *)ep) + rp->b_read;
 830		if (step_len && copy_to_user(buf, ptr, step_len)) {
 831			mutex_unlock(&rp->fetch_lock);
 832			return -EFAULT;
 833		}
 834		nbytes -= step_len;
 835		buf += step_len;
 836		rp->b_read += step_len;
 837		done += step_len;
 838	}
 839
 840	if (rp->b_read >= hdrbytes) {
 841		step_len = ep->len_cap;
 842		step_len -= rp->b_read - hdrbytes;
 843		if (step_len > nbytes)
 844			step_len = nbytes;
 845		offset = rp->b_out + PKT_SIZE;
 846		offset += rp->b_read - hdrbytes;
 847		if (offset >= rp->b_size)
 848			offset -= rp->b_size;
 849		if (copy_from_buf(rp, offset, buf, step_len)) {
 850			mutex_unlock(&rp->fetch_lock);
 851			return -EFAULT;
 852		}
 853		nbytes -= step_len;
 854		buf += step_len;
 855		rp->b_read += step_len;
 856		done += step_len;
 857	}
 858
 859	/*
 860	 * Check if whole packet was read, and if so, jump to the next one.
 861	 */
 862	if (rp->b_read >= hdrbytes + ep->len_cap) {
 863		spin_lock_irqsave(&rp->b_lock, flags);
 864		mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
 865		spin_unlock_irqrestore(&rp->b_lock, flags);
 866		rp->b_read = 0;
 867	}
 868
 869	mutex_unlock(&rp->fetch_lock);
 870	return done;
 871}
 872
 873/*
 874 * Remove at most nevents from chunked buffer.
 875 * Returns the number of removed events.
 876 */
 877static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
 878{
 879	unsigned long flags;
 880	struct mon_bin_hdr *ep;
 881	int i;
 882
 883	mutex_lock(&rp->fetch_lock);
 884	spin_lock_irqsave(&rp->b_lock, flags);
 885	for (i = 0; i < nevents; ++i) {
 886		if (MON_RING_EMPTY(rp))
 887			break;
 888
 889		ep = MON_OFF2HDR(rp, rp->b_out);
 890		mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
 891	}
 892	spin_unlock_irqrestore(&rp->b_lock, flags);
 893	rp->b_read = 0;
 894	mutex_unlock(&rp->fetch_lock);
 895	return i;
 896}
 897
 898/*
 899 * Fetch at most max event offsets into the buffer and put them into vec.
 900 * The events are usually freed later with mon_bin_flush.
 901 * Return the effective number of events fetched.
 902 */
 903static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
 904    u32 __user *vec, unsigned int max)
 905{
 906	unsigned int cur_out;
 907	unsigned int bytes, avail;
 908	unsigned int size;
 909	unsigned int nevents;
 910	struct mon_bin_hdr *ep;
 911	unsigned long flags;
 912	int rc;
 913
 914	mutex_lock(&rp->fetch_lock);
 915
 916	if ((rc = mon_bin_wait_event(file, rp)) < 0) {
 917		mutex_unlock(&rp->fetch_lock);
 918		return rc;
 919	}
 920
 921	spin_lock_irqsave(&rp->b_lock, flags);
 922	avail = rp->b_cnt;
 923	spin_unlock_irqrestore(&rp->b_lock, flags);
 924
 925	cur_out = rp->b_out;
 926	nevents = 0;
 927	bytes = 0;
 928	while (bytes < avail) {
 929		if (nevents >= max)
 930			break;
 931
 932		ep = MON_OFF2HDR(rp, cur_out);
 933		if (put_user(cur_out, &vec[nevents])) {
 934			mutex_unlock(&rp->fetch_lock);
 935			return -EFAULT;
 936		}
 937
 938		nevents++;
 939		size = ep->len_cap + PKT_SIZE;
 940		size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 941		if ((cur_out += size) >= rp->b_size)
 942			cur_out -= rp->b_size;
 943		bytes += size;
 944	}
 945
 946	mutex_unlock(&rp->fetch_lock);
 947	return nevents;
 948}
 949
 950/*
 951 * Count events. This is almost the same as the above mon_bin_fetch,
 952 * only we do not store offsets into user vector, and we have no limit.
 953 */
 954static int mon_bin_queued(struct mon_reader_bin *rp)
 955{
 956	unsigned int cur_out;
 957	unsigned int bytes, avail;
 958	unsigned int size;
 959	unsigned int nevents;
 960	struct mon_bin_hdr *ep;
 961	unsigned long flags;
 962
 963	mutex_lock(&rp->fetch_lock);
 964
 965	spin_lock_irqsave(&rp->b_lock, flags);
 966	avail = rp->b_cnt;
 967	spin_unlock_irqrestore(&rp->b_lock, flags);
 968
 969	cur_out = rp->b_out;
 970	nevents = 0;
 971	bytes = 0;
 972	while (bytes < avail) {
 973		ep = MON_OFF2HDR(rp, cur_out);
 974
 975		nevents++;
 976		size = ep->len_cap + PKT_SIZE;
 977		size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
 978		if ((cur_out += size) >= rp->b_size)
 979			cur_out -= rp->b_size;
 980		bytes += size;
 981	}
 982
 983	mutex_unlock(&rp->fetch_lock);
 984	return nevents;
 985}
 986
 987/*
 988 */
 989static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 990{
 991	struct mon_reader_bin *rp = file->private_data;
 992	// struct mon_bus* mbus = rp->r.m_bus;
 993	int ret = 0;
 994	struct mon_bin_hdr *ep;
 995	unsigned long flags;
 996
 997	switch (cmd) {
 998
 999	case MON_IOCQ_URB_LEN:
1000		/*
1001		 * N.B. This only returns the size of data, without the header.
1002		 */
1003		spin_lock_irqsave(&rp->b_lock, flags);
1004		if (!MON_RING_EMPTY(rp)) {
1005			ep = MON_OFF2HDR(rp, rp->b_out);
1006			ret = ep->len_cap;
1007		}
1008		spin_unlock_irqrestore(&rp->b_lock, flags);
1009		break;
1010
1011	case MON_IOCQ_RING_SIZE:
1012		mutex_lock(&rp->fetch_lock);
1013		ret = rp->b_size;
1014		mutex_unlock(&rp->fetch_lock);
1015		break;
1016
1017	case MON_IOCT_RING_SIZE:
1018		/*
1019		 * Changing the buffer size will flush it's contents; the new
1020		 * buffer is allocated before releasing the old one to be sure
1021		 * the device will stay functional also in case of memory
1022		 * pressure.
1023		 */
1024		{
1025		int size;
1026		struct mon_pgmap *vec;
1027
1028		if (arg < BUFF_MIN || arg > BUFF_MAX)
1029			return -EINVAL;
1030
1031		size = CHUNK_ALIGN(arg);
1032		vec = kcalloc(size / CHUNK_SIZE, sizeof(struct mon_pgmap),
1033			      GFP_KERNEL);
1034		if (vec == NULL) {
1035			ret = -ENOMEM;
1036			break;
1037		}
1038
1039		ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
1040		if (ret < 0) {
1041			kfree(vec);
1042			break;
1043		}
1044
1045		mutex_lock(&rp->fetch_lock);
1046		spin_lock_irqsave(&rp->b_lock, flags);
1047		if (rp->mmap_active) {
1048			mon_free_buff(vec, size/CHUNK_SIZE);
1049			kfree(vec);
1050			ret = -EBUSY;
1051		} else {
1052			mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
1053			kfree(rp->b_vec);
1054			rp->b_vec  = vec;
1055			rp->b_size = size;
1056			rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
1057			rp->cnt_lost = 0;
1058		}
1059		spin_unlock_irqrestore(&rp->b_lock, flags);
1060		mutex_unlock(&rp->fetch_lock);
1061		}
1062		break;
1063
1064	case MON_IOCH_MFLUSH:
1065		ret = mon_bin_flush(rp, arg);
1066		break;
1067
1068	case MON_IOCX_GET:
1069	case MON_IOCX_GETX:
1070		{
1071		struct mon_bin_get getb;
1072
1073		if (copy_from_user(&getb, (void __user *)arg,
1074					    sizeof(struct mon_bin_get)))
1075			return -EFAULT;
1076
1077		if (getb.alloc > 0x10000000)	/* Want to cast to u32 */
1078			return -EINVAL;
1079		ret = mon_bin_get_event(file, rp, getb.hdr,
1080		    (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
1081		    getb.data, (unsigned int)getb.alloc);
1082		}
1083		break;
1084
1085	case MON_IOCX_MFETCH:
1086		{
1087		struct mon_bin_mfetch mfetch;
1088		struct mon_bin_mfetch __user *uptr;
1089
1090		uptr = (struct mon_bin_mfetch __user *)arg;
1091
1092		if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1093			return -EFAULT;
1094
1095		if (mfetch.nflush) {
1096			ret = mon_bin_flush(rp, mfetch.nflush);
1097			if (ret < 0)
1098				return ret;
1099			if (put_user(ret, &uptr->nflush))
1100				return -EFAULT;
1101		}
1102		ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1103		if (ret < 0)
1104			return ret;
1105		if (put_user(ret, &uptr->nfetch))
1106			return -EFAULT;
1107		ret = 0;
1108		}
1109		break;
1110
1111	case MON_IOCG_STATS: {
1112		struct mon_bin_stats __user *sp;
1113		unsigned int nevents;
1114		unsigned int ndropped;
1115
1116		spin_lock_irqsave(&rp->b_lock, flags);
1117		ndropped = rp->cnt_lost;
1118		rp->cnt_lost = 0;
1119		spin_unlock_irqrestore(&rp->b_lock, flags);
1120		nevents = mon_bin_queued(rp);
1121
1122		sp = (struct mon_bin_stats __user *)arg;
1123		if (put_user(ndropped, &sp->dropped))
1124			return -EFAULT;
1125		if (put_user(nevents, &sp->queued))
1126			return -EFAULT;
1127
1128		}
1129		break;
1130
1131	default:
1132		return -ENOTTY;
1133	}
1134
1135	return ret;
1136}
1137
1138#ifdef CONFIG_COMPAT
1139static long mon_bin_compat_ioctl(struct file *file,
1140    unsigned int cmd, unsigned long arg)
1141{
1142	struct mon_reader_bin *rp = file->private_data;
1143	int ret;
1144
1145	switch (cmd) {
1146
1147	case MON_IOCX_GET32:
1148	case MON_IOCX_GETX32:
1149		{
1150		struct mon_bin_get32 getb;
1151
1152		if (copy_from_user(&getb, (void __user *)arg,
1153					    sizeof(struct mon_bin_get32)))
1154			return -EFAULT;
1155
1156		ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1157		    (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1158		    compat_ptr(getb.data32), getb.alloc32);
1159		if (ret < 0)
1160			return ret;
1161		}
1162		return 0;
1163
1164	case MON_IOCX_MFETCH32:
1165		{
1166		struct mon_bin_mfetch32 mfetch;
1167		struct mon_bin_mfetch32 __user *uptr;
1168
1169		uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1170
1171		if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1172			return -EFAULT;
1173
1174		if (mfetch.nflush32) {
1175			ret = mon_bin_flush(rp, mfetch.nflush32);
1176			if (ret < 0)
1177				return ret;
1178			if (put_user(ret, &uptr->nflush32))
1179				return -EFAULT;
1180		}
1181		ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1182		    mfetch.nfetch32);
1183		if (ret < 0)
1184			return ret;
1185		if (put_user(ret, &uptr->nfetch32))
1186			return -EFAULT;
1187		}
1188		return 0;
1189
1190	case MON_IOCG_STATS:
1191		return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1192
1193	case MON_IOCQ_URB_LEN:
1194	case MON_IOCQ_RING_SIZE:
1195	case MON_IOCT_RING_SIZE:
1196	case MON_IOCH_MFLUSH:
1197		return mon_bin_ioctl(file, cmd, arg);
1198
1199	default:
1200		;
1201	}
1202	return -ENOTTY;
1203}
1204#endif /* CONFIG_COMPAT */
1205
1206static __poll_t
1207mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1208{
1209	struct mon_reader_bin *rp = file->private_data;
1210	__poll_t mask = 0;
1211	unsigned long flags;
1212
1213	if (file->f_mode & FMODE_READ)
1214		poll_wait(file, &rp->b_wait, wait);
1215
1216	spin_lock_irqsave(&rp->b_lock, flags);
1217	if (!MON_RING_EMPTY(rp))
1218		mask |= EPOLLIN | EPOLLRDNORM;    /* readable */
1219	spin_unlock_irqrestore(&rp->b_lock, flags);
1220	return mask;
1221}
1222
1223/*
1224 * open and close: just keep track of how many times the device is
1225 * mapped, to use the proper memory allocation function.
1226 */
1227static void mon_bin_vma_open(struct vm_area_struct *vma)
1228{
1229	struct mon_reader_bin *rp = vma->vm_private_data;
1230	unsigned long flags;
1231
1232	spin_lock_irqsave(&rp->b_lock, flags);
1233	rp->mmap_active++;
1234	spin_unlock_irqrestore(&rp->b_lock, flags);
1235}
1236
1237static void mon_bin_vma_close(struct vm_area_struct *vma)
1238{
1239	unsigned long flags;
1240
1241	struct mon_reader_bin *rp = vma->vm_private_data;
1242	spin_lock_irqsave(&rp->b_lock, flags);
1243	rp->mmap_active--;
1244	spin_unlock_irqrestore(&rp->b_lock, flags);
1245}
1246
1247/*
1248 * Map ring pages to user space.
1249 */
1250static vm_fault_t mon_bin_vma_fault(struct vm_fault *vmf)
1251{
1252	struct mon_reader_bin *rp = vmf->vma->vm_private_data;
1253	unsigned long offset, chunk_idx;
1254	struct page *pageptr;
1255	unsigned long flags;
1256
1257	spin_lock_irqsave(&rp->b_lock, flags);
1258	offset = vmf->pgoff << PAGE_SHIFT;
1259	if (offset >= rp->b_size) {
1260		spin_unlock_irqrestore(&rp->b_lock, flags);
1261		return VM_FAULT_SIGBUS;
1262	}
1263	chunk_idx = offset / CHUNK_SIZE;
1264	pageptr = rp->b_vec[chunk_idx].pg;
1265	get_page(pageptr);
1266	vmf->page = pageptr;
1267	spin_unlock_irqrestore(&rp->b_lock, flags);
1268	return 0;
1269}
1270
1271static const struct vm_operations_struct mon_bin_vm_ops = {
1272	.open =     mon_bin_vma_open,
1273	.close =    mon_bin_vma_close,
1274	.fault =    mon_bin_vma_fault,
1275};
1276
1277static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1278{
1279	/* don't do anything here: "fault" will set up page table entries */
1280	vma->vm_ops = &mon_bin_vm_ops;
1281
1282	if (vma->vm_flags & VM_WRITE)
1283		return -EPERM;
1284
1285	vm_flags_mod(vma, VM_DONTEXPAND | VM_DONTDUMP, VM_MAYWRITE);
1286	vma->vm_private_data = filp->private_data;
1287	mon_bin_vma_open(vma);
1288	return 0;
1289}
1290
1291static const struct file_operations mon_fops_binary = {
1292	.owner =	THIS_MODULE,
1293	.open =		mon_bin_open,
1294	.read =		mon_bin_read,
1295	/* .write =	mon_text_write, */
1296	.poll =		mon_bin_poll,
1297	.unlocked_ioctl = mon_bin_ioctl,
1298#ifdef CONFIG_COMPAT
1299	.compat_ioctl =	mon_bin_compat_ioctl,
1300#endif
1301	.release =	mon_bin_release,
1302	.mmap =		mon_bin_mmap,
1303};
1304
1305static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1306{
1307	DECLARE_WAITQUEUE(waita, current);
1308	unsigned long flags;
1309
1310	add_wait_queue(&rp->b_wait, &waita);
1311	set_current_state(TASK_INTERRUPTIBLE);
1312
1313	spin_lock_irqsave(&rp->b_lock, flags);
1314	while (MON_RING_EMPTY(rp)) {
1315		spin_unlock_irqrestore(&rp->b_lock, flags);
1316
1317		if (file->f_flags & O_NONBLOCK) {
1318			set_current_state(TASK_RUNNING);
1319			remove_wait_queue(&rp->b_wait, &waita);
1320			return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1321		}
1322		schedule();
1323		if (signal_pending(current)) {
1324			remove_wait_queue(&rp->b_wait, &waita);
1325			return -EINTR;
1326		}
1327		set_current_state(TASK_INTERRUPTIBLE);
1328
1329		spin_lock_irqsave(&rp->b_lock, flags);
1330	}
1331	spin_unlock_irqrestore(&rp->b_lock, flags);
1332
1333	set_current_state(TASK_RUNNING);
1334	remove_wait_queue(&rp->b_wait, &waita);
1335	return 0;
1336}
1337
1338static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1339{
1340	int n;
1341	unsigned long vaddr;
1342
1343	for (n = 0; n < npages; n++) {
1344		vaddr = get_zeroed_page(GFP_KERNEL);
1345		if (vaddr == 0) {
1346			while (n-- != 0)
1347				free_page((unsigned long) map[n].ptr);
1348			return -ENOMEM;
1349		}
1350		map[n].ptr = (unsigned char *) vaddr;
1351		map[n].pg = virt_to_page((void *) vaddr);
1352	}
1353	return 0;
1354}
1355
1356static void mon_free_buff(struct mon_pgmap *map, int npages)
1357{
1358	int n;
1359
1360	for (n = 0; n < npages; n++)
1361		free_page((unsigned long) map[n].ptr);
1362}
1363
1364int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1365{
1366	struct device *dev;
1367	unsigned minor = ubus? ubus->busnum: 0;
1368
1369	if (minor >= MON_BIN_MAX_MINOR)
1370		return 0;
1371
1372	dev = device_create(&mon_bin_class, ubus ? ubus->controller : NULL,
1373			    MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1374			    "usbmon%d", minor);
1375	if (IS_ERR(dev))
1376		return 0;
1377
1378	mbus->classdev = dev;
1379	return 1;
1380}
1381
1382void mon_bin_del(struct mon_bus *mbus)
1383{
1384	device_destroy(&mon_bin_class, mbus->classdev->devt);
1385}
1386
1387int __init mon_bin_init(void)
1388{
1389	int rc;
1390
1391	rc = class_register(&mon_bin_class);
1392	if (rc)
1393		goto err_class;
1394
1395	rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1396	if (rc < 0)
1397		goto err_dev;
1398
1399	cdev_init(&mon_bin_cdev, &mon_fops_binary);
1400	mon_bin_cdev.owner = THIS_MODULE;
1401
1402	rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1403	if (rc < 0)
1404		goto err_add;
1405
1406	return 0;
1407
1408err_add:
1409	unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1410err_dev:
1411	class_unregister(&mon_bin_class);
1412err_class:
1413	return rc;
1414}
1415
1416void mon_bin_exit(void)
1417{
1418	cdev_del(&mon_bin_cdev);
1419	unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1420	class_unregister(&mon_bin_class);
1421}
~/ Read-only file preview.