usbgadget.init 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  30. setup_configuration() {
  31. local cfg="$1"
  32. local gadget
  33. config_get gadget "$cfg" gadget
  34. local cfgs_path="${GADGET_FS}/${GADGET_PREFIX}${gadget}/configs"
  35. [ -d "${cfgs_path}" ] || return
  36. local cfg_path="${cfgs_path}/${cfg}.1"
  37. mkdir "$cfg_path" || {
  38. log "failed to create configuration ${cfg}"
  39. return
  40. }
  41. apply_configs "$cfg_path" "$cfg"
  42. local strings_path="${cfg_path}/strings/0x409"
  43. mkdir "$strings_path"
  44. apply_configs "$strings_path" "$cfg"
  45. }
  46. setup_function() {
  47. local cfg="$1"
  48. local usbcfg gadget usbfun
  49. config_get usbcfg "$cfg" configuration
  50. [ -z "$usbcfg" ] && return
  51. config_get usbfun "$cfg" function
  52. [ -z "$usbfun" ] && return
  53. config_get gadget "$usbcfg" gadget
  54. local gadget_path="${GADGET_FS}/${GADGET_PREFIX}${gadget}"
  55. local cfg_path="${gadget_path}/configs/${usbcfg}.1"
  56. [ -d "${cfg_path}" ] || return
  57. local fun_path="${gadget_path}/functions/${usbfun}.${cfg}"
  58. mkdir "$fun_path" || {
  59. log "failed to create function ${usbfun}.${cfg}"
  60. return
  61. }
  62. apply_configs "$fun_path" "$cfg"
  63. ln -s "$fun_path" "$cfg_path"
  64. }
  65. attach_gadget() {
  66. local cfg="$1"
  67. local gadget_path="${GADGET_FS}/${GADGET_PREFIX}${cfg}"
  68. local param udc
  69. config_get udc "$cfg" UDC
  70. [ -z "$udc" ] && return
  71. echo "$udc" > "$gadget_path/UDC"
  72. }
  73. load_gadget() {
  74. config_foreach setup_gadget gadget
  75. config_foreach setup_configuration configuration
  76. config_foreach setup_function function
  77. config_foreach attach_gadget gadget
  78. }
  79. # use subshell for isolated env
  80. apply_preset() (
  81. local preset="$1"
  82. local gadget="$2"
  83. UCI_CONFIG_DIR=$GADGET_PRESETS_DIR config_load "$1"
  84. config_set g1 UDC "$2"
  85. load_gadget
  86. )
  87. load_preset() {
  88. local cfg="$1"
  89. config_get name "$cfg" name
  90. config_get udc "$cfg" UDC
  91. [ -z "$udc" ] && return
  92. apply_preset $name $udc
  93. }
  94. start() {
  95. grep -q /sys/kernel/config /proc/mounts || \
  96. mount -t configfs configfs /sys/kernel/config
  97. [ -d /sys/kernel/config/usb_gadget ] || {
  98. log "usb_gadget support not found."
  99. return 1
  100. }
  101. config_load usbgadget
  102. config_foreach load_preset preset
  103. load_gadget
  104. }
  105. stop() {
  106. for gadget_path in ${GADGET_FS}/${GADGET_PREFIX}* ; do
  107. [ -d "$gadget_path" ] || continue
  108. echo "" > ${gadget_path}/UDC
  109. find ${gadget_path}/configs -maxdepth 2 -type l -exec rm '{}' ';'
  110. rmdir ${gadget_path}/configs/*/strings/*
  111. rmdir ${gadget_path}/configs/*
  112. rmdir ${gadget_path}/functions/*
  113. rmdir ${gadget_path}/strings/*
  114. rmdir $gadget_path
  115. done
  116. }