| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 1 | /* | 
|  | 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> | 
|  | 11 |  | 
|  | 12 | /* | 
|  | 13 | */ | 
|  | 14 | #define USU_MOD_FL_THREAD   1	/* Thread is running */ | 
|  | 15 | #define USU_MOD_FL_PRESENT  2	/* The module is loaded */ | 
|  | 16 |  | 
|  | 17 | struct mod_status { | 
|  | 18 | unsigned long fls; | 
|  | 19 | }; | 
|  | 20 |  | 
|  | 21 | static struct mod_status stat[3]; | 
|  | 22 | static DEFINE_SPINLOCK(usu_lock); | 
|  | 23 |  | 
|  | 24 | /* | 
|  | 25 | */ | 
|  | 26 | #define USB_US_DEFAULT_BIAS	USB_US_TYPE_STOR | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 27 | static atomic_t usu_bias = ATOMIC_INIT(USB_US_DEFAULT_BIAS); | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #define BIAS_NAME_SIZE  (sizeof("usb-storage")) | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 30 | static const char *bias_names[3] = { "none", "usb-storage", "ub" }; | 
|  | 31 |  | 
|  | 32 | static DECLARE_MUTEX_LOCKED(usu_init_notify); | 
|  | 33 | static DECLARE_COMPLETION(usu_end_notify); | 
|  | 34 | static atomic_t total_threads = ATOMIC_INIT(0); | 
|  | 35 |  | 
|  | 36 | static int usu_probe_thread(void *arg); | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | /* | 
|  | 39 | * The table. | 
|  | 40 | */ | 
|  | 41 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ | 
|  | 42 | vendorName, productName,useProtocol, useTransport, \ | 
|  | 43 | initFunction, flags) \ | 
|  | 44 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \ | 
|  | 45 | .driver_info = (flags)|(USB_US_TYPE_STOR<<24) } | 
|  | 46 |  | 
|  | 47 | #define USUAL_DEV(useProto, useTrans, useType) \ | 
|  | 48 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \ | 
|  | 49 | .driver_info = ((useType)<<24) } | 
|  | 50 |  | 
|  | 51 | struct usb_device_id storage_usb_ids [] = { | 
|  | 52 | #	include "unusual_devs.h" | 
|  | 53 | { } /* Terminating entry */ | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | #undef USUAL_DEV | 
|  | 57 | #undef UNUSUAL_DEV | 
|  | 58 |  | 
|  | 59 | MODULE_DEVICE_TABLE(usb, storage_usb_ids); | 
|  | 60 | EXPORT_SYMBOL_GPL(storage_usb_ids); | 
|  | 61 |  | 
|  | 62 | /* | 
|  | 63 | * @type: the module type as an integer | 
|  | 64 | */ | 
|  | 65 | void usb_usual_set_present(int type) | 
|  | 66 | { | 
|  | 67 | struct mod_status *st; | 
|  | 68 | unsigned long flags; | 
|  | 69 |  | 
|  | 70 | if (type <= 0 || type >= 3) | 
|  | 71 | return; | 
|  | 72 | st = &stat[type]; | 
|  | 73 | spin_lock_irqsave(&usu_lock, flags); | 
|  | 74 | st->fls |= USU_MOD_FL_PRESENT; | 
|  | 75 | spin_unlock_irqrestore(&usu_lock, flags); | 
|  | 76 | } | 
|  | 77 | EXPORT_SYMBOL_GPL(usb_usual_set_present); | 
|  | 78 |  | 
|  | 79 | void usb_usual_clear_present(int type) | 
|  | 80 | { | 
|  | 81 | struct mod_status *st; | 
|  | 82 | unsigned long flags; | 
|  | 83 |  | 
|  | 84 | if (type <= 0 || type >= 3) | 
|  | 85 | return; | 
|  | 86 | st = &stat[type]; | 
|  | 87 | spin_lock_irqsave(&usu_lock, flags); | 
|  | 88 | st->fls &= ~USU_MOD_FL_PRESENT; | 
|  | 89 | spin_unlock_irqrestore(&usu_lock, flags); | 
|  | 90 | } | 
|  | 91 | EXPORT_SYMBOL_GPL(usb_usual_clear_present); | 
|  | 92 |  | 
|  | 93 | /* | 
|  | 94 | * Match the calling driver type against the table. | 
|  | 95 | * Returns: 0 if the device matches. | 
|  | 96 | */ | 
|  | 97 | int usb_usual_check_type(const struct usb_device_id *id, int caller_type) | 
|  | 98 | { | 
|  | 99 | int id_type = USB_US_TYPE(id->driver_info); | 
|  | 100 |  | 
|  | 101 | if (caller_type <= 0 || caller_type >= 3) | 
|  | 102 | return -EINVAL; | 
|  | 103 |  | 
|  | 104 | /* Drivers grab fixed assignment devices */ | 
|  | 105 | if (id_type == caller_type) | 
|  | 106 | return 0; | 
|  | 107 | /* Drivers grab devices biased to them */ | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 108 | if (id_type == USB_US_TYPE_NONE && caller_type == atomic_read(&usu_bias)) | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 109 | return 0; | 
|  | 110 | return -ENODEV; | 
|  | 111 | } | 
|  | 112 | EXPORT_SYMBOL_GPL(usb_usual_check_type); | 
|  | 113 |  | 
|  | 114 | /* | 
|  | 115 | */ | 
|  | 116 | static int usu_probe(struct usb_interface *intf, | 
|  | 117 | const struct usb_device_id *id) | 
|  | 118 | { | 
| Alan Cox | 6d453b9 | 2006-01-17 15:39:25 -0800 | [diff] [blame] | 119 | unsigned long type; | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 120 | int rc; | 
|  | 121 | unsigned long flags; | 
|  | 122 |  | 
|  | 123 | type = USB_US_TYPE(id->driver_info); | 
|  | 124 | if (type == 0) | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 125 | type = atomic_read(&usu_bias); | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | spin_lock_irqsave(&usu_lock, flags); | 
|  | 128 | if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) { | 
|  | 129 | spin_unlock_irqrestore(&usu_lock, flags); | 
|  | 130 | return -ENXIO; | 
|  | 131 | } | 
|  | 132 | stat[type].fls |= USU_MOD_FL_THREAD; | 
|  | 133 | spin_unlock_irqrestore(&usu_lock, flags); | 
|  | 134 |  | 
|  | 135 | rc = kernel_thread(usu_probe_thread, (void*)type, CLONE_VM); | 
|  | 136 | if (rc < 0) { | 
|  | 137 | printk(KERN_WARNING "libusual: " | 
|  | 138 | "Unable to start the thread for %s: %d\n", | 
|  | 139 | bias_names[type], rc); | 
|  | 140 | spin_lock_irqsave(&usu_lock, flags); | 
|  | 141 | stat[type].fls &= ~USU_MOD_FL_THREAD; | 
|  | 142 | spin_unlock_irqrestore(&usu_lock, flags); | 
|  | 143 | return rc;	/* Not being -ENXIO causes a message printed */ | 
|  | 144 | } | 
|  | 145 | atomic_inc(&total_threads); | 
|  | 146 |  | 
|  | 147 | return -ENXIO; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static void usu_disconnect(struct usb_interface *intf) | 
|  | 151 | { | 
|  | 152 | ;	/* We should not be here. */ | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | static struct usb_driver usu_driver = { | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 156 | .name =		"libusual", | 
|  | 157 | .probe =	usu_probe, | 
|  | 158 | .disconnect =	usu_disconnect, | 
|  | 159 | .id_table =	storage_usb_ids, | 
|  | 160 | }; | 
|  | 161 |  | 
|  | 162 | /* | 
|  | 163 | * A whole new thread for a purpose of request_module seems quite stupid. | 
|  | 164 | * The request_module forks once inside again. However, if we attempt | 
|  | 165 | * to load a storage module from our own modprobe thread, that module | 
|  | 166 | * references our symbols, which cannot be resolved until our module is | 
|  | 167 | * initialized. I wish there was a way to wait for the end of initialization. | 
|  | 168 | * The module notifier reports MODULE_STATE_COMING only. | 
|  | 169 | * So, we wait until module->init ends as the next best thing. | 
|  | 170 | */ | 
|  | 171 | static int usu_probe_thread(void *arg) | 
|  | 172 | { | 
|  | 173 | int type = (unsigned long) arg; | 
|  | 174 | struct mod_status *st = &stat[type]; | 
|  | 175 | int rc; | 
|  | 176 | unsigned long flags; | 
|  | 177 |  | 
|  | 178 | daemonize("libusual_%d", type);	/* "usb-storage" is kinda too long */ | 
|  | 179 |  | 
|  | 180 | /* 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 | */ | 
|  | 202 | static int __init usb_usual_init(void) | 
|  | 203 | { | 
|  | 204 | int rc; | 
|  | 205 |  | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 206 | rc = usb_register(&usu_driver); | 
|  | 207 | up(&usu_init_notify); | 
|  | 208 | return rc; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | static 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 Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 228 | */ | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 229 | static int usu_set_bias(const char *bias_s, struct kernel_param *kp) | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 230 | { | 
|  | 231 | int i; | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 232 | int len; | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 233 | int bias_n = 0; | 
|  | 234 |  | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 235 | 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 Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 245 | } | 
|  | 246 | } | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 247 | if (bias_n == 0) | 
|  | 248 | return -EINVAL; | 
|  | 249 |  | 
|  | 250 | atomic_set(&usu_bias, bias_n); | 
|  | 251 | return 0; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | static int usu_get_bias(char *buffer, struct kernel_param *kp) | 
|  | 255 | { | 
|  | 256 | return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)])); | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
|  | 259 | module_init(usb_usual_init); | 
|  | 260 | module_exit(usb_usual_exit); | 
|  | 261 |  | 
| Pete Zaitcev | 5ba35bd | 2005-12-16 00:39:36 -0800 | [diff] [blame] | 262 | module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR); | 
|  | 263 | __MODULE_PARM_TYPE(bias, "string"); | 
| Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 264 | MODULE_PARM_DESC(bias, "Bias to usb-storage or ub"); | 
|  | 265 |  | 
|  | 266 | MODULE_LICENSE("GPL"); |