| 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 ([email protected]) |
| 8 |
* Copyright (C) 2006,2007 Pete Zaitcev ([email protected]) |
| 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 |
*/ |
| 91 |
struct 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 |
*/ |
| 123 |
struct 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 */ |
| 131 |
struct mon_bin_stats { |
| 132 |
u32 queued; |
| 133 |
u32 dropped; |
| 134 |
}; |
| 135 |
|
| 136 |
struct 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 |
|
| 142 |
struct 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 |
| 149 |
struct mon_bin_get32 { |
| 150 |
u32 hdr32; |
| 151 |
u32 data32; |
| 152 |
u32 alloc32; |
| 153 |
}; |
| 154 |
|
| 155 |
struct 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 |
*/ |
| 177 |
struct 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 |
*/ |
| 185 |
struct 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 |
|
| 205 |
static 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 |
|
| 214 |
static unsigned char xfer_to_pipe[4] = { |
| 215 |
PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT |
| 216 |
}; |
| 217 |
|
| 218 |
static const struct class mon_bin_class = { |
| 219 |
.name = "usbmon", |
| 220 |
}; |
| 221 |
|
| 222 |
static dev_t mon_bin_dev0; |
| 223 |
static struct cdev mon_bin_cdev; |
| 224 |
|
| 225 |
static void mon_buff_area_fill(const struct mon_reader_bin *rp, |
| 226 |
unsigned int offset, unsigned int size); |
| 227 |
static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp); |
| 228 |
static int mon_alloc_buff(struct mon_pgmap *map, int npages); |
| 229 |
static 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 |
*/ |
| 234 |
static 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 |
*/ |
| 266 |
static 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 |
*/ |
| 300 |
static 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 |
*/ |
| 325 |
static 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 |
*/ |
| 364 |
static 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 |
*/ |
| 378 |
static 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 |
|
| 387 |
static 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 |
|
| 398 |
static 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 |
|
| 408 |
static 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 |
*/ |
| 452 |
static 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 |
|
| 470 |
static 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 |
|
| 490 |
static 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 |
|
| 625 |
static 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 |
|
| 631 |
static 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 |
|
| 637 |
static 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 |
|
| 678 |
static 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_obj(struct mon_reader_bin); |
| 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 |
|
| 728 |
err_allocbuff: |
| 729 |
kfree(rp->b_vec); |
| 730 |
err_allocvec: |
| 731 |
kfree(rp); |
| 732 |
err_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 |
*/ |
| 742 |
static 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 |
|
| 783 |
static 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 |
|
| 805 |
static 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 |
*/ |
| 877 |
static 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 |
*/ |
| 903 |
static 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 |
*/ |
| 954 |
static 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 |
*/ |
| 989 |
static 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 = kzalloc_objs(struct mon_pgmap, size / CHUNK_SIZE); |
| 1033 |
if (vec == NULL) { |
| 1034 |
ret = -ENOMEM; |
| 1035 |
break; |
| 1036 |
} |
| 1037 |
|
| 1038 |
ret = mon_alloc_buff(vec, size/CHUNK_SIZE); |
| 1039 |
if (ret < 0) { |
| 1040 |
kfree(vec); |
| 1041 |
break; |
| 1042 |
} |
| 1043 |
|
| 1044 |
mutex_lock(&rp->fetch_lock); |
| 1045 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1046 |
if (rp->mmap_active) { |
| 1047 |
mon_free_buff(vec, size/CHUNK_SIZE); |
| 1048 |
kfree(vec); |
| 1049 |
ret = -EBUSY; |
| 1050 |
} else { |
| 1051 |
mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE); |
| 1052 |
kfree(rp->b_vec); |
| 1053 |
rp->b_vec = vec; |
| 1054 |
rp->b_size = size; |
| 1055 |
rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0; |
| 1056 |
rp->cnt_lost = 0; |
| 1057 |
} |
| 1058 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1059 |
mutex_unlock(&rp->fetch_lock); |
| 1060 |
} |
| 1061 |
break; |
| 1062 |
|
| 1063 |
case MON_IOCH_MFLUSH: |
| 1064 |
ret = mon_bin_flush(rp, arg); |
| 1065 |
break; |
| 1066 |
|
| 1067 |
case MON_IOCX_GET: |
| 1068 |
case MON_IOCX_GETX: |
| 1069 |
{ |
| 1070 |
struct mon_bin_get getb; |
| 1071 |
|
| 1072 |
if (copy_from_user(&getb, (void __user *)arg, |
| 1073 |
sizeof(struct mon_bin_get))) |
| 1074 |
return -EFAULT; |
| 1075 |
|
| 1076 |
if (getb.alloc > 0x10000000) /* Want to cast to u32 */ |
| 1077 |
return -EINVAL; |
| 1078 |
ret = mon_bin_get_event(file, rp, getb.hdr, |
| 1079 |
(cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1, |
| 1080 |
getb.data, (unsigned int)getb.alloc); |
| 1081 |
} |
| 1082 |
break; |
| 1083 |
|
| 1084 |
case MON_IOCX_MFETCH: |
| 1085 |
{ |
| 1086 |
struct mon_bin_mfetch mfetch; |
| 1087 |
struct mon_bin_mfetch __user *uptr; |
| 1088 |
|
| 1089 |
uptr = (struct mon_bin_mfetch __user *)arg; |
| 1090 |
|
| 1091 |
if (copy_from_user(&mfetch, uptr, sizeof(mfetch))) |
| 1092 |
return -EFAULT; |
| 1093 |
|
| 1094 |
if (mfetch.nflush) { |
| 1095 |
ret = mon_bin_flush(rp, mfetch.nflush); |
| 1096 |
if (ret < 0) |
| 1097 |
return ret; |
| 1098 |
if (put_user(ret, &uptr->nflush)) |
| 1099 |
return -EFAULT; |
| 1100 |
} |
| 1101 |
ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch); |
| 1102 |
if (ret < 0) |
| 1103 |
return ret; |
| 1104 |
if (put_user(ret, &uptr->nfetch)) |
| 1105 |
return -EFAULT; |
| 1106 |
ret = 0; |
| 1107 |
} |
| 1108 |
break; |
| 1109 |
|
| 1110 |
case MON_IOCG_STATS: { |
| 1111 |
struct mon_bin_stats __user *sp; |
| 1112 |
unsigned int nevents; |
| 1113 |
unsigned int ndropped; |
| 1114 |
|
| 1115 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1116 |
ndropped = rp->cnt_lost; |
| 1117 |
rp->cnt_lost = 0; |
| 1118 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1119 |
nevents = mon_bin_queued(rp); |
| 1120 |
|
| 1121 |
sp = (struct mon_bin_stats __user *)arg; |
| 1122 |
if (put_user(ndropped, &sp->dropped)) |
| 1123 |
return -EFAULT; |
| 1124 |
if (put_user(nevents, &sp->queued)) |
| 1125 |
return -EFAULT; |
| 1126 |
|
| 1127 |
} |
| 1128 |
break; |
| 1129 |
|
| 1130 |
default: |
| 1131 |
return -ENOTTY; |
| 1132 |
} |
| 1133 |
|
| 1134 |
return ret; |
| 1135 |
} |
| 1136 |
|
| 1137 |
#ifdef CONFIG_COMPAT |
| 1138 |
static long mon_bin_compat_ioctl(struct file *file, |
| 1139 |
unsigned int cmd, unsigned long arg) |
| 1140 |
{ |
| 1141 |
struct mon_reader_bin *rp = file->private_data; |
| 1142 |
int ret; |
| 1143 |
|
| 1144 |
switch (cmd) { |
| 1145 |
|
| 1146 |
case MON_IOCX_GET32: |
| 1147 |
case MON_IOCX_GETX32: |
| 1148 |
{ |
| 1149 |
struct mon_bin_get32 getb; |
| 1150 |
|
| 1151 |
if (copy_from_user(&getb, (void __user *)arg, |
| 1152 |
sizeof(struct mon_bin_get32))) |
| 1153 |
return -EFAULT; |
| 1154 |
|
| 1155 |
ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32), |
| 1156 |
(cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1, |
| 1157 |
compat_ptr(getb.data32), getb.alloc32); |
| 1158 |
if (ret < 0) |
| 1159 |
return ret; |
| 1160 |
} |
| 1161 |
return 0; |
| 1162 |
|
| 1163 |
case MON_IOCX_MFETCH32: |
| 1164 |
{ |
| 1165 |
struct mon_bin_mfetch32 mfetch; |
| 1166 |
struct mon_bin_mfetch32 __user *uptr; |
| 1167 |
|
| 1168 |
uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg); |
| 1169 |
|
| 1170 |
if (copy_from_user(&mfetch, uptr, sizeof(mfetch))) |
| 1171 |
return -EFAULT; |
| 1172 |
|
| 1173 |
if (mfetch.nflush32) { |
| 1174 |
ret = mon_bin_flush(rp, mfetch.nflush32); |
| 1175 |
if (ret < 0) |
| 1176 |
return ret; |
| 1177 |
if (put_user(ret, &uptr->nflush32)) |
| 1178 |
return -EFAULT; |
| 1179 |
} |
| 1180 |
ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32), |
| 1181 |
mfetch.nfetch32); |
| 1182 |
if (ret < 0) |
| 1183 |
return ret; |
| 1184 |
if (put_user(ret, &uptr->nfetch32)) |
| 1185 |
return -EFAULT; |
| 1186 |
} |
| 1187 |
return 0; |
| 1188 |
|
| 1189 |
case MON_IOCG_STATS: |
| 1190 |
return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 1191 |
|
| 1192 |
case MON_IOCQ_URB_LEN: |
| 1193 |
case MON_IOCQ_RING_SIZE: |
| 1194 |
case MON_IOCT_RING_SIZE: |
| 1195 |
case MON_IOCH_MFLUSH: |
| 1196 |
return mon_bin_ioctl(file, cmd, arg); |
| 1197 |
|
| 1198 |
default: |
| 1199 |
; |
| 1200 |
} |
| 1201 |
return -ENOTTY; |
| 1202 |
} |
| 1203 |
#endif /* CONFIG_COMPAT */ |
| 1204 |
|
| 1205 |
static __poll_t |
| 1206 |
mon_bin_poll(struct file *file, struct poll_table_struct *wait) |
| 1207 |
{ |
| 1208 |
struct mon_reader_bin *rp = file->private_data; |
| 1209 |
__poll_t mask = 0; |
| 1210 |
unsigned long flags; |
| 1211 |
|
| 1212 |
if (file->f_mode & FMODE_READ) |
| 1213 |
poll_wait(file, &rp->b_wait, wait); |
| 1214 |
|
| 1215 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1216 |
if (!MON_RING_EMPTY(rp)) |
| 1217 |
mask |= EPOLLIN | EPOLLRDNORM; /* readable */ |
| 1218 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1219 |
return mask; |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* |
| 1223 |
* open and close: just keep track of how many times the device is |
| 1224 |
* mapped, to use the proper memory allocation function. |
| 1225 |
*/ |
| 1226 |
static void mon_bin_vma_open(struct vm_area_struct *vma) |
| 1227 |
{ |
| 1228 |
struct mon_reader_bin *rp = vma->vm_private_data; |
| 1229 |
unsigned long flags; |
| 1230 |
|
| 1231 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1232 |
rp->mmap_active++; |
| 1233 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1234 |
} |
| 1235 |
|
| 1236 |
static void mon_bin_vma_close(struct vm_area_struct *vma) |
| 1237 |
{ |
| 1238 |
unsigned long flags; |
| 1239 |
|
| 1240 |
struct mon_reader_bin *rp = vma->vm_private_data; |
| 1241 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1242 |
rp->mmap_active--; |
| 1243 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1244 |
} |
| 1245 |
|
| 1246 |
/* |
| 1247 |
* Map ring pages to user space. |
| 1248 |
*/ |
| 1249 |
static vm_fault_t mon_bin_vma_fault(struct vm_fault *vmf) |
| 1250 |
{ |
| 1251 |
struct mon_reader_bin *rp = vmf->vma->vm_private_data; |
| 1252 |
unsigned long offset, chunk_idx; |
| 1253 |
struct page *pageptr; |
| 1254 |
unsigned long flags; |
| 1255 |
|
| 1256 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1257 |
offset = vmf->pgoff << PAGE_SHIFT; |
| 1258 |
if (offset >= rp->b_size) { |
| 1259 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1260 |
return VM_FAULT_SIGBUS; |
| 1261 |
} |
| 1262 |
chunk_idx = offset / CHUNK_SIZE; |
| 1263 |
pageptr = rp->b_vec[chunk_idx].pg; |
| 1264 |
get_page(pageptr); |
| 1265 |
vmf->page = pageptr; |
| 1266 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1267 |
return 0; |
| 1268 |
} |
| 1269 |
|
| 1270 |
static const struct vm_operations_struct mon_bin_vm_ops = { |
| 1271 |
.open = mon_bin_vma_open, |
| 1272 |
.close = mon_bin_vma_close, |
| 1273 |
.fault = mon_bin_vma_fault, |
| 1274 |
}; |
| 1275 |
|
| 1276 |
static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1277 |
{ |
| 1278 |
/* don't do anything here: "fault" will set up page table entries */ |
| 1279 |
vma->vm_ops = &mon_bin_vm_ops; |
| 1280 |
|
| 1281 |
if (vma->vm_flags & VM_WRITE) |
| 1282 |
return -EPERM; |
| 1283 |
|
| 1284 |
vm_flags_mod(vma, VM_DONTEXPAND | VM_DONTDUMP, VM_MAYWRITE); |
| 1285 |
vma->vm_private_data = filp->private_data; |
| 1286 |
mon_bin_vma_open(vma); |
| 1287 |
return 0; |
| 1288 |
} |
| 1289 |
|
| 1290 |
static const struct file_operations mon_fops_binary = { |
| 1291 |
.owner = THIS_MODULE, |
| 1292 |
.open = mon_bin_open, |
| 1293 |
.read = mon_bin_read, |
| 1294 |
/* .write = mon_text_write, */ |
| 1295 |
.poll = mon_bin_poll, |
| 1296 |
.unlocked_ioctl = mon_bin_ioctl, |
| 1297 |
#ifdef CONFIG_COMPAT |
| 1298 |
.compat_ioctl = mon_bin_compat_ioctl, |
| 1299 |
#endif |
| 1300 |
.release = mon_bin_release, |
| 1301 |
.mmap = mon_bin_mmap, |
| 1302 |
}; |
| 1303 |
|
| 1304 |
static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp) |
| 1305 |
{ |
| 1306 |
DECLARE_WAITQUEUE(waita, current); |
| 1307 |
unsigned long flags; |
| 1308 |
|
| 1309 |
add_wait_queue(&rp->b_wait, &waita); |
| 1310 |
set_current_state(TASK_INTERRUPTIBLE); |
| 1311 |
|
| 1312 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1313 |
while (MON_RING_EMPTY(rp)) { |
| 1314 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1315 |
|
| 1316 |
if (file->f_flags & O_NONBLOCK) { |
| 1317 |
set_current_state(TASK_RUNNING); |
| 1318 |
remove_wait_queue(&rp->b_wait, &waita); |
| 1319 |
return -EWOULDBLOCK; /* Same as EAGAIN in Linux */ |
| 1320 |
} |
| 1321 |
schedule(); |
| 1322 |
if (signal_pending(current)) { |
| 1323 |
remove_wait_queue(&rp->b_wait, &waita); |
| 1324 |
return -EINTR; |
| 1325 |
} |
| 1326 |
set_current_state(TASK_INTERRUPTIBLE); |
| 1327 |
|
| 1328 |
spin_lock_irqsave(&rp->b_lock, flags); |
| 1329 |
} |
| 1330 |
spin_unlock_irqrestore(&rp->b_lock, flags); |
| 1331 |
|
| 1332 |
set_current_state(TASK_RUNNING); |
| 1333 |
remove_wait_queue(&rp->b_wait, &waita); |
| 1334 |
return 0; |
| 1335 |
} |
| 1336 |
|
| 1337 |
static int mon_alloc_buff(struct mon_pgmap *map, int npages) |
| 1338 |
{ |
| 1339 |
int n; |
| 1340 |
unsigned long vaddr; |
| 1341 |
|
| 1342 |
for (n = 0; n < npages; n++) { |
| 1343 |
vaddr = get_zeroed_page(GFP_KERNEL); |
| 1344 |
if (vaddr == 0) { |
| 1345 |
while (n-- != 0) |
| 1346 |
free_page((unsigned long) map[n].ptr); |
| 1347 |
return -ENOMEM; |
| 1348 |
} |
| 1349 |
map[n].ptr = (unsigned char *) vaddr; |
| 1350 |
map[n].pg = virt_to_page((void *) vaddr); |
| 1351 |
} |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
static void mon_free_buff(struct mon_pgmap *map, int npages) |
| 1356 |
{ |
| 1357 |
int n; |
| 1358 |
|
| 1359 |
for (n = 0; n < npages; n++) |
| 1360 |
free_page((unsigned long) map[n].ptr); |
| 1361 |
} |
| 1362 |
|
| 1363 |
int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus) |
| 1364 |
{ |
| 1365 |
struct device *dev; |
| 1366 |
unsigned minor = ubus? ubus->busnum: 0; |
| 1367 |
|
| 1368 |
if (minor >= MON_BIN_MAX_MINOR) |
| 1369 |
return 0; |
| 1370 |
|
| 1371 |
dev = device_create(&mon_bin_class, ubus ? ubus->controller : NULL, |
| 1372 |
MKDEV(MAJOR(mon_bin_dev0), minor), NULL, |
| 1373 |
"usbmon%d", minor); |
| 1374 |
if (IS_ERR(dev)) |
| 1375 |
return 0; |
| 1376 |
|
| 1377 |
mbus->classdev = dev; |
| 1378 |
return 1; |
| 1379 |
} |
| 1380 |
|
| 1381 |
void mon_bin_del(struct mon_bus *mbus) |
| 1382 |
{ |
| 1383 |
device_destroy(&mon_bin_class, mbus->classdev->devt); |
| 1384 |
} |
| 1385 |
|
| 1386 |
int __init mon_bin_init(void) |
| 1387 |
{ |
| 1388 |
int rc; |
| 1389 |
|
| 1390 |
rc = class_register(&mon_bin_class); |
| 1391 |
if (rc) |
| 1392 |
goto err_class; |
| 1393 |
|
| 1394 |
rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon"); |
| 1395 |
if (rc < 0) |
| 1396 |
goto err_dev; |
| 1397 |
|
| 1398 |
cdev_init(&mon_bin_cdev, &mon_fops_binary); |
| 1399 |
mon_bin_cdev.owner = THIS_MODULE; |
| 1400 |
|
| 1401 |
rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR); |
| 1402 |
if (rc < 0) |
| 1403 |
goto err_add; |
| 1404 |
|
| 1405 |
return 0; |
| 1406 |
|
| 1407 |
err_add: |
| 1408 |
unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR); |
| 1409 |
err_dev: |
| 1410 |
class_unregister(&mon_bin_class); |
| 1411 |
err_class: |
| 1412 |
return rc; |
| 1413 |
} |
| 1414 |
|
| 1415 |
void mon_bin_exit(void) |
| 1416 |
{ |
| 1417 |
cdev_del(&mon_bin_cdev); |
| 1418 |
unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR); |
| 1419 |
class_unregister(&mon_bin_class); |
| 1420 |
} |