mon_text.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * This is a text format reader.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/usb.h>
  9. #include <linux/time.h>
  10. #include <asm/uaccess.h>
  11. #include "usb_mon.h"
  12. /*
  13. * No, we do not want arbitrarily long data strings.
  14. * Use the binary interface if you want to capture bulk data!
  15. */
  16. #define DATA_MAX 32
  17. /*
  18. * This limit exists to prevent OOMs when the user process stops reading.
  19. */
  20. #define EVENT_MAX 25
  21. #define PRINTF_DFL 120
  22. struct mon_event_text {
  23. struct list_head e_link;
  24. int type; /* submit, complete, etc. */
  25. unsigned int pipe; /* Pipe */
  26. unsigned long id; /* From pointer, most of the time */
  27. unsigned int tstamp;
  28. int length; /* Depends on type: xfer length or act length */
  29. int status;
  30. char data_flag;
  31. unsigned char data[DATA_MAX];
  32. };
  33. #define SLAB_NAME_SZ 30
  34. struct mon_reader_text {
  35. kmem_cache_t *e_slab;
  36. int nevents;
  37. struct list_head e_list;
  38. struct mon_reader r; /* In C, parent class can be placed anywhere */
  39. wait_queue_head_t wait;
  40. int printf_size;
  41. char *printf_buf;
  42. struct semaphore printf_lock;
  43. char slab_name[SLAB_NAME_SZ];
  44. };
  45. static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
  46. static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
  47. /*
  48. * mon_text_submit
  49. * mon_text_complete
  50. *
  51. * May be called from an interrupt.
  52. *
  53. * This is called with the whole mon_bus locked, so no additional lock.
  54. */
  55. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  56. int len, char ev_type)
  57. {
  58. int pipe = urb->pipe;
  59. unsigned char *data;
  60. /*
  61. * The check to see if it's safe to poke at data has an enormous
  62. * number of corner cases, but it seems that the following is
  63. * more or less safe.
  64. *
  65. * We do not even try to look transfer_buffer, because it can
  66. * contain non-NULL garbage in case the upper level promised to
  67. * set DMA for the HCD.
  68. */
  69. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  70. return 'D';
  71. if (len <= 0)
  72. return 'L';
  73. if ((data = urb->transfer_buffer) == NULL)
  74. return 'Z'; /* '0' would be not as pretty. */
  75. /*
  76. * Bulk is easy to shortcut reliably.
  77. * XXX Control needs setup packet taken.
  78. * XXX Other pipe types need consideration. Currently, we overdo it
  79. * and collect garbage for them: better more than less.
  80. */
  81. if (usb_pipebulk(pipe) || usb_pipecontrol(pipe)) {
  82. if (usb_pipein(pipe)) {
  83. if (ev_type == 'S')
  84. return '<';
  85. } else {
  86. if (ev_type == 'C')
  87. return '>';
  88. }
  89. }
  90. if (len >= DATA_MAX)
  91. len = DATA_MAX;
  92. memcpy(ep->data, urb->transfer_buffer, len);
  93. return 0;
  94. }
  95. static inline unsigned int mon_get_timestamp(void)
  96. {
  97. struct timeval tval;
  98. unsigned int stamp;
  99. do_gettimeofday(&tval);
  100. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  101. stamp = stamp * 1000000 + tval.tv_usec;
  102. return stamp;
  103. }
  104. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  105. char ev_type)
  106. {
  107. struct mon_event_text *ep;
  108. unsigned int stamp;
  109. stamp = mon_get_timestamp();
  110. if (rp->nevents >= EVENT_MAX ||
  111. (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
  112. rp->r.m_bus->cnt_text_lost++;
  113. return;
  114. }
  115. ep->type = ev_type;
  116. ep->pipe = urb->pipe;
  117. ep->id = (unsigned long) urb;
  118. ep->tstamp = stamp;
  119. ep->length = (ev_type == 'S') ?
  120. urb->transfer_buffer_length : urb->actual_length;
  121. /* Collecting status makes debugging sense for submits, too */
  122. ep->status = urb->status;
  123. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type);
  124. rp->nevents++;
  125. list_add_tail(&ep->e_link, &rp->e_list);
  126. wake_up(&rp->wait);
  127. }
  128. static void mon_text_submit(void *data, struct urb *urb)
  129. {
  130. struct mon_reader_text *rp = data;
  131. mon_text_event(rp, urb, 'S');
  132. }
  133. static void mon_text_complete(void *data, struct urb *urb)
  134. {
  135. struct mon_reader_text *rp = data;
  136. mon_text_event(rp, urb, 'C');
  137. }
  138. /*
  139. * Fetch next event from the circular buffer.
  140. */
  141. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  142. struct mon_bus *mbus)
  143. {
  144. struct list_head *p;
  145. unsigned long flags;
  146. spin_lock_irqsave(&mbus->lock, flags);
  147. if (list_empty(&rp->e_list)) {
  148. spin_unlock_irqrestore(&mbus->lock, flags);
  149. return NULL;
  150. }
  151. p = rp->e_list.next;
  152. list_del(p);
  153. --rp->nevents;
  154. spin_unlock_irqrestore(&mbus->lock, flags);
  155. return list_entry(p, struct mon_event_text, e_link);
  156. }
  157. /*
  158. */
  159. static int mon_text_open(struct inode *inode, struct file *file)
  160. {
  161. struct mon_bus *mbus;
  162. struct usb_bus *ubus;
  163. struct mon_reader_text *rp;
  164. int rc;
  165. down(&mon_lock);
  166. mbus = inode->u.generic_ip;
  167. ubus = mbus->u_bus;
  168. rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  169. if (rp == NULL) {
  170. rc = -ENOMEM;
  171. goto err_alloc;
  172. }
  173. memset(rp, 0, sizeof(struct mon_reader_text));
  174. INIT_LIST_HEAD(&rp->e_list);
  175. init_waitqueue_head(&rp->wait);
  176. init_MUTEX(&rp->printf_lock);
  177. rp->printf_size = PRINTF_DFL;
  178. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  179. if (rp->printf_buf == NULL) {
  180. rc = -ENOMEM;
  181. goto err_alloc_pr;
  182. }
  183. rp->r.m_bus = mbus;
  184. rp->r.r_data = rp;
  185. rp->r.rnf_submit = mon_text_submit;
  186. rp->r.rnf_complete = mon_text_complete;
  187. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
  188. (long)rp);
  189. rp->e_slab = kmem_cache_create(rp->slab_name,
  190. sizeof(struct mon_event_text), sizeof(long), 0,
  191. mon_text_ctor, mon_text_dtor);
  192. if (rp->e_slab == NULL) {
  193. rc = -ENOMEM;
  194. goto err_slab;
  195. }
  196. mon_reader_add(mbus, &rp->r);
  197. file->private_data = rp;
  198. up(&mon_lock);
  199. return 0;
  200. // err_busy:
  201. // kmem_cache_destroy(rp->e_slab);
  202. err_slab:
  203. kfree(rp->printf_buf);
  204. err_alloc_pr:
  205. kfree(rp);
  206. err_alloc:
  207. up(&mon_lock);
  208. return rc;
  209. }
  210. /*
  211. * For simplicity, we read one record in one system call and throw out
  212. * what does not fit. This means that the following does not work:
  213. * dd if=/dbg/usbmon/0t bs=10
  214. * Also, we do not allow seeks and do not bother advancing the offset.
  215. */
  216. static ssize_t mon_text_read(struct file *file, char __user *buf,
  217. size_t nbytes, loff_t *ppos)
  218. {
  219. struct mon_reader_text *rp = file->private_data;
  220. struct mon_bus *mbus = rp->r.m_bus;
  221. DECLARE_WAITQUEUE(waita, current);
  222. struct mon_event_text *ep;
  223. int cnt, limit;
  224. char *pbuf;
  225. char udir, utype;
  226. int data_len, i;
  227. add_wait_queue(&rp->wait, &waita);
  228. set_current_state(TASK_INTERRUPTIBLE);
  229. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  230. if (file->f_flags & O_NONBLOCK) {
  231. set_current_state(TASK_RUNNING);
  232. remove_wait_queue(&rp->wait, &waita);
  233. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  234. }
  235. /*
  236. * We do not count nwaiters, because ->release is supposed
  237. * to be called when all openers are gone only.
  238. */
  239. schedule();
  240. if (signal_pending(current)) {
  241. remove_wait_queue(&rp->wait, &waita);
  242. return -EINTR;
  243. }
  244. set_current_state(TASK_INTERRUPTIBLE);
  245. }
  246. set_current_state(TASK_RUNNING);
  247. remove_wait_queue(&rp->wait, &waita);
  248. down(&rp->printf_lock);
  249. cnt = 0;
  250. pbuf = rp->printf_buf;
  251. limit = rp->printf_size;
  252. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  253. switch (usb_pipetype(ep->pipe)) {
  254. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  255. case PIPE_INTERRUPT: utype = 'I'; break;
  256. case PIPE_CONTROL: utype = 'C'; break;
  257. default: /* PIPE_BULK */ utype = 'B';
  258. }
  259. cnt += snprintf(pbuf + cnt, limit - cnt,
  260. "%lx %u %c %c%c:%03u:%02u %d %d",
  261. ep->id, ep->tstamp, ep->type,
  262. utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe),
  263. ep->status, ep->length);
  264. if ((data_len = ep->length) > 0) {
  265. if (ep->data_flag == 0) {
  266. cnt += snprintf(pbuf + cnt, limit - cnt, " =");
  267. if (data_len >= DATA_MAX)
  268. data_len = DATA_MAX;
  269. for (i = 0; i < data_len; i++) {
  270. if (i % 4 == 0) {
  271. cnt += snprintf(pbuf + cnt, limit - cnt,
  272. " ");
  273. }
  274. cnt += snprintf(pbuf + cnt, limit - cnt,
  275. "%02x", ep->data[i]);
  276. }
  277. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  278. } else {
  279. cnt += snprintf(pbuf + cnt, limit - cnt,
  280. " %c\n", ep->data_flag);
  281. }
  282. } else {
  283. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  284. }
  285. if (copy_to_user(buf, rp->printf_buf, cnt))
  286. cnt = -EFAULT;
  287. up(&rp->printf_lock);
  288. kmem_cache_free(rp->e_slab, ep);
  289. return cnt;
  290. }
  291. static int mon_text_release(struct inode *inode, struct file *file)
  292. {
  293. struct mon_reader_text *rp = file->private_data;
  294. struct mon_bus *mbus;
  295. /* unsigned long flags; */
  296. struct list_head *p;
  297. struct mon_event_text *ep;
  298. down(&mon_lock);
  299. mbus = inode->u.generic_ip;
  300. if (mbus->nreaders <= 0) {
  301. printk(KERN_ERR TAG ": consistency error on close\n");
  302. up(&mon_lock);
  303. return 0;
  304. }
  305. mon_reader_del(mbus, &rp->r);
  306. /*
  307. * In theory, e_list is protected by mbus->lock. However,
  308. * after mon_reader_del has finished, the following is the case:
  309. * - we are not on reader list anymore, so new events won't be added;
  310. * - whole mbus may be dropped if it was orphaned.
  311. * So, we better not touch mbus.
  312. */
  313. /* spin_lock_irqsave(&mbus->lock, flags); */
  314. while (!list_empty(&rp->e_list)) {
  315. p = rp->e_list.next;
  316. ep = list_entry(p, struct mon_event_text, e_link);
  317. list_del(p);
  318. --rp->nevents;
  319. kmem_cache_free(rp->e_slab, ep);
  320. }
  321. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  322. kmem_cache_destroy(rp->e_slab);
  323. kfree(rp->printf_buf);
  324. kfree(rp);
  325. up(&mon_lock);
  326. return 0;
  327. }
  328. struct file_operations mon_fops_text = {
  329. .owner = THIS_MODULE,
  330. .open = mon_text_open,
  331. .llseek = no_llseek,
  332. .read = mon_text_read,
  333. /* .write = mon_text_write, */
  334. /* .poll = mon_text_poll, */
  335. /* .ioctl = mon_text_ioctl, */
  336. .release = mon_text_release,
  337. };
  338. /*
  339. * Slab interface: constructor.
  340. */
  341. static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  342. {
  343. /*
  344. * Nothing to initialize. No, really!
  345. * So, we fill it with garbage to emulate a reused object.
  346. */
  347. memset(mem, 0xe5, sizeof(struct mon_event_text));
  348. }
  349. static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  350. {
  351. ;
  352. }