mon_text.c 19 KB

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