Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.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/device.h>
9 #include <linux/usb.h>
10 #include <linux/usb_usual.h>
11 #include <linux/vmalloc.h>
12
13 /*
14  */
15 #define USU_MOD_FL_THREAD   1   /* Thread is running */
16 #define USU_MOD_FL_PRESENT  2   /* The module is loaded */
17 #define USU_MOD_FL_FAILED   4   /* The module failed to load */
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 DECLARE_MUTEX_LOCKED(usu_init_notify);
35 static DECLARE_COMPLETION(usu_end_notify);
36 static atomic_t total_threads = ATOMIC_INIT(0);
37
38 static int usu_kick(unsigned long type);
39 static int usu_probe_thread(void *arg);
40
41 static struct class *usu_class;
42 static struct class_device *usu_class_device;
43
44 /*
45  * The table.
46  */
47 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
48                     vendorName, productName,useProtocol, useTransport, \
49                     initFunction, flags) \
50 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
51   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
52
53 #define USUAL_DEV(useProto, useTrans, useType) \
54 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
55   .driver_info = ((useType)<<24) }
56
57 struct usb_device_id storage_usb_ids [] = {
58 #       include "unusual_devs.h"
59         { } /* Terminating entry */
60 };
61
62 #undef USUAL_DEV
63 #undef UNUSUAL_DEV
64
65 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
66 EXPORT_SYMBOL_GPL(storage_usb_ids);
67
68 /*
69  * @type: the module type as an integer
70  */
71 void usb_usual_set_present(int type)
72 {
73         struct mod_status *st;
74         unsigned long flags;
75
76         if (type <= 0 || type >= 3)
77                 return;
78         st = &stat[type];
79         spin_lock_irqsave(&usu_lock, flags);
80         st->fls |= USU_MOD_FL_PRESENT;
81         spin_unlock_irqrestore(&usu_lock, flags);
82 }
83 EXPORT_SYMBOL_GPL(usb_usual_set_present);
84
85 void usb_usual_clear_present(int type)
86 {
87         struct mod_status *st;
88         unsigned long flags;
89
90         if (type <= 0 || type >= 3)
91                 return;
92         st = &stat[type];
93         spin_lock_irqsave(&usu_lock, flags);
94         st->fls &= ~USU_MOD_FL_PRESENT;
95         spin_unlock_irqrestore(&usu_lock, flags);
96 }
97 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
98
99 /*
100  * Match the calling driver type against the table.
101  * Returns: 0 if the device matches.
102  */
103 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
104 {
105         int id_type = USB_US_TYPE(id->driver_info);
106
107         if (caller_type <= 0 || caller_type >= 3)
108                 return -EINVAL;
109
110         /* Drivers grab fixed assignment devices */
111         if (id_type == caller_type)
112                 return 0;
113         /* Drivers grab devices biased to them */
114         if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
115                 return 0;
116         return -ENODEV;
117 }
118 EXPORT_SYMBOL_GPL(usb_usual_check_type);
119
120 /*
121  */
122 static int usu_uevent(struct class_device *class_dev,
123     char **envp, int num_envp, char *buffer, int buffer_size)
124 {
125         unsigned long flags;
126         int i;
127
128         for (i = 1; i < 3; i++) {
129                 spin_lock_irqsave(&usu_lock, flags);
130                 if (stat[i].fls & USU_MOD_FL_FAILED) {
131                         stat[i].fls &= ~USU_MOD_FL_FAILED;
132                         spin_unlock_irqrestore(&usu_lock, flags);
133                         usu_kick(i);
134                 } else {
135                         spin_unlock_irqrestore(&usu_lock, flags);
136                 }
137         }
138         return 0;
139 }
140
141 /*
142  */
143 static int usu_probe(struct usb_interface *intf,
144                          const struct usb_device_id *id)
145 {
146         unsigned long type;
147
148         type = USB_US_TYPE(id->driver_info);
149         if (type == 0)
150                 type = atomic_read(&usu_bias);
151         return usu_kick(type);
152 }
153
154 static int usu_kick(unsigned long type)
155 {
156         int rc;
157         unsigned long flags;
158
159         spin_lock_irqsave(&usu_lock, flags);
160         if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
161                 spin_unlock_irqrestore(&usu_lock, flags);
162                 return -ENXIO;
163         }
164         stat[type].fls |= USU_MOD_FL_THREAD;
165         spin_unlock_irqrestore(&usu_lock, flags);
166
167         rc = kernel_thread(usu_probe_thread, (void*)type, CLONE_VM);
168         if (rc < 0) {
169                 printk(KERN_WARNING "libusual: "
170                     "Unable to start the thread for %s: %d\n",
171                     bias_names[type], rc);
172                 spin_lock_irqsave(&usu_lock, flags);
173                 stat[type].fls &= ~USU_MOD_FL_THREAD;
174                 spin_unlock_irqrestore(&usu_lock, flags);
175                 return rc;      /* Not being -ENXIO causes a message printed */
176         }
177         atomic_inc(&total_threads);
178
179         return -ENXIO;
180 }
181
182 static void usu_disconnect(struct usb_interface *intf)
183 {
184         ;       /* We should not be here. */
185 }
186
187 static struct usb_driver usu_driver = {
188         .name =         "libusual",
189         .probe =        usu_probe,
190         .disconnect =   usu_disconnect,
191         .id_table =     storage_usb_ids,
192 };
193
194 /*
195  * A whole new thread for a purpose of request_module seems quite stupid.
196  * The request_module forks once inside again. However, if we attempt
197  * to load a storage module from our own modprobe thread, that module
198  * references our symbols, which cannot be resolved until our module is
199  * initialized. I wish there was a way to wait for the end of initialization.
200  * The module notifier reports MODULE_STATE_COMING only.
201  * So, we wait until module->init ends as the next best thing.
202  */
203 static int usu_probe_thread(void *arg)
204 {
205         int type = (unsigned long) arg;
206         struct mod_status *st = &stat[type];
207         int rc;
208         unsigned long flags;
209
210         daemonize("libusual_%d", type); /* "usb-storage" is kinda too long */
211
212         /* A completion does not work here because it's counted. */
213         down(&usu_init_notify);
214         up(&usu_init_notify);
215
216         rc = request_module(bias_names[type]);
217         spin_lock_irqsave(&usu_lock, flags);
218         if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
219                 /*
220                  * This should not happen, but let us keep tabs on it.
221                  * One common source of this a user who builds USB statically,
222                  * then uses initrd, and has a USB device. When static devices
223                  * are probed, request_module() calls a fake modprobe and fails.
224                  */
225                 printk(KERN_NOTICE "libusual: "
226                     "request for %s succeeded, but module is not present\n",
227                     bias_names[type]);
228                 st->fls |= USU_MOD_FL_FAILED;
229         }
230         st->fls &= ~USU_MOD_FL_THREAD;
231         spin_unlock_irqrestore(&usu_lock, flags);
232
233         complete_and_exit(&usu_end_notify, 0);
234 }
235
236 /*
237  */
238 static int __init usb_usual_init(void)
239 {
240         int rc;
241
242         usu_class = class_create(THIS_MODULE, "libusual");
243         if (IS_ERR(usu_class)) {
244                 rc = PTR_ERR(usu_class_device);
245                 goto err_class;
246         }
247         usu_class_device = class_device_create(usu_class, NULL, 0, NULL, "0");
248         if (IS_ERR(usu_class_device)) {
249                 rc = PTR_ERR(usu_class_device);
250                 goto err_classdev;
251         }
252         usu_class_device->uevent = usu_uevent;
253
254         rc = usb_register(&usu_driver);
255         up(&usu_init_notify);
256         return rc;
257
258         // class_device_destroy(usu_class, 0);
259 err_classdev:
260         class_destroy(usu_class);
261 err_class:
262         return rc;
263 }
264
265 static void __exit usb_usual_exit(void)
266 {
267         /*
268          * We do not check for any drivers present, because
269          * they keep us pinned with symbol references.
270          */
271
272         usb_deregister(&usu_driver);
273
274         while (atomic_read(&total_threads) > 0) {
275                 wait_for_completion(&usu_end_notify);
276                 atomic_dec(&total_threads);
277         }
278
279         class_device_destroy(usu_class, 0);
280         class_destroy(usu_class);
281 }
282
283 /*
284  * Validate and accept the bias parameter.
285  */
286 static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
287 {
288         int i;
289         int len;
290         int bias_n = 0;
291
292         len = strlen(bias_s);
293         if (len == 0)
294                 return -EDOM;
295         if (bias_s[len-1] == '\n')
296                 --len;
297
298         for (i = 1; i < 3; i++) {
299                 if (strncmp(bias_s, bias_names[i], len) == 0) {
300                         bias_n = i;
301                         break;
302                 }
303         }
304         if (bias_n == 0)
305                 return -EINVAL;
306
307         atomic_set(&usu_bias, bias_n);
308         return 0;
309 }
310
311 static int usu_get_bias(char *buffer, struct kernel_param *kp)
312 {
313         return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
314 }
315
316 module_init(usb_usual_init);
317 module_exit(usb_usual_exit);
318
319 module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
320 __MODULE_PARM_TYPE(bias, "string");
321 MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
322
323 MODULE_LICENSE("GPL");