blob: 4b55285de9a07c2710e987a7c3a867a760d21386 [file] [log] [blame]
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001/*
2 * All the USB notify logic
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * notifier functions originally based on those in kernel/sys.c
7 * but fixed up to not be so broken.
8 *
9 */
10
11
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/notifier.h>
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070015#include <linux/usb.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010016#include <linux/mutex.h>
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070017#include "usb.h"
18
19
20static struct notifier_block *usb_notifier_list;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010021static DEFINE_MUTEX(usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070022
23static void usb_notifier_chain_register(struct notifier_block **list,
24 struct notifier_block *n)
25{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026 mutex_lock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070027 while (*list) {
28 if (n->priority > (*list)->priority)
29 break;
30 list = &((*list)->next);
31 }
32 n->next = *list;
33 *list = n;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010034 mutex_unlock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070035}
36
37static void usb_notifier_chain_unregister(struct notifier_block **nl,
38 struct notifier_block *n)
39{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010040 mutex_lock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070041 while ((*nl)!=NULL) {
42 if ((*nl)==n) {
43 *nl = n->next;
44 goto exit;
45 }
46 nl=&((*nl)->next);
47 }
48exit:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010049 mutex_unlock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070050}
51
52static int usb_notifier_call_chain(struct notifier_block **n,
53 unsigned long val, void *v)
54{
55 int ret=NOTIFY_DONE;
56 struct notifier_block *nb = *n;
57
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010058 mutex_lock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070059 while (nb) {
60 ret = nb->notifier_call(nb,val,v);
61 if (ret&NOTIFY_STOP_MASK) {
62 goto exit;
63 }
64 nb = nb->next;
65 }
66exit:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010067 mutex_unlock(&usb_notifier_lock);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -070068 return ret;
69}
70
71/**
72 * usb_register_notify - register a notifier callback whenever a usb change happens
73 * @nb: pointer to the notifier block for the callback events.
74 *
75 * These changes are either USB devices or busses being added or removed.
76 */
77void usb_register_notify(struct notifier_block *nb)
78{
79 usb_notifier_chain_register(&usb_notifier_list, nb);
80}
81EXPORT_SYMBOL_GPL(usb_register_notify);
82
83/**
84 * usb_unregister_notify - unregister a notifier callback
85 * @nb: pointer to the notifier block for the callback events.
86 *
87 * usb_register_notifier() must have been previously called for this function
88 * to work properly.
89 */
90void usb_unregister_notify(struct notifier_block *nb)
91{
92 usb_notifier_chain_unregister(&usb_notifier_list, nb);
93}
94EXPORT_SYMBOL_GPL(usb_unregister_notify);
95
96
97void usb_notify_add_device(struct usb_device *udev)
98{
99 usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_ADD, udev);
100}
101
102void usb_notify_remove_device(struct usb_device *udev)
103{
104 usb_notifier_call_chain(&usb_notifier_list, USB_DEVICE_REMOVE, udev);
105}
106
107void usb_notify_add_bus(struct usb_bus *ubus)
108{
109 usb_notifier_call_chain(&usb_notifier_list, USB_BUS_ADD, ubus);
110}
111
112void usb_notify_remove_bus(struct usb_bus *ubus)
113{
114 usb_notifier_call_chain(&usb_notifier_list, USB_BUS_REMOVE, ubus);
115}