blob: d617e8ae6b006b6d0b014774d86eb78bd1c9c6e3 [file] [log] [blame]
Pete Zaitceva00828e2005-10-22 20:15:09 -07001/*
2 * libusual
3 *
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>
Matthew Dharm0e3c8c22006-08-31 13:37:29 -070011#include <linux/kthread.h>
Daniel Walker75c43b62008-02-04 23:57:42 -080012#include <linux/mutex.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070013
14/*
15 */
16#define USU_MOD_FL_THREAD 1 /* Thread is running */
17#define USU_MOD_FL_PRESENT 2 /* The module is loaded */
18
19struct mod_status {
20 unsigned long fls;
21};
22
23static struct mod_status stat[3];
24static DEFINE_SPINLOCK(usu_lock);
25
26/*
27 */
28#define USB_US_DEFAULT_BIAS USB_US_TYPE_STOR
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -080029static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
Pete Zaitceva00828e2005-10-22 20:15:09 -070030
31#define BIAS_NAME_SIZE (sizeof("usb-storage"))
Pete Zaitceva00828e2005-10-22 20:15:09 -070032static const char *bias_names[3] = { "none", "usb-storage", "ub" };
33
Daniel Walker75c43b62008-02-04 23:57:42 -080034static DEFINE_MUTEX(usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -070035static DECLARE_COMPLETION(usu_end_notify);
36static atomic_t total_threads = ATOMIC_INIT(0);
37
38static int usu_probe_thread(void *arg);
Pete Zaitceva00828e2005-10-22 20:15:09 -070039
40/*
41 * The table.
42 */
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) }
48
49#define USUAL_DEV(useProto, useTrans, useType) \
50{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
51 .driver_info = ((useType)<<24) }
52
53struct usb_device_id storage_usb_ids [] = {
54# include "unusual_devs.h"
55 { } /* Terminating entry */
56};
57
58#undef USUAL_DEV
59#undef UNUSUAL_DEV
60
61MODULE_DEVICE_TABLE(usb, storage_usb_ids);
62EXPORT_SYMBOL_GPL(storage_usb_ids);
63
64/*
65 * @type: the module type as an integer
66 */
67void usb_usual_set_present(int type)
68{
69 struct mod_status *st;
70 unsigned long flags;
71
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);
78}
79EXPORT_SYMBOL_GPL(usb_usual_set_present);
80
81void usb_usual_clear_present(int type)
82{
83 struct mod_status *st;
84 unsigned long flags;
85
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);
92}
93EXPORT_SYMBOL_GPL(usb_usual_clear_present);
94
95/*
96 * Match the calling driver type against the table.
97 * Returns: 0 if the device matches.
98 */
99int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
100{
101 int id_type = USB_US_TYPE(id->driver_info);
102
103 if (caller_type <= 0 || caller_type >= 3)
104 return -EINVAL;
105
106 /* Drivers grab fixed assignment devices */
107 if (id_type == caller_type)
108 return 0;
109 /* Drivers grab devices biased to them */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800110 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
Pete Zaitceva00828e2005-10-22 20:15:09 -0700111 return 0;
112 return -ENODEV;
113}
114EXPORT_SYMBOL_GPL(usb_usual_check_type);
115
116/*
117 */
118static int usu_probe(struct usb_interface *intf,
119 const struct usb_device_id *id)
120{
Pete Zaitcev64e35d92007-03-08 20:02:26 -0800121 int rc;
Alan Cox6d453b92006-01-17 15:39:25 -0800122 unsigned long type;
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700123 struct task_struct* task;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700124 unsigned long flags;
125
126 type = USB_US_TYPE(id->driver_info);
127 if (type == 0)
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800128 type = atomic_read(&usu_bias);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700129
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;
134 }
135 stat[type].fls |= USU_MOD_FL_THREAD;
136 spin_unlock_irqrestore(&usu_lock, flags);
137
Rusty Russell04304212008-04-21 06:38:34 +1000138 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%ld", type);
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700139 if (IS_ERR(task)) {
Pete Zaitcev64e35d92007-03-08 20:02:26 -0800140 rc = PTR_ERR(task);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700141 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 */
148 }
149 atomic_inc(&total_threads);
150
151 return -ENXIO;
152}
153
154static void usu_disconnect(struct usb_interface *intf)
155{
156 ; /* We should not be here. */
157}
158
159static struct usb_driver usu_driver = {
Pete Zaitceva00828e2005-10-22 20:15:09 -0700160 .name = "libusual",
161 .probe = usu_probe,
162 .disconnect = usu_disconnect,
163 .id_table = storage_usb_ids,
164};
165
166/*
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.
174 */
175static int usu_probe_thread(void *arg)
176{
177 int type = (unsigned long) arg;
178 struct mod_status *st = &stat[type];
179 int rc;
180 unsigned long flags;
181
Daniel Walker75c43b62008-02-04 23:57:42 -0800182 mutex_lock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700183 rc = request_module(bias_names[type]);
184 spin_lock_irqsave(&usu_lock, flags);
185 if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
186 /*
187 * This should not happen, but let us keep tabs on it.
188 */
189 printk(KERN_NOTICE "libusual: "
190 "modprobe for %s succeeded, but module is not present\n",
191 bias_names[type]);
192 }
193 st->fls &= ~USU_MOD_FL_THREAD;
194 spin_unlock_irqrestore(&usu_lock, flags);
Daniel Walker75c43b62008-02-04 23:57:42 -0800195 mutex_unlock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700196
197 complete_and_exit(&usu_end_notify, 0);
198}
199
200/*
201 */
202static int __init usb_usual_init(void)
203{
204 int rc;
205
Daniel Walker75c43b62008-02-04 23:57:42 -0800206 mutex_lock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700207 rc = usb_register(&usu_driver);
Daniel Walker75c43b62008-02-04 23:57:42 -0800208 mutex_unlock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700209 return rc;
210}
211
212static void __exit usb_usual_exit(void)
213{
214 /*
215 * We do not check for any drivers present, because
216 * they keep us pinned with symbol references.
217 */
218
219 usb_deregister(&usu_driver);
220
221 while (atomic_read(&total_threads) > 0) {
222 wait_for_completion(&usu_end_notify);
223 atomic_dec(&total_threads);
224 }
225}
226
227/*
228 * Validate and accept the bias parameter.
Pete Zaitceva00828e2005-10-22 20:15:09 -0700229 */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800230static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
Pete Zaitceva00828e2005-10-22 20:15:09 -0700231{
232 int i;
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800233 int len;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700234 int bias_n = 0;
235
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800236 len = strlen(bias_s);
237 if (len == 0)
238 return -EDOM;
239 if (bias_s[len-1] == '\n')
240 --len;
241
242 for (i = 1; i < 3; i++) {
243 if (strncmp(bias_s, bias_names[i], len) == 0) {
244 bias_n = i;
245 break;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700246 }
247 }
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800248 if (bias_n == 0)
249 return -EINVAL;
250
251 atomic_set(&usu_bias, bias_n);
252 return 0;
253}
254
255static int usu_get_bias(char *buffer, struct kernel_param *kp)
256{
257 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
Pete Zaitceva00828e2005-10-22 20:15:09 -0700258}
259
260module_init(usb_usual_init);
261module_exit(usb_usual_exit);
262
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800263module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
264__MODULE_PARM_TYPE(bias, "string");
Pete Zaitceva00828e2005-10-22 20:15:09 -0700265MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
266
267MODULE_LICENSE("GPL");