mon_main.c 8.7 KB

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