mon_bin.c 28 KB

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