hd-usb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. # dependencies:
  3. # mount grep readlink hdparm cp
  4. # These _must_ be defined outside the script
  5. [ -z "$node" ] && echo "You must export \$node variable!" && exit 1
  6. [ -z "$hdparm_device" ] && echo "You must export \$hdparm_device variable!" && exit 1
  7. basepath="/sys/bus/usb"
  8. bind_file="$basepath/drivers/usb/bind"
  9. unbind_file="$basepath/drivers/usb/unbind"
  10. autosuspend_file="$basepath/devices/$node/power/autosuspend_delay_ms"
  11. control_file="$basepath/devices/$node/power/control"
  12. restore_autosuspend="${restore_autosuspend:-2000}" # default value
  13. restore_control="${restore_control:-on}" # default value
  14. case "$1" in
  15. off)
  16. # Must not be mounted
  17. if [ -r "$hdparm_device" ]; then
  18. mount | grep "^$(readlink -f "$hdparm_device")" && echo "Must not be mounted!" && exit 1
  19. # First, send it to sleep.
  20. /usr/bin/hdparm -Y "$hdparm_device" || exit 1 # systemd unit will retry on fail
  21. fi
  22. # echoing states
  23. echo "Before:"
  24. printf "%s: %s\n" "$autosuspend_file" "$(cat "$autosuspend_file")"
  25. printf "%s: %s\n" "$control_file" "$(cat "$control_file")"
  26. # stackoverflow.com/a/12675749
  27. echo "0" > "$autosuspend_file" || exit 1
  28. echo "auto" > "$control_file" || exit 1
  29. echo "After:"
  30. printf "%s: %s\n" "$autosuspend_file" "$(cat "$autosuspend_file")"
  31. printf "%s: %s\n" "$control_file" "$(cat "$control_file")"
  32. # stackoverflow.com/a/18098075
  33. echo "$node" > "$unbind_file" && echo "Unbinding $node succeeded." || echo "Unbinding $node failed, but that's not necessarily a problem. Please investigate."
  34. exit 0
  35. ;;
  36. on)
  37. # Waking it up in reverse order
  38. echo "$node" > "$bind_file" && echo "Binding $node succeeded" || echo "Binding $node failed, but that's not necessarily a problem. Please investigate."
  39. # echoing states
  40. echo "Before:"
  41. printf "%s: %s\n" "$autosuspend_file" "$(cat "$autosuspend_file")"
  42. printf "%s: %s\n" "$control_file" "$(cat "$control_file")"
  43. # Action
  44. echo "$restore_autosuspend" > "$autosuspend_file" || exit 1
  45. echo "$restore_control" > "$control_file" || exit 1
  46. ###############################################
  47. echo "After:"
  48. printf "%s: %s\n" "$autosuspend_file" "$(cat "$autosuspend_file")"
  49. printf "%s: %s\n" "$control_file" "$(cat "$control_file")"
  50. # There's no need to wake the device up with hdparm; it will do so when accessed, albeit with a delay.
  51. exit 0
  52. ;;
  53. *)
  54. echo "Wrong argument $1"; exit 1
  55. ;;
  56. esac >&2