mon_bin.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The USB Monitor, inspired by Dave Harding's USBMon.
  4. *
  5. * This is a binary format reader.
  6. *
  7. * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
  8. * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/types.h>
  13. #include <linux/fs.h>
  14. #include <linux/cdev.h>
  15. #include <linux/export.h>
  16. #include <linux/usb.h>
  17. #include <linux/poll.h>
  18. #include <linux/compat.h>
  19. #include <linux/mm.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/slab.h>
  22. #include <linux/time64.h>
  23. #include <linux/uaccess.h>
  24. #include "usb_mon.h"
  25. /*
  26. * Defined by USB 2.0 clause 9.3, table 9.2.
  27. */
  28. #define SETUP_LEN 8
  29. /* ioctl macros */
  30. #define MON_IOC_MAGIC 0x92
  31. #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
  32. /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
  33. #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
  34. #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
  35. #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
  36. #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
  37. #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
  38. #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
  39. /* #9 was MON_IOCT_SETAPI */
  40. #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
  41. #ifdef CONFIG_COMPAT
  42. #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
  43. #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
  44. #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
  45. #endif
  46. /*
  47. * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
  48. * But it's all right. Just use a simple way to make sure the chunk is never
  49. * smaller than a page.
  50. *
  51. * N.B. An application does not know our chunk size.
  52. *
  53. * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
  54. * page-sized chunks for the time being.
  55. */
  56. #define CHUNK_SIZE PAGE_SIZE
  57. #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
  58. /*
  59. * The magic limit was calculated so that it allows the monitoring
  60. * application to pick data once in two ticks. This way, another application,
  61. * which presumably drives the bus, gets to hog CPU, yet we collect our data.
  62. * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
  63. * enormous overhead built into the bus protocol, so we need about 1000 KB.
  64. *
  65. * This is still too much for most cases, where we just snoop a few
  66. * descriptor fetches for enumeration. So, the default is a "reasonable"
  67. * amount for systems with HZ=250 and incomplete bus saturation.
  68. *
  69. * XXX What about multi-megabyte URBs which take minutes to transfer?
  70. */
  71. #define BUFF_MAX CHUNK_ALIGN(1200*1024)
  72. #define BUFF_DFL CHUNK_ALIGN(300*1024)
  73. #define BUFF_MIN CHUNK_ALIGN(8*1024)
  74. /*
  75. * The per-event API header (2 per URB).
  76. *
  77. * This structure is seen in userland as defined by the documentation.
  78. */
  79. struct mon_bin_hdr {
  80. u64 id; /* URB ID - from submission to callback */
  81. unsigned char type; /* Same as in text API; extensible. */
  82. unsigned char xfer_type; /* ISO, Intr, Control, Bulk */
  83. unsigned char epnum; /* Endpoint number and transfer direction */
  84. unsigned char devnum; /* Device address */
  85. unsigned short busnum; /* Bus number */
  86. char flag_setup;
  87. char flag_data;
  88. s64 ts_sec; /* ktime_get_real_ts64 */
  89. s32 ts_usec; /* ktime_get_real_ts64 */
  90. int status;
  91. unsigned int len_urb; /* Length of data (submitted or actual) */
  92. unsigned int len_cap; /* Delivered length */
  93. union {
  94. unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
  95. struct iso_rec {
  96. int error_count;
  97. int numdesc;
  98. } iso;
  99. } s;
  100. int interval;
  101. int start_frame;
  102. unsigned int xfer_flags;
  103. unsigned int ndesc; /* Actual number of ISO descriptors */
  104. };
  105. /*
  106. * ISO vector, packed into the head of data stream.
  107. * This has to take 16 bytes to make sure that the end of buffer
  108. * wrap is not happening in the middle of a descriptor.
  109. */
  110. struct mon_bin_isodesc {
  111. int iso_status;
  112. unsigned int iso_off;
  113. unsigned int iso_len;
  114. u32 _pad;
  115. };
  116. /* per file statistic */
  117. struct mon_bin_stats {
  118. u32 queued;
  119. u32 dropped;
  120. };
  121. struct mon_bin_get {
  122. struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
  123. void __user *data;
  124. size_t alloc; /* Length of data (can be zero) */
  125. };
  126. struct mon_bin_mfetch {
  127. u32 __user *offvec; /* Vector of events fetched */
  128. u32 nfetch; /* Number of events to fetch (out: fetched) */
  129. u32 nflush; /* Number of events to flush */
  130. };
  131. #ifdef CONFIG_COMPAT
  132. struct mon_bin_get32 {
  133. u32 hdr32;
  134. u32 data32;
  135. u32 alloc32;
  136. };
  137. struct mon_bin_mfetch32 {
  138. u32 offvec32;
  139. u32 nfetch32;
  140. u32 nflush32;
  141. };
  142. #endif
  143. /* Having these two values same prevents wrapping of the mon_bin_hdr */
  144. #define PKT_ALIGN 64
  145. #define PKT_SIZE 64
  146. #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
  147. #define PKT_SZ_API1 64 /* API 1 size: extra fields */
  148. #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
  149. /* max number of USB bus supported */
  150. #define MON_BIN_MAX_MINOR 128
  151. /*
  152. * The buffer: map of used pages.
  153. */
  154. struct mon_pgmap {
  155. struct page *pg;
  156. unsigned char *ptr; /* XXX just use page_to_virt everywhere? */
  157. };
  158. /*
  159. * This gets associated with an open file struct.
  160. */
  161. struct mon_reader_bin {
  162. /* The buffer: one per open. */
  163. spinlock_t b_lock; /* Protect b_cnt, b_in */
  164. unsigned int b_size; /* Current size of the buffer - bytes */
  165. unsigned int b_cnt; /* Bytes used */
  166. unsigned int b_in, b_out; /* Offsets into buffer - bytes */
  167. unsigned int b_read; /* Amount of read data in curr. pkt. */
  168. struct mon_pgmap *b_vec; /* The map array */
  169. wait_queue_head_t b_wait; /* Wait for data here */
  170. struct mutex fetch_lock; /* Protect b_read, b_out */
  171. int mmap_active;
  172. /* A list of these is needed for "bus 0". Some time later. */
  173. struct mon_reader r;
  174. /* Stats */
  175. unsigned int cnt_lost;
  176. };
  177. static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
  178. unsigned int offset)
  179. {
  180. return (struct mon_bin_hdr *)
  181. (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
  182. }
  183. #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
  184. static unsigned char xfer_to_pipe[4] = {
  185. PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
  186. };
  187. static struct class *mon_bin_class;
  188. static dev_t mon_bin_dev0;
  189. static struct cdev mon_bin_cdev;
  190. static void mon_buff_area_fill(const struct mon_reader_bin *rp,
  191. unsigned int offset, unsigned int size);
  192. static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
  193. static int mon_alloc_buff(struct mon_pgmap *map, int npages);
  194. static void mon_free_buff(struct mon_pgmap *map, int npages);
  195. /*
  196. * This is a "chunked memcpy". It does not manipulate any counters.
  197. */
  198. static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
  199. unsigned int off, const unsigned char *from, unsigned int length)
  200. {
  201. unsigned int step_len;
  202. unsigned char *buf;
  203. unsigned int in_page;
  204. while (length) {
  205. /*
  206. * Determine step_len.
  207. */
  208. step_len = length;
  209. in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
  210. if (in_page < step_len)
  211. step_len = in_page;
  212. /*
  213. * Copy data and advance pointers.
  214. */
  215. buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
  216. memcpy(buf, from, step_len);
  217. if ((off += step_len) >= this->b_size) off = 0;
  218. from += step_len;
  219. length -= step_len;
  220. }
  221. return off;
  222. }
  223. /*
  224. * This is a little worse than the above because it's "chunked copy_to_user".
  225. * The return value is an error code, not an offset.
  226. */
  227. static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
  228. char __user *to, int length)
  229. {
  230. unsigned int step_len;
  231. unsigned char *buf;
  232. unsigned int in_page;
  233. while (length) {
  234. /*
  235. * Determine step_len.
  236. */
  237. step_len = length;
  238. in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
  239. if (in_page < step_len)
  240. step_len = in_page;
  241. /*
  242. * Copy data and advance pointers.
  243. */
  244. buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
  245. if (copy_to_user(to, buf, step_len))
  246. return -EINVAL;
  247. if ((off += step_len) >= this->b_size) off = 0;
  248. to += step_len;
  249. length -= step_len;
  250. }
  251. return 0;
  252. }
  253. /*
  254. * Allocate an (aligned) area in the buffer.
  255. * This is called under b_lock.
  256. * Returns ~0 on failure.
  257. */
  258. static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
  259. unsigned int size)
  260. {
  261. unsigned int offset;
  262. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  263. if (rp->b_cnt + size > rp->b_size)
  264. return ~0;
  265. offset = rp->b_in;
  266. rp->b_cnt += size;
  267. if ((rp->b_in += size) >= rp->b_size)
  268. rp->b_in -= rp->b_size;
  269. return offset;
  270. }
  271. /*
  272. * This is the same thing as mon_buff_area_alloc, only it does not allow
  273. * buffers to wrap. This is needed by applications which pass references
  274. * into mmap-ed buffers up their stacks (libpcap can do that).
  275. *
  276. * Currently, we always have the header stuck with the data, although
  277. * it is not strictly speaking necessary.
  278. *
  279. * When a buffer would wrap, we place a filler packet to mark the space.
  280. */
  281. static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
  282. unsigned int size)
  283. {
  284. unsigned int offset;
  285. unsigned int fill_size;
  286. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  287. if (rp->b_cnt + size > rp->b_size)
  288. return ~0;
  289. if (rp->b_in + size > rp->b_size) {
  290. /*
  291. * This would wrap. Find if we still have space after
  292. * skipping to the end of the buffer. If we do, place
  293. * a filler packet and allocate a new packet.
  294. */
  295. fill_size = rp->b_size - rp->b_in;
  296. if (rp->b_cnt + size + fill_size > rp->b_size)
  297. return ~0;
  298. mon_buff_area_fill(rp, rp->b_in, fill_size);
  299. offset = 0;
  300. rp->b_in = size;
  301. rp->b_cnt += size + fill_size;
  302. } else if (rp->b_in + size == rp->b_size) {
  303. offset = rp->b_in;
  304. rp->b_in = 0;
  305. rp->b_cnt += size;
  306. } else {
  307. offset = rp->b_in;
  308. rp->b_in += size;
  309. rp->b_cnt += size;
  310. }
  311. return offset;
  312. }
  313. /*
  314. * Return a few (kilo-)bytes to the head of the buffer.
  315. * This is used if a data fetch fails.
  316. */
  317. static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
  318. {
  319. /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
  320. rp->b_cnt -= size;
  321. if (rp->b_in < size)
  322. rp->b_in += rp->b_size;
  323. rp->b_in -= size;
  324. }
  325. /*
  326. * This has to be called under both b_lock and fetch_lock, because
  327. * it accesses both b_cnt and b_out.
  328. */
  329. static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
  330. {
  331. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  332. rp->b_cnt -= size;
  333. if ((rp->b_out += size) >= rp->b_size)
  334. rp->b_out -= rp->b_size;
  335. }
  336. static void mon_buff_area_fill(const struct mon_reader_bin *rp,
  337. unsigned int offset, unsigned int size)
  338. {
  339. struct mon_bin_hdr *ep;
  340. ep = MON_OFF2HDR(rp, offset);
  341. memset(ep, 0, PKT_SIZE);
  342. ep->type = '@';
  343. ep->len_cap = size - PKT_SIZE;
  344. }
  345. static inline char mon_bin_get_setup(unsigned char *setupb,
  346. const struct urb *urb, char ev_type)
  347. {
  348. if (urb->setup_packet == NULL)
  349. return 'Z';
  350. memcpy(setupb, urb->setup_packet, SETUP_LEN);
  351. return 0;
  352. }
  353. static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
  354. unsigned int offset, struct urb *urb, unsigned int length,
  355. char *flag)
  356. {
  357. int i;
  358. struct scatterlist *sg;
  359. unsigned int this_len;
  360. *flag = 0;
  361. if (urb->num_sgs == 0) {
  362. if (urb->transfer_buffer == NULL) {
  363. *flag = 'Z';
  364. return length;
  365. }
  366. mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
  367. length = 0;
  368. } else {
  369. /* If IOMMU coalescing occurred, we cannot trust sg_page */
  370. if (urb->transfer_flags & URB_DMA_SG_COMBINED) {
  371. *flag = 'D';
  372. return length;
  373. }
  374. /* Copy up to the first non-addressable segment */
  375. for_each_sg(urb->sg, sg, urb->num_sgs, i) {
  376. if (length == 0 || PageHighMem(sg_page(sg)))
  377. break;
  378. this_len = min_t(unsigned int, sg->length, length);
  379. offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
  380. this_len);
  381. length -= this_len;
  382. }
  383. if (i == 0)
  384. *flag = 'D';
  385. }
  386. return length;
  387. }
  388. /*
  389. * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
  390. * be used to determine the length of the whole contiguous buffer.
  391. */
  392. static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin *rp,
  393. struct urb *urb, unsigned int ndesc)
  394. {
  395. struct usb_iso_packet_descriptor *fp;
  396. unsigned int length;
  397. length = 0;
  398. fp = urb->iso_frame_desc;
  399. while (ndesc-- != 0) {
  400. if (fp->actual_length != 0) {
  401. if (fp->offset + fp->actual_length > length)
  402. length = fp->offset + fp->actual_length;
  403. }
  404. fp++;
  405. }
  406. return length;
  407. }
  408. static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
  409. unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
  410. {
  411. struct mon_bin_isodesc *dp;
  412. struct usb_iso_packet_descriptor *fp;
  413. fp = urb->iso_frame_desc;
  414. while (ndesc-- != 0) {
  415. dp = (struct mon_bin_isodesc *)
  416. (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
  417. dp->iso_status = fp->status;
  418. dp->iso_off = fp->offset;
  419. dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
  420. dp->_pad = 0;
  421. if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
  422. offset = 0;
  423. fp++;
  424. }
  425. }
  426. static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
  427. char ev_type, int status)
  428. {
  429. const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
  430. struct timespec64 ts;
  431. unsigned long flags;
  432. unsigned int urb_length;
  433. unsigned int offset;
  434. unsigned int length;
  435. unsigned int delta;
  436. unsigned int ndesc, lendesc;
  437. unsigned char dir;
  438. struct mon_bin_hdr *ep;
  439. char data_tag = 0;
  440. ktime_get_real_ts64(&ts);
  441. spin_lock_irqsave(&rp->b_lock, flags);
  442. /*
  443. * Find the maximum allowable length, then allocate space.
  444. */
  445. urb_length = (ev_type == 'S') ?
  446. urb->transfer_buffer_length : urb->actual_length;
  447. length = urb_length;
  448. if (usb_endpoint_xfer_isoc(epd)) {
  449. if (urb->number_of_packets < 0) {
  450. ndesc = 0;
  451. } else if (urb->number_of_packets >= ISODESC_MAX) {
  452. ndesc = ISODESC_MAX;
  453. } else {
  454. ndesc = urb->number_of_packets;
  455. }
  456. if (ev_type == 'C' && usb_urb_dir_in(urb))
  457. length = mon_bin_collate_isodesc(rp, urb, ndesc);
  458. } else {
  459. ndesc = 0;
  460. }
  461. lendesc = ndesc*sizeof(struct mon_bin_isodesc);
  462. /* not an issue unless there's a subtle bug in a HCD somewhere */
  463. if (length >= urb->transfer_buffer_length)
  464. length = urb->transfer_buffer_length;
  465. if (length >= rp->b_size/5)
  466. length = rp->b_size/5;
  467. if (usb_urb_dir_in(urb)) {
  468. if (ev_type == 'S') {
  469. length = 0;
  470. data_tag = '<';
  471. }
  472. /* Cannot rely on endpoint number in case of control ep.0 */
  473. dir = USB_DIR_IN;
  474. } else {
  475. if (ev_type == 'C') {
  476. length = 0;
  477. data_tag = '>';
  478. }
  479. dir = 0;
  480. }
  481. if (rp->mmap_active) {
  482. offset = mon_buff_area_alloc_contiguous(rp,
  483. length + PKT_SIZE + lendesc);
  484. } else {
  485. offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
  486. }
  487. if (offset == ~0) {
  488. rp->cnt_lost++;
  489. spin_unlock_irqrestore(&rp->b_lock, flags);
  490. return;
  491. }
  492. ep = MON_OFF2HDR(rp, offset);
  493. if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
  494. /*
  495. * Fill the allocated area.
  496. */
  497. memset(ep, 0, PKT_SIZE);
  498. ep->type = ev_type;
  499. ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
  500. ep->epnum = dir | usb_endpoint_num(epd);
  501. ep->devnum = urb->dev->devnum;
  502. ep->busnum = urb->dev->bus->busnum;
  503. ep->id = (unsigned long) urb;
  504. ep->ts_sec = ts.tv_sec;
  505. ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
  506. ep->status = status;
  507. ep->len_urb = urb_length;
  508. ep->len_cap = length + lendesc;
  509. ep->xfer_flags = urb->transfer_flags;
  510. if (usb_endpoint_xfer_int(epd)) {
  511. ep->interval = urb->interval;
  512. } else if (usb_endpoint_xfer_isoc(epd)) {
  513. ep->interval = urb->interval;
  514. ep->start_frame = urb->start_frame;
  515. ep->s.iso.error_count = urb->error_count;
  516. ep->s.iso.numdesc = urb->number_of_packets;
  517. }
  518. if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
  519. ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
  520. } else {
  521. ep->flag_setup = '-';
  522. }
  523. if (ndesc != 0) {
  524. ep->ndesc = ndesc;
  525. mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
  526. if ((offset += lendesc) >= rp->b_size)
  527. offset -= rp->b_size;
  528. }
  529. if (length != 0) {
  530. length = mon_bin_get_data(rp, offset, urb, length,
  531. &ep->flag_data);
  532. if (length > 0) {
  533. delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  534. ep->len_cap -= length;
  535. delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  536. mon_buff_area_shrink(rp, delta);
  537. }
  538. } else {
  539. ep->flag_data = data_tag;
  540. }
  541. spin_unlock_irqrestore(&rp->b_lock, flags);
  542. wake_up(&rp->b_wait);
  543. }
  544. static void mon_bin_submit(void *data, struct urb *urb)
  545. {
  546. struct mon_reader_bin *rp = data;
  547. mon_bin_event(rp, urb, 'S', -EINPROGRESS);
  548. }
  549. static void mon_bin_complete(void *data, struct urb *urb, int status)
  550. {
  551. struct mon_reader_bin *rp = data;
  552. mon_bin_event(rp, urb, 'C', status);
  553. }
  554. static void mon_bin_error(void *data, struct urb *urb, int error)
  555. {
  556. struct mon_reader_bin *rp = data;
  557. struct timespec64 ts;
  558. unsigned long flags;
  559. unsigned int offset;
  560. struct mon_bin_hdr *ep;
  561. ktime_get_real_ts64(&ts);
  562. spin_lock_irqsave(&rp->b_lock, flags);
  563. offset = mon_buff_area_alloc(rp, PKT_SIZE);
  564. if (offset == ~0) {
  565. /* Not incrementing cnt_lost. Just because. */
  566. spin_unlock_irqrestore(&rp->b_lock, flags);
  567. return;
  568. }
  569. ep = MON_OFF2HDR(rp, offset);
  570. memset(ep, 0, PKT_SIZE);
  571. ep->type = 'E';
  572. ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
  573. ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
  574. ep->epnum |= usb_endpoint_num(&urb->ep->desc);
  575. ep->devnum = urb->dev->devnum;
  576. ep->busnum = urb->dev->bus->busnum;
  577. ep->id = (unsigned long) urb;
  578. ep->ts_sec = ts.tv_sec;
  579. ep->ts_usec = ts.tv_nsec / NSEC_PER_USEC;
  580. ep->status = error;
  581. ep->flag_setup = '-';
  582. ep->flag_data = 'E';
  583. spin_unlock_irqrestore(&rp->b_lock, flags);
  584. wake_up(&rp->b_wait);
  585. }
  586. static int mon_bin_open(struct inode *inode, struct file *file)
  587. {
  588. struct mon_bus *mbus;
  589. struct mon_reader_bin *rp;
  590. size_t size;
  591. int rc;
  592. mutex_lock(&mon_lock);
  593. mbus = mon_bus_lookup(iminor(inode));
  594. if (mbus == NULL) {
  595. mutex_unlock(&mon_lock);
  596. return -ENODEV;
  597. }
  598. if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
  599. printk(KERN_ERR TAG ": consistency error on open\n");
  600. mutex_unlock(&mon_lock);
  601. return -ENODEV;
  602. }
  603. rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
  604. if (rp == NULL) {
  605. rc = -ENOMEM;
  606. goto err_alloc;
  607. }
  608. spin_lock_init(&rp->b_lock);
  609. init_waitqueue_head(&rp->b_wait);
  610. mutex_init(&rp->fetch_lock);
  611. rp->b_size = BUFF_DFL;
  612. size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
  613. if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
  614. rc = -ENOMEM;
  615. goto err_allocvec;
  616. }
  617. if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
  618. goto err_allocbuff;
  619. rp->r.m_bus = mbus;
  620. rp->r.r_data = rp;
  621. rp->r.rnf_submit = mon_bin_submit;
  622. rp->r.rnf_error = mon_bin_error;
  623. rp->r.rnf_complete = mon_bin_complete;
  624. mon_reader_add(mbus, &rp->r);
  625. file->private_data = rp;
  626. mutex_unlock(&mon_lock);
  627. return 0;
  628. err_allocbuff:
  629. kfree(rp->b_vec);
  630. err_allocvec:
  631. kfree(rp);
  632. err_alloc:
  633. mutex_unlock(&mon_lock);
  634. return rc;
  635. }
  636. /*
  637. * Extract an event from buffer and copy it to user space.
  638. * Wait if there is no event ready.
  639. * Returns zero or error.
  640. */
  641. static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
  642. struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
  643. void __user *data, unsigned int nbytes)
  644. {
  645. unsigned long flags;
  646. struct mon_bin_hdr *ep;
  647. size_t step_len;
  648. unsigned int offset;
  649. int rc;
  650. mutex_lock(&rp->fetch_lock);
  651. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  652. mutex_unlock(&rp->fetch_lock);
  653. return rc;
  654. }
  655. ep = MON_OFF2HDR(rp, rp->b_out);
  656. if (copy_to_user(hdr, ep, hdrbytes)) {
  657. mutex_unlock(&rp->fetch_lock);
  658. return -EFAULT;
  659. }
  660. step_len = min(ep->len_cap, nbytes);
  661. if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
  662. if (copy_from_buf(rp, offset, data, step_len)) {
  663. mutex_unlock(&rp->fetch_lock);
  664. return -EFAULT;
  665. }
  666. spin_lock_irqsave(&rp->b_lock, flags);
  667. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  668. spin_unlock_irqrestore(&rp->b_lock, flags);
  669. rp->b_read = 0;
  670. mutex_unlock(&rp->fetch_lock);
  671. return 0;
  672. }
  673. static int mon_bin_release(struct inode *inode, struct file *file)
  674. {
  675. struct mon_reader_bin *rp = file->private_data;
  676. struct mon_bus* mbus = rp->r.m_bus;
  677. mutex_lock(&mon_lock);
  678. if (mbus->nreaders <= 0) {
  679. printk(KERN_ERR TAG ": consistency error on close\n");
  680. mutex_unlock(&mon_lock);
  681. return 0;
  682. }
  683. mon_reader_del(mbus, &rp->r);
  684. mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
  685. kfree(rp->b_vec);
  686. kfree(rp);
  687. mutex_unlock(&mon_lock);
  688. return 0;
  689. }
  690. static ssize_t mon_bin_read(struct file *file, char __user *buf,
  691. size_t nbytes, loff_t *ppos)
  692. {
  693. struct mon_reader_bin *rp = file->private_data;
  694. unsigned int hdrbytes = PKT_SZ_API0;
  695. unsigned long flags;
  696. struct mon_bin_hdr *ep;
  697. unsigned int offset;
  698. size_t step_len;
  699. char *ptr;
  700. ssize_t done = 0;
  701. int rc;
  702. mutex_lock(&rp->fetch_lock);
  703. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  704. mutex_unlock(&rp->fetch_lock);
  705. return rc;
  706. }
  707. ep = MON_OFF2HDR(rp, rp->b_out);
  708. if (rp->b_read < hdrbytes) {
  709. step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
  710. ptr = ((char *)ep) + rp->b_read;
  711. if (step_len && copy_to_user(buf, ptr, step_len)) {
  712. mutex_unlock(&rp->fetch_lock);
  713. return -EFAULT;
  714. }
  715. nbytes -= step_len;
  716. buf += step_len;
  717. rp->b_read += step_len;
  718. done += step_len;
  719. }
  720. if (rp->b_read >= hdrbytes) {
  721. step_len = ep->len_cap;
  722. step_len -= rp->b_read - hdrbytes;
  723. if (step_len > nbytes)
  724. step_len = nbytes;
  725. offset = rp->b_out + PKT_SIZE;
  726. offset += rp->b_read - hdrbytes;
  727. if (offset >= rp->b_size)
  728. offset -= rp->b_size;
  729. if (copy_from_buf(rp, offset, buf, step_len)) {
  730. mutex_unlock(&rp->fetch_lock);
  731. return -EFAULT;
  732. }
  733. nbytes -= step_len;
  734. buf += step_len;
  735. rp->b_read += step_len;
  736. done += step_len;
  737. }
  738. /*
  739. * Check if whole packet was read, and if so, jump to the next one.
  740. */
  741. if (rp->b_read >= hdrbytes + ep->len_cap) {
  742. spin_lock_irqsave(&rp->b_lock, flags);
  743. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  744. spin_unlock_irqrestore(&rp->b_lock, flags);
  745. rp->b_read = 0;
  746. }
  747. mutex_unlock(&rp->fetch_lock);
  748. return done;
  749. }
  750. /*
  751. * Remove at most nevents from chunked buffer.
  752. * Returns the number of removed events.
  753. */
  754. static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
  755. {
  756. unsigned long flags;
  757. struct mon_bin_hdr *ep;
  758. int i;
  759. mutex_lock(&rp->fetch_lock);
  760. spin_lock_irqsave(&rp->b_lock, flags);
  761. for (i = 0; i < nevents; ++i) {
  762. if (MON_RING_EMPTY(rp))
  763. break;
  764. ep = MON_OFF2HDR(rp, rp->b_out);
  765. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  766. }
  767. spin_unlock_irqrestore(&rp->b_lock, flags);
  768. rp->b_read = 0;
  769. mutex_unlock(&rp->fetch_lock);
  770. return i;
  771. }
  772. /*
  773. * Fetch at most max event offsets into the buffer and put them into vec.
  774. * The events are usually freed later with mon_bin_flush.
  775. * Return the effective number of events fetched.
  776. */
  777. static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
  778. u32 __user *vec, unsigned int max)
  779. {
  780. unsigned int cur_out;
  781. unsigned int bytes, avail;
  782. unsigned int size;
  783. unsigned int nevents;
  784. struct mon_bin_hdr *ep;
  785. unsigned long flags;
  786. int rc;
  787. mutex_lock(&rp->fetch_lock);
  788. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  789. mutex_unlock(&rp->fetch_lock);
  790. return rc;
  791. }
  792. spin_lock_irqsave(&rp->b_lock, flags);
  793. avail = rp->b_cnt;
  794. spin_unlock_irqrestore(&rp->b_lock, flags);
  795. cur_out = rp->b_out;
  796. nevents = 0;
  797. bytes = 0;
  798. while (bytes < avail) {
  799. if (nevents >= max)
  800. break;
  801. ep = MON_OFF2HDR(rp, cur_out);
  802. if (put_user(cur_out, &vec[nevents])) {
  803. mutex_unlock(&rp->fetch_lock);
  804. return -EFAULT;
  805. }
  806. nevents++;
  807. size = ep->len_cap + PKT_SIZE;
  808. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  809. if ((cur_out += size) >= rp->b_size)
  810. cur_out -= rp->b_size;
  811. bytes += size;
  812. }
  813. mutex_unlock(&rp->fetch_lock);
  814. return nevents;
  815. }
  816. /*
  817. * Count events. This is almost the same as the above mon_bin_fetch,
  818. * only we do not store offsets into user vector, and we have no limit.
  819. */
  820. static int mon_bin_queued(struct mon_reader_bin *rp)
  821. {
  822. unsigned int cur_out;
  823. unsigned int bytes, avail;
  824. unsigned int size;
  825. unsigned int nevents;
  826. struct mon_bin_hdr *ep;
  827. unsigned long flags;
  828. mutex_lock(&rp->fetch_lock);
  829. spin_lock_irqsave(&rp->b_lock, flags);
  830. avail = rp->b_cnt;
  831. spin_unlock_irqrestore(&rp->b_lock, flags);
  832. cur_out = rp->b_out;
  833. nevents = 0;
  834. bytes = 0;
  835. while (bytes < avail) {
  836. ep = MON_OFF2HDR(rp, cur_out);
  837. nevents++;
  838. size = ep->len_cap + PKT_SIZE;
  839. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  840. if ((cur_out += size) >= rp->b_size)
  841. cur_out -= rp->b_size;
  842. bytes += size;
  843. }
  844. mutex_unlock(&rp->fetch_lock);
  845. return nevents;
  846. }
  847. /*
  848. */
  849. static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  850. {
  851. struct mon_reader_bin *rp = file->private_data;
  852. // struct mon_bus* mbus = rp->r.m_bus;
  853. int ret = 0;
  854. struct mon_bin_hdr *ep;
  855. unsigned long flags;
  856. switch (cmd) {
  857. case MON_IOCQ_URB_LEN:
  858. /*
  859. * N.B. This only returns the size of data, without the header.
  860. */
  861. spin_lock_irqsave(&rp->b_lock, flags);
  862. if (!MON_RING_EMPTY(rp)) {
  863. ep = MON_OFF2HDR(rp, rp->b_out);
  864. ret = ep->len_cap;
  865. }
  866. spin_unlock_irqrestore(&rp->b_lock, flags);
  867. break;
  868. case MON_IOCQ_RING_SIZE:
  869. mutex_lock(&rp->fetch_lock);
  870. ret = rp->b_size;
  871. mutex_unlock(&rp->fetch_lock);
  872. break;
  873. case MON_IOCT_RING_SIZE:
  874. /*
  875. * Changing the buffer size will flush it's contents; the new
  876. * buffer is allocated before releasing the old one to be sure
  877. * the device will stay functional also in case of memory
  878. * pressure.
  879. */
  880. {
  881. int size;
  882. struct mon_pgmap *vec;
  883. if (arg < BUFF_MIN || arg > BUFF_MAX)
  884. return -EINVAL;
  885. size = CHUNK_ALIGN(arg);
  886. vec = kcalloc(size / CHUNK_SIZE, sizeof(struct mon_pgmap),
  887. GFP_KERNEL);
  888. if (vec == NULL) {
  889. ret = -ENOMEM;
  890. break;
  891. }
  892. ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
  893. if (ret < 0) {
  894. kfree(vec);
  895. break;
  896. }
  897. mutex_lock(&rp->fetch_lock);
  898. spin_lock_irqsave(&rp->b_lock, flags);
  899. mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
  900. kfree(rp->b_vec);
  901. rp->b_vec = vec;
  902. rp->b_size = size;
  903. rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
  904. rp->cnt_lost = 0;
  905. spin_unlock_irqrestore(&rp->b_lock, flags);
  906. mutex_unlock(&rp->fetch_lock);
  907. }
  908. break;
  909. case MON_IOCH_MFLUSH:
  910. ret = mon_bin_flush(rp, arg);
  911. break;
  912. case MON_IOCX_GET:
  913. case MON_IOCX_GETX:
  914. {
  915. struct mon_bin_get getb;
  916. if (copy_from_user(&getb, (void __user *)arg,
  917. sizeof(struct mon_bin_get)))
  918. return -EFAULT;
  919. if (getb.alloc > 0x10000000) /* Want to cast to u32 */
  920. return -EINVAL;
  921. ret = mon_bin_get_event(file, rp, getb.hdr,
  922. (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
  923. getb.data, (unsigned int)getb.alloc);
  924. }
  925. break;
  926. case MON_IOCX_MFETCH:
  927. {
  928. struct mon_bin_mfetch mfetch;
  929. struct mon_bin_mfetch __user *uptr;
  930. uptr = (struct mon_bin_mfetch __user *)arg;
  931. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  932. return -EFAULT;
  933. if (mfetch.nflush) {
  934. ret = mon_bin_flush(rp, mfetch.nflush);
  935. if (ret < 0)
  936. return ret;
  937. if (put_user(ret, &uptr->nflush))
  938. return -EFAULT;
  939. }
  940. ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
  941. if (ret < 0)
  942. return ret;
  943. if (put_user(ret, &uptr->nfetch))
  944. return -EFAULT;
  945. ret = 0;
  946. }
  947. break;
  948. case MON_IOCG_STATS: {
  949. struct mon_bin_stats __user *sp;
  950. unsigned int nevents;
  951. unsigned int ndropped;
  952. spin_lock_irqsave(&rp->b_lock, flags);
  953. ndropped = rp->cnt_lost;
  954. rp->cnt_lost = 0;
  955. spin_unlock_irqrestore(&rp->b_lock, flags);
  956. nevents = mon_bin_queued(rp);
  957. sp = (struct mon_bin_stats __user *)arg;
  958. if (put_user(ndropped, &sp->dropped))
  959. return -EFAULT;
  960. if (put_user(nevents, &sp->queued))
  961. return -EFAULT;
  962. }
  963. break;
  964. default:
  965. return -ENOTTY;
  966. }
  967. return ret;
  968. }
  969. #ifdef CONFIG_COMPAT
  970. static long mon_bin_compat_ioctl(struct file *file,
  971. unsigned int cmd, unsigned long arg)
  972. {
  973. struct mon_reader_bin *rp = file->private_data;
  974. int ret;
  975. switch (cmd) {
  976. case MON_IOCX_GET32:
  977. case MON_IOCX_GETX32:
  978. {
  979. struct mon_bin_get32 getb;
  980. if (copy_from_user(&getb, (void __user *)arg,
  981. sizeof(struct mon_bin_get32)))
  982. return -EFAULT;
  983. ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
  984. (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
  985. compat_ptr(getb.data32), getb.alloc32);
  986. if (ret < 0)
  987. return ret;
  988. }
  989. return 0;
  990. case MON_IOCX_MFETCH32:
  991. {
  992. struct mon_bin_mfetch32 mfetch;
  993. struct mon_bin_mfetch32 __user *uptr;
  994. uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
  995. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  996. return -EFAULT;
  997. if (mfetch.nflush32) {
  998. ret = mon_bin_flush(rp, mfetch.nflush32);
  999. if (ret < 0)
  1000. return ret;
  1001. if (put_user(ret, &uptr->nflush32))
  1002. return -EFAULT;
  1003. }
  1004. ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
  1005. mfetch.nfetch32);
  1006. if (ret < 0)
  1007. return ret;
  1008. if (put_user(ret, &uptr->nfetch32))
  1009. return -EFAULT;
  1010. }
  1011. return 0;
  1012. case MON_IOCG_STATS:
  1013. return mon_bin_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  1014. case MON_IOCQ_URB_LEN:
  1015. case MON_IOCQ_RING_SIZE:
  1016. case MON_IOCT_RING_SIZE:
  1017. case MON_IOCH_MFLUSH:
  1018. return mon_bin_ioctl(file, cmd, arg);
  1019. default:
  1020. ;
  1021. }
  1022. return -ENOTTY;
  1023. }
  1024. #endif /* CONFIG_COMPAT */
  1025. static __poll_t
  1026. mon_bin_poll(struct file *file, struct poll_table_struct *wait)
  1027. {
  1028. struct mon_reader_bin *rp = file->private_data;
  1029. __poll_t mask = 0;
  1030. unsigned long flags;
  1031. if (file->f_mode & FMODE_READ)
  1032. poll_wait(file, &rp->b_wait, wait);
  1033. spin_lock_irqsave(&rp->b_lock, flags);
  1034. if (!MON_RING_EMPTY(rp))
  1035. mask |= EPOLLIN | EPOLLRDNORM; /* readable */
  1036. spin_unlock_irqrestore(&rp->b_lock, flags);
  1037. return mask;
  1038. }
  1039. /*
  1040. * open and close: just keep track of how many times the device is
  1041. * mapped, to use the proper memory allocation function.
  1042. */
  1043. static void mon_bin_vma_open(struct vm_area_struct *vma)
  1044. {
  1045. struct mon_reader_bin *rp = vma->vm_private_data;
  1046. rp->mmap_active++;
  1047. }
  1048. static void mon_bin_vma_close(struct vm_area_struct *vma)
  1049. {
  1050. struct mon_reader_bin *rp = vma->vm_private_data;
  1051. rp->mmap_active--;
  1052. }
  1053. /*
  1054. * Map ring pages to user space.
  1055. */
  1056. static vm_fault_t mon_bin_vma_fault(struct vm_fault *vmf)
  1057. {
  1058. struct mon_reader_bin *rp = vmf->vma->vm_private_data;
  1059. unsigned long offset, chunk_idx;
  1060. struct page *pageptr;
  1061. mutex_lock(&rp->fetch_lock);
  1062. offset = vmf->pgoff << PAGE_SHIFT;
  1063. if (offset >= rp->b_size) {
  1064. mutex_unlock(&rp->fetch_lock);
  1065. return VM_FAULT_SIGBUS;
  1066. }
  1067. chunk_idx = offset / CHUNK_SIZE;
  1068. pageptr = rp->b_vec[chunk_idx].pg;
  1069. get_page(pageptr);
  1070. mutex_unlock(&rp->fetch_lock);
  1071. vmf->page = pageptr;
  1072. return 0;
  1073. }
  1074. static const struct vm_operations_struct mon_bin_vm_ops = {
  1075. .open = mon_bin_vma_open,
  1076. .close = mon_bin_vma_close,
  1077. .fault = mon_bin_vma_fault,
  1078. };
  1079. static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
  1080. {
  1081. /* don't do anything here: "fault" will set up page table entries */
  1082. vma->vm_ops = &mon_bin_vm_ops;
  1083. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1084. vma->vm_private_data = filp->private_data;
  1085. mon_bin_vma_open(vma);
  1086. return 0;
  1087. }
  1088. static const struct file_operations mon_fops_binary = {
  1089. .owner = THIS_MODULE,
  1090. .open = mon_bin_open,
  1091. .llseek = no_llseek,
  1092. .read = mon_bin_read,
  1093. /* .write = mon_text_write, */
  1094. .poll = mon_bin_poll,
  1095. .unlocked_ioctl = mon_bin_ioctl,
  1096. #ifdef CONFIG_COMPAT
  1097. .compat_ioctl = mon_bin_compat_ioctl,
  1098. #endif
  1099. .release = mon_bin_release,
  1100. .mmap = mon_bin_mmap,
  1101. };
  1102. static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
  1103. {
  1104. DECLARE_WAITQUEUE(waita, current);
  1105. unsigned long flags;
  1106. add_wait_queue(&rp->b_wait, &waita);
  1107. set_current_state(TASK_INTERRUPTIBLE);
  1108. spin_lock_irqsave(&rp->b_lock, flags);
  1109. while (MON_RING_EMPTY(rp)) {
  1110. spin_unlock_irqrestore(&rp->b_lock, flags);
  1111. if (file->f_flags & O_NONBLOCK) {
  1112. set_current_state(TASK_RUNNING);
  1113. remove_wait_queue(&rp->b_wait, &waita);
  1114. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  1115. }
  1116. schedule();
  1117. if (signal_pending(current)) {
  1118. remove_wait_queue(&rp->b_wait, &waita);
  1119. return -EINTR;
  1120. }
  1121. set_current_state(TASK_INTERRUPTIBLE);
  1122. spin_lock_irqsave(&rp->b_lock, flags);
  1123. }
  1124. spin_unlock_irqrestore(&rp->b_lock, flags);
  1125. set_current_state(TASK_RUNNING);
  1126. remove_wait_queue(&rp->b_wait, &waita);
  1127. return 0;
  1128. }
  1129. static int mon_alloc_buff(struct mon_pgmap *map, int npages)
  1130. {
  1131. int n;
  1132. unsigned long vaddr;
  1133. for (n = 0; n < npages; n++) {
  1134. vaddr = get_zeroed_page(GFP_KERNEL);
  1135. if (vaddr == 0) {
  1136. while (n-- != 0)
  1137. free_page((unsigned long) map[n].ptr);
  1138. return -ENOMEM;
  1139. }
  1140. map[n].ptr = (unsigned char *) vaddr;
  1141. map[n].pg = virt_to_page((void *) vaddr);
  1142. }
  1143. return 0;
  1144. }
  1145. static void mon_free_buff(struct mon_pgmap *map, int npages)
  1146. {
  1147. int n;
  1148. for (n = 0; n < npages; n++)
  1149. free_page((unsigned long) map[n].ptr);
  1150. }
  1151. int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  1152. {
  1153. struct device *dev;
  1154. unsigned minor = ubus? ubus->busnum: 0;
  1155. if (minor >= MON_BIN_MAX_MINOR)
  1156. return 0;
  1157. dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
  1158. MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
  1159. "usbmon%d", minor);
  1160. if (IS_ERR(dev))
  1161. return 0;
  1162. mbus->classdev = dev;
  1163. return 1;
  1164. }
  1165. void mon_bin_del(struct mon_bus *mbus)
  1166. {
  1167. device_destroy(mon_bin_class, mbus->classdev->devt);
  1168. }
  1169. int __init mon_bin_init(void)
  1170. {
  1171. int rc;
  1172. mon_bin_class = class_create(THIS_MODULE, "usbmon");
  1173. if (IS_ERR(mon_bin_class)) {
  1174. rc = PTR_ERR(mon_bin_class);
  1175. goto err_class;
  1176. }
  1177. rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
  1178. if (rc < 0)
  1179. goto err_dev;
  1180. cdev_init(&mon_bin_cdev, &mon_fops_binary);
  1181. mon_bin_cdev.owner = THIS_MODULE;
  1182. rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
  1183. if (rc < 0)
  1184. goto err_add;
  1185. return 0;
  1186. err_add:
  1187. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1188. err_dev:
  1189. class_destroy(mon_bin_class);
  1190. err_class:
  1191. return rc;
  1192. }
  1193. void mon_bin_exit(void)
  1194. {
  1195. cdev_del(&mon_bin_cdev);
  1196. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1197. class_destroy(mon_bin_class);
  1198. }