mon_bin.c 29 KB

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