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>
15 #define USU_MOD_FL_THREAD 1 /* Thread is running */
16 #define USU_MOD_FL_PRESENT 2 /* The module is loaded */
17 #define USU_MOD_FL_FAILED 4 /* The module failed to load */
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 DECLARE_MUTEX_LOCKED(usu_init_notify);
35 static DECLARE_COMPLETION(usu_end_notify);
36 static atomic_t total_threads = ATOMIC_INIT(0);
38 static int usu_kick(unsigned long type);
39 static int usu_probe_thread(void *arg);
41 static struct class *usu_class;
42 static struct class_device *usu_class_device;
47 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
48 vendorName, productName,useProtocol, useTransport, \
49 initFunction, flags) \
50 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
51 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
53 #define USUAL_DEV(useProto, useTrans, useType) \
54 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
55 .driver_info = ((useType)<<24) }
57 struct usb_device_id storage_usb_ids [] = {
58 # include "unusual_devs.h"
59 { } /* Terminating entry */
65 MODULE_DEVICE_TABLE(usb, storage_usb_ids);
66 EXPORT_SYMBOL_GPL(storage_usb_ids);
69 * @type: the module type as an integer
71 void usb_usual_set_present(int type)
73 struct mod_status *st;
76 if (type <= 0 || type >= 3)
79 spin_lock_irqsave(&usu_lock, flags);
80 st->fls |= USU_MOD_FL_PRESENT;
81 spin_unlock_irqrestore(&usu_lock, flags);
83 EXPORT_SYMBOL_GPL(usb_usual_set_present);
85 void usb_usual_clear_present(int type)
87 struct mod_status *st;
90 if (type <= 0 || type >= 3)
93 spin_lock_irqsave(&usu_lock, flags);
94 st->fls &= ~USU_MOD_FL_PRESENT;
95 spin_unlock_irqrestore(&usu_lock, flags);
97 EXPORT_SYMBOL_GPL(usb_usual_clear_present);
100 * Match the calling driver type against the table.
101 * Returns: 0 if the device matches.
103 int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
105 int id_type = USB_US_TYPE(id->driver_info);
107 if (caller_type <= 0 || caller_type >= 3)
110 /* Drivers grab fixed assignment devices */
111 if (id_type == caller_type)
113 /* Drivers grab devices biased to them */
114 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
118 EXPORT_SYMBOL_GPL(usb_usual_check_type);
122 static int usu_uevent(struct class_device *class_dev,
123 char **envp, int num_envp, char *buffer, int buffer_size)
128 for (i = 1; i < 3; i++) {
129 spin_lock_irqsave(&usu_lock, flags);
130 if (stat[i].fls & USU_MOD_FL_FAILED) {
131 stat[i].fls &= ~USU_MOD_FL_FAILED;
132 spin_unlock_irqrestore(&usu_lock, flags);
135 spin_unlock_irqrestore(&usu_lock, flags);
143 static int usu_probe(struct usb_interface *intf,
144 const struct usb_device_id *id)
148 type = USB_US_TYPE(id->driver_info);
150 type = atomic_read(&usu_bias);
151 return usu_kick(type);
154 static int usu_kick(unsigned long type)
159 spin_lock_irqsave(&usu_lock, flags);
160 if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
161 spin_unlock_irqrestore(&usu_lock, flags);
164 stat[type].fls |= USU_MOD_FL_THREAD;
165 spin_unlock_irqrestore(&usu_lock, flags);
167 rc = kernel_thread(usu_probe_thread, (void*)type, CLONE_VM);
169 printk(KERN_WARNING "libusual: "
170 "Unable to start the thread for %s: %d\n",
171 bias_names[type], rc);
172 spin_lock_irqsave(&usu_lock, flags);
173 stat[type].fls &= ~USU_MOD_FL_THREAD;
174 spin_unlock_irqrestore(&usu_lock, flags);
175 return rc; /* Not being -ENXIO causes a message printed */
177 atomic_inc(&total_threads);
182 static void usu_disconnect(struct usb_interface *intf)
184 ; /* We should not be here. */
187 static struct usb_driver usu_driver = {
190 .disconnect = usu_disconnect,
191 .id_table = storage_usb_ids,
195 * A whole new thread for a purpose of request_module seems quite stupid.
196 * The request_module forks once inside again. However, if we attempt
197 * to load a storage module from our own modprobe thread, that module
198 * references our symbols, which cannot be resolved until our module is
199 * initialized. I wish there was a way to wait for the end of initialization.
200 * The module notifier reports MODULE_STATE_COMING only.
201 * So, we wait until module->init ends as the next best thing.
203 static int usu_probe_thread(void *arg)
205 int type = (unsigned long) arg;
206 struct mod_status *st = &stat[type];
210 daemonize("libusual_%d", type); /* "usb-storage" is kinda too long */
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");