mon_main.c 8.4 KB

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