mon_text.c 19 KB

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