mon_text.c 19 KB

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