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