mon_bin.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  891. {
  892. struct mon_reader_bin *rp = vma->vm_private_data;
  893. unsigned long offset, chunk_idx;
  894. struct page *pageptr;
  895. offset = vmf->pgoff << PAGE_SHIFT;
  896. if (offset >= rp->b_size)
  897. return VM_FAULT_SIGBUS;
  898. chunk_idx = offset / CHUNK_SIZE;
  899. pageptr = rp->b_vec[chunk_idx].pg;
  900. get_page(pageptr);
  901. vmf->page = pageptr;
  902. return 0;
  903. }
  904. static struct vm_operations_struct mon_bin_vm_ops = {
  905. .open = mon_bin_vma_open,
  906. .close = mon_bin_vma_close,
  907. .fault = mon_bin_vma_fault,
  908. };
  909. static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
  910. {
  911. /* don't do anything here: "fault" will set up page table entries */
  912. vma->vm_ops = &mon_bin_vm_ops;
  913. vma->vm_flags |= VM_RESERVED;
  914. vma->vm_private_data = filp->private_data;
  915. mon_bin_vma_open(vma);
  916. return 0;
  917. }
  918. static const struct file_operations mon_fops_binary = {
  919. .owner = THIS_MODULE,
  920. .open = mon_bin_open,
  921. .llseek = no_llseek,
  922. .read = mon_bin_read,
  923. /* .write = mon_text_write, */
  924. .poll = mon_bin_poll,
  925. .ioctl = mon_bin_ioctl,
  926. .release = mon_bin_release,
  927. .mmap = mon_bin_mmap,
  928. };
  929. static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
  930. {
  931. DECLARE_WAITQUEUE(waita, current);
  932. unsigned long flags;
  933. add_wait_queue(&rp->b_wait, &waita);
  934. set_current_state(TASK_INTERRUPTIBLE);
  935. spin_lock_irqsave(&rp->b_lock, flags);
  936. while (MON_RING_EMPTY(rp)) {
  937. spin_unlock_irqrestore(&rp->b_lock, flags);
  938. if (file->f_flags & O_NONBLOCK) {
  939. set_current_state(TASK_RUNNING);
  940. remove_wait_queue(&rp->b_wait, &waita);
  941. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  942. }
  943. schedule();
  944. if (signal_pending(current)) {
  945. remove_wait_queue(&rp->b_wait, &waita);
  946. return -EINTR;
  947. }
  948. set_current_state(TASK_INTERRUPTIBLE);
  949. spin_lock_irqsave(&rp->b_lock, flags);
  950. }
  951. spin_unlock_irqrestore(&rp->b_lock, flags);
  952. set_current_state(TASK_RUNNING);
  953. remove_wait_queue(&rp->b_wait, &waita);
  954. return 0;
  955. }
  956. static int mon_alloc_buff(struct mon_pgmap *map, int npages)
  957. {
  958. int n;
  959. unsigned long vaddr;
  960. for (n = 0; n < npages; n++) {
  961. vaddr = get_zeroed_page(GFP_KERNEL);
  962. if (vaddr == 0) {
  963. while (n-- != 0)
  964. free_page((unsigned long) map[n].ptr);
  965. return -ENOMEM;
  966. }
  967. map[n].ptr = (unsigned char *) vaddr;
  968. map[n].pg = virt_to_page(vaddr);
  969. }
  970. return 0;
  971. }
  972. static void mon_free_buff(struct mon_pgmap *map, int npages)
  973. {
  974. int n;
  975. for (n = 0; n < npages; n++)
  976. free_page((unsigned long) map[n].ptr);
  977. }
  978. int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  979. {
  980. struct device *dev;
  981. unsigned minor = ubus? ubus->busnum: 0;
  982. if (minor >= MON_BIN_MAX_MINOR)
  983. return 0;
  984. dev = device_create(mon_bin_class, ubus? ubus->controller: NULL,
  985. MKDEV(MAJOR(mon_bin_dev0), minor), "usbmon%d", minor);
  986. if (IS_ERR(dev))
  987. return 0;
  988. mbus->classdev = dev;
  989. return 1;
  990. }
  991. void mon_bin_del(struct mon_bus *mbus)
  992. {
  993. device_destroy(mon_bin_class, mbus->classdev->devt);
  994. }
  995. int __init mon_bin_init(void)
  996. {
  997. int rc;
  998. mon_bin_class = class_create(THIS_MODULE, "usbmon");
  999. if (IS_ERR(mon_bin_class)) {
  1000. rc = PTR_ERR(mon_bin_class);
  1001. goto err_class;
  1002. }
  1003. rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
  1004. if (rc < 0)
  1005. goto err_dev;
  1006. cdev_init(&mon_bin_cdev, &mon_fops_binary);
  1007. mon_bin_cdev.owner = THIS_MODULE;
  1008. rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
  1009. if (rc < 0)
  1010. goto err_add;
  1011. return 0;
  1012. err_add:
  1013. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1014. err_dev:
  1015. class_destroy(mon_bin_class);
  1016. err_class:
  1017. return rc;
  1018. }
  1019. void mon_bin_exit(void)
  1020. {
  1021. cdev_del(&mon_bin_cdev);
  1022. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1023. class_destroy(mon_bin_class);
  1024. }