mon_bin.c 28 KB

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