blob: 599ad10a761b703da02f989eb51a88d0941ae0fd [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>
Pete Zaitceva00828e2005-10-22 20:15:09 -070012
13/*
14 */
15#define USU_MOD_FL_THREAD 1 /* Thread is running */
16#define USU_MOD_FL_PRESENT 2 /* The module is loaded */
17
18struct mod_status {
19 unsigned long fls;
20};
21
22static struct mod_status stat[3];
23static DEFINE_SPINLOCK(usu_lock);
24
25/*
26 */
27#define USB_US_DEFAULT_BIAS USB_US_TYPE_STOR
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -080028static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS);
Pete Zaitceva00828e2005-10-22 20:15:09 -070029
30#define BIAS_NAME_SIZE (sizeof("usb-storage"))
Pete Zaitceva00828e2005-10-22 20:15:09 -070031static const char *bias_names[3] = { "none", "usb-storage", "ub" };
32
33static DECLARE_MUTEX_LOCKED(usu_init_notify);
34static DECLARE_COMPLETION(usu_end_notify);
35static atomic_t total_threads = ATOMIC_INIT(0);
36
37static int usu_probe_thread(void *arg);
Pete Zaitceva00828e2005-10-22 20:15:09 -070038
39/*
40 * The table.
41 */
42#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
43 vendorName, productName,useProtocol, useTransport, \
44 initFunction, flags) \
45{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
46 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
47
48#define USUAL_DEV(useProto, useTrans, useType) \
49{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
50 .driver_info = ((useType)<<24) }
51
52struct usb_device_id storage_usb_ids [] = {
53# include "unusual_devs.h"
54 { } /* Terminating entry */
55};
56
57#undef USUAL_DEV
58#undef UNUSUAL_DEV
59
60MODULE_DEVICE_TABLE(usb, storage_usb_ids);
61EXPORT_SYMBOL_GPL(storage_usb_ids);
62
63/*
64 * @type: the module type as an integer
65 */
66void usb_usual_set_present(int type)
67{
68 struct mod_status *st;
69 unsigned long flags;
70
71 if (type <= 0 || type >= 3)
72 return;
73 st = &stat[type];
74 spin_lock_irqsave(&usu_lock, flags);
75 st->fls |= USU_MOD_FL_PRESENT;
76 spin_unlock_irqrestore(&usu_lock, flags);
77}
78EXPORT_SYMBOL_GPL(usb_usual_set_present);
79
80void usb_usual_clear_present(int type)
81{
82 struct mod_status *st;
83 unsigned long flags;
84
85 if (type <= 0 || type >= 3)
86 return;
87 st = &stat[type];
88 spin_lock_irqsave(&usu_lock, flags);
89 st->fls &= ~USU_MOD_FL_PRESENT;
90 spin_unlock_irqrestore(&usu_lock, flags);
91}
92EXPORT_SYMBOL_GPL(usb_usual_clear_present);
93
94/*
95 * Match the calling driver type against the table.
96 * Returns: 0 if the device matches.
97 */
98int usb_usual_check_type(const struct usb_device_id *id, int caller_type)
99{
100 int id_type = USB_US_TYPE(id->driver_info);
101
102 if (caller_type <= 0 || caller_type >= 3)
103 return -EINVAL;
104
105 /* Drivers grab fixed assignment devices */
106 if (id_type == caller_type)
107 return 0;
108 /* Drivers grab devices biased to them */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800109 if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias))
Pete Zaitceva00828e2005-10-22 20:15:09 -0700110 return 0;
111 return -ENODEV;
112}
113EXPORT_SYMBOL_GPL(usb_usual_check_type);
114
115/*
116 */
117static int usu_probe(struct usb_interface *intf,
118 const struct usb_device_id *id)
119{
Alan Cox6d453b92006-01-17 15:39:25 -0800120 unsigned long type;
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700121 struct task_struct* task;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700122 unsigned long flags;
123
124 type = USB_US_TYPE(id->driver_info);
125 if (type == 0)
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800126 type = atomic_read(&usu_bias);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700127
128 spin_lock_irqsave(&usu_lock, flags);
129 if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
130 spin_unlock_irqrestore(&usu_lock, flags);
131 return -ENXIO;
132 }
133 stat[type].fls |= USU_MOD_FL_THREAD;
134 spin_unlock_irqrestore(&usu_lock, flags);
135
Matthew Dharm0e3c8c22006-08-31 13:37:29 -0700136 task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
137 if (IS_ERR(task)) {
138 int rc = PTR_ERR(task);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700139 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 */
146 }
147 atomic_inc(&total_threads);
148
149 return -ENXIO;
150}
151
152static void usu_disconnect(struct usb_interface *intf)
153{
154 ; /* We should not be here. */
155}
156
157static struct usb_driver usu_driver = {
Pete Zaitceva00828e2005-10-22 20:15:09 -0700158 .name = "libusual",
159 .probe = usu_probe,
160 .disconnect = usu_disconnect,
161 .id_table = storage_usb_ids,
162};
163
164/*
165 * A whole new thread for a purpose of request_module seems quite stupid.
166 * The request_module forks once inside again. However, if we attempt
167 * to load a storage module from our own modprobe thread, that module
168 * references our symbols, which cannot be resolved until our module is
169 * initialized. I wish there was a way to wait for the end of initialization.
170 * The module notifier reports MODULE_STATE_COMING only.
171 * So, we wait until module->init ends as the next best thing.
172 */
173static int usu_probe_thread(void *arg)
174{
175 int type = (unsigned long) arg;
176 struct mod_status *st = &stat[type];
177 int rc;
178 unsigned long flags;
179
Pete Zaitceva00828e2005-10-22 20:15:09 -0700180 /* A completion does not work here because it's counted. */
181 down(&usu_init_notify);
182 up(&usu_init_notify);
183
184 rc = request_module(bias_names[type]);
185 spin_lock_irqsave(&usu_lock, flags);
186 if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
187 /*
188 * This should not happen, but let us keep tabs on it.
189 */
190 printk(KERN_NOTICE "libusual: "
191 "modprobe for %s succeeded, but module is not present\n",
192 bias_names[type]);
193 }
194 st->fls &= ~USU_MOD_FL_THREAD;
195 spin_unlock_irqrestore(&usu_lock, flags);
196
197 complete_and_exit(&usu_end_notify, 0);
198}
199
200/*
201 */
202static int __init usb_usual_init(void)
203{
204 int rc;
205
Pete Zaitceva00828e2005-10-22 20:15:09 -0700206 rc = usb_register(&usu_driver);
207 up(&usu_init_notify);
208 return rc;
209}
210
211static void __exit usb_usual_exit(void)
212{
213 /*
214 * We do not check for any drivers present, because
215 * they keep us pinned with symbol references.
216 */
217
218 usb_deregister(&usu_driver);
219
220 while (atomic_read(&total_threads) > 0) {
221 wait_for_completion(&usu_end_notify);
222 atomic_dec(&total_threads);
223 }
224}
225
226/*
227 * Validate and accept the bias parameter.
Pete Zaitceva00828e2005-10-22 20:15:09 -0700228 */
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800229static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
Pete Zaitceva00828e2005-10-22 20:15:09 -0700230{
231 int i;
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800232 int len;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700233 int bias_n = 0;
234
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800235 len = strlen(bias_s);
236 if (len == 0)
237 return -EDOM;
238 if (bias_s[len-1] == '\n')
239 --len;
240
241 for (i = 1; i < 3; i++) {
242 if (strncmp(bias_s, bias_names[i], len) == 0) {
243 bias_n = i;
244 break;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700245 }
246 }
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800247 if (bias_n == 0)
248 return -EINVAL;
249
250 atomic_set(&usu_bias, bias_n);
251 return 0;
252}
253
254static int usu_get_bias(char *buffer, struct kernel_param *kp)
255{
256 return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
Pete Zaitceva00828e2005-10-22 20:15:09 -0700257}
258
259module_init(usb_usual_init);
260module_exit(usb_usual_exit);
261
Pete Zaitcev5ba35bd2005-12-16 00:39:36 -0800262module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
263__MODULE_PARM_TYPE(bias, "string");
Pete Zaitceva00828e2005-10-22 20:15:09 -0700264MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
265
266MODULE_LICENSE("GPL");