libusual.c 6.3 KB

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