mon_bin.c 32 KB

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