blob: f970b27ba3083ef6cbd4ae62a477caab6d74eae8 [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
Alan Stern25ff1c32008-12-15 12:43:41 -050049#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) }
54
Pete Zaitceva00828e2005-10-22 20:15:09 -070055#define USUAL_DEV(useProto, useTrans, useType) \
56{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
57 .driver_info = ((useType)<<24) }
58
59struct usb_device_id storage_usb_ids [] = {
60# include "unusual_devs.h"
61 { } /* Terminating entry */
62};
63
64#undef USUAL_DEV
65#undef UNUSUAL_DEV
Alan Stern25ff1c32008-12-15 12:43:41 -050066#undef COMPLIANT_DEV
Pete Zaitceva00828e2005-10-22 20:15:09 -070067
68MODULE_DEVICE_TABLE(usb, storage_usb_ids);
69EXPORT_SYMBOL_GPL(storage_usb_ids);
70
71/*
72 * @type: the module type as an integer
73 */
74void usb_usual_set_present(int type)
75{
76 struct mod_status *st;
77 unsigned long flags;
78
79 if (type <= 0 || type >= 3)
80 return;
81 st = &stat[type];
82 spin_lock_irqsave(&usu_lock, flags);
83 st->fls |= USU_MOD_FL_PRESENT;
84 spin_unlock_irqrestore(&usu_lock, flags);
85}
86EXPORT_SYMBOL_GPL(usb_usual_set_present);
87
88void usb_usual_clear_present(int type)
89{
90 struct mod_status *st;
91 unsigned long flags;
92
93 if (type <= 0 || type >= 3)
94 return;
95 st = &stat[type];
96 spin_lock_irqsave(&usu_lock, flags);
97 st->fls &= ~USU_MOD_FL_PRESENT;
98 spin_unlock_irqrestore(&usu_lock, flags);
99}
100EXPORT_SYMBOL_GPL(usb_usual_clear_present);
101
102/*
103 * Match the calling driver type against the table.
104 * Returns: 0 if the device matches.
105 */
106int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
107{
108 int id_type = USB_US_TYPE(id->driver_info);
109
110 if (caller_type <= 0 || caller_type >= 3)
111 return -EINVAL;
112
113 /* Drivers grab fixed assignment devices */
114 if (id_type == caller_type)
115 return 0;
116 /* Drivers grab devices biased to them */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800117 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
Pete Zaitceva00828e2005-10-22 20:15:09 -0700118 return 0;
119 return -ENODEV;
120}
121EXPORT_SYMBOL_GPL(usb_usual_check_type);
122
123/*
124 */
125static int usu_probe(struct usb_interface *intf,
126 const struct usb_device_id *id)
127{
Pete Zaitcev64e35d92007-03-08 20:02:26 -0800128 int rc;
Alan Cox6d453b92006-01-17 15:39:25 -0800129 unsigned long type;
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700130 struct task_struct* task;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700131 unsigned long flags;
132
133 type = USB_US_TYPE(id->driver_info);
134 if (type == 0)
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800135 type = atomic_read(&usu_bias);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700136
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);
140 return -ENXIO;
141 }
142 stat[type].fls |= USU_MOD_FL_THREAD;
143 spin_unlock_irqrestore(&usu_lock, flags);
144
Rusty Russell04304212008-04-21 06:38:34 +1000145 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%ld", type);
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700146 if (IS_ERR(task)) {
Pete Zaitcev64e35d92007-03-08 20:02:26 -0800147 rc = PTR_ERR(task);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700148 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 */
155 }
156 atomic_inc(&total_threads);
157
158 return -ENXIO;
159}
160
161static void usu_disconnect(struct usb_interface *intf)
162{
163 ; /* We should not be here. */
164}
165
166static struct usb_driver usu_driver = {
Pete Zaitceva00828e2005-10-22 20:15:09 -0700167 .name = "libusual",
168 .probe = usu_probe,
169 .disconnect = usu_disconnect,
170 .id_table = storage_usb_ids,
171};
172
173/*
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.
181 */
182static int usu_probe_thread(void *arg)
183{
184 int type = (unsigned long) arg;
185 struct mod_status *st = &stat[type];
186 int rc;
187 unsigned long flags;
188
Daniel Walker75c43b62008-02-04 23:57:42 -0800189 mutex_lock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700190 rc = request_module(bias_names[type]);
191 spin_lock_irqsave(&usu_lock, flags);
192 if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
193 /*
194 * This should not happen, but let us keep tabs on it.
195 */
196 printk(KERN_NOTICE "libusual: "
197 "modprobe for %s succeeded, but module is not present\n",
198 bias_names[type]);
199 }
200 st->fls &= ~USU_MOD_FL_THREAD;
201 spin_unlock_irqrestore(&usu_lock, flags);
Daniel Walker75c43b62008-02-04 23:57:42 -0800202 mutex_unlock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700203
204 complete_and_exit(&usu_end_notify, 0);
205}
206
207/*
208 */
209static int __init usb_usual_init(void)
210{
211 int rc;
212
Daniel Walker75c43b62008-02-04 23:57:42 -0800213 mutex_lock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700214 rc = usb_register(&usu_driver);
Daniel Walker75c43b62008-02-04 23:57:42 -0800215 mutex_unlock(&usu_probe_mutex);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700216 return rc;
217}
218
219static void __exit usb_usual_exit(void)
220{
221 /*
222 * We do not check for any drivers present, because
223 * they keep us pinned with symbol references.
224 */
225
226 usb_deregister(&usu_driver);
227
228 while (atomic_read(&total_threads) > 0) {
229 wait_for_completion(&usu_end_notify);
230 atomic_dec(&total_threads);
231 }
232}
233
234/*
235 * Validate and accept the bias parameter.
Pete Zaitceva00828e2005-10-22 20:15:09 -0700236 */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800237static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
Pete Zaitceva00828e2005-10-22 20:15:09 -0700238{
239 int i;
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800240 int len;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700241 int bias_n = 0;
242
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800243 len = strlen(bias_s);
244 if (len == 0)
245 return -EDOM;
246 if (bias_s[len-1] == '\n')
247 --len;
248
249 for (i = 1; i < 3; i++) {
250 if (strncmp(bias_s, bias_names[i], len) == 0) {
251 bias_n = i;
252 break;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700253 }
254 }
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800255 if (bias_n == 0)
256 return -EINVAL;
257
258 atomic_set(&usu_bias, bias_n);
259 return 0;
260}
261
262static int usu_get_bias(char *buffer, struct kernel_param *kp)
263{
264 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
Pete Zaitceva00828e2005-10-22 20:15:09 -0700265}
266
267module_init(usb_usual_init);
268module_exit(usb_usual_exit);
269
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800270module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
271__MODULE_PARM_TYPE(bias, "string");
Pete Zaitceva00828e2005-10-22 20:15:09 -0700272MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
273
274MODULE_LICENSE("GPL");