Advertisement
Guest User

USB Gadget RNDIS/ECM + 2xACM

a guest
Jun 4th, 2016
3,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.66 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # References:
  4. #   http://answers.microsoft.com/en-us/windows/forum/windows_10-networking/
  5. #       windows-10-vs-remote-ndis-ethernet-usbgadget-not/cb30520a-753c-4219-b908-ad3d45590447
  6. #
  7.  
  8. modprobe libcomposite
  9. mount -t configfs none /sys/kernel/config
  10.  
  11. set -e
  12.  
  13. g=/sys/kernel/config/usb_gadget/device
  14. # Device name can be retrieved from /sys/class/udc/
  15. device="ci_hdrc.0"
  16.  
  17. usb_ver="0x0200" # USB 2.0
  18. dev_class="2"
  19. vid="0x1d50"
  20. pid="0x60c7"
  21. mfg="Mfg"
  22. prod="Prod"
  23. serial="0123456789"
  24. attr="0x80" # Bus powered
  25. pwr="500" # mA
  26. cfg1="RNDIS"
  27. cfg2="CDC"
  28. mac="01:23:45:67:89:ab"
  29. # Change the first number for each MAC address - the second digit of 2 indicates
  30. # that these are "locally assigned (b2=1), unicast (b1=0)" addresses. This is
  31. # so that they don't conflict with any existing vendors. Care should be taken
  32. # not to change these two bits.
  33. dev_mac="02$(echo ${mac} | cut -b 3-)"
  34. host_mac="12$(echo ${mac} | cut -b 3-)"
  35. ms_vendor_code="0xcd" # Microsoft
  36. ms_qw_sign="MSFT100" # also Microsoft (if you couldn't tell)
  37. ms_compat_id="RNDIS" # matches Windows RNDIS Drivers
  38. ms_subcompat_id="5162001" # matches Windows RNDIS 6.0 Driver
  39.  
  40. # Create a new gadget
  41.  
  42. mkdir -p ${g}
  43. echo "${usb_ver}" > ${g}/bcdUSB
  44. echo "${dev_class}" > ${g}/bDeviceClass
  45. echo "${vid}" > ${g}/idVendor
  46. echo "${pid}" > ${g}/idProduct
  47. mkdir ${g}/strings/0x409
  48. echo "${mfg}" > ${g}/strings/0x409/manufacturer
  49. echo "${prod}" > ${g}/strings/0x409/product
  50. echo "${serial}" > ${g}/strings/0x409/serialnumber
  51.  
  52. # Create 2 configurations. The first will be RNDIS, which is required by
  53. # Windows to be first. The second will be CDC. Linux and Mac are smart
  54. # enough to ignore RNDIS and load the CDC configuration.
  55.  
  56. # config 1 is for RNDIS
  57.  
  58. mkdir ${g}/configs/c.1
  59. echo "${attr}" > ${g}/configs/c.1/bmAttributes
  60. echo "${pwr}" > ${g}/configs/c.1/MaxPower
  61. mkdir ${g}/configs/c.1/strings/0x409
  62. echo "${cfg1}" > ${g}/configs/c.1/strings/0x409/configuration
  63.  
  64. # On Windows 7 and later, the RNDIS 5.1 driver would be used by default,
  65. # but it does not work very well. The RNDIS 6.0 driver works better. In
  66. # order to get this driver to load automatically, we have to use a
  67. # Microsoft-specific extension of USB.
  68.  
  69. echo "1" > ${g}/os_desc/use
  70. echo "${ms_vendor_code}" > ${g}/os_desc/b_vendor_code
  71. echo "${ms_qw_sign}" > ${g}/os_desc/qw_sign
  72.  
  73. # Create the RNDIS function, including the Microsoft-specific bits
  74.  
  75. mkdir ${g}/functions/rndis.usb0
  76. echo "${dev_mac}" > ${g}/functions/rndis.usb0/dev_addr
  77. echo "${host_mac}" > ${g}/functions/rndis.usb0/host_addr
  78. echo "${ms_compat_id}" > ${g}/functions/rndis.usb0/os_desc/interface.rndis/compatible_id
  79. echo "${ms_subcompat_id}" > ${g}/functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id
  80.  
  81. # config 2 is for CDC
  82.  
  83. mkdir ${g}/configs/c.2
  84. echo "${attr}" > ${g}/configs/c.2/bmAttributes
  85. echo "${pwr}" > ${g}/configs/c.2/MaxPower
  86. mkdir ${g}/configs/c.2/strings/0x409
  87. echo "${cfg2}" > ${g}/configs/c.2/strings/0x409/configuration
  88.  
  89. # Create the CDC ECM function
  90.  
  91. mkdir ${g}/functions/ecm.usb0
  92. echo "${dev_mac}" > ${g}/functions/ecm.usb0/dev_addr
  93. echo "${host_mac}" > ${g}/functions/ecm.usb0/host_addr
  94.  
  95. # Create the CDC ACM functions (common for both configurations)
  96.  
  97. mkdir ${g}/functions/acm.GS0
  98. mkdir ${g}/functions/acm.GS1
  99.  
  100. # Link everything up and bind the USB device
  101.  
  102. ln -s ${g}/configs/c.1          ${g}/os_desc
  103.  
  104. ln -s ${g}/functions/rndis.usb0 ${g}/configs/c.1
  105. ln -s ${g}/functions/acm.GS0    ${g}/configs/c.1
  106. ln -s ${g}/functions/acm.GS1    ${g}/configs/c.1
  107.  
  108. ln -s ${g}/functions/ecm.usb0   ${g}/configs/c.2
  109. ln -s ${g}/functions/acm.GS0    ${g}/configs/c.2
  110. ln -s ${g}/functions/acm.GS1    ${g}/configs/c.2
  111.  
  112. echo "${device}" > ${g}/UDC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement