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