mon_main.c 9.2 KB

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