mon_text.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * This is a text format reader.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/usb.h>
  9. #include <linux/time.h>
  10. #include <linux/mutex.h>
  11. #include <linux/debugfs.h>
  12. #include <asm/uaccess.h>
  13. #include "usb_mon.h"
  14. /*
  15. * No, we do not want arbitrarily long data strings.
  16. * Use the binary interface if you want to capture bulk data!
  17. */
  18. #define DATA_MAX 32
  19. /*
  20. * Defined by USB 2.0 clause 9.3, table 9.2.
  21. */
  22. #define SETUP_MAX 8
  23. /*
  24. * This limit exists to prevent OOMs when the user process stops reading.
  25. * If usbmon were available to unprivileged processes, it might be open
  26. * to a local DoS. But we have to keep to root in order to prevent
  27. * password sniffing from HID devices.
  28. */
  29. #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
  30. /*
  31. * Potentially unlimited number; we limit it for similar allocations.
  32. * The usbfs limits this to 128, but we're not quite as generous.
  33. */
  34. #define ISODESC_MAX 5
  35. #define PRINTF_DFL 250 /* with 5 ISOs segs */
  36. struct mon_iso_desc {
  37. int status;
  38. unsigned int offset;
  39. unsigned int length; /* Unsigned here, signed in URB. Historic. */
  40. };
  41. struct mon_event_text {
  42. struct list_head e_link;
  43. int type; /* submit, complete, etc. */
  44. unsigned int pipe; /* Pipe */
  45. unsigned long id; /* From pointer, most of the time */
  46. unsigned int tstamp;
  47. int busnum;
  48. int length; /* Depends on type: xfer length or act length */
  49. int status;
  50. int interval;
  51. int start_frame;
  52. int error_count;
  53. char setup_flag;
  54. char data_flag;
  55. int numdesc; /* Full number */
  56. struct mon_iso_desc isodesc[ISODESC_MAX];
  57. unsigned char setup[SETUP_MAX];
  58. unsigned char data[DATA_MAX];
  59. };
  60. #define SLAB_NAME_SZ 30
  61. struct mon_reader_text {
  62. struct kmem_cache *e_slab;
  63. int nevents;
  64. struct list_head e_list;
  65. struct mon_reader r; /* In C, parent class can be placed anywhere */
  66. wait_queue_head_t wait;
  67. int printf_size;
  68. char *printf_buf;
  69. struct mutex printf_lock;
  70. char slab_name[SLAB_NAME_SZ];
  71. };
  72. static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
  73. static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
  74. struct mon_text_ptr {
  75. int cnt, limit;
  76. char *pbuf;
  77. };
  78. static struct mon_event_text *
  79. mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
  80. static void mon_text_read_head_t(struct mon_reader_text *rp,
  81. struct mon_text_ptr *p, const struct mon_event_text *ep);
  82. static void mon_text_read_head_u(struct mon_reader_text *rp,
  83. struct mon_text_ptr *p, const struct mon_event_text *ep);
  84. static void mon_text_read_statset(struct mon_reader_text *rp,
  85. struct mon_text_ptr *p, const struct mon_event_text *ep);
  86. static void mon_text_read_intstat(struct mon_reader_text *rp,
  87. struct mon_text_ptr *p, const struct mon_event_text *ep);
  88. static void mon_text_read_isostat(struct mon_reader_text *rp,
  89. struct mon_text_ptr *p, const struct mon_event_text *ep);
  90. static void mon_text_read_isodesc(struct mon_reader_text *rp,
  91. struct mon_text_ptr *p, const struct mon_event_text *ep);
  92. static void mon_text_read_data(struct mon_reader_text *rp,
  93. struct mon_text_ptr *p, const struct mon_event_text *ep);
  94. /*
  95. * mon_text_submit
  96. * mon_text_complete
  97. *
  98. * May be called from an interrupt.
  99. *
  100. * This is called with the whole mon_bus locked, so no additional lock.
  101. */
  102. static inline char mon_text_get_setup(struct mon_event_text *ep,
  103. struct urb *urb, char ev_type, struct mon_bus *mbus)
  104. {
  105. if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
  106. return '-';
  107. if (urb->dev->bus->uses_dma &&
  108. (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  109. return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
  110. }
  111. if (urb->setup_packet == NULL)
  112. return 'Z'; /* '0' would be not as pretty. */
  113. memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
  114. return 0;
  115. }
  116. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  117. int len, char ev_type, struct mon_bus *mbus)
  118. {
  119. int pipe = urb->pipe;
  120. if (len <= 0)
  121. return 'L';
  122. if (len >= DATA_MAX)
  123. len = DATA_MAX;
  124. if (usb_pipein(pipe)) {
  125. if (ev_type != 'C')
  126. return '<';
  127. } else {
  128. if (ev_type != 'S')
  129. return '>';
  130. }
  131. /*
  132. * The check to see if it's safe to poke at data has an enormous
  133. * number of corner cases, but it seems that the following is
  134. * more or less safe.
  135. *
  136. * We do not even try to look at transfer_buffer, because it can
  137. * contain non-NULL garbage in case the upper level promised to
  138. * set DMA for the HCD.
  139. */
  140. if (urb->dev->bus->uses_dma &&
  141. (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  142. return mon_dmapeek(ep->data, urb->transfer_dma, len);
  143. }
  144. if (urb->transfer_buffer == NULL)
  145. return 'Z'; /* '0' would be not as pretty. */
  146. memcpy(ep->data, urb->transfer_buffer, len);
  147. return 0;
  148. }
  149. static inline unsigned int mon_get_timestamp(void)
  150. {
  151. struct timeval tval;
  152. unsigned int stamp;
  153. do_gettimeofday(&tval);
  154. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  155. stamp = stamp * 1000000 + tval.tv_usec;
  156. return stamp;
  157. }
  158. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  159. char ev_type)
  160. {
  161. struct mon_event_text *ep;
  162. unsigned int stamp;
  163. struct usb_iso_packet_descriptor *fp;
  164. struct mon_iso_desc *dp;
  165. int i, ndesc;
  166. stamp = mon_get_timestamp();
  167. if (rp->nevents >= EVENT_MAX ||
  168. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  169. rp->r.m_bus->cnt_text_lost++;
  170. return;
  171. }
  172. ep->type = ev_type;
  173. ep->pipe = urb->pipe;
  174. ep->id = (unsigned long) urb;
  175. ep->busnum = urb->dev->bus->busnum;
  176. ep->tstamp = stamp;
  177. ep->length = (ev_type == 'S') ?
  178. urb->transfer_buffer_length : urb->actual_length;
  179. /* Collecting status makes debugging sense for submits, too */
  180. ep->status = urb->status;
  181. if (usb_pipeint(urb->pipe)) {
  182. ep->interval = urb->interval;
  183. } else if (usb_pipeisoc(urb->pipe)) {
  184. ep->interval = urb->interval;
  185. ep->start_frame = urb->start_frame;
  186. ep->error_count = urb->error_count;
  187. }
  188. ep->numdesc = urb->number_of_packets;
  189. if (usb_pipeisoc(urb->pipe) && urb->number_of_packets > 0) {
  190. if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
  191. ndesc = ISODESC_MAX;
  192. fp = urb->iso_frame_desc;
  193. dp = ep->isodesc;
  194. for (i = 0; i < ndesc; i++) {
  195. dp->status = fp->status;
  196. dp->offset = fp->offset;
  197. dp->length = (ev_type == 'S') ?
  198. fp->length : fp->actual_length;
  199. fp++;
  200. dp++;
  201. }
  202. }
  203. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
  204. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
  205. rp->r.m_bus);
  206. rp->nevents++;
  207. list_add_tail(&ep->e_link, &rp->e_list);
  208. wake_up(&rp->wait);
  209. }
  210. static void mon_text_submit(void *data, struct urb *urb)
  211. {
  212. struct mon_reader_text *rp = data;
  213. mon_text_event(rp, urb, 'S');
  214. }
  215. static void mon_text_complete(void *data, struct urb *urb)
  216. {
  217. struct mon_reader_text *rp = data;
  218. mon_text_event(rp, urb, 'C');
  219. }
  220. static void mon_text_error(void *data, struct urb *urb, int error)
  221. {
  222. struct mon_reader_text *rp = data;
  223. struct mon_event_text *ep;
  224. if (rp->nevents >= EVENT_MAX ||
  225. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  226. rp->r.m_bus->cnt_text_lost++;
  227. return;
  228. }
  229. ep->type = 'E';
  230. ep->pipe = urb->pipe;
  231. ep->id = (unsigned long) urb;
  232. ep->busnum = 0;
  233. ep->tstamp = 0;
  234. ep->length = 0;
  235. ep->status = error;
  236. ep->setup_flag = '-';
  237. ep->data_flag = 'E';
  238. rp->nevents++;
  239. list_add_tail(&ep->e_link, &rp->e_list);
  240. wake_up(&rp->wait);
  241. }
  242. /*
  243. * Fetch next event from the circular buffer.
  244. */
  245. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  246. struct mon_bus *mbus)
  247. {
  248. struct list_head *p;
  249. unsigned long flags;
  250. spin_lock_irqsave(&mbus->lock, flags);
  251. if (list_empty(&rp->e_list)) {
  252. spin_unlock_irqrestore(&mbus->lock, flags);
  253. return NULL;
  254. }
  255. p = rp->e_list.next;
  256. list_del(p);
  257. --rp->nevents;
  258. spin_unlock_irqrestore(&mbus->lock, flags);
  259. return list_entry(p, struct mon_event_text, e_link);
  260. }
  261. /*
  262. */
  263. static int mon_text_open(struct inode *inode, struct file *file)
  264. {
  265. struct mon_bus *mbus;
  266. struct mon_reader_text *rp;
  267. int rc;
  268. mutex_lock(&mon_lock);
  269. mbus = inode->i_private;
  270. rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  271. if (rp == NULL) {
  272. rc = -ENOMEM;
  273. goto err_alloc;
  274. }
  275. INIT_LIST_HEAD(&rp->e_list);
  276. init_waitqueue_head(&rp->wait);
  277. mutex_init(&rp->printf_lock);
  278. rp->printf_size = PRINTF_DFL;
  279. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  280. if (rp->printf_buf == NULL) {
  281. rc = -ENOMEM;
  282. goto err_alloc_pr;
  283. }
  284. rp->r.m_bus = mbus;
  285. rp->r.r_data = rp;
  286. rp->r.rnf_submit = mon_text_submit;
  287. rp->r.rnf_error = mon_text_error;
  288. rp->r.rnf_complete = mon_text_complete;
  289. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
  290. rp->e_slab = kmem_cache_create(rp->slab_name,
  291. sizeof(struct mon_event_text), sizeof(long), 0,
  292. mon_text_ctor, NULL);
  293. if (rp->e_slab == NULL) {
  294. rc = -ENOMEM;
  295. goto err_slab;
  296. }
  297. mon_reader_add(mbus, &rp->r);
  298. file->private_data = rp;
  299. mutex_unlock(&mon_lock);
  300. return 0;
  301. // err_busy:
  302. // kmem_cache_destroy(rp->e_slab);
  303. err_slab:
  304. kfree(rp->printf_buf);
  305. err_alloc_pr:
  306. kfree(rp);
  307. err_alloc:
  308. mutex_unlock(&mon_lock);
  309. return rc;
  310. }
  311. /*
  312. * For simplicity, we read one record in one system call and throw out
  313. * what does not fit. This means that the following does not work:
  314. * dd if=/dbg/usbmon/0t bs=10
  315. * Also, we do not allow seeks and do not bother advancing the offset.
  316. */
  317. static ssize_t mon_text_read_t(struct file *file, char __user *buf,
  318. size_t nbytes, loff_t *ppos)
  319. {
  320. struct mon_reader_text *rp = file->private_data;
  321. struct mon_event_text *ep;
  322. struct mon_text_ptr ptr;
  323. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  324. return PTR_ERR(ep);
  325. mutex_lock(&rp->printf_lock);
  326. ptr.cnt = 0;
  327. ptr.pbuf = rp->printf_buf;
  328. ptr.limit = rp->printf_size;
  329. mon_text_read_head_t(rp, &ptr, ep);
  330. mon_text_read_statset(rp, &ptr, ep);
  331. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  332. " %d", ep->length);
  333. mon_text_read_data(rp, &ptr, ep);
  334. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  335. ptr.cnt = -EFAULT;
  336. mutex_unlock(&rp->printf_lock);
  337. kmem_cache_free(rp->e_slab, ep);
  338. return ptr.cnt;
  339. }
  340. static ssize_t mon_text_read_u(struct file *file, char __user *buf,
  341. size_t nbytes, loff_t *ppos)
  342. {
  343. struct mon_reader_text *rp = file->private_data;
  344. struct mon_event_text *ep;
  345. struct mon_text_ptr ptr;
  346. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  347. return PTR_ERR(ep);
  348. mutex_lock(&rp->printf_lock);
  349. ptr.cnt = 0;
  350. ptr.pbuf = rp->printf_buf;
  351. ptr.limit = rp->printf_size;
  352. mon_text_read_head_u(rp, &ptr, ep);
  353. if (ep->type == 'E') {
  354. mon_text_read_statset(rp, &ptr, ep);
  355. } else if (usb_pipeisoc(ep->pipe)) {
  356. mon_text_read_isostat(rp, &ptr, ep);
  357. mon_text_read_isodesc(rp, &ptr, ep);
  358. } else if (usb_pipeint(ep->pipe)) {
  359. mon_text_read_intstat(rp, &ptr, ep);
  360. } else {
  361. mon_text_read_statset(rp, &ptr, ep);
  362. }
  363. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  364. " %d", ep->length);
  365. mon_text_read_data(rp, &ptr, ep);
  366. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  367. ptr.cnt = -EFAULT;
  368. mutex_unlock(&rp->printf_lock);
  369. kmem_cache_free(rp->e_slab, ep);
  370. return ptr.cnt;
  371. }
  372. static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
  373. struct file *file)
  374. {
  375. struct mon_bus *mbus = rp->r.m_bus;
  376. DECLARE_WAITQUEUE(waita, current);
  377. struct mon_event_text *ep;
  378. add_wait_queue(&rp->wait, &waita);
  379. set_current_state(TASK_INTERRUPTIBLE);
  380. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  381. if (file->f_flags & O_NONBLOCK) {
  382. set_current_state(TASK_RUNNING);
  383. remove_wait_queue(&rp->wait, &waita);
  384. return ERR_PTR(-EWOULDBLOCK);
  385. }
  386. /*
  387. * We do not count nwaiters, because ->release is supposed
  388. * to be called when all openers are gone only.
  389. */
  390. schedule();
  391. if (signal_pending(current)) {
  392. remove_wait_queue(&rp->wait, &waita);
  393. return ERR_PTR(-EINTR);
  394. }
  395. set_current_state(TASK_INTERRUPTIBLE);
  396. }
  397. set_current_state(TASK_RUNNING);
  398. remove_wait_queue(&rp->wait, &waita);
  399. return ep;
  400. }
  401. static void mon_text_read_head_t(struct mon_reader_text *rp,
  402. struct mon_text_ptr *p, const struct mon_event_text *ep)
  403. {
  404. char udir, utype;
  405. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  406. switch (usb_pipetype(ep->pipe)) {
  407. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  408. case PIPE_INTERRUPT: utype = 'I'; break;
  409. case PIPE_CONTROL: utype = 'C'; break;
  410. default: /* PIPE_BULK */ utype = 'B';
  411. }
  412. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  413. "%lx %u %c %c%c:%03u:%02u",
  414. ep->id, ep->tstamp, ep->type,
  415. utype, udir,
  416. usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
  417. }
  418. static void mon_text_read_head_u(struct mon_reader_text *rp,
  419. struct mon_text_ptr *p, const struct mon_event_text *ep)
  420. {
  421. char udir, utype;
  422. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  423. switch (usb_pipetype(ep->pipe)) {
  424. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  425. case PIPE_INTERRUPT: utype = 'I'; break;
  426. case PIPE_CONTROL: utype = 'C'; break;
  427. default: /* PIPE_BULK */ utype = 'B';
  428. }
  429. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  430. "%lx %u %c %c%c:%d:%03u:%u",
  431. ep->id, ep->tstamp, ep->type,
  432. utype, udir,
  433. ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
  434. }
  435. static void mon_text_read_statset(struct mon_reader_text *rp,
  436. struct mon_text_ptr *p, const struct mon_event_text *ep)
  437. {
  438. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  439. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  440. " s %02x %02x %04x %04x %04x",
  441. ep->setup[0],
  442. ep->setup[1],
  443. (ep->setup[3] << 8) | ep->setup[2],
  444. (ep->setup[5] << 8) | ep->setup[4],
  445. (ep->setup[7] << 8) | ep->setup[6]);
  446. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  447. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  448. " %c __ __ ____ ____ ____", ep->setup_flag);
  449. } else { /* No setup for this kind of URB */
  450. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  451. " %d", ep->status);
  452. }
  453. }
  454. static void mon_text_read_intstat(struct mon_reader_text *rp,
  455. struct mon_text_ptr *p, const struct mon_event_text *ep)
  456. {
  457. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  458. " %d:%d", ep->status, ep->interval);
  459. }
  460. static void mon_text_read_isostat(struct mon_reader_text *rp,
  461. struct mon_text_ptr *p, const struct mon_event_text *ep)
  462. {
  463. if (ep->type == 'S') {
  464. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  465. " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
  466. } else {
  467. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  468. " %d:%d:%d:%d",
  469. ep->status, ep->interval, ep->start_frame, ep->error_count);
  470. }
  471. }
  472. static void mon_text_read_isodesc(struct mon_reader_text *rp,
  473. struct mon_text_ptr *p, const struct mon_event_text *ep)
  474. {
  475. int ndesc; /* Display this many */
  476. int i;
  477. const struct mon_iso_desc *dp;
  478. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  479. " %d", ep->numdesc);
  480. ndesc = ep->numdesc;
  481. if (ndesc > ISODESC_MAX)
  482. ndesc = ISODESC_MAX;
  483. if (ndesc < 0)
  484. ndesc = 0;
  485. dp = ep->isodesc;
  486. for (i = 0; i < ndesc; i++) {
  487. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  488. " %d:%u:%u", dp->status, dp->offset, dp->length);
  489. dp++;
  490. }
  491. }
  492. static void mon_text_read_data(struct mon_reader_text *rp,
  493. struct mon_text_ptr *p, const struct mon_event_text *ep)
  494. {
  495. int data_len, i;
  496. if ((data_len = ep->length) > 0) {
  497. if (ep->data_flag == 0) {
  498. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  499. " =");
  500. if (data_len >= DATA_MAX)
  501. data_len = DATA_MAX;
  502. for (i = 0; i < data_len; i++) {
  503. if (i % 4 == 0) {
  504. p->cnt += snprintf(p->pbuf + p->cnt,
  505. p->limit - p->cnt,
  506. " ");
  507. }
  508. p->cnt += snprintf(p->pbuf + p->cnt,
  509. p->limit - p->cnt,
  510. "%02x", ep->data[i]);
  511. }
  512. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  513. "\n");
  514. } else {
  515. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  516. " %c\n", ep->data_flag);
  517. }
  518. } else {
  519. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
  520. }
  521. }
  522. static int mon_text_release(struct inode *inode, struct file *file)
  523. {
  524. struct mon_reader_text *rp = file->private_data;
  525. struct mon_bus *mbus;
  526. /* unsigned long flags; */
  527. struct list_head *p;
  528. struct mon_event_text *ep;
  529. mutex_lock(&mon_lock);
  530. mbus = inode->i_private;
  531. if (mbus->nreaders <= 0) {
  532. printk(KERN_ERR TAG ": consistency error on close\n");
  533. mutex_unlock(&mon_lock);
  534. return 0;
  535. }
  536. mon_reader_del(mbus, &rp->r);
  537. /*
  538. * In theory, e_list is protected by mbus->lock. However,
  539. * after mon_reader_del has finished, the following is the case:
  540. * - we are not on reader list anymore, so new events won't be added;
  541. * - whole mbus may be dropped if it was orphaned.
  542. * So, we better not touch mbus.
  543. */
  544. /* spin_lock_irqsave(&mbus->lock, flags); */
  545. while (!list_empty(&rp->e_list)) {
  546. p = rp->e_list.next;
  547. ep = list_entry(p, struct mon_event_text, e_link);
  548. list_del(p);
  549. --rp->nevents;
  550. kmem_cache_free(rp->e_slab, ep);
  551. }
  552. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  553. kmem_cache_destroy(rp->e_slab);
  554. kfree(rp->printf_buf);
  555. kfree(rp);
  556. mutex_unlock(&mon_lock);
  557. return 0;
  558. }
  559. static const struct file_operations mon_fops_text_t = {
  560. .owner = THIS_MODULE,
  561. .open = mon_text_open,
  562. .llseek = no_llseek,
  563. .read = mon_text_read_t,
  564. .release = mon_text_release,
  565. };
  566. static const struct file_operations mon_fops_text_u = {
  567. .owner = THIS_MODULE,
  568. .open = mon_text_open,
  569. .llseek = no_llseek,
  570. .read = mon_text_read_u,
  571. .release = mon_text_release,
  572. };
  573. int mon_text_add(struct mon_bus *mbus, int busnum)
  574. {
  575. struct dentry *d;
  576. enum { NAMESZ = 10 };
  577. char name[NAMESZ];
  578. int rc;
  579. rc = snprintf(name, NAMESZ, "%dt", busnum);
  580. if (rc <= 0 || rc >= NAMESZ)
  581. goto err_print_t;
  582. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
  583. if (d == NULL)
  584. goto err_create_t;
  585. mbus->dent_t = d;
  586. rc = snprintf(name, NAMESZ, "%du", busnum);
  587. if (rc <= 0 || rc >= NAMESZ)
  588. goto err_print_u;
  589. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
  590. if (d == NULL)
  591. goto err_create_u;
  592. mbus->dent_u = d;
  593. rc = snprintf(name, NAMESZ, "%ds", busnum);
  594. if (rc <= 0 || rc >= NAMESZ)
  595. goto err_print_s;
  596. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
  597. if (d == NULL)
  598. goto err_create_s;
  599. mbus->dent_s = d;
  600. return 1;
  601. err_create_s:
  602. err_print_s:
  603. debugfs_remove(mbus->dent_u);
  604. mbus->dent_u = NULL;
  605. err_create_u:
  606. err_print_u:
  607. debugfs_remove(mbus->dent_t);
  608. mbus->dent_t = NULL;
  609. err_create_t:
  610. err_print_t:
  611. return 0;
  612. }
  613. void mon_text_del(struct mon_bus *mbus)
  614. {
  615. debugfs_remove(mbus->dent_u);
  616. debugfs_remove(mbus->dent_t);
  617. debugfs_remove(mbus->dent_s);
  618. }
  619. /*
  620. * Slab interface: constructor.
  621. */
  622. static void mon_text_ctor(void *mem, struct kmem_cache *slab, unsigned long sflags)
  623. {
  624. /*
  625. * Nothing to initialize. No, really!
  626. * So, we fill it with garbage to emulate a reused object.
  627. */
  628. memset(mem, 0xe5, sizeof(struct mon_event_text));
  629. }
  630. int __init mon_text_init(void)
  631. {
  632. struct dentry *mondir;
  633. mondir = debugfs_create_dir("usbmon", NULL);
  634. if (IS_ERR(mondir)) {
  635. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  636. return -ENODEV;
  637. }
  638. if (mondir == NULL) {
  639. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  640. return -ENODEV;
  641. }
  642. mon_dir = mondir;
  643. return 0;
  644. }
  645. void mon_text_exit(void)
  646. {
  647. debugfs_remove(mon_dir);
  648. }