mon_text.c 20 KB

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