libusual.c 6.3 KB

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