mon_bin.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 = ep->len_cap;
  588. step_len -= rp->b_read - sizeof(struct mon_bin_hdr);
  589. if (step_len > nbytes)
  590. step_len = nbytes;
  591. offset = rp->b_out + PKT_SIZE;
  592. offset += rp->b_read - sizeof(struct mon_bin_hdr);
  593. if (offset >= rp->b_size)
  594. offset -= rp->b_size;
  595. if (copy_from_buf(rp, offset, buf, step_len)) {
  596. mutex_unlock(&rp->fetch_lock);
  597. return -EFAULT;
  598. }
  599. nbytes -= step_len;
  600. buf += step_len;
  601. rp->b_read += step_len;
  602. done += step_len;
  603. }
  604. /*
  605. * Check if whole packet was read, and if so, jump to the next one.
  606. */
  607. if (rp->b_read >= sizeof(struct mon_bin_hdr) + ep->len_cap) {
  608. spin_lock_irqsave(&rp->b_lock, flags);
  609. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  610. spin_unlock_irqrestore(&rp->b_lock, flags);
  611. rp->b_read = 0;
  612. }
  613. mutex_unlock(&rp->fetch_lock);
  614. return done;
  615. }
  616. /*
  617. * Remove at most nevents from chunked buffer.
  618. * Returns the number of removed events.
  619. */
  620. static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
  621. {
  622. unsigned long flags;
  623. struct mon_bin_hdr *ep;
  624. int i;
  625. mutex_lock(&rp->fetch_lock);
  626. spin_lock_irqsave(&rp->b_lock, flags);
  627. for (i = 0; i < nevents; ++i) {
  628. if (MON_RING_EMPTY(rp))
  629. break;
  630. ep = MON_OFF2HDR(rp, rp->b_out);
  631. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  632. }
  633. spin_unlock_irqrestore(&rp->b_lock, flags);
  634. rp->b_read = 0;
  635. mutex_unlock(&rp->fetch_lock);
  636. return i;
  637. }
  638. /*
  639. * Fetch at most max event offsets into the buffer and put them into vec.
  640. * The events are usually freed later with mon_bin_flush.
  641. * Return the effective number of events fetched.
  642. */
  643. static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
  644. u32 __user *vec, unsigned int max)
  645. {
  646. unsigned int cur_out;
  647. unsigned int bytes, avail;
  648. unsigned int size;
  649. unsigned int nevents;
  650. struct mon_bin_hdr *ep;
  651. unsigned long flags;
  652. int rc;
  653. mutex_lock(&rp->fetch_lock);
  654. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  655. mutex_unlock(&rp->fetch_lock);
  656. return rc;
  657. }
  658. spin_lock_irqsave(&rp->b_lock, flags);
  659. avail = rp->b_cnt;
  660. spin_unlock_irqrestore(&rp->b_lock, flags);
  661. cur_out = rp->b_out;
  662. nevents = 0;
  663. bytes = 0;
  664. while (bytes < avail) {
  665. if (nevents >= max)
  666. break;
  667. ep = MON_OFF2HDR(rp, cur_out);
  668. if (put_user(cur_out, &vec[nevents])) {
  669. mutex_unlock(&rp->fetch_lock);
  670. return -EFAULT;
  671. }
  672. nevents++;
  673. size = ep->len_cap + PKT_SIZE;
  674. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  675. if ((cur_out += size) >= rp->b_size)
  676. cur_out -= rp->b_size;
  677. bytes += size;
  678. }
  679. mutex_unlock(&rp->fetch_lock);
  680. return nevents;
  681. }
  682. /*
  683. * Count events. This is almost the same as the above mon_bin_fetch,
  684. * only we do not store offsets into user vector, and we have no limit.
  685. */
  686. static int mon_bin_queued(struct mon_reader_bin *rp)
  687. {
  688. unsigned int cur_out;
  689. unsigned int bytes, avail;
  690. unsigned int size;
  691. unsigned int nevents;
  692. struct mon_bin_hdr *ep;
  693. unsigned long flags;
  694. mutex_lock(&rp->fetch_lock);
  695. spin_lock_irqsave(&rp->b_lock, flags);
  696. avail = rp->b_cnt;
  697. spin_unlock_irqrestore(&rp->b_lock, flags);
  698. cur_out = rp->b_out;
  699. nevents = 0;
  700. bytes = 0;
  701. while (bytes < avail) {
  702. ep = MON_OFF2HDR(rp, cur_out);
  703. nevents++;
  704. size = ep->len_cap + PKT_SIZE;
  705. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  706. if ((cur_out += size) >= rp->b_size)
  707. cur_out -= rp->b_size;
  708. bytes += size;
  709. }
  710. mutex_unlock(&rp->fetch_lock);
  711. return nevents;
  712. }
  713. /*
  714. */
  715. static int mon_bin_ioctl(struct inode *inode, struct file *file,
  716. unsigned int cmd, unsigned long arg)
  717. {
  718. struct mon_reader_bin *rp = file->private_data;
  719. // struct mon_bus* mbus = rp->r.m_bus;
  720. int ret = 0;
  721. struct mon_bin_hdr *ep;
  722. unsigned long flags;
  723. switch (cmd) {
  724. case MON_IOCQ_URB_LEN:
  725. /*
  726. * N.B. This only returns the size of data, without the header.
  727. */
  728. spin_lock_irqsave(&rp->b_lock, flags);
  729. if (!MON_RING_EMPTY(rp)) {
  730. ep = MON_OFF2HDR(rp, rp->b_out);
  731. ret = ep->len_cap;
  732. }
  733. spin_unlock_irqrestore(&rp->b_lock, flags);
  734. break;
  735. case MON_IOCQ_RING_SIZE:
  736. ret = rp->b_size;
  737. break;
  738. case MON_IOCT_RING_SIZE:
  739. /*
  740. * Changing the buffer size will flush it's contents; the new
  741. * buffer is allocated before releasing the old one to be sure
  742. * the device will stay functional also in case of memory
  743. * pressure.
  744. */
  745. {
  746. int size;
  747. struct mon_pgmap *vec;
  748. if (arg < BUFF_MIN || arg > BUFF_MAX)
  749. return -EINVAL;
  750. size = CHUNK_ALIGN(arg);
  751. if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
  752. GFP_KERNEL)) == NULL) {
  753. ret = -ENOMEM;
  754. break;
  755. }
  756. ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
  757. if (ret < 0) {
  758. kfree(vec);
  759. break;
  760. }
  761. mutex_lock(&rp->fetch_lock);
  762. spin_lock_irqsave(&rp->b_lock, flags);
  763. mon_free_buff(rp->b_vec, size/CHUNK_SIZE);
  764. kfree(rp->b_vec);
  765. rp->b_vec = vec;
  766. rp->b_size = size;
  767. rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
  768. rp->cnt_lost = 0;
  769. spin_unlock_irqrestore(&rp->b_lock, flags);
  770. mutex_unlock(&rp->fetch_lock);
  771. }
  772. break;
  773. case MON_IOCH_MFLUSH:
  774. ret = mon_bin_flush(rp, arg);
  775. break;
  776. case MON_IOCX_GET:
  777. {
  778. struct mon_bin_get getb;
  779. if (copy_from_user(&getb, (void __user *)arg,
  780. sizeof(struct mon_bin_get)))
  781. return -EFAULT;
  782. if (getb.alloc > 0x10000000) /* Want to cast to u32 */
  783. return -EINVAL;
  784. ret = mon_bin_get_event(file, rp,
  785. getb.hdr, getb.data, (unsigned int)getb.alloc);
  786. }
  787. break;
  788. case MON_IOCX_MFETCH:
  789. {
  790. struct mon_bin_mfetch mfetch;
  791. struct mon_bin_mfetch __user *uptr;
  792. uptr = (struct mon_bin_mfetch __user *)arg;
  793. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  794. return -EFAULT;
  795. if (mfetch.nflush) {
  796. ret = mon_bin_flush(rp, mfetch.nflush);
  797. if (ret < 0)
  798. return ret;
  799. if (put_user(ret, &uptr->nflush))
  800. return -EFAULT;
  801. }
  802. ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
  803. if (ret < 0)
  804. return ret;
  805. if (put_user(ret, &uptr->nfetch))
  806. return -EFAULT;
  807. ret = 0;
  808. }
  809. break;
  810. case MON_IOCG_STATS: {
  811. struct mon_bin_stats __user *sp;
  812. unsigned int nevents;
  813. unsigned int ndropped;
  814. spin_lock_irqsave(&rp->b_lock, flags);
  815. ndropped = rp->cnt_lost;
  816. rp->cnt_lost = 0;
  817. spin_unlock_irqrestore(&rp->b_lock, flags);
  818. nevents = mon_bin_queued(rp);
  819. sp = (struct mon_bin_stats __user *)arg;
  820. if (put_user(rp->cnt_lost, &sp->dropped))
  821. return -EFAULT;
  822. if (put_user(nevents, &sp->queued))
  823. return -EFAULT;
  824. }
  825. break;
  826. default:
  827. return -ENOTTY;
  828. }
  829. return ret;
  830. }
  831. #ifdef CONFIG_COMPAT
  832. static long mon_bin_compat_ioctl(struct file *file,
  833. unsigned int cmd, unsigned long arg)
  834. {
  835. struct mon_reader_bin *rp = file->private_data;
  836. int ret;
  837. switch (cmd) {
  838. case MON_IOCX_GET32: {
  839. struct mon_bin_get32 getb;
  840. if (copy_from_user(&getb, (void __user *)arg,
  841. sizeof(struct mon_bin_get32)))
  842. return -EFAULT;
  843. ret = mon_bin_get_event(file, rp,
  844. compat_ptr(getb.hdr32), compat_ptr(getb.data32),
  845. getb.alloc32);
  846. if (ret < 0)
  847. return ret;
  848. }
  849. return 0;
  850. case MON_IOCX_MFETCH32:
  851. {
  852. struct mon_bin_mfetch32 mfetch;
  853. struct mon_bin_mfetch32 __user *uptr;
  854. uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
  855. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  856. return -EFAULT;
  857. if (mfetch.nflush32) {
  858. ret = mon_bin_flush(rp, mfetch.nflush32);
  859. if (ret < 0)
  860. return ret;
  861. if (put_user(ret, &uptr->nflush32))
  862. return -EFAULT;
  863. }
  864. ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
  865. mfetch.nfetch32);
  866. if (ret < 0)
  867. return ret;
  868. if (put_user(ret, &uptr->nfetch32))
  869. return -EFAULT;
  870. }
  871. return 0;
  872. case MON_IOCG_STATS:
  873. return mon_bin_ioctl(NULL, file, cmd,
  874. (unsigned long) compat_ptr(arg));
  875. case MON_IOCQ_URB_LEN:
  876. case MON_IOCQ_RING_SIZE:
  877. case MON_IOCT_RING_SIZE:
  878. case MON_IOCH_MFLUSH:
  879. return mon_bin_ioctl(NULL, file, cmd, arg);
  880. default:
  881. ;
  882. }
  883. return -ENOTTY;
  884. }
  885. #endif /* CONFIG_COMPAT */
  886. static unsigned int
  887. mon_bin_poll(struct file *file, struct poll_table_struct *wait)
  888. {
  889. struct mon_reader_bin *rp = file->private_data;
  890. unsigned int mask = 0;
  891. unsigned long flags;
  892. if (file->f_mode & FMODE_READ)
  893. poll_wait(file, &rp->b_wait, wait);
  894. spin_lock_irqsave(&rp->b_lock, flags);
  895. if (!MON_RING_EMPTY(rp))
  896. mask |= POLLIN | POLLRDNORM; /* readable */
  897. spin_unlock_irqrestore(&rp->b_lock, flags);
  898. return mask;
  899. }
  900. /*
  901. * open and close: just keep track of how many times the device is
  902. * mapped, to use the proper memory allocation function.
  903. */
  904. static void mon_bin_vma_open(struct vm_area_struct *vma)
  905. {
  906. struct mon_reader_bin *rp = vma->vm_private_data;
  907. rp->mmap_active++;
  908. }
  909. static void mon_bin_vma_close(struct vm_area_struct *vma)
  910. {
  911. struct mon_reader_bin *rp = vma->vm_private_data;
  912. rp->mmap_active--;
  913. }
  914. /*
  915. * Map ring pages to user space.
  916. */
  917. static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  918. {
  919. struct mon_reader_bin *rp = vma->vm_private_data;
  920. unsigned long offset, chunk_idx;
  921. struct page *pageptr;
  922. offset = vmf->pgoff << PAGE_SHIFT;
  923. if (offset >= rp->b_size)
  924. return VM_FAULT_SIGBUS;
  925. chunk_idx = offset / CHUNK_SIZE;
  926. pageptr = rp->b_vec[chunk_idx].pg;
  927. get_page(pageptr);
  928. vmf->page = pageptr;
  929. return 0;
  930. }
  931. static struct vm_operations_struct mon_bin_vm_ops = {
  932. .open = mon_bin_vma_open,
  933. .close = mon_bin_vma_close,
  934. .fault = mon_bin_vma_fault,
  935. };
  936. static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
  937. {
  938. /* don't do anything here: "fault" will set up page table entries */
  939. vma->vm_ops = &mon_bin_vm_ops;
  940. vma->vm_flags |= VM_RESERVED;
  941. vma->vm_private_data = filp->private_data;
  942. mon_bin_vma_open(vma);
  943. return 0;
  944. }
  945. static const struct file_operations mon_fops_binary = {
  946. .owner = THIS_MODULE,
  947. .open = mon_bin_open,
  948. .llseek = no_llseek,
  949. .read = mon_bin_read,
  950. /* .write = mon_text_write, */
  951. .poll = mon_bin_poll,
  952. .ioctl = mon_bin_ioctl,
  953. #ifdef CONFIG_COMPAT
  954. .compat_ioctl = mon_bin_compat_ioctl,
  955. #endif
  956. .release = mon_bin_release,
  957. .mmap = mon_bin_mmap,
  958. };
  959. static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
  960. {
  961. DECLARE_WAITQUEUE(waita, current);
  962. unsigned long flags;
  963. add_wait_queue(&rp->b_wait, &waita);
  964. set_current_state(TASK_INTERRUPTIBLE);
  965. spin_lock_irqsave(&rp->b_lock, flags);
  966. while (MON_RING_EMPTY(rp)) {
  967. spin_unlock_irqrestore(&rp->b_lock, flags);
  968. if (file->f_flags & O_NONBLOCK) {
  969. set_current_state(TASK_RUNNING);
  970. remove_wait_queue(&rp->b_wait, &waita);
  971. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  972. }
  973. schedule();
  974. if (signal_pending(current)) {
  975. remove_wait_queue(&rp->b_wait, &waita);
  976. return -EINTR;
  977. }
  978. set_current_state(TASK_INTERRUPTIBLE);
  979. spin_lock_irqsave(&rp->b_lock, flags);
  980. }
  981. spin_unlock_irqrestore(&rp->b_lock, flags);
  982. set_current_state(TASK_RUNNING);
  983. remove_wait_queue(&rp->b_wait, &waita);
  984. return 0;
  985. }
  986. static int mon_alloc_buff(struct mon_pgmap *map, int npages)
  987. {
  988. int n;
  989. unsigned long vaddr;
  990. for (n = 0; n < npages; n++) {
  991. vaddr = get_zeroed_page(GFP_KERNEL);
  992. if (vaddr == 0) {
  993. while (n-- != 0)
  994. free_page((unsigned long) map[n].ptr);
  995. return -ENOMEM;
  996. }
  997. map[n].ptr = (unsigned char *) vaddr;
  998. map[n].pg = virt_to_page(vaddr);
  999. }
  1000. return 0;
  1001. }
  1002. static void mon_free_buff(struct mon_pgmap *map, int npages)
  1003. {
  1004. int n;
  1005. for (n = 0; n < npages; n++)
  1006. free_page((unsigned long) map[n].ptr);
  1007. }
  1008. int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  1009. {
  1010. struct device *dev;
  1011. unsigned minor = ubus? ubus->busnum: 0;
  1012. if (minor >= MON_BIN_MAX_MINOR)
  1013. return 0;
  1014. dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
  1015. MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
  1016. "usbmon%d", minor);
  1017. if (IS_ERR(dev))
  1018. return 0;
  1019. mbus->classdev = dev;
  1020. return 1;
  1021. }
  1022. void mon_bin_del(struct mon_bus *mbus)
  1023. {
  1024. device_destroy(mon_bin_class, mbus->classdev->devt);
  1025. }
  1026. int __init mon_bin_init(void)
  1027. {
  1028. int rc;
  1029. mon_bin_class = class_create(THIS_MODULE, "usbmon");
  1030. if (IS_ERR(mon_bin_class)) {
  1031. rc = PTR_ERR(mon_bin_class);
  1032. goto err_class;
  1033. }
  1034. rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
  1035. if (rc < 0)
  1036. goto err_dev;
  1037. cdev_init(&mon_bin_cdev, &mon_fops_binary);
  1038. mon_bin_cdev.owner = THIS_MODULE;
  1039. rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
  1040. if (rc < 0)
  1041. goto err_add;
  1042. return 0;
  1043. err_add:
  1044. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1045. err_dev:
  1046. class_destroy(mon_bin_class);
  1047. err_class:
  1048. return rc;
  1049. }
  1050. void mon_bin_exit(void)
  1051. {
  1052. cdev_del(&mon_bin_cdev);
  1053. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1054. class_destroy(mon_bin_class);
  1055. }