[PATCH] USB: drivers/usb/storage/libusual
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / storage / libusual.c
blob61f73d8a2c0fb092019e013728ee0405ec6d18cb
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>
14 #define USU_MOD_FL_THREAD 1 /* Thread is running */
15 #define USU_MOD_FL_PRESENT 2 /* The module is loaded */
17 struct mod_status {
18 unsigned long fls;
21 static struct mod_status stat[3];
22 static DEFINE_SPINLOCK(usu_lock);
26 #define USB_US_DEFAULT_BIAS USB_US_TYPE_STOR
28 #define BIAS_NAME_SIZE (sizeof("usb-storage"))
29 static char bias[BIAS_NAME_SIZE];
30 static int usb_usual_bias;
31 static const char *bias_names[3] = { "none", "usb-storage", "ub" };
33 static DECLARE_MUTEX_LOCKED(usu_init_notify);
34 static DECLARE_COMPLETION(usu_end_notify);
35 static atomic_t total_threads = ATOMIC_INIT(0);
37 static int usu_probe_thread(void *arg);
38 static int parse_bias(const char *bias_s);
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 == usb_usual_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 type;
122 int rc;
123 unsigned long flags;
125 type = USB_US_TYPE(id->driver_info);
126 if (type == 0)
127 type = usb_usual_bias;
129 spin_lock_irqsave(&usu_lock, flags);
130 if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
131 spin_unlock_irqrestore(&usu_lock, flags);
132 return -ENXIO;
134 stat[type].fls |= USU_MOD_FL_THREAD;
135 spin_unlock_irqrestore(&usu_lock, flags);
137 rc = kernel_thread(usu_probe_thread, (void*)type, CLONE_VM);
138 if (rc < 0) {
139 printk(KERN_WARNING "libusual: "
140 "Unable to start the thread for %s: %d\n",
141 bias_names[type], rc);
142 spin_lock_irqsave(&usu_lock, flags);
143 stat[type].fls &= ~USU_MOD_FL_THREAD;
144 spin_unlock_irqrestore(&usu_lock, flags);
145 return rc; /* Not being -ENXIO causes a message printed */
147 atomic_inc(&total_threads);
149 return -ENXIO;
152 static void usu_disconnect(struct usb_interface *intf)
154 ; /* We should not be here. */
157 static struct usb_driver usu_driver = {
158 .owner = THIS_MODULE,
159 .name = "libusual",
160 .probe = usu_probe,
161 .disconnect = usu_disconnect,
162 .id_table = storage_usb_ids,
166 * A whole new thread for a purpose of request_module seems quite stupid.
167 * The request_module forks once inside again. However, if we attempt
168 * to load a storage module from our own modprobe thread, that module
169 * references our symbols, which cannot be resolved until our module is
170 * initialized. I wish there was a way to wait for the end of initialization.
171 * The module notifier reports MODULE_STATE_COMING only.
172 * So, we wait until module->init ends as the next best thing.
174 static int usu_probe_thread(void *arg)
176 int type = (unsigned long) arg;
177 struct mod_status *st = &stat[type];
178 int rc;
179 unsigned long flags;
181 daemonize("libusual_%d", type); /* "usb-storage" is kinda too long */
183 /* A completion does not work here because it's counted. */
184 down(&usu_init_notify);
185 up(&usu_init_notify);
187 rc = request_module(bias_names[type]);
188 spin_lock_irqsave(&usu_lock, flags);
189 if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
191 * This should not happen, but let us keep tabs on it.
193 printk(KERN_NOTICE "libusual: "
194 "modprobe for %s succeeded, but module is not present\n",
195 bias_names[type]);
197 st->fls &= ~USU_MOD_FL_THREAD;
198 spin_unlock_irqrestore(&usu_lock, flags);
200 complete_and_exit(&usu_end_notify, 0);
205 static int __init usb_usual_init(void)
207 int rc;
209 bias[BIAS_NAME_SIZE-1] = 0;
210 usb_usual_bias = parse_bias(bias);
212 rc = usb_register(&usu_driver);
213 up(&usu_init_notify);
214 return rc;
217 static void __exit usb_usual_exit(void)
220 * We do not check for any drivers present, because
221 * they keep us pinned with symbol references.
224 usb_deregister(&usu_driver);
226 while (atomic_read(&total_threads) > 0) {
227 wait_for_completion(&usu_end_notify);
228 atomic_dec(&total_threads);
233 * Validate and accept the bias parameter.
234 * Maybe make an sysfs method later. XXX
236 static int parse_bias(const char *bias_s)
238 int i;
239 int bias_n = 0;
241 if (bias_s[0] == 0 || bias_s[0] == ' ') {
242 bias_n = USB_US_DEFAULT_BIAS;
243 } else {
244 for (i = 1; i < 3; i++) {
245 if (strcmp(bias_s, bias_names[i]) == 0) {
246 bias_n = i;
247 break;
250 if (bias_n == 0) {
251 bias_n = USB_US_DEFAULT_BIAS;
252 printk(KERN_INFO
253 "libusual: unknown bias \"%s\", using \"%s\"\n",
254 bias_s, bias_names[bias_n]);
257 return bias_n;
260 module_init(usb_usual_init);
261 module_exit(usb_usual_exit);
263 module_param_string(bias, bias, BIAS_NAME_SIZE, S_IRUGO|S_IWUSR);
264 MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
266 MODULE_LICENSE("GPL");