adb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/sh
  2. #set -xv
  3. SELF=${SELF:-$(basename $0)}
  4. source /usr/local/lib/utils
  5. source usb_gadget
  6. # The composite gadget directory
  7. GADGET=/sys/kernel/config/usb_gadget/FunKey
  8. # USB VID for Intel
  9. ID_VENDOR="0x8087"
  10. # USB PID for Multifunction Composite Gadget
  11. ID_PRODUCT="0x011e"
  12. # Get the CPU serial number
  13. SERIAL="$(grep Serial /proc/cpuinfo | sed 's/Serial\s*: \(\w*\)/\1/')"
  14. # Initialize the ADB
  15. init_adb() {
  16. # Don't proceed if existing gadget is present
  17. if [ -e ${GADGET} ]; then
  18. return 0
  19. fi
  20. # Get the legacy drivers out of the way
  21. modprobe -r g_ether
  22. modprobe -r g_mass_storage
  23. # Load the libcomposite USB driver, configfs and various other drivers
  24. modprobe libcomposite
  25. modprobe usb_f_serial
  26. modprobe usb_f_fs
  27. modprobe usb_f_acm
  28. # USB Device Controller Driver
  29. local udc_driver=$(ls /sys/class/udc | cut -f1 | head -n 1)
  30. # Create our gadget directory
  31. mkdir ${GADGET}
  32. mkdir ${GADGET}/strings/0x409
  33. mkdir ${GADGET}/configs/FunKey.1
  34. mkdir ${GADGET}/configs/FunKey.1/strings/0x409
  35. mkdir ${GADGET}/functions/acm.GS0
  36. mkdir ${GADGET}/functions/ffs.adb
  37. # USB VID and PID
  38. echo ${ID_VENDOR} > ${GADGET}/idVendor
  39. echo ${ID_PRODUCT} > ${GADGET}/idProduct
  40. # Device String Descriptiors
  41. echo "Intel" > ${GADGET}/strings/0x409/manufacturer
  42. echo "FunKey S" > ${GADGET}/strings/0x409/product
  43. echo ${SERIAL} > ${GADGET}/strings/0x409/serialnumber
  44. # Configuration
  45. # Maximum power is 120 mA
  46. echo 120 > ${GADGET}/configs/FunKey.1/MaxPower
  47. # Configuration String Descriptors
  48. echo "ADB+CDC" > ${GADGET}/configs/FunKey.1/strings/0x409/configuration
  49. # Add the ACM function to the FunKey.1 configuration
  50. ln -s ${GADGET}/functions/acm.GS0 ${GADGET}/configs/FunKey.1
  51. # Add the FunctionFS function to the FunKey.1 configuration
  52. ln -s ${GADGET}/functions/ffs.adb ${GADGET}/configs/FunKey.1
  53. # Create the function filesystem
  54. mkdir /dev/usb-ffs
  55. mkdir /dev/usb-ffs/adb
  56. # Mount the ADB function filesystem
  57. mount -t functionfs adb /dev/usb-ffs/adb
  58. # Bring up the loopback network
  59. ifup lo
  60. # Launch the ADB daemon
  61. adbd >/dev/null &
  62. # Sleeping is required to wait for the UDC to come up
  63. sleep 5
  64. # Bind the USB Gadget
  65. echo ${udc_driver} > ${GADGET}/UDC
  66. return 0
  67. }
  68. # Deinitialize the ADB
  69. deinit_adb() {
  70. # Unbind the device
  71. echo > ${GADGET}/UDC
  72. # Kill the ADB daemon
  73. killall adbd
  74. # Bring down the local network
  75. ifdown lo
  76. # Unmount the ADB function filesystem
  77. umount /dev/usb-ffs/adb
  78. # Delete the function filesystem
  79. rmdir /dev/usb-ffs/adb
  80. rmdir /dev/usb-ffs
  81. # Remove functions from configurations
  82. rm ${GADGET}/configs/FunKey.1/acm.GS0
  83. rm ${GADGET}/configs/FunKey.1/ffs.adb
  84. # Remove string directories in configurations
  85. rmdir ${GADGET}/configs/FunKey.1/strings/0x409
  86. # Remove configurations
  87. rmdir ${GADGET}/configs/FunKey.1
  88. # Remove functions
  89. rmdir ${GADGET}/functions/acm.GS0
  90. rmdir ${GADGET}/functions/ffs.adb
  91. # Remove strings
  92. rmdir ${GADGET}/strings/0x409
  93. # Finallyy remove the gadget
  94. rmdir ${GADGET}
  95. # Unload the kernel modules
  96. modprobe -r usb_f_serial usb_f_fs usb_f_acm
  97. }
  98. case "$1" in
  99. start)
  100. deinit_usb_gadget
  101. init_adb
  102. ;;
  103. stop)
  104. deinit_adb
  105. init_usb_gadget
  106. ;;
  107. *)
  108. die 15 "Usage $0 {start|stop}"
  109. ;;
  110. esac
  111. exit $?