mon_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * mon_main.c: Main file, module initiation and exit, registrations, etc.
  5. *
  6. * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/usb.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/notifier.h>
  14. #include <linux/mutex.h>
  15. #include "usb_mon.h"
  16. #include "../core/hcd.h"
  17. static void mon_submit(struct usb_bus *ubus, struct urb *urb);
  18. static void mon_complete(struct usb_bus *ubus, struct urb *urb);
  19. static void mon_stop(struct mon_bus *mbus);
  20. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
  21. static void mon_bus_drop(struct kref *r);
  22. static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
  23. DEFINE_MUTEX(mon_lock);
  24. static struct dentry *mon_dir; /* /dbg/usbmon */
  25. static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
  26. /*
  27. * Link a reader into the bus.
  28. *
  29. * This must be called with mon_lock taken because of mbus->ref.
  30. */
  31. void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
  32. {
  33. unsigned long flags;
  34. struct usb_bus *ubus;
  35. spin_lock_irqsave(&mbus->lock, flags);
  36. if (mbus->nreaders == 0) {
  37. ubus = mbus->u_bus;
  38. if (ubus->monitored) {
  39. /*
  40. * Something is really broken, refuse to go on and
  41. * possibly corrupt ops pointers or worse.
  42. */
  43. printk(KERN_ERR TAG ": bus %d is already monitored\n",
  44. ubus->busnum);
  45. spin_unlock_irqrestore(&mbus->lock, flags);
  46. return;
  47. }
  48. ubus->monitored = 1;
  49. }
  50. mbus->nreaders++;
  51. list_add_tail(&r->r_link, &mbus->r_list);
  52. spin_unlock_irqrestore(&mbus->lock, flags);
  53. kref_get(&mbus->ref);
  54. }
  55. /*
  56. * Unlink reader from the bus.
  57. *
  58. * This is called with mon_lock taken, so we can decrement mbus->ref.
  59. */
  60. void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
  61. {
  62. unsigned long flags;
  63. spin_lock_irqsave(&mbus->lock, flags);
  64. list_del(&r->r_link);
  65. --mbus->nreaders;
  66. if (mbus->nreaders == 0)
  67. mon_stop(mbus);
  68. spin_unlock_irqrestore(&mbus->lock, flags);
  69. kref_put(&mbus->ref, mon_bus_drop);
  70. }
  71. /*
  72. */
  73. static void mon_submit(struct usb_bus *ubus, struct urb *urb)
  74. {
  75. struct mon_bus *mbus;
  76. unsigned long flags;
  77. struct list_head *pos;
  78. struct mon_reader *r;
  79. mbus = ubus->mon_bus;
  80. if (mbus == NULL)
  81. goto out_unlocked;
  82. spin_lock_irqsave(&mbus->lock, flags);
  83. if (mbus->nreaders == 0)
  84. goto out_locked;
  85. mbus->cnt_events++;
  86. list_for_each (pos, &mbus->r_list) {
  87. r = list_entry(pos, struct mon_reader, r_link);
  88. r->rnf_submit(r->r_data, urb);
  89. }
  90. spin_unlock_irqrestore(&mbus->lock, flags);
  91. return;
  92. out_locked:
  93. spin_unlock_irqrestore(&mbus->lock, flags);
  94. out_unlocked:
  95. return;
  96. }
  97. /*
  98. */
  99. static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
  100. {
  101. struct mon_bus *mbus;
  102. mbus = ubus->mon_bus;
  103. if (mbus == NULL)
  104. goto out_unlocked;
  105. /*
  106. * XXX Capture the error code and the 'E' event.
  107. */
  108. return;
  109. out_unlocked:
  110. return;
  111. }
  112. /*
  113. */
  114. static void mon_complete(struct usb_bus *ubus, struct urb *urb)
  115. {
  116. struct mon_bus *mbus;
  117. unsigned long flags;
  118. struct list_head *pos;
  119. struct mon_reader *r;
  120. mbus = ubus->mon_bus;
  121. if (mbus == NULL) {
  122. /*
  123. * This should not happen.
  124. * At this point we do not even know the bus number...
  125. */
  126. printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
  127. urb->pipe);
  128. return;
  129. }
  130. spin_lock_irqsave(&mbus->lock, flags);
  131. mbus->cnt_events++;
  132. list_for_each (pos, &mbus->r_list) {
  133. r = list_entry(pos, struct mon_reader, r_link);
  134. r->rnf_complete(r->r_data, urb);
  135. }
  136. spin_unlock_irqrestore(&mbus->lock, flags);
  137. }
  138. /* int (*unlink_urb) (struct urb *urb, int status); */
  139. /*
  140. * Stop monitoring.
  141. */
  142. static void mon_stop(struct mon_bus *mbus)
  143. {
  144. struct usb_bus *ubus = mbus->u_bus;
  145. /*
  146. * A stop can be called for a dissolved mon_bus in case of
  147. * a reader staying across an rmmod foo_hcd.
  148. */
  149. if (ubus != NULL) {
  150. ubus->monitored = 0;
  151. mb();
  152. }
  153. }
  154. /*
  155. * Add a USB bus (usually by a modprobe foo-hcd)
  156. *
  157. * This does not return an error code because the core cannot care less
  158. * if monitoring is not established.
  159. */
  160. static void mon_bus_add(struct usb_bus *ubus)
  161. {
  162. mon_bus_init(mon_dir, ubus);
  163. }
  164. /*
  165. * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
  166. */
  167. static void mon_bus_remove(struct usb_bus *ubus)
  168. {
  169. struct mon_bus *mbus = ubus->mon_bus;
  170. mutex_lock(&mon_lock);
  171. list_del(&mbus->bus_link);
  172. debugfs_remove(mbus->dent_t);
  173. debugfs_remove(mbus->dent_s);
  174. mon_dissolve(mbus, ubus);
  175. kref_put(&mbus->ref, mon_bus_drop);
  176. mutex_unlock(&mon_lock);
  177. }
  178. static int mon_notify(struct notifier_block *self, unsigned long action,
  179. void *dev)
  180. {
  181. switch (action) {
  182. case USB_BUS_ADD:
  183. mon_bus_add(dev);
  184. break;
  185. case USB_BUS_REMOVE:
  186. mon_bus_remove(dev);
  187. }
  188. return NOTIFY_OK;
  189. }
  190. static struct notifier_block mon_nb = {
  191. .notifier_call = mon_notify,
  192. };
  193. /*
  194. * Ops
  195. */
  196. static struct usb_mon_operations mon_ops_0 = {
  197. .urb_submit = mon_submit,
  198. .urb_submit_error = mon_submit_error,
  199. .urb_complete = mon_complete,
  200. };
  201. /*
  202. * Tear usb_bus and mon_bus apart.
  203. */
  204. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
  205. {
  206. /*
  207. * Never happens, but...
  208. */
  209. if (ubus->monitored) {
  210. printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
  211. ubus->busnum);
  212. ubus->monitored = 0;
  213. mb();
  214. }
  215. ubus->mon_bus = NULL;
  216. mbus->u_bus = NULL;
  217. mb();
  218. // usb_bus_put(ubus);
  219. }
  220. /*
  221. */
  222. static void mon_bus_drop(struct kref *r)
  223. {
  224. struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
  225. kfree(mbus);
  226. }
  227. /*
  228. * Initialize a bus for us:
  229. * - allocate mon_bus
  230. * - refcount USB bus struct
  231. * - link
  232. */
  233. static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
  234. {
  235. struct dentry *d;
  236. struct mon_bus *mbus;
  237. enum { NAMESZ = 10 };
  238. char name[NAMESZ];
  239. int rc;
  240. if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
  241. goto err_alloc;
  242. kref_init(&mbus->ref);
  243. spin_lock_init(&mbus->lock);
  244. INIT_LIST_HEAD(&mbus->r_list);
  245. /*
  246. * This usb_bus_get here is superfluous, because we receive
  247. * a notification if usb_bus is about to be removed.
  248. */
  249. // usb_bus_get(ubus);
  250. mbus->u_bus = ubus;
  251. ubus->mon_bus = mbus;
  252. rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
  253. if (rc <= 0 || rc >= NAMESZ)
  254. goto err_print_t;
  255. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
  256. if (d == NULL)
  257. goto err_create_t;
  258. mbus->dent_t = d;
  259. rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
  260. if (rc <= 0 || rc >= NAMESZ)
  261. goto err_print_s;
  262. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
  263. if (d == NULL)
  264. goto err_create_s;
  265. mbus->dent_s = d;
  266. mutex_lock(&mon_lock);
  267. list_add_tail(&mbus->bus_link, &mon_buses);
  268. mutex_unlock(&mon_lock);
  269. return;
  270. err_create_s:
  271. err_print_s:
  272. debugfs_remove(mbus->dent_t);
  273. err_create_t:
  274. err_print_t:
  275. kfree(mbus);
  276. err_alloc:
  277. return;
  278. }
  279. static int __init mon_init(void)
  280. {
  281. struct usb_bus *ubus;
  282. struct dentry *mondir;
  283. mondir = debugfs_create_dir("usbmon", NULL);
  284. if (IS_ERR(mondir)) {
  285. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  286. return -ENODEV;
  287. }
  288. if (mondir == NULL) {
  289. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  290. return -ENODEV;
  291. }
  292. mon_dir = mondir;
  293. if (usb_mon_register(&mon_ops_0) != 0) {
  294. printk(KERN_NOTICE TAG ": unable to register with the core\n");
  295. debugfs_remove(mondir);
  296. return -ENODEV;
  297. }
  298. // MOD_INC_USE_COUNT(which_module?);
  299. usb_register_notify(&mon_nb);
  300. mutex_lock(&usb_bus_list_lock);
  301. list_for_each_entry (ubus, &usb_bus_list, bus_list) {
  302. mon_bus_init(mondir, ubus);
  303. }
  304. mutex_unlock(&usb_bus_list_lock);
  305. return 0;
  306. }
  307. static void __exit mon_exit(void)
  308. {
  309. struct mon_bus *mbus;
  310. struct list_head *p;
  311. usb_unregister_notify(&mon_nb);
  312. usb_mon_deregister();
  313. mutex_lock(&mon_lock);
  314. while (!list_empty(&mon_buses)) {
  315. p = mon_buses.next;
  316. mbus = list_entry(p, struct mon_bus, bus_link);
  317. list_del(p);
  318. debugfs_remove(mbus->dent_t);
  319. debugfs_remove(mbus->dent_s);
  320. /*
  321. * This never happens, because the open/close paths in
  322. * file level maintain module use counters and so rmmod fails
  323. * before reaching here. However, better be safe...
  324. */
  325. if (mbus->nreaders) {
  326. printk(KERN_ERR TAG
  327. ": Outstanding opens (%d) on usb%d, leaking...\n",
  328. mbus->nreaders, mbus->u_bus->busnum);
  329. atomic_set(&mbus->ref.refcount, 2); /* Force leak */
  330. }
  331. mon_dissolve(mbus, mbus->u_bus);
  332. kref_put(&mbus->ref, mon_bus_drop);
  333. }
  334. mutex_unlock(&mon_lock);
  335. debugfs_remove(mon_dir);
  336. }
  337. module_init(mon_init);
  338. module_exit(mon_exit);
  339. MODULE_LICENSE("GPL");