mon_text.c 11 KB

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