mon_text.c 19 KB

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