libusual.c 6.6 KB

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