mon_text.c 20 KB

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