mon_bin.c 28 KB

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