mon_bin.c 33 KB

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