mon_bin.c 32 KB

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