PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / usb / storage / libusual.c
blobd617e8ae6b006b6d0b014774d86eb78bd1c9c6e3
1 /*
2 * libusual
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>
16 #define USU_MOD_FL_THREAD 1 /* Thread is running */
17 #define USU_MOD_FL_PRESENT 2 /* The module is loaded */
19 struct mod_status {
20 unsigned long fls;
23 static struct mod_status stat[3];
24 static DEFINE_SPINLOCK(usu_lock);
28 #define USB_US_DEFAULT_BIAS USB_US_TYPE_STOR
29 static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
31 #define BIAS_NAME_SIZE (sizeof("usb-storage"))
32 static const char *bias_names[3] = { "none", "usb-storage", "ub" };
34 static DEFINE_MUTEX(usu_probe_mutex);
35 static DECLARE_COMPLETION(usu_end_notify);
36 static atomic_t total_threads = ATOMIC_INIT(0);
38 static int usu_probe_thread(void *arg);
41 * The table.
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) }
49 #define USUAL_DEV(useProto, useTrans, useType) \
50 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
51 .driver_info = ((useType)<<24) }
53 struct usb_device_id storage_usb_ids [] = {
54 # include "unusual_devs.h"
55 { } /* Terminating entry */
58 #undef USUAL_DEV
59 #undef UNUSUAL_DEV
61 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
62 EXPORT_SYMBOL_GPL(storage_usb_ids);
65 * @type: the module type as an integer
67 void usb_usual_set_present(int type)
69 struct mod_status *st;
70 unsigned long flags;
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);
79 EXPORT_SYMBOL_GPL(usb_usual_set_present);
81 void usb_usual_clear_present(int type)
83 struct mod_status *st;
84 unsigned long flags;
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);
93 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
96 * Match the calling driver type against the table.
97 * Returns: 0 if the device matches.
99 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
101 int id_type = USB_US_TYPE(id->driver_info);
103 if (caller_type <= 0 || caller_type >= 3)
104 return -EINVAL;
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;
114 EXPORT_SYMBOL_GPL(usb_usual_check_type);
118 static int usu_probe(struct usb_interface *intf,
119 const struct usb_device_id *id)
121 int rc;
122 unsigned long type;
123 struct task_struct* task;
124 unsigned long flags;
126 type = USB_US_TYPE(id->driver_info);
127 if (type == 0)
128 type = atomic_read(&usu_bias);
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;
135 stat[type].fls |= USU_MOD_FL_THREAD;
136 spin_unlock_irqrestore(&usu_lock, flags);
138 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%ld", 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 */
149 atomic_inc(&total_threads);
151 return -ENXIO;
154 static void usu_disconnect(struct usb_interface *intf)
156 ; /* We should not be here. */
159 static struct usb_driver usu_driver = {
160 .name = "libusual",
161 .probe = usu_probe,
162 .disconnect = usu_disconnect,
163 .id_table = storage_usb_ids,
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.
175 static int usu_probe_thread(void *arg)
177 int type = (unsigned long) arg;
178 struct mod_status *st = &stat[type];
179 int rc;
180 unsigned long flags;
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) {
187 * This should not happen, but let us keep tabs on it.
189 printk(KERN_NOTICE "libusual: "
190 "modprobe for %s succeeded, but module is not present\n",
191 bias_names[type]);
193 st->fls &= ~USU_MOD_FL_THREAD;
194 spin_unlock_irqrestore(&usu_lock, flags);
195 mutex_unlock(&usu_probe_mutex);
197 complete_and_exit(&usu_end_notify, 0);
202 static int __init usb_usual_init(void)
204 int rc;
206 mutex_lock(&usu_probe_mutex);
207 rc = usb_register(&usu_driver);
208 mutex_unlock(&usu_probe_mutex);
209 return rc;
212 static void __exit usb_usual_exit(void)
215 * We do not check for any drivers present, because
216 * they keep us pinned with symbol references.
219 usb_deregister(&usu_driver);
221 while (atomic_read(&total_threads) > 0) {
222 wait_for_completion(&usu_end_notify);
223 atomic_dec(&total_threads);
228 * Validate and accept the bias parameter.
230 static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
232 int i;
233 int len;
234 int bias_n = 0;
236 len = strlen(bias_s);
237 if (len == 0)
238 return -EDOM;
239 if (bias_s[len-1] == '\n')
240 --len;
242 for (i = 1; i < 3; i++) {
243 if (strncmp(bias_s, bias_names[i], len) == 0) {
244 bias_n = i;
245 break;
248 if (bias_n == 0)
249 return -EINVAL;
251 atomic_set(&usu_bias, bias_n);
252 return 0;
255 static int usu_get_bias(char *buffer, struct kernel_param *kp)
257 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
260 module_init(usb_usual_init);
261 module_exit(usb_usual_exit);
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");
267 MODULE_LICENSE("GPL");