mon_main.c 9.1 KB

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