Advertisement
zefie

pi0 gadget_setup (otg part 2)

Sep 5th, 2017
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.77 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. SERIAL=$(grep Serial /proc/cpuinfo | sed 's/Serial\s*: 0000\(\w*\)/\1/')
  4. BASEMAC="$(echo $(echo ${SERIAL} | cut -b 3-) | sed 's/\(\w\w\)/:\1/g' | cut -b 2-)"
  5. DEV_MAC="02:${BASEMAC}"
  6. HOST_MAC="12:${BASEMAC}"
  7. DIAG_IP="192.168.99.1/30" # Default
  8. MSFILE=''
  9.  
  10. if [ $(grep "^zefie_diag_ip=" /boot/config.txt | wc -l) -eq 1 ]; then
  11.         DIAG_IP="$(grep "^zefie_diag_ip=" /boot/config.txt | cut -d'=' -f2)"
  12. fi
  13.  
  14. if [ $(grep "^zefie_msd_file=" /boot/config.txt | wc -l) -eq 1 ]; then
  15.         MSFILE="$(grep "^zefie_msd_file=" /boot/config.txt | cut -d'=' -f2)"
  16.         if [ ! -f "${MSFILE}" ]; then
  17.                 # Unset since file doesn't exist
  18.                 MSFILE='';
  19.         fi
  20. fi
  21.  
  22. if [ $(grep "^zefie_enable_configfs=1" /boot/config.txt | wc -l) -eq 1 ]; then
  23.         # Use configfs
  24.         modprobe libcomposite
  25.  
  26.         CONFIGFS="/sys/kernel/config/usb_gadget"
  27.         G="${CONFIGFS}/gadget"
  28.  
  29.         mkdir ${G}
  30.         mkdir -p ${G}/strings/0x409
  31.         echo 0x1d6b > ${G}/idVendor  # Linux Foundation
  32.         echo 0x0104 > ${G}/idProduct # Multifunction Composite Gadget
  33.         echo 0x0201 > ${G}/bcdDevice # v2.0.1
  34.         echo 0x0200 > ${G}/bcdUSB    # USB 2.0
  35.         echo "${SERIAL}" > ${G}/strings/0x409/serialnumber
  36.         echo "Zefie Networks" > ${G}/strings/0x409/manufacturer
  37.         echo "Pi Zero Gadget" > ${G}/strings/0x409/product
  38.  
  39.         # Main Config
  40.         mkdir -p ${G}/configs/c.1
  41.         echo 1 > ${G}/configs/c.1/MaxPower
  42.         mkdir -p ${G}/configs/c.1/strings/0x409
  43.         echo "Config 1" > ${G}/configs/c.1/strings/0x409/configuration
  44.  
  45.         # Serial 1 (tty)
  46.         mkdir -p ${G}/functions/acm.0
  47.         ln -s ${G}/functions/acm.0 ${G}/configs/c.1
  48.  
  49.         if [ $(grep "^zefie_second_serial=1" /boot/config.txt | wc -l) -eq 1 ]; then
  50.                 # Serial 2
  51.                 mkdir -p ${G}/functions/acm.1
  52.                 ln -s ${G}/functions/acm.1 ${G}/configs/c.1
  53.         fi
  54.  
  55.         # RNDIS Ether
  56.         mkdir -p ${G}/functions/rndis.0
  57.         echo "${DEV_MAC}" > ${G}/functions/rndis.0/dev_addr
  58.         echo "${HOST_MAC}" > ${G}/functions/rndis.0/host_addr
  59.         ln -s ${G}/functions/rndis.0 ${G}/configs/c.1
  60.  
  61.         # Mass Storage
  62.         if [ ! -z "${MSFILE}" ]; then
  63.                 mkdir -p ${G}/functions/mass_storage.0
  64.                 echo 0 > ${G}/functions/mass_storage.0/stall
  65.                 mkdir -p ${G}/functions/mass_storage.0/lun.0
  66.                 echo 1 > ${G}/functions/mass_storage.0/lun.0/removable
  67.                 if [ $(grep "^zefie_msd_cdrom=1" /boot/config.txt | wc -l) -eq 1 ]; then
  68.                         echo 1 > ${G}/functions/mass_storage.0/lun.0/cdrom
  69.                         echo 1 > ${G}/functions/mass_storage.0/lun.0/ro
  70.                 else
  71.                         echo 0 > ${G}/functions/mass_storage.0/lun.0/cdrom
  72.                         echo 0 > ${G}/functions/mass_storage.0/lun.0/ro
  73.                 fi
  74.                 echo 0 > ${G}/functions/mass_storage.0/lun.0/nofua
  75.                 echo ${MSFILE} > ${G}/functions/mass_storage.0/lun.0/file
  76.                 ln -s ${G}/functions/mass_storage.0 ${G}/configs/c.1
  77.         fi
  78.  
  79.         # Windows Compatibility
  80.         echo 1 > ${G}/os_desc/use
  81.         echo "0xcd" > ${G}/os_desc/b_vendor_code
  82.         echo "MSFT100" > ${G}/os_desc/qw_sign
  83.  
  84.         echo "RNDIS" > ${G}/functions/rndis.0/os_desc/interface.rndis/compatible_id
  85.         echo 5162001 > ${G}/functions/rndis.0/os_desc/interface.rndis/sub_compatible_id
  86.  
  87.         ln -s ${G}/configs/c.1 ${G}/os_desc
  88.  
  89.         # Enable it all
  90.         echo 20980000.usb > ${G}/UDC
  91. else
  92.         # Use g_ether
  93.         modprobe g_ether host_addr=${HOST_MAC} dev_addr=${DEV_MAC}
  94. fi
  95.  
  96. # We have ethernet regardless, so we set this.
  97. ip addr add ${DIAG_IP} dev usb0
  98. ip link set usb0 up
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement