USB: libusual: locking cleanup
[pandora-kernel.git] / drivers / usb / storage / libusual.c
1 /*
2  * libusual
3  *
4  * The libusual contains the table of devices common for ub and usb-storage.
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/usb.h>
9 #include <linux/usb_usual.h>
10 #include <linux/vmalloc.h>
11 #include <linux/kthread.h>
12 #include <linux/mutex.h>
13
14 /*
15  */
16 #define USU_MOD_FL_THREAD   1   /* Thread is running */
17 #define USU_MOD_FL_PRESENT  2   /* The module is loaded */
18
19 struct mod_status {
20         unsigned long fls;
21 };
22
23 static struct mod_status stat[3];
24 static DEFINE_SPINLOCK(usu_lock);
25
26 /*
27  */
28 #define USB_US_DEFAULT_BIAS     USB_US_TYPE_STOR
29 static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
30
31 #define BIAS_NAME_SIZE  (sizeof("usb-storage"))
32 static const char *bias_names[3] = { "none", "usb-storage", "ub" };
33
34 static DEFINE_MUTEX(usu_probe_mutex);
35 static DECLARE_COMPLETION(usu_end_notify);
36 static atomic_t total_threads = ATOMIC_INIT(0);
37
38 static int usu_probe_thread(void *arg);
39
40 /*
41  * The table.
42  */
43 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
44                     vendorName, productName,useProtocol, useTransport, \
45                     initFunction, flags) \
46 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
47   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
48
49 #define USUAL_DEV(useProto, useTrans, useType) \
50 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
51   .driver_info = ((useType)<<24) }
52
53 struct usb_device_id storage_usb_ids [] = {
54 #       include "unusual_devs.h"
55         { } /* Terminating entry */
56 };
57
58 #undef USUAL_DEV
59 #undef UNUSUAL_DEV
60
61 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
62 EXPORT_SYMBOL_GPL(storage_usb_ids);
63
64 /*
65  * @type: the module type as an integer
66  */
67 void usb_usual_set_present(int type)
68 {
69         struct mod_status *st;
70         unsigned long flags;
71
72         if (type <= 0 || type >= 3)
73                 return;
74         st = &stat[type];
75         spin_lock_irqsave(&usu_lock, flags);
76         st->fls |= USU_MOD_FL_PRESENT;
77         spin_unlock_irqrestore(&usu_lock, flags);
78 }
79 EXPORT_SYMBOL_GPL(usb_usual_set_present);
80
81 void usb_usual_clear_present(int type)
82 {
83         struct mod_status *st;
84         unsigned long flags;
85
86         if (type <= 0 || type >= 3)
87                 return;
88         st = &stat[type];
89         spin_lock_irqsave(&usu_lock, flags);
90         st->fls &= ~USU_MOD_FL_PRESENT;
91         spin_unlock_irqrestore(&usu_lock, flags);
92 }
93 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
94
95 /*
96  * Match the calling driver type against the table.
97  * Returns: 0 if the device matches.
98  */
99 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
100 {
101         int id_type = USB_US_TYPE(id->driver_info);
102
103         if (caller_type <= 0 || caller_type >= 3)
104                 return -EINVAL;
105
106         /* Drivers grab fixed assignment devices */
107         if (id_type == caller_type)
108                 return 0;
109         /* Drivers grab devices biased to them */
110         if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
111                 return 0;
112         return -ENODEV;
113 }
114 EXPORT_SYMBOL_GPL(usb_usual_check_type);
115
116 /*
117  */
118 static int usu_probe(struct usb_interface *intf,
119                          const struct usb_device_id *id)
120 {
121         int rc;
122         unsigned long type;
123         struct task_struct* task;
124         unsigned long flags;
125
126         type = USB_US_TYPE(id->driver_info);
127         if (type == 0)
128                 type = atomic_read(&usu_bias);
129
130         spin_lock_irqsave(&usu_lock, flags);
131         if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
132                 spin_unlock_irqrestore(&usu_lock, flags);
133                 return -ENXIO;
134         }
135         stat[type].fls |= USU_MOD_FL_THREAD;
136         spin_unlock_irqrestore(&usu_lock, flags);
137
138         task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
139         if (IS_ERR(task)) {
140                 rc = PTR_ERR(task);
141                 printk(KERN_WARNING "libusual: "
142                     "Unable to start the thread for %s: %d\n",
143                     bias_names[type], rc);
144                 spin_lock_irqsave(&usu_lock, flags);
145                 stat[type].fls &= ~USU_MOD_FL_THREAD;
146                 spin_unlock_irqrestore(&usu_lock, flags);
147                 return rc;      /* Not being -ENXIO causes a message printed */
148         }
149         atomic_inc(&total_threads);
150
151         return -ENXIO;
152 }
153
154 static void usu_disconnect(struct usb_interface *intf)
155 {
156         ;       /* We should not be here. */
157 }
158
159 static struct usb_driver usu_driver = {
160         .name =         "libusual",
161         .probe =        usu_probe,
162         .disconnect =   usu_disconnect,
163         .id_table =     storage_usb_ids,
164 };
165
166 /*
167  * A whole new thread for a purpose of request_module seems quite stupid.
168  * The request_module forks once inside again. However, if we attempt
169  * to load a storage module from our own modprobe thread, that module
170  * references our symbols, which cannot be resolved until our module is
171  * initialized. I wish there was a way to wait for the end of initialization.
172  * The module notifier reports MODULE_STATE_COMING only.
173  * So, we wait until module->init ends as the next best thing.
174  */
175 static int usu_probe_thread(void *arg)
176 {
177         int type = (unsigned long) arg;
178         struct mod_status *st = &stat[type];
179         int rc;
180         unsigned long flags;
181
182         mutex_lock(&usu_probe_mutex);
183         rc = request_module(bias_names[type]);
184         spin_lock_irqsave(&usu_lock, flags);
185         if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
186                 /*
187                  * This should not happen, but let us keep tabs on it.
188                  */
189                 printk(KERN_NOTICE "libusual: "
190                     "modprobe for %s succeeded, but module is not present\n",
191                     bias_names[type]);
192         }
193         st->fls &= ~USU_MOD_FL_THREAD;
194         spin_unlock_irqrestore(&usu_lock, flags);
195         mutex_unlock(&usu_probe_mutex);
196
197         complete_and_exit(&usu_end_notify, 0);
198 }
199
200 /*
201  */
202 static int __init usb_usual_init(void)
203 {
204         int rc;
205
206         mutex_lock(&usu_probe_mutex);
207         rc = usb_register(&usu_driver);
208         mutex_unlock(&usu_probe_mutex);
209         return rc;
210 }
211
212 static void __exit usb_usual_exit(void)
213 {
214         /*
215          * We do not check for any drivers present, because
216          * they keep us pinned with symbol references.
217          */
218
219         usb_deregister(&usu_driver);
220
221         while (atomic_read(&total_threads) > 0) {
222                 wait_for_completion(&usu_end_notify);
223                 atomic_dec(&total_threads);
224         }
225 }
226
227 /*
228  * Validate and accept the bias parameter.
229  */
230 static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
231 {
232         int i;
233         int len;
234         int bias_n = 0;
235
236         len = strlen(bias_s);
237         if (len == 0)
238                 return -EDOM;
239         if (bias_s[len-1] == '\n')
240                 --len;
241
242         for (i = 1; i < 3; i++) {
243                 if (strncmp(bias_s, bias_names[i], len) == 0) {
244                         bias_n = i;
245                         break;
246                 }
247         }
248         if (bias_n == 0)
249                 return -EINVAL;
250
251         atomic_set(&usu_bias, bias_n);
252         return 0;
253 }
254
255 static int usu_get_bias(char *buffer, struct kernel_param *kp)
256 {
257         return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
258 }
259
260 module_init(usb_usual_init);
261 module_exit(usb_usual_exit);
262
263 module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
264 __MODULE_PARM_TYPE(bias, "string");
265 MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
266
267 MODULE_LICENSE("GPL");