Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The USB Monitor, inspired by Dave Harding's USBMon. |
| 3 | * |
| 4 | * mon_dma.c: Library which snoops on DMA areas. |
| 5 | * |
| 6 | * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com) |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/list.h> |
| 10 | #include <linux/highmem.h> |
| 11 | #include <asm/page.h> |
| 12 | |
| 13 | #include <linux/usb.h> /* Only needed for declarations in usb_mon.h */ |
| 14 | #include "usb_mon.h" |
| 15 | |
Pete Zaitcev | 5b1c674 | 2006-06-09 20:10:10 -0700 | [diff] [blame] | 16 | /* |
| 17 | * PC-compatibles, are, fortunately, sufficiently cache-coherent for this. |
| 18 | */ |
| 19 | #if defined(__i386__) || defined(__x86_64__) /* CONFIG_ARCH_I386 doesn't exit */ |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 20 | #define MON_HAS_UNMAP 1 |
| 21 | |
| 22 | #define phys_to_page(phys) pfn_to_page((phys) >> PAGE_SHIFT) |
| 23 | |
| 24 | char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len) |
| 25 | { |
| 26 | struct page *pg; |
| 27 | unsigned long flags; |
| 28 | unsigned char *map; |
| 29 | unsigned char *ptr; |
| 30 | |
| 31 | /* |
| 32 | * On i386, a DMA handle is the "physical" address of a page. |
| 33 | * In other words, the bus address is equal to physical address. |
| 34 | * There is no IOMMU. |
| 35 | */ |
| 36 | pg = phys_to_page(dma_addr); |
| 37 | |
| 38 | /* |
| 39 | * We are called from hardware IRQs in case of callbacks. |
| 40 | * But we can be called from softirq or process context in case |
| 41 | * of submissions. In such case, we need to protect KM_IRQ0. |
| 42 | */ |
| 43 | local_irq_save(flags); |
| 44 | map = kmap_atomic(pg, KM_IRQ0); |
| 45 | ptr = map + (dma_addr & (PAGE_SIZE-1)); |
| 46 | memcpy(dst, ptr, len); |
| 47 | kunmap_atomic(map, KM_IRQ0); |
| 48 | local_irq_restore(flags); |
| 49 | return 0; |
| 50 | } |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 51 | |
| 52 | void mon_dmapeek_vec(const struct mon_reader_bin *rp, |
| 53 | unsigned int offset, dma_addr_t dma_addr, unsigned int length) |
| 54 | { |
| 55 | unsigned long flags; |
| 56 | unsigned int step_len; |
| 57 | struct page *pg; |
| 58 | unsigned char *map; |
| 59 | unsigned long page_off, page_len; |
| 60 | |
| 61 | local_irq_save(flags); |
| 62 | while (length) { |
| 63 | /* compute number of bytes we are going to copy in this page */ |
| 64 | step_len = length; |
| 65 | page_off = dma_addr & (PAGE_SIZE-1); |
| 66 | page_len = PAGE_SIZE - page_off; |
| 67 | if (page_len < step_len) |
| 68 | step_len = page_len; |
| 69 | |
| 70 | /* copy data and advance pointers */ |
| 71 | pg = phys_to_page(dma_addr); |
| 72 | map = kmap_atomic(pg, KM_IRQ0); |
| 73 | offset = mon_copy_to_buff(rp, offset, map + page_off, step_len); |
| 74 | kunmap_atomic(map, KM_IRQ0); |
| 75 | dma_addr += step_len; |
| 76 | length -= step_len; |
| 77 | } |
| 78 | local_irq_restore(flags); |
| 79 | } |
| 80 | |
Pete Zaitcev | 0256839 | 2005-08-15 16:53:57 -0700 | [diff] [blame] | 81 | #endif /* __i386__ */ |
| 82 | |
| 83 | #ifndef MON_HAS_UNMAP |
| 84 | char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len) |
| 85 | { |
| 86 | return 'D'; |
| 87 | } |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 88 | |
| 89 | void mon_dmapeek_vec(const struct mon_reader_bin *rp, |
| 90 | unsigned int offset, dma_addr_t dma_addr, unsigned int length) |
| 91 | { |
| 92 | ; |
| 93 | } |
| 94 | |
| 95 | #endif /* MON_HAS_UNMAP */ |