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