4 * The libusual contains the table of devices common for ub and usb-storage.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/device.h>
10 #include <linux/usb_usual.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
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 */
24 static struct mod_status stat[3];
25 static DEFINE_SPINLOCK(usu_lock);
29 #define USB_US_DEFAULT_BIAS USB_US_TYPE_STOR
30 static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
32 #define BIAS_NAME_SIZE (sizeof("usb-storage"))
33 static const char *bias_names[3] = { "none", "usb-storage", "ub" };
35 static DECLARE_MUTEX_LOCKED(usu_init_notify);
36 static DECLARE_COMPLETION(usu_end_notify);
37 static atomic_t total_threads = ATOMIC_INIT(0);
39 static int usu_kick(unsigned long type);
40 static int usu_probe_thread(void *arg);
42 static struct class *usu_class;
43 static struct class_device *usu_class_device;
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) }
54 #define USUAL_DEV(useProto, useTrans, useType) \
55 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
56 .driver_info = ((useType)<<24) }
58 struct usb_device_id storage_usb_ids [] = {
59 # include "unusual_devs.h"
60 { } /* Terminating entry */
66 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
67 EXPORT_SYMBOL_GPL(storage_usb_ids);
70 * @type: the module type as an integer
72 void usb_usual_set_present(int type)
74 struct mod_status *st;
77 if (type <= 0 || type >= 3)
80 spin_lock_irqsave(&usu_lock, flags);
81 st->fls |= USU_MOD_FL_PRESENT;
82 spin_unlock_irqrestore(&usu_lock, flags);
84 EXPORT_SYMBOL_GPL(usb_usual_set_present);
86 void usb_usual_clear_present(int type)
88 struct mod_status *st;
91 if (type <= 0 || type >= 3)
94 spin_lock_irqsave(&usu_lock, flags);
95 st->fls &= ~USU_MOD_FL_PRESENT;
96 spin_unlock_irqrestore(&usu_lock, flags);
98 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
101 * Match the calling driver type against the table.
102 * Returns: 0 if the device matches.
104 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
106 int id_type = USB_US_TYPE(id->driver_info);
108 if (caller_type <= 0 || caller_type >= 3)
111 /* Drivers grab fixed assignment devices */
112 if (id_type == caller_type)
114 /* Drivers grab devices biased to them */
115 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
119 EXPORT_SYMBOL_GPL(usb_usual_check_type);
123 static int usu_uevent(struct class_device *class_dev,
124 char **envp, int num_envp, char *buffer, int buffer_size)
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);
136 spin_unlock_irqrestore(&usu_lock, flags);
144 static int usu_probe(struct usb_interface *intf,
145 const struct usb_device_id *id)
149 type = USB_US_TYPE(id->driver_info);
151 type = atomic_read(&usu_bias);
152 return usu_kick(type);
155 static int usu_kick(unsigned long type)
158 struct task_struct* task;
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);
165 stat[type].fls |= USU_MOD_FL_THREAD;
166 spin_unlock_irqrestore(&usu_lock, flags);
168 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
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 */
179 atomic_inc(&total_threads);
184 static void usu_disconnect(struct usb_interface *intf)
186 ; /* We should not be here. */
189 static struct usb_driver usu_driver = {
192 .disconnect = usu_disconnect,
193 .id_table = storage_usb_ids,
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.
205 static int usu_probe_thread(void *arg)
207 int type = (unsigned long) arg;
208 struct mod_status *st = &stat[type];
212 /* A completion does not work here because it's counted. */
213 down(&usu_init_notify);
214 up(&usu_init_notify);
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) {
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.
225 printk(KERN_NOTICE "libusual: "
226 "request for %s succeeded, but module is not present\n",
228 st->fls |= USU_MOD_FL_FAILED;
230 st->fls &= ~USU_MOD_FL_THREAD;
231 spin_unlock_irqrestore(&usu_lock, flags);
233 complete_and_exit(&usu_end_notify, 0);
238 static int __init usb_usual_init(void)
242 usu_class = class_create(THIS_MODULE, "libusual");
243 if (IS_ERR(usu_class)) {
244 rc = PTR_ERR(usu_class_device);
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);
252 usu_class_device->uevent = usu_uevent;
254 rc = usb_register(&usu_driver);
255 up(&usu_init_notify);
258 // class_device_destroy(usu_class, 0);
260 class_destroy(usu_class);
265 static void __exit usb_usual_exit(void)
268 * We do not check for any drivers present, because
269 * they keep us pinned with symbol references.
272 usb_deregister(&usu_driver);
274 while (atomic_read(&total_threads) > 0) {
275 wait_for_completion(&usu_end_notify);
276 atomic_dec(&total_threads);
279 class_device_destroy(usu_class, 0);
280 class_destroy(usu_class);
284 * Validate and accept the bias parameter.
286 static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
292 len = strlen(bias_s);
295 if (bias_s[len-1] == '\n')
298 for (i = 1; i < 3; i++) {
299 if (strncmp(bias_s, bias_names[i], len) == 0) {
307 atomic_set(&usu_bias, bias_n);
311 static int usu_get_bias(char *buffer, struct kernel_param *kp)
313 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
316 module_init(usb_usual_init);
317 module_exit(usb_usual_exit);
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");
323 MODULE_LICENSE("GPL");