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. /*
  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. int rc;
  101. unsigned long type;
  102. struct task_struct* task;
  103. unsigned long flags;
  104. type = USB_US_TYPE(id->driver_info);
  105. if (type == 0)
  106. type = atomic_read(&usu_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. task = kthread_run(usu_probe_thread, (void*)type, "libusual_%d", type);
  115. if (IS_ERR(task)) {
  116. rc = PTR_ERR(task);
  117. printk(KERN_WARNING "libusual: "
  118. "Unable to start the thread for %s: %d\n",
  119. bias_names[type], rc);
  120. spin_lock_irqsave(&usu_lock, flags);
  121. stat[type].fls &= ~USU_MOD_FL_THREAD;
  122. spin_unlock_irqrestore(&usu_lock, flags);
  123. return rc; /* Not being -ENXIO causes a message printed */
  124. }
  125. atomic_inc(&total_threads);
  126. return -ENXIO;
  127. }
  128. static void usu_disconnect(struct usb_interface *intf)
  129. {
  130. ; /* We should not be here. */
  131. }
  132. static struct usb_driver usu_driver = {
  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. /* A completion does not work here because it's counted. */
  154. down(&usu_init_notify);
  155. up(&usu_init_notify);
  156. rc = request_module(bias_names[type]);
  157. spin_lock_irqsave(&usu_lock, flags);
  158. if (rc == 0 && (st->fls & USU_MOD_FL_PRESENT) == 0) {
  159. /*
  160. * This should not happen, but let us keep tabs on it.
  161. */
  162. printk(KERN_NOTICE "libusual: "
  163. "modprobe for %s succeeded, but module is not present\n",
  164. bias_names[type]);
  165. }
  166. st->fls &= ~USU_MOD_FL_THREAD;
  167. spin_unlock_irqrestore(&usu_lock, flags);
  168. complete_and_exit(&usu_end_notify, 0);
  169. }
  170. /*
  171. */
  172. static int __init usb_usual_init(void)
  173. {
  174. int rc;
  175. rc = usb_register(&usu_driver);
  176. up(&usu_init_notify);
  177. return rc;
  178. }
  179. static void __exit usb_usual_exit(void)
  180. {
  181. /*
  182. * We do not check for any drivers present, because
  183. * they keep us pinned with symbol references.
  184. */
  185. usb_deregister(&usu_driver);
  186. while (atomic_read(&total_threads) > 0) {
  187. wait_for_completion(&usu_end_notify);
  188. atomic_dec(&total_threads);
  189. }
  190. }
  191. /*
  192. * Validate and accept the bias parameter.
  193. */
  194. static int usu_set_bias(const char *bias_s, struct kernel_param *kp)
  195. {
  196. int i;
  197. int len;
  198. int bias_n = 0;
  199. len = strlen(bias_s);
  200. if (len == 0)
  201. return -EDOM;
  202. if (bias_s[len-1] == '\n')
  203. --len;
  204. for (i = 1; i < 3; i++) {
  205. if (strncmp(bias_s, bias_names[i], len) == 0) {
  206. bias_n = i;
  207. break;
  208. }
  209. }
  210. if (bias_n == 0)
  211. return -EINVAL;
  212. atomic_set(&usu_bias, bias_n);
  213. return 0;
  214. }
  215. static int usu_get_bias(char *buffer, struct kernel_param *kp)
  216. {
  217. return strlen(strcpy(buffer, bias_names[atomic_read(&usu_bias)]));
  218. }
  219. module_init(usb_usual_init);
  220. module_exit(usb_usual_exit);
  221. module_param_call(bias, usu_set_bias, usu_get_bias, NULL, S_IRUGO|S_IWUSR);
  222. __MODULE_PARM_TYPE(bias, "string");
  223. MODULE_PARM_DESC(bias, "Bias to usb-storage or ub");
  224. MODULE_LICENSE("GPL");