mon_main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. list_for_each (pos, &mbus->r_list) {
  86. r = list_entry(pos, struct mon_reader, r_link);
  87. r->rnf_submit(r->r_data, urb);
  88. }
  89. spin_unlock_irqrestore(&mbus->lock, flags);
  90. return;
  91. out_locked:
  92. spin_unlock_irqrestore(&mbus->lock, flags);
  93. out_unlocked:
  94. return;
  95. }
  96. /*
  97. */
  98. static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
  99. {
  100. struct mon_bus *mbus;
  101. mbus = ubus->mon_bus;
  102. if (mbus == NULL)
  103. goto out_unlocked;
  104. /*
  105. * XXX Capture the error code and the 'E' event.
  106. */
  107. return;
  108. out_unlocked:
  109. return;
  110. }
  111. /*
  112. */
  113. static void mon_complete(struct usb_bus *ubus, struct urb *urb)
  114. {
  115. struct mon_bus *mbus;
  116. unsigned long flags;
  117. struct list_head *pos;
  118. struct mon_reader *r;
  119. mbus = ubus->mon_bus;
  120. if (mbus == NULL) {
  121. /*
  122. * This should not happen.
  123. * At this point we do not even know the bus number...
  124. */
  125. printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
  126. urb->pipe);
  127. return;
  128. }
  129. spin_lock_irqsave(&mbus->lock, flags);
  130. list_for_each (pos, &mbus->r_list) {
  131. r = list_entry(pos, struct mon_reader, r_link);
  132. r->rnf_complete(r->r_data, urb);
  133. }
  134. spin_unlock_irqrestore(&mbus->lock, flags);
  135. }
  136. /* int (*unlink_urb) (struct urb *urb, int status); */
  137. /*
  138. * Stop monitoring.
  139. * Obviously this must be well locked, so no need to play with mb's.
  140. */
  141. static void mon_stop(struct mon_bus *mbus)
  142. {
  143. struct usb_bus *ubus = mbus->u_bus;
  144. /*
  145. * A stop can be called for a dissolved mon_bus in case of
  146. * a reader staying across an rmmod foo_hcd.
  147. */
  148. if (ubus != NULL) {
  149. ubus->monitored = 0;
  150. mb();
  151. }
  152. }
  153. /*
  154. * Add a USB bus (usually by a modprobe foo-hcd)
  155. *
  156. * This does not return an error code because the core cannot care less
  157. * if monitoring is not established.
  158. */
  159. static void mon_bus_add(struct usb_bus *ubus)
  160. {
  161. mon_bus_init(mon_dir, ubus);
  162. }
  163. /*
  164. * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
  165. */
  166. static void mon_bus_remove(struct usb_bus *ubus)
  167. {
  168. struct mon_bus *mbus = ubus->mon_bus;
  169. mutex_lock(&mon_lock);
  170. list_del(&mbus->bus_link);
  171. debugfs_remove(mbus->dent_t);
  172. debugfs_remove(mbus->dent_s);
  173. mon_dissolve(mbus, ubus);
  174. kref_put(&mbus->ref, mon_bus_drop);
  175. mutex_unlock(&mon_lock);
  176. }
  177. static int mon_notify(struct notifier_block *self, unsigned long action,
  178. void *dev)
  179. {
  180. switch (action) {
  181. case USB_BUS_ADD:
  182. mon_bus_add(dev);
  183. break;
  184. case USB_BUS_REMOVE:
  185. mon_bus_remove(dev);
  186. }
  187. return NOTIFY_OK;
  188. }
  189. static struct notifier_block mon_nb = {
  190. .notifier_call = mon_notify,
  191. };
  192. /*
  193. * Ops
  194. */
  195. static struct usb_mon_operations mon_ops_0 = {
  196. .urb_submit = mon_submit,
  197. .urb_submit_error = mon_submit_error,
  198. .urb_complete = mon_complete,
  199. };
  200. /*
  201. * Tear usb_bus and mon_bus apart.
  202. */
  203. static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
  204. {
  205. /*
  206. * Never happens, but...
  207. */
  208. if (ubus->monitored) {
  209. printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
  210. ubus->busnum);
  211. ubus->monitored = 0;
  212. mb();
  213. }
  214. ubus->mon_bus = NULL;
  215. mbus->u_bus = NULL;
  216. mb();
  217. // usb_bus_put(ubus);
  218. }
  219. /*
  220. */
  221. static void mon_bus_drop(struct kref *r)
  222. {
  223. struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
  224. kfree(mbus);
  225. }
  226. /*
  227. * Initialize a bus for us:
  228. * - allocate mon_bus
  229. * - refcount USB bus struct
  230. * - link
  231. */
  232. static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
  233. {
  234. struct dentry *d;
  235. struct mon_bus *mbus;
  236. enum { NAMESZ = 10 };
  237. char name[NAMESZ];
  238. int rc;
  239. if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
  240. goto err_alloc;
  241. kref_init(&mbus->ref);
  242. spin_lock_init(&mbus->lock);
  243. INIT_LIST_HEAD(&mbus->r_list);
  244. /*
  245. * This usb_bus_get here is superfluous, because we receive
  246. * a notification if usb_bus is about to be removed.
  247. */
  248. // usb_bus_get(ubus);
  249. mbus->u_bus = ubus;
  250. ubus->mon_bus = mbus;
  251. rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
  252. if (rc <= 0 || rc >= NAMESZ)
  253. goto err_print_t;
  254. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
  255. if (d == NULL)
  256. goto err_create_t;
  257. mbus->dent_t = d;
  258. rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
  259. if (rc <= 0 || rc >= NAMESZ)
  260. goto err_print_s;
  261. d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
  262. if (d == NULL)
  263. goto err_create_s;
  264. mbus->dent_s = d;
  265. mutex_lock(&mon_lock);
  266. list_add_tail(&mbus->bus_link, &mon_buses);
  267. mutex_unlock(&mon_lock);
  268. return;
  269. err_create_s:
  270. err_print_s:
  271. debugfs_remove(mbus->dent_t);
  272. err_create_t:
  273. err_print_t:
  274. kfree(mbus);
  275. err_alloc:
  276. return;
  277. }
  278. static int __init mon_init(void)
  279. {
  280. struct usb_bus *ubus;
  281. struct dentry *mondir;
  282. mondir = debugfs_create_dir("usbmon", NULL);
  283. if (IS_ERR(mondir)) {
  284. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  285. return -ENODEV;
  286. }
  287. if (mondir == NULL) {
  288. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  289. return -ENODEV;
  290. }
  291. mon_dir = mondir;
  292. if (usb_mon_register(&mon_ops_0) != 0) {
  293. printk(KERN_NOTICE TAG ": unable to register with the core\n");
  294. debugfs_remove(mondir);
  295. return -ENODEV;
  296. }
  297. // MOD_INC_USE_COUNT(which_module?);
  298. usb_register_notify(&mon_nb);
  299. mutex_lock(&usb_bus_list_lock);
  300. list_for_each_entry (ubus, &usb_bus_list, bus_list) {
  301. mon_bus_init(mondir, ubus);
  302. }
  303. mutex_unlock(&usb_bus_list_lock);
  304. return 0;
  305. }
  306. static void __exit mon_exit(void)
  307. {
  308. struct mon_bus *mbus;
  309. struct list_head *p;
  310. usb_unregister_notify(&mon_nb);
  311. usb_mon_deregister();
  312. mutex_lock(&mon_lock);
  313. while (!list_empty(&mon_buses)) {
  314. p = mon_buses.next;
  315. mbus = list_entry(p, struct mon_bus, bus_link);
  316. list_del(p);
  317. debugfs_remove(mbus->dent_t);
  318. debugfs_remove(mbus->dent_s);
  319. /*
  320. * This never happens, because the open/close paths in
  321. * file level maintain module use counters and so rmmod fails
  322. * before reaching here. However, better be safe...
  323. */
  324. if (mbus->nreaders) {
  325. printk(KERN_ERR TAG
  326. ": Outstanding opens (%d) on usb%d, leaking...\n",
  327. mbus->nreaders, mbus->u_bus->busnum);
  328. atomic_set(&mbus->ref.refcount, 2); /* Force leak */
  329. }
  330. mon_dissolve(mbus, mbus->u_bus);
  331. kref_put(&mbus->ref, mon_bus_drop);
  332. }
  333. mutex_unlock(&mon_lock);
  334. debugfs_remove(mon_dir);
  335. }
  336. module_init(mon_init);
  337. module_exit(mon_exit);
  338. MODULE_LICENSE("GPL");