usbgadget.init 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/sh /etc/rc.common
  2. START=19
  3. GADGET_FS=/sys/kernel/config/usb_gadget
  4. GADGET_PRESETS_DIR=/usr/share/usbgadget
  5. GADGET_PREFIX=owrt_
  6. log() {
  7. logger -t usbgadget "$@"
  8. }
  9. apply_configs() {
  10. local fs_path="$1"
  11. local cfg="$2"
  12. for param in $(find "$fs_path" -maxdepth 1 -type f -exec basename '{}' ';'); do
  13. [ "$param" = "UDC" ] && continue
  14. config_get val "$cfg" "$param"
  15. [ -n "$val" ] && echo "$val" > "${fs_path}/${param}"
  16. done
  17. }
  18. setup_gadget() {
  19. local cfg="$1"
  20. local gadget_path="${GADGET_FS}/${GADGET_PREFIX}${cfg}"
  21. local param udc
  22. config_get udc "$cfg" UDC
  23. [ -z "$udc" ] && return
  24. mkdir "$gadget_path" || return
  25. apply_configs "$gadget_path" "$cfg"
  26. local strings_path="${gadget_path}/strings/0x409"
  27. mkdir "$strings_path"
  28. apply_configs "$strings_path" "$cfg"
  29. local os_desc_path="${gadget_path}/os_desc"
  30. apply_configs "$os_desc_path" "$cfg"
  31. }
  32. setup_configuration() {
  33. local cfg="$1"
  34. local gadget
  35. config_get gadget "$cfg" gadget
  36. local cfgs_path="${GADGET_FS}/${GADGET_PREFIX}${gadget}/configs"
  37. [ -d "${cfgs_path}" ] || return
  38. local cfg_path="${cfgs_path}/${cfg}.1"
  39. mkdir "$cfg_path" || {
  40. log "failed to create configuration ${cfg}"
  41. return
  42. }
  43. apply_configs "$cfg_path" "$cfg"
  44. local strings_path="${cfg_path}/strings/0x409"
  45. mkdir "$strings_path"
  46. apply_configs "$strings_path" "$cfg"
  47. ln -s "$cfg_path" "${GADGET_FS}/${GADGET_PREFIX}${gadget}/os_desc"
  48. }
  49. setup_function() {
  50. local cfg="$1"
  51. local usbcfg gadget usbfun
  52. config_get usbcfg "$cfg" configuration
  53. [ -z "$usbcfg" ] && return
  54. config_get usbfun "$cfg" function
  55. [ -z "$usbfun" ] && return
  56. config_get gadget "$usbcfg" gadget
  57. local gadget_path="${GADGET_FS}/${GADGET_PREFIX}${gadget}"
  58. local cfg_path="${gadget_path}/configs/${usbcfg}.1"
  59. [ -d "${cfg_path}" ] || return
  60. local fun_path="${gadget_path}/functions/${usbfun}.${cfg}"
  61. mkdir "$fun_path" || {
  62. log "failed to create function ${usbfun}.${cfg}"
  63. return
  64. }
  65. apply_configs "$fun_path" "$cfg"
  66. local os_desc_path="$(find ${fun_path}/os_desc/* -type d)"
  67. apply_configs "$os_desc_path" "$cfg"
  68. ln -s "$fun_path" "$cfg_path"
  69. }
  70. attach_gadget() {
  71. local cfg="$1"
  72. local gadget_path="${GADGET_FS}/${GADGET_PREFIX}${cfg}"
  73. local param udc
  74. config_get udc "$cfg" UDC
  75. [ -z "$udc" ] && return
  76. echo "$udc" > "$gadget_path/UDC"
  77. }
  78. load_gadget() {
  79. config_foreach setup_gadget gadget
  80. config_foreach setup_configuration configuration
  81. config_foreach setup_function function
  82. config_foreach attach_gadget gadget
  83. }
  84. # use subshell for isolated env
  85. apply_preset() (
  86. local preset="$1"
  87. local gadget="$2"
  88. UCI_CONFIG_DIR=$GADGET_PRESETS_DIR config_load "$1"
  89. config_set g1 UDC "$2"
  90. load_gadget
  91. )
  92. load_preset() {
  93. local cfg="$1"
  94. config_get name "$cfg" name
  95. config_get udc "$cfg" UDC
  96. [ -z "$udc" ] && return
  97. apply_preset $name $udc
  98. }
  99. start() {
  100. grep -q /sys/kernel/config /proc/mounts || \
  101. mount -t configfs configfs /sys/kernel/config
  102. [ -d /sys/kernel/config/usb_gadget ] || {
  103. log "usb_gadget support not found."
  104. return 1
  105. }
  106. config_load usbgadget
  107. config_foreach load_preset preset
  108. load_gadget
  109. }
  110. stop() {
  111. for gadget_path in ${GADGET_FS}/${GADGET_PREFIX}* ; do
  112. [ -d "$gadget_path" ] || continue
  113. echo "" > ${gadget_path}/UDC
  114. find ${gadget_path}/os_desc -maxdepth 1 -type l -exec rm '{}' ';'
  115. find ${gadget_path}/configs -maxdepth 2 -type l -exec rm '{}' ';'
  116. rmdir ${gadget_path}/configs/*/strings/*
  117. rmdir ${gadget_path}/configs/*
  118. rmdir ${gadget_path}/functions/*
  119. rmdir ${gadget_path}/strings/*
  120. rmdir $gadget_path
  121. done
  122. }