mon_bin.c 28 KB

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