libusual.c 5.7 KB

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