tcm_usb_gadget.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Target based USB-Gadget
  3. *
  4. * UAS protocol handling, target callbacks, configfs handling,
  5. * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
  6. *
  7. * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/configfs.h>
  14. #include <linux/ctype.h>
  15. #include <linux/usb/ch9.h>
  16. #include <linux/usb/composite.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/storage.h>
  19. #include <scsi/scsi_tcq.h>
  20. #include <target/target_core_base.h>
  21. #include <target/target_core_fabric.h>
  22. #include <asm/unaligned.h>
  23. #include "u_tcm.h"
  24. USB_GADGET_COMPOSITE_OPTIONS();
  25. #define UAS_VENDOR_ID 0x0525 /* NetChip */
  26. #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  27. static struct usb_device_descriptor usbg_device_desc = {
  28. .bLength = sizeof(usbg_device_desc),
  29. .bDescriptorType = USB_DT_DEVICE,
  30. /* .bcdUSB = DYNAMIC */
  31. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  32. .idVendor = cpu_to_le16(UAS_VENDOR_ID),
  33. .idProduct = cpu_to_le16(UAS_PRODUCT_ID),
  34. .bNumConfigurations = 1,
  35. };
  36. #define USB_G_STR_CONFIG USB_GADGET_FIRST_AVAIL_IDX
  37. static struct usb_string usbg_us_strings[] = {
  38. [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufacturer",
  39. [USB_GADGET_PRODUCT_IDX].s = "Target Product",
  40. [USB_GADGET_SERIAL_IDX].s = "000000000001",
  41. [USB_G_STR_CONFIG].s = "default config",
  42. { },
  43. };
  44. static struct usb_gadget_strings usbg_stringtab = {
  45. .language = 0x0409,
  46. .strings = usbg_us_strings,
  47. };
  48. static struct usb_gadget_strings *usbg_strings[] = {
  49. &usbg_stringtab,
  50. NULL,
  51. };
  52. static struct usb_function_instance *fi_tcm;
  53. static struct usb_function *f_tcm;
  54. static int guas_unbind(struct usb_composite_dev *cdev)
  55. {
  56. if (!IS_ERR_OR_NULL(f_tcm))
  57. usb_put_function(f_tcm);
  58. return 0;
  59. }
  60. static int tcm_do_config(struct usb_configuration *c)
  61. {
  62. int status;
  63. f_tcm = usb_get_function(fi_tcm);
  64. if (IS_ERR(f_tcm))
  65. return PTR_ERR(f_tcm);
  66. status = usb_add_function(c, f_tcm);
  67. if (status < 0) {
  68. usb_put_function(f_tcm);
  69. return status;
  70. }
  71. return 0;
  72. }
  73. static struct usb_configuration usbg_config_driver = {
  74. .label = "Linux Target",
  75. .bConfigurationValue = 1,
  76. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  77. };
  78. static int usbg_attach(struct usb_function_instance *f);
  79. static void usbg_detach(struct usb_function_instance *f);
  80. static int usb_target_bind(struct usb_composite_dev *cdev)
  81. {
  82. int ret;
  83. ret = usb_string_ids_tab(cdev, usbg_us_strings);
  84. if (ret)
  85. return ret;
  86. usbg_device_desc.iManufacturer =
  87. usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
  88. usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
  89. usbg_device_desc.iSerialNumber =
  90. usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
  91. usbg_config_driver.iConfiguration =
  92. usbg_us_strings[USB_G_STR_CONFIG].id;
  93. ret = usb_add_config(cdev, &usbg_config_driver, tcm_do_config);
  94. if (ret)
  95. return ret;
  96. usb_composite_overwrite_options(cdev, &coverwrite);
  97. return 0;
  98. }
  99. static struct usb_composite_driver usbg_driver = {
  100. .name = "g_target",
  101. .dev = &usbg_device_desc,
  102. .strings = usbg_strings,
  103. .max_speed = USB_SPEED_SUPER,
  104. .bind = usb_target_bind,
  105. .unbind = guas_unbind,
  106. };
  107. static int usbg_attach(struct usb_function_instance *f)
  108. {
  109. return usb_composite_probe(&usbg_driver);
  110. }
  111. static void usbg_detach(struct usb_function_instance *f)
  112. {
  113. usb_composite_unregister(&usbg_driver);
  114. }
  115. static int __init usb_target_gadget_init(void)
  116. {
  117. struct f_tcm_opts *tcm_opts;
  118. fi_tcm = usb_get_function_instance("tcm");
  119. if (IS_ERR(fi_tcm))
  120. return PTR_ERR(fi_tcm);
  121. tcm_opts = container_of(fi_tcm, struct f_tcm_opts, func_inst);
  122. mutex_lock(&tcm_opts->dep_lock);
  123. tcm_opts->tcm_register_callback = usbg_attach;
  124. tcm_opts->tcm_unregister_callback = usbg_detach;
  125. tcm_opts->dependent = THIS_MODULE;
  126. tcm_opts->can_attach = true;
  127. tcm_opts->has_dep = true;
  128. mutex_unlock(&tcm_opts->dep_lock);
  129. fi_tcm->set_inst_name(fi_tcm, "tcm-legacy");
  130. return 0;
  131. }
  132. module_init(usb_target_gadget_init);
  133. static void __exit usb_target_gadget_exit(void)
  134. {
  135. if (!IS_ERR_OR_NULL(fi_tcm))
  136. usb_put_function_instance(fi_tcm);
  137. }
  138. module_exit(usb_target_gadget_exit);
  139. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
  140. MODULE_DESCRIPTION("usb-gadget fabric");
  141. MODULE_LICENSE("GPL v2");