fedora core 6 1.2949 + vserver 2.2.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 #include <linux/kthread.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 #define USU_MOD_FL_FAILED   4   /* The module failed to load */
19
20 struct mod_status {
21         unsigned long fls;
22 };
23
24 static struct mod_status stat[3];
25 static DEFINE_SPINLOCK(usu_lock);
26
27 /*
28  */
29 #define USB_US_DEFAULT_BIAS     USB_US_TYPE_STOR
30 static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
31
32 #define BIAS_NAME_SIZE  (sizeof("usb-storage"))
33 static const char *bias_names[3] = { "none", "usb-storage", "ub" };
34
35 static DECLARE_MUTEX_LOCKED(usu_init_notify);
36 static DECLARE_COMPLETION(usu_end_notify);
37 static atomic_t total_threads = ATOMIC_INIT(0);
38
39 static int usu_kick(unsigned long type);
40 static int usu_probe_thread(void *arg);
41
42 static struct class *usu_class;
43 static struct class_device *usu_class_device;
44
45 /*
46  * The table.
47  */
48 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
49                     vendorName, productName,useProtocol, useTransport, \
50                     initFunction, flags) \
51 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
52   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
53
54 #define USUAL_DEV(useProto, useTrans, useType) \
55 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
56   .driver_info = ((useType)<<24) }
57
58 struct usb_device_id storage_usb_ids [] = {
59 #       include "unusual_devs.h"
60         { } /* Terminating entry */
61 };
62
63 #undef USUAL_DEV
64 #undef UNUSUAL_DEV
65
66 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
67 EXPORT_SYMBOL_GPL(storage_usb_ids);
68
69 /*
70  * @type: the module type as an integer
71  */
72 void usb_usual_set_present(int type)
73 {
74         struct mod_status *st;
75         unsigned long flags;
76
77         if (type <= 0 || type >= 3)
78                 return;
79         st = &stat[type];
80         spin_lock_irqsave(&usu_lock, flags);
81         st->fls |= USU_MOD_FL_PRESENT;
82         spin_unlock_irqrestore(&usu_lock, flags);
83 }
84 EXPORT_SYMBOL_GPL(usb_usual_set_present);
85
86 void usb_usual_clear_present(int type)
87 {
88         struct mod_status *st;
89         unsigned long flags;
90
91         if (type <= 0 || type >= 3)
92                 return;
93         st = &stat[type];
94         spin_lock_irqsave(&usu_lock, flags);
95         st->fls &= ~USU_MOD_FL_PRESENT;
96         spin_unlock_irqrestore(&usu_lock, flags);
97 }
98 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
99
100 /*
101  * Match the calling driver type against the table.
102  * Returns: 0 if the device matches.
103  */
104 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
105 {
106         int id_type = USB_US_TYPE(id->driver_info);
107
108         if (caller_type <= 0 || caller_type >= 3)
109                 return -EINVAL;
110
111         /* Drivers grab fixed assignment devices */
112         if (id_type == caller_type)
113                 return 0;
114         /* Drivers grab devices biased to them */
115         if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
116                 return 0;
117         return -ENODEV;
118 }
119 EXPORT_SYMBOL_GPL(usb_usual_check_type);
120
121 /*
122  */
123 static int usu_uevent(struct class_device *class_dev,
124     char **envp, int num_envp, char *buffer, int buffer_size)
125 {
126         unsigned long flags;
127         int i;
128
129         for (i = 1; i < 3; i++) {
130                 spin_lock_irqsave(&usu_lock, flags);
131                 if (stat[i].fls & USU_MOD_FL_FAILED) {
132                         stat[i].fls &= ~USU_MOD_FL_FAILED;
133                         spin_unlock_irqrestore(&usu_lock, flags);
134                         usu_kick(i);
135                 } else {
136                         spin_unlock_irqrestore(&usu_lock, flags);
137                 }
138         }
139         return 0;
140 }
141
142 /*
143  */
144 static int usu_probe(struct usb_interface *intf,
145                          const struct usb_device_id *id)
146 {
147         unsigned long type;
148
149         type = USB_US_TYPE(id->driver_info);
150         if (type == 0)
151                 type = atomic_read(&usu_bias);
152         return usu_kick(type);
153 }
154
155 static int usu_kick(unsigned long type)
156 {
157         unsigned long flags;
158         struct task_struct* task;
159
160         spin_lock_irqsave(&usu_lock, flags);
161         if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
162                 spin_unlock_irqrestore(&usu_lock, flags);
163                 return -ENXIO;
164         }
165         stat[type].fls |= USU_MOD_FL_THREAD;
166         spin_unlock_irqrestore(&usu_lock, flags);
167
168         task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
169         if (IS_ERR(task)) {
170                 int rc = PTR_ERR(task);
171                 printk(KERN_WARNING "libusual: "
172                     "Unable to start the thread for %s: %d\n",
173                     bias_names[type], rc);
174                 spin_lock_irqsave(&usu_lock, flags);
175                 stat[type].fls &= ~USU_MOD_FL_THREAD;
176                 spin_unlock_irqrestore(&usu_lock, flags);
177                 return rc;      /* Not being -ENXIO causes a message printed */
178         }
179         atomic_inc(&total_threads);
180
181         return -ENXIO;
182 }
183
184 static void usu_disconnect(struct usb_interface *intf)
185 {
186         ;       /* We should not be here. */
187 }
188
189 static struct usb_driver usu_driver = {
190         .name =         "libusual",
191         .probe =        usu_probe,
192         .disconnect =   usu_disconnect,
193         .id_table =     storage_usb_ids,
194 };
195
196 /*
197  * A whole new thread for a purpose of request_module seems quite stupid.
198  * The request_module forks once inside again. However, if we attempt
199  * to load a storage module from our own modprobe thread, that module
200  * references our symbols, which cannot be resolved until our module is
201  * initialized. I wish there was a way to wait for the end of initialization.
202  * The module notifier reports MODULE_STATE_COMING only.
203  * So, we wait until module->init ends as the next best thing.
204  */
205 static int usu_probe_thread(void *arg)
206 {
207         int type = (unsigned long) arg;
208         struct mod_status *st = &stat[type];
209         int rc;
210         unsigned long flags;
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");