One way is with a couple op amps and a mosfet, e.g.:
Or run in simulator:
https://tinyurl.com/cngchkd (I have a charging capacitor for load, reset sim to restart, center scope is capacitor current).
In that circuit:
- R5 senses the current; choose a small value to reduce voltage drop. Then:
\[{I}_{load}=\frac{{V}_{R5}}{R5}\].
- Top op amp senses voltage drop across R5, optionally amplifying it to a reasonable value. R1=R3 and R2=R4 and gain = R1/R2 therefore, where Voa1 = voltage output from top op amp:
\[{V}_{oa1}={V}_{R5}*\frac{{R1}}{{R2}}={I}_{load}*{{R5}}*\frac{{R1}}{{R2}}\]
- Bottom op amp provides feedback and controls mosfet to try and keep Vctl = Voa1. You pick Vctl by plugging current limit and resistor values into above equation. So e.g. above circuit with a 50mA limit:
\[{V}_{ctl}={I}_{max}*{{R5}}*\frac{{R1}}{{R2}}={50mA}*{0.1\Omega}*\frac{{100k\Omega}}{{1k\Omega}}={0.5V}\]
So a Vctl of 0.5V gives you a 50mA current limit.
Remember op amp outputs will be clamped to some distance from the rails and have some input and output offset and also there is voltage drop across R5 and the mosfet so mind your component specs and mosfet threshold and resistance.
Note the +5V at the top supplies the load and can be from anywhere if USB can't power the motors, as long as a common reference point is used.
The purpose of the differential amp is to let us keep sense resistor on + side so we don't shift the load's 0V away from system ground, but if you don't care about that you can put the sense resistor between the load and ground, lose the differential amp, and have the bottom op amp sense voltage drop (and possibly amplify) and control mosfet.
There are other ways, that's the way I'm most familiar with though. Hope that helps.
- - - Updated - - -
P.S. You can only pull 100mA from a USB port unless your device specifically requests high power mode (500mA). Those current limits are total per internal hub not necessarily per port. You can pull more current if you use a powered hub though, possibly enough to drive your motors without worrying about it.