4 * The libusual contains the table of devices common for ub and usb-storage.
6 #include <linux/kernel.h>
7 #include <linux/module.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 */
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);
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 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) }
55 #define USUAL_DEV(useProto, useTrans, useType) \
56 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
57 .driver_info = ((useType)<<24) }
59 struct usb_device_id storage_usb_ids [] = {
60 # include "unusual_devs.h"
61 { } /* Terminating entry */
68 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
69 EXPORT_SYMBOL_GPL(storage_usb_ids);
72 * @type: the module type as an integer
74 void usb_usual_set_present(int type)
76 struct mod_status *st;
79 if (type <= 0 || type >= 3)
82 spin_lock_irqsave(&usu_lock, flags);
83 st->fls |= USU_MOD_FL_PRESENT;
84 spin_unlock_irqrestore(&usu_lock, flags);
86 EXPORT_SYMBOL_GPL(usb_usual_set_present);
88 void usb_usual_clear_present(int type)
90 struct mod_status *st;
93 if (type <= 0 || type >= 3)
96 spin_lock_irqsave(&usu_lock, flags);
97 st->fls &= ~USU_MOD_FL_PRESENT;
98 spin_unlock_irqrestore(&usu_lock, flags);
100 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
103 * Match the calling driver type against the table.
104 * Returns: 0 if the device matches.
106 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
108 int id_type = USB_US_TYPE(id->driver_info);
110 if (caller_type <= 0 || caller_type >= 3)
113 /* Drivers grab fixed assignment devices */
114 if (id_type == caller_type)
116 /* Drivers grab devices biased to them */
117 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
121 EXPORT_SYMBOL_GPL(usb_usual_check_type);
125 static int usu_probe(struct usb_interface *intf,
126 const struct usb_device_id *id)
130 struct task_struct* task;
133 type = USB_US_TYPE(id->driver_info);
135 type = atomic_read(&usu_bias);
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);
142 stat[type].fls |= USU_MOD_FL_THREAD;
143 spin_unlock_irqrestore(&usu_lock, flags);
145 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%ld", type);
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 */
156 atomic_inc(&total_threads);
161 static void usu_disconnect(struct usb_interface *intf)
163 ; /* We should not be here. */
166 static struct usb_driver usu_driver = {
169 .disconnect = usu_disconnect,
170 .id_table = storage_usb_ids,
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.
182 static int usu_probe_thread(void *arg)
184 int type = (unsigned long) arg;
185 struct mod_status *st = &stat[type];
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) {
194 * This should not happen, but let us keep tabs on it.
196 printk(KERN_NOTICE "libusual: "
197 "modprobe for %s succeeded, but module is not present\n",
200 st->fls &= ~USU_MOD_FL_THREAD;
201 spin_unlock_irqrestore(&usu_lock, flags);
202 mutex_unlock(&usu_probe_mutex);
204 complete_and_exit(&usu_end_notify, 0);
209 static int __init usb_usual_init(void)
213 mutex_lock(&usu_probe_mutex);
214 rc = usb_register(&usu_driver);
215 mutex_unlock(&usu_probe_mutex);
219 static void __exit usb_usual_exit(void)
222 * We do not check for any drivers present, because
223 * they keep us pinned with symbol references.
226 usb_deregister(&usu_driver);
228 while (atomic_read(&total_threads) > 0) {
229 wait_for_completion(&usu_end_notify);
230 atomic_dec(&total_threads);
235 * Validate and accept the bias parameter.
237 static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
243 len = strlen(bias_s);
246 if (bias_s[len-1] == '\n')
249 for (i = 1; i < 3; i++) {
250 if (strncmp(bias_s, bias_names[i], len) == 0) {
258 atomic_set(&usu_bias, bias_n);
262 static int usu_get_bias(char *buffer, struct kernel_param *kp)
264 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
267 module_init(usb_usual_init);
268 module_exit(usb_usual_exit);
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");
274 MODULE_LICENSE("GPL");