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