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. #define BIAS_NAME_SIZE (sizeof("usb-storage"))
  24. static char bias[BIAS_NAME_SIZE];
  25. static int usb_usual_bias;
  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. static int parse_bias(const char *bias_s);
  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 == usb_usual_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 type;
  102. int rc;
  103. unsigned long flags;
  104. type = USB_US_TYPE(id->driver_info);
  105. if (type == 0)
  106. type = usb_usual_bias;
  107. spin_lock_irqsave(&usu_lock, flags);
  108. if ((stat[type].fls & (USU_MOD_FL_THREAD|USU_MOD_FL_PRESENT)) != 0) {
  109. spin_unlock_irqrestore(&usu_lock, flags);
  110. return -ENXIO;
  111. }
  112. stat[type].fls |= USU_MOD_FL_THREAD;
  113. spin_unlock_irqrestore(&usu_lock, flags);
  114. rc = kernel_thread(usu_probe_thread, (void*)type, CLONE_VM);
  115. if (rc < 0) {
  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. .owner = THIS_MODULE,
  133. .name = "libusual",
  134. .probe = usu_probe,
  135. .disconnect = usu_disconnect,
  136. .id_table = storage_usb_ids,
  137. };
  138. /*
  139. * A whole new thread for a purpose of request_module seems quite stupid.
  140. * The request_module forks once inside again. However, if we attempt
  141. * to load a storage module from our own modprobe thread, that module
  142. * references our symbols, which cannot be resolved until our module is
  143. * initialized. I wish there was a way to wait for the end of initialization.
  144. * The module notifier reports MODULE_STATE_COMING only.
  145. * So, we wait until module->init ends as the next best thing.
  146. */
  147. static int usu_probe_thread(void *arg)
  148. {
  149. int type = (unsigned long) arg;
  150. struct mod_status *st = &stat[type];
  151. int rc;
  152. unsigned long flags;
  153. daemonize("libusual_%d", type); /* "usb-storage" is kinda too long */
  154. /* A completion does not work here because it's counted. */
  155. down(&usu_init_notify);
  156. up(&usu_init_notify);
  157. rc = request_module(bias_names[type]);
  158. spin_lock_irqsave(&usu_lock, flags);
  159. if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
  160. /*
  161. * This should not happen, but let us keep tabs on it.
  162. */
  163. printk(KERN_NOTICE "libusual: "
  164. "modprobe for %s succeeded, but module is not present\n",
  165. bias_names[type]);
  166. }
  167. st->fls &= ~USU_MOD_FL_THREAD;
  168. spin_unlock_irqrestore(&usu_lock, flags);
  169. complete_and_exit(&usu_end_notify, 0);
  170. }
  171. /*
  172. */
  173. static int __init usb_usual_init(void)
  174. {
  175. int rc;
  176. bias[BIAS_NAME_SIZE-1] = 0;
  177. usb_usual_bias = parse_bias(bias);
  178. rc = usb_register(&usu_driver);
  179. up(&usu_init_notify);
  180. return rc;
  181. }
  182. static void __exit usb_usual_exit(void)
  183. {
  184. /*
  185. * We do not check for any drivers present, because
  186. * they keep us pinned with symbol references.
  187. */
  188. usb_deregister(&usu_driver);
  189. while (atomic_read(&total_threads) > 0) {
  190. wait_for_completion(&usu_end_notify);
  191. atomic_dec(&total_threads);
  192. }
  193. }
  194. /*
  195. * Validate and accept the bias parameter.
  196. * Maybe make an sysfs method later. XXX
  197. */
  198. static int parse_bias(const char *bias_s)
  199. {
  200. int i;
  201. int bias_n = 0;
  202. if (bias_s[0] == 0 || bias_s[0] == ' ') {
  203. bias_n = USB_US_DEFAULT_BIAS;
  204. } else {
  205. for (i = 1; i < 3; i++) {
  206. if (strcmp(bias_s, bias_names[i]) == 0) {
  207. bias_n = i;
  208. break;
  209. }
  210. }
  211. if (bias_n == 0) {
  212. bias_n = USB_US_DEFAULT_BIAS;
  213. printk(KERN_INFO
  214. "libusual: unknown bias \"%s\", using \"%s\"\n",
  215. bias_s, bias_names[bias_n]);
  216. }
  217. }
  218. return bias_n;
  219. }
  220. module_init(usb_usual_init);
  221. module_exit(usb_usual_exit);
  222. module_param_string(bias, bias, BIAS_NAME_SIZE, S_IRUGO|S_IWUSR);
  223. MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
  224. MODULE_LICENSE("GPL");